Web Inspector: ES6: Show Symbol properties on Objects
[WebKit-https.git] / LayoutTests / inspector / event-listener.html
1 <!doctype html>
2 <html>
3 <head>
4 <script type="text/javascript" src="../http/tests/inspector/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     const KaboomEvent = "kaboom";
9     var emitter = new WebInspector.Object();
10     var context = new WebInspector.Object();
11     var data = [1, 2, 3];
12
13     function kaboomCallback(event) {
14         InspectorTest.assert(this === context, "Callback invoked with wrong |this| binding.");
15         InspectorTest.assert(event.target === emitter, "Callback invoked with wrong event emitter.");
16         InspectorTest.assert(event.data === data, "Callback invoked with wrong event data.");
17
18         InspectorTest.log("Invoked callback for kaboom event.");
19     }
20
21     // Test for multiple firings of the listener.
22
23     var listener = new WebInspector.EventListener(context);
24     InspectorTest.assert(!emitter.hasEventListeners(KaboomEvent), "Emitter should not have any listeners.");
25     emitter.dispatchEventToListeners(KaboomEvent, data); // Should not fire anything.
26
27     InspectorTest.log("Connecting the listener.");
28     listener.connect(emitter, KaboomEvent, kaboomCallback);
29     InspectorTest.assert(emitter.hasEventListeners(KaboomEvent), "Emitter should have a listener.");
30     emitter.dispatchEventToListeners(KaboomEvent, data); // Should fire.
31     emitter.dispatchEventToListeners(KaboomEvent, data); // Should fire.
32     InspectorTest.log("Disconnecting the listener.");
33     listener.disconnect();
34
35     emitter.dispatchEventToListeners(KaboomEvent, data); // Should not fire anything.
36     InspectorTest.assert(!emitter.hasEventListeners(KaboomEvent), "Emitter should not have any listeners.");
37
38     // Test reconnection.
39
40     InspectorTest.log("Connecting the listener.");
41     listener.connect(emitter, KaboomEvent, kaboomCallback);
42     emitter.dispatchEventToListeners(KaboomEvent, data); // Should fire.
43     InspectorTest.log("Disconnecting the listener.");
44     listener.disconnect();
45
46     // Test unused listener.
47
48     InspectorTest.log("Connecting the listener.");
49     listener.connect(emitter, KaboomEvent, kaboomCallback);
50     InspectorTest.log("Disconnecting the listener.");
51     listener.disconnect();
52
53     // Test for single firing of the listener.
54
55     var singleListener = new WebInspector.EventListener(context, true);
56     InspectorTest.assert(!emitter.hasEventListeners(KaboomEvent), "Emitter should not have any listeners.");
57     emitter.dispatchEventToListeners(KaboomEvent, data); // Should not fire anything.
58
59     InspectorTest.log("Connecting the single-fire listener.");
60     singleListener.connect(emitter, KaboomEvent, kaboomCallback);
61     InspectorTest.assert(emitter.hasEventListeners(KaboomEvent), "Emitter should have a listener.");
62     emitter.dispatchEventToListeners(KaboomEvent, data); // Should fire.
63     InspectorTest.assert(!emitter.hasEventListeners(KaboomEvent), "Emitter should not have any listeners.");
64     emitter.dispatchEventToListeners(KaboomEvent, data); // Should not fire.
65     InspectorTest.log("Disconnecting the single-fire listener.");
66     singleListener.disconnect(); // Should cause an error.
67     emitter.dispatchEventToListeners(KaboomEvent, data); // Should not fire.
68
69     // Test for various error cases and abuse.
70
71     var badListener = new WebInspector.EventListener(context);
72     badListener.connect(data, data, data); // Should complain about non-callable callback.
73     badListener.connect(null, KaboomEvent, kaboomCallback); // Should complain about non-callable callback.
74     badListener.connect(emitter, KaboomEvent, null); // Should complain about non-callable callback.
75     badListener.connect(emitter, null, kaboomCallback); // Should complain about null event.
76
77     var badListener2 = new WebInspector.EventListener(context);
78     badListener2.disconnect(); // Should complain about already disconnected.
79     badListener2.connect(emitter, KaboomEvent, kaboomCallback);
80     badListener2.connect(emitter, KaboomEvent, kaboomCallback); // Should complain about already connected.
81     emitter.dispatchEventToListeners(KaboomEvent, data); // Should fire.
82     badListener2.connect(emitter, KaboomEvent, kaboomCallback); // Should complain about already connected.
83     badListener2.disconnect();
84     badListener2.disconnect(); // Should complain about already disconnected.
85
86     InspectorTest.completeTest();
87 }
88 </script>
89 </head>
90 <body onload="runTest()">
91     <p>Testing basic functionality of WebInspector.EventListener.</p>
92 </body>
93 </html>