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: function test() { michael@0: /** Test for Bug 490040 **/ michael@0: michael@0: waitForExplicitFinish(); michael@0: michael@0: function testWithState(aState) { michael@0: // Ensure we can store the window if needed. michael@0: let curClosedWindowCount = ss.getClosedWindowCount(); michael@0: gPrefService.setIntPref("browser.sessionstore.max_windows_undo", michael@0: curClosedWindowCount + 1); michael@0: michael@0: var origWin; michael@0: function windowObserver(aSubject, aTopic, aData) { michael@0: let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow); michael@0: if (origWin && theWin != origWin) michael@0: return; michael@0: michael@0: switch (aTopic) { michael@0: case "domwindowopened": michael@0: origWin = theWin; michael@0: theWin.addEventListener("load", function () { michael@0: theWin.removeEventListener("load", arguments.callee, false); michael@0: executeSoon(function () { michael@0: // Close the window as soon as the first tab loads, or michael@0: // immediately if there are no tabs. michael@0: if (aState.windowState.windows[0].tabs[0].entries.length) { michael@0: theWin.gBrowser.addEventListener("load", function() { michael@0: theWin.gBrowser.removeEventListener("load", michael@0: arguments.callee, true); michael@0: theWin.close(); michael@0: }, true); michael@0: } else { michael@0: executeSoon(function () { michael@0: theWin.close(); michael@0: }); michael@0: } michael@0: ss.setWindowState(theWin, JSON.stringify(aState.windowState), michael@0: true); michael@0: }); michael@0: }, false); michael@0: break; michael@0: michael@0: case "domwindowclosed": michael@0: Services.ww.unregisterNotification(windowObserver); michael@0: // Use executeSoon to ensure this happens after SS observer. michael@0: executeSoon(function () { michael@0: is(ss.getClosedWindowCount(), michael@0: curClosedWindowCount + (aState.shouldBeAdded ? 1 : 0), michael@0: "That window should " + (aState.shouldBeAdded ? "" : "not ") + michael@0: "be restorable"); michael@0: executeSoon(runNextTest); michael@0: }); michael@0: break; michael@0: } michael@0: } michael@0: Services.ww.registerNotification(windowObserver); michael@0: Services.ww.openWindow(null, michael@0: location, michael@0: "_blank", michael@0: "chrome,all,dialog=no", michael@0: null); michael@0: } michael@0: michael@0: // Only windows with open tabs are restorable. Windows where a lone tab is michael@0: // detached may have _closedTabs, but is left with just an empty tab. michael@0: let states = [ michael@0: { michael@0: shouldBeAdded: true, michael@0: windowState: { michael@0: windows: [{ michael@0: tabs: [{ entries: [{ url: "http://example.com", title: "example.com" }] }], michael@0: selected: 1, michael@0: _closedTabs: [] michael@0: }] michael@0: } michael@0: }, michael@0: { michael@0: shouldBeAdded: false, michael@0: windowState: { michael@0: windows: [{ michael@0: tabs: [{ entries: [] }], michael@0: _closedTabs: [] michael@0: }] michael@0: } michael@0: }, michael@0: { michael@0: shouldBeAdded: false, michael@0: windowState: { michael@0: windows: [{ michael@0: tabs: [{ entries: [] }], michael@0: _closedTabs: [{ state: { entries: [{ url: "http://example.com", index: 1 }] } }] michael@0: }] michael@0: } michael@0: }, michael@0: { michael@0: shouldBeAdded: false, michael@0: windowState: { michael@0: windows: [{ michael@0: tabs: [{ entries: [] }], michael@0: _closedTabs: [], michael@0: extData: { keyname: "pi != " + Math.random() } michael@0: }] michael@0: } michael@0: } michael@0: ]; michael@0: michael@0: function runNextTest() { michael@0: if (states.length) { michael@0: let state = states.shift(); michael@0: testWithState(state); michael@0: } michael@0: else { michael@0: gPrefService.clearUserPref("browser.sessionstore.max_windows_undo"); michael@0: finish(); michael@0: } michael@0: } michael@0: runNextTest(); michael@0: } michael@0: