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: // Mirrors WINDOW_ATTRIBUTES IN nsSessionStore.js michael@0: const WINDOW_ATTRIBUTES = ["width", "height", "screenX", "screenY", "sizemode"]; michael@0: michael@0: let stateBackup = ss.getBrowserState(); michael@0: michael@0: let originalWarnOnClose = gPrefService.getBoolPref("browser.tabs.warnOnClose"); michael@0: let originalStartupPage = gPrefService.getIntPref("browser.startup.page"); michael@0: let originalWindowType = document.documentElement.getAttribute("windowtype"); michael@0: michael@0: let gotLastWindowClosedTopic = false; michael@0: let shouldPinTab = false; michael@0: let shouldOpenTabs = false; michael@0: let shouldCloseTab = false; michael@0: let testNum = 0; michael@0: let afterTestCallback; michael@0: michael@0: // Set state so we know the closed windows content michael@0: let testState = { michael@0: windows: [ michael@0: { tabs: [{ entries: [{ url: "http://example.org" }] }] } michael@0: ], michael@0: _closedWindows: [] michael@0: }; michael@0: michael@0: // We'll push a set of conditions and callbacks into this array michael@0: // Ideally we would also test win/linux under a complete set of conditions, but michael@0: // the tests for osx mirror the other set of conditions possible on win/linux. michael@0: let tests = []; michael@0: michael@0: // the third & fourth test share a condition check, keep it DRY michael@0: function checkOSX34Generator(num) { michael@0: return function(aPreviousState, aCurState) { michael@0: // In here, we should have restored the pinned tab, so only the unpinned tab michael@0: // should be in aCurState. So let's shape our expectations. michael@0: let expectedState = JSON.parse(aPreviousState); michael@0: expectedState[0].tabs.shift(); michael@0: // size attributes are stripped out in _prepDataForDeferredRestore in nsSessionStore. michael@0: // This isn't the best approach, but neither is comparing JSON strings michael@0: WINDOW_ATTRIBUTES.forEach(function (attr) delete expectedState[0][attr]); michael@0: michael@0: is(aCurState, JSON.stringify(expectedState), michael@0: "test #" + num + ": closedWindowState is as expected"); michael@0: }; michael@0: } michael@0: function checkNoWindowsGenerator(num) { michael@0: return function(aPreviousState, aCurState) { michael@0: is(aCurState, "[]", "test #" + num + ": there should be no closedWindowsLeft"); michael@0: }; michael@0: } michael@0: michael@0: // The first test has 0 pinned tabs and 1 unpinned tab michael@0: tests.push({ michael@0: pinned: false, michael@0: extra: false, michael@0: close: false, michael@0: checkWinLin: checkNoWindowsGenerator(1), michael@0: checkOSX: function(aPreviousState, aCurState) { michael@0: is(aCurState, aPreviousState, "test #1: closed window state is unchanged"); michael@0: } michael@0: }); michael@0: michael@0: // The second test has 1 pinned tab and 0 unpinned tabs. michael@0: tests.push({ michael@0: pinned: true, michael@0: extra: false, michael@0: close: false, michael@0: checkWinLin: checkNoWindowsGenerator(2), michael@0: checkOSX: checkNoWindowsGenerator(2) michael@0: }); michael@0: michael@0: // The third test has 1 pinned tab and 2 unpinned tabs. michael@0: tests.push({ michael@0: pinned: true, michael@0: extra: true, michael@0: close: false, michael@0: checkWinLin: checkNoWindowsGenerator(3), michael@0: checkOSX: checkOSX34Generator(3) michael@0: }); michael@0: michael@0: // The fourth test has 1 pinned tab, 2 unpinned tabs, and closes one unpinned tab. michael@0: tests.push({ michael@0: pinned: true, michael@0: extra: true, michael@0: close: "one", michael@0: checkWinLin: checkNoWindowsGenerator(4), michael@0: checkOSX: checkOSX34Generator(4) michael@0: }); michael@0: michael@0: // The fifth test has 1 pinned tab, 2 unpinned tabs, and closes both unpinned tabs. michael@0: tests.push({ michael@0: pinned: true, michael@0: extra: true, michael@0: close: "both", michael@0: checkWinLin: checkNoWindowsGenerator(5), michael@0: checkOSX: checkNoWindowsGenerator(5) michael@0: }); michael@0: michael@0: michael@0: function test() { michael@0: /** Test for Bug 589246 - Closed window state getting corrupted when closing michael@0: and reopening last browser window without exiting browser **/ michael@0: waitForExplicitFinish(); michael@0: // windows opening & closing, so extending the timeout michael@0: requestLongerTimeout(2); michael@0: michael@0: // We don't want the quit dialog pref michael@0: gPrefService.setBoolPref("browser.tabs.warnOnClose", false); michael@0: // Ensure that we would restore the session (important for Windows) michael@0: gPrefService.setIntPref("browser.startup.page", 3); michael@0: michael@0: runNextTestOrFinish(); michael@0: } michael@0: michael@0: function runNextTestOrFinish() { michael@0: if (tests.length) { michael@0: setupForTest(tests.shift()) michael@0: } michael@0: else { michael@0: // some state is cleaned up at the end of each test, but not all michael@0: ["browser.tabs.warnOnClose", "browser.startup.page"].forEach(function(p) { michael@0: if (gPrefService.prefHasUserValue(p)) michael@0: gPrefService.clearUserPref(p); michael@0: }); michael@0: michael@0: ss.setBrowserState(stateBackup); michael@0: executeSoon(finish); michael@0: } michael@0: } michael@0: michael@0: function setupForTest(aConditions) { michael@0: // reset some checks michael@0: gotLastWindowClosedTopic = false; michael@0: shouldPinTab = aConditions.pinned; michael@0: shouldOpenTabs = aConditions.extra; michael@0: shouldCloseTab = aConditions.close; michael@0: testNum++; michael@0: michael@0: // set our test callback michael@0: afterTestCallback = /Mac/.test(navigator.platform) ? aConditions.checkOSX michael@0: : aConditions.checkWinLin; michael@0: michael@0: // Add observers michael@0: Services.obs.addObserver(onLastWindowClosed, "browser-lastwindow-close-granted", false); michael@0: michael@0: // Set the state michael@0: Services.obs.addObserver(onStateRestored, "sessionstore-browser-state-restored", false); michael@0: ss.setBrowserState(JSON.stringify(testState)); michael@0: } michael@0: michael@0: function onStateRestored(aSubject, aTopic, aData) { michael@0: info("test #" + testNum + ": onStateRestored"); michael@0: Services.obs.removeObserver(onStateRestored, "sessionstore-browser-state-restored"); michael@0: michael@0: // change this window's windowtype so that closing a new window will trigger michael@0: // browser-lastwindow-close-granted. michael@0: document.documentElement.setAttribute("windowtype", "navigator:testrunner"); michael@0: michael@0: let newWin = openDialog(location, "_blank", "chrome,all,dialog=no", "http://example.com"); michael@0: newWin.addEventListener("load", function(aEvent) { michael@0: newWin.removeEventListener("load", arguments.callee, false); michael@0: michael@0: whenBrowserLoaded(newWin.gBrowser.selectedBrowser, function() { michael@0: // pin this tab michael@0: if (shouldPinTab) michael@0: newWin.gBrowser.pinTab(newWin.gBrowser.selectedTab); michael@0: michael@0: newWin.addEventListener("unload", function () { michael@0: newWin.removeEventListener("unload", arguments.callee, false); michael@0: onWindowUnloaded(); michael@0: }, false); michael@0: // Open a new tab as well. On Windows/Linux this will be restored when the michael@0: // new window is opened below (in onWindowUnloaded). On OS X we'll just michael@0: // restore the pinned tabs, leaving the unpinned tab in the closedWindowsData. michael@0: if (shouldOpenTabs) { michael@0: let newTab = newWin.gBrowser.addTab("about:config"); michael@0: let newTab2 = newWin.gBrowser.addTab("about:buildconfig"); michael@0: michael@0: newTab.linkedBrowser.addEventListener("load", function() { michael@0: newTab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: michael@0: if (shouldCloseTab == "one") { michael@0: newWin.gBrowser.removeTab(newTab2); michael@0: } michael@0: else if (shouldCloseTab == "both") { michael@0: newWin.gBrowser.removeTab(newTab); michael@0: newWin.gBrowser.removeTab(newTab2); michael@0: } michael@0: newWin.BrowserTryToCloseWindow(); michael@0: }, true); michael@0: } michael@0: else { michael@0: newWin.BrowserTryToCloseWindow(); michael@0: } michael@0: }); michael@0: }, false); michael@0: } michael@0: michael@0: // This will be called before the window is actually closed michael@0: function onLastWindowClosed(aSubject, aTopic, aData) { michael@0: info("test #" + testNum + ": onLastWindowClosed"); michael@0: Services.obs.removeObserver(onLastWindowClosed, "browser-lastwindow-close-granted"); michael@0: gotLastWindowClosedTopic = true; michael@0: } michael@0: michael@0: // This is the unload event listener on the new window (from onStateRestored). michael@0: // Unload is fired after the window is closed, so sessionstore has already michael@0: // updated _closedWindows (which is important). We'll open a new window here michael@0: // which should actually trigger the bug. michael@0: function onWindowUnloaded() { michael@0: info("test #" + testNum + ": onWindowClosed"); michael@0: ok(gotLastWindowClosedTopic, "test #" + testNum + ": browser-lastwindow-close-granted was notified prior"); michael@0: michael@0: let previousClosedWindowData = ss.getClosedWindowData(); michael@0: michael@0: // Now we want to open a new window michael@0: let newWin = openDialog(location, "_blank", "chrome,all,dialog=no", "about:mozilla"); michael@0: newWin.addEventListener("load", function(aEvent) { michael@0: newWin.removeEventListener("load", arguments.callee, false); michael@0: michael@0: newWin.gBrowser.selectedBrowser.addEventListener("load", function () { michael@0: newWin.gBrowser.selectedBrowser.removeEventListener("load", arguments.callee, true); michael@0: michael@0: // Good enough for checking the state michael@0: afterTestCallback(previousClosedWindowData, ss.getClosedWindowData()); michael@0: afterTestCleanup(newWin); michael@0: }, true); michael@0: michael@0: }, false); michael@0: } michael@0: michael@0: function afterTestCleanup(aNewWin) { michael@0: executeSoon(function() { michael@0: aNewWin.close(); michael@0: document.documentElement.setAttribute("windowtype", originalWindowType); michael@0: runNextTestOrFinish(); michael@0: }); michael@0: }