<!DOCTYPE html>
<html>
<body>
<div id="console">Tests that event handler is called in correct world.<br></div>

<script id="isolatedScript0" type="isolated/world">
HTMLElement.prototype.world = "Isolated World 0";
function listener()
{
    log("in isoloated world 0: " + document.body.world);
}
document.body.addEventListener("MessageEvent", listener, false);
log("Sending from world 0...");
sendMessage();
</script>

<script id="isolatedScript1" type="isolated/world">
HTMLElement.prototype.world = "Isolated World 1";
log("Sending from world 1...");
sendMessage();
</script>

<script>
function log(message)
{
    document.getElementById("console").innerHTML += message + "<br>";
}

function sendMessage()
{
    var event = document.createEvent("MessageEvent");
    event.initMessageEvent("MessageEvent", false, false, "", location.url);
    document.body.dispatchEvent(event);
}

HTMLElement.prototype.world = "Main World";
function listener(evt)
{
    log("In main world: " + document.body.world);
}

document.body.addEventListener("MessageEvent", listener, false);

if (window.testRunner) {
    testRunner.dumpAsText();
    // setup common functions in worlds
    for (var world  = 0; world < 2; ++world) {
        var script = log.toString() + sendMessage.toString() + document.getElementById("isolatedScript" + world).innerHTML;
        testRunner.evaluateScriptInIsolatedWorld(world, script);
    }
    log("Sending from main world...");
    sendMessage();
}
</script>
</body>
</html>
