1 if (Cc
=== undefined) {
2 var Cc
= Components
.classes
;
3 var Ci
= Components
.interfaces
;
5 window
.addEventListener("load", testOnLoad
, false);
7 function testOnLoad() {
8 window
.removeEventListener("load", testOnLoad
, false);
10 // Make sure to launch the test harness for the first opened window only
11 var prefs
= Cc
["@mozilla.org/preferences-service;1"].
12 getService(Ci
.nsIPrefBranch
);
13 if (prefs
.prefHasUserValue("testing.browserTestHarness.running"))
16 prefs
.setBoolPref("testing.browserTestHarness.running", true);
18 var ww
= Cc
["@mozilla.org/embedcomp/window-watcher;1"].
19 getService(Ci
.nsIWindowWatcher
);
20 var sstring
= Cc
["@mozilla.org/supports-string;1"].
21 createInstance(Ci
.nsISupportsString
);
22 sstring
.data
= location
.search
;
23 ww
.openWindow(window
, "chrome://mochikit/content/browser-harness.xul", "browserTest",
24 "chrome,centerscreen,dialog,resizable,titlebar,toolbar=no,width=800,height=600", sstring
);
27 function Tester(aTests
, aCallback
) {
29 this.callback
= aCallback
;
35 return this.tests
[this.currentTestIndex
];
38 return this.currentTestIndex
== this.tests
.length
- 1;
40 step
: function Tester_step() {
41 this.currentTestIndex
++;
44 start
: function Tester_start() {
48 finish
: function Tester_finish() {
49 // Tests complete, notify the callback and return
50 this.callback(this.tests
);
55 execTest
: function Tester_execTest() {
61 // Move to the next test (or first test).
64 // Load the tests into a testscope
65 this.currentTest
.scope
= new testScope(this.currentTest
.tests
);
67 var scriptLoader
= Cc
["@mozilla.org/moz/jssubscript-loader;1"].
68 getService(Ci
.mozIJSSubScriptLoader
);
70 scriptLoader
.loadSubScript(this.currentTest
.path
, this.currentTest
.scope
);
73 this.currentTest
.scope
.test();
75 this.currentTest
.tests
.push(new testResult(false, "Exception thrown", ex
, false));
76 this.currentTest
.scope
.done
= true;
79 // If the test ran synchronously, move to the next test,
80 // otherwise start a poller to monitor it's progress.
81 if (this.currentTest
.scope
.done
) {
85 this.checker
= new resultPoller(this.currentTest
, function () { self
.execTest(); });
91 function testResult(aCondition
, aName
, aDiag
, aIsTodo
) {
94 this.pass
= !!aCondition
;
99 this.result
= "TEST-KNOWN-FAIL";
101 this.result
= "TEST-PASS";
104 this.msg
+= " - " + aDiag
;
106 this.result
= "TEST-UNEXPECTED-PASS";
108 this.result
= "TEST-UNEXPECTED-FAIL";
112 function testScope(aTests
) {
113 var scriptLoader
= Cc
["@mozilla.org/moz/jssubscript-loader;1"].
114 getService(Ci
.mozIJSSubScriptLoader
);
115 scriptLoader
.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils
);
120 this.ok
= function test_ok(condition
, name
, diag
) {
121 self
.tests
.push(new testResult(condition
, name
, diag
, false));
123 this.is
= function test_is(a
, b
, name
) {
124 self
.ok(a
== b
, name
, "Got " + a
+ ", expected " + b
);
126 this.isnot
= function test_isnot(a
, b
, name
) {
127 self
.ok(a
!= b
, name
, "Didn't expect " + a
+ ", but got it");
129 this.todo
= function test_todo(condition
, name
, diag
) {
130 self
.tests
.push(new testResult(!condition
, name
, diag
, true));
132 this.todo_is
= function test_todo_is(a
, b
, name
) {
133 self
.todo(a
== b
, name
, "Got " + a
+ ", expected " + b
);
135 this.todo_isnot
= function test_todo_isnot(a
, b
, name
) {
136 self
.todo(a
!= b
, name
, "Didn't expect " + a
+ ", but got it");
139 this.waitForExplicitFinish
= function test_WFEF() {
142 this.finish
= function test_finish() {
146 testScope
.prototype = {
152 // Check whether the test has completed every 3 seconds
153 const CHECK_INTERVAL
= 3000;
154 // Test timeout (seconds)
155 const TIMEOUT_SECONDS
= 30;
157 const MAX_LOOP_COUNT
= (TIMEOUT_SECONDS
* 1000) / CHECK_INTERVAL
;
159 function resultPoller(aTest
, aCallback
) {
161 this.callback
= aCallback
;
163 resultPoller
.prototype = {
167 start
: function resultPoller_start() {
169 function checkDone() {
172 if (self
.loopCount
> MAX_LOOP_COUNT
) {
173 self
.test
.tests
.push(new testResult(false, "Timed out", "", false));
174 self
.test
.scope
.done
= true;
177 if (self
.test
.scope
.done
) {
178 clearInterval(self
.interval
);
180 // Notify the callback
182 self
.callback
= null;
186 this.interval
= setInterval(checkDone
, CHECK_INTERVAL
);