Bug 528469 - browser-test.js should ensure windows state between tests (further modified by gavin, r=me), r=gavin

This commit is contained in:
Marco Bonardo 2010-01-21 13:05:27 +01:00
parent 3f3130f146
commit 364bf6fdec
3 changed files with 79 additions and 34 deletions

View File

@ -33,14 +33,18 @@ function Tester(aTests, aDumper, aCallback) {
this.callback = aCallback;
this._cs = Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService);
this._wm = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
this._fm = Cc["@mozilla.org/focus-manager;1"].
getService(Ci.nsIFocusManager);
var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader);
scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils);
this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader);
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils);
// Avoid polluting this scope with packed.js contents.
var simpleTestScope = {};
scriptLoader.loadSubScript("chrome://mochikit/content/MochiKit/packed.js", simpleTestScope);
scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", simpleTestScope);
this._scriptLoader.loadSubScript("chrome://mochikit/content/MochiKit/packed.js", simpleTestScope);
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", simpleTestScope);
this.SimpleTest = simpleTestScope.SimpleTest;
}
Tester.prototype = {
@ -55,20 +59,52 @@ Tester.prototype = {
get done() {
return this.currentTestIndex == this.tests.length - 1;
},
step: function Tester_step() {
this.currentTestIndex++;
},
start: function Tester_start() {
this.dumper.dump("*** Start BrowserChrome Test Results ***\n");
this._cs.registerListener(this);
if (this.tests.length)
this.execTest();
this.nextTest();
else
this.finish();
},
waitForWindowsState: function Tester_waitForWindowsState(aCallback) {
this.dumper.dump("TEST-INFO | checking window state\n");
let windowsEnum = this._wm.getEnumerator("navigator:browser");
while (windowsEnum.hasMoreElements()) {
let win = windowsEnum.getNext();
if (win != window && !win.closed) {
let msg = "Found an unexpected browser window";
if (this.currentTest) {
msg += " at the end of test run";
this.currentTest.addResult(new testResult(false, msg, "", false));
}
else
this.dumper.dump("TEST-UNEXPECTED-FAIL | (browser-test.js) | " + msg + "\n");
win.close();
}
}
// Make sure the window is raised before each test.
if (this._fm.activeWindow != window) {
this.dumper.dump("TEST-INFO | (browser-test.js) | Waiting for window activation...\n");
let self = this;
window.addEventListener("activate", function () {
window.removeEventListener("activate", arguments.callee, false);
setTimeout(function () {
aCallback.apply(self);
}, 0);
}, false);
window.focus();
return;
}
aCallback.apply(this);
},
finish: function Tester_finish(aSkipSummary) {
this._cs.unregisterListener(this);
@ -100,33 +136,30 @@ Tester.prototype = {
observe: function Tester_observe(aConsoleMessage) {
var msg = "Console message: " + aConsoleMessage.message;
this.currentTest.addResult(new testMessage(msg));
if (this.currentTest)
this.currentTest.addResult(new testMessage(msg));
else
this.dumper.dump("TEST-INFO | (browser-test.js) | " + msg);
},
execTest: function Tester_execTest() {
nextTest: function Tester_nextTest() {
// Check the window state for the current test before moving to the next one.
// This also causes us to check before starting any tests, since nextTest()
// is invoked to start the tests.
this.waitForWindowsState(this.realNextTest);
},
realNextTest: function Test_realNextTest() {
if (this.done) {
this.finish();
return;
}
// Make sure the window is raised before each test.
let fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
if (fm.activeWindow != window) {
this.dumper.dump("Waiting for window activation...\n");
let self = this;
window.addEventListener("activate", function () {
window.removeEventListener("activate", arguments.callee, false);
setTimeout(function () {
self.execTest();
}, 0);
}, false);
window.focus();
return;
}
// Move to the next test (or first test).
this.step();
this.currentTestIndex++;
this.execTest();
},
execTest: function Tester_execTest() {
this.dumper.dump("Running " + this.currentTest.path + "...\n");
// Load the tests into a testscope
@ -136,10 +169,9 @@ Tester.prototype = {
this.currentTest.scope.EventUtils = this.EventUtils;
this.currentTest.scope.SimpleTest = this.SimpleTest;
var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader);
try {
scriptLoader.loadSubScript(this.currentTest.path, this.currentTest.scope);
this._scriptLoader.loadSubScript(this.currentTest.path,
this.currentTest.scope);
// Run the test
this.currentTest.scope.test();
@ -151,14 +183,14 @@ Tester.prototype = {
// If the test ran synchronously, move to the next test, otherwise the test
// will trigger the next test when it is done.
if (this.currentTest.scope.__done) {
this.execTest();
this.nextTest();
}
else {
var self = this;
this.currentTest.scope.__waitTimer = setTimeout(function() {
self.currentTest.addResult(new testResult(false, "Timed out", "", false));
self.currentTest.scope.__waitTimer = null;
self.execTest();
self.nextTest();
}, TIMEOUT_SECONDS * 1000);
}
},
@ -259,7 +291,7 @@ function testScope(aTester, aTest) {
if (self.__done && self.__waitTimer) {
clearTimeout(self.__waitTimer);
self.__waitTimer = null;
self.__tester.execTest();
self.__tester.nextTest();
}
});
}

View File

@ -48,6 +48,7 @@ _BROWSER_TEST_FILES = browser_pass.js \
browser_async.js \
browser_privileges.js \
# Disabled, these are only good for testing the harness' failure reporting
# browser_zz_fail_openwindow.js \
# browser_fail.js \
# browser_fail_async_throw.js \
# browser_fail_fp.js \

View File

@ -0,0 +1,12 @@
function test() {
waitForExplicitFinish();
function done() {
ok(true, "timeout ran");
finish();
}
ok(OpenBrowserWindow(), "opened browser window");
// and didn't close it!
setTimeout(done, 10000);
}