anim_test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.fx.animTest');
  15. goog.setTestOnly('goog.fx.animTest');
  16. goog.require('goog.async.AnimationDelay');
  17. goog.require('goog.async.Delay');
  18. goog.require('goog.events');
  19. goog.require('goog.functions');
  20. goog.require('goog.fx.Animation');
  21. goog.require('goog.fx.anim');
  22. goog.require('goog.object');
  23. goog.require('goog.testing.MockClock');
  24. goog.require('goog.testing.PropertyReplacer');
  25. goog.require('goog.testing.jsunit');
  26. goog.require('goog.testing.recordFunction');
  27. goog.require('goog.userAgent');
  28. var clock, replacer;
  29. function setUpPage() {
  30. clock = new goog.testing.MockClock(true);
  31. }
  32. function tearDownPage() {
  33. clock.dispose();
  34. }
  35. function setUp() {
  36. replacer = new goog.testing.PropertyReplacer();
  37. }
  38. function tearDown() {
  39. replacer.reset();
  40. goog.fx.anim.tearDown();
  41. }
  42. function testDelayWithMocks() {
  43. goog.fx.anim.setAnimationWindow(null);
  44. registerAndUnregisterAnimationWithMocks(goog.async.Delay);
  45. }
  46. function testAnimationDelayWithMocks() {
  47. goog.fx.anim.setAnimationWindow(window);
  48. registerAndUnregisterAnimationWithMocks(goog.async.AnimationDelay);
  49. }
  50. /**
  51. * @param {!Function} delayType The constructor for Delay or AnimationDelay.
  52. * The methods will be mocked out.
  53. */
  54. function registerAndUnregisterAnimationWithMocks(delayType) {
  55. var timerCount = 0;
  56. replacer.set(delayType.prototype, 'start', function() { timerCount++; });
  57. replacer.set(delayType.prototype, 'stop', function() { timerCount--; });
  58. replacer.set(
  59. delayType.prototype, 'isActive', function() { return timerCount > 0; });
  60. var forbiddenDelayType = delayType == goog.async.AnimationDelay ?
  61. goog.async.Delay :
  62. goog.async.AnimationDelay;
  63. replacer.set(forbiddenDelayType.prototype, 'start', goog.functions.error());
  64. replacer.set(forbiddenDelayType.prototype, 'stop', goog.functions.error());
  65. replacer.set(
  66. forbiddenDelayType.prototype, 'isActive', goog.functions.error());
  67. var anim = new goog.fx.Animation([0], [1], 1000);
  68. var anim2 = new goog.fx.Animation([0], [1], 1000);
  69. goog.fx.anim.registerAnimation(anim);
  70. assertTrue(
  71. 'Should contain the animation',
  72. goog.object.containsValue(goog.fx.anim.activeAnimations_, anim));
  73. assertEquals('Should have called start once', 1, timerCount);
  74. goog.fx.anim.registerAnimation(anim2);
  75. assertEquals('Should not have called start again', 1, timerCount);
  76. // Add anim again.
  77. goog.fx.anim.registerAnimation(anim);
  78. assertTrue(
  79. 'Should contain the animation',
  80. goog.object.containsValue(goog.fx.anim.activeAnimations_, anim));
  81. assertEquals('Should not have called start again', 1, timerCount);
  82. goog.fx.anim.unregisterAnimation(anim);
  83. assertFalse(
  84. 'Should not contain the animation',
  85. goog.object.containsValue(goog.fx.anim.activeAnimations_, anim));
  86. assertEquals('clearTimeout should not have been called', 1, timerCount);
  87. goog.fx.anim.unregisterAnimation(anim2);
  88. assertEquals('There should be no remaining timers', 0, timerCount);
  89. // Make sure we don't trigger setTimeout or setInterval.
  90. clock.tick(1000);
  91. goog.fx.anim.cycleAnimations_(goog.now());
  92. assertEquals('There should be no remaining timers', 0, timerCount);
  93. anim.dispose();
  94. anim2.dispose();
  95. }
  96. function testRegisterAndUnregisterAnimationWithRequestAnimationFrameGecko() {
  97. // Only FF4 onwards support requestAnimationFrame.
  98. if (!goog.userAgent.GECKO || !goog.userAgent.isVersionOrHigher('2.0') ||
  99. goog.userAgent.isVersionOrHigher('17')) {
  100. return;
  101. }
  102. goog.fx.anim.setAnimationWindow(window);
  103. var anim = new goog.fx.Animation([0], [1], 1000);
  104. var anim2 = new goog.fx.Animation([0], [1], 1000);
  105. goog.fx.anim.registerAnimation(anim);
  106. assertTrue(
  107. 'Should contain the animation',
  108. goog.object.containsValue(goog.fx.anim.activeAnimations_, anim));
  109. assertEquals(
  110. 'Should have listen to MozBeforePaint once', 1,
  111. goog.events.getListeners(window, 'MozBeforePaint', false).length);
  112. goog.fx.anim.registerAnimation(anim2);
  113. assertEquals(
  114. 'Should not add more listener for MozBeforePaint', 1,
  115. goog.events.getListeners(window, 'MozBeforePaint', false).length);
  116. // Add anim again.
  117. goog.fx.anim.registerAnimation(anim);
  118. assertTrue(
  119. 'Should contain the animation',
  120. goog.object.containsValue(goog.fx.anim.activeAnimations_, anim));
  121. assertEquals(
  122. 'Should not add more listener for MozBeforePaint', 1,
  123. goog.events.getListeners(window, 'MozBeforePaint', false).length);
  124. goog.fx.anim.unregisterAnimation(anim);
  125. assertFalse(
  126. 'Should not contain the animation',
  127. goog.object.containsValue(goog.fx.anim.activeAnimations_, anim));
  128. assertEquals(
  129. 'Should not clear listener for MozBeforePaint yet', 1,
  130. goog.events.getListeners(window, 'MozBeforePaint', false).length);
  131. goog.fx.anim.unregisterAnimation(anim2);
  132. assertEquals(
  133. 'There should be no more listener for MozBeforePaint', 0,
  134. goog.events.getListeners(window, 'MozBeforePaint', false).length);
  135. anim.dispose();
  136. anim2.dispose();
  137. goog.fx.anim.setAnimationWindow(null);
  138. }
  139. function testRegisterUnregisterAnimation() {
  140. var anim = new goog.fx.Animation([0], [1], 1000);
  141. goog.fx.anim.registerAnimation(anim);
  142. assertTrue(
  143. 'There should be an active timer',
  144. goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive());
  145. assertEquals(
  146. 'There should be an active animations', 1,
  147. goog.object.getCount(goog.fx.anim.activeAnimations_));
  148. goog.fx.anim.unregisterAnimation(anim);
  149. assertTrue(
  150. 'There should be no active animations',
  151. goog.object.isEmpty(goog.fx.anim.activeAnimations_));
  152. assertFalse(
  153. 'There should be no active timer',
  154. goog.fx.anim.animationDelay_ && goog.fx.anim.animationDelay_.isActive());
  155. anim.dispose();
  156. }
  157. function testCycleWithMockClock() {
  158. goog.fx.anim.setAnimationWindow(null);
  159. var anim = new goog.fx.Animation([0], [1], 1000);
  160. anim.onAnimationFrame = goog.testing.recordFunction();
  161. goog.fx.anim.registerAnimation(anim);
  162. clock.tick(goog.fx.anim.TIMEOUT);
  163. assertEquals(1, anim.onAnimationFrame.getCallCount());
  164. }
  165. function testCycleWithMockClockAndAnimationWindow() {
  166. goog.fx.anim.setAnimationWindow(window);
  167. var anim = new goog.fx.Animation([0], [1], 1000);
  168. anim.onAnimationFrame = goog.testing.recordFunction();
  169. goog.fx.anim.registerAnimation(anim);
  170. clock.tick(goog.fx.anim.TIMEOUT);
  171. assertEquals(1, anim.onAnimationFrame.getCallCount());
  172. }