Don't add extra app list launcher page webviews.
[chromium-blink-merge.git] / cc / animation / layer_animation_controller_unittest.cc
blob48e9e926a1d19365c8b4fbee31b8f76bc93610ff
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);
302 EXPECT_FALSE(controller_impl->GetAnimation(Animation::OPACITY));
304 int animation_id =
305 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
306 int group_id = controller->GetAnimationById(animation_id)->group();
308 controller->PushAnimationUpdatesTo(controller_impl.get());
309 controller_impl->ActivateAnimations();
311 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
312 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
313 controller_impl->GetAnimationById(animation_id)->run_state());
315 // Notify main thread controller that the animation has started.
316 AnimationEvent animation_started_event(AnimationEvent::STARTED, 0, group_id,
317 Animation::OPACITY, kInitialTickTime);
318 controller->NotifyAnimationStarted(animation_started_event);
320 // Force animation to complete on impl thread.
321 controller_impl->RemoveAnimation(animation_id);
323 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
325 controller->PushAnimationUpdatesTo(controller_impl.get());
326 controller_impl->ActivateAnimations();
328 // Even though the main thread has a 'new' animation, it should not be pushed
329 // because the animation has already completed on the impl thread.
330 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
333 // Ensure that a finished animation is eventually deleted by both the
334 // main-thread and the impl-thread controllers.
335 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
336 FakeLayerAnimationValueObserver dummy;
337 FakeLayerAnimationValueObserver dummy_impl;
338 scoped_ptr<AnimationEventsVector> events(
339 make_scoped_ptr(new AnimationEventsVector));
340 scoped_refptr<LayerAnimationController> controller(
341 LayerAnimationController::Create(0));
342 scoped_refptr<LayerAnimationController> controller_impl(
343 LayerAnimationController::Create(0));
344 controller->AddValueObserver(&dummy);
345 controller_impl->AddValueObserver(&dummy_impl);
347 AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
348 controller->Animate(kInitialTickTime);
349 controller->UpdateState(true, nullptr);
350 controller->PushAnimationUpdatesTo(controller_impl.get());
351 controller_impl->ActivateAnimations();
353 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
354 controller_impl->UpdateState(true, events.get());
356 // There should be a STARTED event for the animation.
357 EXPECT_EQ(1u, events->size());
358 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
359 controller->NotifyAnimationStarted((*events)[0]);
361 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
362 controller->UpdateState(true, nullptr);
364 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
365 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
367 events.reset(new AnimationEventsVector);
368 controller_impl->Animate(kInitialTickTime +
369 TimeDelta::FromMilliseconds(2000));
370 controller_impl->UpdateState(true, events.get());
372 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
374 // There should be a FINISHED event for the animation.
375 EXPECT_EQ(1u, events->size());
376 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
378 // Neither controller should have deleted the animation yet.
379 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
380 EXPECT_TRUE(controller_impl->GetAnimation(Animation::OPACITY));
382 controller->NotifyAnimationFinished((*events)[0]);
384 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
385 controller->UpdateState(true, nullptr);
386 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
388 controller->PushAnimationUpdatesTo(controller_impl.get());
390 // Both controllers should now have deleted the animation. The impl controller
391 // should have deleted the animation even though activation has not occurred,
392 // since the animation was already waiting for deletion when
393 // PushAnimationUpdatesTo was called.
394 EXPECT_FALSE(controller->has_any_animation());
395 EXPECT_FALSE(controller_impl->has_any_animation());
398 // Tests that transitioning opacity from 0 to 1 works as expected.
400 static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
401 const AnimationEventsVector* events) {
402 const AnimationEvent* event = 0;
403 for (size_t i = 0; i < events->size(); ++i)
404 if ((*events)[i].type == AnimationEvent::PROPERTY_UPDATE)
405 event = &(*events)[i];
407 return event;
410 TEST(LayerAnimationControllerTest, TrivialTransition) {
411 scoped_ptr<AnimationEventsVector> events(
412 make_scoped_ptr(new AnimationEventsVector));
413 FakeLayerAnimationValueObserver dummy;
414 scoped_refptr<LayerAnimationController> controller(
415 LayerAnimationController::Create(0));
416 controller->AddValueObserver(&dummy);
418 scoped_ptr<Animation> to_add(CreateAnimation(
419 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
420 1, Animation::OPACITY));
422 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
423 controller->AddAnimation(to_add.Pass());
424 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
425 controller->Animate(kInitialTickTime);
426 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
427 controller->UpdateState(true, events.get());
428 EXPECT_TRUE(controller->HasActiveAnimation());
429 EXPECT_EQ(0.f, dummy.opacity());
430 // A non-impl-only animation should not generate property updates.
431 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
432 EXPECT_FALSE(event);
433 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
434 controller->UpdateState(true, events.get());
435 EXPECT_EQ(1.f, dummy.opacity());
436 EXPECT_FALSE(controller->HasActiveAnimation());
437 event = GetMostRecentPropertyUpdateEvent(events.get());
438 EXPECT_FALSE(event);
441 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
442 scoped_ptr<AnimationEventsVector> events(
443 make_scoped_ptr(new AnimationEventsVector));
444 FakeLayerAnimationValueObserver dummy_impl;
445 scoped_refptr<LayerAnimationController> controller_impl(
446 LayerAnimationController::Create(0));
447 controller_impl->AddValueObserver(&dummy_impl);
449 scoped_ptr<Animation> to_add(CreateAnimation(
450 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
451 1, Animation::OPACITY));
452 to_add->set_is_impl_only(true);
454 controller_impl->AddAnimation(to_add.Pass());
455 controller_impl->Animate(kInitialTickTime);
456 controller_impl->UpdateState(true, events.get());
457 EXPECT_TRUE(controller_impl->HasActiveAnimation());
458 EXPECT_EQ(0.f, dummy_impl.opacity());
459 EXPECT_EQ(1u, events->size());
460 const AnimationEvent* start_opacity_event =
461 GetMostRecentPropertyUpdateEvent(events.get());
462 EXPECT_EQ(0.f, start_opacity_event->opacity);
464 controller_impl->Animate(kInitialTickTime +
465 TimeDelta::FromMilliseconds(1000));
466 controller_impl->UpdateState(true, events.get());
467 EXPECT_EQ(1.f, dummy_impl.opacity());
468 EXPECT_FALSE(controller_impl->HasActiveAnimation());
469 EXPECT_EQ(2u, events->size());
470 const AnimationEvent* end_opacity_event =
471 GetMostRecentPropertyUpdateEvent(events.get());
472 EXPECT_EQ(1.f, end_opacity_event->opacity);
475 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
476 scoped_ptr<AnimationEventsVector> events(
477 make_scoped_ptr(new AnimationEventsVector));
478 FakeLayerAnimationValueObserver dummy_impl;
479 scoped_refptr<LayerAnimationController> controller_impl(
480 LayerAnimationController::Create(0));
481 controller_impl->AddValueObserver(&dummy_impl);
483 // Choose different values for x and y to avoid coincidental values in the
484 // observed transforms.
485 const float delta_x = 3;
486 const float delta_y = 4;
488 scoped_ptr<KeyframedTransformAnimationCurve> curve(
489 KeyframedTransformAnimationCurve::Create());
491 // Create simple TRANSFORM animation.
492 TransformOperations operations;
493 curve->AddKeyframe(
494 TransformKeyframe::Create(base::TimeDelta(), operations, nullptr));
495 operations.AppendTranslate(delta_x, delta_y, 0);
496 curve->AddKeyframe(TransformKeyframe::Create(
497 base::TimeDelta::FromSecondsD(1.0), operations, nullptr));
499 scoped_ptr<Animation> animation(
500 Animation::Create(curve.Pass(), 1, 0, Animation::TRANSFORM));
501 animation->set_is_impl_only(true);
502 controller_impl->AddAnimation(animation.Pass());
504 // Run animation.
505 controller_impl->Animate(kInitialTickTime);
506 controller_impl->UpdateState(true, events.get());
507 EXPECT_TRUE(controller_impl->HasActiveAnimation());
508 EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
509 EXPECT_EQ(1u, events->size());
510 const AnimationEvent* start_transform_event =
511 GetMostRecentPropertyUpdateEvent(events.get());
512 ASSERT_TRUE(start_transform_event);
513 EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
514 EXPECT_TRUE(start_transform_event->is_impl_only);
516 gfx::Transform expected_transform;
517 expected_transform.Translate(delta_x, delta_y);
519 controller_impl->Animate(kInitialTickTime +
520 TimeDelta::FromMilliseconds(1000));
521 controller_impl->UpdateState(true, events.get());
522 EXPECT_EQ(expected_transform, dummy_impl.transform());
523 EXPECT_FALSE(controller_impl->HasActiveAnimation());
524 EXPECT_EQ(2u, events->size());
525 const AnimationEvent* end_transform_event =
526 GetMostRecentPropertyUpdateEvent(events.get());
527 EXPECT_EQ(expected_transform, end_transform_event->transform);
528 EXPECT_TRUE(end_transform_event->is_impl_only);
531 TEST(LayerAnimationControllerTest, FilterTransition) {
532 scoped_ptr<AnimationEventsVector> events(
533 make_scoped_ptr(new AnimationEventsVector));
534 FakeLayerAnimationValueObserver dummy;
535 scoped_refptr<LayerAnimationController> controller(
536 LayerAnimationController::Create(0));
537 controller->AddValueObserver(&dummy);
539 scoped_ptr<KeyframedFilterAnimationCurve> curve(
540 KeyframedFilterAnimationCurve::Create());
542 FilterOperations start_filters;
543 start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
544 curve->AddKeyframe(
545 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
546 FilterOperations end_filters;
547 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
548 curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
549 end_filters, nullptr));
551 scoped_ptr<Animation> animation(
552 Animation::Create(curve.Pass(), 1, 0, Animation::FILTER));
553 controller->AddAnimation(animation.Pass());
555 controller->Animate(kInitialTickTime);
556 controller->UpdateState(true, events.get());
557 EXPECT_TRUE(controller->HasActiveAnimation());
558 EXPECT_EQ(start_filters, dummy.filters());
559 // A non-impl-only animation should not generate property updates.
560 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
561 EXPECT_FALSE(event);
563 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
564 controller->UpdateState(true, events.get());
565 EXPECT_EQ(1u, dummy.filters().size());
566 EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
567 dummy.filters().at(0));
568 event = GetMostRecentPropertyUpdateEvent(events.get());
569 EXPECT_FALSE(event);
571 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
572 controller->UpdateState(true, events.get());
573 EXPECT_EQ(end_filters, dummy.filters());
574 EXPECT_FALSE(controller->HasActiveAnimation());
575 event = GetMostRecentPropertyUpdateEvent(events.get());
576 EXPECT_FALSE(event);
579 TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
580 scoped_ptr<AnimationEventsVector> events(
581 make_scoped_ptr(new AnimationEventsVector));
582 FakeLayerAnimationValueObserver dummy_impl;
583 scoped_refptr<LayerAnimationController> controller_impl(
584 LayerAnimationController::Create(0));
585 controller_impl->AddValueObserver(&dummy_impl);
587 scoped_ptr<KeyframedFilterAnimationCurve> curve(
588 KeyframedFilterAnimationCurve::Create());
590 // Create simple FILTER animation.
591 FilterOperations start_filters;
592 start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
593 curve->AddKeyframe(
594 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
595 FilterOperations end_filters;
596 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
597 curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
598 end_filters, nullptr));
600 scoped_ptr<Animation> animation(
601 Animation::Create(curve.Pass(), 1, 0, Animation::FILTER));
602 animation->set_is_impl_only(true);
603 controller_impl->AddAnimation(animation.Pass());
605 // Run animation.
606 controller_impl->Animate(kInitialTickTime);
607 controller_impl->UpdateState(true, events.get());
608 EXPECT_TRUE(controller_impl->HasActiveAnimation());
609 EXPECT_EQ(start_filters, dummy_impl.filters());
610 EXPECT_EQ(1u, events->size());
611 const AnimationEvent* start_filter_event =
612 GetMostRecentPropertyUpdateEvent(events.get());
613 EXPECT_TRUE(start_filter_event);
614 EXPECT_EQ(start_filters, start_filter_event->filters);
615 EXPECT_TRUE(start_filter_event->is_impl_only);
617 controller_impl->Animate(kInitialTickTime +
618 TimeDelta::FromMilliseconds(1000));
619 controller_impl->UpdateState(true, events.get());
620 EXPECT_EQ(end_filters, dummy_impl.filters());
621 EXPECT_FALSE(controller_impl->HasActiveAnimation());
622 EXPECT_EQ(2u, events->size());
623 const AnimationEvent* end_filter_event =
624 GetMostRecentPropertyUpdateEvent(events.get());
625 EXPECT_TRUE(end_filter_event);
626 EXPECT_EQ(end_filters, end_filter_event->filters);
627 EXPECT_TRUE(end_filter_event->is_impl_only);
630 TEST(LayerAnimationControllerTest, ScrollOffsetTransition) {
631 FakeLayerAnimationValueObserver dummy_impl;
632 FakeLayerAnimationValueProvider dummy_provider_impl;
633 scoped_refptr<LayerAnimationController> controller_impl(
634 LayerAnimationController::Create(0));
635 controller_impl->AddValueObserver(&dummy_impl);
636 controller_impl->set_value_provider(&dummy_provider_impl);
637 scoped_ptr<AnimationEventsVector> events(
638 make_scoped_ptr(new AnimationEventsVector));
639 FakeLayerAnimationValueObserver dummy;
640 FakeLayerAnimationValueProvider dummy_provider;
641 scoped_refptr<LayerAnimationController> controller(
642 LayerAnimationController::Create(0));
643 controller->AddValueObserver(&dummy);
644 controller->set_value_provider(&dummy_provider);
646 gfx::ScrollOffset initial_value(100.f, 300.f);
647 gfx::ScrollOffset target_value(300.f, 200.f);
648 scoped_ptr<ScrollOffsetAnimationCurve> curve(
649 ScrollOffsetAnimationCurve::Create(
650 target_value,
651 EaseInOutTimingFunction::Create().Pass()));
653 scoped_ptr<Animation> animation(
654 Animation::Create(curve.Pass(), 1, 0, Animation::SCROLL_OFFSET));
655 animation->set_needs_synchronized_start_time(true);
656 controller->AddAnimation(animation.Pass());
658 dummy_provider_impl.set_scroll_offset(initial_value);
659 controller->PushAnimationUpdatesTo(controller_impl.get());
660 controller_impl->ActivateAnimations();
661 EXPECT_TRUE(controller_impl->GetAnimation(Animation::SCROLL_OFFSET));
662 TimeDelta duration = controller_impl->GetAnimation(Animation::SCROLL_OFFSET)
663 ->curve()
664 ->Duration();
665 EXPECT_EQ(
666 duration,
667 controller->GetAnimation(Animation::SCROLL_OFFSET)->curve()->Duration());
669 controller->Animate(kInitialTickTime);
670 controller->UpdateState(true, nullptr);
671 EXPECT_TRUE(controller->HasActiveAnimation());
672 EXPECT_EQ(initial_value, dummy.scroll_offset());
674 controller_impl->Animate(kInitialTickTime);
675 controller_impl->UpdateState(true, events.get());
676 EXPECT_TRUE(controller_impl->HasActiveAnimation());
677 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
678 // Scroll offset animations should not generate property updates.
679 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
680 EXPECT_FALSE(event);
682 controller->NotifyAnimationStarted((*events)[0]);
683 controller->Animate(kInitialTickTime + duration / 2);
684 controller->UpdateState(true, nullptr);
685 EXPECT_TRUE(controller->HasActiveAnimation());
686 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
688 controller_impl->Animate(kInitialTickTime + duration / 2);
689 controller_impl->UpdateState(true, events.get());
690 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
691 dummy_impl.scroll_offset());
692 event = GetMostRecentPropertyUpdateEvent(events.get());
693 EXPECT_FALSE(event);
695 controller_impl->Animate(kInitialTickTime + duration);
696 controller_impl->UpdateState(true, events.get());
697 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
698 EXPECT_FALSE(controller_impl->HasActiveAnimation());
699 event = GetMostRecentPropertyUpdateEvent(events.get());
700 EXPECT_FALSE(event);
702 controller->Animate(kInitialTickTime + duration);
703 controller->UpdateState(true, nullptr);
704 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
705 EXPECT_FALSE(controller->HasActiveAnimation());
708 // Ensure that when the impl controller doesn't have a value provider,
709 // the main-thread controller's value provider is used to obtain the intial
710 // scroll offset.
711 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
712 FakeLayerAnimationValueObserver dummy_impl;
713 scoped_refptr<LayerAnimationController> controller_impl(
714 LayerAnimationController::Create(0));
715 controller_impl->AddValueObserver(&dummy_impl);
716 scoped_ptr<AnimationEventsVector> events(
717 make_scoped_ptr(new AnimationEventsVector));
718 FakeLayerAnimationValueObserver dummy;
719 FakeLayerAnimationValueProvider dummy_provider;
720 scoped_refptr<LayerAnimationController> controller(
721 LayerAnimationController::Create(0));
722 controller->AddValueObserver(&dummy);
723 controller->set_value_provider(&dummy_provider);
725 gfx::ScrollOffset initial_value(500.f, 100.f);
726 gfx::ScrollOffset target_value(300.f, 200.f);
727 scoped_ptr<ScrollOffsetAnimationCurve> curve(
728 ScrollOffsetAnimationCurve::Create(
729 target_value,
730 EaseInOutTimingFunction::Create().Pass()));
732 scoped_ptr<Animation> animation(
733 Animation::Create(curve.Pass(), 1, 0, Animation::SCROLL_OFFSET));
734 animation->set_needs_synchronized_start_time(true);
735 controller->AddAnimation(animation.Pass());
737 dummy_provider.set_scroll_offset(initial_value);
738 controller->PushAnimationUpdatesTo(controller_impl.get());
739 controller_impl->ActivateAnimations();
740 EXPECT_TRUE(controller_impl->GetAnimation(Animation::SCROLL_OFFSET));
741 TimeDelta duration = controller_impl->GetAnimation(Animation::SCROLL_OFFSET)
742 ->curve()
743 ->Duration();
744 EXPECT_EQ(
745 duration,
746 controller->GetAnimation(Animation::SCROLL_OFFSET)->curve()->Duration());
748 controller->Animate(kInitialTickTime);
749 controller->UpdateState(true, nullptr);
750 EXPECT_TRUE(controller->HasActiveAnimation());
751 EXPECT_EQ(initial_value, dummy.scroll_offset());
753 controller_impl->Animate(kInitialTickTime);
754 controller_impl->UpdateState(true, events.get());
755 EXPECT_TRUE(controller_impl->HasActiveAnimation());
756 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
757 // Scroll offset animations should not generate property updates.
758 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
759 EXPECT_FALSE(event);
762 controller->NotifyAnimationStarted((*events)[0]);
763 controller->Animate(kInitialTickTime + duration / 2);
764 controller->UpdateState(true, nullptr);
765 EXPECT_TRUE(controller->HasActiveAnimation());
766 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
768 controller_impl->Animate(kInitialTickTime + duration / 2);
769 controller_impl->UpdateState(true, events.get());
770 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
771 dummy_impl.scroll_offset());
772 event = GetMostRecentPropertyUpdateEvent(events.get());
773 EXPECT_FALSE(event);
775 controller_impl->Animate(kInitialTickTime + duration);
776 controller_impl->UpdateState(true, events.get());
777 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
778 EXPECT_FALSE(controller_impl->HasActiveAnimation());
779 event = GetMostRecentPropertyUpdateEvent(events.get());
780 EXPECT_FALSE(event);
782 controller->Animate(kInitialTickTime + duration);
783 controller->UpdateState(true, nullptr);
784 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
785 EXPECT_FALSE(controller->HasActiveAnimation());
788 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
789 FakeLayerAnimationValueObserver dummy_impl;
790 scoped_refptr<LayerAnimationController> controller_impl(
791 LayerAnimationController::Create(0));
792 controller_impl->AddValueObserver(&dummy_impl);
793 scoped_ptr<AnimationEventsVector> events(
794 make_scoped_ptr(new AnimationEventsVector));
796 gfx::ScrollOffset initial_value(100.f, 300.f);
797 gfx::ScrollOffset target_value(300.f, 200.f);
798 scoped_ptr<ScrollOffsetAnimationCurve> curve(
799 ScrollOffsetAnimationCurve::Create(
800 target_value,
801 EaseInOutTimingFunction::Create().Pass()));
802 curve->SetInitialValue(initial_value);
803 double duration_in_seconds = curve->Duration().InSecondsF();
805 scoped_ptr<Animation> animation(
806 Animation::Create(curve.Pass(), 1, 0, Animation::SCROLL_OFFSET));
807 animation->set_is_impl_only(true);
808 controller_impl->AddAnimation(animation.Pass());
810 controller_impl->Animate(kInitialTickTime);
811 controller_impl->UpdateState(true, events.get());
812 EXPECT_TRUE(controller_impl->HasActiveAnimation());
813 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
814 // Scroll offset animations should not generate property updates.
815 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
816 EXPECT_FALSE(event);
818 TimeDelta duration = TimeDelta::FromMicroseconds(
819 duration_in_seconds * base::Time::kMicrosecondsPerSecond);
821 controller_impl->Animate(kInitialTickTime + duration / 2);
822 controller_impl->UpdateState(true, events.get());
823 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
824 dummy_impl.scroll_offset());
825 event = GetMostRecentPropertyUpdateEvent(events.get());
826 EXPECT_FALSE(event);
828 controller_impl->Animate(kInitialTickTime + duration);
829 controller_impl->UpdateState(true, events.get());
830 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
831 EXPECT_FALSE(controller_impl->HasActiveAnimation());
832 event = GetMostRecentPropertyUpdateEvent(events.get());
833 EXPECT_FALSE(event);
836 TEST(LayerAnimationControllerTest, ScrollOffsetRemovalClearsScrollDelta) {
837 FakeLayerAnimationValueObserver dummy_impl;
838 FakeLayerAnimationValueProvider dummy_provider_impl;
839 scoped_refptr<LayerAnimationController> controller_impl(
840 LayerAnimationController::Create(0));
841 controller_impl->AddValueObserver(&dummy_impl);
842 controller_impl->set_value_provider(&dummy_provider_impl);
843 scoped_ptr<AnimationEventsVector> events(
844 make_scoped_ptr(new AnimationEventsVector));
845 FakeLayerAnimationValueObserver dummy;
846 FakeLayerAnimationValueProvider dummy_provider;
847 scoped_refptr<LayerAnimationController> controller(
848 LayerAnimationController::Create(0));
849 controller->AddValueObserver(&dummy);
850 controller->set_value_provider(&dummy_provider);
852 // First test the 1-argument version of RemoveAnimation.
853 gfx::ScrollOffset target_value(300.f, 200.f);
854 scoped_ptr<ScrollOffsetAnimationCurve> curve(
855 ScrollOffsetAnimationCurve::Create(
856 target_value, EaseInOutTimingFunction::Create().Pass()));
858 int animation_id = 1;
859 scoped_ptr<Animation> animation(Animation::Create(
860 curve.Pass(), animation_id, 0, Animation::SCROLL_OFFSET));
861 animation->set_needs_synchronized_start_time(true);
862 controller->AddAnimation(animation.Pass());
863 controller->PushAnimationUpdatesTo(controller_impl.get());
864 controller_impl->ActivateAnimations();
865 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
866 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
868 controller->RemoveAnimation(animation_id);
869 EXPECT_TRUE(controller->scroll_offset_animation_was_interrupted());
871 controller->PushAnimationUpdatesTo(controller_impl.get());
872 EXPECT_TRUE(controller_impl->scroll_offset_animation_was_interrupted());
873 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
875 controller_impl->ActivateAnimations();
876 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
878 // Now, test the 2-argument version of RemoveAnimation.
879 curve = ScrollOffsetAnimationCurve::Create(
880 target_value, EaseInOutTimingFunction::Create().Pass());
881 animation = Animation::Create(curve.Pass(), animation_id, 0,
882 Animation::SCROLL_OFFSET);
883 animation->set_needs_synchronized_start_time(true);
884 controller->AddAnimation(animation.Pass());
885 controller->PushAnimationUpdatesTo(controller_impl.get());
886 controller_impl->ActivateAnimations();
887 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
888 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
890 controller->RemoveAnimation(animation_id);
891 EXPECT_TRUE(controller->scroll_offset_animation_was_interrupted());
893 controller->PushAnimationUpdatesTo(controller_impl.get());
894 EXPECT_TRUE(controller_impl->scroll_offset_animation_was_interrupted());
895 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
897 controller_impl->ActivateAnimations();
898 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
900 // Check that removing non-scroll-offset animations does not cause
901 // scroll_offset_animation_was_interrupted() to get set.
902 animation_id = AddAnimatedTransformToController(controller.get(), 1.0, 1, 2);
903 controller->PushAnimationUpdatesTo(controller_impl.get());
904 controller_impl->ActivateAnimations();
905 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
906 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
908 controller->RemoveAnimation(animation_id);
909 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
911 controller->PushAnimationUpdatesTo(controller_impl.get());
912 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
913 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
915 controller_impl->ActivateAnimations();
916 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
918 animation_id =
919 AddAnimatedFilterToController(controller.get(), 1.0, 0.1f, 0.2f);
920 controller->PushAnimationUpdatesTo(controller_impl.get());
921 controller_impl->ActivateAnimations();
922 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
923 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
925 controller->RemoveAnimation(animation_id);
926 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
928 controller->PushAnimationUpdatesTo(controller_impl.get());
929 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
930 EXPECT_FALSE(controller->scroll_offset_animation_was_interrupted());
932 controller_impl->ActivateAnimations();
933 EXPECT_FALSE(controller_impl->scroll_offset_animation_was_interrupted());
936 class FakeAnimationDelegate : public AnimationDelegate {
937 public:
938 FakeAnimationDelegate()
939 : started_(false),
940 finished_(false) {}
942 void NotifyAnimationStarted(TimeTicks monotonic_time,
943 Animation::TargetProperty target_property,
944 int group) override {
945 started_ = true;
948 void NotifyAnimationFinished(TimeTicks monotonic_time,
949 Animation::TargetProperty target_property,
950 int group) override {
951 finished_ = true;
954 bool started() { return started_; }
956 bool finished() { return finished_; }
958 private:
959 bool started_;
960 bool finished_;
963 // Tests that impl-only animations lead to start and finished notifications
964 // on the impl thread controller's animation delegate.
965 TEST(LayerAnimationControllerTest,
966 NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate) {
967 FakeLayerAnimationValueObserver dummy_impl;
968 scoped_refptr<LayerAnimationController> controller_impl(
969 LayerAnimationController::Create(0));
970 controller_impl->AddValueObserver(&dummy_impl);
971 scoped_ptr<AnimationEventsVector> events(
972 make_scoped_ptr(new AnimationEventsVector));
973 FakeAnimationDelegate delegate;
974 controller_impl->set_layer_animation_delegate(&delegate);
976 scoped_ptr<Animation> to_add(CreateAnimation(
977 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
978 1, Animation::OPACITY));
979 to_add->set_is_impl_only(true);
980 controller_impl->AddAnimation(to_add.Pass());
982 EXPECT_FALSE(delegate.started());
983 EXPECT_FALSE(delegate.finished());
985 controller_impl->Animate(kInitialTickTime);
986 controller_impl->UpdateState(true, events.get());
988 EXPECT_TRUE(delegate.started());
989 EXPECT_FALSE(delegate.finished());
991 events.reset(new AnimationEventsVector);
992 controller_impl->Animate(kInitialTickTime +
993 TimeDelta::FromMilliseconds(1000));
994 controller_impl->UpdateState(true, events.get());
996 EXPECT_TRUE(delegate.started());
997 EXPECT_TRUE(delegate.finished());
1000 // Tests animations that are waiting for a synchronized start time do not
1001 // finish.
1002 TEST(LayerAnimationControllerTest,
1003 AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
1004 scoped_ptr<AnimationEventsVector> events(
1005 make_scoped_ptr(new AnimationEventsVector));
1006 FakeLayerAnimationValueObserver dummy;
1007 scoped_refptr<LayerAnimationController> controller(
1008 LayerAnimationController::Create(0));
1009 controller->AddValueObserver(&dummy);
1011 scoped_ptr<Animation> to_add(CreateAnimation(
1012 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1013 1, Animation::OPACITY));
1014 to_add->set_needs_synchronized_start_time(true);
1016 // We should pause at the first keyframe indefinitely waiting for that
1017 // animation to start.
1018 controller->AddAnimation(to_add.Pass());
1019 controller->Animate(kInitialTickTime);
1020 controller->UpdateState(true, events.get());
1021 EXPECT_TRUE(controller->HasActiveAnimation());
1022 EXPECT_EQ(0.f, dummy.opacity());
1023 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1024 controller->UpdateState(true, events.get());
1025 EXPECT_TRUE(controller->HasActiveAnimation());
1026 EXPECT_EQ(0.f, dummy.opacity());
1027 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1028 controller->UpdateState(true, events.get());
1029 EXPECT_TRUE(controller->HasActiveAnimation());
1030 EXPECT_EQ(0.f, dummy.opacity());
1032 // Send the synchronized start time.
1033 controller->NotifyAnimationStarted(
1034 AnimationEvent(AnimationEvent::STARTED, 0, 1, Animation::OPACITY,
1035 kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
1036 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
1037 controller->UpdateState(true, events.get());
1038 EXPECT_EQ(1.f, dummy.opacity());
1039 EXPECT_FALSE(controller->HasActiveAnimation());
1042 // Tests that two queued animations affecting the same property run in sequence.
1043 TEST(LayerAnimationControllerTest, TrivialQueuing) {
1044 scoped_ptr<AnimationEventsVector> events(
1045 make_scoped_ptr(new AnimationEventsVector));
1046 FakeLayerAnimationValueObserver dummy;
1047 scoped_refptr<LayerAnimationController> controller(
1048 LayerAnimationController::Create(0));
1049 controller->AddValueObserver(&dummy);
1051 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1053 controller->AddAnimation(CreateAnimation(
1054 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1055 1, Animation::OPACITY));
1056 controller->AddAnimation(CreateAnimation(
1057 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f))
1058 .Pass(),
1059 2, Animation::OPACITY));
1061 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1063 controller->Animate(kInitialTickTime);
1065 // The second animation still needs to be started.
1066 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1068 controller->UpdateState(true, events.get());
1069 EXPECT_TRUE(controller->HasActiveAnimation());
1070 EXPECT_EQ(0.f, dummy.opacity());
1072 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1073 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1074 controller->UpdateState(true, events.get());
1075 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1077 EXPECT_TRUE(controller->HasActiveAnimation());
1078 EXPECT_EQ(1.f, dummy.opacity());
1079 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1080 controller->UpdateState(true, events.get());
1081 EXPECT_EQ(0.5f, dummy.opacity());
1082 EXPECT_FALSE(controller->HasActiveAnimation());
1085 // Tests interrupting a transition with another transition.
1086 TEST(LayerAnimationControllerTest, Interrupt) {
1087 scoped_ptr<AnimationEventsVector> events(
1088 make_scoped_ptr(new AnimationEventsVector));
1089 FakeLayerAnimationValueObserver dummy;
1090 scoped_refptr<LayerAnimationController> controller(
1091 LayerAnimationController::Create(0));
1092 controller->AddValueObserver(&dummy);
1093 controller->AddAnimation(CreateAnimation(
1094 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1095 1, Animation::OPACITY));
1096 controller->Animate(kInitialTickTime);
1097 controller->UpdateState(true, events.get());
1098 EXPECT_TRUE(controller->HasActiveAnimation());
1099 EXPECT_EQ(0.f, dummy.opacity());
1101 scoped_ptr<Animation> to_add(CreateAnimation(
1102 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f))
1103 .Pass(),
1104 2, Animation::OPACITY));
1105 controller->AbortAnimations(Animation::OPACITY);
1106 controller->AddAnimation(to_add.Pass());
1108 // Since the previous animation was aborted, the new animation should start
1109 // right in this call to animate.
1110 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1111 controller->UpdateState(true, events.get());
1112 EXPECT_TRUE(controller->HasActiveAnimation());
1113 EXPECT_EQ(1.f, dummy.opacity());
1114 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1115 controller->UpdateState(true, events.get());
1116 EXPECT_EQ(0.5f, dummy.opacity());
1117 EXPECT_FALSE(controller->HasActiveAnimation());
1120 // Tests scheduling two animations to run together when only one property is
1121 // free.
1122 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
1123 scoped_ptr<AnimationEventsVector> events(
1124 make_scoped_ptr(new AnimationEventsVector));
1125 FakeLayerAnimationValueObserver dummy;
1126 scoped_refptr<LayerAnimationController> controller(
1127 LayerAnimationController::Create(0));
1128 controller->AddValueObserver(&dummy);
1130 controller->AddAnimation(CreateAnimation(
1131 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1,
1132 Animation::TRANSFORM));
1133 controller->AddAnimation(CreateAnimation(
1134 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 2,
1135 Animation::TRANSFORM));
1136 controller->AddAnimation(CreateAnimation(
1137 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1138 2, Animation::OPACITY));
1140 controller->Animate(kInitialTickTime);
1141 controller->UpdateState(true, events.get());
1142 EXPECT_EQ(0.f, dummy.opacity());
1143 EXPECT_TRUE(controller->HasActiveAnimation());
1144 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1145 controller->UpdateState(true, events.get());
1146 // Should not have started the float transition yet.
1147 EXPECT_TRUE(controller->HasActiveAnimation());
1148 EXPECT_EQ(0.f, dummy.opacity());
1149 // The float animation should have started at time 1 and should be done.
1150 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1151 controller->UpdateState(true, events.get());
1152 EXPECT_EQ(1.f, dummy.opacity());
1153 EXPECT_FALSE(controller->HasActiveAnimation());
1156 // Tests scheduling two animations to run together with different lengths and
1157 // another animation queued to start when the shorter animation finishes (should
1158 // wait for both to finish).
1159 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
1160 scoped_ptr<AnimationEventsVector> events(
1161 make_scoped_ptr(new AnimationEventsVector));
1162 FakeLayerAnimationValueObserver dummy;
1163 scoped_refptr<LayerAnimationController> controller(
1164 LayerAnimationController::Create(0));
1165 controller->AddValueObserver(&dummy);
1167 controller->AddAnimation(CreateAnimation(
1168 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(), 1,
1169 Animation::TRANSFORM));
1170 controller->AddAnimation(CreateAnimation(
1171 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1172 1, Animation::OPACITY));
1173 controller->AddAnimation(CreateAnimation(
1174 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f))
1175 .Pass(),
1176 2, Animation::OPACITY));
1178 // Animations with id 1 should both start now.
1179 controller->Animate(kInitialTickTime);
1180 controller->UpdateState(true, events.get());
1181 EXPECT_TRUE(controller->HasActiveAnimation());
1182 EXPECT_EQ(0.f, dummy.opacity());
1183 // The opacity animation should have finished at time 1, but the group
1184 // of animations with id 1 don't finish until time 2 because of the length
1185 // of the transform animation.
1186 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1187 controller->UpdateState(true, events.get());
1188 // Should not have started the float transition yet.
1189 EXPECT_TRUE(controller->HasActiveAnimation());
1190 EXPECT_EQ(1.f, dummy.opacity());
1192 // The second opacity animation should start at time 2 and should be done by
1193 // time 3.
1194 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1195 controller->UpdateState(true, events.get());
1196 EXPECT_EQ(0.5f, dummy.opacity());
1197 EXPECT_FALSE(controller->HasActiveAnimation());
1200 // Test that a looping animation loops and for the correct number of iterations.
1201 TEST(LayerAnimationControllerTest, TrivialLooping) {
1202 scoped_ptr<AnimationEventsVector> events(
1203 make_scoped_ptr(new AnimationEventsVector));
1204 FakeLayerAnimationValueObserver dummy;
1205 scoped_refptr<LayerAnimationController> controller(
1206 LayerAnimationController::Create(0));
1207 controller->AddValueObserver(&dummy);
1209 scoped_ptr<Animation> to_add(CreateAnimation(
1210 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1211 1, Animation::OPACITY));
1212 to_add->set_iterations(3);
1213 controller->AddAnimation(to_add.Pass());
1215 controller->Animate(kInitialTickTime);
1216 controller->UpdateState(true, events.get());
1217 EXPECT_TRUE(controller->HasActiveAnimation());
1218 EXPECT_EQ(0.f, dummy.opacity());
1219 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1220 controller->UpdateState(true, events.get());
1221 EXPECT_TRUE(controller->HasActiveAnimation());
1222 EXPECT_EQ(0.25f, dummy.opacity());
1223 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1224 controller->UpdateState(true, events.get());
1225 EXPECT_TRUE(controller->HasActiveAnimation());
1226 EXPECT_EQ(0.75f, dummy.opacity());
1227 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
1228 controller->UpdateState(true, events.get());
1229 EXPECT_TRUE(controller->HasActiveAnimation());
1230 EXPECT_EQ(0.25f, dummy.opacity());
1231 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
1232 controller->UpdateState(true, events.get());
1233 EXPECT_TRUE(controller->HasActiveAnimation());
1234 EXPECT_EQ(0.75f, dummy.opacity());
1235 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1236 controller->UpdateState(true, events.get());
1237 EXPECT_FALSE(controller->HasActiveAnimation());
1238 EXPECT_EQ(1.f, dummy.opacity());
1240 // Just be extra sure.
1241 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
1242 controller->UpdateState(true, events.get());
1243 EXPECT_EQ(1.f, dummy.opacity());
1246 // Test that an infinitely looping animation does indeed go until aborted.
1247 TEST(LayerAnimationControllerTest, InfiniteLooping) {
1248 scoped_ptr<AnimationEventsVector> events(
1249 make_scoped_ptr(new AnimationEventsVector));
1250 FakeLayerAnimationValueObserver dummy;
1251 scoped_refptr<LayerAnimationController> controller(
1252 LayerAnimationController::Create(0));
1253 controller->AddValueObserver(&dummy);
1255 scoped_ptr<Animation> to_add(CreateAnimation(
1256 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1257 1, Animation::OPACITY));
1258 to_add->set_iterations(-1);
1259 controller->AddAnimation(to_add.Pass());
1261 controller->Animate(kInitialTickTime);
1262 controller->UpdateState(true, events.get());
1263 EXPECT_TRUE(controller->HasActiveAnimation());
1264 EXPECT_EQ(0.f, dummy.opacity());
1265 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1266 controller->UpdateState(true, events.get());
1267 EXPECT_TRUE(controller->HasActiveAnimation());
1268 EXPECT_EQ(0.25f, dummy.opacity());
1269 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1270 controller->UpdateState(true, events.get());
1271 EXPECT_TRUE(controller->HasActiveAnimation());
1272 EXPECT_EQ(0.75f, dummy.opacity());
1274 controller->Animate(kInitialTickTime +
1275 TimeDelta::FromMilliseconds(1073741824250));
1276 controller->UpdateState(true, events.get());
1277 EXPECT_TRUE(controller->HasActiveAnimation());
1278 EXPECT_EQ(0.25f, dummy.opacity());
1279 controller->Animate(kInitialTickTime +
1280 TimeDelta::FromMilliseconds(1073741824750));
1281 controller->UpdateState(true, events.get());
1282 EXPECT_TRUE(controller->HasActiveAnimation());
1283 EXPECT_EQ(0.75f, dummy.opacity());
1285 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
1286 controller->GetAnimation(Animation::OPACITY)
1287 ->SetRunState(Animation::ABORTED,
1288 kInitialTickTime + TimeDelta::FromMilliseconds(750));
1289 EXPECT_FALSE(controller->HasActiveAnimation());
1290 EXPECT_EQ(0.75f, dummy.opacity());
1293 // Test that pausing and resuming work as expected.
1294 TEST(LayerAnimationControllerTest, PauseResume) {
1295 scoped_ptr<AnimationEventsVector> events(
1296 make_scoped_ptr(new AnimationEventsVector));
1297 FakeLayerAnimationValueObserver dummy;
1298 scoped_refptr<LayerAnimationController> controller(
1299 LayerAnimationController::Create(0));
1300 controller->AddValueObserver(&dummy);
1302 controller->AddAnimation(CreateAnimation(
1303 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1304 1, Animation::OPACITY));
1306 controller->Animate(kInitialTickTime);
1307 controller->UpdateState(true, events.get());
1308 EXPECT_TRUE(controller->HasActiveAnimation());
1309 EXPECT_EQ(0.f, dummy.opacity());
1310 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1311 controller->UpdateState(true, events.get());
1312 EXPECT_TRUE(controller->HasActiveAnimation());
1313 EXPECT_EQ(0.5f, dummy.opacity());
1315 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
1316 controller->GetAnimation(Animation::OPACITY)
1317 ->SetRunState(Animation::PAUSED,
1318 kInitialTickTime + TimeDelta::FromMilliseconds(500));
1320 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1321 controller->UpdateState(true, events.get());
1322 EXPECT_TRUE(controller->HasActiveAnimation());
1323 EXPECT_EQ(0.5f, dummy.opacity());
1325 EXPECT_TRUE(controller->GetAnimation(Animation::OPACITY));
1326 controller->GetAnimation(Animation::OPACITY)
1327 ->SetRunState(Animation::RUNNING,
1328 kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1329 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
1330 controller->UpdateState(true, events.get());
1331 EXPECT_TRUE(controller->HasActiveAnimation());
1332 EXPECT_EQ(0.75f, dummy.opacity());
1334 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
1335 controller->UpdateState(true, events.get());
1336 EXPECT_FALSE(controller->HasActiveAnimation());
1337 EXPECT_EQ(1.f, dummy.opacity());
1340 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1341 scoped_ptr<AnimationEventsVector> events(
1342 make_scoped_ptr(new AnimationEventsVector));
1343 FakeLayerAnimationValueObserver dummy;
1344 scoped_refptr<LayerAnimationController> controller(
1345 LayerAnimationController::Create(0));
1346 controller->AddValueObserver(&dummy);
1348 const int animation_id = 2;
1349 controller->AddAnimation(Animation::Create(
1350 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1, 1,
1351 Animation::TRANSFORM));
1352 controller->AddAnimation(Animation::Create(
1353 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1354 animation_id, 1, Animation::OPACITY));
1355 controller->AddAnimation(Animation::Create(
1356 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.75f))
1357 .Pass(),
1358 3, 2, Animation::OPACITY));
1360 controller->Animate(kInitialTickTime);
1361 controller->UpdateState(true, events.get());
1362 EXPECT_TRUE(controller->HasActiveAnimation());
1363 EXPECT_EQ(0.f, dummy.opacity());
1364 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1365 controller->UpdateState(true, events.get());
1366 EXPECT_TRUE(controller->HasActiveAnimation());
1367 EXPECT_EQ(0.5f, dummy.opacity());
1369 EXPECT_TRUE(controller->GetAnimationById(animation_id));
1370 controller->GetAnimationById(animation_id)
1371 ->SetRunState(Animation::ABORTED,
1372 kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1373 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1374 controller->UpdateState(true, events.get());
1375 EXPECT_TRUE(controller->HasActiveAnimation());
1376 EXPECT_EQ(1.f, dummy.opacity());
1377 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1378 controller->UpdateState(true, events.get());
1379 EXPECT_TRUE(!controller->HasActiveAnimation());
1380 EXPECT_EQ(0.75f, dummy.opacity());
1383 TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1384 FakeLayerAnimationValueObserver dummy_impl;
1385 scoped_refptr<LayerAnimationController> controller_impl(
1386 LayerAnimationController::Create(0));
1387 controller_impl->AddValueObserver(&dummy_impl);
1388 scoped_ptr<AnimationEventsVector> events(
1389 make_scoped_ptr(new AnimationEventsVector));
1390 FakeLayerAnimationValueObserver dummy;
1391 scoped_refptr<LayerAnimationController> controller(
1392 LayerAnimationController::Create(0));
1393 controller->AddValueObserver(&dummy);
1395 scoped_ptr<Animation> to_add(CreateAnimation(
1396 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1397 0, Animation::OPACITY));
1398 to_add->set_needs_synchronized_start_time(true);
1399 controller->AddAnimation(to_add.Pass());
1401 controller->Animate(kInitialTickTime);
1402 controller->UpdateState(true, events.get());
1403 EXPECT_TRUE(controller->HasActiveAnimation());
1404 Animation* active_animation = controller->GetAnimation(Animation::OPACITY);
1405 EXPECT_TRUE(active_animation);
1406 EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1408 controller->PushAnimationUpdatesTo(controller_impl.get());
1409 controller_impl->ActivateAnimations();
1411 active_animation = controller_impl->GetAnimation(Animation::OPACITY);
1412 EXPECT_TRUE(active_animation);
1413 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1414 active_animation->run_state());
1417 // Tests that skipping a call to UpdateState works as expected.
1418 TEST(LayerAnimationControllerTest, SkipUpdateState) {
1419 scoped_ptr<AnimationEventsVector> events(
1420 make_scoped_ptr(new AnimationEventsVector));
1421 FakeLayerAnimationValueObserver dummy;
1422 scoped_refptr<LayerAnimationController> controller(
1423 LayerAnimationController::Create(0));
1424 controller->AddValueObserver(&dummy);
1426 controller->AddAnimation(CreateAnimation(
1427 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(), 1,
1428 Animation::TRANSFORM));
1430 controller->Animate(kInitialTickTime);
1431 controller->UpdateState(true, events.get());
1433 controller->AddAnimation(CreateAnimation(
1434 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1435 2, Animation::OPACITY));
1437 // Animate but don't UpdateState.
1438 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1440 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1441 events.reset(new AnimationEventsVector);
1442 controller->UpdateState(true, events.get());
1444 // Should have one STARTED event and one FINISHED event.
1445 EXPECT_EQ(2u, events->size());
1446 EXPECT_NE((*events)[0].type, (*events)[1].type);
1448 // The float transition should still be at its starting point.
1449 EXPECT_TRUE(controller->HasActiveAnimation());
1450 EXPECT_EQ(0.f, dummy.opacity());
1452 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1453 controller->UpdateState(true, events.get());
1455 // The float tranisition should now be done.
1456 EXPECT_EQ(1.f, dummy.opacity());
1457 EXPECT_FALSE(controller->HasActiveAnimation());
1460 // Tests that an animation controller with only a pending observer gets ticked
1461 // but doesn't progress animations past the STARTING state.
1462 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1463 scoped_ptr<AnimationEventsVector> events(
1464 make_scoped_ptr(new AnimationEventsVector));
1465 FakeLayerAnimationValueObserver dummy;
1466 FakeInactiveLayerAnimationValueObserver pending_dummy;
1467 scoped_refptr<LayerAnimationController> controller(
1468 LayerAnimationController::Create(0));
1470 const int id = 1;
1471 controller->AddAnimation(CreateAnimation(
1472 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.5f, 1.f))
1473 .Pass(),
1474 id, Animation::OPACITY));
1476 // Without an observer, the animation shouldn't progress to the STARTING
1477 // state.
1478 controller->Animate(kInitialTickTime);
1479 controller->UpdateState(true, events.get());
1480 EXPECT_EQ(0u, events->size());
1481 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1482 controller->GetAnimation(Animation::OPACITY)->run_state());
1484 controller->AddValueObserver(&pending_dummy);
1486 // With only a pending observer, the animation should progress to the
1487 // STARTING state and get ticked at its starting point, but should not
1488 // progress to RUNNING.
1489 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1490 controller->UpdateState(true, events.get());
1491 EXPECT_EQ(0u, events->size());
1492 EXPECT_EQ(Animation::STARTING,
1493 controller->GetAnimation(Animation::OPACITY)->run_state());
1494 EXPECT_EQ(0.5f, pending_dummy.opacity());
1496 // Even when already in the STARTING state, the animation should stay
1497 // there, and shouldn't be ticked past its starting point.
1498 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1499 controller->UpdateState(true, events.get());
1500 EXPECT_EQ(0u, events->size());
1501 EXPECT_EQ(Animation::STARTING,
1502 controller->GetAnimation(Animation::OPACITY)->run_state());
1503 EXPECT_EQ(0.5f, pending_dummy.opacity());
1505 controller->AddValueObserver(&dummy);
1507 // Now that an active observer has been added, the animation should still
1508 // initially tick at its starting point, but should now progress to RUNNING.
1509 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1510 controller->UpdateState(true, events.get());
1511 EXPECT_EQ(1u, events->size());
1512 EXPECT_EQ(Animation::RUNNING,
1513 controller->GetAnimation(Animation::OPACITY)->run_state());
1514 EXPECT_EQ(0.5f, pending_dummy.opacity());
1515 EXPECT_EQ(0.5f, dummy.opacity());
1517 // The animation should now tick past its starting point.
1518 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
1519 EXPECT_NE(0.5f, pending_dummy.opacity());
1520 EXPECT_NE(0.5f, dummy.opacity());
1523 TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
1524 scoped_refptr<LayerAnimationController> controller_impl(
1525 LayerAnimationController::Create(0));
1527 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1528 KeyframedTransformAnimationCurve::Create());
1530 TransformOperations operations1;
1531 curve1->AddKeyframe(
1532 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1533 operations1.AppendTranslate(10.0, 15.0, 0.0);
1534 curve1->AddKeyframe(TransformKeyframe::Create(
1535 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1537 scoped_ptr<Animation> animation(
1538 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
1539 controller_impl->AddAnimation(animation.Pass());
1541 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1542 KeyframedTransformAnimationCurve::Create());
1544 TransformOperations operations2;
1545 curve2->AddKeyframe(
1546 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1547 operations2.AppendScale(2.0, 3.0, 4.0);
1548 curve2->AddKeyframe(TransformKeyframe::Create(
1549 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1551 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::TRANSFORM);
1552 controller_impl->AddAnimation(animation.Pass());
1554 gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1555 gfx::BoxF bounds;
1557 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1558 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1559 bounds.ToString());
1561 controller_impl->GetAnimationById(1)
1562 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1564 // Only the unfinished animation should affect the animated bounds.
1565 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1566 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1567 bounds.ToString());
1569 controller_impl->GetAnimationById(2)
1570 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1572 // There are no longer any running animations.
1573 EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
1575 // Add an animation whose bounds we don't yet support computing.
1576 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1577 KeyframedTransformAnimationCurve::Create());
1578 TransformOperations operations3;
1579 gfx::Transform transform3;
1580 transform3.Scale3d(1.0, 2.0, 3.0);
1581 curve3->AddKeyframe(
1582 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
1583 operations3.AppendMatrix(transform3);
1584 curve3->AddKeyframe(TransformKeyframe::Create(
1585 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
1586 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::TRANSFORM);
1587 controller_impl->AddAnimation(animation.Pass());
1588 EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1591 // Tests that AbortAnimations aborts all animations targeting the specified
1592 // property.
1593 TEST(LayerAnimationControllerTest, AbortAnimations) {
1594 FakeLayerAnimationValueObserver dummy;
1595 scoped_refptr<LayerAnimationController> controller(
1596 LayerAnimationController::Create(0));
1597 controller->AddValueObserver(&dummy);
1599 // Start with several animations, and allow some of them to reach the finished
1600 // state.
1601 controller->AddAnimation(Animation::Create(
1602 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 1, 1,
1603 Animation::TRANSFORM));
1604 controller->AddAnimation(Animation::Create(
1605 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1606 2, 2, Animation::OPACITY));
1607 controller->AddAnimation(Animation::Create(
1608 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 3, 3,
1609 Animation::TRANSFORM));
1610 controller->AddAnimation(Animation::Create(
1611 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(), 4, 4,
1612 Animation::TRANSFORM));
1613 controller->AddAnimation(Animation::Create(
1614 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1615 5, 5, Animation::OPACITY));
1617 controller->Animate(kInitialTickTime);
1618 controller->UpdateState(true, nullptr);
1619 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1620 controller->UpdateState(true, nullptr);
1622 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(1)->run_state());
1623 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(2)->run_state());
1624 EXPECT_EQ(Animation::RUNNING, controller->GetAnimationById(3)->run_state());
1625 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
1626 controller->GetAnimationById(4)->run_state());
1627 EXPECT_EQ(Animation::RUNNING, controller->GetAnimationById(5)->run_state());
1629 controller->AbortAnimations(Animation::TRANSFORM);
1631 // Only un-finished TRANSFORM animations should have been aborted.
1632 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(1)->run_state());
1633 EXPECT_EQ(Animation::FINISHED, controller->GetAnimationById(2)->run_state());
1634 EXPECT_EQ(Animation::ABORTED, controller->GetAnimationById(3)->run_state());
1635 EXPECT_EQ(Animation::ABORTED, controller->GetAnimationById(4)->run_state());
1636 EXPECT_EQ(Animation::RUNNING, controller->GetAnimationById(5)->run_state());
1639 // An animation aborted on the main thread should get deleted on both threads.
1640 TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
1641 FakeLayerAnimationValueObserver dummy_impl;
1642 scoped_refptr<LayerAnimationController> controller_impl(
1643 LayerAnimationController::Create(0));
1644 controller_impl->AddValueObserver(&dummy_impl);
1645 FakeLayerAnimationValueObserver dummy;
1646 scoped_refptr<LayerAnimationController> controller(
1647 LayerAnimationController::Create(0));
1648 controller->AddValueObserver(&dummy);
1650 int animation_id =
1651 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1653 controller->PushAnimationUpdatesTo(controller_impl.get());
1654 controller_impl->ActivateAnimations();
1655 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
1657 controller->AbortAnimations(Animation::OPACITY);
1658 EXPECT_EQ(Animation::ABORTED,
1659 controller->GetAnimation(Animation::OPACITY)->run_state());
1660 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1661 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1663 controller->Animate(kInitialTickTime);
1664 controller->UpdateState(true, nullptr);
1665 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1666 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
1667 controller->GetAnimation(Animation::OPACITY)->run_state());
1669 controller->PushAnimationUpdatesTo(controller_impl.get());
1670 controller_impl->ActivateAnimations();
1671 EXPECT_FALSE(controller->GetAnimationById(animation_id));
1672 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
1675 // An animation aborted on the impl thread should get deleted on both threads.
1676 TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
1677 FakeLayerAnimationValueObserver dummy_impl;
1678 scoped_refptr<LayerAnimationController> controller_impl(
1679 LayerAnimationController::Create(0));
1680 controller_impl->AddValueObserver(&dummy_impl);
1681 FakeLayerAnimationValueObserver dummy;
1682 scoped_refptr<LayerAnimationController> controller(
1683 LayerAnimationController::Create(0));
1684 controller->AddValueObserver(&dummy);
1686 int animation_id =
1687 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1689 controller->PushAnimationUpdatesTo(controller_impl.get());
1690 controller_impl->ActivateAnimations();
1691 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
1693 controller_impl->AbortAnimations(Animation::OPACITY);
1694 EXPECT_EQ(Animation::ABORTED,
1695 controller_impl->GetAnimation(Animation::OPACITY)->run_state());
1696 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1697 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1699 AnimationEventsVector events;
1700 controller_impl->Animate(kInitialTickTime);
1701 controller_impl->UpdateState(true, &events);
1702 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
1703 EXPECT_EQ(1u, events.size());
1704 EXPECT_EQ(AnimationEvent::ABORTED, events[0].type);
1705 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
1706 controller_impl->GetAnimation(Animation::OPACITY)->run_state());
1708 controller->NotifyAnimationAborted(events[0]);
1709 EXPECT_EQ(Animation::ABORTED,
1710 controller->GetAnimation(Animation::OPACITY)->run_state());
1712 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1713 controller->UpdateState(true, nullptr);
1714 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1715 EXPECT_EQ(Animation::WAITING_FOR_DELETION,
1716 controller->GetAnimation(Animation::OPACITY)->run_state());
1718 controller->PushAnimationUpdatesTo(controller_impl.get());
1719 controller_impl->ActivateAnimations();
1720 EXPECT_FALSE(controller->GetAnimationById(animation_id));
1721 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id));
1724 // Ensure that we only generate FINISHED events for animations in a group
1725 // once all animations in that group are finished.
1726 TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
1727 scoped_ptr<AnimationEventsVector> events(
1728 make_scoped_ptr(new AnimationEventsVector));
1729 FakeLayerAnimationValueObserver dummy_impl;
1730 scoped_refptr<LayerAnimationController> controller_impl(
1731 LayerAnimationController::Create(0));
1732 controller_impl->AddValueObserver(&dummy_impl);
1734 const int group_id = 1;
1736 // Add two animations with the same group id but different durations.
1737 controller_impl->AddAnimation(Animation::Create(
1738 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(), 1,
1739 group_id, Animation::TRANSFORM));
1740 controller_impl->AddAnimation(Animation::Create(
1741 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1742 2, group_id, Animation::OPACITY));
1744 controller_impl->Animate(kInitialTickTime);
1745 controller_impl->UpdateState(true, events.get());
1747 // Both animations should have started.
1748 EXPECT_EQ(2u, events->size());
1749 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
1750 EXPECT_EQ(AnimationEvent::STARTED, (*events)[1].type);
1752 events.reset(new AnimationEventsVector);
1753 controller_impl->Animate(kInitialTickTime +
1754 TimeDelta::FromMilliseconds(1000));
1755 controller_impl->UpdateState(true, events.get());
1757 // The opacity animation should be finished, but should not have generated
1758 // a FINISHED event yet.
1759 EXPECT_EQ(0u, events->size());
1760 EXPECT_EQ(Animation::FINISHED,
1761 controller_impl->GetAnimationById(2)->run_state());
1762 EXPECT_EQ(Animation::RUNNING,
1763 controller_impl->GetAnimationById(1)->run_state());
1765 controller_impl->Animate(kInitialTickTime +
1766 TimeDelta::FromMilliseconds(2000));
1767 controller_impl->UpdateState(true, events.get());
1769 // Both animations should have generated FINISHED events.
1770 EXPECT_EQ(2u, events->size());
1771 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
1772 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[1].type);
1775 // Ensure that when a group has a mix of aborted and finished animations,
1776 // we generate a FINISHED event for the finished animation and an ABORTED
1777 // event for the aborted animation.
1778 TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
1779 scoped_ptr<AnimationEventsVector> events(
1780 make_scoped_ptr(new AnimationEventsVector));
1781 FakeLayerAnimationValueObserver dummy_impl;
1782 scoped_refptr<LayerAnimationController> controller_impl(
1783 LayerAnimationController::Create(0));
1784 controller_impl->AddValueObserver(&dummy_impl);
1786 // Add two animations with the same group id.
1787 controller_impl->AddAnimation(CreateAnimation(
1788 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(), 1,
1789 Animation::TRANSFORM));
1790 controller_impl->AddAnimation(CreateAnimation(
1791 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1792 1, Animation::OPACITY));
1794 controller_impl->Animate(kInitialTickTime);
1795 controller_impl->UpdateState(true, events.get());
1797 // Both animations should have started.
1798 EXPECT_EQ(2u, events->size());
1799 EXPECT_EQ(AnimationEvent::STARTED, (*events)[0].type);
1800 EXPECT_EQ(AnimationEvent::STARTED, (*events)[1].type);
1802 controller_impl->AbortAnimations(Animation::OPACITY);
1804 events.reset(new AnimationEventsVector);
1805 controller_impl->Animate(kInitialTickTime +
1806 TimeDelta::FromMilliseconds(1000));
1807 controller_impl->UpdateState(true, events.get());
1809 // We should have exactly 2 events: a FINISHED event for the tranform
1810 // animation, and an ABORTED event for the opacity animation.
1811 EXPECT_EQ(2u, events->size());
1812 EXPECT_EQ(AnimationEvent::FINISHED, (*events)[0].type);
1813 EXPECT_EQ(Animation::TRANSFORM, (*events)[0].target_property);
1814 EXPECT_EQ(AnimationEvent::ABORTED, (*events)[1].type);
1815 EXPECT_EQ(Animation::OPACITY, (*events)[1].target_property);
1818 TEST(LayerAnimationControllerTest, HasAnimationThatAffectsScale) {
1819 scoped_refptr<LayerAnimationController> controller_impl(
1820 LayerAnimationController::Create(0));
1822 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1824 controller_impl->AddAnimation(CreateAnimation(
1825 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1826 1, Animation::OPACITY));
1828 // Opacity animations don't affect scale.
1829 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1831 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1832 KeyframedTransformAnimationCurve::Create());
1834 TransformOperations operations1;
1835 curve1->AddKeyframe(
1836 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1837 operations1.AppendTranslate(10.0, 15.0, 0.0);
1838 curve1->AddKeyframe(TransformKeyframe::Create(
1839 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1841 scoped_ptr<Animation> animation(
1842 Animation::Create(curve1.Pass(), 2, 2, Animation::TRANSFORM));
1843 controller_impl->AddAnimation(animation.Pass());
1845 // Translations don't affect scale.
1846 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1848 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1849 KeyframedTransformAnimationCurve::Create());
1851 TransformOperations operations2;
1852 curve2->AddKeyframe(
1853 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1854 operations2.AppendScale(2.0, 3.0, 4.0);
1855 curve2->AddKeyframe(TransformKeyframe::Create(
1856 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1858 animation = Animation::Create(curve2.Pass(), 3, 3, Animation::TRANSFORM);
1859 controller_impl->AddAnimation(animation.Pass());
1861 EXPECT_TRUE(controller_impl->HasAnimationThatAffectsScale());
1863 controller_impl->GetAnimationById(3)
1864 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1866 // Only unfinished animations should be considered by
1867 // HasAnimationThatAffectsScale.
1868 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1871 TEST(LayerAnimationControllerTest, HasOnlyTranslationTransforms) {
1872 scoped_refptr<LayerAnimationController> controller_impl(
1873 LayerAnimationController::Create(0));
1875 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1877 controller_impl->AddAnimation(CreateAnimation(
1878 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1879 1, Animation::OPACITY));
1881 // Opacity animations aren't non-translation transforms.
1882 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1884 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1885 KeyframedTransformAnimationCurve::Create());
1887 TransformOperations operations1;
1888 curve1->AddKeyframe(
1889 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1890 operations1.AppendTranslate(10.0, 15.0, 0.0);
1891 curve1->AddKeyframe(TransformKeyframe::Create(
1892 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1894 scoped_ptr<Animation> animation(
1895 Animation::Create(curve1.Pass(), 2, 2, Animation::TRANSFORM));
1896 controller_impl->AddAnimation(animation.Pass());
1898 // The only transform animation we've added is a translation.
1899 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1901 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1902 KeyframedTransformAnimationCurve::Create());
1904 TransformOperations operations2;
1905 curve2->AddKeyframe(
1906 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1907 operations2.AppendScale(2.0, 3.0, 4.0);
1908 curve2->AddKeyframe(TransformKeyframe::Create(
1909 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1911 animation = Animation::Create(curve2.Pass(), 3, 3, Animation::TRANSFORM);
1912 controller_impl->AddAnimation(animation.Pass());
1914 // A scale animation is not a translation.
1915 EXPECT_FALSE(controller_impl->HasOnlyTranslationTransforms());
1917 controller_impl->GetAnimationById(3)
1918 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1920 // Only unfinished animations should be considered by
1921 // HasOnlyTranslationTransforms.
1922 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1925 TEST(LayerAnimationControllerTest, MaximumTargetScale) {
1926 scoped_refptr<LayerAnimationController> controller_impl(
1927 LayerAnimationController::Create(0));
1929 float max_scale = 0.f;
1930 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1931 EXPECT_EQ(0.f, max_scale);
1933 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1934 KeyframedTransformAnimationCurve::Create());
1936 TransformOperations operations1;
1937 curve1->AddKeyframe(
1938 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1939 operations1.AppendScale(2.0, 3.0, 4.0);
1940 curve1->AddKeyframe(TransformKeyframe::Create(
1941 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1943 scoped_ptr<Animation> animation(
1944 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
1945 controller_impl->AddAnimation(animation.Pass());
1947 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1948 EXPECT_EQ(4.f, max_scale);
1950 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1951 KeyframedTransformAnimationCurve::Create());
1953 TransformOperations operations2;
1954 curve2->AddKeyframe(
1955 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1956 operations2.AppendScale(6.0, 5.0, 4.0);
1957 curve2->AddKeyframe(TransformKeyframe::Create(
1958 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1960 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::TRANSFORM);
1961 controller_impl->AddAnimation(animation.Pass());
1963 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1964 EXPECT_EQ(6.f, max_scale);
1966 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1967 KeyframedTransformAnimationCurve::Create());
1969 TransformOperations operations3;
1970 curve3->AddKeyframe(
1971 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
1972 operations3.AppendPerspective(6.0);
1973 curve3->AddKeyframe(TransformKeyframe::Create(
1974 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
1976 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::TRANSFORM);
1977 controller_impl->AddAnimation(animation.Pass());
1979 EXPECT_FALSE(controller_impl->MaximumTargetScale(&max_scale));
1981 controller_impl->GetAnimationById(3)
1982 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1983 controller_impl->GetAnimationById(2)
1984 ->SetRunState(Animation::FINISHED, TicksFromSecondsF(0.0));
1986 // Only unfinished animations should be considered by
1987 // MaximumTargetScale.
1988 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1989 EXPECT_EQ(4.f, max_scale);
1992 TEST(LayerAnimationControllerTest, MaximumTargetScaleWithDirection) {
1993 scoped_refptr<LayerAnimationController> controller_impl(
1994 LayerAnimationController::Create(0));
1996 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1997 KeyframedTransformAnimationCurve::Create());
1998 TransformOperations operations1;
1999 operations1.AppendScale(1.0, 2.0, 3.0);
2000 curve1->AddKeyframe(
2001 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2002 TransformOperations operations2;
2003 operations2.AppendScale(4.0, 5.0, 6.0);
2004 curve1->AddKeyframe(TransformKeyframe::Create(
2005 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
2007 scoped_ptr<Animation> animation_owned(
2008 Animation::Create(curve1.Pass(), 1, 1, Animation::TRANSFORM));
2009 Animation* animation = animation_owned.get();
2010 controller_impl->AddAnimation(animation_owned.Pass());
2012 float max_scale = 0.f;
2014 EXPECT_GT(animation->playback_rate(), 0.0);
2016 // NORMAL direction with positive playback rate.
2017 animation->set_direction(Animation::DIRECTION_NORMAL);
2018 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2019 EXPECT_EQ(6.f, max_scale);
2021 // ALTERNATE direction with positive playback rate.
2022 animation->set_direction(Animation::DIRECTION_ALTERNATE);
2023 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2024 EXPECT_EQ(6.f, max_scale);
2026 // REVERSE direction with positive playback rate.
2027 animation->set_direction(Animation::DIRECTION_REVERSE);
2028 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2029 EXPECT_EQ(3.f, max_scale);
2031 // ALTERNATE reverse direction.
2032 animation->set_direction(Animation::DIRECTION_REVERSE);
2033 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2034 EXPECT_EQ(3.f, max_scale);
2036 animation->set_playback_rate(-1.0);
2038 // NORMAL direction with negative playback rate.
2039 animation->set_direction(Animation::DIRECTION_NORMAL);
2040 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2041 EXPECT_EQ(3.f, max_scale);
2043 // ALTERNATE direction with negative playback rate.
2044 animation->set_direction(Animation::DIRECTION_ALTERNATE);
2045 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2046 EXPECT_EQ(3.f, max_scale);
2048 // REVERSE direction with negative playback rate.
2049 animation->set_direction(Animation::DIRECTION_REVERSE);
2050 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2051 EXPECT_EQ(6.f, max_scale);
2053 // ALTERNATE reverse direction with negative playback rate.
2054 animation->set_direction(Animation::DIRECTION_REVERSE);
2055 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2056 EXPECT_EQ(6.f, max_scale);
2059 TEST(LayerAnimationControllerTest, NewlyPushedAnimationWaitsForActivation) {
2060 scoped_ptr<AnimationEventsVector> events(
2061 make_scoped_ptr(new AnimationEventsVector));
2062 FakeLayerAnimationValueObserver dummy_impl;
2063 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2064 scoped_refptr<LayerAnimationController> controller_impl(
2065 LayerAnimationController::Create(0));
2066 controller_impl->AddValueObserver(&dummy_impl);
2067 controller_impl->AddValueObserver(&pending_dummy_impl);
2068 FakeLayerAnimationValueObserver dummy;
2069 scoped_refptr<LayerAnimationController> controller(
2070 LayerAnimationController::Create(0));
2071 controller->AddValueObserver(&dummy);
2073 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
2074 int animation_id =
2075 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, false);
2076 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
2078 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2079 controller->PushAnimationUpdatesTo(controller_impl.get());
2080 EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
2082 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
2083 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
2084 controller_impl->GetAnimationById(animation_id)->run_state());
2085 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2086 ->affects_pending_observers());
2087 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id)
2088 ->affects_active_observers());
2090 controller_impl->Animate(kInitialTickTime);
2091 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2092 controller_impl->UpdateState(true, events.get());
2094 // Since the animation hasn't been activated, it should still be STARTING
2095 // rather than RUNNING.
2096 EXPECT_EQ(Animation::STARTING,
2097 controller_impl->GetAnimationById(animation_id)->run_state());
2099 // Since the animation hasn't been activated, only the pending observer
2100 // should have been ticked.
2101 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2102 EXPECT_EQ(0.f, dummy_impl.opacity());
2104 controller_impl->ActivateAnimations();
2105 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2106 ->affects_pending_observers());
2107 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2108 ->affects_active_observers());
2110 controller_impl->Animate(kInitialTickTime +
2111 TimeDelta::FromMilliseconds(1000));
2112 controller_impl->UpdateState(true, events.get());
2114 // Since the animation has been activated, it should have reached the
2115 // RUNNING state and the active observer should start to get ticked.
2116 EXPECT_EQ(Animation::RUNNING,
2117 controller_impl->GetAnimationById(animation_id)->run_state());
2118 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2119 EXPECT_EQ(0.5f, dummy_impl.opacity());
2122 TEST(LayerAnimationControllerTest, ActivationBetweenAnimateAndUpdateState) {
2123 scoped_ptr<AnimationEventsVector> events(
2124 make_scoped_ptr(new AnimationEventsVector));
2125 FakeLayerAnimationValueObserver dummy_impl;
2126 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2127 scoped_refptr<LayerAnimationController> controller_impl(
2128 LayerAnimationController::Create(0));
2129 controller_impl->AddValueObserver(&dummy_impl);
2130 controller_impl->AddValueObserver(&pending_dummy_impl);
2131 FakeLayerAnimationValueObserver dummy;
2132 scoped_refptr<LayerAnimationController> controller(
2133 LayerAnimationController::Create(0));
2134 controller->AddValueObserver(&dummy);
2136 int animation_id =
2137 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2139 controller->PushAnimationUpdatesTo(controller_impl.get());
2141 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id));
2142 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY,
2143 controller_impl->GetAnimationById(animation_id)->run_state());
2144 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2145 ->affects_pending_observers());
2146 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id)
2147 ->affects_active_observers());
2149 controller_impl->Animate(kInitialTickTime);
2151 // Since the animation hasn't been activated, only the pending observer
2152 // should have been ticked.
2153 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2154 EXPECT_EQ(0.f, dummy_impl.opacity());
2156 controller_impl->ActivateAnimations();
2157 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2158 ->affects_pending_observers());
2159 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2160 ->affects_active_observers());
2162 controller_impl->UpdateState(true, events.get());
2164 // Since the animation has been activated, it should have reached the
2165 // RUNNING state.
2166 EXPECT_EQ(Animation::RUNNING,
2167 controller_impl->GetAnimationById(animation_id)->run_state());
2169 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2171 // Both observers should have been ticked.
2172 EXPECT_EQ(0.75f, pending_dummy_impl.opacity());
2173 EXPECT_EQ(0.75f, dummy_impl.opacity());
2176 TEST(LayerAnimationControllerTest, ClippedOpacityValues) {
2177 FakeLayerAnimationValueObserver dummy;
2178 scoped_refptr<LayerAnimationController> controller(
2179 LayerAnimationController::Create(0));
2180 controller->AddValueObserver(&dummy);
2182 AddOpacityTransitionToController(controller.get(), 1, 1.f, 2.f, true);
2184 controller->Animate(kInitialTickTime);
2185 EXPECT_EQ(1.f, dummy.opacity());
2187 // Opacity values are clipped [0,1]
2188 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2189 EXPECT_EQ(1.f, dummy.opacity());
2192 TEST(LayerAnimationControllerTest, ClippedNegativeOpacityValues) {
2193 FakeLayerAnimationValueObserver dummy;
2194 scoped_refptr<LayerAnimationController> controller(
2195 LayerAnimationController::Create(0));
2196 controller->AddValueObserver(&dummy);
2198 AddOpacityTransitionToController(controller.get(), 1, 0.f, -2.f, true);
2200 controller->Animate(kInitialTickTime);
2201 EXPECT_EQ(0.f, dummy.opacity());
2203 // Opacity values are clipped [0,1]
2204 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2205 EXPECT_EQ(0.f, dummy.opacity());
2208 TEST(LayerAnimationControllerTest, PushedDeletedAnimationWaitsForActivation) {
2209 scoped_ptr<AnimationEventsVector> events(
2210 make_scoped_ptr(new AnimationEventsVector));
2211 FakeLayerAnimationValueObserver dummy_impl;
2212 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2213 scoped_refptr<LayerAnimationController> controller_impl(
2214 LayerAnimationController::Create(0));
2215 controller_impl->AddValueObserver(&dummy_impl);
2216 controller_impl->AddValueObserver(&pending_dummy_impl);
2217 FakeLayerAnimationValueObserver dummy;
2218 scoped_refptr<LayerAnimationController> controller(
2219 LayerAnimationController::Create(0));
2220 controller->AddValueObserver(&dummy);
2222 int animation_id =
2223 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2225 controller->PushAnimationUpdatesTo(controller_impl.get());
2226 controller_impl->ActivateAnimations();
2227 controller_impl->Animate(kInitialTickTime);
2228 controller_impl->UpdateState(true, events.get());
2229 EXPECT_EQ(Animation::RUNNING,
2230 controller_impl->GetAnimationById(animation_id)->run_state());
2231 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2232 EXPECT_EQ(0.5f, dummy_impl.opacity());
2234 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2235 ->affects_pending_observers());
2236 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2237 ->affects_active_observers());
2239 // Delete the animation on the main-thread controller.
2240 controller->RemoveAnimation(
2241 controller->GetAnimation(Animation::OPACITY)->id());
2242 controller->PushAnimationUpdatesTo(controller_impl.get());
2244 // The animation should no longer affect pending observers.
2245 EXPECT_FALSE(controller_impl->GetAnimationById(animation_id)
2246 ->affects_pending_observers());
2247 EXPECT_TRUE(controller_impl->GetAnimationById(animation_id)
2248 ->affects_active_observers());
2250 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2251 controller_impl->UpdateState(true, events.get());
2253 // Only the active observer should have been ticked.
2254 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2255 EXPECT_EQ(0.75f, dummy_impl.opacity());
2257 controller_impl->ActivateAnimations();
2259 // Activation should cause the animation to be deleted.
2260 EXPECT_FALSE(controller_impl->has_any_animation());
2263 // Tests that an animation that affects only active observers won't block
2264 // an animation that affects only pending observers from starting.
2265 TEST(LayerAnimationControllerTest, StartAnimationsAffectingDifferentObservers) {
2266 scoped_ptr<AnimationEventsVector> events(
2267 make_scoped_ptr(new AnimationEventsVector));
2268 FakeLayerAnimationValueObserver dummy_impl;
2269 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2270 scoped_refptr<LayerAnimationController> controller_impl(
2271 LayerAnimationController::Create(0));
2272 controller_impl->AddValueObserver(&dummy_impl);
2273 controller_impl->AddValueObserver(&pending_dummy_impl);
2274 FakeLayerAnimationValueObserver dummy;
2275 scoped_refptr<LayerAnimationController> controller(
2276 LayerAnimationController::Create(0));
2277 controller->AddValueObserver(&dummy);
2279 int first_animation_id =
2280 AddOpacityTransitionToController(controller.get(), 1, 0.f, 1.f, true);
2282 controller->PushAnimationUpdatesTo(controller_impl.get());
2283 controller_impl->ActivateAnimations();
2284 controller_impl->Animate(kInitialTickTime);
2285 controller_impl->UpdateState(true, events.get());
2287 // Remove the first animation from the main-thread controller, and add a
2288 // new animation affecting the same property.
2289 controller->RemoveAnimation(
2290 controller->GetAnimation(Animation::OPACITY)->id());
2291 int second_animation_id =
2292 AddOpacityTransitionToController(controller.get(), 1, 1.f, 0.5f, true);
2293 controller->PushAnimationUpdatesTo(controller_impl.get());
2295 // The original animation should only affect active observers, and the new
2296 // animation should only affect pending observers.
2297 EXPECT_FALSE(controller_impl->GetAnimationById(first_animation_id)
2298 ->affects_pending_observers());
2299 EXPECT_TRUE(controller_impl->GetAnimationById(first_animation_id)
2300 ->affects_active_observers());
2301 EXPECT_TRUE(controller_impl->GetAnimationById(second_animation_id)
2302 ->affects_pending_observers());
2303 EXPECT_FALSE(controller_impl->GetAnimationById(second_animation_id)
2304 ->affects_active_observers());
2306 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2307 controller_impl->UpdateState(true, events.get());
2309 // The original animation should still be running, and the new animation
2310 // should be starting.
2311 EXPECT_EQ(Animation::RUNNING,
2312 controller_impl->GetAnimationById(first_animation_id)->run_state());
2313 EXPECT_EQ(
2314 Animation::STARTING,
2315 controller_impl->GetAnimationById(second_animation_id)->run_state());
2317 // The active observer should have been ticked by the original animation,
2318 // and the pending observer should have been ticked by the new animation.
2319 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2320 EXPECT_EQ(0.5f, dummy_impl.opacity());
2322 controller_impl->ActivateAnimations();
2324 // The original animation should have been deleted, and the new animation
2325 // should now affect both observers.
2326 EXPECT_FALSE(controller_impl->GetAnimationById(first_animation_id));
2327 EXPECT_TRUE(controller_impl->GetAnimationById(second_animation_id)
2328 ->affects_pending_observers());
2329 EXPECT_TRUE(controller_impl->GetAnimationById(second_animation_id)
2330 ->affects_active_observers());
2332 controller_impl->Animate(kInitialTickTime +
2333 TimeDelta::FromMilliseconds(1000));
2334 controller_impl->UpdateState(true, events.get());
2336 // The new animation should be running, and the active observer should have
2337 // been ticked at the new animation's starting point.
2338 EXPECT_EQ(
2339 Animation::RUNNING,
2340 controller_impl->GetAnimationById(second_animation_id)->run_state());
2341 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2342 EXPECT_EQ(1.f, dummy_impl.opacity());
2345 TEST(LayerAnimationControllerTest, TestIsAnimatingProperty) {
2346 FakeLayerAnimationValueObserver dummy;
2347 scoped_refptr<LayerAnimationController> controller(
2348 LayerAnimationController::Create(0));
2349 controller->AddValueObserver(&dummy);
2351 scoped_ptr<Animation> animation(CreateAnimation(
2352 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2353 1, Animation::OPACITY));
2354 controller->AddAnimation(animation.Pass());
2355 controller->Animate(kInitialTickTime);
2356 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::OPACITY));
2357 controller->UpdateState(true, nullptr);
2358 EXPECT_TRUE(controller->HasActiveAnimation());
2359 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::OPACITY));
2360 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::FILTER));
2361 EXPECT_EQ(0.f, dummy.opacity());
2364 TEST(LayerAnimationControllerTest, TestIsAnimatingPropertyTimeOffsetFillMode) {
2365 FakeLayerAnimationValueObserver dummy;
2366 scoped_refptr<LayerAnimationController> controller(
2367 LayerAnimationController::Create(0));
2368 controller->AddValueObserver(&dummy);
2370 scoped_ptr<Animation> animation(CreateAnimation(
2371 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2372 1, Animation::OPACITY));
2373 animation->set_fill_mode(Animation::FILL_MODE_NONE);
2374 animation->set_time_offset(TimeDelta::FromMilliseconds(-2000));
2375 controller->AddAnimation(animation.Pass());
2377 controller->Animate(kInitialTickTime);
2378 controller->UpdateState(true, nullptr);
2379 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::OPACITY));
2380 EXPECT_TRUE(controller->HasActiveAnimation());
2381 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::OPACITY));
2382 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::FILTER));
2384 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2385 controller->UpdateState(true, nullptr);
2386 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::OPACITY));
2389 } // namespace
2390 } // namespace cc