testinit.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*jshint multistr:true, quotmark:false */
  2. var amdDefined, fireNative,
  3. originaljQuery = this.jQuery || "jQuery",
  4. original$ = this.$ || "$",
  5. hasPHP = true,
  6. isLocal = window.location.protocol === "file:",
  7. // see RFC 2606
  8. externalHost = "example.com";
  9. // For testing .noConflict()
  10. this.jQuery = originaljQuery;
  11. this.$ = original$;
  12. /**
  13. * Set up a mock AMD define function for testing AMD registration.
  14. */
  15. function define( name, dependencies, callback ) {
  16. amdDefined = callback();
  17. }
  18. define.amd = {
  19. jQuery: true
  20. };
  21. /**
  22. * Returns an array of elements with the given IDs
  23. * @example q("main", "foo", "bar")
  24. * @result [<div id="main">, <span id="foo">, <input id="bar">]
  25. */
  26. function q() {
  27. var r = [],
  28. i = 0;
  29. for ( ; i < arguments.length; i++ ) {
  30. r.push( document.getElementById( arguments[i] ) );
  31. }
  32. return r;
  33. }
  34. /**
  35. * Asserts that a select matches the given IDs
  36. * @param {String} a - Assertion name
  37. * @param {String} b - Sizzle selector
  38. * @param {String} c - Array of ids to construct what is expected
  39. * @example t("Check for something", "//[a]", ["foo", "baar"]);
  40. * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
  41. */
  42. function t( a, b, c ) {
  43. var f = jQuery(b).get(),
  44. s = "",
  45. i = 0;
  46. for ( ; i < f.length; i++ ) {
  47. s += ( s && "," ) + '"' + f[ i ].id + '"';
  48. }
  49. deepEqual(f, q.apply( q, c ), a + " (" + b + ")");
  50. }
  51. function createDashboardXML() {
  52. var string = '<?xml version="1.0" encoding="UTF-8"?> \
  53. <dashboard> \
  54. <locations class="foo"> \
  55. <location for="bar" checked="different"> \
  56. <infowindowtab normal="ab" mixedCase="yes"> \
  57. <tab title="Location"><![CDATA[blabla]]></tab> \
  58. <tab title="Users"><![CDATA[blublu]]></tab> \
  59. </infowindowtab> \
  60. </location> \
  61. </locations> \
  62. </dashboard>';
  63. return jQuery.parseXML(string);
  64. }
  65. function createWithFriesXML() {
  66. var string = '<?xml version="1.0" encoding="UTF-8"?> \
  67. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" \
  68. xmlns:xsd="http://www.w3.org/2001/XMLSchema" \
  69. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \
  70. <soap:Body> \
  71. <jsconf xmlns="http://{{ externalHost }}/ns1"> \
  72. <response xmlns:ab="http://{{ externalHost }}/ns2"> \
  73. <meta> \
  74. <component id="seite1" class="component"> \
  75. <properties xmlns:cd="http://{{ externalHost }}/ns3"> \
  76. <property name="prop1"> \
  77. <thing /> \
  78. <value>1</value> \
  79. </property> \
  80. <property name="prop2"> \
  81. <thing att="something" /> \
  82. </property> \
  83. <foo_bar>foo</foo_bar> \
  84. </properties> \
  85. </component> \
  86. </meta> \
  87. </response> \
  88. </jsconf> \
  89. </soap:Body> \
  90. </soap:Envelope>';
  91. return jQuery.parseXML( string.replace( /\{\{\s*externalHost\s*\}\}/g, externalHost ) );
  92. }
  93. function createXMLFragment() {
  94. var xml, frag;
  95. if ( window.ActiveXObject ) {
  96. xml = new ActiveXObject("msxml2.domdocument");
  97. } else {
  98. xml = document.implementation.createDocument( "", "", null );
  99. }
  100. if ( xml ) {
  101. frag = xml.createElement("data");
  102. }
  103. return frag;
  104. }
  105. fireNative = document.createEvent ?
  106. function( node, type ) {
  107. var event = document.createEvent('HTMLEvents');
  108. event.initEvent( type, true, true );
  109. node.dispatchEvent( event );
  110. } :
  111. function( node, type ) {
  112. var event = document.createEventObject();
  113. node.fireEvent( 'on' + type, event );
  114. };
  115. /**
  116. * Add random number to url to stop caching
  117. *
  118. * @example url("data/test.html")
  119. * @result "data/test.html?10538358428943"
  120. *
  121. * @example url("data/test.php?foo=bar")
  122. * @result "data/test.php?foo=bar&10538358345554"
  123. */
  124. function url( value ) {
  125. return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random() * 100000, 10);
  126. }
  127. // Ajax testing helper
  128. function ajaxTest( title, expect, options ) {
  129. var requestOptions;
  130. if ( jQuery.isFunction( options ) ) {
  131. options = options();
  132. }
  133. options = options || [];
  134. requestOptions = options.requests || options.request || options;
  135. if ( !jQuery.isArray( requestOptions ) ) {
  136. requestOptions = [ requestOptions ];
  137. }
  138. asyncTest( title, expect, function() {
  139. if ( options.setup ) {
  140. options.setup();
  141. }
  142. var completed = false,
  143. remaining = requestOptions.length,
  144. complete = function() {
  145. if ( !completed && --remaining === 0 ) {
  146. completed = true;
  147. delete ajaxTest.abort;
  148. if ( options.teardown ) {
  149. options.teardown();
  150. }
  151. start();
  152. }
  153. },
  154. requests = jQuery.map( requestOptions, function( options ) {
  155. var request = ( options.create || jQuery.ajax )( options ),
  156. callIfDefined = function( deferType, optionType ) {
  157. var handler = options[ deferType ] || !!options[ optionType ];
  158. return function( _, status ) {
  159. if ( !completed ) {
  160. if ( !handler ) {
  161. ok( false, "unexpected " + status );
  162. } else if ( jQuery.isFunction( handler ) ) {
  163. handler.apply( this, arguments );
  164. }
  165. }
  166. };
  167. };
  168. if ( options.afterSend ) {
  169. options.afterSend( request );
  170. }
  171. return request
  172. .done( callIfDefined( "done", "success" ) )
  173. .fail( callIfDefined( "fail", "error" ) )
  174. .always( complete );
  175. });
  176. ajaxTest.abort = function( reason ) {
  177. if ( !completed ) {
  178. completed = true;
  179. delete ajaxTest.abort;
  180. ok( false, "aborted " + reason );
  181. jQuery.each( requests, function( i, request ) {
  182. request.abort();
  183. });
  184. }
  185. };
  186. });
  187. }
  188. (function () {
  189. this.testIframe = function( fileName, name, fn ) {
  190. test(name, function() {
  191. // pause execution for now
  192. stop();
  193. // load fixture in iframe
  194. var iframe = loadFixture(),
  195. win = iframe.contentWindow,
  196. interval = setInterval( function() {
  197. if ( win && win.jQuery && win.jQuery.isReady ) {
  198. clearInterval( interval );
  199. // continue
  200. start();
  201. // call actual tests passing the correct jQuery instance to use
  202. fn.call( this, win.jQuery, win, win.document );
  203. document.body.removeChild( iframe );
  204. iframe = null;
  205. }
  206. }, 15 );
  207. });
  208. function loadFixture() {
  209. var src = url("./data/" + fileName + ".html"),
  210. iframe = jQuery("<iframe />").appendTo("body")[0];
  211. iframe.style.cssText = "width: 500px; height: 500px; position: absolute; top: -600px; left: -600px; visibility: hidden;";
  212. iframe.contentWindow.location = src;
  213. return iframe;
  214. }
  215. };
  216. this.testIframeWithCallback = function( title, fileName, func ) {
  217. test( title, function() {
  218. var iframe;
  219. stop();
  220. window.iframeCallback = function() {
  221. var self = this,
  222. args = arguments;
  223. setTimeout(function() {
  224. window.iframeCallback = undefined;
  225. iframe.remove();
  226. func.apply( self, args );
  227. func = function() {};
  228. start();
  229. }, 0 );
  230. };
  231. iframe = jQuery( "<div/>" ).append(
  232. jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) )
  233. ).appendTo( "body" );
  234. });
  235. };
  236. window.iframeCallback = undefined;
  237. }());