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
;
96 this.msg
= "\tTODO PASS - " + aName
;
98 this.msg
= "\tPASS - " + aName
;
100 this.msg
= "\tFAIL - ";
102 this.msg
+= "TODO Worked? - ";
105 this.msg
+= " - " + aDiag
;
109 function testScope(aTests
) {
110 var scriptLoader
= Cc
["@mozilla.org/moz/jssubscript-loader;1"].
111 getService(Ci
.mozIJSSubScriptLoader
);
112 scriptLoader
.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils
);
117 this.ok
= function test_ok(condition
, name
, diag
) {
118 self
.tests
.push(new testResult(condition
, name
, diag
, false));
120 this.is
= function test_is(a
, b
, name
) {
121 self
.ok(a
== b
, name
, "Got " + a
+ ", expected " + b
);
123 this.isnot
= function test_isnot(a
, b
, name
) {
124 self
.ok(a
!= b
, name
, "Didn't expect " + a
+ ", but got it");
126 this.todo
= function test_todo(condition
, name
, diag
) {
127 self
.tests
.push(new testResult(!condition
, name
, diag
, true));
129 this.todo_is
= function test_todo_is(a
, b
, name
) {
130 self
.todo(a
== b
, name
, "Got " + a
+ ", expected " + b
);
132 this.todo_isnot
= function test_todo_isnot(a
, b
, name
) {
133 self
.todo(a
!= b
, name
, "Didn't expect " + a
+ ", but got it");
136 this.waitForExplicitFinish
= function test_WFEF() {
139 this.finish
= function test_finish() {
143 testScope
.prototype = {
149 // Check whether the test has completed every 3 seconds
150 const CHECK_INTERVAL
= 3000;
151 // Test timeout (seconds)
152 const TIMEOUT_SECONDS
= 30;
154 const MAX_LOOP_COUNT
= (TIMEOUT_SECONDS
* 1000) / CHECK_INTERVAL
;
156 function resultPoller(aTest
, aCallback
) {
158 this.callback
= aCallback
;
160 resultPoller
.prototype = {
164 start
: function resultPoller_start() {
166 function checkDone() {
169 if (self
.loopCount
> MAX_LOOP_COUNT
) {
170 self
.test
.tests
.push(new testResult(false, "Timed out", "", false));
171 self
.test
.scope
.done
= true;
174 if (self
.test
.scope
.done
) {
175 clearInterval(self
.interval
);
177 // Notify the callback
179 self
.callback
= null;
183 this.interval
= setInterval(checkDone
, CHECK_INTERVAL
);