<!DOCTYPE html>
<html>
<title>Test how event handlers work with isolated Worlds</title>
<script>

// This test is meaningless without testRunner.
if (window.testRunner && window.eventSender) {

    testRunner.dumpAsText();
    testRunner.waitUntilDone();

    window.textAttributeEventsCount = 0;
    window.xhrEventsCount = 0;
    window.functionAttributeEventsCount = 0;
    window.addEventListener("message", function(message)
    {
        testRunner.notifyDone();
    }, false);

    window.textAttributeEventsCallback = function()
    {
        if (textAttributeEventsCount++)
            document.getElementById("log").innerHTML += "PASS";
    }

    function newTest(title)
    {
        document.getElementById("log").innerHTML += "<br>" + title + ": ";
    }

    function textAttributeEventListener()
    {
        window.textAttributeEventsCallback = function()
        {
            if (window.alreadyFailed)
                return;
             document.getElementById("log").innerHTML += "FAIL";
             window.alreadyFailed = true;
        }

        var test = document.getElementById("test");
        test.innerHTML = "<button id='button1' onclick='textAttributeEventsCallback()'></button>";
        document.getElementById("button1").click();
    }

    function functionAttributeEventListener()
    {
        var test = document.getElementById("test");
        var button = test.appendChild(document.createElement("button"));
        window.functionAttributeEventsCount = 0;
        button.onclick = function()
        {
            if (window.functionAttributeEventsCount++)
                document.getElementById("log").innerHTML += "PASS";
        }
        button.style.cssText = "width: 30px;height:30px";
        button.click();
    }

    function xhrEventListener()
    {
        window.xhrWorld = 1;
        var done;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "anyfile", true);
        xhr.onreadystatechange = function() {
            if (done)
                return;
            done = true;
            document.getElementById("log").innerHTML += window.xhrWorld  ? "PASS" : "FAIL";
            window.postMessage("done", "*");
        };
        xhr.send(null);
    }

    function runTestInWorld(worldId, funcName)
    {
        testRunner.evaluateScriptInIsolatedWorld(worldId, String(eval(funcName)) + "\n" + funcName + "();");
    }

    function runTest() 
    {
        newTest("Text attribute event listeners should always fire in main world");
        runTestInWorld(1, "textAttributeEventListener");
        var button1 = document.getElementById("button1");
        button1.click();
        button1.parentElement.removeChild(button1);
        
        newTest("Function attribute event listeners should fire in the world where they were created");
        runTestInWorld(1, "functionAttributeEventListener");
        var testDiv = document.getElementById("test");
        // Land somewhere on the button.
        eventSender.mouseMoveTo(testDiv.offsetLeft + 5, testDiv.offsetTop + 5);
        eventSender.mouseDown();
        eventSender.mouseUp();

        newTest("XHR attribute event listeners should fire in the world where they were created");
        runTestInWorld(1, "xhrEventListener");
    }
}
</script>
<body onload="runTest()">
    <div id="test"></div>
    <div id="log"></div>
</body>
</html>
