summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/tests/browser/browser_recently_closed_tabs_keyboard.js
blob: 8d82db2b9332d937b7545f6fba0f5fe61b7df065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

async function dismiss_tab_keyboard(closedTab, document) {
  const enter = () => {
    info("Enter");
    EventUtils.synthesizeKey("KEY_Enter");
  };
  const tab = (shiftKey = false) => {
    info(`${shiftKey ? "Shift + Tab" : "Tab"}`);
    EventUtils.synthesizeKey("KEY_Tab", { shiftKey });
  };
  const closedObjectsChanged = () =>
    TestUtils.topicObserved("sessionstore-closed-objects-changed");
  let firstTabMainContent = closedTab.querySelector(".closed-tab-li-main");
  let dismissButton = closedTab.querySelector(".closed-tab-li-dismiss");
  firstTabMainContent.focus();
  tab();
  Assert.equal(
    document.activeElement,
    dismissButton,
    "Focus should be on the dismiss button for the first item in the recently closed list"
  );
  enter();
  await closedObjectsChanged();
}

/**
 * Tests keyboard navigation of the recently closed tabs component
 */
add_task(async function test_keyboard_navigation() {
  const enter = () => {
    info("Enter");
    EventUtils.synthesizeKey("KEY_Enter");
  };
  const tab = (shiftKey = false) => {
    info(`${shiftKey ? "Shift + Tab" : "Tab"}`);
    EventUtils.synthesizeKey("KEY_Tab", { shiftKey });
  };
  /**
   * Focus the summary element and asserts that:
   * - The recently closed details should be initially opened
   * - The recently closed details can be opened and closed via the Enter key
   *
   * @param {Document} document The currently used browser's content window document
   * @param {HTMLElement} summary The header section element for recently closed tabs
   */
  const assertPreconditions = (document, summary) => {
    let details = document.getElementById("recently-closed-tabs-container");
    ok(
      details.open,
      "Recently closed details should be initially open on load"
    );
    summary.focus();
    enter();
    ok(!details.open, "Recently closed details should be closed");
    enter();
    ok(details.open, "Recently closed details should be opened");
  };
  await SpecialPowers.clearUserPref(RECENTLY_CLOSED_STATE_PREF);
  Services.obs.notifyObservers(null, "browser:purge-session-history");
  is(
    SessionStore.getClosedTabCountForWindow(window),
    0,
    "Closed tab count after purging session history"
  );

  const sandbox = sinon.createSandbox();
  let setupCompleteStub = sandbox.stub(
    TabsSetupFlowManager,
    "isTabSyncSetupComplete"
  );
  setupCompleteStub.returns(true);
  await open_then_close(URLs[0]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
    let summary = document.getElementById(
      "recently-closed-tabs-header-section"
    );

    assertPreconditions(document, summary);

    tab();

    Assert.equal(
      list[0].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The first link is focused"
    );

    tab(true);
    Assert.equal(
      summary,
      document.activeElement,
      "The container is focused when using shift+tab in the list"
    );
  });
  // clean up extra tabs
  while (gBrowser.tabs.length > 1) {
    BrowserTestUtils.removeTab(gBrowser.tabs.at(-1));
  }

  clearHistory();

  await open_then_close(URLs[0]);
  await open_then_close(URLs[1]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
    let summary = document.getElementById(
      "recently-closed-tabs-header-section"
    );
    assertPreconditions(document, summary);

    tab();

    Assert.equal(
      list[0].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The first link is focused"
    );
    tab();
    tab();
    Assert.equal(
      list[1].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The second link is focused"
    );
    tab(true);
    tab(true);
    Assert.equal(
      list[0].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The first link is focused again"
    );

    tab(true);
    Assert.equal(
      summary,
      document.activeElement,
      "The container is focused when using shift+tab in the list"
    );
  });

  // clean up extra tabs
  while (gBrowser.tabs.length > 1) {
    BrowserTestUtils.removeTab(gBrowser.tabs.at(-1));
  }

  clearHistory();

  await open_then_close(URLs[0]);
  await open_then_close(URLs[1]);
  await open_then_close(URLs[2]);

  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;
    const list = document.querySelectorAll(".closed-tab-li");
    let summary = document.getElementById(
      "recently-closed-tabs-header-section"
    );
    assertPreconditions(document, summary);

    tab();

    Assert.equal(
      list[0].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The first link is focused"
    );
    tab();
    tab();
    Assert.equal(
      list[1].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The second link is focused"
    );
    tab();
    tab();
    Assert.equal(
      list[2].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The third link is focused"
    );
    tab(true);
    tab(true);
    Assert.equal(
      list[1].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The second link is focused"
    );
    tab(true);
    tab(true);
    Assert.equal(
      list[0].querySelector(".closed-tab-li-main"),
      document.activeElement,
      "The first link is focused"
    );
  });
});

add_task(async function test_dismiss_tab_keyboard() {
  Services.obs.notifyObservers(null, "browser:purge-session-history");
  Assert.equal(
    SessionStore.getClosedTabCountForWindow(window),
    0,
    "Closed tab count after purging session history"
  );
  await withFirefoxView({}, async browser => {
    const { document } = browser.contentWindow;

    await open_then_close(URLs[0]);
    await open_then_close(URLs[1]);
    await open_then_close(URLs[2]);

    await EventUtils.synthesizeMouseAtCenter(
      gBrowser.ownerDocument.getElementById("firefox-view-button"),
      { type: "mousedown" },
      window
    );

    const tabsList = document.querySelector("ol.closed-tabs-list");

    await dismiss_tab_keyboard(tabsList.children[0], document);

    Assert.equal(
      tabsList.children[0].dataset.targeturi,
      URLs[1],
      `First recently closed item should be ${URLs[1]}`
    );

    Assert.equal(
      tabsList.children.length,
      2,
      "recently-closed-tabs-list should have two list items"
    );

    await dismiss_tab_keyboard(tabsList.children[0], document);

    Assert.equal(
      tabsList.children[0].dataset.targeturi,
      URLs[0],
      `First recently closed item should be ${URLs[0]}`
    );

    Assert.equal(
      tabsList.children.length,
      1,
      "recently-closed-tabs-list should have one list item"
    );

    await dismiss_tab_keyboard(tabsList.children[0], document);

    Assert.ok(
      document.getElementById("recently-closed-tabs-placeholder"),
      "The empty message is displayed."
    );

    Assert.ok(
      !document.querySelector("ol.closed-tabs-list"),
      "The recently clsoed tabs list is not displayed."
    );
  });
});