Retool the fx test again, making it more apparent which order the queue tests will...
[jquery.git] / test / unit / fx.js
1 module("fx");
2
3 test("show()", function() {
4         expect(15);
5         var pass = true, div = jQuery("#main div");
6         div.show().each(function(){
7                 if ( this.style.display == "none" ) pass = false;
8         });
9         ok( pass, "Show" );
10
11         jQuery("#main").append('<div id="show-tests"><div><p><a href="#"></a></p><code></code><pre></pre><span></span></div><table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody></table><ul><li></li></ul></div>');
12
13         var old = jQuery("#show-tests table").show().css("display") !== "table";
14
15         var test = {
16                 "div"      : "block",
17                 "p"        : "block",
18                 "a"        : "inline",
19                 "code"     : "inline",
20                 "pre"      : "block",
21                 "span"     : "inline",
22                 "table"    : old ? "block" : "table",
23                 "thead"    : old ? "block" : "table-header-group",
24                 "tbody"    : old ? "block" : "table-row-group",
25                 "tr"       : old ? "block" : "table-row",
26                 "th"       : old ? "block" : "table-cell",
27                 "td"       : old ? "block" : "table-cell",
28                 "ul"       : "block",
29                 "li"       : old ? "block" : "list-item"
30         };
31
32         jQuery.each(test, function(selector, expected) {
33                 var elem = jQuery(selector, "#show-tests").show();
34                 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
35         });
36 });
37
38 test("animate(Hash, Object, Function)", function() {
39         expect(1);
40         stop();
41         var hash = {opacity: 'show'};
42         var hashCopy = jQuery.extend({}, hash);
43         jQuery('#foo').animate(hash, 0, function() {
44                 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
45                 start();
46         });
47 });
48
49 test("animate option (queue === false)", function () {
50         expect(1);
51         stop();
52
53         var order = [];
54
55         var $foo = jQuery("#foo");
56         $foo.animate({width:'100px'}, 3000, function () {
57                 // should finish after unqueued animation so second
58                 order.push(2);
59                 isSet( order, [ 1, 2 ], "Animations finished in the correct order" );
60                 start();
61         });
62         $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
63                 // short duration and out of queue so should finish first
64                 order.push(1);
65         }});
66 });
67
68 test("animate duration 0", function() {
69         expect(5);
70         
71         stop();
72         
73         var $elems = jQuery([{ a:0 },{ a:0 }]),
74                 counter = 0,
75                 count = function(){
76                         counter++;
77                 };
78         
79         equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
80                 
81         $elems.eq(0).animate( {a:1}, 0, count );
82         
83         // Failed until [6115]
84         equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
85         
86         equals( counter, 1, "One synchronic animations" );
87         
88         $elems.animate( { a:2 }, 0, count );
89         
90         equals( counter, 3, "Multiple synchronic animations" );
91         
92         $elems.eq(0).animate( {a:3}, 0, count );
93         $elems.eq(1).animate( {a:3}, 20, function(){
94                 count();
95                 // Failed until [6115]
96                 equals( counter, 5, "One synchronic and one asynchronic" );
97                 start();
98         });     
99 });
100
101 test("animate non-element", function(){
102         expect(1);
103         stop();
104
105         var obj = { test: 0 };
106
107         jQuery(obj).animate({test: 200}, 200, function(){
108                 equals( obj.test, 200, "The custom property should be modified." );
109                 start();
110         });
111 });
112
113 test("stop()", function() {
114         expect(3);
115         stop();
116
117         var $foo = jQuery("#nothiddendiv");
118         var w = 0;
119         $foo.hide().width(200).width();
120
121         $foo.animate({ width:'show' }, 1000);
122         setTimeout(function(){
123                 var nw = $foo.width();
124                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
125                 $foo.stop();
126
127                 nw = $foo.width();
128                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
129                 setTimeout(function(){
130                         equals( nw, $foo.width(), "The animation didn't continue" );
131                         start();
132                 }, 100);
133         }, 100);
134 });
135
136 test("stop() - several in queue", function() {
137         expect(3);
138         stop();
139
140         var $foo = jQuery("#nothiddendivchild");
141         var w = 0;
142         $foo.hide().width(200).width();
143
144         $foo.animate({ width:'show' }, 1000);
145         $foo.animate({ width:'hide' }, 1000);
146         $foo.animate({ width:'show' }, 1000);
147         setTimeout(function(){
148                 equals( $foo.queue().length, 3, "All 3 still in the queue" );
149                 var nw = $foo.width();
150                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
151                 $foo.stop();
152
153                 nw = $foo.width();
154                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
155                 // Disabled, being flaky
156                 //equals( $foo.queue().length, 1, "The next animation continued" );
157                 $foo.stop(true);
158                 start();
159         }, 100);
160 });
161
162 test("stop(clearQueue)", function() {
163         expect(4);
164         stop();
165
166         var $foo = jQuery("#nothiddendiv");
167         var w = 0;
168         $foo.hide().width(200).width();
169
170         $foo.animate({ width:'show' }, 1000);
171         $foo.animate({ width:'hide' }, 1000);
172         $foo.animate({ width:'show' }, 1000);
173         setTimeout(function(){
174                 var nw = $foo.width();
175                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
176                 $foo.stop(true);
177
178                 nw = $foo.width();
179                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
180
181                 equals( $foo.queue().length, 0, "The animation queue was cleared" );
182                 setTimeout(function(){
183                         equals( nw, $foo.width(), "The animation didn't continue" );
184                         start();
185                 }, 100);
186         }, 100);
187 });
188
189 test("stop(clearQueue, gotoEnd)", function() {
190         expect(1);
191         stop();
192
193         var $foo = jQuery("#nothiddendivchild");
194         var w = 0;
195         $foo.hide().width(200).width();
196
197         $foo.animate({ width:'show' }, 1000);
198         $foo.animate({ width:'hide' }, 1000);
199         $foo.animate({ width:'show' }, 1000);
200         $foo.animate({ width:'hide' }, 1000);
201         setTimeout(function(){
202                 var nw = $foo.width();
203                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
204                 $foo.stop(false, true);
205
206                 nw = $foo.width();
207                 // Disabled, being flaky
208                 //equals( nw, 1, "Stop() reset the animation" );
209
210                 setTimeout(function(){
211                         // Disabled, being flaky
212                         //equals( $foo.queue().length, 2, "The next animation continued" );
213                         $foo.stop(true);
214                         start();
215                 }, 100);
216         }, 100);
217 });
218
219 test("toggle()", function() {
220         expect(6);
221         var x = jQuery("#nothiddendiv");
222         ok( x.is(":visible"), "is visible" );
223         x.toggle();
224         ok( x.is(":hidden"), "is hidden" );
225         x.toggle();
226         ok( x.is(":visible"), "is visible again" );
227         
228         x.toggle(true);
229         ok( x.is(":visible"), "is visible" );
230         x.toggle(false);
231         ok( x.is(":hidden"), "is hidden" );
232         x.toggle(true);
233         ok( x.is(":visible"), "is visible again" );
234 });
235
236 jQuery.checkOverflowDisplay = function(){
237         var o = jQuery.css( this, "overflow" );
238
239         equals(o, "visible", "Overflow should be visible: " + o);
240         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
241
242         start();
243 }
244
245 test("JS Overflow and Display", function() {
246         expect(2);
247         stop();
248         jQuery.makeTest( "JS Overflow and Display" )
249                 .addClass("widewidth")
250                 .css({ overflow: "visible", display: "inline" })
251                 .addClass("widewidth")
252                 .text("Some sample text.")
253                 .before("text before")
254                 .after("text after")
255                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
256 });
257                 
258 test("CSS Overflow and Display", function() {
259         expect(2);
260         stop();
261         jQuery.makeTest( "CSS Overflow and Display" )
262                 .addClass("overflow inline")
263                 .addClass("widewidth")
264                 .text("Some sample text.")
265                 .before("text before")
266                 .after("text after")
267                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
268 });
269
270 jQuery.each( {
271         "CSS Auto": function(elem,prop){
272                 jQuery(elem).addClass("auto" + prop)
273                         .text("This is a long string of text.");
274                 return "";
275         },
276         "JS Auto": function(elem,prop){
277                 jQuery(elem).css(prop,"auto")
278                         .text("This is a long string of text.");
279                 return "";
280         },
281         "CSS 100": function(elem,prop){
282                 jQuery(elem).addClass("large" + prop);
283                 return "";
284         },
285         "JS 100": function(elem,prop){
286                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
287                 return prop == "opacity" ? 1 : 100;
288         },
289         "CSS 50": function(elem,prop){
290                 jQuery(elem).addClass("med" + prop);
291                 return "";
292         },
293         "JS 50": function(elem,prop){
294                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
295                 return prop == "opacity" ? 0.5 : 50;
296         },
297         "CSS 0": function(elem,prop){
298                 jQuery(elem).addClass("no" + prop);
299                 return "";
300         },
301         "JS 0": function(elem,prop){
302                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
303                 return 0;
304         }
305 }, function(fn, f){
306         jQuery.each( {
307                 "show": function(elem,prop){
308                         jQuery(elem).hide().addClass("wide"+prop);
309                         return "show";
310                 },
311                 "hide": function(elem,prop){
312                         jQuery(elem).addClass("wide"+prop);
313                         return "hide";
314                 },
315                 "100": function(elem,prop){
316                         jQuery(elem).addClass("wide"+prop);
317                         return prop == "opacity" ? 1 : 100;
318                 },
319                 "50": function(elem,prop){
320                         return prop == "opacity" ? 0.50 : 50;
321                 },
322                 "0": function(elem,prop){
323                         jQuery(elem).addClass("noback");
324                         return 0;
325                 }
326         }, function(tn, t){
327                 test(fn + " to " + tn, function() {
328                         var elem = jQuery.makeTest( fn + " to " + tn );
329         
330                         var t_w = t( elem, "width" );
331                         var f_w = f( elem, "width" );
332                         var t_h = t( elem, "height" );
333                         var f_h = f( elem, "height" );
334                         var t_o = t( elem, "opacity" );
335                         var f_o = f( elem, "opacity" );
336                         
337                         var num = 0;
338                         
339                         if ( t_h == "show" ) num++;
340                         if ( t_w == "show" ) num++;
341                         if ( t_w == "hide"||t_w == "show" ) num++;
342                         if ( t_h == "hide"||t_h == "show" ) num++;
343                         if ( t_o == "hide"||t_o == "show" ) num++;
344                         if ( t_w == "hide" ) num++;
345                         if ( t_o.constructor == Number ) num += 2;
346                         if ( t_w.constructor == Number ) num += 2;
347                         if ( t_h.constructor == Number ) num +=2;
348                         
349                         expect(num);
350                         stop();
351         
352                         var anim = { width: t_w, height: t_h, opacity: t_o };
353         
354                         elem.animate(anim, 50, function(){
355                                 if ( t_w == "show" )
356                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
357                                         
358                                 if ( t_w == "hide"||t_w == "show" )
359                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
360                                         
361                                 if ( t_h == "hide"||t_h == "show" )
362                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
363                                         
364                                 var cur_o = jQuery.style(this, "opacity");
365                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
366         
367                                 if ( t_o == "hide"||t_o == "show" )
368                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
369                                         
370                                 if ( t_w == "hide" )
371                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
372                                         
373                                 if ( t_o.constructor == Number ) {
374                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
375                                         
376                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
377                                 }
378                                         
379                                 if ( t_w.constructor == Number ) {
380                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
381                                         
382                                         var cur_w = jQuery.css(this,"width");
383
384                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
385                                 }
386                                         
387                                 if ( t_h.constructor == Number ) {
388                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
389                                         
390                                         var cur_h = jQuery.css(this,"height");
391
392                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
393                                 }
394                                 
395                                 if ( t_h == "show" ) {
396                                         var old_h = jQuery.curCSS(this, "height");
397                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
398                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
399                                 }
400         
401                                 start();
402                         });
403                 });
404         });
405 });
406
407 jQuery.fn.saveState = function(){
408         var check = ['opacity','height','width','display','overflow'];  
409         expect(check.length);
410         
411         stop();
412         return this.each(function(){
413                 var self = this;
414                 self.save = {};
415                 jQuery.each(check, function(i,c){
416                         self.save[c] = jQuery.css(self,c);
417                 });
418         });
419 };
420
421 jQuery.checkState = function(){
422         var self = this;
423         jQuery.each(this.save, function(c,v){
424                 var cur = jQuery.css(self,c);
425                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
426         });
427         start();
428 }
429
430 // Chaining Tests
431 test("Chain fadeOut fadeIn", function() {
432         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
433 });
434 test("Chain fadeIn fadeOut", function() {
435         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
436 });
437
438 test("Chain hide show", function() {
439         jQuery('#show div').saveState().hide('fast').show('fast',jQuery.checkState);
440 });
441 test("Chain show hide", function() {
442         jQuery('#hide div').saveState().show('fast').hide('fast',jQuery.checkState);
443 });
444
445 test("Chain toggle in", function() {
446         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
447 });
448 test("Chain toggle out", function() {
449         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
450 });
451
452 test("Chain slideDown slideUp", function() {
453         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',jQuery.checkState);
454 });
455 test("Chain slideUp slideDown", function() {
456         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',jQuery.checkState);
457 });
458
459 test("Chain slideToggle in", function() {
460         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
461 });
462 test("Chain slideToggle out", function() {
463         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
464 });
465
466 jQuery.makeTest = function( text ){
467         var elem = jQuery("<div></div>")
468                 .attr("id", "test" + jQuery.makeTest.id++)
469                 .addClass("box");
470
471         jQuery("<h4></h4>")
472                 .text( text )
473                 .appendTo("#fx-tests")
474                 .click(function(){
475                         jQuery(this).next().toggle();
476                 })
477                 .after( elem );
478
479         return elem;
480 }
481
482 jQuery.makeTest.id = 1;