Implements RLZTrackerDelegate on iOS.
[chromium-blink-merge.git] / cc / animation / layer_animation_controller_unittest.cc
blobe267eea1288c7bd05ab45c7f5635dba196133706
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/animation_delegate.h"
10 #include "cc/animation/animation_registrar.h"
11 #include "cc/animation/keyframed_animation_curve.h"
12 #include "cc/animation/scroll_offset_animation_curve.h"
13 #include "cc/animation/transform_operations.h"
14 #include "cc/test/animation_test_common.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/box_f.h"
18 #include "ui/gfx/transform.h"
20 namespace cc {
21 namespace {
23 using base::TimeDelta;
24 using base::TimeTicks;
26 static base::TimeTicks TicksFromSecondsF(double seconds) {
27 return base::TimeTicks::FromInternalValue(seconds *
28 base::Time::kMicrosecondsPerSecond);
31 // A LayerAnimationController cannot be ticked at 0.0, since an animation
32 // with start time 0.0 is treated as an animation whose start time has
33 // not yet been set.
34 const TimeTicks kInitialTickTime = TicksFromSecondsF(1.0);
36 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve,
37 int group_id,
38 Animation::TargetProperty property) {
39 return Animation::Create(curve.Pass(), 0, group_id, property);
42 TEST(LayerAnimationControllerTest, SyncNewAnimation) {
43 FakeLayerAnimationValueObserver dummy_impl;
44 scoped_refptr<LayerAnimationController> controller_impl(
45 LayerAnimationController::Create(0));
46 controller_impl->AddValueObserver(&dummy_impl);
47 FakeLayerAnimationValueObserver dummy;
48 scoped_refptr<LayerAnimationController> controller(
49 LayerAnimationController::Create(0));
50 controller->AddValueObserver(&dummy);
52 EXPECT_FALSE(controller_impl->GetAnimation(Animation::OPACITY));
54 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
55 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
57 int animation_id =
58 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
59 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
61 controller->PushAnimationUpdatesTo(controller_impl.get());
62 EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
63 controller_impl->ActivateAnimations();
65 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
66 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
67 controller_impl->GetAnimationById(animation_id)->run_state());
70 // If an animation is started on the impl thread before it is ticked on the main
71 // thread, we must be sure to respect the synchronized start time.
72 TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) {
73 FakeLayerAnimationValueObserver dummy_impl;
74 scoped_refptr<LayerAnimationController> controller_impl(
75 LayerAnimationController::Create(0));
76 controller_impl->AddValueObserver(&dummy_impl);
77 FakeLayerAnimationValueObserver dummy;
78 scoped_refptr<LayerAnimationController> controller(
79 LayerAnimationController::Create(0));
80 controller->AddValueObserver(&dummy);
82 EXPECT_FALSE(controller_impl->GetAnimation(Animation::OPACITY));
84 int animation_id =
85 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
87 controller->PushAnimationUpdatesTo(controller_impl.get());
88 controller_impl->ActivateAnimations();
90 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
91 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
92 controller_impl->GetAnimationById(animation_id)->run_state());
94 AnimationEventsVector events;
95 controller_impl->Animate(kInitialTickTime);
96 controller_impl->UpdateState(true, &events);
98 // Synchronize the start times.
99 EXPECT_EQ(1u, events.size());
100 controller->NotifyAnimationStarted(events[0]);
101 EXPECT_EQ(controller->GetAnimationById(animation_id)->start_time(),
102 controller_impl->GetAnimationById(animation_id)->start_time());
104 // Start the animation on the main thread. Should not affect the start time.
105 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
106 controller->UpdateState(true, nullptr);
107 EXPECT_EQ(controller->GetAnimationById(animation_id)->start_time(),
108 controller_impl->GetAnimationById(animation_id)->start_time());
111 TEST(LayerAnimationControllerTest, UseSpecifiedStartTimes) {
112 FakeLayerAnimationValueObserver dummy_impl;
113 scoped_refptr<LayerAnimationController> controller_impl(
114 LayerAnimationController::Create(0));
115 controller_impl->AddValueObserver(&dummy_impl);
116 FakeLayerAnimationValueObserver dummy;
117 scoped_refptr<LayerAnimationController> controller(
118 LayerAnimationController::Create(0));
119 controller->AddValueObserver(&dummy);
121 int animation_id =
122 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
124 const TimeTicks start_time = TicksFromSecondsF(123);
125 controller->GetAnimation(Animation::OPACITY)->set_start_time(start_time);
127 controller->PushAnimationUpdatesTo(controller_impl.get());
128 controller_impl->ActivateAnimations();
130 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
131 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
132 controller_impl->GetAnimationById(animation_id)->run_state());
134 AnimationEventsVector events;
135 controller_impl->Animate(kInitialTickTime);
136 controller_impl->UpdateState(true, &events);
138 // Synchronize the start times.
139 EXPECT_EQ(1u, events.size());
140 controller->NotifyAnimationStarted(events[0]);
142 EXPECT_EQ(start_time,
143 controller->GetAnimationById(animation_id)->start_time());
144 EXPECT_EQ(controller->GetAnimationById(animation_id)->start_time(),
145 controller_impl->GetAnimationById(animation_id)->start_time());
147 // Start the animation on the main thread. Should not affect the start time.
148 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
149 controller->UpdateState(true, nullptr);
150 EXPECT_EQ(start_time,
151 controller->GetAnimationById(animation_id)->start_time());
152 EXPECT_EQ(controller->GetAnimationById(animation_id)->start_time(),
153 controller_impl->GetAnimationById(animation_id)->start_time());
156 // Tests that controllers activate and deactivate as expected.
157 TEST(LayerAnimationControllerTest, Activation) {
158 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
159 scoped_ptr<AnimationRegistrar> registrar_impl = AnimationRegistrar::Create();
161 FakeLayerAnimationValueObserver dummy_impl;
162 scoped_refptr<LayerAnimationController> controller_impl(
163 LayerAnimationController::Create(0));
164 controller_impl->AddValueObserver(&dummy_impl);
165 FakeLayerAnimationValueObserver dummy;
166 scoped_refptr<LayerAnimationController> controller(
167 LayerAnimationController::Create(0));
168 controller->AddValueObserver(&dummy);
169 scoped_ptr<AnimationEventsVector> events(
170 make_scoped_ptr(new AnimationEventsVector));
172 controller->SetAnimationRegistrar(registrar.get());
173 controller_impl->SetAnimationRegistrar(registrar_impl.get());
174 EXPECT_EQ(1u, registrar->all_animation_controllers_for_testing().size());
175 EXPECT_EQ(1u, registrar_impl->all_animation_controllers_for_testing().size());
177 // Initially, both controllers should be inactive.
178 EXPECT_EQ(0u, registrar->active_animation_controllers_for_testing().size());
179 EXPECT_EQ(0u,
180 registrar_impl->active_animation_controllers_for_testing().size());
182 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
183 // The main thread controller should now be active.
184 EXPECT_EQ(1u, registrar->active_animation_controllers_for_testing().size());
186 controller->PushAnimationUpdatesTo(controller_impl.get());
187 controller_impl->ActivateAnimations();
188 // Both controllers should now be active.
189 EXPECT_EQ(1u, registrar->active_animation_controllers_for_testing().size());
190 EXPECT_EQ(1u,
191 registrar_impl->active_animation_controllers_for_testing().size());
193 controller_impl->Animate(kInitialTickTime);
194 controller_impl->UpdateState(true, events.get());
195 EXPECT_EQ(1u, events->size());
196 controller->NotifyAnimationStarted((*events)[0]);
198 EXPECT_EQ(1u, registrar->active_animation_controllers_for_testing().size());
199 EXPECT_EQ(1u,
200 registrar_impl->active_animation_controllers_for_testing().size());
202 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
203 controller->UpdateState(true, nullptr);
204 EXPECT_EQ(1u, registrar->active_animation_controllers_for_testing().size());
206 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
207 controller->UpdateState(true, nullptr);
208 EXPECT_EQ(Animation::FINISHED,
209 controller->GetAnimation(Animation::OPACITY)->run_state());
210 EXPECT_EQ(1u, registrar->active_animation_controllers_for_testing().size());
212 events.reset(new AnimationEventsVector);
213 controller_impl->Animate(kInitialTickTime +
214 TimeDelta::FromMilliseconds(1500));
215 controller_impl->UpdateState(true, events.get());
217 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
218 controller_impl->GetAnimation(Animation::OPACITY)->run_state());
219 // The impl thread controller should have de-activated.
220 EXPECT_EQ(0u,
221 registrar_impl->active_animation_controllers_for_testing().size());
223 EXPECT_EQ(1u, events->size());
224 controller->NotifyAnimationFinished((*events)[0]);
225 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
226 controller->UpdateState(true, nullptr);
228 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
229 controller->GetAnimation(Animation::OPACITY)->run_state());
230 // The main thread controller should have de-activated.
231 EXPECT_EQ(0u, registrar->active_animation_controllers_for_testing().size());
233 controller->PushAnimationUpdatesTo(controller_impl.get());
234 controller_impl->ActivateAnimations();
235 EXPECT_FALSE(controller->has_any_animation());
236 EXPECT_FALSE(controller_impl->has_any_animation());
237 EXPECT_EQ(0u, registrar->active_animation_controllers_for_testing().size());
238 EXPECT_EQ(0u,
239 registrar_impl->active_animation_controllers_for_testing().size());
241 controller->SetAnimationRegistrar(nullptr);
242 controller_impl->SetAnimationRegistrar(nullptr);
245 TEST(LayerAnimationControllerTest, SyncPause) {
246 FakeLayerAnimationValueObserver dummy_impl;
247 scoped_refptr<LayerAnimationController> controller_impl(
248 LayerAnimationController::Create(0));
249 controller_impl->AddValueObserver(&dummy_impl);
250 FakeLayerAnimationValueObserver dummy;
251 scoped_refptr<LayerAnimationController> controller(
252 LayerAnimationController::Create(0));
253 controller->AddValueObserver(&dummy);
255 EXPECT_FALSE(controller_impl->GetAnimation(Animation::OPACITY));
257 int animation_id =
258 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
260 controller->PushAnimationUpdatesTo(controller_impl.get());
261 controller_impl->ActivateAnimations();
263 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
264 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
265 controller_impl->GetAnimationById(animation_id)->run_state());
267 // Start the animations on each controller.
268 AnimationEventsVector events;
269 controller_impl->Animate(kInitialTickTime);
270 controller_impl->UpdateState(true, &events);
271 controller->Animate(kInitialTickTime);
272 controller->UpdateState(true, nullptr);
273 EXPECT_EQ(Animation::RUNNING,
274 controller_impl->GetAnimationById(animation_id)->run_state());
275 EXPECT_EQ(Animation::RUNNING,
276 controller->GetAnimationById(animation_id)->run_state());
278 // Pause the main-thread animation.
279 controller->PauseAnimation(
280 animation_id,
281 TimeDelta::FromMilliseconds(1000) + TimeDelta::FromMilliseconds(1000));
282 EXPECT_EQ(Animation::PAUSED,
283 controller->GetAnimationById(animation_id)->run_state());
285 // The pause run state change should make it to the impl thread controller.
286 controller->PushAnimationUpdatesTo(controller_impl.get());
287 controller_impl->ActivateAnimations();
288 EXPECT_EQ(Animation::PAUSED,
289 controller_impl->GetAnimationById(animation_id)->run_state());
292 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
293 FakeLayerAnimationValueObserver dummy_impl;
294 scoped_refptr<LayerAnimationController> controller_impl(
295 LayerAnimationController::Create(0));
296 controller_impl->AddValueObserver(&dummy_impl);
297 FakeLayerAnimationValueObserver dummy;
298 scoped_refptr<LayerAnimationController> controller(
299 LayerAnimationController::Create(0));
300 controller->AddValueObserver(&dummy);
301 scoped_ptr<AnimationEventsVector> events(
302 make_scoped_ptr(new AnimationEventsVector));
304 EXPECT_FALSE(controller_impl->GetAnimation(Animation::OPACITY));
306 int animation_id =
307 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
309 controller->PushAnimationUpdatesTo(controller_impl.get());
310 controller_impl->ActivateAnimations();
312 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
313 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
314 controller_impl->GetAnimationById(animation_id)->run_state());
316 events.reset(new AnimationEventsVector);
317 controller_impl->Animate(kInitialTickTime);
318 controller_impl->UpdateState(true, events.get());
319 EXPECT_EQ(1u, events->size());
320 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
322 // Notify main thread controller that the animation has started.
323 controller->NotifyAnimationStarted((*events)[0]);
325 // Complete animation on impl thread.
326 events.reset(new AnimationEventsVector);
327 controller_impl->Animate(kInitialTickTime + TimeDelta::FromSeconds(1));
328 controller_impl->UpdateState(true, events.get());
329 EXPECT_EQ(1u, events->size());
330 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
332 controller->NotifyAnimationFinished((*events)[0]);
334 controller->Animate(kInitialTickTime + TimeDelta::FromSeconds(2));
335 controller->UpdateState(true, nullptr);
337 controller->PushAnimationUpdatesTo(controller_impl.get());
338 controller_impl->ActivateAnimations();
339 EXPECT_FALSE(controller->GetAnimationById(animation_id));
340 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
343 // Ensure that a finished animation is eventually deleted by both the
344 // main-thread and the impl-thread controllers.
345 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
346 FakeLayerAnimationValueObserver dummy;
347 FakeLayerAnimationValueObserver dummy_impl;
348 scoped_ptr<AnimationEventsVector> events(
349 make_scoped_ptr(new AnimationEventsVector));
350 scoped_refptr<LayerAnimationController> controller(
351 LayerAnimationController::Create(0));
352 scoped_refptr<LayerAnimationController> controller_impl(
353 LayerAnimationController::Create(0));
354 controller->AddValueObserver(&dummy);
355 controller_impl->AddValueObserver(&dummy_impl);
357 AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
358 controller->Animate(kInitialTickTime);
359 controller->UpdateState(true, nullptr);
360 controller->PushAnimationUpdatesTo(controller_impl.get());
361 controller_impl->ActivateAnimations();
363 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
364 controller_impl->UpdateState(true, events.get());
366 // There should be a STARTED event for the animation.
367 EXPECT_EQ(1u, events->size());
368 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
369 controller->NotifyAnimationStarted((*events)[0]);
371 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
372 controller->UpdateState(true, nullptr);
374 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
375 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
377 events.reset(new AnimationEventsVector);
378 controller_impl->Animate(kInitialTickTime +
379 TimeDelta::FromMilliseconds(2000));
380 controller_impl->UpdateState(true, events.get());
382 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
384 // There should be a FINISHED event for the animation.
385 EXPECT_EQ(1u, events->size());
386 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
388 // Neither controller should have deleted the animation yet.
389 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
390 EXPECT_TRUE(controller_impl->GetAnimation(Animation::OPACITY));
392 controller->NotifyAnimationFinished((*events)[0]);
394 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
395 controller->UpdateState(true, nullptr);
396 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
398 controller->PushAnimationUpdatesTo(controller_impl.get());
400 // Both controllers should now have deleted the animation. The impl controller
401 // should have deleted the animation even though activation has not occurred,
402 // since the animation was already waiting for deletion when
403 // PushAnimationUpdatesTo was called.
404 EXPECT_FALSE(controller->has_any_animation());
405 EXPECT_FALSE(controller_impl->has_any_animation());
408 // Tests that transitioning opacity from 0 to 1 works as expected.
410 static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
411 const AnimationEventsVector* events) {
412 const AnimationEvent* event = 0;
413 for (size_t i = 0; i < events->size(); ++i)
414 if ((*events)[i].type == AnimationEvent::PROPERTY_UPDATE)
415 event = &(*events)[i];
417 return event;
420 TEST(LayerAnimationControllerTest, TrivialTransition) {
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->AddValueObserver(&dummy);
428 scoped_ptr<Animation> to_add(CreateAnimation(
429 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
430 1, Animation::OPACITY));
432 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
433 controller->AddAnimation(to_add.Pass());
434 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
435 controller->Animate(kInitialTickTime);
436 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
437 controller->UpdateState(true, events.get());
438 EXPECT_TRUE(controller->HasActiveAnimation());
439 EXPECT_EQ(0.f, dummy.opacity());
440 // A non-impl-only animation should not generate property updates.
441 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
442 EXPECT_FALSE(event);
443 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
444 controller->UpdateState(true, events.get());
445 EXPECT_EQ(1.f, dummy.opacity());
446 EXPECT_FALSE(controller->HasActiveAnimation());
447 event = GetMostRecentPropertyUpdateEvent(events.get());
448 EXPECT_FALSE(event);
451 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
452 scoped_ptr<AnimationEventsVector> events(
453 make_scoped_ptr(new AnimationEventsVector));
454 FakeLayerAnimationValueObserver dummy_impl;
455 scoped_refptr<LayerAnimationController> controller_impl(
456 LayerAnimationController::Create(0));
457 controller_impl->AddValueObserver(&dummy_impl);
459 scoped_ptr<Animation> to_add(CreateAnimation(
460 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
461 1, Animation::OPACITY));
462 to_add->set_is_impl_only(true);
464 controller_impl->AddAnimation(to_add.Pass());
465 controller_impl->Animate(kInitialTickTime);
466 controller_impl->UpdateState(true, events.get());
467 EXPECT_TRUE(controller_impl->HasActiveAnimation());
468 EXPECT_EQ(0.f, dummy_impl.opacity());
469 EXPECT_EQ(1u, events->size());
470 const AnimationEvent* start_opacity_event =
471 GetMostRecentPropertyUpdateEvent(events.get());
472 EXPECT_EQ(0.f, start_opacity_event->opacity);
474 controller_impl->Animate(kInitialTickTime +
475 TimeDelta::FromMilliseconds(1000));
476 controller_impl->UpdateState(true, events.get());
477 EXPECT_EQ(1.f, dummy_impl.opacity());
478 EXPECT_FALSE(controller_impl->HasActiveAnimation());
479 EXPECT_EQ(2u, events->size());
480 const AnimationEvent* end_opacity_event =
481 GetMostRecentPropertyUpdateEvent(events.get());
482 EXPECT_EQ(1.f, end_opacity_event->opacity);
485 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
486 scoped_ptr<AnimationEventsVector> events(
487 make_scoped_ptr(new AnimationEventsVector));
488 FakeLayerAnimationValueObserver dummy_impl;
489 scoped_refptr<LayerAnimationController> controller_impl(
490 LayerAnimationController::Create(0));
491 controller_impl->AddValueObserver(&dummy_impl);
493 // Choose different values for x and y to avoid coincidental values in the
494 // observed transforms.
495 const float delta_x = 3;
496 const float delta_y = 4;
498 scoped_ptr<KeyframedTransformAnimationCurve> curve(
499 KeyframedTransformAnimationCurve::Create());
501 // Create simple TRANSFORM animation.
502 TransformOperations operations;
503 curve->AddKeyframe(
504 TransformKeyframe::Create(base::TimeDelta(), operations, nullptr));
505 operations.AppendTranslate(delta_x, delta_y, 0);
506 curve->AddKeyframe(TransformKeyframe::Create(
507 base::TimeDelta::FromSecondsD(1.0), operations, nullptr));
509 scoped_ptr<Animation> animation(
510 Animation::Create(curve.Pass(), 1, 0, Animation::TRANSFORM));
511 animation->set_is_impl_only(true);
512 controller_impl->AddAnimation(animation.Pass());
514 // Run animation.
515 controller_impl->Animate(kInitialTickTime);
516 controller_impl->UpdateState(true, events.get());
517 EXPECT_TRUE(controller_impl->HasActiveAnimation());
518 EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
519 EXPECT_EQ(1u, events->size());
520 const AnimationEvent* start_transform_event =
521 GetMostRecentPropertyUpdateEvent(events.get());
522 ASSERT_TRUE(start_transform_event);
523 EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
524 EXPECT_TRUE(start_transform_event->is_impl_only);
526 gfx::Transform expected_transform;
527 expected_transform.Translate(delta_x, delta_y);
529 controller_impl->Animate(kInitialTickTime +
530 TimeDelta::FromMilliseconds(1000));
531 controller_impl->UpdateState(true, events.get());
532 EXPECT_EQ(expected_transform, dummy_impl.transform());
533 EXPECT_FALSE(controller_impl->HasActiveAnimation());
534 EXPECT_EQ(2u, events->size());
535 const AnimationEvent* end_transform_event =
536 GetMostRecentPropertyUpdateEvent(events.get());
537 EXPECT_EQ(expected_transform, end_transform_event->transform);
538 EXPECT_TRUE(end_transform_event->is_impl_only);
541 TEST(LayerAnimationControllerTest, FilterTransition) {
542 scoped_ptr<AnimationEventsVector> events(
543 make_scoped_ptr(new AnimationEventsVector));
544 FakeLayerAnimationValueObserver dummy;
545 scoped_refptr<LayerAnimationController> controller(
546 LayerAnimationController::Create(0));
547 controller->AddValueObserver(&dummy);
549 scoped_ptr<KeyframedFilterAnimationCurve> curve(
550 KeyframedFilterAnimationCurve::Create());
552 FilterOperations start_filters;
553 start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
554 curve->AddKeyframe(
555 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
556 FilterOperations end_filters;
557 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
558 curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
559 end_filters, nullptr));
561 scoped_ptr<Animation> animation(
562 Animation::Create(curve.Pass(), 1, 0, Animation::FILTER));
563 controller->AddAnimation(animation.Pass());
565 controller->Animate(kInitialTickTime);
566 controller->UpdateState(true, events.get());
567 EXPECT_TRUE(controller->HasActiveAnimation());
568 EXPECT_EQ(start_filters, dummy.filters());
569 // A non-impl-only animation should not generate property updates.
570 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
571 EXPECT_FALSE(event);
573 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
574 controller->UpdateState(true, events.get());
575 EXPECT_EQ(1u, dummy.filters().size());
576 EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
577 dummy.filters().at(0));
578 event = GetMostRecentPropertyUpdateEvent(events.get());
579 EXPECT_FALSE(event);
581 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
582 controller->UpdateState(true, events.get());
583 EXPECT_EQ(end_filters, dummy.filters());
584 EXPECT_FALSE(controller->HasActiveAnimation());
585 event = GetMostRecentPropertyUpdateEvent(events.get());
586 EXPECT_FALSE(event);
589 TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
590 scoped_ptr<AnimationEventsVector> events(
591 make_scoped_ptr(new AnimationEventsVector));
592 FakeLayerAnimationValueObserver dummy_impl;
593 scoped_refptr<LayerAnimationController> controller_impl(
594 LayerAnimationController::Create(0));
595 controller_impl->AddValueObserver(&dummy_impl);
597 scoped_ptr<KeyframedFilterAnimationCurve> curve(
598 KeyframedFilterAnimationCurve::Create());
600 // Create simple FILTER animation.
601 FilterOperations start_filters;
602 start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
603 curve->AddKeyframe(
604 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
605 FilterOperations end_filters;
606 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
607 curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
608 end_filters, nullptr));
610 scoped_ptr<Animation> animation(
611 Animation::Create(curve.Pass(), 1, 0, Animation::FILTER));
612 animation->set_is_impl_only(true);
613 controller_impl->AddAnimation(animation.Pass());
615 // Run animation.
616 controller_impl->Animate(kInitialTickTime);
617 controller_impl->UpdateState(true, events.get());
618 EXPECT_TRUE(controller_impl->HasActiveAnimation());
619 EXPECT_EQ(start_filters, dummy_impl.filters());
620 EXPECT_EQ(1u, events->size());
621 const AnimationEvent* start_filter_event =
622 GetMostRecentPropertyUpdateEvent(events.get());
623 EXPECT_TRUE(start_filter_event);
624 EXPECT_EQ(start_filters, start_filter_event->filters);
625 EXPECT_TRUE(start_filter_event->is_impl_only);
627 controller_impl->Animate(kInitialTickTime +
628 TimeDelta::FromMilliseconds(1000));
629 controller_impl->UpdateState(true, events.get());
630 EXPECT_EQ(end_filters, dummy_impl.filters());
631 EXPECT_FALSE(controller_impl->HasActiveAnimation());
632 EXPECT_EQ(2u, events->size());
633 const AnimationEvent* end_filter_event =
634 GetMostRecentPropertyUpdateEvent(events.get());
635 EXPECT_TRUE(end_filter_event);
636 EXPECT_EQ(end_filters, end_filter_event->filters);
637 EXPECT_TRUE(end_filter_event->is_impl_only);
640 TEST(LayerAnimationControllerTest, ScrollOffsetTransition) {
641 FakeLayerAnimationValueObserver dummy_impl;
642 FakeLayerAnimationValueProvider dummy_provider_impl;
643 scoped_refptr<LayerAnimationController> controller_impl(
644 LayerAnimationController::Create(0));
645 controller_impl->AddValueObserver(&dummy_impl);
646 controller_impl->set_value_provider(&dummy_provider_impl);
647 scoped_ptr<AnimationEventsVector> events(
648 make_scoped_ptr(new AnimationEventsVector));
649 FakeLayerAnimationValueObserver dummy;
650 FakeLayerAnimationValueProvider dummy_provider;
651 scoped_refptr<LayerAnimationController> controller(
652 LayerAnimationController::Create(0));
653 controller->AddValueObserver(&dummy);
654 controller->set_value_provider(&dummy_provider);
656 gfx::ScrollOffset initial_value(100.f, 300.f);
657 gfx::ScrollOffset target_value(300.f, 200.f);
658 scoped_ptr<ScrollOffsetAnimationCurve> curve(
659 ScrollOffsetAnimationCurve::Create(
660 target_value,
661 EaseInOutTimingFunction::Create().Pass()));
663 scoped_ptr<Animation> animation(
664 Animation::Create(curve.Pass(), 1, 0, Animation::SCROLL_OFFSET));
665 animation->set_needs_synchronized_start_time(true);
666 controller->AddAnimation(animation.Pass());
668 dummy_provider_impl.set_scroll_offset(initial_value);
669 controller->PushAnimationUpdatesTo(controller_impl.get());
670 controller_impl->ActivateAnimations();
671 EXPECT_TRUE(controller_impl->GetAnimation(Animation::SCROLL_OFFSET));
672 TimeDelta duration = controller_impl->GetAnimation(Animation::SCROLL_OFFSET)
673 ->curve()
674 ->Duration();
675 EXPECT_EQ(
676 duration,
677 controller->GetAnimation(Animation::SCROLL_OFFSET)->curve()->Duration());
679 controller->Animate(kInitialTickTime);
680 controller->UpdateState(true, nullptr);
681 EXPECT_TRUE(controller->HasActiveAnimation());
682 EXPECT_EQ(initial_value, dummy.scroll_offset());
684 controller_impl->Animate(kInitialTickTime);
685 controller_impl->UpdateState(true, events.get());
686 EXPECT_TRUE(controller_impl->HasActiveAnimation());
687 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
688 // Scroll offset animations should not generate property updates.
689 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
690 EXPECT_FALSE(event);
692 controller->NotifyAnimationStarted((*events)[0]);
693 controller->Animate(kInitialTickTime + duration / 2);
694 controller->UpdateState(true, nullptr);
695 EXPECT_TRUE(controller->HasActiveAnimation());
696 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
698 controller_impl->Animate(kInitialTickTime + duration / 2);
699 controller_impl->UpdateState(true, events.get());
700 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
701 dummy_impl.scroll_offset());
702 event = GetMostRecentPropertyUpdateEvent(events.get());
703 EXPECT_FALSE(event);
705 controller_impl->Animate(kInitialTickTime + duration);
706 controller_impl->UpdateState(true, events.get());
707 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
708 EXPECT_FALSE(controller_impl->HasActiveAnimation());
709 event = GetMostRecentPropertyUpdateEvent(events.get());
710 EXPECT_FALSE(event);
712 controller->Animate(kInitialTickTime + duration);
713 controller->UpdateState(true, nullptr);
714 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
715 EXPECT_FALSE(controller->HasActiveAnimation());
718 // Ensure that when the impl controller doesn't have a value provider,
719 // the main-thread controller's value provider is used to obtain the intial
720 // scroll offset.
721 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
722 FakeLayerAnimationValueObserver dummy_impl;
723 scoped_refptr<LayerAnimationController> controller_impl(
724 LayerAnimationController::Create(0));
725 controller_impl->AddValueObserver(&dummy_impl);
726 scoped_ptr<AnimationEventsVector> events(
727 make_scoped_ptr(new AnimationEventsVector));
728 FakeLayerAnimationValueObserver dummy;
729 FakeLayerAnimationValueProvider dummy_provider;
730 scoped_refptr<LayerAnimationController> controller(
731 LayerAnimationController::Create(0));
732 controller->AddValueObserver(&dummy);
733 controller->set_value_provider(&dummy_provider);
735 gfx::ScrollOffset initial_value(500.f, 100.f);
736 gfx::ScrollOffset target_value(300.f, 200.f);
737 scoped_ptr<ScrollOffsetAnimationCurve> curve(
738 ScrollOffsetAnimationCurve::Create(
739 target_value,
740 EaseInOutTimingFunction::Create().Pass()));
742 scoped_ptr<Animation> animation(
743 Animation::Create(curve.Pass(), 1, 0, Animation::SCROLL_OFFSET));
744 animation->set_needs_synchronized_start_time(true);
745 controller->AddAnimation(animation.Pass());
747 dummy_provider.set_scroll_offset(initial_value);
748 controller->PushAnimationUpdatesTo(controller_impl.get());
749 controller_impl->ActivateAnimations();
750 EXPECT_TRUE(controller_impl->GetAnimation(Animation::SCROLL_OFFSET));
751 TimeDelta duration = controller_impl->GetAnimation(Animation::SCROLL_OFFSET)
752 ->curve()
753 ->Duration();
754 EXPECT_EQ(
755 duration,
756 controller->GetAnimation(Animation::SCROLL_OFFSET)->curve()->Duration());
758 controller->Animate(kInitialTickTime);
759 controller->UpdateState(true, nullptr);
760 EXPECT_TRUE(controller->HasActiveAnimation());
761 EXPECT_EQ(initial_value, dummy.scroll_offset());
763 controller_impl->Animate(kInitialTickTime);
764 controller_impl->UpdateState(true, events.get());
765 EXPECT_TRUE(controller_impl->HasActiveAnimation());
766 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
767 // Scroll offset animations should not generate property updates.
768 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
769 EXPECT_FALSE(event);
772 controller->NotifyAnimationStarted((*events)[0]);
773 controller->Animate(kInitialTickTime + duration / 2);
774 controller->UpdateState(true, nullptr);
775 EXPECT_TRUE(controller->HasActiveAnimation());
776 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
778 controller_impl->Animate(kInitialTickTime + duration / 2);
779 controller_impl->UpdateState(true, events.get());
780 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
781 dummy_impl.scroll_offset());
782 event = GetMostRecentPropertyUpdateEvent(events.get());
783 EXPECT_FALSE(event);
785 controller_impl->Animate(kInitialTickTime + duration);
786 controller_impl->UpdateState(true, events.get());
787 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
788 EXPECT_FALSE(controller_impl->HasActiveAnimation());
789 event = GetMostRecentPropertyUpdateEvent(events.get());
790 EXPECT_FALSE(event);
792 controller->Animate(kInitialTickTime + duration);
793 controller->UpdateState(true, nullptr);
794 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
795 EXPECT_FALSE(controller->HasActiveAnimation());
798 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
799 FakeLayerAnimationValueObserver dummy_impl;
800 scoped_refptr<LayerAnimationController> controller_impl(
801 LayerAnimationController::Create(0));
802 controller_impl->AddValueObserver(&dummy_impl);
803 scoped_ptr<AnimationEventsVector> events(
804 make_scoped_ptr(new AnimationEventsVector));
806 gfx::ScrollOffset initial_value(100.f, 300.f);
807 gfx::ScrollOffset target_value(300.f, 200.f);
808 scoped_ptr<ScrollOffsetAnimationCurve> curve(
809 ScrollOffsetAnimationCurve::Create(
810 target_value,
811 EaseInOutTimingFunction::Create().Pass()));
812 curve->SetInitialValue(initial_value);
813 double duration_in_seconds = curve->Duration().InSecondsF();
815 scoped_ptr<Animation> animation(
816 Animation::Create(curve.Pass(), 1, 0, Animation::SCROLL_OFFSET));
817 animation->set_is_impl_only(true);
818 controller_impl->AddAnimation(animation.Pass());
820 controller_impl->Animate(kInitialTickTime);
821 controller_impl->UpdateState(true, events.get());
822 EXPECT_TRUE(controller_impl->HasActiveAnimation());
823 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
824 // Scroll offset animations should not generate property updates.
825 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
826 EXPECT_FALSE(event);
828 TimeDelta duration = TimeDelta::FromMicroseconds(
829 duration_in_seconds * base::Time::kMicrosecondsPerSecond);
831 controller_impl->Animate(kInitialTickTime + duration / 2);
832 controller_impl->UpdateState(true, events.get());
833 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
834 dummy_impl.scroll_offset());
835 event = GetMostRecentPropertyUpdateEvent(events.get());
836 EXPECT_FALSE(event);
838 controller_impl->Animate(kInitialTickTime + duration);
839 controller_impl->UpdateState(true, events.get());
840 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
841 EXPECT_FALSE(controller_impl->HasActiveAnimation());
842 event = GetMostRecentPropertyUpdateEvent(events.get());
843 EXPECT_FALSE(event);
846 TEST(LayerAnimationControllerTest, ScrollOffsetRemovalClearsScrollDelta) {
847 FakeLayerAnimationValueObserver dummy_impl;
848 FakeLayerAnimationValueProvider dummy_provider_impl;
849 scoped_refptr<LayerAnimationController> controller_impl(
850 LayerAnimationController::Create(0));
851 controller_impl->AddValueObserver(&dummy_impl);
852 controller_impl->set_value_provider(&dummy_provider_impl);
853 scoped_ptr<AnimationEventsVector> events(
854 make_scoped_ptr(new AnimationEventsVector));
855 FakeLayerAnimationValueObserver dummy;
856 FakeLayerAnimationValueProvider dummy_provider;
857 scoped_refptr<LayerAnimationController> controller(
858 LayerAnimationController::Create(0));
859 controller->AddValueObserver(&dummy);
860 controller->set_value_provider(&dummy_provider);
862 // First test the 1-argument version of RemoveAnimation.
863 gfx::ScrollOffset target_value(300.f, 200.f);
864 scoped_ptr<ScrollOffsetAnimationCurve> curve(
865 ScrollOffsetAnimationCurve::Create(
866 target_value, EaseInOutTimingFunction::Create().Pass()));
868 int animation_id = 1;
869 scoped_ptr<Animation> animation(Animation::Create(
870 curve.Pass(), animation_id, 0, Animation::SCROLL_OFFSET));
871 animation->set_needs_synchronized_start_time(true);
872 controller->AddAnimation(animation.Pass());
873 controller->PushAnimationUpdatesTo(controller_impl.get());
874 controller_impl->ActivateAnimations();
875 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
876 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
878 controller->RemoveAnimation(animation_id);
879 EXPECT_TRUE(controller->scroll_offset_animation_was_interrupted());
881 controller->PushAnimationUpdatesTo(controller_impl.get());
882 EXPECT_TRUE(controller_impl->scroll_offset_animation_was_interrupted());
883 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
885 controller_impl->ActivateAnimations();
886 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
888 // Now, test the 2-argument version of RemoveAnimation.
889 curve = ScrollOffsetAnimationCurve::Create(
890 target_value, EaseInOutTimingFunction::Create().Pass());
891 animation = Animation::Create(curve.Pass(), animation_id, 0,
892 Animation::SCROLL_OFFSET);
893 animation->set_needs_synchronized_start_time(true);
894 controller->AddAnimation(animation.Pass());
895 controller->PushAnimationUpdatesTo(controller_impl.get());
896 controller_impl->ActivateAnimations();
897 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
898 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
900 controller->RemoveAnimation(animation_id);
901 EXPECT_TRUE(controller->scroll_offset_animation_was_interrupted());
903 controller->PushAnimationUpdatesTo(controller_impl.get());
904 EXPECT_TRUE(controller_impl->scroll_offset_animation_was_interrupted());
905 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
907 controller_impl->ActivateAnimations();
908 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
910 // Check that removing non-scroll-offset animations does not cause
911 // scroll_offset_animation_was_interrupted() to get set.
912 animation_id = AddAnimatedTransformToController(controller.get(), 1.0, 1, 2);
913 controller->PushAnimationUpdatesTo(controller_impl.get());
914 controller_impl->ActivateAnimations();
915 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
916 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
918 controller->RemoveAnimation(animation_id);
919 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
921 controller->PushAnimationUpdatesTo(controller_impl.get());
922 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
923 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
925 controller_impl->ActivateAnimations();
926 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
928 animation_id =
929 AddAnimatedFilterToController(controller.get(), 1.0, 0.1f, 0.2f);
930 controller->PushAnimationUpdatesTo(controller_impl.get());
931 controller_impl->ActivateAnimations();
932 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
933 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
935 controller->RemoveAnimation(animation_id);
936 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
938 controller->PushAnimationUpdatesTo(controller_impl.get());
939 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
940 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
942 controller_impl->ActivateAnimations();
943 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
946 class FakeAnimationDelegate : public AnimationDelegate {
947 public:
948 FakeAnimationDelegate()
949 : started_(false), finished_(false), start_time_(base::TimeTicks()) {}
951 void NotifyAnimationStarted(TimeTicks monotonic_time,
952 Animation::TargetProperty target_property,
953 int group) override {
954 started_ = true;
955 start_time_ = monotonic_time;
958 void NotifyAnimationFinished(TimeTicks monotonic_time,
959 Animation::TargetProperty target_property,
960 int group) override {
961 finished_ = true;
964 bool started() { return started_; }
966 bool finished() { return finished_; }
968 TimeTicks start_time() { return start_time_; }
970 private:
971 bool started_;
972 bool finished_;
973 TimeTicks start_time_;
976 // Tests that impl-only animations lead to start and finished notifications
977 // on the impl thread controller's animation delegate.
978 TEST(LayerAnimationControllerTest,
979 NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate) {
980 FakeLayerAnimationValueObserver dummy_impl;
981 scoped_refptr<LayerAnimationController> controller_impl(
982 LayerAnimationController::Create(0));
983 controller_impl->AddValueObserver(&dummy_impl);
984 scoped_ptr<AnimationEventsVector> events(
985 make_scoped_ptr(new AnimationEventsVector));
986 FakeAnimationDelegate delegate;
987 controller_impl->set_layer_animation_delegate(&delegate);
989 scoped_ptr<Animation> to_add(CreateAnimation(
990 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
991 1, Animation::OPACITY));
992 to_add->set_is_impl_only(true);
993 controller_impl->AddAnimation(to_add.Pass());
995 EXPECT_FALSE(delegate.started());
996 EXPECT_FALSE(delegate.finished());
998 controller_impl->Animate(kInitialTickTime);
999 controller_impl->UpdateState(true, events.get());
1001 EXPECT_TRUE(delegate.started());
1002 EXPECT_FALSE(delegate.finished());
1004 events.reset(new AnimationEventsVector);
1005 controller_impl->Animate(kInitialTickTime +
1006 TimeDelta::FromMilliseconds(1000));
1007 controller_impl->UpdateState(true, events.get());
1009 EXPECT_TRUE(delegate.started());
1010 EXPECT_TRUE(delegate.finished());
1013 // Tests that specified start times are sent to the main thread delegate
1014 TEST(LayerAnimationControllerTest,
1015 SpecifiedStartTimesAreSentToMainThreadDelegate) {
1016 FakeLayerAnimationValueObserver dummy_impl;
1017 scoped_refptr<LayerAnimationController> controller_impl(
1018 LayerAnimationController::Create(0));
1019 controller_impl->AddValueObserver(&dummy_impl);
1020 FakeLayerAnimationValueObserver dummy;
1021 scoped_refptr<LayerAnimationController> controller(
1022 LayerAnimationController::Create(0));
1023 controller->AddValueObserver(&dummy);
1024 FakeAnimationDelegate delegate;
1025 controller->set_layer_animation_delegate(&delegate);
1027 int animation_id =
1028 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
1030 const TimeTicks start_time = TicksFromSecondsF(123);
1031 controller->GetAnimation(Animation::OPACITY)->set_start_time(start_time);
1033 controller->PushAnimationUpdatesTo(controller_impl.get());
1034 controller_impl->ActivateAnimations();
1036 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
1037 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1038 controller_impl->GetAnimationById(animation_id)->run_state());
1040 AnimationEventsVector events;
1041 controller_impl->Animate(kInitialTickTime);
1042 controller_impl->UpdateState(true, &events);
1044 // Synchronize the start times.
1045 EXPECT_EQ(1u, events.size());
1046 controller->NotifyAnimationStarted(events[0]);
1048 // Validate start time on the main thread delegate.
1049 EXPECT_EQ(start_time, delegate.start_time());
1052 class FakeLayerAnimationEventObserver : public LayerAnimationEventObserver {
1053 public:
1054 FakeLayerAnimationEventObserver() : start_time_(base::TimeTicks()) {}
1056 void OnAnimationStarted(const AnimationEvent& event) override {
1057 start_time_ = event.monotonic_time;
1060 TimeTicks start_time() { return start_time_; }
1062 private:
1063 TimeTicks start_time_;
1066 // Tests that specified start times are sent to the event observers
1067 TEST(LayerAnimationControllerTest, SpecifiedStartTimesAreSentToEventObservers) {
1068 FakeLayerAnimationValueObserver dummy_impl;
1069 scoped_refptr<LayerAnimationController> controller_impl(
1070 LayerAnimationController::Create(0));
1071 controller_impl->AddValueObserver(&dummy_impl);
1072 FakeLayerAnimationValueObserver dummy;
1073 scoped_refptr<LayerAnimationController> controller(
1074 LayerAnimationController::Create(0));
1075 controller->AddValueObserver(&dummy);
1076 FakeLayerAnimationEventObserver observer;
1077 controller->AddEventObserver(&observer);
1079 int animation_id =
1080 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
1082 const TimeTicks start_time = TicksFromSecondsF(123);
1083 controller->GetAnimation(Animation::OPACITY)->set_start_time(start_time);
1085 controller->PushAnimationUpdatesTo(controller_impl.get());
1086 controller_impl->ActivateAnimations();
1088 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
1089 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1090 controller_impl->GetAnimationById(animation_id)->run_state());
1092 AnimationEventsVector events;
1093 controller_impl->Animate(kInitialTickTime);
1094 controller_impl->UpdateState(true, &events);
1096 // Synchronize the start times.
1097 EXPECT_EQ(1u, events.size());
1098 controller->NotifyAnimationStarted(events[0]);
1100 // Validate start time on the event observer.
1101 EXPECT_EQ(start_time, observer.start_time());
1104 // Tests animations that are waiting for a synchronized start time do not
1105 // finish.
1106 TEST(LayerAnimationControllerTest,
1107 AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
1108 scoped_ptr<AnimationEventsVector> events(
1109 make_scoped_ptr(new AnimationEventsVector));
1110 FakeLayerAnimationValueObserver dummy;
1111 scoped_refptr<LayerAnimationController> controller(
1112 LayerAnimationController::Create(0));
1113 controller->AddValueObserver(&dummy);
1115 scoped_ptr<Animation> to_add(CreateAnimation(
1116 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1117 1, Animation::OPACITY));
1118 to_add->set_needs_synchronized_start_time(true);
1120 // We should pause at the first keyframe indefinitely waiting for that
1121 // animation to start.
1122 controller->AddAnimation(to_add.Pass());
1123 controller->Animate(kInitialTickTime);
1124 controller->UpdateState(true, events.get());
1125 EXPECT_TRUE(controller->HasActiveAnimation());
1126 EXPECT_EQ(0.f, dummy.opacity());
1127 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1128 controller->UpdateState(true, events.get());
1129 EXPECT_TRUE(controller->HasActiveAnimation());
1130 EXPECT_EQ(0.f, dummy.opacity());
1131 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1132 controller->UpdateState(true, events.get());
1133 EXPECT_TRUE(controller->HasActiveAnimation());
1134 EXPECT_EQ(0.f, dummy.opacity());
1136 // Send the synchronized start time.
1137 controller->NotifyAnimationStarted(
1138 AnimationEvent(AnimationEvent::STARTED, 0, 1, Animation::OPACITY,
1139 kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
1140 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
1141 controller->UpdateState(true, events.get());
1142 EXPECT_EQ(1.f, dummy.opacity());
1143 EXPECT_FALSE(controller->HasActiveAnimation());
1146 // Tests that two queued animations affecting the same property run in sequence.
1147 TEST(LayerAnimationControllerTest, TrivialQueuing) {
1148 scoped_ptr<AnimationEventsVector> events(
1149 make_scoped_ptr(new AnimationEventsVector));
1150 FakeLayerAnimationValueObserver dummy;
1151 scoped_refptr<LayerAnimationController> controller(
1152 LayerAnimationController::Create(0));
1153 controller->AddValueObserver(&dummy);
1155 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1157 controller->AddAnimation(CreateAnimation(
1158 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1159 1, Animation::OPACITY));
1160 controller->AddAnimation(CreateAnimation(
1161 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f))
1162 .Pass(),
1163 2, Animation::OPACITY));
1165 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1167 controller->Animate(kInitialTickTime);
1169 // The second animation still needs to be started.
1170 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1172 controller->UpdateState(true, events.get());
1173 EXPECT_TRUE(controller->HasActiveAnimation());
1174 EXPECT_EQ(0.f, dummy.opacity());
1176 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1177 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1178 controller->UpdateState(true, events.get());
1179 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1181 EXPECT_TRUE(controller->HasActiveAnimation());
1182 EXPECT_EQ(1.f, dummy.opacity());
1183 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1184 controller->UpdateState(true, events.get());
1185 EXPECT_EQ(0.5f, dummy.opacity());
1186 EXPECT_FALSE(controller->HasActiveAnimation());
1189 // Tests interrupting a transition with another transition.
1190 TEST(LayerAnimationControllerTest, Interrupt) {
1191 scoped_ptr<AnimationEventsVector> events(
1192 make_scoped_ptr(new AnimationEventsVector));
1193 FakeLayerAnimationValueObserver dummy;
1194 scoped_refptr<LayerAnimationController> controller(
1195 LayerAnimationController::Create(0));
1196 controller->AddValueObserver(&dummy);
1197 controller->AddAnimation(CreateAnimation(
1198 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1199 1, Animation::OPACITY));
1200 controller->Animate(kInitialTickTime);
1201 controller->UpdateState(true, events.get());
1202 EXPECT_TRUE(controller->HasActiveAnimation());
1203 EXPECT_EQ(0.f, dummy.opacity());
1205 scoped_ptr<Animation> to_add(CreateAnimation(
1206 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f))
1207 .Pass(),
1208 2, Animation::OPACITY));
1209 controller->AbortAnimations(Animation::OPACITY);
1210 controller->AddAnimation(to_add.Pass());
1212 // Since the previous animation was aborted, the new animation should start
1213 // right in this call to animate.
1214 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1215 controller->UpdateState(true, events.get());
1216 EXPECT_TRUE(controller->HasActiveAnimation());
1217 EXPECT_EQ(1.f, dummy.opacity());
1218 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1219 controller->UpdateState(true, events.get());
1220 EXPECT_EQ(0.5f, dummy.opacity());
1221 EXPECT_FALSE(controller->HasActiveAnimation());
1224 // Tests scheduling two animations to run together when only one property is
1225 // free.
1226 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
1227 scoped_ptr<AnimationEventsVector> events(
1228 make_scoped_ptr(new AnimationEventsVector));
1229 FakeLayerAnimationValueObserver dummy;
1230 scoped_refptr<LayerAnimationController> controller(
1231 LayerAnimationController::Create(0));
1232 controller->AddValueObserver(&dummy);
1234 controller->AddAnimation(CreateAnimation(
1235 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1,
1236 Animation::TRANSFORM));
1237 controller->AddAnimation(CreateAnimation(
1238 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 2,
1239 Animation::TRANSFORM));
1240 controller->AddAnimation(CreateAnimation(
1241 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1242 2, Animation::OPACITY));
1244 controller->Animate(kInitialTickTime);
1245 controller->UpdateState(true, events.get());
1246 EXPECT_EQ(0.f, dummy.opacity());
1247 EXPECT_TRUE(controller->HasActiveAnimation());
1248 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1249 controller->UpdateState(true, events.get());
1250 // Should not have started the float transition yet.
1251 EXPECT_TRUE(controller->HasActiveAnimation());
1252 EXPECT_EQ(0.f, dummy.opacity());
1253 // The float animation should have started at time 1 and should be done.
1254 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1255 controller->UpdateState(true, events.get());
1256 EXPECT_EQ(1.f, dummy.opacity());
1257 EXPECT_FALSE(controller->HasActiveAnimation());
1260 // Tests scheduling two animations to run together with different lengths and
1261 // another animation queued to start when the shorter animation finishes (should
1262 // wait for both to finish).
1263 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
1264 scoped_ptr<AnimationEventsVector> events(
1265 make_scoped_ptr(new AnimationEventsVector));
1266 FakeLayerAnimationValueObserver dummy;
1267 scoped_refptr<LayerAnimationController> controller(
1268 LayerAnimationController::Create(0));
1269 controller->AddValueObserver(&dummy);
1271 controller->AddAnimation(CreateAnimation(
1272 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(), 1,
1273 Animation::TRANSFORM));
1274 controller->AddAnimation(CreateAnimation(
1275 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1276 1, Animation::OPACITY));
1277 controller->AddAnimation(CreateAnimation(
1278 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f))
1279 .Pass(),
1280 2, Animation::OPACITY));
1282 // Animations with id 1 should both start now.
1283 controller->Animate(kInitialTickTime);
1284 controller->UpdateState(true, events.get());
1285 EXPECT_TRUE(controller->HasActiveAnimation());
1286 EXPECT_EQ(0.f, dummy.opacity());
1287 // The opacity animation should have finished at time 1, but the group
1288 // of animations with id 1 don't finish until time 2 because of the length
1289 // of the transform animation.
1290 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1291 controller->UpdateState(true, events.get());
1292 // Should not have started the float transition yet.
1293 EXPECT_TRUE(controller->HasActiveAnimation());
1294 EXPECT_EQ(1.f, dummy.opacity());
1296 // The second opacity animation should start at time 2 and should be done by
1297 // time 3.
1298 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1299 controller->UpdateState(true, events.get());
1300 EXPECT_EQ(0.5f, dummy.opacity());
1301 EXPECT_FALSE(controller->HasActiveAnimation());
1304 // Test that a looping animation loops and for the correct number of iterations.
1305 TEST(LayerAnimationControllerTest, TrivialLooping) {
1306 scoped_ptr<AnimationEventsVector> events(
1307 make_scoped_ptr(new AnimationEventsVector));
1308 FakeLayerAnimationValueObserver dummy;
1309 scoped_refptr<LayerAnimationController> controller(
1310 LayerAnimationController::Create(0));
1311 controller->AddValueObserver(&dummy);
1313 scoped_ptr<Animation> to_add(CreateAnimation(
1314 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1315 1, Animation::OPACITY));
1316 to_add->set_iterations(3);
1317 controller->AddAnimation(to_add.Pass());
1319 controller->Animate(kInitialTickTime);
1320 controller->UpdateState(true, events.get());
1321 EXPECT_TRUE(controller->HasActiveAnimation());
1322 EXPECT_EQ(0.f, dummy.opacity());
1323 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1324 controller->UpdateState(true, events.get());
1325 EXPECT_TRUE(controller->HasActiveAnimation());
1326 EXPECT_EQ(0.25f, dummy.opacity());
1327 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1328 controller->UpdateState(true, events.get());
1329 EXPECT_TRUE(controller->HasActiveAnimation());
1330 EXPECT_EQ(0.75f, dummy.opacity());
1331 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
1332 controller->UpdateState(true, events.get());
1333 EXPECT_TRUE(controller->HasActiveAnimation());
1334 EXPECT_EQ(0.25f, dummy.opacity());
1335 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
1336 controller->UpdateState(true, events.get());
1337 EXPECT_TRUE(controller->HasActiveAnimation());
1338 EXPECT_EQ(0.75f, dummy.opacity());
1339 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1340 controller->UpdateState(true, events.get());
1341 EXPECT_FALSE(controller->HasActiveAnimation());
1342 EXPECT_EQ(1.f, dummy.opacity());
1344 // Just be extra sure.
1345 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
1346 controller->UpdateState(true, events.get());
1347 EXPECT_EQ(1.f, dummy.opacity());
1350 // Test that an infinitely looping animation does indeed go until aborted.
1351 TEST(LayerAnimationControllerTest, InfiniteLooping) {
1352 scoped_ptr<AnimationEventsVector> events(
1353 make_scoped_ptr(new AnimationEventsVector));
1354 FakeLayerAnimationValueObserver dummy;
1355 scoped_refptr<LayerAnimationController> controller(
1356 LayerAnimationController::Create(0));
1357 controller->AddValueObserver(&dummy);
1359 scoped_ptr<Animation> to_add(CreateAnimation(
1360 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1361 1, Animation::OPACITY));
1362 to_add->set_iterations(-1);
1363 controller->AddAnimation(to_add.Pass());
1365 controller->Animate(kInitialTickTime);
1366 controller->UpdateState(true, events.get());
1367 EXPECT_TRUE(controller->HasActiveAnimation());
1368 EXPECT_EQ(0.f, dummy.opacity());
1369 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1370 controller->UpdateState(true, events.get());
1371 EXPECT_TRUE(controller->HasActiveAnimation());
1372 EXPECT_EQ(0.25f, dummy.opacity());
1373 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1374 controller->UpdateState(true, events.get());
1375 EXPECT_TRUE(controller->HasActiveAnimation());
1376 EXPECT_EQ(0.75f, dummy.opacity());
1378 controller->Animate(kInitialTickTime +
1379 TimeDelta::FromMilliseconds(1073741824250));
1380 controller->UpdateState(true, events.get());
1381 EXPECT_TRUE(controller->HasActiveAnimation());
1382 EXPECT_EQ(0.25f, dummy.opacity());
1383 controller->Animate(kInitialTickTime +
1384 TimeDelta::FromMilliseconds(1073741824750));
1385 controller->UpdateState(true, events.get());
1386 EXPECT_TRUE(controller->HasActiveAnimation());
1387 EXPECT_EQ(0.75f, dummy.opacity());
1389 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
1390 controller->GetAnimation(Animation::OPACITY)
1391 ->SetRunState(Animation::ABORTED,
1392 kInitialTickTime + TimeDelta::FromMilliseconds(750));
1393 EXPECT_FALSE(controller->HasActiveAnimation());
1394 EXPECT_EQ(0.75f, dummy.opacity());
1397 // Test that pausing and resuming work as expected.
1398 TEST(LayerAnimationControllerTest, PauseResume) {
1399 scoped_ptr<AnimationEventsVector> events(
1400 make_scoped_ptr(new AnimationEventsVector));
1401 FakeLayerAnimationValueObserver dummy;
1402 scoped_refptr<LayerAnimationController> controller(
1403 LayerAnimationController::Create(0));
1404 controller->AddValueObserver(&dummy);
1406 controller->AddAnimation(CreateAnimation(
1407 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1408 1, Animation::OPACITY));
1410 controller->Animate(kInitialTickTime);
1411 controller->UpdateState(true, events.get());
1412 EXPECT_TRUE(controller->HasActiveAnimation());
1413 EXPECT_EQ(0.f, dummy.opacity());
1414 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1415 controller->UpdateState(true, events.get());
1416 EXPECT_TRUE(controller->HasActiveAnimation());
1417 EXPECT_EQ(0.5f, dummy.opacity());
1419 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
1420 controller->GetAnimation(Animation::OPACITY)
1421 ->SetRunState(Animation::PAUSED,
1422 kInitialTickTime + TimeDelta::FromMilliseconds(500));
1424 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1425 controller->UpdateState(true, events.get());
1426 EXPECT_TRUE(controller->HasActiveAnimation());
1427 EXPECT_EQ(0.5f, dummy.opacity());
1429 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
1430 controller->GetAnimation(Animation::OPACITY)
1431 ->SetRunState(Animation::RUNNING,
1432 kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1433 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
1434 controller->UpdateState(true, events.get());
1435 EXPECT_TRUE(controller->HasActiveAnimation());
1436 EXPECT_EQ(0.75f, dummy.opacity());
1438 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
1439 controller->UpdateState(true, events.get());
1440 EXPECT_FALSE(controller->HasActiveAnimation());
1441 EXPECT_EQ(1.f, dummy.opacity());
1444 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1445 scoped_ptr<AnimationEventsVector> events(
1446 make_scoped_ptr(new AnimationEventsVector));
1447 FakeLayerAnimationValueObserver dummy;
1448 scoped_refptr<LayerAnimationController> controller(
1449 LayerAnimationController::Create(0));
1450 controller->AddValueObserver(&dummy);
1452 const int animation_id = 2;
1453 controller->AddAnimation(Animation::Create(
1454 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1, 1,
1455 Animation::TRANSFORM));
1456 controller->AddAnimation(Animation::Create(
1457 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1458 animation_id, 1, Animation::OPACITY));
1459 controller->AddAnimation(Animation::Create(
1460 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.75f))
1461 .Pass(),
1462 3, 2, Animation::OPACITY));
1464 controller->Animate(kInitialTickTime);
1465 controller->UpdateState(true, events.get());
1466 EXPECT_TRUE(controller->HasActiveAnimation());
1467 EXPECT_EQ(0.f, dummy.opacity());
1468 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1469 controller->UpdateState(true, events.get());
1470 EXPECT_TRUE(controller->HasActiveAnimation());
1471 EXPECT_EQ(0.5f, dummy.opacity());
1473 EXPECT_TRUE(controller->GetAnimationById(animation_id));
1474 controller->GetAnimationById(animation_id)
1475 ->SetRunState(Animation::ABORTED,
1476 kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1477 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1478 controller->UpdateState(true, events.get());
1479 EXPECT_TRUE(controller->HasActiveAnimation());
1480 EXPECT_EQ(1.f, dummy.opacity());
1481 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1482 controller->UpdateState(true, events.get());
1483 EXPECT_TRUE(!controller->HasActiveAnimation());
1484 EXPECT_EQ(0.75f, dummy.opacity());
1487 TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1488 FakeLayerAnimationValueObserver dummy_impl;
1489 scoped_refptr<LayerAnimationController> controller_impl(
1490 LayerAnimationController::Create(0));
1491 controller_impl->AddValueObserver(&dummy_impl);
1492 scoped_ptr<AnimationEventsVector> events(
1493 make_scoped_ptr(new AnimationEventsVector));
1494 FakeLayerAnimationValueObserver dummy;
1495 scoped_refptr<LayerAnimationController> controller(
1496 LayerAnimationController::Create(0));
1497 controller->AddValueObserver(&dummy);
1499 scoped_ptr<Animation> to_add(CreateAnimation(
1500 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1501 0, Animation::OPACITY));
1502 to_add->set_needs_synchronized_start_time(true);
1503 controller->AddAnimation(to_add.Pass());
1505 controller->Animate(kInitialTickTime);
1506 controller->UpdateState(true, events.get());
1507 EXPECT_TRUE(controller->HasActiveAnimation());
1508 Animation* active_animation = controller->GetAnimation(Animation::OPACITY);
1509 EXPECT_TRUE(active_animation);
1510 EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1512 controller->PushAnimationUpdatesTo(controller_impl.get());
1513 controller_impl->ActivateAnimations();
1515 active_animation = controller_impl->GetAnimation(Animation::OPACITY);
1516 EXPECT_TRUE(active_animation);
1517 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1518 active_animation->run_state());
1521 // Tests that skipping a call to UpdateState works as expected.
1522 TEST(LayerAnimationControllerTest, SkipUpdateState) {
1523 scoped_ptr<AnimationEventsVector> events(
1524 make_scoped_ptr(new AnimationEventsVector));
1525 FakeLayerAnimationValueObserver dummy;
1526 scoped_refptr<LayerAnimationController> controller(
1527 LayerAnimationController::Create(0));
1528 controller->AddValueObserver(&dummy);
1530 scoped_ptr<Animation> first_animation(CreateAnimation(
1531 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1,
1532 Animation::TRANSFORM));
1533 first_animation->set_is_controlling_instance_for_test(true);
1534 controller->AddAnimation(first_animation.Pass());
1536 controller->Animate(kInitialTickTime);
1537 controller->UpdateState(true, events.get());
1539 scoped_ptr<Animation> second_animation(CreateAnimation(
1540 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1541 2, Animation::OPACITY));
1542 second_animation->set_is_controlling_instance_for_test(true);
1543 controller->AddAnimation(second_animation.Pass());
1545 // Animate but don't UpdateState.
1546 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1548 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1549 events.reset(new AnimationEventsVector);
1550 controller->UpdateState(true, events.get());
1552 // Should have one STARTED event and one FINISHED event.
1553 EXPECT_EQ(2u, events->size());
1554 EXPECT_NE((*events)[0].type, (*events)[1].type);
1556 // The float transition should still be at its starting point.
1557 EXPECT_TRUE(controller->HasActiveAnimation());
1558 EXPECT_EQ(0.f, dummy.opacity());
1560 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1561 controller->UpdateState(true, events.get());
1563 // The float tranisition should now be done.
1564 EXPECT_EQ(1.f, dummy.opacity());
1565 EXPECT_FALSE(controller->HasActiveAnimation());
1568 // Tests that an animation controller with only a pending observer gets ticked
1569 // but doesn't progress animations past the STARTING state.
1570 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1571 scoped_ptr<AnimationEventsVector> events(
1572 make_scoped_ptr(new AnimationEventsVector));
1573 FakeLayerAnimationValueObserver dummy;
1574 FakeInactiveLayerAnimationValueObserver pending_dummy;
1575 scoped_refptr<LayerAnimationController> controller(
1576 LayerAnimationController::Create(0));
1578 const int id = 1;
1579 controller->AddAnimation(CreateAnimation(
1580 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.5f, 1.f))
1581 .Pass(),
1582 id, Animation::OPACITY));
1584 // Without an observer, the animation shouldn't progress to the STARTING
1585 // state.
1586 controller->Animate(kInitialTickTime);
1587 controller->UpdateState(true, events.get());
1588 EXPECT_EQ(0u, events->size());
1589 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1590 controller->GetAnimation(Animation::OPACITY)->run_state());
1592 controller->AddValueObserver(&pending_dummy);
1594 // With only a pending observer, the animation should progress to the
1595 // STARTING state and get ticked at its starting point, but should not
1596 // progress to RUNNING.
1597 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1598 controller->UpdateState(true, events.get());
1599 EXPECT_EQ(0u, events->size());
1600 EXPECT_EQ(Animation::STARTING,
1601 controller->GetAnimation(Animation::OPACITY)->run_state());
1602 EXPECT_EQ(0.5f, pending_dummy.opacity());
1604 // Even when already in the STARTING state, the animation should stay
1605 // there, and shouldn't be ticked past its starting point.
1606 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1607 controller->UpdateState(true, events.get());
1608 EXPECT_EQ(0u, events->size());
1609 EXPECT_EQ(Animation::STARTING,
1610 controller->GetAnimation(Animation::OPACITY)->run_state());
1611 EXPECT_EQ(0.5f, pending_dummy.opacity());
1613 controller->AddValueObserver(&dummy);
1615 // Now that an active observer has been added, the animation should still
1616 // initially tick at its starting point, but should now progress to RUNNING.
1617 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1618 controller->UpdateState(true, events.get());
1619 EXPECT_EQ(1u, events->size());
1620 EXPECT_EQ(Animation::RUNNING,
1621 controller->GetAnimation(Animation::OPACITY)->run_state());
1622 EXPECT_EQ(0.5f, pending_dummy.opacity());
1623 EXPECT_EQ(0.5f, dummy.opacity());
1625 // The animation should now tick past its starting point.
1626 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
1627 EXPECT_NE(0.5f, pending_dummy.opacity());
1628 EXPECT_NE(0.5f, dummy.opacity());
1631 TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
1632 scoped_refptr<LayerAnimationController> controller_impl(
1633 LayerAnimationController::Create(0));
1635 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1636 KeyframedTransformAnimationCurve::Create());
1638 TransformOperations operations1;
1639 curve1->AddKeyframe(
1640 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1641 operations1.AppendTranslate(10.0, 15.0, 0.0);
1642 curve1->AddKeyframe(TransformKeyframe::Create(
1643 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1645 scoped_ptr<Animation> animation(
1646 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
1647 controller_impl->AddAnimation(animation.Pass());
1649 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1650 KeyframedTransformAnimationCurve::Create());
1652 TransformOperations operations2;
1653 curve2->AddKeyframe(
1654 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1655 operations2.AppendScale(2.0, 3.0, 4.0);
1656 curve2->AddKeyframe(TransformKeyframe::Create(
1657 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1659 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::TRANSFORM);
1660 controller_impl->AddAnimation(animation.Pass());
1662 gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1663 gfx::BoxF bounds;
1665 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1666 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1667 bounds.ToString());
1669 controller_impl->GetAnimationById(1)
1670 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1672 // Only the unfinished animation should affect the animated bounds.
1673 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1674 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1675 bounds.ToString());
1677 controller_impl->GetAnimationById(2)
1678 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1680 // There are no longer any running animations.
1681 EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
1683 // Add an animation whose bounds we don't yet support computing.
1684 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1685 KeyframedTransformAnimationCurve::Create());
1686 TransformOperations operations3;
1687 gfx::Transform transform3;
1688 transform3.Scale3d(1.0, 2.0, 3.0);
1689 curve3->AddKeyframe(
1690 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
1691 operations3.AppendMatrix(transform3);
1692 curve3->AddKeyframe(TransformKeyframe::Create(
1693 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
1694 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::TRANSFORM);
1695 controller_impl->AddAnimation(animation.Pass());
1696 EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1699 // Tests that AbortAnimations aborts all animations targeting the specified
1700 // property.
1701 TEST(LayerAnimationControllerTest, AbortAnimations) {
1702 FakeLayerAnimationValueObserver dummy;
1703 scoped_refptr<LayerAnimationController> controller(
1704 LayerAnimationController::Create(0));
1705 controller->AddValueObserver(&dummy);
1707 // Start with several animations, and allow some of them to reach the finished
1708 // state.
1709 controller->AddAnimation(Animation::Create(
1710 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 1, 1,
1711 Animation::TRANSFORM));
1712 controller->AddAnimation(Animation::Create(
1713 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1714 2, 2, Animation::OPACITY));
1715 controller->AddAnimation(Animation::Create(
1716 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 3, 3,
1717 Animation::TRANSFORM));
1718 controller->AddAnimation(Animation::Create(
1719 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(), 4, 4,
1720 Animation::TRANSFORM));
1721 controller->AddAnimation(Animation::Create(
1722 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1723 5, 5, Animation::OPACITY));
1725 controller->Animate(kInitialTickTime);
1726 controller->UpdateState(true, nullptr);
1727 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1728 controller->UpdateState(true, nullptr);
1730 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(1)->run_state());
1731 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(2)->run_state());
1732 EXPECT_EQ(Animation::RUNNING, controller->GetAnimationById(3)->run_state());
1733 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1734 controller->GetAnimationById(4)->run_state());
1735 EXPECT_EQ(Animation::RUNNING, controller->GetAnimationById(5)->run_state());
1737 controller->AbortAnimations(Animation::TRANSFORM);
1739 // Only un-finished TRANSFORM animations should have been aborted.
1740 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(1)->run_state());
1741 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(2)->run_state());
1742 EXPECT_EQ(Animation::ABORTED, controller->GetAnimationById(3)->run_state());
1743 EXPECT_EQ(Animation::ABORTED, controller->GetAnimationById(4)->run_state());
1744 EXPECT_EQ(Animation::RUNNING, controller->GetAnimationById(5)->run_state());
1747 // An animation aborted on the main thread should get deleted on both threads.
1748 TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
1749 FakeLayerAnimationValueObserver dummy_impl;
1750 scoped_refptr<LayerAnimationController> controller_impl(
1751 LayerAnimationController::Create(0));
1752 controller_impl->AddValueObserver(&dummy_impl);
1753 FakeLayerAnimationValueObserver dummy;
1754 scoped_refptr<LayerAnimationController> controller(
1755 LayerAnimationController::Create(0));
1756 controller->AddValueObserver(&dummy);
1758 int animation_id =
1759 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1761 controller->PushAnimationUpdatesTo(controller_impl.get());
1762 controller_impl->ActivateAnimations();
1763 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
1765 controller->AbortAnimations(Animation::OPACITY);
1766 EXPECT_EQ(Animation::ABORTED,
1767 controller->GetAnimation(Animation::OPACITY)->run_state());
1768 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1769 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1771 controller->Animate(kInitialTickTime);
1772 controller->UpdateState(true, nullptr);
1773 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1774 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
1775 controller->GetAnimation(Animation::OPACITY)->run_state());
1777 controller->PushAnimationUpdatesTo(controller_impl.get());
1778 controller_impl->ActivateAnimations();
1779 EXPECT_FALSE(controller->GetAnimationById(animation_id));
1780 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
1783 // An animation aborted on the impl thread should get deleted on both threads.
1784 TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
1785 FakeLayerAnimationValueObserver dummy_impl;
1786 scoped_refptr<LayerAnimationController> controller_impl(
1787 LayerAnimationController::Create(0));
1788 controller_impl->AddValueObserver(&dummy_impl);
1789 FakeLayerAnimationValueObserver dummy;
1790 scoped_refptr<LayerAnimationController> controller(
1791 LayerAnimationController::Create(0));
1792 controller->AddValueObserver(&dummy);
1794 int animation_id =
1795 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1797 controller->PushAnimationUpdatesTo(controller_impl.get());
1798 controller_impl->ActivateAnimations();
1799 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
1801 controller_impl->AbortAnimations(Animation::OPACITY);
1802 EXPECT_EQ(Animation::ABORTED,
1803 controller_impl->GetAnimation(Animation::OPACITY)->run_state());
1804 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1805 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1807 AnimationEventsVector events;
1808 controller_impl->Animate(kInitialTickTime);
1809 controller_impl->UpdateState(true, &events);
1810 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
1811 EXPECT_EQ(1u, events.size());
1812 EXPECT_EQ(AnimationEvent::ABORTED, events[0].type);
1813 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
1814 controller_impl->GetAnimation(Animation::OPACITY)->run_state());
1816 controller->NotifyAnimationAborted(events[0]);
1817 EXPECT_EQ(Animation::ABORTED,
1818 controller->GetAnimation(Animation::OPACITY)->run_state());
1820 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1821 controller->UpdateState(true, nullptr);
1822 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1823 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
1824 controller->GetAnimation(Animation::OPACITY)->run_state());
1826 controller->PushAnimationUpdatesTo(controller_impl.get());
1827 controller_impl->ActivateAnimations();
1828 EXPECT_FALSE(controller->GetAnimationById(animation_id));
1829 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
1832 // Ensure that we only generate FINISHED events for animations in a group
1833 // once all animations in that group are finished.
1834 TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
1835 scoped_ptr<AnimationEventsVector> events(
1836 make_scoped_ptr(new AnimationEventsVector));
1837 FakeLayerAnimationValueObserver dummy_impl;
1838 scoped_refptr<LayerAnimationController> controller_impl(
1839 LayerAnimationController::Create(0));
1840 controller_impl->AddValueObserver(&dummy_impl);
1842 const int group_id = 1;
1844 // Add two animations with the same group id but different durations.
1845 scoped_ptr<Animation> first_animation(Animation::Create(
1846 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(), 1,
1847 group_id, Animation::TRANSFORM));
1848 first_animation->set_is_controlling_instance_for_test(true);
1849 controller_impl->AddAnimation(first_animation.Pass());
1851 scoped_ptr<Animation> second_animation(Animation::Create(
1852 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1853 2, group_id, Animation::OPACITY));
1854 second_animation->set_is_controlling_instance_for_test(true);
1855 controller_impl->AddAnimation(second_animation.Pass());
1857 controller_impl->Animate(kInitialTickTime);
1858 controller_impl->UpdateState(true, events.get());
1860 // Both animations should have started.
1861 EXPECT_EQ(2u, events->size());
1862 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
1863 EXPECT_EQ(AnimationEvent::STARTED, (*events)[1].type);
1865 events.reset(new AnimationEventsVector);
1866 controller_impl->Animate(kInitialTickTime +
1867 TimeDelta::FromMilliseconds(1000));
1868 controller_impl->UpdateState(true, events.get());
1870 // The opacity animation should be finished, but should not have generated
1871 // a FINISHED event yet.
1872 EXPECT_EQ(0u, events->size());
1873 EXPECT_EQ(Animation::FINISHED,
1874 controller_impl->GetAnimationById(2)->run_state());
1875 EXPECT_EQ(Animation::RUNNING,
1876 controller_impl->GetAnimationById(1)->run_state());
1878 controller_impl->Animate(kInitialTickTime +
1879 TimeDelta::FromMilliseconds(2000));
1880 controller_impl->UpdateState(true, events.get());
1882 // Both animations should have generated FINISHED events.
1883 EXPECT_EQ(2u, events->size());
1884 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
1885 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[1].type);
1888 // Ensure that when a group has a mix of aborted and finished animations,
1889 // we generate a FINISHED event for the finished animation and an ABORTED
1890 // event for the aborted animation.
1891 TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
1892 scoped_ptr<AnimationEventsVector> events(
1893 make_scoped_ptr(new AnimationEventsVector));
1894 FakeLayerAnimationValueObserver dummy_impl;
1895 scoped_refptr<LayerAnimationController> controller_impl(
1896 LayerAnimationController::Create(0));
1897 controller_impl->AddValueObserver(&dummy_impl);
1899 // Add two animations with the same group id.
1900 scoped_ptr<Animation> first_animation(CreateAnimation(
1901 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 1,
1902 Animation::TRANSFORM));
1903 first_animation->set_is_controlling_instance_for_test(true);
1904 controller_impl->AddAnimation(first_animation.Pass());
1906 scoped_ptr<Animation> second_animation(CreateAnimation(
1907 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1908 1, Animation::OPACITY));
1909 second_animation->set_is_controlling_instance_for_test(true);
1910 controller_impl->AddAnimation(second_animation.Pass());
1912 controller_impl->Animate(kInitialTickTime);
1913 controller_impl->UpdateState(true, events.get());
1915 // Both animations should have started.
1916 EXPECT_EQ(2u, events->size());
1917 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
1918 EXPECT_EQ(AnimationEvent::STARTED, (*events)[1].type);
1920 controller_impl->AbortAnimations(Animation::OPACITY);
1922 events.reset(new AnimationEventsVector);
1923 controller_impl->Animate(kInitialTickTime +
1924 TimeDelta::FromMilliseconds(1000));
1925 controller_impl->UpdateState(true, events.get());
1927 // We should have exactly 2 events: a FINISHED event for the tranform
1928 // animation, and an ABORTED event for the opacity animation.
1929 EXPECT_EQ(2u, events->size());
1930 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
1931 EXPECT_EQ(Animation::TRANSFORM, (*events)[0].target_property);
1932 EXPECT_EQ(AnimationEvent::ABORTED, (*events)[1].type);
1933 EXPECT_EQ(Animation::OPACITY, (*events)[1].target_property);
1936 TEST(LayerAnimationControllerTest, HasAnimationThatAffectsScale) {
1937 scoped_refptr<LayerAnimationController> controller_impl(
1938 LayerAnimationController::Create(0));
1940 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1942 controller_impl->AddAnimation(CreateAnimation(
1943 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1944 1, Animation::OPACITY));
1946 // Opacity animations don't affect scale.
1947 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1949 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1950 KeyframedTransformAnimationCurve::Create());
1952 TransformOperations operations1;
1953 curve1->AddKeyframe(
1954 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1955 operations1.AppendTranslate(10.0, 15.0, 0.0);
1956 curve1->AddKeyframe(TransformKeyframe::Create(
1957 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1959 scoped_ptr<Animation> animation(
1960 Animation::Create(curve1.Pass(), 2, 2, Animation::TRANSFORM));
1961 controller_impl->AddAnimation(animation.Pass());
1963 // Translations don't affect scale.
1964 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1966 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1967 KeyframedTransformAnimationCurve::Create());
1969 TransformOperations operations2;
1970 curve2->AddKeyframe(
1971 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1972 operations2.AppendScale(2.0, 3.0, 4.0);
1973 curve2->AddKeyframe(TransformKeyframe::Create(
1974 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1976 animation = Animation::Create(curve2.Pass(), 3, 3, Animation::TRANSFORM);
1977 controller_impl->AddAnimation(animation.Pass());
1979 EXPECT_TRUE(controller_impl->HasAnimationThatAffectsScale());
1981 controller_impl->GetAnimationById(3)
1982 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1984 // Only unfinished animations should be considered by
1985 // HasAnimationThatAffectsScale.
1986 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1989 TEST(LayerAnimationControllerTest, HasOnlyTranslationTransforms) {
1990 scoped_refptr<LayerAnimationController> controller_impl(
1991 LayerAnimationController::Create(0));
1993 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1995 controller_impl->AddAnimation(CreateAnimation(
1996 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1997 1, Animation::OPACITY));
1999 // Opacity animations aren't non-translation transforms.
2000 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
2002 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
2003 KeyframedTransformAnimationCurve::Create());
2005 TransformOperations operations1;
2006 curve1->AddKeyframe(
2007 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2008 operations1.AppendTranslate(10.0, 15.0, 0.0);
2009 curve1->AddKeyframe(TransformKeyframe::Create(
2010 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
2012 scoped_ptr<Animation> animation(
2013 Animation::Create(curve1.Pass(), 2, 2, Animation::TRANSFORM));
2014 controller_impl->AddAnimation(animation.Pass());
2016 // The only transform animation we've added is a translation.
2017 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
2019 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
2020 KeyframedTransformAnimationCurve::Create());
2022 TransformOperations operations2;
2023 curve2->AddKeyframe(
2024 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
2025 operations2.AppendScale(2.0, 3.0, 4.0);
2026 curve2->AddKeyframe(TransformKeyframe::Create(
2027 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
2029 animation = Animation::Create(curve2.Pass(), 3, 3, Animation::TRANSFORM);
2030 controller_impl->AddAnimation(animation.Pass());
2032 // A scale animation is not a translation.
2033 EXPECT_FALSE(controller_impl->HasOnlyTranslationTransforms());
2035 controller_impl->GetAnimationById(3)
2036 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
2038 // Only unfinished animations should be considered by
2039 // HasOnlyTranslationTransforms.
2040 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
2043 TEST(LayerAnimationControllerTest, AnimationStartScale) {
2044 scoped_refptr<LayerAnimationController> controller_impl(
2045 LayerAnimationController::Create(0));
2046 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
2047 KeyframedTransformAnimationCurve::Create());
2049 TransformOperations operations1;
2050 operations1.AppendScale(2.0, 3.0, 4.0);
2051 curve1->AddKeyframe(
2052 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2053 TransformOperations operations2;
2054 curve1->AddKeyframe(TransformKeyframe::Create(
2055 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
2056 scoped_ptr<Animation> animation(
2057 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
2058 controller_impl->AddAnimation(animation.Pass());
2060 float start_scale = 0.f;
2061 EXPECT_TRUE(controller_impl->AnimationStartScale(&start_scale));
2062 EXPECT_EQ(4.f, start_scale);
2064 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
2065 KeyframedTransformAnimationCurve::Create());
2067 TransformOperations operations3;
2068 curve2->AddKeyframe(
2069 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
2070 operations3.AppendScale(6.0, 5.0, 4.0);
2071 curve2->AddKeyframe(TransformKeyframe::Create(
2072 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
2074 controller_impl->RemoveAnimation(1);
2075 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::TRANSFORM);
2077 // Reverse Direction
2078 animation->set_direction(Animation::DIRECTION_REVERSE);
2079 controller_impl->AddAnimation(animation.Pass());
2081 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
2082 KeyframedTransformAnimationCurve::Create());
2084 TransformOperations operations4;
2085 operations4.AppendScale(5.0, 3.0, 1.0);
2086 curve3->AddKeyframe(
2087 TransformKeyframe::Create(base::TimeDelta(), operations4, nullptr));
2088 TransformOperations operations5;
2089 curve3->AddKeyframe(TransformKeyframe::Create(
2090 base::TimeDelta::FromSecondsD(1.0), operations5, nullptr));
2092 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::TRANSFORM);
2093 controller_impl->AddAnimation(animation.Pass());
2095 EXPECT_TRUE(controller_impl->AnimationStartScale(&start_scale));
2096 EXPECT_EQ(6.f, start_scale);
2098 controller_impl->GetAnimationById(2)
2099 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
2101 // Only unfinished animations should be considered by
2102 // AnimationStartScale.
2103 EXPECT_TRUE(controller_impl->AnimationStartScale(&start_scale));
2104 EXPECT_EQ(5.f, start_scale);
2107 TEST(LayerAnimationControllerTest, MaximumTargetScale) {
2108 scoped_refptr<LayerAnimationController> controller_impl(
2109 LayerAnimationController::Create(0));
2111 float max_scale = 0.f;
2112 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2113 EXPECT_EQ(0.f, max_scale);
2115 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
2116 KeyframedTransformAnimationCurve::Create());
2118 TransformOperations operations1;
2119 curve1->AddKeyframe(
2120 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2121 operations1.AppendScale(2.0, 3.0, 4.0);
2122 curve1->AddKeyframe(TransformKeyframe::Create(
2123 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
2125 scoped_ptr<Animation> animation(
2126 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
2127 controller_impl->AddAnimation(animation.Pass());
2129 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2130 EXPECT_EQ(4.f, max_scale);
2132 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
2133 KeyframedTransformAnimationCurve::Create());
2135 TransformOperations operations2;
2136 curve2->AddKeyframe(
2137 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
2138 operations2.AppendScale(6.0, 5.0, 4.0);
2139 curve2->AddKeyframe(TransformKeyframe::Create(
2140 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
2142 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::TRANSFORM);
2143 controller_impl->AddAnimation(animation.Pass());
2145 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2146 EXPECT_EQ(6.f, max_scale);
2148 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
2149 KeyframedTransformAnimationCurve::Create());
2151 TransformOperations operations3;
2152 curve3->AddKeyframe(
2153 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
2154 operations3.AppendPerspective(6.0);
2155 curve3->AddKeyframe(TransformKeyframe::Create(
2156 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
2158 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::TRANSFORM);
2159 controller_impl->AddAnimation(animation.Pass());
2161 EXPECT_FALSE(controller_impl->MaximumTargetScale(&max_scale));
2163 controller_impl->GetAnimationById(3)
2164 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
2165 controller_impl->GetAnimationById(2)
2166 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
2168 // Only unfinished animations should be considered by
2169 // MaximumTargetScale.
2170 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2171 EXPECT_EQ(4.f, max_scale);
2174 TEST(LayerAnimationControllerTest, MaximumTargetScaleWithDirection) {
2175 scoped_refptr<LayerAnimationController> controller_impl(
2176 LayerAnimationController::Create(0));
2178 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
2179 KeyframedTransformAnimationCurve::Create());
2180 TransformOperations operations1;
2181 operations1.AppendScale(1.0, 2.0, 3.0);
2182 curve1->AddKeyframe(
2183 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2184 TransformOperations operations2;
2185 operations2.AppendScale(4.0, 5.0, 6.0);
2186 curve1->AddKeyframe(TransformKeyframe::Create(
2187 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
2189 scoped_ptr<Animation> animation_owned(
2190 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
2191 Animation* animation = animation_owned.get();
2192 controller_impl->AddAnimation(animation_owned.Pass());
2194 float max_scale = 0.f;
2196 EXPECT_GT(animation->playback_rate(), 0.0);
2198 // NORMAL direction with positive playback rate.
2199 animation->set_direction(Animation::DIRECTION_NORMAL);
2200 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2201 EXPECT_EQ(6.f, max_scale);
2203 // ALTERNATE direction with positive playback rate.
2204 animation->set_direction(Animation::DIRECTION_ALTERNATE);
2205 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2206 EXPECT_EQ(6.f, max_scale);
2208 // REVERSE direction with positive playback rate.
2209 animation->set_direction(Animation::DIRECTION_REVERSE);
2210 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2211 EXPECT_EQ(3.f, max_scale);
2213 // ALTERNATE reverse direction.
2214 animation->set_direction(Animation::DIRECTION_REVERSE);
2215 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2216 EXPECT_EQ(3.f, max_scale);
2218 animation->set_playback_rate(-1.0);
2220 // NORMAL direction with negative playback rate.
2221 animation->set_direction(Animation::DIRECTION_NORMAL);
2222 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2223 EXPECT_EQ(3.f, max_scale);
2225 // ALTERNATE direction with negative playback rate.
2226 animation->set_direction(Animation::DIRECTION_ALTERNATE);
2227 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2228 EXPECT_EQ(3.f, max_scale);
2230 // REVERSE direction with negative playback rate.
2231 animation->set_direction(Animation::DIRECTION_REVERSE);
2232 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2233 EXPECT_EQ(6.f, max_scale);
2235 // ALTERNATE reverse direction with negative playback rate.
2236 animation->set_direction(Animation::DIRECTION_REVERSE);
2237 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2238 EXPECT_EQ(6.f, max_scale);
2241 TEST(LayerAnimationControllerTest, NewlyPushedAnimationWaitsForActivation) {
2242 scoped_ptr<AnimationEventsVector> events(
2243 make_scoped_ptr(new AnimationEventsVector));
2244 FakeLayerAnimationValueObserver dummy_impl;
2245 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2246 scoped_refptr<LayerAnimationController> controller_impl(
2247 LayerAnimationController::Create(0));
2248 controller_impl->AddValueObserver(&dummy_impl);
2249 controller_impl->AddValueObserver(&pending_dummy_impl);
2250 FakeLayerAnimationValueObserver dummy;
2251 scoped_refptr<LayerAnimationController> controller(
2252 LayerAnimationController::Create(0));
2253 controller->AddValueObserver(&dummy);
2255 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
2256 int animation_id =
2257 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, false);
2258 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
2260 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2261 controller->PushAnimationUpdatesTo(controller_impl.get());
2262 EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
2264 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
2265 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
2266 controller_impl->GetAnimationById(animation_id)->run_state());
2267 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2268 ->affects_pending_observers());
2269 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id)
2270 ->affects_active_observers());
2272 controller_impl->Animate(kInitialTickTime);
2273 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2274 controller_impl->UpdateState(true, events.get());
2276 // Since the animation hasn't been activated, it should still be STARTING
2277 // rather than RUNNING.
2278 EXPECT_EQ(Animation::STARTING,
2279 controller_impl->GetAnimationById(animation_id)->run_state());
2281 // Since the animation hasn't been activated, only the pending observer
2282 // should have been ticked.
2283 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2284 EXPECT_EQ(0.f, dummy_impl.opacity());
2286 controller_impl->ActivateAnimations();
2287 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2288 ->affects_pending_observers());
2289 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2290 ->affects_active_observers());
2292 controller_impl->Animate(kInitialTickTime +
2293 TimeDelta::FromMilliseconds(1000));
2294 controller_impl->UpdateState(true, events.get());
2296 // Since the animation has been activated, it should have reached the
2297 // RUNNING state and the active observer should start to get ticked.
2298 EXPECT_EQ(Animation::RUNNING,
2299 controller_impl->GetAnimationById(animation_id)->run_state());
2300 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2301 EXPECT_EQ(0.5f, dummy_impl.opacity());
2304 TEST(LayerAnimationControllerTest, ActivationBetweenAnimateAndUpdateState) {
2305 scoped_ptr<AnimationEventsVector> events(
2306 make_scoped_ptr(new AnimationEventsVector));
2307 FakeLayerAnimationValueObserver dummy_impl;
2308 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2309 scoped_refptr<LayerAnimationController> controller_impl(
2310 LayerAnimationController::Create(0));
2311 controller_impl->AddValueObserver(&dummy_impl);
2312 controller_impl->AddValueObserver(&pending_dummy_impl);
2313 FakeLayerAnimationValueObserver dummy;
2314 scoped_refptr<LayerAnimationController> controller(
2315 LayerAnimationController::Create(0));
2316 controller->AddValueObserver(&dummy);
2318 int animation_id =
2319 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2321 controller->PushAnimationUpdatesTo(controller_impl.get());
2323 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
2324 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
2325 controller_impl->GetAnimationById(animation_id)->run_state());
2326 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2327 ->affects_pending_observers());
2328 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id)
2329 ->affects_active_observers());
2331 controller_impl->Animate(kInitialTickTime);
2333 // Since the animation hasn't been activated, only the pending observer
2334 // should have been ticked.
2335 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2336 EXPECT_EQ(0.f, dummy_impl.opacity());
2338 controller_impl->ActivateAnimations();
2339 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2340 ->affects_pending_observers());
2341 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2342 ->affects_active_observers());
2344 controller_impl->UpdateState(true, events.get());
2346 // Since the animation has been activated, it should have reached the
2347 // RUNNING state.
2348 EXPECT_EQ(Animation::RUNNING,
2349 controller_impl->GetAnimationById(animation_id)->run_state());
2351 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2353 // Both observers should have been ticked.
2354 EXPECT_EQ(0.75f, pending_dummy_impl.opacity());
2355 EXPECT_EQ(0.75f, dummy_impl.opacity());
2358 TEST(LayerAnimationControllerTest, ClippedOpacityValues) {
2359 FakeLayerAnimationValueObserver dummy;
2360 scoped_refptr<LayerAnimationController> controller(
2361 LayerAnimationController::Create(0));
2362 controller->AddValueObserver(&dummy);
2364 AddOpacityTransitionToController(controller.get(), 1, 1.f, 2.f, true);
2366 controller->Animate(kInitialTickTime);
2367 EXPECT_EQ(1.f, dummy.opacity());
2369 // Opacity values are clipped [0,1]
2370 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2371 EXPECT_EQ(1.f, dummy.opacity());
2374 TEST(LayerAnimationControllerTest, ClippedNegativeOpacityValues) {
2375 FakeLayerAnimationValueObserver dummy;
2376 scoped_refptr<LayerAnimationController> controller(
2377 LayerAnimationController::Create(0));
2378 controller->AddValueObserver(&dummy);
2380 AddOpacityTransitionToController(controller.get(), 1, 0.f, -2.f, true);
2382 controller->Animate(kInitialTickTime);
2383 EXPECT_EQ(0.f, dummy.opacity());
2385 // Opacity values are clipped [0,1]
2386 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2387 EXPECT_EQ(0.f, dummy.opacity());
2390 TEST(LayerAnimationControllerTest, PushedDeletedAnimationWaitsForActivation) {
2391 scoped_ptr<AnimationEventsVector> events(
2392 make_scoped_ptr(new AnimationEventsVector));
2393 FakeLayerAnimationValueObserver dummy_impl;
2394 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2395 scoped_refptr<LayerAnimationController> controller_impl(
2396 LayerAnimationController::Create(0));
2397 controller_impl->AddValueObserver(&dummy_impl);
2398 controller_impl->AddValueObserver(&pending_dummy_impl);
2399 FakeLayerAnimationValueObserver dummy;
2400 scoped_refptr<LayerAnimationController> controller(
2401 LayerAnimationController::Create(0));
2402 controller->AddValueObserver(&dummy);
2404 int animation_id =
2405 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2407 controller->PushAnimationUpdatesTo(controller_impl.get());
2408 controller_impl->ActivateAnimations();
2409 controller_impl->Animate(kInitialTickTime);
2410 controller_impl->UpdateState(true, events.get());
2411 EXPECT_EQ(Animation::RUNNING,
2412 controller_impl->GetAnimationById(animation_id)->run_state());
2413 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2414 EXPECT_EQ(0.5f, dummy_impl.opacity());
2416 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2417 ->affects_pending_observers());
2418 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2419 ->affects_active_observers());
2421 // Delete the animation on the main-thread controller.
2422 controller->RemoveAnimation(
2423 controller->GetAnimation(Animation::OPACITY)->id());
2424 controller->PushAnimationUpdatesTo(controller_impl.get());
2426 // The animation should no longer affect pending observers.
2427 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id)
2428 ->affects_pending_observers());
2429 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2430 ->affects_active_observers());
2432 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2433 controller_impl->UpdateState(true, events.get());
2435 // Only the active observer should have been ticked.
2436 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2437 EXPECT_EQ(0.75f, dummy_impl.opacity());
2439 controller_impl->ActivateAnimations();
2441 // Activation should cause the animation to be deleted.
2442 EXPECT_FALSE(controller_impl->has_any_animation());
2445 // Tests that an animation that affects only active observers won't block
2446 // an animation that affects only pending observers from starting.
2447 TEST(LayerAnimationControllerTest, StartAnimationsAffectingDifferentObservers) {
2448 scoped_ptr<AnimationEventsVector> events(
2449 make_scoped_ptr(new AnimationEventsVector));
2450 FakeLayerAnimationValueObserver dummy_impl;
2451 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2452 scoped_refptr<LayerAnimationController> controller_impl(
2453 LayerAnimationController::Create(0));
2454 controller_impl->AddValueObserver(&dummy_impl);
2455 controller_impl->AddValueObserver(&pending_dummy_impl);
2456 FakeLayerAnimationValueObserver dummy;
2457 scoped_refptr<LayerAnimationController> controller(
2458 LayerAnimationController::Create(0));
2459 controller->AddValueObserver(&dummy);
2461 int first_animation_id =
2462 AddOpacityTransitionToController(controller.get(), 1, 0.f, 1.f, true);
2464 controller->PushAnimationUpdatesTo(controller_impl.get());
2465 controller_impl->ActivateAnimations();
2466 controller_impl->Animate(kInitialTickTime);
2467 controller_impl->UpdateState(true, events.get());
2469 // Remove the first animation from the main-thread controller, and add a
2470 // new animation affecting the same property.
2471 controller->RemoveAnimation(
2472 controller->GetAnimation(Animation::OPACITY)->id());
2473 int second_animation_id =
2474 AddOpacityTransitionToController(controller.get(), 1, 1.f, 0.5f, true);
2475 controller->PushAnimationUpdatesTo(controller_impl.get());
2477 // The original animation should only affect active observers, and the new
2478 // animation should only affect pending observers.
2479 EXPECT_FALSE(controller_impl->GetAnimationById(first_animation_id)
2480 ->affects_pending_observers());
2481 EXPECT_TRUE(controller_impl->GetAnimationById(first_animation_id)
2482 ->affects_active_observers());
2483 EXPECT_TRUE(controller_impl->GetAnimationById(second_animation_id)
2484 ->affects_pending_observers());
2485 EXPECT_FALSE(controller_impl->GetAnimationById(second_animation_id)
2486 ->affects_active_observers());
2488 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2489 controller_impl->UpdateState(true, events.get());
2491 // The original animation should still be running, and the new animation
2492 // should be starting.
2493 EXPECT_EQ(Animation::RUNNING,
2494 controller_impl->GetAnimationById(first_animation_id)->run_state());
2495 EXPECT_EQ(
2496 Animation::STARTING,
2497 controller_impl->GetAnimationById(second_animation_id)->run_state());
2499 // The active observer should have been ticked by the original animation,
2500 // and the pending observer should have been ticked by the new animation.
2501 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2502 EXPECT_EQ(0.5f, dummy_impl.opacity());
2504 controller_impl->ActivateAnimations();
2506 // The original animation should have been deleted, and the new animation
2507 // should now affect both observers.
2508 EXPECT_FALSE(controller_impl->GetAnimationById(first_animation_id));
2509 EXPECT_TRUE(controller_impl->GetAnimationById(second_animation_id)
2510 ->affects_pending_observers());
2511 EXPECT_TRUE(controller_impl->GetAnimationById(second_animation_id)
2512 ->affects_active_observers());
2514 controller_impl->Animate(kInitialTickTime +
2515 TimeDelta::FromMilliseconds(1000));
2516 controller_impl->UpdateState(true, events.get());
2518 // The new animation should be running, and the active observer should have
2519 // been ticked at the new animation's starting point.
2520 EXPECT_EQ(
2521 Animation::RUNNING,
2522 controller_impl->GetAnimationById(second_animation_id)->run_state());
2523 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2524 EXPECT_EQ(1.f, dummy_impl.opacity());
2527 TEST(LayerAnimationControllerTest, TestIsAnimatingProperty) {
2528 FakeLayerAnimationValueObserver dummy;
2529 scoped_refptr<LayerAnimationController> controller(
2530 LayerAnimationController::Create(0));
2531 controller->AddValueObserver(&dummy);
2533 scoped_ptr<Animation> animation(CreateAnimation(
2534 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2535 1, Animation::OPACITY));
2536 controller->AddAnimation(animation.Pass());
2537 controller->Animate(kInitialTickTime);
2538 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::OPACITY));
2539 controller->UpdateState(true, nullptr);
2540 EXPECT_TRUE(controller->HasActiveAnimation());
2541 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::OPACITY));
2542 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::FILTER));
2543 EXPECT_EQ(0.f, dummy.opacity());
2546 TEST(LayerAnimationControllerTest, TestIsAnimatingPropertyTimeOffsetFillMode) {
2547 FakeLayerAnimationValueObserver dummy;
2548 scoped_refptr<LayerAnimationController> controller(
2549 LayerAnimationController::Create(0));
2550 controller->AddValueObserver(&dummy);
2552 scoped_ptr<Animation> animation(CreateAnimation(
2553 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2554 1, Animation::OPACITY));
2555 animation->set_fill_mode(Animation::FILL_MODE_NONE);
2556 animation->set_time_offset(TimeDelta::FromMilliseconds(-2000));
2557 controller->AddAnimation(animation.Pass());
2559 controller->Animate(kInitialTickTime);
2560 controller->UpdateState(true, nullptr);
2561 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::OPACITY));
2562 EXPECT_TRUE(controller->HasActiveAnimation());
2563 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::OPACITY));
2564 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::FILTER));
2566 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2567 controller->UpdateState(true, nullptr);
2568 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::OPACITY));
2571 } // namespace
2572 } // namespace cc