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: Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, false); michael@0: registerCleanupFunction(function () { michael@0: Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND); michael@0: }); michael@0: michael@0: // The first state will be loaded using setBrowserState, followed by the 2nd michael@0: // state also being loaded using setBrowserState, interrupting the first restore. michael@0: let state1 = { windows: [ michael@0: { michael@0: 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: ], michael@0: selected: 1 michael@0: }, michael@0: { michael@0: 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: ], michael@0: selected: 3 michael@0: } michael@0: ] }; michael@0: let state2 = { windows: [ michael@0: { michael@0: tabs: [ 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: ], michael@0: selected: 3 michael@0: }, michael@0: { michael@0: tabs: [ michael@0: { entries: [{ url: "http://example.com#5" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com#6" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com#7" }], extData: { "uniq": r() } }, michael@0: { entries: [{ url: "http://example.com#8" }], extData: { "uniq": r() } }, michael@0: ], michael@0: selected: 1 michael@0: } michael@0: ] }; michael@0: michael@0: // interruptedAfter will be set after the selected tab from each window have loaded. michael@0: let interruptedAfter = 0; michael@0: let loadedWindow1 = false; michael@0: let loadedWindow2 = false; michael@0: let numTabs = state2.windows[0].tabs.length + state2.windows[1].tabs.length; michael@0: michael@0: let loadCount = 0; michael@0: gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { michael@0: loadCount++; michael@0: michael@0: if (aBrowser.currentURI.spec == state1.windows[0].tabs[2].entries[0].url) michael@0: loadedWindow1 = true; michael@0: if (aBrowser.currentURI.spec == state1.windows[1].tabs[0].entries[0].url) michael@0: loadedWindow2 = true; michael@0: michael@0: if (!interruptedAfter && loadedWindow1 && loadedWindow2) { michael@0: interruptedAfter = loadCount; michael@0: ss.setBrowserState(JSON.stringify(state2)); michael@0: return; michael@0: } michael@0: michael@0: if (loadCount < numTabs + interruptedAfter) michael@0: return; michael@0: michael@0: // We don't actually care about load order in this test, just that they all michael@0: // do load. michael@0: is(loadCount, numTabs + interruptedAfter, "all tabs were restored"); michael@0: is(aNeedRestore, 0, "there are no tabs left needing restore"); michael@0: michael@0: // Remove the progress listener from this window, it will be removed from michael@0: // theWin when that window is closed (in setBrowserState). michael@0: gProgressListener.unsetCallback(); michael@0: executeSoon(next); michael@0: }); michael@0: michael@0: // We also want to catch the extra windows (there should be 2), so we need to observe domwindowopened michael@0: Services.ww.registerNotification(function observer(aSubject, aTopic, aData) { michael@0: if (aTopic == "domwindowopened") { michael@0: let win = aSubject.QueryInterface(Ci.nsIDOMWindow); michael@0: win.addEventListener("load", function onLoad() { michael@0: win.removeEventListener("load", onLoad); michael@0: Services.ww.unregisterNotification(observer); michael@0: win.gBrowser.addTabsProgressListener(gProgressListener); michael@0: }); michael@0: } michael@0: }); michael@0: michael@0: yield ss.setBrowserState(JSON.stringify(state1)); michael@0: }