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