Bug 449371 Firefox/Thunderbird crashes at exit [@ gdk_display_x11_finalize], p=Brian...
[wine-gecko.git] / testing / mochitest / browser-test.js
blob5cfe93913f59e258f21ffcca8d1b8725329fca82
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"))
12 return;
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) {
26 this.tests = aTests;
27 this.callback = aCallback;
29 Tester.prototype = {
30 checker: null,
31 currentTestIndex: -1,
32 get currentTest() {
33 return this.tests[this.currentTestIndex];
35 get done() {
36 return this.currentTestIndex == this.tests.length - 1;
38 step: function Tester_step() {
39 this.currentTestIndex++;
42 start: function Tester_start() {
43 this.execTest();
46 finish: function Tester_finish() {
47 // Tests complete, notify the callback and return
48 this.callback(this.tests);
49 this.callback = null;
50 this.tests = null;
53 execTest: function Tester_execTest() {
54 if (this.done) {
55 this.finish();
56 return;
59 // Move to the next test (or first test).
60 this.step();
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);
67 try {
68 scriptLoader.loadSubScript(this.currentTest.path, this.currentTest.scope);
70 // Run the test
71 this.currentTest.scope.test();
72 } catch (ex) {
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) {
80 this.execTest();
81 } else {
82 var self = this;
83 this.checker = new resultPoller(this.currentTest, function () { self.execTest(); });
84 this.checker.start();
89 function testResult(aCondition, aName, aDiag, aIsTodo) {
90 aName = aName || "";
92 this.pass = !!aCondition;
93 this.todo = aIsTodo;
94 this.msg = aName;
95 if (this.pass) {
96 if (aIsTodo)
97 this.result = "TEST-KNOWN-FAIL";
98 else
99 this.result = "TEST-PASS";
100 } else {
101 if (aDiag)
102 this.msg += " - " + aDiag;
103 if (aIsTodo)
104 this.result = "TEST-UNEXPECTED-PASS";
105 else
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);
115 this.tests = aTests;
117 var self = this;
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() {
138 self.done = false;
140 this.finish = function test_finish() {
141 self.done = true;
144 testScope.prototype = {
145 done: true,
147 EventUtils: {}
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) {
158 this.test = aTest;
159 this.callback = aCallback;
161 resultPoller.prototype = {
162 loopCount: 0,
163 interval: 0,
165 start: function resultPoller_start() {
166 var self = this;
167 function checkDone() {
168 self.loopCount++;
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
179 self.callback();
180 self.callback = null;
181 self.test = null;
184 this.interval = setInterval(checkDone, CHECK_INTERVAL);