Debian Codegraph @

closure-library/closure/goog/dom/animationframe/animationframe_test.js

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