levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 1 | <body> |
| 2 | <p>Test that pages and workers can send MessagePorts to one another. |
| 3 | Should print "DONE" when done.</p> |
| 4 | <div id=result></div> |
| 5 | <script> |
| 6 | function log(message) |
| 7 | { |
| 8 | document.getElementById("result").innerHTML += message + "<br>"; |
| 9 | } |
| 10 | |
rniwa@webkit.org | 224c8b5 | 2012-08-04 01:13:22 +0000 | [diff] [blame] | 11 | if (window.testRunner) { |
| 12 | testRunner.dumpAsText(); |
| 13 | testRunner.waitUntilDone(); |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | var worker = new Worker("resources/worker-messageport.js"); |
| 17 | var channel = new MessageChannel(); |
| 18 | |
| 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) { |
| 22 | log(evt.data); |
atwilson@chromium.org | 271695b | 2009-09-03 18:36:10 +0000 | [diff] [blame] | 23 | worker.postMessage("port", [channel.port1]); |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 24 | worker.onmessage = function(evt) { |
| 25 | log(evt.data); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | // Send a message on the new port to make sure it gets to the worker. |
| 30 | channel.port2.postMessage("ping"); |
| 31 | |
| 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"); |
| 38 | } else { |
| 39 | log("FAIL: Received unknown event: " + evt.data); |
| 40 | } |
| 41 | } |
| 42 | channel.port2.start(); |
| 43 | |
| 44 | // Invoked once the first batch of tests are done, to test sending from the worker. |
| 45 | function awaitPortFromWorker(evt) |
| 46 | { |
| 47 | if (evt.data == "port") { |
atwilson@chromium.org | 271695b | 2009-09-03 18:36:10 +0000 | [diff] [blame] | 48 | if (!evt.ports) { |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 49 | log("FAIL: Did not get port from worker"); |
atwilson@chromium.org | 271695b | 2009-09-03 18:36:10 +0000 | [diff] [blame] | 50 | } else if (evt.ports.length != 1) { |
| 51 | log("FAIL: Got the wrong number of ports from worker: " + evt.ports.length); |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 52 | } else { |
| 53 | log("PASS: Got port from worker"); |
cdumez@apple.com | efa8d8e | 2016-06-08 22:14:55 +0000 | [diff] [blame] | 54 | try { |
| 55 | // Missing parameter, should throw. |
| 56 | evt.ports[0].postMessage(); |
| 57 | log("FAIL: Calling MessagePort.postMessage() without parameter did not throw."); |
| 58 | } catch (e) { |
| 59 | log("PASS: Calling MessagePort.postMessage() without parameter threw exception: " + e); |
| 60 | } |
atwilson@chromium.org | 271695b | 2009-09-03 18:36:10 +0000 | [diff] [blame] | 61 | evt.ports[0].postMessage("ping"); |
| 62 | evt.ports[0].onmessage = function(evt) { |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 63 | if (evt.data == "pong") { |
| 64 | log("PASS: Received final response from worker"); |
| 65 | } else { |
| 66 | log("FAIL: Got unexpected response: " + evt.data); |
| 67 | } |
| 68 | startSpamTest(); |
| 69 | } |
atwilson@chromium.org | 271695b | 2009-09-03 18:36:10 +0000 | [diff] [blame] | 70 | evt.ports[0].start(); |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 71 | } |
| 72 | } else { |
| 73 | log(evt.data); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | function startSpamTest() |
| 78 | { |
| 79 | var channel = new MessageChannel(); |
| 80 | worker.onmessage = function () { gotSpam(channel.port1); } |
atwilson@chromium.org | 271695b | 2009-09-03 18:36:10 +0000 | [diff] [blame] | 81 | worker.postMessage("spam", [channel.port2]); |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | function gotSpam(port) |
| 85 | { |
| 86 | var spamCount = 0; |
| 87 | port.onmessage = function(evt) { |
| 88 | if (evt.data != spamCount) |
| 89 | log("FAIL: Got out of order message: " + spamCount); |
| 90 | spamCount++; |
| 91 | if (spamCount == 1000) { |
| 92 | log("PASS: Got 1000 messages"); |
| 93 | done(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | function done() |
| 99 | { |
| 100 | log("DONE"); |
rniwa@webkit.org | 224c8b5 | 2012-08-04 01:13:22 +0000 | [diff] [blame] | 101 | if (window.testRunner) |
| 102 | testRunner.notifyDone(); |
levin@chromium.org | 0ea1cd8 | 2009-06-30 17:32:41 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | </script> |
| 106 | </body> |
| 107 | </html> |