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 jQuery("#foo").css({ display: "inline", width: '', height: '' }).animate({ width: 42, height: 42 }, 100, function() {
132 equals( jQuery(this).css("display"), jQuery.support.inlineBlockNeedsLayout ? "inline" : "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
133 equals( this.offsetWidth, 42, "width was animated" );
134 equals( this.offsetHeight, 42, "height was animated" );
139 test("animate native inline width/height", function() {
142 jQuery("#foo").css({ display: "", width: '', height: '' })
143 .append('<span>text</span>')
145 .animate({ width: 42, height: 42 }, 100, function() {
146 equals( jQuery(this).css("display"), "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
147 equals( this.offsetWidth, 42, "width was animated" );
148 equals( this.offsetHeight, 42, "height was animated" );
153 test("animate block width/height", function() {
156 jQuery("#foo").css({ display: "block", width: 20, height: 20 }).animate({ width: 42, height: 42 }, 100, function() {
157 equals( jQuery(this).css("display"), "block", "inline-block was not set on block element when animating width/height" );
158 equals( this.offsetWidth, 42, "width was animated" );
159 equals( this.offsetHeight, 42, "height was animated" );
164 test("animate table width/height", function() {
168 var displayMode = jQuery("#table").css("display") !== "table" ? "block" : "table";
170 jQuery("#table").animate({ width: 42, height: 42 }, 100, function() {
171 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
176 test("animate table-row width/height", function() {
179 var tr = jQuery("#table")
180 .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
181 .html("<tr style='height:42px;'><td style='padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
184 // IE<8 uses “block” instead of the correct display type
185 var displayMode = tr.css("display") !== "table-row" ? "block" : "table-row";
187 tr.animate({ width: 10, height: 10 }, 100, function() {
188 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
189 equals( this.offsetWidth, 20, "width animated to shrink wrap point" );
190 equals( this.offsetHeight, 20, "height animated to shrink wrap point" );
195 test("animate table-cell width/height", function() {
198 var td = jQuery("#table")
199 .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
200 .html("<tr><td style='width:42px;height:42px;padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
203 // IE<8 uses “block” instead of the correct display type
204 var displayMode = td.css("display") !== "table-cell" ? "block" : "table-cell";
206 td.animate({ width: 10, height: 10 }, 100, function() {
207 equals( jQuery(this).css("display"), displayMode, "display mode is correct" );
208 equals( this.offsetWidth, 20, "width animated to shrink wrap point" );
209 equals( this.offsetHeight, 20, "height animated to shrink wrap point" );
214 test("animate resets overflow-x and overflow-y when finished", function() {
218 .css({ display: "block", width: 20, height: 20, overflowX: "visible", overflowY: "auto" })
219 .animate({ width: 42, height: 42 }, 100, function() {
220 equals( this.style.overflowX, "visible", "overflow-x is visible" );
221 equals( this.style.overflowY, "auto", "overflow-y is auto" );
226 /* // This test ends up being flaky depending upon the CPU load
227 test("animate option (queue === false)", function () {
233 var $foo = jQuery("#foo");
234 $foo.animate({width:'100px'}, 3000, function () {
235 // should finish after unqueued animation so second
237 same( order, [ 1, 2 ], "Animations finished in the correct order" );
240 $foo.animate({fontSize:'2em'}, {queue:false, duration:10, complete:function () {
241 // short duration and out of queue so should finish first
247 test("animate with no properties", function() {
250 var divs = jQuery("div"), count = 0;
252 divs.animate({}, function(){
256 equals( divs.length, count, "Make sure that callback is called for each element in the set." );
260 var foo = jQuery("#foo");
263 foo.animate({top: 10}, 100, function(){
264 ok( true, "Animation was properly dequeued." );
269 test("animate duration 0", function() {
274 var $elems = jQuery([{ a:0 },{ a:0 }]), counter = 0;
276 equals( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
278 $elems.eq(0).animate( {a:1}, 0, function(){
279 ok( true, "Animate a simple property." );
283 // Failed until [6115]
284 equals( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
286 equals( counter, 1, "One synchronic animations" );
288 $elems.animate( { a:2 }, 0, function(){
289 ok( true, "Animate a second simple property." );
293 equals( counter, 3, "Multiple synchronic animations" );
295 $elems.eq(0).animate( {a:3}, 0, function(){
296 ok( true, "Animate a third simple property." );
299 $elems.eq(1).animate( {a:3}, 200, function(){
301 // Failed until [6115]
302 equals( counter, 5, "One synchronic and one asynchronic" );
306 var $elem = jQuery("<div />");
307 $elem.show(0, function(){
308 ok(true, "Show callback with no duration");
310 $elem.hide(0, function(){
311 ok(true, "Hide callback with no duration");
315 test("animate hyphenated properties", function(){
319 jQuery("#nothiddendiv")
320 .css("font-size", 10)
321 .animate({"font-size": 20}, 200, function(){
322 equals( this.style.fontSize, "20px", "The font-size property was animated." );
327 test("animate non-element", function(){
331 var obj = { test: 0 };
333 jQuery(obj).animate({test: 200}, 200, function(){
334 equals( obj.test, 200, "The custom property should be modified." );
339 test("stop()", function() {
343 var $foo = jQuery("#nothiddendiv");
345 $foo.hide().width(200).width();
347 $foo.animate({ width:'show' }, 1000);
348 setTimeout(function(){
349 var nw = $foo.width();
350 notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px");
354 notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px");
355 setTimeout(function(){
356 equals( nw, $foo.width(), "The animation didn't continue" );
362 test("stop() - several in queue", function() {
366 var $foo = jQuery("#nothiddendivchild");
368 $foo.hide().width(200).width();
370 $foo.animate({ width:'show' }, 1000);
371 $foo.animate({ width:'hide' }, 1000);
372 $foo.animate({ width:'show' }, 1000);
373 setTimeout(function(){
374 equals( $foo.queue().length, 3, "All 3 still in the queue" );
375 var nw = $foo.width();
376 notEqual( nw, w, "An animation occurred " + nw + "px " + w + "px");
380 notEqual( nw, w, "Stop didn't reset the animation " + nw + "px " + w + "px");
387 test("stop(clearQueue)", function() {
391 var $foo = jQuery("#nothiddendiv");
393 $foo.hide().width(200).width();
395 $foo.animate({ width:'show' }, 1000);
396 $foo.animate({ width:'hide' }, 1000);
397 $foo.animate({ width:'show' }, 1000);
398 setTimeout(function(){
399 var nw = $foo.width();
400 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
404 ok( nw != w, "Stop didn't reset the animation " + nw + "px " + w + "px");
406 equals( $foo.queue().length, 0, "The animation queue was cleared" );
407 setTimeout(function(){
408 equals( nw, $foo.width(), "The animation didn't continue" );
414 test("stop(clearQueue, gotoEnd)", function() {
418 var $foo = jQuery("#nothiddendivchild");
420 $foo.hide().width(200).width();
422 $foo.animate({ width:'show' }, 1000);
423 $foo.animate({ width:'hide' }, 1000);
424 $foo.animate({ width:'show' }, 1000);
425 $foo.animate({ width:'hide' }, 1000);
426 setTimeout(function(){
427 var nw = $foo.width();
428 ok( nw != w, "An animation occurred " + nw + "px " + w + "px");
429 $foo.stop(false, true);
432 // Disabled, being flaky
433 //equals( nw, 1, "Stop() reset the animation" );
435 setTimeout(function(){
436 // Disabled, being flaky
437 //equals( $foo.queue().length, 2, "The next animation continued" );
444 test("toggle()", function() {
446 var x = jQuery("#nothiddendiv");
447 ok( x.is(":visible"), "is visible" );
449 ok( x.is(":hidden"), "is hidden" );
451 ok( x.is(":visible"), "is visible again" );
454 ok( x.is(":visible"), "is visible" );
456 ok( x.is(":hidden"), "is hidden" );
458 ok( x.is(":visible"), "is visible again" );
461 jQuery.checkOverflowDisplay = function(){
462 var o = jQuery.css( this, "overflow" );
464 equals(o, "visible", "Overflow should be visible: " + o);
465 equals(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with.");
470 test("JS Overflow and Display", function() {
473 jQuery.makeTest( "JS Overflow and Display" )
474 .addClass("widewidth")
475 .css({ overflow: "visible", display: "inline" })
476 .addClass("widewidth")
477 .text("Some sample text.")
478 .before("text before")
480 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
483 test("CSS Overflow and Display", function() {
486 jQuery.makeTest( "CSS Overflow and Display" )
487 .addClass("overflow inline")
488 .addClass("widewidth")
489 .text("Some sample text.")
490 .before("text before")
492 .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay);
496 "CSS Auto": function(elem,prop){
497 jQuery(elem).addClass("auto" + prop)
498 .text("This is a long string of text.");
501 "JS Auto": function(elem,prop){
502 jQuery(elem).css(prop,"")
503 .text("This is a long string of text.");
506 "CSS 100": function(elem,prop){
507 jQuery(elem).addClass("large" + prop);
510 "JS 100": function(elem,prop){
511 jQuery(elem).css(prop,prop == "opacity" ? 1 : "100px");
512 return prop == "opacity" ? 1 : 100;
514 "CSS 50": function(elem,prop){
515 jQuery(elem).addClass("med" + prop);
518 "JS 50": function(elem,prop){
519 jQuery(elem).css(prop,prop == "opacity" ? 0.50 : "50px");
520 return prop == "opacity" ? 0.5 : 50;
522 "CSS 0": function(elem,prop){
523 jQuery(elem).addClass("no" + prop);
526 "JS 0": function(elem,prop){
527 jQuery(elem).css(prop,prop == "opacity" ? 0 : "0px");
532 "show": function(elem,prop){
533 jQuery(elem).hide().addClass("wide"+prop);
536 "hide": function(elem,prop){
537 jQuery(elem).addClass("wide"+prop);
540 "100": function(elem,prop){
541 jQuery(elem).addClass("wide"+prop);
542 return prop == "opacity" ? 1 : 100;
544 "50": function(elem,prop){
545 return prop == "opacity" ? 0.50 : 50;
547 "0": function(elem,prop){
548 jQuery(elem).addClass("noback");
552 test(fn + " to " + tn, function() {
553 var elem = jQuery.makeTest( fn + " to " + tn );
555 var t_w = t( elem, "width" );
556 var f_w = f( elem, "width" );
557 var t_h = t( elem, "height" );
558 var f_h = f( elem, "height" );
559 var t_o = t( elem, "opacity" );
560 var f_o = f( elem, "opacity" );
564 if ( t_h == "show" ) num++;
565 if ( t_w == "show" ) num++;
566 if ( t_w == "hide"||t_w == "show" ) num++;
567 if ( t_h == "hide"||t_h == "show" ) num++;
568 if ( t_o == "hide"||t_o == "show" ) num++;
569 if ( t_w == "hide" ) num++;
570 if ( t_o.constructor == Number ) num += 2;
571 if ( t_w.constructor == Number ) num += 2;
572 if ( t_h.constructor == Number ) num +=2;
577 var anim = { width: t_w, height: t_h, opacity: t_o };
579 elem.animate(anim, 50, function(){
581 equals( this.style.display, "block", "Showing, display should block: " + this.style.display);
583 if ( t_w == "hide"||t_w == "show" )
584 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);
586 if ( t_h == "hide"||t_h == "show" )
587 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);
589 var cur_o = jQuery.style(this, "opacity");
591 if ( t_o == "hide" || t_o == "show" )
592 equals(cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o);
595 equals(this.style.display, "none", "Hiding, display should be none: " + this.style.display);
597 if ( t_o.constructor == Number ) {
598 equals(cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o);
600 ok(jQuery.css(this, "opacity") != "" || cur_o == t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o);
603 if ( t_w.constructor == Number ) {
604 equals(this.style.width, t_w + "px", "Final width should be " + t_w + ": " + this.style.width);
606 var cur_w = jQuery.css(this,"width");
608 ok(this.style.width != "" || cur_w == t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w);
611 if ( t_h.constructor == Number ) {
612 equals(this.style.height, t_h + "px", "Final height should be " + t_h + ": " + this.style.height);
614 var cur_h = jQuery.css(this,"height");
616 ok(this.style.height != "" || cur_h == t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_w);
619 if ( t_h == "show" ) {
620 var old_h = jQuery.css(this, "height");
621 jQuery(this).append("<br/>Some more text<br/>and some more...");
623 if ( /Auto/.test( fn ) ) {
624 notEqual(jQuery.css(this, "height"), old_h, "Make sure height is auto.");
626 equals(jQuery.css(this, "height"), old_h, "Make sure height is not auto.");
636 jQuery.fn.saveState = function(hiddenOverflow){
637 var check = ['opacity','height','width','display','overflow'];
638 expect(check.length);
641 return this.each(function(){
644 jQuery.each(check, function(i,c){
645 self.save[c] = c === "overflow" && hiddenOverflow ? "hidden" : self.style[ c ] || jQuery.css(self,c);
650 jQuery.checkState = function(){
652 jQuery.each(this.save, function(c,v){
653 var cur = self.style[ c ] || jQuery.css(self, c);
654 equals( cur, v, "Make sure that " + c + " is reset (Old: " + v + " Cur: " + cur + ")");
660 test("Chain fadeOut fadeIn", function() {
661 jQuery('#fadein div').saveState().fadeOut('fast').fadeIn('fast',jQuery.checkState);
663 test("Chain fadeIn fadeOut", function() {
664 jQuery('#fadeout div').saveState().fadeIn('fast').fadeOut('fast',jQuery.checkState);
667 test("Chain hide show", function() {
668 jQuery('#show div').saveState(jQuery.support.shrinkWrapBlocks).hide('fast').show('fast',jQuery.checkState);
670 test("Chain show hide", function() {
671 jQuery('#hide div').saveState(jQuery.support.shrinkWrapBlocks).show('fast').hide('fast',jQuery.checkState);
673 test("Chain show hide with easing and callback", function() {
674 jQuery('#hide div').saveState().show('fast').hide('fast','linear',jQuery.checkState);
677 test("Chain toggle in", function() {
678 jQuery('#togglein div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast',jQuery.checkState);
680 test("Chain toggle out", function() {
681 jQuery('#toggleout div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast',jQuery.checkState);
683 test("Chain toggle out with easing and callback", function() {
684 jQuery('#toggleout div').saveState(jQuery.support.shrinkWrapBlocks).toggle('fast').toggle('fast','linear',jQuery.checkState);
686 test("Chain slideDown slideUp", function() {
687 jQuery('#slidedown div').saveState(jQuery.support.shrinkWrapBlocks).slideDown('fast').slideUp('fast',jQuery.checkState);
689 test("Chain slideUp slideDown", function() {
690 jQuery('#slideup div').saveState(jQuery.support.shrinkWrapBlocks).slideUp('fast').slideDown('fast',jQuery.checkState);
692 test("Chain slideUp slideDown with easing and callback", function() {
693 jQuery('#slideup div').saveState(jQuery.support.shrinkWrapBlocks).slideUp('fast').slideDown('fast','linear',jQuery.checkState);
696 test("Chain slideToggle in", function() {
697 jQuery('#slidetogglein div').saveState(jQuery.support.shrinkWrapBlocks).slideToggle('fast').slideToggle('fast',jQuery.checkState);
699 test("Chain slideToggle out", function() {
700 jQuery('#slidetoggleout div').saveState(jQuery.support.shrinkWrapBlocks).slideToggle('fast').slideToggle('fast',jQuery.checkState);
703 test("Chain fadeTo 0.5 1.0 with easing and callback)", function() {
704 jQuery('#fadeto div').saveState().fadeTo('fast',0.5).fadeTo('fast',1.0,'linear',jQuery.checkState);
707 jQuery.makeTest = function( text ){
708 var elem = jQuery("<div></div>")
709 .attr("id", "test" + jQuery.makeTest.id++)
714 .appendTo("#fx-tests")
716 jQuery(this).next().toggle();
723 jQuery.makeTest.id = 1;
725 test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
729 var $checkedtest = jQuery("#checkedtest");
730 // IE6 was clearing "checked" in jQuery(elem).show("fast");
731 $checkedtest.hide().show("fast", function() {
732 ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
733 ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
734 ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
735 ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
740 test("animate with per-property easing", function(){
745 var _test1_called = false;
746 var _test2_called = false;
747 var _default_test_called = false;
749 jQuery.easing['_test1'] = function() {
750 _test1_called = true;
753 jQuery.easing['_test2'] = function() {
754 _test2_called = true;
757 jQuery.easing['_default_test'] = function() {
758 _default_test_called = true;
761 jQuery({a:0,b:0,c:0}).animate({
765 }, 400, '_default_test', function(){
767 ok(_test1_called, "Easing function (1) called");
768 ok(_test2_called, "Easing function (2) called");
769 ok(_default_test_called, "Easing function (_default) called");