Moved logic for handling .animate({}), doesn't queue anymore. Fixes #5459.
[jquery.git] / test / unit / effects.js
1 module("effects");
2
3 test("show()", function() {
4         expect(16);
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         pass = true;
12         div.hide().show(null).each(function() {
13                 if ( this.style.display == "none" ) pass = false;
14         });
15         ok( pass, "Show will null speed");
16
17         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>');
18
19         var old = jQuery("#show-tests table").show().css("display") !== "table";
20
21         var test = {
22                 "div"      : "block",
23                 "p"        : "block",
24                 "a"        : "inline",
25                 "code"     : "inline",
26                 "pre"      : "block",
27                 "span"     : "inline",
28                 "table"    : old ? "block" : "table",
29                 "thead"    : old ? "block" : "table-header-group",
30                 "tbody"    : old ? "block" : "table-row-group",
31                 "tr"       : old ? "block" : "table-row",
32                 "th"       : old ? "block" : "table-cell",
33                 "td"       : old ? "block" : "table-cell",
34                 "ul"       : "block",
35                 "li"       : old ? "block" : "list-item"
36         };
37
38         jQuery.each(test, function(selector, expected) {
39                 var elem = jQuery(selector, "#show-tests").show();
40                 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
41         });
42 });
43
44 test("show(Number) - other displays", function() {
45         expect(15);
46         reset();
47         stop();
48
49         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>');
50
51         var old = jQuery("#show-tests table").show().css("display") !== "table",
52                 num = 0;
53
54         var test = {
55                 "div"      : "block",
56                 "p"        : "block",
57                 "a"        : "inline",
58                 "code"     : "inline",
59                 "pre"      : "block",
60                 "span"     : "inline",
61                 "table"    : old ? "block" : "table",
62                 "thead"    : old ? "block" : "table-header-group",
63                 "tbody"    : old ? "block" : "table-row-group",
64                 "tr"       : old ? "block" : "table-row",
65                 "th"       : old ? "block" : "table-cell",
66                 "td"       : old ? "block" : "table-cell",
67                 "ul"       : "block",
68                 "li"       : old ? "block" : "list-item"
69         };
70
71         jQuery.each(test, function(selector, expected) {
72                 var elem = jQuery(selector, "#show-tests").show(1, function() {
73                         equals( elem.css("display"), expected, "Show using correct display type for " + selector );
74                         if ( ++num === 15 ) {
75                                 start();
76                         }
77                 });
78         });
79 });
80
81 test("animate(Hash, Object, Function)", function() {
82         expect(1);
83         stop();
84         var hash = {opacity: 'show'};
85         var hashCopy = jQuery.extend({}, hash);
86         jQuery('#foo').animate(hash, 0, function() {
87                 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
88                 start();
89         });
90 });
91
92 test("animate negative height", function() {
93         expect(1);
94         stop();
95         jQuery("#foo").animate({ height: -100 }, 100, function() {
96                 equals( this.offsetHeight, 0, "Verify height." );
97                 start();
98         });
99 });
100
101 /* // This test ends up being flaky depending upon the CPU load
102 test("animate option (queue === false)", function () {
103         expect(1);
104         stop();
105
106         var order = [];
107
108         var $foo = jQuery("#foo");
109         $foo.animate({width:'100px'}, 3000, function () {
110                 // should finish after unqueued animation so second
111                 order.push(2);
112                 same( order, [ 1, 2 ], "Animations finished in the correct order" );
113                 start();
114         });
115         $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
116                 // short duration and out of queue so should finish first
117                 order.push(1);
118         }});
119 });
120 */
121
122 test("animate with no properties", function() {
123         expect(2);
124         
125         var divs = jQuery("div"), count = 0;
126
127         divs.animate({}, function(){
128                 count++;
129         });
130
131         equals( divs.length, count, "Make sure that callback is called for each element in the set." );
132
133         stop();
134
135         var foo = jQuery("#foo");
136
137         foo.animate({});
138         foo.animate({top: 10}, 100, function(){
139                 ok( true, "Animation was properly dequeued." );
140                 start();
141         });
142 });
143
144 test("animate duration 0", function() {
145         expect(11);
146         
147         stop();
148         
149         var $elems = jQuery([{ a:0 },{ a:0 }]), counter = 0;
150         
151         equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
152                 
153         $elems.eq(0).animate( {a:1}, 0, function(){
154                 ok( true, "Animate a simple property." );
155                 counter++;
156         });
157         
158         // Failed until [6115]
159         equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
160         
161         equals( counter, 1, "One synchronic animations" );
162         
163         $elems.animate( { a:2 }, 0, function(){
164                 ok( true, "Animate a second simple property." );
165                 counter++;
166         });
167         
168         equals( counter, 3, "Multiple synchronic animations" );
169         
170         $elems.eq(0).animate( {a:3}, 0, function(){
171                 ok( true, "Animate a third simple property." );
172                 counter++;
173         });
174         $elems.eq(1).animate( {a:3}, 200, function(){
175                 counter++;
176                 // Failed until [6115]
177                 equals( counter, 5, "One synchronic and one asynchronic" );
178                 start();
179         });
180         
181         var $elem = jQuery("<div />");
182         $elem.show(0, function(){ 
183                 ok(true, "Show callback with no duration");
184         });
185         $elem.hide(0, function(){ 
186                 ok(true, "Hide callback with no duration");
187         });
188 });
189
190 test("animate hyphenated properties", function(){
191         expect(1);
192         stop();
193
194         jQuery("#nothiddendiv")
195                 .css("font-size", 10)
196                 .animate({"font-size": 20}, 200, function(){
197                         equals( this.style.fontSize, "20px", "The font-size property was animated." );
198                         start();
199                 });
200 });
201
202 test("animate non-element", function(){
203         expect(1);
204         stop();
205
206         var obj = { test: 0 };
207
208         jQuery(obj).animate({test: 200}, 200, function(){
209                 equals( obj.test, 200, "The custom property should be modified." );
210                 start();
211         });
212 });
213
214 test("stop()", function() {
215         expect(3);
216         stop();
217
218         var $foo = jQuery("#nothiddendiv");
219         var w = 0;
220         $foo.hide().width(200).width();
221
222         $foo.animate({ width:'show' }, 1000);
223         setTimeout(function(){
224                 var nw = $foo.width();
225                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
226                 $foo.stop();
227
228                 nw = $foo.width();
229                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
230                 setTimeout(function(){
231                         equals( nw, $foo.width(), "The animation didn't continue" );
232                         start();
233                 }, 100);
234         }, 100);
235 });
236
237 test("stop() - several in queue", function() {
238         expect(3);
239         stop();
240
241         var $foo = jQuery("#nothiddendivchild");
242         var w = 0;
243         $foo.hide().width(200).width();
244
245         $foo.animate({ width:'show' }, 1000);
246         $foo.animate({ width:'hide' }, 1000);
247         $foo.animate({ width:'show' }, 1000);
248         setTimeout(function(){
249                 equals( $foo.queue().length, 3, "All 3 still in the queue" );
250                 var nw = $foo.width();
251                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
252                 $foo.stop();
253
254                 nw = $foo.width();
255                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
256                 // Disabled, being flaky
257                 //equals( $foo.queue().length, 1, "The next animation continued" );
258                 $foo.stop(true);
259                 start();
260         }, 100);
261 });
262
263 test("stop(clearQueue)", function() {
264         expect(4);
265         stop();
266
267         var $foo = jQuery("#nothiddendiv");
268         var w = 0;
269         $foo.hide().width(200).width();
270
271         $foo.animate({ width:'show' }, 1000);
272         $foo.animate({ width:'hide' }, 1000);
273         $foo.animate({ width:'show' }, 1000);
274         setTimeout(function(){
275                 var nw = $foo.width();
276                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
277                 $foo.stop(true);
278
279                 nw = $foo.width();
280                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
281
282                 equals( $foo.queue().length, 0, "The animation queue was cleared" );
283                 setTimeout(function(){
284                         equals( nw, $foo.width(), "The animation didn't continue" );
285                         start();
286                 }, 100);
287         }, 100);
288 });
289
290 test("stop(clearQueue, gotoEnd)", function() {
291         expect(1);
292         stop();
293
294         var $foo = jQuery("#nothiddendivchild");
295         var w = 0;
296         $foo.hide().width(200).width();
297
298         $foo.animate({ width:'show' }, 1000);
299         $foo.animate({ width:'hide' }, 1000);
300         $foo.animate({ width:'show' }, 1000);
301         $foo.animate({ width:'hide' }, 1000);
302         setTimeout(function(){
303                 var nw = $foo.width();
304                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
305                 $foo.stop(false, true);
306
307                 nw = $foo.width();
308                 // Disabled, being flaky
309                 //equals( nw, 1, "Stop() reset the animation" );
310
311                 setTimeout(function(){
312                         // Disabled, being flaky
313                         //equals( $foo.queue().length, 2, "The next animation continued" );
314                         $foo.stop(true);
315                         start();
316                 }, 100);
317         }, 100);
318 });
319
320 test("toggle()", function() {
321         expect(6);
322         var x = jQuery("#nothiddendiv");
323         ok( x.is(":visible"), "is visible" );
324         x.toggle();
325         ok( x.is(":hidden"), "is hidden" );
326         x.toggle();
327         ok( x.is(":visible"), "is visible again" );
328         
329         x.toggle(true);
330         ok( x.is(":visible"), "is visible" );
331         x.toggle(false);
332         ok( x.is(":hidden"), "is hidden" );
333         x.toggle(true);
334         ok( x.is(":visible"), "is visible again" );
335 });
336
337 jQuery.checkOverflowDisplay = function(){
338         var o = jQuery.css( this, "overflow" );
339
340         equals(o, "visible", "Overflow should be visible: " + o);
341         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
342
343         start();
344 }
345
346 test("JS Overflow and Display", function() {
347         expect(2);
348         stop();
349         jQuery.makeTest( "JS Overflow and Display" )
350                 .addClass("widewidth")
351                 .css({ overflow: "visible", display: "inline" })
352                 .addClass("widewidth")
353                 .text("Some sample text.")
354                 .before("text before")
355                 .after("text after")
356                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
357 });
358                 
359 test("CSS Overflow and Display", function() {
360         expect(2);
361         stop();
362         jQuery.makeTest( "CSS Overflow and Display" )
363                 .addClass("overflow inline")
364                 .addClass("widewidth")
365                 .text("Some sample text.")
366                 .before("text before")
367                 .after("text after")
368                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
369 });
370
371 jQuery.each( {
372         "CSS Auto": function(elem,prop){
373                 jQuery(elem).addClass("auto" + prop)
374                         .text("This is a long string of text.");
375                 return "";
376         },
377         "JS Auto": function(elem,prop){
378                 jQuery(elem).css(prop,"auto")
379                         .text("This is a long string of text.");
380                 return "";
381         },
382         "CSS 100": function(elem,prop){
383                 jQuery(elem).addClass("large" + prop);
384                 return "";
385         },
386         "JS 100": function(elem,prop){
387                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
388                 return prop == "opacity" ? 1 : 100;
389         },
390         "CSS 50": function(elem,prop){
391                 jQuery(elem).addClass("med" + prop);
392                 return "";
393         },
394         "JS 50": function(elem,prop){
395                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
396                 return prop == "opacity" ? 0.5 : 50;
397         },
398         "CSS 0": function(elem,prop){
399                 jQuery(elem).addClass("no" + prop);
400                 return "";
401         },
402         "JS 0": function(elem,prop){
403                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
404                 return 0;
405         }
406 }, function(fn, f){
407         jQuery.each( {
408                 "show": function(elem,prop){
409                         jQuery(elem).hide().addClass("wide"+prop);
410                         return "show";
411                 },
412                 "hide": function(elem,prop){
413                         jQuery(elem).addClass("wide"+prop);
414                         return "hide";
415                 },
416                 "100": function(elem,prop){
417                         jQuery(elem).addClass("wide"+prop);
418                         return prop == "opacity" ? 1 : 100;
419                 },
420                 "50": function(elem,prop){
421                         return prop == "opacity" ? 0.50 : 50;
422                 },
423                 "0": function(elem,prop){
424                         jQuery(elem).addClass("noback");
425                         return 0;
426                 }
427         }, function(tn, t){
428                 test(fn + " to " + tn, function() {
429                         var elem = jQuery.makeTest( fn + " to " + tn );
430         
431                         var t_w = t( elem, "width" );
432                         var f_w = f( elem, "width" );
433                         var t_h = t( elem, "height" );
434                         var f_h = f( elem, "height" );
435                         var t_o = t( elem, "opacity" );
436                         var f_o = f( elem, "opacity" );
437                         
438                         var num = 0;
439                         
440                         if ( t_h == "show" ) num++;
441                         if ( t_w == "show" ) num++;
442                         if ( t_w == "hide"||t_w == "show" ) num++;
443                         if ( t_h == "hide"||t_h == "show" ) num++;
444                         if ( t_o == "hide"||t_o == "show" ) num++;
445                         if ( t_w == "hide" ) num++;
446                         if ( t_o.constructor == Number ) num += 2;
447                         if ( t_w.constructor == Number ) num += 2;
448                         if ( t_h.constructor == Number ) num +=2;
449                         
450                         expect(num);
451                         stop();
452         
453                         var anim = { width: t_w, height: t_h, opacity: t_o };
454         
455                         elem.animate(anim, 50, function(){
456                                 if ( t_w == "show" )
457                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
458                                         
459                                 if ( t_w == "hide"||t_w == "show" )
460                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
461                                         
462                                 if ( t_h == "hide"||t_h == "show" )
463                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
464                                         
465                                 var cur_o = jQuery.style(this, "opacity");
466                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
467         
468                                 if ( t_o == "hide"||t_o == "show" )
469                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
470                                         
471                                 if ( t_w == "hide" )
472                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
473                                         
474                                 if ( t_o.constructor == Number ) {
475                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
476                                         
477                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
478                                 }
479                                         
480                                 if ( t_w.constructor == Number ) {
481                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
482                                         
483                                         var cur_w = jQuery.css(this,"width");
484
485                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
486                                 }
487                                         
488                                 if ( t_h.constructor == Number ) {
489                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
490                                         
491                                         var cur_h = jQuery.css(this,"height");
492
493                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
494                                 }
495                                 
496                                 if ( t_h == "show" ) {
497                                         var old_h = jQuery.curCSS(this, "height");
498                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
499                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
500                                 }
501         
502                                 start();
503                         });
504                 });
505         });
506 });
507
508 jQuery.fn.saveState = function(){
509         var check = ['opacity','height','width','display','overflow'];  
510         expect(check.length);
511         
512         stop();
513         return this.each(function(){
514                 var self = this;
515                 self.save = {};
516                 jQuery.each(check, function(i,c){
517                         self.save[c] = jQuery.css(self,c);
518                 });
519         });
520 };
521
522 jQuery.checkState = function(){
523         var self = this;
524         jQuery.each(this.save, function(c,v){
525                 var cur = jQuery.css(self,c);
526                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
527         });
528         start();
529 }
530
531 // Chaining Tests
532 test("Chain fadeOut fadeIn", function() {
533         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
534 });
535 test("Chain fadeIn fadeOut", function() {
536         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
537 });
538
539 test("Chain hide show", function() {
540         jQuery('#show div').saveState().hide('fast').show('fast',jQuery.checkState);
541 });
542 test("Chain show hide", function() {
543         jQuery('#hide div').saveState().show('fast').hide('fast',jQuery.checkState);
544 });
545
546 test("Chain toggle in", function() {
547         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
548 });
549 test("Chain toggle out", function() {
550         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
551 });
552
553 test("Chain slideDown slideUp", function() {
554         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',jQuery.checkState);
555 });
556 test("Chain slideUp slideDown", function() {
557         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',jQuery.checkState);
558 });
559
560 test("Chain slideToggle in", function() {
561         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
562 });
563 test("Chain slideToggle out", function() {
564         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
565 });
566
567 jQuery.makeTest = function( text ){
568         var elem = jQuery("<div></div>")
569                 .attr("id", "test" + jQuery.makeTest.id++)
570                 .addClass("box");
571
572         jQuery("<h4></h4>")
573                 .text( text )
574                 .appendTo("#fx-tests")
575                 .click(function(){
576                         jQuery(this).next().toggle();
577                 })
578                 .after( elem );
579
580         return elem;
581 }
582
583 jQuery.makeTest.id = 1;
584
585 test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
586         expect(4);
587   stop();
588
589         var $checkedtest = jQuery("#checkedtest");
590         // IE6 was clearing "checked" in jQuery(elem).show("fast");
591         $checkedtest.hide().show("fast", function() {
592         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
593         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
594         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
595         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
596         start();
597         });
598 });
599
600 test("animate with per-property easing", function(){
601         
602         expect(3);
603         stop();
604         
605         var _test1_called = false;
606         var _test2_called = false;
607         var _default_test_called = false;
608         
609         jQuery.easing['_test1'] = function() {
610                 _test1_called = true;
611         };
612         
613         jQuery.easing['_test2'] = function() {
614                 _test2_called = true;
615         };
616         
617         jQuery.easing['_default_test'] = function() {
618                 _default_test_called = true;
619         };
620         
621         jQuery({a:0,b:0,c:0}).animate({
622                 a: [100, '_test1'],
623                 b: [100, '_test2'],
624                 c: 100
625         }, 400, '_default_test', function(){
626                 start();
627                 ok(_test1_called, "Easing function (1) called");
628                 ok(_test2_called, "Easing function (2) called");
629                 ok(_default_test_called, "Easing function (_default) called");
630         });
631         
632 });