summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_htmleditor_keyevent_handling.html
blob: bfec290a58b35ef0ee87982dbc72f9f32ed2e6b1 (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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
<html>
<head>
  <title>Test for key event handler of HTML editor</title>
  <script type="text/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css"
          href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<div id="display">
  <div id="htmlEditor" contenteditable="true"><br></div>
</div>
<div id="content" style="display: none">
  
</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests, window);

var htmlEditor = document.getElementById("htmlEditor");

const kIsMac = navigator.platform.indexOf("Mac") == 0;
const kIsWin = navigator.platform.indexOf("Win") == 0;
const kIsLinux = navigator.platform.indexOf("Linux") == 0 || navigator.platform.indexOf("SunOS") == 0 ;

function runTests()
{
  document.execCommand("stylewithcss", false, "true");

  var fm = SpecialPowers.Cc["@mozilla.org/focus-manager;1"].
    getService(SpecialPowers.Ci.nsIFocusManager);

  var capturingPhase = { fired: false, prevented: false };
  var bubblingPhase = { fired: false, prevented: false };

  var listener = {
    handleEvent: function _hv(aEvent)
    {
      is(aEvent.type, "keypress", "unexpected event is handled");
      switch (aEvent.eventPhase) {
      case aEvent.CAPTURING_PHASE:
        capturingPhase.fired = true;
        capturingPhase.prevented = aEvent.defaultPrevented;
        break;
      case aEvent.BUBBLING_PHASE:
        bubblingPhase.fired = true;
        bubblingPhase.prevented = aEvent.defaultPrevented;
        aEvent.preventDefault(); // prevent the browser default behavior
        break;
      default:
        ok(false, "event is handled in unexpected phase");
      }
    }
  };

  function check(aDescription,
                 aFiredOnCapture, aFiredOnBubbling, aPreventedOnBubbling)
  {
    function getDesciption(aExpected)
    {
      return aDescription + (aExpected ? " wasn't " : " was ");
    }
    is(capturingPhase.fired, aFiredOnCapture,
       getDesciption(aFiredOnCapture) + "fired on capture phase");
    is(bubblingPhase.fired, aFiredOnBubbling,
       getDesciption(aFiredOnBubbling) + "fired on bubbling phase");

    // If the event is fired on bubbling phase and it was already prevented
    // on capture phase, it must be prevented on bubbling phase too.
    if (capturingPhase.prevented) {
      todo(false, aDescription +
           " was consumed already, so, we cannot test the editor behavior actually");
      aPreventedOnBubbling = true;
    }

    is(bubblingPhase.prevented, aPreventedOnBubbling,
       getDesciption(aPreventedOnBubbling) + "prevented on bubbling phase");
  }

  SpecialPowers.addSystemEventListener(window, "keypress", listener, true);
  SpecialPowers.addSystemEventListener(window, "keypress", listener, false);

  function doTest(aElement, aDescription,
                  aIsReadonly, aIsTabbable, aIsPlaintext)
  {
    function reset(aText)
    {
      capturingPhase.fired = false;
      capturingPhase.prevented = false;
      bubblingPhase.fired = false;
      bubblingPhase.prevented = false;
      aElement.innerHTML = aText;
      var sel = window.getSelection();
      var range = document.createRange();
      range.setStart(aElement, aElement.childNodes.length);
      sel.removeAllRanges();
      sel.addRange(range);
    }

    function resetForIndent(aText)
    {
      capturingPhase.fired = false;
      capturingPhase.prevented = false;
      bubblingPhase.fired = false;
      bubblingPhase.prevented = false;
      aElement.innerHTML = aText;
      var sel = window.getSelection();
      var range = document.createRange();
      var target = document.getElementById("target").firstChild;
      range.setStart(target, target.length);
      sel.removeAllRanges();
      sel.addRange(range);
    }

    if (document.activeElement) {
      document.activeElement.blur();
    }

    aDescription += ": "

    aElement.focus();
    is(SpecialPowers.unwrap(fm.focusedElement), aElement, aDescription + "failed to move focus");

    // Backspace key:
    //   If editor is readonly, it doesn't consume.
    //   If editor is editable, it consumes backspace and shift+backspace.
    //   Otherwise, editor doesn't consume the event.
    reset("");
    synthesizeKey("VK_BACK_SPACE", { });
    check(aDescription + "Backspace", true, true, true);

    reset("");
    synthesizeKey("VK_BACK_SPACE", { shiftKey: true });
    check(aDescription + "Shift+Backspace", true, true, true);

    reset("");
    synthesizeKey("VK_BACK_SPACE", { ctrlKey: true });
    check(aDescription + "Ctrl+Backspace", true, true, aIsReadonly);

    reset("");
    synthesizeKey("VK_BACK_SPACE", { altKey: true });
    check(aDescription + "Alt+Backspace", true, true, aIsReadonly || kIsMac);

    reset("");
    synthesizeKey("VK_BACK_SPACE", { metaKey: true });
    check(aDescription + "Meta+Backspace", true, true, aIsReadonly);

    reset("");
    synthesizeKey("VK_BACK_SPACE", { osKey: true });
    check(aDescription + "OS+Backspace", true, true, aIsReadonly);

    // Delete key:
    //   If editor is readonly, it doesn't consume.
    //   If editor is editable, delete is consumed.
    //   Otherwise, editor doesn't consume the event.
    reset("");
    synthesizeKey("VK_DELETE", { });
    check(aDescription + "Delete", true, true, !aIsReadonly || kIsMac);

    reset("");
    synthesizeKey("VK_DELETE", { shiftKey: true });
    check(aDescription + "Shift+Delete", true, true, kIsMac);

    reset("");
    synthesizeKey("VK_DELETE", { ctrlKey: true });
    check(aDescription + "Ctrl+Delete", true, true, false);

    reset("");
    synthesizeKey("VK_DELETE", { altKey: true });
    check(aDescription + "Alt+Delete", true, true, kIsMac);

    reset("");
    synthesizeKey("VK_DELETE", { metaKey: true });
    check(aDescription + "Meta+Delete", true, true, false);

    reset("");
    synthesizeKey("VK_DELETE", { osKey: true });
    check(aDescription + "OS+Delete", true, true, false);

    // Return key:
    //   If editor is readonly, it doesn't consume.
    //   If editor is editable and not single line editor, it consumes Return
    //   and Shift+Return.
    //   Otherwise, editor doesn't consume the event.
    reset("a");
    synthesizeKey("VK_RETURN", { });
    check(aDescription + "Return",
          true, true, !aIsReadonly);
    is(aElement.innerHTML, aIsReadonly ? "a" : "a<br><br>",
       aDescription + "Return");

    reset("a");
    synthesizeKey("VK_RETURN", { shiftKey: true });
    check(aDescription + "Shift+Return",
          true, true, !aIsReadonly);
    is(aElement.innerHTML, aIsReadonly ? "a" : "a<br><br>",
       aDescription + "Shift+Return");

    reset("a");
    synthesizeKey("VK_RETURN", { ctrlKey: true });
    check(aDescription + "Ctrl+Return", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "Ctrl+Return");

    reset("a");
    synthesizeKey("VK_RETURN", { altKey: true });
    check(aDescription + "Alt+Return", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "Alt+Return");

    reset("a");
    synthesizeKey("VK_RETURN", { metaKey: true });
    check(aDescription + "Meta+Return", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "Meta+Return");

    reset("a");
    synthesizeKey("VK_RETURN", { osKey: true });
    check(aDescription + "OS+Return", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "OS+Return");

    // Tab key:
    //   If editor is tabbable, editor doesn't consume all tab key events.
    //   Otherwise, editor consumes tab key event without any modifier keys.
    reset("a");
    synthesizeKey("VK_TAB", { });
    check(aDescription + "Tab",
          true, true, !aIsTabbable && !aIsReadonly);
    is(aElement.innerHTML,
       aIsTabbable || aIsReadonly ? "a" :
         aIsPlaintext ? "a\t" : "a&nbsp;&nbsp;&nbsp; <br>",
       aDescription + "Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Tab)");

    reset("a");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "Shift+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab)");

    // Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
    // event should never be fired.
    reset("a");
    synthesizeKey("VK_TAB", { ctrlKey: true });
    check(aDescription + "Ctrl+Tab", false, false, false);
    is(aElement.innerHTML, "a", aDescription + "Ctrl+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Ctrl+Tab)");

    reset("a");
    synthesizeKey("VK_TAB", { altKey: true });
    check(aDescription + "Alt+Tab", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "Alt+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Alt+Tab)");

    reset("a");
    synthesizeKey("VK_TAB", { metaKey: true });
    check(aDescription + "Meta+Tab", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "Meta+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Meta+Tab)");

    reset("a");
    synthesizeKey("VK_TAB", { osKey: true });
    check(aDescription + "OS+Tab", true, true, false);
    is(aElement.innerHTML, "a", aDescription + "OS+Tab");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (OS+Tab)");

    // Indent/Outdent tests:
    // UL
    resetForIndent("<ul><li id=\"target\">ul list item</li></ul>");
    synthesizeKey("VK_TAB", { });
    check(aDescription + "Tab on UL",
          true, true, !aIsTabbable && !aIsReadonly);
    is(aElement.innerHTML,
       aIsReadonly || aIsTabbable ?
         "<ul><li id=\"target\">ul list item</li></ul>" :
         aIsPlaintext ? "<ul><li id=\"target\">ul list item\t</li></ul>" :
           "<ul><ul><li id=\"target\">ul list item</li></ul></ul>",
       aDescription + "Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Tab on UL)");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab after Tab on UL",
          true, true, !aIsTabbable && !aIsReadonly && !aIsPlaintext);
    is(aElement.innerHTML,
       aIsReadonly || aIsTabbable || (!aIsPlaintext) ?
         "<ul><li id=\"target\">ul list item</li></ul>" :
         "<ul><li id=\"target\">ul list item\t</li></ul>",
       aDescription + "Shift+Tab after Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab after Tab on UL)");

    resetForIndent("<ul><li id=\"target\">ul list item</li></ul>");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab on UL",
          true, true, !aIsTabbable && !aIsReadonly && !aIsPlaintext);
    is(aElement.innerHTML,
       aIsReadonly || aIsTabbable || aIsPlaintext ?
         "<ul><li id=\"target\">ul list item</li></ul>" : "ul list item",
       aDescription + "Shift+Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab on UL)");

    // Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
    // event should never be fired.
    resetForIndent("<ul><li id=\"target\">ul list item</li></ul>");
    synthesizeKey("VK_TAB", { ctrlKey: true });
    check(aDescription + "Ctrl+Tab on UL", false, false, false);
    is(aElement.innerHTML, "<ul><li id=\"target\">ul list item</li></ul>",
       aDescription + "Ctrl+Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Ctrl+Tab on UL)");

    resetForIndent("<ul><li id=\"target\">ul list item</li></ul>");
    synthesizeKey("VK_TAB", { altKey: true });
    check(aDescription + "Alt+Tab on UL", true, true, false);
    is(aElement.innerHTML, "<ul><li id=\"target\">ul list item</li></ul>",
       aDescription + "Alt+Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Alt+Tab on UL)");

    resetForIndent("<ul><li id=\"target\">ul list item</li></ul>");
    synthesizeKey("VK_TAB", { metaKey: true });
    check(aDescription + "Meta+Tab on UL", true, true, false);
    is(aElement.innerHTML, "<ul><li id=\"target\">ul list item</li></ul>",
       aDescription + "Meta+Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Meta+Tab on UL)");

    resetForIndent("<ul><li id=\"target\">ul list item</li></ul>");
    synthesizeKey("VK_TAB", { osKey: true });
    check(aDescription + "OS+Tab on UL", true, true, false);
    is(aElement.innerHTML, "<ul><li id=\"target\">ul list item</li></ul>",
       aDescription + "OS+Tab on UL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (OS+Tab on UL)");

    // OL
    resetForIndent("<ol><li id=\"target\">ol list item</li></ol>");
    synthesizeKey("VK_TAB", { });
    check(aDescription + "Tab on OL",
          true, true, !aIsTabbable && !aIsReadonly);
    is(aElement.innerHTML,
       aIsReadonly || aIsTabbable ?
         "<ol><li id=\"target\">ol list item</li></ol>" :
         aIsPlaintext ? "<ol><li id=\"target\">ol list item\t</li></ol>" :
           "<ol><ol><li id=\"target\">ol list item</li></ol></ol>",
       aDescription + "Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Tab on OL)");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab after Tab on OL",
          true, true, !aIsTabbable && !aIsReadonly && !aIsPlaintext);
    is(aElement.innerHTML,
       aIsReadonly || aIsTabbable || (!aIsPlaintext) ?
         "<ol><li id=\"target\">ol list item</li></ol>" :
         "<ol><li id=\"target\">ol list item\t</li></ol>",
       aDescription + "Shift+Tab after Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab after Tab on OL)");

    resetForIndent("<ol><li id=\"target\">ol list item</li></ol>");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab on OL",
          true, true, !aIsTabbable && !aIsReadonly && !aIsPlaintext);
    is(aElement.innerHTML,
       aIsReadonly || aIsTabbable || aIsPlaintext ?
         "<ol><li id=\"target\">ol list item</li></ol>" : "ol list item",
       aDescription + "Shfit+Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab on OL)");

    // Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
    // event should never be fired.
    resetForIndent("<ol><li id=\"target\">ol list item</li></ol>");
    synthesizeKey("VK_TAB", { ctrlKey: true });
    check(aDescription + "Ctrl+Tab on OL", false, false, false);
    is(aElement.innerHTML, "<ol><li id=\"target\">ol list item</li></ol>",
       aDescription + "Ctrl+Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Ctrl+Tab on OL)");

    resetForIndent("<ol><li id=\"target\">ol list item</li></ol>");
    synthesizeKey("VK_TAB", { altKey: true });
    check(aDescription + "Alt+Tab on OL", true, true, false);
    is(aElement.innerHTML, "<ol><li id=\"target\">ol list item</li></ol>",
       aDescription + "Alt+Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Alt+Tab on OL)");

    resetForIndent("<ol><li id=\"target\">ol list item</li></ol>");
    synthesizeKey("VK_TAB", { metaKey: true });
    check(aDescription + "Meta+Tab on OL", true, true, false);
    is(aElement.innerHTML, "<ol><li id=\"target\">ol list item</li></ol>",
       aDescription + "Meta+Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Meta+Tab on OL)");

    resetForIndent("<ol><li id=\"target\">ol list item</li></ol>");
    synthesizeKey("VK_TAB", { osKey: true });
    check(aDescription + "OS+Tab on OL", true, true, false);
    is(aElement.innerHTML, "<ol><li id=\"target\">ol list item</li></ol>",
       aDescription + "OS+Tab on OL");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (OS+Tab on OL)");

    // TD
    resetForIndent("<table><tr><td id=\"target\">td</td></tr></table>");
    synthesizeKey("VK_TAB", { });
    check(aDescription + "Tab on TD",
          true, true, !aIsTabbable && !aIsReadonly);
    is(aElement.innerHTML,
       aIsTabbable || aIsReadonly ?
         "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>" :
         aIsPlaintext ? "<table><tbody><tr><td id=\"target\">td\t</td></tr></tbody></table>" :
           "<table><tbody><tr><td id=\"target\">td</td></tr><tr><td style=\"vertical-align: top;\"><br></td></tr></tbody></table>",
       aDescription + "Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Tab on TD)");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab after Tab on TD",
          true, true, !aIsTabbable && !aIsReadonly && !aIsPlaintext);
    is(aElement.innerHTML,
       aIsTabbable || aIsReadonly ?
         "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>" :
         aIsPlaintext ? "<table><tbody><tr><td id=\"target\">td\t</td></tr></tbody></table>" :
           "<table><tbody><tr><td id=\"target\">td</td></tr><tr><td style=\"vertical-align: top;\"><br></td></tr></tbody></table>",
       aDescription + "Shift+Tab after Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab after Tab on TD)");

    resetForIndent("<table><tr><td id=\"target\">td</td></tr></table>");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab on TD", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>",
       aDescription + "Shift+Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab on TD)");

    // Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
    // event should never be fired.
    resetForIndent("<table><tr><td id=\"target\">td</td></tr></table>");
    synthesizeKey("VK_TAB", { ctrlKey: true });
    check(aDescription + "Ctrl+Tab on TD", false, false, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>",
       aDescription + "Ctrl+Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Ctrl+Tab on TD)");

    resetForIndent("<table><tr><td id=\"target\">td</td></tr></table>");
    synthesizeKey("VK_TAB", { altKey: true });
    check(aDescription + "Alt+Tab on TD", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>",
       aDescription + "Alt+Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Alt+Tab on TD)");

    resetForIndent("<table><tr><td id=\"target\">td</td></tr></table>");
    synthesizeKey("VK_TAB", { metaKey: true });
    check(aDescription + "Meta+Tab on TD", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>",
       aDescription + "Meta+Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Meta+Tab on TD)");

    resetForIndent("<table><tr><td id=\"target\">td</td></tr></table>");
    synthesizeKey("VK_TAB", { osKey: true });
    check(aDescription + "OS+Tab on TD", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><td id=\"target\">td</td></tr></tbody></table>",
       aDescription + "OS+Tab on TD");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (OS+Tab on TD)");

    // TH
    resetForIndent("<table><tr><th id=\"target\">th</th></tr></table>");
    synthesizeKey("VK_TAB", { });
    check(aDescription + "Tab on TH",
          true, true, !aIsTabbable && !aIsReadonly);
    is(aElement.innerHTML,
       aIsTabbable || aIsReadonly ?
         "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>" :
         aIsPlaintext ? "<table><tbody><tr><th id=\"target\">th\t</th></tr></tbody></table>" :
           "<table><tbody><tr><th id=\"target\">th</th></tr><tr><td style=\"vertical-align: top;\"><br></td></tr></tbody></table>",
       aDescription + "Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Tab on TH)");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab after Tab on TH",
          true, true, !aIsTabbable && !aIsReadonly && !aIsPlaintext);
    is(aElement.innerHTML,
       aIsTabbable || aIsReadonly ?
         "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>" :
         aIsPlaintext ? "<table><tbody><tr><th id=\"target\">th\t</th></tr></tbody></table>" :
           "<table><tbody><tr><th id=\"target\">th</th></tr><tr><td style=\"vertical-align: top;\"><br></td></tr></tbody></table>",
       aDescription + "Shift+Tab after Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab after Tab on TH)");

    resetForIndent("<table><tr><th id=\"target\">th</th></tr></table>");
    synthesizeKey("VK_TAB", { shiftKey: true });
    check(aDescription + "Shift+Tab on TH", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>",
       aDescription + "Shift+Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Shift+Tab on TH)");

    // Ctrl+Tab should be consumed by tabbrowser at keydown, so, keypress
    // event should never be fired.
    resetForIndent("<table><tr><th id=\"target\">th</th></tr></table>");
    synthesizeKey("VK_TAB", { ctrlKey: true });
    check(aDescription + "Ctrl+Tab on TH", false, false, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>",
       aDescription + "Ctrl+Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Ctrl+Tab on TH)");

    resetForIndent("<table><tr><th id=\"target\">th</th></tr></table>");
    synthesizeKey("VK_TAB", { altKey: true });
    check(aDescription + "Alt+Tab on TH", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>",
       aDescription + "Alt+Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Alt+Tab on TH)");

    resetForIndent("<table><tr><th id=\"target\">th</th></tr></table>");
    synthesizeKey("VK_TAB", { metaKey: true });
    check(aDescription + "Meta+Tab on TH", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>",
       aDescription + "Meta+Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (Meta+Tab on TH)");

    resetForIndent("<table><tr><th id=\"target\">th</th></tr></table>");
    synthesizeKey("VK_TAB", { osKey: true });
    check(aDescription + "OS+Tab on TH", true, true, false);
    is(aElement.innerHTML,
       "<table><tbody><tr><th id=\"target\">th</th></tr></tbody></table>",
       aDescription + "OS+Tab on TH");
    is(SpecialPowers.unwrap(fm.focusedElement), aElement,
       aDescription + "focus moved unexpectedly (OS+Tab on TH)");

    // Esc key:
    //   In all cases, esc key events are not consumed
    reset("abc");
    synthesizeKey("VK_ESCAPE", { });
    check(aDescription + "Esc", true, true, false);

    reset("abc");
    synthesizeKey("VK_ESCAPE", { shiftKey: true });
    check(aDescription + "Shift+Esc", true, true, false);

    reset("abc");
    synthesizeKey("VK_ESCAPE", { ctrlKey: true });
    check(aDescription + "Ctrl+Esc", true, true, false);

    reset("abc");
    synthesizeKey("VK_ESCAPE", { altKey: true });
    check(aDescription + "Alt+Esc", true, true, false);

    reset("abc");
    synthesizeKey("VK_ESCAPE", { metaKey: true });
    check(aDescription + "Meta+Esc", true, true, false);

    reset("abc");
    synthesizeKey("VK_ESCAPE", { osKey: true });
    check(aDescription + "OS+Esc", true, true, false);

    // typical typing tests:
    reset("");
    synthesizeKey("M", { shiftKey: true });
    check(aDescription + "M", true, true, !aIsReadonly);
    synthesizeKey("o", { });
    check(aDescription + "o", true, true, !aIsReadonly);
    synthesizeKey("z", { });
    check(aDescription + "z", true, true, !aIsReadonly);
    synthesizeKey("i", { });
    check(aDescription + "i", true, true, !aIsReadonly);
    synthesizeKey("l", { });
    check(aDescription + "l", true, true, !aIsReadonly);
    synthesizeKey("l", { });
    check(aDescription + "l", true, true, !aIsReadonly);
    synthesizeKey("a", { });
    check(aDescription + "a", true, true, !aIsReadonly);
    synthesizeKey(" ", { });
    check(aDescription + "' '", true, true, !aIsReadonly);
    is(aElement.innerHTML,
       aIsReadonly ? "" : aIsPlaintext ? "Mozilla " : "Mozilla <br>",
       aDescription + "typed \"Mozilla \"");
  }

  doTest(htmlEditor, "contenteditable=\"true\"", false, true, false);

  const nsIPlaintextEditor = SpecialPowers.Ci.nsIPlaintextEditor;
  var editor = SpecialPowers.wrap(window).
      QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor).
      getInterface(SpecialPowers.Ci.nsIWebNavigation).
      QueryInterface(SpecialPowers.Ci.nsIDocShell).editor;
  var flags = editor.flags;
  // readonly
  editor.flags = flags | nsIPlaintextEditor.eEditorReadonlyMask;
  doTest(htmlEditor, "readonly HTML editor", true, true, false);

  // non-tabbable
  editor.flags = flags & ~(nsIPlaintextEditor.eEditorAllowInteraction);
  doTest(htmlEditor, "non-tabbable HTML editor", false, false, false);

  // readonly and non-tabbable
  editor.flags =
    (flags | nsIPlaintextEditor.eEditorReadonlyMask) &
      ~(nsIPlaintextEditor.eEditorAllowInteraction);
  doTest(htmlEditor, "readonly and non-tabbable HTML editor",
         true, false, false);

  // plaintext
  editor.flags = flags | nsIPlaintextEditor.eEditorPlaintextMask;
  doTest(htmlEditor, "HTML editor but plaintext mode", false, true, true);

  // plaintext and non-tabbable
  editor.flags = (flags | nsIPlaintextEditor.eEditorPlaintextMask) &
                 ~(nsIPlaintextEditor.eEditorAllowInteraction);
  doTest(htmlEditor, "non-tabbable HTML editor but plaintext mode",
         false, false, true);


  // readonly and plaintext
  editor.flags = flags | nsIPlaintextEditor.eEditorPlaintextMask |
                         nsIPlaintextEditor.eEditorReadonlyMask;
  doTest(htmlEditor, "readonly HTML editor but plaintext mode",
         true, true, true);

  // readonly, plaintext and non-tabbable
  editor.flags = (flags | nsIPlaintextEditor.eEditorPlaintextMask |
                          nsIPlaintextEditor.eEditorReadonlyMask) &
                 ~(nsIPlaintextEditor.eEditorAllowInteraction);
  doTest(htmlEditor, "readonly and non-tabbable HTML editor but plaintext mode",
         true, false, true);

  SpecialPowers.removeSystemEventListener(window, "keypress", listener, true);
  SpecialPowers.removeSystemEventListener(window, "keypress", listener, false);

  SimpleTest.finish();
}

</script>
</body>

</html>