/*global CompletenessTest */ /*jshint evil: true */ ( function ( $, mw, QUnit, undefined ) { 'use strict'; var mwTestIgnore, mwTester, addons, envExecCount, ELEMENT_NODE = 1, TEXT_NODE = 3; /** * Add bogus to url to prevent IE crazy caching * * @param value {String} a relative path (eg. 'data/foo.js' * or 'data/test.php?foo=bar'). * @return {String} Such as 'data/foo.js?131031765087663960' */ QUnit.fixurl = function ( value ) { return value + (/\?/.test( value ) ? '&' : '?') + String( new Date().getTime() ) + String( parseInt( Math.random() * 100000, 10 ) ); }; /** * Configuration */ // When a test() indicates asynchronicity with stop(), // allow 10 seconds to pass before killing the test(), // and assuming failure. QUnit.config.testTimeout = 10 * 1000; // Add a checkbox to QUnit header to toggle MediaWiki ResourceLoader debug mode. QUnit.config.urlConfig.push( { id: 'debug', label: 'Enable ResourceLoaderDebug', tooltip: 'Enable debug mode in ResourceLoader' } ); QUnit.config.requireExpects = true; /** * Load TestSwarm agent */ // Only if the current url indicates that there is a TestSwarm instance watching us // (TestSwarm appends swarmURL to the test suites url it loads in iframes). // Otherwise this is just a simple view of Special:JavaScriptTest/qunit directly, // no point in loading inject.js in that case. Also, make sure that this instance // of MediaWiki has actually been configured with the required url to that inject.js // script. By default it is false. if ( QUnit.urlParams.swarmURL && mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) { jQuery.getScript( QUnit.fixurl( mw.config.get( 'QUnitTestSwarmInjectJSPath' ) ) ); } /** * CompletenessTest * * Adds toggle checkbox to header */ QUnit.config.urlConfig.push( { id: 'completenesstest', label: 'Run CompletenessTest', tooltip: 'Run the completeness test' } ); // Initiate when enabled if ( QUnit.urlParams.completenesstest ) { // Return true to ignore mwTestIgnore = function ( val, tester ) { // Don't record methods of the properties of constructors, // to avoid getting into a loop (prototype.constructor.prototype..). // Since we're therefor skipping any injection for // "new mw.Foo()", manually set it to true here. if ( val instanceof mw.Map ) { tester.methodCallTracker.Map = true; return true; } if ( val instanceof mw.Title ) { tester.methodCallTracker.Title = true; return true; } // Don't record methods of the properties of a jQuery object if ( val instanceof $ ) { return true; } return false; }; mwTester = new CompletenessTest( mw, mwTestIgnore ); } /** * Test environment recommended for all QUnit test modules * * Whether to log environment changes to the console */ QUnit.config.urlConfig.push( 'mwlogenv' ); /** * Reset mw.config and others to a fresh copy of the live config for each test(), * and restore it back to the live one afterwards. * @param localEnv {Object} [optional] * @example (see test suite at the bottom of this file) * */ QUnit.newMwEnvironment = ( function () { var log, liveConfig, liveMessages; liveConfig = mw.config.values; liveMessages = mw.messages.values; function freshConfigCopy( custom ) { // Tests should mock all factors that directly influence the tested code. // For backwards compatibility though we set mw.config to a copy of the live config // and extend it with the (optionally) given custom settings for this test // (instead of starting blank with only the given custmo settings). // This is a shallow copy, so we don't end up with settings taking an array value // extended with the custom settings - setting a config property means you override it, // not extend it. return $.extend( {}, liveConfig, custom ); } function freshMessagesCopy( custom ) { return $.extend( /*deep=*/true, {}, liveMessages, custom ); } log = QUnit.urlParams.mwlogenv ? mw.log : function () {}; return function ( localEnv ) { localEnv = $.extend( { // QUnit setup: $.noop, teardown: $.noop, // MediaWiki config: {}, messages: {} }, localEnv ); return { setup: function () { log( 'MwEnvironment> SETUP for "' + QUnit.config.current.module + ': ' + QUnit.config.current.testName + '"' ); // Greetings, mock environment! mw.config.values = freshConfigCopy( localEnv.config ); mw.messages.values = freshMessagesCopy( localEnv.messages ); localEnv.setup(); }, teardown: function () { log( 'MwEnvironment> TEARDOWN for "' + QUnit.config.current.module + ': ' + QUnit.config.current.testName + '"' ); localEnv.teardown(); // Farewell, mock environment! mw.config.values = liveConfig; mw.messages.values = liveMessages; } }; }; }() ); // $.when stops as soon as one fails, which makes sense in most // practical scenarios, but not in a unit test where we really do // need to wait until all of them are finished. QUnit.whenPromisesComplete = function () { var altPromises = []; $.each( arguments, function ( i, arg ) { var alt = $.Deferred(); altPromises.push( alt ); // Whether this one fails or not, forwards it to // the 'done' (resolve) callback of the alternative promise. arg.always( alt.resolve ); } ); return $.when.apply( $, altPromises ); }; /** * Recursively convert a node to a plain object representing its structure. * Only considers attributes and contents (elements and text nodes). * Attribute values are compared strictly and not normalised. * * @param {Node} node * @return {Object|string} Plain JavaScript value representing the node. */ function getDomStructure( node ) { var $node, children, processedChildren, i, len, el; $node = $( node ); if ( node.nodeType === ELEMENT_NODE ) { children = $node.contents(); processedChildren = []; for ( i = 0, len = children.length; i < len; i++ ) { el = children[i]; if ( el.nodeType === ELEMENT_NODE || el.nodeType === TEXT_NODE ) { processedChildren.push( getDomStructure( el ) ); } } return { tagName: node.tagName, attributes: $node.getAttrs(), contents: processedChildren }; } else { // Should be text node return $node.text(); } } /** * Gets structure of node for this HTML. * * @param {string} html HTML markup for one or more nodes. */ function getHtmlStructure( html ) { var el = $( '
Child paragraph with A link
Regular textA spanChild paragraph with A link
Regular textA spanChild paragraph with A link
Regular textA spanChild paragraph with A link
Regular textA span