summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_menulist_keynav.xhtml
blob: 86e86b651044e1d668da1a1017c1ad342f7994a2 (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
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window title="Menulist Key Navigation Tests"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

<button id="button1" label="One"/>
<menulist id="list">
  <menupopup id="popup" onpopupshowing="return gShowPopup;">
    <menuitem id="i1" label="One"/>
    <menuitem id="i2" label="Two"/>
    <menuitem id="i2b" disabled="true" label="Two and a Half"/>
    <menuitem id="i3" label="Three"/>
    <menuitem id="i4" label="Four"/>
  </menupopup>
</menulist>
<button id="button2" label="Two"/>
<menulist id="list2">
  <menupopup id="popup" onpopupshown="checkCursorNavigation();">
    <menuitem id="b1" label="One"/>
    <menuitem id="b2" label="Two" selected="true"/>
    <menuitem id="b3" label="Three"/>
    <menuitem id="b4" label="Four"/>
  </menupopup>
</menulist>
<menulist id="list3" sizetopopup="none">
  <menupopup>
    <menuitem id="s1" label="One"/>
    <menuitem id="s2" label="Two"/>
    <menuitem id="s3" label="Three"/>
    <menuitem id="s4" label="Four"/>
  </menupopup>
</menulist>

<script class="testbody" type="application/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

var gShowPopup = false;
var gModifiers = 0;
var gOpenPhase = false;

var list = $("list");
let expectCommandEvent;

var iswin = (navigator.platform.indexOf("Win") == 0);
var ismac = (navigator.platform.indexOf("Mac") == 0);

function runTests()
{
  list.focus();

  // on Mac, up and cursor keys open the menu, but on other platforms, the
  // cursor keys navigate between items without opening the menu
  if (!ismac) {
    expectCommandEvent = true;
    keyCheck(list, "KEY_ArrowDown", 2, 1, "cursor down");
    keyCheck(list, "KEY_ArrowDown", 3, 1, "cursor down skip disabled");
    keyCheck(list, "KEY_ArrowUp", 2, 1, "cursor up skip disabled");
    keyCheck(list, "KEY_ArrowUp", 1, 1, "cursor up");

    // On Windows, wrapping doesn't occur.
    expectCommandEvent = !iswin;
    keyCheck(list, "KEY_ArrowUp", iswin ? 1 : 4, 1, "cursor up wrap");

    list.selectedIndex = 4;
    list.activeChild = list.selectedItem;
    keyCheck(list, "KEY_ArrowDown", iswin ? 4 : 1, 4, "cursor down wrap");

    list.selectedIndex = 0;
    list.activeChild = list.selectedItem;
  }

  // check that attempting to open the menulist does not change the selection
  synthesizeKey("KEY_ArrowDown", {altKey: !ismac});
  is(list.selectedItem, $("i1"), "open menulist down selectedItem");
  synthesizeKey("KEY_ArrowUp", {altKey: !ismac});
  is(list.selectedItem, $("i1"), "open menulist up selectedItem");

  list.selectedItem = $("i1");

  pressLetter();
}

function pressLetter()
{
  // A command event should be fired only if the menulist is closed.
  expectCommandEvent = !gOpenPhase || iswin;

  sendString("G");
  is(list.selectedItem, $("i1"), "letter pressed not found selectedItem");

  keyCheck(list, "T", 2, 1, "letter pressed");

  setTimeout(pressedAgain, 1200);
}

function pressedAgain()
{
  keyCheck(list, "T", 3, 1, "letter pressed again");
  SpecialPowers.setIntPref("ui.menu.incremental_search.timeout", 0); // prevent to timeout
  keyCheck(list, "W", 2, 1, "second letter pressed");
  SpecialPowers.clearUserPref("ui.menu.incremental_search.timeout");
  setTimeout(differentPressed, 1200);
}

function differentPressed()
{
  keyCheck(list, "O", 1, 1, "different letter pressed");

  if (gOpenPhase) {
    list.open = false;
    tabAndScroll();
  }
  else {
     // Run the letter tests again with the popup open
    info("list open phase");

    list.selectedItem = $("i1");

    // Hide and show the list to avoid using any existing incremental key state.
    list.hidden = true;
    list.clientWidth;
    list.hidden = false;

    gShowPopup = true;
    gOpenPhase = true;

    list.addEventListener("popupshown", function popupShownListener() {
      pressLetter();
    }, { once: true});

    list.open = true;
  }
}

function inputMargin(el) {
  let cs = getComputedStyle(el);
  // XXX Internal properties are not exposed in getComputedStyle, so we have to
  // use margin and rely on our knowledge of them matching negative margins
  // where appropriate.
  // return parseFloat(cs.getPropertyValue("-moz-window-input-region-margin"));
  return ismac ? 0 : Math.max(-parseFloat(cs.marginLeft), 0);
}

function tabAndScroll()
{
  list = $("list");

  if (!ismac) {
    $("button1").focus();
    synthesizeKeyExpectEvent("KEY_Tab", {}, list, "focus", "focus to menulist");
    synthesizeKeyExpectEvent("KEY_Tab", {}, $("button2"), "focus", "focus to button");
    is(document.activeElement, $("button2"), "tab from menulist focused button");
  }

  // now make sure that using a key scrolls the menu correctly

  for (let i = 0; i < 65; i++) {
    list.appendItem("Item" + i, "item" + i);
  }
  list.open = true;
  is(list.getBoundingClientRect().width, list.menupopup.getBoundingClientRect().width - 2 * inputMargin(list.menupopup),
     "menu and popup width match");
  var minScrollbarWidth = window.matchMedia("(-moz-overlay-scrollbars)").matches ? 0 : 3;
  ok(list.getBoundingClientRect().width >= list.getItemAtIndex(0).getBoundingClientRect().width + minScrollbarWidth,
     "menuitem width accounts for scrollbar");
  list.open = false;

  list.menupopup.style.maxHeight = "100px";
  list.open = true;

  var rowdiff = list.getItemAtIndex(1).getBoundingClientRect().top -
                list.getItemAtIndex(0).getBoundingClientRect().top;

  var item = list.getItemAtIndex(10);
  var originalPosition = item.getBoundingClientRect().top;

  list.activeChild = item;
  ok(item.getBoundingClientRect().top < originalPosition,
    "position of item 1: " + item.getBoundingClientRect().top + " -> " + originalPosition);

  originalPosition = item.getBoundingClientRect().top;

  synthesizeKey("KEY_ArrowDown");
  is(item.getBoundingClientRect().top, originalPosition - rowdiff, "position of item 10");

  list.open = false;

  checkEnter();
}

function keyCheck(list, key, index, defaultindex, testname)
{
  info(`keyCheck(${index}, ${key}, ${index}, ${defaultindex}, ${testname}, ${expectCommandEvent})`);
  var item = $("i" + index);
  var defaultitem = $("i" + defaultindex || 1);

  synthesizeKeyExpectEvent(key, { }, item, expectCommandEvent ? "command" : "!command", testname);
  is(list.selectedItem, expectCommandEvent ? item : defaultitem, testname + " selectedItem----" + list.selectedItem.id);
}

function checkModifiers(event)
{
  var expectedModifiers = (gModifiers == 1);
  is(event.shiftKey, expectedModifiers, "shift key pressed");
  is(event.ctrlKey, expectedModifiers, "ctrl key pressed");
  is(event.altKey, expectedModifiers, "alt key pressed");
  is(event.metaKey, expectedModifiers, "meta key pressed");
  gModifiers++;
}

function checkEnter()
{
  list.addEventListener("popuphidden", checkEnterWithModifiers);
  list.addEventListener("command", checkModifiers);
  list.open = true;
  synthesizeKey("KEY_Enter");
}

function checkEnterWithModifiers()
{
  is(gModifiers, 1, "modifiers checked when not set");

  ok(!list.open, "list closed on enter press");
  list.removeEventListener("popuphidden", checkEnterWithModifiers);

  list.addEventListener("popuphidden", verifyPopupOnClose);
  list.open = true;

  synthesizeKey("KEY_Enter", {shiftKey: true, ctrlKey: true, altKey: true, metaKey: true});
}

function verifyPopupOnClose()
{
  is(gModifiers, 2, "modifiers checked when set");

  ok(!list.open, "list closed on enter press with modifiers");
  list.removeEventListener("popuphidden", verifyPopupOnClose);

  list = $("list2");
  list.focus();
  list.open = true;
}

function checkCursorNavigation()
{
  var commandEventsCount = 0;
  list.addEventListener("command", event => {
    is(event.target, list.selectedItem, "command event fired on selected item");
    commandEventsCount++;
  });

  is(list.selectedIndex, 1, "selectedIndex before cursor down");
  synthesizeKey("KEY_ArrowDown");
  is(list.selectedIndex, iswin ? 2 : 1, "selectedIndex after cursor down");
  is(commandEventsCount, iswin ? 1 : 0, "selectedIndex after cursor down command event");
  is(list.menupopup.state, "open", "cursor down popup state");
  synthesizeKey("KEY_PageDown");
  is(list.selectedIndex, iswin ? 2 : 1, "selectedIndex after page down");
  is(commandEventsCount, iswin ? 1 : 0, "selectedIndex after page down command event");
  is(list.menupopup.state, "open", "page down popup state");

  // Check whether cursor up and down wraps.
  list.selectedIndex = 0;
  list.activeChild = list.selectedItem;
  synthesizeKey("KEY_ArrowUp");
  is(list.activeChild,
     document.getElementById(iswin || ismac ? "b1" : "b4"), "cursor up wrap while open");

  list.selectedIndex = 3;
  list.activeChild = list.selectedItem;
  synthesizeKey("KEY_ArrowDown");
  is(list.activeChild,
     document.getElementById(iswin || ismac ? "b4" : "b1"), "cursor down wrap while open");

  synthesizeKey("KEY_ArrowUp", {altKey: true});
  is(list.open, ismac, "alt+up closes popup");

  if (ismac) {
    list.open = false;
  }

  // Finally, test a menulist with sizetopopup="none" to ensure keyboard navigation
  // still works when the popup has not been opened.
  if (!ismac) {
    let unsizedMenulist = document.getElementById("list3");
    unsizedMenulist.focus();
    synthesizeKey("KEY_ArrowDown");
    is(unsizedMenulist.selectedIndex, 1, "correct menulist index on keydown");
    is(unsizedMenulist.label, "Two", "correct menulist label on keydown");
  }

  SimpleTest.finish();
}

SimpleTest.waitForFocus(runTests);

]]>
</script>

<body xmlns="http://www.w3.org/1999/xhtml">
<p id="display">
</p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>

</window>