Bug 481406: Include console messages in the log. r=sdwilsh

This commit is contained in:
Dave Townsend 2009-05-20 10:18:27 +01:00
parent 8b8f8200db
commit 76add14cd8
3 changed files with 67 additions and 28 deletions

View File

@ -153,34 +153,19 @@
} }
browserTestFile.prototype = { browserTestFile.prototype = {
get passCount() { get passCount() {
return this.tests.filter(function (t) !t.todo && t.pass).length; return this.tests.filter(function (t) !t.info && !t.todo && t.pass).length;
}, },
get todoCount() { get todoCount() {
return this.tests.filter(function (t) t.todo && t.pass).length; return this.tests.filter(function (t) !t.info && t.todo && t.pass).length;
}, },
get failCount() { get failCount() {
return this.tests.filter(function (t) !t.pass).length; return this.tests.filter(function (t) !t.info && !t.pass).length;
}, },
get log() { get log() {
var path = this.path; return this.tests.map(function (t) t.log).join("\n");
return this.tests.map(function (t) {
return t.result + " | " + path + " | " + t.msg;
}).join("\n");
}, },
get htmlLog() { get htmlLog() {
let txtToHTML = Cc["@mozilla.org/txttohtmlconv;1"]. return this.tests.map(function (t) t.htmlLog).join("\n");
getService(Ci.mozITXTToHTMLConv);
function _entityEncode(str) {
return txtToHTML.scanTXT(str, Ci.mozITXTToHTMLConv.kEntities);
}
var path = _entityEncode( this.path );
return this.tests.map(function (t) {
var result = "<p class=\"result ";
result += t.pass ? "passed" : "failed";
result += "\">" + t.result + " | " + path +
" | " + _entityEncode( t.msg ) + "</p>";
return result;
}).join("\n");
} }
}; };

View File

@ -7,6 +7,10 @@ if (Cc === undefined) {
} }
window.addEventListener("load", testOnLoad, false); window.addEventListener("load", testOnLoad, false);
let txtToHTML = Cc["@mozilla.org/txttohtmlconv;1"].
getService(Ci.mozITXTToHTMLConv);
function entityEncode(str) txtToHTML.scanTXT(str, Ci.mozITXTToHTMLConv.kEntities);
function testOnLoad() { function testOnLoad() {
window.removeEventListener("load", testOnLoad, false); window.removeEventListener("load", testOnLoad, false);
@ -30,6 +34,8 @@ function testOnLoad() {
function Tester(aTests, aCallback) { function Tester(aTests, aCallback) {
this.tests = aTests; this.tests = aTests;
this.callback = aCallback; this.callback = aCallback;
this._cs = Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService);
} }
Tester.prototype = { Tester.prototype = {
checker: null, checker: null,
@ -45,6 +51,7 @@ Tester.prototype = {
}, },
start: function Tester_start() { start: function Tester_start() {
this._cs.registerListener(this);
if (this.tests.length) if (this.tests.length)
this.execTest(); this.execTest();
else else
@ -53,11 +60,17 @@ Tester.prototype = {
finish: function Tester_finish() { finish: function Tester_finish() {
// Tests complete, notify the callback and return // Tests complete, notify the callback and return
this._cs.unregisterListener(this);
this.callback(this.tests); this.callback(this.tests);
this.callback = null; this.callback = null;
this.tests = null; this.tests = null;
}, },
observe: function Tester_observe(aConsoleMessage) {
var msg = "Console message: " + aConsoleMessage.message;
this.currentTest.tests.push(new testMessage(this.currentTest.path, msg));
},
execTest: function Tester_execTest() { execTest: function Tester_execTest() {
if (this.done) { if (this.done) {
this.finish(); this.finish();
@ -78,7 +91,7 @@ Tester.prototype = {
// Run the test // Run the test
this.currentTest.scope.test(); this.currentTest.scope.test();
} catch (ex) { } catch (ex) {
this.currentTest.tests.push(new testResult(false, "Exception thrown", ex, false)); this.currentTest.tests.push(new testResult(this.currentTest.path, false, "Exception thrown", ex, false));
this.currentTest.scope.done = true; this.currentTest.scope.done = true;
} }
@ -90,20 +103,28 @@ Tester.prototype = {
else { else {
var self = this; var self = this;
this.currentTest.scope.waitTimer = setTimeout(function() { this.currentTest.scope.waitTimer = setTimeout(function() {
self.currentTest.tests.push(new testResult(false, "Timed out", "", false)); self.currentTest.tests.push(new testResult(self.currentTest.path, false, "Timed out", "", false));
self.currentTest.scope.waitTimer = null; self.currentTest.scope.waitTimer = null;
self.execTest(); self.execTest();
}, TIMEOUT_SECONDS * 1000); }, TIMEOUT_SECONDS * 1000);
} }
},
QueryInterface: function(aIID) {
if (aIID.equals(Ci.nsIConsoleListener) ||
aIID.equals(Ci.nsISupports))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
} }
}; };
function testResult(aCondition, aName, aDiag, aIsTodo) { function testResult(aPath, aCondition, aName, aDiag, aIsTodo) {
aName = aName || ""; this.info = false;
this.path = aPath;
this.pass = !!aCondition; this.pass = !!aCondition;
this.todo = aIsTodo; this.todo = aIsTodo;
this.msg = aName; this.msg = aName || "";
if (this.pass) { if (this.pass) {
if (aIsTodo) if (aIsTodo)
this.result = "TEST-KNOWN-FAIL"; this.result = "TEST-KNOWN-FAIL";
@ -119,6 +140,35 @@ function testResult(aCondition, aName, aDiag, aIsTodo) {
} }
} }
testResult.prototype = {
get log() {
return this.result + " | " + this.path + " | " + this.msg;
},
get htmlLog() {
return "<p class=\"result " + (this.pass ? "passed" : "failed") + "\">" +
this.result + " | " + entityEncode(this.path) + " | " +
entityEncode(this.msg) + "</p>";
}
}
function testMessage(aPath, aMessage) {
this.info = true;
this.path = aPath;
this.msg = aMessage || "";
}
testMessage.prototype = {
get log() {
return "TEST-INFO | " + this.path + " | " + this.msg;
},
get htmlLog() {
return "<p class=\"info\">TEST-INFO | " + entityEncode(this.path) + " | " +
entityEncode(this.msg) + "</p>";
}
}
function testScope(aTester, aTests) { function testScope(aTester, aTests) {
var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
getService(Ci.mozIJSSubScriptLoader); getService(Ci.mozIJSSubScriptLoader);
@ -129,7 +179,7 @@ function testScope(aTester, aTests) {
var self = this; var self = this;
this.ok = function test_ok(condition, name, diag) { this.ok = function test_ok(condition, name, diag) {
self.tests.push(new testResult(condition, name, diag, false)); self.tests.push(new testResult(self.tester.currentTest.path, condition, name, diag, false));
}; };
this.is = function test_is(a, b, name) { this.is = function test_is(a, b, name) {
self.ok(a == b, name, "Got " + a + ", expected " + b); self.ok(a == b, name, "Got " + a + ", expected " + b);
@ -138,7 +188,7 @@ function testScope(aTester, aTests) {
self.ok(a != b, name, "Didn't expect " + a + ", but got it"); self.ok(a != b, name, "Didn't expect " + a + ", but got it");
}; };
this.todo = function test_todo(condition, name, diag) { this.todo = function test_todo(condition, name, diag) {
self.tests.push(new testResult(!condition, name, diag, true)); self.tests.push(new testResult(self.tester.currentTest.path, !condition, name, diag, true));
}; };
this.todo_is = function test_todo_is(a, b, name) { this.todo_is = function test_todo_is(a, b, name) {
self.todo(a == b, name, "Got " + a + ", expected " + b); self.todo(a == b, name, "Got " + a + ", expected " + b);
@ -146,6 +196,9 @@ function testScope(aTester, aTests) {
this.todo_isnot = function test_todo_isnot(a, b, name) { this.todo_isnot = function test_todo_isnot(a, b, name) {
self.todo(a != b, name, "Didn't expect " + a + ", but got it"); self.todo(a != b, name, "Didn't expect " + a + ", but got it");
}; };
this.info = function(message) {
self.tests.push(new testMessage(self.tester.currentTest.path, message));
}
this.executeSoon = function test_executeSoon(func) { this.executeSoon = function test_executeSoon(func) {
let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager); let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);

View File

@ -5,6 +5,7 @@ function test() {
todo(false, "pass todo"); todo(false, "pass todo");
todo_is(false, true, "pass todo_is"); todo_is(false, true, "pass todo_is");
todo_isnot(true, true, "pass todo_isnot"); todo_isnot(true, true, "pass todo_isnot");
info("info message");
var func = is; var func = is;
func(true, true, "pass indirect is"); func(true, true, "pass indirect is");