core: removed newline from version.txt, easier to use it via Ant then
[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 jQuery.checkOverflowDisplay = function(){
170         var o = jQuery.css( this, "overflow" );
171
172         equals(o, "visible", "Overflow should be visible: " + o);
173         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
174
175         start();
176 }
177
178 test("JS Overflow and Display", function() {
179         expect(2);
180         stop();
181         jQuery.makeTest( "JS Overflow and Display" )
182                 .addClass("widewidth")
183                 .css({ overflow: "visible", display: "inline" })
184                 .addClass("widewidth")
185                 .text("Some sample text.")
186                 .before("text before")
187                 .after("text after")
188                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
189 });
190                 
191 test("CSS Overflow and Display", function() {
192         expect(2);
193         stop();
194         jQuery.makeTest( "CSS Overflow and Display" )
195                 .addClass("overflow inline")
196                 .addClass("widewidth")
197                 .text("Some sample text.")
198                 .before("text before")
199                 .after("text after")
200                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
201 });
202
203 jQuery.each( {
204         "CSS Auto": function(elem,prop){
205                 jQuery(elem).addClass("auto" + prop)
206                         .text("This is a long string of text.");
207                 return "";
208         },
209         "JS Auto": function(elem,prop){
210                 jQuery(elem).css(prop,"auto")
211                         .text("This is a long string of text.");
212                 return "";
213         },
214         "CSS 100": function(elem,prop){
215                 jQuery(elem).addClass("large" + prop);
216                 return "";
217         },
218         "JS 100": function(elem,prop){
219                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
220                 return prop == "opacity" ? 1 : 100;
221         },
222         "CSS 50": function(elem,prop){
223                 jQuery(elem).addClass("med" + prop);
224                 return "";
225         },
226         "JS 50": function(elem,prop){
227                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
228                 return prop == "opacity" ? 0.5 : 50;
229         },
230         "CSS 0": function(elem,prop){
231                 jQuery(elem).addClass("no" + prop);
232                 return "";
233         },
234         "JS 0": function(elem,prop){
235                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
236                 return 0;
237         }
238 }, function(fn, f){
239         jQuery.each( {
240                 "show": function(elem,prop){
241                         jQuery(elem).hide().addClass("wide"+prop);
242                         return "show";
243                 },
244                 "hide": function(elem,prop){
245                         jQuery(elem).addClass("wide"+prop);
246                         return "hide";
247                 },
248                 "100": function(elem,prop){
249                         jQuery(elem).addClass("wide"+prop);
250                         return prop == "opacity" ? 1 : 100;
251                 },
252                 "50": function(elem,prop){
253                         return prop == "opacity" ? 0.50 : 50;
254                 },
255                 "0": function(elem,prop){
256                         jQuery(elem).addClass("noback");
257                         return 0;
258                 }
259         }, function(tn, t){
260                 test(fn + " to " + tn, function() {
261                         var elem = jQuery.makeTest( fn + " to " + tn );
262         
263                         var t_w = t( elem, "width" );
264                         var f_w = f( elem, "width" );
265                         var t_h = t( elem, "height" );
266                         var f_h = f( elem, "height" );
267                         var t_o = t( elem, "opacity" );
268                         var f_o = f( elem, "opacity" );
269                         
270                         var num = 0;
271                         
272                         if ( t_h == "show" ) num++;
273                         if ( t_w == "show" ) num++;
274                         if ( t_w == "hide"||t_w == "show" ) num++;
275                         if ( t_h == "hide"||t_h == "show" ) num++;
276                         if ( t_o == "hide"||t_o == "show" ) num++;
277                         if ( t_w == "hide" ) num++;
278                         if ( t_o.constructor == Number ) num += 2;
279                         if ( t_w.constructor == Number ) num += 2;
280                         if ( t_h.constructor == Number ) num +=2;
281                         
282                         expect(num);
283                         stop();
284         
285                         var anim = { width: t_w, height: t_h, opacity: t_o };
286         
287                         elem.animate(anim, 50, function(){
288                                 if ( t_w == "show" )
289                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
290                                         
291                                 if ( t_w == "hide"||t_w == "show" )
292                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
293                                         
294                                 if ( t_h == "hide"||t_h == "show" )
295                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
296                                         
297                                 var cur_o = jQuery.attr(this.style, "opacity");
298                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
299         
300                                 if ( t_o == "hide"||t_o == "show" )
301                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
302                                         
303                                 if ( t_w == "hide" )
304                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
305                                         
306                                 if ( t_o.constructor == Number ) {
307                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
308                                         
309                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
310                                 }
311                                         
312                                 if ( t_w.constructor == Number ) {
313                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
314                                         
315                                         var cur_w = jQuery.css(this,"width");
316
317                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
318                                 }
319                                         
320                                 if ( t_h.constructor == Number ) {
321                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
322                                         
323                                         var cur_h = jQuery.css(this,"height");
324
325                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
326                                 }
327                                 
328                                 if ( t_h == "show" ) {
329                                         var old_h = jQuery.curCSS(this, "height");
330                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
331                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
332                                 }
333         
334                                 start();
335                         });
336                 });
337         });
338 });
339
340 jQuery.fn.saveState = function(){
341         var check = ['opacity','height','width','display','overflow'];  
342         expect(check.length);
343         
344         stop();
345         return this.each(function(){
346                 var self = this;
347                 self.save = {};
348                 jQuery.each(check, function(i,c){
349                         self.save[c] = jQuery.css(self,c);
350                 });
351         });
352 };
353
354 jQuery.checkState = function(){
355         var self = this;
356         jQuery.each(this.save, function(c,v){
357                 var cur = jQuery.css(self,c);
358                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
359         });
360         start();
361 }
362
363 // Chaining Tests
364 test("Chain fadeOut fadeIn", function() {
365         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
366 });
367 test("Chain fadeIn fadeOut", function() {
368         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
369 });
370
371 test("Chain hide show", function() {
372         jQuery('#show div').saveState().hide('fast').show('fast',jQuery.checkState);
373 });
374 test("Chain show hide", function() {
375         jQuery('#hide div').saveState().show('fast').hide('fast',jQuery.checkState);
376 });
377
378 test("Chain toggle in", function() {
379         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
380 });
381 test("Chain toggle out", function() {
382         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
383 });
384
385 test("Chain slideDown slideUp", function() {
386         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',jQuery.checkState);
387 });
388 test("Chain slideUp slideDown", function() {
389         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',jQuery.checkState);
390 });
391
392 test("Chain slideToggle in", function() {
393         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
394 });
395 test("Chain slideToggle out", function() {
396         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
397 });
398
399 jQuery.makeTest = function( text ){
400         var elem = jQuery("<div></div>")
401                 .attr("id", "test" + jQuery.makeTest.id++)
402                 .addClass("box");
403
404         jQuery("<h4></h4>")
405                 .text( text )
406                 .appendTo("#fx-tests")
407                 .click(function(){
408                         jQuery(this).next().toggle();
409                 })
410                 .after( elem );
411
412         return elem;
413 }
414
415 jQuery.makeTest.id = 1;