Bug 458113. Fix syntax error that broke OS/2 build. r+wuno
[wine-gecko.git] / testing / mochitest / browser-test.js
blob71b000e8b741b5ac54c54572b1b30a7703254e85
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"))
14 return;
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) {
28 this.tests = aTests;
29 this.callback = aCallback;
31 Tester.prototype = {
32 checker: null,
33 currentTestIndex: -1,
34 get currentTest() {
35 return this.tests[this.currentTestIndex];
37 get done() {
38 return this.currentTestIndex == this.tests.length - 1;
40 step: function Tester_step() {
41 this.currentTestIndex++;
44 start: function Tester_start() {
45 this.execTest();
48 finish: function Tester_finish() {
49 // Tests complete, notify the callback and return
50 this.callback(this.tests);
51 this.callback = null;
52 this.tests = null;
55 execTest: function Tester_execTest() {
56 if (this.done) {
57 this.finish();
58 return;
61 // Move to the next test (or first test).
62 this.step();
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);
69 try {
70 scriptLoader.loadSubScript(this.currentTest.path, this.currentTest.scope);
72 // Run the test
73 this.currentTest.scope.test();
74 } catch (ex) {
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) {
82 this.execTest();
83 } else {
84 var self = this;
85 this.checker = new resultPoller(this.currentTest, function () { self.execTest(); });
86 this.checker.start();
91 function testResult(aCondition, aName, aDiag, aIsTodo) {
92 aName = aName || "";
94 this.pass = !!aCondition;
95 this.todo = aIsTodo;
96 this.msg = aName;
97 if (this.pass) {
98 if (aIsTodo)
99 this.result = "TEST-KNOWN-FAIL";
100 else
101 this.result = "TEST-PASS";
102 } else {
103 if (aDiag)
104 this.msg += " - " + aDiag;
105 if (aIsTodo)
106 this.result = "TEST-UNEXPECTED-PASS";
107 else
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);
117 this.tests = aTests;
119 var self = this;
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() {
140 self.done = false;
142 this.finish = function test_finish() {
143 self.done = true;
146 testScope.prototype = {
147 done: true,
149 EventUtils: {}
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) {
160 this.test = aTest;
161 this.callback = aCallback;
163 resultPoller.prototype = {
164 loopCount: 0,
165 interval: 0,
167 start: function resultPoller_start() {
168 var self = this;
169 function checkDone() {
170 self.loopCount++;
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
181 self.callback();
182 self.callback = null;
183 self.test = null;
186 this.interval = setInterval(checkDone, CHECK_INTERVAL);