Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const stateBackup = JSON.parse(ss.getBrowserState()); |
michael@0 | 6 | const testState = { |
michael@0 | 7 | windows: [{ |
michael@0 | 8 | tabs: [ |
michael@0 | 9 | { entries: [{ url: "about:blank" }] }, |
michael@0 | 10 | { entries: [{ url: "about:rights" }] } |
michael@0 | 11 | ] |
michael@0 | 12 | }] |
michael@0 | 13 | }; |
michael@0 | 14 | const lameMultiWindowState = { windows: [ |
michael@0 | 15 | { |
michael@0 | 16 | tabs: [ |
michael@0 | 17 | { entries: [{ url: "http://example.org#1" }], extData: { "uniq": r() } }, |
michael@0 | 18 | { entries: [{ url: "http://example.org#2" }], extData: { "uniq": r() } }, |
michael@0 | 19 | { entries: [{ url: "http://example.org#3" }], extData: { "uniq": r() } }, |
michael@0 | 20 | { entries: [{ url: "http://example.org#4" }], extData: { "uniq": r() } } |
michael@0 | 21 | ], |
michael@0 | 22 | selected: 1 |
michael@0 | 23 | }, |
michael@0 | 24 | { |
michael@0 | 25 | tabs: [ |
michael@0 | 26 | { entries: [{ url: "http://example.com#1" }], extData: { "uniq": r() } }, |
michael@0 | 27 | { entries: [{ url: "http://example.com#2" }], extData: { "uniq": r() } }, |
michael@0 | 28 | { entries: [{ url: "http://example.com#3" }], extData: { "uniq": r() } }, |
michael@0 | 29 | { entries: [{ url: "http://example.com#4" }], extData: { "uniq": r() } }, |
michael@0 | 30 | ], |
michael@0 | 31 | selected: 3 |
michael@0 | 32 | } |
michael@0 | 33 | ] }; |
michael@0 | 34 | |
michael@0 | 35 | |
michael@0 | 36 | function getOuterWindowID(aWindow) { |
michael@0 | 37 | return aWindow.QueryInterface(Ci.nsIInterfaceRequestor). |
michael@0 | 38 | getInterface(Ci.nsIDOMWindowUtils).outerWindowID; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | function test() { |
michael@0 | 42 | /** Test for Bug 615394 - Session Restore should notify when it is beginning and ending a restore **/ |
michael@0 | 43 | waitForExplicitFinish(); |
michael@0 | 44 | // Preemptively extend the timeout to prevent [orange] |
michael@0 | 45 | requestLongerTimeout(2); |
michael@0 | 46 | runNextTest(); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | |
michael@0 | 50 | let tests = [ |
michael@0 | 51 | test_setTabState, |
michael@0 | 52 | test_duplicateTab, |
michael@0 | 53 | test_undoCloseTab, |
michael@0 | 54 | test_setWindowState, |
michael@0 | 55 | test_setBrowserState, |
michael@0 | 56 | test_undoCloseWindow |
michael@0 | 57 | ]; |
michael@0 | 58 | function runNextTest() { |
michael@0 | 59 | // set an empty state & run the next test, or finish |
michael@0 | 60 | if (tests.length) { |
michael@0 | 61 | // Enumerate windows and close everything but our primary window. We can't |
michael@0 | 62 | // use waitForFocus() because apparently it's buggy. See bug 599253. |
michael@0 | 63 | var windowsEnum = Services.wm.getEnumerator("navigator:browser"); |
michael@0 | 64 | while (windowsEnum.hasMoreElements()) { |
michael@0 | 65 | var currentWindow = windowsEnum.getNext(); |
michael@0 | 66 | if (currentWindow != window) { |
michael@0 | 67 | currentWindow.close(); |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | // If we closed a window, give it time to close |
michael@0 | 72 | executeSoon(function() { |
michael@0 | 73 | let currentTest = tests.shift(); |
michael@0 | 74 | info("prepping for " + currentTest.name); |
michael@0 | 75 | waitForBrowserState(testState, currentTest); |
michael@0 | 76 | }); |
michael@0 | 77 | } |
michael@0 | 78 | else { |
michael@0 | 79 | waitForBrowserState(stateBackup, finish); |
michael@0 | 80 | } |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /** ACTUAL TESTS **/ |
michael@0 | 84 | |
michael@0 | 85 | function test_setTabState() { |
michael@0 | 86 | let tab = gBrowser.tabs[1]; |
michael@0 | 87 | let newTabState = JSON.stringify({ entries: [{ url: "http://example.org" }], extData: { foo: "bar" } }); |
michael@0 | 88 | let busyEventCount = 0; |
michael@0 | 89 | let readyEventCount = 0; |
michael@0 | 90 | |
michael@0 | 91 | function onSSWindowStateBusy(aEvent) { |
michael@0 | 92 | busyEventCount++; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | function onSSWindowStateReady(aEvent) { |
michael@0 | 96 | readyEventCount++; |
michael@0 | 97 | is(ss.getTabValue(tab, "foo"), "bar"); |
michael@0 | 98 | ss.setTabValue(tab, "baz", "qux"); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | function onSSTabRestored(aEvent) { |
michael@0 | 102 | is(busyEventCount, 1); |
michael@0 | 103 | is(readyEventCount, 1); |
michael@0 | 104 | is(ss.getTabValue(tab, "baz"), "qux"); |
michael@0 | 105 | is(tab.linkedBrowser.currentURI.spec, "http://example.org/"); |
michael@0 | 106 | |
michael@0 | 107 | window.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 108 | window.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 109 | gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 110 | |
michael@0 | 111 | runNextTest(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | window.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 115 | window.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 116 | gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 117 | ss.setTabState(tab, newTabState); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | |
michael@0 | 121 | function test_duplicateTab() { |
michael@0 | 122 | let tab = gBrowser.tabs[1]; |
michael@0 | 123 | let busyEventCount = 0; |
michael@0 | 124 | let readyEventCount = 0; |
michael@0 | 125 | let newTab; |
michael@0 | 126 | |
michael@0 | 127 | // We'll look to make sure this value is on the duplicated tab |
michael@0 | 128 | ss.setTabValue(tab, "foo", "bar"); |
michael@0 | 129 | |
michael@0 | 130 | function onSSWindowStateBusy(aEvent) { |
michael@0 | 131 | busyEventCount++; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | function onSSWindowStateReady(aEvent) { |
michael@0 | 135 | newTab = gBrowser.tabs[2]; |
michael@0 | 136 | readyEventCount++; |
michael@0 | 137 | is(ss.getTabValue(newTab, "foo"), "bar"); |
michael@0 | 138 | ss.setTabValue(newTab, "baz", "qux"); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | function onSSTabRestored(aEvent) { |
michael@0 | 142 | is(busyEventCount, 1); |
michael@0 | 143 | is(readyEventCount, 1); |
michael@0 | 144 | is(ss.getTabValue(newTab, "baz"), "qux"); |
michael@0 | 145 | is(newTab.linkedBrowser.currentURI.spec, "about:rights"); |
michael@0 | 146 | |
michael@0 | 147 | window.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 148 | window.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 149 | gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 150 | |
michael@0 | 151 | runNextTest(); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | window.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 155 | window.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 156 | gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 157 | |
michael@0 | 158 | newTab = ss.duplicateTab(window, tab); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | |
michael@0 | 162 | function test_undoCloseTab() { |
michael@0 | 163 | let tab = gBrowser.tabs[1], |
michael@0 | 164 | busyEventCount = 0, |
michael@0 | 165 | readyEventCount = 0, |
michael@0 | 166 | reopenedTab; |
michael@0 | 167 | |
michael@0 | 168 | ss.setTabValue(tab, "foo", "bar"); |
michael@0 | 169 | |
michael@0 | 170 | function onSSWindowStateBusy(aEvent) { |
michael@0 | 171 | busyEventCount++; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | function onSSWindowStateReady(aEvent) { |
michael@0 | 175 | reopenedTab = gBrowser.tabs[1]; |
michael@0 | 176 | readyEventCount++; |
michael@0 | 177 | is(ss.getTabValue(reopenedTab, "foo"), "bar"); |
michael@0 | 178 | ss.setTabValue(reopenedTab, "baz", "qux"); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | function onSSTabRestored(aEvent) { |
michael@0 | 182 | is(busyEventCount, 1); |
michael@0 | 183 | is(readyEventCount, 1); |
michael@0 | 184 | is(ss.getTabValue(reopenedTab, "baz"), "qux"); |
michael@0 | 185 | is(reopenedTab.linkedBrowser.currentURI.spec, "about:rights"); |
michael@0 | 186 | |
michael@0 | 187 | window.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 188 | window.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 189 | gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 190 | |
michael@0 | 191 | runNextTest(); |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | window.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 195 | window.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 196 | gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 197 | |
michael@0 | 198 | gBrowser.removeTab(tab); |
michael@0 | 199 | reopenedTab = ss.undoCloseTab(window, 0); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | |
michael@0 | 203 | function test_setWindowState() { |
michael@0 | 204 | let testState = { |
michael@0 | 205 | windows: [{ |
michael@0 | 206 | tabs: [ |
michael@0 | 207 | { entries: [{ url: "about:mozilla" }], extData: { "foo": "bar" } }, |
michael@0 | 208 | { entries: [{ url: "http://example.org" }], extData: { "baz": "qux" } } |
michael@0 | 209 | ] |
michael@0 | 210 | }] |
michael@0 | 211 | }; |
michael@0 | 212 | |
michael@0 | 213 | let busyEventCount = 0, |
michael@0 | 214 | readyEventCount = 0, |
michael@0 | 215 | tabRestoredCount = 0; |
michael@0 | 216 | |
michael@0 | 217 | function onSSWindowStateBusy(aEvent) { |
michael@0 | 218 | busyEventCount++; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | function onSSWindowStateReady(aEvent) { |
michael@0 | 222 | readyEventCount++; |
michael@0 | 223 | is(ss.getTabValue(gBrowser.tabs[0], "foo"), "bar"); |
michael@0 | 224 | is(ss.getTabValue(gBrowser.tabs[1], "baz"), "qux"); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | function onSSTabRestored(aEvent) { |
michael@0 | 228 | if (++tabRestoredCount < 2) |
michael@0 | 229 | return; |
michael@0 | 230 | |
michael@0 | 231 | is(busyEventCount, 1); |
michael@0 | 232 | is(readyEventCount, 1); |
michael@0 | 233 | is(gBrowser.tabs[0].linkedBrowser.currentURI.spec, "about:mozilla"); |
michael@0 | 234 | is(gBrowser.tabs[1].linkedBrowser.currentURI.spec, "http://example.org/"); |
michael@0 | 235 | |
michael@0 | 236 | window.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 237 | window.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 238 | gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 239 | |
michael@0 | 240 | runNextTest(); |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | window.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 244 | window.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 245 | gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 246 | |
michael@0 | 247 | ss.setWindowState(window, JSON.stringify(testState), true); |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | |
michael@0 | 251 | function test_setBrowserState() { |
michael@0 | 252 | // We'll track events per window so we are sure that they are each happening once |
michael@0 | 253 | // pre window. |
michael@0 | 254 | let windowEvents = {}; |
michael@0 | 255 | windowEvents[getOuterWindowID(window)] = { busyEventCount: 0, readyEventCount: 0 }; |
michael@0 | 256 | |
michael@0 | 257 | // waitForBrowserState does it's own observing for windows, but doesn't attach |
michael@0 | 258 | // the listeners we want here, so do it ourselves. |
michael@0 | 259 | let newWindow; |
michael@0 | 260 | function windowObserver(aSubject, aTopic, aData) { |
michael@0 | 261 | if (aTopic == "domwindowopened") { |
michael@0 | 262 | newWindow = aSubject.QueryInterface(Ci.nsIDOMWindow); |
michael@0 | 263 | newWindow.addEventListener("load", function() { |
michael@0 | 264 | newWindow.removeEventListener("load", arguments.callee, false); |
michael@0 | 265 | |
michael@0 | 266 | Services.ww.unregisterNotification(windowObserver); |
michael@0 | 267 | |
michael@0 | 268 | windowEvents[getOuterWindowID(newWindow)] = { busyEventCount: 0, readyEventCount: 0 }; |
michael@0 | 269 | |
michael@0 | 270 | newWindow.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 271 | newWindow.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 272 | }, false); |
michael@0 | 273 | } |
michael@0 | 274 | } |
michael@0 | 275 | |
michael@0 | 276 | function onSSWindowStateBusy(aEvent) { |
michael@0 | 277 | windowEvents[getOuterWindowID(aEvent.originalTarget)].busyEventCount++; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | function onSSWindowStateReady(aEvent) { |
michael@0 | 281 | windowEvents[getOuterWindowID(aEvent.originalTarget)].readyEventCount++; |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | window.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 285 | window.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 286 | Services.ww.registerNotification(windowObserver); |
michael@0 | 287 | |
michael@0 | 288 | waitForBrowserState(lameMultiWindowState, function() { |
michael@0 | 289 | let checkedWindows = 0; |
michael@0 | 290 | for (let id of Object.keys(windowEvents)) { |
michael@0 | 291 | let winEvents = windowEvents[id]; |
michael@0 | 292 | is(winEvents.busyEventCount, 1, |
michael@0 | 293 | "[test_setBrowserState] window" + id + " busy event count correct"); |
michael@0 | 294 | is(winEvents.readyEventCount, 1, |
michael@0 | 295 | "[test_setBrowserState] window" + id + " ready event count correct"); |
michael@0 | 296 | checkedWindows++; |
michael@0 | 297 | } |
michael@0 | 298 | is(checkedWindows, 2, |
michael@0 | 299 | "[test_setBrowserState] checked 2 windows"); |
michael@0 | 300 | window.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 301 | window.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 302 | newWindow.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 303 | newWindow.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 304 | runNextTest(); |
michael@0 | 305 | }); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | |
michael@0 | 309 | function test_undoCloseWindow() { |
michael@0 | 310 | let newWindow, reopenedWindow; |
michael@0 | 311 | |
michael@0 | 312 | function firstWindowObserver(aSubject, aTopic, aData) { |
michael@0 | 313 | if (aTopic == "domwindowopened") { |
michael@0 | 314 | newWindow = aSubject.QueryInterface(Ci.nsIDOMWindow); |
michael@0 | 315 | Services.ww.unregisterNotification(firstWindowObserver); |
michael@0 | 316 | } |
michael@0 | 317 | } |
michael@0 | 318 | Services.ww.registerNotification(firstWindowObserver); |
michael@0 | 319 | |
michael@0 | 320 | waitForBrowserState(lameMultiWindowState, function() { |
michael@0 | 321 | // Close the window which isn't window |
michael@0 | 322 | newWindow.close(); |
michael@0 | 323 | // Now give it time to close |
michael@0 | 324 | executeSoon(function() { |
michael@0 | 325 | reopenedWindow = ss.undoCloseWindow(0); |
michael@0 | 326 | reopenedWindow.addEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 327 | reopenedWindow.addEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 328 | |
michael@0 | 329 | reopenedWindow.addEventListener("load", function() { |
michael@0 | 330 | reopenedWindow.removeEventListener("load", arguments.callee, false); |
michael@0 | 331 | |
michael@0 | 332 | reopenedWindow.gBrowser.tabContainer.addEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 333 | }, false); |
michael@0 | 334 | }); |
michael@0 | 335 | }); |
michael@0 | 336 | |
michael@0 | 337 | let busyEventCount = 0, |
michael@0 | 338 | readyEventCount = 0, |
michael@0 | 339 | tabRestoredCount = 0; |
michael@0 | 340 | // These will listen to the reopened closed window... |
michael@0 | 341 | function onSSWindowStateBusy(aEvent) { |
michael@0 | 342 | busyEventCount++; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | function onSSWindowStateReady(aEvent) { |
michael@0 | 346 | readyEventCount++; |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | function onSSTabRestored(aEvent) { |
michael@0 | 350 | if (++tabRestoredCount < 4) |
michael@0 | 351 | return; |
michael@0 | 352 | |
michael@0 | 353 | is(busyEventCount, 1); |
michael@0 | 354 | is(readyEventCount, 1); |
michael@0 | 355 | |
michael@0 | 356 | reopenedWindow.removeEventListener("SSWindowStateBusy", onSSWindowStateBusy, false); |
michael@0 | 357 | reopenedWindow.removeEventListener("SSWindowStateReady", onSSWindowStateReady, false); |
michael@0 | 358 | reopenedWindow.gBrowser.tabContainer.removeEventListener("SSTabRestored", onSSTabRestored, false); |
michael@0 | 359 | |
michael@0 | 360 | reopenedWindow.close(); |
michael@0 | 361 | |
michael@0 | 362 | // Give it time to close |
michael@0 | 363 | executeSoon(runNextTest); |
michael@0 | 364 | } |
michael@0 | 365 | } |