browser/components/sessionstore/test/browser_819510_perwindowpb.js

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

mercurial