summaryrefslogtreecommitdiffstats
path: root/browser/components/customizableui/test/browser_panel_keyboard_navigation.js
blob: 9b5ad48cce650ed2523328960c156a58fb84db54 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use strict";

/**
 * Test keyboard navigation in the app menu panel.
 */

const kHelpButtonId = "appMenu-help-button2";

function getEnabledNavigableElementsForView(panelView) {
  return Array.from(
    panelView.querySelectorAll("button,toolbarbutton,menulist,.text-link")
  ).filter(element => {
    let bounds = element.getBoundingClientRect();
    return !element.disabled && bounds.width > 0 && bounds.height > 0;
  });
}

add_task(async function testUpDownKeys() {
  await gCUITestUtils.openMainMenu();

  let buttons = getEnabledNavigableElementsForView(PanelUI.mainView);

  for (let button of buttons) {
    if (button.disabled) {
      continue;
    }
    EventUtils.synthesizeKey("KEY_ArrowDown");
    Assert.equal(
      document.commandDispatcher.focusedElement,
      button,
      "The correct button should be focused after navigating downward"
    );
  }

  EventUtils.synthesizeKey("KEY_ArrowDown");
  Assert.equal(
    document.commandDispatcher.focusedElement,
    buttons[0],
    "Pressing upwards should cycle around and select the first button again"
  );

  for (let i = buttons.length - 1; i >= 0; --i) {
    let button = buttons[i];
    if (button.disabled) {
      continue;
    }
    EventUtils.synthesizeKey("KEY_ArrowUp");
    Assert.equal(
      document.commandDispatcher.focusedElement,
      button,
      "The first button should be focused after navigating upward"
    );
  }

  await gCUITestUtils.hideMainMenu();
});

add_task(async function testHomeEndKeys() {
  await gCUITestUtils.openMainMenu();

  let buttons = getEnabledNavigableElementsForView(PanelUI.mainView);
  let enabledButtons = buttons.filter(btn => !btn.disabled);
  let firstButton = enabledButtons[0];
  let lastButton = enabledButtons.pop();

  Assert.ok(firstButton != lastButton, "There is more than one button");

  EventUtils.synthesizeKey("KEY_End");
  Assert.equal(
    document.commandDispatcher.focusedElement,
    lastButton,
    "The last button should be focused after pressing End"
  );

  EventUtils.synthesizeKey("KEY_Home");
  Assert.equal(
    document.commandDispatcher.focusedElement,
    firstButton,
    "The first button should be focused after pressing Home"
  );

  await gCUITestUtils.hideMainMenu();
});

add_task(async function testEnterKeyBehaviors() {
  await gCUITestUtils.openMainMenu();

  let buttons = getEnabledNavigableElementsForView(PanelUI.mainView);

  // Navigate to the 'Help' button, which points to a subview.
  EventUtils.synthesizeKey("KEY_ArrowUp");
  let focusedElement = document.commandDispatcher.focusedElement;
  Assert.equal(
    focusedElement,
    buttons[buttons.length - 1],
    "The last button should be focused after navigating upward"
  );

  // Make sure the Help button is in focus.
  while (
    !focusedElement ||
    !focusedElement.id ||
    focusedElement.id != kHelpButtonId
  ) {
    EventUtils.synthesizeKey("KEY_ArrowUp");
    focusedElement = document.commandDispatcher.focusedElement;
  }
  EventUtils.synthesizeKey("KEY_Enter");

  let helpView = document.getElementById("PanelUI-helpView");
  await BrowserTestUtils.waitForEvent(helpView, "ViewShown");

  let helpButtons = getEnabledNavigableElementsForView(helpView);
  Assert.ok(
    helpButtons[0].classList.contains("subviewbutton-back"),
    "First button in help view should be a back button"
  );

  // For posterity, check navigating the subview using up/ down arrow keys as well.
  // When opening a subview, the first control *after* the Back button gets
  // focus.
  EventUtils.synthesizeKey("KEY_ArrowUp");
  focusedElement = document.commandDispatcher.focusedElement;
  Assert.equal(
    focusedElement,
    helpButtons[0],
    "The Back button should be focused after navigating upward"
  );
  for (let i = helpButtons.length - 1; i >= 0; --i) {
    let button = helpButtons[i];
    if (button.disabled) {
      continue;
    }
    EventUtils.synthesizeKey("KEY_ArrowUp");
    focusedElement = document.commandDispatcher.focusedElement;
    Assert.equal(
      focusedElement,
      button,
      "The previous button should be focused after navigating upward"
    );
  }

  // Make sure the back button is in focus again.
  while (focusedElement != helpButtons[0]) {
    EventUtils.synthesizeKey("KEY_ArrowDown");
    focusedElement = document.commandDispatcher.focusedElement;
  }

  // The first button is the back button. Hittin Enter should navigate us back.
  let promise = BrowserTestUtils.waitForEvent(PanelUI.mainView, "ViewShown");
  EventUtils.synthesizeKey("KEY_Enter");
  await promise;

  // Let's test a 'normal' command button.
  focusedElement = document.commandDispatcher.focusedElement;
  const kFindButtonId = "appMenu-find-button2";
  while (
    !focusedElement ||
    !focusedElement.id ||
    focusedElement.id != kFindButtonId
  ) {
    EventUtils.synthesizeKey("KEY_ArrowUp");
    focusedElement = document.commandDispatcher.focusedElement;
  }
  let findBarPromise = gBrowser.isFindBarInitialized()
    ? null
    : BrowserTestUtils.waitForEvent(gBrowser.selectedTab, "TabFindInitialized");
  Assert.equal(
    focusedElement.id,
    kFindButtonId,
    "Find button should be selected"
  );

  await gCUITestUtils.hidePanelMultiView(PanelUI.panel, () =>
    EventUtils.synthesizeKey("KEY_Enter")
  );

  await findBarPromise;
  Assert.ok(!gFindBar.hidden, "Findbar should have opened");
  gFindBar.close();
});

add_task(async function testLeftRightKeys() {
  await gCUITestUtils.openMainMenu();

  // Navigate to the 'Help' button, which points to a subview.
  let focusedElement = document.commandDispatcher.focusedElement;
  while (
    !focusedElement ||
    !focusedElement.id ||
    focusedElement.id != kHelpButtonId
  ) {
    EventUtils.synthesizeKey("KEY_ArrowUp");
    focusedElement = document.commandDispatcher.focusedElement;
  }
  Assert.equal(
    focusedElement.id,
    kHelpButtonId,
    "The last button should be focused after navigating upward"
  );

  // Hitting ArrowRight on a button that points to a subview should navigate us
  // there.
  EventUtils.synthesizeKey("KEY_ArrowRight");
  let helpView = document.getElementById("PanelUI-helpView");
  await BrowserTestUtils.waitForEvent(helpView, "ViewShown");

  // Hitting ArrowLeft should navigate us back.
  let promise = BrowserTestUtils.waitForEvent(PanelUI.mainView, "ViewShown");
  EventUtils.synthesizeKey("KEY_ArrowLeft");
  await promise;

  focusedElement = document.commandDispatcher.focusedElement;
  Assert.equal(
    focusedElement.id,
    kHelpButtonId,
    "Help button should be focused again now that we're back in the main view"
  );

  await gCUITestUtils.hideMainMenu();
});

add_task(async function testTabKey() {
  await gCUITestUtils.openMainMenu();

  let buttons = getEnabledNavigableElementsForView(PanelUI.mainView);

  for (let button of buttons) {
    if (button.disabled) {
      continue;
    }
    EventUtils.synthesizeKey("KEY_Tab");
    Assert.equal(
      document.commandDispatcher.focusedElement,
      button,
      "The correct button should be focused after tabbing"
    );
  }

  EventUtils.synthesizeKey("KEY_Tab");
  Assert.equal(
    document.commandDispatcher.focusedElement,
    buttons[0],
    "Pressing tab should cycle around and select the first button again"
  );

  for (let i = buttons.length - 1; i >= 0; --i) {
    let button = buttons[i];
    if (button.disabled) {
      continue;
    }
    EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
    Assert.equal(
      document.commandDispatcher.focusedElement,
      button,
      "The correct button should be focused after shift + tabbing"
    );
  }

  EventUtils.synthesizeKey("KEY_Tab", { shiftKey: true });
  Assert.equal(
    document.commandDispatcher.focusedElement,
    buttons[buttons.length - 1],
    "Pressing shift + tab should cycle around and select the last button again"
  );

  await gCUITestUtils.hideMainMenu();
});

add_task(async function testInterleavedTabAndArrowKeys() {
  await gCUITestUtils.openMainMenu();

  let buttons = getEnabledNavigableElementsForView(PanelUI.mainView);
  let tab = false;

  for (let button of buttons) {
    if (button.disabled) {
      continue;
    }
    if (tab) {
      EventUtils.synthesizeKey("KEY_Tab");
    } else {
      EventUtils.synthesizeKey("KEY_ArrowDown");
    }
    tab = !tab;
  }

  Assert.equal(
    document.commandDispatcher.focusedElement,
    buttons[buttons.length - 1],
    "The last button should be focused after a mix of Tab and ArrowDown"
  );

  await gCUITestUtils.hideMainMenu();
});

add_task(async function testSpaceDownAfterTabNavigation() {
  await gCUITestUtils.openMainMenu();

  let buttons = getEnabledNavigableElementsForView(PanelUI.mainView);
  let button;

  for (button of buttons) {
    if (button.disabled) {
      continue;
    }
    EventUtils.synthesizeKey("KEY_Tab");
    if (button.id == kHelpButtonId) {
      break;
    }
  }

  Assert.equal(
    document.commandDispatcher.focusedElement,
    button,
    "Help button should be focused after tabbing to it."
  );

  // Pressing down space on a button that points to a subview should navigate us
  // there, before keyup.
  EventUtils.synthesizeKey(" ", { type: "keydown" });
  let helpView = document.getElementById("PanelUI-helpView");
  await BrowserTestUtils.waitForEvent(helpView, "ViewShown");

  await gCUITestUtils.hideMainMenu();
});