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