B2G/gecko/browser/components/sessionstore/test/browser_500328.js
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
This Source Code Form is subject to the terms of the Mozilla Public
License, v.
This Source Code Form is subject to the terms of the Mozilla Public License v .
function checkState(tab) {
   We also add a node to the document's body when after going back and make
   sure it's still there after we go forward -- this is to test that the two
   history entries correspond to the same document.
  let popStateCount = 0;
  tab.linkedBrowser.addEventListener('popstate', function(aEvent) {
    let contentWindow = tab.linkedBrowser.contentWindow;
    if (popStateCount == 0) {
      popStateCount++;
      is(tab.linkedBrowser.contentWindow.testState, 'foo',
         'testState after going back');
      ok(aEvent.state, "Event should have a state property.");
      is(JSON.stringify(tab.linkedBrowser.contentWindow.history.state), JSON.stringify({obj1:1}),
         "first popstate object.");
       Add a node with id "new-elem" to the document.
      let doc = contentWindow.document;
      ok(!doc.getElementById("new-elem"),
         "doc shouldn't contain new-elem before we add it.");
      let elem = doc.createElement("div");
      elem.id = "new-elem";
      doc.body.appendChild(elem);
      contentWindow.history.forward();
    }
    else if (popStateCount == 1) {
      popStateCount++;
      is(aEvent.state.obj3.toString(), '/^a$/', "second popstate object.");
       Make sure that the new-elem node is present in the document.  If it's
       not, then this history entry has a different doc identifier than the
       previous entry, which is bad.
      let doc = contentWindow.document;
      let newElem = doc.getElementById("new-elem");
      ok(newElem, "doc should contain new-elem.");
      newElem.parentNode.removeChild(newElem);
      ok(!doc.getElementById("new-elem"), "new-elem should be removed.");
       Clean up after ourselves and finish the test.
      tab.linkedBrowser.removeEventListener("popstate", arguments.callee, true);
      gBrowser.removeTab(tab);
      finish();
    }
  }, true);
   Set some state in the page's window.  When we go back(), the page should
   be retrieved from bfcache, and this state should still be there.
  tab.linkedBrowser.contentWindow.testState = 'foo';
   Now go back.  This should trigger the popstate event handler above.
  tab.linkedBrowser.contentWindow.history.back();
}
function checkState ( tab ) { let popStateCount = 0 ; tab . linkedBrowser . addEventListener ( 'popstate' , function ( aEvent ) { let contentWindow = tab . linkedBrowser . contentWindow ; if ( popStateCount == 0 ) { popStateCount ++ ; is ( tab . linkedBrowser . contentWindow . testState , 'foo' , 'testState after going back' ) ; ok ( aEvent . state , "Event should have a state property." ) ; is ( JSON . stringify ( tab . linkedBrowser . contentWindow . history . state ) , JSON . stringify ( { obj1 : 1 } ) , "first popstate object." ) ; let doc = contentWindow . document ; ok ( ! doc . getElementById ( "new-elem" ) , "doc shouldn't contain new-elem before we add it." ) ; let elem = doc . createElement ( "div" ) ; elem . id = "new-elem" ; doc . body . appendChild ( elem ) ; contentWindow . history . forward ( ) ; } else if ( popStateCount == 1 ) { popStateCount ++ ; is ( aEvent . state . obj3 . toString ( ) , '/^a$/' , "second popstate object." ) ; let doc = contentWindow . document ; let newElem = doc . getElementById ( "new-elem" ) ; ok ( newElem , "doc should contain new-elem." ) ; newElem . parentNode . removeChild ( newElem ) ; ok ( ! doc . getElementById ( "new-elem" ) , "new-elem should be removed." ) ; tab . linkedBrowser . removeEventListener ( "popstate" , arguments . callee , true ) ; gBrowser . removeTab ( tab ) ; finish ( ) ; } } , true ) ; tab . linkedBrowser . contentWindow . testState = 'foo' ; tab . linkedBrowser . contentWindow . history . back ( ) ; }
This Source Code Form is subject to the terms of the Mozilla Public
 License, v. 2.0.
This Source Code Form is subject to the terms of the Mozilla Public License v . 2 . 0 .
// Go back and then forward, and make sure that the state objects received // from the popState event are as we expect them to be. // // We also add a node to the document's body when after going back and make // sure it's still there after we go forward -- this is to test that the two // history entries correspond to the same document. // Add a node with id "new-elem" to the document. // Make sure that the new-elem node is present in the document.  If it's // not, then this history entry has a different doc identifier than the // previous entry, which is bad. // Clean up after ourselves and finish the test. // Set some state in the page's window.  When we go back(), the page should // be retrieved from bfcache, and this state should still be there. // Now go back.  This should trigger the popstate event handler above.