animationqueue_test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // Copyright 2011 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.AnimationQueueTest');
  15. goog.setTestOnly('goog.fx.AnimationQueueTest');
  16. goog.require('goog.events');
  17. goog.require('goog.fx.Animation');
  18. goog.require('goog.fx.AnimationParallelQueue');
  19. goog.require('goog.fx.AnimationSerialQueue');
  20. goog.require('goog.fx.Transition');
  21. goog.require('goog.fx.anim');
  22. goog.require('goog.testing.MockClock');
  23. goog.require('goog.testing.jsunit');
  24. var clock;
  25. function setUpPage() {
  26. clock = new goog.testing.MockClock(true);
  27. goog.fx.anim.setAnimationWindow(null);
  28. }
  29. function tearDownPage() {
  30. clock.dispose();
  31. }
  32. function testParallelEvents() {
  33. var anim = new goog.fx.AnimationParallelQueue();
  34. anim.add(new goog.fx.Animation([0], [100], 200));
  35. anim.add(new goog.fx.Animation([0], [100], 400));
  36. anim.add(new goog.fx.Animation([0], [100], 600));
  37. assertTrue(anim.queue[0].isStopped());
  38. assertTrue(anim.queue[1].isStopped());
  39. assertTrue(anim.queue[2].isStopped());
  40. assertTrue(anim.isStopped());
  41. var playEvents = 0, beginEvents = 0, resumeEvents = 0, pauseEvents = 0;
  42. var endEvents = 0, stopEvents = 0, finishEvents = 0;
  43. goog.events.listen(
  44. anim, goog.fx.Transition.EventType.PLAY, function() { ++playEvents; });
  45. goog.events.listen(
  46. anim, goog.fx.Transition.EventType.BEGIN, function() { ++beginEvents; });
  47. goog.events.listen(anim, goog.fx.Transition.EventType.RESUME, function() {
  48. ++resumeEvents;
  49. });
  50. goog.events.listen(
  51. anim, goog.fx.Transition.EventType.PAUSE, function() { ++pauseEvents; });
  52. goog.events.listen(
  53. anim, goog.fx.Transition.EventType.END, function() { ++endEvents; });
  54. goog.events.listen(
  55. anim, goog.fx.Transition.EventType.STOP, function() { ++stopEvents; });
  56. goog.events.listen(anim, goog.fx.Transition.EventType.FINISH, function() {
  57. ++finishEvents;
  58. });
  59. // PLAY, BEGIN
  60. anim.play();
  61. // No queue events.
  62. clock.tick(100);
  63. // PAUSE
  64. anim.pause();
  65. // No queue events
  66. clock.tick(200);
  67. // PLAY, RESUME
  68. anim.play();
  69. // No queue events.
  70. clock.tick(400);
  71. // END, STOP
  72. anim.stop();
  73. // PLAY, BEGIN
  74. anim.play();
  75. // No queue events.
  76. clock.tick(400);
  77. // END, FINISH
  78. clock.tick(200);
  79. // Make sure the event counts are right.
  80. assertEquals(3, playEvents);
  81. assertEquals(2, beginEvents);
  82. assertEquals(1, resumeEvents);
  83. assertEquals(1, pauseEvents);
  84. assertEquals(2, endEvents);
  85. assertEquals(1, stopEvents);
  86. assertEquals(1, finishEvents);
  87. }
  88. function testSerialEvents() {
  89. var anim = new goog.fx.AnimationSerialQueue();
  90. anim.add(new goog.fx.Animation([0], [100], 100));
  91. anim.add(new goog.fx.Animation([0], [100], 200));
  92. anim.add(new goog.fx.Animation([0], [100], 300));
  93. assertTrue(anim.queue[0].isStopped());
  94. assertTrue(anim.queue[1].isStopped());
  95. assertTrue(anim.queue[2].isStopped());
  96. assertTrue(anim.isStopped());
  97. var playEvents = 0, beginEvents = 0, resumeEvents = 0, pauseEvents = 0;
  98. var endEvents = 0, stopEvents = 0, finishEvents = 0;
  99. goog.events.listen(
  100. anim, goog.fx.Transition.EventType.PLAY, function() { ++playEvents; });
  101. goog.events.listen(
  102. anim, goog.fx.Transition.EventType.BEGIN, function() { ++beginEvents; });
  103. goog.events.listen(anim, goog.fx.Transition.EventType.RESUME, function() {
  104. ++resumeEvents;
  105. });
  106. goog.events.listen(
  107. anim, goog.fx.Transition.EventType.PAUSE, function() { ++pauseEvents; });
  108. goog.events.listen(
  109. anim, goog.fx.Transition.EventType.END, function() { ++endEvents; });
  110. goog.events.listen(
  111. anim, goog.fx.Transition.EventType.STOP, function() { ++stopEvents; });
  112. goog.events.listen(anim, goog.fx.Transition.EventType.FINISH, function() {
  113. ++finishEvents;
  114. });
  115. // PLAY, BEGIN
  116. anim.play();
  117. // No queue events.
  118. clock.tick(100);
  119. // PAUSE
  120. anim.pause();
  121. // No queue events
  122. clock.tick(200);
  123. // PLAY, RESUME
  124. anim.play();
  125. // No queue events.
  126. clock.tick(400);
  127. // END, STOP
  128. anim.stop();
  129. // PLAY, BEGIN
  130. anim.play(true);
  131. // No queue events.
  132. clock.tick(400);
  133. // END, FINISH
  134. clock.tick(200);
  135. // Make sure the event counts are right.
  136. assertEquals(3, playEvents);
  137. assertEquals(2, beginEvents);
  138. assertEquals(1, resumeEvents);
  139. assertEquals(1, pauseEvents);
  140. assertEquals(2, endEvents);
  141. assertEquals(1, stopEvents);
  142. assertEquals(1, finishEvents);
  143. }
  144. function testParallelPause() {
  145. var anim = new goog.fx.AnimationParallelQueue();
  146. anim.add(new goog.fx.Animation([0], [100], 100));
  147. anim.add(new goog.fx.Animation([0], [100], 200));
  148. anim.add(new goog.fx.Animation([0], [100], 300));
  149. assertTrue(anim.queue[0].isStopped());
  150. assertTrue(anim.queue[1].isStopped());
  151. assertTrue(anim.queue[2].isStopped());
  152. assertTrue(anim.isStopped());
  153. anim.play();
  154. assertTrue(anim.queue[0].isPlaying());
  155. assertTrue(anim.queue[1].isPlaying());
  156. assertTrue(anim.queue[2].isPlaying());
  157. assertTrue(anim.isPlaying());
  158. clock.tick(100);
  159. assertTrue(anim.queue[0].isStopped());
  160. assertTrue(anim.queue[1].isPlaying());
  161. assertTrue(anim.queue[2].isPlaying());
  162. assertTrue(anim.isPlaying());
  163. anim.pause();
  164. assertTrue(anim.queue[0].isStopped());
  165. assertTrue(anim.queue[1].isPaused());
  166. assertTrue(anim.queue[2].isPaused());
  167. assertTrue(anim.isPaused());
  168. clock.tick(200);
  169. assertTrue(anim.queue[0].isStopped());
  170. assertTrue(anim.queue[1].isPaused());
  171. assertTrue(anim.queue[2].isPaused());
  172. assertTrue(anim.isPaused());
  173. anim.play();
  174. assertTrue(anim.queue[0].isStopped());
  175. assertTrue(anim.queue[1].isPlaying());
  176. assertTrue(anim.queue[2].isPlaying());
  177. assertTrue(anim.isPlaying());
  178. clock.tick(100);
  179. assertTrue(anim.queue[0].isStopped());
  180. assertTrue(anim.queue[1].isStopped());
  181. assertTrue(anim.queue[2].isPlaying());
  182. assertTrue(anim.isPlaying());
  183. anim.pause();
  184. assertTrue(anim.queue[0].isStopped());
  185. assertTrue(anim.queue[1].isStopped());
  186. assertTrue(anim.queue[2].isPaused());
  187. assertTrue(anim.isPaused());
  188. clock.tick(200);
  189. assertTrue(anim.queue[0].isStopped());
  190. assertTrue(anim.queue[1].isStopped());
  191. assertTrue(anim.queue[2].isPaused());
  192. assertTrue(anim.isPaused());
  193. anim.play();
  194. assertTrue(anim.queue[0].isStopped());
  195. assertTrue(anim.queue[1].isStopped());
  196. assertTrue(anim.queue[2].isPlaying());
  197. assertTrue(anim.isPlaying());
  198. clock.tick(100);
  199. assertTrue(anim.queue[0].isStopped());
  200. assertTrue(anim.queue[1].isStopped());
  201. assertTrue(anim.queue[2].isStopped());
  202. assertTrue(anim.isStopped());
  203. }
  204. function testSerialPause() {
  205. var anim = new goog.fx.AnimationSerialQueue();
  206. anim.add(new goog.fx.Animation([0], [100], 100));
  207. anim.add(new goog.fx.Animation([0], [100], 200));
  208. anim.add(new goog.fx.Animation([0], [100], 300));
  209. assertTrue(anim.queue[0].isStopped());
  210. assertTrue(anim.queue[1].isStopped());
  211. assertTrue(anim.queue[2].isStopped());
  212. assertTrue(anim.isStopped());
  213. anim.play();
  214. assertTrue(anim.queue[0].isPlaying());
  215. assertTrue(anim.queue[1].isStopped());
  216. assertTrue(anim.queue[2].isStopped());
  217. assertTrue(anim.isPlaying());
  218. clock.tick(100);
  219. assertTrue(anim.queue[0].isStopped());
  220. assertTrue(anim.queue[1].isPlaying());
  221. assertTrue(anim.queue[2].isStopped());
  222. assertTrue(anim.isPlaying());
  223. anim.pause();
  224. assertTrue(anim.queue[0].isStopped());
  225. assertTrue(anim.queue[1].isPaused());
  226. assertTrue(anim.queue[2].isStopped());
  227. assertTrue(anim.isPaused());
  228. clock.tick(400);
  229. assertTrue(anim.queue[0].isStopped());
  230. assertTrue(anim.queue[1].isPaused());
  231. assertTrue(anim.queue[2].isStopped());
  232. assertTrue(anim.isPaused());
  233. anim.play();
  234. assertTrue(anim.queue[0].isStopped());
  235. assertTrue(anim.queue[1].isPlaying());
  236. assertTrue(anim.queue[2].isStopped());
  237. assertTrue(anim.isPlaying());
  238. clock.tick(200);
  239. assertTrue(anim.queue[0].isStopped());
  240. assertTrue(anim.queue[1].isStopped());
  241. assertTrue(anim.queue[2].isPlaying());
  242. assertTrue(anim.isPlaying());
  243. anim.pause();
  244. assertTrue(anim.queue[0].isStopped());
  245. assertTrue(anim.queue[1].isStopped());
  246. assertTrue(anim.queue[2].isPaused());
  247. assertTrue(anim.isPaused());
  248. clock.tick(300);
  249. assertTrue(anim.queue[0].isStopped());
  250. assertTrue(anim.queue[1].isStopped());
  251. assertTrue(anim.queue[2].isPaused());
  252. assertTrue(anim.isPaused());
  253. anim.play();
  254. clock.tick(300);
  255. assertTrue(anim.queue[0].isStopped());
  256. assertTrue(anim.queue[1].isStopped());
  257. assertTrue(anim.queue[2].isStopped());
  258. assertTrue(anim.isStopped());
  259. }