2 <p>Test cross-frame MessagePort communication.</p>
3 <p>Should be a series of SUCCESS messages, followed with DONE.</p>
8 document.getElementById("log").innerHTML += message + "<br>";
11 if (window.testRunner) {
12 testRunner.dumpAsText();
13 testRunner.waitUntilDone();
23 var fun = currentTest % 2 == 0 ? "postMessage" : "webkitPostMessage";
24 var testFunNo = Math.floor(currentTest / 2);
25 if (testFunNo < allTests.length)
27 var test = allTests[testFunNo];
37 function test(postMessageFun)
39 var channel = new MessageChannel;
41 window.frames[0][postMessageFun]("msg", [channel.port2], "*");
42 mainPort = channel.port1;
43 mainPort[postMessageFun]("ping");
44 mainPort.onmessage = function(evt) {
45 if (evt.data == "pong" && evt.origin == "")
46 log("Simple exchange: SUCCESS");
48 log("Simple exchange: FAIL. Got message '" + evt.data + "' from '" + evt.origin + "'.");
53 function test2(postMessageFun)
55 var channel = new MessageChannel;
56 mainPort[postMessageFun]("newPort", [channel.port2]);
57 channel.port1.onmessage = function(evt) {
58 if (evt.data == "yo" && evt.origin == "")
59 log("Posted port: SUCCESS");
61 log("Posted port: FAIL. Got message '" + evt.data + "' from '" + evt.origin + "'.");
66 function test3(postMessageFun)
68 var channel = new MessageChannel;
69 channel.port1[postMessageFun]("1");
71 channel.port2.addEventListener("message", function(evt) {
73 log("Event Listener: SUCCESS");
76 log("Event Listener: FAIL. Event listener called before start()");
78 setTimeout(function() {
79 channel.port2.start();
85 function test4(postMessageFun)
87 var channel = new MessageChannel;
88 channel.port1[postMessageFun]("1");
89 channel.port1.close();
90 channel.port1[postMessageFun]("2");
91 channel.port2.onmessage = function(evt) {
92 if (evt.data == "1") {
93 log("Closed port: SUCCESS");
96 log("Closed port: FAIL. Got message: " + evt.data + " after close");
100 function test5(postMessageFun)
102 // Create a channel and put some messages in it which will go undelivered and should not leak
103 var channel = new MessageChannel;
104 channel.port1[postMessageFun]("abcde");
108 function test6(postMessageFun)
110 var channel = new MessageChannel;
111 channel.port1[postMessageFun]("howdy");
112 channel.port2.onmessage = function(event) {
113 log("MessageEvent.cancelable defaults to " + event.cancelable + ": " + (event.cancelable ? "FAIL" : "PASS"));
119 // Wait a short period of time to ensure no messages come in from previous tests.
120 setTimeout(function() {
122 if (window.testRunner)
123 testRunner.notifyDone();
127 allTests = [ test, test2, test3, test4, test5, test6 ]
130 <iframe src="resources/message-port-iframe.html" onload="nextTest()"></iframe>