(Bug 41352) Provide tests for edit conflicts.
[lhc/web/wiklou.git] / tests / qunit / data / testrunner.js
1 ( function ( $, mw, QUnit, undefined ) {
2 /*global CompletenessTest */
3 /*jshint evil:true */
4 'use strict';
5
6 var mwTestIgnore, mwTester, addons;
7
8 /**
9 * Add bogus to url to prevent IE crazy caching
10 *
11 * @param value {String} a relative path (eg. 'data/foo.js'
12 * or 'data/test.php?foo=bar').
13 * @return {String} Such as 'data/foo.js?131031765087663960'
14 */
15 QUnit.fixurl = function ( value ) {
16 return value + (/\?/.test( value ) ? '&' : '?')
17 + String( new Date().getTime() )
18 + String( parseInt( Math.random() * 100000, 10 ) );
19 };
20
21 /**
22 * Configuration
23 */
24
25 // When a test() indicates asynchronicity with stop(),
26 // allow 10 seconds to pass before killing the test(),
27 // and assuming failure.
28 QUnit.config.testTimeout = 10 * 1000;
29
30 // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode.
31 QUnit.config.urlConfig.push( {
32 id: 'debug',
33 label: 'Enable ResourceLoaderDebug',
34 tooltip: 'Enable debug mode in ResourceLoader'
35 } );
36
37 /**
38 * Load TestSwarm agent
39 */
40 // Only if the current url indicates that there is a TestSwarm instance watching us
41 // (TestSwarm appends swarmURL to the test suites url it loads in iframes).
42 // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly,
43 // no point in loading inject.js in that case. Also, make sure that this instance
44 // of MediaWiki has actually been configured with the required url to that inject.js
45 // script. By default it is false.
46 if ( QUnit.urlParams.swarmURL && mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) {
47 document.write( "<scr" + "ipt src='" + QUnit.fixurl( mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) + "'></scr" + "ipt>" );
48 }
49
50 /**
51 * CompletenessTest
52 */
53 // Adds toggle checkbox to header
54 QUnit.config.urlConfig.push( {
55 id: 'completenesstest',
56 label: 'Run CompletenessTest',
57 tooltip: 'Run the completeness test'
58 } );
59
60 // Initiate when enabled
61 if ( QUnit.urlParams.completenesstest ) {
62
63 // Return true to ignore
64 mwTestIgnore = function ( val, tester, funcPath ) {
65
66 // Don't record methods of the properties of constructors,
67 // to avoid getting into a loop (prototype.constructor.prototype..).
68 // Since we're therefor skipping any injection for
69 // "new mw.Foo()", manually set it to true here.
70 if ( val instanceof mw.Map ) {
71 tester.methodCallTracker.Map = true;
72 return true;
73 }
74 if ( val instanceof mw.Title ) {
75 tester.methodCallTracker.Title = true;
76 return true;
77 }
78
79 // Don't record methods of the properties of a jQuery object
80 if ( val instanceof $ ) {
81 return true;
82 }
83
84 return false;
85 };
86
87 mwTester = new CompletenessTest( mw, mwTestIgnore );
88 }
89
90 /**
91 * Test environment recommended for all QUnit test modules
92 */
93 // Whether to log environment changes to the console
94 QUnit.config.urlConfig.push( 'mwlogenv' );
95
96 /**
97 * Reset mw.config and others to a fresh copy of the live config for each test(),
98 * and restore it back to the live one afterwards.
99 * @param localEnv {Object} [optional]
100 * @example (see test suite at the bottom of this file)
101 * </code>
102 */
103 QUnit.newMwEnvironment = ( function () {
104 var log, liveConfig, liveMessages;
105
106 liveConfig = mw.config.values;
107 liveMessages = mw.messages.values;
108
109 function freshConfigCopy( custom ) {
110 // "deep=true" is important here.
111 // Otherwise we just create a new object with values referring to live config.
112 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
113 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
114 // was passed by reference in $.extend's loop.
115 return $.extend( {}, liveConfig, custom, /*deep=*/true );
116 }
117
118 function freshMessagesCopy( custom ) {
119 return $.extend( {}, liveMessages, custom, /*deep=*/true );
120 }
121
122 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
123
124 return function ( localEnv ) {
125 localEnv = $.extend( {
126 // QUnit
127 setup: $.noop,
128 teardown: $.noop,
129 // MediaWiki
130 config: {},
131 messages: {}
132 }, localEnv );
133
134 return {
135 setup: function () {
136 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
137 + ': ' + QUnit.config.current.testName + '"' );
138
139 // Greetings, mock environment!
140 mw.config.values = freshConfigCopy( localEnv.config );
141 mw.messages.values = freshMessagesCopy( localEnv.messages );
142
143 localEnv.setup();
144 },
145
146 teardown: function () {
147 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
148 + ': ' + QUnit.config.current.testName + '"' );
149
150 localEnv.teardown();
151
152 // Farewell, mock environment!
153 mw.config.values = liveConfig;
154 mw.messages.values = liveMessages;
155 }
156 };
157 };
158 }() );
159
160 // $.when stops as soon as one fails, which makes sense in most
161 // practical scenarios, but not in a unit test where we really do
162 // need to wait until all of them are finished.
163 QUnit.whenPromisesComplete = function () {
164 var altPromises = [];
165
166 $.each( arguments, function ( i, arg ) {
167 var alt = $.Deferred();
168 altPromises.push( alt );
169
170 // Whether this one fails or not, forwards it to
171 // the 'done' (resolve) callback of the alternative promise.
172 arg.always( alt.resolve );
173 });
174
175 return $.when.apply( $, altPromises );
176 };
177
178 /**
179 * Add-on assertion helpers
180 */
181 // Define the add-ons
182 addons = {
183
184 // Expect boolean true
185 assertTrue: function ( actual, message ) {
186 QUnit.push( actual === true, actual, true, message );
187 },
188
189 // Expect boolean false
190 assertFalse: function ( actual, message ) {
191 QUnit.push( actual === false, actual, false, message );
192 },
193
194 // Expect numerical value less than X
195 lt: function ( actual, expected, message ) {
196 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
197 },
198
199 // Expect numerical value less than or equal to X
200 ltOrEq: function ( actual, expected, message ) {
201 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
202 },
203
204 // Expect numerical value greater than X
205 gt: function ( actual, expected, message ) {
206 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
207 },
208
209 // Expect numerical value greater than or equal to X
210 gtOrEq: function ( actual, expected, message ) {
211 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
212 }
213 };
214
215 $.extend( QUnit.assert, addons );
216
217 /**
218 * Small test suite to confirm proper functionality of the utilities and
219 * initializations in this file.
220 */
221 var envExecCount = 0;
222 QUnit.module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({
223 setup: function () {
224 envExecCount += 1;
225 this.mwHtmlLive = mw.html;
226 mw.html = {
227 escape: function () {
228 return 'mocked-' + envExecCount;
229 }
230 };
231 },
232 teardown: function () {
233 mw.html = this.mwHtmlLive;
234 },
235 config: {
236 testVar: 'foo'
237 },
238 messages: {
239 testMsg: 'Foo.'
240 }
241 }) );
242
243 QUnit.test( 'Setup', 3, function ( assert ) {
244 assert.equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
245 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
246 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
247
248 mw.config.set( 'testVar', 'bar' );
249 mw.messages.set( 'testMsg', 'Bar.' );
250 });
251
252 QUnit.test( 'Teardown', 3, function ( assert ) {
253 assert.equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
254 assert.equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
255 assert.equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
256 });
257
258 QUnit.module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() );
259
260 QUnit.test( 'Teardown', 3, function ( assert ) {
261 assert.equal( mw.html.escape( '<' ), '&lt;', 'extra teardown() callback was ran.' );
262 assert.equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
263 assert.equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
264 });
265
266 }( jQuery, mediaWiki, QUnit ) );