<!DOCTYPE html> 
<html> 
<head>
  <script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<pre id="console"></pre>

<iframe src="data:text/plain,iframe1" id="iframe1" name="iframe1"></iframe> 
<iframe src="data:text/plain,iframe2" id="iframe2" name="iframe2"></iframe> 

<script>
var wentBack = false;

description('Tests that we trigger same-document navigation when history entries are generated via pushState, even if the frames present in the document change between history entries.');

if (window.testRunner) {
    testRunner.dumpChildFramesAsText();
    testRunner.clearBackForwardList();
    testRunner.dumpBackForwardList();
}

onload = function()
{
    // Run afer onload, to make sure that we can generate history entries.
    setTimeout(beginTest, 0);
}

onpopstate = function(event) {
    var loc = location.href;
    var filenameAndQuery = loc.substring(loc.lastIndexOf('/') + 1);
    debug('popstate to state: ' + event.state + ' filename: ' + filenameAndQuery);
    
    if (!event.state) {
        if (wentBack) {
            debug('going forward');
            history.forward();
        }
    } else if (event.state == 'newState') {
        finishJSTest();
    } else {
        testFailed('Unexpected state');
    }
};


function beginTest() 
{
    debug('removing iframe2');
    document.body.removeChild(document.getElementById('iframe2'));

    debug('pushState to newState');
    history.pushState('newState', null, '?newState');

    window.setTimeout(function() {
        wentBack = true;
        debug('going back');
        history.back();
    }, 0);
}
var jsTestIsAsync = true;
</script>  
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
