1 if (Cc
=== undefined) {
2 var Cc
= Components
.classes
;
3 var Ci
= Components
.interfaces
;
5 window
.addEventListener("load", testOnLoad
, false);
7 function testOnLoad() {
8 // Make sure to launch the test harness for the first opened window only
9 var prefs
= Cc
["@mozilla.org/preferences-service;1"].
10 getService(Ci
.nsIPrefBranch
);
11 if (prefs
.prefHasUserValue("testing.browserTestHarness.running"))
14 prefs
.setBoolPref("testing.browserTestHarness.running", true);
16 var ww
= Cc
["@mozilla.org/embedcomp/window-watcher;1"].
17 getService(Ci
.nsIWindowWatcher
);
18 var sstring
= Cc
["@mozilla.org/supports-string;1"].
19 createInstance(Ci
.nsISupportsString
);
20 sstring
.data
= location
.search
;
21 ww
.openWindow(window
, "chrome://mochikit/content/browser-harness.xul", "browserTest",
22 "chrome,centerscreen,dialog,resizable,titlebar,toolbar=no,width=800,height=600", sstring
);
25 function Tester(aTests
, aCallback
) {
27 this.callback
= aCallback
;
33 return this.tests
[this.currentTestIndex
];
36 return this.currentTestIndex
== this.tests
.length
- 1;
38 step
: function Tester_step() {
39 this.currentTestIndex
++;
42 start
: function Tester_start() {
46 finish
: function Tester_finish() {
47 // Tests complete, notify the callback and return
48 this.callback(this.tests
);
53 execTest
: function Tester_execTest() {
59 // Move to the next test (or first test).
62 // Load the tests into a testscope
63 this.currentTest
.scope
= new testScope(this.currentTest
.tests
);
65 var scriptLoader
= Cc
["@mozilla.org/moz/jssubscript-loader;1"].
66 getService(Ci
.mozIJSSubScriptLoader
);
68 scriptLoader
.loadSubScript(this.currentTest
.path
, this.currentTest
.scope
);
71 this.currentTest
.scope
.test();
73 this.currentTest
.tests
.push(new testResult(false, "Exception thrown", ex
, false));
74 this.currentTest
.scope
.done
= true;
77 // If the test ran synchronously, move to the next test,
78 // otherwise start a poller to monitor it's progress.
79 if (this.currentTest
.scope
.done
) {
83 this.checker
= new resultPoller(this.currentTest
, function () { self
.execTest(); });
89 function testResult(aCondition
, aName
, aDiag
, aIsTodo
) {
92 this.pass
= !!aCondition
;
97 this.result
= "TEST-KNOWN-FAIL";
99 this.result
= "TEST-PASS";
102 this.msg
+= " - " + aDiag
;
104 this.result
= "TEST-UNEXPECTED-PASS";
106 this.result
= "TEST-UNEXPECTED-FAIL";
110 function testScope(aTests
) {
111 var scriptLoader
= Cc
["@mozilla.org/moz/jssubscript-loader;1"].
112 getService(Ci
.mozIJSSubScriptLoader
);
113 scriptLoader
.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils
);
118 this.ok
= function test_ok(condition
, name
, diag
) {
119 self
.tests
.push(new testResult(condition
, name
, diag
, false));
121 this.is
= function test_is(a
, b
, name
) {
122 self
.ok(a
== b
, name
, "Got " + a
+ ", expected " + b
);
124 this.isnot
= function test_isnot(a
, b
, name
) {
125 self
.ok(a
!= b
, name
, "Didn't expect " + a
+ ", but got it");
127 this.todo
= function test_todo(condition
, name
, diag
) {
128 self
.tests
.push(new testResult(!condition
, name
, diag
, true));
130 this.todo_is
= function test_todo_is(a
, b
, name
) {
131 self
.todo(a
== b
, name
, "Got " + a
+ ", expected " + b
);
133 this.todo_isnot
= function test_todo_isnot(a
, b
, name
) {
134 self
.todo(a
!= b
, name
, "Didn't expect " + a
+ ", but got it");
137 this.waitForExplicitFinish
= function test_WFEF() {
140 this.finish
= function test_finish() {
144 testScope
.prototype = {
150 // Check whether the test has completed every 3 seconds
151 const CHECK_INTERVAL
= 3000;
152 // Test timeout (seconds)
153 const TIMEOUT_SECONDS
= 30;
155 const MAX_LOOP_COUNT
= (TIMEOUT_SECONDS
* 1000) / CHECK_INTERVAL
;
157 function resultPoller(aTest
, aCallback
) {
159 this.callback
= aCallback
;
161 resultPoller
.prototype = {
165 start
: function resultPoller_start() {
167 function checkDone() {
170 if (self
.loopCount
> MAX_LOOP_COUNT
) {
171 self
.test
.tests
.push(new testResult(false, "Timed out", "", false));
172 self
.test
.scope
.done
= true;
175 if (self
.test
.scope
.done
) {
176 clearInterval(self
.interval
);
178 // Notify the callback
180 self
.callback
= null;
184 this.interval
= setInterval(checkDone
, CHECK_INTERVAL
);