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><table id="test-table"></table>');
43 var old = jQuery("#test-table").show().css("display") !== "table";
44 jQuery("#test-table").remove();
53 "table" : old ? "block" : "table",
54 "thead" : old ? "block" : "table-header-group",
55 "tbody" : old ? "block" : "table-row-group",
56 "tr" : old ? "block" : "table-row",
57 "th" : old ? "block" : "table-cell",
58 "td" : old ? "block" : "table-cell",
60 "li" : old ? "block" : "list-item"
63 jQuery.each(test, function(selector, expected) {
64 var elem = jQuery(selector, "#show-tests").show();
65 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
69 test("show(Number) - other displays", function() {
74 // #show-tests * is set display: none in CSS
75 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><table id="test-table"></table>');
77 var old = jQuery("#test-table").show().css("display") !== "table",
79 jQuery("#test-table").remove();
88 "table" : old ? "block" : "table",
89 "thead" : old ? "block" : "table-header-group",
90 "tbody" : old ? "block" : "table-row-group",
91 "tr" : old ? "block" : "table-row",
92 "th" : old ? "block" : "table-cell",
93 "td" : old ? "block" : "table-cell",
95 "li" : old ? "block" : "list-item"
98 jQuery.each(test, function(selector, expected) {
99 var elem = jQuery(selector, "#show-tests").show(1, function() {
100 equals( elem.css("display"), expected, "Show using correct display type for " + selector );
101 if ( ++num === 15 ) {
108 test("animate(Hash, Object, Function)", function() {
111 var hash = {opacity: 'show'};
112 var hashCopy = jQuery.extend({}, hash);
113 jQuery('#foo').animate(hash, 0, function() {
114 equals( hash.opacity, hashCopy.opacity, 'Check if animate changed the hash parameter' );
119 test("animate negative height", function() {
122 jQuery("#foo").animate({ height: -100 }, 100, function() {
123 equals( this.offsetHeight, 0, "Verify height." );
128 test("animate block as inline width/height", function() {
131 var span = jQuery("<span>").css("display", "inline-block").appendTo("body"),
132 expected = span.css("display");
136 if ( jQuery.support.inlineBlockNeedsLayout || expected === "inline-block" ) {
139 jQuery("#foo").css({ display: "inline", width: '', height: '' }).animate({ width: 42, height: 42 }, 100, function() {
140 equals( jQuery(this).css("display"), jQuery.support.inlineBlockNeedsLayout ? "inline" : "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
141 equals( this.offsetWidth, 42, "width was animated" );
142 equals( this.offsetHeight, 42, "height was animated" );
146 // Browser doesn't support inline-block
148 ok( true, "Browser doesn't support inline-block" );
149 ok( true, "Browser doesn't support inline-block" );
150 ok( true, "Browser doesn't support inline-block" );
154 test("animate native inline width/height", function() {
157 var span = jQuery("<span>").css("display", "inline-block").appendTo("body"),
158 expected = span.css("display");
162 if ( jQuery.support.inlineBlockNeedsLayout || expected === "inline-block" ) {
164 jQuery("#foo").css({ display: "", width: '', height: '' })
165 .append('<span>text</span>')
167 .animate({ width: 42, height: 42 }, 100, function() {
168 equals( jQuery(this).css("display"), "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
169 equals( this.offsetWidth, 42, "width was animated" );
170 equals( this.offsetHeight, 42, "height was animated" );
174 // Browser doesn't support inline-block
176 ok( true, "Browser doesn't support inline-block" );
177 ok( true, "Browser doesn't support inline-block" );
178 ok( true, "Browser doesn't support inline-block" );
182 test("animate block width/height", function() {
185 jQuery("#foo").css({ display: "block", width: 20, height: 20 }).animate({ width: 42, height: 42 }, 100, function() {
186 equals( jQuery(this).css("display"), "block", "inline-block was not set on block element when animating width/height" );
187 equals( this.offsetWidth, 42, "width was animated" );
188 equals( this.offsetHeight, 42, "height was animated" );
193 test("animate table width/height", function() {
197 var displayMode = jQuery("#table").css("display") !== "table" ? "block" : "table";
199 jQuery("#table").animate({ width: 42, height: 42 }, 100, function() {
200 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
205 test("animate table-row width/height", function() {
208 var tr = jQuery("#table")
209 .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
210 .html("<tr style='height:42px;'><td style='padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
213 // IE<8 uses “block” instead of the correct display type
214 var displayMode = tr.css("display") !== "table-row" ? "block" : "table-row";
216 tr.animate({ width: 10, height: 10 }, 100, function() {
217 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
218 equals( this.offsetWidth, 20, "width animated to shrink wrap point" );
219 equals( this.offsetHeight, 20, "height animated to shrink wrap point" );
224 test("animate table-cell width/height", function() {
227 var td = jQuery("#table")
228 .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
229 .html("<tr><td style='width:42px;height:42px;padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
232 // IE<8 uses “block” instead of the correct display type
233 var displayMode = td.css("display") !== "table-cell" ? "block" : "table-cell";
235 td.animate({ width: 10, height: 10 }, 100, function() {
236 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
237 equals( this.offsetWidth, 20, "width animated to shrink wrap point" );
238 equals( this.offsetHeight, 20, "height animated to shrink wrap point" );
243 test("animate resets overflow-x and overflow-y when finished", function() {
247 .css({ display: "block", width: 20, height: 20, overflowX: "visible", overflowY: "auto" })
248 .animate({ width: 42, height: 42 }, 100, function() {
249 equals( this.style.overflowX, "visible", "overflow-x is visible" );
250 equals( this.style.overflowY, "auto", "overflow-y is auto" );
255 /* // This test ends up being flaky depending upon the CPU load
256 test("animate option (queue === false)", function () {
262 var $foo = jQuery("#foo");
263 $foo.animate({width:'100px'}, 3000, function () {
264 // should finish after unqueued animation so second
266 same( order, [ 1, 2 ], "Animations finished in the correct order" );
269 $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
270 // short duration and out of queue so should finish first
276 test("animate with no properties", function() {
279 var divs = jQuery("div"), count = 0;
281 divs.animate({}, function(){
285 equals( divs.length, count, "Make sure that callback is called for each element in the set." );
289 var foo = jQuery("#foo");
292 foo.animate({top: 10}, 100, function(){
293 ok( true, "Animation was properly dequeued." );
298 test("animate duration 0", function() {
303 var $elems = jQuery([{ a:0 },{ a:0 }]), counter = 0;
305 equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
307 $elems.eq(0).animate( {a:1}, 0, function(){
308 ok( true, "Animate a simple property." );
312 // Failed until [6115]
313 equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
315 equals( counter, 1, "One synchronic animations" );
317 $elems.animate( { a:2 }, 0, function(){
318 ok( true, "Animate a second simple property." );
322 equals( counter, 3, "Multiple synchronic animations" );
324 $elems.eq(0).animate( {a:3}, 0, function(){
325 ok( true, "Animate a third simple property." );
328 $elems.eq(1).animate( {a:3}, 200, function(){
330 // Failed until [6115]
331 equals( counter, 5, "One synchronic and one asynchronic" );
335 var $elem = jQuery("<div />");
336 $elem.show(0, function(){
337 ok(true, "Show callback with no duration");
339 $elem.hide(0, function(){
340 ok(true, "Hide callback with no duration");
344 test("animate hyphenated properties", function(){
348 jQuery("#nothiddendiv")
349 .css("font-size", 10)
350 .animate({"font-size": 20}, 200, function(){
351 equals( this.style.fontSize, "20px", "The font-size property was animated." );
356 test("animate non-element", function(){
360 var obj = { test: 0 };
362 jQuery(obj).animate({test: 200}, 200, function(){
363 equals( obj.test, 200, "The custom property should be modified." );
368 test("stop()", function() {
372 var $foo = jQuery("#nothiddendiv");
374 $foo.hide().width(200).width();
376 $foo.animate({ width:'show' }, 1000);
377 setTimeout(function(){
378 var nw = $foo.width();
379 notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px");
383 notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px");
384 setTimeout(function(){
385 equals( nw, $foo.width(), "The animation didn't continue" );
391 test("stop() - several in queue", function() {
395 var $foo = jQuery("#nothiddendivchild");
397 $foo.hide().width(200).width();
399 $foo.animate({ width:'show' }, 1000);
400 $foo.animate({ width:'hide' }, 1000);
401 $foo.animate({ width:'show' }, 1000);
402 setTimeout(function(){
403 equals( $foo.queue().length, 3, "All 3 still in the queue" );
404 var nw = $foo.width();
405 notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px");
409 notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px");
416 test("stop(clearQueue)", function() {
420 var $foo = jQuery("#nothiddendiv");
422 $foo.hide().width(200).width();
424 $foo.animate({ width:'show' }, 1000);
425 $foo.animate({ width:'hide' }, 1000);
426 $foo.animate({ width:'show' }, 1000);
427 setTimeout(function(){
428 var nw = $foo.width();
429 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
433 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
435 equals( $foo.queue().length, 0, "The animation queue was cleared" );
436 setTimeout(function(){
437 equals( nw, $foo.width(), "The animation didn't continue" );
443 test("stop(clearQueue, gotoEnd)", function() {
447 var $foo = jQuery("#nothiddendivchild");
449 $foo.hide().width(200).width();
451 $foo.animate({ width:'show' }, 1000);
452 $foo.animate({ width:'hide' }, 1000);
453 $foo.animate({ width:'show' }, 1000);
454 $foo.animate({ width:'hide' }, 1000);
455 setTimeout(function(){
456 var nw = $foo.width();
457 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
458 $foo.stop(false, true);
461 // Disabled, being flaky
462 //equals( nw, 1, "Stop() reset the animation" );
464 setTimeout(function(){
465 // Disabled, being flaky
466 //equals( $foo.queue().length, 2, "The next animation continued" );
473 test("toggle()", function() {
475 var x = jQuery("#nothiddendiv");
476 ok( x.is(":visible"), "is visible" );
478 ok( x.is(":hidden"), "is hidden" );
480 ok( x.is(":visible"), "is visible again" );
483 ok( x.is(":visible"), "is visible" );
485 ok( x.is(":hidden"), "is hidden" );
487 ok( x.is(":visible"), "is visible again" );
490 jQuery.checkOverflowDisplay = function(){
491 var o = jQuery.css( this, "overflow" );
493 equals(o, "visible", "Overflow should be visible: " + o);
494 equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
499 test("JS Overflow and Display", function() {
502 jQuery.makeTest( "JS Overflow and Display" )
503 .addClass("widewidth")
504 .css({ overflow: "visible", display: "inline" })
505 .addClass("widewidth")
506 .text("Some sample text.")
507 .before("text before")
509 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
512 test("CSS Overflow and Display", function() {
515 jQuery.makeTest( "CSS Overflow and Display" )
516 .addClass("overflow inline")
517 .addClass("widewidth")
518 .text("Some sample text.")
519 .before("text before")
521 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
525 "CSS Auto": function(elem,prop){
526 jQuery(elem).addClass("auto" + prop)
527 .text("This is a long string of text.");
530 "JS Auto": function(elem,prop){
531 jQuery(elem).css(prop,"")
532 .text("This is a long string of text.");
535 "CSS 100": function(elem,prop){
536 jQuery(elem).addClass("large" + prop);
539 "JS 100": function(elem,prop){
540 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
541 return prop == "opacity" ? 1 : 100;
543 "CSS 50": function(elem,prop){
544 jQuery(elem).addClass("med" + prop);
547 "JS 50": function(elem,prop){
548 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
549 return prop == "opacity" ? 0.5 : 50;
551 "CSS 0": function(elem,prop){
552 jQuery(elem).addClass("no" + prop);
555 "JS 0": function(elem,prop){
556 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
561 "show": function(elem,prop){
562 jQuery(elem).hide().addClass("wide"+prop);
565 "hide": function(elem,prop){
566 jQuery(elem).addClass("wide"+prop);
569 "100": function(elem,prop){
570 jQuery(elem).addClass("wide"+prop);
571 return prop == "opacity" ? 1 : 100;
573 "50": function(elem,prop){
574 return prop == "opacity" ? 0.50 : 50;
576 "0": function(elem,prop){
577 jQuery(elem).addClass("noback");
581 test(fn + " to " + tn, function() {
582 var elem = jQuery.makeTest( fn + " to " + tn );
584 var t_w = t( elem, "width" );
585 var f_w = f( elem, "width" );
586 var t_h = t( elem, "height" );
587 var f_h = f( elem, "height" );
588 var t_o = t( elem, "opacity" );
589 var f_o = f( elem, "opacity" );
593 if ( t_h == "show" ) num++;
594 if ( t_w == "show" ) num++;
595 if ( t_w == "hide"||t_w == "show" ) num++;
596 if ( t_h == "hide"||t_h == "show" ) num++;
597 if ( t_o == "hide"||t_o == "show" ) num++;
598 if ( t_w == "hide" ) num++;
599 if ( t_o.constructor == Number ) num += 2;
600 if ( t_w.constructor == Number ) num += 2;
601 if ( t_h.constructor == Number ) num +=2;
606 var anim = { width: t_w, height: t_h, opacity: t_o };
608 elem.animate(anim, 50, function(){
610 equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
612 if ( t_w == "hide"||t_w == "show" )
613 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);
615 if ( t_h == "hide"||t_h == "show" )
616 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);
618 var cur_o = jQuery.style(this, "opacity");
620 if ( t_o == "hide" || t_o == "show" )
621 equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
624 equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
626 if ( t_o.constructor == Number ) {
627 equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
629 ok(jQuery.css(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
632 if ( t_w.constructor == Number ) {
633 equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
635 var cur_w = jQuery.css(this,"width");
637 ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
640 if ( t_h.constructor == Number ) {
641 equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
643 var cur_h = jQuery.css(this,"height");
645 ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
648 if ( t_h == "show" ) {
649 var old_h = jQuery.css(this, "height");
650 jQuery(this).append("<br/>Some more text<br/>and some more...");
652 if ( /Auto/.test( fn ) ) {
653 notEqual(jQuery.css(this, "height"), old_h, "Make sure height is auto.");
655 equals(jQuery.css(this, "height"), old_h, "Make sure height is not auto.");
665 jQuery.fn.saveState = function(hiddenOverflow){
666 var check = ['opacity','height','width','display','overflow'];
667 expect(check.length);
670 return this.each(function(){
673 jQuery.each(check, function(i,c){
674 self.save[c] = c === "overflow" && hiddenOverflow ? "hidden" : self.style[ c ] || jQuery.css(self,c);
679 jQuery.checkState = function(){
681 jQuery.each(this.save, function(c,v){
682 var cur = self.style[ c ] || jQuery.css(self, c);
683 equals( cur, v, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
689 test("Chain fadeOut fadeIn", function() {
690 jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
692 test("Chain fadeIn fadeOut", function() {
693 jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
696 test("Chain hide show", function() {
697 jQuery('#show div').saveState(jQuery.support.shrinkWrapBlocks).hide('fast').show('fast',jQuery.checkState);
699 test("Chain show hide", function() {
700 jQuery('#hide div').saveState(jQuery.support.shrinkWrapBlocks).show('fast').hide('fast',jQuery.checkState);
702 test("Chain show hide with easing and callback", function() {
703 jQuery('#hide div').saveState().show('fast').hide('fast','linear',jQuery.checkState);
706 test("Chain toggle in", function() {
707 jQuery('#togglein div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast',jQuery.checkState);
709 test("Chain toggle out", function() {
710 jQuery('#toggleout div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast',jQuery.checkState);
712 test("Chain toggle out with easing and callback", function() {
713 jQuery('#toggleout div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast','linear',jQuery.checkState);
715 test("Chain slideDown slideUp", function() {
716 jQuery('#slidedown div').saveState(jQuery.support.shrinkWrapBlocks).slideDown('fast').slideUp('fast',jQuery.checkState);
718 test("Chain slideUp slideDown", function() {
719 jQuery('#slideup div').saveState(jQuery.support.shrinkWrapBlocks).slideUp('fast').slideDown('fast',jQuery.checkState);
721 test("Chain slideUp slideDown with easing and callback", function() {
722 jQuery('#slideup div').saveState(jQuery.support.shrinkWrapBlocks).slideUp('fast').slideDown('fast','linear',jQuery.checkState);
725 test("Chain slideToggle in", function() {
726 jQuery('#slidetogglein div').saveState(jQuery.support.shrinkWrapBlocks).slideToggle('fast').slideToggle('fast',jQuery.checkState);
728 test("Chain slideToggle out", function() {
729 jQuery('#slidetoggleout div').saveState(jQuery.support.shrinkWrapBlocks).slideToggle('fast').slideToggle('fast',jQuery.checkState);
732 test("Chain fadeTo 0.5 1.0 with easing and callback)", function() {
733 jQuery('#fadeto div').saveState().fadeTo('fast',0.5).fadeTo('fast',1.0,'linear',jQuery.checkState);
736 jQuery.makeTest = function( text ){
737 var elem = jQuery("<div></div>")
738 .attr("id", "test" + jQuery.makeTest.id++)
743 .appendTo("#fx-tests")
745 jQuery(this).next().toggle();
752 jQuery.makeTest.id = 1;
754 test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
758 var $checkedtest = jQuery("#checkedtest");
759 // IE6 was clearing "checked" in jQuery(elem).show("fast");
760 $checkedtest.hide().show("fast", function() {
761 ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
762 ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
763 ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
764 ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
769 test("animate with per-property easing", function(){
774 var _test1_called = false;
775 var _test2_called = false;
776 var _default_test_called = false;
778 jQuery.easing['_test1'] = function() {
779 _test1_called = true;
782 jQuery.easing['_test2'] = function() {
783 _test2_called = true;
786 jQuery.easing['_default_test'] = function() {
787 _default_test_called = true;
790 jQuery({a:0,b:0,c:0}).animate({
794 }, 400, '_default_test', function(){
796 ok(_test1_called, "Easing function (1) called");
797 ok(_test2_called, "Easing function (2) called");
798 ok(_default_test_called, "Easing function (_default) called");
803 test("hide hidden elements (bug #7141)", function() {
807 var div = jQuery("<div style='display:none'></div>").appendTo("#main");
808 equals( div.css("display"), "none", "Element is hidden by default" );
810 ok( !div.data("olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
812 equals( div.css("display"), "block", "Show a double-hidden element" );
817 test("hide hidden elements, with animation (bug #7141)", function() {
822 var div = jQuery("<div style='display:none'></div>").appendTo("#main");
823 equals( div.css("display"), "none", "Element is hidden by default" );
824 div.hide(1, function () {
825 ok( !div.data("olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
826 div.show(1, function () {
827 equals( div.css("display"), "block", "Show a double-hidden element" );