testinit.js 6.7 KB

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