browser/components/sessionstore/test/browser_819510_perwindowpb.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 const originalState = ss.getBrowserState();
michael@0 5
michael@0 6 /** Private Browsing Test for Bug 819510 **/
michael@0 7 function test() {
michael@0 8 waitForExplicitFinish();
michael@0 9 runNextTest();
michael@0 10 }
michael@0 11
michael@0 12 let tests = [test_1, test_2, test_3 ];
michael@0 13
michael@0 14 const testState = {
michael@0 15 windows: [{
michael@0 16 tabs: [
michael@0 17 { entries: [{ url: "about:blank" }] },
michael@0 18 ]
michael@0 19 }]
michael@0 20 };
michael@0 21
michael@0 22 function runNextTest() {
michael@0 23 // Set an empty state
michael@0 24 closeAllButPrimaryWindow();
michael@0 25
michael@0 26 // Run the next test, or finish
michael@0 27 if (tests.length) {
michael@0 28 let currentTest = tests.shift();
michael@0 29 waitForBrowserState(testState, currentTest);
michael@0 30 } else {
michael@0 31 Services.obs.addObserver(
michael@0 32 function observe(aSubject, aTopic, aData) {
michael@0 33 Services.obs.removeObserver(observe, aTopic);
michael@0 34 finish();
michael@0 35 },
michael@0 36 "sessionstore-browser-state-restored", false);
michael@0 37 ss.setBrowserState(originalState);
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 // Test opening default mochitest-normal-private-normal-private windows
michael@0 42 // (saving the state with last window being private)
michael@0 43 function test_1() {
michael@0 44 testOnWindow(false, function(aWindow) {
michael@0 45 aWindow.gBrowser.addTab("http://www.example.com/1");
michael@0 46 testOnWindow(true, function(aWindow) {
michael@0 47 aWindow.gBrowser.addTab("http://www.example.com/2");
michael@0 48 testOnWindow(false, function(aWindow) {
michael@0 49 aWindow.gBrowser.addTab("http://www.example.com/3");
michael@0 50 testOnWindow(true, function(aWindow) {
michael@0 51 aWindow.gBrowser.addTab("http://www.example.com/4");
michael@0 52
michael@0 53 let curState = JSON.parse(ss.getBrowserState());
michael@0 54 is (curState.windows.length, 5, "Browser has opened 5 windows");
michael@0 55 is (curState.windows[2].isPrivate, true, "Window is private");
michael@0 56 is (curState.windows[4].isPrivate, true, "Last window is private");
michael@0 57 is (curState.selectedWindow, 5, "Last window opened is the one selected");
michael@0 58
michael@0 59 forceWriteState(function(state) {
michael@0 60 is(state.windows.length, 3,
michael@0 61 "sessionstore state: 3 windows in data being written to disk");
michael@0 62 is (state.selectedWindow, 3,
michael@0 63 "Selected window is updated to match one of the saved windows");
michael@0 64 state.windows.forEach(function(win) {
michael@0 65 is(!win.isPrivate, true, "Saved window is not private");
michael@0 66 });
michael@0 67 is(state._closedWindows.length, 0,
michael@0 68 "sessionstore state: no closed windows in data being written to disk");
michael@0 69 runNextTest();
michael@0 70 });
michael@0 71 });
michael@0 72 });
michael@0 73 });
michael@0 74 });
michael@0 75 }
michael@0 76
michael@0 77 // Test opening default mochitest window + 2 private windows
michael@0 78 function test_2() {
michael@0 79 testOnWindow(true, function(aWindow) {
michael@0 80 aWindow.gBrowser.addTab("http://www.example.com/1");
michael@0 81 testOnWindow(true, function(aWindow) {
michael@0 82 aWindow.gBrowser.addTab("http://www.example.com/2");
michael@0 83
michael@0 84 let curState = JSON.parse(ss.getBrowserState());
michael@0 85 is (curState.windows.length, 3, "Browser has opened 3 windows");
michael@0 86 is (curState.windows[1].isPrivate, true, "Window 1 is private");
michael@0 87 is (curState.windows[2].isPrivate, true, "Window 2 is private");
michael@0 88 is (curState.selectedWindow, 3, "Last window opened is the one selected");
michael@0 89
michael@0 90 forceWriteState(function(state) {
michael@0 91 is(state.windows.length, 1,
michael@0 92 "sessionstore state: 1 windows in data being written to disk");
michael@0 93 is (state.selectedWindow, 1,
michael@0 94 "Selected window is updated to match one of the saved windows");
michael@0 95 is(state._closedWindows.length, 0,
michael@0 96 "sessionstore state: no closed windows in data being written to disk");
michael@0 97 runNextTest();
michael@0 98 });
michael@0 99 });
michael@0 100 });
michael@0 101 }
michael@0 102
michael@0 103 // Test opening default-normal-private-normal windows and closing a normal window
michael@0 104 function test_3() {
michael@0 105 testOnWindow(false, function(normalWindow) {
michael@0 106 waitForTabLoad(normalWindow, "http://www.example.com/", function() {
michael@0 107 testOnWindow(true, function(aWindow) {
michael@0 108 waitForTabLoad(aWindow, "http://www.example.com/", function() {
michael@0 109 testOnWindow(false, function(aWindow) {
michael@0 110 waitForTabLoad(aWindow, "http://www.example.com/", function() {
michael@0 111
michael@0 112 let curState = JSON.parse(ss.getBrowserState());
michael@0 113 is(curState.windows.length, 4, "Browser has opened 4 windows");
michael@0 114 is(curState.windows[2].isPrivate, true, "Window 2 is private");
michael@0 115 is(curState.selectedWindow, 4, "Last window opened is the one selected");
michael@0 116
michael@0 117 waitForWindowClose(normalWindow, function() {
michael@0 118 // Pin and unpin a tab before checking the written state so that
michael@0 119 // the list of restoring windows gets cleared. Otherwise the
michael@0 120 // window we just closed would be marked as not closed.
michael@0 121 let tab = aWindow.gBrowser.tabs[0];
michael@0 122 aWindow.gBrowser.pinTab(tab);
michael@0 123 aWindow.gBrowser.unpinTab(tab);
michael@0 124
michael@0 125 forceWriteState(function(state) {
michael@0 126 is(state.windows.length, 2,
michael@0 127 "sessionstore state: 2 windows in data being written to disk");
michael@0 128 is(state.selectedWindow, 2,
michael@0 129 "Selected window is updated to match one of the saved windows");
michael@0 130 state.windows.forEach(function(win) {
michael@0 131 is(!win.isPrivate, true, "Saved window is not private");
michael@0 132 });
michael@0 133 is(state._closedWindows.length, 1,
michael@0 134 "sessionstore state: 1 closed window in data being written to disk");
michael@0 135 state._closedWindows.forEach(function(win) {
michael@0 136 is(!win.isPrivate, true, "Closed window is not private");
michael@0 137 });
michael@0 138 runNextTest();
michael@0 139 });
michael@0 140 });
michael@0 141 });
michael@0 142 });
michael@0 143 });
michael@0 144 });
michael@0 145 });
michael@0 146 });
michael@0 147 }
michael@0 148
michael@0 149 function waitForWindowClose(aWin, aCallback) {
michael@0 150 let winCount = JSON.parse(ss.getBrowserState()).windows.length;
michael@0 151 aWin.addEventListener("SSWindowClosing", function onWindowClosing() {
michael@0 152 aWin.removeEventListener("SSWindowClosing", onWindowClosing, false);
michael@0 153 function checkCount() {
michael@0 154 let state = JSON.parse(ss.getBrowserState());
michael@0 155 if (state.windows.length == (winCount - 1)) {
michael@0 156 aCallback();
michael@0 157 } else {
michael@0 158 executeSoon(checkCount);
michael@0 159 }
michael@0 160 }
michael@0 161 executeSoon(checkCount);
michael@0 162 }, false);
michael@0 163 aWin.close();
michael@0 164 }
michael@0 165
michael@0 166 function forceWriteState(aCallback) {
michael@0 167 return promiseSaveFileContents().then(function(data) {
michael@0 168 aCallback(JSON.parse(data));
michael@0 169 });
michael@0 170 }
michael@0 171
michael@0 172 function testOnWindow(aIsPrivate, aCallback) {
michael@0 173 whenNewWindowLoaded({private: aIsPrivate}, aCallback);
michael@0 174 }
michael@0 175
michael@0 176 function waitForTabLoad(aWin, aURL, aCallback) {
michael@0 177 let browser = aWin.gBrowser.selectedBrowser;
michael@0 178 whenBrowserLoaded(browser, function () {
michael@0 179 SyncHandlers.get(browser).flush();
michael@0 180 executeSoon(aCallback);
michael@0 181 });
michael@0 182 browser.loadURI(aURL);
michael@0 183 }

mercurial