/*jshint multistr:true, quotmark:false */ var fireNative, originaljQuery, original$, baseURL = "", supportjQuery = this.jQuery, // see RFC 2606 externalHost = "example.com"; this.hasPHP = true; this.isLocal = window.location.protocol === "file:"; // Setup global variables before loading jQuery for testing .noConflict() supportjQuery.noConflict( true ); originaljQuery = this.jQuery = undefined; original$ = this.$ = "replaced"; /** * Returns an array of elements with the given IDs * @example q( "main", "foo", "bar" ) * @result [
, , ] */ this.q = function() { var r = [], i = 0; for ( ; i < arguments.length; i++ ) { r.push( document.getElementById( arguments[ i ] ) ); } return r; }; /** * Asserts that a select matches the given IDs * @param {String} message - Assertion name * @param {String} selector - Sizzle selector * @param {String} expectedIds - Array of ids to construct what is expected * @param {(String|Node)=document} context - Selector context * @example match("Check for something", "p", ["foo", "bar"]); */ function match( message, selector, expectedIds, context ) { var f = jQuery( selector, context ).get(), s = "", i = 0; for ( ; i < f.length; i++ ) { s += ( s && "," ) + '"' + f[ i ].id + '"'; } this.deepEqual( f, q.apply( q, expectedIds ), message + " (" + selector + ")" ); } /** * Asserts that a select matches the given IDs. * The select is not bound by a context. * @param {String} message - Assertion name * @param {String} selector - Sizzle selector * @param {String} expectedIds - Array of ids to construct what is expected * @example t("Check for something", "p", ["foo", "bar"]); */ QUnit.assert.t = function( message, selector, expectedIds ) { match( message, selector, expectedIds, undefined ); }; /** * Asserts that a select matches the given IDs. * The select is performed within the `#qunit-fixture` context. * @param {String} message - Assertion name * @param {String} selector - Sizzle selector * @param {String} expectedIds - Array of ids to construct what is expected * @example selectInFixture("Check for something", "p", ["foo", "bar"]); */ QUnit.assert.selectInFixture = function( message, selector, expectedIds ) { match( message, selector, expectedIds, "#qunit-fixture" ); }; this.createDashboardXML = function() { var string = ' \ \ \ \ \ \ \ \ \ \ '; return jQuery.parseXML( string ); }; this.createWithFriesXML = function() { var string = ' \ \ \ \ \ \ \ \ \ \ 1 \ \ \ \ \ foo \ \ \ \ \ \ \ '; return jQuery.parseXML( string.replace( /\{\{\s*externalHost\s*\}\}/g, externalHost ) ); }; this.createXMLFragment = function() { var xml, frag; if ( window.ActiveXObject ) { xml = new ActiveXObject( "msxml2.domdocument" ); } else { xml = document.implementation.createDocument( "", "", null ); } if ( xml ) { frag = xml.createElement( "data" ); } return frag; }; fireNative = document.createEvent ? function( node, type ) { var event = document.createEvent( "HTMLEvents" ); event.initEvent( type, true, true ); node.dispatchEvent( event ); } : function( node, type ) { node.fireEvent( "on" + type, document.createEventObject() ); }; /** * Add random number to url to stop caching * * @example url("data/test.html") * @result "data/test.html?10538358428943" * * @example url("data/test.php?foo=bar") * @result "data/test.php?foo=bar&10538358345554" */ function url( value ) { return baseURL + value + ( /\?/.test( value ) ? "&" : "?" ) + new Date().getTime() + "" + parseInt( Math.random() * 100000, 10 ); } // Ajax testing helper this.ajaxTest = function( title, expect, options ) { QUnit.test( title, expect, function( assert ) { var requestOptions; if ( jQuery.isFunction( options ) ) { options = options( assert ); } options = options || []; requestOptions = options.requests || options.request || options; if ( !jQuery.isArray( requestOptions ) ) { requestOptions = [ requestOptions ]; } var done = assert.async(); if ( options.setup ) { options.setup(); } var completed = false, remaining = requestOptions.length, complete = function() { if ( !completed && --remaining === 0 ) { completed = true; delete ajaxTest.abort; if ( options.teardown ) { options.teardown(); } // Make sure all events will be called before done() setTimeout( done ); } }, requests = jQuery.map( requestOptions, function( options ) { var request = ( options.create || jQuery.ajax )( options ), callIfDefined = function( deferType, optionType ) { var handler = options[ deferType ] || !!options[ optionType ]; return function( _, status ) { if ( !completed ) { if ( !handler ) { assert.ok( false, "unexpected " + status ); } else if ( jQuery.isFunction( handler ) ) { handler.apply( this, arguments ); } } }; }; if ( options.afterSend ) { options.afterSend( request, assert ); } return request .done( callIfDefined( "done", "success" ) ) .fail( callIfDefined( "fail", "error" ) ) .always( complete ); } ); ajaxTest.abort = function( reason ) { if ( !completed ) { completed = true; delete ajaxTest.abort; assert.ok( false, "aborted " + reason ); jQuery.each( requests, function( i, request ) { request.abort(); } ); } }; } ); }; this.testIframe = function( fileName, name, fn ) { QUnit.test( name, function( assert ) { var done = assert.async(); // load fixture in iframe var iframe = loadFixture(), win = iframe.contentWindow, interval = setInterval( function() { if ( win && win.jQuery && win.jQuery.isReady ) { clearInterval( interval ); // call actual tests passing the correct jQuery instance to use fn.call( this, win.jQuery, win, win.document, assert ); done(); document.body.removeChild( iframe ); iframe = null; } }, 15 ); } ); function loadFixture() { var src = url( "./data/" + fileName + ".html" ), iframe = jQuery( "