michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand"; michael@0: michael@0: function test() { michael@0: TestRunner.run(); michael@0: } michael@0: michael@0: function runTests() { michael@0: // Request a longer timeout because the test takes quite a while michael@0: // to complete on slow Windows debug machines and we would otherwise michael@0: // see a lot of (not so) intermittent test failures. michael@0: requestLongerTimeout(2); michael@0: michael@0: Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, true); michael@0: registerCleanupFunction(function () { michael@0: Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND); michael@0: }); michael@0: michael@0: let state = { windows: [{ tabs: [ michael@0: { entries: [{ url: "http://example.org/#1" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#2" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#3" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#4" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#5" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#6" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#7" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#8" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#9" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#10" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#11" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#12" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#13" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#14" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#15" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#16" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#17" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.org/#18" }], extData: { "uniq": r() } } michael@0: ], selected: 1 }] }; michael@0: michael@0: let loadCount = 0; michael@0: gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { michael@0: loadCount++; michael@0: is(aBrowser.currentURI.spec, state.windows[0].tabs[loadCount - 1].entries[0].url, michael@0: "load " + loadCount + " - browser loaded correct url"); michael@0: michael@0: if (loadCount <= state.windows[0].tabs.length) { michael@0: // double check that this tab was the right one michael@0: let expectedData = state.windows[0].tabs[loadCount - 1].extData.uniq; michael@0: let tab; michael@0: for (let i = 0; i < window.gBrowser.tabs.length; i++) { michael@0: if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) michael@0: tab = window.gBrowser.tabs[i]; michael@0: } michael@0: is(ss.getTabValue(tab, "uniq"), expectedData, michael@0: "load " + loadCount + " - correct tab was restored"); michael@0: michael@0: if (loadCount == state.windows[0].tabs.length) { michael@0: gProgressListener.unsetCallback(); michael@0: executeSoon(function () { michael@0: reloadAllTabs(state, function () { michael@0: waitForBrowserState(TestRunner.backupState, testCascade); michael@0: }); michael@0: }); michael@0: } else { michael@0: // reload the next tab michael@0: window.gBrowser.reloadTab(window.gBrowser.tabs[loadCount]); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: yield ss.setBrowserState(JSON.stringify(state)); michael@0: } michael@0: michael@0: function testCascade() { michael@0: Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, false); michael@0: michael@0: let state = { windows: [{ tabs: [ michael@0: { entries: [{ url: "http://example.com/#1" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com/#2" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com/#3" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com/#4" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com/#5" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com/#6" }], extData: { "uniq": r() } } michael@0: ] }] }; michael@0: michael@0: let loadCount = 0; michael@0: gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { michael@0: if (++loadCount < state.windows[0].tabs.length) { michael@0: return; michael@0: } michael@0: michael@0: gProgressListener.unsetCallback(); michael@0: executeSoon(function () { michael@0: reloadAllTabs(state, next); michael@0: }); michael@0: }); michael@0: michael@0: ss.setBrowserState(JSON.stringify(state)); michael@0: } michael@0: michael@0: function reloadAllTabs(aState, aCallback) { michael@0: // Simulate a left mouse button click with no modifiers, which is what michael@0: // Command-R, or clicking reload does. michael@0: let fakeEvent = { michael@0: button: 0, michael@0: metaKey: false, michael@0: altKey: false, michael@0: ctrlKey: false, michael@0: shiftKey: false michael@0: }; michael@0: michael@0: let loadCount = 0; michael@0: gWebProgressListener.setCallback(function (aBrowser) { michael@0: if (++loadCount <= aState.windows[0].tabs.length) { michael@0: // double check that this tab was the right one michael@0: let expectedData = aState.windows[0].tabs[loadCount - 1].extData.uniq; michael@0: let tab; michael@0: for (let i = 0; i < window.gBrowser.tabs.length; i++) { michael@0: if (!tab && window.gBrowser.tabs[i].linkedBrowser == aBrowser) michael@0: tab = window.gBrowser.tabs[i]; michael@0: } michael@0: is(ss.getTabValue(tab, "uniq"), expectedData, michael@0: "load " + loadCount + " - correct tab was reloaded"); michael@0: michael@0: if (loadCount == aState.windows[0].tabs.length) { michael@0: gWebProgressListener.unsetCallback(); michael@0: executeSoon(aCallback); michael@0: } else { michael@0: // reload the next tab michael@0: window.gBrowser.selectTabAtIndex(loadCount); michael@0: BrowserReloadOrDuplicate(fakeEvent); michael@0: } michael@0: } michael@0: }); michael@0: michael@0: BrowserReloadOrDuplicate(fakeEvent); michael@0: }