Merge "Make a VirtualRESTService class for Parsoid"
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 /*global CompletenessTest, sinon */
2 /*jshint evil: true */
3 ( function ( $, mw, QUnit ) {
4 'use strict';
5
6 var mwTestIgnore, mwTester,
7 addons,
8 ELEMENT_NODE = 1,
9 TEXT_NODE = 3;
10
11 /**
12 * Add bogus to url to prevent IE crazy caching
13 *
14 * @param value {String} a relative path (eg. 'data/foo.js'
15 * or 'data/test.php?foo=bar').
16 * @return {String} Such as 'data/foo.js?131031765087663960'
17 */
18 QUnit.fixurl = function ( value ) {
19 return value + (/\?/.test( value ) ? '&' : '?')
20 + String( new Date().getTime() )
21 + String( parseInt( Math.random() * 100000, 10 ) );
22 };
23
24 /**
25 * Configuration
26 */
27
28 // When a test() indicates asynchronicity with stop(),
29 // allow 30 seconds to pass before killing the test(),
30 // and assuming failure.
31 QUnit.config.testTimeout = 30 * 1000;
32
33 QUnit.config.requireExpects = true;
34
35 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
36 QUnit.config.urlConfig.push( {
37 id: 'debug',
38 label: 'Enable ResourceLoaderDebug',
39 tooltip: 'Enable debug mode in ResourceLoader'
40 } );
41
42 /**
43 * CompletenessTest
44 *
45 * Adds toggle checkbox to header
46 */
47 QUnit.config.urlConfig.push( {
48 id: 'completenesstest',
49 label: 'Run CompletenessTest',
50 tooltip: 'Run the completeness test'
51 } );
52
53 /**
54 * SinonJS
55 *
56 * Glue code for nicer integration with QUnit setup/teardown
57 * Inspired by http://sinonjs.org/releases/sinon-qunit-1.0.0.js
58 * Fixes:
59 * - Work properly with asynchronous QUnit by using module setup/teardown
60 * instead of synchronously wrapping QUnit.test.
61 */
62 sinon.assert.fail = function ( msg ) {
63 QUnit.assert.ok( false, msg );
64 };
65 sinon.assert.pass = function ( msg ) {
66 QUnit.assert.ok( true, msg );
67 };
68 sinon.config = {
69 injectIntoThis: true,
70 injectInto: null,
71 properties: ['spy', 'stub', 'mock', 'sandbox'],
72 // Don't fake timers by default
73 useFakeTimers: false,
74 useFakeServer: false
75 };
76 ( function () {
77 var orgModule = QUnit.module;
78
79 QUnit.module = function ( name, localEnv ) {
80 localEnv = localEnv || {};
81 orgModule( name, {
82 setup: function () {
83 var config = sinon.getConfig( sinon.config );
84 config.injectInto = this;
85 sinon.sandbox.create( config );
86
87 if ( localEnv.setup ) {
88 localEnv.setup.call( this );
89 }
90 },
91 teardown: function () {
92 this.sandbox.verifyAndRestore();
93
94 if ( localEnv.teardown ) {
95 localEnv.teardown.call( this );
96 }
97 }
98 } );
99 };
100 }() );
101
102 // Extend QUnit.module to provide a fixture element.
103 ( function () {
104 var orgModule = QUnit.module;
105
106 QUnit.module = function ( name, localEnv ) {
107 var fixture;
108 localEnv = localEnv || {};
109 orgModule( name, {
110 setup: function () {
111 fixture = document.createElement( 'div' );
112 fixture.id = 'qunit-fixture';
113 document.body.appendChild( fixture );
114
115 if ( localEnv.setup ) {
116 localEnv.setup.call( this );
117 }
118 },
119 teardown: function () {
120 if ( localEnv.teardown ) {
121 localEnv.teardown.call( this );
122 }
123
124 fixture.parentNode.removeChild( fixture );
125 }
126 } );
127 };
128 }() );
129
130 // Initiate when enabled
131 if ( QUnit.urlParams.completenesstest ) {
132
133 // Return true to ignore
134 mwTestIgnore = function ( val, tester ) {
135
136 // Don't record methods of the properties of constructors,
137 // to avoid getting into a loop (prototype.constructor.prototype..).
138 // Since we're therefor skipping any injection for
139 // "new mw.Foo()", manually set it to true here.
140 if ( val instanceof mw.Map ) {
141 tester.methodCallTracker.Map = true;
142 return true;
143 }
144 if ( val instanceof mw.Title ) {
145 tester.methodCallTracker.Title = true;
146 return true;
147 }
148
149 // Don't record methods of the properties of a jQuery object
150 if ( val instanceof $ ) {
151 return true;
152 }
153
154 // Don't iterate over the module registry (the 'script' references would
155 // be listed as untested methods otherwise)
156 if ( val === mw.loader.moduleRegistry ) {
157 return true;
158 }
159
160 return false;
161 };
162
163 mwTester = new CompletenessTest( mw, mwTestIgnore );
164 }
165
166 /**
167 * Test environment recommended for all QUnit test modules
168 *
169 * Whether to log environment changes to the console
170 */
171 QUnit.config.urlConfig.push( 'mwlogenv' );
172
173 /**
174 * Reset mw.config and others to a fresh copy of the live config for each test(),
175 * and restore it back to the live one afterwards.
176 * @param localEnv {Object} [optional]
177 * @example (see test suite at the bottom of this file)
178 * </code>
179 */
180 QUnit.newMwEnvironment = ( function () {
181 var warn, log, liveConfig, liveMessages;
182
183 liveConfig = mw.config.values;
184 liveMessages = mw.messages.values;
185
186 function suppressWarnings() {
187 warn = mw.log.warn;
188 mw.log.warn = $.noop;
189 }
190
191 function restoreWarnings() {
192 if ( warn !== undefined ) {
193 mw.log.warn = warn;
194 warn = undefined;
195 }
196 }
197
198 function freshConfigCopy( custom ) {
199 var copy;
200 // Tests should mock all factors that directly influence the tested code.
201 // For backwards compatibility though we set mw.config to a fresh copy of the live
202 // config. This way any modifications made to mw.config during the test will not
203 // affect other tests, nor the global scope outside the test runner.
204 // This is a shallow copy, since overriding an array or object value via "custom"
205 // should replace it. Setting a config property means you override it, not extend it.
206 // NOTE: It is important that we suppress warnings because extend() will also access
207 // deprecated properties and trigger deprecation warnings from mw.log#deprecate.
208 suppressWarnings();
209 copy = $.extend( {}, liveConfig, custom );
210 restoreWarnings();
211
212 return copy;
213 }
214
215 function freshMessagesCopy( custom ) {
216 return $.extend( /*deep=*/true, {}, liveMessages, custom );
217 }
218
219 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
220
221 return function ( localEnv ) {
222 localEnv = $.extend( {
223 // QUnit
224 setup: $.noop,
225 teardown: $.noop,
226 // MediaWiki
227 config: {},
228 messages: {}
229 }, localEnv );
230
231 return {
232 setup: function () {
233 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
234 + ': ' + QUnit.config.current.testName + '"' );
235
236 // Greetings, mock environment!
237 mw.config.values = freshConfigCopy( localEnv.config );
238 mw.messages.values = freshMessagesCopy( localEnv.messages );
239 this.suppressWarnings = suppressWarnings;
240 this.restoreWarnings = restoreWarnings;
241
242 localEnv.setup.call( this );
243 },
244
245 teardown: function () {
246 var timers;
247 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
248 + ': ' + QUnit.config.current.testName + '"' );
249
250 localEnv.teardown.call( this );
251
252 // Farewell, mock environment!
253 mw.config.values = liveConfig;
254 mw.messages.values = liveMessages;
255
256 // As a convenience feature, automatically restore warnings if they're
257 // still suppressed by the end of the test.
258 restoreWarnings();
259
260 // Check for incomplete animations/requests/etc and throw
261 // error if there are any.
262 if ( $.timers && $.timers.length !== 0 ) {
263 timers = $.timers.length;
264 // Tests shoulld use fake timers or wait for animations to complete
265 $.each( $.timers, function ( i, timer ) {
266 var node = timer.elem;
267 mw.log.warn( 'Unfinished animation #' + i + ' in ' + timer.queue + ' queue on ' +
268 mw.html.element( node.nodeName.toLowerCase(), $(node).getAttrs() )
269 );
270 } );
271 // Force animations to stop to give the next test a clean start
272 $.fx.stop();
273
274 throw new Error( 'Unfinished animations: ' + timers );
275 }
276 if ( $.active !== undefined && $.active !== 0 ) {
277 // Test may need to use fake XHR, wait for requests or
278 // call abort().
279 throw new Error( 'Unfinished AJAX requests: ' + $.active );
280 }
281 }
282 };
283 };
284 }() );
285
286 // $.when stops as soon as one fails, which makes sense in most
287 // practical scenarios, but not in a unit test where we really do
288 // need to wait until all of them are finished.
289 QUnit.whenPromisesComplete = function () {
290 var altPromises = [];
291
292 $.each( arguments, function ( i, arg ) {
293 var alt = $.Deferred();
294 altPromises.push( alt );
295
296 // Whether this one fails or not, forwards it to
297 // the 'done' (resolve) callback of the alternative promise.
298 arg.always( alt.resolve );
299 } );
300
301 return $.when.apply( $, altPromises );
302 };
303
304 /**
305 * Recursively convert a node to a plain object representing its structure.
306 * Only considers attributes and contents (elements and text nodes).
307 * Attribute values are compared strictly and not normalised.
308 *
309 * @param {Node} node
310 * @return {Object|string} Plain JavaScript value representing the node.
311 */
312 function getDomStructure( node ) {
313 var $node, children, processedChildren, i, len, el;
314 $node = $( node );
315 if ( node.nodeType === ELEMENT_NODE ) {
316 children = $node.contents();
317 processedChildren = [];
318 for ( i = 0, len = children.length; i < len; i++ ) {
319 el = children[i];
320 if ( el.nodeType === ELEMENT_NODE || el.nodeType === TEXT_NODE ) {
321 processedChildren.push( getDomStructure( el ) );
322 }
323 }
324
325 return {
326 tagName: node.tagName,
327 attributes: $node.getAttrs(),
328 contents: processedChildren
329 };
330 } else {
331 // Should be text node
332 return $node.text();
333 }
334 }
335
336 /**
337 * Gets structure of node for this HTML.
338 *
339 * @param {string} html HTML markup for one or more nodes.
340 */
341 function getHtmlStructure( html ) {
342 var el = $( '<div>' ).append( html )[0];
343 return getDomStructure( el );
344 }
345
346 /**
347 * Add-on assertion helpers
348 */
349 // Define the add-ons
350 addons = {
351
352 // Expect boolean true
353 assertTrue: function ( actual, message ) {
354 QUnit.push( actual === true, actual, true, message );
355 },
356
357 // Expect boolean false
358 assertFalse: function ( actual, message ) {
359 QUnit.push( actual === false, actual, false, message );
360 },
361
362 // Expect numerical value less than X
363 lt: function ( actual, expected, message ) {
364 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
365 },
366
367 // Expect numerical value less than or equal to X
368 ltOrEq: function ( actual, expected, message ) {
369 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
370 },
371
372 // Expect numerical value greater than X
373 gt: function ( actual, expected, message ) {
374 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
375 },
376
377 // Expect numerical value greater than or equal to X
378 gtOrEq: function ( actual, expected, message ) {
379 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
380 },
381
382 /**
383 * Asserts that two HTML strings are structurally equivalent.
384 *
385 * @param {string} actualHtml Actual HTML markup.
386 * @param {string} expectedHtml Expected HTML markup
387 * @param {string} message Assertion message.
388 */
389 htmlEqual: function ( actualHtml, expectedHtml, message ) {
390 var actual = getHtmlStructure( actualHtml ),
391 expected = getHtmlStructure( expectedHtml );
392
393 QUnit.push(
394 QUnit.equiv(
395 actual,
396 expected
397 ),
398 actual,
399 expected,
400 message
401 );
402 },
403
404 /**
405 * Asserts that two HTML strings are not structurally equivalent.
406 *
407 * @param {string} actualHtml Actual HTML markup.
408 * @param {string} expectedHtml Expected HTML markup.
409 * @param {string} message Assertion message.
410 */
411 notHtmlEqual: function ( actualHtml, expectedHtml, message ) {
412 var actual = getHtmlStructure( actualHtml ),
413 expected = getHtmlStructure( expectedHtml );
414
415 QUnit.push(
416 !QUnit.equiv(
417 actual,
418 expected
419 ),
420 actual,
421 expected,
422 message
423 );
424 }
425 };
426
427 $.extend( QUnit.assert, addons );
428
429 /**
430 * Small test suite to confirm proper functionality of the utilities and
431 * initializations defined above in this file.
432 */
433 QUnit.module( 'test.mediawiki.qunit.testrunner', QUnit.newMwEnvironment( {
434 setup: function () {
435 this.mwHtmlLive = mw.html;
436 mw.html = {
437 escape: function () {
438 return 'mocked';
439 }
440 };
441 },
442 teardown: function () {
443 mw.html = this.mwHtmlLive;
444 },
445 config: {
446 testVar: 'foo'
447 },
448 messages: {
449 testMsg: 'Foo.'
450 }
451 } ) );
452
453 QUnit.test( 'Setup', 3, function ( assert ) {
454 assert.equal( mw.html.escape( 'foo' ), 'mocked', 'setup() callback was ran.' );
455 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
456 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
457
458 mw.config.set( 'testVar', 'bar' );
459 mw.messages.set( 'testMsg', 'Bar.' );
460 } );
461
462 QUnit.test( 'Teardown', 2, function ( assert ) {
463 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
464 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
465 } );
466
467 QUnit.test( 'Loader status', 2, function ( assert ) {
468 var i, len, state,
469 modules = mw.loader.getModuleNames(),
470 error = [],
471 missing = [];
472
473 for ( i = 0, len = modules.length; i < len; i++ ) {
474 state = mw.loader.getState( modules[i] );
475 if ( state === 'error' ) {
476 error.push( modules[i] );
477 } else if ( state === 'missing' ) {
478 missing.push( modules[i] );
479 }
480 }
481
482 assert.deepEqual( error, [], 'Modules in error state' );
483 assert.deepEqual( missing, [], 'Modules in missing state' );
484 } );
485
486 QUnit.test( 'htmlEqual', 8, function ( assert ) {
487 assert.htmlEqual(
488 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
489 '<div><p data-length=\'10\' class=\'some classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
490 'Attribute order, spacing and quotation marks (equal)'
491 );
492
493 assert.notHtmlEqual(
494 '<div><p class="some classes" data-length="10">Child paragraph with <a href="http://example.com">A link</a></p>Regular text<span>A span</span></div>',
495 '<div><p data-length=\'10\' class=\'some more classes\'>Child paragraph with <a href=\'http://example.com\' >A link</a></p>Regular text<span>A span</span></div>',
496 'Attribute order, spacing and quotation marks (not equal)'
497 );
498
499 assert.htmlEqual(
500 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
501 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
502 'Multiple root nodes (equal)'
503 );
504
505 assert.notHtmlEqual(
506 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="minor">Last</label><input id="lastname" />',
507 '<label for="firstname" accesskey="f" class="important">First</label><input id="firstname" /><label for="lastname" accesskey="l" class="important" >Last</label><input id="lastname" />',
508 'Multiple root nodes (not equal, last label node is different)'
509 );
510
511 assert.htmlEqual(
512 'fo&quot;o<br/>b&gt;ar',
513 'fo"o<br/>b>ar',
514 'Extra escaping is equal'
515 );
516 assert.notHtmlEqual(
517 'foo&lt;br/&gt;bar',
518 'foo<br/>bar',
519 'Text escaping (not equal)'
520 );
521
522 assert.htmlEqual(
523 'foo<a href="http://example.com">example</a>bar',
524 'foo<a href="http://example.com">example</a>bar',
525 'Outer text nodes are compared (equal)'
526 );
527
528 assert.notHtmlEqual(
529 'foo<a href="http://example.com">example</a>bar',
530 'foo<a href="http://example.com">example</a>quux',
531 'Outer text nodes are compared (last text node different)'
532 );
533
534 } );
535
536 QUnit.module( 'test.mediawiki.qunit.testrunner-after', QUnit.newMwEnvironment() );
537
538 QUnit.test( 'Teardown', 3, function ( assert ) {
539 assert.equal( mw.html.escape( '<' ), '&lt;', 'teardown() callback was ran.' );
540 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
541 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
542 } );
543
544 }( jQuery, mediaWiki, QUnit ) );