Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | function checkState(tab) { |
michael@0 | 6 | // Go back and then forward, and make sure that the state objects received |
michael@0 | 7 | // from the popState event are as we expect them to be. |
michael@0 | 8 | // |
michael@0 | 9 | // We also add a node to the document's body when after going back and make |
michael@0 | 10 | // sure it's still there after we go forward -- this is to test that the two |
michael@0 | 11 | // history entries correspond to the same document. |
michael@0 | 12 | |
michael@0 | 13 | let popStateCount = 0; |
michael@0 | 14 | |
michael@0 | 15 | tab.linkedBrowser.addEventListener('popstate', function(aEvent) { |
michael@0 | 16 | let contentWindow = tab.linkedBrowser.contentWindow; |
michael@0 | 17 | if (popStateCount == 0) { |
michael@0 | 18 | popStateCount++; |
michael@0 | 19 | |
michael@0 | 20 | is(tab.linkedBrowser.contentWindow.testState, 'foo', |
michael@0 | 21 | 'testState after going back'); |
michael@0 | 22 | |
michael@0 | 23 | ok(aEvent.state, "Event should have a state property."); |
michael@0 | 24 | is(JSON.stringify(tab.linkedBrowser.contentWindow.history.state), JSON.stringify({obj1:1}), |
michael@0 | 25 | "first popstate object."); |
michael@0 | 26 | |
michael@0 | 27 | // Add a node with id "new-elem" to the document. |
michael@0 | 28 | let doc = contentWindow.document; |
michael@0 | 29 | ok(!doc.getElementById("new-elem"), |
michael@0 | 30 | "doc shouldn't contain new-elem before we add it."); |
michael@0 | 31 | let elem = doc.createElement("div"); |
michael@0 | 32 | elem.id = "new-elem"; |
michael@0 | 33 | doc.body.appendChild(elem); |
michael@0 | 34 | |
michael@0 | 35 | contentWindow.history.forward(); |
michael@0 | 36 | } |
michael@0 | 37 | else if (popStateCount == 1) { |
michael@0 | 38 | popStateCount++; |
michael@0 | 39 | is(aEvent.state.obj3.toString(), '/^a$/', "second popstate object."); |
michael@0 | 40 | |
michael@0 | 41 | // Make sure that the new-elem node is present in the document. If it's |
michael@0 | 42 | // not, then this history entry has a different doc identifier than the |
michael@0 | 43 | // previous entry, which is bad. |
michael@0 | 44 | let doc = contentWindow.document; |
michael@0 | 45 | let newElem = doc.getElementById("new-elem"); |
michael@0 | 46 | ok(newElem, "doc should contain new-elem."); |
michael@0 | 47 | newElem.parentNode.removeChild(newElem); |
michael@0 | 48 | ok(!doc.getElementById("new-elem"), "new-elem should be removed."); |
michael@0 | 49 | |
michael@0 | 50 | // Clean up after ourselves and finish the test. |
michael@0 | 51 | tab.linkedBrowser.removeEventListener("popstate", arguments.callee, true); |
michael@0 | 52 | gBrowser.removeTab(tab); |
michael@0 | 53 | finish(); |
michael@0 | 54 | } |
michael@0 | 55 | }, true); |
michael@0 | 56 | |
michael@0 | 57 | // Set some state in the page's window. When we go back(), the page should |
michael@0 | 58 | // be retrieved from bfcache, and this state should still be there. |
michael@0 | 59 | tab.linkedBrowser.contentWindow.testState = 'foo'; |
michael@0 | 60 | |
michael@0 | 61 | // Now go back. This should trigger the popstate event handler above. |
michael@0 | 62 | tab.linkedBrowser.contentWindow.history.back(); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | function test() { |
michael@0 | 66 | // Tests session restore functionality of history.pushState and |
michael@0 | 67 | // history.replaceState(). (Bug 500328) |
michael@0 | 68 | |
michael@0 | 69 | waitForExplicitFinish(); |
michael@0 | 70 | |
michael@0 | 71 | // We open a new blank window, let it load, and then load in |
michael@0 | 72 | // http://example.com. We need to load the blank window first, otherwise the |
michael@0 | 73 | // docshell gets confused and doesn't have a current history entry. |
michael@0 | 74 | let tab = gBrowser.addTab("about:blank"); |
michael@0 | 75 | let browser = tab.linkedBrowser; |
michael@0 | 76 | |
michael@0 | 77 | whenBrowserLoaded(browser, function() { |
michael@0 | 78 | browser.loadURI("http://example.com", null, null); |
michael@0 | 79 | |
michael@0 | 80 | whenBrowserLoaded(browser, function() { |
michael@0 | 81 | // After these push/replaceState calls, the window should have three |
michael@0 | 82 | // history entries: |
michael@0 | 83 | // testURL (state object: null) <-- oldest |
michael@0 | 84 | // testURL (state object: {obj1:1}) |
michael@0 | 85 | // testURL?page2 (state object: {obj3:/^a$/}) <-- newest |
michael@0 | 86 | let contentWindow = tab.linkedBrowser.contentWindow; |
michael@0 | 87 | let history = contentWindow.history; |
michael@0 | 88 | history.pushState({obj1:1}, "title-obj1"); |
michael@0 | 89 | history.pushState({obj2:2}, "title-obj2", "?page2"); |
michael@0 | 90 | history.replaceState({obj3:/^a$/}, "title-obj3"); |
michael@0 | 91 | |
michael@0 | 92 | SyncHandlers.get(tab.linkedBrowser).flush(); |
michael@0 | 93 | let state = ss.getTabState(tab); |
michael@0 | 94 | gBrowser.removeTab(tab); |
michael@0 | 95 | |
michael@0 | 96 | // Restore the state into a new tab. Things don't work well when we |
michael@0 | 97 | // restore into the old tab, but that's not a real use case anyway. |
michael@0 | 98 | let tab2 = gBrowser.addTab("about:blank"); |
michael@0 | 99 | ss.setTabState(tab2, state, true); |
michael@0 | 100 | |
michael@0 | 101 | // Run checkState() once the tab finishes loading its restored state. |
michael@0 | 102 | whenTabRestored(tab2, function() { |
michael@0 | 103 | SimpleTest.executeSoon(function() { |
michael@0 | 104 | checkState(tab2); |
michael@0 | 105 | }); |
michael@0 | 106 | }); |
michael@0 | 107 | |
michael@0 | 108 | }); |
michael@0 | 109 | }); |
michael@0 | 110 | } |