3 <script src=
"../../resources/js-test.js"></script>
7 description("Tests various use cases when cloning MessagePorts.");
8 window
.jsTestIsAsync
= true;
10 var channel
= new MessageChannel
;
11 channel
.port1
.onmessage
= channel
.port2
.onmessage = function(evt
) {
12 testFailed("Should not have received message: " + evt
.data
);
15 // Posting port to itself should throw an exception.
16 shouldThrow("channel.port1.postMessage('msg', [channel.port1])", '"DataCloneError: Failed to execute \'postMessage\' on \'MessagePort\': Port at index 0 contains the source port."');
18 debug("Posting port to entangled pair neuters the port and does nothing else:");
19 channel
= new MessageChannel
;
20 var channel2
= new MessageChannel
;
21 channel
.port1
.postMessage("msg", [channel
.port2
]);
22 shouldThrow("channel2.port1.postMessage('msg', [channel.port2])", '"DataCloneError: Failed to execute \'postMessage\' on \'MessagePort\': Port at index 0 is already neutered."');
24 channel
= new MessageChannel
;
25 channel2
= new MessageChannel
26 channel
.port1
.postMessage("msg", [channel2
.port1
]);
28 debug("Posting a neutered ArrayBuffer should throw.");
29 channel
= new MessageChannel
;
30 var arrayBuffer
= new ArrayBuffer(2);
32 channel
.port1
.postMessage("msg", [arrayBuffer
]);
33 shouldThrow("channel.port1.postMessage(arrayBuffer, [])", '"DataCloneError: Failed to execute \'postMessage\' on \'MessagePort\': An ArrayBuffer is neutered and could not be cloned."');
35 debug("Posting a neutered ArrayBufferView should throw.");
36 channel
= new MessageChannel
;
37 var uint16Array
= new Uint16Array(10);
39 shouldThrow("channel.port1.postMessage('msg', [uint16Array])", '"TypeError: Failed to execute \'postMessage\' on \'MessagePort\': Value at index 0 does not have a transferable type."');
41 channel
.port1
.postMessage("msg", [uint16Array
.buffer
]);
42 shouldThrow("channel.port1.postMessage(uint16Array, [])", '"DataCloneError: Failed to execute \'postMessage\' on \'MessagePort\': An ArrayBuffer is neutered and could not be cloned."');
44 // Should not be able to post a cloned port.
45 shouldThrow("channel.port1.postMessage('msg', [channel2.port1])", '"DataCloneError: Failed to execute \'postMessage\' on \'MessagePort\': Port at index 0 is already neutered."');
47 // Test posting messages to a port in cloned state.
49 var channel
= new MessageChannel
;
50 var channel2
= new MessageChannel
;
52 // Post messages before and after clone to make sure ordering is preserved and all messages are received.
53 channel2
.port2
.postMessage("1");
54 channel
.port1
.postMessage("msg", [channel2
.port1
]);
55 channel2
.port2
.postMessage("2");
56 channel2
.port2
.postMessage("3");
59 channel
.port2
.onmessage = function(evt
) {
61 shouldBe("testEvent.ports.length", "1");
62 evt
.ports
[0].onmessage = function(evt
) {
64 shouldBe("parseInt(testEvent.data)", "" + messageIndex
);
66 if (messageIndex
== 4) {
67 testPassed("Posted messages to cloned port.");
68 testDoublyClonedPort();
73 function testDoublyClonedPort()
75 var channel
= new MessageChannel
;
76 var channel2
= new MessageChannel
;
77 channel
.port1
.postMessage("msg", [channel2
.port1
]);
78 channel
.port2
.postMessage("msg", [channel2
.port2
]);
80 channel
.port1
.onmessage = function(evt
) {
81 evt
.ports
[0].postMessage("testme");
83 channel
.port2
.onmessage = function(evt
) {
84 evt
.ports
[0].onmessage = function (evt
) {
86 shouldBe("testEvent.data", "'testme'");
93 // *Should* be able to post a closed port.
94 function testPostClosePort()
96 var channel
= new MessageChannel
;
97 var channel2
= new MessageChannel
;
98 channel2
.port2
.close();
99 channel
.port1
.postMessage("closed", [channel2
.port2
]);
100 channel
.port2
.onmessage = function(evt
) {
102 shouldNotBe("testEvent.ports", "null");
103 shouldBe("testEvent.ports.length", "1");
104 shouldBe("testEvent.data", "'closed'");