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