browser_526613.js (2449B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 function test() { 6 /** Test for Bug 526613 */ 7 8 // test setup 9 waitForExplicitFinish(); 10 11 function browserWindowsCount(expected) { 12 let count = 0; 13 for (let win of Services.wm.getEnumerator("navigator:browser")) { 14 if (!win.closed) { 15 ++count; 16 } 17 } 18 is( 19 count, 20 expected, 21 "number of open browser windows according to nsIWindowMediator" 22 ); 23 let state = ss.getBrowserState(); 24 info(state); 25 is( 26 JSON.parse(state).windows.length, 27 expected, 28 "number of open browser windows according to getBrowserState" 29 ); 30 } 31 32 browserWindowsCount(1); 33 34 // backup old state 35 let oldState = ss.getBrowserState(); 36 // create a new state for testing 37 let testState = { 38 windows: [ 39 { tabs: [{ entries: [{ url: "http://example.com/" }] }], selected: 1 }, 40 { tabs: [{ entries: [{ url: "about:mozilla" }] }], selected: 1 }, 41 ], 42 // make sure the first window is focused, otherwise when restoring the 43 // old state, the first window is closed and the test harness gets unloaded 44 selectedWindow: 1, 45 }; 46 47 let pass = 1; 48 function observer(aSubject, aTopic) { 49 is( 50 aTopic, 51 "sessionstore-browser-state-restored", 52 "The sessionstore-browser-state-restored notification was observed" 53 ); 54 55 if (pass++ == 1) { 56 browserWindowsCount(2); 57 58 // let the first window be focused (see above) 59 function pollMostRecentWindow() { 60 if (Services.wm.getMostRecentWindow("navigator:browser") == window) { 61 ss.setBrowserState(oldState); 62 } else { 63 info("waiting for the current window to become active"); 64 setTimeout(pollMostRecentWindow, 0); 65 window.focus(); // XXX Why is this needed? 66 } 67 } 68 pollMostRecentWindow(); 69 } else { 70 browserWindowsCount(1); 71 ok( 72 !window.closed, 73 "Restoring the old state should have left this window open" 74 ); 75 Services.obs.removeObserver( 76 observer, 77 "sessionstore-browser-state-restored" 78 ); 79 finish(); 80 } 81 } 82 Services.obs.addObserver(observer, "sessionstore-browser-state-restored"); 83 84 // set browser to test state 85 ss.setBrowserState(JSON.stringify(testState)); 86 }