summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/frameSelectEvents.html
blob: 21995c86ef603ffdc1f771462a26e855f1caf4d0 (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
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
<!doctype html>
<html>
  <head>
    <title>Testing Selection Events</title>
    <script src="/tests/SimpleTest/EventUtils.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  </head>

  <body>
    <div id="normal">
      <span id="inner">A bunch of text in a span inside of a div which should be selected</span>
    </div>

    <div id="ce">
      This is a random block of text
    </div>

    <input type="text" id="input" value="XXXXXXXXXXXXXXXXXXX" width="200"> <br>

    <textarea id="textarea" width="200">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</textarea>

    <script>
      // Call the testing methods from the parent window
      var is = parent.is;
      var ok = parent.ok;

      // spin() spins the event loop for two cycles, giving time for
      // selectionchange events to be fired, and handled by our listeners.
      function spin() {
        return new Promise(function(a) {
          parent.SimpleTest.executeSoon(function() {
            parent.SimpleTest.executeSoon(a)
          });
        });
      }

      /**
       * @param {Node} node
       */
      function isProperSelectionChangeTarget(node) {
        return node === document || node === input || node === textarea;
      }

      // The main test
      parent.add_task(async function() {
        await spin();

        var selectstart = 0;
        var selectionchange = 0;
        var inputSelectionchange = 0;
        var textareaSelectionchange = 0;

        var cancel = false;
        var selectstartTarget = null;

        async function UpdateSelectEventsOnTextControlsPref({ selectstart, selectionchange }) {
          await SpecialPowers.pushPrefEnv({
            'set': [
              ['dom.select_events.textcontrols.selectstart.enabled', !!selectstart],
              ['dom.select_events.textcontrols.selectionchange.enabled', !!selectionchange],
            ]
          });
        }
        await UpdateSelectEventsOnTextControlsPref({
          selectstart: false,
          selectionchange: false,
        });

        document.addEventListener('selectstart', function(aEvent) {
          console.log("originaltarget", aEvent.originalTarget, "new", selectstartTarget);
          is(aEvent.originalTarget, selectstartTarget,
             "The original target of selectstart");
          selectstartTarget = null;

          console.log(selectstart);
          selectstart++;

          if (cancel) {
            aEvent.preventDefault();
          }
        });
        document.addEventListener('selectionchange', function(aEvent) {
          ok(isProperSelectionChangeTarget(aEvent.target),
             "The target of selectionchange should be one of document, input, or textarea");
          console.log(selectionchange);
          selectionchange++;
        });

        function elt(aId) { return document.getElementById(aId); }
        function reset() {
          selectstart = 0;
          selectionchange = 0;
          inputSelectionchange = 0;
          textareaSelectionchange = 0;
          cancel = false;
        }

        elt("input").addEventListener('selectionchange', function(aEvent) {
          is (aEvent.originalTarget, elt("input"),
              "The original target of selectionchange should be the input");
          console.log(inputSelectionchange);
          inputSelectionchange++;
        });
        elt("textarea").addEventListener('selectionchange', function(aEvent) {
          is (aEvent.originalTarget, elt("textarea"),
              "The original target of selectionchange should be the textarea");
          console.log(textareaSelectionchange);
          textareaSelectionchange++;
        });

        function checkEventCounts(
          aTestDescription,
          aSituationDescription,
          aExpectedEventCounts
        ) {
          let {
            selectstartOnDocument = 0,
            selectionchangeOnDocument = 0,
            selectionchangeOnInput = 0,
            selectionchangeOnTextarea = 0,
          } = aExpectedEventCounts;

          is(
            selectstart,
            selectstartOnDocument,
            `${
              aTestDescription
            }: "selectstart" event on the document node should be fired ${
              selectstartOnDocument
            } times ${aSituationDescription}`
          );
          is(
            selectionchange,
            selectionchangeOnDocument,
            `${
              aTestDescription
            }: "selectionchange" event on the document node should be fired ${
              selectionchangeOnDocument
            } times ${aSituationDescription}`
          );
          is(
            inputSelectionchange,
            selectionchangeOnInput,
            `${
              aTestDescription
            }: "selectionchange" event on the <input> should be fired ${
              selectionchangeOnInput
            } times ${aSituationDescription}`
          );
          is(
            textareaSelectionchange,
            selectionchangeOnTextarea,
            `${
              aTestDescription
            }: "selectionchange" event on the <textarea> should be fired ${
              selectionchangeOnTextarea
            } times ${aSituationDescription}`
          );
        }

        async function testWithSynthesizingMouse(
          aDescription,
          aElement,
          aOffset,
          aType,
          aExpectedEventCounts
        ) {
          const eventObject = aType == "click" ? {} : { type: aType };
          if (aOffset.y === undefined || aOffset.y === null) {
            aOffset.y = 10;
          }
          synthesizeMouse(aElement, aOffset.x, aOffset.y, eventObject);
          await spin();

          checkEventCounts(
            aDescription,
            `after synthesizing ${aType} at ${aOffset.x}, ${aOffset.y}`,
            aExpectedEventCounts
          );
          reset();
        }

        async function testWithSynthesizingKey(
          aDescription,
          aKey,
          aEventObject,
          aExpectedEventCounts
        ) {
          synthesizeKey(aKey, aEventObject);
          await spin();

          checkEventCounts(
            aDescription,
            `after synthesizing a key press of "${aKey}"`,
            aExpectedEventCounts
          );
          reset();
        }

        async function testWithSettingContentEditableAttribute(
          aDescription,
          aElement,
          aContentEditableValue,
          aExpectedEventCounts
        ) {
          aElement.setAttribute("contenteditable",
                                aContentEditableValue ? "true" : "false");
          await spin();

          checkEventCounts(
            aDescription,
            `after setting contenteditable attribute to ${
              aElement.getAttribute("contenteditable")
            }`,
            aExpectedEventCounts
          );
          reset();
        }

        var selection = document.getSelection();
        function isCollapsed() {
          is(selection.isCollapsed, true, "Selection is collapsed");
        }
        function isNotCollapsed() {
          is(selection.isCollapsed, false, "Selection is not collapsed");
        }

        // Make sure setting the element to contentEditable doesn't cause any selectionchange events
        await testWithSettingContentEditableAttribute(
          "Setting contenteditable attribute to true of <div> should not change selection",
          elt("ce"),
          true,
          {}
        );

        // Make sure setting the element to not be contentEditable doesn't cause any selectionchange events
        await testWithSettingContentEditableAttribute(
          'Setting contenteditable attribute to false of <div contenteditable="true"> should not change selection',
          elt("ce"),
          false,
          {}
        );

        // Now make the div contentEditable and proceed with the test
        await testWithSettingContentEditableAttribute(
          'Setting contenteditable attribute to true of <div contenteditable="false"> should not change selection',
          elt("ce"),
          true,
          {}
        );

        // Focus the contenteditable text
        await testWithSynthesizingMouse(
          'Clicking in <div contenteditable="true"> should change selection',
          elt("ce"),
          { x: 100 },
          "click",
          { selectionchangeOnDocument: 1 }
        );
        isCollapsed();

        // Move the selection to the right, this should only fire selectstart once
        selectstartTarget = elt("ce").firstChild;
        await testWithSynthesizingKey(
          'Synthesizing Shift-ArrowRight to select a character in the text node of <div contenteditable="true"> should start to select again and change selection',
          "KEY_ArrowRight",
          { shiftKey: true },
          { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();
        await testWithSynthesizingKey(
          'Synthesizing Shift-ArrowRight again to select 2 characters in the text node of <div contenteditable="true"> should change selection',
          "KEY_ArrowRight",
          { shiftKey: true },
          { selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();

        // Move it back so that the selection is empty again
        await testWithSynthesizingKey(
          'Synthesizing Shift-ArrowLeft to shrink selection in the text node of <div contenteditable="true"> should change selection',
          "KEY_ArrowLeft",
          { shiftKey: true },
          { selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();
        await testWithSynthesizingKey(
          'Synthesizing Shift-ArrowLeft again to collapse selection in the text node of <div contenteditable="true"> should change selection',
          "KEY_ArrowLeft",
          { shiftKey: true },
          { selectionchangeOnDocument: 1 }
        );
        isCollapsed();

        // Going from empty to non-empty should fire selectstart again
        selectstartTarget = elt("ce").firstChild;
        await testWithSynthesizingKey(
          'Synthesizing Shift-ArrowLeft again to select a character on the other side in the text node of <div contenteditable="true"> should start to select and change selection',
          "KEY_ArrowLeft",
          { shiftKey: true },
          { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();

        async function testWithSynthesizingMouseDrag(
          aDescription,
          aElement,
          aSelectstartTarget
        ) {
          // Select a region
          await testWithSynthesizingMouse(
            `Pressing left mouse button ${
              aDescription
            } should not start to select but should change selection`,
            aElement,
            { x: 50 },
            "mousedown",
            { selectionchangeOnDocument: 1 }
          );
          isCollapsed();

          selectstartTarget = aSelectstartTarget;
          await testWithSynthesizingMouse(
            `Dragging mouse to right to extend selection ${
              aDescription
            } should start to select and change selection`,
            aElement,
            { x: 100 },
            "mousemove",
            { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
          );
          isNotCollapsed();

          // Moving it more shouldn't trigger a start (move back to empty)
          await testWithSynthesizingMouse(
            `Dragging mouse to left to shrink selection ${
              aDescription
            } should change selection`,
            aElement,
            { x: 75 },
            "mousemove",
            { selectionchangeOnDocument: 1 }
          );
          isNotCollapsed();
          await testWithSynthesizingMouse(
            `Dragging mouse to left to collapse selection ${
              aDescription
            } should change selection`,
            aElement,
            { x: 50 },
            "mousemove",
            { selectionchangeOnDocument: 1 }
          );
          isCollapsed();

          // Wiggling the mouse a little such that it doesn't select any
          // characters shouldn't trigger a selection
          await testWithSynthesizingMouse(
            `Dragging mouse to bottom a bit ${
              aDescription
            } should not cause selection change`,
            aElement,
            { x: 50, y: 11 },
            "mousemove",
            {}
          );
          isCollapsed();

          // Moving the mouse again from an empty selection should trigger a
          // selectstart
          selectstartTarget = aSelectstartTarget;
          await testWithSynthesizingMouse(
            `Dragging mouse to left to extend selection ${
              aDescription
            } should start to select and change selection`,
            aElement,
            { x: 25 },
            "mousemove",
            { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
          );
          isNotCollapsed();

          // Releasing the mouse shouldn't do anything
          await testWithSynthesizingMouse(
            `Releasing left mouse button to stop dragging ${
              aDescription
            } should not change selection`,
            aElement,
            { x: 25 },
            "mouseup",
            {}
          );
          isNotCollapsed();

          // And neither should moving your mouse around when the mouse
          // button isn't pressed
          await testWithSynthesizingMouse(
            `Just moving mouse to right ${
              aDescription
            } should not start to select nor change selection`,
            aElement,
            { x: 50 },
            "mousemove",
            {}
          );
          isNotCollapsed();

          // Clicking in an random location should move the selection, but not perform a
          // selectstart
          await testWithSynthesizingMouse(
            `Clicking to collapse selection ${
              aDescription
            } should cause only selection change`,
            aElement,
            { x: 50 },
            "click",
            { selectionchangeOnDocument: 1 }
          );
          isCollapsed();

          // Clicking there again should do nothing
          await testWithSynthesizingMouse(
            `Clicking same position again ${
              aDescription
            } should not change selection`,
            aElement,
            { x: 50 },
            "click",
            {}
          );
          isCollapsed();

          // Selecting a region, and canceling the selectstart should mean that the
          // selection remains collapsed
          await testWithSynthesizingMouse(
            `Pressing left mouse button on different character to move caret ${
              aDescription
            } should cause only selection change`,
            aElement,
            { x: 75 },
            "mousedown",
            { selectionchangeOnDocument: 1 }
          );
          isCollapsed();
          cancel = true;
          selectstartTarget = aSelectstartTarget;
          await testWithSynthesizingMouse(
            `Moving mouse to right to extend selection but selectstart event will be prevented default ${
              aDescription
            } should start to select and change selection`,
            aElement,
            { x: 100 },
            "mousemove",
            { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
          );
          isCollapsed();
          await testWithSynthesizingMouse(
            `Releasing the left mouse button after dragging but selectstart was prevented the default ${
              aDescription
            } should not change selection`,
            aElement,
            { x: 100 },
            "mouseup",
            {}
          );
          isCollapsed();
        }

        // Should work both on normal
        await testWithSynthesizingMouseDrag(
          "on the text node in the non-editable <div>",
          elt("inner"),
          elt("inner").firstChild
        );
        // and contenteditable fields
        await testWithSynthesizingMouseDrag(
          'on the text node in the editable <div contenteditable="true">',
          elt("ce"),
          elt("ce").firstChild
        );
        // and fields with elements in them
        await testWithSynthesizingMouseDrag(
          "on the text node in the non-editable <div>'s child",
          elt("normal"),
          elt("inner").firstChild
        );

        await testWithSynthesizingMouse(
          "Clicking in the text node in the `<div>` should change selection",
          elt("inner"),
          { x: 50 },
          "click",
          { selectionchangeOnDocument: 1 }
        );
        isCollapsed();

        reset();
        // Select all should fire both selectstart and change
        selectstartTarget = document.body;
        await testWithSynthesizingKey(
          "Select All when no editor has focus should start to select and select all content",
          "a", { accelKey: true },
          { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();

        // Clear the selection
        await testWithSynthesizingMouse(
          "Clicking in the non-editable <div> should clear selection",
          elt("inner"),
          { x: 50 },
          "click",
          { selectionchangeOnDocument: 1 }
        );
        isCollapsed();

        // Even if we already have a selection
        await testWithSynthesizingMouse(
          "Pressing the left mouse button in non-editable <div> should change selection",
          elt("inner"),
          { x: 75 },
          "mousedown",
          { selectionchangeOnDocument: 1 }
        );
        isCollapsed();
        selectstartTarget = elt("inner").firstChild;
        await testWithSynthesizingMouse(
          "Dragging mouse to right to extend selection should start and change selection",
          elt("inner"),
          { x: 100 },
          "mousemove",
          { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();
        await testWithSynthesizingMouse(
          "Releasing the left mouse button should not change selection",
          elt("inner"),
          { x: 100 },
          "mouseup",
          {}
        );
        isNotCollapsed();

        selectstartTarget = document.body;
        await testWithSynthesizingKey(
          "Select All when no editor has focus should start to select and select all content (again)",
          "a",
          { accelKey: true },
          { selectstartOnDocument: 1, selectionchangeOnDocument: 1 }
        );
        isNotCollapsed();

        // Clear the selection
        await testWithSynthesizingMouse(
          "Clicking in the non-editable <div> should clear selection (again)",
          elt("inner"),
          { x: 50 },
          "click",
          {selectionchangeOnDocument: 1 }
        );
        isCollapsed();

        // Make sure that a synthesized selection change doesn't fire selectstart
        getSelection().removeAllRanges();
        await spin();
        is(
          selectstart,
          0,
          "Selection.removeAllRanges() should not cause selectstart event"
        );
        is(
          selectionchange,
          1,
          "Selection.removeAllRanges() should cause selectionchange event"
        );
        reset();
        isCollapsed();

        await (async function test_Selection_selectNode() {
          const range = document.createRange();
          range.selectNode(elt("inner"));
          getSelection().addRange(range);
          await spin();
          is(
            selectstart,
            0,
            "Selection.addRange() should not cause selectstart event"
          );
          is(
            selectionchange,
            1,
            "Selection.addRange() should cause selectionchange event"
          );
          reset();
          isNotCollapsed();
        })();

        // Change the range, without replacing
        await (async function test_Selection_getRangeAt_selectNode() {
          getSelection().getRangeAt(0).selectNode(elt("ce"));
          await spin();
          is(
            selectstart,
            0,
            "Selection.getRangeAt(0).selectNode() should not cause selectstart event"
          );
          is(
            selectionchange,
            1,
            "Selection.getRangeAt(0).selectNode() should cause selectionchange event"
          );
          reset();
          isNotCollapsed();
        })();

        // Remove the range
        getSelection().removeAllRanges();
        await spin();
        is(
          selectstart,
          0,
          "Selection.removeAllRanges() should not cause selectstart event (again)"
        );
        is(
          selectionchange,
          1,
          "Selection.removeAllRanges() should cause selectionchange event (again)"
        );
        reset();
        isCollapsed();

        for (const textControl of [elt("input"), elt("textarea")]) {
          await UpdateSelectEventsOnTextControlsPref({
            selectstart: false,
            selectionchange: false,
          });

          // Without the dom.select_events.textcontrols.enabled pref,
          // pressing the mouse shouldn't do anything.
          await testWithSynthesizingMouse(
            `Pressing the left mouse button in <${
                textControl.tagName.toLocaleLowerCase()
            }> should change selection of the document`,
            textControl,
            { x: 50 },
            "mousedown",
            {
              // XXX Bug 1721287: For some reason we fire 2 selectchange events
              // on the body when switching from the <input> to the <textarea>.
              selectionchangeOnDocument:
                document.activeElement != textControl &&
                  (document.activeElement.tagName.toLocaleLowerCase() == "input" ||
                   document.activeElement.tagName.toLocaleLowerCase() == "textarea")
                  ? 2
                  : 1,
            }
          );

          // Releasing the mouse shouldn't do anything
          await testWithSynthesizingMouse(
            `Releasing the left mouse button in <${
                textControl.tagName.toLocaleLowerCase()
            }> should not change any selection`,
            textControl,
            { x: 50 },
            "mouseup",
            {}
          );

          for (const selectstart of [1, 0]) {
            await UpdateSelectEventsOnTextControlsPref({
              selectstart,
              selectionchange: true,
            });

            const selectstartEventSetting = `selectstart in text controls is ${
              selectstart ? "enabled" : "disabled"
            }`;

            const isInput = textControl.tagName.toLocaleLowerCase() == "input";

            await testWithSynthesizingMouse(
              `Pressing the left mouse button in <${
                textControl.tagName.toLocaleLowerCase()
              }> should change selection (${selectstartEventSetting})`,
              textControl,
              { x: 40 },
              "mousedown",
              {
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );

            selectstartTarget = textControl;
            await testWithSynthesizingMouse(
              `Dragging mouse to right to extend selection in <${
                textControl.tagName.toLocaleLowerCase()
              }> should start to select and change selection (${
                selectstartEventSetting
              })`,
              textControl,
              { x: 100 },
              "mousemove",
              {
                selectstartOnDocument: selectstart,
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );

            // Moving it more shouldn't trigger a start (move back to empty)
            await testWithSynthesizingMouse(
              `Dragging mouse to left to shrink selection in <${
                textControl.tagName.toLocaleLowerCase()
              }> should change selection (${selectstartEventSetting})`,
              textControl,
              { x: 75 },
              "mousemove",
              {
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );
            await testWithSynthesizingMouse(
              `Dragging mouse to left to collapse selection in <${
                textControl.tagName.toLocaleLowerCase()
              }> should change selection (${selectstartEventSetting})`,
              textControl,
              { x: 40 },
              "mousemove",
              {
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );

            // Wiggling the mouse a little such that it doesn't select any
            // characters shouldn't trigger a selection
            await testWithSynthesizingMouse(
              `Pressing the left mouse button at caret in <${
                textControl.tagName.toLocaleLowerCase()
              }> should not change selection (${selectstartEventSetting})`,
              textControl,
              {
                x: 40,
                y: 11,
              },
              "mousemove",
              {}
            );

            // Moving the mouse again from an empty selection should trigger a
            // selectstart
            selectstartTarget = textControl;
            await testWithSynthesizingMouse(
              `Dragging mouse to left to extend selection in <${
                textControl.tagName.toLocaleLowerCase()
              }> should start to select and change selection (${
                selectstartEventSetting
              })`,
              textControl,
              { x: 25 },
              "mousemove",
              {
                selectstartOnDocument: selectstart,
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );

            // Releasing the mouse shouldn't do anything
            await testWithSynthesizingMouse(
              `Releasing the left mouse button in <${
                textControl.tagName.toLocaleLowerCase()
              }> should not change selection (${selectstartEventSetting})`,
              textControl,
              { x: 25 },
              "mouseup",
              {}
            );

            // And neither should moving your mouse around when the mouse
            // button isn't pressed
            await testWithSynthesizingMouse(
              `Just moving mouse to right in <${
                textControl.tagName.toLocaleLowerCase()
              }> should not start to select nor change selection (${
                selectstartEventSetting
              })`,
              textControl,
              { x: 50 },
              "mousemove",
              {}
            );

            // Clicking in an random location should move the selection, but
            // not perform a selectstart
            await testWithSynthesizingMouse(
              `Clicking in <${
                textControl.tagName.toLocaleLowerCase()
              }> should change selection, but should not start selection (${
                selectstartEventSetting
              })`,
              textControl,
              { x: 50 },
              "click",
              {
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );

            // Clicking there again should do nothing
            await testWithSynthesizingMouse(
              `Clicking at caret in <${
                textControl.tagName.toLocaleLowerCase()
              }> should not change selection (${selectstartEventSetting})`,
              textControl,
              { x: 50 },
              "click",
              {}
            );

            // Selecting a region, and canceling the selectstart should mean that the
            // selection remains collapsed
            await testWithSynthesizingMouse(
              `Pressing the left mouse button at different character in <${
                textControl.tagName.toLocaleLowerCase()
              }> should change selection (${selectstartEventSetting})`,
              textControl,
              { x: 75 },
              "mousedown",
              {
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );
            cancel = true;
            selectstartTarget = textControl;
            await testWithSynthesizingMouse(
              `Dragging mouse to right to extend selection in <${
                textControl.tagName.toLocaleLowerCase()
              }> but the default of selectstart is prevented should cause selectstart and selectionchange events (${
                selectstartEventSetting
              })`,
              textControl,
              { x: 100 },
              "mousemove",
              {
                selectstartOnDocument: selectstart,
                selectionchangeOnDocument: 1,
                selectionchangeOnInput: isInput ? 1 : 0,
                selectionchangeOnTextarea: isInput ? 0 : 1,
              }
            );
            await testWithSynthesizingMouse(
              `Releasing the left mouse button in <${
                textControl.tagName.toLocaleLowerCase()
              }> should not cause changing selection (${selectstartEventSetting})`,
              textControl,
              { x: 100 },
              "mouseup",
              {}
            );
          }
        }

        // Marking the input and textarea as display: none and then as visible again
        // shouldn't trigger any changes, although the nodes will be re-framed
        for (const textControl of [elt("input"), elt("textarea")]) {
          await (async function test_set_display_of_text_control_to_none() {
            textControl.setAttribute("style", "display: none;");
            await spin();
            checkEventCounts(
              `Setting display of <${
                textControl.tagName.toLocaleLowerCase()
              }> to none`,
              "",
              {}
            );
            reset();
          })();

          await (async function test_remove_display_none_of_text_control() {
            textControl.setAttribute("style", "");
            await spin();
            checkEventCounts(
              `Removing display:none of <${
                textControl.tagName.toLocaleLowerCase()
              }>`,
              "",
              {}
            );
            reset();
          })();
        }

        // When selection is at the end of contentEditable's content,
        // clearing the content should trigger selection events.
        await (async function test_removing_contenteditable() {
          const savedContent = elt("ce").innerHTML;
          document.getSelection().setBaseAndExtent(elt("ce"), 1, elt("ce"), 1);
          await spin();
          reset();

          elt("ce").firstChild.remove();
          await spin();
          checkEventCounts(
            'Removing <div contenteditable="true"> from the DOM tree',
            "",
            { selectionchangeOnDocument: 1 }
          );

          elt("ce").innerHTML = savedContent;
          await spin();
          reset();
        })();
      });
    </script>
  </body>
</html>