michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: const originalState = ss.getBrowserState(); michael@0: michael@0: /** Private Browsing Test for Bug 819510 **/ michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: runNextTest(); michael@0: } michael@0: michael@0: let tests = [test_1, test_2, test_3 ]; michael@0: michael@0: const testState = { michael@0: windows: [{ michael@0: tabs: [ michael@0: { entries: [{ url: "about:blank" }] }, michael@0: ] michael@0: }] michael@0: }; michael@0: michael@0: function runNextTest() { michael@0: // Set an empty state michael@0: closeAllButPrimaryWindow(); michael@0: michael@0: // Run the next test, or finish michael@0: if (tests.length) { michael@0: let currentTest = tests.shift(); michael@0: waitForBrowserState(testState, currentTest); michael@0: } else { michael@0: Services.obs.addObserver( michael@0: function observe(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(observe, aTopic); michael@0: finish(); michael@0: }, michael@0: "sessionstore-browser-state-restored", false); michael@0: ss.setBrowserState(originalState); michael@0: } michael@0: } michael@0: michael@0: // Test opening default mochitest-normal-private-normal-private windows michael@0: // (saving the state with last window being private) michael@0: function test_1() { michael@0: testOnWindow(false, function(aWindow) { michael@0: aWindow.gBrowser.addTab("http://www.example.com/1"); michael@0: testOnWindow(true, function(aWindow) { michael@0: aWindow.gBrowser.addTab("http://www.example.com/2"); michael@0: testOnWindow(false, function(aWindow) { michael@0: aWindow.gBrowser.addTab("http://www.example.com/3"); michael@0: testOnWindow(true, function(aWindow) { michael@0: aWindow.gBrowser.addTab("http://www.example.com/4"); michael@0: michael@0: let curState = JSON.parse(ss.getBrowserState()); michael@0: is (curState.windows.length, 5, "Browser has opened 5 windows"); michael@0: is (curState.windows[2].isPrivate, true, "Window is private"); michael@0: is (curState.windows[4].isPrivate, true, "Last window is private"); michael@0: is (curState.selectedWindow, 5, "Last window opened is the one selected"); michael@0: michael@0: forceWriteState(function(state) { michael@0: is(state.windows.length, 3, michael@0: "sessionstore state: 3 windows in data being written to disk"); michael@0: is (state.selectedWindow, 3, michael@0: "Selected window is updated to match one of the saved windows"); michael@0: state.windows.forEach(function(win) { michael@0: is(!win.isPrivate, true, "Saved window is not private"); michael@0: }); michael@0: is(state._closedWindows.length, 0, michael@0: "sessionstore state: no closed windows in data being written to disk"); michael@0: runNextTest(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: // Test opening default mochitest window + 2 private windows michael@0: function test_2() { michael@0: testOnWindow(true, function(aWindow) { michael@0: aWindow.gBrowser.addTab("http://www.example.com/1"); michael@0: testOnWindow(true, function(aWindow) { michael@0: aWindow.gBrowser.addTab("http://www.example.com/2"); michael@0: michael@0: let curState = JSON.parse(ss.getBrowserState()); michael@0: is (curState.windows.length, 3, "Browser has opened 3 windows"); michael@0: is (curState.windows[1].isPrivate, true, "Window 1 is private"); michael@0: is (curState.windows[2].isPrivate, true, "Window 2 is private"); michael@0: is (curState.selectedWindow, 3, "Last window opened is the one selected"); michael@0: michael@0: forceWriteState(function(state) { michael@0: is(state.windows.length, 1, michael@0: "sessionstore state: 1 windows in data being written to disk"); michael@0: is (state.selectedWindow, 1, michael@0: "Selected window is updated to match one of the saved windows"); michael@0: is(state._closedWindows.length, 0, michael@0: "sessionstore state: no closed windows in data being written to disk"); michael@0: runNextTest(); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: // Test opening default-normal-private-normal windows and closing a normal window michael@0: function test_3() { michael@0: testOnWindow(false, function(normalWindow) { michael@0: waitForTabLoad(normalWindow, "http://www.example.com/", function() { michael@0: testOnWindow(true, function(aWindow) { michael@0: waitForTabLoad(aWindow, "http://www.example.com/", function() { michael@0: testOnWindow(false, function(aWindow) { michael@0: waitForTabLoad(aWindow, "http://www.example.com/", function() { michael@0: michael@0: let curState = JSON.parse(ss.getBrowserState()); michael@0: is(curState.windows.length, 4, "Browser has opened 4 windows"); michael@0: is(curState.windows[2].isPrivate, true, "Window 2 is private"); michael@0: is(curState.selectedWindow, 4, "Last window opened is the one selected"); michael@0: michael@0: waitForWindowClose(normalWindow, function() { michael@0: // Pin and unpin a tab before checking the written state so that michael@0: // the list of restoring windows gets cleared. Otherwise the michael@0: // window we just closed would be marked as not closed. michael@0: let tab = aWindow.gBrowser.tabs[0]; michael@0: aWindow.gBrowser.pinTab(tab); michael@0: aWindow.gBrowser.unpinTab(tab); michael@0: michael@0: forceWriteState(function(state) { michael@0: is(state.windows.length, 2, michael@0: "sessionstore state: 2 windows in data being written to disk"); michael@0: is(state.selectedWindow, 2, michael@0: "Selected window is updated to match one of the saved windows"); michael@0: state.windows.forEach(function(win) { michael@0: is(!win.isPrivate, true, "Saved window is not private"); michael@0: }); michael@0: is(state._closedWindows.length, 1, michael@0: "sessionstore state: 1 closed window in data being written to disk"); michael@0: state._closedWindows.forEach(function(win) { michael@0: is(!win.isPrivate, true, "Closed window is not private"); michael@0: }); michael@0: runNextTest(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function waitForWindowClose(aWin, aCallback) { michael@0: let winCount = JSON.parse(ss.getBrowserState()).windows.length; michael@0: aWin.addEventListener("SSWindowClosing", function onWindowClosing() { michael@0: aWin.removeEventListener("SSWindowClosing", onWindowClosing, false); michael@0: function checkCount() { michael@0: let state = JSON.parse(ss.getBrowserState()); michael@0: if (state.windows.length == (winCount - 1)) { michael@0: aCallback(); michael@0: } else { michael@0: executeSoon(checkCount); michael@0: } michael@0: } michael@0: executeSoon(checkCount); michael@0: }, false); michael@0: aWin.close(); michael@0: } michael@0: michael@0: function forceWriteState(aCallback) { michael@0: return promiseSaveFileContents().then(function(data) { michael@0: aCallback(JSON.parse(data)); michael@0: }); michael@0: } michael@0: michael@0: function testOnWindow(aIsPrivate, aCallback) { michael@0: whenNewWindowLoaded({private: aIsPrivate}, aCallback); michael@0: } michael@0: michael@0: function waitForTabLoad(aWin, aURL, aCallback) { michael@0: let browser = aWin.gBrowser.selectedBrowser; michael@0: whenBrowserLoaded(browser, function () { michael@0: SyncHandlers.get(browser).flush(); michael@0: executeSoon(aCallback); michael@0: }); michael@0: browser.loadURI(aURL); michael@0: }