Merge "Introducing LinksUpdateTest."
[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 and others to a fresh copy of the live config for each test(),
88 * and restore it back to the live one afterwards.
89 * @param localEnv {Object} [optional]
90 * @example (see test suite at the bottom of this file)
91 * </code>
92 */
93 QUnit.newMwEnvironment = ( function () {
94 var log, liveConfig, liveMessages;
95
96 liveConfig = mw.config.values;
97 liveMessages = mw.messages.values;
98
99 function freshConfigCopy( custom ) {
100 // "deep=true" is important here.
101 // Otherwise we just create a new object with values referring to live config.
102 // e.g. mw.config.set( 'wgFileExtensions', [] ) would not effect liveConfig,
103 // but mw.config.get( 'wgFileExtensions' ).push( 'png' ) would as the array
104 // was passed by reference in $.extend's loop.
105 return $.extend( {}, liveConfig, custom, /*deep=*/true );
106 }
107
108 function freshMessagesCopy( custom ) {
109 return $.extend( {}, liveMessages, custom, /*deep=*/true );
110 }
111
112 log = QUnit.urlParams.mwlogenv ? mw.log : function () {};
113
114 return function ( localEnv ) {
115 localEnv = $.extend( {
116 // QUnit
117 setup: $.noop,
118 teardown: $.noop,
119 // MediaWiki
120 config: {},
121 messages: {}
122 }, localEnv );
123
124 return {
125 setup: function () {
126 log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module
127 + ': ' + QUnit.config.current.testName + '"' );
128
129 // Greetings, mock environment!
130 mw.config.values = freshConfigCopy( localEnv.config );
131 mw.messages.values = freshMessagesCopy( localEnv.messages );
132
133 localEnv.setup()
134 },
135
136 teardown: function () {
137 log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module
138 + ': ' + QUnit.config.current.testName + '"' );
139
140 localEnv.teardown();
141
142 // Farewell, mock environment!
143 mw.config.values = liveConfig;
144 mw.messages.values = liveMessages;
145 }
146 };
147 };
148 }() );
149
150 /**
151 * Add-on assertion helpers
152 */
153 // Define the add-ons
154 addons = {
155
156 // Expect boolean true
157 assertTrue: function ( actual, message ) {
158 strictEqual( actual, true, message );
159 },
160
161 // Expect boolean false
162 assertFalse: function ( actual, message ) {
163 strictEqual( actual, false, message );
164 },
165
166 // Expect numerical value less than X
167 lt: function ( actual, expected, message ) {
168 QUnit.push( actual < expected, actual, 'less than ' + expected, message );
169 },
170
171 // Expect numerical value less than or equal to X
172 ltOrEq: function ( actual, expected, message ) {
173 QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message );
174 },
175
176 // Expect numerical value greater than X
177 gt: function ( actual, expected, message ) {
178 QUnit.push( actual > expected, actual, 'greater than ' + expected, message );
179 },
180
181 // Expect numerical value greater than or equal to X
182 gtOrEq: function ( actual, expected, message ) {
183 QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message );
184 },
185
186 // Backwards compatible with new verions of QUnit
187 equals: window.equal,
188 same: window.deepEqual
189 };
190
191 $.extend( QUnit, addons );
192 $.extend( window, addons );
193
194 /**
195 * Small test suite to confirm proper functionality of the utilities and
196 * initializations in this file.
197 */
198 var envExecCount = 0;
199 module( 'mediawiki.tests.qunit.testrunner', QUnit.newMwEnvironment({
200 setup: function () {
201 envExecCount += 1;
202 this.mwHtmlLive = mw.html;
203 mw.html = {
204 escape: function () {
205 return 'mocked-' + envExecCount;
206 }
207 };
208 },
209 teardown: function () {
210 mw.html = this.mwHtmlLive;
211 },
212 config: {
213 testVar: 'foo'
214 },
215 messages: {
216 testMsg: 'Foo.'
217 }
218 }) );
219
220 test( 'Setup', function () {
221 expect( 3 );
222
223 equal( mw.html.escape( 'foo' ), 'mocked-1', 'extra setup() callback was ran.' );
224 equal( mw.config.get( 'testVar' ), 'foo', 'config object applied' );
225 equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object applied' );
226
227 mw.config.set( 'testVar', 'bar' );
228 mw.messages.set( 'testMsg', 'Bar.' );
229 });
230
231 test( 'Teardown', function () {
232 expect( 3 );
233
234 equal( mw.html.escape( 'foo' ), 'mocked-2', 'extra setup() callback was re-ran.' );
235 equal( mw.config.get( 'testVar' ), 'foo', 'config object restored and re-applied after test()' );
236 equal( mw.messages.get( 'testMsg' ), 'Foo.', 'messages object restored and re-applied after test()' );
237 });
238
239 module( 'mediawiki.tests.qunit.testrunner-after', QUnit.newMwEnvironment() );
240
241 test( 'Teardown', function () {
242 expect( 3 );
243
244 equal( mw.html.escape( '<' ), '&lt;', 'extra teardown() callback was ran.' );
245 equal( mw.config.get( 'testVar' ), null, 'config object restored to live in next module()' );
246 equal( mw.messages.get( 'testMsg' ), null, 'messages object restored to live in next module()' );
247 });
248
249 })( jQuery, mediaWiki, QUnit );