Bug 494749 - mochitest-browser-chrome: browser_490040.js times out intermittently, refactoring of the test, r=dietrich

This commit is contained in:
Marco Bonardo 2009-07-29 23:50:42 +02:00
parent 0a849852a4
commit 2bfec9f475

View File

@ -38,43 +38,36 @@
function test() {
/** Test for Bug 490040 **/
let ss = Cc["@mozilla.org/browser/sessionstore;1"].getService(Ci.nsISessionStore);
let os = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
let ss = Cc["@mozilla.org/browser/sessionstore;1"].
getService(Ci.nsISessionStore);
let os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher);
waitForExplicitFinish();
function testWithState(aState, aCallback) {
// ensure we can store the window if need be
function testWithState(aState) {
// Ensure we can store the window if needed.
let curClosedWindowCount = ss.getClosedWindowCount();
gPrefService.setIntPref("browser.sessionstore.max_windows_undo", curClosedWindowCount + 1);
gPrefService.setIntPref("browser.sessionstore.max_windows_undo",
curClosedWindowCount + 1);
let theWin = openDialog(location, "_blank", "chrome,all,dialog=no");
theWin.addEventListener("load", function(aEvent) {
theWin.removeEventListener("load", arguments.callee, true);
ss.setWindowState(theWin, JSON.stringify(aState.windowState), true);
let observer = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
Ci.nsISupportsWeakReference]),
let windowObserver = {
observe: function(aSubject, aTopic, aData) {
let _this = this;
// use executeSoon to ensure this happens after SS observer
executeSoon(function() {
is(ss.getClosedWindowCount(), curClosedWindowCount + (aState.shouldBeAdded ? 1 : 0),
"That window should " + (aState.shouldBeAdded ? "" : "not ") + "be restorable");
os.removeObserver(_this, "domwindowclosed");
executeSoon(aCallback);
});
}
};
os.addObserver(observer, "domwindowclosed", true);
let theWin = aSubject.QueryInterface(Ci.nsIDOMWindow);
// Close the window as soon as the first tab loads, or immediately if
// there are no tabs.
switch(aTopic) {
case "domwindowopened":
theWin.addEventListener("load", function () {
theWin.removeEventListener("load", arguments.callee, false);
executeSoon(function() {
// Close the window as soon as the first tab loads, or
// immediately if there are no tabs.
if (aState.windowState.windows[0].tabs[0].entries.length) {
theWin.gBrowser.addEventListener("load", function() {
theWin.gBrowser.removeEventListener("load", arguments.callee, true);
theWin.gBrowser.removeEventListener("load",
arguments.callee, true);
theWin.close();
}, true);
} else {
@ -82,7 +75,32 @@ function test() {
theWin.close();
});
}
}, true);
ss.setWindowState(theWin, JSON.stringify(aState.windowState),
true);
});
}, false);
break;
case "domwindowclosed":
ww.unregisterNotification(this);
// Use executeSoon to ensure this happens after SS observer.
executeSoon(function() {
is(ss.getClosedWindowCount(),
curClosedWindowCount + (aState.shouldBeAdded ? 1 : 0),
"That window should " + (aState.shouldBeAdded ? "" : "not ") +
"be restorable");
executeSoon(runNextTest);
});
break;
}
}
}
ww.registerNotification(windowObserver);
ww.openWindow(null,
location,
"_blank",
"chrome,all,dialog=no",
null);
}
// Only windows with open tabs are restorable. Windows where a lone tab is
@ -128,15 +146,16 @@ function test() {
}
];
testWithState(states[0], function() {
testWithState(states[1], function() {
testWithState(states[2], function() {
testWithState(states[3], function() {
function runNextTest() {
if (states.length) {
let state = states.shift();
testWithState(state);
}
else {
gPrefService.clearUserPref("browser.sessionstore.max_windows_undo");
finish();
});
});
});
});
}
}
runNextTest();
}