Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / events / message-port-clone.html
blobe158ec60e5f6ce514e43239face1a2014f55e6a5
1 <html>
2 <head>
3 <script src="../../resources/js-test.js"></script>
4 </head>
5 <body>
6 <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");
57 var testEvent;
58 var messageIndex = 1;
59 channel.port2.onmessage = function(evt) {
60 testEvent = evt;
61 shouldBe("testEvent.ports.length", "1");
62 evt.ports[0].onmessage = function(evt) {
63 testEvent = evt;
64 shouldBe("parseInt(testEvent.data)", "" + messageIndex);
65 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]);
79 gc();
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) {
85 testEvent = evt;
86 shouldBe("testEvent.data", "'testme'");
87 testPostClosePort();
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) {
101 testEvent = evt;
102 shouldNotBe("testEvent.ports", "null");
103 shouldBe("testEvent.ports.length", "1");
104 shouldBe("testEvent.data", "'closed'");
106 finishJSTest();
110 </script>
111 </body>
112 </html>