<html>
<head>
<script>

if (window.testRunner) {
    if (!sessionStorage.stage)
        testRunner.clearBackForwardList();
    testRunner.dumpAsText();
    testRunner.waitUntilDone();
}

function lastPathComponent()
{
    return window.location.href.split('/').pop();
}

function runFirstStageOfTest()
{
    history.replaceState("FirstEntry", null, "?FirstEntry");
    history.pushState("SecondEntry", null, "?SecondEntry");
    history.back();
}

function runTest()
{
    if (!sessionStorage.stage) {
        // Location changes need to happen outside the onload handler to generate history entries.
        setTimeout(runFirstStageOfTest, 0);
    } else if (sessionStorage.stage == 2)
        alert("Last path component of location is " + lastPathComponent());
}

function continueTest(state)
{
    if (state == "FirstEntry") {
        history.replaceState("FirstEntryWillLaterBeReactivated", null, "?FirstEntryWillLaterBeReactivated");
        history.forward();
    } else if (state == "SecondEntry") {
        history.replaceState("SecondEntryWillLaterBeReactivated", null, "?SecondEntryWillLaterBeReactivated");
        window.location = "resources/navigate-back.html";
    } else if (state == "SecondEntryWillLaterBeReactivated")
        history.back();
    else if (state == "FirstEntryWillLaterBeReactivated") {
        alert("Test complete");
        sessionStorage.clear();
        if (window.testRunner)
            testRunner.notifyDone();
    }
}

window.onpopstate = function statePopped()
{
    var state = event.state;
    alert("State popped - " + state + " (type " + typeof state + ")");
    continueTest(state);
}

window.onpageshow = function pageShown()
{
    if (sessionStorage.stage == 2) {
        var state = history.state;
        alert("Page shown - " + state + " (type " + typeof state + ")");
        continueTest(state);
    }
}

</script>
<body onload="runTest();" onunload="/* disable page cache */">
<pre>
This test:
-Builds up a list of state object entries with fragment URLs.
-Navigates through them to verify that the popstate event is fired.
-Navigates away to a new document, with the old document being destroyed.
-Navigates back to the state object entries and verifies the pageshow or popstate events are fired on the new documents.
</pre><br>
<pre id="logger"></pre>
</body>
</html>
