Removed comments and code related to Safari being unable to do a sync global eval...
[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 = $.extend({}, hash);
8         $('#foo').animate(hash, 0, function() {
9                 ok( 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 = $("#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 = $("#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         reset();
58
59         var foo = $("#foo")[0];
60         var h = foo.style.height;
61
62         $("#foo").slideUp(1000);
63         setTimeout(function(){
64                 var nh = foo.style.height;
65                 ok( nh != h, "An animation occurred " + nh + " " + h );
66                 $("#foo").stop();
67
68                 nh = foo.style.height;
69                 ok( nh != h, "Stop didn't reset the animation " + nh + " " + h );
70                 setTimeout(function(){
71                         equals( nh, foo.style.height, "The animation didn't continue" );
72                         start();
73                 }, 100);
74         }, 100);
75 });
76
77 test("toggle()", function() {
78         expect(3);
79         var x = $("#foo");
80         ok( x.is(":visible") );
81         x.toggle();
82         ok( x.is(":hidden") );
83         x.toggle();
84         ok( x.is(":visible") );
85 });
86
87 var visible = {
88         Normal: function(elem){},
89         "CSS Hidden": function(elem){
90                 $(this).addClass("hidden");
91         },
92         "JS Hidden": function(elem){
93                 $(this).hide();
94         }
95 };
96
97 var from = {
98         "CSS Auto": function(elem,prop){
99                 $(elem).addClass("auto" + prop)
100                         .text("This is a long string of text.");
101                 return "";
102         },
103         "JS Auto": function(elem,prop){
104                 $(elem).css(prop,"auto")
105                         .text("This is a long string of text.");
106                 return "";
107         },
108         "CSS 100": function(elem,prop){
109                 $(elem).addClass("large" + prop);
110                 return "";
111         },
112         "JS 100": function(elem,prop){
113                 $(elem).css(prop,prop == "opacity" ? 1 : "100px");
114                 return prop == "opacity" ? 1 : 100;
115         },
116         "CSS 50": function(elem,prop){
117                 $(elem).addClass("med" + prop);
118                 return "";
119         },
120         "JS 50": function(elem,prop){
121                 $(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
122                 return prop == "opacity" ? 0.5 : 50;
123         },
124         "CSS 0": function(elem,prop){
125                 $(elem).addClass("no" + prop);
126                 return "";
127         },
128         "JS 0": function(elem,prop){
129                 $(elem).css(prop,prop == "opacity" ? 0 : "0px");
130                 return 0;
131         }
132 };
133
134 var to = {
135         "show": function(elem,prop){
136                 $(elem).hide().addClass("wide"+prop);
137                 return "show";
138         },
139         "hide": function(elem,prop){
140                 $(elem).addClass("wide"+prop);
141                 return "hide";
142         },
143         "100": function(elem,prop){
144                 $(elem).addClass("wide"+prop);
145                 return prop == "opacity" ? 1 : 100;
146         },
147         "50": function(elem,prop){
148                 return prop == "opacity" ? 0.50 : 50;
149         },
150         "0": function(elem,prop){
151                 $(elem).addClass("noback");
152                 return 0;
153         }
154 };
155
156 function checkOverflowDisplay(){
157         var o = jQuery.css( this, "overflow" );
158
159         ok(o == "visible", "Overflow should be visible: " + o);
160         ok(jQuery.css( this, "display" ) == "inline", "Display shouldn't be tampered with.");
161
162         start();
163 }
164
165 test("JS Overflow and Display", function() {
166         expect(2);
167         stop();
168         makeTest( "JS Overflow and Display" )
169                 .addClass("widewidth")
170                 .css({ overflow: "visible", display: "inline" })
171                 .addClass("widewidth")
172                 .text("Some sample text.")
173                 .before("text before")
174                 .after("text after")
175                 .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay);
176 });
177                 
178 test("CSS Overflow and Display", function() {
179         expect(2);
180         stop();
181         makeTest( "CSS Overflow and Display" )
182                 .addClass("overflow inline")
183                 .addClass("widewidth")
184                 .text("Some sample text.")
185                 .before("text before")
186                 .after("text after")
187                 .animate({ opacity: 0.5 }, "slow", checkOverflowDisplay);
188 });
189
190 jQuery.each( from, function(fn, f){
191         jQuery.each( to, function(tn, t){
192                 test(fn + " to " + tn, function() {
193                         var elem = makeTest( fn + " to " + tn );
194         
195                         var t_w = t( elem, "width" );
196                         var f_w = f( elem, "width" );
197                         var t_h = t( elem, "height" );
198                         var f_h = f( elem, "height" );
199                         var t_o = t( elem, "opacity" );
200                         var f_o = f( elem, "opacity" );
201                         
202                         var num = 0;
203                         
204                         if ( t_h == "show" ) num++;
205                         if ( t_w == "show" ) num++;
206                         if ( t_w == "hide"||t_w == "show" ) num++;
207                         if ( t_h == "hide"||t_h == "show" ) num++;
208                         if ( t_o == "hide"||t_o == "show" ) num++;
209                         if ( t_w == "hide" ) num++;
210                         if ( t_o.constructor == Number ) num += 2;
211                         if ( t_w.constructor == Number ) num += 2;
212                         if ( t_h.constructor == Number ) num +=2;
213                         
214                         expect(num);
215                         stop();
216         
217                         var anim = { width: t_w, height: t_h, opacity: t_o };
218         
219                         elem.animate(anim, 50, function(){
220                                 if ( t_w == "show" )
221                                         ok( this.style.display == "block", "Showing, display should block: " + this.style.display);
222                                         
223                                 if ( t_w == "hide"||t_w == "show" )
224                                         ok(this.style.width.indexOf(f_w) == 0, "Width must be reset to " + f_w + ": " + this.style.width);
225                                         
226                                 if ( t_h == "hide"||t_h == "show" )
227                                         ok(this.style.height.indexOf(f_h) == 0, "Height must be reset to " + f_h + ": " + this.style.height);
228                                         
229                                 var cur_o = jQuery.attr(this.style, "opacity");
230                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
231         
232                                 if ( t_o == "hide"||t_o == "show" )
233                                         ok(cur_o == f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
234                                         
235                                 if ( t_w == "hide" )
236                                         ok(this.style.display == "none", "Hiding, display should be none: " + this.style.display);
237                                         
238                                 if ( t_o.constructor == Number ) {
239                                         ok(cur_o == t_o, "Final opacity should be " + t_o + ": " + cur_o);
240                                         
241                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
242                                 }
243                                         
244                                 if ( t_w.constructor == Number ) {
245                                         ok(this.style.width == t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
246                                         
247                                         var cur_w = jQuery.css(this,"width");
248
249                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
250                                 }
251                                         
252                                 if ( t_h.constructor == Number ) {
253                                         ok(this.style.height == t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
254                                         
255                                         var cur_h = jQuery.css(this,"height");
256
257                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
258                                 }
259                                 
260                                 if ( t_h == "show" ) {
261                                         var old_h = jQuery.curCSS(this, "height");
262                                         $(elem).append("<br/>Some more text<br/>and some more...");
263                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
264                                 }
265         
266                                 start();
267                         });
268                 });
269         });
270 });
271
272 var check = ['opacity','height','width','display','overflow'];
273
274 jQuery.fn.saveState = function(){
275         expect(check.length);
276         stop();
277         return this.each(function(){
278                 var self = this;
279                 self.save = {};
280                 jQuery.each(check, function(i,c){
281                         self.save[c] = jQuery.css(self,c);
282                 });
283         });
284 };
285
286 function checkState(){
287         var self = this;
288         jQuery.each(this.save, function(c,v){
289                 var cur = jQuery.css(self,c);
290                 ok( v == cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
291         });
292         start();
293 }
294
295 // Chaining Tests
296 test("Chain fadeOut fadeIn", function() {
297         $('#fadein div').saveState().fadeOut('fast').fadeIn('fast',checkState);
298 });
299 test("Chain fadeIn fadeOut", function() {
300         $('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',checkState);
301 });
302
303 test("Chain hide show", function() {
304         $('#show div').saveState().hide('fast').show('fast',checkState);
305 });
306 test("Chain show hide", function() {
307         $('#hide div').saveState().show('fast').hide('fast',checkState);
308 });
309
310 test("Chain toggle in", function() {
311         $('#togglein div').saveState().toggle('fast').toggle('fast',checkState);
312 });
313 test("Chain toggle out", function() {
314         $('#toggleout div').saveState().toggle('fast').toggle('fast',checkState);
315 });
316
317 test("Chain slideDown slideUp", function() {
318         $('#slidedown div').saveState().slideDown('fast').slideUp('fast',checkState);
319 });
320 test("Chain slideUp slideDown", function() {
321         $('#slideup div').saveState().slideUp('fast').slideDown('fast',checkState);
322 });
323
324 test("Chain slideToggle in", function() {
325         $('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',checkState);
326 });
327 test("Chain slideToggle out", function() {
328         $('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',checkState);
329 });
330
331 function makeTest( text ){
332         var elem = $("<div></div>")
333                 .attr("id", "test" + makeTest.id++)
334                 .addClass("box");
335
336         $("<h4></h4>")
337                 .text( text )
338                 .appendTo("#fx-tests")
339                 .click(function(){
340                         $(this).next().toggle();
341                 })
342                 .after( elem );
343
344         return elem;
345 }
346
347 makeTest.id = 1;