summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/window_clipboard_events.html
blob: ee88cc5fa0c2c526c72f1be440443902ebc09fdb (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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for Clipboard Events</title>
  <script>
    var SimpleTest = opener.SimpleTest;
    var SpecialPowers = opener.SpecialPowers;
    var ok = opener.ok;
    var is = opener.is;
    var isnot = opener.isnot;
    var todo = opener.todo;
    var todo_is = opener.todo_is;
    var add_task = opener.add_task;
  </script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<body>
<p id="display"></p>
<div id="content" style="border: 3px solid black; padding: 3em;">CONTENT TEXT<input id="content-input" value="INPUT TEXT"></div>
<img id="image" src="image_50.png">
<button id="button">Button</button>

<div id="syntheticSpot" oncut="compareSynthetic(event, 'cut')"
                        oncopy="compareSynthetic(event, 'copy')"
                        onpaste="compareSynthetic(event, 'paste')">Spot</div>

<div id="contenteditableContainer"></div>

<pre id="test">
<script class="testbody" type="text/javascript">
var content = document.getElementById("content");
var contentInput = document.getElementById("content-input");
var contenteditableContainer = document.getElementById("contenteditableContainer");
var clipboardInitialValue = "empty";

var cachedCutData, cachedCopyData, cachedPasteData;

ok(SpecialPowers.getBoolPref("dom.events.dataTransfer.protected.enabled"),
   "The following require dom.events.dataTransfer.protected.enabled is enabled");
isnot(typeof window.onbeforeinput, "undefined",
      "The following tests require onbeforeinput attribute");

// Before each test function is run, the clipboard is initialized
// to clipboardInitialValue, and the contents of div#content are
// set as the window's selection.

add_task(async function initialize_for_tests() {
  disableNonTestMouseEvents(true);

  await SimpleTest.promiseFocus(window);

  // Test that clearing and reading the clipboard works.  A random number
  // is used to make sure that leftover clipboard values from a previous
  // test run don't cause a false-positive test.
  try {
    var cb_text = "empty_" + Math.random();
    await putOnClipboard(cb_text, () => { setClipboardText(cb_text) },
                         "Failed to initial set/get clipboard text");
  } catch (e) {
    ok(false, e.toString());
  }
});

var eventListeners = [];

// Note that don't use EventTarget.addEventListener directly in this file
// because if it's remained in next test, it makes developers harder to
// investigate such oranges. addEventListenerTo cleans up when `reset()`
// is called by first of next test and then, all event listeners including
// marked as "once" are removed automatically.
function addEventListenerTo(aEventTarget, aEventType, aListener, aOptions) {
  eventListeners.push({
    target: aEventTarget,
    type: aEventType,
    listener: aListener,
    options: aOptions
  });
  aEventTarget.addEventListener(aEventType, aListener, aOptions);
}

async function reset() {
  [content, contentInput, document, document.documentElement].forEach(eventTarget => {
    ["oncut", "oncopy", "onpaste", "oninput", "onbeforeinput"].forEach(attr => {
      eventTarget[attr] = null;
    });
  });
  eventListeners.forEach(data => {
    data.target.removeEventListener(data.type, data.listener, data.options);
  });
  eventListeners = [];

  // Init clipboard
  await putOnClipboard(clipboardInitialValue,
                       () => { setClipboardText(clipboardInitialValue) },
                       "reset clipboard");

  // Reset value of editors.
  contentInput.value = "INPUT TEXT";
  contenteditableContainer.innerHTML = "";
}

function getClipboardText() {
  return SpecialPowers.getClipboardData("text/plain");
}

function getHTMLEditor() {
  let editingSession = SpecialPowers.wrap(window).docShell.editingSession;
  if (!editingSession) {
    return null;
  }
  let editor = editingSession.getEditorForWindow(window);
  if (!editor) {
    return null;
  }
  return editor.QueryInterface(SpecialPowers.Ci.nsIHTMLEditor);
}

async function putOnClipboard(expected, operationFn, desc, type) {
  try {
    await SimpleTest.promiseClipboardChange(expected, operationFn, type, 1000);
  } catch (e) {
    throw `Failed "${desc}" due to "${e.toString()}"`
  }
}

async function wontPutOnClipboard(unexpectedData, operationFn, desc, type) {
  try {
    // SimpleTest.promiseClipboardChange() doesn't throw exception when
    // it unexpectedly succeeds to copy something.  Therefore, we need
    // to throw an exception by ourselves.
    await SimpleTest.promiseClipboardChange(null, operationFn, type, 300, true, false)
                    .then(aData => {
                      if (aData == unexpectedData) {
                        throw `Failed "${desc}", the clipboard data is modified to "${aData.toString()}"`;
                      }
                    });
  } catch (e) {
    throw `Failed "${desc}" due to "${e.toString()}"`
  }
}

function setClipboardText(text) {
  var helper = SpecialPowers.Cc["@mozilla.org/widget/clipboardhelper;1"]
    .getService(SpecialPowers.Ci.nsIClipboardHelper);
  helper.copyString(text);
}

function selectContentDiv() {
  // Set selection
  var selection = window.getSelection();
  selection.removeAllRanges();
  selection.selectAllChildren(content);
}

function selectContentInput() {
  contentInput.focus();
  contentInput.select();
}

add_task(async function test_dom_oncopy() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncopy event handler, fire copy.  Ensure that the event
  // handler was called, and the clipboard contents have set to CONTENT TEXT.
  // Test firing oncopy event on ctrl-c:
  selectContentDiv();

  var oncopy_fired = false;
  content.oncopy = function() { oncopy_fired = true; };
  try {
    await putOnClipboard("CONTENT TEXT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy on DOM element set clipboard correctly");
    ok(oncopy_fired, "copy event firing on DOM element");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_dom_oncut() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncut event handler, fire cut.  Ensure that the event handler
  // was called.  The <div> doesn't handle a cut, so ensure that the
  // clipboard text shouldn't be "CONTENT TEXT".
  selectContentDiv();
  var oncut_fired = false;
  content.oncut = function() { oncut_fired = true; };
  try {
    await wontPutOnClipboard("CONTENT TEXT", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "cut on DOM element set clipboard correctly");
    ok(oncut_fired, "cut event firing on DOM element")
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_dom_onpaste() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an onpaste event handler, fire paste.  Ensure that the event
  // handler was called.
  selectContentDiv();
  var onpaste_fired = false;
  content.onpaste = function() { onpaste_fired = true; };
  synthesizeKey("v", {accelKey: 1});
  ok(onpaste_fired, "paste event firing on DOM element");
});

add_task(async function test_dom_oncopy_abort() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncopy event handler that aborts the copy, and fire the copy
  // event.  Ensure that the event handler was fired, and the clipboard
  // contents have not been modified.
  selectContentDiv();
  var oncopy_fired = false;
  content.oncopy = function() { oncopy_fired = true; return false; };
  try {
    await wontPutOnClipboard("CONTENT TEXT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "aborted copy on DOM element did not modify clipboard");
    ok(oncopy_fired, "copy event (to-be-cancelled) firing on DOM element");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_oncopy() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncopy event handler, fire copy.  Ensure that the event
  // handler was called, and the clipboard contents have been set to 'PUT TE',
  // which is the part that is selected below.
  selectContentInput();
  contentInput.focus();
  contentInput.setSelectionRange(2, 8);

  let oncopy_fired = false;
  let onbeforeinput_fired = false;
  let oninput_fired = false;
  contentInput.oncopy = () => { oncopy_fired = true; };
  contentInput.onbeforeinput = () => { onbeforeinput = true; };
  contentInput.oninput = () => { oninput_fired = true; };
  try {
    await putOnClipboard("PUT TE", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy on plaintext editor set clipboard correctly");
    ok(oncopy_fired, "copy event firing on plaintext editor");
    ok(!onbeforeinput_fired, "beforeinput event shouldn't be fired on plaintext editor by copy");
    ok(!oninput_fired, "input event shouldn't be fired on plaintext editor by copy");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_oncut() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncut event handler, and fire cut.  Ensure that the event
  // handler was fired, the clipboard contains the INPUT TEXT, and
  // that the input itself is empty.
  selectContentInput();
  let oncut_fired = false;
  let beforeInputEvents = [];
  let inputEvents = [];
  contentInput.oncut = () => { oncut_fired = true; };
  contentInput.onbeforeinput = (aEvent) => { beforeInputEvents.push(aEvent); }
  contentInput.oninput = (aEvent) => { inputEvents.push(aEvent); }
  try {
    await putOnClipboard("INPUT TEXT", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "cut on plaintext editor set clipboard correctly");
    ok(oncut_fired, "cut event firing on plaintext editor");
    is(beforeInputEvents.length, 1, '"beforeinput" event should be fired once by cut');
    if (beforeInputEvents.length) {
      is(beforeInputEvents[0].inputType, "deleteByCut", '"inputType" of "beforeinput" event should be "deleteByCut"');
      is(beforeInputEvents[0].cancelable, true, '"beforeinput" event for "deleteByCut" should be cancelable');
      is(beforeInputEvents[0].data, null, '"data" of "beforeinput" event for "deleteByCut" should be null');
      is(beforeInputEvents[0].dataTransfer, null, '"dataTransfer" of "beforeinput" event for "deleteByCut" should be null');
      is(beforeInputEvents[0].getTargetRanges().length, 0, 'getTargetRanges() of "beforeinput" event for "deleteByCut" should return empty array');
    }
    is(inputEvents.length, 1, '"input" event should be fired once by cut');
    if (inputEvents.length) {
      is(inputEvents[0].inputType, "deleteByCut", '"inputType" of "input" event should be "deleteByCut"');
      is(inputEvents[0].cancelable, false, '"input" event for "deleteByCut" should not be cancelable');
      is(inputEvents[0].data, null, '"data" of "input" event for "deleteByCut" should be null');
      is(inputEvents[0].dataTransfer, null, '"dataTransfer" of "input" event for "deleteByCut" should be null');
      is(inputEvents[0].getTargetRanges().length, 0, 'getTargetRanges() of "input" event for "deleteByCut" should return empty array');
    }
    is(contentInput.value, "",
      "cut on plaintext editor emptied editor");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_onpaste() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an onpaste event handler, and fire paste.  Ensure that the event
  // handler was fired, the clipboard contents didn't change, and that the
  // input value did change (ie. paste succeeded).
  selectContentInput();
  let onpaste_fired = false;
  let beforeInputEvents = [];
  let inputEvents = [];
  contentInput.onpaste = () => { onpaste_fired = true; };
  contentInput.onbeforeinput = (aEvent) => { beforeInputEvents.push(aEvent); }
  contentInput.oninput = (aEvent) => { inputEvents.push(aEvent); }

  synthesizeKey("v", {accelKey: 1});
  ok(onpaste_fired, "paste event firing on plaintext editor");
  is(getClipboardText(), clipboardInitialValue,
    "paste on plaintext editor did not modify clipboard contents");
  is(beforeInputEvents.length, 1, '"beforeinput" event should be fired once by paste');
  if (beforeInputEvents.length) {
    is(beforeInputEvents[0].inputType, "insertFromPaste", '"inputType" of "beforeinput" event should be "insertFromPaste"');
    is(beforeInputEvents[0].cancelable, true, '"beforeinput" event for "insertFromPaste" should be cancelable');
    is(beforeInputEvents[0].data, clipboardInitialValue, `"data" of "beforeinput" event for "insertFromPaste" should be "${clipboardInitialValue}"`);
    is(beforeInputEvents[0].dataTransfer, null, '"dataTransfer" of "beforeinput" event for "insertFromPaste" should be null');
    is(beforeInputEvents[0].getTargetRanges().length, 0, 'getTargetRanges() of "beforeinput" event for "insertFromPaste" should return empty array');
  }
  is(inputEvents.length, 1, '"input" event should be fired once by paste');
  if (inputEvents.length) {
    is(inputEvents[0].inputType, "insertFromPaste", '"inputType" of "input" event should be "insertFromPaste"');
    is(inputEvents[0].cancelable, false, '"input" event for "insertFromPaste" should not be cancelable');
    is(inputEvents[0].data, clipboardInitialValue, `"data" of "input" event for "insertFromPaste" should be "${clipboardInitialValue}"`);
    is(inputEvents[0].dataTransfer, null, '"dataTransfer" of "input" event for "insertFromPaste" should be null');
    is(inputEvents[0].getTargetRanges().length, 0, 'getTargetRanges() of "input" event for "insertFromPaste" should return empty array');
  }
  is(contentInput.value, clipboardInitialValue,
    "paste on plaintext editor did modify editor value");
});

add_task(async function test_input_oncopy_abort() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncopy event handler, fire copy.  Ensure that the event
  // handler was called, and that the clipboard value did NOT change.
  selectContentInput();
  let oncopy_fired = false;
  contentInput.oncopy = () => { oncopy_fired = true; return false; };
  contentInput.onbeforeinput = () => {
    ok(false, '"beforeinput" event should not be fired by copy but canceled');
  };
  contentInput.oninput = function() {
    ok(false, '"input" event should not be fired by copy but canceled');
  };
  try {
    await wontPutOnClipboard("CONTENT TEXT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "aborted copy on plaintext editor did not modify clipboard");
    ok(oncopy_fired, "copy event (to-be-cancelled) firing on plaintext editor");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_oncut_abort() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncut event handler, and fire cut.  Ensure that the event
  // handler was fired, the clipboard contains the INPUT TEXT, and
  // that the input itself is empty.
  selectContentInput();
  let oncut_fired = false;
  contentInput.oncut = () => { oncut_fired = true; return false; };
  contentInput.onbeforeinput = () => {
    ok(false, '"beforeinput" event should not be fired by cut but canceled by "cut" event listener');
  };
  contentInput.oninput = () => {
    ok(false, '"input" event should not be fired by cut but canceled by "cut" event listener');
  };
  try {
    await wontPutOnClipboard("CONTENT TEXT", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "aborted cut on plaintext editor did not modify clipboard");
    ok(oncut_fired, "cut event (to-be-cancelled) firing on plaintext editor");
    is(contentInput.value, "INPUT TEXT",
      "aborted cut on plaintext editor did not modify editor contents");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_oncut_beforeinput_abort() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an oncut event handler, and fire cut.  Ensure that the event
  // handler was fired, the clipboard contains the INPUT TEXT, and
  // that the input itself is empty.
  selectContentInput();
  let oncut_fired = false;
  let beforeInputEvents = [];
  let inputEvents = [];
  contentInput.oncut = () => { oncut_fired = true; };
  contentInput.onbeforeinput = (aEvent) => { beforeInputEvents.push(aEvent); aEvent.preventDefault(); }
  contentInput.oninput = (aEvent) => { inputEvents.push(aEvent); }
  try {
    await putOnClipboard("INPUT TEXT", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "cut on plaintext editor set clipboard correctly");
    ok(oncut_fired, "cut event firing on plaintext editor");
    is(beforeInputEvents.length, 1, '"beforeinput" event should be fired once by cut');
    if (beforeInputEvents.length) {
      is(beforeInputEvents[0].inputType, "deleteByCut", '"inputType" of "beforeinput" event should be "deleteByCut"');
      is(beforeInputEvents[0].cancelable, true, '"beforeinput" event for "deleteByCut" should be cancelable');
      is(beforeInputEvents[0].data, null, '"data" of "beforeinput" event for "deleteByCut" should be null');
      is(beforeInputEvents[0].dataTransfer, null, '"dataTransfer" of "beforeinput" event for "deleteByCut" should be null');
      is(beforeInputEvents[0].getTargetRanges().length, 0, 'getTargetRanges() of "beforeinput" event for "deleteByCut" should return empty array');
    }
    is(inputEvents.length, 0, '"input" event should not be fired by cut if "beforeinput" event is canceled');
    is(contentInput.value, "INPUT TEXT",
      'cut on plaintext editor should not change editor since "beforeinput" event was canceled');
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_onpaste_abort() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an onpaste event handler, and fire paste.  Ensure that the event
  // handler was fired, the clipboard contents didn't change, and that the
  // input value did change (ie. paste succeeded).
  selectContentInput();
  let onpaste_fired = false;
  contentInput.onpaste = () => { onpaste_fired = true; return false; };
  contentInput.onbeforeinput = () => {
    ok(false, '"beforeinput" event should not be fired by paste but canceled');
  };
  contentInput.oninput = () => {
    ok(false, '"input" event should not be fired by paste but canceled');
  };
  synthesizeKey("v", {accelKey: 1});
  ok(onpaste_fired,
    "paste event (to-be-cancelled) firing on plaintext editor");
  is(getClipboardText(), clipboardInitialValue,
    "aborted paste on plaintext editor did not modify clipboard");
  is(contentInput.value, "INPUT TEXT",
    "aborted paste on plaintext editor did not modify modified editor value");
});

add_task(async function test_input_onpaste_beforeinput_abort() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Setup an onpaste event handler, and fire paste.  Ensure that the event
  // handler was fired, the clipboard contents didn't change, and that the
  // input value did change (ie. paste succeeded).
  selectContentInput();
  let onpaste_fired = false;
  let beforeInputEvents = [];
  let inputEvents = [];
  contentInput.onpaste = () => { onpaste_fired = true; };
  contentInput.onbeforeinput = (aEvent) => { beforeInputEvents.push(aEvent); aEvent.preventDefault(); }
  contentInput.oninput = (aEvent) => { inputEvents.push(aEvent); }

  synthesizeKey("v", {accelKey: 1});
  ok(onpaste_fired, "paste event firing on plaintext editor");
  is(getClipboardText(), clipboardInitialValue,
    "paste on plaintext editor did not modify clipboard contents");
  is(beforeInputEvents.length, 1, '"beforeinput" event should be fired once by paste');
  if (beforeInputEvents.length) {
    is(beforeInputEvents[0].inputType, "insertFromPaste", '"inputType" of "beforeinput" event should be "insertFromPaste"');
    is(beforeInputEvents[0].cancelable, true, '"beforeinput" event for "insertFromPaste" should be cancelable');
    is(beforeInputEvents[0].data, clipboardInitialValue, `"data" of "beforeinput" event for "insertFromPaste" should be "${clipboardInitialValue}"`);
    is(beforeInputEvents[0].dataTransfer, null, '"dataTransfer" of "beforeinput" event for "insertFromPaste" should be null');
    is(beforeInputEvents[0].getTargetRanges().length, 0, 'getTargetRanges() of "beforeinput" event for "insertFromPaste" should return empty array');
  }
  is(inputEvents.length, 0, '"input" event should not be fired by paste when "beforeinput" is canceled');
  is(contentInput.value, "INPUT TEXT",
    "paste on plaintext editor did modify editor value");
});

add_task(async function test_input_cut_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Cut using event.dataTransfer. The event is not cancelled so the default
  // cut should occur
  selectContentInput();
  contentInput.oncut = function(event) {
    ok(event instanceof ClipboardEvent, "cut event is a ClipboardEvent");
    ok(event.clipboardData instanceof DataTransfer, "cut event dataTransfer is a DataTransfer");
    is(event.target, contentInput, "cut event target");
    is(SpecialPowers.wrap(event.clipboardData).mozItemCount, 0, "cut event mozItemCount");
    is(event.clipboardData.getData("text/plain"), "", "cut event getData");
    event.clipboardData.setData("text/plain", "This is some dataTransfer text");
    cachedCutData = event.clipboardData;
  };
  try {
    await putOnClipboard("INPUT TEXT", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "cut using dataTransfer on plaintext editor set clipboard correctly");
    is(contentInput.value, "",
      "cut using dataTransfer on plaintext editor cleared input");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_cut_abort_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Cut using event.dataTransfer but cancel the event. The data should be
  // put on the clipboard but since we don't modify the input value, the input
  // should have the same value.
  selectContentInput();
  contentInput.oncut = function(event) {
    event.clipboardData.setData("text/plain", "Cut dataTransfer text");
    return false;
  };
  try {
    await putOnClipboard("Cut dataTransfer text", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "aborted cut using dataTransfer on plaintext editor set clipboard correctly");
    is(contentInput.value, "INPUT TEXT",
      "aborted cut using dataTransfer on plaintext editor did not modify input");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_copy_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Copy using event.dataTransfer
  selectContentInput();
  contentInput.oncopy = function(event) {
    ok(event instanceof ClipboardEvent, "copy event is a ClipboardEvent");
    ok(event.clipboardData instanceof DataTransfer, "copy event dataTransfer is a DataTransfer");
    is(event.target, contentInput, "copy event target");
    is(SpecialPowers.wrap(event.clipboardData).mozItemCount, 0, "copy event mozItemCount");
    is(event.clipboardData.getData("text/plain"), "", "copy event getData");
    event.clipboardData.setData("text/plain", "Copied dataTransfer text");
    cachedCopyData = event.clipboardData;
  };
  try {
    await putOnClipboard("INPUT TEXT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy using dataTransfer on plaintext editor set clipboard correctly");
    is(contentInput.value, "INPUT TEXT",
      "copy using dataTransfer on plaintext editor did not modify input");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_copy_abort_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Copy using event.dataTransfer but cancel the event.
  selectContentInput();
  contentInput.oncopy = function(event) {
    event.clipboardData.setData("text/plain", "Copy dataTransfer text");
    return false;
  };
  try {
    await putOnClipboard("Copy dataTransfer text", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "aborted copy using dataTransfer on plaintext editor set clipboard correctly");
    is(contentInput.value, "INPUT TEXT",
      "aborted copy using dataTransfer on plaintext editor did not modify input");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_input_paste_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Paste using event.dataTransfer
  selectContentInput();
  contentInput.onpaste = function(event) {
    ok(event instanceof ClipboardEvent, "paste event is an ClipboardEvent");
    ok(event.clipboardData instanceof DataTransfer, "paste event dataTransfer is a DataTransfer");
    is(event.target, contentInput, "paste event target");
    is(SpecialPowers.wrap(event.clipboardData).mozItemCount, 1, "paste event mozItemCount");
    is(event.clipboardData.getData("text/plain"), clipboardInitialValue, "paste event getData");
    cachedPasteData = event.clipboardData;
  };
  synthesizeKey("v", {accelKey: 1});
  is(getClipboardText(), clipboardInitialValue,
    "paste using dataTransfer on plaintext editor did not modify clipboard contents");
  is(contentInput.value, clipboardInitialValue,
    "paste using dataTransfer on plaintext editor modified input");
});

add_task(async function test_input_paste_abort_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Paste using event.dataTransfer but cancel the event
  selectContentInput();
  contentInput.onpaste = function(event) {
    is(event.clipboardData.getData("text/plain"), clipboardInitialValue, "get data on aborted paste");
    contentInput.value = "Alternate Paste";
    return false;
  };
  synthesizeKey("v", {accelKey: 1});
  is(getClipboardText(), clipboardInitialValue,
     "aborted paste using dataTransfer on plaintext editor did not modify clipboard contents");
  is(contentInput.value, "Alternate Paste",
     "aborted paste using dataTransfer on plaintext editor modified input");
});

add_task(async function test_input_copypaste_dataTransfer_multiple() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Cut several types of data and paste it again
  contentInput.value = "This is a line of text";
  contentInput.oncopy = function(event) {
    var cd = event.clipboardData;
    cd.setData("text/plain", "would be a phrase");

    var exh = false;
    try { SpecialPowers.wrap(cd).mozSetDataAt("text/plain", "Text", 1); } catch (ex) { exh = true; }
    ok(exh, "exception occured mozSetDataAt 1");
    exh = false;
    try { SpecialPowers.wrap(cd).mozTypesAt(1); } catch (ex) { exh = true; }
    ok(exh, "exception occured mozTypesAt 1");
    exh = false;
    try { SpecialPowers.wrap(cd).mozGetDataAt("text/plain", 1); } catch (ex) { exh = true; }
    ok(exh, "exception occured mozGetDataAt 1");
    exh = false;
    try { cd.mozClearDataAt("text/plain", 1); } catch (ex) { exh = true; }
    ok(exh, "exception occured mozClearDataAt 1");

    cd.setData("text/x-moz-url", "http://www.mozilla.org");
    SpecialPowers.wrap(cd).mozSetDataAt("text/x-custom", "Custom Text with \u0000 null", 0);
    is(SpecialPowers.wrap(cd).mozItemCount, 1, "mozItemCount after set multiple types");
    return false;
  };

  try {
    selectContentInput();

    await putOnClipboard("would be a phrase", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy multiple types text");
    contentInput.oncopy = null;  // XXX Not sure why this is required...
  } catch (e) {
    ok(false, e.toString());
    return;
  }

  contentInput.setSelectionRange(5, 14);

  contentInput.onpaste = function(event) {
    var cd = event.clipboardData;
    is(SpecialPowers.wrap(cd).mozItemCount, 1, "paste after copy multiple types mozItemCount");
    is(cd.getData("text/plain"), "would be a phrase", "paste text/plain multiple types");

    // Firefox for Android's clipboard code doesn't handle x-moz-url. Therefore
    // disabling the following test. Enable this once bug #840101 is fixed.
    if (!navigator.appVersion.includes("Android")) {
      is(cd.getData("text/x-moz-url"), "http://www.mozilla.org", "paste text/x-moz-url multiple types");
      is(cd.getData("text/x-custom"), "Custom Text with \u0000 null", "paste text/custom multiple types");
    } else {
      is(cd.getData("text/x-custom"), "", "paste text/custom multiple types");
    }

    is(cd.getData("application/x-moz-custom-clipdata"), "", "application/x-moz-custom-clipdata is not present");

    exh = false;
    try { cd.setData("application/x-moz-custom-clipdata", "Some Data"); } catch (ex) { exh = true; }
    ok(exh, "exception occured setData with application/x-moz-custom-clipdata");

    exh = false;
    try { cd.setData("text/plain", "Text on Paste"); } catch (ex) { exh = true; }
    ok(exh, "exception occured setData on paste");

    is(cd.getData("text/plain"), "would be a phrase", "text/plain data unchanged");
  };
  synthesizeKey("v", {accelKey: 1});
  is(contentInput.value, "This would be a phrase of text",
    "default paste after copy multiple types");
});

add_task(async function test_input_copy_button_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Copy using event.dataTransfer when a button is focused.
  var button = document.getElementById("button");
  button.focus();
  button.oncopy = function(event) {
    ok(false, "should not be firing copy event on button");
    return false;
  };
  try {
    // copy should not occur here because buttons don't have any controller
    // for the copy command
    await wontPutOnClipboard("", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "Accel-C on the `<button>` shouldn't modify the clipboard data");
    ok(true, "Accel-C on the <button> shouldn't modify the clipboard data");
  } catch (e) {
    ok(false, e.toString());
  }

  try {
    selectContentDiv();

    await putOnClipboard("CONTENT TEXT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "Accel-C with selecting the content <div> should modify the clipboard data with text in it");
  } catch (e) {
    ok(false, e.toString());
  }
});

add_task(async function test_eventspref_disabled() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Disable clipboard events
  try {
    await SpecialPowers.pushPrefEnv({
      set: [['dom.event.clipboardevents.enabled', false]]
    });

    var event_fired = false;
    var input_data = undefined;
    contentInput.oncut = function() { event_fired = true; };
    contentInput.oncopy = function() { event_fired = true; };
    contentInput.onpaste = function() { event_fired = true; };
    contentInput.oninput = function(event) { input_data = event.data; };

    selectContentInput();
    contentInput.setSelectionRange(1, 4);

    await putOnClipboard("NPU", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "cut changed clipboard when preference is disabled");
    is(contentInput.value, "IT TEXT", "cut changed text when preference is disabled");
    ok(!event_fired, "cut event did not fire when preference is disabled");
    is(input_data, null, "cut should cause input event whose data value is null");

    event_fired = false;
    input_data = undefined;
    contentInput.setSelectionRange(3, 6);
    await putOnClipboard("TEX", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy changed clipboard when preference is disabled");
    ok(!event_fired, "copy event did not fire when preference is disabled")
    is(input_data, undefined, "copy shouldn't cause input event");

    event_fired = false;
    contentInput.setSelectionRange(0, 2);
    synthesizeKey("v", {accelKey: 1});
    is(contentInput.value, "TEX TEXT", "paste changed text when preference is disabled");
    ok(!event_fired, "paste event did not fire when preference is disabled");
    is(input_data, "",
       "paste should cause input event but whose data value should be empty string if clipboard event is disabled");
  } catch (e) {
    ok(false, e.toString());
  } finally {
    await SpecialPowers.popPrefEnv();
  }
});

let expectedData = [];

// Check to make that synthetic events do not change the clipboard
add_task(async function test_synthetic_events() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  let syntheticSpot = document.getElementById("syntheticSpot");

  // No dataType specified
  let event = new ClipboardEvent("cut", { data: "something" });
  expectedData = { type: "cut", data: null }
  compareSynthetic(event, "before");
  syntheticSpot.dispatchEvent(event);
  ok(expectedData.eventFired, "cut event fired");
  compareSynthetic(event, "after");

  event = new ClipboardEvent("cut", { dataType: "text/plain", data: "something" });
  expectedData = { type: "cut", dataType: "text/plain", data: "something" }
  compareSynthetic(event, "before");
  syntheticSpot.dispatchEvent(event);
  ok(expectedData.eventFired, "cut event fired");
  compareSynthetic(event, "after");

  event = new ClipboardEvent("copy", { dataType: "text/plain", data: "something" });
  expectedData = { type: "copy", dataType: "text/plain", data: "something" }
  compareSynthetic(event, "before");
  syntheticSpot.dispatchEvent(event);
  ok(expectedData.eventFired, "copy event fired");
  compareSynthetic(event, "after");

  event = new ClipboardEvent("copy", { dataType: "text/plain" });
  expectedData = { type: "copy", dataType: "text/plain", data: "" }
  compareSynthetic(event, "before");
  syntheticSpot.dispatchEvent(event);
  ok(expectedData.eventFired, "copy event fired");
  compareSynthetic(event, "after");

  event = new ClipboardEvent("paste", { dataType: "text/plain", data: "something" });
  expectedData = { type: "paste", dataType: "text/plain", data: "something" }
  compareSynthetic(event, "before");
  syntheticSpot.dispatchEvent(event);
  ok(expectedData.eventFired, "paste event fired");
  compareSynthetic(event, "after");

  event = new ClipboardEvent("paste", { dataType: "application/unknown", data: "unknown" });
  expectedData = { type: "paste", dataType: "application/unknown", data: "unknown" }
  compareSynthetic(event, "before");
  syntheticSpot.dispatchEvent(event);
  ok(expectedData.eventFired, "paste event fired");
  compareSynthetic(event, "after");
});

function compareSynthetic(event, eventtype) {
  let step = (eventtype == "cut" || eventtype == "copy" || eventtype == "paste") ? "during" : eventtype;
  if (step == "during") {
    is(eventtype, expectedData.type, "synthetic " + eventtype + " event fired");
  }

  ok(event.clipboardData instanceof DataTransfer, "clipboardData is assigned");

  is(event.type, expectedData.type, "synthetic " + eventtype + " event type");
  if (expectedData.data === null) {
    is(SpecialPowers.wrap(event.clipboardData).mozItemCount, 0, "synthetic " + eventtype + " empty data");
  }
  else {
    is(SpecialPowers.wrap(event.clipboardData).mozItemCount, 1, "synthetic " + eventtype + " item count");
    is(event.clipboardData.types.length, 1, "synthetic " + eventtype + " types length");
    is(event.clipboardData.getData(expectedData.dataType), expectedData.data,
       "synthetic " + eventtype + " data");
  }

  is(getClipboardText(), "empty", "event does not change the clipboard " + step + " dispatch");

  if (step == "during") {
    expectedData.eventFired = true;
  }
}

async function checkCachedDataTransfer(cd, eventtype) {
  var testprefix = "cached " + eventtype + " dataTransfer";

  try {
    await putOnClipboard("Some Clipboard Text", () => { setClipboardText("Some Clipboard Text") },
                         "change clipboard outside of event");
  } catch (e) {
    ok(false, e.toString());
    return;
  }

  var oldtext = cd.getData("text/plain");
  ok(!oldtext, "clipboard get using " + testprefix);

  try {
    SpecialPowers.wrap(cd).mozSetDataAt("text/plain", "Test Cache Data", 0);
  } catch (ex) {}
  ok(!cd.getData("text/plain"), "clipboard set using " + testprefix);

  is(getClipboardText(), "Some Clipboard Text", "clipboard not changed using " + testprefix);

  try {
    cd.mozClearDataAt("text/plain", 0);
  } catch (ex) {}
  ok(!cd.getData("text/plain"), "clipboard clear using " + testprefix);

  is(getClipboardText(), "Some Clipboard Text", "clipboard not changed using " + testprefix);
}

add_task(async function test_modify_datatransfer_outofevent() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Check if the cached clipboard data can be accessed or modified
  // and whether it modifies the real clipboard

  // XXX Depends on test_input_cut_dataTransfer()
  if (cachedCutData) {
    await checkCachedDataTransfer(cachedCutData, "cut");
  } else {
    todo(false, "test_input_cut_dataTransfer must have been failed, skipping tests with its dataTransfer");
  }
  // XXX Depends on test_input_copy_dataTransfer()
  if (cachedCopyData) {
    await checkCachedDataTransfer(cachedCopyData, "copy");
  } else {
    todo(false, "test_input_copy_dataTransfer must have been failed, skipping tests with its dataTransfer");
  }
  // XXX Depends on test_input_paste_dataTransfer()
  if (cachedPasteData) {
    await checkCachedDataTransfer(cachedPasteData, "paste");
  } else {
    todo(false, "test_input_paste_dataTransfer must have been failed, skipping tests with its dataTransfer");
  }
});

add_task(async function test_input_cut_disallowed_types_dataTransfer() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  selectContentInput();
  let oncutExecuted = false;
  contentInput.oncut = function(event) {
    // Setting an arbitrary type should be OK
    try {
      event.clipboardData.setData("apple/cider", "Anything your heart desires");
      ok(true, "We should have successfully executed the setData call");
    } catch(e) {
      ok(false, "We should not have gotten an exception for trying to set that data");
    }

    // Unless that type happens to be application/x-moz-custom-clipdata
    try {
      event.clipboardData.setData("application/x-moz-custom-clipdata", "Anything your heart desires");
      ok(false, "We should not have successfully executed the setData call");
    } catch(e) {
      is(e.name, "NotSupportedError",
         "We should have gotten an NotSupportedError exception for trying to set that data");
    }
    oncutExecuted = true;
  };

  try {
    await putOnClipboard("INPUT TEXT", () => {
      synthesizeKey("x", {accelKey: 1});
    }, "The oncut handler should have been executed data");
    ok(oncutExecuted, "The oncut handler should have been executed");
  } catch (e) {
    ok(false, "Failed to copy the data given by the oncut");
  }
});

// Try copying an image to the clipboard and make sure that it looks correct when pasting it.
add_task(async function test_image_dataTransfer() {
  // cmd_copyImageContents errors on Android (bug 1299578).
  if (navigator.userAgent.includes("Android")) {
    return;
  }

  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  // Copy the image's data to the clipboard
  try {
    await putOnClipboard("", () => {
      SpecialPowers.setCommandNode(window, document.getElementById("image"));
      SpecialPowers.doCommand(window, "cmd_copyImageContents");
    }, "copy changed clipboard when preference is disabled");
  } catch (e) {
    ok(false, e.toString());
  }

  let onpasteCalled = false;
  document.onpaste = function(event) {
    ok(event instanceof ClipboardEvent, "paste event is an ClipboardEvent");
    ok(event.clipboardData instanceof DataTransfer, "paste event dataTransfer is a DataTransfer");
    let items = event.clipboardData.items;
    let foundData = false;
    for (let i = 0; i < items.length; ++i)  {
      if (items[i].kind == "file") {
        foundData = true;
        is(items[i].type, "image/png", "The type of the data must be image/png");
        is(items[i].getAsFile().type, "image/png", "The attached file must be image/png");
      }
    }
    ok(foundData, "Should have found a file entry in the DataTransferItemList");
    let files = event.clipboardData.files;
    is(files.length, 1, "There should only be one file on the DataTransfer");
    is(files[0].type, "image/png", "The only file should be an image/png");
    onpasteCalled = true;
  }

  synthesizeKey("v", {accelKey: 1});
  ok(onpasteCalled, "The paste event listener must have been called");
});

add_task(async function test_event_target() {
  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  let copyTarget = null;
  addEventListenerTo(document, "copy", (event) => { copyTarget = event.target; }, {once: true});

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

  let selection = document.getSelection();
  selection.setBaseAndExtent(content.firstChild, "CONTENT ".length,
                            content.firstChild, "CONTENT TEXT".length);

  try {
    await putOnClipboard("TEXT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy text from non-editable element");
  } catch (e) {
    ok(false, e.toString());
  }

  is(copyTarget.getAttribute("id"), "content", "Copy event's target should be always an element");

  // Create a contenteditable element to check complicated event target.
  contenteditableContainer.innerHTML = '<div contenteditable><p id="p1">foo</p><p id="p2">bar</p></div>';
  contenteditableContainer.firstChild.focus();

  let p1 = document.getElementById("p1");
  let p2 = document.getElementById("p2");
  selection.setBaseAndExtent(p1.firstChild, 1, p2.firstChild, 1);

  let pasteTarget = null;
  let pasteEventCount = 0;
  function pasteEventLogger(event) {
    pasteTarget = event.target;
    pasteEventCount++;
  }
  addEventListenerTo(document, "paste", pasteEventLogger);
  synthesizeKey("v", {accelKey: 1});
  is(pasteTarget.getAttribute("id"), "p1",
    "'paste' event's target should be always an element which includes start container of the first Selection range");
  is(pasteEventCount, 1,
    "'paste' event should be fired only once when Accel+'v' is pressed");
});

add_task(async function test_paste_event_for_middle_click_without_HTMLEditor() {
  await SpecialPowers.pushPrefEnv({"set": [["middlemouse.paste", true],
                                           ["middlemouse.contentLoadURL", false]]});

  try {
    await reset();
  } catch (e) {
    ok(false, `Failed to reset (${e.toString()})`);
    return;
  }

  contenteditableContainer.innerHTML = '<div id="non-editable-target">non-editable</div>';
  let noneditableDiv = document.getElementById("non-editable-target");

  ok(!getHTMLEditor(), "There should not be HTMLEditor");

  let selection = document.getSelection();
  selection.setBaseAndExtent(content.firstChild, 0,
                             content.firstChild, "CONTENT".length);

  try {
    await putOnClipboard("CONTENT", () => {
      synthesizeKey("c", {accelKey: 1});
    }, "copy text from non-editable element");
  } catch (e) {
    ok(false, e.toString());
    return;
  }

  let auxclickFired = false;
  function onAuxClick(event) {
    auxclickFired = true;
  }
  addEventListenerTo(document, "auxclick", onAuxClick);

  let pasteEventCount = 0;
  function onPaste(event) {
    pasteEventCount++;
    ok(auxclickFired, "'auxclick' event should be fired before 'paste' event");
    is(event.target, noneditableDiv,
      "'paste' event should be fired on the clicked element");
  }
  addEventListenerTo(document, "paste", onPaste);

  synthesizeMouseAtCenter(noneditableDiv, {button: 1});
  is(pasteEventCount, 1, "'paste' event should be fired just once");

  pasteEventCount = 0;
  auxclickFired = false;
  addEventListenerTo(document, "mouseup", (event) => { event.preventDefault(); }, {once: true});
  synthesizeMouseAtCenter(noneditableDiv, {button: 1});
  is(pasteEventCount, 1,
    "Even if 'mouseup' event is consumed, 'paste' event should be fired");

  pasteEventCount = 0;
  auxclickFired = false;
  addEventListenerTo(document, "auxclick", (event) => { event.preventDefault(); }, {once: true, capture: true});
  synthesizeMouseAtCenter(noneditableDiv, {button: 1});
  ok(auxclickFired, "'auxclickFired' fired");
  is(pasteEventCount, 0,
    "If 'auxclick' event is consumed at capturing phase at the document node, 'paste' event should not be fired");

  pasteEventCount = 0;
  auxclickFired = false;
  addEventListenerTo(noneditableDiv, "auxclick", (event) => { event.preventDefault(); }, {once: true});
  synthesizeMouseAtCenter(noneditableDiv, {button: 1});
  ok(auxclickFired, "'auxclick' fired");
  is(pasteEventCount, 0,
    "If 'auxclick' event listener is added to the click event target, 'paste' event should not be fired");

  pasteEventCount = 0;
  auxclickFired = false;
  addEventListenerTo(document, "auxclick", (event) => { event.preventDefault(); }, {once: true});
  synthesizeMouseAtCenter(noneditableDiv, {button: 1});
  ok(auxclickFired, "'auxclick' fired");
  is(pasteEventCount, 0,
    "If 'auxclick' event is consumed, 'paste' event should be not be fired");
});

add_task(function cleaning_up() {
  try {
    disableNonTestMouseEvents(false);
  } finally {
    window.close();
  }
});
</script>
</pre>
</body>
</html>