1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_394759_perwindowpb.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,109 @@ 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 +/** Private Browsing Test for Bug 394759 **/ 1.9 +function test() { 1.10 + waitForExplicitFinish(); 1.11 + 1.12 + let windowsToClose = []; 1.13 + let closedWindowCount = 0; 1.14 + // Prevent VM timers issues, cache now and increment it manually. 1.15 + let now = Date.now(); 1.16 + const TESTS = [ 1.17 + { url: "about:config", 1.18 + key: "bug 394759 Non-PB", 1.19 + value: "uniq" + (++now) }, 1.20 + { url: "about:mozilla", 1.21 + key: "bug 394759 PB", 1.22 + value: "uniq" + (++now) }, 1.23 + ]; 1.24 + 1.25 + registerCleanupFunction(function() { 1.26 + Services.prefs.clearUserPref("browser.sessionstore.interval"); 1.27 + windowsToClose.forEach(function(win) { 1.28 + win.close(); 1.29 + }); 1.30 + }); 1.31 + 1.32 + function testOpenCloseWindow(aIsPrivate, aTest, aCallback) { 1.33 + whenNewWindowLoaded({ private: aIsPrivate }, function(win) { 1.34 + whenBrowserLoaded(win.gBrowser.selectedBrowser, function() { 1.35 + executeSoon(function() { 1.36 + // Mark the window with some unique data to be restored later on. 1.37 + ss.setWindowValue(win, aTest.key, aTest.value); 1.38 + // Close. 1.39 + win.close(); 1.40 + aCallback(); 1.41 + }); 1.42 + }); 1.43 + win.gBrowser.selectedBrowser.loadURI(aTest.url); 1.44 + }); 1.45 + } 1.46 + 1.47 + function testOnWindow(aIsPrivate, aValue, aCallback) { 1.48 + whenNewWindowLoaded({ private: aIsPrivate }, function(win) { 1.49 + windowsToClose.push(win); 1.50 + executeSoon(function() checkClosedWindows(aIsPrivate, aValue, aCallback)); 1.51 + }); 1.52 + } 1.53 + 1.54 + function checkClosedWindows(aIsPrivate, aValue, aCallback) { 1.55 + let data = JSON.parse(ss.getClosedWindowData())[0]; 1.56 + is(ss.getClosedWindowCount(), 1, "Check the closed window count"); 1.57 + ok(JSON.stringify(data).indexOf(aValue) > -1, 1.58 + "Check the closed window data was stored correctly"); 1.59 + aCallback(); 1.60 + } 1.61 + 1.62 + function setupBlankState(aCallback) { 1.63 + // Set interval to a large time so state won't be written while we setup 1.64 + // environment. 1.65 + Services.prefs.setIntPref("browser.sessionstore.interval", 100000); 1.66 + 1.67 + // Set up the browser in a blank state. Popup windows in previous tests 1.68 + // result in different states on different platforms. 1.69 + let blankState = JSON.stringify({ 1.70 + windows: [{ 1.71 + tabs: [{ entries: [{ url: "about:blank" }] }], 1.72 + _closedTabs: [] 1.73 + }], 1.74 + _closedWindows: [] 1.75 + }); 1.76 + ss.setBrowserState(blankState); 1.77 + 1.78 + // Wait for the sessionstore.js file to be written before going on. 1.79 + // Note: we don't wait for the complete event, since if asyncCopy fails we 1.80 + // would timeout. 1.81 + waitForSaveState(function(writing) { 1.82 + ok(writing, "sessionstore.js is being written"); 1.83 + closedWindowCount = ss.getClosedWindowCount(); 1.84 + is(closedWindowCount, 0, "Correctly set window count"); 1.85 + 1.86 + executeSoon(aCallback); 1.87 + }); 1.88 + 1.89 + // Remove the sessionstore.js file before setting the interval to 0 1.90 + let profilePath = Services.dirsvc.get("ProfD", Ci.nsIFile); 1.91 + let sessionStoreJS = profilePath.clone(); 1.92 + sessionStoreJS.append("sessionstore.js"); 1.93 + if (sessionStoreJS.exists()) 1.94 + sessionStoreJS.remove(false); 1.95 + info("sessionstore.js was correctly removed: " + (!sessionStoreJS.exists())); 1.96 + 1.97 + // Make sure that sessionstore.js can be forced to be created by setting 1.98 + // the interval pref to 0. 1.99 + Services.prefs.setIntPref("browser.sessionstore.interval", 0); 1.100 + } 1.101 + 1.102 + setupBlankState(function() { 1.103 + testOpenCloseWindow(false, TESTS[0], function() { 1.104 + testOpenCloseWindow(true, TESTS[1], function() { 1.105 + testOnWindow(false, TESTS[0].value, function() { 1.106 + testOnWindow(true, TESTS[0].value, finish); 1.107 + }); 1.108 + }); 1.109 + }); 1.110 + }); 1.111 +} 1.112 +