chromium/third_party/blink/web_tests/fast/dom/location-hash.html

<html>
<head>
<script>
    function print(message, color) 
    {
        var paragraph = document.createElement("div");
        paragraph.appendChild(document.createTextNode(message));
        paragraph.style.fontFamily = "monospace";
        if (color)
            paragraph.style.color = color;
            document.getElementById("console").appendChild(paragraph);
    }

    function shouldBe(a, b)
    {
        var evalA = eval(a);
        if (evalA == b) 
            print("PASS: " + a + " should be " + b + " and is.", "green");
        else {
            print("FAIL: " + a + " should be " + b + " but instead is " + evalA + ".", "red");
            numErrors ++;
        }
    }

    function step() {
        state++;
        switch (state) {
        case 1:
            shouldBe('window.history.length == originalHistoryLength', true);
            shouldBe('window.location.hash', '');
            window.location.hash = 'foo';
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            shouldBe('window.history.length ==  originalHistoryLength + 1', true);
            // hashchange will jump to the next step.
            break;
        case 2:
            window.location.hash = 'bar';
            shouldBe('window.location.hash', '#bar');
            shouldBe("window.location == originalLocation + '#bar'", true);
            shouldBe('window.history.length == originalHistoryLength + 2', true);
            // hashchange will jump to the next step.
            break;
        case 3:
            window.location.hash = 'bar';
            shouldBe('window.location.hash', '#bar');
            shouldBe("window.location == originalLocation + '#bar'", true);
            shouldBe('window.history.length == originalHistoryLength + 2', true);
            step(); // No hashchange.
            break;
        case 4:
            shouldBe('window.location.hash', '#bar');
            shouldBe("window.location == originalLocation + '#bar'", true);
            window.history.back();
            // history.back() is asychronous, location should be unchanged
            shouldBe('window.location.hash', '#bar');
            shouldBe("window.location == originalLocation + '#bar'", true);
            // hashchange will jump to the next step.
            break;
        case 5:
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            window.history.back();
            // history.back() is asychronous, location should be unchanged
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            // hashchange will jump to the next step.
            break;
        case 6:
            shouldBe('window.location.hash', '');
            shouldBe("window.location == originalLocation", true);
            window.history.forward();
            // history.forward() is asychronous, location should be unchanged
            shouldBe('window.location.hash', '');
            shouldBe("window.location == originalLocation", true);
            // hashchange will jump to the next step.
            break;
        case 7:
            shouldBe('window.location.hash', '#foo');
            shouldBe("window.location == originalLocation + '#foo'", true);
            window.location.hash = '';
            shouldBe('window.location.hash', '');
            shouldBe("window.location == originalLocation + '#'", true);
            window.location.hash = '#';
            shouldBe('window.location.hash', '');
            shouldBe("window.location == originalLocation + '#'", true);
            if (numErrors == 0)
                print("SUCCESS!", "green")
            else
                print("FAILURE: one or more tests failed", "red");

            if (window.testRunner)
                testRunner.notifyDone();

            return;
        }
    }

    function runTests() {
        if (window.testRunner) {
            testRunner.clearBackForwardList();
            testRunner.dumpAsText();
            testRunner.waitUntilDone();
        }

        state = 0;
        numErrors = 0;
        originalLocation = window.location.href;
        originalHistoryLength = window.history.length;

        window.onhashchange = step;

        // Location changes need to happen outside the onload handler to generate history entries.
        setTimeout(step, 0);
    }
    </script>
</head>
<body onload="runTests();">
<p>This tests that modifying location.hash works as it should</p>
<div id="console">
</div>
</body>
</html>