OLD | NEW |
| 1 <html> |
| 2 <head> |
| 3 <script src="../js/resources/js-test-pre.js"></script> |
| 4 </head> |
1 <body> | 5 <body> |
2 <p>Tests various use cases when cloning MessagePorts.</p> | |
3 <p>Should be a series of SUCCESS messages, followed with DONE.</p> | |
4 <pre id=log></pre> | |
5 <script> | 6 <script> |
6 | 7 description("Tests various use cases when cloning MessagePorts."); |
7 function gc() | 8 window.jsTestIsAsync = true; |
8 { | |
9 if (window.GCController) | |
10 return GCController.collect(); | |
11 | |
12 for (var i = 0; i < 10000; i++) { // force garbage collection (FF requires a
bout 9K allocations before a collect). | |
13 var s = new String("abc"); | |
14 } | |
15 } | |
16 | |
17 function log(message) | |
18 { | |
19 document.getElementById("log").innerHTML += message + "<br>"; | |
20 } | |
21 | |
22 if (window.testRunner) { | |
23 testRunner.dumpAsText(); | |
24 testRunner.waitUntilDone(); | |
25 } | |
26 | 9 |
27 var channel = new MessageChannel; | 10 var channel = new MessageChannel; |
28 channel.port1.onmessage = channel.port2.onmessage = function(evt) { | 11 channel.port1.onmessage = channel.port2.onmessage = function(evt) { |
29 fail("FAIL: Should not have received message: " + evt.data); | 12 testFailed("Should not have received message: " + evt.data); |
30 } | |
31 try { | |
32 channel.port1.postMessage("msg", [channel.port1]); | |
33 log("FAIL: Posting port to itself should throw an exception."); | |
34 } catch (ex) { | |
35 log("SUCCESS: Posting port to itself: " + ex); | |
36 } | 13 } |
37 | 14 |
38 try { | 15 // Posting port to itself should throw an exception. |
39 channel.port1.postMessage("msg", [channel.port2]); | 16 shouldThrow("channel.port1.postMessage('msg', [channel.port1])", "'DataCloneErro
r: An object could not be cloned.'"); |
40 log("FAIL: Posting port to entangled pair should throw an exception."); | 17 |
41 } catch (ex) { | 18 // Posting port to entangled pair should throw an exception. |
42 log("SUCCESS: Posting entangled port: " + ex); | 19 shouldThrow("channel.port1.postMessage('msg', [channel.port2])", "'DataCloneErro
r: An object could not be cloned.'"); |
43 } | |
44 | 20 |
45 channel = new MessageChannel; | 21 channel = new MessageChannel; |
46 var channel2 = new MessageChannel; | 22 var channel2 = new MessageChannel; |
47 channel.port1.postMessage("msg", [channel2.port1]); | 23 channel.port1.postMessage("msg", [channel2.port1]); |
48 | 24 |
49 // Should not be able to post a cloned port. | 25 // Should not be able to post a cloned port. |
50 try { | 26 shouldThrow("channel.port1.postMessage('msg', [channel2.port1])", "'DataCloneErr
or: An object could not be cloned.'"); |
51 channel.port1.postMessage("msg", [channel2.port1]); | |
52 log("FAIL: Posting cloned port should throw an exception."); | |
53 } catch (ex) { | |
54 log("SUCCESS: Posting cloned port."); | |
55 } | |
56 | 27 |
57 // Test posting messages to a port in cloned state. | 28 // Test posting messages to a port in cloned state. |
58 | 29 |
59 var channel = new MessageChannel; | 30 var channel = new MessageChannel; |
60 var channel2 = new MessageChannel; | 31 var channel2 = new MessageChannel; |
61 | 32 |
62 // Post messages before and after clone to make sure ordering is preserved and a
ll messages are received. | 33 // Post messages before and after clone to make sure ordering is preserved and a
ll messages are received. |
63 channel2.port2.postMessage("1"); | 34 channel2.port2.postMessage("1"); |
64 channel.port1.postMessage("msg", [channel2.port1]); | 35 channel.port1.postMessage("msg", [channel2.port1]); |
65 channel2.port2.postMessage("2"); | 36 channel2.port2.postMessage("2"); |
66 channel2.port2.postMessage("3"); | 37 channel2.port2.postMessage("3"); |
| 38 var testEvent; |
| 39 var messageIndex = 1; |
67 channel.port2.onmessage = function(evt) { | 40 channel.port2.onmessage = function(evt) { |
68 var messageIndex = 1; | 41 testEvent = evt; |
69 if (evt.ports.length != 1) | 42 shouldBe("testEvent.ports.length", "1"); |
70 log("FAIL: Got message without wrong number of ports: " + evt.ports.leng
th); | |
71 evt.ports[0].onmessage = function(evt) { | 43 evt.ports[0].onmessage = function(evt) { |
72 if (evt.data != messageIndex) | 44 testEvent = evt; |
73 log("FAIL: Got message " + evt.data + ", expected " + messageIndex); | 45 shouldBe("parseInt(testEvent.data)", "" + messageIndex); |
74 messageIndex++; | 46 messageIndex++; |
75 if (messageIndex == 4) { | 47 if (messageIndex == 4) { |
76 log("SUCCESS: Posted messages to cloned port."); | 48 testPassed("Posted messages to cloned port."); |
77 testDoublyClonedPort(); | 49 testDoublyClonedPort(); |
78 } | 50 } |
79 } | 51 } |
80 } | 52 } |
81 | 53 |
82 function testDoublyClonedPort() | 54 function testDoublyClonedPort() |
83 { | 55 { |
84 var channel = new MessageChannel; | 56 var channel = new MessageChannel; |
85 var channel2 = new MessageChannel; | 57 var channel2 = new MessageChannel; |
86 channel.port1.postMessage("msg", [channel2.port1]); | 58 channel.port1.postMessage("msg", [channel2.port1]); |
87 channel.port2.postMessage("msg", [channel2.port2]); | 59 channel.port2.postMessage("msg", [channel2.port2]); |
88 gc(); | 60 gc(); |
89 channel.port1.onmessage = function(evt) { | 61 channel.port1.onmessage = function(evt) { |
90 evt.ports[0].postMessage("testme"); | 62 evt.ports[0].postMessage("testme"); |
91 } | 63 } |
92 channel.port2.onmessage = function(evt) { | 64 channel.port2.onmessage = function(evt) { |
93 evt.ports[0].onmessage = function (evt) { | 65 evt.ports[0].onmessage = function (evt) { |
94 if (evt.data == "testme") | 66 testEvent = evt; |
95 log("SUCCESS: Cloned both endpoints of a channel."); | 67 shouldBe("testEvent.data", "'testme'"); |
96 else | |
97 log("FAIL: Invalid message arrived: " + evt.data); | |
98 testPostClosePort(); | 68 testPostClosePort(); |
99 } | 69 } |
100 } | 70 } |
101 | 71 |
102 } | 72 } |
103 | 73 |
104 // *Should* be able to post a closed port. | 74 // *Should* be able to post a closed port. |
105 function testPostClosePort() | 75 function testPostClosePort() |
106 { | 76 { |
107 var channel = new MessageChannel; | 77 var channel = new MessageChannel; |
108 var channel2 = new MessageChannel; | 78 var channel2 = new MessageChannel; |
109 channel2.port2.close(); | 79 channel2.port2.close(); |
110 channel.port1.postMessage("closed", [channel2.port2]); | 80 channel.port1.postMessage("closed", [channel2.port2]); |
111 channel.port2.onmessage = function(evt) { | 81 channel.port2.onmessage = function(evt) { |
112 if (!evt.ports || evt.ports.length != 1) | 82 testEvent = evt; |
113 log("FAIL: Closed port not sent."); | 83 shouldNotBe("testEvent.ports", "null"); |
114 else if (evt.data != "closed") | 84 shouldBe("testEvent.ports.length", "1"); |
115 log("FAIL: Unexpected message: " + evt.data); | 85 shouldBe("testEvent.data", "'closed'"); |
116 else | 86 |
117 log("SUCCESS: Got closed port with event message " + evt.data); | 87 finishJSTest(); |
118 done(); | |
119 } | 88 } |
120 } | 89 } |
121 | |
122 function done() | |
123 { | |
124 log("DONE"); | |
125 | |
126 if (window.testRunner) | |
127 testRunner.notifyDone(); | |
128 } | |
129 </script> | 90 </script> |
| 91 <script src="../js/resources/js-test-post.js"></script> |
130 </body> | 92 </body> |
| 93 </html> |
OLD | NEW |