|
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 - port cloning</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 runTest() { |
|
20 var MAX = 100; |
|
21 |
|
22 var a = new MessageChannel(); |
|
23 ok(a, "MessageChannel created"); |
|
24 |
|
25 // Populate the message queue of this port. |
|
26 for (var i = 0; i < MAX; ++i) { |
|
27 a.port1.postMessage(i); |
|
28 } |
|
29 |
|
30 window.addEventListener('message', receiveMessage, false); |
|
31 function receiveMessage(evt) { |
|
32 |
|
33 // This test sends the port from this window to the iframe and viceversa. |
|
34 if (evt.data.type == 'PORT') { |
|
35 var port = evt.data.port; |
|
36 var counter = 0; |
|
37 port.onmessage = function(evt) { |
|
38 // only 1 message should be received by this port. |
|
39 if (counter++ == 0) { |
|
40 ok(evt.data % 2, "The number " + evt.data + " has been received correctly by the main window"); |
|
41 |
|
42 if (evt.data < MAX - 1) { |
|
43 ifr.contentWindow.postMessage({ type: 'PORT', port: port }, '*', [port]); |
|
44 } else { |
|
45 SimpleTest.finish(); |
|
46 } |
|
47 } else { |
|
48 ok(false, "Wrong message!"); |
|
49 } |
|
50 } |
|
51 } else if (evt.data.type == 'OK') { |
|
52 ok(true, evt.data.msg); |
|
53 } else if (evt.data.type == 'KO') { |
|
54 ok(false, evt.data.msg); |
|
55 } else { |
|
56 ok(false, "Unknown message"); |
|
57 } |
|
58 } |
|
59 |
|
60 var div = document.getElementById("content"); |
|
61 ok(div, "Parent exists"); |
|
62 |
|
63 var ifr = document.createElement("iframe"); |
|
64 ifr.addEventListener("load", iframeLoaded, false); |
|
65 ifr.setAttribute('src', "iframe_messageChannel_pingpong.html"); |
|
66 div.appendChild(ifr); |
|
67 |
|
68 function iframeLoaded() { |
|
69 ifr.contentWindow.postMessage({ type: 'PORT', port: a.port2 }, '*', [a.port2]); |
|
70 } |
|
71 } |
|
72 |
|
73 SimpleTest.waitForExplicitFinish(); |
|
74 SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTest); |
|
75 </script> |
|
76 </body> |
|
77 </html> |