effects.js 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. (function() {
  2. // Can't test what ain't there
  3. if ( !jQuery.fx ) {
  4. return;
  5. }
  6. var off = jQuery.fx.off;
  7. module("effects", {
  8. teardown: function() {
  9. jQuery.fx.off = off;
  10. return moduleTeardown.apply( this, arguments );
  11. }
  12. });
  13. test("sanity check", function() {
  14. expect(1);
  15. ok( jQuery("#dl:visible, #qunit-fixture:visible, #foo:visible").length === 3, "QUnit state is correct for testing effects" );
  16. });
  17. test("show() basic", 2, function() {
  18. var div,
  19. hiddendiv = jQuery("div.hidden");
  20. hiddendiv.hide().show();
  21. equal( hiddendiv.css("display"), "block", "Make sure a pre-hidden div is visible." );
  22. div = jQuery("<div>").hide().appendTo("#qunit-fixture").show();
  23. equal( div.css("display"), "block", "Make sure pre-hidden divs show" );
  24. // Clean up the detached node
  25. div.remove();
  26. QUnit.expectJqData(hiddendiv, "olddisplay");
  27. });
  28. test("show()", 27, function () {
  29. var div, speeds, old, test,
  30. hiddendiv = jQuery("div.hidden");
  31. equal(jQuery.css( hiddendiv[0], "display"), "none", "hiddendiv is display: none");
  32. hiddendiv.css("display", "block");
  33. equal(jQuery.css( hiddendiv[0], "display"), "block", "hiddendiv is display: block");
  34. hiddendiv.show();
  35. equal(jQuery.css( hiddendiv[0], "display"), "block", "hiddendiv is display: block");
  36. hiddendiv.css("display","");
  37. div = jQuery("#fx-queue div").slice(0, 4);
  38. div.show().each(function() {
  39. notEqual(this.style.display, "none", "don't change any <div> with display block");
  40. });
  41. speeds = {
  42. "null speed": null,
  43. "undefined speed": undefined,
  44. "false speed": false
  45. };
  46. jQuery.each(speeds, function(name, speed) {
  47. var pass = true;
  48. div.hide().show(speed).each(function() {
  49. if ( this.style.display === "none" ) {
  50. pass = false;
  51. }
  52. });
  53. ok( pass, "Show with " + name);
  54. });
  55. jQuery.each(speeds, function(name, speed) {
  56. var pass = true;
  57. div.hide().show(speed, function() {
  58. pass = false;
  59. });
  60. ok( pass, "Show with " + name + " does not call animate callback" );
  61. });
  62. // Tolerate data from show()/hide()
  63. QUnit.expectJqData(div, "olddisplay");
  64. // #show-tests * is set display: none in CSS
  65. jQuery("#qunit-fixture").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>");
  66. old = jQuery("#test-table").show().css("display") !== "table";
  67. jQuery("#test-table").remove();
  68. test = {
  69. "div" : "block",
  70. "p" : "block",
  71. "a" : "inline",
  72. "code" : "inline",
  73. "pre" : "block",
  74. "span" : "inline",
  75. "table" : old ? "block" : "table",
  76. "thead" : old ? "block" : "table-header-group",
  77. "tbody" : old ? "block" : "table-row-group",
  78. "tr" : old ? "block" : "table-row",
  79. "th" : old ? "block" : "table-cell",
  80. "td" : old ? "block" : "table-cell",
  81. "ul" : "block",
  82. "li" : old ? "block" : "list-item"
  83. };
  84. jQuery.each(test, function(selector, expected) {
  85. var elem = jQuery(selector, "#show-tests").show();
  86. equal( elem.css("display"), expected, "Show using correct display type for " + selector );
  87. });
  88. jQuery("#show-tests").remove();
  89. // Make sure that showing or hiding a text node doesn't cause an error
  90. jQuery("<div>test</div> text <span>test</span>").show().remove();
  91. jQuery("<div>test</div> text <span>test</span>").hide().remove();
  92. });
  93. test("show(Number) - other displays", function() {
  94. expect(15);
  95. QUnit.reset();
  96. stop();
  97. // #show-tests * is set display: none in CSS
  98. jQuery("#qunit-fixture").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>");
  99. var test,
  100. old = jQuery("#test-table").show().css("display") !== "table",
  101. num = 0;
  102. jQuery("#test-table").remove();
  103. test = {
  104. "div" : "block",
  105. "p" : "block",
  106. "a" : "inline",
  107. "code" : "inline",
  108. "pre" : "block",
  109. "span" : "inline",
  110. "table" : old ? "block" : "table",
  111. "thead" : old ? "block" : "table-header-group",
  112. "tbody" : old ? "block" : "table-row-group",
  113. "tr" : old ? "block" : "table-row",
  114. "th" : old ? "block" : "table-cell",
  115. "td" : old ? "block" : "table-cell",
  116. "ul" : "block",
  117. "li" : old ? "block" : "list-item"
  118. };
  119. jQuery.each(test, function(selector, expected) {
  120. var elem = jQuery(selector, "#show-tests").show(1, function() {
  121. equal( elem.css("display"), expected, "Show using correct display type for " + selector );
  122. if ( ++num === 15 ) {
  123. start();
  124. }
  125. });
  126. });
  127. jQuery("#show-tests").remove();
  128. });
  129. // Supports #7397
  130. test("Persist correct display value", function() {
  131. expect(3);
  132. QUnit.reset();
  133. stop();
  134. // #show-tests * is set display: none in CSS
  135. jQuery("#qunit-fixture").append("<div id='show-tests'><span style='position:absolute;'>foo</span></div>");
  136. var $span = jQuery("#show-tests span"),
  137. displayNone = $span.css("display"),
  138. display = "";
  139. $span.show();
  140. display = $span.css("display");
  141. $span.hide();
  142. $span.fadeIn(100, function() {
  143. equal($span.css("display"), display, "Expecting display: " + display);
  144. $span.fadeOut(100, function () {
  145. equal($span.css("display"), displayNone, "Expecting display: " + displayNone);
  146. $span.fadeIn(100, function() {
  147. equal($span.css("display"), display, "Expecting display: " + display);
  148. start();
  149. });
  150. });
  151. });
  152. QUnit.expectJqData($span, "olddisplay");
  153. });
  154. test("animate(Hash, Object, Function)", function() {
  155. expect(1);
  156. stop();
  157. var hash = {opacity: "show"},
  158. hashCopy = jQuery.extend({}, hash);
  159. jQuery("#foo").animate(hash, 0, function() {
  160. equal( hash.opacity, hashCopy.opacity, "Check if animate changed the hash parameter" );
  161. start();
  162. });
  163. });
  164. test("animate relative values", function() {
  165. stop();
  166. var value = 40,
  167. bases = [ "%", "px", "em" ],
  168. adjustments = [ "px", "em" ],
  169. container = jQuery("<div></div>")
  170. .css({ position: "absolute", height: "50em", width: "50em" }),
  171. animations = bases.length * adjustments.length;
  172. expect( 2 * animations );
  173. jQuery.each( bases, function( _, baseUnit ) {
  174. jQuery.each( adjustments, function( _, adjustUnit ) {
  175. var base = value + baseUnit,
  176. adjust = { height: "+=2" + adjustUnit, width: "-=2" + adjustUnit },
  177. elem = jQuery("<div></div>")
  178. .appendTo( container.clone().appendTo("#qunit-fixture") )
  179. .css({
  180. position: "absolute",
  181. height: base,
  182. width: value + adjustUnit
  183. }),
  184. baseScale = elem[ 0 ].offsetHeight / value,
  185. adjustScale = elem[ 0 ].offsetWidth / value;
  186. elem.css( "width", base ).animate( adjust, 100, function() {
  187. equal( this.offsetHeight, value * baseScale + 2 * adjustScale,
  188. baseUnit + "+=" + adjustUnit );
  189. equal( this.offsetWidth, value * baseScale - 2 * adjustScale,
  190. baseUnit + "-=" + adjustUnit );
  191. if ( --animations === 0 ) {
  192. start();
  193. }
  194. });
  195. });
  196. });
  197. });
  198. test("animate negative height", function() {
  199. expect(1);
  200. stop();
  201. jQuery("#foo").animate({ height: -100 }, 100, function() {
  202. equal( this.offsetHeight, 0, "Verify height." );
  203. start();
  204. });
  205. });
  206. test("animate negative margin", function() {
  207. expect(1);
  208. stop();
  209. jQuery("#foo").animate({ "marginTop": -100 }, 100, function() {
  210. equal( jQuery(this).css("marginTop"), "-100px", "Verify margin." );
  211. start();
  212. });
  213. });
  214. test("animate negative margin with px", function() {
  215. expect(1);
  216. stop();
  217. jQuery("#foo").animate({ marginTop: "-100px" }, 100, function() {
  218. equal( jQuery(this).css("marginTop"), "-100px", "Verify margin." );
  219. start();
  220. });
  221. });
  222. test("animate negative padding", function() {
  223. expect(1);
  224. stop();
  225. jQuery("#foo").animate({ "paddingBottom": -100 }, 100, function() {
  226. equal( jQuery(this).css("paddingBottom"), "0px", "Verify paddingBottom." );
  227. start();
  228. });
  229. });
  230. test("animate block as inline width/height", function() {
  231. expect(3);
  232. stop();
  233. jQuery("#foo").css({ display: "inline", width: "", height: "" }).animate({ width: 42, height: 42 }, 100, function() {
  234. equal( jQuery(this).css("display"), "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
  235. equal( this.offsetWidth, 42, "width was animated" );
  236. equal( this.offsetHeight, 42, "height was animated" );
  237. start();
  238. });
  239. });
  240. test("animate native inline width/height", function() {
  241. expect(3);
  242. stop();
  243. jQuery("#foo").css({ display: "", width: "", height: "" })
  244. .append("<span>text</span>")
  245. .children("span")
  246. .animate({ width: 42, height: 42 }, 100, function() {
  247. equal( jQuery(this).css("display"), "inline-block", "inline-block was set on non-floated inline element when animating width/height" );
  248. equal( this.offsetWidth, 42, "width was animated" );
  249. equal( this.offsetHeight, 42, "height was animated" );
  250. start();
  251. });
  252. });
  253. test( "animate block width/height", function() {
  254. expect( 3 );
  255. stop();
  256. jQuery("<div>").appendTo("#qunit-fixture").css({
  257. display: "block",
  258. width: 20,
  259. height: 20,
  260. paddingLeft: 60
  261. }).animate({
  262. width: 42,
  263. height: 42
  264. }, {
  265. duration: 100,
  266. step: function() {
  267. if ( jQuery( this ).width() > 42 ) {
  268. ok( false, "width was incorrectly augmented during animation" );
  269. }
  270. },
  271. complete: function() {
  272. equal( jQuery( this ).css("display"), "block", "inline-block was not set on block element when animating width/height" );
  273. equal( jQuery( this ).width(), 42, "width was animated" );
  274. equal( jQuery( this ).height(), 42, "height was animated" );
  275. start();
  276. }
  277. });
  278. });
  279. test("animate table width/height", function() {
  280. expect(1);
  281. stop();
  282. var displayMode = jQuery("#table").css("display") !== "table" ? "block" : "table";
  283. jQuery("#table").animate({ width: 42, height: 42 }, 100, function() {
  284. equal( jQuery(this).css("display"), displayMode, "display mode is correct" );
  285. start();
  286. });
  287. });
  288. test("animate table-row width/height", function() {
  289. expect(3);
  290. stop();
  291. var displayMode,
  292. tr = jQuery("#table")
  293. .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
  294. .html("<tr style='height:42px;'><td style='padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
  295. .find("tr");
  296. // IE<8 uses "block" instead of the correct display type
  297. displayMode = tr.css("display") !== "table-row" ? "block" : "table-row";
  298. tr.animate({ width: 10, height: 10 }, 100, function() {
  299. equal( jQuery(this).css("display"), displayMode, "display mode is correct" );
  300. equal( this.offsetWidth, 20, "width animated to shrink wrap point" );
  301. equal( this.offsetHeight, 20, "height animated to shrink wrap point" );
  302. start();
  303. });
  304. });
  305. test("animate table-cell width/height", function() {
  306. expect(3);
  307. stop();
  308. var displayMode,
  309. td = jQuery("#table")
  310. .attr({ "cellspacing": 0, "cellpadding": 0, "border": 0 })
  311. .html("<tr><td style='width:42px;height:42px;padding:0;'><div style='width:20px;height:20px;'></div></td></tr>")
  312. .find("td");
  313. // IE<8 uses "block" instead of the correct display type
  314. displayMode = td.css("display") !== "table-cell" ? "block" : "table-cell";
  315. td.animate({ width: 10, height: 10 }, 100, function() {
  316. equal( jQuery(this).css("display"), displayMode, "display mode is correct" );
  317. equal( this.offsetWidth, 20, "width animated to shrink wrap point" );
  318. equal( this.offsetHeight, 20, "height animated to shrink wrap point" );
  319. start();
  320. });
  321. });
  322. test("animate percentage(%) on width/height", function() {
  323. expect( 2 );
  324. var $div = jQuery("<div style='position:absolute;top:-999px;left:-999px;width:60px;height:60px;'><div style='width:50%;height:50%;'></div></div>")
  325. .appendTo("#qunit-fixture").children("div");
  326. stop();
  327. $div.animate({ width: "25%", height: "25%" }, 13, function() {
  328. var $this = jQuery(this);
  329. equal( $this.css("width"), "15px", "Width was animated to 15px rather than 25px");
  330. equal( $this.css("height"), "15px", "Height was animated to 15px rather than 25px");
  331. start();
  332. });
  333. });
  334. test("animate resets overflow-x and overflow-y when finished", function() {
  335. expect(2);
  336. stop();
  337. jQuery("#foo")
  338. .css({ display: "block", width: 20, height: 20, overflowX: "visible", overflowY: "auto" })
  339. .animate({ width: 42, height: 42 }, 100, function() {
  340. equal( this.style.overflowX, "visible", "overflow-x is visible" );
  341. equal( this.style.overflowY, "auto", "overflow-y is auto" );
  342. start();
  343. });
  344. });
  345. /* // This test ends up being flaky depending upon the CPU load
  346. test("animate option (queue === false)", function () {
  347. expect(1);
  348. stop();
  349. var order = [];
  350. var $foo = jQuery("#foo");
  351. $foo.animate({width:"100px"}, 3000, function () {
  352. // should finish after unqueued animation so second
  353. order.push(2);
  354. deepEqual( order, [ 1, 2 ], "Animations finished in the correct order" );
  355. start();
  356. });
  357. $foo.animate({fontSize:"2em"}, {queue:false, duration:10, complete:function () {
  358. // short duration and out of queue so should finish first
  359. order.push(1);
  360. }});
  361. });
  362. */
  363. asyncTest( "animate option { queue: false }", function() {
  364. expect( 2 );
  365. var foo = jQuery( "#foo" );
  366. foo.animate({
  367. fontSize: "2em"
  368. }, {
  369. queue: false,
  370. duration: 10,
  371. complete: function() {
  372. ok( true, "Animation Completed" );
  373. start();
  374. }
  375. });
  376. equal( foo.queue().length, 0, "Queue is empty" );
  377. });
  378. asyncTest( "animate option { queue: true }", function() {
  379. expect( 2 );
  380. var foo = jQuery( "#foo" );
  381. foo.animate({
  382. fontSize: "2em"
  383. }, {
  384. queue: true,
  385. duration: 10,
  386. complete: function() {
  387. ok( true, "Animation Completed" );
  388. start();
  389. }
  390. });
  391. notEqual( foo.queue().length, 0, "Default queue is not empty" );
  392. });
  393. asyncTest( "animate option { queue: 'name' }", function() {
  394. expect( 5 );
  395. var foo = jQuery( "#foo" ),
  396. origWidth = parseFloat( foo.css("width") ),
  397. order = [];
  398. foo.animate( { width: origWidth + 100 }, {
  399. queue: "name",
  400. duration: 1,
  401. complete: function() {
  402. // second callback function
  403. order.push( 2 );
  404. equal( parseFloat( foo.css("width") ), origWidth + 100, "Animation ended" );
  405. equal( foo.queue("name").length, 1, "Queue length of 'name' queue" );
  406. }
  407. }).queue( "name", function() {
  408. // last callback function
  409. deepEqual( order, [ 1, 2 ], "Callbacks in expected order" );
  410. start();
  411. });
  412. setTimeout( function() {
  413. // this is the first callback function that should be called
  414. order.push( 1 );
  415. equal( parseFloat( foo.css("width") ), origWidth, "Animation does not start on its own." );
  416. equal( foo.queue("name").length, 2, "Queue length of 'name' queue" );
  417. foo.dequeue( "name" );
  418. }, 100 );
  419. });
  420. test("animate with no properties", function() {
  421. expect(2);
  422. var foo,
  423. divs = jQuery("div"),
  424. count = 0;
  425. divs.animate({}, function(){
  426. count++;
  427. });
  428. equal( divs.length, count, "Make sure that callback is called for each element in the set." );
  429. stop();
  430. foo = jQuery("#foo");
  431. foo.animate({});
  432. foo.animate({top: 10}, 100, function(){
  433. ok( true, "Animation was properly dequeued." );
  434. start();
  435. });
  436. });
  437. test("animate duration 0", function() {
  438. expect(11);
  439. stop();
  440. var $elem,
  441. $elems = jQuery([{ a:0 },{ a:0 }]),
  442. counter = 0;
  443. equal( jQuery.timers.length, 0, "Make sure no animation was running from another test" );
  444. $elems.eq(0).animate( {a:1}, 0, function(){
  445. ok( true, "Animate a simple property." );
  446. counter++;
  447. });
  448. // Failed until [6115]
  449. equal( jQuery.timers.length, 0, "Make sure synchronic animations are not left on jQuery.timers" );
  450. equal( counter, 1, "One synchronic animations" );
  451. $elems.animate( { a:2 }, 0, function(){
  452. ok( true, "Animate a second simple property." );
  453. counter++;
  454. });
  455. equal( counter, 3, "Multiple synchronic animations" );
  456. $elems.eq(0).animate( {a:3}, 0, function(){
  457. ok( true, "Animate a third simple property." );
  458. counter++;
  459. });
  460. $elems.eq(1).animate( {a:3}, 200, function(){
  461. counter++;
  462. // Failed until [6115]
  463. equal( counter, 5, "One synchronic and one asynchronic" );
  464. start();
  465. });
  466. $elem = jQuery("<div />");
  467. $elem.show(0, function(){
  468. ok(true, "Show callback with no duration");
  469. });
  470. $elem.hide(0, function(){
  471. ok(true, "Hide callback with no duration");
  472. });
  473. // manually clean up detached elements
  474. $elem.remove();
  475. });
  476. test("animate hyphenated properties", function() {
  477. expect(1);
  478. stop();
  479. jQuery("#foo")
  480. .css("font-size", 10)
  481. .animate({"font-size": 20}, 200, function() {
  482. equal( this.style.fontSize, "20px", "The font-size property was animated." );
  483. start();
  484. });
  485. });
  486. test("animate non-element", function() {
  487. expect(1);
  488. stop();
  489. var obj = { test: 0 };
  490. jQuery(obj).animate({test: 200}, 200, function(){
  491. equal( obj.test, 200, "The custom property should be modified." );
  492. start();
  493. });
  494. });
  495. test("stop()", function() {
  496. expect( 4 );
  497. stop();
  498. var $one, $two,
  499. $foo = jQuery("#foo"),
  500. tests = 2,
  501. w = 0;
  502. $foo.hide().css( "width", 200 )
  503. .animate( { "width": "show" }, 1500 );
  504. setTimeout(function() {
  505. var nw = $foo.css("width");
  506. notEqual( parseFloat( nw ), w, "An animation occurred " + nw + " " + w + "px" );
  507. $foo.stop();
  508. nw = $foo.css("width");
  509. notEqual( parseFloat( nw ), w, "Stop didn't reset the animation " + nw + " " + w + "px" );
  510. setTimeout(function() {
  511. $foo.removeData();
  512. $foo.removeData(undefined, true);
  513. equal( nw, $foo.css("width"), "The animation didn't continue" );
  514. if ( --tests === 0 ) {
  515. start();
  516. }
  517. }, 100);
  518. }, 100);
  519. $one = jQuery("#fadein");
  520. $two = jQuery("#show");
  521. $one.fadeTo(100, 0, function() {
  522. $one.stop();
  523. });
  524. setTimeout(function() {
  525. $two.fadeTo(100, 0, function() {
  526. equal( $two.css("opacity"), "0", "Stop does not interfere with animations on other elements (#6641)" );
  527. // Reset styles
  528. $one.add( $two ).css("opacity", "");
  529. if ( --tests === 0 ) {
  530. start();
  531. }
  532. });
  533. }, 50);
  534. });
  535. test("stop() - several in queue", function() {
  536. expect( 5 );
  537. var nw, time,
  538. $foo = jQuery( "#foo" );
  539. // default duration is 400ms, so 800px ensures we aren't 0 or 1 after 1ms
  540. $foo.hide().css( "width", 800 );
  541. $foo.animate({ "width": "show" }, 400, "linear");
  542. $foo.animate({ "width": "hide" });
  543. $foo.animate({ "width": "show" });
  544. // could be replaced by something nicer using sinon.
  545. time = jQuery.now();
  546. while( time === jQuery.now() ) {}
  547. jQuery.fx.tick();
  548. equal( $foo.queue().length, 3, "3 in the queue" );
  549. nw = $foo.css( "width" );
  550. notEqual( parseFloat( nw ), 1, "An animation occurred " + nw );
  551. $foo.stop();
  552. equal( $foo.queue().length, 2, "2 in the queue" );
  553. nw = $foo.css( "width" );
  554. notEqual( parseFloat( nw ), 1, "Stop didn't reset the animation " + nw );
  555. $foo.stop( true );
  556. equal( $foo.queue().length, 0, "0 in the queue" );
  557. });
  558. test("stop(clearQueue)", function() {
  559. expect(4);
  560. stop();
  561. var $foo = jQuery("#foo"),
  562. w = 0;
  563. $foo.hide().css( "width", 200 ).css("width");
  564. $foo.animate({ "width": "show" }, 1000);
  565. $foo.animate({ "width": "hide" }, 1000);
  566. $foo.animate({ "width": "show" }, 1000);
  567. setTimeout(function(){
  568. var nw = $foo.css("width");
  569. ok( parseFloat( nw ) !== w, "An animation occurred " + nw + " " + w + "px");
  570. $foo.stop(true);
  571. nw = $foo.css("width");
  572. ok( parseFloat( nw ) !== w, "Stop didn't reset the animation " + nw + " " + w + "px");
  573. equal( $foo.queue().length, 0, "The animation queue was cleared" );
  574. setTimeout(function(){
  575. equal( nw, $foo.css("width"), "The animation didn't continue" );
  576. start();
  577. }, 100);
  578. }, 100);
  579. });
  580. test("stop(clearQueue, gotoEnd)", function() {
  581. expect(1);
  582. stop();
  583. var $foo = jQuery("#foo"),
  584. w = 0;
  585. $foo.hide().css( "width", 200 ).css("width");
  586. $foo.animate({ width: "show" }, 1000);
  587. $foo.animate({ width: "hide" }, 1000);
  588. $foo.animate({ width: "show" }, 1000);
  589. $foo.animate({ width: "hide" }, 1000);
  590. setTimeout(function(){
  591. var nw = $foo.css("width");
  592. ok( parseFloat( nw ) !== w, "An animation occurred " + nw + " " + w + "px");
  593. $foo.stop(false, true);
  594. nw = $foo.css("width");
  595. // Disabled, being flaky
  596. //equal( nw, 1, "Stop() reset the animation" );
  597. setTimeout(function(){
  598. // Disabled, being flaky
  599. //equal( $foo.queue().length, 2, "The next animation continued" );
  600. $foo.stop(true);
  601. start();
  602. }, 100);
  603. }, 100);
  604. });
  605. asyncTest( "stop( queue, ..., ... ) - Stop single queues", function() {
  606. expect( 3 );
  607. var saved,
  608. foo = jQuery("#foo").css({ width: 200, height: 200 });
  609. foo.animate({
  610. width: 400
  611. },{
  612. duration: 500,
  613. complete: function() {
  614. equal( parseFloat( foo.css("width") ), 400, "Animation completed for standard queue" );
  615. equal( parseFloat( foo.css("height") ), saved, "Height was not changed after the second stop");
  616. start();
  617. }
  618. });
  619. foo.animate({
  620. height: 400
  621. },{
  622. duration: 1000,
  623. queue: "height"
  624. }).dequeue("height").stop( "height", false, true );
  625. equal( parseFloat( foo.css("height") ), 400, "Height was stopped with gotoEnd" );
  626. foo.animate({
  627. height: 200
  628. },{
  629. duration: 1000,
  630. queue: "height"
  631. }).dequeue( "height" ).stop( "height", false, false );
  632. saved = parseFloat( foo.css("height") );
  633. });
  634. test("toggle()", function() {
  635. expect(6);
  636. var x = jQuery("#foo");
  637. ok( x.is(":visible"), "is visible" );
  638. x.toggle();
  639. ok( x.is(":hidden"), "is hidden" );
  640. x.toggle();
  641. ok( x.is(":visible"), "is visible again" );
  642. x.toggle(true);
  643. ok( x.is(":visible"), "is visible" );
  644. x.toggle(false);
  645. ok( x.is(":hidden"), "is hidden" );
  646. x.toggle(true);
  647. ok( x.is(":visible"), "is visible again" );
  648. });
  649. test( "jQuery.fx.prototype.cur() - <1.8 Back Compat", 7, function() {
  650. var div = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).css({
  651. color: "#ABC",
  652. border: "5px solid black",
  653. left: "auto",
  654. marginBottom: "-11000px"
  655. })[0];
  656. equal(
  657. ( new jQuery.fx( div, {}, "color" ) ).cur(),
  658. jQuery.css( div, "color" ),
  659. "Return the same value as jQuery.css for complex properties (bug #7912)"
  660. );
  661. strictEqual(
  662. ( new jQuery.fx( div, {}, "borderLeftWidth" ) ).cur(),
  663. 5,
  664. "Return simple values parsed as Float"
  665. );
  666. // backgroundPosition actually returns 0% 0% in most browser
  667. // this fakes a "" return
  668. // hook now gets called twice because Tween will grab the current
  669. // value as it is being newed
  670. jQuery.cssHooks.backgroundPosition = {
  671. get: function() {
  672. ok( true, "hook used" );
  673. return "";
  674. }
  675. };
  676. strictEqual(
  677. ( new jQuery.fx( div, {}, "backgroundPosition" ) ).cur(),
  678. 0,
  679. "Return 0 when jQuery.css returns an empty string"
  680. );
  681. delete jQuery.cssHooks.backgroundPosition;
  682. strictEqual(
  683. ( new jQuery.fx( div, {}, "left" ) ).cur(),
  684. 0,
  685. "Return 0 when jQuery.css returns 'auto'"
  686. );
  687. equal(
  688. ( new jQuery.fx( div, {}, "marginBottom" ) ).cur(),
  689. -11000,
  690. "support negative values < -10000 (bug #7193)"
  691. );
  692. jQuery( div ).remove();
  693. });
  694. test("Overflow and Display", function() {
  695. expect(4);
  696. stop();
  697. var
  698. testClass = jQuery.makeTest("Overflow and Display")
  699. .addClass("overflow inline"),
  700. testStyle = jQuery.makeTest("Overflow and Display (inline style)")
  701. .css({ overflow: "visible", display: "inline" }),
  702. remaining = 2,
  703. done = function() {
  704. equal( jQuery.css( this, "overflow" ), "visible", "Overflow should be 'visible'" );
  705. equal( jQuery.css( this, "display" ), "inline", "Display should be 'inline'" );
  706. if ( --remaining === 0 ) {
  707. start();
  708. }
  709. };
  710. testClass.add( testStyle )
  711. .addClass("widewidth")
  712. .text("Some sample text.")
  713. .before("text before")
  714. .after("text after")
  715. .animate({ opacity: 0.5 }, "slow", done );
  716. });
  717. jQuery.each({
  718. "CSS Auto": function( elem, prop ) {
  719. jQuery( elem ).addClass( "auto" + prop )
  720. .text( "This is a long string of text." );
  721. return "";
  722. },
  723. "JS Auto": function( elem, prop ) {
  724. jQuery( elem ).css( prop, "" )
  725. .text( "This is a long string of text." );
  726. return "";
  727. },
  728. "CSS 100": function( elem, prop ) {
  729. jQuery( elem ).addClass( "large" + prop );
  730. return "";
  731. },
  732. "JS 100": function( elem, prop ) {
  733. jQuery( elem ).css( prop, prop === "opacity" ? 1 : "100px" );
  734. return prop === "opacity" ? 1 : 100;
  735. },
  736. "CSS 50": function( elem, prop ) {
  737. jQuery( elem ).addClass( "med" + prop );
  738. return "";
  739. },
  740. "JS 50": function( elem, prop ) {
  741. jQuery( elem ).css( prop, prop === "opacity" ? 0.50 : "50px" );
  742. return prop === "opacity" ? 0.5 : 50;
  743. },
  744. "CSS 0": function( elem, prop ) {
  745. jQuery( elem ).addClass( "no" + prop );
  746. return "";
  747. },
  748. "JS 0": function( elem, prop ) {
  749. jQuery( elem ).css( prop, prop === "opacity" ? 0 : "0px" );
  750. return 0;
  751. }
  752. }, function( fn, f ) {
  753. jQuery.each({
  754. "show": function( elem, prop ) {
  755. jQuery( elem ).hide( ).addClass( "wide" + prop );
  756. return "show";
  757. },
  758. "hide": function( elem, prop ) {
  759. jQuery( elem ).addClass( "wide" + prop );
  760. return "hide";
  761. },
  762. "100": function( elem, prop ) {
  763. jQuery( elem ).addClass( "wide" + prop );
  764. return prop === "opacity" ? 1 : 100;
  765. },
  766. "50": function( elem, prop ) {
  767. return prop === "opacity" ? 0.50 : 50;
  768. },
  769. "0": function( elem ) {
  770. jQuery( elem ).addClass( "noback" );
  771. return 0;
  772. }
  773. }, function( tn, t ) {
  774. test(fn + " to " + tn, function() {
  775. var num, anim,
  776. elem = jQuery.makeTest( fn + " to " + tn ),
  777. t_w = t( elem, "width" ),
  778. f_w = f( elem, "width" ),
  779. t_h = t( elem, "height" ),
  780. f_h = f( elem, "height" ),
  781. t_o = t( elem, "opacity" ),
  782. f_o = f( elem, "opacity" );
  783. if ( f_o === "" ) {
  784. f_o = 1;
  785. }
  786. num = 0;
  787. // TODO: uncrowd this
  788. if ( t_h === "show" ) {num++;}
  789. if ( t_w === "show" ) {num++;}
  790. if ( t_w === "hide" || t_w === "show" ) {num++;}
  791. if ( t_h === "hide" || t_h === "show" ) {num++;}
  792. if ( t_o === "hide" || t_o === "show" ) {num++;}
  793. if ( t_w === "hide" ) {num++;}
  794. if ( t_o.constructor === Number ) {num += 2;}
  795. if ( t_w.constructor === Number ) {num += 2;}
  796. if ( t_h.constructor === Number ) {num +=2;}
  797. expect( num );
  798. stop();
  799. anim = { width: t_w, height: t_h, opacity: t_o };
  800. elem.animate(anim, 50);
  801. jQuery.when( elem ).done(function( elem ) {
  802. var cur_o, cur_w, cur_h, old_h;
  803. elem = elem[ 0 ];
  804. if ( t_w === "show" ) {
  805. equal( elem.style.display, "block", "Showing, display should block: " + elem.style.display );
  806. }
  807. if ( t_w === "hide" || t_w === "show" ) {
  808. ok( f_w === "" ? elem.style.width === f_w : elem.style.width.indexOf( f_w ) === 0, "Width must be reset to " + f_w + ": " + elem.style.width );
  809. }
  810. if ( t_h === "hide" || t_h === "show" ) {
  811. ok( f_h === "" ? elem.style.height === f_h : elem.style.height.indexOf( f_h ) === 0, "Height must be reset to " + f_h + ": " + elem.style.height );
  812. }
  813. cur_o = jQuery.style(elem, "opacity");
  814. if ( f_o !== jQuery.css(elem, "opacity") ) {
  815. f_o = f( elem, "opacity" );
  816. }
  817. // The only time an _empty_string_ will be matched is in IE
  818. // otherwise, the correct values will be tested as usual
  819. if ( f_o === "" ) {
  820. f_o = 1;
  821. }
  822. // See above
  823. if ( cur_o === "" ) {
  824. cur_o = 1;
  825. }
  826. if ( t_o === "hide" || t_o === "show" ) {
  827. equal( cur_o, f_o, "Opacity must be reset to " + f_o + ": " + cur_o );
  828. }
  829. if ( t_w === "hide" ) {
  830. equal( elem.style.display, "none", "Hiding, display should be none: " + elem.style.display );
  831. }
  832. if ( t_o.constructor === Number ) {
  833. equal( cur_o, t_o, "Final opacity should be " + t_o + ": " + cur_o );
  834. ok( jQuery.css(elem, "opacity") !== "" || cur_o === t_o, "Opacity should be explicitly set to " + t_o + ", is instead: " + cur_o );
  835. }
  836. if ( t_w.constructor === Number ) {
  837. equal( elem.style.width, t_w + "px", "Final width should be " + t_w + ": " + elem.style.width );
  838. cur_w = jQuery.css( elem,"width" );
  839. ok( elem.style.width !== "" || cur_w === t_w, "Width should be explicitly set to " + t_w + ", is instead: " + cur_w );
  840. }
  841. if ( t_h.constructor === Number ) {
  842. equal( elem.style.height, t_h + "px", "Final height should be " + t_h + ": " + elem.style.height );
  843. cur_h = jQuery.css( elem,"height" );
  844. ok( elem.style.height !== "" || cur_h === t_h, "Height should be explicitly set to " + t_h + ", is instead: " + cur_h );
  845. }
  846. if ( t_h === "show" ) {
  847. old_h = jQuery.css( elem, "height" );
  848. jQuery( elem ).append("<br/>Some more text<br/>and some more...");
  849. if ( /Auto/.test( fn ) ) {
  850. notEqual( jQuery.css( elem, "height" ), old_h, "Make sure height is auto." );
  851. } else {
  852. equal( jQuery.css( elem, "height" ), old_h, "Make sure height is not auto." );
  853. }
  854. }
  855. // manually remove generated element
  856. jQuery( elem ).remove();
  857. start();
  858. });
  859. });
  860. });
  861. });
  862. asyncTest("Effects chaining", function() {
  863. var remaining = 16,
  864. props = [ "opacity", "height", "width", "display", "overflow" ],
  865. setup = function( name, selector ) {
  866. var $el = jQuery( selector );
  867. return $el.data( getProps( $el[0] ) ).data( "name", name );
  868. },
  869. assert = function() {
  870. var data = jQuery.data( this ),
  871. name = data.name;
  872. delete data.name;
  873. deepEqual( getProps( this ), data, name );
  874. jQuery.removeData( this );
  875. if ( --remaining === 0 ) {
  876. start();
  877. }
  878. },
  879. getProps = function( el ) {
  880. var obj = {};
  881. jQuery.each( props, function( i, prop ) {
  882. obj[ prop ] = prop === "overflow" && el.style[ prop ] || jQuery.css( el, prop );
  883. });
  884. return obj;
  885. };
  886. expect( remaining );
  887. setup( ".fadeOut().fadeIn()", "#fadein div" ).fadeOut("fast").fadeIn( "fast", assert );
  888. setup( ".fadeIn().fadeOut()", "#fadeout div" ).fadeIn("fast").fadeOut( "fast", assert );
  889. setup( ".hide().show()", "#show div" ).hide("fast").show( "fast", assert );
  890. setup( ".show().hide()", "#hide div" ).show("fast").hide( "fast", assert );
  891. setup( ".show().hide(easing)", "#easehide div" ).show("fast").hide( "fast", "linear", assert );
  892. setup( ".toggle().toggle() - in", "#togglein div" ).toggle("fast").toggle( "fast", assert );
  893. setup( ".toggle().toggle() - out", "#toggleout div" ).toggle("fast").toggle( "fast", assert );
  894. setup( ".toggle().toggle(easing) - out", "#easetoggleout div" ).toggle("fast").toggle( "fast", "linear", assert );
  895. setup( ".slideDown().slideUp()", "#slidedown div" ).slideDown("fast").slideUp( "fast", assert );
  896. setup( ".slideUp().slideDown()", "#slideup div" ).slideUp("fast").slideDown( "fast", assert );
  897. setup( ".slideUp().slideDown(easing)", "#easeslideup div" ).slideUp("fast").slideDown( "fast", "linear", assert );
  898. setup( ".slideToggle().slideToggle() - in", "#slidetogglein div" ).slideToggle("fast").slideToggle( "fast", assert );
  899. setup( ".slideToggle().slideToggle() - out", "#slidetoggleout div" ).slideToggle("fast").slideToggle( "fast", assert );
  900. setup( ".fadeToggle().fadeToggle() - in", "#fadetogglein div" ).fadeToggle("fast").fadeToggle( "fast", assert );
  901. setup( ".fadeToggle().fadeToggle() - out", "#fadetoggleout div" ).fadeToggle("fast").fadeToggle( "fast", assert );
  902. setup( ".fadeTo(0.5).fadeTo(1.0, easing)", "#fadeto div" ).fadeTo( "fast", 0.5 ).fadeTo( "fast", 1.0, "linear", assert );
  903. });
  904. jQuery.makeTest = function( text ){
  905. var elem = jQuery("<div></div>")
  906. .attr( "id", "test" + jQuery.makeTest.id++ )
  907. .addClass("box");
  908. jQuery("<h4></h4>")
  909. .text( text )
  910. .appendTo("#fx-tests")
  911. .after( elem );
  912. return elem;
  913. };
  914. jQuery.makeTest.id = 1;
  915. test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () {
  916. expect(4);
  917. stop();
  918. var $checkedtest = jQuery("#checkedtest");
  919. $checkedtest.hide().show("fast", function() {
  920. ok( jQuery("input[type='radio']", $checkedtest).first().attr("checked"), "Check first radio still checked." );
  921. ok( !jQuery("input[type='radio']", $checkedtest).last().attr("checked"), "Check last radio still NOT checked." );
  922. ok( jQuery("input[type='checkbox']", $checkedtest).first().attr("checked"), "Check first checkbox still checked." );
  923. ok( !jQuery("input[type='checkbox']", $checkedtest).last().attr("checked"), "Check last checkbox still NOT checked." );
  924. start();
  925. });
  926. });
  927. test( "interrupt toggle", function() {
  928. expect( 24 );
  929. stop();
  930. var longDuration = 2000,
  931. shortDuration = 500,
  932. remaining = 0,
  933. $elems = jQuery(".chain-test"),
  934. finish = function() {
  935. if ( !(--remaining) ) {
  936. start();
  937. }
  938. };
  939. jQuery.each( { slideToggle: "height", fadeToggle: "opacity", toggle: "width" }, function( method, prop ) {
  940. var $methodElems = $elems.filter( "[id^='" + method.toLowerCase() + "']" ).each(function() {
  941. // Don't end test until we're done with this element
  942. remaining++;
  943. // Save original property value for comparison
  944. jQuery.data( this, "startVal", jQuery( this ).css( prop ) );
  945. // Expect olddisplay data from our .hide() call below
  946. QUnit.expectJqData( this, "olddisplay" );
  947. });
  948. // Interrupt a hiding toggle
  949. $methodElems[ method ]( longDuration );
  950. setTimeout(function() {
  951. $methodElems.stop().each(function() {
  952. notEqual( jQuery( this ).css( prop ), jQuery.data( this, "startVal" ), ".stop() before completion of hiding ." + method + "() - #" + this.id );
  953. });
  954. // Restore
  955. $methodElems[ method ]( shortDuration, function() {
  956. var id = this.id,
  957. $elem = jQuery( this ),
  958. startVal = $elem.data("startVal");
  959. $elem.removeData("startVal");
  960. equal( $elem.css( prop ), startVal, "original value restored by ." + method + "() - #" + id );
  961. // Interrupt a showing toggle
  962. $elem.hide()[ method ]( longDuration );
  963. setTimeout(function() {
  964. $elem.stop();
  965. notEqual( $elem.css( prop ), startVal, ".stop() before completion of showing ." + method + "() - #" + id );
  966. // Restore
  967. $elem[ method ]( shortDuration, function() {
  968. equal( $elem.css( prop ), startVal, "original value restored by ." + method + "() - #" + id );
  969. finish();
  970. });
  971. }, shortDuration );
  972. });
  973. }, shortDuration );
  974. });
  975. });
  976. test("animate with per-property easing", function(){
  977. expect(5);
  978. stop();
  979. var data = { a:0, b:0, c:0 },
  980. _test1_called = false,
  981. _test2_called = false,
  982. _default_test_called = false,
  983. props = {
  984. a: [ 100, "_test1" ],
  985. b: [ 100, "_test2" ],
  986. c: 100
  987. };
  988. jQuery.easing["_test1"] = function(p) {
  989. _test1_called = true;
  990. return p;
  991. };
  992. jQuery.easing["_test2"] = function(p) {
  993. _test2_called = true;
  994. return p;
  995. };
  996. jQuery.easing["_default_test"] = function(p) {
  997. _default_test_called = true;
  998. return p;
  999. };
  1000. jQuery(data).animate( props, 400, "_default_test", function(){
  1001. start();
  1002. ok( _test1_called, "Easing function (_test1) called" );
  1003. ok( _test2_called, "Easing function (_test2) called" );
  1004. ok( _default_test_called, "Easing function (_default) called" );
  1005. equal( props.a[ 1 ], "_test1", "animate does not change original props (per-property easing would be lost)");
  1006. equal( props.b[ 1 ], "_test2", "animate does not change original props (per-property easing would be lost)");
  1007. });
  1008. });
  1009. test("animate with CSS shorthand properties", function(){
  1010. expect(11);
  1011. stop();
  1012. var _default_count = 0,
  1013. _special_count = 0,
  1014. propsBasic = { "padding": "10 20 30" },
  1015. propsSpecial = { "padding": [ "1 2 3", "_special" ] };
  1016. jQuery.easing._default = function(p) {
  1017. if ( p >= 1 ) {
  1018. _default_count++;
  1019. }
  1020. return p;
  1021. };
  1022. jQuery.easing._special = function(p) {
  1023. if ( p >= 1 ) {
  1024. _special_count++;
  1025. }
  1026. return p;
  1027. };
  1028. jQuery("#foo")
  1029. .animate( propsBasic, 200, "_default", function() {
  1030. equal( this.style.paddingTop, "10px", "padding-top was animated" );
  1031. equal( this.style.paddingLeft, "20px", "padding-left was animated" );
  1032. equal( this.style.paddingRight, "20px", "padding-right was animated" );
  1033. equal( this.style.paddingBottom, "30px", "padding-bottom was animated" );
  1034. equal( _default_count, 4, "per-animation default easing called for each property" );
  1035. _default_count = 0;
  1036. })
  1037. .animate( propsSpecial, 200, "_default", function() {
  1038. equal( this.style.paddingTop, "1px", "padding-top was animated again" );
  1039. equal( this.style.paddingLeft, "2px", "padding-left was animated again" );
  1040. equal( this.style.paddingRight, "2px", "padding-right was animated again" );
  1041. equal( this.style.paddingBottom, "3px", "padding-bottom was animated again" );
  1042. equal( _default_count, 0, "per-animation default easing not called" );
  1043. equal( _special_count, 4, "special easing called for each property" );
  1044. jQuery(this).css("padding", "0");
  1045. delete jQuery.easing._default;
  1046. delete jQuery.easing._special;
  1047. start();
  1048. });
  1049. });
  1050. test("hide hidden elements, with animation (bug #7141)", function() {
  1051. expect(3);
  1052. QUnit.reset();
  1053. stop();
  1054. var div = jQuery("<div style='display:none'></div>").appendTo("#qunit-fixture");
  1055. equal( div.css("display"), "none", "Element is hidden by default" );
  1056. div.hide(1, function () {
  1057. ok( !jQuery._data(div, "olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
  1058. div.show(1, function () {
  1059. equal( div.css("display"), "block", "Show a double-hidden element" );
  1060. start();
  1061. });
  1062. });
  1063. });
  1064. test("animate unit-less properties (#4966)", 2, function() {
  1065. stop();
  1066. var div = jQuery( "<div style='z-index: 0; position: absolute;'></div>" ).appendTo( "#qunit-fixture" );
  1067. equal( div.css( "z-index" ), "0", "z-index is 0" );
  1068. div.animate({ zIndex: 2 }, function() {
  1069. equal( div.css( "z-index" ), "2", "z-index is 2" );
  1070. start();
  1071. });
  1072. });
  1073. test( "animate properties missing px w/ opacity as last (#9074)", 2, function() {
  1074. expect( 6 );
  1075. stop();
  1076. var div = jQuery( "<div style='position: absolute; margin-left: 0; left: 0px;'></div>" )
  1077. .appendTo( "#qunit-fixture" );
  1078. function cssInt( prop ) {
  1079. return parseInt( div.css( prop ), 10 );
  1080. }
  1081. equal( cssInt( "marginLeft" ), 0, "Margin left is 0" );
  1082. equal( cssInt( "left" ), 0, "Left is 0" );
  1083. div.animate({
  1084. left: 200,
  1085. marginLeft: 200,
  1086. opacity: 0
  1087. }, 2000);
  1088. setTimeout(function() {
  1089. var ml = cssInt( "marginLeft" ),
  1090. l = cssInt( "left" );
  1091. notEqual( ml, 0, "Margin left is not 0 after partial animate" );
  1092. notEqual( ml, 200, "Margin left is not 200 after partial animate" );
  1093. notEqual( l, 0, "Left is not 0 after partial animate" );
  1094. notEqual( l, 200, "Left is not 200 after partial animate" );
  1095. div.stop().remove();
  1096. start();
  1097. }, 500);
  1098. });
  1099. test("callbacks should fire in correct order (#9100)", function() {
  1100. expect( 1 );
  1101. stop();
  1102. var a = 1,
  1103. cb = 0;
  1104. jQuery("<p data-operation='*2'></p><p data-operation='^2'></p>").appendTo("#qunit-fixture")
  1105. // The test will always pass if no properties are animated or if the duration is 0
  1106. .animate({fontSize: 12}, 13, function() {
  1107. a *= jQuery(this).data("operation") === "*2" ? 2 : a;
  1108. cb++;
  1109. if ( cb === 2 ) {
  1110. equal( a, 4, "test value has been *2 and _then_ ^2");
  1111. start();
  1112. }
  1113. });
  1114. });
  1115. asyncTest( "callbacks that throw exceptions will be removed (#5684)", function() {
  1116. expect( 2 );
  1117. var foo = jQuery( "#foo" );
  1118. function TestException() {
  1119. }
  1120. foo.animate({ height: 1 }, 1, function() {
  1121. throw new TestException();
  1122. });
  1123. // this test thoroughly abuses undocumented methods - please feel free to update
  1124. // with any changes internally to these functions.
  1125. // make sure that the standard timer loop will NOT run.
  1126. jQuery.fx.stop();
  1127. setTimeout(function() {
  1128. // the first call to fx.tick should raise the callback exception
  1129. raises( jQuery.fx.tick, TestException, "Exception was thrown" );
  1130. // the second call shouldn't
  1131. jQuery.fx.tick();
  1132. ok( true, "Test completed without throwing a second exception" );
  1133. start();
  1134. }, 1);
  1135. });
  1136. test("animate will scale margin properties individually", function() {
  1137. expect( 2 );
  1138. stop();
  1139. var foo = jQuery( "#foo" ).css({
  1140. "margin": 0,
  1141. "marginLeft": 100
  1142. });
  1143. ok( foo.css( "marginLeft" ) !== foo.css( "marginRight" ), "Sanity Check" );
  1144. foo.animate({
  1145. "margin": 200
  1146. }).stop();
  1147. ok( foo.css( "marginLeft") !== foo.css( "marginRight" ), "The margin properties are different");
  1148. // clean up for next test
  1149. foo.css({
  1150. "marginLeft": "",
  1151. "marginRight": "",
  1152. "marginTop": "",
  1153. "marginBottom": ""
  1154. });
  1155. start();
  1156. });
  1157. test("Do not append px to 'fill-opacity' #9548", 1, function() {
  1158. var $div = jQuery("<div>").appendTo("#qunit-fixture");
  1159. $div.css("fill-opacity", 0).animate({ "fill-opacity": 1.0 }, 0, function () {
  1160. equal( jQuery(this).css("fill-opacity"), 1, "Do not append px to 'fill-opacity'");
  1161. $div.remove();
  1162. });
  1163. });
  1164. test("line-height animates correctly (#13855)", function() {
  1165. expect( 12 );
  1166. stop();
  1167. var
  1168. animated = jQuery(
  1169. "<p style='line-height: 4;'>unitless</p>" +
  1170. "<p style='line-height: 50px;'>px</p>" +
  1171. "<p style='line-height: 420%;'>percent</p>" +
  1172. "<p style='line-height: 2.5em;'>em</p>"
  1173. ).appendTo("#qunit-fixture"),
  1174. initialHeight = jQuery.map( animated, function( el ) {
  1175. return jQuery( el ).height();
  1176. });
  1177. animated.animate( { "line-height": "hide" }, 1500 );
  1178. setTimeout(function() {
  1179. animated.each(function( i ) {
  1180. var label = jQuery.text( this ),
  1181. initial = initialHeight[ i ],
  1182. height = jQuery( this ).height();
  1183. ok( height < initial, "hide " + label + ": upper bound" );
  1184. ok( height > initial / 2, "hide " + label + ": lower bound" );
  1185. });
  1186. animated.stop( true, true ).hide().animate( { "line-height": "show" }, 1500 );
  1187. setTimeout(function() {
  1188. animated.each(function( i ) {
  1189. var label = jQuery.text( this ),
  1190. initial = initialHeight[ i ],
  1191. height = jQuery( this ).height();
  1192. ok( height < initial / 2, "show " + label + ": upper bound" );
  1193. });
  1194. animated.stop( true, true );
  1195. start();
  1196. }, 400 );
  1197. }, 400 );
  1198. });
  1199. // Start 1.8 Animation tests
  1200. asyncTest( "jQuery.Animation( object, props, opts )", 4, function() {
  1201. var animation,
  1202. testObject = {
  1203. "foo": 0,
  1204. "bar": 1,
  1205. "width": 100
  1206. },
  1207. testDest = {
  1208. "foo": 1,
  1209. "bar": 0,
  1210. "width": 200
  1211. };
  1212. animation = jQuery.Animation( testObject, testDest, { "duration": 1 });
  1213. animation.done(function() {
  1214. for ( var prop in testDest ) {
  1215. equal( testObject[ prop ], testDest[ prop ], "Animated: " + prop );
  1216. }
  1217. animation.done(function() {
  1218. deepEqual( testObject, testDest, "No unexpected properties" );
  1219. start();
  1220. });
  1221. });
  1222. });
  1223. asyncTest( "Animate Option: step: function( percent, tween )", 1, function() {
  1224. var counter = {};
  1225. jQuery( "#foo" ).animate({
  1226. prop1: 1,
  1227. prop2: 2,
  1228. prop3: 3
  1229. }, {
  1230. duration: 1,
  1231. step: function( value, tween ) {
  1232. var calls = counter[ tween.prop ] = counter[ tween.prop ] || [];
  1233. // in case this is called multiple times for either, lets store it in
  1234. // 0 or 1 in the array
  1235. calls[ value === 0 ? 0 : 1 ] = value;
  1236. }
  1237. }).queue( function( next ) {
  1238. deepEqual( counter, {
  1239. prop1: [0, 1],
  1240. prop2: [0, 2],
  1241. prop3: [0, 3]
  1242. }, "Step function was called once at 0% and once at 100% for each property");
  1243. next();
  1244. start();
  1245. });
  1246. });
  1247. asyncTest( "Animate callbacks have correct context", 2, function() {
  1248. var foo = jQuery( "#foo" );
  1249. foo.animate({
  1250. height: 10
  1251. }, 10, function() {
  1252. equal( foo[ 0 ], this, "Complete callback after stop(true) `this` is element" );
  1253. }).stop( true, true );
  1254. foo.animate({
  1255. height: 100
  1256. }, 10, function() {
  1257. equal( foo[ 0 ], this, "Complete callback `this` is element" );
  1258. start();
  1259. });
  1260. });
  1261. asyncTest( "User supplied callback called after show when fx off (#8892)", 2, function() {
  1262. var foo = jQuery( "#foo" );
  1263. jQuery.fx.off = true;
  1264. foo.hide();
  1265. foo.fadeIn( 500, function() {
  1266. ok( jQuery( this ).is( ":visible" ), "Element is visible in callback" );
  1267. foo.fadeOut( 500, function() {
  1268. ok( jQuery( this ).is( ":hidden" ), "Element is hidden in callback" );
  1269. jQuery.fx.off = false;
  1270. start();
  1271. });
  1272. });
  1273. });
  1274. test( "animate should set display for disconnected nodes", function() {
  1275. expect( 18 );
  1276. var i = 0,
  1277. methods = {
  1278. toggle: [ 1 ],
  1279. slideToggle: [],
  1280. fadeIn: [],
  1281. fadeTo: [ "fast", 0.5 ],
  1282. slideDown: [ "fast" ],
  1283. show: [ 1 ],
  1284. animate: [{ width: "show" }]
  1285. },
  1286. $divTest = jQuery("<div>test</div>"),
  1287. // parentNode = null
  1288. $divEmpty = jQuery("<div/>"),
  1289. $divNone = jQuery("<div style='display: none;'/>"),
  1290. $divInline = jQuery("<div style='display: inline;'/>");
  1291. strictEqual( $divTest.show()[ 0 ].style.display, "block", "set display with show() for element with parentNode = document fragment" );
  1292. strictEqual( $divEmpty.show()[ 0 ].style.display, "block", "set display with show() for element with parentNode = null" );
  1293. strictEqual( $divNone.show()[ 0 ].style.display, "block", "show() should change display if it already set to none" );
  1294. strictEqual( $divInline.show()[ 0 ].style.display, "inline", "show() should not change display if it already set" );
  1295. QUnit.expectJqData( $divTest[ 0 ], "olddisplay" );
  1296. QUnit.expectJqData( $divEmpty[ 0 ], "olddisplay" );
  1297. QUnit.expectJqData( $divNone[ 0 ], "olddisplay" );
  1298. stop();
  1299. jQuery.each( methods, function( name, opt ) {
  1300. jQuery.each([
  1301. // parentNode = document fragment
  1302. jQuery("<div>test</div>"),
  1303. // parentNode = null
  1304. jQuery("<div/>")
  1305. ], function() {
  1306. var callback = [function () {
  1307. strictEqual( this.style.display, "block", "set display to block with " + name );
  1308. QUnit.expectJqData( this, "olddisplay" );
  1309. if ( ++i === 14 ) {
  1310. start();
  1311. }
  1312. }];
  1313. jQuery.fn[ name ].apply( this, opt.concat( callback ) );
  1314. });
  1315. });
  1316. });
  1317. asyncTest("Animation callback should not show animated element as :animated (#7157)", 1, function() {
  1318. var foo = jQuery( "#foo" );
  1319. foo.animate({
  1320. opacity: 0
  1321. }, 100, function() {
  1322. ok( !foo.is(":animated"), "The element is not animated" );
  1323. start();
  1324. });
  1325. });
  1326. asyncTest( "hide called on element within hidden parent should set display to none (#10045)", 3, function() {
  1327. var hidden = jQuery(".hidden"),
  1328. elems = jQuery("<div>hide</div><div>hide0</div><div>hide1</div>");
  1329. hidden.append( elems );
  1330. jQuery.when(
  1331. elems.eq( 0 ).hide(),
  1332. elems.eq( 1 ).hide( 0 ),
  1333. elems.eq( 2 ).hide( 1 )
  1334. ).done(function() {
  1335. strictEqual( elems.get( 0 ).style.display, "none", "hide() called on element within hidden parent should set display to none" );
  1336. strictEqual( elems.get( 1 ).style.display, "none", "hide( 0 ) called on element within hidden parent should set display to none" );
  1337. strictEqual( elems.get( 2 ).style.display, "none", "hide( 1 ) called on element within hidden parent should set display to none" );
  1338. elems.remove();
  1339. start();
  1340. });
  1341. });
  1342. asyncTest( "hide, fadeOut and slideUp called on element width height and width = 0 should set display to none", 5, function() {
  1343. var foo = jQuery("#foo"),
  1344. i = 0,
  1345. elems = jQuery();
  1346. for ( ; i < 5; i++ ) {
  1347. elems = elems.add("<div style='width:0;height:0;'></div>");
  1348. }
  1349. foo.append( elems );
  1350. jQuery.when(
  1351. elems.eq( 0 ).hide(),
  1352. elems.eq( 1 ).hide( jQuery.noop ),
  1353. elems.eq( 2 ).hide( 1 ),
  1354. elems.eq( 3 ).fadeOut(),
  1355. elems.eq( 4 ).slideUp()
  1356. ).done(function() {
  1357. strictEqual( elems.get( 0 ).style.display, "none", "hide() called on element width height and width = 0 should set display to none" );
  1358. strictEqual( elems.get( 1 ).style.display, "none",
  1359. "hide( jQuery.noop ) called on element width height and width = 0 should set display to none" );
  1360. strictEqual( elems.get( 2 ).style.display, "none", "hide( 1 ) called on element width height and width = 0 should set display to none" );
  1361. strictEqual( elems.get( 3 ).style.display, "none", "fadeOut() called on element width height and width = 0 should set display to none" );
  1362. strictEqual( elems.get( 4 ).style.display, "none", "slideUp() called on element width height and width = 0 should set display to none" );
  1363. start();
  1364. });
  1365. });
  1366. asyncTest( "Handle queue:false promises", 10, function() {
  1367. var foo = jQuery( "#foo" ).clone().addBack(),
  1368. step = 1;
  1369. foo.animate({
  1370. top: 1
  1371. }, {
  1372. duration: 10,
  1373. queue: false,
  1374. complete: function() {
  1375. ok( step++ <= 2, "Step one or two" );
  1376. }
  1377. }).animate({
  1378. bottom: 1
  1379. }, {
  1380. duration: 10,
  1381. complete: function() {
  1382. ok( step > 2 && step < 5, "Step three or four" );
  1383. step++;
  1384. }
  1385. });
  1386. foo.promise().done( function() {
  1387. equal( step++, 5, "steps 1-5: queue:false then queue:fx done" );
  1388. foo.animate({
  1389. top: 10
  1390. }, {
  1391. duration: 10,
  1392. complete: function() {
  1393. ok( step > 5 && step < 8, "Step six or seven" );
  1394. step++;
  1395. }
  1396. }).animate({
  1397. bottom: 10
  1398. }, {
  1399. duration: 10,
  1400. queue: false,
  1401. complete: function() {
  1402. ok( step > 7 && step < 10, "Step eight or nine" );
  1403. step++;
  1404. }
  1405. }).promise().done( function() {
  1406. equal( step++, 10, "steps 6-10: queue:fx then queue:false" );
  1407. start();
  1408. });
  1409. });
  1410. });
  1411. asyncTest( "multiple unqueued and promise", 4, function() {
  1412. var foo = jQuery( "#foo" ),
  1413. step = 1;
  1414. foo.animate({
  1415. marginLeft: 300
  1416. }, {
  1417. duration: 500,
  1418. queue: false,
  1419. complete: function() {
  1420. strictEqual( step++, 2, "Step 2" );
  1421. }
  1422. }).animate({
  1423. top: 100
  1424. }, {
  1425. duration: 1000,
  1426. queue: false,
  1427. complete: function() {
  1428. strictEqual( step++, 3, "Step 3" );
  1429. }
  1430. }).animate({}, {
  1431. duration: 2000,
  1432. queue: false,
  1433. complete: function() {
  1434. // no properties is a non-op and finishes immediately
  1435. strictEqual( step++, 1, "Step 1" );
  1436. }
  1437. }).promise().done( function() {
  1438. strictEqual( step++, 4, "Step 4" );
  1439. start();
  1440. });
  1441. });
  1442. asyncTest( "animate does not change start value for non-px animation (#7109)", 1, function() {
  1443. var parent = jQuery( "<div><div></div></div>" ).css({ width: 284, height: 1 }).appendTo( "#qunit-fixture" ),
  1444. child = parent.children().css({ fontSize: "98.6in", width: "0.01em", height: 1 }),
  1445. actual = parseFloat( child.css( "width" ) ),
  1446. computed = [];
  1447. child.animate({ width: "0%" }, {
  1448. duration: 1,
  1449. step: function() {
  1450. computed.push( parseFloat( child.css( "width" ) ) );
  1451. }
  1452. }).queue( function( next ) {
  1453. var ratio = computed[ 0 ] / actual;
  1454. ok( ratio > 0.9 && ratio < 1.1 , "Starting width was close enough" );
  1455. next();
  1456. parent.remove();
  1457. start();
  1458. });
  1459. });
  1460. asyncTest( "non-px animation handles non-numeric start (#11971)", 2, function() {
  1461. var foo = jQuery("#foo"),
  1462. initial = foo.css("backgroundPositionX");
  1463. if ( !initial ) {
  1464. expect(1);
  1465. ok( true, "Style property not understood" );
  1466. start();
  1467. return;
  1468. }
  1469. foo.animate({ backgroundPositionX: "42%" }, {
  1470. duration: 1,
  1471. progress: function( anim, percent ) {
  1472. if ( percent ) {
  1473. return;
  1474. }
  1475. if ( parseFloat( initial ) ) {
  1476. equal( jQuery.style( this, "backgroundPositionX" ), initial, "Numeric start preserved" );
  1477. } else {
  1478. equal( jQuery.style( this, "backgroundPositionX" ), "0%", "Non-numeric start zeroed" );
  1479. }
  1480. },
  1481. done: function() {
  1482. equal( jQuery.style( this, "backgroundPositionX" ), "42%", "End reached" );
  1483. start();
  1484. }
  1485. });
  1486. });
  1487. asyncTest("Animation callbacks (#11797)", 15, function() {
  1488. var targets = jQuery("#foo").children(),
  1489. done = false,
  1490. expectedProgress = 0;
  1491. targets.eq( 0 ).animate( {}, {
  1492. duration: 1,
  1493. start: function() {
  1494. ok( true, "empty: start" );
  1495. },
  1496. progress: function( anim, percent ) {
  1497. equal( percent, 0, "empty: progress 0" );
  1498. },
  1499. done: function() {
  1500. ok( true, "empty: done" );
  1501. },
  1502. fail: function() {
  1503. ok( false, "empty: fail" );
  1504. },
  1505. always: function() {
  1506. ok( true, "empty: always" );
  1507. done = true;
  1508. }
  1509. });
  1510. ok( done, "empty: done immediately" );
  1511. done = false;
  1512. targets.eq( 1 ).animate({
  1513. opacity: 0
  1514. }, {
  1515. duration: 1,
  1516. start: function() {
  1517. ok( true, "stopped: start" );
  1518. },
  1519. progress: function( anim, percent ) {
  1520. equal( percent, 0, "stopped: progress 0" );
  1521. },
  1522. done: function() {
  1523. ok( false, "stopped: done" );
  1524. },
  1525. fail: function() {
  1526. ok( true, "stopped: fail" );
  1527. },
  1528. always: function() {
  1529. ok( true, "stopped: always" );
  1530. done = true;
  1531. }
  1532. }).stop();
  1533. ok( done, "stopped: stopped immediately" );
  1534. targets.eq( 2 ).animate({
  1535. opacity: 0
  1536. }, {
  1537. duration: 1,
  1538. start: function() {
  1539. ok( true, "async: start" );
  1540. },
  1541. progress: function( anim, percent ) {
  1542. // occasionally the progress handler is called twice in first frame.... *shrug*
  1543. if ( percent === 0 && expectedProgress === 1 ) {
  1544. return;
  1545. }
  1546. equal( percent, expectedProgress, "async: progress " + expectedProgress );
  1547. // once at 0, once at 1
  1548. expectedProgress++;
  1549. },
  1550. done: function() {
  1551. ok( true, "async: done" );
  1552. },
  1553. fail: function() {
  1554. ok( false, "async: fail" );
  1555. },
  1556. always: function() {
  1557. ok( true, "async: always" );
  1558. start();
  1559. }
  1560. });
  1561. });
  1562. test( "Animate properly sets overflow hidden when animating width/height (#12117)", 8, function() {
  1563. jQuery.each( [ "height", "width" ], function( _, prop ) {
  1564. jQuery.each( [ 100, 0 ], function( _, value ) {
  1565. var div = jQuery("<div>").css( "overflow", "auto" ),
  1566. props = {};
  1567. props[ prop ] = value;
  1568. div.animate( props, 1 );
  1569. equal( div.css( "overflow" ), "hidden",
  1570. "overflow: hidden set when animating " + prop + " to " + value );
  1571. div.stop();
  1572. equal( div.css( "overflow" ), "auto",
  1573. "overflow: auto restored after animating " + prop + " to " + value );
  1574. });
  1575. });
  1576. });
  1577. test( "Each tick of the timer loop uses a fresh time (#12837)", function() {
  1578. var lastVal, current,
  1579. tmp = jQuery({
  1580. test: 0
  1581. });
  1582. expect( 3 );
  1583. tmp.animate({
  1584. test: 100
  1585. }, {
  1586. step: function( p, fx ) {
  1587. ok( fx.now !== lastVal, "Current value is not the last value: " + lastVal + " - " + fx.now );
  1588. lastVal = fx.now;
  1589. }
  1590. });
  1591. current = jQuery.now();
  1592. // intentionally empty, we want to spin wheels until the time changes.
  1593. while ( current === jQuery.now() ) { }
  1594. // now that we have a new time, run another tick
  1595. jQuery.fx.tick();
  1596. current = jQuery.now();
  1597. // intentionally empty, we want to spin wheels until the time changes.
  1598. while ( current === jQuery.now() ) { }
  1599. jQuery.fx.tick();
  1600. tmp.stop();
  1601. });
  1602. test( "Animations with 0 duration don't ease (#12273)", 1, function() {
  1603. jQuery.easing.test = function() {
  1604. ok( false, "Called easing" );
  1605. };
  1606. jQuery( "#foo" ).animate({
  1607. height: 100
  1608. }, {
  1609. duration: 0,
  1610. easing: "test",
  1611. complete: function() {
  1612. equal( jQuery( this ).height(), 100, "Height is 100" );
  1613. }
  1614. });
  1615. delete jQuery.easing.test;
  1616. });
  1617. jQuery.map([ "toggle", "slideToggle", "fadeToggle" ], function ( method ) {
  1618. // this test would look a lot better if we were using something to override
  1619. // the default timers
  1620. var duration = 1500;
  1621. asyncTest( "toggle state tests: " + method + " (#8685)", function() {
  1622. function secondToggle() {
  1623. var stopped = parseFloat( element.css( check ) );
  1624. tested = false;
  1625. element[ method ]({
  1626. duration: duration,
  1627. step: function( p, fx ) {
  1628. if ( fx.pos > 0.1 && fx.prop === check && !tested ) {
  1629. tested = true;
  1630. equal( fx.start, stopped, check + " starts at " + stopped + " where it stopped" );
  1631. equal( fx.end, original, check + " ending value is " + original );
  1632. element.stop();
  1633. }
  1634. },
  1635. always: start
  1636. });
  1637. }
  1638. var tested,
  1639. original,
  1640. check = method === "slideToggle" ? "height" : "opacity",
  1641. element = jQuery("#foo").height( 200 );
  1642. expect( 4 );
  1643. element[ method ]({
  1644. duration: duration,
  1645. easing: "linear",
  1646. step: function( p, fx ) {
  1647. if ( fx.pos > 0.1 && fx.prop === check && !tested ) {
  1648. tested = true;
  1649. original = fx.start;
  1650. ok( fx.start !== 0, check + " is starting at " + original + " on first toggle (non-zero)" );
  1651. equal( fx.end, 0, check + " is ending at 0 on first toggle" );
  1652. element.stop();
  1653. }
  1654. },
  1655. always: secondToggle
  1656. });
  1657. });
  1658. });
  1659. test( "jQuery.fx.start & jQuery.fx.stop hook points", function() {
  1660. var oldStart = jQuery.fx.start,
  1661. oldStop = jQuery.fx.stop,
  1662. foo = jQuery({ foo: 0 });
  1663. expect( 3 );
  1664. jQuery.fx.start = function() {
  1665. ok( true, "start called" );
  1666. };
  1667. jQuery.fx.stop = function() {
  1668. ok( true, "stop called" );
  1669. };
  1670. // calls start
  1671. foo.animate({ foo: 1 }, { queue: false });
  1672. // calls start
  1673. foo.animate({ foo: 2 }, { queue: false });
  1674. foo.stop();
  1675. // calls stop
  1676. jQuery.fx.tick();
  1677. // cleanup
  1678. jQuery.fx.start = oldStart;
  1679. jQuery.fx.stop = oldStop;
  1680. });
  1681. test( ".finish() completes all queued animations", function() {
  1682. var animations = {
  1683. top: 100,
  1684. left: 100,
  1685. height: 100,
  1686. width: 100
  1687. },
  1688. div = jQuery("<div>");
  1689. expect( 11 );
  1690. jQuery.each( animations, function( prop, value ) {
  1691. var anim = {};
  1692. anim[ prop ] = value;
  1693. // the delay shouldn't matter at all!
  1694. div.css( prop, 1 ).animate( anim, function() {
  1695. ok( true, "Called animation callback for " + prop );
  1696. }).delay( 100 );
  1697. });
  1698. equal( div.queue().length, 8, "8 animations in the queue" );
  1699. div.finish();
  1700. jQuery.each( animations, function( prop, value ) {
  1701. equal( parseFloat( div.css( prop ) ), value, prop + " finished at correct value" );
  1702. });
  1703. equal( div.queue().length, 0, "empty queue when done" );
  1704. equal( div.is(":animated"), false, ":animated doesn't match" );
  1705. // cleanup
  1706. div.remove();
  1707. // leaves a "shadow timer" which does nothing around, need to force a tick
  1708. jQuery.fx.tick();
  1709. });
  1710. test( ".finish( false ) - unqueued animations", function() {
  1711. var animations = {
  1712. top: 100,
  1713. left: 100,
  1714. height: 100,
  1715. width: 100
  1716. },
  1717. div = jQuery("<div>");
  1718. expect( 10 );
  1719. jQuery.each( animations, function( prop, value ) {
  1720. var anim = {};
  1721. anim[ prop ] = value;
  1722. div.css( prop, 1 ).animate( anim, {
  1723. queue: false,
  1724. complete: function() {
  1725. ok( true, "Called animation callback for " + prop );
  1726. }
  1727. });
  1728. });
  1729. equal( div.queue().length, 0, "0 animations in the queue" );
  1730. div.finish( false );
  1731. jQuery.each( animations, function( prop, value ) {
  1732. equal( parseFloat( div.css( prop ) ), value, prop + " finished at correct value" );
  1733. });
  1734. equal( div.is(":animated"), false, ":animated doesn't match" );
  1735. // cleanup
  1736. div.remove();
  1737. // leaves a "shadow timer" which does nothing around, need to force a tick
  1738. jQuery.fx.tick();
  1739. });
  1740. test( ".finish( \"custom\" ) - custom queue animations", function() {
  1741. var animations = {
  1742. top: 100,
  1743. left: 100,
  1744. height: 100,
  1745. width: 100
  1746. },
  1747. div = jQuery("<div>");
  1748. expect( 11 );
  1749. jQuery.each( animations, function( prop, value ) {
  1750. var anim = {};
  1751. anim[ prop ] = value;
  1752. div.css( prop, 1 ).animate( anim, {
  1753. queue: "custom",
  1754. complete: function() {
  1755. ok( true, "Called animation callback for " + prop );
  1756. }
  1757. });
  1758. });
  1759. equal( div.queue( "custom" ).length, 4, "4 animations in the queue" );
  1760. // start the first animation
  1761. div.dequeue( "custom" );
  1762. equal( div.is(":animated"), true, ":animated matches" );
  1763. div.finish( "custom" );
  1764. jQuery.each( animations, function( prop, value ) {
  1765. equal( parseFloat( div.css( prop ) ), value, prop + " finished at correct value" );
  1766. });
  1767. equal( div.is(":animated"), false, ":animated doesn't match" );
  1768. // cleanup
  1769. div.remove();
  1770. // leaves a "shadow timer" which does nothing around, need to force a tick
  1771. jQuery.fx.tick();
  1772. });
  1773. test( ".finish() calls finish of custom queue functions", function() {
  1774. function queueTester( next, hooks ) {
  1775. hooks.stop = function( gotoEnd ) {
  1776. inside++;
  1777. equal( this, div[0] );
  1778. ok( gotoEnd, "hooks.stop(true) called");
  1779. };
  1780. }
  1781. var div = jQuery( "<div>" ),
  1782. inside = 0,
  1783. outside = 0;
  1784. expect( 6 );
  1785. queueTester.finish = function() {
  1786. outside++;
  1787. ok( true, "Finish called on custom queue function" );
  1788. };
  1789. div.queue( queueTester ).queue( queueTester ).queue( queueTester ).finish();
  1790. equal( inside, 1, "1 stop(true) callback" );
  1791. equal( outside, 2, "2 finish callbacks" );
  1792. div.remove();
  1793. });
  1794. asyncTest( ".finish() is applied correctly when multiple elements were animated (#13937)", function() {
  1795. expect( 3 );
  1796. var elems = jQuery("<a>0</a><a>1</a><a>2</a>");
  1797. elems.animate( { opacity: 0 }, 1500 ).animate( { opacity: 1 }, 1500 );
  1798. setTimeout(function() {
  1799. elems.eq( 1 ).finish();
  1800. ok( !elems.eq( 1 ).queue().length, "empty queue for .finish()ed element" );
  1801. ok( elems.eq( 0 ).queue().length, "non-empty queue for preceding element" );
  1802. ok( elems.eq( 2 ).queue().length, "non-empty queue for following element" );
  1803. elems.stop( true );
  1804. // setTimeout needed in order to avoid setInterval/setTimeout execution bug in FF
  1805. window.setTimeout(function() {
  1806. start();
  1807. }, 1000 );
  1808. }, 100 );
  1809. });
  1810. asyncTest( "slideDown() after stop() (#13483)", 2, function() {
  1811. var ul = jQuery( "<ul style='height: 100px;display: block'></ul>" ),
  1812. origHeight = ul.height();
  1813. // First test. slideUp() -> stop() in the middle -> slideDown() until the end
  1814. ul.slideUp( 1000 );
  1815. setTimeout( function() {
  1816. ul.stop( true );
  1817. ul.slideDown( 1, function() {
  1818. equal( ul.height(), origHeight, "slideDown() after interrupting slideUp() with stop(). Height must be in original value" );
  1819. // Second test. slideDown() -> stop() in the middle -> slideDown() until the end
  1820. ul.slideUp( 1, function() {
  1821. ul.slideDown( 1000 );
  1822. setTimeout( function() {
  1823. ul.stop( true );
  1824. ul.slideDown( 1, function() {
  1825. equal( ul.height(), origHeight, "slideDown() after interrupting slideDown() with stop(). Height must be in original value" );
  1826. // Cleanup
  1827. ul.remove();
  1828. start();
  1829. });
  1830. }, 500 );
  1831. });
  1832. });
  1833. }, 500 );
  1834. });
  1835. asyncTest( "fadeIn() after stop() (related to #13483)", 2, function() {
  1836. var ul = jQuery( "<ul style='height: 100px;display: block; opacity: 1'></ul>" ),
  1837. origOpacity = ul.css( "opacity" );
  1838. // First test. fadeOut() -> stop() in the middle -> fadeIn() until the end
  1839. ul.fadeOut( 1000 );
  1840. setTimeout( function() {
  1841. ul.stop( true );
  1842. ul.fadeIn( 1, function() {
  1843. equal( ul.css( "opacity" ), origOpacity, "fadeIn() after interrupting fadeOut() with stop(). Opacity must be in original value" );
  1844. // Second test. fadeIn() -> stop() in the middle -> fadeIn() until the end
  1845. ul.fadeOut( 1, function() {
  1846. ul.fadeIn( 1000 );
  1847. setTimeout( function() {
  1848. ul.stop( true );
  1849. ul.fadeIn( 1, function() {
  1850. equal( ul.css("opacity"), origOpacity, "fadeIn() after interrupting fadeIn() with stop(). Opacity must be in original value" );
  1851. // Cleanup
  1852. ul.remove();
  1853. start();
  1854. });
  1855. }, 500 );
  1856. });
  1857. });
  1858. }, 500 );
  1859. });
  1860. })();