<!DOCTYPE html>
<p>Tests that properties of various events are cloned between isolated worlds.</p>
<div id="main"></div>
<div id="isolated"></div>
<script>
testRunner.dumpAsText();

function addListener(eventType, prop, worldType) {
    document.getElementById(worldType).addEventListener(eventType, function(event) {
        console.log(eventType + " received in " + worldType + " world");
        console.log(prop + " was " + JSON.stringify(event[prop]));
    });
}

function sendCloneableObject(eventType, prop, targetWorldType) {
    var newEvent = eval("new " + eventType + "('" + eventType + "', { " + prop + ": { foo: 5, bar: 'hello', targetWorld: targetWorldType } })");
    document.getElementById(targetWorldType).dispatchEvent(newEvent);
}

function runScript(eventType, prop) {
    var sendScript = "(" + sendCloneableObject.toString() + ")('" + eventType + "', '" + prop + "', 'main');";
    addListener(eventType, prop, "main");
    testRunner.evaluateScriptInIsolatedWorld(1, sendScript);
    var receiveScript = "(" + addListener.toString() + ")('" + eventType + "', '" + prop + "', 'isolated');";
    testRunner.evaluateScriptInIsolatedWorld(1, receiveScript);
    sendCloneableObject(eventType, prop, "isolated");

}

// The events that we want to test, with the properties that each one uses.
var events = [
    { eventType: "CustomEvent", prop: "detail" },
    { eventType: "MessageEvent", prop: "data" },
    { eventType: "PopStateEvent", prop: "state" }
];

for (var i = 0; i < events.length; ++i) {
    runScript(events[i].eventType, events[i].prop);
}
</script>
