summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_MozEditableElement_setUserInput.html
blob: 06380776f6dce0c038006a90af5740624520b9a1 (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
<!DOCTYPE>
<html>
<head>
  <title>Test for MozEditableElement.setUserInput()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<div id="display">
</div>
<div id="content"></div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();
// eslint-disable-next-line complexity
SimpleTest.waitForFocus(async () => {
  const kSetUserInputCancelable = SpecialPowers.getBoolPref("dom.input_event.allow_to_cancel_set_user_input");

  let content = document.getElementById("content");
  /**
   * Test structure:
   *   element: the tag name to create.
   *   type: the type attribute value for the element.  If unnecessary omit it.
   *   input: the values calling setUserInput() with.
   *     before: used when calling setUserInput() before the element gets focus.
   *     after: used when calling setUserInput() after the element gets focus.
   *   result: the results of calling setUserInput().
   *     before: the element's expected value of calling setUserInput() before the element gets focus.
   *     after: the element's expected value of calling setUserInput() after the element gets focus.
   *     fireBeforeInputEvent: true if "beforeinput" event should be fired.  Otherwise, false.
   *     fireInputEvent: true if "input" event should be fired.  Otherwise, false.
   */
  for (let test of [{element: "input", type: "hidden",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: false}},
                    {element: "input", type: "text",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    {element: "input", type: "search",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    {element: "input", type: "tel",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    {element: "input", type: "url",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    {element: "input", type: "email",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    {element: "input", type: "password",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    // "date" does not support setUserInput, but dispatches "input" event...
                    {element: "input", type: "date",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    // "month" does not support setUserInput, but dispatches "input" event...
                    {element: "input", type: "month",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    // "week" does not support setUserInput, but dispatches "input" event...
                    {element: "input", type: "week",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    // "time" does not support setUserInput, but dispatches "input" event...
                    {element: "input", type: "time",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    // "datetime-local" does not support setUserInput, but dispatches "input" event...
                    {element: "input", type: "datetime-local",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    {element: "input", type: "number",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}},
                    {element: "input", type: "range",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    // "color" does not support setUserInput, but dispatches "input" event...
                    {element: "input", type: "color",
                     input: {before: "#5C5C5C", after: "#FFFFFF"},
                     result: {before: "#5c5c5c", after:"#ffffff", fireBeforeInputEvent: false, fireInputEvent: true}},
                    {element: "input", type: "checkbox",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    {element: "input", type: "radio",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: true}},
                    // "file" is not supported by setUserInput? But there is a path...
                    {element: "input", type: "file",
                     input: {before: "3", after: "6"},
                     result: {before: "", after:"", fireBeforeInputEvent: false, fireInputEvent: true}},
                    {element: "input", type: "submit",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: false}},
                    {element: "input", type: "image",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: false}},
                    {element: "input", type: "reset",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: false}},
                    {element: "input", type: "button",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: false, fireInputEvent: false}},
                    {element: "textarea",
                     input: {before: "3", after: "6"},
                     result: {before: "3", after:"6", fireBeforeInputEvent: true, fireInputEvent: true}}]) {
    let tag =
      test.type !== undefined ? `<${test.element} type="${test.type}">` :
                                `<${test.element}>`;
    content.innerHTML =
      test.element !== "input" ? tag : `${tag}</${test.element}>`;
    content.scrollTop; // Flush pending layout.
    let target = content.firstChild;

    let inputEvents = [], beforeInputEvents = [];
    function onBeforeInput(aEvent) {
      beforeInputEvents.push(aEvent);
    }
    function onInput(aEvent) {
      inputEvents.push(aEvent);
    }
    target.addEventListener("beforeinput", onBeforeInput);
    target.addEventListener("input", onInput);

    // Before setting focus, editor of the element may have not been created yet.
    let previousValue = target.value;
    SpecialPowers.wrap(target).setUserInput(test.input.before);
    if (target.value == previousValue && test.result.before != previousValue) {
      todo_is(target.value, test.result.before, `setUserInput("${test.input.before}") before ${tag} gets focus should set its value to "${test.result.before}"`);
    } else {
      is(target.value, test.result.before, `setUserInput("${test.input.before}") before ${tag} gets focus should set its value to "${test.result.before}"`);
    }
    if (target.value == previousValue) {
      if (test.type === "date" || test.type === "time" || test.type === "datetime-local") {
        todo_is(inputEvents.length, 0,
                `No "input" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
      } else {
        is(inputEvents.length, 0,
           `No "input" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
      }
    } else {
      if (!test.result.fireBeforeInputEvent) {
        is(beforeInputEvents.length, 0,
           `No "beforeinput" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
      } else {
        is(beforeInputEvents.length, 1,
           `Only one "beforeinput" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
      }
      if (!test.result.fireInputEvent) {
        // HTML spec defines that "input" elements whose type are "hidden",
        // "submit", "image", "reset" and "button" shouldn't fire input event
        // when its value is changed.
        // XXX Perhaps, we shouldn't support setUserInput() with such types.
        if (test.type === "hidden" ||
            test.type === "submit" ||
            test.type === "image" ||
            test.type === "reset" ||
            test.type === "button") {
          todo_is(inputEvents.length, 0,
                  `No "input" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
        } else {
          is(inputEvents.length, 0,
             `No "input" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
        }
      } else {
        is(inputEvents.length, 1,
           `Only one "input" event should be dispatched when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
      }
    }
    if (inputEvents.length) {
      if (SpecialPowers.wrap(target).isInputEventTarget) {
        if (test.type === "time") {
          todo(inputEvents[0] instanceof InputEvent,
               `"input" event should be dispatched with InputEvent interface when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
        } else {
          if (beforeInputEvents.length && test.result.fireBeforeInputEvent) {
            is(beforeInputEvents[0].cancelable, kSetUserInputCancelable,
               `"beforeinput" event for "insertReplacementText" should be cancelable when setUserInput("${test.input.before}") is called before ${tag} gets focus unless it's suppressed by the pref`);
            is(beforeInputEvents[0].inputType, "insertReplacementText",
               `inputType of "beforeinput"event should be "insertReplacementText" when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
            is(beforeInputEvents[0].data, test.input.before,
               `data of "beforeinput" event should be "${test.input.before}" when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
            is(beforeInputEvents[0].dataTransfer, null,
               `dataTransfer of "beforeinput" event should be null when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
            is(beforeInputEvents[0].getTargetRanges().length, 0,
               `getTargetRanges() of "beforeinput" event should return empty array when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
          }
          ok(inputEvents[0] instanceof InputEvent,
             `"input" event should be dispatched with InputEvent interface when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
          is(inputEvents[0].inputType, "insertReplacementText",
             `inputType of "input" event should be "insertReplacementText" when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
          is(inputEvents[0].data, test.input.before,
             `data of "input" event should be "${test.input.before}" when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
          is(inputEvents[0].dataTransfer, null,
             `dataTransfer of "input" event should be null when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
          is(inputEvents[0].getTargetRanges().length, 0,
             `getTargetRanges() of "input" event should return empty array when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
        }
      } else {
        ok(inputEvents[0] instanceof Event && !(inputEvents[0] instanceof UIEvent),
           `"input" event should be dispatched with Event interface when setUserInput("${test.input.before}") is called before ${tag} gets focus`);
      }
      is(inputEvents[0].cancelable, false,
         `"input" event should be never cancelable (${tag}, before getting focus)`);
      is(inputEvents[0].bubbles, true,
         `"input" event should always bubble (${tag}, before getting focus)`);
    }

    beforeInputEvents = [];
    inputEvents = [];
    target.focus();
    previousValue = target.value;
    SpecialPowers.wrap(target).setUserInput(test.input.after);
    if (target.value == previousValue && test.result.after != previousValue) {
      todo_is(target.value, test.result.after, `setUserInput("${test.input.after}") after ${tag} gets focus should set its value to "${test.result.after}"`);
    } else {
      is(target.value, test.result.after, `setUserInput("${test.input.after}") after ${tag} gets focus should set its value to "${test.result.after}"`);
    }
    if (target.value == previousValue) {
      if (test.type === "date" || test.type === "time" || test.type === "datetime-local") {
        todo_is(inputEvents.length, 0,
                `No "input" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
      } else {
        is(inputEvents.length, 0,
           `No "input" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
      }
    } else {
      if (!test.result.fireBeforeInputEvent) {
        is(beforeInputEvents.length, 0,
           `No "beforeinput" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
      } else {
        is(beforeInputEvents.length, 1,
           `Only one "beforeinput" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
      }
      if (!test.result.fireInputEvent) {
        // HTML spec defines that "input" elements whose type are "hidden",
        // "submit", "image", "reset" and "button" shouldn't fire input event
        // when its value is changed.
        // XXX Perhaps, we shouldn't support setUserInput() with such types.
        if (test.type === "hidden" ||
            test.type === "submit" ||
            test.type === "image" ||
            test.type === "reset" ||
            test.type === "button") {
          todo_is(inputEvents.length, 0,
                  `No "input" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
        } else {
          is(inputEvents.length, 0,
             `No "input" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
        }
      } else {
        is(inputEvents.length, 1,
           `Only one "input" event should be dispatched when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
      }
    }
    if (inputEvents.length) {
      if (SpecialPowers.wrap(target).isInputEventTarget) {
        if (test.type === "time") {
          todo(inputEvents[0] instanceof InputEvent,
               `"input" event should be dispatched with InputEvent interface when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
        } else {
          if (beforeInputEvents.length && test.result.fireBeforeInputEvent) {
            is(beforeInputEvents[0].cancelable, kSetUserInputCancelable,
               `"beforeinput" event should be cancelable when setUserInput("${test.input.after}") is called after ${tag} gets focus unless it's suppressed by the pref`);
            is(beforeInputEvents[0].inputType, "insertReplacementText",
               `inputType of "beforeinput" event should be "insertReplacementText" when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
            is(beforeInputEvents[0].data, test.input.after,
               `data of "beforeinput" should be "${test.input.after}" when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
            is(beforeInputEvents[0].dataTransfer, null,
               `dataTransfer of "beforeinput" should be null when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
            is(beforeInputEvents[0].getTargetRanges().length, 0,
               `getTargetRanges() of "beforeinput" should return empty array when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
          }
          ok(inputEvents[0] instanceof InputEvent,
             `"input" event should be dispatched with InputEvent interface when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
          is(inputEvents[0].inputType, "insertReplacementText",
             `inputType of "input" event should be "insertReplacementText" when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
          is(inputEvents[0].data, test.input.after,
             `data of "input" event should be "${test.input.after}" when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
          is(inputEvents[0].dataTransfer, null,
             `dataTransfer of "input" event should be null when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
          is(inputEvents[0].getTargetRanges().length, 0,
             `getTargetRanges() of "input" event should return empty array when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
        }
      } else {
        ok(inputEvents[0] instanceof Event && !(inputEvents[0] instanceof UIEvent),
           `"input" event should be dispatched with Event interface when setUserInput("${test.input.after}") is called after ${tag} gets focus`);
      }
      is(inputEvents[0].cancelable, false,
         `"input" event should be never cancelable (${tag}, after getting focus)`);
      is(inputEvents[0].bubbles, true,
         `"input" event should always bubble (${tag}, after getting focus)`);
    }

    target.removeEventListener("input", onInput);
  }

  function testValidationMessage(aType, aInvalidValue, aValidValue) {
    let tag = `<input type="${aType}">`
    content.innerHTML = tag;
    content.scrollTop; // Flush pending layout.
    let target = content.firstChild;

    let inputEvents = [];
    let validationMessage = "";

    function reset() {
      inputEvents = [];
      validationMessage = "";
    }

    function onInput(aEvent) {
      inputEvents.push(aEvent);
      validationMessage = aEvent.target.validationMessage;
    }
    target.addEventListener("input", onInput);

    reset();
    SpecialPowers.wrap(target).setUserInput(aInvalidValue);
    is(inputEvents.length, 1,
       `Only one "input" event should be dispatched when setUserInput("${aInvalidValue}") is called before ${tag} gets focus`);
    isnot(validationMessage, "",
          `${tag}.validationMessage should not be empty when setUserInput("${aInvalidValue}") is called before ${tag} gets focus`);
    ok(target.matches(":invalid"),
       `The target should have invalid pseudo class when setUserInput("${aInvalidValue}") is called before ${tag} gets focus`);

    reset();
    SpecialPowers.wrap(target).setUserInput(aValidValue);
    is(inputEvents.length, 1,
       `Only one "input" event should be dispatched when setUserInput("${aValidValue}") is called before ${tag} gets focus`);
    is(validationMessage, "",
       `${tag}.validationMessage should be empty when setUserInput("${aValidValue}") is called before ${tag} gets focus`);
    ok(!target.matches(":invalid"),
       `The target shouldn't have invalid pseudo class when setUserInput("${aValidValue}") is called before ${tag} gets focus`);

    reset();
    SpecialPowers.wrap(target).setUserInput(aInvalidValue);
    is(inputEvents.length, 1,
       `Only one "input" event should be dispatched again when setUserInput("${aInvalidValue}") is called before ${tag} gets focus`);
    isnot(validationMessage, "",
          `${tag}.validationMessage should not be empty again when setUserInput("${aInvalidValue}") is called before ${tag} gets focus`);
    ok(target.matches(":invalid"),
       `The target should have invalid pseudo class again when setUserInput("${aInvalidValue}") is called before ${tag} gets focus`);

    target.value = "";
    target.focus();

    reset();
    SpecialPowers.wrap(target).setUserInput(aInvalidValue);
    is(inputEvents.length, 1,
       `Only one "input" event should be dispatched when setUserInput("${aInvalidValue}") is called after ${tag} gets focus`);
    isnot(validationMessage, "",
          `${tag}.validationMessage should not be empty when setUserInput("${aInvalidValue}") is called after ${tag} gets focus`);
    ok(target.matches(":invalid"),
       `The target should have invalid pseudo class when setUserInput("${aInvalidValue}") is called after ${tag} gets focus`);

    reset();
    SpecialPowers.wrap(target).setUserInput(aValidValue);
    is(inputEvents.length, 1,
       `Only one "input" event should be dispatched when setUserInput("${aValidValue}") is called after ${tag} gets focus`);
    is(validationMessage, "",
       `${tag}.validationMessage should be empty when setUserInput("${aValidValue}") is called after ${tag} gets focus`);
    ok(!target.matches(":invalid"),
       `The target shouldn't have invalid pseudo class when setUserInput("${aValidValue}") is called after ${tag} gets focus`);

    reset();
    SpecialPowers.wrap(target).setUserInput(aInvalidValue);
    is(inputEvents.length, 1,
       `Only one "input" event should be dispatched again when setUserInput("${aInvalidValue}") is called after ${tag} gets focus`);
    isnot(validationMessage, "",
          `${tag}.validationMessage should not be empty again when setUserInput("${aInvalidValue}") is called after ${tag} gets focus`);
    ok(target.matches(":invalid"),
       `The target should have invalid pseudo class again when setUserInput("${aInvalidValue}") is called after ${tag} gets focus`);

    target.removeEventListener("input", onInput);
  }
  testValidationMessage("email", "f", "foo@example.com");

  function testValueMissing(aType, aValidValue) {
    let tag = aType === "textarea" ? "<textarea required>" : `<input type="${aType}" required>`;
    content.innerHTML = `${tag}${aType === "textarea" ? "</textarea>" : ""}`;
    content.scrollTop; // Flush pending layout.
    let target = content.firstChild;

    let inputEvents = [], beforeInputEvents = [];
    function reset() {
      beforeInputEvents = [];
      inputEvents = [];
    }

    function onBeforeInput(aEvent) {
      aEvent.validity = aEvent.target.checkValidity();
      beforeInputEvents.push(aEvent);
    }
    function onInput(aEvent) {
      aEvent.validity = aEvent.target.checkValidity();
      inputEvents.push(aEvent);
    }
    target.addEventListener("beforeinput", onBeforeInput);
    target.addEventListener("input", onInput);

    reset();
    SpecialPowers.wrap(target).setUserInput(aValidValue);
    is(beforeInputEvents.length, 1, `Calling  ${tag}.setUserInput(${aValidValue}) should cause a "beforeinput" event (before gets focus)`);
    if (beforeInputEvents.length) {
      is(beforeInputEvents[0].validity, false,
         `The ${tag} should be invalid at "beforeinput" event (before gets focus)`);
    }
    is(inputEvents.length, 1, `Calling  ${tag}.setUserInput(${aValidValue}) should cause a "input" event (before gets focus)`);
    if (inputEvents.length) {
      is(inputEvents[0].validity, true,
         `The ${tag} should be valid at "input" event (before gets focus)`);
    }

    target.removeEventListener("beforeinput", onBeforeInput);
    target.removeEventListener("input", onInput);

    content.innerHTML = "";
    content.scrollTop; // Flush pending layout.
    content.innerHTML = `${tag}${aType === "textarea" ? "</textarea>" : ""}`;
    content.scrollTop; // Flush pending layout.
    target = content.firstChild;

    target.focus();
    target.addEventListener("beforeinput", onBeforeInput);
    target.addEventListener("input", onInput);

    reset();
    SpecialPowers.wrap(target).setUserInput(aValidValue);
    is(beforeInputEvents.length, 1, `Calling  ${tag}.setUserInput(${aValidValue}) should cause a "beforeinput" event (after gets focus)`);
    if (beforeInputEvents.length) {
      is(beforeInputEvents[0].validity, false,
         `The ${tag} should be invalid at "beforeinput" event (after gets focus)`);
    }
    is(inputEvents.length, 1, `Calling  ${tag}.setUserInput(${aValidValue}) should cause a "input" event (after gets focus)`);
    if (inputEvents.length) {
      is(inputEvents[0].validity, true,
         `The ${tag} should be valid at "input" event (after gets focus)`);
    }

    target.removeEventListener("beforeinput", onBeforeInput);
    target.removeEventListener("input", onInput);
  }
  testValueMissing("text", "abc");
  testValueMissing("password", "abc");
  testValueMissing("textarea", "abc");
  testValueMissing("email", "foo@example.com");
  testValueMissing("url", "https://example.com/");

  function testEditorValueAtEachEvent(aType) {
    let tag = aType === "textarea" ? "<textarea>" : `<input type="${aType}">`
    let closeTag = aType === "textarea" ? "</textarea>" : "";
    content.innerHTML = `${tag}${closeTag}`;
    content.scrollTop; // Flush pending layout.
    let target = content.firstChild;
    target.value = "Old Value";
    let description = `Setting new value of ${tag} before setting focus: `;
    let onBeforeInput = (aEvent) => {
      is(target.value, "Old Value",
         `${description}The value should not have been modified at "beforeinput" event yet (inputType: "${aEvent.inputType}", data: "${aEvent.data}")`);
    };
    let onInput = (aEvent) => {
      is(target.value, "New Value",
         `${description}The value should have been modified at "input" event (inputType: "${aEvent.inputType}", data: "${aEvent.data}"`);
    };
    target.addEventListener("beforeinput", onBeforeInput);
    target.addEventListener("input", onInput);
    SpecialPowers.wrap(target).setUserInput("New Value");

    description = `Setting new value of ${tag} after setting focus: `;
    target.value = "Old Value";
    target.focus();
    SpecialPowers.wrap(target).setUserInput("New Value");

    target.removeEventListener("beforeinput", onBeforeInput);
    target.removeEventListener("input", onInput);

    // FYI: This is not realistic situation because we should do nothing
    //      while user composing IME.
    // TODO: TextControlState should stop returning setting value as the value
    //       while committing composition.
    description = `Setting new value of ${tag} during composition: `;
    target.value = "";
    target.focus();
    synthesizeCompositionChange({
      composition: {
        string: "composition string",
        clauses: [{length: 18, attr: COMPOSITION_ATTR_RAW_CLAUSE}],
      },
      caret: {start: 18, length: 0},
    });
    let onCompositionUpdate = (aEvent) => {
      todo_is(target.value, "composition string",
         `${description}The value should not have been modified at "compositionupdate" event yet (data: "${aEvent.data}")`);
    };
    let onCompositionEnd = (aEvent) => {
      todo_is(target.value, "composition string",
         `${description}The value should not have been modified at "compositionupdate" event yet (data: "${aEvent.data}")`);
    };
    onBeforeInput = (aEvent) => {
      if (aEvent.inputType === "insertCompositionText") {
        todo_is(target.value, "composition string",
           `${description}The value should not have been modified at "beforeinput" event yet (inputType: "${aEvent.inputType}", data: "${aEvent.data}")`);
      } else {
        is(target.value, "composition string",
           `${description}The value should not have been modified at "beforeinput" event yet (inputType: "${aEvent.inputType}", data: "${aEvent.data}")`);
      }
    };
    onInput = (aEvent) => {
      if (aEvent.inputType === "insertCompositionText") {
        todo_is(target.value, "composition string",
           `${description}The value should not have been modified at "input" event yet (inputType: "${aEvent.inputType}", data: "${aEvent.data}")`);
      } else {
        is(target.value, "New Value",
           `${description}The value should have been modified at "input" event (inputType: "${aEvent.inputType}", data: "${aEvent.data}"`);
      }
    };
    target.addEventListener("compositionupdate", onCompositionUpdate);
    target.addEventListener("compositionend", onCompositionEnd);
    target.addEventListener("beforeinput", onBeforeInput);
    target.addEventListener("input", onInput);
    SpecialPowers.wrap(target).setUserInput("New Value");
    target.removeEventListener("compositionupdate", onCompositionUpdate);
    target.removeEventListener("compositionend", onCompositionEnd);
    target.removeEventListener("beforeinput", onBeforeInput);
    target.removeEventListener("input", onInput);
  }
  testEditorValueAtEachEvent("text");
  testEditorValueAtEachEvent("textarea");

  async function testBeforeInputCancelable(aType) {
    let tag = aType === "textarea" ? "<textarea>" : `<input type="${aType}">`
    let closeTag = aType === "textarea" ? "</textarea>" : "";
    for (const kShouldBeCancelable of [true, false]) {
      await SpecialPowers.pushPrefEnv({
        set: [["dom.input_event.allow_to_cancel_set_user_input", kShouldBeCancelable]],
      });

      content.innerHTML = `${tag}${closeTag}`;
      content.scrollTop; // Flush pending layout.
      let target = content.firstChild;
      target.value = "Old Value";
      let description = `Setting new value of ${tag} before setting focus (the pref ${kShouldBeCancelable ? "allows" : "disallows"} to cancel beforeinput): `;
      let onBeforeInput = (aEvent) => {
        is(aEvent.cancelable, kShouldBeCancelable,
          `${description}The "beforeinput" event should be ${kShouldBeCancelable ? "cancelable" : "not be cancelable due to suppressed by the pref"}`);
      };
      let onInput = (aEvent) => {
        is(aEvent.cancelable, false,
          `${description}The value should have been modified at "input" event (inputType: "${aEvent.inputType}", data: "${aEvent.data}"`);
      };
      target.addEventListener("beforeinput", onBeforeInput);
      target.addEventListener("input", onInput);
      SpecialPowers.wrap(target).setUserInput("New Value");

      description = `Setting new value of ${tag} after setting focus (the pref ${kShouldBeCancelable ? "allows" : "disallows"} to cancel beforeinput): `;
      target.value = "Old Value";
      target.focus();
      SpecialPowers.wrap(target).setUserInput("New Value");

      target.removeEventListener("beforeinput", onBeforeInput);
      target.removeEventListener("input", onInput);
    }

    await SpecialPowers.clearUserPref({
      clear: [["dom.input_event.allow_to_cancel_set_user_input"]],
    });
  }
  await testBeforeInputCancelable("text");
  await testBeforeInputCancelable("textarea");

  SimpleTest.finish();
});
</script>
</body>
</html>