1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_490040.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,123 @@ 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 test() { 1.9 + /** Test for Bug 490040 **/ 1.10 + 1.11 + waitForExplicitFinish(); 1.12 + 1.13 + function testWithState(aState) { 1.14 + // Ensure we can store the window if needed. 1.15 + let curClosedWindowCount = ss.getClosedWindowCount(); 1.16 + gPrefService.setIntPref("browser.sessionstore.max_windows_undo", 1.17 + curClosedWindowCount + 1); 1.18 + 1.19 + var origWin; 1.20 + function windowObserver(aSubject, aTopic, aData) { 1.21 + let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow); 1.22 + if (origWin && theWin != origWin) 1.23 + return; 1.24 + 1.25 + switch (aTopic) { 1.26 + case "domwindowopened": 1.27 + origWin = theWin; 1.28 + theWin.addEventListener("load", function () { 1.29 + theWin.removeEventListener("load", arguments.callee, false); 1.30 + executeSoon(function () { 1.31 + // Close the window as soon as the first tab loads, or 1.32 + // immediately if there are no tabs. 1.33 + if (aState.windowState.windows[0].tabs[0].entries.length) { 1.34 + theWin.gBrowser.addEventListener("load", function() { 1.35 + theWin.gBrowser.removeEventListener("load", 1.36 + arguments.callee, true); 1.37 + theWin.close(); 1.38 + }, true); 1.39 + } else { 1.40 + executeSoon(function () { 1.41 + theWin.close(); 1.42 + }); 1.43 + } 1.44 + ss.setWindowState(theWin, JSON.stringify(aState.windowState), 1.45 + true); 1.46 + }); 1.47 + }, false); 1.48 + break; 1.49 + 1.50 + case "domwindowclosed": 1.51 + Services.ww.unregisterNotification(windowObserver); 1.52 + // Use executeSoon to ensure this happens after SS observer. 1.53 + executeSoon(function () { 1.54 + is(ss.getClosedWindowCount(), 1.55 + curClosedWindowCount + (aState.shouldBeAdded ? 1 : 0), 1.56 + "That window should " + (aState.shouldBeAdded ? "" : "not ") + 1.57 + "be restorable"); 1.58 + executeSoon(runNextTest); 1.59 + }); 1.60 + break; 1.61 + } 1.62 + } 1.63 + Services.ww.registerNotification(windowObserver); 1.64 + Services.ww.openWindow(null, 1.65 + location, 1.66 + "_blank", 1.67 + "chrome,all,dialog=no", 1.68 + null); 1.69 + } 1.70 + 1.71 + // Only windows with open tabs are restorable. Windows where a lone tab is 1.72 + // detached may have _closedTabs, but is left with just an empty tab. 1.73 + let states = [ 1.74 + { 1.75 + shouldBeAdded: true, 1.76 + windowState: { 1.77 + windows: [{ 1.78 + tabs: [{ entries: [{ url: "http://example.com", title: "example.com" }] }], 1.79 + selected: 1, 1.80 + _closedTabs: [] 1.81 + }] 1.82 + } 1.83 + }, 1.84 + { 1.85 + shouldBeAdded: false, 1.86 + windowState: { 1.87 + windows: [{ 1.88 + tabs: [{ entries: [] }], 1.89 + _closedTabs: [] 1.90 + }] 1.91 + } 1.92 + }, 1.93 + { 1.94 + shouldBeAdded: false, 1.95 + windowState: { 1.96 + windows: [{ 1.97 + tabs: [{ entries: [] }], 1.98 + _closedTabs: [{ state: { entries: [{ url: "http://example.com", index: 1 }] } }] 1.99 + }] 1.100 + } 1.101 + }, 1.102 + { 1.103 + shouldBeAdded: false, 1.104 + windowState: { 1.105 + windows: [{ 1.106 + tabs: [{ entries: [] }], 1.107 + _closedTabs: [], 1.108 + extData: { keyname: "pi != " + Math.random() } 1.109 + }] 1.110 + } 1.111 + } 1.112 + ]; 1.113 + 1.114 + function runNextTest() { 1.115 + if (states.length) { 1.116 + let state = states.shift(); 1.117 + testWithState(state); 1.118 + } 1.119 + else { 1.120 + gPrefService.clearUserPref("browser.sessionstore.max_windows_undo"); 1.121 + finish(); 1.122 + } 1.123 + } 1.124 + runNextTest(); 1.125 +} 1.126 +