2 <p>Test that pages and workers can send MessagePorts to one another.
3 Should print
"DONE" when done.
</p>
8 document
.getElementById("result").innerHTML
+= message
+ "<br>";
11 if (window
.testRunner
) {
12 testRunner
.dumpAsText();
13 testRunner
.waitUntilDone();
16 var worker
= new Worker("resources/worker-messageport.js");
17 var channel
= new MessageChannel();
19 // Send messages with and without ports to the worker to make sure it gets them.
20 worker
.postMessage("noport");
21 worker
.onmessage = function(evt
) {
23 worker
.postMessage("port", [channel
.port1
]);
24 worker
.onmessage = function(evt
) {
29 // Send a message on the new port to make sure it gets to the worker.
30 channel
.port2
.postMessage("ping");
32 // Wait for the response.
33 channel
.port2
.onmessage = function(evt
) {
34 if (evt
.data
== "pong") {
35 log("PASS: Received response from Worker via MessagePort");
36 worker
.onmessage
= awaitPortFromWorker
;
37 worker
.postMessage("getport");
39 log("FAIL: Received unknown event: " + evt
.data
);
42 channel
.port2
.start();
44 // Invoked once the first batch of tests are done, to test sending from the worker.
45 function awaitPortFromWorker(evt
)
47 if (evt
.data
== "port") {
49 log("FAIL: Did not get port from worker");
50 } else if (evt
.ports
.length
!= 1) {
51 log("FAIL: Got the wrong number of ports from worker: " + evt
.ports
.length
);
53 log("PASS: Got port from worker");
54 evt
.ports
[0].postMessage("ping");
55 evt
.ports
[0].onmessage = function(evt
) {
56 if (evt
.data
== "pong") {
57 log("PASS: Received final response from worker");
59 log("FAIL: Got unexpected response: " + evt
.data
);
70 function startSpamTest()
72 var channel
= new MessageChannel();
73 worker
.onmessage = function () { gotSpam(channel
.port1
); }
74 worker
.postMessage("spam", [channel
.port2
]);
77 function gotSpam(port
)
80 port
.onmessage = function(evt
) {
81 if (evt
.data
!= spamCount
)
82 log("FAIL: Got out of order message: " + spamCount
);
84 if (spamCount
== 1000) {
85 log("PASS: Got 1000 messages");
94 if (window
.testRunner
)
95 testRunner
.notifyDone();