3 <script src=
"../../../resources/js-test.js"></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
) {
18 debug("Received message '" + evt
.data
+ "' with " + evt
.ports
.length
+ " ports.");
20 debug("Received message '" + evt
.data
);
22 if (evt
.data
== 'done')
26 window
.addEventListener('message', onmessage
, false);
28 function tryPostMessageFunction(postMessageFunction
, first
, second
, third
, shouldFail
) {
31 postMessageFunction(first
, second
, third
);
33 reason
= " did not throw an exception";
36 reason
= ": threw exception " + e
;
39 testPassed("Posting message ('" + first
+ "', " + third
+ ")" + reason
);
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
);
80 testPassed("arrayBuffer neutered");
82 if (!(int8View
.length
== 0))
83 testFailed("view was not neutered; length = " + int8View
.length
);
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', '*');