<html>
<head></head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var console = document.getElementById("console");

function onmessage(evt) {
    if (evt.ports)
        console.innerHTML += "Received message '" + evt.data + "' with " + evt.ports.length + " ports.<br>";
    else
        console.innerHTML += "Received message '" + evt.data + "'<br>";

    if (evt.data == 'done' && window.testRunner)
        testRunner.notifyDone();
}

window.addEventListener('message', onmessage, false);

function tryPostMessageFunction(postMessageFunction, first, second, third, shouldFail) {
   try {
        if (!third)
            postMessageFunction(first, second);
        else
            postMessageFunction(first, second, third);
        console.innerHTML += (shouldFail ? "FAIL" : "PASS") + ": Posting message ('" + first + "', " + third + ") did not throw an exception<br>";
   } catch (e) {
        console.innerHTML += (shouldFail ? "PASS" : "FAIL") + ": Posting message ('" + first + "', " + third + "): threw exception " + e + "<br>";
   }
}

function tryPostMessage(first, second, third, shouldFail) {
    tryPostMessageFunction(window.postMessage, first, second, third, shouldFail);
}

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>";

tryPostMessage('1', '*', 1, true);
tryPostMessage('2', '*', 'c', true);
tryPostMessage('3', '*', { x: 1 }, true);
tryPostMessage('4', '*', window, true);
tryPostMessage('4a', window, '*', true); // Legacy argument order.
tryPostMessage('5', '*', null);
tryPostMessage('6', '*', void 0);
var channel1 = new MessageChannel;
tryPostMessageFunction(window.postMessage, '7', '*', [channel1.port1, channel1.port2]);
var channel1a = new MessageChannel;
tryPostMessageFunction(window.postMessage, '7a', [channel1a.port1, channel1a.port2], '*', true);
var channel2 = new MessageChannel;
tryPostMessageFunction(window.postMessage, '7', '*', [channel2.port1, channel2.port2]);
var channel3 = new MessageChannel;
tryPostMessage(2147483648, '*', null);
tryPostMessageFunction(window.postMessage, channel3.port1, '*', [channel3.port1, channel3.port2]);
var channel4 = new MessageChannel;
tryPostMessageFunction(window.postMessage, channel4.port1, '*', [channel4.port1, channel4.port2]);
var channel5 = new MessageChannel;
tryPostMessageFunction(window.postMessage, [channel5.port1, channel5.port2], '*', [channel5.port1, channel5.port2]);

var arrayBuffer = new ArrayBuffer(30);
var int8View = new Int8Array(arrayBuffer, 2, 10);
tryPostMessageFunction(window.postMessage, arrayBuffer, '*', [arrayBuffer]);
if (!(arrayBuffer.byteLength === 0)) {
    console.innerHTML += "FAIL: arrayBuffer not neutered; byteLength = " + arrayBuffer.byteLength + "<br>";
}
else {
    console.innerHTML += "PASS: arrayBuffer neutered<br>";
}

if (!(int8View.length == 0)) {
    console.innerHTML += "FAIL: view was not neutered; length = " + int8View.length + "<br>";
}
else {
    console.innerHTML += "PASS: view neutered<br>"
}


tryPostMessageFunction(window.postMessage, 'done', '*');
</script>
</body>
</html>
