Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / events / dispatch-event-being-dispatched.html
blob37fae28835750046c4a6e58355b10d21685b2ec1
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test.js"></script>
5 </head>
6 <body>
7 <p class="description">
8 Tests that dispatchEvent raises DISPATCH_REQUEST_ERR if the event
9 being dispatched is already being dispatched.
10 </p>
11 <pre id="console">
12 </pre>
13 <script>
14 if (window.testRunner)
15 testRunner.dumpAsText();
17 jsTestIsAsync = true;
19 function shouldBeDispatchRequestErr(exception) {
20 var ok = Object.getPrototypeOf(exception) == DOMException.prototype && exception.name == 'InvalidStateError';
21 (ok ? testPassed : testFailed)("should have got DISPATCH_REQUEST_ERR EventException");
24 // try redispatching an event in the process of being dispatched with
25 // dispatchEvent
27 function redispatchCustom(event) {
28 try {
29 window.dispatchEvent(event);
30 testFailed('dispatchEvent of an event being dispatched should throw an exception');
31 } catch (ex) {
32 shouldBeDispatchRequestErr(ex);
35 redispatchCustom.wasInvoked = true;
38 var customEvent = document.createEvent('CustomEvent');
39 customEvent.initCustomEvent('foo', true, true, null);
40 var p = document.querySelector('.description');
41 p.addEventListener('foo', redispatchCustom);
42 p.dispatchEvent(customEvent);
43 shouldBeTrue('redispatchCustom.wasInvoked');
45 // try redispatching an event that has already finished being dispatched
47 function checkCustom(event) {
48 checkCustom.wasInvoked = true;
51 p.removeEventListener('foo', redispatchCustom, true);
52 p.addEventListener('foo', checkCustom, true);
53 p.dispatchEvent(customEvent);
54 shouldBeTrue('checkCustom.wasInvoked');
56 // try redispatching an event in the process of being dispatched by
57 // the browser
59 function redispatchLoad(event) {
60 if (redispatchLoad.dispatching) {
61 testFailed('dispatchEvent of an event being dispatched should not dispatch the event again');
62 return;
65 try {
66 redispatchLoad.dispatching = true;
67 document.dispatchEvent(event);
68 testFailed('dispatchEvent of an event being dispatched should throw an exception');
69 } catch (ex) {
70 shouldBeDispatchRequestErr(ex);
71 } finally {
72 delete redispatchLoad.dispatching;
75 finishJSTest();
78 window.addEventListener('load', redispatchLoad, true);
79 </script>
80 </body>
81 </html>