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