3 test("sanity check", function() {
5 ok( jQuery("#dl:visible, #main:visible, #foo:visible").length === 3, "QUnit state is correct for testing effects" );
8 test("show()", function() {
10 var pass = true, div = jQuery("#main div");
11 div.show().each(function(){
12 if ( this.style.display == "none" ) pass = false;
18 "undefined speed": undefined,
19 "empty string speed": "",
23 jQuery.each(speeds, function(name, speed) {
25 div.hide().show(speed).each(function() {
26 if ( this.style.display == "none" ) pass = false;
28 ok( pass, "Show with " + name);
32 jQuery.each(speeds, function(name, speed) {
34 div.hide().show(speed, function() {
37 ok( pass, "Show with " + name + " does not call animate callback" );
40 // #show-tests * is set display: none in CSS
41 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>');
43 var old = jQuery("#show-tests table").show().css("display") !== "table";
52 "table" : old ? "block" : "table",
53 "thead" : old ? "block" : "table-header-group",
54 "tbody" : old ? "block" : "table-row-group",
55 "tr" : old ? "block" : "table-row",
56 "th" : old ? "block" : "table-cell",
57 "td" : old ? "block" : "table-cell",
59 "li" : old ? "block" : "list-item"
62 jQuery.each(test, function(selector, expected) {
63 var elem = jQuery(selector, "#show-tests").show();
64 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
68 test("show(Number) - other displays", function() {
73 // #show-tests * is set display: none in CSS
74 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>');
76 var old = jQuery("#show-tests table").show().css("display") !== "table",
86 "table" : old ? "block" : "table",
87 "thead" : old ? "block" : "table-header-group",
88 "tbody" : old ? "block" : "table-row-group",
89 "tr" : old ? "block" : "table-row",
90 "th" : old ? "block" : "table-cell",
91 "td" : old ? "block" : "table-cell",
93 "li" : old ? "block" : "list-item"
96 jQuery.each(test, function(selector, expected) {
97 // IE sometimes has issues with chained functions referencing
98 // assignments from outside the closure
99 var elem = jQuery(selector, "#show-tests");
100 elem.show(1, function() {
101 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
102 if ( ++num === 15 ) {
109 test("animate(Hash, Object, Function)", function() {
112 var hash = {opacity: 'show'};
113 var hashCopy = jQuery.extend({}, hash);
114 jQuery('#foo').animate(hash, 0, function() {
115 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
120 test("animate negative height", function() {
123 jQuery("#foo").animate({ height: -100 }, 100, function() {
124 equals( this.offsetHeight, 0, "Verify height." );
129 test("animate block as inline width/height", function() {
132 jQuery("#foo").css({ display: "inline", width: '', height: '' }).animate({ width: 42, height: 42 }, 100, function() {
133 equals( jQuery(this).css("display"), jQuery.support.inlineBlockNeedsLayout ? "inline" : "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
134 equals( this.offsetWidth, 42, "width was animated" );
135 equals( this.offsetHeight, 42, "height was animated" );
140 test("animate native inline width/height", function() {
143 jQuery("#foo").css({ display: "", width: '', height: '' })
144 .append('<span>text</span>')
146 .animate({ width: 42, height: 42 }, 100, function() {
147 equals( jQuery(this).css("display"), "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
148 equals( this.offsetWidth, 42, "width was animated" );
149 equals( this.offsetHeight, 42, "height was animated" );
154 test("animate block width/height", function() {
157 jQuery("#foo").css({ display: "block", width: 20, height: 20 }).animate({ width: 42, height: 42 }, 100, function() {
158 equals( jQuery(this).css("display"), "block", "inline-block was not set on block element when animating width/height" );
159 equals( this.offsetWidth, 42, "width was animated" );
160 equals( this.offsetHeight, 42, "height was animated" );
165 test("animate table width/height", function() {
169 var displayMode = jQuery("#table").css("display") !== "table" ? "block" : "table";
171 jQuery("#table").animate({ width: 42, height: 42 }, 100, function() {
172 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
177 test("animate table-row width/height", function() {
180 var tr = jQuery("#table")
181 .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
182 .html("<tr style='height:42px;'><td style='padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
185 // IE<8 uses “block” instead of the correct display type
186 var displayMode = tr.css("display") !== "table-row" ? "block" : "table-row";
188 tr.animate({ width: 10, height: 10 }, 100, function() {
189 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
190 equals( this.offsetWidth, 20, "width animated to shrink wrap point" );
191 equals( this.offsetHeight, 20, "height animated to shrink wrap point" );
196 test("animate table-cell width/height", function() {
199 var td = jQuery("#table")
200 .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
201 .html("<tr><td style='width:42px;height:42px;padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
204 // IE<8 uses “block” instead of the correct display type
205 var displayMode = td.css("display") !== "table-cell" ? "block" : "table-cell";
207 td.animate({ width: 10, height: 10 }, 100, function() {
208 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
209 equals( this.offsetWidth, 20, "width animated to shrink wrap point" );
210 equals( this.offsetHeight, 20, "height animated to shrink wrap point" );
215 test("animate resets overflow-x and overflow-y when finished", function() {
219 .css({ display: "block", width: 20, height: 20, overflowX: "visible", overflowY: "auto" })
220 .animate({ width: 42, height: 42 }, 100, function() {
221 equals( this.style.overflowX, "visible", "overflow-x is visible" );
222 equals( this.style.overflowY, "auto", "overflow-y is auto" );
227 /* // This test ends up being flaky depending upon the CPU load
228 test("animate option (queue === false)", function () {
234 var $foo = jQuery("#foo");
235 $foo.animate({width:'100px'}, 3000, function () {
236 // should finish after unqueued animation so second
238 same( order, [ 1, 2 ], "Animations finished in the correct order" );
241 $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
242 // short duration and out of queue so should finish first
248 test("animate with no properties", function() {
251 var divs = jQuery("div"), count = 0;
253 divs.animate({}, function(){
257 equals( divs.length, count, "Make sure that callback is called for each element in the set." );
261 var foo = jQuery("#foo");
264 foo.animate({top: 10}, 100, function(){
265 ok( true, "Animation was properly dequeued." );
270 test("animate duration 0", function() {
275 var $elems = jQuery([{ a:0 },{ a:0 }]), counter = 0;
277 equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
279 $elems.eq(0).animate( {a:1}, 0, function(){
280 ok( true, "Animate a simple property." );
284 // Failed until [6115]
285 equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
287 equals( counter, 1, "One synchronic animations" );
289 $elems.animate( { a:2 }, 0, function(){
290 ok( true, "Animate a second simple property." );
294 equals( counter, 3, "Multiple synchronic animations" );
296 $elems.eq(0).animate( {a:3}, 0, function(){
297 ok( true, "Animate a third simple property." );
300 $elems.eq(1).animate( {a:3}, 200, function(){
302 // Failed until [6115]
303 equals( counter, 5, "One synchronic and one asynchronic" );
307 var $elem = jQuery("<div />");
308 $elem.show(0, function(){
309 ok(true, "Show callback with no duration");
311 $elem.hide(0, function(){
312 ok(true, "Hide callback with no duration");
316 test("animate hyphenated properties", function(){
320 jQuery("#nothiddendiv")
321 .css("font-size", 10)
322 .animate({"font-size": 20}, 200, function(){
323 equals( this.style.fontSize, "20px", "The font-size property was animated." );
328 test("animate non-element", function(){
332 var obj = { test: 0 };
334 jQuery(obj).animate({test: 200}, 200, function(){
335 equals( obj.test, 200, "The custom property should be modified." );
340 test("stop()", function() {
344 var $foo = jQuery("#nothiddendiv");
346 $foo.hide().width(200).width();
348 $foo.animate({ width:'show' }, 1000);
349 setTimeout(function(){
350 var nw = $foo.width();
351 notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px");
355 notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px");
356 setTimeout(function(){
357 equals( nw, $foo.width(), "The animation didn't continue" );
363 test("stop() - several in queue", function() {
367 var $foo = jQuery("#nothiddendivchild");
369 $foo.hide().width(200).width();
371 $foo.animate({ width:'show' }, 1000);
372 $foo.animate({ width:'hide' }, 1000);
373 $foo.animate({ width:'show' }, 1000);
374 setTimeout(function(){
375 equals( $foo.queue().length, 3, "All 3 still in the queue" );
376 var nw = $foo.width();
377 notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px");
381 notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px");
388 test("stop(clearQueue)", function() {
392 var $foo = jQuery("#nothiddendiv");
394 $foo.hide().width(200).width();
396 $foo.animate({ width:'show' }, 1000);
397 $foo.animate({ width:'hide' }, 1000);
398 $foo.animate({ width:'show' }, 1000);
399 setTimeout(function(){
400 var nw = $foo.width();
401 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
405 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
407 equals( $foo.queue().length, 0, "The animation queue was cleared" );
408 setTimeout(function(){
409 equals( nw, $foo.width(), "The animation didn't continue" );
415 test("stop(clearQueue, gotoEnd)", function() {
419 var $foo = jQuery("#nothiddendivchild");
421 $foo.hide().width(200).width();
423 $foo.animate({ width:'show' }, 1000);
424 $foo.animate({ width:'hide' }, 1000);
425 $foo.animate({ width:'show' }, 1000);
426 $foo.animate({ width:'hide' }, 1000);
427 setTimeout(function(){
428 var nw = $foo.width();
429 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
430 $foo.stop(false, true);
433 // Disabled, being flaky
434 //equals( nw, 1, "Stop() reset the animation" );
436 setTimeout(function(){
437 // Disabled, being flaky
438 //equals( $foo.queue().length, 2, "The next animation continued" );
445 test("toggle()", function() {
447 var x = jQuery("#nothiddendiv");
448 ok( x.is(":visible"), "is visible" );
450 ok( x.is(":hidden"), "is hidden" );
452 ok( x.is(":visible"), "is visible again" );
455 ok( x.is(":visible"), "is visible" );
457 ok( x.is(":hidden"), "is hidden" );
459 ok( x.is(":visible"), "is visible again" );
462 jQuery.checkOverflowDisplay = function(){
463 var o = jQuery.css( this, "overflow" );
465 equals(o, "visible", "Overflow should be visible: " + o);
466 equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
471 test("JS Overflow and Display", function() {
474 jQuery.makeTest( "JS Overflow and Display" )
475 .addClass("widewidth")
476 .css({ overflow: "visible", display: "inline" })
477 .addClass("widewidth")
478 .text("Some sample text.")
479 .before("text before")
481 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
484 test("CSS Overflow and Display", function() {
487 jQuery.makeTest( "CSS Overflow and Display" )
488 .addClass("overflow inline")
489 .addClass("widewidth")
490 .text("Some sample text.")
491 .before("text before")
493 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
497 "CSS Auto": function(elem,prop){
498 jQuery(elem).addClass("auto" + prop)
499 .text("This is a long string of text.");
502 "JS Auto": function(elem,prop){
503 jQuery(elem).css(prop,"")
504 .text("This is a long string of text.");
507 "CSS 100": function(elem,prop){
508 jQuery(elem).addClass("large" + prop);
511 "JS 100": function(elem,prop){
512 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
513 return prop == "opacity" ? 1 : 100;
515 "CSS 50": function(elem,prop){
516 jQuery(elem).addClass("med" + prop);
519 "JS 50": function(elem,prop){
520 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
521 return prop == "opacity" ? 0.5 : 50;
523 "CSS 0": function(elem,prop){
524 jQuery(elem).addClass("no" + prop);
527 "JS 0": function(elem,prop){
528 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
533 "show": function(elem,prop){
534 jQuery(elem).hide().addClass("wide"+prop);
537 "hide": function(elem,prop){
538 jQuery(elem).addClass("wide"+prop);
541 "100": function(elem,prop){
542 jQuery(elem).addClass("wide"+prop);
543 return prop == "opacity" ? 1 : 100;
545 "50": function(elem,prop){
546 return prop == "opacity" ? 0.50 : 50;
548 "0": function(elem,prop){
549 jQuery(elem).addClass("noback");
553 test(fn + " to " + tn, function() {
554 var elem = jQuery.makeTest( fn + " to " + tn );
556 var t_w = t( elem, "width" );
557 var f_w = f( elem, "width" );
558 var t_h = t( elem, "height" );
559 var f_h = f( elem, "height" );
560 var t_o = t( elem, "opacity" );
561 var f_o = f( elem, "opacity" );
565 if ( t_h == "show" ) num++;
566 if ( t_w == "show" ) num++;
567 if ( t_w == "hide"||t_w == "show" ) num++;
568 if ( t_h == "hide"||t_h == "show" ) num++;
569 if ( t_o == "hide"||t_o == "show" ) num++;
570 if ( t_w == "hide" ) num++;
571 if ( t_o.constructor == Number ) num += 2;
572 if ( t_w.constructor == Number ) num += 2;
573 if ( t_h.constructor == Number ) num +=2;
578 var anim = { width: t_w, height: t_h, opacity: t_o };
580 elem.animate(anim, 50, function(){
582 equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
584 if ( t_w == "hide"||t_w == "show" )
585 ok(f_w === "" ? this.style.width === f_w : this.style.width.indexOf(f_w) === 0, "Width must be reset to " + f_w + ": " + this.style.width);
587 if ( t_h == "hide"||t_h == "show" )
588 ok(f_h === "" ? this.style.height === f_h : this.style.height.indexOf(f_h) === 0, "Height must be reset to " + f_h + ": " + this.style.height);
590 var cur_o = jQuery.style(this, "opacity");
592 if ( t_o == "hide" || t_o == "show" )
593 equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
596 equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
598 if ( t_o.constructor == Number ) {
599 equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
601 ok(jQuery.css(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
604 if ( t_w.constructor == Number ) {
605 equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
607 var cur_w = jQuery.css(this,"width");
609 ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
612 if ( t_h.constructor == Number ) {
613 equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
615 var cur_h = jQuery.css(this,"height");
617 ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
620 if ( t_h == "show" ) {
621 var old_h = jQuery.css(this, "height");
622 jQuery(this).append("<br/>Some more text<br/>and some more...");
624 if ( /Auto/.test( fn ) ) {
625 notEqual(jQuery.css(this, "height"), old_h, "Make sure height is auto.");
627 equals(jQuery.css(this, "height"), old_h, "Make sure height is not auto.");
637 jQuery.fn.saveState = function(hiddenOverflow){
638 var check = ['opacity','height','width','display','overflow'];
639 expect(check.length);
642 return this.each(function(){
645 jQuery.each(check, function(i,c){
646 self.save[c] = c === "overflow" && hiddenOverflow ? "hidden" : self.style[ c ] || jQuery.css(self,c);
651 jQuery.checkState = function(){
653 jQuery.each(this.save, function(c,v){
654 var cur = self.style[ c ] || jQuery.css(self, c);
655 equals( cur, v, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
661 test("Chain fadeOut fadeIn", function() {
662 jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
664 test("Chain fadeIn fadeOut", function() {
665 jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
668 test("Chain hide show", function() {
669 jQuery('#show div').saveState(jQuery.support.shrinkWrapBlocks).hide('fast').show('fast',jQuery.checkState);
671 test("Chain show hide", function() {
672 jQuery('#hide div').saveState(jQuery.support.shrinkWrapBlocks).show('fast').hide('fast',jQuery.checkState);
674 test("Chain show hide with easing and callback", function() {
675 jQuery('#hide div').saveState().show('fast').hide('fast','linear',jQuery.checkState);
678 test("Chain toggle in", function() {
679 jQuery('#togglein div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast',jQuery.checkState);
681 test("Chain toggle out", function() {
682 jQuery('#toggleout div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast',jQuery.checkState);
684 test("Chain toggle out with easing and callback", function() {
685 jQuery('#toggleout div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast','linear',jQuery.checkState);
687 test("Chain slideDown slideUp", function() {
688 jQuery('#slidedown div').saveState(jQuery.support.shrinkWrapBlocks).slideDown('fast').slideUp('fast',jQuery.checkState);
690 test("Chain slideUp slideDown", function() {
691 jQuery('#slideup div').saveState(jQuery.support.shrinkWrapBlocks).slideUp('fast').slideDown('fast',jQuery.checkState);
693 test("Chain slideUp slideDown with easing and callback", function() {
694 jQuery('#slideup div').saveState(jQuery.support.shrinkWrapBlocks).slideUp('fast').slideDown('fast','linear',jQuery.checkState);
697 test("Chain slideToggle in", function() {
698 jQuery('#slidetogglein div').saveState(jQuery.support.shrinkWrapBlocks).slideToggle('fast').slideToggle('fast',jQuery.checkState);
700 test("Chain slideToggle out", function() {
701 jQuery('#slidetoggleout div').saveState(jQuery.support.shrinkWrapBlocks).slideToggle('fast').slideToggle('fast',jQuery.checkState);
704 test("Chain fadeTo 0.5 1.0 with easing and callback)", function() {
705 jQuery('#fadeto div').saveState().fadeTo('fast',0.5).fadeTo('fast',1.0,'linear',jQuery.checkState);
708 jQuery.makeTest = function( text ){
709 var elem = jQuery("<div></div>")
710 .attr("id", "test" + jQuery.makeTest.id++)
715 .appendTo("#fx-tests")
717 jQuery(this).next().toggle();
724 jQuery.makeTest.id = 1;
726 test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
730 var $checkedtest = jQuery("#checkedtest");
731 // IE6 was clearing "checked" in jQuery(elem).show("fast");
732 $checkedtest.hide().show("fast", function() {
733 ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
734 ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
735 ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
736 ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
741 test("animate with per-property easing", function(){
746 var _test1_called = false;
747 var _test2_called = false;
748 var _default_test_called = false;
750 jQuery.easing['_test1'] = function() {
751 _test1_called = true;
754 jQuery.easing['_test2'] = function() {
755 _test2_called = true;
758 jQuery.easing['_default_test'] = function() {
759 _default_test_called = true;
762 jQuery({a:0,b:0,c:0}).animate({
766 }, 400, '_default_test', function(){
768 ok(_test1_called, "Easing function (1) called");
769 ok(_test2_called, "Easing function (2) called");
770 ok(_default_test_called, "Easing function (_default) called");