animationframe_test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2014 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. /**
  15. * @fileoverview Tests for goog.dom.animationFrame.
  16. */
  17. goog.setTestOnly();
  18. goog.require('goog.dom.animationFrame');
  19. goog.require('goog.testing.MockClock');
  20. goog.require('goog.testing.jsunit');
  21. var NEXT_FRAME = goog.testing.MockClock.REQUEST_ANIMATION_FRAME_TIMEOUT;
  22. var mockClock;
  23. var t0, t1;
  24. var result;
  25. function setUp() {
  26. mockClock = new goog.testing.MockClock(true);
  27. result = '';
  28. t0 = goog.dom.animationFrame.createTask({
  29. measure: function() { result += 'me0'; },
  30. mutate: function() { result += 'mu0'; }
  31. });
  32. t1 = goog.dom.animationFrame.createTask({
  33. measure: function() { result += 'me1'; },
  34. mutate: function() { result += 'mu1'; }
  35. });
  36. assertEquals('', result);
  37. }
  38. function tearDown() {
  39. mockClock.dispose();
  40. }
  41. function testCreateTask_one() {
  42. t0();
  43. assertEquals('', result);
  44. mockClock.tick(NEXT_FRAME);
  45. assertEquals('me0mu0', result);
  46. mockClock.tick(NEXT_FRAME);
  47. assertEquals('me0mu0', result);
  48. t0();
  49. t0(); // Should do nothing.
  50. mockClock.tick(NEXT_FRAME);
  51. assertEquals('me0mu0me0mu0', result);
  52. }
  53. function testCreateTask_onlyMutate() {
  54. t0 = goog.dom.animationFrame.createTask(
  55. {mutate: function() { result += 'mu0'; }});
  56. t0();
  57. assertEquals('', result);
  58. mockClock.tick(NEXT_FRAME);
  59. assertEquals('mu0', result);
  60. }
  61. function testCreateTask_onlyMeasure() {
  62. t0 = goog.dom.animationFrame.createTask(
  63. {mutate: function() { result += 'me0'; }});
  64. t0();
  65. assertEquals('', result);
  66. mockClock.tick(NEXT_FRAME);
  67. assertEquals('me0', result);
  68. }
  69. function testCreateTask_two() {
  70. t0();
  71. t1();
  72. assertEquals('', result);
  73. mockClock.tick(NEXT_FRAME);
  74. assertEquals('me0me1mu0mu1', result);
  75. mockClock.tick(NEXT_FRAME);
  76. assertEquals('me0me1mu0mu1', result);
  77. t0();
  78. t1();
  79. t0();
  80. t1();
  81. mockClock.tick(NEXT_FRAME);
  82. assertEquals('me0me1mu0mu1me0me1mu0mu1', result);
  83. }
  84. function testCreateTask_recurse() {
  85. var stop = false;
  86. var recurse = goog.dom.animationFrame.createTask({
  87. measure: function() {
  88. if (!stop) {
  89. recurse();
  90. }
  91. result += 're0';
  92. },
  93. mutate: function() { result += 'ru0'; }
  94. });
  95. recurse();
  96. mockClock.tick(NEXT_FRAME);
  97. assertEquals('re0ru0', result);
  98. mockClock.tick(NEXT_FRAME);
  99. assertEquals('re0ru0re0ru0', result);
  100. mockClock.tick(NEXT_FRAME);
  101. assertEquals('re0ru0re0ru0re0ru0', result);
  102. t0();
  103. stop = true;
  104. mockClock.tick(NEXT_FRAME);
  105. assertEquals('re0ru0re0ru0re0ru0re0me0ru0mu0', result);
  106. // Recursion should have stopped now.
  107. mockClock.tick(NEXT_FRAME);
  108. assertEquals('re0ru0re0ru0re0ru0re0me0ru0mu0', result);
  109. assertFalse(goog.dom.animationFrame.requestedFrame_);
  110. mockClock.tick(NEXT_FRAME);
  111. assertEquals('re0ru0re0ru0re0ru0re0me0ru0mu0', result);
  112. assertFalse(goog.dom.animationFrame.requestedFrame_);
  113. }
  114. function testCreateTask_recurseTwoMethodsWithState() {
  115. var stop = false;
  116. var recurse1 = goog.dom.animationFrame.createTask({
  117. measure: function(state) {
  118. if (!stop) {
  119. recurse2();
  120. }
  121. result += 'r1e0';
  122. state.text = 'T0';
  123. },
  124. mutate: function(state) { result += 'r1u0' + state.text; }
  125. });
  126. var recurse2 = goog.dom.animationFrame.createTask({
  127. measure: function(state) {
  128. if (!stop) {
  129. recurse1();
  130. }
  131. result += 'r2e0';
  132. state.text = 'T1';
  133. },
  134. mutate: function(state) { result += 'r2u0' + state.text; }
  135. });
  136. var taskLength = goog.dom.animationFrame.tasks_[0].length;
  137. recurse1();
  138. mockClock.tick(NEXT_FRAME);
  139. // Only recurse1 executed.
  140. assertEquals('r1e0r1u0T0', result);
  141. mockClock.tick(NEXT_FRAME);
  142. // Recurse2 executed and queueup recurse1.
  143. assertEquals('r1e0r1u0T0r2e0r2u0T1', result);
  144. mockClock.tick(NEXT_FRAME);
  145. // Recurse1 executed and queueup recurse2.
  146. assertEquals('r1e0r1u0T0r2e0r2u0T1r1e0r1u0T0', result);
  147. stop = true;
  148. mockClock.tick(NEXT_FRAME);
  149. // Recurse2 executed and should have stopped.
  150. assertEquals('r1e0r1u0T0r2e0r2u0T1r1e0r1u0T0r2e0r2u0T1', result);
  151. assertFalse(goog.dom.animationFrame.requestedFrame_);
  152. mockClock.tick(NEXT_FRAME);
  153. assertEquals('r1e0r1u0T0r2e0r2u0T1r1e0r1u0T0r2e0r2u0T1', result);
  154. assertFalse(goog.dom.animationFrame.requestedFrame_);
  155. mockClock.tick(NEXT_FRAME);
  156. assertEquals('r1e0r1u0T0r2e0r2u0T1r1e0r1u0T0r2e0r2u0T1', result);
  157. assertFalse(goog.dom.animationFrame.requestedFrame_);
  158. }
  159. function testCreateTask_args() {
  160. var context = {context: true};
  161. var s = goog.dom.animationFrame.createTask(
  162. {
  163. measure: function(state) {
  164. assertEquals(context, this);
  165. assertUndefined(state.foo);
  166. state.foo = 'foo';
  167. },
  168. mutate: function(state) {
  169. assertEquals(context, this);
  170. result += state.foo;
  171. }
  172. },
  173. context);
  174. s();
  175. mockClock.tick(NEXT_FRAME);
  176. assertEquals('foo', result);
  177. var moreArgs = goog.dom.animationFrame.createTask({
  178. measure: function(event, state) {
  179. assertEquals('event', event);
  180. state.baz = 'baz';
  181. },
  182. mutate: function(event, state) {
  183. assertEquals('event', event);
  184. result += state.baz;
  185. }
  186. });
  187. moreArgs('event');
  188. mockClock.tick(NEXT_FRAME);
  189. assertEquals('foobaz', result);
  190. }
  191. function testIsRunning() {
  192. var result = '';
  193. var task = goog.dom.animationFrame.createTask({
  194. measure: function() {
  195. result += 'me';
  196. assertTrue(goog.dom.animationFrame.isRunning());
  197. },
  198. mutate: function() {
  199. result += 'mu';
  200. assertTrue(goog.dom.animationFrame.isRunning());
  201. }
  202. });
  203. task();
  204. assertFalse(goog.dom.animationFrame.isRunning());
  205. mockClock.tick(NEXT_FRAME);
  206. assertFalse(goog.dom.animationFrame.isRunning());
  207. assertEquals('memu', result);
  208. }