1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/sessionstore/test/browser_526613.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,72 @@ 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 +function test() { 1.9 + /** Test for Bug 526613 **/ 1.10 + 1.11 + // test setup 1.12 + waitForExplicitFinish(); 1.13 + 1.14 + function browserWindowsCount(expected) { 1.15 + let count = 0; 1.16 + let e = Services.wm.getEnumerator("navigator:browser"); 1.17 + while (e.hasMoreElements()) { 1.18 + if (!e.getNext().closed) 1.19 + ++count; 1.20 + } 1.21 + is(count, expected, 1.22 + "number of open browser windows according to nsIWindowMediator"); 1.23 + let state = ss.getBrowserState(); 1.24 + info(state); 1.25 + is(JSON.parse(state).windows.length, expected, 1.26 + "number of open browser windows according to getBrowserState"); 1.27 + } 1.28 + 1.29 + browserWindowsCount(1); 1.30 + 1.31 + // backup old state 1.32 + let oldState = ss.getBrowserState(); 1.33 + // create a new state for testing 1.34 + let testState = { 1.35 + windows: [ 1.36 + { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 }, 1.37 + { tabs: [{ entries: [{ url: "about:mozilla" }] }], selected: 1 }, 1.38 + ], 1.39 + // make sure the first window is focused, otherwise when restoring the 1.40 + // old state, the first window is closed and the test harness gets unloaded 1.41 + selectedWindow: 1 1.42 + }; 1.43 + 1.44 + let pass = 1; 1.45 + function observer(aSubject, aTopic, aData) { 1.46 + is(aTopic, "sessionstore-browser-state-restored", 1.47 + "The sessionstore-browser-state-restored notification was observed"); 1.48 + 1.49 + if (pass++ == 1) { 1.50 + browserWindowsCount(2); 1.51 + 1.52 + // let the first window be focused (see above) 1.53 + function pollMostRecentWindow() { 1.54 + if (Services.wm.getMostRecentWindow("navigator:browser") == window) { 1.55 + ss.setBrowserState(oldState); 1.56 + } else { 1.57 + info("waiting for the current window to become active"); 1.58 + setTimeout(pollMostRecentWindow, 0); 1.59 + window.focus(); //XXX Why is this needed? 1.60 + } 1.61 + } 1.62 + pollMostRecentWindow(); 1.63 + } 1.64 + else { 1.65 + browserWindowsCount(1); 1.66 + ok(!window.closed, "Restoring the old state should have left this window open"); 1.67 + Services.obs.removeObserver(observer, "sessionstore-browser-state-restored"); 1.68 + finish(); 1.69 + } 1.70 + } 1.71 + Services.obs.addObserver(observer, "sessionstore-browser-state-restored", false); 1.72 + 1.73 + // set browser to test state 1.74 + ss.setBrowserState(JSON.stringify(testState)); 1.75 +}