michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function test() { michael@0: /** Test for Bug 526613 **/ michael@0: michael@0: // test setup michael@0: waitForExplicitFinish(); michael@0: michael@0: function browserWindowsCount(expected) { michael@0: let count = 0; michael@0: let e = Services.wm.getEnumerator("navigator:browser"); michael@0: while (e.hasMoreElements()) { michael@0: if (!e.getNext().closed) michael@0: ++count; michael@0: } michael@0: is(count, expected, michael@0: "number of open browser windows according to nsIWindowMediator"); michael@0: let state = ss.getBrowserState(); michael@0: info(state); michael@0: is(JSON.parse(state).windows.length, expected, michael@0: "number of open browser windows according to getBrowserState"); michael@0: } michael@0: michael@0: browserWindowsCount(1); michael@0: michael@0: // backup old state michael@0: let oldState = ss.getBrowserState(); michael@0: // create a new state for testing michael@0: let testState = { michael@0: windows: [ michael@0: { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 }, michael@0: { tabs: [{ entries: [{ url: "about:mozilla" }] }], selected: 1 }, michael@0: ], michael@0: // make sure the first window is focused, otherwise when restoring the michael@0: // old state, the first window is closed and the test harness gets unloaded michael@0: selectedWindow: 1 michael@0: }; michael@0: michael@0: let pass = 1; michael@0: function observer(aSubject, aTopic, aData) { michael@0: is(aTopic, "sessionstore-browser-state-restored", michael@0: "The sessionstore-browser-state-restored notification was observed"); michael@0: michael@0: if (pass++ == 1) { michael@0: browserWindowsCount(2); michael@0: michael@0: // let the first window be focused (see above) michael@0: function pollMostRecentWindow() { michael@0: if (Services.wm.getMostRecentWindow("navigator:browser") == window) { michael@0: ss.setBrowserState(oldState); michael@0: } else { michael@0: info("waiting for the current window to become active"); michael@0: setTimeout(pollMostRecentWindow, 0); michael@0: window.focus(); //XXX Why is this needed? michael@0: } michael@0: } michael@0: pollMostRecentWindow(); michael@0: } michael@0: else { michael@0: browserWindowsCount(1); michael@0: ok(!window.closed, "Restoring the old state should have left this window open"); michael@0: Services.obs.removeObserver(observer, "sessionstore-browser-state-restored"); michael@0: finish(); michael@0: } michael@0: } michael@0: Services.obs.addObserver(observer, "sessionstore-browser-state-restored", false); michael@0: michael@0: // set browser to test state michael@0: ss.setBrowserState(JSON.stringify(testState)); michael@0: }