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