2 <p>Tests various use cases when cloning MessagePorts.</p>
3 <p>Should be a series of SUCCESS messages, followed with DONE.</p>
9 if (window.GCController)
10 return GCController.collect();
12 for (var i = 0; i < 10000; i++) { // force garbage collection (FF requires about 9K allocations before a collect).
13 var s = new String("abc");
19 document.getElementById("log").innerHTML += message + "<br>";
22 if (window.testRunner) {
23 testRunner.dumpAsText();
24 testRunner.waitUntilDone();
27 var channel = new MessageChannel;
28 channel.port1.onmessage = channel.port2.onmessage = function(evt) {
29 fail("FAIL: Should not have received message: " + evt.data);
32 channel.port1.postMessage("msg", [channel.port1]);
33 log("FAIL: Posting port to itself should throw an exception.");
35 log("SUCCESS: Posting port to itself: " + ex);
39 channel.port1.postMessage("msg", [channel.port2]);
40 log("FAIL: Posting port to entangled pair should throw an exception.");
42 log("SUCCESS: Posting entangled port: " + ex);
45 channel = new MessageChannel;
46 var channel2 = new MessageChannel;
47 channel.port1.postMessage("msg", [channel2.port1]);
49 // Should not be able to post a cloned port.
51 channel.port1.postMessage("msg", [channel2.port1]);
52 log("FAIL: Posting cloned port should throw an exception.");
54 log("SUCCESS: Posting cloned port.");
57 // Test posting messages to a port in cloned state.
59 var channel = new MessageChannel;
60 var channel2 = new MessageChannel;
62 // Post messages before and after clone to make sure ordering is preserved and all messages are received.
63 channel2.port2.postMessage("1");
64 channel.port1.postMessage("msg", [channel2.port1]);
65 channel2.port2.postMessage("2");
66 channel2.port2.postMessage("3");
67 channel.port2.onmessage = function(evt) {
69 if (evt.ports.length != 1)
70 log("FAIL: Got message without wrong number of ports: " + evt.ports.length);
71 evt.ports[0].onmessage = function(evt) {
72 if (evt.data != messageIndex)
73 log("FAIL: Got message " + evt.data + ", expected " + messageIndex);
75 if (messageIndex == 4) {
76 log("SUCCESS: Posted messages to cloned port.");
77 testDoublyClonedPort();
82 function testDoublyClonedPort()
84 var channel = new MessageChannel;
85 var channel2 = new MessageChannel;
86 channel.port1.postMessage("msg", [channel2.port1]);
87 channel.port2.postMessage("msg", [channel2.port2]);
89 channel.port1.onmessage = function(evt) {
90 evt.ports[0].postMessage("testme");
92 channel.port2.onmessage = function(evt) {
93 evt.ports[0].onmessage = function (evt) {
94 if (evt.data == "testme")
95 log("SUCCESS: Cloned both endpoints of a channel.");
97 log("FAIL: Invalid message arrived: " + evt.data);
104 // *Should* be able to post a closed port.
105 function testPostClosePort()
107 var channel = new MessageChannel;
108 var channel2 = new MessageChannel;
109 channel2.port2.close();
110 channel.port1.postMessage("closed", [channel2.port2]);
111 channel.port2.onmessage = function(evt) {
112 if (!evt.ports || evt.ports.length != 1)
113 log("FAIL: Closed port not sent.");
114 else if (evt.data != "closed")
115 log("FAIL: Unexpected message: " + evt.data);
117 log("SUCCESS: Got closed port with event message " + evt.data);
126 if (window.testRunner)
127 testRunner.notifyDone();