summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_input_event.html
blob: 72863ca335c5e6458b9f672e206ca89c105cecc5 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=851780
-->
<head>
<title>Test for input event</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/EventUtils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=851780">Mozilla Bug 851780</a>
<p id="display"></p>
<div id="content"></div>
<pre id="test">
<script class="testbody" type="text/javascript">

  /** Test for input event. This is highly based on test_change_event.html **/

  const isDesktop = !/Mobile|Tablet/.test(navigator.userAgent);

  let expectedInputType = "";
  let expectedData = null;
  let expectedBeforeInputCancelable = false;
  function checkBeforeInputEvent(aEvent, aDescription) {
    ok(aEvent instanceof InputEvent,
       `"beforeinput" event should be dispatched with InputEvent interface ${aDescription}`);
    is(aEvent.inputType, expectedInputType,
       `inputType of "beforeinput" event should be "${expectedInputType}" ${aDescription}`);
    is(aEvent.data, expectedData,
       `data of "beforeinput" event should be ${expectedData} ${aDescription}`);
    is(aEvent.dataTransfer, null,
       `dataTransfer of "beforeinput" event should be null ${aDescription}`);
    is(aEvent.getTargetRanges().length, 0,
       `getTargetRanges() of "beforeinput" event should return empty array ${aDescription}`);
    is(aEvent.cancelable, expectedBeforeInputCancelable,
       `"beforeinput" event for "${expectedInputType}" should ${expectedBeforeInputCancelable ? "be" : "not be"} cancelable ${aDescription}`);
    is(aEvent.bubbles, true,
       `"beforeinput" event should always bubble ${aDescription}`);
  }

  let skipExpectedDataCheck = false;
  function checkIfInputIsInputEvent(aEvent, aDescription) {
    ok(aEvent instanceof InputEvent,
       `"input" event should be dispatched with InputEvent interface ${aDescription}`);
    is(aEvent.inputType, expectedInputType,
       `inputType should be "${expectedInputType}" ${aDescription}`);
    if (!skipExpectedDataCheck)
      is(aEvent.data, expectedData, `data should be ${expectedData} ${aDescription}`);
    else
      info(`data is ${aEvent.data} ${aDescription}`);
    is(aEvent.dataTransfer, null,
       `dataTransfer should be null ${aDescription}`);
    is(aEvent.cancelable, false,
       `"input" event should be never cancelable ${aDescription}`);
    is(aEvent.bubbles, true,
       `"input" event should always bubble ${aDescription}`);
  }

  function checkIfInputIsEvent(aEvent, aDescription) {
    ok(aEvent instanceof Event && !(aEvent instanceof UIEvent),
       `"input" event should be dispatched with InputEvent interface ${aDescription}`);
    is(aEvent.cancelable, false,
       `"input" event should be never cancelable ${aDescription}`);
    is(aEvent.bubbles, true,
       `"input" event should always bubble ${aDescription}`);
  }

  let textareaInput = 0, textareaBeforeInput = 0;
  let textTypes = ["text", "email", "search", "tel", "url", "password"];
  let textBeforeInput = [0, 0, 0, 0, 0, 0];
  let textInput = [0, 0, 0, 0, 0, 0];
  let nonTextTypes = ["button", "submit", "image", "reset", "radio", "checkbox"];
  let nonTextBeforeInput = [0, 0, 0, 0, 0, 0];
  let nonTextInput = [0, 0, 0, 0, 0, 0];
  let rangeInput = 0, rangeBeforeInput = 0;
  let numberInput = 0, numberBeforeInput = 0;

  // Don't create elements whose event listener attributes are required before enabling `beforeinput` event.
  function init() {
    document.getElementById("content").innerHTML =
    `<input type="file" id="fileInput">
     <textarea id="textarea"></textarea>
     <input type="text" id="input_text">
     <input type="email" id="input_email">
     <input type="search" id="input_search">
     <input type="tel" id="input_tel">
     <input type="url" id="input_url">
     <input type="password" id="input_password">

     <!-- "Non-text" inputs-->
     <input type="button" id="input_button">
     <input type="submit" id="input_submit">
     <input type="image" id="input_image">
     <input type="reset" id="input_reset">
     <input type="radio" id="input_radio">
     <input type="checkbox" id="input_checkbox">
     <input type="range" id="input_range">
     <input type="number" id="input_number">`;

    document.getElementById("textarea").addEventListener("beforeinput", (aEvent) => {
      ++textareaBeforeInput;
      checkBeforeInputEvent(aEvent, "on textarea element");
    });
    document.getElementById("textarea").addEventListener("input", (aEvent) => {
      ++textareaInput;
      checkIfInputIsInputEvent(aEvent, "on textarea element");
    });

    // These are the type were the input event apply.
    for (let id of ["input_text", "input_email", "input_search", "input_tel", "input_url", "input_password"]) {
      document.getElementById(id).addEventListener("beforeinput", (aEvent) => {
        ++textBeforeInput[textTypes.indexOf(aEvent.target.type)];
        checkBeforeInputEvent(aEvent, `on input element whose type is ${aEvent.target.type}`);
      });
      document.getElementById(id).addEventListener("input", (aEvent) => {
        ++textInput[textTypes.indexOf(aEvent.target.type)];
        checkIfInputIsInputEvent(aEvent, `on input element whose type is ${aEvent.target.type}`);
      });
    }

    // These are the type were the input event does not apply.
    for (let id of ["input_button", "input_submit", "input_image", "input_reset", "input_radio", "input_checkbox"]) {
      document.getElementById(id).addEventListener("beforeinput", (aEvent) => {
        ++nonTextBeforeInput[nonTextTypes.indexOf(aEvent.target.type)];
      });
      document.getElementById(id).addEventListener("input", (aEvent) => {
        ++nonTextInput[nonTextTypes.indexOf(aEvent.target.type)];
        checkIfInputIsEvent(aEvent, `on input element whose type is ${aEvent.target.type}`);
      });
    }

    document.getElementById("input_range").addEventListener("beforeinput", (aEvent) => {
      ++rangeBeforeInput;
    });
    document.getElementById("input_range").addEventListener("input", (aEvent) => {
      ++rangeInput;
      checkIfInputIsEvent(aEvent, "on input element whose type is range");
    });

    document.getElementById("input_number").addEventListener("beforeinput", (aEvent) => {
      ++numberBeforeInput;
    });
    document.getElementById("input_number").addEventListener("input", (aEvent) => {
      ++numberInput;
      checkIfInputIsInputEvent(aEvent, "on input element whose type is number");
    });
  }

  var MockFilePicker = SpecialPowers.MockFilePicker;
  MockFilePicker.init(window);

  function testUserInput() {
    // Simulating an OK click and with a file name return.
    MockFilePicker.useBlobFile();
    MockFilePicker.returnValue = MockFilePicker.returnOK;
    var input = document.getElementById('fileInput');
    input.focus();

    input.addEventListener("beforeinput", function (aEvent) {
      ok(false, "beforeinput event shouldn't be dispatched on file input.");
    });
    input.addEventListener("input", function (aEvent) {
      ok(true, "input event should've been dispatched on file input.");
      checkIfInputIsEvent(aEvent, "on file input");
    });

    input.click();
    SimpleTest.executeSoon(testUserInput2);
  }

  function testUserInput2() {
    // Some generic checks for types that support the input event.
    for (var i = 0; i < textTypes.length; ++i) {
      input = document.getElementById("input_" + textTypes[i]);
      input.focus();
      expectedInputType = "insertLineBreak";
      expectedData = null;
      expectedBeforeInputCancelable = true;
      synthesizeKey("KEY_Enter");
      is(textBeforeInput[i], 1, "beforeinput event should've been dispatched on " + textTypes[i] + " input element");
      is(textInput[i], 0, "input event shouldn't be dispatched on " + textTypes[i] + " input element");

      expectedInputType = "insertText";
      expectedData = "m";
      expectedBeforeInputCancelable = true;
      sendString("m");
      is(textBeforeInput[i], 2, textTypes[i] + " input element should've been dispatched beforeinput event.");
      is(textInput[i], 1, textTypes[i] + " input element should've been dispatched input event.");
      expectedInputType = "insertLineBreak";
      expectedData = null;
      expectedBeforeInputCancelable = true;
      synthesizeKey("KEY_Enter", {shiftKey: true});
      is(textBeforeInput[i], 3, "input event should've been dispatched on " + textTypes[i] + " input element");
      is(textInput[i], 1, "input event shouldn't be dispatched on " + textTypes[i] + " input element");

      expectedInputType = "deleteContentBackward";
      expectedData = null;
      expectedBeforeInputCancelable = true;
      synthesizeKey("KEY_Backspace");
      is(textBeforeInput[i], 4, textTypes[i] + " input element should've been dispatched beforeinput event.");
      is(textInput[i], 2, textTypes[i] + " input element should've been dispatched input event.");
    }

    // Some scenarios of value changing from script and from user input.
    input = document.getElementById("input_text");
    input.focus();
    expectedInputType = "insertText";
    expectedData = "f";
    expectedBeforeInputCancelable = true;
    sendString("f");
    is(textBeforeInput[0], 5, "beforeinput event should've been dispatched");
    is(textInput[0], 3, "input event should've been dispatched");
    input.blur();
    is(textBeforeInput[0], 5, "input event should not have been dispatched");
    is(textInput[0], 3, "input event should not have been dispatched");

    input.focus();
    input.value = 'foo';
    is(textBeforeInput[0], 5, "beforeinput event should not have been dispatched");
    is(textInput[0], 3, "input event should not have been dispatched");
    input.blur();
    is(textBeforeInput[0], 5, "beforeinput event should not have been dispatched");
    is(textInput[0], 3, "input event should not have been dispatched");

    input.focus();
    expectedInputType = "insertText";
    expectedData = "f";
    expectedBeforeInputCancelable = true;
    sendString("f");
    is(textBeforeInput[0], 6, "beforeinput event should've been dispatched");
    is(textInput[0], 4, "input event should've been dispatched");
    input.value = 'bar';
    is(textBeforeInput[0], 6, "beforeinput event should not have been dispatched");
    is(textInput[0], 4, "input event should not have been dispatched");
    input.blur();
    is(textBeforeInput[0], 6, "beforeinput event should not have been dispatched");
    is(textInput[0], 4, "input event should not have been dispatched");

    // Same for textarea.
    var textarea = document.getElementById("textarea");
    textarea.focus();
    expectedInputType = "insertText";
    expectedData = "f";
    expectedBeforeInputCancelable = true;
    sendString("f");
    is(textareaBeforeInput, 1, "beforeinput event should've been dispatched");
    is(textareaInput, 1, "input event should've been dispatched");
    textarea.blur();
    is(textareaBeforeInput, 1, "beforeinput event should not have been dispatched");
    is(textareaInput, 1, "input event should not have been dispatched");

    textarea.focus();
    textarea.value = 'foo';
    is(textareaBeforeInput, 1, "beforeinput event should not have been dispatched");
    is(textareaInput, 1, "input event should not have been dispatched");
    textarea.blur();
    is(textareaBeforeInput, 1, "beforeinput event should not have been dispatched");
    is(textareaInput, 1, "input event should not have been dispatched");

    textarea.focus();
    expectedInputType = "insertText";
    expectedData = "f";
    expectedBeforeInputCancelable = true;
    sendString("f");
    is(textareaBeforeInput, 2, "beforeinput event should've been dispatched");
    is(textareaInput, 2, "input event should've been dispatched");
    textarea.value = 'bar';
    is(textareaBeforeInput, 2, "beforeinput event should not have been dispatched");
    is(textareaInput, 2, "input event should not have been dispatched");
    expectedInputType = "deleteContentBackward";
    expectedData = null;
    expectedBeforeInputCancelable = true;
    synthesizeKey("KEY_Backspace");
    is(textareaBeforeInput, 3, "beforeinput event should've been dispatched");
    is(textareaInput, 3, "input event should've been dispatched");
    textarea.blur();
    is(textareaBeforeInput, 3, "beforeinput event should not have been dispatched");
    is(textareaInput, 3, "input event should not have been dispatched");

    // Non-text input tests:
    for (var i = 0; i < nonTextTypes.length; ++i) {
      // Button, submit, image and reset input type tests.
      if (i < 4) {
        input = document.getElementById("input_" + nonTextTypes[i]);
        input.focus();
        input.click();
        is(nonTextBeforeInput[i], 0, "beforeinput event doesn't apply");
        is(nonTextInput[i], 0, "input event doesn't apply");
        input.blur();
        is(nonTextBeforeInput[i], 0, "beforeinput event doesn't apply");
        is(nonTextInput[i], 0, "input event doesn't apply");
      }
      // For radio and checkboxes, input event should be dispatched.
      else {
        input = document.getElementById("input_" + nonTextTypes[i]);
        input.focus();
        input.click();
        is(nonTextBeforeInput[i], 0, "beforeinput event should not have been dispatched");
        is(nonTextInput[i], 1, "input event should've been dispatched");
        input.blur();
        is(nonTextBeforeInput[i], 0, "beforeinput event should not have been dispatched");
        is(nonTextInput[i], 1, "input event should not have been dispatched");

        // Test that input event is not dispatched if click event is cancelled.
        function preventDefault(e) {
          e.preventDefault();
        }
        input.addEventListener("click", preventDefault);
        input.click();
        is(nonTextBeforeInput[i], 0, "beforeinput event shouldn't be dispatched if click event is cancelled");
        is(nonTextInput[i], 1, "input event shouldn't be dispatched if click event is cancelled");
        input.removeEventListener("click", preventDefault);
      }
    }

    // Type changes.
    var input = document.createElement('input');
    input.type = 'text';
    input.value = 'foo';
    input.onbeforeinput = function () {
      ok(false, "we shouldn't get a beforeinput event when the type changes");
    };
    input.oninput = function() {
      ok(false, "we shouldn't get an input event when the type changes");
    };
    input.type = 'range';
    isnot(input.value, 'foo');

    // Tests for type='range'.
    var range = document.getElementById("input_range");

    range.focus();
    sendString("a");
    range.blur();
    is(rangeBeforeInput, 0, "beforeinput event shouldn't be dispatched on range input " +
                            "element for key changes that don't change its value");
    is(rangeInput, 0, "input event shouldn't be dispatched on range input " +
                      "element for key changes that don't change its value");

    range.focus();
    synthesizeKey("KEY_Home");
    is(rangeBeforeInput, 0, "beforeinput event shouldn't be dispatched even for key changes");
    is(rangeInput, 1, "input event should be dispatched for key changes");
    range.blur();
    is(rangeBeforeInput, 0, "beforeinput event shouldn't be dispatched on blur");
    is(rangeInput, 1, "input event shouldn't be dispatched on blur");

    range.focus();
    var bcr = range.getBoundingClientRect();
    var centerOfRangeX = bcr.width / 2;
    var centerOfRangeY = bcr.height / 2;
    synthesizeMouse(range, centerOfRangeX - 10, centerOfRangeY, { type: "mousedown" });
    is(rangeBeforeInput, 0, "beforeinput event shouldn't be dispatched on mousedown if the value changes");
    is(rangeInput, 2, "Input event should be dispatched on mousedown if the value changes");
    synthesizeMouse(range, centerOfRangeX - 5, centerOfRangeY, { type: "mousemove" });
    is(rangeBeforeInput, 0, "beforeinput event shouldn't be dispatched during a drag");
    is(rangeInput, 3, "Input event should be dispatched during a drag");
    synthesizeMouse(range, centerOfRangeX, centerOfRangeY, { type: "mouseup" });
    is(rangeBeforeInput, 0, "beforeinput event shouldn't be dispatched at the end of a drag");
    is(rangeInput, 4, "Input event should be dispatched at the end of a drag");

    // Tests for type='number'.
    // We only test key events here since input events for mouse event changes
    // are tested in test_input_number_mouse_events.html
    var number = document.getElementById("input_number");

    if (isDesktop) { // up/down arrow keys not supported on android
      number.value = "";
      number.focus();
      // <input type="number">'s inputType value hasn't been decided, see
      // https://github.com/w3c/input-events/issues/88
      expectedInputType = "insertReplacementText";
      expectedData = "1";
      expectedBeforeInputCancelable = false;
      synthesizeKey("KEY_ArrowUp");
      is(numberBeforeInput, 1, "beforeinput event should be dispatched for up/down arrow key keypress");
      is(numberInput, 1, "input event should be dispatched for up/down arrow key keypress");
      is(number.value, "1", "sanity check value of number control after keypress");

      // `data` will be the value of the input, but we can't change
      // `expectedData` and use {repeat: 3} at the same time.
      skipExpectedDataCheck = true;
      synthesizeKey("KEY_ArrowDown", {repeat: 3});
      is(numberBeforeInput, 4, "beforeinput event should be dispatched for each up/down arrow key keypress event, even when rapidly repeated");
      is(numberInput, 4, "input event should be dispatched for each up/down arrow key keypress event, even when rapidly repeated");
      is(number.value, "-2", "sanity check value of number control after multiple keydown events");
      skipExpectedDataCheck = false;

      number.blur();
      is(numberBeforeInput, 4, "beforeinput event shouldn't be dispatched on blur");
      is(numberInput, 4, "input event shouldn't be dispatched on blur");
    }

    MockFilePicker.cleanup();
    SimpleTest.finish();
  }

  SimpleTest.waitForExplicitFinish();
  document.addEventListener("DOMContentLoaded", () => {
    init();
    SimpleTest.waitForFocus(testUserInput);
  }, {once: true});

</script>
</pre>
</body>
</html>