2006-11-18 14:37:01 +01:00
|
|
|
var _config = {
|
|
|
|
fixture: null,
|
|
|
|
Test: [],
|
|
|
|
stats: {
|
|
|
|
all: 0,
|
|
|
|
bad: 0
|
|
|
|
},
|
|
|
|
queue: [],
|
|
|
|
blocking: true,
|
|
|
|
timeout: null,
|
|
|
|
expected: null,
|
|
|
|
currentModule: null,
|
|
|
|
asyncTimeout: 2 // seconds for async timeout
|
2006-09-19 11:49:22 +02:00
|
|
|
};
|
2006-11-18 14:37:01 +01:00
|
|
|
|
2008-04-24 23:32:35 +02:00
|
|
|
_config.filters = location.search.length > 1 && //restrict modules/tests by get parameters
|
|
|
|
$.map( location.search.slice(1).split('&'), decodeURIComponent );
|
|
|
|
|
2007-08-20 01:37:26 +02:00
|
|
|
var isLocal = !!(window.location.protocol == 'file:');
|
|
|
|
|
2006-11-18 14:37:01 +01:00
|
|
|
$(function() {
|
|
|
|
$('#userAgent').html(navigator.userAgent);
|
2007-06-15 00:12:49 +02:00
|
|
|
runTest();
|
2006-11-18 14:37:01 +01:00
|
|
|
});
|
2006-09-19 11:49:22 +02:00
|
|
|
|
|
|
|
function synchronize(callback) {
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.queue[_config.queue.length] = callback;
|
|
|
|
if(!_config.blocking) {
|
2006-09-19 11:49:22 +02:00
|
|
|
process();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function process() {
|
2006-11-18 14:37:01 +01:00
|
|
|
while(_config.queue.length && !_config.blocking) {
|
|
|
|
var call = _config.queue[0];
|
|
|
|
_config.queue = _config.queue.slice(1);
|
2006-09-19 11:49:22 +02:00
|
|
|
call();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-21 16:23:59 +01:00
|
|
|
function stop(allowFailure) {
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.blocking = true;
|
2006-12-21 16:23:59 +01:00
|
|
|
var handler = allowFailure ? start : function() {
|
2006-12-21 14:35:32 +01:00
|
|
|
ok( false, "Test timed out" );
|
|
|
|
start();
|
2006-12-21 16:23:59 +01:00
|
|
|
};
|
2007-08-20 01:37:26 +02:00
|
|
|
// Disabled, caused too many random errors
|
|
|
|
//_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
|
2006-09-19 11:49:22 +02:00
|
|
|
}
|
|
|
|
function start() {
|
2007-08-20 01:37:26 +02:00
|
|
|
// A slight delay, to avoid any current callbacks
|
|
|
|
setTimeout(function(){
|
|
|
|
if(_config.timeout)
|
|
|
|
clearTimeout(_config.timeout);
|
|
|
|
_config.blocking = false;
|
|
|
|
process();
|
|
|
|
}, 13);
|
2006-09-19 11:49:22 +02:00
|
|
|
}
|
|
|
|
|
2008-04-24 23:32:35 +02:00
|
|
|
function validTest( name ) {
|
|
|
|
var filters = _config.filters;
|
|
|
|
if( !filters )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
var i = filters.length,
|
|
|
|
run = false;
|
|
|
|
while( i-- ){
|
|
|
|
var filter = filters[i],
|
|
|
|
not = filter.charAt(0) == '!';
|
|
|
|
if( not )
|
|
|
|
filter = filter.slice(1);
|
|
|
|
if( name.indexOf(filter) != -1 )
|
|
|
|
return !not;
|
|
|
|
if( not )
|
|
|
|
run = true;
|
|
|
|
}
|
|
|
|
return run;
|
2008-04-24 22:08:50 +02:00
|
|
|
}
|
|
|
|
|
2006-11-18 14:37:01 +01:00
|
|
|
function runTest() {
|
|
|
|
_config.blocking = false;
|
|
|
|
var time = new Date();
|
|
|
|
_config.fixture = document.getElementById('main').innerHTML;
|
2006-09-19 11:49:22 +02:00
|
|
|
synchronize(function() {
|
2006-11-18 14:37:01 +01:00
|
|
|
time = new Date() - time;
|
|
|
|
$("<div>").html(['<p class="result">Tests completed in ',
|
|
|
|
time, ' milliseconds.<br/>',
|
|
|
|
_config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
|
|
|
|
.join(''))
|
|
|
|
.appendTo("body");
|
2006-11-20 21:54:42 +01:00
|
|
|
$("#banner").addClass(_config.stats.bad ? "fail" : "pass");
|
2006-09-19 11:49:22 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2007-01-14 23:36:54 +01:00
|
|
|
function test(name, callback, nowait) {
|
2006-11-18 14:37:01 +01:00
|
|
|
if(_config.currentModule)
|
|
|
|
name = _config.currentModule + " module: " + name;
|
2007-01-14 23:36:54 +01:00
|
|
|
|
2008-04-24 23:32:35 +02:00
|
|
|
if ( !validTest(name) )
|
2007-06-15 00:12:49 +02:00
|
|
|
return;
|
|
|
|
|
2006-09-19 11:49:22 +02:00
|
|
|
synchronize(function() {
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.Test = [];
|
2006-09-19 11:49:22 +02:00
|
|
|
try {
|
|
|
|
callback();
|
|
|
|
} catch(e) {
|
2006-10-03 12:54:55 +02:00
|
|
|
if( typeof console != "undefined" && console.error && console.warn ) {
|
2006-09-19 11:49:22 +02:00
|
|
|
console.error("Test " + name + " died, exception and test follows");
|
|
|
|
console.error(e);
|
|
|
|
console.warn(callback.toString());
|
|
|
|
}
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e ] );
|
2006-09-19 11:49:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
synchronize(function() {
|
|
|
|
reset();
|
|
|
|
|
2007-01-14 23:36:54 +01:00
|
|
|
// don't output pause tests
|
|
|
|
if(nowait) return;
|
|
|
|
|
2006-11-18 14:37:01 +01:00
|
|
|
if(_config.expected && _config.expected != _config.Test.length) {
|
|
|
|
_config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
|
|
|
|
}
|
|
|
|
_config.expected = null;
|
|
|
|
|
2006-09-19 11:49:22 +02:00
|
|
|
var good = 0, bad = 0;
|
|
|
|
var ol = document.createElement("ol");
|
2006-11-03 12:30:57 +01:00
|
|
|
ol.style.display = "none";
|
2006-09-19 11:49:22 +02:00
|
|
|
var li = "", state = "pass";
|
2006-11-18 14:37:01 +01:00
|
|
|
for ( var i = 0; i < _config.Test.length; i++ ) {
|
2006-09-19 11:49:22 +02:00
|
|
|
var li = document.createElement("li");
|
2006-11-18 14:37:01 +01:00
|
|
|
li.className = _config.Test[i][0] ? "pass" : "fail";
|
|
|
|
li.innerHTML = _config.Test[i][1];
|
2006-09-19 11:49:22 +02:00
|
|
|
ol.appendChild( li );
|
|
|
|
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.stats.all++;
|
|
|
|
if ( !_config.Test[i][0] ) {
|
2006-09-19 11:49:22 +02:00
|
|
|
state = "fail";
|
|
|
|
bad++;
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.stats.bad++;
|
2006-09-19 11:49:22 +02:00
|
|
|
} else good++;
|
|
|
|
}
|
|
|
|
|
|
|
|
var li = document.createElement("li");
|
|
|
|
li.className = state;
|
|
|
|
|
2007-06-15 00:12:49 +02:00
|
|
|
var b = document.createElement("strong");
|
2006-11-18 14:37:01 +01:00
|
|
|
b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
|
2006-09-19 11:49:22 +02:00
|
|
|
b.onclick = function(){
|
|
|
|
var n = this.nextSibling;
|
|
|
|
if ( jQuery.css( n, "display" ) == "none" )
|
|
|
|
n.style.display = "block";
|
|
|
|
else
|
|
|
|
n.style.display = "none";
|
|
|
|
};
|
2007-06-20 23:13:56 +02:00
|
|
|
$(b).dblclick(function(event) {
|
2007-06-15 00:12:49 +02:00
|
|
|
var target = jQuery(event.target).filter("strong").clone();
|
|
|
|
if ( target.length ) {
|
|
|
|
target.children().remove();
|
|
|
|
location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text()));
|
|
|
|
}
|
2007-06-20 23:13:56 +02:00
|
|
|
});
|
2006-09-19 11:49:22 +02:00
|
|
|
li.appendChild( b );
|
|
|
|
li.appendChild( ol );
|
|
|
|
|
|
|
|
document.getElementById("tests").appendChild( li );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2006-11-18 14:37:01 +01:00
|
|
|
// call on start of module test to prepend name to all tests
|
|
|
|
function module(moduleName) {
|
|
|
|
_config.currentModule = moduleName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
|
|
|
|
*/
|
|
|
|
function expect(asserts) {
|
|
|
|
_config.expected = asserts;
|
|
|
|
}
|
|
|
|
|
2006-09-19 11:49:22 +02:00
|
|
|
/**
|
|
|
|
* Resets the test setup. Useful for tests that modify the DOM.
|
|
|
|
*/
|
|
|
|
function reset() {
|
2007-09-09 01:31:23 +02:00
|
|
|
$("#main").html( _config.fixture );
|
2006-09-19 11:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts true.
|
|
|
|
* @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
|
|
|
|
*/
|
|
|
|
function ok(a, msg) {
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.Test.push( [ !!a, msg ] );
|
2006-09-19 11:49:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that two arrays are the same
|
|
|
|
*/
|
|
|
|
function isSet(a, b, msg) {
|
|
|
|
var ret = true;
|
2007-04-29 20:39:07 +02:00
|
|
|
if ( a && b && a.length != undefined && a.length == b.length ) {
|
2007-03-24 22:55:03 +01:00
|
|
|
for ( var i = 0; i < a.length; i++ )
|
2006-09-19 11:49:22 +02:00
|
|
|
if ( a[i] != b[i] )
|
|
|
|
ret = false;
|
|
|
|
} else
|
|
|
|
ret = false;
|
2006-10-08 18:19:51 +02:00
|
|
|
if ( !ret )
|
2007-03-24 22:55:03 +01:00
|
|
|
_config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
|
2006-10-08 18:19:51 +02:00
|
|
|
else
|
2006-11-18 14:37:01 +01:00
|
|
|
_config.Test.push( [ ret, msg ] );
|
2006-09-19 11:49:22 +02:00
|
|
|
}
|
|
|
|
|
2007-04-29 20:39:07 +02:00
|
|
|
/**
|
|
|
|
* Asserts that two objects are equivalent
|
|
|
|
*/
|
|
|
|
function isObj(a, b, msg) {
|
|
|
|
var ret = true;
|
|
|
|
|
|
|
|
if ( a && b ) {
|
|
|
|
for ( var i in a )
|
|
|
|
if ( a[i] != b[i] )
|
|
|
|
ret = false;
|
|
|
|
|
|
|
|
for ( i in b )
|
|
|
|
if ( a[i] != b[i] )
|
|
|
|
ret = false;
|
|
|
|
} else
|
|
|
|
ret = false;
|
|
|
|
|
|
|
|
_config.Test.push( [ ret, msg ] );
|
|
|
|
}
|
|
|
|
|
2007-03-24 22:55:03 +01:00
|
|
|
function serialArray( a ) {
|
|
|
|
var r = [];
|
2007-04-29 20:39:07 +02:00
|
|
|
|
|
|
|
if ( a && a.length )
|
|
|
|
for ( var i = 0; i < a.length; i++ ) {
|
|
|
|
var str = a[i].nodeName;
|
|
|
|
if ( str ) {
|
|
|
|
str = str.toLowerCase();
|
|
|
|
if ( a[i].id )
|
|
|
|
str += "#" + a[i].id;
|
|
|
|
} else
|
|
|
|
str = a[i];
|
|
|
|
r.push( str );
|
|
|
|
}
|
2007-03-24 22:55:03 +01:00
|
|
|
|
2008-04-24 23:32:35 +02:00
|
|
|
return "[ " + r.join(", ") + " ]";
|
2007-03-24 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2006-09-19 11:49:22 +02:00
|
|
|
/**
|
|
|
|
* Returns an array of elements with the given IDs, eg.
|
|
|
|
* @example q("main", "foo", "bar")
|
|
|
|
* @result [<div id="main">, <span id="foo">, <input id="bar">]
|
|
|
|
*/
|
|
|
|
function q() {
|
|
|
|
var r = [];
|
|
|
|
for ( var i = 0; i < arguments.length; i++ )
|
|
|
|
r.push( document.getElementById( arguments[i] ) );
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a select matches the given IDs
|
|
|
|
* @example t("Check for something", "//[a]", ["foo", "baar"]);
|
|
|
|
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
|
|
|
|
*/
|
|
|
|
function t(a,b,c) {
|
2007-03-24 22:55:03 +01:00
|
|
|
var f = jQuery(b);
|
2006-09-19 11:49:22 +02:00
|
|
|
var s = "";
|
|
|
|
for ( var i = 0; i < f.length; i++ )
|
|
|
|
s += (s && ",") + '"' + f[i].id + '"';
|
|
|
|
isSet(f, q.apply(q,c), a + " (" + b + ")");
|
2006-12-31 16:09:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add random number to url to stop IE from caching
|
|
|
|
*
|
|
|
|
* @example url("data/test.html")
|
|
|
|
* @result "data/test.html?10538358428943"
|
|
|
|
*
|
|
|
|
* @example url("data/test.php?foo=bar")
|
|
|
|
* @result "data/test.php?foo=bar&10538358345554"
|
|
|
|
*/
|
|
|
|
function url(value) {
|
|
|
|
return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
|
2007-02-06 12:55:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that the first two arguments are equal, with an optional message.
|
|
|
|
* Prints out both expected and actual values on failure.
|
|
|
|
*
|
|
|
|
* Prefered to ok( expected == actual, message )
|
|
|
|
*
|
|
|
|
* @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
|
|
|
|
*
|
|
|
|
* @param Object actual
|
2007-12-04 17:42:42 +01:00
|
|
|
* @param Object expected
|
2007-02-06 12:55:44 +01:00
|
|
|
* @param String message (optional)
|
|
|
|
*/
|
2007-08-20 01:37:26 +02:00
|
|
|
function equals(actual, expected, message) {
|
2007-02-06 12:55:44 +01:00
|
|
|
var result = expected == actual;
|
2007-03-25 20:06:18 +02:00
|
|
|
message = message || (result ? "okay" : "failed");
|
2007-02-06 12:55:44 +01:00
|
|
|
_config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
|
|
|
|
}
|
2007-03-25 20:06:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger an event on an element.
|
|
|
|
*
|
|
|
|
* @example triggerEvent( document.body, "click" );
|
|
|
|
*
|
|
|
|
* @param DOMElement elem
|
|
|
|
* @param String type
|
|
|
|
*/
|
|
|
|
function triggerEvent( elem, type, event ) {
|
2007-03-26 04:06:50 +02:00
|
|
|
if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
|
2007-03-25 20:06:18 +02:00
|
|
|
event = document.createEvent("MouseEvents");
|
|
|
|
event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
|
|
|
|
0, 0, 0, 0, 0, false, false, false, false, 0, null);
|
|
|
|
elem.dispatchEvent( event );
|
2007-03-26 04:06:50 +02:00
|
|
|
} else if ( jQuery.browser.msie ) {
|
|
|
|
elem.fireEvent("on"+type);
|
2007-03-25 20:06:18 +02:00
|
|
|
}
|
|
|
|
}
|