Merge "(bug 35240) Fix mw.loader state machine."
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 ( function ( $, mw, QUnit, undefined ) {
2 "use strict";
3
4 var mwTestIgnore, mwTester, addons;
5
6 /**
7 * Add bogus to url to prevent IE crazy caching
8 *
9 * @param value {String} a relative path (eg. 'data/foo.js'
10 * or 'data/test.php?foo=bar').
11 * @return {String} Such as 'data/foo.js?131031765087663960'
12 */
13 QUnit.fixurl = function ( value ) {
14 return value + (/\?/.test( value ) ? '&' : '?')
15 + String( new Date().getTime() )
16 + String( parseInt( Math.random() * 100000, 10 ) );
17 };
18
19 /**
20 * Configuration
21 */
22
23 // When a test() indicates asynchronicity with stop(),
24 // allow 10 seconds to pass before killing the test(),
25 // and assuming failure.
26 QUnit.config.testTimeout = 10 * 1000;
27
28 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
29 QUnit.config.urlConfig.push( 'debug' );
30
31 /**
32 * Load TestSwarm agent
33 */
34 // Only if the current url indicates that there is a TestSwarm instance watching us
35 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
36 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
37 // no point in loading inject.js in that case. Also, make sure that this instance
38 // of MediaWiki has actually been configured with the required url to that inject.js
39 // script. By default it is false.
40 if ( QUnit.urlParams.swarmURL && mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) {
41 document.write( "<scr" + "ipt src='" + QUnit.fixurl( mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
42 }
43
44 /**
45 * CompletenessTest
46 */
47 // Adds toggle checkbox to header
48 QUnit.config.urlConfig.push( 'completenesstest' );
49
50 // Initiate when enabled
51 if ( QUnit.urlParams.completenesstest ) {
52
53 // Return true to ignore
54 mwTestIgnore = function ( val, tester, funcPath ) {
55
56 // Don't record methods of the properties of constructors,
57 // to avoid getting into a loop (prototype.constructor.prototype..).
58 // Since we're therefor skipping any injection for
59 // "new mw.Foo()", manually set it to true here.
60 if ( val instanceof mw.Map ) {
61 tester.methodCallTracker.Map = true;
62 return true;
63 }
64 if ( val instanceof mw.Title ) {
65 tester.methodCallTracker.Title = true;
66 return true;
67 }
68
69 // Don't record methods of the properties of a jQuery object
70 if ( val instanceof $ ) {
71 return true;
72 }
73
74 return false;
75 };
76
77 mwTester = new CompletenessTest( mw, mwTestIgnore );
78 }
79
80 /**
81 * Test environment recommended for all QUnit test modules
82 */
83 // Whether to log environment changes to the console
84 QUnit.config.urlConfig.push( 'mwlogenv' );
85
86 /**
87 * Reset mw.config to a fresh copy of the live config for each test();
88 * @param override {Object} [optional]
89 * @example:
90 * <code>
91 * module( .., newMwEnvironment() );
92 *
93 * test( .., function () {
94 * mw.config.set( 'foo', 'bar' ); // just for this test
95 * } );
96 *
97 * test( .., function () {
98 * mw.config.get( 'foo' ); // doesn't exist
99 * } );
100 *
101 *
102 * module( .., newMwEnvironment({ quux: 'corge' }) );
103 *
104 * test( .., function () {
105 * mw.config.get( 'quux' ); // "corge"
106 * mw.config.set( 'quux', "grault" );
107 * } );
108 *
109 * test( .., function () {
110 * mw.config.get( 'quux' ); // "corge"
111 * } );
112 * </code>
113 */
114 QUnit.newMwEnvironment = ( function () {
115 var log, liveConfig, liveMsgs;
116
117 liveConfig = mw.config.values;
118 liveMsgs = mw.messages.values;
119
120 function freshConfigCopy( custom ) {
121 // "deep=true" is important here.
122 // Otherwise we just create a new object with values referring to live config.
123 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
124 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
125 // was passed by reference in $.extend's loop.
126 return $.extend({}, liveConfig, custom, /*deep=*/true );
127 }
128
129 function freshMsgsCopy( custom ) {
130 return $.extend({}, liveMsgs, custom, /*deep=*/true );
131 }
132
133 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
134
135 return function ( overrideConfig, overrideMsgs ) {
136 overrideConfig = overrideConfig || {};
137 overrideMsgs = overrideMsgs || {};
138
139 return {
140 setup: function () {
141 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
142 + ': ' + QUnit.config.current.testName + '"' );
143 // Greetings, mock environment!
144 mw.config.values = freshConfigCopy( overrideConfig );
145 mw.messages.values = freshMsgsCopy( overrideMsgs );
146 },
147
148 teardown: function () {
149 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
150 + ': ' + QUnit.config.current.testName + '"' );
151 // Farewell, mock environment!
152 mw.config.values = liveConfig;
153 mw.messages.values = liveMsgs;
154 }
155 };
156 };
157 }() );
158
159 /**
160 * Add-on assertion helpers
161 */
162 // Define the add-ons
163 addons = {
164
165 // Expect boolean true
166 assertTrue: function ( actual, message ) {
167 strictEqual( actual, true, message );
168 },
169
170 // Expect boolean false
171 assertFalse: function ( actual, message ) {
172 strictEqual( actual, false, message );
173 },
174
175 // Expect numerical value less than X
176 lt: function ( actual, expected, message ) {
177 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
178 },
179
180 // Expect numerical value less than or equal to X
181 ltOrEq: function ( actual, expected, message ) {
182 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
183 },
184
185 // Expect numerical value greater than X
186 gt: function ( actual, expected, message ) {
187 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
188 },
189
190 // Expect numerical value greater than or equal to X
191 gtOrEq: function ( actual, expected, message ) {
192 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
193 },
194
195 // Backwards compatible with new verions of QUnit
196 equals: window.equal,
197 same: window.deepEqual
198 };
199
200 $.extend( QUnit, addons );
201 $.extend( window, addons );
202
203 })( jQuery, mediaWiki, QUnit );