OLD | NEW |
| (Empty) |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/layer_animation_controller.h" | |
6 | |
7 #include "cc/animation.h" | |
8 #include "cc/animation_curve.h" | |
9 #include "cc/keyframed_animation_curve.h" | |
10 #include "cc/test/animation_test_common.h" | |
11 #include "cc/transform_operations.h" | |
12 #include "testing/gmock/include/gmock/gmock.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 #include "ui/gfx/transform.h" | |
15 | |
16 namespace cc { | |
17 namespace { | |
18 | |
19 void ExpectTranslateX(double translate_x, const gfx::Transform& matrix) { | |
20 EXPECT_FLOAT_EQ(translate_x, matrix.matrix().getDouble(0, 3)); } | |
21 | |
22 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve, | |
23 int id, | |
24 Animation::TargetProperty property) { | |
25 return Animation::Create(curve.Pass(), 0, id, property); | |
26 } | |
27 | |
28 TEST(LayerAnimationControllerTest, SyncNewAnimation) { | |
29 FakeLayerAnimationValueObserver dummy_impl; | |
30 scoped_refptr<LayerAnimationController> controller_impl( | |
31 LayerAnimationController::Create(0)); | |
32 controller_impl->AddObserver(&dummy_impl); | |
33 FakeLayerAnimationValueObserver dummy; | |
34 scoped_refptr<LayerAnimationController> controller( | |
35 LayerAnimationController::Create(0)); | |
36 controller->AddObserver(&dummy); | |
37 | |
38 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
39 | |
40 addOpacityTransitionToController(*controller, 1, 0, 1, false); | |
41 | |
42 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
43 | |
44 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
45 EXPECT_EQ(Animation::WaitingForTargetAvailability, | |
46 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
47 } | |
48 | |
49 // If an animation is started on the impl thread before it is ticked on the main | |
50 // thread, we must be sure to respect the synchronized start time. | |
51 TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) { | |
52 FakeLayerAnimationValueObserver dummy_impl; | |
53 scoped_refptr<LayerAnimationController> controller_impl( | |
54 LayerAnimationController::Create(0)); | |
55 controller_impl->AddObserver(&dummy_impl); | |
56 FakeLayerAnimationValueObserver dummy; | |
57 scoped_refptr<LayerAnimationController> controller( | |
58 LayerAnimationController::Create(0)); | |
59 controller->AddObserver(&dummy); | |
60 | |
61 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
62 | |
63 addOpacityTransitionToController(*controller, 1, 0, 1, false); | |
64 | |
65 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
66 | |
67 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
68 EXPECT_EQ(Animation::WaitingForTargetAvailability, | |
69 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
70 | |
71 AnimationEventsVector events; | |
72 controller_impl->Animate(1.0); | |
73 controller_impl->UpdateState(&events); | |
74 | |
75 // Synchronize the start times. | |
76 EXPECT_EQ(1u, events.size()); | |
77 controller->OnAnimationStarted(events[0]); | |
78 EXPECT_EQ(controller->GetAnimation(0, Animation::Opacity)->start_time(), | |
79 controller_impl->GetAnimation(0, Animation::Opacity)->start_time()); | |
80 | |
81 // Start the animation on the main thread. Should not affect the start time. | |
82 controller->Animate(1.5); | |
83 controller->UpdateState(NULL); | |
84 EXPECT_EQ(controller->GetAnimation(0, Animation::Opacity)->start_time(), | |
85 controller_impl->GetAnimation(0, Animation::Opacity)->start_time()); | |
86 } | |
87 | |
88 TEST(LayerAnimationControllerTest, SyncPauseAndResume) { | |
89 FakeLayerAnimationValueObserver dummy_impl; | |
90 scoped_refptr<LayerAnimationController> controller_impl( | |
91 LayerAnimationController::Create(0)); | |
92 controller_impl->AddObserver(&dummy_impl); | |
93 FakeLayerAnimationValueObserver dummy; | |
94 scoped_refptr<LayerAnimationController> controller( | |
95 LayerAnimationController::Create(0)); | |
96 controller->AddObserver(&dummy); | |
97 | |
98 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
99 | |
100 addOpacityTransitionToController(*controller, 1, 0, 1, false); | |
101 | |
102 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
103 | |
104 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
105 EXPECT_EQ(Animation::WaitingForTargetAvailability, | |
106 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
107 | |
108 // Start the animations on each controller. | |
109 AnimationEventsVector events; | |
110 controller_impl->Animate(0.0); | |
111 controller_impl->UpdateState(&events); | |
112 controller->Animate(0.0); | |
113 controller->UpdateState(NULL); | |
114 EXPECT_EQ(Animation::Running, | |
115 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
116 EXPECT_EQ(Animation::Running, | |
117 controller->GetAnimation(0, Animation::Opacity)->run_state()); | |
118 | |
119 // Pause the main-thread animation. | |
120 controller->SuspendAnimations(1.0); | |
121 EXPECT_EQ(Animation::Paused, | |
122 controller->GetAnimation(0, Animation::Opacity)->run_state()); | |
123 | |
124 // The pause run state change should make it to the impl thread controller. | |
125 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
126 EXPECT_EQ(Animation::Paused, | |
127 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
128 | |
129 // Resume the main-thread animation. | |
130 controller->ResumeAnimations(2.0); | |
131 EXPECT_EQ(Animation::Running, | |
132 controller->GetAnimation(0, Animation::Opacity)->run_state()); | |
133 | |
134 // The pause run state change should make it to the impl thread controller. | |
135 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
136 EXPECT_EQ(Animation::Running, | |
137 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
138 } | |
139 | |
140 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) { | |
141 FakeLayerAnimationValueObserver dummy_impl; | |
142 scoped_refptr<LayerAnimationController> controller_impl( | |
143 LayerAnimationController::Create(0)); | |
144 controller_impl->AddObserver(&dummy_impl); | |
145 FakeLayerAnimationValueObserver dummy; | |
146 scoped_refptr<LayerAnimationController> controller( | |
147 LayerAnimationController::Create(0)); | |
148 controller->AddObserver(&dummy); | |
149 | |
150 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
151 | |
152 int animation_id = | |
153 addOpacityTransitionToController(*controller, 1, 0, 1, false); | |
154 | |
155 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
156 | |
157 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity)); | |
158 EXPECT_EQ(Animation::WaitingForTargetAvailability, | |
159 controller_impl->GetAnimation(0, Animation::Opacity)->run_state()); | |
160 | |
161 // Notify main thread controller that the animation has started. | |
162 AnimationEvent animation_started_event( | |
163 AnimationEvent::Started, 0, 0, Animation::Opacity, 0); | |
164 controller->OnAnimationStarted(animation_started_event); | |
165 | |
166 // Force animation to complete on impl thread. | |
167 controller_impl->RemoveAnimation(animation_id); | |
168 | |
169 EXPECT_FALSE(controller_impl->GetAnimation(animation_id, Animation::Opacity)); | |
170 | |
171 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
172 | |
173 // Even though the main thread has a 'new' animation, it should not be pushed | |
174 // because the animation has already completed on the impl thread. | |
175 EXPECT_FALSE(controller_impl->GetAnimation(animation_id, Animation::Opacity)); | |
176 } | |
177 | |
178 // Tests that transitioning opacity from 0 to 1 works as expected. | |
179 | |
180 static const AnimationEvent* GetMostRecentPropertyUpdateEvent( | |
181 const AnimationEventsVector* events) { | |
182 const AnimationEvent* event = 0; | |
183 for (size_t i = 0; i < events->size(); ++i) | |
184 if ((*events)[i].type == AnimationEvent::PropertyUpdate) | |
185 event = &(*events)[i]; | |
186 | |
187 return event; | |
188 } | |
189 | |
190 TEST(LayerAnimationControllerTest, TrivialTransition) { | |
191 scoped_ptr<AnimationEventsVector> events( | |
192 make_scoped_ptr(new AnimationEventsVector)); | |
193 FakeLayerAnimationValueObserver dummy; | |
194 scoped_refptr<LayerAnimationController> controller( | |
195 LayerAnimationController::Create(0)); | |
196 controller->AddObserver(&dummy); | |
197 | |
198 scoped_ptr<Animation> to_add(CreateAnimation( | |
199 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
200 1, | |
201 Animation::Opacity)); | |
202 | |
203 controller->AddAnimation(to_add.Pass()); | |
204 controller->Animate(0.0); | |
205 controller->UpdateState(events.get()); | |
206 EXPECT_TRUE(controller->HasActiveAnimation()); | |
207 EXPECT_EQ(0.f, dummy.opacity()); | |
208 // A non-implOnly animation should not generate property updates. | |
209 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get()); | |
210 EXPECT_FALSE(event); | |
211 controller->Animate(1.0); | |
212 controller->UpdateState(events.get()); | |
213 EXPECT_EQ(1.f, dummy.opacity()); | |
214 EXPECT_FALSE(controller->HasActiveAnimation()); | |
215 event = GetMostRecentPropertyUpdateEvent(events.get()); | |
216 EXPECT_FALSE(event); | |
217 } | |
218 | |
219 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) { | |
220 scoped_ptr<AnimationEventsVector> events( | |
221 make_scoped_ptr(new AnimationEventsVector)); | |
222 FakeLayerAnimationValueObserver dummy_impl; | |
223 scoped_refptr<LayerAnimationController> controller_impl( | |
224 LayerAnimationController::Create(0)); | |
225 controller_impl->AddObserver(&dummy_impl); | |
226 | |
227 scoped_ptr<Animation> to_add(CreateAnimation( | |
228 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
229 1, | |
230 Animation::Opacity)); | |
231 to_add->set_is_impl_only(true); | |
232 | |
233 controller_impl->AddAnimation(to_add.Pass()); | |
234 controller_impl->Animate(0.0); | |
235 controller_impl->UpdateState(events.get()); | |
236 EXPECT_TRUE(controller_impl->HasActiveAnimation()); | |
237 EXPECT_EQ(0.f, dummy_impl.opacity()); | |
238 EXPECT_EQ(2, events->size()); | |
239 const AnimationEvent* start_opacity_event = | |
240 GetMostRecentPropertyUpdateEvent(events.get()); | |
241 EXPECT_EQ(0, start_opacity_event->opacity); | |
242 | |
243 controller_impl->Animate(1.0); | |
244 controller_impl->UpdateState(events.get()); | |
245 EXPECT_EQ(1.f, dummy_impl.opacity()); | |
246 EXPECT_FALSE(controller_impl->HasActiveAnimation()); | |
247 EXPECT_EQ(4, events->size()); | |
248 const AnimationEvent* end_opacity_event = | |
249 GetMostRecentPropertyUpdateEvent(events.get()); | |
250 EXPECT_EQ(1, end_opacity_event->opacity); | |
251 } | |
252 | |
253 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) { | |
254 scoped_ptr<AnimationEventsVector> events( | |
255 make_scoped_ptr(new AnimationEventsVector)); | |
256 FakeLayerAnimationValueObserver dummy_impl; | |
257 scoped_refptr<LayerAnimationController> controller_impl( | |
258 LayerAnimationController::Create(0)); | |
259 controller_impl->AddObserver(&dummy_impl); | |
260 | |
261 // Choose different values for x and y to avoid coincidental values in the | |
262 // observed transforms. | |
263 const float delta_x = 3; | |
264 const float delta_y = 4; | |
265 | |
266 scoped_ptr<KeyframedTransformAnimationCurve> curve( | |
267 KeyframedTransformAnimationCurve::Create()); | |
268 | |
269 // Create simple Transform animation. | |
270 TransformOperations operations; | |
271 curve->AddKeyframe(TransformKeyframe::Create( | |
272 0, operations, scoped_ptr<cc::TimingFunction>())); | |
273 operations.AppendTranslate(delta_x, delta_y, 0); | |
274 curve->AddKeyframe(TransformKeyframe::Create( | |
275 1, operations, scoped_ptr<cc::TimingFunction>())); | |
276 | |
277 scoped_ptr<Animation> animation(Animation::Create( | |
278 curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform)); | |
279 animation->set_is_impl_only(true); | |
280 controller_impl->AddAnimation(animation.Pass()); | |
281 | |
282 // Run animation. | |
283 controller_impl->Animate(0.0); | |
284 controller_impl->UpdateState(events.get()); | |
285 EXPECT_TRUE(controller_impl->HasActiveAnimation()); | |
286 EXPECT_EQ(gfx::Transform(), dummy_impl.transform()); | |
287 EXPECT_EQ(2, events->size()); | |
288 const AnimationEvent* start_transform_event = | |
289 GetMostRecentPropertyUpdateEvent(events.get()); | |
290 ASSERT_TRUE(start_transform_event); | |
291 EXPECT_EQ(gfx::Transform(), start_transform_event->transform); | |
292 | |
293 gfx::Transform expected_transform; | |
294 expected_transform.Translate(delta_x, delta_y); | |
295 | |
296 controller_impl->Animate(1.0); | |
297 controller_impl->UpdateState(events.get()); | |
298 EXPECT_EQ(expected_transform, dummy_impl.transform()); | |
299 EXPECT_FALSE(controller_impl->HasActiveAnimation()); | |
300 EXPECT_EQ(4, events->size()); | |
301 const AnimationEvent* end_transform_event = | |
302 GetMostRecentPropertyUpdateEvent(events.get()); | |
303 EXPECT_EQ(expected_transform, end_transform_event->transform); | |
304 } | |
305 | |
306 // Tests animations that are waiting for a synchronized start time do not | |
307 // finish. | |
308 TEST(LayerAnimationControllerTest, | |
309 AnimationsWaitingForStartTimeDoNotFinishIfTheyWaitLongerToStartThanTheirDur
ation) { | |
310 scoped_ptr<AnimationEventsVector> events( | |
311 make_scoped_ptr(new AnimationEventsVector)); | |
312 FakeLayerAnimationValueObserver dummy; | |
313 scoped_refptr<LayerAnimationController> controller( | |
314 LayerAnimationController::Create(0)); | |
315 controller->AddObserver(&dummy); | |
316 | |
317 scoped_ptr<Animation> to_add(CreateAnimation( | |
318 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
319 1, | |
320 Animation::Opacity)); | |
321 to_add->set_needs_synchronized_start_time(true); | |
322 | |
323 // We should pause at the first keyframe indefinitely waiting for that | |
324 // animation to start. | |
325 controller->AddAnimation(to_add.Pass()); | |
326 controller->Animate(0.0); | |
327 controller->UpdateState(events.get()); | |
328 EXPECT_TRUE(controller->HasActiveAnimation()); | |
329 EXPECT_EQ(0.f, dummy.opacity()); | |
330 controller->Animate(1.0); | |
331 controller->UpdateState(events.get()); | |
332 EXPECT_TRUE(controller->HasActiveAnimation()); | |
333 EXPECT_EQ(0.f, dummy.opacity()); | |
334 controller->Animate(2.0); | |
335 controller->UpdateState(events.get()); | |
336 EXPECT_TRUE(controller->HasActiveAnimation()); | |
337 EXPECT_EQ(0.f, dummy.opacity()); | |
338 | |
339 // Send the synchronized start time. | |
340 controller->OnAnimationStarted(AnimationEvent( | |
341 AnimationEvent::Started, 0, 1, Animation::Opacity, 2)); | |
342 controller->Animate(5.0); | |
343 controller->UpdateState(events.get()); | |
344 EXPECT_EQ(1.f, dummy.opacity()); | |
345 EXPECT_FALSE(controller->HasActiveAnimation()); | |
346 } | |
347 | |
348 // Tests that two queued animations affecting the same property run in sequence. | |
349 TEST(LayerAnimationControllerTest, TrivialQueuing) { | |
350 scoped_ptr<AnimationEventsVector> events( | |
351 make_scoped_ptr(new AnimationEventsVector)); | |
352 FakeLayerAnimationValueObserver dummy; | |
353 scoped_refptr<LayerAnimationController> controller( | |
354 LayerAnimationController::Create(0)); | |
355 controller->AddObserver(&dummy); | |
356 | |
357 controller->AddAnimation(CreateAnimation( | |
358 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
359 1, | |
360 Animation::Opacity)); | |
361 controller->AddAnimation(CreateAnimation( | |
362 scoped_ptr<AnimationCurve>( | |
363 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(), | |
364 2, | |
365 Animation::Opacity)); | |
366 | |
367 controller->Animate(0.0); | |
368 controller->UpdateState(events.get()); | |
369 EXPECT_TRUE(controller->HasActiveAnimation()); | |
370 EXPECT_EQ(0.f, dummy.opacity()); | |
371 controller->Animate(1.0); | |
372 controller->UpdateState(events.get()); | |
373 EXPECT_TRUE(controller->HasActiveAnimation()); | |
374 EXPECT_EQ(1.f, dummy.opacity()); | |
375 controller->Animate(2.0); | |
376 controller->UpdateState(events.get()); | |
377 EXPECT_EQ(0.5f, dummy.opacity()); | |
378 EXPECT_FALSE(controller->HasActiveAnimation()); | |
379 } | |
380 | |
381 // Tests interrupting a transition with another transition. | |
382 TEST(LayerAnimationControllerTest, Interrupt) { | |
383 scoped_ptr<AnimationEventsVector> events( | |
384 make_scoped_ptr(new AnimationEventsVector)); | |
385 FakeLayerAnimationValueObserver dummy; | |
386 scoped_refptr<LayerAnimationController> controller( | |
387 LayerAnimationController::Create(0)); | |
388 controller->AddObserver(&dummy); | |
389 controller->AddAnimation(CreateAnimation( | |
390 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
391 1, | |
392 Animation::Opacity)); | |
393 controller->Animate(0.0); | |
394 controller->UpdateState(events.get()); | |
395 EXPECT_TRUE(controller->HasActiveAnimation()); | |
396 EXPECT_EQ(0.f, dummy.opacity()); | |
397 | |
398 scoped_ptr<Animation> to_add(CreateAnimation( | |
399 scoped_ptr<AnimationCurve>( | |
400 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(), | |
401 2, | |
402 Animation::Opacity)); | |
403 to_add->SetRunState(Animation::WaitingForNextTick, 0); | |
404 controller->AddAnimation(to_add.Pass()); | |
405 | |
406 // Since the animation was in the WaitingForNextTick state, it should start | |
407 // right in this call to animate. | |
408 controller->Animate(0.5); | |
409 controller->UpdateState(events.get()); | |
410 EXPECT_TRUE(controller->HasActiveAnimation()); | |
411 EXPECT_EQ(1.f, dummy.opacity()); | |
412 controller->Animate(1.5); | |
413 controller->UpdateState(events.get()); | |
414 EXPECT_EQ(0.5f, dummy.opacity()); | |
415 EXPECT_FALSE(controller->HasActiveAnimation()); | |
416 } | |
417 | |
418 // Tests scheduling two animations to run together when only one property is | |
419 // free. | |
420 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) { | |
421 scoped_ptr<AnimationEventsVector> events( | |
422 make_scoped_ptr(new AnimationEventsVector)); | |
423 FakeLayerAnimationValueObserver dummy; | |
424 scoped_refptr<LayerAnimationController> controller( | |
425 LayerAnimationController::Create(0)); | |
426 controller->AddObserver(&dummy); | |
427 | |
428 controller->AddAnimation(CreateAnimation( | |
429 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), | |
430 1, | |
431 Animation::Transform)); | |
432 controller->AddAnimation(CreateAnimation( | |
433 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), | |
434 2, | |
435 Animation::Transform)); | |
436 controller->AddAnimation(CreateAnimation( | |
437 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
438 2, | |
439 Animation::Opacity)); | |
440 | |
441 controller->Animate(0.0); | |
442 controller->UpdateState(events.get()); | |
443 EXPECT_EQ(0.f, dummy.opacity()); | |
444 EXPECT_TRUE(controller->HasActiveAnimation()); | |
445 controller->Animate(1.0); | |
446 controller->UpdateState(events.get()); | |
447 // Should not have started the float transition yet. | |
448 EXPECT_TRUE(controller->HasActiveAnimation()); | |
449 EXPECT_EQ(0.f, dummy.opacity()); | |
450 // The float animation should have started at time 1 and should be done. | |
451 controller->Animate(2.0); | |
452 controller->UpdateState(events.get()); | |
453 EXPECT_EQ(1.f, dummy.opacity()); | |
454 EXPECT_FALSE(controller->HasActiveAnimation()); | |
455 } | |
456 | |
457 // Tests scheduling two animations to run together with different lengths and | |
458 // another animation queued to start when the shorter animation finishes (should | |
459 // wait for both to finish). | |
460 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) { | |
461 scoped_ptr<AnimationEventsVector> events( | |
462 make_scoped_ptr(new AnimationEventsVector)); | |
463 FakeLayerAnimationValueObserver dummy; | |
464 scoped_refptr<LayerAnimationController> controller( | |
465 LayerAnimationController::Create(0)); | |
466 controller->AddObserver(&dummy); | |
467 | |
468 controller->AddAnimation(CreateAnimation( | |
469 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(), | |
470 1, | |
471 Animation::Transform)); | |
472 controller->AddAnimation(CreateAnimation( | |
473 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
474 1, | |
475 Animation::Opacity)); | |
476 controller->AddAnimation(CreateAnimation( | |
477 scoped_ptr<AnimationCurve>( | |
478 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(), | |
479 2, | |
480 Animation::Opacity)); | |
481 | |
482 // Animations with id 1 should both start now. | |
483 controller->Animate(0.0); | |
484 controller->UpdateState(events.get()); | |
485 EXPECT_TRUE(controller->HasActiveAnimation()); | |
486 EXPECT_EQ(0.f, dummy.opacity()); | |
487 // The opacity animation should have finished at time 1, but the group | |
488 // of animations with id 1 don't finish until time 2 because of the length | |
489 // of the transform animation. | |
490 controller->Animate(2.0); | |
491 controller->UpdateState(events.get()); | |
492 // Should not have started the float transition yet. | |
493 EXPECT_TRUE(controller->HasActiveAnimation()); | |
494 EXPECT_EQ(1.f, dummy.opacity()); | |
495 | |
496 // The second opacity animation should start at time 2 and should be done by | |
497 // time 3. | |
498 controller->Animate(3.0); | |
499 controller->UpdateState(events.get()); | |
500 EXPECT_EQ(0.5f, dummy.opacity()); | |
501 EXPECT_FALSE(controller->HasActiveAnimation()); | |
502 } | |
503 | |
504 // Tests scheduling an animation to start in the future. | |
505 TEST(LayerAnimationControllerTest, ScheduleAnimation) { | |
506 scoped_ptr<AnimationEventsVector> events( | |
507 make_scoped_ptr(new AnimationEventsVector)); | |
508 FakeLayerAnimationValueObserver dummy; | |
509 scoped_refptr<LayerAnimationController> controller( | |
510 LayerAnimationController::Create(0)); | |
511 controller->AddObserver(&dummy); | |
512 | |
513 scoped_ptr<Animation> to_add(CreateAnimation( | |
514 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
515 1, | |
516 Animation::Opacity)); | |
517 to_add->SetRunState(Animation::WaitingForStartTime, 0); | |
518 to_add->set_start_time(1.f); | |
519 controller->AddAnimation(to_add.Pass()); | |
520 | |
521 controller->Animate(0.0); | |
522 controller->UpdateState(events.get()); | |
523 EXPECT_TRUE(controller->HasActiveAnimation()); | |
524 EXPECT_EQ(0.f, dummy.opacity()); | |
525 controller->Animate(1.0); | |
526 controller->UpdateState(events.get()); | |
527 EXPECT_TRUE(controller->HasActiveAnimation()); | |
528 EXPECT_EQ(0.f, dummy.opacity()); | |
529 controller->Animate(2.0); | |
530 controller->UpdateState(events.get()); | |
531 EXPECT_EQ(1.f, dummy.opacity()); | |
532 EXPECT_FALSE(controller->HasActiveAnimation()); | |
533 } | |
534 | |
535 // Tests scheduling an animation to start in the future that's interrupting a | |
536 // running animation. | |
537 TEST(LayerAnimationControllerTest, | |
538 ScheduledAnimationInterruptsRunningAnimation) { | |
539 scoped_ptr<AnimationEventsVector> events( | |
540 make_scoped_ptr(new AnimationEventsVector)); | |
541 FakeLayerAnimationValueObserver dummy; | |
542 scoped_refptr<LayerAnimationController> controller( | |
543 LayerAnimationController::Create(0)); | |
544 controller->AddObserver(&dummy); | |
545 | |
546 controller->AddAnimation(CreateAnimation( | |
547 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), | |
548 1, | |
549 Animation::Opacity)); | |
550 | |
551 scoped_ptr<Animation> to_add(CreateAnimation( | |
552 scoped_ptr<AnimationCurve>( | |
553 new FakeFloatTransition(1.0, 0.5f, 0.f)).Pass(), | |
554 2, | |
555 Animation::Opacity)); | |
556 to_add->SetRunState(Animation::WaitingForStartTime, 0); | |
557 to_add->set_start_time(1.f); | |
558 controller->AddAnimation(to_add.Pass()); | |
559 | |
560 // First 2s opacity transition should start immediately. | |
561 controller->Animate(0.0); | |
562 controller->UpdateState(events.get()); | |
563 EXPECT_TRUE(controller->HasActiveAnimation()); | |
564 EXPECT_EQ(0.f, dummy.opacity()); | |
565 controller->Animate(0.5); | |
566 controller->UpdateState(events.get()); | |
567 EXPECT_TRUE(controller->HasActiveAnimation()); | |
568 EXPECT_EQ(0.25f, dummy.opacity()); | |
569 controller->Animate(1.0); | |
570 controller->UpdateState(events.get()); | |
571 EXPECT_TRUE(controller->HasActiveAnimation()); | |
572 EXPECT_EQ(0.5f, dummy.opacity()); | |
573 controller->Animate(2.0); | |
574 controller->UpdateState(events.get()); | |
575 EXPECT_EQ(0.f, dummy.opacity()); | |
576 EXPECT_FALSE(controller->HasActiveAnimation()); | |
577 } | |
578 | |
579 // Tests scheduling an animation to start in the future that interrupts a | |
580 // running animation and there is yet another animation queued to start later. | |
581 TEST(LayerAnimationControllerTest, | |
582 ScheduledAnimationInterruptsRunningAnimationWithAnimInQueue) { | |
583 scoped_ptr<AnimationEventsVector> events( | |
584 make_scoped_ptr(new AnimationEventsVector)); | |
585 FakeLayerAnimationValueObserver dummy; | |
586 scoped_refptr<LayerAnimationController> controller( | |
587 LayerAnimationController::Create(0)); | |
588 controller->AddObserver(&dummy); | |
589 | |
590 controller->AddAnimation(CreateAnimation( | |
591 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), | |
592 1, | |
593 Animation::Opacity)); | |
594 | |
595 scoped_ptr<Animation> to_add(CreateAnimation( | |
596 scoped_ptr<AnimationCurve>( | |
597 new FakeFloatTransition(2.0, 0.5f, 0.f)).Pass(), | |
598 2, | |
599 Animation::Opacity)); | |
600 to_add->SetRunState(Animation::WaitingForStartTime, 0); | |
601 to_add->set_start_time(1.f); | |
602 controller->AddAnimation(to_add.Pass()); | |
603 | |
604 controller->AddAnimation(CreateAnimation( | |
605 scoped_ptr<AnimationCurve>( | |
606 new FakeFloatTransition(1.0, 0.f, 0.75f)).Pass(), | |
607 3, | |
608 Animation::Opacity)); | |
609 | |
610 // First 2s opacity transition should start immediately. | |
611 controller->Animate(0.0); | |
612 controller->UpdateState(events.get()); | |
613 EXPECT_TRUE(controller->HasActiveAnimation()); | |
614 EXPECT_EQ(0.f, dummy.opacity()); | |
615 controller->Animate(0.5); | |
616 controller->UpdateState(events.get()); | |
617 EXPECT_TRUE(controller->HasActiveAnimation()); | |
618 EXPECT_EQ(0.25f, dummy.opacity()); | |
619 EXPECT_TRUE(controller->HasActiveAnimation()); | |
620 controller->Animate(1.0); | |
621 controller->UpdateState(events.get()); | |
622 EXPECT_TRUE(controller->HasActiveAnimation()); | |
623 EXPECT_EQ(0.5f, dummy.opacity()); | |
624 controller->Animate(3.0); | |
625 controller->UpdateState(events.get()); | |
626 EXPECT_TRUE(controller->HasActiveAnimation()); | |
627 EXPECT_EQ(0.f, dummy.opacity()); | |
628 controller->Animate(4.0); | |
629 controller->UpdateState(events.get()); | |
630 EXPECT_EQ(0.75f, dummy.opacity()); | |
631 EXPECT_FALSE(controller->HasActiveAnimation()); | |
632 } | |
633 | |
634 // Test that a looping animation loops and for the correct number of iterations. | |
635 TEST(LayerAnimationControllerTest, TrivialLooping) { | |
636 scoped_ptr<AnimationEventsVector> events( | |
637 make_scoped_ptr(new AnimationEventsVector)); | |
638 FakeLayerAnimationValueObserver dummy; | |
639 scoped_refptr<LayerAnimationController> controller( | |
640 LayerAnimationController::Create(0)); | |
641 controller->AddObserver(&dummy); | |
642 | |
643 scoped_ptr<Animation> to_add(CreateAnimation( | |
644 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
645 1, | |
646 Animation::Opacity)); | |
647 to_add->set_iterations(3); | |
648 controller->AddAnimation(to_add.Pass()); | |
649 | |
650 controller->Animate(0.0); | |
651 controller->UpdateState(events.get()); | |
652 EXPECT_TRUE(controller->HasActiveAnimation()); | |
653 EXPECT_EQ(0.f, dummy.opacity()); | |
654 controller->Animate(1.25); | |
655 controller->UpdateState(events.get()); | |
656 EXPECT_TRUE(controller->HasActiveAnimation()); | |
657 EXPECT_EQ(0.25f, dummy.opacity()); | |
658 controller->Animate(1.75); | |
659 controller->UpdateState(events.get()); | |
660 EXPECT_TRUE(controller->HasActiveAnimation()); | |
661 EXPECT_EQ(0.75f, dummy.opacity()); | |
662 controller->Animate(2.25); | |
663 controller->UpdateState(events.get()); | |
664 EXPECT_TRUE(controller->HasActiveAnimation()); | |
665 EXPECT_EQ(0.25f, dummy.opacity()); | |
666 controller->Animate(2.75); | |
667 controller->UpdateState(events.get()); | |
668 EXPECT_TRUE(controller->HasActiveAnimation()); | |
669 EXPECT_EQ(0.75f, dummy.opacity()); | |
670 controller->Animate(3.0); | |
671 controller->UpdateState(events.get()); | |
672 EXPECT_FALSE(controller->HasActiveAnimation()); | |
673 EXPECT_EQ(1.f, dummy.opacity()); | |
674 | |
675 // Just be extra sure. | |
676 controller->Animate(4.0); | |
677 controller->UpdateState(events.get()); | |
678 EXPECT_EQ(1.f, dummy.opacity()); | |
679 } | |
680 | |
681 // Test that an infinitely looping animation does indeed go until aborted. | |
682 TEST(LayerAnimationControllerTest, InfiniteLooping) { | |
683 scoped_ptr<AnimationEventsVector> events( | |
684 make_scoped_ptr(new AnimationEventsVector)); | |
685 FakeLayerAnimationValueObserver dummy; | |
686 scoped_refptr<LayerAnimationController> controller( | |
687 LayerAnimationController::Create(0)); | |
688 controller->AddObserver(&dummy); | |
689 | |
690 const int id = 1; | |
691 scoped_ptr<Animation> to_add(CreateAnimation( | |
692 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
693 id, | |
694 Animation::Opacity)); | |
695 to_add->set_iterations(-1); | |
696 controller->AddAnimation(to_add.Pass()); | |
697 | |
698 controller->Animate(0.0); | |
699 controller->UpdateState(events.get()); | |
700 EXPECT_TRUE(controller->HasActiveAnimation()); | |
701 EXPECT_EQ(0.f, dummy.opacity()); | |
702 controller->Animate(1.25); | |
703 controller->UpdateState(events.get()); | |
704 EXPECT_TRUE(controller->HasActiveAnimation()); | |
705 EXPECT_EQ(0.25f, dummy.opacity()); | |
706 controller->Animate(1.75); | |
707 controller->UpdateState(events.get()); | |
708 EXPECT_TRUE(controller->HasActiveAnimation()); | |
709 EXPECT_EQ(0.75f, dummy.opacity()); | |
710 | |
711 controller->Animate(1073741824.25); | |
712 controller->UpdateState(events.get()); | |
713 EXPECT_TRUE(controller->HasActiveAnimation()); | |
714 EXPECT_EQ(0.25f, dummy.opacity()); | |
715 controller->Animate(1073741824.75); | |
716 controller->UpdateState(events.get()); | |
717 EXPECT_TRUE(controller->HasActiveAnimation()); | |
718 EXPECT_EQ(0.75f, dummy.opacity()); | |
719 | |
720 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); | |
721 controller->GetAnimation(id, Animation::Opacity)->SetRunState( | |
722 Animation::Aborted, 0.75); | |
723 EXPECT_FALSE(controller->HasActiveAnimation()); | |
724 EXPECT_EQ(0.75f, dummy.opacity()); | |
725 } | |
726 | |
727 // Test that pausing and resuming work as expected. | |
728 TEST(LayerAnimationControllerTest, PauseResume) { | |
729 scoped_ptr<AnimationEventsVector> events( | |
730 make_scoped_ptr(new AnimationEventsVector)); | |
731 FakeLayerAnimationValueObserver dummy; | |
732 scoped_refptr<LayerAnimationController> controller( | |
733 LayerAnimationController::Create(0)); | |
734 controller->AddObserver(&dummy); | |
735 | |
736 const int id = 1; | |
737 controller->AddAnimation(CreateAnimation( | |
738 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
739 id, | |
740 Animation::Opacity)); | |
741 | |
742 controller->Animate(0.0); | |
743 controller->UpdateState(events.get()); | |
744 EXPECT_TRUE(controller->HasActiveAnimation()); | |
745 EXPECT_EQ(0.f, dummy.opacity()); | |
746 controller->Animate(0.5); | |
747 controller->UpdateState(events.get()); | |
748 EXPECT_TRUE(controller->HasActiveAnimation()); | |
749 EXPECT_EQ(0.5f, dummy.opacity()); | |
750 | |
751 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); | |
752 controller->GetAnimation(id, Animation::Opacity)->SetRunState( | |
753 Animation::Paused, 0.5); | |
754 | |
755 controller->Animate(1024); | |
756 controller->UpdateState(events.get()); | |
757 EXPECT_TRUE(controller->HasActiveAnimation()); | |
758 EXPECT_EQ(0.5f, dummy.opacity()); | |
759 | |
760 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); | |
761 controller->GetAnimation(id, Animation::Opacity)->SetRunState( | |
762 Animation::Running, 1024); | |
763 | |
764 controller->Animate(1024.25); | |
765 controller->UpdateState(events.get()); | |
766 EXPECT_TRUE(controller->HasActiveAnimation()); | |
767 EXPECT_EQ(0.75f, dummy.opacity()); | |
768 controller->Animate(1024.5); | |
769 controller->UpdateState(events.get()); | |
770 EXPECT_FALSE(controller->HasActiveAnimation()); | |
771 EXPECT_EQ(1.f, dummy.opacity()); | |
772 } | |
773 | |
774 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) { | |
775 scoped_ptr<AnimationEventsVector> events( | |
776 make_scoped_ptr(new AnimationEventsVector)); | |
777 FakeLayerAnimationValueObserver dummy; | |
778 scoped_refptr<LayerAnimationController> controller( | |
779 LayerAnimationController::Create(0)); | |
780 controller->AddObserver(&dummy); | |
781 | |
782 const int id = 1; | |
783 controller->AddAnimation(CreateAnimation( | |
784 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), | |
785 id, | |
786 Animation::Transform)); | |
787 controller->AddAnimation(CreateAnimation( | |
788 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), | |
789 id, | |
790 Animation::Opacity)); | |
791 controller->AddAnimation(CreateAnimation( | |
792 scoped_ptr<AnimationCurve>( | |
793 new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(), | |
794 2, | |
795 Animation::Opacity)); | |
796 | |
797 controller->Animate(0.0); | |
798 controller->UpdateState(events.get()); | |
799 EXPECT_TRUE(controller->HasActiveAnimation()); | |
800 EXPECT_EQ(0.f, dummy.opacity()); | |
801 controller->Animate(1.0); | |
802 controller->UpdateState(events.get()); | |
803 EXPECT_TRUE(controller->HasActiveAnimation()); | |
804 EXPECT_EQ(0.5f, dummy.opacity()); | |
805 | |
806 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity)); | |
807 controller->GetAnimation(id, Animation::Opacity)->SetRunState( | |
808 Animation::Aborted, 1); | |
809 controller->Animate(1.0); | |
810 controller->UpdateState(events.get()); | |
811 EXPECT_TRUE(controller->HasActiveAnimation()); | |
812 EXPECT_EQ(1.f, dummy.opacity()); | |
813 controller->Animate(2.0); | |
814 controller->UpdateState(events.get()); | |
815 EXPECT_TRUE(!controller->HasActiveAnimation()); | |
816 EXPECT_EQ(0.75f, dummy.opacity()); | |
817 } | |
818 | |
819 TEST(LayerAnimationControllerTest, ForceSyncWhenSynchronizedStartTimeNeeded) { | |
820 FakeLayerAnimationValueObserver dummy_impl; | |
821 scoped_refptr<LayerAnimationController> controller_impl( | |
822 LayerAnimationController::Create(0)); | |
823 controller_impl->AddObserver(&dummy_impl); | |
824 scoped_ptr<AnimationEventsVector> events( | |
825 make_scoped_ptr(new AnimationEventsVector)); | |
826 FakeLayerAnimationValueObserver dummy; | |
827 scoped_refptr<LayerAnimationController> controller( | |
828 LayerAnimationController::Create(0)); | |
829 controller->AddObserver(&dummy); | |
830 | |
831 scoped_ptr<Animation> to_add(CreateAnimation( | |
832 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(), | |
833 0, | |
834 Animation::Opacity)); | |
835 to_add->set_needs_synchronized_start_time(true); | |
836 controller->AddAnimation(to_add.Pass()); | |
837 | |
838 controller->Animate(0.0); | |
839 controller->UpdateState(events.get()); | |
840 EXPECT_TRUE(controller->HasActiveAnimation()); | |
841 Animation* active_animation = controller->GetAnimation(0, Animation::Opacity); | |
842 EXPECT_TRUE(active_animation); | |
843 EXPECT_TRUE(active_animation->needs_synchronized_start_time()); | |
844 | |
845 controller->set_force_sync(); | |
846 | |
847 controller->PushAnimationUpdatesTo(controller_impl.get()); | |
848 | |
849 active_animation = controller_impl->GetAnimation(0, Animation::Opacity); | |
850 EXPECT_TRUE(active_animation); | |
851 EXPECT_EQ(Animation::WaitingForTargetAvailability, | |
852 active_animation->run_state()); | |
853 } | |
854 | |
855 // Tests that skipping a call to updateState works as expected. | |
856 TEST(LayerAnimationControllerTest, SkipUpdateState) { | |
857 scoped_ptr<AnimationEventsVector> events( | |
858 make_scoped_ptr(new AnimationEventsVector)); | |
859 FakeLayerAnimationValueObserver dummy; | |
860 scoped_refptr<LayerAnimationController> controller( | |
861 LayerAnimationController::Create(0)); | |
862 controller->AddObserver(&dummy); | |
863 | |
864 controller->AddAnimation(CreateAnimation( | |
865 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), | |
866 1, | |
867 Animation::Transform)); | |
868 | |
869 controller->Animate(0.0); | |
870 controller->UpdateState(events.get()); | |
871 | |
872 controller->AddAnimation(CreateAnimation( | |
873 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(), | |
874 2, | |
875 Animation::Opacity)); | |
876 | |
877 // Animate but don't updateState. | |
878 controller->Animate(1.0); | |
879 | |
880 controller->Animate(2.0); | |
881 events.reset(new AnimationEventsVector); | |
882 controller->UpdateState(events.get()); | |
883 | |
884 // Should have one Started event and one Finished event. | |
885 EXPECT_EQ(2, events->size()); | |
886 EXPECT_NE((*events)[0].type, (*events)[1].type); | |
887 | |
888 // The float transition should still be at its starting point. | |
889 EXPECT_TRUE(controller->HasActiveAnimation()); | |
890 EXPECT_EQ(0.f, dummy.opacity()); | |
891 | |
892 controller->Animate(3.0); | |
893 controller->UpdateState(events.get()); | |
894 | |
895 // The float tranisition should now be done. | |
896 EXPECT_EQ(1.f, dummy.opacity()); | |
897 EXPECT_FALSE(controller->HasActiveAnimation()); | |
898 } | |
899 | |
900 } // namespace | |
901 } // namespace cc | |
OLD | NEW |