aboutsummaryrefslogtreecommitdiffstats
path: root/test/data/testinit.js
blob: 2a3c33d76a014c7bef37ed812953f74c462e598d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*jshint multistr:true, quotmark:false */

var fireNative, originaljQuery, original$,
	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 [<div id="main">, <span id="foo">, <input id="bar">]
 */
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} a - Assertion name
 * @param {String} b - Sizzle selector
 * @param {String} c - Array of ids to construct what is expected
 * @example t("Check for something", "//[a]", ["foo", "baar"]);
 * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
 */
this.t = function( a, b, c ) {
	var f = jQuery(b).get(),
		s = "",
		i = 0;

	for ( ; i < f.length; i++ ) {
		s += ( s && "," ) + '"' + f[ i ].id + '"';
	}

	deepEqual(f, q.apply( q, c ), a + " (" + b + ")");
};

this.createDashboardXML = function() {
	var string = '<?xml version="1.0" encoding="UTF-8"?> \
	<dashboard> \
		<locations class="foo"> \
			<location for="bar" checked="different"> \
				<infowindowtab normal="ab" mixedCase="yes"> \
					<tab title="Location"><![CDATA[blabla]]></tab> \
					<tab title="Users"><![CDATA[blublu]]></tab> \
				</infowindowtab> \
			</location> \
		</locations> \
	</dashboard>';

	return jQuery.parseXML(string);
};

this.createWithFriesXML = function() {
	var string = '<?xml version="1.0" encoding="UTF-8"?> \
	<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" \
		xmlns:xsd="http://www.w3.org/2001/XMLSchema" \
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> \
		<soap:Body> \
			<jsconf xmlns="http://{{ externalHost }}/ns1"> \
				<response xmlns:ab="http://{{ externalHost }}/ns2"> \
					<meta> \
						<component id="seite1" class="component"> \
							<properties xmlns:cd="http://{{ externalHost }}/ns3"> \
								<property name="prop1"> \
									<thing /> \
									<value>1</value> \
								</property> \
								<property name="prop2"> \
									<thing att="something" /> \
								</property> \
								<foo_bar>foo</foo_bar> \
							</properties> \
						</component> \
					</meta> \
				</response> \
			</jsconf> \
		</soap:Body> \
	</soap:Envelope>';

	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 ) {
		var event = document.createEventObject();
		node.fireEvent( 'on' + type, event );
	};

/**
 * 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 value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random() * 100000, 10);
}

// Ajax testing helper
this.ajaxTest = function( title, expect, options ) {
	var requestOptions;
	if ( jQuery.isFunction( options ) ) {
		options = options();
	}
	options = options || [];
	requestOptions = options.requests || options.request || options;
	if ( !jQuery.isArray( requestOptions ) ) {
		requestOptions = [ requestOptions ];
	}
	asyncTest( title, expect, function() {
		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();
					}
					start();
				}
			},
			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 ) {
									ok( false, "unexpected " + status );
								} else if ( jQuery.isFunction( handler ) ) {
									handler.apply( this, arguments );
								}
							}
						};
					};

				if ( options.afterSend ) {
					options.afterSend( request );
				}

				return request
					.done( callIfDefined( "done", "success" ) )
					.fail( callIfDefined( "fail", "error" ) )
					.always( complete );
			});

		ajaxTest.abort = function( reason ) {
			if ( !completed ) {
				completed = true;
				delete ajaxTest.abort;
				ok( false, "aborted " + reason );
				jQuery.each( requests, function( i, request ) {
					request.abort();
				});
			}
		};
	});
};


this.testIframe = function( fileName, name, fn ) {

	test(name, function() {
		// pause execution for now
		stop();

		// load fixture in iframe
		var iframe = loadFixture(),
			win = iframe.contentWindow,
			interval = setInterval( function() {
				if ( win && win.jQuery && win.jQuery.isReady ) {
					clearInterval( interval );
					// continue
					start();
					// call actual tests passing the correct jQuery instance to use
					fn.call( this, win.jQuery, win, win.document );
					document.body.removeChild( iframe );
					iframe = null;
				}
			}, 15 );
	});

	function loadFixture() {
		var src = url("./data/" + fileName + ".html"),
			iframe = jQuery("<iframe />").appendTo("body")[0];
			iframe.style.cssText = "width: 500px; height: 500px; position: absolute; top: -600px; left: -600px; visibility: hidden;";
		iframe.contentWindow.location = src;
		return iframe;
	}
};

this.testIframeWithCallback = function( title, fileName, func ) {

	test( title, function() {
		var iframe;

		stop();
		window.iframeCallback = function() {
			var self = this,
				args = arguments;
			setTimeout(function() {
				window.iframeCallback = undefined;
				iframe.remove();
				func.apply( self, args );
				func = function() {};
				start();
			}, 0 );
		};
		iframe = jQuery( "<div/>" ).append(
			jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) )
		).appendTo( "body" );
	});
};
this.iframeCallback = undefined;

// Tests are always loaded async
QUnit.config.autostart = false;
this.loadTests = function() {
	var loadSwarm,
		url = window.location.search,
		tests = [
		"data/testrunner.js",
		"unit/core.js",
		"unit/callbacks.js",
		"unit/deferred.js",
		"unit/support.js",
		"unit/data.js",
		"unit/queue.js",
		"unit/attributes.js",
		"unit/event.js",
		"unit/selector.js",
		"unit/traversing.js",
		"unit/manipulation.js",
		"unit/wrap.js",
		"unit/css.js",
		"unit/serialize.js",
		"unit/ajax.js",
		"unit/effects.js",
		"unit/offset.js",
		"unit/dimensions.js"
	];
	url = decodeURIComponent( url.slice( url.indexOf("swarmURL=") + "swarmURL=".length ) );
	loadSwarm = url && url.indexOf("http") === 0;

	// Ensure load order (to preserve test numbers)
	(function loadDep() {
		var dep = tests.shift();
		if ( dep ) {
			require( [ dep ], loadDep );
		} else {
			// Subproject tests must be last because they replace our test fixture
			window.testSubproject( "Sizzle", "../bower_components/sizzle/test/", /^unit\/.*\.js$/, function() {
				// Call load to build module filter select element
				QUnit.load();

				// Load the TestSwarm listener if swarmURL is in the address.
				if ( loadSwarm ) {
					require( [ "http://swarm.jquery.org/js/inject.js?" + (new Date()).getTime() ], function() {
						QUnit.start();
					});
				} else {
					QUnit.start();
				}

				/**
				 * Run in noConflict mode
				 */
				jQuery.noConflict();

				// Expose Sizzle for Sizzle's selector tests
				// We remove Sizzle's globalization in jQuery
				window.Sizzle = window.Sizzle || jQuery.find;

				// For checking globals pollution despite auto-created globals in various environments
				supportjQuery.each( [ jQuery.expando, "getInterface", "Packages", "java", "netscape" ], function( i, name ) {
					window[ name ] = window[ name ];
				});
			});
		}
	})();
};