Added some tests to test repeated injected of scripts. Was fixed when fixing #5094.
[jquery.git] / test / unit / effects.js
1 module("effects");
2
3 test("show()", function() {
4         expect(23);
5         var pass = true, div = jQuery("#main div");
6         div.show().each(function(){
7                 if ( this.style.display == "none" ) pass = false;
8         });
9         ok( pass, "Show" );
10
11         var speeds = {
12           "null speed": null,
13           "undefined speed": undefined,
14           "empty string speed": "",
15           "false speed": false
16         };
17
18         jQuery.each(speeds, function(name, speed) {
19     pass = true;
20         div.hide().show(speed).each(function() {
21                 if ( this.style.display == "none" ) pass = false;
22         });
23         ok( pass, "Show with " + name);
24   });
25
26
27         jQuery.each(speeds, function(name, speed) {
28     pass = true;
29         div.hide().show(speed, function() {
30                         pass = false;
31                 });
32                 ok( pass, "Show with " + name + " does not call animate callback" );
33         });
34
35         jQuery("#main").append('<div id="show-tests"><div><p><a href="#"></a></p><code></code><pre></pre><span></span></div><table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody></table><ul><li></li></ul></div>');
36
37         var old = jQuery("#show-tests table").show().css("display") !== "table";
38
39         var test = {
40                 "div"      : "block",
41                 "p"        : "block",
42                 "a"        : "inline",
43                 "code"     : "inline",
44                 "pre"      : "block",
45                 "span"     : "inline",
46                 "table"    : old ? "block" : "table",
47                 "thead"    : old ? "block" : "table-header-group",
48                 "tbody"    : old ? "block" : "table-row-group",
49                 "tr"       : old ? "block" : "table-row",
50                 "th"       : old ? "block" : "table-cell",
51                 "td"       : old ? "block" : "table-cell",
52                 "ul"       : "block",
53                 "li"       : old ? "block" : "list-item"
54         };
55
56         jQuery.each(test, function(selector, expected) {
57                 var elem = jQuery(selector, "#show-tests").show();
58                 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
59         });
60 });
61
62 test("show(Number) - other displays", function() {
63         expect(15);
64         reset();
65         stop();
66
67         jQuery("#main").append('<div id="show-tests"><div><p><a href="#"></a></p><code></code><pre></pre><span></span></div><table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody></table><ul><li></li></ul></div>');
68
69         var old = jQuery("#show-tests table").show().css("display") !== "table",
70                 num = 0;
71
72         var test = {
73                 "div"      : "block",
74                 "p"        : "block",
75                 "a"        : "inline",
76                 "code"     : "inline",
77                 "pre"      : "block",
78                 "span"     : "inline",
79                 "table"    : old ? "block" : "table",
80                 "thead"    : old ? "block" : "table-header-group",
81                 "tbody"    : old ? "block" : "table-row-group",
82                 "tr"       : old ? "block" : "table-row",
83                 "th"       : old ? "block" : "table-cell",
84                 "td"       : old ? "block" : "table-cell",
85                 "ul"       : "block",
86                 "li"       : old ? "block" : "list-item"
87         };
88
89         jQuery.each(test, function(selector, expected) {
90                 var elem = jQuery(selector, "#show-tests").show(1, function() {
91                         equals( elem.css("display"), expected, "Show using correct display type for " + selector );
92                         if ( ++num === 15 ) {
93                                 start();
94                         }
95                 });
96         });
97 });
98
99 test("animate(Hash, Object, Function)", function() {
100         expect(1);
101         stop();
102         var hash = {opacity: 'show'};
103         var hashCopy = jQuery.extend({}, hash);
104         jQuery('#foo').animate(hash, 0, function() {
105                 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
106                 start();
107         });
108 });
109
110 test("animate negative height", function() {
111         expect(1);
112         stop();
113         jQuery("#foo").animate({ height: -100 }, 100, function() {
114                 equals( this.offsetHeight, 0, "Verify height." );
115                 start();
116         });
117 });
118
119 /* // This test ends up being flaky depending upon the CPU load
120 test("animate option (queue === false)", function () {
121         expect(1);
122         stop();
123
124         var order = [];
125
126         var $foo = jQuery("#foo");
127         $foo.animate({width:'100px'}, 3000, function () {
128                 // should finish after unqueued animation so second
129                 order.push(2);
130                 same( order, [ 1, 2 ], "Animations finished in the correct order" );
131                 start();
132         });
133         $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
134                 // short duration and out of queue so should finish first
135                 order.push(1);
136         }});
137 });
138 */
139
140 test("animate with no properties", function() {
141         expect(2);
142         
143         var divs = jQuery("div"), count = 0;
144
145         divs.animate({}, function(){
146                 count++;
147         });
148
149         equals( divs.length, count, "Make sure that callback is called for each element in the set." );
150
151         stop();
152
153         var foo = jQuery("#foo");
154
155         foo.animate({});
156         foo.animate({top: 10}, 100, function(){
157                 ok( true, "Animation was properly dequeued." );
158                 start();
159         });
160 });
161
162 test("animate duration 0", function() {
163         expect(11);
164         
165         stop();
166         
167         var $elems = jQuery([{ a:0 },{ a:0 }]), counter = 0;
168         
169         equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
170                 
171         $elems.eq(0).animate( {a:1}, 0, function(){
172                 ok( true, "Animate a simple property." );
173                 counter++;
174         });
175         
176         // Failed until [6115]
177         equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
178         
179         equals( counter, 1, "One synchronic animations" );
180         
181         $elems.animate( { a:2 }, 0, function(){
182                 ok( true, "Animate a second simple property." );
183                 counter++;
184         });
185         
186         equals( counter, 3, "Multiple synchronic animations" );
187         
188         $elems.eq(0).animate( {a:3}, 0, function(){
189                 ok( true, "Animate a third simple property." );
190                 counter++;
191         });
192         $elems.eq(1).animate( {a:3}, 200, function(){
193                 counter++;
194                 // Failed until [6115]
195                 equals( counter, 5, "One synchronic and one asynchronic" );
196                 start();
197         });
198         
199         var $elem = jQuery("<div />");
200         $elem.show(0, function(){ 
201                 ok(true, "Show callback with no duration");
202         });
203         $elem.hide(0, function(){ 
204                 ok(true, "Hide callback with no duration");
205         });
206 });
207
208 test("animate hyphenated properties", function(){
209         expect(1);
210         stop();
211
212         jQuery("#nothiddendiv")
213                 .css("font-size", 10)
214                 .animate({"font-size": 20}, 200, function(){
215                         equals( this.style.fontSize, "20px", "The font-size property was animated." );
216                         start();
217                 });
218 });
219
220 test("animate non-element", function(){
221         expect(1);
222         stop();
223
224         var obj = { test: 0 };
225
226         jQuery(obj).animate({test: 200}, 200, function(){
227                 equals( obj.test, 200, "The custom property should be modified." );
228                 start();
229         });
230 });
231
232 test("stop()", function() {
233         expect(3);
234         stop();
235
236         var $foo = jQuery("#nothiddendiv");
237         var w = 0;
238         $foo.hide().width(200).width();
239
240         $foo.animate({ width:'show' }, 1000);
241         setTimeout(function(){
242                 var nw = $foo.width();
243                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
244                 $foo.stop();
245
246                 nw = $foo.width();
247                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
248                 setTimeout(function(){
249                         equals( nw, $foo.width(), "The animation didn't continue" );
250                         start();
251                 }, 100);
252         }, 100);
253 });
254
255 test("stop() - several in queue", function() {
256         expect(3);
257         stop();
258
259         var $foo = jQuery("#nothiddendivchild");
260         var w = 0;
261         $foo.hide().width(200).width();
262
263         $foo.animate({ width:'show' }, 1000);
264         $foo.animate({ width:'hide' }, 1000);
265         $foo.animate({ width:'show' }, 1000);
266         setTimeout(function(){
267                 equals( $foo.queue().length, 3, "All 3 still in the queue" );
268                 var nw = $foo.width();
269                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
270                 $foo.stop();
271
272                 nw = $foo.width();
273                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
274                 // Disabled, being flaky
275                 //equals( $foo.queue().length, 1, "The next animation continued" );
276                 $foo.stop(true);
277                 start();
278         }, 100);
279 });
280
281 test("stop(clearQueue)", function() {
282         expect(4);
283         stop();
284
285         var $foo = jQuery("#nothiddendiv");
286         var w = 0;
287         $foo.hide().width(200).width();
288
289         $foo.animate({ width:'show' }, 1000);
290         $foo.animate({ width:'hide' }, 1000);
291         $foo.animate({ width:'show' }, 1000);
292         setTimeout(function(){
293                 var nw = $foo.width();
294                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
295                 $foo.stop(true);
296
297                 nw = $foo.width();
298                 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
299
300                 equals( $foo.queue().length, 0, "The animation queue was cleared" );
301                 setTimeout(function(){
302                         equals( nw, $foo.width(), "The animation didn't continue" );
303                         start();
304                 }, 100);
305         }, 100);
306 });
307
308 test("stop(clearQueue, gotoEnd)", function() {
309         expect(1);
310         stop();
311
312         var $foo = jQuery("#nothiddendivchild");
313         var w = 0;
314         $foo.hide().width(200).width();
315
316         $foo.animate({ width:'show' }, 1000);
317         $foo.animate({ width:'hide' }, 1000);
318         $foo.animate({ width:'show' }, 1000);
319         $foo.animate({ width:'hide' }, 1000);
320         setTimeout(function(){
321                 var nw = $foo.width();
322                 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
323                 $foo.stop(false, true);
324
325                 nw = $foo.width();
326                 // Disabled, being flaky
327                 //equals( nw, 1, "Stop() reset the animation" );
328
329                 setTimeout(function(){
330                         // Disabled, being flaky
331                         //equals( $foo.queue().length, 2, "The next animation continued" );
332                         $foo.stop(true);
333                         start();
334                 }, 100);
335         }, 100);
336 });
337
338 test("toggle()", function() {
339         expect(6);
340         var x = jQuery("#nothiddendiv");
341         ok( x.is(":visible"), "is visible" );
342         x.toggle();
343         ok( x.is(":hidden"), "is hidden" );
344         x.toggle();
345         ok( x.is(":visible"), "is visible again" );
346         
347         x.toggle(true);
348         ok( x.is(":visible"), "is visible" );
349         x.toggle(false);
350         ok( x.is(":hidden"), "is hidden" );
351         x.toggle(true);
352         ok( x.is(":visible"), "is visible again" );
353 });
354
355 jQuery.checkOverflowDisplay = function(){
356         var o = jQuery.css( this, "overflow" );
357
358         equals(o, "visible", "Overflow should be visible: " + o);
359         equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
360
361         start();
362 }
363
364 test("JS Overflow and Display", function() {
365         expect(2);
366         stop();
367         jQuery.makeTest( "JS Overflow and Display" )
368                 .addClass("widewidth")
369                 .css({ overflow: "visible", display: "inline" })
370                 .addClass("widewidth")
371                 .text("Some sample text.")
372                 .before("text before")
373                 .after("text after")
374                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
375 });
376                 
377 test("CSS Overflow and Display", function() {
378         expect(2);
379         stop();
380         jQuery.makeTest( "CSS Overflow and Display" )
381                 .addClass("overflow inline")
382                 .addClass("widewidth")
383                 .text("Some sample text.")
384                 .before("text before")
385                 .after("text after")
386                 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
387 });
388
389 jQuery.each( {
390         "CSS Auto": function(elem,prop){
391                 jQuery(elem).addClass("auto" + prop)
392                         .text("This is a long string of text.");
393                 return "";
394         },
395         "JS Auto": function(elem,prop){
396                 jQuery(elem).css(prop,"auto")
397                         .text("This is a long string of text.");
398                 return "";
399         },
400         "CSS 100": function(elem,prop){
401                 jQuery(elem).addClass("large" + prop);
402                 return "";
403         },
404         "JS 100": function(elem,prop){
405                 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
406                 return prop == "opacity" ? 1 : 100;
407         },
408         "CSS 50": function(elem,prop){
409                 jQuery(elem).addClass("med" + prop);
410                 return "";
411         },
412         "JS 50": function(elem,prop){
413                 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
414                 return prop == "opacity" ? 0.5 : 50;
415         },
416         "CSS 0": function(elem,prop){
417                 jQuery(elem).addClass("no" + prop);
418                 return "";
419         },
420         "JS 0": function(elem,prop){
421                 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
422                 return 0;
423         }
424 }, function(fn, f){
425         jQuery.each( {
426                 "show": function(elem,prop){
427                         jQuery(elem).hide().addClass("wide"+prop);
428                         return "show";
429                 },
430                 "hide": function(elem,prop){
431                         jQuery(elem).addClass("wide"+prop);
432                         return "hide";
433                 },
434                 "100": function(elem,prop){
435                         jQuery(elem).addClass("wide"+prop);
436                         return prop == "opacity" ? 1 : 100;
437                 },
438                 "50": function(elem,prop){
439                         return prop == "opacity" ? 0.50 : 50;
440                 },
441                 "0": function(elem,prop){
442                         jQuery(elem).addClass("noback");
443                         return 0;
444                 }
445         }, function(tn, t){
446                 test(fn + " to " + tn, function() {
447                         var elem = jQuery.makeTest( fn + " to " + tn );
448         
449                         var t_w = t( elem, "width" );
450                         var f_w = f( elem, "width" );
451                         var t_h = t( elem, "height" );
452                         var f_h = f( elem, "height" );
453                         var t_o = t( elem, "opacity" );
454                         var f_o = f( elem, "opacity" );
455                         
456                         var num = 0;
457                         
458                         if ( t_h == "show" ) num++;
459                         if ( t_w == "show" ) num++;
460                         if ( t_w == "hide"||t_w == "show" ) num++;
461                         if ( t_h == "hide"||t_h == "show" ) num++;
462                         if ( t_o == "hide"||t_o == "show" ) num++;
463                         if ( t_w == "hide" ) num++;
464                         if ( t_o.constructor == Number ) num += 2;
465                         if ( t_w.constructor == Number ) num += 2;
466                         if ( t_h.constructor == Number ) num +=2;
467                         
468                         expect(num);
469                         stop();
470         
471                         var anim = { width: t_w, height: t_h, opacity: t_o };
472         
473                         elem.animate(anim, 50, function(){
474                                 if ( t_w == "show" )
475                                         equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
476                                         
477                                 if ( t_w == "hide"||t_w == "show" )
478                                         equals(this.style.width.indexOf(f_w), 0, "Width must be reset to " + f_w + ": " + this.style.width);
479                                         
480                                 if ( t_h == "hide"||t_h == "show" )
481                                         equals(this.style.height.indexOf(f_h), 0, "Height must be reset to " + f_h + ": " + this.style.height);
482                                         
483                                 var cur_o = jQuery.style(this, "opacity");
484                                 if ( cur_o !== "" ) cur_o = parseFloat( cur_o );
485         
486                                 if ( t_o == "hide"||t_o == "show" )
487                                         equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
488                                         
489                                 if ( t_w == "hide" )
490                                         equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
491                                         
492                                 if ( t_o.constructor == Number ) {
493                                         equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
494                                         
495                                         ok(jQuery.curCSS(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
496                                 }
497                                         
498                                 if ( t_w.constructor == Number ) {
499                                         equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
500                                         
501                                         var cur_w = jQuery.css(this,"width");
502
503                                         ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
504                                 }
505                                         
506                                 if ( t_h.constructor == Number ) {
507                                         equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
508                                         
509                                         var cur_h = jQuery.css(this,"height");
510
511                                         ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
512                                 }
513                                 
514                                 if ( t_h == "show" ) {
515                                         var old_h = jQuery.curCSS(this, "height");
516                                         jQuery(elem).append("<br/>Some more text<br/>and some more...");
517                                         ok(old_h != jQuery.css(this, "height" ), "Make sure height is auto.");
518                                 }
519         
520                                 start();
521                         });
522                 });
523         });
524 });
525
526 jQuery.fn.saveState = function(){
527         var check = ['opacity','height','width','display','overflow'];  
528         expect(check.length);
529         
530         stop();
531         return this.each(function(){
532                 var self = this;
533                 self.save = {};
534                 jQuery.each(check, function(i,c){
535                         self.save[c] = jQuery.css(self,c);
536                 });
537         });
538 };
539
540 jQuery.checkState = function(){
541         var self = this;
542         jQuery.each(this.save, function(c,v){
543                 var cur = jQuery.css(self,c);
544                 equals( v, cur, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
545         });
546         start();
547 }
548
549 // Chaining Tests
550 test("Chain fadeOut fadeIn", function() {
551         jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
552 });
553 test("Chain fadeIn fadeOut", function() {
554         jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
555 });
556
557 test("Chain hide show", function() {
558         jQuery('#show div').saveState().hide('fast').show('fast',jQuery.checkState);
559 });
560 test("Chain show hide", function() {
561         jQuery('#hide div').saveState().show('fast').hide('fast',jQuery.checkState);
562 });
563
564 test("Chain toggle in", function() {
565         jQuery('#togglein div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
566 });
567 test("Chain toggle out", function() {
568         jQuery('#toggleout div').saveState().toggle('fast').toggle('fast',jQuery.checkState);
569 });
570
571 test("Chain slideDown slideUp", function() {
572         jQuery('#slidedown div').saveState().slideDown('fast').slideUp('fast',jQuery.checkState);
573 });
574 test("Chain slideUp slideDown", function() {
575         jQuery('#slideup div').saveState().slideUp('fast').slideDown('fast',jQuery.checkState);
576 });
577
578 test("Chain slideToggle in", function() {
579         jQuery('#slidetogglein div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
580 });
581 test("Chain slideToggle out", function() {
582         jQuery('#slidetoggleout div').saveState().slideToggle('fast').slideToggle('fast',jQuery.checkState);
583 });
584
585 jQuery.makeTest = function( text ){
586         var elem = jQuery("<div></div>")
587                 .attr("id", "test" + jQuery.makeTest.id++)
588                 .addClass("box");
589
590         jQuery("<h4></h4>")
591                 .text( text )
592                 .appendTo("#fx-tests")
593                 .click(function(){
594                         jQuery(this).next().toggle();
595                 })
596                 .after( elem );
597
598         return elem;
599 }
600
601 jQuery.makeTest.id = 1;
602
603 test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
604         expect(4);
605   stop();
606
607         var $checkedtest = jQuery("#checkedtest");
608         // IE6 was clearing "checked" in jQuery(elem).show("fast");
609         $checkedtest.hide().show("fast", function() {
610         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
611         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
612         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
613         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
614         start();
615         });
616 });
617
618 test("animate with per-property easing", function(){
619         
620         expect(3);
621         stop();
622         
623         var _test1_called = false;
624         var _test2_called = false;
625         var _default_test_called = false;
626         
627         jQuery.easing['_test1'] = function() {
628                 _test1_called = true;
629         };
630         
631         jQuery.easing['_test2'] = function() {
632                 _test2_called = true;
633         };
634         
635         jQuery.easing['_default_test'] = function() {
636                 _default_test_called = true;
637         };
638         
639         jQuery({a:0,b:0,c:0}).animate({
640                 a: [100, '_test1'],
641                 b: [100, '_test2'],
642                 c: 100
643         }, 400, '_default_test', function(){
644                 start();
645                 ok(_test1_called, "Easing function (1) called");
646                 ok(_test2_called, "Easing function (2) called");
647                 ok(_default_test_called, "Easing function (_default) called");
648         });
649         
650 });