[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.22
[framework/web/webkit-efl.git] / LayoutTests / fast / events / dispatch-event-being-dispatched.html
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">
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.layoutTestController)
15     layoutTestController.dumpAsText();
16
17 jsTestIsAsync = true;
18
19 function 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
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);
33     }
34
35     redispatchCustom.wasInvoked = true;
36 }
37
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');
44
45 // try redispatching an event that has already finished being dispatched
46
47 function checkCustom(event) {
48     checkCustom.wasInvoked = true;
49 }
50
51 p.removeEventListener('foo', redispatchCustom, true);
52 p.addEventListener('foo', checkCustom, true);
53 p.dispatchEvent(customEvent);
54 shouldBeTrue('checkCustom.wasInvoked');
55
56 // try redispatching an event in the process of being dispatched by
57 // the browser
58
59 function 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
78 window.addEventListener('load', redispatchLoad, true);
79 </script>
80 <script src="../js/resources/js-test-post.js"></script>
81 </body>
82 </html>