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