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