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