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