Bug 435739 Poor performance of Firefox 3 with no X RENDER extension
[wine-gecko.git] / testing / mochitest / browser-test.js
blobec2b7d5303a48df260189bfff5b189f0e266222f
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 if (this.pass) {
95 if (aIsTodo)
96 this.msg = "\tTODO PASS - " + aName;
97 else
98 this.msg = "\tPASS - " + aName;
99 } else {
100 this.msg = "\tFAIL - ";
101 if (aIsTodo)
102 this.msg += "TODO Worked? - ";
103 this.msg += aName;
104 if (aDiag)
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);
114 this.tests = aTests;
116 var self = this;
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() {
137 self.done = false;
139 this.finish = function test_finish() {
140 self.done = true;
143 testScope.prototype = {
144 done: true,
146 EventUtils: {}
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) {
157 this.test = aTest;
158 this.callback = aCallback;
160 resultPoller.prototype = {
161 loopCount: 0,
162 interval: 0,
164 start: function resultPoller_start() {
165 var self = this;
166 function checkDone() {
167 self.loopCount++;
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
178 self.callback();
179 self.callback = null;
180 self.test = null;
183 this.interval = setInterval(checkDone, CHECK_INTERVAL);