<body>
<p>Test cross-frame MessagePort communication.</p>
<p>Should be a series of SUCCESS messages, followed with DONE.</p>
<pre id=log></pre>
<script>
function log(message)
{
    document.getElementById("log").innerHTML += message + "<br>";
}

if (window.testRunner) {
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

var mainPort;

var allTests;
var currentTest = 0;

function nextTest() 
{
    var fun = "postMessage";
    var testFunNo = Math.floor(currentTest / 2);
    if (testFunNo < allTests.length)
    {
        var test = allTests[testFunNo];
        currentTest++;
        test(fun);
    }
    else
    {
        done();
    }
}

function test(postMessageFun)
{
    var channel = new MessageChannel;

    window.frames[0][postMessageFun]("msg", "*", [channel.port2]);
    mainPort = channel.port1;
    mainPort[postMessageFun]("ping");
    mainPort.onmessage = function(evt) {
        if (evt.data == "pong" && evt.origin == "")
            log("Simple exchange: SUCCESS");
        else
            log("Simple exchange: FAIL. Got message '" + evt.data + "' from '" + evt.origin + "'.");
        nextTest();
    };
}

function test2(postMessageFun)
{
    var channel = new MessageChannel;
    mainPort[postMessageFun]("newPort", [channel.port2]);
    channel.port1.onmessage = function(evt) {
        if (evt.data == "yo" && evt.origin == "")
            log("Posted port: SUCCESS");
        else
            log("Posted port: FAIL. Got message '" + evt.data + "' from '" + evt.origin + "'.");
        nextTest();
    };
}

function test3(postMessageFun)
{
    var channel = new MessageChannel;
    channel.port1[postMessageFun]("1");
    var started = false;
    channel.port2.addEventListener("message", function(evt) {
        if (started) {
            log("Event Listener: SUCCESS");
            nextTest();
        } else
            log("Event Listener: FAIL. Event listener called before start()");
   });
   setTimeout(function() {
      channel.port2.start();
      started = true;
   }, 100);

}

function test4(postMessageFun)
{
    var channel = new MessageChannel;
    channel.port1[postMessageFun]("1");
    channel.port1.close();
    channel.port1[postMessageFun]("2");
    channel.port2.onmessage = function(evt) {
        if (evt.data == "1") {
            log("Closed port: SUCCESS");
            nextTest();
        } else
            log("Closed port: FAIL. Got message: " + evt.data + " after close");
   }
}

function test5(postMessageFun)
{
    // Create a channel and put some messages in it which will go undelivered and should not leak
    var channel = new MessageChannel;
    channel.port1[postMessageFun]("abcde");
    nextTest();
}

function test6(postMessageFun)
{
    var channel = new MessageChannel;
    channel.port1[postMessageFun]("howdy");
    channel.port2.onmessage = function(event) {
        log("MessageEvent.cancelable defaults to " + event.cancelable + ": " + (event.cancelable ? "FAIL" : "PASS"));
        nextTest();
    }
}

function done() {
    // Wait a short period of time to ensure no messages come in from previous tests.
    setTimeout(function() {
        log("DONE");
        if (window.testRunner)
            testRunner.notifyDone();
    }, 100);
}

allTests = [ test, test2, test3, test4, test5, test6 ]

</script>
<iframe src="resources/message-port-iframe.html" onload="nextTest()"></iframe>
</body>
