dom/base/test/test_messageChannel_start.html

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:eb07af8dc549
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=677638
5 -->
6 <head>
7 <meta charset="utf-8">
8 <title>Test for Bug 677638 - start/close</title>
9 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
11 </head>
12 <body>
13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a>
14 <div id="content"></div>
15 <pre id="test">
16 </pre>
17 <script type="application/javascript">
18
19 function runTests() {
20 if (!tests.length) {
21 SimpleTest.finish();
22 return;
23 }
24
25 var test = tests.shift();
26 test();
27 }
28
29 function testOnMessage() {
30 var a = new MessageChannel();
31 ok(a, "MessageChannel created");
32
33 a.port1.postMessage(42);
34 a.port2.postMessage(43);
35 ok(true, "MessagePort{1,2}.postmessage() invoked");
36
37 var events = 2;
38
39 a.port1.onmessage = function(evt) {
40 ok(true, "This method should be called");
41 if (!--events) runTests();
42 }
43
44 a.port2.onmessage = function(evt) {
45 ok(true, "This method should be called");
46 if (!--events) runTests();
47 }
48 }
49
50 function testAddEventListener() {
51 var a = new MessageChannel();
52 ok(a, "MessageChannel created");
53
54 a.port1.postMessage(42);
55 a.port2.postMessage(43);
56 ok(true, "MessagePort{1,2}.postmessage() invoked");
57
58 a.port1.addEventListener('message', function(evt) {
59 ok(false, "This method should not be called");
60 }, false);
61
62 a.port2.addEventListener('message', function(evt) {
63 ok(false, "This method should not be called");
64 }, false);
65
66 setTimeout(runTests, 0);
67 }
68
69 function testAddEventListenerAndStart() {
70 var a = new MessageChannel();
71 ok(a, "MessageChannel created");
72
73 a.port1.postMessage(42);
74 a.port2.postMessage(43);
75 ok(true, "MessagePort{1,2}.postmessage() invoked");
76
77 var events = 2;
78
79 a.port1.addEventListener('message', function(evt) {
80 ok(true, "This method should be called");
81 if (!--events) runTests();
82 }, false);
83
84 a.port2.addEventListener('message', function(evt) {
85 ok(true, "This method should be called");
86 if (!--events) runTests();
87 }, false);
88
89 a.port1.start();
90 a.port2.start();
91 }
92
93 function testAddEventListener1AndStart() {
94 var a = new MessageChannel();
95 ok(a, "MessageChannel created");
96
97 a.port1.postMessage(42);
98 a.port2.postMessage(43);
99 ok(true, "MessagePort{1,2}.postmessage() invoked");
100
101 var events = 1;
102
103 a.port1.addEventListener('message', function(evt) {
104 ok(true, "This method should be called");
105 if (!--events) runTests();
106 }, false);
107
108 a.port2.addEventListener('message', function(evt) {
109 ok(false, "This method should not be called");
110 }, false);
111
112 a.port1.start();
113 }
114
115 function testAddEventListener2AndStart() {
116 var a = new MessageChannel();
117 ok(a, "MessageChannel created");
118
119 a.port1.postMessage(42);
120 a.port2.postMessage(43);
121 ok(true, "MessagePort{1,2}.postmessage() invoked");
122
123 var events = 1;
124
125 a.port1.addEventListener('message', function(evt) {
126 ok(false, "This method should not be called");
127 }, false);
128
129 a.port2.addEventListener('message', function(evt) {
130 ok(true, "This method should be called");
131 if (!--events) runTests();
132 }, false);
133
134 a.port2.start();
135 }
136
137 function testTimer() {
138 var a = new MessageChannel();
139 ok(a, "MessageChannel created");
140
141 a.port1.postMessage(42);
142 a.port2.postMessage(43);
143 ok(true, "MessagePort{1,2}.postmessage() invoked");
144
145 setTimeout(function() {
146 var events = 2;
147 a.port1.onmessage = function(evt) {
148 ok(true, "This method should be called");
149 if (!--events) runTests();
150 }
151
152 a.port2.onmessage = function(evt) {
153 ok(true, "This method should be called");
154 if (!--events) runTests();
155 }
156 }, 200);
157 }
158
159 function testAddEventListenerAndStartWrongOrder() {
160 var a = new MessageChannel();
161 ok(a, "MessageChannel created");
162
163 a.port1.postMessage(42);
164 a.port2.postMessage(43);
165 ok(true, "MessagePort{1,2}.postmessage() invoked");
166
167 var events = 2;
168
169 a.port1.start();
170 a.port1.addEventListener('message', function(evt) {
171 ok(true, "This method should be called");
172 if (!--events) runTests();
173 }, false);
174
175 a.port2.start();
176 a.port2.addEventListener('message', function(evt) {
177 ok(true, "This method should be called");
178 if (!--events) runTests();
179 }, false);
180 }
181
182 function testOnMessageClone() {
183 var a = new MessageChannel();
184 ok(a, "MessageChannel created");
185
186 a.port1.postMessage(42);
187 a.port2.postMessage(43);
188 ok(true, "MessagePort{1,2}.postmessage() invoked");
189
190 var events = 2;
191
192 addEventListener('message', testOnMessageCloneCb, false);
193 function testOnMessageCloneCb(evt) {
194 a.port1.onmessage = function(evt) {
195 ok(true, "This method should be called");
196 testOnMessageCloneFinish();
197 }
198
199 evt.data.onmessage = function(evt) {
200 ok(true, "This method should be called");
201 testOnMessageCloneFinish();
202 }
203
204 a.port2.onmessage = function(evt) {
205 ok(false, "This method should not be called");
206 }
207 }
208
209 function testOnMessageCloneFinish() {
210 if (!--events) {
211 removeEventListener('message', testOnMessageCloneCb);
212 runTests();
213 }
214 }
215
216 postMessage(a.port2, '*', [a.port2]);
217 }
218
219 var tests = [
220 testOnMessage,
221 testAddEventListener,
222 testAddEventListenerAndStart,
223 testAddEventListener1AndStart,
224 testAddEventListener2AndStart,
225 testTimer,
226 testAddEventListenerAndStartWrongOrder,
227 testOnMessageClone,
228 ];
229
230 SimpleTest.waitForExplicitFinish();
231 SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTests);
232 </script>
233 </body>
234 </html>

mercurial