summaryrefslogtreecommitdiffstats
path: root/browser/components/downloads/test/browser/browser_downloads_keynav.js
blob: 23acf2041785bf0e9b4dd61548cbecc0f3b96979 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

registerCleanupFunction(async function () {
  await task_resetState();
});

function changeSelection(listbox, down) {
  let selectPromise = BrowserTestUtils.waitForEvent(listbox, "select");
  EventUtils.synthesizeKey(down ? "VK_DOWN" : "VK_UP", {});
  return selectPromise;
}

add_task(async function test_downloads_keynav() {
  // Ensure that state is reset in case previous tests didn't finish.
  await task_resetState();

  await SpecialPowers.pushPrefEnv({ set: [["accessibility.tabfocus", 7]] });

  // Move the mouse pointer out of the way first so it doesn't
  // interfere with the selection.
  let listbox = document.getElementById("downloadsListBox");
  EventUtils.synthesizeMouse(listbox, -5, -5, { type: "mousemove" });

  let downloads = [];
  for (let i = 0; i < 2; i++) {
    downloads.push({ state: DownloadsCommon.DOWNLOAD_FINISHED });
  }
  downloads.push({ state: DownloadsCommon.DOWNLOAD_FAILED });
  downloads.push({ state: DownloadsCommon.DOWNLOAD_BLOCKED });

  await task_addDownloads(downloads);
  await task_openPanel();

  is(document.activeElement, listbox, "downloads list is focused");
  is(listbox.selectedIndex, 0, "downloads list selected index starts at 0");

  let footer = document.getElementById("downloadsHistory");

  await changeSelection(listbox, true);
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after down to index 1"
  );
  is(
    listbox.selectedIndex,
    1,
    "downloads list selected index after down is pressed"
  );

  checkTabbing(listbox, 1);

  await changeSelection(listbox, true);
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after down to index 2"
  );
  is(
    listbox.selectedIndex,
    2,
    "downloads list selected index after down to index 2"
  );

  checkTabbing(listbox, 2);

  await changeSelection(listbox, true);
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after down to index 3"
  );
  is(
    listbox.selectedIndex,
    3,
    "downloads list selected index after down to index 3"
  );

  checkTabbing(listbox, 3);

  await changeSelection(listbox, true);
  is(document.activeElement, footer, "footer is focused");
  is(
    listbox.selectedIndex,
    -1,
    "downloads list selected index after down to footer"
  );

  EventUtils.synthesizeKey("VK_TAB", {});
  is(
    document.activeElement,
    listbox,
    "downloads list should be focused after tab when footer is focused"
  );
  is(
    listbox.selectedIndex,
    0,
    "downloads list should be focused after tab when footer is focused selected index"
  );

  // Move back to the footer.
  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true });
  is(document.activeElement, footer, "downloads footer is focused again");
  is(
    listbox.selectedIndex,
    0,
    "downloads footer is focused again selected index"
  );

  EventUtils.synthesizeKey("VK_DOWN", {});
  is(
    document.activeElement,
    footer,
    "downloads footer is still focused after down past footer"
  );
  is(
    listbox.selectedIndex,
    -1,
    "downloads footer is still focused selected index after down past footer"
  );

  await changeSelection(listbox, false);
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after up to index 3"
  );
  is(
    listbox.selectedIndex,
    3,
    "downloads list selected index after up to index 3"
  );

  await changeSelection(listbox, false);
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after up to index 2"
  );
  is(
    listbox.selectedIndex,
    2,
    "downloads list selected index after up to index 2"
  );

  EventUtils.synthesizeMouseAtCenter(listbox.getItemAtIndex(0), {
    type: "mousemove",
  });
  EventUtils.synthesizeMouseAtCenter(listbox.getItemAtIndex(1), {
    type: "mousemove",
  });
  is(listbox.selectedIndex, 0, "downloads list selected index after mousemove");

  checkTabbing(listbox, 0);

  EventUtils.synthesizeKey("VK_UP", {});
  is(
    document.activeElement,
    listbox,
    "downloads list is still focused after up past start"
  );
  is(
    listbox.selectedIndex,
    0,
    "downloads list is still focused after up past start selected index"
  );

  // Move the mouse pointer out of the way again so we don't
  // hover over an item unintentionally if this test is run in verify mode.
  EventUtils.synthesizeMouse(listbox, -5, -5, { type: "mousemove" });

  await task_resetState();
});

async function checkTabbing(listbox, buttonIndex) {
  let button = listbox.getItemAtIndex(buttonIndex).querySelector("button");
  let footer = document.getElementById("downloadsHistory");

  listbox.clientWidth; // flush layout first

  EventUtils.synthesizeKey("VK_TAB", {});
  is(
    document.activeElement,
    button,
    "downloads button is focused after tab is pressed"
  );
  is(
    listbox.selectedIndex,
    buttonIndex,
    "downloads button selected index after tab is pressed"
  );

  EventUtils.synthesizeKey("VK_TAB", {});
  is(
    document.activeElement,
    footer,
    "downloads footer is focused after tab is pressed again"
  );
  is(
    listbox.selectedIndex,
    buttonIndex,
    "downloads footer selected index after tab is pressed again"
  );

  EventUtils.synthesizeKey("VK_TAB", {});
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after tab is pressed yet again"
  );
  is(
    listbox.selectedIndex,
    buttonIndex,
    "downloads list selected index after tab is pressed yet again"
  );

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true });
  is(
    document.activeElement,
    footer,
    "downloads footer is focused after shift+tab is pressed"
  );
  is(
    listbox.selectedIndex,
    buttonIndex,
    "downloads footer selected index after shift+tab is pressed"
  );

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true });
  is(
    document.activeElement,
    button,
    "downloads button is focused after shift+tab is pressed again"
  );
  is(
    listbox.selectedIndex,
    buttonIndex,
    "downloads button selected index after shift+tab is pressed again"
  );

  EventUtils.synthesizeKey("VK_TAB", { shiftKey: true });
  is(
    document.activeElement,
    listbox,
    "downloads list is focused after shift+tab is pressed yet again"
  );
  is(
    listbox.selectedIndex,
    buttonIndex,
    "downloads list selected index after shift+tab is pressed yet again"
  );
}