1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/tests/mochitest/ajax/jquery/test/data/testrunner.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,334 @@ 1.4 +var _config = { 1.5 + fixture: null, 1.6 + Test: [], 1.7 + stats: { 1.8 + all: 0, 1.9 + bad: 0 1.10 + }, 1.11 + queue: [], 1.12 + blocking: true, 1.13 + timeout: null, 1.14 + expected: null, 1.15 + currentModule: null, 1.16 + asyncTimeout: 2 // seconds for async timeout 1.17 +}; 1.18 + 1.19 +_config.filters = location.search.length > 1 && //restrict modules/tests by get parameters 1.20 + $.map( location.search.slice(1).split('&'), decodeURIComponent ); 1.21 + 1.22 +var isLocal = !!(window.location.protocol == 'file:'); 1.23 + 1.24 +$(function() { 1.25 + $('#userAgent').html(navigator.userAgent); 1.26 + runTest(); 1.27 +}); 1.28 + 1.29 +function synchronize(callback) { 1.30 + _config.queue[_config.queue.length] = callback; 1.31 + if(!_config.blocking) { 1.32 + process(); 1.33 + } 1.34 +} 1.35 + 1.36 +function process() { 1.37 + while(_config.queue.length && !_config.blocking) { 1.38 + var call = _config.queue[0]; 1.39 + _config.queue = _config.queue.slice(1); 1.40 + call(); 1.41 + } 1.42 +} 1.43 + 1.44 +function stop(allowFailure) { 1.45 + _config.blocking = true; 1.46 + var handler = allowFailure ? start : function() { 1.47 + ok( false, "Test timed out" ); 1.48 + start(); 1.49 + }; 1.50 + // Disabled, caused too many random errors 1.51 + //_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000); 1.52 +} 1.53 +function start() { 1.54 + // A slight delay, to avoid any current callbacks 1.55 + setTimeout(function(){ 1.56 + if(_config.timeout) 1.57 + clearTimeout(_config.timeout); 1.58 + _config.blocking = false; 1.59 + process(); 1.60 + }, 13); 1.61 +} 1.62 + 1.63 +function validTest( name ) { 1.64 + var filters = _config.filters; 1.65 + if( !filters ) 1.66 + return true; 1.67 + 1.68 + var i = filters.length, 1.69 + run = false; 1.70 + while( i-- ){ 1.71 + var filter = filters[i], 1.72 + not = filter.charAt(0) == '!'; 1.73 + if( not ) 1.74 + filter = filter.slice(1); 1.75 + if( name.indexOf(filter) != -1 ) 1.76 + return !not; 1.77 + if( not ) 1.78 + run = true; 1.79 + } 1.80 + return run; 1.81 +} 1.82 + 1.83 +function runTest() { 1.84 + _config.blocking = false; 1.85 + var time = new Date(); 1.86 + _config.fixture = document.getElementById('main').innerHTML; 1.87 + _config.ajaxSettings = $.ajaxSettings; 1.88 + synchronize(function() { 1.89 + time = new Date() - time; 1.90 + $("<div>").html(['<p class="result">Tests completed in ', 1.91 + time, ' milliseconds.<br/>', 1.92 + _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>'] 1.93 + .join('')) 1.94 + .appendTo("body"); 1.95 + $("#banner").addClass(_config.stats.bad ? "fail" : "pass"); 1.96 + if ( parent.runAJAXTest ) 1.97 + parent.runAJAXTest(); 1.98 + 1.99 + }); 1.100 +} 1.101 + 1.102 +function test(name, callback, nowait) { 1.103 + if(_config.currentModule) 1.104 + name = _config.currentModule + " module: " + name; 1.105 + 1.106 + if ( !validTest(name) ) 1.107 + return; 1.108 + 1.109 + synchronize(function() { 1.110 + _config.Test = []; 1.111 + try { 1.112 + callback(); 1.113 + } catch(e) { 1.114 + if( typeof console != "undefined" && console.error && console.warn ) { 1.115 + console.error("Test " + name + " died, exception and test follows"); 1.116 + console.error(e); 1.117 + console.warn(callback.toString()); 1.118 + } 1.119 + _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e.message ] ); 1.120 + } 1.121 + }); 1.122 + synchronize(function() { 1.123 + reset(); 1.124 + 1.125 + // don't output pause tests 1.126 + if(nowait) return; 1.127 + 1.128 + if(_config.expected && _config.expected != _config.Test.length) { 1.129 + _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] ); 1.130 + } 1.131 + _config.expected = null; 1.132 + 1.133 + var good = 0, bad = 0; 1.134 + var ol = document.createElement("ol"); 1.135 + ol.style.display = "none"; 1.136 + var li = "", state = "pass"; 1.137 + for ( var i = 0; i < _config.Test.length; i++ ) { 1.138 + var li = document.createElement("li"); 1.139 + li.className = _config.Test[i][0] ? "pass" : "fail"; 1.140 + li.innerHTML = _config.Test[i][1]; 1.141 + ol.appendChild( li ); 1.142 + 1.143 + _config.stats.all++; 1.144 + if ( !_config.Test[i][0] ) { 1.145 + state = "fail"; 1.146 + bad++; 1.147 + _config.stats.bad++; 1.148 + } else good++; 1.149 + 1.150 + if ( parent.SimpleTest ){ 1.151 + parent.SimpleTest.ok( _config.Test[i][0], name, _config.Test[i][1] );} 1.152 + } 1.153 + 1.154 + var li = document.createElement("li"); 1.155 + li.className = state; 1.156 + 1.157 + var b = document.createElement("strong"); 1.158 + b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>"; 1.159 + b.onclick = function(){ 1.160 + var n = this.nextSibling; 1.161 + if ( jQuery.css( n, "display" ) == "none" ) 1.162 + n.style.display = "block"; 1.163 + else 1.164 + n.style.display = "none"; 1.165 + }; 1.166 + $(b).dblclick(function(event) { 1.167 + var target = jQuery(event.target).filter("strong").clone(); 1.168 + if ( target.length ) { 1.169 + target.children().remove(); 1.170 + location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text())); 1.171 + } 1.172 + }); 1.173 + li.appendChild( b ); 1.174 + li.appendChild( ol ); 1.175 + 1.176 + document.getElementById("tests").appendChild( li ); 1.177 + }); 1.178 +} 1.179 + 1.180 +// call on start of module test to prepend name to all tests 1.181 +function module(moduleName) { 1.182 + _config.currentModule = moduleName; 1.183 +} 1.184 + 1.185 +/** 1.186 + * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through. 1.187 + */ 1.188 +function expect(asserts) { 1.189 + _config.expected = asserts; 1.190 +} 1.191 + 1.192 +/** 1.193 + * Resets the test setup. Useful for tests that modify the DOM. 1.194 + */ 1.195 +function reset() { 1.196 + $("#main").html( _config.fixture ); 1.197 + $.event.global = {}; 1.198 + $.ajaxSettings = $.extend({}, _config.ajaxSettings); 1.199 +} 1.200 + 1.201 +/** 1.202 + * Asserts true. 1.203 + * @example ok( $("a").size() > 5, "There must be at least 5 anchors" ); 1.204 + */ 1.205 +function ok(a, msg) { 1.206 + _config.Test.push( [ !!a, msg ] ); 1.207 +} 1.208 + 1.209 +/** 1.210 + * Asserts that two arrays are the same 1.211 + */ 1.212 +function isSet(a, b, msg) { 1.213 + var ret = true; 1.214 + if ( a && b && a.length != undefined && a.length == b.length ) { 1.215 + for ( var i = 0; i < a.length; i++ ) 1.216 + if ( a[i] != b[i] ) 1.217 + ret = false; 1.218 + } else 1.219 + ret = false; 1.220 + if ( !ret ) 1.221 + _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] ); 1.222 + else 1.223 + _config.Test.push( [ ret, msg ] ); 1.224 +} 1.225 + 1.226 +/** 1.227 + * Asserts that two objects are equivalent 1.228 + */ 1.229 +function isObj(a, b, msg) { 1.230 + var ret = true; 1.231 + 1.232 + if ( a && b ) { 1.233 + for ( var i in a ) 1.234 + if ( a[i] != b[i] ) 1.235 + ret = false; 1.236 + 1.237 + for ( i in b ) 1.238 + if ( a[i] != b[i] ) 1.239 + ret = false; 1.240 + } else 1.241 + ret = false; 1.242 + 1.243 + _config.Test.push( [ ret, msg ] ); 1.244 +} 1.245 + 1.246 +function serialArray( a ) { 1.247 + var r = []; 1.248 + 1.249 + if ( a && a.length ) 1.250 + for ( var i = 0; i < a.length; i++ ) { 1.251 + var str = a[i].nodeName; 1.252 + if ( str ) { 1.253 + str = str.toLowerCase(); 1.254 + if ( a[i].id ) 1.255 + str += "#" + a[i].id; 1.256 + } else 1.257 + str = a[i]; 1.258 + r.push( str ); 1.259 + } 1.260 + 1.261 + return "[ " + r.join(", ") + " ]"; 1.262 +} 1.263 + 1.264 +/** 1.265 + * Returns an array of elements with the given IDs, eg. 1.266 + * @example q("main", "foo", "bar") 1.267 + * @result [<div id="main">, <span id="foo">, <input id="bar">] 1.268 + */ 1.269 +function q() { 1.270 + var r = []; 1.271 + for ( var i = 0; i < arguments.length; i++ ) 1.272 + r.push( document.getElementById( arguments[i] ) ); 1.273 + return r; 1.274 +} 1.275 + 1.276 +/** 1.277 + * Asserts that a select matches the given IDs 1.278 + * @example t("Check for something", "//[a]", ["foo", "baar"]); 1.279 + * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar' 1.280 + */ 1.281 +function t(a,b,c) { 1.282 + var f = jQuery(b); 1.283 + var s = ""; 1.284 + for ( var i = 0; i < f.length; i++ ) 1.285 + s += (s && ",") + '"' + f[i].id + '"'; 1.286 + isSet(f, q.apply(q,c), a + " (" + b + ")"); 1.287 +} 1.288 + 1.289 +/** 1.290 + * Add random number to url to stop IE from caching 1.291 + * 1.292 + * @example url("data/test.html") 1.293 + * @result "data/test.html?10538358428943" 1.294 + * 1.295 + * @example url("data/test.php?foo=bar") 1.296 + * @result "data/test.php?foo=bar&10538358345554" 1.297 + */ 1.298 +function url(value) { 1.299 + return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000); 1.300 +} 1.301 + 1.302 +/** 1.303 + * Checks that the first two arguments are equal, with an optional message. 1.304 + * Prints out both expected and actual values on failure. 1.305 + * 1.306 + * Prefered to ok( expected == actual, message ) 1.307 + * 1.308 + * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) ); 1.309 + * 1.310 + * @param Object actual 1.311 + * @param Object expected 1.312 + * @param String message (optional) 1.313 + */ 1.314 +function equals(actual, expected, message) { 1.315 + var result = expected == actual; 1.316 + message = message || (result ? "okay" : "failed"); 1.317 + _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] ); 1.318 +} 1.319 + 1.320 +/** 1.321 + * Trigger an event on an element. 1.322 + * 1.323 + * @example triggerEvent( document.body, "click" ); 1.324 + * 1.325 + * @param DOMElement elem 1.326 + * @param String type 1.327 + */ 1.328 +function triggerEvent( elem, type, event ) { 1.329 + if ( jQuery.browser.mozilla || jQuery.browser.opera ) { 1.330 + event = document.createEvent("MouseEvents"); 1.331 + event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView, 1.332 + 0, 0, 0, 0, 0, false, false, false, false, 0, null); 1.333 + elem.dispatchEvent( event ); 1.334 + } else if ( jQuery.browser.msie ) { 1.335 + elem.fireEvent("on"+type); 1.336 + } 1.337 +} 1.338 \ No newline at end of file