1 // Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
2 // (c) 2005 Jon Tirsen (http://www.tirsen.com)
3 // (c) 2005 Michael Schuerig (http://www.schuerig.de/michael/)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 // experimental, Firefox-only
26 Event.simulateMouse = function(element, eventName) {
27 var options = Object.extend({
31 }, arguments[2] || {});
32 var oEvent = document.createEvent("MouseEvents");
33 oEvent.initMouseEvent(eventName, true, true, document.defaultView,
34 options.buttons, options.pointerX, options.pointerY, options.pointerX, options.pointerY,
35 false, false, false, false, 0, $(element));
37 if(this.mark) Element.remove(this.mark);
38 this.mark = document.createElement('div');
39 this.mark.appendChild(document.createTextNode(" "));
40 document.body.appendChild(this.mark);
41 this.mark.style.position = 'absolute';
42 this.mark.style.top = options.pointerY + "px";
43 this.mark.style.left = options.pointerX + "px";
44 this.mark.style.width = "5px";
45 this.mark.style.height = "5px;";
46 this.mark.style.borderTop = "1px solid red;"
47 this.mark.style.borderLeft = "1px solid red;"
50 alert('['+new Date().getTime().toString()+'] '+eventName+'/'+Test.Unit.inspect(options));
52 $(element).dispatchEvent(oEvent);
55 // Note: Due to a fix in Firefox 1.0.5/6 that probably fixed "too much", this doesn't work in 1.0.6 or DP2.
56 // You need to downgrade to 1.0.4 for now to get this working
57 // See https://bugzilla.mozilla.org/show_bug.cgi?id=289940 for the fix that fixed too much
58 Event.simulateKey = function(element, eventName) {
59 var options = Object.extend({
66 }, arguments[2] || {});
68 var oEvent = document.createEvent("KeyEvents");
69 oEvent.initKeyEvent(eventName, true, true, window,
70 options.ctrlKey, options.altKey, options.shiftKey, options.metaKey,
71 options.keyCode, options.charCode );
72 $(element).dispatchEvent(oEvent);
75 Event.simulateKeys = function(element, command) {
76 for(var i=0; i<command.length; i++) {
77 Event.simulateKey(element,'keypress',{charCode:command.charCodeAt(i)});
84 // security exception workaround
85 Test.Unit.inspect = function(obj) {
88 if(typeof obj=="string" ||
89 typeof obj=="number") {
93 if(typeof obj[property]!="function")
94 info.push(property + ' => ' +
95 (typeof obj[property] == "string" ?
96 '"' + obj[property] + '"' :
100 return ("'" + obj + "' #" + typeof obj +
101 ": {" + info.join(", ") + "}");
104 Test.Unit.Logger = Class.create();
105 Test.Unit.Logger.prototype = {
106 initialize: function(log) {
109 this._createLogTable();
112 start: function(testName) {
113 if (!this.log) return;
114 this.testName = testName;
115 this.lastLogLine = document.createElement('tr');
116 this.statusCell = document.createElement('td');
117 this.nameCell = document.createElement('td');
118 this.nameCell.appendChild(document.createTextNode(testName));
119 this.messageCell = document.createElement('td');
120 this.lastLogLine.appendChild(this.statusCell);
121 this.lastLogLine.appendChild(this.nameCell);
122 this.lastLogLine.appendChild(this.messageCell);
123 this.loglines.appendChild(this.lastLogLine);
125 finish: function(status, summary) {
126 if (!this.log) return;
127 this.lastLogLine.className = status;
128 this.statusCell.innerHTML = status;
129 this.messageCell.innerHTML = this._toHTML(summary);
131 message: function(message) {
132 if (!this.log) return;
133 this.messageCell.innerHTML = this._toHTML(message);
135 summary: function(summary) {
136 if (!this.log) return;
137 this.logsummary.innerHTML = this._toHTML(summary);
139 _createLogTable: function() {
141 '<div id="logsummary"></div>' +
142 '<table id="logtable">' +
143 '<thead><tr><th>Status</th><th>Test</th><th>Message</th></tr></thead>' +
144 '<tbody id="loglines"></tbody>' +
146 this.logsummary = $('logsummary')
147 this.loglines = $('loglines');
149 _toHTML: function(txt) {
150 return txt.escapeHTML().replace(/\n/g,"<br/>");
154 Test.Unit.Runner = Class.create();
155 Test.Unit.Runner.prototype = {
156 initialize: function(testcases) {
157 this.options = Object.extend({
159 }, arguments[1] || {});
160 this.options.resultsURL = this.parseResultsURLQueryParameter();
161 if (this.options.testLog) {
162 this.options.testLog = $(this.options.testLog) || null;
164 if(this.options.tests) {
166 for(var i = 0; i < this.options.tests.length; i++) {
167 if(/^test/.test(this.options.tests[i])) {
168 this.tests.push(new Test.Unit.Testcase(this.options.tests[i], testcases[this.options.tests[i]], testcases["setup"], testcases["teardown"]));
172 if (this.options.test) {
173 this.tests = [new Test.Unit.Testcase(this.options.test, testcases[this.options.test], testcases["setup"], testcases["teardown"])];
176 for(var testcase in testcases) {
177 if(/^test/.test(testcase)) {
178 this.tests.push(new Test.Unit.Testcase(testcase, testcases[testcase], testcases["setup"], testcases["teardown"]));
183 this.currentTest = 0;
184 this.logger = new Test.Unit.Logger(this.options.testLog);
185 setTimeout(this.runTests.bind(this), 1000);
187 parseResultsURLQueryParameter: function() {
188 return window.location.search.parseQuery()["resultsURL"];
191 // "ERROR" if there was an error,
192 // "FAILURE" if there was a failure, or
193 // "SUCCESS" if there was neither
194 getResult: function() {
195 var hasFailure = false;
196 for(var i=0;i<this.tests.length;i++) {
197 if (this.tests[i].errors > 0) {
200 if (this.tests[i].failures > 0) {
210 postResults: function() {
211 if (this.options.resultsURL) {
212 new Ajax.Request(this.options.resultsURL,
213 { method: 'get', parameters: 'result=' + this.getResult(), asynchronous: false });
216 runTests: function() {
217 var test = this.tests[this.currentTest];
221 this.logger.summary(this.summary());
224 if(!test.isWaiting) {
225 this.logger.start(test.name);
229 this.logger.message("Waiting for " + test.timeToWait + "ms");
230 setTimeout(this.runTests.bind(this), test.timeToWait || 1000);
232 this.logger.finish(test.status(), test.summary());
234 // tail recursive, hopefully the browser will skip the stackframe
238 summary: function() {
243 for(var i=0;i<this.tests.length;i++) {
244 assertions += this.tests[i].assertions;
245 failures += this.tests[i].failures;
246 errors += this.tests[i].errors;
249 this.tests.length + " tests, " +
250 assertions + " assertions, " +
251 failures + " failures, " +
256 Test.Unit.Assertions = Class.create();
257 Test.Unit.Assertions.prototype = {
258 initialize: function() {
264 summary: function() {
266 this.assertions + " assertions, " +
267 this.failures + " failures, " +
268 this.errors + " errors" + "\n" +
269 this.messages.join("\n"));
274 fail: function(message) {
276 this.messages.push("Failure: " + message);
278 error: function(error) {
280 this.messages.push(error.name + ": "+ error.message + "(" + Test.Unit.inspect(error) +")");
283 if (this.failures > 0) return 'failed';
284 if (this.errors > 0) return 'error';
287 assert: function(expression) {
288 var message = arguments[1] || 'assert: got "' + Test.Unit.inspect(expression) + '"';
289 try { expression ? this.pass() :
290 this.fail(message); }
291 catch(e) { this.error(e); }
293 assertEqual: function(expected, actual) {
294 var message = arguments[2] || "assertEqual";
295 try { (expected == actual) ? this.pass() :
296 this.fail(message + ': expected "' + Test.Unit.inspect(expected) +
297 '", actual "' + Test.Unit.inspect(actual) + '"'); }
298 catch(e) { this.error(e); }
300 assertNotEqual: function(expected, actual) {
301 var message = arguments[2] || "assertNotEqual";
302 try { (expected != actual) ? this.pass() :
303 this.fail(message + ': got "' + Test.Unit.inspect(actual) + '"'); }
304 catch(e) { this.error(e); }
306 assertNull: function(obj) {
307 var message = arguments[1] || 'assertNull'
308 try { (obj==null) ? this.pass() :
309 this.fail(message + ': got "' + Test.Unit.inspect(obj) + '"'); }
310 catch(e) { this.error(e); }
312 assertHidden: function(element) {
313 var message = arguments[1] || 'assertHidden';
314 this.assertEqual("none", element.style.display, message);
316 assertNotNull: function(object) {
317 var message = arguments[1] || 'assertNotNull';
318 this.assert(object != null, message);
320 assertInstanceOf: function(expected, actual) {
321 var message = arguments[2] || 'assertInstanceOf';
323 (actual instanceof expected) ? this.pass() :
324 this.fail(message + ": object was not an instance of the expected type"); }
325 catch(e) { this.error(e); }
327 assertNotInstanceOf: function(expected, actual) {
328 var message = arguments[2] || 'assertNotInstanceOf';
330 !(actual instanceof expected) ? this.pass() :
331 this.fail(message + ": object was an instance of the not expected type"); }
332 catch(e) { this.error(e); }
334 _isVisible: function(element) {
335 element = $(element);
336 if(!element.parentNode) return true;
337 this.assertNotNull(element);
338 if(element.style && Element.getStyle(element, 'display') == 'none')
341 return this._isVisible(element.parentNode);
343 assertNotVisible: function(element) {
344 this.assert(!this._isVisible(element), Test.Unit.inspect(element) + " was not hidden and didn't have a hidden parent either. " + ("" || arguments[1]));
346 assertVisible: function(element) {
347 this.assert(this._isVisible(element), Test.Unit.inspect(element) + " was not visible. " + ("" || arguments[1]));
351 Test.Unit.Testcase = Class.create();
352 Object.extend(Object.extend(Test.Unit.Testcase.prototype, Test.Unit.Assertions.prototype), {
353 initialize: function(name, test, setup, teardown) {
354 Test.Unit.Assertions.prototype.initialize.bind(this)();
356 this.test = test || function() {};
357 this.setup = setup || function() {};
358 this.teardown = teardown || function() {};
359 this.isWaiting = false;
360 this.timeToWait = 1000;
362 wait: function(time, nextPart) {
363 this.isWaiting = true;
364 this.test = nextPart;
365 this.timeToWait = time;
370 if (!this.isWaiting) this.setup.bind(this)();
371 this.isWaiting = false;
372 this.test.bind(this)();
374 if(!this.isWaiting) {
375 this.teardown.bind(this)();
379 catch(e) { this.error(e); }