Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / Window / window-postmessage-args.html
blob97155c3d15785043cd5a979c36a1e07665a660eb
1 <html>
2 <head>
3 <script src="../../../resources/js-test.js"></script>
4 </head>
5 <body>
6 <script>
7 description("Test window.postMessage() argument handling.");
9 self.jsTestIsAsync = true;
11 if (window.testRunner) {
12 testRunner.dumpAsText();
13 testRunner.waitUntilDone();
16 function onmessage(evt) {
17 if (evt.ports)
18 debug("Received message '" + evt.data + "' with " + evt.ports.length + " ports.");
19 else
20 debug("Received message '" + evt.data);
22 if (evt.data == 'done')
23 finishJSTest();
26 window.addEventListener('message', onmessage, false);
28 function tryPostMessageFunction(postMessageFunction, first, second, third, shouldFail) {
29 var pass, reason;
30 try {
31 postMessageFunction(first, second, third);
32 pass = !shouldFail;
33 reason = " did not throw an exception";
34 } catch (e) {
35 pass = shouldFail;
36 reason = ": threw exception " + e;
38 if (pass)
39 testPassed("Posting message ('" + first + "', " + third + ")" + reason);
40 else
41 testFailed("Posting message ('" + first + "', " + third + ")" + reason);
44 function tryPostMessage(first, second, third, shouldFail) {
45 tryPostMessageFunction(window.postMessage, first, second, third, shouldFail);
48 document.getElementById("description").innerHTML = "Test that the second argument of window.postMessage is ignored or triggers an error if it is not a message port. You should see PASS message '1' through '7', followed by 'done', with messages 4-7 received below.<br><br>";
50 tryPostMessage('1', '*', 1, true);
51 tryPostMessage('2', '*', 'c', true); // Legacy overload resolution will consider 3rd argument to be the (string) origin.
52 tryPostMessage('3', '*', { x: 1 }, true);
53 tryPostMessage('3a', { x: 1 }, '*', true); // Legacy argument order.
54 tryPostMessage('4', '*', window); // Passes because window has a "length" attribute of value '0', so it looks like an array
55 tryPostMessage('4a', window, '*'); // Legacy argument order.
56 tryPostMessage('5', '*', null);
57 tryPostMessage('6', '*', void 0);
58 var channel1 = new MessageChannel;
59 tryPostMessageFunction(window.postMessage, '7', '*', [channel1.port1, channel1.port2]);
60 var channel1a = new MessageChannel;
61 tryPostMessageFunction(window.postMessage, '7a', [channel1a.port1, channel1a.port2], '*');
62 var channel2 = new MessageChannel;
63 tryPostMessageFunction(window.postMessage, '7', '*', [channel2.port1, channel2.port2]);
64 var channel3 = new MessageChannel;
65 tryPostMessage(2147483648, '*', null);
66 tryPostMessageFunction(window.postMessage, channel3.port1, '*', [channel3.port1, channel3.port2]);
67 var channel4 = new MessageChannel;
68 tryPostMessageFunction(window.postMessage, channel4.port1, '*', [channel4.port1, channel4.port2]);
69 var channel5 = new MessageChannel;
70 tryPostMessageFunction(window.postMessage, [channel5.port1, channel5.port2], '*', [channel5.port1, channel5.port2]);
71 tryPostMessageFunction(window.postMessage, 'data', '*', [channel5.port1, channel5.port2], true);
72 tryPostMessageFunction(window.postMessage, [channel5.port1, channel5.port2], '*', [], true);
74 var arrayBuffer = new ArrayBuffer(30);
75 var int8View = new Int8Array(arrayBuffer, 2, 10);
76 tryPostMessageFunction(window.postMessage, arrayBuffer, '*', [arrayBuffer]);
77 if (!(arrayBuffer.byteLength === 0))
78 testFailed("arrayBuffer not neutered; byteLength = " + arrayBuffer.byteLength);
79 else
80 testPassed("arrayBuffer neutered");
82 if (!(int8View.length == 0))
83 testFailed("view was not neutered; length = " + int8View.length);
84 else
85 testPassed("view neutered");
87 tryPostMessageFunction(window.postMessage, arrayBuffer, '*', [], true);
88 tryPostMessageFunction(window.postMessage, 'data', '*', [arrayBuffer], true);
90 tryPostMessageFunction(window.postMessage, int8View, '*', [], true);
91 tryPostMessageFunction(window.postMessage, 'data', '*', [int8View], true);
93 tryPostMessageFunction(window.postMessage, 'data', '*', {length:1}, true);
94 tryPostMessageFunction(window.postMessage, 'data', '*', [1,,2], true);
95 tryPostMessageFunction(window.postMessage, 'data', '*', [null, window.postMessage], true);
97 shouldThrow("window.postMessage()");
98 shouldThrow("window.postMessage('a')");
100 tryPostMessageFunction(window.postMessage, 'done', '*');
101 </script>
102 </body>
103 </html>