Bug 822093 - Make browser_819510_perwindowpb.js wait for the sessionstore data to update before performing tests on it. r=jdm

This commit is contained in:
Andres Hernandez 2012-12-28 16:22:52 -06:00
Родитель d165c2299d
Коммит 5ef9c3160a
1 изменённых файлов: 45 добавлений и 42 удалений

Просмотреть файл

@ -8,7 +8,6 @@ function test() {
waitForExplicitFinish(); waitForExplicitFinish();
registerCleanupFunction(function() { registerCleanupFunction(function() {
Services.prefs.clearUserPref("browser.sessionstore.interval");
ss.setBrowserState(originalState); ss.setBrowserState(originalState);
}); });
@ -34,8 +33,6 @@ function runNextTest() {
let currentTest = tests.shift(); let currentTest = tests.shift();
waitForBrowserState(testState, currentTest); waitForBrowserState(testState, currentTest);
} else { } else {
Services.prefs.clearUserPref("browser.sessionstore.interval");
ss.setBrowserState(originalState);
finish(); finish();
} }
} }
@ -105,33 +102,34 @@ function test_2() {
// Test opening default-normal-private-normal windows and closing a normal window // Test opening default-normal-private-normal windows and closing a normal window
function test_3() { function test_3() {
testOnWindow(false, function(normalWindow) { testOnWindow(false, function(normalWindow) {
let tab = normalWindow.gBrowser.addTab("http://www.example.com/1"); waitForTabLoad(normalWindow, "http://www.example.com/", function() {
whenBrowserLoaded(tab.linkedBrowser, function() {
testOnWindow(true, function(aWindow) { testOnWindow(true, function(aWindow) {
aWindow.gBrowser.addTab("http://www.example.com/2"); waitForTabLoad(aWindow, "http://www.example.com/", function() {
testOnWindow(false, function(aWindow) { testOnWindow(false, function(aWindow) {
aWindow.gBrowser.addTab("http://www.example.com/3"); waitForTabLoad(aWindow, "http://www.example.com/", function() {
let curState = JSON.parse(ss.getBrowserState()); let curState = JSON.parse(ss.getBrowserState());
is (curState.windows.length, 4, "Browser has opened 4 windows"); is(curState.windows.length, 4, "Browser has opened 4 windows");
is (curState.windows[2].isPrivate, true, "Window 2 is private"); is(curState.windows[2].isPrivate, true, "Window 2 is private");
is (curState.selectedWindow, 4, "Last window opened is the one selected"); is(curState.selectedWindow, 4, "Last window opened is the one selected");
waitForWindowClose(normalWindow, function() { waitForWindowClose(normalWindow, function() {
forceWriteState(function(state) { forceWriteState(function(state) {
is(state.windows.length, 2, is(state.windows.length, 2,
"sessionstore state: 2 windows in data being writted to disk"); "sessionstore state: 2 windows in data being writted to disk");
is(state.selectedWindow, 2, is(state.selectedWindow, 2,
"Selected window is updated to match one of the saved windows"); "Selected window is updated to match one of the saved windows");
state.windows.forEach(function(win) { state.windows.forEach(function(win) {
is(!win.isPrivate, true, "Saved window is not private"); is(!win.isPrivate, true, "Saved window is not private");
});
is(state._closedWindows.length, 1,
"sessionstore state: 1 closed window in data being writted to disk");
state._closedWindows.forEach(function(win) {
is(!win.isPrivate, true, "Closed window is not private");
});
runNextTest();
});
}); });
is(state._closedWindows.length, 1,
"sessionstore state: 1 closed window in data being writted to disk");
state._closedWindows.forEach(function(win) {
is(!win.isPrivate, true, "Closed window is not private");
});
runNextTest();
}); });
}); });
}); });
@ -141,30 +139,27 @@ function test_3() {
} }
function waitForWindowClose(aWin, aCallback) { function waitForWindowClose(aWin, aCallback) {
Services.obs.addObserver(function observe(aSubject, aTopic, aData) { let winCount = JSON.parse(ss.getBrowserState()).windows.length;
if (aTopic == "domwindowclosed" && aWin == aSubject) { aWin.addEventListener("SSWindowClosing", function onWindowClosing() {
Services.obs.removeObserver(observe, aTopic); aWin.removeEventListener("SSWindowClosing", onWindowClosing, false);
checkWindowIsClosed(aWin, aCallback); function checkCount() {
let state = JSON.parse(ss.getBrowserState());
if (state.windows.length == (winCount - 1)) {
aCallback();
} else {
executeSoon(checkCount);
}
} }
}, "domwindowclosed", false); executeSoon(checkCount);
}, false);
aWin.close(); aWin.close();
} }
function checkWindowIsClosed(aWin, aCallback) {
if (aWin.closed) {
info("Window is closed");
executeSoon(aCallback);
} else {
executeSoon(function() {
checkWindowIsClosed(aWin, aCallback);
});
}
}
function forceWriteState(aCallback) { function forceWriteState(aCallback) {
Services.obs.addObserver(function observe(aSubject, aTopic, aData) { Services.obs.addObserver(function observe(aSubject, aTopic, aData) {
if (aTopic == "sessionstore-state-write") { if (aTopic == "sessionstore-state-write") {
Services.obs.removeObserver(observe, aTopic); Services.obs.removeObserver(observe, aTopic);
Services.prefs.clearUserPref("browser.sessionstore.interval");
aSubject.QueryInterface(Ci.nsISupportsString); aSubject.QueryInterface(Ci.nsISupportsString);
aCallback(JSON.parse(aSubject.data)); aCallback(JSON.parse(aSubject.data));
} }
@ -179,3 +174,11 @@ function testOnWindow(aIsPrivate, aCallback) {
executeSoon(function() { aCallback(win); }); executeSoon(function() { aCallback(win); });
}, false); }, false);
} }
function waitForTabLoad(aWin, aURL, aCallback) {
aWin.gBrowser.selectedBrowser.addEventListener("load", function onLoad() {
aWin.gBrowser.selectedBrowser.removeEventListener("load", onLoad, true);
aCallback();
}, true);
aWin.gBrowser.selectedBrowser.loadURI(aURL);
}