1<!DOCTYPE html>
2<html>
3<head>
4<script src="../js/resources/js-test-pre.js"></script>
5</head>
6<body>
7<p class="description">
8Tests that dispatchEvent raises DISPATCH_REQUEST_ERR if the event
9being dispatched is already being dispatched.
10</p>
11<pre id="console">
12</pre>
13<script>
14if (window.layoutTestController)
15 layoutTestController.dumpAsText();
16
17jsTestIsAsync = true;
18
19function shouldBeDispatchRequestErr(exception) {
20 var ok = EventException.prototype.isPrototypeOf(exception) && exception.code == EventException.DISPATCH_REQUEST_ERR;
21 (ok ? testPassed : testFailed)("should have got DISPATCH_REQUEST_ERR EventException");
22}
23
24// try redispatching an event in the process of being dispatched with
25// dispatchEvent
26
27function 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);
33 }
34
35 redispatchCustom.wasInvoked = true;
36}
37
38var customEvent = document.createEvent('CustomEvent');
39customEvent.initCustomEvent('foo', true, true, null);
40var p = document.querySelector('.description');
41p.addEventListener('foo', redispatchCustom);
42p.dispatchEvent(customEvent);
43shouldBeTrue('redispatchCustom.wasInvoked');
44
45// try redispatching an event that has already finished being dispatched
46
47function checkCustom(event) {
48 checkCustom.wasInvoked = true;
49}
50
51p.removeEventListener('foo', redispatchCustom, true);
52p.addEventListener('foo', checkCustom, true);
53p.dispatchEvent(customEvent);
54shouldBeTrue('checkCustom.wasInvoked');
55
56// try redispatching an event in the process of being dispatched by
57// the browser
58
59function redispatchLoad(event) {
60 if (redispatchLoad.dispatching) {
61 testFailed('dispatchEvent of an event being dispatched should not dispatch the event again');
62 return;
63 }
64
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;
73 }
74
75 finishJSTest();
76}
77
78window.addEventListener('load', redispatchLoad, true);
79
80var successfullyParsed = true;
81</script>
82<script src="../js/resources/js-test-post.js"></script>
83</body>
84</html>