summaryrefslogtreecommitdiffstats
path: root/layout/forms/test/test_bug935876.html
blob: bb40902117fd184bbb8a578aef07f6280d1dd7dd (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=935876
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 935876</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=935876">Mozilla Bug 935876</a>
<p id="display"></p>
<div>
<select id="listbox" size="3">
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
</select>
<select id="multipleListbox" size="3" multiple>
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
</select>
<select id="combobox">
  <option selected>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
</select>
</div>
<pre id="test">
</pre>
<script type="application/javascript">
SimpleTest.waitForExplicitFinish();

const kIsWin = navigator.platform.indexOf("Win") == 0;
const kIsMac = navigator.platform.indexOf("Mac") == 0;
const kIsAndroid = navigator.appVersion.indexOf("Android") != 0;

function runTests()
{
  var doPreventDefault = false;
  function onKeydown(aEvent)
  {
    if (doPreventDefault) {
      aEvent.preventDefault();
    }
  }

  var keyPressEventFired = false;
  function onKeypress(aEvent)
  {
    keyPressEventFired = true;
  }

  var keyDownEventConsumedByJS = false;
  var keyDownEventConsumed = false;
  function onkeydownInSystemEventGroup(aEvent)
  {
    keyDownEventConsumedByJS = aEvent.defaultPrevented;
    keyDownEventConsumed = aEvent.getPreventDefault();
  }

  function reset()
  {
    keyPressEventFired = false;
    keyDownEventConsumedByJS = false;
    keyDownEventConsumed = false;
  }

  function check(aExpectingKeydownConsumed, aDescription)
  {
    if (doPreventDefault) {
      ok(!keyPressEventFired, "keypress event shouldn't be fired for " + aDescription +
                              " if preventDefault() of keydown event was called");
      ok(keyDownEventConsumedByJS, "keydown event of " + aDescription +
                                   " should be consumed in content level if preventDefault() of keydown event is called");
      ok(keyDownEventConsumed, "keydown event of " + aDescription +
                               " should be consumed in system level if preventDefault() of keydown event is called");
    } else if (aExpectingKeydownConsumed) {
      ok(!keyPressEventFired, "keypress event shouldn't be fired for " + aDescription);
      ok(!keyDownEventConsumedByJS, "keydown event of " + aDescription + " shouldn't be consumed in content level");
      ok(keyDownEventConsumed, "keydown event of " + aDescription + " should be consumed in system level");
    } else {
      ok(keyPressEventFired, "keypress event should be fired for " + aDescription);
      ok(!keyDownEventConsumedByJS, "keydown event of " + aDescription + " shouldn't be consumed in content level");
      ok(!keyDownEventConsumed, "keydown event of " + aDescription + " should be consumed in system level");
    }
  }

  var listbox = document.getElementById("listbox");
  listbox.addEventListener("keydown", onKeydown, false);
  listbox.addEventListener("keypress", onKeypress, false);
  SpecialPowers.addSystemEventListener(listbox, "keydown", onkeydownInSystemEventGroup, false);

  listbox.focus();

  [ false, true ].forEach(function (consume) {
    doPreventDefault = consume;
    for (var i = 0; i < listbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_DOWN", {});
      check(true, "DownArrow key on listbox #" + i);
    }

    for (var i = 0; i < listbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_UP", {});
      check(true, "UpArrow key on listbox #" + i);
    }

    for (var i = 0; i < listbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_RIGHT", {});
      check(true, "RightArrow key on listbox #" + i);
    }

    for (var i = 0; i < listbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_LEFT", {});
      check(true, "LeftArrow key on listbox #" + i);
    }

    for (var i = 0; i < 4; i++) {
      reset()
      synthesizeKey("VK_PAGE_DOWN", {});
      check(true, "PageDown key on listbox #" + i);
    }

    for (var i = 0; i < 4; i++) {
      reset()
      synthesizeKey("VK_PAGE_UP", {});
      check(true, "PageUp key on listbox #" + i);
    }

    for (var i = 0; i < 2; i++) {
      reset()
      synthesizeKey("VK_END", {});
      check(true, "End key on listbox #" + i);
    }

    for (var i = 0; i < 2; i++) {
      reset()
      synthesizeKey("VK_HOME", {});
      check(true, "Home key on listbox #" + i);
    }

    reset()
    synthesizeKey("VK_RETURN", {});
    check(false, "Enter key on listbox");

    reset()
    synthesizeKey("VK_ESCAPE", {});
    check(false, "Esc key on listbox");

    reset()
    synthesizeKey("VK_F4", {});
    check(false, "F4 key on listbox");

    reset()
    synthesizeKey("a", {});
    check(false, "'A' key on listbox");
  });

  listbox.removeEventListener("keydown", onKeydown, false);
  listbox.removeEventListener("keypress", onKeypress, false);
  SpecialPowers.removeSystemEventListener(listbox, "keydown", onkeydownInSystemEventGroup, false);



  var multipleListbox = document.getElementById("multipleListbox");
  multipleListbox.addEventListener("keydown", onKeydown, false);
  multipleListbox.addEventListener("keypress", onKeypress, false);
  SpecialPowers.addSystemEventListener(multipleListbox, "keydown", onkeydownInSystemEventGroup, false);

  multipleListbox.focus();

  [ false, true ].forEach(function (consume) {
    doPreventDefault = consume;
    for (var i = 0; i < multipleListbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_DOWN", {});
      check(true, "DownArrow key on multiple listbox #" + i);
    }

    for (var i = 0; i < multipleListbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_UP", {});
      check(true, "UpArrow key on multiple listbox #" + i);
    }

    for (var i = 0; i < multipleListbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_RIGHT", {});
      check(true, "RightArrow key on multiple listbox #" + i);
    }

    for (var i = 0; i < multipleListbox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_LEFT", {});
      check(true, "LeftArrow key on multiple listbox #" + i);
    }

    for (var i = 0; i < 4; i++) {
      reset()
      synthesizeKey("VK_PAGE_DOWN", {});
      check(true, "PageDown key on multiple listbox #" + i);
    }

    for (var i = 0; i < 4; i++) {
      reset()
      synthesizeKey("VK_PAGE_UP", {});
      check(true, "PageUp key on multiple listbox #" + i);
    }

    for (var i = 0; i < 2; i++) {
      reset()
      synthesizeKey("VK_END", {});
      check(true, "End key on multiple listbox #" + i);
    }

    for (var i = 0; i < 2; i++) {
      reset()
      synthesizeKey("VK_HOME", {});
      check(true, "Home key on multiple listbox #" + i);
    }

    reset()
    synthesizeKey("VK_RETURN", {});
    check(true, "Enter key on multiple listbox");

    reset()
    synthesizeKey("VK_ESCAPE", {});
    check(false, "Esc key on multiple listbox");

    reset()
    synthesizeKey("VK_F4", {});
    check(false, "F4 key on multiple listbox");

    reset()
    synthesizeKey("a", {});
    check(false, "'A' key on multiple listbox");
  });

  multipleListbox.removeEventListener("keydown", onKeydown, false);
  multipleListbox.removeEventListener("keypress", onKeypress, false);
  SpecialPowers.removeSystemEventListener(multipleListbox, "keydown", onkeydownInSystemEventGroup, false);



  var combobox = document.getElementById("combobox");
  combobox.addEventListener("keydown", onKeydown, false);
  combobox.addEventListener("keypress", onKeypress, false);
  SpecialPowers.addSystemEventListener(combobox, "keydown", onkeydownInSystemEventGroup, false);

  combobox.focus();

  [ false, true ].forEach(function (consume) {
    doPreventDefault = consume;
    if (!kIsMac) {
      for (var i = 0; i < combobox.options.length + 1; i++) {
        reset()
        synthesizeKey("VK_DOWN", {});
        check(true, "DownArrow key on combobox #" + i);
      }

      for (var i = 0; i < combobox.options.length + 1; i++) {
        reset()
        synthesizeKey("VK_UP", {});
        check(true, "UpArrow key on combobox #" + i);
      }
    } else {
      todo(false, "Make this test work on OSX");
    }

    for (var i = 0; i < combobox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_RIGHT", {});
      check(true, "RightArrow key on combobox #" + i);
    }

    for (var i = 0; i < combobox.options.length + 1; i++) {
      reset()
      synthesizeKey("VK_LEFT", {});
      check(true, "LeftArrow key on combobox #" + i);
    }

    for (var i = 0; i < 4; i++) {
      reset()
      synthesizeKey("VK_PAGE_DOWN", {});
      check(true, "PageDown key on combobox #" + i);
    }

    for (var i = 0; i < 4; i++) {
      reset()
      synthesizeKey("VK_PAGE_UP", {});
      check(true, "PageUp key on combobox #" + i);
    }

    for (var i = 0; i < 2; i++) {
      reset()
      synthesizeKey("VK_END", {});
      check(true, "End key on combobox #" + i);
    }

    for (var i = 0; i < 2; i++) {
      reset()
      synthesizeKey("VK_HOME", {});
      check(true, "Home key on combobox #" + i);
    }

    reset()
    synthesizeKey("VK_RETURN", {});
    check(false, "Enter key on combobox");

    reset()
    synthesizeKey("VK_ESCAPE", {});
    check(true, "Esc key on combobox");

    if (!kIsWin) {
      reset()
      synthesizeKey("VK_F4", {});
      check(false, "F4 key on combobox");
    }

    reset()
    synthesizeKey("a", {});
    check(false, "'A' key on combobox");
  });

  function finish()
  {
    combobox.removeEventListener("keydown", onKeydown, false);
    combobox.removeEventListener("keypress", onKeypress, false);
    SpecialPowers.removeSystemEventListener(combobox, "keydown", onkeydownInSystemEventGroup, false);
    SimpleTest.finish();
  }

  // Mac uses native popup for dropdown.  Let's skip the tests for popup
  // since it's not handled in nsListControlFrame.
  // Similarly, Android doesn't use popup for dropdown.
  if (kIsMac || kIsAndroid) {
    finish();
    return;
  }

  function testDropDown(aCallback)
  {
    testOpenDropDown(function () {
      reset()
      synthesizeKey("VK_DOWN", { altKey: true });
    }, function () {
      check(true, "Alt + DownArrow key on combobox at opening dropdown");

      for (var i = 0; i < combobox.options.length + 1; i++) {
        reset()
        synthesizeKey("VK_DOWN", {});
        check(true, "DownArrow key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < combobox.options.length + 1; i++) {
        reset()
        synthesizeKey("VK_UP", {});
        check(true, "UpArrow key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < combobox.options.length + 1; i++) {
        reset()
        synthesizeKey("VK_RIGHT", {});
        check(true, "RightArrow key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < combobox.options.length + 1; i++) {
        reset()
        synthesizeKey("VK_LEFT", {});
        check(true, "LeftArrow key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < 4; i++) {
        reset()
        synthesizeKey("VK_PAGE_DOWN", {});
        check(true, "PageDown key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < 4; i++) {
        reset()
        synthesizeKey("VK_PAGE_UP", {});
        check(true, "PageUp key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < 2; i++) {
        reset()
        synthesizeKey("VK_END", {});
        check(true, "End key on combobox during dropdown open #" + i);
      }

      for (var i = 0; i < 2; i++) {
        reset()
        synthesizeKey("VK_HOME", {});
        check(true, "Home key on combobox during dropdown open #" + i);
      }

      testCloseDropDown(function () {
        reset()
        synthesizeKey("VK_RETURN", {});
      }, function () {
        testOpenDropDown(function () {
          check(true, "Enter key on combobox at closing dropdown");

          synthesizeKey("VK_UP", { altKey: true });
        }, function () {
          check(true, "Alt + UpArrow key on combobox at opening dropdown");

          testCloseDropDown(function () {
            reset()
            synthesizeKey("VK_ESCAPE", {});
          }, function () {
            check(true, "Esc key on combobox at closing dropdown");

            // F4 key opens/closes dropdown only on Windows. So, other platforms
            // don't need to do anymore.
            if (!kIsWin) {
              aCallback();
              return;
            }

            testOpenDropDown(function () {
              reset()
              synthesizeKey("VK_F4", {});
            }, function () {
              check(true, "F4 key on combobox at opening dropdown on Windows");

              testCloseDropDown(function () {
                reset()
                synthesizeKey("VK_F4", {});
              }, function () {
                check(true, "F4 key on combobox at closing dropdown on Windows");

                aCallback();
                return;
              });
            });
          });
        });
      });
    });
  }

  doPreventDefault = false;
  testDropDown(function () {
    // Even if keydown event is consumed by JS, opening/closing dropdown
    // should work for a11y and security (e.g., cannot close dropdown causes
    // staying top-most window on the screen).  If it's blocked by JS, this
    // test would cause permanent timeout.
    doPreventDefault = true;
    testDropDown(finish);
  });
}

function testOpenDropDown(aTest, aOnOpenDropDown)
{
  document.addEventListener("popupshowing", function (aEvent) {
    document.removeEventListener(aEvent.type, arguments.callee, false);
    setTimeout(aOnOpenDropDown, 0);
  }, false);
  aTest();
}

function testCloseDropDown(aTest, aOnCloseDropDown)
{
  document.addEventListener("popuphiding", function (aEvent) {
    document.removeEventListener(aEvent.type, arguments.callee, false);
    setTimeout(aOnCloseDropDown, 0)
  }, false);
  aTest();
}

SimpleTest.waitForFocus(runTests);
</script>
</body>
</html>