<html>
<head>
<script type="text/javascript" src="../resources/protocol-test.js"></script>
<script>

var frame;

function appendIframe()
{
    frame = document.createElement("iframe");
    frame.src = "http://localhost:8000/inspector/page/resources/slow-test-page.html";
    document.body.appendChild(frame);
}

function removeIframe()
{
    frame.remove();
}

function test()
{
    /* 
    Testing that the Node ID can be retrieved before the onload event is triggered:
    1. Create an iframe and point it to a page with a slow-loading image.
    2. Wait until the JS context is created in the iframe.
    3. Use the JS contet to identify the "window.document" object inside the iframe.
    4. Use the JS object to retrieve the DOM agent nodeid for the document node.
    */

    InspectorProtocol.eventHandler["Page.frameStartedLoading"] = step2_onFrameStartedLoading;
    InspectorProtocol.eventHandler["Runtime.executionContextCreated"] = step3_onExecutionContextCreated;
    InspectorProtocol.eventHandler["Page.loadEventFired"] = onLoadEventFired;
    
    var targetFrameId = null;

    function step1_bootstrap() {
        ProtocolTest.log("step1_bootstrap");
        // Enable the frame events.
        InspectorProtocol.sendCommand("Page.enable", {}, function() {
            // Enable the Runtime.executionContextCreated event.
            InspectorProtocol.sendCommand("Runtime.enable", {}, function() {
                // Initialize the DOM agent.
                InspectorProtocol.sendCommand("DOM.getDocument", {}, function() {
                    ProtocolTest.log("Main document loaded");

                    // Add the iframe to the DOM.
                    InspectorProtocol.sendCommand("Runtime.evaluate", { "expression": "appendIframe()" });
                });
            });
        });
    }

    function step2_onFrameStartedLoading(response)
    {
        ProtocolTest.log("step2_onFrameStartedLoading");
        targetFrameId = response.params.frameId;
    }

    function step3_onExecutionContextCreated(event)
    {
        var frameId = event.params.context.frameId;
        if (frameId !== targetFrameId)
            return;

        ProtocolTest.log("step3_onExecutionContextCreated: Requesting document from iframe's context.");

        InspectorProtocol.sendCommand("Runtime.evaluate", {
            "expression": "document",
            "objectGroup": "console",
            "includeCommandLineAPI": false,
            "contextId": event.params.context.id,
            "doNotPauseOnExceptionsAndMuteConsole": false,
            "returnByValue": false,
            "generatePreview": false
        }, callback);

        function callback(response)
        {
            var objectId = response.result.result.objectId;
            ProtocolTest.log("Received script object for iframe's document node");
            step4_requestNode(frameId, objectId);
        }
    }

    function step4_requestNode(frameId, objectId)
    {
        function callback(response)
        {
            ProtocolTest.log(response.result.nodeId ? "PASS: Received node for iframe's document node" : "FAIL: Iframe's document node is not available");
            completeTest();
        }

        ProtocolTest.log("step4_requestNode: Requesting DOM node for iframe's document node");
        InspectorProtocol.sendCommand("DOM.requestNode", { objectId: objectId }, callback);
    }

    function completeTest()
    {
        ProtocolTest.log("Test finished");
        // Stop loading the iframe to avoid timing out the test.
        InspectorProtocol.sendCommand("Runtime.evaluate", { "expression": "removeIframe()" });
        ProtocolTest.completeTest();
    }

    function onLoadEventFired()
    {
        // We should finish the test before this event is triggered.
        // If you see this in the output, then the slow-image is not loaded correctly in the iframe.
        ProtocolTest.log("FAIL: Iframe load event fired before the test finished.");
    }

    step1_bootstrap()
}

</script>
</head>
<body onload="runTest()">
</body>
</html>
