Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / events / message-port.html
blob36dc0a160537686a65d295c36fd2bfefff586507
1 <body>
2 <p>Test cross-frame MessagePort communication.</p>
3 <p>Should be a series of SUCCESS messages, followed with DONE.</p>
4 <pre id=log></pre>
5 <script>
6 function log(message)
8 document.getElementById("log").innerHTML += message + "<br>";
11 if (window.testRunner) {
12 testRunner.dumpAsText();
13 testRunner.waitUntilDone();
16 var mainPort;
18 var allTests;
19 var currentTest = 0;
21 function nextTest()
23 var fun = "postMessage";
24 var testFunNo = Math.floor(currentTest / 2);
25 if (testFunNo < allTests.length)
27 var test = allTests[testFunNo];
28 currentTest++;
29 test(fun);
31 else
33 done();
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");
47 else
48 log("Simple exchange: FAIL. Got message '" + evt.data + "' from '" + evt.origin + "'.");
49 nextTest();
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");
60 else
61 log("Posted port: FAIL. Got message '" + evt.data + "' from '" + evt.origin + "'.");
62 nextTest();
66 function test3(postMessageFun)
68 var channel = new MessageChannel;
69 channel.port1[postMessageFun]("1");
70 var started = false;
71 channel.port2.addEventListener("message", function(evt) {
72 if (started) {
73 log("Event Listener: SUCCESS");
74 nextTest();
75 } else
76 log("Event Listener: FAIL. Event listener called before start()");
77 });
78 setTimeout(function() {
79 channel.port2.start();
80 started = true;
81 }, 100);
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");
94 nextTest();
95 } else
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");
105 nextTest();
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"));
114 nextTest();
118 function done() {
119 // Wait a short period of time to ensure no messages come in from previous tests.
120 setTimeout(function() {
121 log("DONE");
122 if (window.testRunner)
123 testRunner.notifyDone();
124 }, 100);
127 allTests = [ test, test2, test3, test4, test5, test6 ]
129 </script>
130 <iframe src="resources/message-port-iframe.html" onload="nextTest()"></iframe>
131 </body>