imported the whole jquery-ui package, refreshed with 1.10.2
[myslice.git] / third-party / jquery-ui-1.10.2 / tests / unit / testsuite.js
1 (function( $ ) {
2
3 var reset, jshintLoaded;
4
5 window.TestHelpers = {};
6
7 function includeStyle( url ) {
8         document.write( "<link rel='stylesheet' href='../../../" + url + "'>" );
9 }
10
11 function includeScript( url ) {
12         document.write( "<script src='../../../" + url + "'></script>" );
13 }
14
15 function url( value ) {
16         return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random() * 100000, 10);
17 }
18
19 reset = QUnit.reset;
20 QUnit.reset = function() {
21         // Ensure jQuery events and data on the fixture are properly removed
22         jQuery("#qunit-fixture").empty();
23         // Let QUnit reset the fixture
24         reset.apply( this, arguments );
25 };
26
27
28 QUnit.config.requireExpects = true;
29
30 QUnit.config.urlConfig.push({
31         id: "min",
32         label: "Minified source",
33         tooltip: "Load minified source files instead of the regular unminified ones."
34 });
35
36 TestHelpers.loadResources = QUnit.urlParams.min ?
37         function() {
38                 includeStyle( "dist/jquery-ui.min.css" );
39                 includeScript( "dist/jquery-ui.min.js" );
40         } :
41         function( resources ) {
42                 $.each( resources.css || [], function( i, resource ) {
43                         includeStyle( "themes/base/jquery." + resource + ".css" );
44                 });
45                 $.each( resources.js || [], function( i, resource ) {
46                         includeScript( resource );
47                 });
48         };
49
50 QUnit.config.urlConfig.push({
51         id: "nojshint",
52         label: "Skip JSHint",
53         tooltip: "Skip running JSHint, e.g. within TestSwarm, where Jenkins runs it already"
54 });
55
56 jshintLoaded = false;
57 TestHelpers.testJshint = function( module ) {
58         if ( QUnit.urlParams.nojshint ) {
59                 return;
60         }
61
62         if ( !jshintLoaded ) {
63                 includeScript( "external/jshint.js" );
64                 jshintLoaded = true;
65         }
66
67         asyncTest( "JSHint", function() {
68                 expect( 1 );
69
70                 $.when(
71                         $.ajax({
72                                 url: url("../../../ui/.jshintrc"),
73                                 dataType: "json"
74                         }),
75                         $.ajax({
76                                 url: url("../../../ui/jquery.ui." + module + ".js"),
77                                 dataType: "text"
78                         })
79                 ).done(function( hintArgs, srcArgs ) {
80                         var passed = JSHINT( srcArgs[ 0 ], hintArgs[ 0 ] ),
81                                 errors = $.map( JSHINT.errors, function( error ) {
82                                         // JSHINT may report null if there are too many errors
83                                         if ( !error ) {
84                                                 return;
85                                         }
86
87                                         return "[L" + error.line + ":C" + error.character + "] " +
88                                                 error.reason + "\n" + error.evidence + "\n";
89                                 }).join( "\n" );
90                         ok( passed, errors );
91                         start();
92                 })
93                 .fail(function() {
94                         ok( false, "error loading source" );
95                         start();
96                 });
97         });
98 };
99
100 function testWidgetDefaults( widget, defaults ) {
101         var pluginDefaults = $.ui[ widget ].prototype.options;
102
103         // ensure that all defaults have the correct value
104         test( "defined defaults", function() {
105                 var count = 0;
106                 $.each( defaults, function( key, val ) {
107                         expect( ++count );
108                         if ( $.isFunction( val ) ) {
109                                 ok( $.isFunction( pluginDefaults[ key ] ), key );
110                                 return;
111                         }
112                         deepEqual( pluginDefaults[ key ], val, key );
113                 });
114         });
115
116         // ensure that all defaults were tested
117         test( "tested defaults", function() {
118                 var count = 0;
119                 $.each( pluginDefaults, function( key ) {
120                         expect( ++count );
121                         ok( key in defaults, key );
122                 });
123         });
124 }
125
126 function testWidgetOverrides( widget ) {
127         if ( $.uiBackCompat === false ) {
128                 test( "$.widget overrides", function() {
129                         expect( 4 );
130                         $.each([
131                                 "_createWidget",
132                                 "destroy",
133                                 "option",
134                                 "_trigger"
135                         ], function( i, method ) {
136                                 strictEqual( $.ui[ widget ].prototype[ method ],
137                                         $.Widget.prototype[ method ], "should not override " + method );
138                         });
139                 });
140         }
141 }
142
143 function testBasicUsage( widget ) {
144         test( "basic usage", function() {
145                 expect( 3 );
146
147                 var defaultElement = $.ui[ widget ].prototype.defaultElement;
148                 $( defaultElement ).appendTo( "body" )[ widget ]().remove();
149                 ok( true, "initialized on element" );
150
151                 $( defaultElement )[ widget ]().remove();
152                 ok( true, "initialized on disconnected DOMElement - never connected" );
153
154                 $( defaultElement ).appendTo( "body" ).remove()[ widget ]().remove();
155                 ok( true, "initialized on disconnected DOMElement - removed" );
156         });
157 }
158
159 TestHelpers.commonWidgetTests = function( widget, settings ) {
160         module( widget + ": common widget" );
161
162         TestHelpers.testJshint( widget );
163         testWidgetDefaults( widget, settings.defaults );
164         testWidgetOverrides( widget );
165         testBasicUsage( widget );
166         test( "version", function() {
167                 expect( 1 );
168                 ok( "version" in $.ui[ widget ].prototype, "version property exists" );
169         });
170 };
171
172 /*
173  * Taken from https://github.com/jquery/qunit/tree/master/addons/close-enough
174  */
175 window.closeEnough = function( actual, expected, maxDifference, message ) {
176         var passes = (actual === expected) || Math.abs(actual - expected) <= maxDifference;
177         QUnit.push(passes, actual, expected, message);
178 };
179
180 /*
181  * Experimental assertion for comparing DOM objects.
182  *
183  * Serializes an element and some properties and attributes and it's children if any, otherwise the text.
184  * Then compares the result using deepEqual.
185  */
186 window.domEqual = function( selector, modifier, message ) {
187         var expected, actual,
188                 properties = [
189                         "disabled",
190                         "readOnly"
191                 ],
192                 attributes = [
193                         "autocomplete",
194                         "aria-activedescendant",
195                         "aria-controls",
196                         "aria-describedby",
197                         "aria-disabled",
198                         "aria-expanded",
199                         "aria-haspopup",
200                         "aria-hidden",
201                         "aria-labelledby",
202                         "aria-pressed",
203                         "aria-selected",
204                         "aria-valuemax",
205                         "aria-valuemin",
206                         "aria-valuenow",
207                         "class",
208                         "href",
209                         "id",
210                         "nodeName",
211                         "role",
212                         "tabIndex",
213                         "title"
214                 ];
215
216         function getElementStyles( elem ) {
217                 var key, len,
218                         style = elem.ownerDocument.defaultView ?
219                                 elem.ownerDocument.defaultView.getComputedStyle( elem, null ) :
220                                 elem.currentStyle,
221                         styles = {};
222
223                 if ( style && style.length && style[ 0 ] && style[ style[ 0 ] ] ) {
224                         len = style.length;
225                         while ( len-- ) {
226                                 key = style[ len ];
227                                 if ( typeof style[ key ] === "string" ) {
228                                         styles[ $.camelCase( key ) ] = style[ key ];
229                                 }
230                         }
231                 // support: Opera, IE <9
232                 } else {
233                         for ( key in style ) {
234                                 if ( typeof style[ key ] === "string" ) {
235                                         styles[ key ] = style[ key ];
236                                 }
237                         }
238                 }
239
240                 return styles;
241         }
242
243         function extract( elem ) {
244                 if ( !elem || !elem.length ) {
245                         QUnit.push( false, actual, expected,
246                                 "domEqual failed, can't extract " + selector + ", message was: " + message );
247                         return;
248                 }
249
250                 var children,
251                         result = {};
252                 $.each( properties, function( index, attr ) {
253                         var value = elem.prop( attr );
254                         result[ attrĀ ] = value !== undefined ? value : "";
255                 });
256                 $.each( attributes, function( index, attr ) {
257                         var value = elem.attr( attr );
258                         result[ attrĀ ] = value !== undefined ? value : "";
259                 });
260                 result.style = getElementStyles( elem[ 0 ] );
261                 result.events = $._data( elem[ 0 ], "events" );
262                 result.data = $.extend( {}, elem.data() );
263                 delete result.data[ $.expando ];
264                 children = elem.children();
265                 if ( children.length ) {
266                         result.children = elem.children().map(function() {
267                                 return extract( $( this ) );
268                         }).get();
269                 } else {
270                         result.text = elem.text();
271                 }
272                 return result;
273         }
274
275         function done() {
276                 actual = extract( $( selector ) );
277                 QUnit.push( QUnit.equiv(actual, expected), actual, expected, message );
278         }
279
280         // Get current state prior to modifier
281         expected = extract( $( selector ) );
282
283         // Run modifier (async or sync), then compare state via done()
284         if ( modifier.length ) {
285                 modifier( done );
286         } else {
287                 modifier();
288                 done();
289         }
290 };
291
292 }( jQuery ));