Made some tweaks to the core tests so that they'll run better under XHTML.
[jquery.git] / test / unit / fx.js
1 module("fx");
2
3 test("animate(Hash, Object, Function)", function() {
4         expect(1);
5         stop();
6         var hash = {opacity: 'show'};
7         var hashCopy = jQuery.extend({}, hash);
8         jQuery('#foo').animate(hash, 0, function() {
9                 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
10                 start();
11         });
12 });
13
14 test("animate option (queue === false)", function () {
15         expect(1);
16         stop();
17
18         var order = [];
19
20         var $foo = jQuery("#foo");
21         $foo.animate({width:'100px'}, 200, function () {
22                 // should finish after unqueued animation so second
23                 order.push(2);
24         });
25         $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
26                 // short duration and out of queue so should finish first
27                 order.push(1);
28         }});
29         $foo.animate({height:'100px'}, 10, function() {
30                 // queued behind the first animation so should finish third 
31                 order.push(3);
32                 isSet( order, [ 1, 2, 3], "Animations finished in the correct order" );
33                 start();
34         });
35 });
36
37 test("animate duration 0", function() {
38         expect(5);
39         
40         stop();
41         
42         var $elems = jQuery([{ a:0 },{ a:0 }]),
43                 counter = 0,
44                 count = function(){
45                         counter++;
46                 };
47         
48         equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
49                 
50         $elems.eq(0).animate( {a:1}, 0, count );
51         
52         // Failed until [6115]
53         equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
54         
55         equals( counter, 1, "One synchronic animations" );
56         
57         $elems.animate( { a:2 }, 0, count );
58         
59         equals( counter, 3, "Multiple synchronic animations" );
60         
61         $elems.eq(0).animate( {a:3}, 0, count );
62         $elems.eq(1).animate( {a:3}, 20, function(){
63                 count();
64                 // Failed until [6115]
65                 equals( counter, 5, "One synchronic and one asynchronic" );
66                 start();
67         });     
68 });
69
70 test("animate non-element", function(){
71         expect(1);
72         stop();
73
74         var obj = { test: 0 };
75
76         jQuery(obj).animate({test: 200}, 200, function(){
77                 equals( obj.test, 200, "The custom property should be modified." );
78                 start();
79         });
80 });
81
82 test("stop()", function() {
83         expect(3);
84         stop();
85
86         var $foo = jQuery("#nothiddendiv");
87         var w = 0;
88         $foo.hide().width(200).width();
89
90         $foo.animate({ width:'show' }, 1000);
91         setTimeout(function(){
92                 var nw = $foo.width();
93                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
94                 $foo.stop();
95
96                 nw = $foo.width();
97                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
98                 setTimeout(function(){
99                         equals( nw, $foo.width(), "The animation didn't continue" );
100                         start();
101                 }, 100);
102         }, 100);
103 });
104
105 test("stop() - several in queue", function() {
106         expect(4);
107         stop();
108
109         var $foo = jQuery("#nothiddendiv");
110         var w = 0;
111         $foo.hide().width(200).width();
112
113         $foo.animate({ width:'show' }, 1000);
114         $foo.animate({ width:'hide' }, 1000);
115         $foo.animate({ width:'show' }, 1000);
116         setTimeout(function(){
117                 equals( $foo.queue().length, 3, "All 3 still in the queue" );
118                 var nw = $foo.width();
119                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
120                 $foo.stop();
121
122                 nw = $foo.width();
123                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
124                 equals( $foo.queue().length, 2, "The next animation continued" );
125                 $foo.stop(true);
126                 start();
127         }, 100);
128 });
129
130 test("stop(clearQueue)", function() {
131         expect(4);
132         stop();
133
134         var $foo = jQuery("#nothiddendiv");
135         var w = 0;
136         $foo.hide().width(200).width();
137
138         $foo.animate({ width:'show' }, 1000);
139         $foo.animate({ width:'hide' }, 1000);
140         $foo.animate({ width:'show' }, 1000);
141         setTimeout(function(){
142                 var nw = $foo.width();
143                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
144                 $foo.stop(true);
145
146                 nw = $foo.width();
147                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
148
149                 equals( $foo.queue().length, 0, "The animation queue was cleared" );
150                 setTimeout(function(){
151                         equals( nw, $foo.width(), "The animation didn't continue" );
152                         start();
153                 }, 100);
154         }, 100);
155 });
156
157 test("stop(clearQueue, gotoEnd)", function() {
158         expect(3);
159         stop();
160
161         var $foo = jQuery("#nothiddendiv");
162         var w = 0;
163         $foo.hide().width(200).width();
164
165         $foo.animate({ width:'show' }, 1000);
166         $foo.animate({ width:'hide' }, 1000);
167         $foo.animate({ width:'show' }, 1000);
168         $foo.animate({ width:'hide' }, 1000);
169         setTimeout(function(){
170                 var nw = $foo.width();
171                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
172                 $foo.stop(false, true);
173
174                 nw = $foo.width();
175                 equals( nw, 200, "Stop() reset the animation" );
176
177                 setTimeout(function(){
178                         equals( $foo.queue().length, 3, "The next animation continued" );
179                         $foo.stop(true);
180                         start();
181                 }, 100);
182         }, 100);
183 });
184
185 test("toggle()", function() {
186         expect(6);
187         var x = jQuery("#foo");
188         ok( x.is(":visible"), "is visible" );
189         x.toggle();
190         ok( x.is(":hidden"), "is hidden" );
191         x.toggle();
192         ok( x.is(":visible"), "is visible again" );
193         
194         x.toggle(true);
195         ok( x.is(":visible"), "is visible" );
196         x.toggle(false);
197         ok( x.is(":hidden"), "is hidden" );
198         x.toggle(true);
199         ok( x.is(":visible"), "is visible again" );
200 });
201
202 jQuery.checkOverflowDisplay = function(){
203         var o = jQuery.css( this, "overflow" );
204
205         equals(o, "visible", "Overflow should be visible: " + o);
206         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
207
208         start();
209 }
210
211 test("JS Overflow and Display", function() {
212         expect(2);
213         stop();
214         jQuery.makeTest( "JS Overflow and Display" )
215                 .addClass("widewidth")
216                 .css({ overflow: "visible", display: "inline" })
217                 .addClass("widewidth")
218                 .text("Some sample text.")
219                 .before("text before")
220                 .after("text after")
221                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
222 });
223                 
224 test("CSS Overflow and Display", function() {
225         expect(2);
226         stop();
227         jQuery.makeTest( "CSS Overflow and Display" )
228                 .addClass("overflow inline")
229                 .addClass("widewidth")
230                 .text("Some sample text.")
231                 .before("text before")
232                 .after("text after")
233                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
234 });
235
236 jQuery.each( {
237         "CSS Auto": function(elem,prop){
238                 jQuery(elem).addClass("auto" + prop)
239                         .text("This is a long string of text.");
240                 return "";
241         },
242         "JS Auto": function(elem,prop){
243                 jQuery(elem).css(prop,"auto")
244                         .text("This is a long string of text.");
245                 return "";
246         },
247         "CSS 100": function(elem,prop){
248                 jQuery(elem).addClass("large" + prop);
249                 return "";
250         },
251         "JS 100": function(elem,prop){
252                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
253                 return prop == "opacity" ? 1 : 100;
254         },
255         "CSS 50": function(elem,prop){
256                 jQuery(elem).addClass("med" + prop);
257                 return "";
258         },
259         "JS 50": function(elem,prop){
260                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
261                 return prop == "opacity" ? 0.5 : 50;
262         },
263         "CSS 0": function(elem,prop){
264                 jQuery(elem).addClass("no" + prop);
265                 return "";
266         },
267         "JS 0": function(elem,prop){
268                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
269                 return 0;
270         }
271 }, function(fn, f){
272         jQuery.each( {
273                 "show": function(elem,prop){
274                         jQuery(elem).hide().addClass("wide"+prop);
275                         return "show";
276                 },
277                 "hide": function(elem,prop){
278                         jQuery(elem).addClass("wide"+prop);
279                         return "hide";
280                 },
281                 "100": function(elem,prop){
282                         jQuery(elem).addClass("wide"+prop);
283                         return prop == "opacity" ? 1 : 100;
284                 },
285                 "50": function(elem,prop){
286                         return prop == "opacity" ? 0.50 : 50;
287                 },
288                 "0": function(elem,prop){
289                         jQuery(elem).addClass("noback");
290                         return 0;
291                 }
292         }, function(tn, t){
293                 test(fn + " to " + tn, function() {
294                         var elem = jQuery.makeTest( fn + " to " + tn );
295         
296                         var t_w = t( elem, "width" );
297                         var f_w = f( elem, "width" );
298                         var t_h = t( elem, "height" );
299                         var f_h = f( elem, "height" );
300                         var t_o = t( elem, "opacity" );
301                         var f_o = f( elem, "opacity" );
302                         
303                         var num = 0;
304                         
305                         if ( t_h == "show" ) num++;
306                         if ( t_w == "show" ) num++;
307                         if ( t_w == "hide"||t_w == "show" ) num++;
308                         if ( t_h == "hide"||t_h == "show" ) num++;
309                         if ( t_o == "hide"||t_o == "show" ) num++;
310                         if ( t_w == "hide" ) num++;
311                         if ( t_o.constructor == Number ) num += 2;
312                         if ( t_w.constructor == Number ) num += 2;
313                         if ( t_h.constructor == Number ) num +=2;
314                         
315                         expect(num);
316                         stop();
317         
318                         var anim = { width: t_w, height: t_h, opacity: t_o };
319         
320                         elem.animate(anim, 50, function(){
321                                 if ( t_w == "show" )
322                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
323                                         
324                                 if ( t_w == "hide"||t_w == "show" )
325                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
326                                         
327                                 if ( t_h == "hide"||t_h == "show" )
328                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
329                                         
330                                 var cur_o = jQuery.attr(this.style, "opacity");
331                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
332         
333                                 if ( t_o == "hide"||t_o == "show" )
334                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
335                                         
336                                 if ( t_w == "hide" )
337                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
338                                         
339                                 if ( t_o.constructor == Number ) {
340                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
341                                         
342                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
343                                 }
344                                         
345                                 if ( t_w.constructor == Number ) {
346                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
347                                         
348                                         var cur_w = jQuery.css(this,"width");
349
350                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
351                                 }
352                                         
353                                 if ( t_h.constructor == Number ) {
354                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
355                                         
356                                         var cur_h = jQuery.css(this,"height");
357
358                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
359                                 }
360                                 
361                                 if ( t_h == "show" ) {
362                                         var old_h = jQuery.curCSS(this, "height");
363                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
364                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
365                                 }
366         
367                                 start();
368                         });
369                 });
370         });
371 });
372
373 jQuery.fn.saveState = function(){
374         var check = ['opacity','height','width','display','overflow'];  
375         expect(check.length);
376         
377         stop();
378         return this.each(function(){
379                 var self = this;
380                 self.save = {};
381                 jQuery.each(check, function(i,c){
382                         self.save[c] = jQuery.css(self,c);
383                 });
384         });
385 };
386
387 jQuery.checkState = function(){
388         var self = this;
389         jQuery.each(this.save, function(c,v){
390                 var cur = jQuery.css(self,c);
391                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
392         });
393         start();
394 }
395
396 // Chaining Tests
397 test("Chain fadeOut fadeIn", function() {
398         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
399 });
400 test("Chain fadeIn fadeOut", function() {
401         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
402 });
403
404 test("Chain hide show", function() {
405         jQuery('#show div').saveState().hide('fast').show('fast',jQuery.checkState);
406 });
407 test("Chain show hide", function() {
408         jQuery('#hide div').saveState().show('fast').hide('fast',jQuery.checkState);
409 });
410
411 test("Chain toggle in", function() {
412         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
413 });
414 test("Chain toggle out", function() {
415         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
416 });
417
418 test("Chain slideDown slideUp", function() {
419         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',jQuery.checkState);
420 });
421 test("Chain slideUp slideDown", function() {
422         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',jQuery.checkState);
423 });
424
425 test("Chain slideToggle in", function() {
426         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
427 });
428 test("Chain slideToggle out", function() {
429         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
430 });
431
432 jQuery.makeTest = function( text ){
433         var elem = jQuery("<div></div>")
434                 .attr("id", "test" + jQuery.makeTest.id++)
435                 .addClass("box");
436
437         jQuery("<h4></h4>")
438                 .text( text )
439                 .appendTo("#fx-tests")
440                 .click(function(){
441                         jQuery(this).next().toggle();
442                 })
443                 .after( elem );
444
445         return elem;
446 }
447
448 jQuery.makeTest.id = 1;