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