Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / cc / animation / layer_animation_controller_unittest.cc
blob5ef57ad82cfa37562dc4d768e057259240554b13
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.
5 #include "cc/animation/layer_animation_controller.h"
7 #include "cc/animation/animation.h"
8 #include "cc/animation/animation_curve.h"
9 #include "cc/animation/keyframed_animation_curve.h"
10 #include "cc/animation/transform_operations.h"
11 #include "cc/test/animation_test_common.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/gfx/transform.h"
16 namespace cc {
17 namespace {
19 void ExpectTranslateX(double translate_x, const gfx::Transform& matrix) {
20 EXPECT_FLOAT_EQ(translate_x, matrix.matrix().getDouble(0, 3)); }
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);
28 TEST(LayerAnimationControllerTest, SyncNewAnimation) {
29 FakeLayerAnimationValueObserver dummy_impl;
30 scoped_refptr<LayerAnimationController> controller_impl(
31 LayerAnimationController::Create(0));
32 controller_impl->AddValueObserver(&dummy_impl);
33 FakeLayerAnimationValueObserver dummy;
34 scoped_refptr<LayerAnimationController> controller(
35 LayerAnimationController::Create(0));
36 controller->AddValueObserver(&dummy);
38 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity));
40 AddOpacityTransitionToController(controller, 1, 0, 1, false);
42 controller->PushAnimationUpdatesTo(controller_impl.get());
44 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity));
45 EXPECT_EQ(Animation::WaitingForTargetAvailability,
46 controller_impl->GetAnimation(0, Animation::Opacity)->run_state());
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->AddValueObserver(&dummy_impl);
56 FakeLayerAnimationValueObserver dummy;
57 scoped_refptr<LayerAnimationController> controller(
58 LayerAnimationController::Create(0));
59 controller->AddValueObserver(&dummy);
61 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity));
63 AddOpacityTransitionToController(controller, 1, 0, 1, false);
65 controller->PushAnimationUpdatesTo(controller_impl.get());
67 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity));
68 EXPECT_EQ(Animation::WaitingForTargetAvailability,
69 controller_impl->GetAnimation(0, Animation::Opacity)->run_state());
71 AnimationEventsVector events;
72 controller_impl->Animate(1.0);
73 controller_impl->UpdateState(true, &events);
75 // Synchronize the start times.
76 EXPECT_EQ(1u, events.size());
77 controller->NotifyAnimationStarted(events[0], 0.0);
78 EXPECT_EQ(controller->GetAnimation(0, Animation::Opacity)->start_time(),
79 controller_impl->GetAnimation(0, Animation::Opacity)->start_time());
81 // Start the animation on the main thread. Should not affect the start time.
82 controller->Animate(1.5);
83 controller->UpdateState(true, NULL);
84 EXPECT_EQ(controller->GetAnimation(0, Animation::Opacity)->start_time(),
85 controller_impl->GetAnimation(0, Animation::Opacity)->start_time());
88 TEST(LayerAnimationControllerTest, SyncPauseAndResume) {
89 FakeLayerAnimationValueObserver dummy_impl;
90 scoped_refptr<LayerAnimationController> controller_impl(
91 LayerAnimationController::Create(0));
92 controller_impl->AddValueObserver(&dummy_impl);
93 FakeLayerAnimationValueObserver dummy;
94 scoped_refptr<LayerAnimationController> controller(
95 LayerAnimationController::Create(0));
96 controller->AddValueObserver(&dummy);
98 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity));
100 AddOpacityTransitionToController(controller, 1, 0, 1, false);
102 controller->PushAnimationUpdatesTo(controller_impl.get());
104 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity));
105 EXPECT_EQ(Animation::WaitingForTargetAvailability,
106 controller_impl->GetAnimation(0, Animation::Opacity)->run_state());
108 // Start the animations on each controller.
109 AnimationEventsVector events;
110 controller_impl->Animate(0.0);
111 controller_impl->UpdateState(true, &events);
112 controller->Animate(0.0);
113 controller->UpdateState(true, 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());
119 // Pause the main-thread animation.
120 controller->SuspendAnimations(1.0);
121 EXPECT_EQ(Animation::Paused,
122 controller->GetAnimation(0, Animation::Opacity)->run_state());
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());
129 // Resume the main-thread animation.
130 controller->ResumeAnimations(2.0);
131 EXPECT_EQ(Animation::Running,
132 controller->GetAnimation(0, Animation::Opacity)->run_state());
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());
140 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
141 FakeLayerAnimationValueObserver dummy_impl;
142 scoped_refptr<LayerAnimationController> controller_impl(
143 LayerAnimationController::Create(0));
144 controller_impl->AddValueObserver(&dummy_impl);
145 FakeLayerAnimationValueObserver dummy;
146 scoped_refptr<LayerAnimationController> controller(
147 LayerAnimationController::Create(0));
148 controller->AddValueObserver(&dummy);
150 EXPECT_FALSE(controller_impl->GetAnimation(0, Animation::Opacity));
152 int animation_id =
153 AddOpacityTransitionToController(controller, 1, 0, 1, false);
155 controller->PushAnimationUpdatesTo(controller_impl.get());
157 EXPECT_TRUE(controller_impl->GetAnimation(0, Animation::Opacity));
158 EXPECT_EQ(Animation::WaitingForTargetAvailability,
159 controller_impl->GetAnimation(0, Animation::Opacity)->run_state());
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->NotifyAnimationStarted(animation_started_event, 0.0);
166 // Force animation to complete on impl thread.
167 controller_impl->RemoveAnimation(animation_id);
169 EXPECT_FALSE(controller_impl->GetAnimation(animation_id, Animation::Opacity));
171 controller->PushAnimationUpdatesTo(controller_impl.get());
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));
178 // Ensure that a finished animation is eventually deleted by both the
179 // main-thread and the impl-thread controllers.
180 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
181 FakeLayerAnimationValueObserver dummy;
182 FakeLayerAnimationValueObserver dummy_impl;
183 scoped_ptr<AnimationEventsVector> events(
184 make_scoped_ptr(new AnimationEventsVector));
185 scoped_refptr<LayerAnimationController> controller(
186 LayerAnimationController::Create(0));
187 scoped_refptr<LayerAnimationController> controller_impl(
188 LayerAnimationController::Create(0));
189 controller->AddValueObserver(&dummy);
190 controller_impl->AddValueObserver(&dummy_impl);
192 AddOpacityTransitionToController(controller, 1.0, 0.0f, 1.0f, false);
193 controller->Animate(0.0);
194 controller->UpdateState(true, NULL);
195 controller->PushAnimationUpdatesTo(controller_impl.get());
197 controller_impl->Animate(0.5);
198 controller_impl->UpdateState(true, events.get());
200 // There should be a Started event for the animation.
201 EXPECT_EQ(1u, events->size());
202 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
203 controller->NotifyAnimationStarted((*events)[0], 0.0);
205 controller->Animate(1.0);
206 controller->UpdateState(true, NULL);
208 events.reset(new AnimationEventsVector);
209 controller_impl->Animate(2.0);
210 controller_impl->UpdateState(true, events.get());
212 // There should be a Finished event for the animation.
213 EXPECT_EQ(1u, events->size());
214 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
216 // Neither controller should have deleted the animation yet.
217 EXPECT_TRUE(controller->GetAnimation(Animation::Opacity));
218 EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity));
220 controller->NotifyAnimationFinished((*events)[0], 0.0);
222 controller->Animate(3.0);
223 controller->UpdateState(true, NULL);
225 controller->PushAnimationUpdatesTo(controller_impl.get());
227 // Both controllers should now have deleted the animation.
228 EXPECT_FALSE(controller->has_any_animation());
229 EXPECT_FALSE(controller_impl->has_any_animation());
232 TEST(LayerAnimationControllerTest, TransferAnimationsTo) {
233 FakeLayerAnimationValueObserver dummy;
234 scoped_refptr<LayerAnimationController> controller(
235 LayerAnimationController::Create(0));
236 scoped_refptr<LayerAnimationController> other_controller(
237 LayerAnimationController::Create(1));
238 controller->AddValueObserver(&dummy);
240 int opacity_animation_id =
241 AddOpacityTransitionToController(controller, 1.0, 0.0f, 1.0f, false);
243 int transform_animation_id =
244 AddAnimatedTransformToController(controller, 1.0, 10, 10);
246 controller->Animate(1.0);
248 // Both animations should now be Starting.
249 EXPECT_EQ(Animation::Starting,
250 controller->GetAnimation(Animation::Opacity)->run_state());
251 EXPECT_EQ(Animation::Starting,
252 controller->GetAnimation(Animation::Transform)->run_state());
254 controller->TransferAnimationsTo(other_controller);
256 // Ensure both animations have been transfered.
257 EXPECT_FALSE(controller->has_any_animation());
258 EXPECT_EQ(other_controller->GetAnimation(Animation::Opacity)->id(),
259 opacity_animation_id);
260 EXPECT_EQ(other_controller->GetAnimation(Animation::Transform)->id(),
261 transform_animation_id);
263 // Ensure that the run state of the transferred animations has been
264 // preserved.
265 EXPECT_EQ(Animation::Starting,
266 other_controller->GetAnimation(Animation::Opacity)->run_state());
267 EXPECT_EQ(Animation::Starting,
268 other_controller->GetAnimation(Animation::Transform)->run_state());
271 // Tests that transitioning opacity from 0 to 1 works as expected.
273 static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
274 const AnimationEventsVector* events) {
275 const AnimationEvent* event = 0;
276 for (size_t i = 0; i < events->size(); ++i)
277 if ((*events)[i].type == AnimationEvent::PropertyUpdate)
278 event = &(*events)[i];
280 return event;
283 TEST(LayerAnimationControllerTest, TrivialTransition) {
284 scoped_ptr<AnimationEventsVector> events(
285 make_scoped_ptr(new AnimationEventsVector));
286 FakeLayerAnimationValueObserver dummy;
287 scoped_refptr<LayerAnimationController> controller(
288 LayerAnimationController::Create(0));
289 controller->AddValueObserver(&dummy);
291 scoped_ptr<Animation> to_add(CreateAnimation(
292 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
294 Animation::Opacity));
296 controller->AddAnimation(to_add.Pass());
297 controller->Animate(0.0);
298 controller->UpdateState(true, events.get());
299 EXPECT_TRUE(controller->HasActiveAnimation());
300 EXPECT_EQ(0.f, dummy.opacity());
301 // A non-impl-only animation should not generate property updates.
302 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
303 EXPECT_FALSE(event);
304 controller->Animate(1.0);
305 controller->UpdateState(true, events.get());
306 EXPECT_EQ(1.f, dummy.opacity());
307 EXPECT_FALSE(controller->HasActiveAnimation());
308 event = GetMostRecentPropertyUpdateEvent(events.get());
309 EXPECT_FALSE(event);
312 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
313 scoped_ptr<AnimationEventsVector> events(
314 make_scoped_ptr(new AnimationEventsVector));
315 FakeLayerAnimationValueObserver dummy_impl;
316 scoped_refptr<LayerAnimationController> controller_impl(
317 LayerAnimationController::Create(0));
318 controller_impl->AddValueObserver(&dummy_impl);
320 scoped_ptr<Animation> to_add(CreateAnimation(
321 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
323 Animation::Opacity));
324 to_add->set_is_impl_only(true);
326 controller_impl->AddAnimation(to_add.Pass());
327 controller_impl->Animate(0.0);
328 controller_impl->UpdateState(true, events.get());
329 EXPECT_TRUE(controller_impl->HasActiveAnimation());
330 EXPECT_EQ(0.f, dummy_impl.opacity());
331 EXPECT_EQ(2u, events->size());
332 const AnimationEvent* start_opacity_event =
333 GetMostRecentPropertyUpdateEvent(events.get());
334 EXPECT_EQ(0.f, start_opacity_event->opacity);
336 controller_impl->Animate(1.0);
337 controller_impl->UpdateState(true, events.get());
338 EXPECT_EQ(1.f, dummy_impl.opacity());
339 EXPECT_FALSE(controller_impl->HasActiveAnimation());
340 EXPECT_EQ(4u, events->size());
341 const AnimationEvent* end_opacity_event =
342 GetMostRecentPropertyUpdateEvent(events.get());
343 EXPECT_EQ(1.f, end_opacity_event->opacity);
346 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
347 scoped_ptr<AnimationEventsVector> events(
348 make_scoped_ptr(new AnimationEventsVector));
349 FakeLayerAnimationValueObserver dummy_impl;
350 scoped_refptr<LayerAnimationController> controller_impl(
351 LayerAnimationController::Create(0));
352 controller_impl->AddValueObserver(&dummy_impl);
354 // Choose different values for x and y to avoid coincidental values in the
355 // observed transforms.
356 const float delta_x = 3;
357 const float delta_y = 4;
359 scoped_ptr<KeyframedTransformAnimationCurve> curve(
360 KeyframedTransformAnimationCurve::Create());
362 // Create simple Transform animation.
363 TransformOperations operations;
364 curve->AddKeyframe(TransformKeyframe::Create(
365 0, operations, scoped_ptr<cc::TimingFunction>()));
366 operations.AppendTranslate(delta_x, delta_y, 0);
367 curve->AddKeyframe(TransformKeyframe::Create(
368 1, operations, scoped_ptr<cc::TimingFunction>()));
370 scoped_ptr<Animation> animation(Animation::Create(
371 curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform));
372 animation->set_is_impl_only(true);
373 controller_impl->AddAnimation(animation.Pass());
375 // Run animation.
376 controller_impl->Animate(0.0);
377 controller_impl->UpdateState(true, events.get());
378 EXPECT_TRUE(controller_impl->HasActiveAnimation());
379 EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
380 EXPECT_EQ(2u, events->size());
381 const AnimationEvent* start_transform_event =
382 GetMostRecentPropertyUpdateEvent(events.get());
383 ASSERT_TRUE(start_transform_event);
384 EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
386 gfx::Transform expected_transform;
387 expected_transform.Translate(delta_x, delta_y);
389 controller_impl->Animate(1.0);
390 controller_impl->UpdateState(true, events.get());
391 EXPECT_EQ(expected_transform, dummy_impl.transform());
392 EXPECT_FALSE(controller_impl->HasActiveAnimation());
393 EXPECT_EQ(4u, events->size());
394 const AnimationEvent* end_transform_event =
395 GetMostRecentPropertyUpdateEvent(events.get());
396 EXPECT_EQ(expected_transform, end_transform_event->transform);
399 // Tests animations that are waiting for a synchronized start time do not
400 // finish.
401 TEST(LayerAnimationControllerTest,
402 AnimationsWaitingForStartTimeDoNotFinishIfTheyWaitLongerToStartThanTheirDuration) {
403 scoped_ptr<AnimationEventsVector> events(
404 make_scoped_ptr(new AnimationEventsVector));
405 FakeLayerAnimationValueObserver dummy;
406 scoped_refptr<LayerAnimationController> controller(
407 LayerAnimationController::Create(0));
408 controller->AddValueObserver(&dummy);
410 scoped_ptr<Animation> to_add(CreateAnimation(
411 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
413 Animation::Opacity));
414 to_add->set_needs_synchronized_start_time(true);
416 // We should pause at the first keyframe indefinitely waiting for that
417 // animation to start.
418 controller->AddAnimation(to_add.Pass());
419 controller->Animate(0.0);
420 controller->UpdateState(true, events.get());
421 EXPECT_TRUE(controller->HasActiveAnimation());
422 EXPECT_EQ(0.f, dummy.opacity());
423 controller->Animate(1.0);
424 controller->UpdateState(true, events.get());
425 EXPECT_TRUE(controller->HasActiveAnimation());
426 EXPECT_EQ(0.f, dummy.opacity());
427 controller->Animate(2.0);
428 controller->UpdateState(true, events.get());
429 EXPECT_TRUE(controller->HasActiveAnimation());
430 EXPECT_EQ(0.f, dummy.opacity());
432 // Send the synchronized start time.
433 controller->NotifyAnimationStarted(
434 AnimationEvent(AnimationEvent::Started, 0, 1, Animation::Opacity, 2),
435 0.0);
436 controller->Animate(5.0);
437 controller->UpdateState(true, events.get());
438 EXPECT_EQ(1.f, dummy.opacity());
439 EXPECT_FALSE(controller->HasActiveAnimation());
442 // Tests that two queued animations affecting the same property run in sequence.
443 TEST(LayerAnimationControllerTest, TrivialQueuing) {
444 scoped_ptr<AnimationEventsVector> events(
445 make_scoped_ptr(new AnimationEventsVector));
446 FakeLayerAnimationValueObserver dummy;
447 scoped_refptr<LayerAnimationController> controller(
448 LayerAnimationController::Create(0));
449 controller->AddValueObserver(&dummy);
451 controller->AddAnimation(CreateAnimation(
452 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
454 Animation::Opacity));
455 controller->AddAnimation(CreateAnimation(
456 scoped_ptr<AnimationCurve>(
457 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
459 Animation::Opacity));
461 controller->Animate(0.0);
462 controller->UpdateState(true, events.get());
463 EXPECT_TRUE(controller->HasActiveAnimation());
464 EXPECT_EQ(0.f, dummy.opacity());
465 controller->Animate(1.0);
466 controller->UpdateState(true, events.get());
467 EXPECT_TRUE(controller->HasActiveAnimation());
468 EXPECT_EQ(1.f, dummy.opacity());
469 controller->Animate(2.0);
470 controller->UpdateState(true, events.get());
471 EXPECT_EQ(0.5f, dummy.opacity());
472 EXPECT_FALSE(controller->HasActiveAnimation());
475 // Tests interrupting a transition with another transition.
476 TEST(LayerAnimationControllerTest, Interrupt) {
477 scoped_ptr<AnimationEventsVector> events(
478 make_scoped_ptr(new AnimationEventsVector));
479 FakeLayerAnimationValueObserver dummy;
480 scoped_refptr<LayerAnimationController> controller(
481 LayerAnimationController::Create(0));
482 controller->AddValueObserver(&dummy);
483 controller->AddAnimation(CreateAnimation(
484 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
486 Animation::Opacity));
487 controller->Animate(0.0);
488 controller->UpdateState(true, events.get());
489 EXPECT_TRUE(controller->HasActiveAnimation());
490 EXPECT_EQ(0.f, dummy.opacity());
492 scoped_ptr<Animation> to_add(CreateAnimation(
493 scoped_ptr<AnimationCurve>(
494 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
496 Animation::Opacity));
497 to_add->SetRunState(Animation::WaitingForNextTick, 0);
498 controller->AddAnimation(to_add.Pass());
500 // Since the animation was in the WaitingForNextTick state, it should start
501 // right in this call to animate.
502 controller->Animate(0.5);
503 controller->UpdateState(true, events.get());
504 EXPECT_TRUE(controller->HasActiveAnimation());
505 EXPECT_EQ(1.f, dummy.opacity());
506 controller->Animate(1.5);
507 controller->UpdateState(true, events.get());
508 EXPECT_EQ(0.5f, dummy.opacity());
509 EXPECT_FALSE(controller->HasActiveAnimation());
512 // Tests scheduling two animations to run together when only one property is
513 // free.
514 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
515 scoped_ptr<AnimationEventsVector> events(
516 make_scoped_ptr(new AnimationEventsVector));
517 FakeLayerAnimationValueObserver dummy;
518 scoped_refptr<LayerAnimationController> controller(
519 LayerAnimationController::Create(0));
520 controller->AddValueObserver(&dummy);
522 controller->AddAnimation(CreateAnimation(
523 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
525 Animation::Transform));
526 controller->AddAnimation(CreateAnimation(
527 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
529 Animation::Transform));
530 controller->AddAnimation(CreateAnimation(
531 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
533 Animation::Opacity));
535 controller->Animate(0.0);
536 controller->UpdateState(true, events.get());
537 EXPECT_EQ(0.f, dummy.opacity());
538 EXPECT_TRUE(controller->HasActiveAnimation());
539 controller->Animate(1.0);
540 controller->UpdateState(true, events.get());
541 // Should not have started the float transition yet.
542 EXPECT_TRUE(controller->HasActiveAnimation());
543 EXPECT_EQ(0.f, dummy.opacity());
544 // The float animation should have started at time 1 and should be done.
545 controller->Animate(2.0);
546 controller->UpdateState(true, events.get());
547 EXPECT_EQ(1.f, dummy.opacity());
548 EXPECT_FALSE(controller->HasActiveAnimation());
551 // Tests scheduling two animations to run together with different lengths and
552 // another animation queued to start when the shorter animation finishes (should
553 // wait for both to finish).
554 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
555 scoped_ptr<AnimationEventsVector> events(
556 make_scoped_ptr(new AnimationEventsVector));
557 FakeLayerAnimationValueObserver dummy;
558 scoped_refptr<LayerAnimationController> controller(
559 LayerAnimationController::Create(0));
560 controller->AddValueObserver(&dummy);
562 controller->AddAnimation(CreateAnimation(
563 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
565 Animation::Transform));
566 controller->AddAnimation(CreateAnimation(
567 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
569 Animation::Opacity));
570 controller->AddAnimation(CreateAnimation(
571 scoped_ptr<AnimationCurve>(
572 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
574 Animation::Opacity));
576 // Animations with id 1 should both start now.
577 controller->Animate(0.0);
578 controller->UpdateState(true, events.get());
579 EXPECT_TRUE(controller->HasActiveAnimation());
580 EXPECT_EQ(0.f, dummy.opacity());
581 // The opacity animation should have finished at time 1, but the group
582 // of animations with id 1 don't finish until time 2 because of the length
583 // of the transform animation.
584 controller->Animate(2.0);
585 controller->UpdateState(true, events.get());
586 // Should not have started the float transition yet.
587 EXPECT_TRUE(controller->HasActiveAnimation());
588 EXPECT_EQ(1.f, dummy.opacity());
590 // The second opacity animation should start at time 2 and should be done by
591 // time 3.
592 controller->Animate(3.0);
593 controller->UpdateState(true, events.get());
594 EXPECT_EQ(0.5f, dummy.opacity());
595 EXPECT_FALSE(controller->HasActiveAnimation());
598 // Tests scheduling an animation to start in the future.
599 TEST(LayerAnimationControllerTest, ScheduleAnimation) {
600 scoped_ptr<AnimationEventsVector> events(
601 make_scoped_ptr(new AnimationEventsVector));
602 FakeLayerAnimationValueObserver dummy;
603 scoped_refptr<LayerAnimationController> controller(
604 LayerAnimationController::Create(0));
605 controller->AddValueObserver(&dummy);
607 scoped_ptr<Animation> to_add(CreateAnimation(
608 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
610 Animation::Opacity));
611 to_add->SetRunState(Animation::WaitingForStartTime, 0);
612 to_add->set_start_time(1.f);
613 controller->AddAnimation(to_add.Pass());
615 controller->Animate(0.0);
616 controller->UpdateState(true, events.get());
617 EXPECT_TRUE(controller->HasActiveAnimation());
618 EXPECT_EQ(0.f, dummy.opacity());
619 controller->Animate(1.0);
620 controller->UpdateState(true, events.get());
621 EXPECT_TRUE(controller->HasActiveAnimation());
622 EXPECT_EQ(0.f, dummy.opacity());
623 controller->Animate(2.0);
624 controller->UpdateState(true, events.get());
625 EXPECT_EQ(1.f, dummy.opacity());
626 EXPECT_FALSE(controller->HasActiveAnimation());
629 // Tests scheduling an animation to start in the future that's interrupting a
630 // running animation.
631 TEST(LayerAnimationControllerTest,
632 ScheduledAnimationInterruptsRunningAnimation) {
633 scoped_ptr<AnimationEventsVector> events(
634 make_scoped_ptr(new AnimationEventsVector));
635 FakeLayerAnimationValueObserver dummy;
636 scoped_refptr<LayerAnimationController> controller(
637 LayerAnimationController::Create(0));
638 controller->AddValueObserver(&dummy);
640 controller->AddAnimation(CreateAnimation(
641 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
643 Animation::Opacity));
645 scoped_ptr<Animation> to_add(CreateAnimation(
646 scoped_ptr<AnimationCurve>(
647 new FakeFloatTransition(1.0, 0.5f, 0.f)).Pass(),
649 Animation::Opacity));
650 to_add->SetRunState(Animation::WaitingForStartTime, 0);
651 to_add->set_start_time(1.f);
652 controller->AddAnimation(to_add.Pass());
654 // First 2s opacity transition should start immediately.
655 controller->Animate(0.0);
656 controller->UpdateState(true, events.get());
657 EXPECT_TRUE(controller->HasActiveAnimation());
658 EXPECT_EQ(0.f, dummy.opacity());
659 controller->Animate(0.5);
660 controller->UpdateState(true, events.get());
661 EXPECT_TRUE(controller->HasActiveAnimation());
662 EXPECT_EQ(0.25f, dummy.opacity());
663 controller->Animate(1.0);
664 controller->UpdateState(true, events.get());
665 EXPECT_TRUE(controller->HasActiveAnimation());
666 EXPECT_EQ(0.5f, dummy.opacity());
667 controller->Animate(2.0);
668 controller->UpdateState(true, events.get());
669 EXPECT_EQ(0.f, dummy.opacity());
670 EXPECT_FALSE(controller->HasActiveAnimation());
673 // Tests scheduling an animation to start in the future that interrupts a
674 // running animation and there is yet another animation queued to start later.
675 TEST(LayerAnimationControllerTest,
676 ScheduledAnimationInterruptsRunningAnimationWithAnimInQueue) {
677 scoped_ptr<AnimationEventsVector> events(
678 make_scoped_ptr(new AnimationEventsVector));
679 FakeLayerAnimationValueObserver dummy;
680 scoped_refptr<LayerAnimationController> controller(
681 LayerAnimationController::Create(0));
682 controller->AddValueObserver(&dummy);
684 controller->AddAnimation(CreateAnimation(
685 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
687 Animation::Opacity));
689 scoped_ptr<Animation> to_add(CreateAnimation(
690 scoped_ptr<AnimationCurve>(
691 new FakeFloatTransition(2.0, 0.5f, 0.f)).Pass(),
693 Animation::Opacity));
694 to_add->SetRunState(Animation::WaitingForStartTime, 0);
695 to_add->set_start_time(1.f);
696 controller->AddAnimation(to_add.Pass());
698 controller->AddAnimation(CreateAnimation(
699 scoped_ptr<AnimationCurve>(
700 new FakeFloatTransition(1.0, 0.f, 0.75f)).Pass(),
702 Animation::Opacity));
704 // First 2s opacity transition should start immediately.
705 controller->Animate(0.0);
706 controller->UpdateState(true, events.get());
707 EXPECT_TRUE(controller->HasActiveAnimation());
708 EXPECT_EQ(0.f, dummy.opacity());
709 controller->Animate(0.5);
710 controller->UpdateState(true, events.get());
711 EXPECT_TRUE(controller->HasActiveAnimation());
712 EXPECT_EQ(0.25f, dummy.opacity());
713 EXPECT_TRUE(controller->HasActiveAnimation());
714 controller->Animate(1.0);
715 controller->UpdateState(true, events.get());
716 EXPECT_TRUE(controller->HasActiveAnimation());
717 EXPECT_EQ(0.5f, dummy.opacity());
718 controller->Animate(3.0);
719 controller->UpdateState(true, events.get());
720 EXPECT_TRUE(controller->HasActiveAnimation());
721 EXPECT_EQ(0.f, dummy.opacity());
722 controller->Animate(4.0);
723 controller->UpdateState(true, events.get());
724 EXPECT_EQ(0.75f, dummy.opacity());
725 EXPECT_FALSE(controller->HasActiveAnimation());
728 // Test that a looping animation loops and for the correct number of iterations.
729 TEST(LayerAnimationControllerTest, TrivialLooping) {
730 scoped_ptr<AnimationEventsVector> events(
731 make_scoped_ptr(new AnimationEventsVector));
732 FakeLayerAnimationValueObserver dummy;
733 scoped_refptr<LayerAnimationController> controller(
734 LayerAnimationController::Create(0));
735 controller->AddValueObserver(&dummy);
737 scoped_ptr<Animation> to_add(CreateAnimation(
738 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
740 Animation::Opacity));
741 to_add->set_iterations(3);
742 controller->AddAnimation(to_add.Pass());
744 controller->Animate(0.0);
745 controller->UpdateState(true, events.get());
746 EXPECT_TRUE(controller->HasActiveAnimation());
747 EXPECT_EQ(0.f, dummy.opacity());
748 controller->Animate(1.25);
749 controller->UpdateState(true, events.get());
750 EXPECT_TRUE(controller->HasActiveAnimation());
751 EXPECT_EQ(0.25f, dummy.opacity());
752 controller->Animate(1.75);
753 controller->UpdateState(true, events.get());
754 EXPECT_TRUE(controller->HasActiveAnimation());
755 EXPECT_EQ(0.75f, dummy.opacity());
756 controller->Animate(2.25);
757 controller->UpdateState(true, events.get());
758 EXPECT_TRUE(controller->HasActiveAnimation());
759 EXPECT_EQ(0.25f, dummy.opacity());
760 controller->Animate(2.75);
761 controller->UpdateState(true, events.get());
762 EXPECT_TRUE(controller->HasActiveAnimation());
763 EXPECT_EQ(0.75f, dummy.opacity());
764 controller->Animate(3.0);
765 controller->UpdateState(true, events.get());
766 EXPECT_FALSE(controller->HasActiveAnimation());
767 EXPECT_EQ(1.f, dummy.opacity());
769 // Just be extra sure.
770 controller->Animate(4.0);
771 controller->UpdateState(true, events.get());
772 EXPECT_EQ(1.f, dummy.opacity());
775 // Test that an infinitely looping animation does indeed go until aborted.
776 TEST(LayerAnimationControllerTest, InfiniteLooping) {
777 scoped_ptr<AnimationEventsVector> events(
778 make_scoped_ptr(new AnimationEventsVector));
779 FakeLayerAnimationValueObserver dummy;
780 scoped_refptr<LayerAnimationController> controller(
781 LayerAnimationController::Create(0));
782 controller->AddValueObserver(&dummy);
784 const int id = 1;
785 scoped_ptr<Animation> to_add(CreateAnimation(
786 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
788 Animation::Opacity));
789 to_add->set_iterations(-1);
790 controller->AddAnimation(to_add.Pass());
792 controller->Animate(0.0);
793 controller->UpdateState(true, events.get());
794 EXPECT_TRUE(controller->HasActiveAnimation());
795 EXPECT_EQ(0.f, dummy.opacity());
796 controller->Animate(1.25);
797 controller->UpdateState(true, events.get());
798 EXPECT_TRUE(controller->HasActiveAnimation());
799 EXPECT_EQ(0.25f, dummy.opacity());
800 controller->Animate(1.75);
801 controller->UpdateState(true, events.get());
802 EXPECT_TRUE(controller->HasActiveAnimation());
803 EXPECT_EQ(0.75f, dummy.opacity());
805 controller->Animate(1073741824.25);
806 controller->UpdateState(true, events.get());
807 EXPECT_TRUE(controller->HasActiveAnimation());
808 EXPECT_EQ(0.25f, dummy.opacity());
809 controller->Animate(1073741824.75);
810 controller->UpdateState(true, events.get());
811 EXPECT_TRUE(controller->HasActiveAnimation());
812 EXPECT_EQ(0.75f, dummy.opacity());
814 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
815 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
816 Animation::Aborted, 0.75);
817 EXPECT_FALSE(controller->HasActiveAnimation());
818 EXPECT_EQ(0.75f, dummy.opacity());
821 // Test that pausing and resuming work as expected.
822 TEST(LayerAnimationControllerTest, PauseResume) {
823 scoped_ptr<AnimationEventsVector> events(
824 make_scoped_ptr(new AnimationEventsVector));
825 FakeLayerAnimationValueObserver dummy;
826 scoped_refptr<LayerAnimationController> controller(
827 LayerAnimationController::Create(0));
828 controller->AddValueObserver(&dummy);
830 const int id = 1;
831 controller->AddAnimation(CreateAnimation(
832 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
834 Animation::Opacity));
836 controller->Animate(0.0);
837 controller->UpdateState(true, events.get());
838 EXPECT_TRUE(controller->HasActiveAnimation());
839 EXPECT_EQ(0.f, dummy.opacity());
840 controller->Animate(0.5);
841 controller->UpdateState(true, events.get());
842 EXPECT_TRUE(controller->HasActiveAnimation());
843 EXPECT_EQ(0.5f, dummy.opacity());
845 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
846 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
847 Animation::Paused, 0.5);
849 controller->Animate(1024);
850 controller->UpdateState(true, events.get());
851 EXPECT_TRUE(controller->HasActiveAnimation());
852 EXPECT_EQ(0.5f, dummy.opacity());
854 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
855 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
856 Animation::Running, 1024);
858 controller->Animate(1024.25);
859 controller->UpdateState(true, events.get());
860 EXPECT_TRUE(controller->HasActiveAnimation());
861 EXPECT_EQ(0.75f, dummy.opacity());
862 controller->Animate(1024.5);
863 controller->UpdateState(true, events.get());
864 EXPECT_FALSE(controller->HasActiveAnimation());
865 EXPECT_EQ(1.f, dummy.opacity());
868 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
869 scoped_ptr<AnimationEventsVector> events(
870 make_scoped_ptr(new AnimationEventsVector));
871 FakeLayerAnimationValueObserver dummy;
872 scoped_refptr<LayerAnimationController> controller(
873 LayerAnimationController::Create(0));
874 controller->AddValueObserver(&dummy);
876 const int id = 1;
877 controller->AddAnimation(CreateAnimation(
878 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
880 Animation::Transform));
881 controller->AddAnimation(CreateAnimation(
882 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
884 Animation::Opacity));
885 controller->AddAnimation(CreateAnimation(
886 scoped_ptr<AnimationCurve>(
887 new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
889 Animation::Opacity));
891 controller->Animate(0.0);
892 controller->UpdateState(true, events.get());
893 EXPECT_TRUE(controller->HasActiveAnimation());
894 EXPECT_EQ(0.f, dummy.opacity());
895 controller->Animate(1.0);
896 controller->UpdateState(true, events.get());
897 EXPECT_TRUE(controller->HasActiveAnimation());
898 EXPECT_EQ(0.5f, dummy.opacity());
900 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
901 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
902 Animation::Aborted, 1);
903 controller->Animate(1.0);
904 controller->UpdateState(true, events.get());
905 EXPECT_TRUE(controller->HasActiveAnimation());
906 EXPECT_EQ(1.f, dummy.opacity());
907 controller->Animate(2.0);
908 controller->UpdateState(true, events.get());
909 EXPECT_TRUE(!controller->HasActiveAnimation());
910 EXPECT_EQ(0.75f, dummy.opacity());
913 TEST(LayerAnimationControllerTest, ForceSyncWhenSynchronizedStartTimeNeeded) {
914 FakeLayerAnimationValueObserver dummy_impl;
915 scoped_refptr<LayerAnimationController> controller_impl(
916 LayerAnimationController::Create(0));
917 controller_impl->AddValueObserver(&dummy_impl);
918 scoped_ptr<AnimationEventsVector> events(
919 make_scoped_ptr(new AnimationEventsVector));
920 FakeLayerAnimationValueObserver dummy;
921 scoped_refptr<LayerAnimationController> controller(
922 LayerAnimationController::Create(0));
923 controller->AddValueObserver(&dummy);
925 scoped_ptr<Animation> to_add(CreateAnimation(
926 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
928 Animation::Opacity));
929 to_add->set_needs_synchronized_start_time(true);
930 controller->AddAnimation(to_add.Pass());
932 controller->Animate(0.0);
933 controller->UpdateState(true, events.get());
934 EXPECT_TRUE(controller->HasActiveAnimation());
935 Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
936 EXPECT_TRUE(active_animation);
937 EXPECT_TRUE(active_animation->needs_synchronized_start_time());
939 controller->set_force_sync();
941 controller->PushAnimationUpdatesTo(controller_impl.get());
943 active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
944 EXPECT_TRUE(active_animation);
945 EXPECT_EQ(Animation::WaitingForTargetAvailability,
946 active_animation->run_state());
949 // Tests that skipping a call to UpdateState works as expected.
950 TEST(LayerAnimationControllerTest, SkipUpdateState) {
951 scoped_ptr<AnimationEventsVector> events(
952 make_scoped_ptr(new AnimationEventsVector));
953 FakeLayerAnimationValueObserver dummy;
954 scoped_refptr<LayerAnimationController> controller(
955 LayerAnimationController::Create(0));
956 controller->AddValueObserver(&dummy);
958 controller->AddAnimation(CreateAnimation(
959 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
961 Animation::Transform));
963 controller->Animate(0.0);
964 controller->UpdateState(true, events.get());
966 controller->AddAnimation(CreateAnimation(
967 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
969 Animation::Opacity));
971 // Animate but don't UpdateState.
972 controller->Animate(1.0);
974 controller->Animate(2.0);
975 events.reset(new AnimationEventsVector);
976 controller->UpdateState(true, events.get());
978 // Should have one Started event and one Finished event.
979 EXPECT_EQ(2u, events->size());
980 EXPECT_NE((*events)[0].type, (*events)[1].type);
982 // The float transition should still be at its starting point.
983 EXPECT_TRUE(controller->HasActiveAnimation());
984 EXPECT_EQ(0.f, dummy.opacity());
986 controller->Animate(3.0);
987 controller->UpdateState(true, events.get());
989 // The float tranisition should now be done.
990 EXPECT_EQ(1.f, dummy.opacity());
991 EXPECT_FALSE(controller->HasActiveAnimation());
994 } // namespace
995 } // namespace cc