2009-02-25 14:27:32 -08:00
|
|
|
// Test timeout (seconds)
|
|
|
|
const TIMEOUT_SECONDS = 30;
|
|
|
|
|
2008-02-28 13:36:30 -08:00
|
|
|
if (Cc === undefined) {
|
|
|
|
var Cc = Components.classes;
|
|
|
|
var Ci = Components.interfaces;
|
|
|
|
}
|
2007-07-09 09:24:15 -07:00
|
|
|
window.addEventListener("load", testOnLoad, false);
|
|
|
|
|
|
|
|
function testOnLoad() {
|
2008-09-15 05:38:09 -07:00
|
|
|
window.removeEventListener("load", testOnLoad, false);
|
|
|
|
|
2007-07-09 09:24:15 -07:00
|
|
|
// Make sure to launch the test harness for the first opened window only
|
|
|
|
var prefs = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch);
|
|
|
|
if (prefs.prefHasUserValue("testing.browserTestHarness.running"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
prefs.setBoolPref("testing.browserTestHarness.running", true);
|
|
|
|
|
|
|
|
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
|
|
|
|
getService(Ci.nsIWindowWatcher);
|
|
|
|
var sstring = Cc["@mozilla.org/supports-string;1"].
|
|
|
|
createInstance(Ci.nsISupportsString);
|
|
|
|
sstring.data = location.search;
|
|
|
|
ww.openWindow(window, "chrome://mochikit/content/browser-harness.xul", "browserTest",
|
|
|
|
"chrome,centerscreen,dialog,resizable,titlebar,toolbar=no,width=800,height=600", sstring);
|
|
|
|
}
|
|
|
|
|
2009-06-11 10:47:37 -07:00
|
|
|
function Tester(aTests, aDumper, aCallback) {
|
|
|
|
this.dumper = aDumper;
|
2007-07-09 09:24:15 -07:00
|
|
|
this.tests = aTests;
|
|
|
|
this.callback = aCallback;
|
2009-07-03 00:22:31 -07:00
|
|
|
this._cs = Cc["@mozilla.org/consoleservice;1"].
|
|
|
|
getService(Ci.nsIConsoleService);
|
2010-01-21 04:05:27 -08:00
|
|
|
this._wm = Cc["@mozilla.org/appshell/window-mediator;1"].
|
|
|
|
getService(Ci.nsIWindowMediator);
|
|
|
|
this._fm = Cc["@mozilla.org/focus-manager;1"].
|
|
|
|
getService(Ci.nsIFocusManager);
|
2009-09-22 02:16:03 -07:00
|
|
|
|
2010-01-21 04:05:27 -08:00
|
|
|
this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
|
|
|
|
getService(Ci.mozIJSSubScriptLoader);
|
|
|
|
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils);
|
2009-09-22 02:16:03 -07:00
|
|
|
// Avoid polluting this scope with packed.js contents.
|
|
|
|
var simpleTestScope = {};
|
2010-01-21 04:05:27 -08:00
|
|
|
this._scriptLoader.loadSubScript("chrome://mochikit/content/MochiKit/packed.js", simpleTestScope);
|
|
|
|
this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", simpleTestScope);
|
2009-09-22 02:16:03 -07:00
|
|
|
this.SimpleTest = simpleTestScope.SimpleTest;
|
2007-07-09 09:24:15 -07:00
|
|
|
}
|
|
|
|
Tester.prototype = {
|
2009-09-22 02:16:03 -07:00
|
|
|
EventUtils: {},
|
|
|
|
SimpleTest: {},
|
|
|
|
|
2007-07-09 09:24:15 -07:00
|
|
|
checker: null,
|
|
|
|
currentTestIndex: -1,
|
|
|
|
get currentTest() {
|
|
|
|
return this.tests[this.currentTestIndex];
|
|
|
|
},
|
|
|
|
get done() {
|
|
|
|
return this.currentTestIndex == this.tests.length - 1;
|
|
|
|
},
|
2008-07-15 22:43:15 -07:00
|
|
|
|
2007-07-09 09:24:15 -07:00
|
|
|
start: function Tester_start() {
|
2009-06-11 10:47:37 -07:00
|
|
|
this.dumper.dump("*** Start BrowserChrome Test Results ***\n");
|
2009-07-03 00:22:31 -07:00
|
|
|
this._cs.registerListener(this);
|
2009-06-11 10:47:37 -07:00
|
|
|
|
2009-01-16 11:19:25 -08:00
|
|
|
if (this.tests.length)
|
2010-01-21 04:05:27 -08:00
|
|
|
this.nextTest();
|
2009-01-16 11:19:25 -08:00
|
|
|
else
|
|
|
|
this.finish();
|
2007-07-09 09:24:15 -07:00
|
|
|
},
|
|
|
|
|
2010-01-21 04:05:27 -08:00
|
|
|
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.
|
2010-05-12 15:51:04 -07:00
|
|
|
let self = this;
|
|
|
|
this.SimpleTest.waitForFocus(function() {
|
|
|
|
aCallback.apply(self);
|
|
|
|
});
|
2010-01-21 04:05:27 -08:00
|
|
|
},
|
|
|
|
|
2009-06-11 10:47:37 -07:00
|
|
|
finish: function Tester_finish(aSkipSummary) {
|
2009-07-03 00:22:31 -07:00
|
|
|
this._cs.unregisterListener(this);
|
|
|
|
|
2009-06-11 10:47:37 -07:00
|
|
|
if (this.tests.length) {
|
|
|
|
this.dumper.dump("\nBrowser Chrome Test Summary\n");
|
2009-10-28 17:29:41 -07:00
|
|
|
|
2009-06-11 10:47:37 -07:00
|
|
|
function sum(a,b) a+b;
|
|
|
|
var passCount = this.tests.map(function (f) f.passCount).reduce(sum);
|
|
|
|
var failCount = this.tests.map(function (f) f.failCount).reduce(sum);
|
|
|
|
var todoCount = this.tests.map(function (f) f.todoCount).reduce(sum);
|
2009-10-28 17:29:41 -07:00
|
|
|
|
|
|
|
this.dumper.dump("\tPassed: " + passCount + "\n" +
|
|
|
|
"\tFailed: " + failCount + "\n" +
|
|
|
|
"\tTodo: " + todoCount + "\n");
|
2009-06-11 10:47:37 -07:00
|
|
|
} else {
|
|
|
|
this.dumper.dump("TEST-UNEXPECTED-FAIL | (browser-test.js) | " +
|
2009-06-23 08:59:32 -07:00
|
|
|
"No tests to run. Did you pass an invalid --test-path?");
|
2009-06-11 10:47:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.dumper.dump("\n*** End BrowserChrome Test Results ***\n");
|
|
|
|
|
|
|
|
this.dumper.done();
|
|
|
|
|
2007-07-09 09:24:15 -07:00
|
|
|
// Tests complete, notify the callback and return
|
|
|
|
this.callback(this.tests);
|
|
|
|
this.callback = null;
|
|
|
|
this.tests = null;
|
|
|
|
},
|
|
|
|
|
2009-07-03 00:22:31 -07:00
|
|
|
observe: function Tester_observe(aConsoleMessage) {
|
|
|
|
var msg = "Console message: " + aConsoleMessage.message;
|
2010-01-21 04:05:27 -08:00
|
|
|
if (this.currentTest)
|
|
|
|
this.currentTest.addResult(new testMessage(msg));
|
|
|
|
else
|
|
|
|
this.dumper.dump("TEST-INFO | (browser-test.js) | " + msg);
|
2009-07-03 00:22:31 -07:00
|
|
|
},
|
|
|
|
|
2010-01-21 04:05:27 -08:00
|
|
|
nextTest: function Tester_nextTest() {
|
2009-11-23 13:32:27 -08:00
|
|
|
if (this.currentTest) {
|
|
|
|
// Run cleanup functions for the current test before moving on to the
|
|
|
|
// next one.
|
|
|
|
let testScope = this.currentTest.scope;
|
|
|
|
while (testScope.__cleanupFunctions.length > 0) {
|
|
|
|
let func = testScope.__cleanupFunctions.shift();
|
|
|
|
func.apply(testScope);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-01-21 04:05:27 -08:00
|
|
|
// 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() {
|
2007-07-09 09:24:15 -07:00
|
|
|
if (this.done) {
|
|
|
|
this.finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-21 04:05:27 -08:00
|
|
|
this.currentTestIndex++;
|
|
|
|
this.execTest();
|
|
|
|
},
|
2007-07-09 09:24:15 -07:00
|
|
|
|
2010-01-21 04:05:27 -08:00
|
|
|
execTest: function Tester_execTest() {
|
2009-06-11 10:47:37 -07:00
|
|
|
this.dumper.dump("Running " + this.currentTest.path + "...\n");
|
|
|
|
|
2007-07-09 09:24:15 -07:00
|
|
|
// Load the tests into a testscope
|
2009-06-11 10:47:37 -07:00
|
|
|
this.currentTest.scope = new testScope(this, this.currentTest);
|
2007-07-09 09:24:15 -07:00
|
|
|
|
2009-09-22 02:16:03 -07:00
|
|
|
// Import utils in the test scope.
|
|
|
|
this.currentTest.scope.EventUtils = this.EventUtils;
|
|
|
|
this.currentTest.scope.SimpleTest = this.SimpleTest;
|
2010-03-18 13:11:20 -07:00
|
|
|
// Override SimpleTest methods with ours.
|
|
|
|
["ok", "is", "isnot", "todo", "todo_is", "todo_isnot"].forEach(function(m) {
|
|
|
|
this.SimpleTest[m] = this[m];
|
|
|
|
}, this.currentTest.scope);
|
2009-09-22 02:16:03 -07:00
|
|
|
|
2009-11-23 13:32:27 -08:00
|
|
|
// Import head.js script if it exists.
|
|
|
|
var currentTestDirPath =
|
|
|
|
this.currentTest.path.substr(0, this.currentTest.path.lastIndexOf("/"));
|
|
|
|
var headPath = currentTestDirPath + "/head.js";
|
|
|
|
try {
|
|
|
|
this._scriptLoader.loadSubScript(headPath, this.currentTest.scope);
|
|
|
|
} catch (ex) { /* no head */ }
|
|
|
|
|
|
|
|
// Import the test script.
|
2007-07-09 09:24:15 -07:00
|
|
|
try {
|
2010-01-21 04:05:27 -08:00
|
|
|
this._scriptLoader.loadSubScript(this.currentTest.path,
|
|
|
|
this.currentTest.scope);
|
2007-07-15 17:21:11 -07:00
|
|
|
|
|
|
|
// Run the test
|
2007-07-09 09:24:15 -07:00
|
|
|
this.currentTest.scope.test();
|
|
|
|
} catch (ex) {
|
2009-06-11 10:47:37 -07:00
|
|
|
this.currentTest.addResult(new testResult(false, "Exception thrown", ex, false));
|
|
|
|
this.currentTest.scope.__done = true;
|
2007-07-09 09:24:15 -07:00
|
|
|
}
|
|
|
|
|
2009-02-25 14:27:32 -08:00
|
|
|
// If the test ran synchronously, move to the next test, otherwise the test
|
|
|
|
// will trigger the next test when it is done.
|
2009-06-11 10:47:37 -07:00
|
|
|
if (this.currentTest.scope.__done) {
|
2010-01-21 04:05:27 -08:00
|
|
|
this.nextTest();
|
2009-02-25 14:27:32 -08:00
|
|
|
}
|
|
|
|
else {
|
2007-07-09 09:24:15 -07:00
|
|
|
var self = this;
|
2009-06-11 10:47:37 -07:00
|
|
|
this.currentTest.scope.__waitTimer = setTimeout(function() {
|
2009-12-04 06:13:22 -08:00
|
|
|
if (--self.currentTest.scope.__timeoutFactor > 0) {
|
|
|
|
// We were asked to wait a bit longer.
|
|
|
|
self.currentTest.scope.info(
|
|
|
|
"Longer timeout required, waiting longer... Remaining timeouts: " +
|
|
|
|
self.currentTest.scope.__timeoutFactor);
|
|
|
|
self.currentTest.scope.__waitTimer =
|
|
|
|
setTimeout(arguments.callee, TIMEOUT_SECONDS * 1000);
|
|
|
|
return;
|
|
|
|
}
|
2009-06-11 10:47:37 -07:00
|
|
|
self.currentTest.addResult(new testResult(false, "Timed out", "", false));
|
|
|
|
self.currentTest.scope.__waitTimer = null;
|
2010-01-21 04:05:27 -08:00
|
|
|
self.nextTest();
|
2009-05-03 09:28:43 -07:00
|
|
|
}, TIMEOUT_SECONDS * 1000);
|
2007-07-09 09:24:15 -07:00
|
|
|
}
|
2009-07-03 00:22:31 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
QueryInterface: function(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsIConsoleListener) ||
|
|
|
|
aIID.equals(Ci.nsISupports))
|
|
|
|
return this;
|
|
|
|
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
2007-07-09 09:24:15 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function testResult(aCondition, aName, aDiag, aIsTodo) {
|
2009-07-03 00:22:31 -07:00
|
|
|
this.msg = aName || "";
|
2007-07-09 09:24:15 -07:00
|
|
|
|
2009-07-03 00:22:31 -07:00
|
|
|
this.info = false;
|
2007-07-09 09:24:15 -07:00
|
|
|
this.pass = !!aCondition;
|
2007-07-12 07:29:33 -07:00
|
|
|
this.todo = aIsTodo;
|
2009-07-03 00:22:31 -07:00
|
|
|
|
2007-07-12 07:29:33 -07:00
|
|
|
if (this.pass) {
|
|
|
|
if (aIsTodo)
|
2008-07-15 22:43:15 -07:00
|
|
|
this.result = "TEST-KNOWN-FAIL";
|
2007-07-12 07:29:33 -07:00
|
|
|
else
|
2008-07-15 22:43:15 -07:00
|
|
|
this.result = "TEST-PASS";
|
2007-07-12 07:29:33 -07:00
|
|
|
} else {
|
2009-07-10 12:34:04 -07:00
|
|
|
if (aDiag) {
|
|
|
|
if (typeof aDiag == "object" && "fileName" in aDiag) {
|
|
|
|
// we have an exception - print filename and linenumber information
|
|
|
|
this.msg += " at " + aDiag.fileName + ":" + aDiag.lineNumber;
|
|
|
|
}
|
2007-07-09 09:24:15 -07:00
|
|
|
this.msg += " - " + aDiag;
|
2009-07-10 12:34:04 -07:00
|
|
|
}
|
2008-07-15 22:43:15 -07:00
|
|
|
if (aIsTodo)
|
|
|
|
this.result = "TEST-UNEXPECTED-PASS";
|
|
|
|
else
|
|
|
|
this.result = "TEST-UNEXPECTED-FAIL";
|
2007-07-09 09:24:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-03 00:22:31 -07:00
|
|
|
function testMessage(aName) {
|
|
|
|
this.msg = aName || "";
|
|
|
|
this.info = true;
|
|
|
|
this.result = "TEST-INFO";
|
|
|
|
}
|
|
|
|
|
2009-06-11 10:47:37 -07:00
|
|
|
// Need to be careful adding properties to this object, since its properties
|
|
|
|
// cannot conflict with global variables used in tests.
|
|
|
|
function testScope(aTester, aTest) {
|
|
|
|
this.__tester = aTester;
|
|
|
|
this.__browserTest = aTest;
|
2008-01-25 13:29:11 -08:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this.ok = function test_ok(condition, name, diag) {
|
2009-06-11 10:47:37 -07:00
|
|
|
self.__browserTest.addResult(new testResult(condition, name, diag, false));
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2008-01-25 13:29:11 -08:00
|
|
|
this.is = function test_is(a, b, name) {
|
|
|
|
self.ok(a == b, name, "Got " + a + ", expected " + b);
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2008-01-25 13:29:11 -08:00
|
|
|
this.isnot = function test_isnot(a, b, name) {
|
|
|
|
self.ok(a != b, name, "Didn't expect " + a + ", but got it");
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2008-01-25 13:29:11 -08:00
|
|
|
this.todo = function test_todo(condition, name, diag) {
|
2009-06-11 10:47:37 -07:00
|
|
|
self.__browserTest.addResult(new testResult(!condition, name, diag, true));
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2008-01-25 15:33:17 -08:00
|
|
|
this.todo_is = function test_todo_is(a, b, name) {
|
|
|
|
self.todo(a == b, name, "Got " + a + ", expected " + b);
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2008-01-25 15:33:17 -08:00
|
|
|
this.todo_isnot = function test_todo_isnot(a, b, name) {
|
|
|
|
self.todo(a != b, name, "Didn't expect " + a + ", but got it");
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2009-07-03 00:22:31 -07:00
|
|
|
this.info = function test_info(name) {
|
|
|
|
self.__browserTest.addResult(new testMessage(name));
|
|
|
|
};
|
2008-01-25 15:33:17 -08:00
|
|
|
|
2008-10-30 10:22:17 -07:00
|
|
|
this.executeSoon = function test_executeSoon(func) {
|
|
|
|
let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
|
|
|
|
|
|
|
|
tm.mainThread.dispatch({
|
|
|
|
run: function() {
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
}, Ci.nsIThread.DISPATCH_NORMAL);
|
|
|
|
};
|
|
|
|
|
2009-11-23 13:32:27 -08:00
|
|
|
this.waitForExplicitFinish = function test_waitForExplicitFinish() {
|
2009-06-11 10:47:37 -07:00
|
|
|
self.__done = false;
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2009-06-11 10:47:37 -07:00
|
|
|
|
2010-05-18 06:31:03 -07:00
|
|
|
this.waitForFocus = function test_waitForFocus(callback, targetWindow, expectBlankPage) {
|
|
|
|
self.SimpleTest.waitForFocus(callback, targetWindow, expectBlankPage);
|
2009-09-22 02:16:03 -07:00
|
|
|
};
|
|
|
|
|
2010-06-02 13:38:32 -07:00
|
|
|
this.waitForClipboard = function test_waitForClipboard(expected, setup, success, failure) {
|
|
|
|
self.SimpleTest.waitForClipboard(expected, setup, success, failure);
|
|
|
|
};
|
|
|
|
|
2009-11-23 13:32:27 -08:00
|
|
|
this.registerCleanupFunction = function test_registerCleanupFunction(aFunction) {
|
|
|
|
self.__cleanupFunctions.push(aFunction);
|
|
|
|
};
|
|
|
|
|
2009-12-04 06:13:22 -08:00
|
|
|
this.requestLongerTimeout = function test_requestLongerTimeout(aFactor) {
|
|
|
|
self.__timeoutFactor = aFactor;
|
|
|
|
};
|
|
|
|
|
2008-01-25 13:29:11 -08:00
|
|
|
this.finish = function test_finish() {
|
2009-06-11 10:47:37 -07:00
|
|
|
self.__done = true;
|
|
|
|
if (self.__waitTimer) {
|
2009-02-25 14:27:32 -08:00
|
|
|
self.executeSoon(function() {
|
2009-06-11 10:47:37 -07:00
|
|
|
if (self.__done && self.__waitTimer) {
|
|
|
|
clearTimeout(self.__waitTimer);
|
|
|
|
self.__waitTimer = null;
|
2010-01-21 04:05:27 -08:00
|
|
|
self.__tester.nextTest();
|
2009-02-25 14:27:32 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2008-07-15 22:43:15 -07:00
|
|
|
};
|
2007-07-09 09:24:15 -07:00
|
|
|
}
|
|
|
|
testScope.prototype = {
|
2009-06-11 10:47:37 -07:00
|
|
|
__done: true,
|
|
|
|
__waitTimer: null,
|
2009-11-23 13:32:27 -08:00
|
|
|
__cleanupFunctions: [],
|
2009-12-04 06:13:22 -08:00
|
|
|
__timeoutFactor: 1,
|
2007-07-09 09:24:15 -07:00
|
|
|
|
2009-09-22 02:16:03 -07:00
|
|
|
EventUtils: {},
|
|
|
|
SimpleTest: {}
|
2007-07-09 09:24:15 -07:00
|
|
|
};
|