browser/components/sessionstore/test/browser_500328.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/sessionstore/test/browser_500328.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,110 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +function checkState(tab) {
     1.9 +  // Go back and then forward, and make sure that the state objects received
    1.10 +  // from the popState event are as we expect them to be.
    1.11 +  //
    1.12 +  // We also add a node to the document's body when after going back and make
    1.13 +  // sure it's still there after we go forward -- this is to test that the two
    1.14 +  // history entries correspond to the same document.
    1.15 +
    1.16 +  let popStateCount = 0;
    1.17 +
    1.18 +  tab.linkedBrowser.addEventListener('popstate', function(aEvent) {
    1.19 +    let contentWindow = tab.linkedBrowser.contentWindow;
    1.20 +    if (popStateCount == 0) {
    1.21 +      popStateCount++;
    1.22 +
    1.23 +      is(tab.linkedBrowser.contentWindow.testState, 'foo',
    1.24 +         'testState after going back');
    1.25 +
    1.26 +      ok(aEvent.state, "Event should have a state property.");
    1.27 +      is(JSON.stringify(tab.linkedBrowser.contentWindow.history.state), JSON.stringify({obj1:1}),
    1.28 +         "first popstate object.");
    1.29 +
    1.30 +      // Add a node with id "new-elem" to the document.
    1.31 +      let doc = contentWindow.document;
    1.32 +      ok(!doc.getElementById("new-elem"),
    1.33 +         "doc shouldn't contain new-elem before we add it.");
    1.34 +      let elem = doc.createElement("div");
    1.35 +      elem.id = "new-elem";
    1.36 +      doc.body.appendChild(elem);
    1.37 +
    1.38 +      contentWindow.history.forward();
    1.39 +    }
    1.40 +    else if (popStateCount == 1) {
    1.41 +      popStateCount++;
    1.42 +      is(aEvent.state.obj3.toString(), '/^a$/', "second popstate object.");
    1.43 +
    1.44 +      // Make sure that the new-elem node is present in the document.  If it's
    1.45 +      // not, then this history entry has a different doc identifier than the
    1.46 +      // previous entry, which is bad.
    1.47 +      let doc = contentWindow.document;
    1.48 +      let newElem = doc.getElementById("new-elem");
    1.49 +      ok(newElem, "doc should contain new-elem.");
    1.50 +      newElem.parentNode.removeChild(newElem);
    1.51 +      ok(!doc.getElementById("new-elem"), "new-elem should be removed.");
    1.52 +
    1.53 +      // Clean up after ourselves and finish the test.
    1.54 +      tab.linkedBrowser.removeEventListener("popstate", arguments.callee, true);
    1.55 +      gBrowser.removeTab(tab);
    1.56 +      finish();
    1.57 +    }
    1.58 +  }, true);
    1.59 +
    1.60 +  // Set some state in the page's window.  When we go back(), the page should
    1.61 +  // be retrieved from bfcache, and this state should still be there.
    1.62 +  tab.linkedBrowser.contentWindow.testState = 'foo';
    1.63 +
    1.64 +  // Now go back.  This should trigger the popstate event handler above.
    1.65 +  tab.linkedBrowser.contentWindow.history.back();
    1.66 +}
    1.67 +
    1.68 +function test() {
    1.69 +  // Tests session restore functionality of history.pushState and
    1.70 +  // history.replaceState().  (Bug 500328)
    1.71 +
    1.72 +  waitForExplicitFinish();
    1.73 +
    1.74 +  // We open a new blank window, let it load, and then load in
    1.75 +  // http://example.com.  We need to load the blank window first, otherwise the
    1.76 +  // docshell gets confused and doesn't have a current history entry.
    1.77 +  let tab = gBrowser.addTab("about:blank");
    1.78 +  let browser = tab.linkedBrowser;
    1.79 +
    1.80 +  whenBrowserLoaded(browser, function() {
    1.81 +    browser.loadURI("http://example.com", null, null);
    1.82 +
    1.83 +    whenBrowserLoaded(browser, function() {
    1.84 +      // After these push/replaceState calls, the window should have three
    1.85 +      // history entries:
    1.86 +      //   testURL        (state object: null)          <-- oldest
    1.87 +      //   testURL        (state object: {obj1:1})
    1.88 +      //   testURL?page2  (state object: {obj3:/^a$/})  <-- newest
    1.89 +      let contentWindow = tab.linkedBrowser.contentWindow;
    1.90 +      let history = contentWindow.history;
    1.91 +      history.pushState({obj1:1}, "title-obj1");
    1.92 +      history.pushState({obj2:2}, "title-obj2", "?page2");
    1.93 +      history.replaceState({obj3:/^a$/}, "title-obj3");
    1.94 +
    1.95 +      SyncHandlers.get(tab.linkedBrowser).flush();
    1.96 +      let state = ss.getTabState(tab);
    1.97 +      gBrowser.removeTab(tab);
    1.98 +
    1.99 +      // Restore the state into a new tab.  Things don't work well when we
   1.100 +      // restore into the old tab, but that's not a real use case anyway.
   1.101 +      let tab2 = gBrowser.addTab("about:blank");
   1.102 +      ss.setTabState(tab2, state, true);
   1.103 +
   1.104 +      // Run checkState() once the tab finishes loading its restored state.
   1.105 +      whenTabRestored(tab2, function() {
   1.106 +        SimpleTest.executeSoon(function() {
   1.107 +          checkState(tab2);
   1.108 +        });
   1.109 +      });
   1.110 +
   1.111 +    });
   1.112 +  });
   1.113 +}

mercurial