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