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: /** Private Browsing Test for Bug 394759 **/ michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let windowsToClose = []; michael@0: let closedWindowCount = 0; michael@0: // Prevent VM timers issues, cache now and increment it manually. michael@0: let now = Date.now(); michael@0: const TESTS = [ michael@0: { url: "about:config", michael@0: key: "bug 394759 Non-PB", michael@0: value: "uniq" + (++now) }, michael@0: { url: "about:mozilla", michael@0: key: "bug 394759 PB", michael@0: value: "uniq" + (++now) }, michael@0: ]; michael@0: michael@0: registerCleanupFunction(function() { michael@0: Services.prefs.clearUserPref("browser.sessionstore.interval"); michael@0: windowsToClose.forEach(function(win) { michael@0: win.close(); michael@0: }); michael@0: }); michael@0: michael@0: function testOpenCloseWindow(aIsPrivate, aTest, aCallback) { michael@0: whenNewWindowLoaded({ private: aIsPrivate }, function(win) { michael@0: whenBrowserLoaded(win.gBrowser.selectedBrowser, function() { michael@0: executeSoon(function() { michael@0: // Mark the window with some unique data to be restored later on. michael@0: ss.setWindowValue(win, aTest.key, aTest.value); michael@0: // Close. michael@0: win.close(); michael@0: aCallback(); michael@0: }); michael@0: }); michael@0: win.gBrowser.selectedBrowser.loadURI(aTest.url); michael@0: }); michael@0: } michael@0: michael@0: function testOnWindow(aIsPrivate, aValue, aCallback) { michael@0: whenNewWindowLoaded({ private: aIsPrivate }, function(win) { michael@0: windowsToClose.push(win); michael@0: executeSoon(function() checkClosedWindows(aIsPrivate, aValue, aCallback)); michael@0: }); michael@0: } michael@0: michael@0: function checkClosedWindows(aIsPrivate, aValue, aCallback) { michael@0: let data = JSON.parse(ss.getClosedWindowData())[0]; michael@0: is(ss.getClosedWindowCount(), 1, "Check the closed window count"); michael@0: ok(JSON.stringify(data).indexOf(aValue) > -1, michael@0: "Check the closed window data was stored correctly"); michael@0: aCallback(); michael@0: } michael@0: michael@0: function setupBlankState(aCallback) { michael@0: // Set interval to a large time so state won't be written while we setup michael@0: // environment. michael@0: Services.prefs.setIntPref("browser.sessionstore.interval", 100000); michael@0: michael@0: // Set up the browser in a blank state. Popup windows in previous tests michael@0: // result in different states on different platforms. michael@0: let blankState = JSON.stringify({ michael@0: windows: [{ michael@0: tabs: [{ entries: [{ url: "about:blank" }] }], michael@0: _closedTabs: [] michael@0: }], michael@0: _closedWindows: [] michael@0: }); michael@0: ss.setBrowserState(blankState); michael@0: michael@0: // Wait for the sessionstore.js file to be written before going on. michael@0: // Note: we don't wait for the complete event, since if asyncCopy fails we michael@0: // would timeout. michael@0: waitForSaveState(function(writing) { michael@0: ok(writing, "sessionstore.js is being written"); michael@0: closedWindowCount = ss.getClosedWindowCount(); michael@0: is(closedWindowCount, 0, "Correctly set window count"); michael@0: michael@0: executeSoon(aCallback); michael@0: }); michael@0: michael@0: // Remove the sessionstore.js file before setting the interval to 0 michael@0: let profilePath = Services.dirsvc.get("ProfD", Ci.nsIFile); michael@0: let sessionStoreJS = profilePath.clone(); michael@0: sessionStoreJS.append("sessionstore.js"); michael@0: if (sessionStoreJS.exists()) michael@0: sessionStoreJS.remove(false); michael@0: info("sessionstore.js was correctly removed: " + (!sessionStoreJS.exists())); michael@0: michael@0: // Make sure that sessionstore.js can be forced to be created by setting michael@0: // the interval pref to 0. michael@0: Services.prefs.setIntPref("browser.sessionstore.interval", 0); michael@0: } michael@0: michael@0: setupBlankState(function() { michael@0: testOpenCloseWindow(false, TESTS[0], function() { michael@0: testOpenCloseWindow(true, TESTS[1], function() { michael@0: testOnWindow(false, TESTS[0].value, function() { michael@0: testOnWindow(true, TESTS[0].value, finish); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: