1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_586068-browser_state_interrupted.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,108 @@ 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 +const PREF_RESTORE_ON_DEMAND = "browser.sessionstore.restore_on_demand"; 1.9 + 1.10 +function test() { 1.11 + TestRunner.run(); 1.12 +} 1.13 + 1.14 +function runTests() { 1.15 + Services.prefs.setBoolPref(PREF_RESTORE_ON_DEMAND, false); 1.16 + registerCleanupFunction(function () { 1.17 + Services.prefs.clearUserPref(PREF_RESTORE_ON_DEMAND); 1.18 + }); 1.19 + 1.20 + // The first state will be loaded using setBrowserState, followed by the 2nd 1.21 + // state also being loaded using setBrowserState, interrupting the first restore. 1.22 + let state1 = { windows: [ 1.23 + { 1.24 + tabs: [ 1.25 + { entries: [{ url: "http://example.org#1" }], extData: { "uniq": r() } }, 1.26 + { entries: [{ url: "http://example.org#2" }], extData: { "uniq": r() } }, 1.27 + { entries: [{ url: "http://example.org#3" }], extData: { "uniq": r() } }, 1.28 + { entries: [{ url: "http://example.org#4" }], extData: { "uniq": r() } } 1.29 + ], 1.30 + selected: 1 1.31 + }, 1.32 + { 1.33 + tabs: [ 1.34 + { entries: [{ url: "http://example.com#1" }], extData: { "uniq": r() } }, 1.35 + { entries: [{ url: "http://example.com#2" }], extData: { "uniq": r() } }, 1.36 + { entries: [{ url: "http://example.com#3" }], extData: { "uniq": r() } }, 1.37 + { entries: [{ url: "http://example.com#4" }], extData: { "uniq": r() } }, 1.38 + ], 1.39 + selected: 3 1.40 + } 1.41 + ] }; 1.42 + let state2 = { windows: [ 1.43 + { 1.44 + tabs: [ 1.45 + { entries: [{ url: "http://example.org#5" }], extData: { "uniq": r() } }, 1.46 + { entries: [{ url: "http://example.org#6" }], extData: { "uniq": r() } }, 1.47 + { entries: [{ url: "http://example.org#7" }], extData: { "uniq": r() } }, 1.48 + { entries: [{ url: "http://example.org#8" }], extData: { "uniq": r() } } 1.49 + ], 1.50 + selected: 3 1.51 + }, 1.52 + { 1.53 + tabs: [ 1.54 + { entries: [{ url: "http://example.com#5" }], extData: { "uniq": r() } }, 1.55 + { entries: [{ url: "http://example.com#6" }], extData: { "uniq": r() } }, 1.56 + { entries: [{ url: "http://example.com#7" }], extData: { "uniq": r() } }, 1.57 + { entries: [{ url: "http://example.com#8" }], extData: { "uniq": r() } }, 1.58 + ], 1.59 + selected: 1 1.60 + } 1.61 + ] }; 1.62 + 1.63 + // interruptedAfter will be set after the selected tab from each window have loaded. 1.64 + let interruptedAfter = 0; 1.65 + let loadedWindow1 = false; 1.66 + let loadedWindow2 = false; 1.67 + let numTabs = state2.windows[0].tabs.length + state2.windows[1].tabs.length; 1.68 + 1.69 + let loadCount = 0; 1.70 + gProgressListener.setCallback(function (aBrowser, aNeedRestore, aRestoring, aRestored) { 1.71 + loadCount++; 1.72 + 1.73 + if (aBrowser.currentURI.spec == state1.windows[0].tabs[2].entries[0].url) 1.74 + loadedWindow1 = true; 1.75 + if (aBrowser.currentURI.spec == state1.windows[1].tabs[0].entries[0].url) 1.76 + loadedWindow2 = true; 1.77 + 1.78 + if (!interruptedAfter && loadedWindow1 && loadedWindow2) { 1.79 + interruptedAfter = loadCount; 1.80 + ss.setBrowserState(JSON.stringify(state2)); 1.81 + return; 1.82 + } 1.83 + 1.84 + if (loadCount < numTabs + interruptedAfter) 1.85 + return; 1.86 + 1.87 + // We don't actually care about load order in this test, just that they all 1.88 + // do load. 1.89 + is(loadCount, numTabs + interruptedAfter, "all tabs were restored"); 1.90 + is(aNeedRestore, 0, "there are no tabs left needing restore"); 1.91 + 1.92 + // Remove the progress listener from this window, it will be removed from 1.93 + // theWin when that window is closed (in setBrowserState). 1.94 + gProgressListener.unsetCallback(); 1.95 + executeSoon(next); 1.96 + }); 1.97 + 1.98 + // We also want to catch the extra windows (there should be 2), so we need to observe domwindowopened 1.99 + Services.ww.registerNotification(function observer(aSubject, aTopic, aData) { 1.100 + if (aTopic == "domwindowopened") { 1.101 + let win = aSubject.QueryInterface(Ci.nsIDOMWindow); 1.102 + win.addEventListener("load", function onLoad() { 1.103 + win.removeEventListener("load", onLoad); 1.104 + Services.ww.unregisterNotification(observer); 1.105 + win.gBrowser.addTabsProgressListener(gProgressListener); 1.106 + }); 1.107 + } 1.108 + }); 1.109 + 1.110 + yield ss.setBrowserState(JSON.stringify(state1)); 1.111 +}