dom/tests/mochitest/ajax/jquery/test/data/testrunner.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 var _config = {
michael@0 2 fixture: null,
michael@0 3 Test: [],
michael@0 4 stats: {
michael@0 5 all: 0,
michael@0 6 bad: 0
michael@0 7 },
michael@0 8 queue: [],
michael@0 9 blocking: true,
michael@0 10 timeout: null,
michael@0 11 expected: null,
michael@0 12 currentModule: null,
michael@0 13 asyncTimeout: 2 // seconds for async timeout
michael@0 14 };
michael@0 15
michael@0 16 _config.filters = location.search.length > 1 && //restrict modules/tests by get parameters
michael@0 17 $.map( location.search.slice(1).split('&'), decodeURIComponent );
michael@0 18
michael@0 19 var isLocal = !!(window.location.protocol == 'file:');
michael@0 20
michael@0 21 $(function() {
michael@0 22 $('#userAgent').html(navigator.userAgent);
michael@0 23 runTest();
michael@0 24 });
michael@0 25
michael@0 26 function synchronize(callback) {
michael@0 27 _config.queue[_config.queue.length] = callback;
michael@0 28 if(!_config.blocking) {
michael@0 29 process();
michael@0 30 }
michael@0 31 }
michael@0 32
michael@0 33 function process() {
michael@0 34 while(_config.queue.length && !_config.blocking) {
michael@0 35 var call = _config.queue[0];
michael@0 36 _config.queue = _config.queue.slice(1);
michael@0 37 call();
michael@0 38 }
michael@0 39 }
michael@0 40
michael@0 41 function stop(allowFailure) {
michael@0 42 _config.blocking = true;
michael@0 43 var handler = allowFailure ? start : function() {
michael@0 44 ok( false, "Test timed out" );
michael@0 45 start();
michael@0 46 };
michael@0 47 // Disabled, caused too many random errors
michael@0 48 //_config.timeout = setTimeout(handler, _config.asyncTimeout * 1000);
michael@0 49 }
michael@0 50 function start() {
michael@0 51 // A slight delay, to avoid any current callbacks
michael@0 52 setTimeout(function(){
michael@0 53 if(_config.timeout)
michael@0 54 clearTimeout(_config.timeout);
michael@0 55 _config.blocking = false;
michael@0 56 process();
michael@0 57 }, 13);
michael@0 58 }
michael@0 59
michael@0 60 function validTest( name ) {
michael@0 61 var filters = _config.filters;
michael@0 62 if( !filters )
michael@0 63 return true;
michael@0 64
michael@0 65 var i = filters.length,
michael@0 66 run = false;
michael@0 67 while( i-- ){
michael@0 68 var filter = filters[i],
michael@0 69 not = filter.charAt(0) == '!';
michael@0 70 if( not )
michael@0 71 filter = filter.slice(1);
michael@0 72 if( name.indexOf(filter) != -1 )
michael@0 73 return !not;
michael@0 74 if( not )
michael@0 75 run = true;
michael@0 76 }
michael@0 77 return run;
michael@0 78 }
michael@0 79
michael@0 80 function runTest() {
michael@0 81 _config.blocking = false;
michael@0 82 var time = new Date();
michael@0 83 _config.fixture = document.getElementById('main').innerHTML;
michael@0 84 _config.ajaxSettings = $.ajaxSettings;
michael@0 85 synchronize(function() {
michael@0 86 time = new Date() - time;
michael@0 87 $("<div>").html(['<p class="result">Tests completed in ',
michael@0 88 time, ' milliseconds.<br/>',
michael@0 89 _config.stats.bad, ' tests of ', _config.stats.all, ' failed.</p>']
michael@0 90 .join(''))
michael@0 91 .appendTo("body");
michael@0 92 $("#banner").addClass(_config.stats.bad ? "fail" : "pass");
michael@0 93 if ( parent.runAJAXTest )
michael@0 94 parent.runAJAXTest();
michael@0 95
michael@0 96 });
michael@0 97 }
michael@0 98
michael@0 99 function test(name, callback, nowait) {
michael@0 100 if(_config.currentModule)
michael@0 101 name = _config.currentModule + " module: " + name;
michael@0 102
michael@0 103 if ( !validTest(name) )
michael@0 104 return;
michael@0 105
michael@0 106 synchronize(function() {
michael@0 107 _config.Test = [];
michael@0 108 try {
michael@0 109 callback();
michael@0 110 } catch(e) {
michael@0 111 if( typeof console != "undefined" && console.error && console.warn ) {
michael@0 112 console.error("Test " + name + " died, exception and test follows");
michael@0 113 console.error(e);
michael@0 114 console.warn(callback.toString());
michael@0 115 }
michael@0 116 _config.Test.push( [ false, "Died on test #" + (_config.Test.length+1) + ": " + e.message ] );
michael@0 117 }
michael@0 118 });
michael@0 119 synchronize(function() {
michael@0 120 reset();
michael@0 121
michael@0 122 // don't output pause tests
michael@0 123 if(nowait) return;
michael@0 124
michael@0 125 if(_config.expected && _config.expected != _config.Test.length) {
michael@0 126 _config.Test.push( [ false, "Expected " + _config.expected + " assertions, but " + _config.Test.length + " were run" ] );
michael@0 127 }
michael@0 128 _config.expected = null;
michael@0 129
michael@0 130 var good = 0, bad = 0;
michael@0 131 var ol = document.createElement("ol");
michael@0 132 ol.style.display = "none";
michael@0 133 var li = "", state = "pass";
michael@0 134 for ( var i = 0; i < _config.Test.length; i++ ) {
michael@0 135 var li = document.createElement("li");
michael@0 136 li.className = _config.Test[i][0] ? "pass" : "fail";
michael@0 137 li.innerHTML = _config.Test[i][1];
michael@0 138 ol.appendChild( li );
michael@0 139
michael@0 140 _config.stats.all++;
michael@0 141 if ( !_config.Test[i][0] ) {
michael@0 142 state = "fail";
michael@0 143 bad++;
michael@0 144 _config.stats.bad++;
michael@0 145 } else good++;
michael@0 146
michael@0 147 if ( parent.SimpleTest ){
michael@0 148 parent.SimpleTest.ok( _config.Test[i][0], name, _config.Test[i][1] );}
michael@0 149 }
michael@0 150
michael@0 151 var li = document.createElement("li");
michael@0 152 li.className = state;
michael@0 153
michael@0 154 var b = document.createElement("strong");
michael@0 155 b.innerHTML = name + " <b style='color:black;'>(<b class='fail'>" + bad + "</b>, <b class='pass'>" + good + "</b>, " + _config.Test.length + ")</b>";
michael@0 156 b.onclick = function(){
michael@0 157 var n = this.nextSibling;
michael@0 158 if ( jQuery.css( n, "display" ) == "none" )
michael@0 159 n.style.display = "block";
michael@0 160 else
michael@0 161 n.style.display = "none";
michael@0 162 };
michael@0 163 $(b).dblclick(function(event) {
michael@0 164 var target = jQuery(event.target).filter("strong").clone();
michael@0 165 if ( target.length ) {
michael@0 166 target.children().remove();
michael@0 167 location.href = location.href.match(/^(.+?)(\?.*)?$/)[1] + "?" + encodeURIComponent($.trim(target.text()));
michael@0 168 }
michael@0 169 });
michael@0 170 li.appendChild( b );
michael@0 171 li.appendChild( ol );
michael@0 172
michael@0 173 document.getElementById("tests").appendChild( li );
michael@0 174 });
michael@0 175 }
michael@0 176
michael@0 177 // call on start of module test to prepend name to all tests
michael@0 178 function module(moduleName) {
michael@0 179 _config.currentModule = moduleName;
michael@0 180 }
michael@0 181
michael@0 182 /**
michael@0 183 * Specify the number of expected assertions to gurantee that failed test (no assertions are run at all) don't slip through.
michael@0 184 */
michael@0 185 function expect(asserts) {
michael@0 186 _config.expected = asserts;
michael@0 187 }
michael@0 188
michael@0 189 /**
michael@0 190 * Resets the test setup. Useful for tests that modify the DOM.
michael@0 191 */
michael@0 192 function reset() {
michael@0 193 $("#main").html( _config.fixture );
michael@0 194 $.event.global = {};
michael@0 195 $.ajaxSettings = $.extend({}, _config.ajaxSettings);
michael@0 196 }
michael@0 197
michael@0 198 /**
michael@0 199 * Asserts true.
michael@0 200 * @example ok( $("a").size() > 5, "There must be at least 5 anchors" );
michael@0 201 */
michael@0 202 function ok(a, msg) {
michael@0 203 _config.Test.push( [ !!a, msg ] );
michael@0 204 }
michael@0 205
michael@0 206 /**
michael@0 207 * Asserts that two arrays are the same
michael@0 208 */
michael@0 209 function isSet(a, b, msg) {
michael@0 210 var ret = true;
michael@0 211 if ( a && b && a.length != undefined && a.length == b.length ) {
michael@0 212 for ( var i = 0; i < a.length; i++ )
michael@0 213 if ( a[i] != b[i] )
michael@0 214 ret = false;
michael@0 215 } else
michael@0 216 ret = false;
michael@0 217 if ( !ret )
michael@0 218 _config.Test.push( [ ret, msg + " expected: " + serialArray(b) + " result: " + serialArray(a) ] );
michael@0 219 else
michael@0 220 _config.Test.push( [ ret, msg ] );
michael@0 221 }
michael@0 222
michael@0 223 /**
michael@0 224 * Asserts that two objects are equivalent
michael@0 225 */
michael@0 226 function isObj(a, b, msg) {
michael@0 227 var ret = true;
michael@0 228
michael@0 229 if ( a && b ) {
michael@0 230 for ( var i in a )
michael@0 231 if ( a[i] != b[i] )
michael@0 232 ret = false;
michael@0 233
michael@0 234 for ( i in b )
michael@0 235 if ( a[i] != b[i] )
michael@0 236 ret = false;
michael@0 237 } else
michael@0 238 ret = false;
michael@0 239
michael@0 240 _config.Test.push( [ ret, msg ] );
michael@0 241 }
michael@0 242
michael@0 243 function serialArray( a ) {
michael@0 244 var r = [];
michael@0 245
michael@0 246 if ( a && a.length )
michael@0 247 for ( var i = 0; i < a.length; i++ ) {
michael@0 248 var str = a[i].nodeName;
michael@0 249 if ( str ) {
michael@0 250 str = str.toLowerCase();
michael@0 251 if ( a[i].id )
michael@0 252 str += "#" + a[i].id;
michael@0 253 } else
michael@0 254 str = a[i];
michael@0 255 r.push( str );
michael@0 256 }
michael@0 257
michael@0 258 return "[ " + r.join(", ") + " ]";
michael@0 259 }
michael@0 260
michael@0 261 /**
michael@0 262 * Returns an array of elements with the given IDs, eg.
michael@0 263 * @example q("main", "foo", "bar")
michael@0 264 * @result [<div id="main">, <span id="foo">, <input id="bar">]
michael@0 265 */
michael@0 266 function q() {
michael@0 267 var r = [];
michael@0 268 for ( var i = 0; i < arguments.length; i++ )
michael@0 269 r.push( document.getElementById( arguments[i] ) );
michael@0 270 return r;
michael@0 271 }
michael@0 272
michael@0 273 /**
michael@0 274 * Asserts that a select matches the given IDs
michael@0 275 * @example t("Check for something", "//[a]", ["foo", "baar"]);
michael@0 276 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
michael@0 277 */
michael@0 278 function t(a,b,c) {
michael@0 279 var f = jQuery(b);
michael@0 280 var s = "";
michael@0 281 for ( var i = 0; i < f.length; i++ )
michael@0 282 s += (s && ",") + '"' + f[i].id + '"';
michael@0 283 isSet(f, q.apply(q,c), a + " (" + b + ")");
michael@0 284 }
michael@0 285
michael@0 286 /**
michael@0 287 * Add random number to url to stop IE from caching
michael@0 288 *
michael@0 289 * @example url("data/test.html")
michael@0 290 * @result "data/test.html?10538358428943"
michael@0 291 *
michael@0 292 * @example url("data/test.php?foo=bar")
michael@0 293 * @result "data/test.php?foo=bar&10538358345554"
michael@0 294 */
michael@0 295 function url(value) {
michael@0 296 return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000);
michael@0 297 }
michael@0 298
michael@0 299 /**
michael@0 300 * Checks that the first two arguments are equal, with an optional message.
michael@0 301 * Prints out both expected and actual values on failure.
michael@0 302 *
michael@0 303 * Prefered to ok( expected == actual, message )
michael@0 304 *
michael@0 305 * @example equals( "Expected 2 characters.", v.formatMessage("Expected {0} characters.", 2) );
michael@0 306 *
michael@0 307 * @param Object actual
michael@0 308 * @param Object expected
michael@0 309 * @param String message (optional)
michael@0 310 */
michael@0 311 function equals(actual, expected, message) {
michael@0 312 var result = expected == actual;
michael@0 313 message = message || (result ? "okay" : "failed");
michael@0 314 _config.Test.push( [ result, result ? message + ": " + expected : message + " expected: " + expected + " actual: " + actual ] );
michael@0 315 }
michael@0 316
michael@0 317 /**
michael@0 318 * Trigger an event on an element.
michael@0 319 *
michael@0 320 * @example triggerEvent( document.body, "click" );
michael@0 321 *
michael@0 322 * @param DOMElement elem
michael@0 323 * @param String type
michael@0 324 */
michael@0 325 function triggerEvent( elem, type, event ) {
michael@0 326 if ( jQuery.browser.mozilla || jQuery.browser.opera ) {
michael@0 327 event = document.createEvent("MouseEvents");
michael@0 328 event.initMouseEvent(type, true, true, elem.ownerDocument.defaultView,
michael@0 329 0, 0, 0, 0, 0, false, false, false, false, 0, null);
michael@0 330 elem.dispatchEvent( event );
michael@0 331 } else if ( jQuery.browser.msie ) {
michael@0 332 elem.fireEvent("on"+type);
michael@0 333 }
michael@0 334 }

mercurial