<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test for setting document.location.hostname to accepted values</title>
</head>
<body>
<pre id="pre"></pre>
<script>
    // This function will be redefined as the test cases get executed
    var testCaseOnload = function() {
        runOneTestThatShouldBeAccepted(0);
    };

    var iframeDocument;
    function iframeOnload() {
        iframeDocument = document.getElementById("targetFrame").contentDocument;
        testCaseOnload();
    }

    function log(s) {
        document.getElementById("pre").appendChild(document.createTextNode(s));
    }

    function runOneTestThatShouldBeAccepted(testIndex) {
        var testCasesThatShouldBeAccepted = [
            { hostString : 'localhost', description : false }
            ,{ hostString : '127.0.0.1', description : false }
        ];

        if (testIndex >= testCasesThatShouldBeAccepted.length) {
            if (window.testRunner)
                testRunner.notifyDone();
        } else {
            // Prepare for next test case by setting testCaseOnload to load the test case
            // and then load the simple HTML file that says "Before"
            testCaseOnload = function () {

                // Prepare test case execution by first setting testCaseOnload to check
                // the test outcome and then load set the iframe's document.location.hostname
                testCaseOnload = function(currentTestIndex) {
                    return function () {
                        var currentCase = testCasesThatShouldBeAccepted[currentTestIndex],
                                description = (currentCase.description ? currentCase.description : currentCase.hostString);
                        if (iframeDocument === null || iframeDocument.location.hostname == currentCase.hostString) {
                            log("PASS: " + description + " was accepted" + (iframeDocument === null ? " (at least the iframe contentDocument was null so it had changed origin)" : "") + ".\n");
                        } else {
                            log("FAIL: " + description + " was not accepted. The iframe contentDocument was not null and its location.hostname was not " + currentCase.hostString + ".\n");
                        }
                        runOneTestThatShouldBeAccepted(currentTestIndex + 1);
                    };
                }(testIndex);

                iframeDocument.location.hostname = testCasesThatShouldBeAccepted[testIndex].hostString;
            };

            document.getElementById("targetFrame").setAttribute("src", "http://127.0.0.1:8000/dom/resources/set-document-location-iframe.html");
        }
    }

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

</script>
<iframe id="targetFrame" src="resources/set-document-location-iframe.html" onload="iframeOnload()"></iframe>
</body>
</html>