summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/events/Event-dispatch-click.html
blob: ab4a24a5ad5096497ef65b075db89a146b38c115 (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
<!doctype html>
<title>Synthetic click event "magic"</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<div id=dump style=display:none></div>
<script>
var dump = document.getElementById("dump")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.click()
}, "basic with click()")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.dispatchEvent(new MouseEvent("click", {bubbles:true})) // equivalent to the above
}, "basic with dispatchEvent()")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_false(input.checked)
  })
  input.dispatchEvent(new Event("click", {bubbles:true})) // no MouseEvent
}, "basic with wrong event class")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var child = input.appendChild(new Text("does not matter"))
  child.dispatchEvent(new MouseEvent("click")) // does not bubble
  assert_false(input.checked)
  t.done()
}, "look at parents only when event bubbles")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  var child = input.appendChild(new Text("does not matter"))
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
}, "look at parents when event bubbles")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  input.onclick = t.step_func(function() {
    assert_false(input.checked, "input pre-click must not be triggered")
  })
  var child = input.appendChild(document.createElement("input"))
  child.type = "checkbox"
  child.onclick = t.step_func(function() {
    assert_true(child.checked, "child pre-click must be triggered")
  })
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
  t.done()
}, "pick the first with activation behavior <input type=checkbox>")

async_test(function(t) { // as above with <a>
  window.hrefComplete = t.step_func(function(a) {
    assert_equals(a, 'child');
    t.done();
  });
  var link = document.createElement("a")
  link.href = "javascript:hrefComplete('link')" // must not be triggered
  dump.appendChild(link)
  var child = link.appendChild(document.createElement("a"))
  child.href = "javascript:hrefComplete('child')"
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
}, "pick the first with activation behavior <a href>")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "radio"
  dump.appendChild(input)
  input.onclick = t.step_func(function() {
    assert_false(input.checked, "input pre-click must not be triggered")
  })
  var child = input.appendChild(document.createElement("input"))
  child.type = "radio"
  child.onclick = t.step_func(function() {
    assert_true(child.checked, "child pre-click must be triggered")
  })
  child.dispatchEvent(new MouseEvent("click", {bubbles:true}))
  t.done()
}, "pick the first with activation behavior <input type=radio>")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var clickEvent = new MouseEvent("click")
  input.onchange = t.step_func_done(function() {
    assert_false(clickEvent.defaultPrevented)
    assert_true(clickEvent.returnValue)
    assert_equals(clickEvent.eventPhase, 0)
    assert_equals(clickEvent.currentTarget, null)
    assert_equals(clickEvent.target, input)
    assert_equals(clickEvent.srcElement, input)
    assert_equals(clickEvent.composedPath().length, 0)
  })
  input.dispatchEvent(clickEvent)
}, "event state during post-click handling")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var clickEvent = new MouseEvent("click")
  var finalTarget = document.createElement("doesnotmatter")
  finalTarget.onclick = t.step_func_done(function() {
    assert_equals(clickEvent.target, finalTarget)
    assert_equals(clickEvent.srcElement, finalTarget)
  })
  input.onchange = t.step_func(function() {
    finalTarget.dispatchEvent(clickEvent)
  })
  input.dispatchEvent(clickEvent)
}, "redispatch during post-click handling")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  dump.appendChild(input)
  var child = input.appendChild(document.createElement("input"))
  child.type = "checkbox"
  child.disabled = true
  child.click()
  assert_false(input.checked)
  assert_false(child.checked)
  t.done()
}, "disabled checkbox still has activation behavior")

async_test(function(t) {
  var state = "start"

  var form = document.createElement("form")
  form.onsubmit = t.step_func(() => {
    if(state == "start" || state == "checkbox") {
      state = "failure"
    } else if(state == "form") {
      state = "done"
    }
    return false
  })
  dump.appendChild(form)
  var button = form.appendChild(document.createElement("button"))
  button.type = "submit"
  var checkbox = button.appendChild(document.createElement("input"))
  checkbox.type = "checkbox"
  checkbox.onclick = t.step_func(() => {
    if(state == "start") {
      assert_unreached()
    } else if(state == "checkbox") {
      assert_true(checkbox.checked)
    }
  })
  checkbox.disabled = true
  checkbox.click()
  assert_equals(state, "start")

  state = "checkbox"
  checkbox.disabled = false
  checkbox.click()
  assert_equals(state, "checkbox")

  state = "form"
  button.click()
  assert_equals(state, "done")

  t.done()
}, "disabled checkbox still has activation behavior, part 2")

async_test(function(t) {
  var state = "start"

  var form = document.createElement("form")
  form.onsubmit = t.step_func(() => {
    if(state == "start" || state == "radio") {
      state = "failure"
    } else if(state == "form") {
      state = "done"
    }
    return false
  })
  dump.appendChild(form)
  var button = form.appendChild(document.createElement("button"))
  button.type = "submit"
  var radio = button.appendChild(document.createElement("input"))
  radio.type = "radio"
  radio.onclick = t.step_func(() => {
    if(state == "start") {
      assert_unreached()
    } else if(state == "radio") {
      assert_true(radio.checked)
    }
  })
  radio.disabled = true
  radio.click()
  assert_equals(state, "start")

  state = "radio"
  radio.disabled = false
  radio.click()
  assert_equals(state, "radio")

  state = "form"
  button.click()
  assert_equals(state, "done")

  t.done()
}, "disabled radio still has activation behavior")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "checkbox"
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.click()
}, "disconnected checkbox should be checked")

async_test(function(t) {
  var input = document.createElement("input")
  input.type = "radio"
  input.onclick = t.step_func_done(function() {
    assert_true(input.checked)
  })
  input.click()
}, "disconnected radio should be checked")

async_test(t => {
  const input = document.createElement('input');
  input.type = 'checkbox';
  input.onclick = t.step_func_done(() => {
    assert_true(input.checked);
  });
  input.dispatchEvent(new MouseEvent('click'));
}, `disconnected checkbox should be checked from dispatchEvent(new MouseEvent('click'))`);

async_test(t => {
  const input = document.createElement('input');
  input.type = 'radio';
  input.onclick = t.step_func_done(() => {
    assert_true(input.checked);
  });
  input.dispatchEvent(new MouseEvent('click'));
}, `disconnected radio should be checked from dispatchEvent(new MouseEvent('click'))`);

test(() => {
  const input = document.createElement("input");
  input.type = "checkbox";
  input.disabled = true;
  input.dispatchEvent(new MouseEvent("click"));
  assert_true(input.checked);
}, `disabled checkbox should be checked from dispatchEvent(new MouseEvent("click"))`);

test(() => {
  const input = document.createElement("input");
  input.type = "radio";
  input.disabled = true;
  input.dispatchEvent(new MouseEvent("click"));
  assert_true(input.checked);
}, `disabled radio should be checked from dispatchEvent(new MouseEvent("click"))`);

async_test(t => {
  const input = document.createElement("input");
  input.type = "checkbox";
  input.disabled = true;
  input.onclick = t.step_func_done();
  input.dispatchEvent(new MouseEvent("click"));
}, `disabled checkbox should fire onclick`);

async_test(t => {
  const input = document.createElement("input");
  input.type = "radio";
  input.disabled = true;
  input.onclick = t.step_func_done();
  input.dispatchEvent(new MouseEvent("click"));
}, `disabled radio should fire onclick`);

async_test(t => {
  const input = document.createElement("input");
  input.type = "checkbox";
  input.disabled = true;
  input.onclick = t.step_func(ev => {
    assert_true(input.checked);
    ev.preventDefault();
    queueMicrotask(t.step_func_done(() => {
      assert_false(input.checked);
    }));
  });
  input.dispatchEvent(new MouseEvent("click", { cancelable: true }));
}, `disabled checkbox should get legacy-canceled-activation behavior`);

async_test(t => {
  const input = document.createElement("input");
  input.type = "radio";
  input.disabled = true;
  input.onclick = t.step_func(ev => {
    assert_true(input.checked);
    ev.preventDefault();
    queueMicrotask(t.step_func_done(() => {
      assert_false(input.checked);
    }));
  });
  input.dispatchEvent(new MouseEvent("click", { cancelable: true }));
}, `disabled radio should get legacy-canceled-activation behavior`);

test(t => {
  const input = document.createElement("input");
  input.type = "checkbox";
  input.disabled = true;
  const ev = new MouseEvent("click", { cancelable: true });
  ev.preventDefault();
  input.dispatchEvent(ev);
  assert_false(input.checked);
}, `disabled checkbox should get legacy-canceled-activation behavior 2`);

test(t => {
  const input = document.createElement("input");
  input.type = "radio";
  input.disabled = true;
  const ev = new MouseEvent("click", { cancelable: true });
  ev.preventDefault();
  input.dispatchEvent(ev);
  assert_false(input.checked);
}, `disabled radio should get legacy-canceled-activation behavior 2`);

for (const type of ["checkbox", "radio"]) {
  for (const handler of ["oninput", "onchange"]) {
    async_test(t => {
      const input = document.createElement("input");
      input.type = type;
      input.onclick = t.step_func(ev => {
        input.disabled = true;
      });
      input[handler] = t.step_func(ev => {
        assert_equals(input.checked, true);
        t.done();
      });
      dump.append(input);
      input.click();
    }, `disabling ${type} in onclick listener shouldn't suppress ${handler}`);
  }
}

async_test(function(t) {
  var form = document.createElement("form")
  var didSubmit = false
  form.onsubmit = t.step_func(() => {
    didSubmit = true
    return false
  })
  var input = form.appendChild(document.createElement("input"))
  input.type = "submit"
  input.click()
  assert_false(didSubmit)
  t.done()
}, "disconnected form should not submit")

async_test(t => {
  const form = document.createElement("form");
  form.onsubmit = t.step_func(ev => {
    ev.preventDefault();
    assert_unreached("The form is unexpectedly submitted.");
  });
  dump.append(form);
  const input = form.appendChild(document.createElement("input"));
  input.type = "submit"
  input.disabled = true;
  input.dispatchEvent(new MouseEvent("click", { cancelable: true }));
  t.done();
}, "disabled submit button should not activate");

async_test(t => {
  const form = document.createElement("form");
  form.onsubmit = t.step_func(ev => {
    ev.preventDefault();
    assert_unreached("The form is unexpectedly submitted.");
  });
  dump.append(form);
  const input = form.appendChild(document.createElement("input"));
  input.onclick = t.step_func(() => {
    input.disabled = true;
  });
  input.type = "submit"
  input.dispatchEvent(new MouseEvent("click", { cancelable: true }));
  t.done();
}, "submit button should not activate if the event listener disables it");

async_test(t => {
  const form = document.createElement("form");
  form.onsubmit = t.step_func(ev => {
    ev.preventDefault();
    assert_unreached("The form is unexpectedly submitted.");
  });
  dump.append(form);
  const input = form.appendChild(document.createElement("input"));
  input.onclick = t.step_func(() => {
    input.type = "submit"
    input.disabled = true;
  });
  input.click();
  t.done();
}, "submit button that morphed from checkbox should not activate");
</script>