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/box_f.h"
18 #include "ui/gfx/transform.h"
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
34 const TimeTicks kInitialTickTime = TicksFromSecondsF(1.0);
36 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve,
38 Animation::TargetProperty property) {
39 return Animation::Create(curve.Pass(), 0, 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 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
58 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
59 int group_id = controller->GetAnimation(Animation::Opacity)->group();
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->GetAnimation(group_id, Animation::Opacity));
66 EXPECT_EQ(Animation::WaitingForTargetAvailability,
67 controller_impl->GetAnimation(group_id,
68 Animation::Opacity)->run_state());
71 // If an animation is started on the impl thread before it is ticked on the main
72 // thread, we must be sure to respect the synchronized start time.
73 TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) {
74 FakeLayerAnimationValueObserver dummy_impl;
75 scoped_refptr<LayerAnimationController> controller_impl(
76 LayerAnimationController::Create(0));
77 controller_impl->AddValueObserver(&dummy_impl);
78 FakeLayerAnimationValueObserver dummy;
79 scoped_refptr<LayerAnimationController> controller(
80 LayerAnimationController::Create(0));
81 controller->AddValueObserver(&dummy);
83 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
85 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
86 int group_id = controller->GetAnimation(Animation::Opacity)->group();
88 controller->PushAnimationUpdatesTo(controller_impl.get());
89 controller_impl->ActivateAnimations();
91 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
92 EXPECT_EQ(Animation::WaitingForTargetAvailability,
93 controller_impl->GetAnimation(group_id,
94 Animation::Opacity)->run_state());
96 AnimationEventsVector events;
97 controller_impl->Animate(kInitialTickTime);
98 controller_impl->UpdateState(true, &events);
100 // Synchronize the start times.
101 EXPECT_EQ(1u, events.size());
102 controller->NotifyAnimationStarted(events[0]);
103 EXPECT_EQ(controller->GetAnimation(group_id,
104 Animation::Opacity)->start_time(),
105 controller_impl->GetAnimation(group_id,
106 Animation::Opacity)->start_time());
108 // Start the animation on the main thread. Should not affect the start time.
109 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
110 controller->UpdateState(true, NULL);
111 EXPECT_EQ(controller->GetAnimation(group_id,
112 Animation::Opacity)->start_time(),
113 controller_impl->GetAnimation(group_id,
114 Animation::Opacity)->start_time());
117 TEST(LayerAnimationControllerTest, UseSpecifiedStartTimes) {
118 FakeLayerAnimationValueObserver dummy_impl;
119 scoped_refptr<LayerAnimationController> controller_impl(
120 LayerAnimationController::Create(0));
121 controller_impl->AddValueObserver(&dummy_impl);
122 FakeLayerAnimationValueObserver dummy;
123 scoped_refptr<LayerAnimationController> controller(
124 LayerAnimationController::Create(0));
125 controller->AddValueObserver(&dummy);
127 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
128 int group_id = controller->GetAnimation(Animation::Opacity)->group();
130 const TimeTicks start_time = TicksFromSecondsF(123);
131 controller->GetAnimation(Animation::Opacity)->set_start_time(start_time);
133 controller->PushAnimationUpdatesTo(controller_impl.get());
134 controller_impl->ActivateAnimations();
136 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
137 EXPECT_EQ(Animation::WaitingForTargetAvailability,
138 controller_impl->GetAnimation(group_id,
139 Animation::Opacity)->run_state());
141 AnimationEventsVector events;
142 controller_impl->Animate(kInitialTickTime);
143 controller_impl->UpdateState(true, &events);
145 // Synchronize the start times.
146 EXPECT_EQ(1u, events.size());
147 controller->NotifyAnimationStarted(events[0]);
149 EXPECT_EQ(start_time,
150 controller->GetAnimation(group_id,
151 Animation::Opacity)->start_time());
152 EXPECT_EQ(controller->GetAnimation(group_id,
153 Animation::Opacity)->start_time(),
154 controller_impl->GetAnimation(group_id,
155 Animation::Opacity)->start_time());
157 // Start the animation on the main thread. Should not affect the start time.
158 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
159 controller->UpdateState(true, NULL);
160 EXPECT_EQ(start_time,
161 controller->GetAnimation(group_id,
162 Animation::Opacity)->start_time());
163 EXPECT_EQ(controller->GetAnimation(group_id,
164 Animation::Opacity)->start_time(),
165 controller_impl->GetAnimation(group_id,
166 Animation::Opacity)->start_time());
169 // Tests that controllers activate and deactivate as expected.
170 TEST(LayerAnimationControllerTest, Activation) {
171 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
172 scoped_ptr<AnimationRegistrar> registrar_impl = AnimationRegistrar::Create();
174 FakeLayerAnimationValueObserver dummy_impl;
175 scoped_refptr<LayerAnimationController> controller_impl(
176 LayerAnimationController::Create(0));
177 controller_impl->AddValueObserver(&dummy_impl);
178 FakeLayerAnimationValueObserver dummy;
179 scoped_refptr<LayerAnimationController> controller(
180 LayerAnimationController::Create(0));
181 controller->AddValueObserver(&dummy);
182 scoped_ptr<AnimationEventsVector> events(
183 make_scoped_ptr(new AnimationEventsVector));
185 controller->SetAnimationRegistrar(registrar.get());
186 controller_impl->SetAnimationRegistrar(registrar_impl.get());
187 EXPECT_EQ(1u, registrar->all_animation_controllers().size());
188 EXPECT_EQ(1u, registrar_impl->all_animation_controllers().size());
190 // Initially, both controllers should be inactive.
191 EXPECT_EQ(0u, registrar->active_animation_controllers().size());
192 EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
194 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
195 // The main thread controller should now be active.
196 EXPECT_EQ(1u, registrar->active_animation_controllers().size());
198 controller->PushAnimationUpdatesTo(controller_impl.get());
199 controller_impl->ActivateAnimations();
200 // Both controllers should now be active.
201 EXPECT_EQ(1u, registrar->active_animation_controllers().size());
202 EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
204 controller_impl->Animate(kInitialTickTime);
205 controller_impl->UpdateState(true, events.get());
206 EXPECT_EQ(1u, events->size());
207 controller->NotifyAnimationStarted((*events)[0]);
209 EXPECT_EQ(1u, registrar->active_animation_controllers().size());
210 EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
212 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
213 controller->UpdateState(true, NULL);
214 EXPECT_EQ(1u, registrar->active_animation_controllers().size());
216 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
217 controller->UpdateState(true, NULL);
218 EXPECT_EQ(Animation::Finished,
219 controller->GetAnimation(Animation::Opacity)->run_state());
220 EXPECT_EQ(1u, registrar->active_animation_controllers().size());
222 events.reset(new AnimationEventsVector);
223 controller_impl->Animate(kInitialTickTime +
224 TimeDelta::FromMilliseconds(1500));
225 controller_impl->UpdateState(true, events.get());
227 EXPECT_EQ(Animation::WaitingForDeletion,
228 controller_impl->GetAnimation(Animation::Opacity)->run_state());
229 // The impl thread controller should have de-activated.
230 EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
232 EXPECT_EQ(1u, events->size());
233 controller->NotifyAnimationFinished((*events)[0]);
234 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
235 controller->UpdateState(true, NULL);
237 EXPECT_EQ(Animation::WaitingForDeletion,
238 controller->GetAnimation(Animation::Opacity)->run_state());
239 // The main thread controller should have de-activated.
240 EXPECT_EQ(0u, registrar->active_animation_controllers().size());
242 controller->PushAnimationUpdatesTo(controller_impl.get());
243 controller_impl->ActivateAnimations();
244 EXPECT_FALSE(controller->has_any_animation());
245 EXPECT_FALSE(controller_impl->has_any_animation());
246 EXPECT_EQ(0u, registrar->active_animation_controllers().size());
247 EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
249 controller->SetAnimationRegistrar(NULL);
250 controller_impl->SetAnimationRegistrar(NULL);
253 TEST(LayerAnimationControllerTest, SyncPause) {
254 FakeLayerAnimationValueObserver dummy_impl;
255 scoped_refptr<LayerAnimationController> controller_impl(
256 LayerAnimationController::Create(0));
257 controller_impl->AddValueObserver(&dummy_impl);
258 FakeLayerAnimationValueObserver dummy;
259 scoped_refptr<LayerAnimationController> controller(
260 LayerAnimationController::Create(0));
261 controller->AddValueObserver(&dummy);
263 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
265 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
266 int group_id = controller->GetAnimation(Animation::Opacity)->group();
267 int animation_id = controller->GetAnimation(Animation::Opacity)->id();
269 controller->PushAnimationUpdatesTo(controller_impl.get());
270 controller_impl->ActivateAnimations();
272 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
273 EXPECT_EQ(Animation::WaitingForTargetAvailability,
274 controller_impl->GetAnimation(group_id,
275 Animation::Opacity)->run_state());
277 // Start the animations on each controller.
278 AnimationEventsVector events;
279 controller_impl->Animate(kInitialTickTime);
280 controller_impl->UpdateState(true, &events);
281 controller->Animate(kInitialTickTime);
282 controller->UpdateState(true, NULL);
283 EXPECT_EQ(Animation::Running,
284 controller_impl->GetAnimation(group_id,
285 Animation::Opacity)->run_state());
286 EXPECT_EQ(Animation::Running,
287 controller->GetAnimation(group_id,
288 Animation::Opacity)->run_state());
290 // Pause the main-thread animation.
291 controller->PauseAnimation(
293 TimeDelta::FromMilliseconds(1000) + TimeDelta::FromMilliseconds(1000));
294 EXPECT_EQ(Animation::Paused,
295 controller->GetAnimation(group_id,
296 Animation::Opacity)->run_state());
298 // The pause run state change should make it to the impl thread controller.
299 controller->PushAnimationUpdatesTo(controller_impl.get());
300 controller_impl->ActivateAnimations();
301 EXPECT_EQ(Animation::Paused,
302 controller_impl->GetAnimation(group_id,
303 Animation::Opacity)->run_state());
306 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
307 FakeLayerAnimationValueObserver dummy_impl;
308 scoped_refptr<LayerAnimationController> controller_impl(
309 LayerAnimationController::Create(0));
310 controller_impl->AddValueObserver(&dummy_impl);
311 FakeLayerAnimationValueObserver dummy;
312 scoped_refptr<LayerAnimationController> controller(
313 LayerAnimationController::Create(0));
314 controller->AddValueObserver(&dummy);
316 EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
319 AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
320 int group_id = controller->GetAnimation(Animation::Opacity)->group();
322 controller->PushAnimationUpdatesTo(controller_impl.get());
323 controller_impl->ActivateAnimations();
325 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
326 EXPECT_EQ(Animation::WaitingForTargetAvailability,
327 controller_impl->GetAnimation(group_id,
328 Animation::Opacity)->run_state());
330 // Notify main thread controller that the animation has started.
331 AnimationEvent animation_started_event(AnimationEvent::Started,
336 controller->NotifyAnimationStarted(animation_started_event);
338 // Force animation to complete on impl thread.
339 controller_impl->RemoveAnimation(animation_id);
341 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
343 controller->PushAnimationUpdatesTo(controller_impl.get());
344 controller_impl->ActivateAnimations();
346 // Even though the main thread has a 'new' animation, it should not be pushed
347 // because the animation has already completed on the impl thread.
348 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
351 // Ensure that a finished animation is eventually deleted by both the
352 // main-thread and the impl-thread controllers.
353 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
354 FakeLayerAnimationValueObserver dummy;
355 FakeLayerAnimationValueObserver dummy_impl;
356 scoped_ptr<AnimationEventsVector> events(
357 make_scoped_ptr(new AnimationEventsVector));
358 scoped_refptr<LayerAnimationController> controller(
359 LayerAnimationController::Create(0));
360 scoped_refptr<LayerAnimationController> controller_impl(
361 LayerAnimationController::Create(0));
362 controller->AddValueObserver(&dummy);
363 controller_impl->AddValueObserver(&dummy_impl);
365 AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
366 controller->Animate(kInitialTickTime);
367 controller->UpdateState(true, NULL);
368 controller->PushAnimationUpdatesTo(controller_impl.get());
369 controller_impl->ActivateAnimations();
371 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
372 controller_impl->UpdateState(true, events.get());
374 // There should be a Started event for the animation.
375 EXPECT_EQ(1u, events->size());
376 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
377 controller->NotifyAnimationStarted((*events)[0]);
379 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
380 controller->UpdateState(true, NULL);
382 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
383 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
385 events.reset(new AnimationEventsVector);
386 controller_impl->Animate(kInitialTickTime +
387 TimeDelta::FromMilliseconds(2000));
388 controller_impl->UpdateState(true, events.get());
390 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
392 // There should be a Finished event for the animation.
393 EXPECT_EQ(1u, events->size());
394 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
396 // Neither controller should have deleted the animation yet.
397 EXPECT_TRUE(controller->GetAnimation(Animation::Opacity));
398 EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity));
400 controller->NotifyAnimationFinished((*events)[0]);
402 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
403 controller->UpdateState(true, NULL);
404 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
406 controller->PushAnimationUpdatesTo(controller_impl.get());
408 // Both controllers should now have deleted the animation. The impl controller
409 // should have deleted the animation even though activation has not occurred,
410 // since the animation was already waiting for deletion when
411 // PushAnimationUpdatesTo was called.
412 EXPECT_FALSE(controller->has_any_animation());
413 EXPECT_FALSE(controller_impl->has_any_animation());
416 // Tests that transitioning opacity from 0 to 1 works as expected.
418 static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
419 const AnimationEventsVector* events) {
420 const AnimationEvent* event = 0;
421 for (size_t i = 0; i < events->size(); ++i)
422 if ((*events)[i].type == AnimationEvent::PropertyUpdate)
423 event = &(*events)[i];
428 TEST(LayerAnimationControllerTest, TrivialTransition) {
429 scoped_ptr<AnimationEventsVector> events(
430 make_scoped_ptr(new AnimationEventsVector));
431 FakeLayerAnimationValueObserver dummy;
432 scoped_refptr<LayerAnimationController> controller(
433 LayerAnimationController::Create(0));
434 controller->AddValueObserver(&dummy);
436 scoped_ptr<Animation> to_add(CreateAnimation(
437 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
439 Animation::Opacity));
441 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
442 controller->AddAnimation(to_add.Pass());
443 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
444 controller->Animate(kInitialTickTime);
445 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
446 controller->UpdateState(true, events.get());
447 EXPECT_TRUE(controller->HasActiveAnimation());
448 EXPECT_EQ(0.f, dummy.opacity());
449 // A non-impl-only animation should not generate property updates.
450 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
452 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
453 controller->UpdateState(true, events.get());
454 EXPECT_EQ(1.f, dummy.opacity());
455 EXPECT_FALSE(controller->HasActiveAnimation());
456 event = GetMostRecentPropertyUpdateEvent(events.get());
460 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
461 scoped_ptr<AnimationEventsVector> events(
462 make_scoped_ptr(new AnimationEventsVector));
463 FakeLayerAnimationValueObserver dummy_impl;
464 scoped_refptr<LayerAnimationController> controller_impl(
465 LayerAnimationController::Create(0));
466 controller_impl->AddValueObserver(&dummy_impl);
468 scoped_ptr<Animation> to_add(CreateAnimation(
469 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
471 Animation::Opacity));
472 to_add->set_is_impl_only(true);
474 controller_impl->AddAnimation(to_add.Pass());
475 controller_impl->Animate(kInitialTickTime);
476 controller_impl->UpdateState(true, events.get());
477 EXPECT_TRUE(controller_impl->HasActiveAnimation());
478 EXPECT_EQ(0.f, dummy_impl.opacity());
479 EXPECT_EQ(1u, events->size());
480 const AnimationEvent* start_opacity_event =
481 GetMostRecentPropertyUpdateEvent(events.get());
482 EXPECT_EQ(0.f, start_opacity_event->opacity);
484 controller_impl->Animate(kInitialTickTime +
485 TimeDelta::FromMilliseconds(1000));
486 controller_impl->UpdateState(true, events.get());
487 EXPECT_EQ(1.f, dummy_impl.opacity());
488 EXPECT_FALSE(controller_impl->HasActiveAnimation());
489 EXPECT_EQ(2u, events->size());
490 const AnimationEvent* end_opacity_event =
491 GetMostRecentPropertyUpdateEvent(events.get());
492 EXPECT_EQ(1.f, end_opacity_event->opacity);
495 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
496 scoped_ptr<AnimationEventsVector> events(
497 make_scoped_ptr(new AnimationEventsVector));
498 FakeLayerAnimationValueObserver dummy_impl;
499 scoped_refptr<LayerAnimationController> controller_impl(
500 LayerAnimationController::Create(0));
501 controller_impl->AddValueObserver(&dummy_impl);
503 // Choose different values for x and y to avoid coincidental values in the
504 // observed transforms.
505 const float delta_x = 3;
506 const float delta_y = 4;
508 scoped_ptr<KeyframedTransformAnimationCurve> curve(
509 KeyframedTransformAnimationCurve::Create());
511 // Create simple Transform animation.
512 TransformOperations operations;
514 TransformKeyframe::Create(0, operations, scoped_ptr<TimingFunction>()));
515 operations.AppendTranslate(delta_x, delta_y, 0);
517 TransformKeyframe::Create(1, operations, scoped_ptr<TimingFunction>()));
519 scoped_ptr<Animation> animation(Animation::Create(
520 curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform));
521 animation->set_is_impl_only(true);
522 controller_impl->AddAnimation(animation.Pass());
525 controller_impl->Animate(kInitialTickTime);
526 controller_impl->UpdateState(true, events.get());
527 EXPECT_TRUE(controller_impl->HasActiveAnimation());
528 EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
529 EXPECT_EQ(1u, events->size());
530 const AnimationEvent* start_transform_event =
531 GetMostRecentPropertyUpdateEvent(events.get());
532 ASSERT_TRUE(start_transform_event);
533 EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
534 EXPECT_TRUE(start_transform_event->is_impl_only);
536 gfx::Transform expected_transform;
537 expected_transform.Translate(delta_x, delta_y);
539 controller_impl->Animate(kInitialTickTime +
540 TimeDelta::FromMilliseconds(1000));
541 controller_impl->UpdateState(true, events.get());
542 EXPECT_EQ(expected_transform, dummy_impl.transform());
543 EXPECT_FALSE(controller_impl->HasActiveAnimation());
544 EXPECT_EQ(2u, events->size());
545 const AnimationEvent* end_transform_event =
546 GetMostRecentPropertyUpdateEvent(events.get());
547 EXPECT_EQ(expected_transform, end_transform_event->transform);
548 EXPECT_TRUE(end_transform_event->is_impl_only);
551 TEST(LayerAnimationControllerTest, FilterTransition) {
552 scoped_ptr<AnimationEventsVector> events(
553 make_scoped_ptr(new AnimationEventsVector));
554 FakeLayerAnimationValueObserver dummy;
555 scoped_refptr<LayerAnimationController> controller(
556 LayerAnimationController::Create(0));
557 controller->AddValueObserver(&dummy);
559 scoped_ptr<KeyframedFilterAnimationCurve> curve(
560 KeyframedFilterAnimationCurve::Create());
562 FilterOperations start_filters;
563 start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
565 FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
566 FilterOperations end_filters;
567 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
569 FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
571 scoped_ptr<Animation> animation(Animation::Create(
572 curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
573 controller->AddAnimation(animation.Pass());
575 controller->Animate(kInitialTickTime);
576 controller->UpdateState(true, events.get());
577 EXPECT_TRUE(controller->HasActiveAnimation());
578 EXPECT_EQ(start_filters, dummy.filters());
579 // A non-impl-only animation should not generate property updates.
580 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
583 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
584 controller->UpdateState(true, events.get());
585 EXPECT_EQ(1u, dummy.filters().size());
586 EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
587 dummy.filters().at(0));
588 event = GetMostRecentPropertyUpdateEvent(events.get());
591 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
592 controller->UpdateState(true, events.get());
593 EXPECT_EQ(end_filters, dummy.filters());
594 EXPECT_FALSE(controller->HasActiveAnimation());
595 event = GetMostRecentPropertyUpdateEvent(events.get());
599 TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
600 scoped_ptr<AnimationEventsVector> events(
601 make_scoped_ptr(new AnimationEventsVector));
602 FakeLayerAnimationValueObserver dummy_impl;
603 scoped_refptr<LayerAnimationController> controller_impl(
604 LayerAnimationController::Create(0));
605 controller_impl->AddValueObserver(&dummy_impl);
607 scoped_ptr<KeyframedFilterAnimationCurve> curve(
608 KeyframedFilterAnimationCurve::Create());
610 // Create simple Filter animation.
611 FilterOperations start_filters;
612 start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
614 FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
615 FilterOperations end_filters;
616 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
618 FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
620 scoped_ptr<Animation> animation(Animation::Create(
621 curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
622 animation->set_is_impl_only(true);
623 controller_impl->AddAnimation(animation.Pass());
626 controller_impl->Animate(kInitialTickTime);
627 controller_impl->UpdateState(true, events.get());
628 EXPECT_TRUE(controller_impl->HasActiveAnimation());
629 EXPECT_EQ(start_filters, dummy_impl.filters());
630 EXPECT_EQ(1u, events->size());
631 const AnimationEvent* start_filter_event =
632 GetMostRecentPropertyUpdateEvent(events.get());
633 EXPECT_TRUE(start_filter_event);
634 EXPECT_EQ(start_filters, start_filter_event->filters);
635 EXPECT_TRUE(start_filter_event->is_impl_only);
637 controller_impl->Animate(kInitialTickTime +
638 TimeDelta::FromMilliseconds(1000));
639 controller_impl->UpdateState(true, events.get());
640 EXPECT_EQ(end_filters, dummy_impl.filters());
641 EXPECT_FALSE(controller_impl->HasActiveAnimation());
642 EXPECT_EQ(2u, events->size());
643 const AnimationEvent* end_filter_event =
644 GetMostRecentPropertyUpdateEvent(events.get());
645 EXPECT_TRUE(end_filter_event);
646 EXPECT_EQ(end_filters, end_filter_event->filters);
647 EXPECT_TRUE(end_filter_event->is_impl_only);
650 TEST(LayerAnimationControllerTest, ScrollOffsetTransition) {
651 FakeLayerAnimationValueObserver dummy_impl;
652 FakeLayerAnimationValueProvider dummy_provider_impl;
653 scoped_refptr<LayerAnimationController> controller_impl(
654 LayerAnimationController::Create(0));
655 controller_impl->AddValueObserver(&dummy_impl);
656 controller_impl->set_value_provider(&dummy_provider_impl);
657 scoped_ptr<AnimationEventsVector> events(
658 make_scoped_ptr(new AnimationEventsVector));
659 FakeLayerAnimationValueObserver dummy;
660 FakeLayerAnimationValueProvider dummy_provider;
661 scoped_refptr<LayerAnimationController> controller(
662 LayerAnimationController::Create(0));
663 controller->AddValueObserver(&dummy);
664 controller->set_value_provider(&dummy_provider);
666 gfx::Vector2dF initial_value(100.f, 300.f);
667 gfx::Vector2dF target_value(300.f, 200.f);
668 scoped_ptr<ScrollOffsetAnimationCurve> curve(
669 ScrollOffsetAnimationCurve::Create(
671 EaseInOutTimingFunction::Create().Pass()));
673 scoped_ptr<Animation> animation(Animation::Create(
674 curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
675 animation->set_needs_synchronized_start_time(true);
676 controller->AddAnimation(animation.Pass());
678 dummy_provider_impl.set_scroll_offset(initial_value);
679 controller->PushAnimationUpdatesTo(controller_impl.get());
680 controller_impl->ActivateAnimations();
681 EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
682 double duration_in_seconds =
683 controller_impl->GetAnimation(Animation::ScrollOffset)
686 TimeDelta duration = TimeDelta::FromMicroseconds(
687 duration_in_seconds * base::Time::kMicrosecondsPerSecond);
690 controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
692 controller->Animate(kInitialTickTime);
693 controller->UpdateState(true, NULL);
694 EXPECT_TRUE(controller->HasActiveAnimation());
695 EXPECT_EQ(initial_value, dummy.scroll_offset());
697 controller_impl->Animate(kInitialTickTime);
698 controller_impl->UpdateState(true, events.get());
699 EXPECT_TRUE(controller_impl->HasActiveAnimation());
700 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
701 // Scroll offset animations should not generate property updates.
702 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
705 controller->NotifyAnimationStarted((*events)[0]);
706 controller->Animate(kInitialTickTime + duration / 2);
707 controller->UpdateState(true, NULL);
708 EXPECT_TRUE(controller->HasActiveAnimation());
709 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
711 controller_impl->Animate(kInitialTickTime + duration / 2);
712 controller_impl->UpdateState(true, events.get());
713 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
714 dummy_impl.scroll_offset());
715 event = GetMostRecentPropertyUpdateEvent(events.get());
718 controller_impl->Animate(kInitialTickTime + duration);
719 controller_impl->UpdateState(true, events.get());
720 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
721 EXPECT_FALSE(controller_impl->HasActiveAnimation());
722 event = GetMostRecentPropertyUpdateEvent(events.get());
725 controller->Animate(kInitialTickTime + duration);
726 controller->UpdateState(true, NULL);
727 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
728 EXPECT_FALSE(controller->HasActiveAnimation());
731 // Ensure that when the impl controller doesn't have a value provider,
732 // the main-thread controller's value provider is used to obtain the intial
734 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
735 FakeLayerAnimationValueObserver dummy_impl;
736 scoped_refptr<LayerAnimationController> controller_impl(
737 LayerAnimationController::Create(0));
738 controller_impl->AddValueObserver(&dummy_impl);
739 scoped_ptr<AnimationEventsVector> events(
740 make_scoped_ptr(new AnimationEventsVector));
741 FakeLayerAnimationValueObserver dummy;
742 FakeLayerAnimationValueProvider dummy_provider;
743 scoped_refptr<LayerAnimationController> controller(
744 LayerAnimationController::Create(0));
745 controller->AddValueObserver(&dummy);
746 controller->set_value_provider(&dummy_provider);
748 gfx::Vector2dF initial_value(500.f, 100.f);
749 gfx::Vector2dF target_value(300.f, 200.f);
750 scoped_ptr<ScrollOffsetAnimationCurve> curve(
751 ScrollOffsetAnimationCurve::Create(
753 EaseInOutTimingFunction::Create().Pass()));
755 scoped_ptr<Animation> animation(Animation::Create(
756 curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
757 animation->set_needs_synchronized_start_time(true);
758 controller->AddAnimation(animation.Pass());
760 dummy_provider.set_scroll_offset(initial_value);
761 controller->PushAnimationUpdatesTo(controller_impl.get());
762 controller_impl->ActivateAnimations();
763 EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
764 double duration_in_seconds =
765 controller_impl->GetAnimation(Animation::ScrollOffset)
770 controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
772 controller->Animate(kInitialTickTime);
773 controller->UpdateState(true, NULL);
774 EXPECT_TRUE(controller->HasActiveAnimation());
775 EXPECT_EQ(initial_value, dummy.scroll_offset());
777 controller_impl->Animate(kInitialTickTime);
778 controller_impl->UpdateState(true, events.get());
779 EXPECT_TRUE(controller_impl->HasActiveAnimation());
780 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
781 // Scroll offset animations should not generate property updates.
782 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
785 TimeDelta duration = TimeDelta::FromMicroseconds(
786 duration_in_seconds * base::Time::kMicrosecondsPerSecond);
788 controller->NotifyAnimationStarted((*events)[0]);
789 controller->Animate(kInitialTickTime + duration / 2);
790 controller->UpdateState(true, NULL);
791 EXPECT_TRUE(controller->HasActiveAnimation());
792 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
794 controller_impl->Animate(kInitialTickTime + duration / 2);
795 controller_impl->UpdateState(true, events.get());
796 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
797 dummy_impl.scroll_offset());
798 event = GetMostRecentPropertyUpdateEvent(events.get());
801 controller_impl->Animate(kInitialTickTime + duration);
802 controller_impl->UpdateState(true, events.get());
803 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
804 EXPECT_FALSE(controller_impl->HasActiveAnimation());
805 event = GetMostRecentPropertyUpdateEvent(events.get());
808 controller->Animate(kInitialTickTime + duration);
809 controller->UpdateState(true, NULL);
810 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
811 EXPECT_FALSE(controller->HasActiveAnimation());
814 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
815 FakeLayerAnimationValueObserver dummy_impl;
816 scoped_refptr<LayerAnimationController> controller_impl(
817 LayerAnimationController::Create(0));
818 controller_impl->AddValueObserver(&dummy_impl);
819 scoped_ptr<AnimationEventsVector> events(
820 make_scoped_ptr(new AnimationEventsVector));
822 gfx::Vector2dF initial_value(100.f, 300.f);
823 gfx::Vector2dF target_value(300.f, 200.f);
824 scoped_ptr<ScrollOffsetAnimationCurve> curve(
825 ScrollOffsetAnimationCurve::Create(
827 EaseInOutTimingFunction::Create().Pass()));
828 curve->SetInitialValue(initial_value);
829 double duration_in_seconds = curve->Duration();
831 scoped_ptr<Animation> animation(Animation::Create(
832 curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
833 animation->set_is_impl_only(true);
834 controller_impl->AddAnimation(animation.Pass());
836 controller_impl->Animate(kInitialTickTime);
837 controller_impl->UpdateState(true, events.get());
838 EXPECT_TRUE(controller_impl->HasActiveAnimation());
839 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
840 // Scroll offset animations should not generate property updates.
841 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
844 TimeDelta duration = TimeDelta::FromMicroseconds(
845 duration_in_seconds * base::Time::kMicrosecondsPerSecond);
847 controller_impl->Animate(kInitialTickTime + duration / 2);
848 controller_impl->UpdateState(true, events.get());
849 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
850 dummy_impl.scroll_offset());
851 event = GetMostRecentPropertyUpdateEvent(events.get());
854 controller_impl->Animate(kInitialTickTime + duration);
855 controller_impl->UpdateState(true, events.get());
856 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
857 EXPECT_FALSE(controller_impl->HasActiveAnimation());
858 event = GetMostRecentPropertyUpdateEvent(events.get());
862 class FakeAnimationDelegate : public AnimationDelegate {
864 FakeAnimationDelegate()
868 virtual void NotifyAnimationStarted(
869 TimeTicks monotonic_time,
870 Animation::TargetProperty target_property) OVERRIDE {
874 virtual void NotifyAnimationFinished(
875 TimeTicks monotonic_time,
876 Animation::TargetProperty target_property) OVERRIDE {
880 bool started() { return started_; }
882 bool finished() { return finished_; }
889 // Tests that impl-only animations lead to start and finished notifications
890 // on the impl thread controller's animation delegate.
891 TEST(LayerAnimationControllerTest,
892 NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate) {
893 FakeLayerAnimationValueObserver dummy_impl;
894 scoped_refptr<LayerAnimationController> controller_impl(
895 LayerAnimationController::Create(0));
896 controller_impl->AddValueObserver(&dummy_impl);
897 scoped_ptr<AnimationEventsVector> events(
898 make_scoped_ptr(new AnimationEventsVector));
899 FakeAnimationDelegate delegate;
900 controller_impl->set_layer_animation_delegate(&delegate);
902 scoped_ptr<Animation> to_add(CreateAnimation(
903 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
905 Animation::Opacity));
906 to_add->set_is_impl_only(true);
907 controller_impl->AddAnimation(to_add.Pass());
909 EXPECT_FALSE(delegate.started());
910 EXPECT_FALSE(delegate.finished());
912 controller_impl->Animate(kInitialTickTime);
913 controller_impl->UpdateState(true, events.get());
915 EXPECT_TRUE(delegate.started());
916 EXPECT_FALSE(delegate.finished());
918 events.reset(new AnimationEventsVector);
919 controller_impl->Animate(kInitialTickTime +
920 TimeDelta::FromMilliseconds(1000));
921 controller_impl->UpdateState(true, events.get());
923 EXPECT_TRUE(delegate.started());
924 EXPECT_TRUE(delegate.finished());
927 // Tests animations that are waiting for a synchronized start time do not
929 TEST(LayerAnimationControllerTest,
930 AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
931 scoped_ptr<AnimationEventsVector> events(
932 make_scoped_ptr(new AnimationEventsVector));
933 FakeLayerAnimationValueObserver dummy;
934 scoped_refptr<LayerAnimationController> controller(
935 LayerAnimationController::Create(0));
936 controller->AddValueObserver(&dummy);
938 scoped_ptr<Animation> to_add(CreateAnimation(
939 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
941 Animation::Opacity));
942 to_add->set_needs_synchronized_start_time(true);
944 // We should pause at the first keyframe indefinitely waiting for that
945 // animation to start.
946 controller->AddAnimation(to_add.Pass());
947 controller->Animate(kInitialTickTime);
948 controller->UpdateState(true, events.get());
949 EXPECT_TRUE(controller->HasActiveAnimation());
950 EXPECT_EQ(0.f, dummy.opacity());
951 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
952 controller->UpdateState(true, events.get());
953 EXPECT_TRUE(controller->HasActiveAnimation());
954 EXPECT_EQ(0.f, dummy.opacity());
955 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
956 controller->UpdateState(true, events.get());
957 EXPECT_TRUE(controller->HasActiveAnimation());
958 EXPECT_EQ(0.f, dummy.opacity());
960 // Send the synchronized start time.
961 controller->NotifyAnimationStarted(
962 AnimationEvent(AnimationEvent::Started,
966 kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
967 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
968 controller->UpdateState(true, events.get());
969 EXPECT_EQ(1.f, dummy.opacity());
970 EXPECT_FALSE(controller->HasActiveAnimation());
973 // Tests that two queued animations affecting the same property run in sequence.
974 TEST(LayerAnimationControllerTest, TrivialQueuing) {
975 scoped_ptr<AnimationEventsVector> events(
976 make_scoped_ptr(new AnimationEventsVector));
977 FakeLayerAnimationValueObserver dummy;
978 scoped_refptr<LayerAnimationController> controller(
979 LayerAnimationController::Create(0));
980 controller->AddValueObserver(&dummy);
982 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
984 controller->AddAnimation(CreateAnimation(
985 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
987 Animation::Opacity));
988 controller->AddAnimation(CreateAnimation(
989 scoped_ptr<AnimationCurve>(
990 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
992 Animation::Opacity));
994 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
996 controller->Animate(kInitialTickTime);
998 // The second animation still needs to be started.
999 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1001 controller->UpdateState(true, events.get());
1002 EXPECT_TRUE(controller->HasActiveAnimation());
1003 EXPECT_EQ(0.f, dummy.opacity());
1005 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1006 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1007 controller->UpdateState(true, events.get());
1008 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1010 EXPECT_TRUE(controller->HasActiveAnimation());
1011 EXPECT_EQ(1.f, dummy.opacity());
1012 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1013 controller->UpdateState(true, events.get());
1014 EXPECT_EQ(0.5f, dummy.opacity());
1015 EXPECT_FALSE(controller->HasActiveAnimation());
1018 // Tests interrupting a transition with another transition.
1019 TEST(LayerAnimationControllerTest, Interrupt) {
1020 scoped_ptr<AnimationEventsVector> events(
1021 make_scoped_ptr(new AnimationEventsVector));
1022 FakeLayerAnimationValueObserver dummy;
1023 scoped_refptr<LayerAnimationController> controller(
1024 LayerAnimationController::Create(0));
1025 controller->AddValueObserver(&dummy);
1026 controller->AddAnimation(CreateAnimation(
1027 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1029 Animation::Opacity));
1030 controller->Animate(kInitialTickTime);
1031 controller->UpdateState(true, events.get());
1032 EXPECT_TRUE(controller->HasActiveAnimation());
1033 EXPECT_EQ(0.f, dummy.opacity());
1035 scoped_ptr<Animation> to_add(CreateAnimation(
1036 scoped_ptr<AnimationCurve>(
1037 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1039 Animation::Opacity));
1040 controller->AbortAnimations(Animation::Opacity);
1041 controller->AddAnimation(to_add.Pass());
1043 // Since the previous animation was aborted, the new animation should start
1044 // right in this call to animate.
1045 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1046 controller->UpdateState(true, events.get());
1047 EXPECT_TRUE(controller->HasActiveAnimation());
1048 EXPECT_EQ(1.f, dummy.opacity());
1049 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1050 controller->UpdateState(true, events.get());
1051 EXPECT_EQ(0.5f, dummy.opacity());
1052 EXPECT_FALSE(controller->HasActiveAnimation());
1055 // Tests scheduling two animations to run together when only one property is
1057 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
1058 scoped_ptr<AnimationEventsVector> events(
1059 make_scoped_ptr(new AnimationEventsVector));
1060 FakeLayerAnimationValueObserver dummy;
1061 scoped_refptr<LayerAnimationController> controller(
1062 LayerAnimationController::Create(0));
1063 controller->AddValueObserver(&dummy);
1065 controller->AddAnimation(CreateAnimation(
1066 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1068 Animation::Transform));
1069 controller->AddAnimation(CreateAnimation(
1070 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1072 Animation::Transform));
1073 controller->AddAnimation(CreateAnimation(
1074 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1076 Animation::Opacity));
1078 controller->Animate(kInitialTickTime);
1079 controller->UpdateState(true, events.get());
1080 EXPECT_EQ(0.f, dummy.opacity());
1081 EXPECT_TRUE(controller->HasActiveAnimation());
1082 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1083 controller->UpdateState(true, events.get());
1084 // Should not have started the float transition yet.
1085 EXPECT_TRUE(controller->HasActiveAnimation());
1086 EXPECT_EQ(0.f, dummy.opacity());
1087 // The float animation should have started at time 1 and should be done.
1088 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1089 controller->UpdateState(true, events.get());
1090 EXPECT_EQ(1.f, dummy.opacity());
1091 EXPECT_FALSE(controller->HasActiveAnimation());
1094 // Tests scheduling two animations to run together with different lengths and
1095 // another animation queued to start when the shorter animation finishes (should
1096 // wait for both to finish).
1097 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
1098 scoped_ptr<AnimationEventsVector> events(
1099 make_scoped_ptr(new AnimationEventsVector));
1100 FakeLayerAnimationValueObserver dummy;
1101 scoped_refptr<LayerAnimationController> controller(
1102 LayerAnimationController::Create(0));
1103 controller->AddValueObserver(&dummy);
1105 controller->AddAnimation(CreateAnimation(
1106 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
1108 Animation::Transform));
1109 controller->AddAnimation(CreateAnimation(
1110 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1112 Animation::Opacity));
1113 controller->AddAnimation(CreateAnimation(
1114 scoped_ptr<AnimationCurve>(
1115 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1117 Animation::Opacity));
1119 // Animations with id 1 should both start now.
1120 controller->Animate(kInitialTickTime);
1121 controller->UpdateState(true, events.get());
1122 EXPECT_TRUE(controller->HasActiveAnimation());
1123 EXPECT_EQ(0.f, dummy.opacity());
1124 // The opacity animation should have finished at time 1, but the group
1125 // of animations with id 1 don't finish until time 2 because of the length
1126 // of the transform animation.
1127 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1128 controller->UpdateState(true, events.get());
1129 // Should not have started the float transition yet.
1130 EXPECT_TRUE(controller->HasActiveAnimation());
1131 EXPECT_EQ(1.f, dummy.opacity());
1133 // The second opacity animation should start at time 2 and should be done by
1135 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1136 controller->UpdateState(true, events.get());
1137 EXPECT_EQ(0.5f, dummy.opacity());
1138 EXPECT_FALSE(controller->HasActiveAnimation());
1141 // Test that a looping animation loops and for the correct number of iterations.
1142 TEST(LayerAnimationControllerTest, TrivialLooping) {
1143 scoped_ptr<AnimationEventsVector> events(
1144 make_scoped_ptr(new AnimationEventsVector));
1145 FakeLayerAnimationValueObserver dummy;
1146 scoped_refptr<LayerAnimationController> controller(
1147 LayerAnimationController::Create(0));
1148 controller->AddValueObserver(&dummy);
1150 scoped_ptr<Animation> to_add(CreateAnimation(
1151 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1153 Animation::Opacity));
1154 to_add->set_iterations(3);
1155 controller->AddAnimation(to_add.Pass());
1157 controller->Animate(kInitialTickTime);
1158 controller->UpdateState(true, events.get());
1159 EXPECT_TRUE(controller->HasActiveAnimation());
1160 EXPECT_EQ(0.f, dummy.opacity());
1161 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1162 controller->UpdateState(true, events.get());
1163 EXPECT_TRUE(controller->HasActiveAnimation());
1164 EXPECT_EQ(0.25f, dummy.opacity());
1165 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1166 controller->UpdateState(true, events.get());
1167 EXPECT_TRUE(controller->HasActiveAnimation());
1168 EXPECT_EQ(0.75f, dummy.opacity());
1169 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
1170 controller->UpdateState(true, events.get());
1171 EXPECT_TRUE(controller->HasActiveAnimation());
1172 EXPECT_EQ(0.25f, dummy.opacity());
1173 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
1174 controller->UpdateState(true, events.get());
1175 EXPECT_TRUE(controller->HasActiveAnimation());
1176 EXPECT_EQ(0.75f, dummy.opacity());
1177 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1178 controller->UpdateState(true, events.get());
1179 EXPECT_FALSE(controller->HasActiveAnimation());
1180 EXPECT_EQ(1.f, dummy.opacity());
1182 // Just be extra sure.
1183 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
1184 controller->UpdateState(true, events.get());
1185 EXPECT_EQ(1.f, dummy.opacity());
1188 // Test that an infinitely looping animation does indeed go until aborted.
1189 TEST(LayerAnimationControllerTest, InfiniteLooping) {
1190 scoped_ptr<AnimationEventsVector> events(
1191 make_scoped_ptr(new AnimationEventsVector));
1192 FakeLayerAnimationValueObserver dummy;
1193 scoped_refptr<LayerAnimationController> controller(
1194 LayerAnimationController::Create(0));
1195 controller->AddValueObserver(&dummy);
1198 scoped_ptr<Animation> to_add(CreateAnimation(
1199 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1201 Animation::Opacity));
1202 to_add->set_iterations(-1);
1203 controller->AddAnimation(to_add.Pass());
1205 controller->Animate(kInitialTickTime);
1206 controller->UpdateState(true, events.get());
1207 EXPECT_TRUE(controller->HasActiveAnimation());
1208 EXPECT_EQ(0.f, dummy.opacity());
1209 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1210 controller->UpdateState(true, events.get());
1211 EXPECT_TRUE(controller->HasActiveAnimation());
1212 EXPECT_EQ(0.25f, dummy.opacity());
1213 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1214 controller->UpdateState(true, events.get());
1215 EXPECT_TRUE(controller->HasActiveAnimation());
1216 EXPECT_EQ(0.75f, dummy.opacity());
1218 controller->Animate(kInitialTickTime +
1219 TimeDelta::FromMilliseconds(1073741824250));
1220 controller->UpdateState(true, events.get());
1221 EXPECT_TRUE(controller->HasActiveAnimation());
1222 EXPECT_EQ(0.25f, dummy.opacity());
1223 controller->Animate(kInitialTickTime +
1224 TimeDelta::FromMilliseconds(1073741824750));
1225 controller->UpdateState(true, events.get());
1226 EXPECT_TRUE(controller->HasActiveAnimation());
1227 EXPECT_EQ(0.75f, dummy.opacity());
1229 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1230 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1231 Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(750));
1232 EXPECT_FALSE(controller->HasActiveAnimation());
1233 EXPECT_EQ(0.75f, dummy.opacity());
1236 // Test that pausing and resuming work as expected.
1237 TEST(LayerAnimationControllerTest, PauseResume) {
1238 scoped_ptr<AnimationEventsVector> events(
1239 make_scoped_ptr(new AnimationEventsVector));
1240 FakeLayerAnimationValueObserver dummy;
1241 scoped_refptr<LayerAnimationController> controller(
1242 LayerAnimationController::Create(0));
1243 controller->AddValueObserver(&dummy);
1246 controller->AddAnimation(CreateAnimation(
1247 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1249 Animation::Opacity));
1251 controller->Animate(kInitialTickTime);
1252 controller->UpdateState(true, events.get());
1253 EXPECT_TRUE(controller->HasActiveAnimation());
1254 EXPECT_EQ(0.f, dummy.opacity());
1255 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1256 controller->UpdateState(true, events.get());
1257 EXPECT_TRUE(controller->HasActiveAnimation());
1258 EXPECT_EQ(0.5f, dummy.opacity());
1260 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1261 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1262 Animation::Paused, kInitialTickTime + TimeDelta::FromMilliseconds(500));
1264 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1265 controller->UpdateState(true, events.get());
1266 EXPECT_TRUE(controller->HasActiveAnimation());
1267 EXPECT_EQ(0.5f, dummy.opacity());
1269 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1270 controller->GetAnimation(id, Animation::Opacity)
1271 ->SetRunState(Animation::Running,
1272 kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1273 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
1274 controller->UpdateState(true, events.get());
1275 EXPECT_TRUE(controller->HasActiveAnimation());
1276 EXPECT_EQ(0.75f, dummy.opacity());
1278 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
1279 controller->UpdateState(true, events.get());
1280 EXPECT_FALSE(controller->HasActiveAnimation());
1281 EXPECT_EQ(1.f, dummy.opacity());
1284 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1285 scoped_ptr<AnimationEventsVector> events(
1286 make_scoped_ptr(new AnimationEventsVector));
1287 FakeLayerAnimationValueObserver dummy;
1288 scoped_refptr<LayerAnimationController> controller(
1289 LayerAnimationController::Create(0));
1290 controller->AddValueObserver(&dummy);
1293 controller->AddAnimation(CreateAnimation(
1294 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1296 Animation::Transform));
1297 controller->AddAnimation(CreateAnimation(
1298 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1300 Animation::Opacity));
1301 controller->AddAnimation(CreateAnimation(
1302 scoped_ptr<AnimationCurve>(
1303 new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
1305 Animation::Opacity));
1307 controller->Animate(kInitialTickTime);
1308 controller->UpdateState(true, events.get());
1309 EXPECT_TRUE(controller->HasActiveAnimation());
1310 EXPECT_EQ(0.f, dummy.opacity());
1311 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1312 controller->UpdateState(true, events.get());
1313 EXPECT_TRUE(controller->HasActiveAnimation());
1314 EXPECT_EQ(0.5f, dummy.opacity());
1316 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1317 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1318 Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1319 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1320 controller->UpdateState(true, events.get());
1321 EXPECT_TRUE(controller->HasActiveAnimation());
1322 EXPECT_EQ(1.f, dummy.opacity());
1323 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1324 controller->UpdateState(true, events.get());
1325 EXPECT_TRUE(!controller->HasActiveAnimation());
1326 EXPECT_EQ(0.75f, dummy.opacity());
1329 TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1330 FakeLayerAnimationValueObserver dummy_impl;
1331 scoped_refptr<LayerAnimationController> controller_impl(
1332 LayerAnimationController::Create(0));
1333 controller_impl->AddValueObserver(&dummy_impl);
1334 scoped_ptr<AnimationEventsVector> events(
1335 make_scoped_ptr(new AnimationEventsVector));
1336 FakeLayerAnimationValueObserver dummy;
1337 scoped_refptr<LayerAnimationController> controller(
1338 LayerAnimationController::Create(0));
1339 controller->AddValueObserver(&dummy);
1341 scoped_ptr<Animation> to_add(CreateAnimation(
1342 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1344 Animation::Opacity));
1345 to_add->set_needs_synchronized_start_time(true);
1346 controller->AddAnimation(to_add.Pass());
1348 controller->Animate(kInitialTickTime);
1349 controller->UpdateState(true, events.get());
1350 EXPECT_TRUE(controller->HasActiveAnimation());
1351 Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
1352 EXPECT_TRUE(active_animation);
1353 EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1355 controller->PushAnimationUpdatesTo(controller_impl.get());
1356 controller_impl->ActivateAnimations();
1358 active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
1359 EXPECT_TRUE(active_animation);
1360 EXPECT_EQ(Animation::WaitingForTargetAvailability,
1361 active_animation->run_state());
1364 // Tests that skipping a call to UpdateState works as expected.
1365 TEST(LayerAnimationControllerTest, SkipUpdateState) {
1366 scoped_ptr<AnimationEventsVector> events(
1367 make_scoped_ptr(new AnimationEventsVector));
1368 FakeLayerAnimationValueObserver dummy;
1369 scoped_refptr<LayerAnimationController> controller(
1370 LayerAnimationController::Create(0));
1371 controller->AddValueObserver(&dummy);
1373 controller->AddAnimation(CreateAnimation(
1374 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1376 Animation::Transform));
1378 controller->Animate(kInitialTickTime);
1379 controller->UpdateState(true, events.get());
1381 controller->AddAnimation(CreateAnimation(
1382 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1384 Animation::Opacity));
1386 // Animate but don't UpdateState.
1387 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1389 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1390 events.reset(new AnimationEventsVector);
1391 controller->UpdateState(true, events.get());
1393 // Should have one Started event and one Finished event.
1394 EXPECT_EQ(2u, events->size());
1395 EXPECT_NE((*events)[0].type, (*events)[1].type);
1397 // The float transition should still be at its starting point.
1398 EXPECT_TRUE(controller->HasActiveAnimation());
1399 EXPECT_EQ(0.f, dummy.opacity());
1401 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1402 controller->UpdateState(true, events.get());
1404 // The float tranisition should now be done.
1405 EXPECT_EQ(1.f, dummy.opacity());
1406 EXPECT_FALSE(controller->HasActiveAnimation());
1409 // Tests that an animation controller with only a pending observer gets ticked
1410 // but doesn't progress animations past the Starting state.
1411 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1412 scoped_ptr<AnimationEventsVector> events(
1413 make_scoped_ptr(new AnimationEventsVector));
1414 FakeLayerAnimationValueObserver dummy;
1415 FakeInactiveLayerAnimationValueObserver pending_dummy;
1416 scoped_refptr<LayerAnimationController> controller(
1417 LayerAnimationController::Create(0));
1420 controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>(
1421 new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(),
1423 Animation::Opacity));
1425 // Without an observer, the animation shouldn't progress to the Starting
1427 controller->Animate(kInitialTickTime);
1428 controller->UpdateState(true, events.get());
1429 EXPECT_EQ(0u, events->size());
1430 EXPECT_EQ(Animation::WaitingForTargetAvailability,
1431 controller->GetAnimation(id, Animation::Opacity)->run_state());
1433 controller->AddValueObserver(&pending_dummy);
1435 // With only a pending observer, the animation should progress to the
1436 // Starting state and get ticked at its starting point, but should not
1437 // progress to Running.
1438 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1439 controller->UpdateState(true, events.get());
1440 EXPECT_EQ(0u, events->size());
1441 EXPECT_EQ(Animation::Starting,
1442 controller->GetAnimation(id, Animation::Opacity)->run_state());
1443 EXPECT_EQ(0.5f, pending_dummy.opacity());
1445 // Even when already in the Starting state, the animation should stay
1446 // there, and shouldn't be ticked past its starting point.
1447 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1448 controller->UpdateState(true, events.get());
1449 EXPECT_EQ(0u, events->size());
1450 EXPECT_EQ(Animation::Starting,
1451 controller->GetAnimation(id, Animation::Opacity)->run_state());
1452 EXPECT_EQ(0.5f, pending_dummy.opacity());
1454 controller->AddValueObserver(&dummy);
1456 // Now that an active observer has been added, the animation should still
1457 // initially tick at its starting point, but should now progress to Running.
1458 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1459 controller->UpdateState(true, events.get());
1460 EXPECT_EQ(1u, events->size());
1461 EXPECT_EQ(Animation::Running,
1462 controller->GetAnimation(id, Animation::Opacity)->run_state());
1463 EXPECT_EQ(0.5f, pending_dummy.opacity());
1464 EXPECT_EQ(0.5f, dummy.opacity());
1466 // The animation should now tick past its starting point.
1467 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
1468 EXPECT_NE(0.5f, pending_dummy.opacity());
1469 EXPECT_NE(0.5f, dummy.opacity());
1472 TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
1473 scoped_refptr<LayerAnimationController> controller_impl(
1474 LayerAnimationController::Create(0));
1476 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1477 KeyframedTransformAnimationCurve::Create());
1479 TransformOperations operations1;
1480 curve1->AddKeyframe(TransformKeyframe::Create(
1481 0.0, operations1, scoped_ptr<TimingFunction>()));
1482 operations1.AppendTranslate(10.0, 15.0, 0.0);
1483 curve1->AddKeyframe(TransformKeyframe::Create(
1484 1.0, operations1, scoped_ptr<TimingFunction>()));
1486 scoped_ptr<Animation> animation(Animation::Create(
1487 curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1488 controller_impl->AddAnimation(animation.Pass());
1490 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1491 KeyframedTransformAnimationCurve::Create());
1493 TransformOperations operations2;
1494 curve2->AddKeyframe(TransformKeyframe::Create(
1495 0.0, operations2, scoped_ptr<TimingFunction>()));
1496 operations2.AppendScale(2.0, 3.0, 4.0);
1497 curve2->AddKeyframe(TransformKeyframe::Create(
1498 1.0, operations2, scoped_ptr<TimingFunction>()));
1500 animation = Animation::Create(
1501 curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
1502 controller_impl->AddAnimation(animation.Pass());
1504 gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1507 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1508 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1511 controller_impl->GetAnimation(1, Animation::Transform)
1512 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1514 // Only the unfinished animation should affect the animated bounds.
1515 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1516 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1519 controller_impl->GetAnimation(2, Animation::Transform)
1520 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1522 // There are no longer any running animations.
1523 EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
1525 // Add an animation whose bounds we don't yet support computing.
1526 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1527 KeyframedTransformAnimationCurve::Create());
1528 TransformOperations operations3;
1529 gfx::Transform transform3;
1530 transform3.Scale3d(1.0, 2.0, 3.0);
1531 curve3->AddKeyframe(TransformKeyframe::Create(
1532 0.0, operations3, scoped_ptr<TimingFunction>()));
1533 operations3.AppendMatrix(transform3);
1534 curve3->AddKeyframe(TransformKeyframe::Create(
1535 1.0, operations3, scoped_ptr<TimingFunction>()));
1536 animation = Animation::Create(
1537 curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1538 controller_impl->AddAnimation(animation.Pass());
1539 EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1542 // Tests that AbortAnimations aborts all animations targeting the specified
1544 TEST(LayerAnimationControllerTest, AbortAnimations) {
1545 FakeLayerAnimationValueObserver dummy;
1546 scoped_refptr<LayerAnimationController> controller(
1547 LayerAnimationController::Create(0));
1548 controller->AddValueObserver(&dummy);
1550 // Start with several animations, and allow some of them to reach the finished
1552 controller->AddAnimation(CreateAnimation(
1553 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1555 Animation::Transform));
1556 controller->AddAnimation(CreateAnimation(
1557 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1559 Animation::Opacity));
1560 controller->AddAnimation(CreateAnimation(
1561 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1563 Animation::Transform));
1564 controller->AddAnimation(CreateAnimation(
1565 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1567 Animation::Transform));
1568 controller->AddAnimation(CreateAnimation(
1569 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1571 Animation::Opacity));
1573 controller->Animate(kInitialTickTime);
1574 controller->UpdateState(true, NULL);
1575 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1576 controller->UpdateState(true, NULL);
1578 EXPECT_EQ(Animation::Finished,
1579 controller->GetAnimation(1, Animation::Transform)->run_state());
1580 EXPECT_EQ(Animation::Finished,
1581 controller->GetAnimation(2, Animation::Opacity)->run_state());
1582 EXPECT_EQ(Animation::Running,
1583 controller->GetAnimation(3, Animation::Transform)->run_state());
1584 EXPECT_EQ(Animation::WaitingForTargetAvailability,
1585 controller->GetAnimation(4, Animation::Transform)->run_state());
1586 EXPECT_EQ(Animation::Running,
1587 controller->GetAnimation(5, Animation::Opacity)->run_state());
1589 controller->AbortAnimations(Animation::Transform);
1591 // Only un-finished Transform animations should have been aborted.
1592 EXPECT_EQ(Animation::Finished,
1593 controller->GetAnimation(1, Animation::Transform)->run_state());
1594 EXPECT_EQ(Animation::Finished,
1595 controller->GetAnimation(2, Animation::Opacity)->run_state());
1596 EXPECT_EQ(Animation::Aborted,
1597 controller->GetAnimation(3, Animation::Transform)->run_state());
1598 EXPECT_EQ(Animation::Aborted,
1599 controller->GetAnimation(4, Animation::Transform)->run_state());
1600 EXPECT_EQ(Animation::Running,
1601 controller->GetAnimation(5, Animation::Opacity)->run_state());
1604 // An animation aborted on the main thread should get deleted on both threads.
1605 TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
1606 FakeLayerAnimationValueObserver dummy_impl;
1607 scoped_refptr<LayerAnimationController> controller_impl(
1608 LayerAnimationController::Create(0));
1609 controller_impl->AddValueObserver(&dummy_impl);
1610 FakeLayerAnimationValueObserver dummy;
1611 scoped_refptr<LayerAnimationController> controller(
1612 LayerAnimationController::Create(0));
1613 controller->AddValueObserver(&dummy);
1615 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1616 int group_id = controller->GetAnimation(Animation::Opacity)->group();
1618 controller->PushAnimationUpdatesTo(controller_impl.get());
1619 controller_impl->ActivateAnimations();
1620 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1622 controller->AbortAnimations(Animation::Opacity);
1623 EXPECT_EQ(Animation::Aborted,
1624 controller->GetAnimation(Animation::Opacity)->run_state());
1625 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1626 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1628 controller->Animate(kInitialTickTime);
1629 controller->UpdateState(true, NULL);
1630 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1631 EXPECT_EQ(Animation::WaitingForDeletion,
1632 controller->GetAnimation(Animation::Opacity)->run_state());
1634 controller->PushAnimationUpdatesTo(controller_impl.get());
1635 controller_impl->ActivateAnimations();
1636 EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1637 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1640 // An animation aborted on the impl thread should get deleted on both threads.
1641 TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
1642 FakeLayerAnimationValueObserver dummy_impl;
1643 scoped_refptr<LayerAnimationController> controller_impl(
1644 LayerAnimationController::Create(0));
1645 controller_impl->AddValueObserver(&dummy_impl);
1646 FakeLayerAnimationValueObserver dummy;
1647 scoped_refptr<LayerAnimationController> controller(
1648 LayerAnimationController::Create(0));
1649 controller->AddValueObserver(&dummy);
1651 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1652 int group_id = controller->GetAnimation(Animation::Opacity)->group();
1654 controller->PushAnimationUpdatesTo(controller_impl.get());
1655 controller_impl->ActivateAnimations();
1656 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1658 controller_impl->AbortAnimations(Animation::Opacity);
1659 EXPECT_EQ(Animation::Aborted,
1660 controller_impl->GetAnimation(Animation::Opacity)->run_state());
1661 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1662 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1664 AnimationEventsVector events;
1665 controller_impl->Animate(kInitialTickTime);
1666 controller_impl->UpdateState(true, &events);
1667 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
1668 EXPECT_EQ(1u, events.size());
1669 EXPECT_EQ(AnimationEvent::Aborted, events[0].type);
1670 EXPECT_EQ(Animation::WaitingForDeletion,
1671 controller_impl->GetAnimation(Animation::Opacity)->run_state());
1673 controller->NotifyAnimationAborted(events[0]);
1674 EXPECT_EQ(Animation::Aborted,
1675 controller->GetAnimation(Animation::Opacity)->run_state());
1677 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1678 controller->UpdateState(true, NULL);
1679 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1680 EXPECT_EQ(Animation::WaitingForDeletion,
1681 controller->GetAnimation(Animation::Opacity)->run_state());
1683 controller->PushAnimationUpdatesTo(controller_impl.get());
1684 controller_impl->ActivateAnimations();
1685 EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1686 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1689 // Ensure that we only generate Finished events for animations in a group
1690 // once all animations in that group are finished.
1691 TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
1692 scoped_ptr<AnimationEventsVector> events(
1693 make_scoped_ptr(new AnimationEventsVector));
1694 FakeLayerAnimationValueObserver dummy_impl;
1695 scoped_refptr<LayerAnimationController> controller_impl(
1696 LayerAnimationController::Create(0));
1697 controller_impl->AddValueObserver(&dummy_impl);
1699 // Add two animations with the same group id but different durations.
1700 controller_impl->AddAnimation(CreateAnimation(
1701 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1703 Animation::Transform));
1704 controller_impl->AddAnimation(CreateAnimation(
1705 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1707 Animation::Opacity));
1709 controller_impl->Animate(kInitialTickTime);
1710 controller_impl->UpdateState(true, events.get());
1712 // Both animations should have started.
1713 EXPECT_EQ(2u, events->size());
1714 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1715 EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1717 events.reset(new AnimationEventsVector);
1718 controller_impl->Animate(kInitialTickTime +
1719 TimeDelta::FromMilliseconds(1000));
1720 controller_impl->UpdateState(true, events.get());
1722 // The opacity animation should be finished, but should not have generated
1723 // a Finished event yet.
1724 EXPECT_EQ(0u, events->size());
1725 EXPECT_EQ(Animation::Finished,
1726 controller_impl->GetAnimation(1, Animation::Opacity)->run_state());
1727 EXPECT_EQ(Animation::Running,
1728 controller_impl->GetAnimation(1,
1729 Animation::Transform)->run_state());
1731 controller_impl->Animate(kInitialTickTime +
1732 TimeDelta::FromMilliseconds(2000));
1733 controller_impl->UpdateState(true, events.get());
1735 // Both animations should have generated Finished events.
1736 EXPECT_EQ(2u, events->size());
1737 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1738 EXPECT_EQ(AnimationEvent::Finished, (*events)[1].type);
1741 // Ensure that when a group has a mix of aborted and finished animations,
1742 // we generate a Finished event for the finished animation and an Aborted
1743 // event for the aborted animation.
1744 TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
1745 scoped_ptr<AnimationEventsVector> events(
1746 make_scoped_ptr(new AnimationEventsVector));
1747 FakeLayerAnimationValueObserver dummy_impl;
1748 scoped_refptr<LayerAnimationController> controller_impl(
1749 LayerAnimationController::Create(0));
1750 controller_impl->AddValueObserver(&dummy_impl);
1752 // Add two animations with the same group id.
1753 controller_impl->AddAnimation(CreateAnimation(
1754 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1756 Animation::Transform));
1757 controller_impl->AddAnimation(CreateAnimation(
1758 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1760 Animation::Opacity));
1762 controller_impl->Animate(kInitialTickTime);
1763 controller_impl->UpdateState(true, events.get());
1765 // Both animations should have started.
1766 EXPECT_EQ(2u, events->size());
1767 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1768 EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1770 controller_impl->AbortAnimations(Animation::Opacity);
1772 events.reset(new AnimationEventsVector);
1773 controller_impl->Animate(kInitialTickTime +
1774 TimeDelta::FromMilliseconds(1000));
1775 controller_impl->UpdateState(true, events.get());
1777 // We should have exactly 2 events: a Finished event for the tranform
1778 // animation, and an Aborted event for the opacity animation.
1779 EXPECT_EQ(2u, events->size());
1780 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1781 EXPECT_EQ(Animation::Transform, (*events)[0].target_property);
1782 EXPECT_EQ(AnimationEvent::Aborted, (*events)[1].type);
1783 EXPECT_EQ(Animation::Opacity, (*events)[1].target_property);
1786 TEST(LayerAnimationControllerTest, HasAnimationThatAffectsScale) {
1787 scoped_refptr<LayerAnimationController> controller_impl(
1788 LayerAnimationController::Create(0));
1790 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1792 controller_impl->AddAnimation(CreateAnimation(
1793 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1795 Animation::Opacity));
1797 // Opacity animations don't affect scale.
1798 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1800 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1801 KeyframedTransformAnimationCurve::Create());
1803 TransformOperations operations1;
1804 curve1->AddKeyframe(TransformKeyframe::Create(
1805 0.0, operations1, scoped_ptr<TimingFunction>()));
1806 operations1.AppendTranslate(10.0, 15.0, 0.0);
1807 curve1->AddKeyframe(TransformKeyframe::Create(
1808 1.0, operations1, scoped_ptr<TimingFunction>()));
1810 scoped_ptr<Animation> animation(Animation::Create(
1811 curve1.PassAs<AnimationCurve>(), 2, 2, Animation::Transform));
1812 controller_impl->AddAnimation(animation.Pass());
1814 // Translations don't affect scale.
1815 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1817 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1818 KeyframedTransformAnimationCurve::Create());
1820 TransformOperations operations2;
1821 curve2->AddKeyframe(TransformKeyframe::Create(
1822 0.0, operations2, scoped_ptr<TimingFunction>()));
1823 operations2.AppendScale(2.0, 3.0, 4.0);
1824 curve2->AddKeyframe(TransformKeyframe::Create(
1825 1.0, operations2, scoped_ptr<TimingFunction>()));
1827 animation = Animation::Create(
1828 curve2.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1829 controller_impl->AddAnimation(animation.Pass());
1831 EXPECT_TRUE(controller_impl->HasAnimationThatAffectsScale());
1833 controller_impl->GetAnimation(3, Animation::Transform)
1834 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1836 // Only unfinished animations should be considered by
1837 // HasAnimationThatAffectsScale.
1838 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1841 TEST(LayerAnimationControllerTest, HasOnlyTranslationTransforms) {
1842 scoped_refptr<LayerAnimationController> controller_impl(
1843 LayerAnimationController::Create(0));
1845 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1847 controller_impl->AddAnimation(CreateAnimation(
1848 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1850 Animation::Opacity));
1852 // Opacity animations aren't non-translation transforms.
1853 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1855 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1856 KeyframedTransformAnimationCurve::Create());
1858 TransformOperations operations1;
1859 curve1->AddKeyframe(TransformKeyframe::Create(
1860 0.0, operations1, scoped_ptr<TimingFunction>()));
1861 operations1.AppendTranslate(10.0, 15.0, 0.0);
1862 curve1->AddKeyframe(TransformKeyframe::Create(
1863 1.0, operations1, scoped_ptr<TimingFunction>()));
1865 scoped_ptr<Animation> animation(Animation::Create(
1866 curve1.PassAs<AnimationCurve>(), 2, 2, Animation::Transform));
1867 controller_impl->AddAnimation(animation.Pass());
1869 // The only transform animation we've added is a translation.
1870 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1872 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1873 KeyframedTransformAnimationCurve::Create());
1875 TransformOperations operations2;
1876 curve2->AddKeyframe(TransformKeyframe::Create(
1877 0.0, operations2, scoped_ptr<TimingFunction>()));
1878 operations2.AppendScale(2.0, 3.0, 4.0);
1879 curve2->AddKeyframe(TransformKeyframe::Create(
1880 1.0, operations2, scoped_ptr<TimingFunction>()));
1882 animation = Animation::Create(
1883 curve2.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1884 controller_impl->AddAnimation(animation.Pass());
1886 // A scale animation is not a translation.
1887 EXPECT_FALSE(controller_impl->HasOnlyTranslationTransforms());
1889 controller_impl->GetAnimation(3, Animation::Transform)
1890 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1892 // Only unfinished animations should be considered by
1893 // HasOnlyTranslationTransforms.
1894 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1897 TEST(LayerAnimationControllerTest, MaximumScale) {
1898 scoped_refptr<LayerAnimationController> controller_impl(
1899 LayerAnimationController::Create(0));
1901 float max_scale = 0.f;
1902 EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
1903 EXPECT_EQ(0.f, max_scale);
1905 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1906 KeyframedTransformAnimationCurve::Create());
1908 TransformOperations operations1;
1909 curve1->AddKeyframe(TransformKeyframe::Create(
1910 0.0, operations1, scoped_ptr<TimingFunction>()));
1911 operations1.AppendScale(2.0, 3.0, 4.0);
1912 curve1->AddKeyframe(TransformKeyframe::Create(
1913 1.0, operations1, scoped_ptr<TimingFunction>()));
1915 scoped_ptr<Animation> animation(Animation::Create(
1916 curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1917 controller_impl->AddAnimation(animation.Pass());
1919 EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
1920 EXPECT_EQ(4.f, max_scale);
1922 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1923 KeyframedTransformAnimationCurve::Create());
1925 TransformOperations operations2;
1926 curve2->AddKeyframe(TransformKeyframe::Create(
1927 0.0, operations2, scoped_ptr<TimingFunction>()));
1928 operations2.AppendScale(6.0, 5.0, 4.0);
1929 curve2->AddKeyframe(TransformKeyframe::Create(
1930 1.0, operations2, scoped_ptr<TimingFunction>()));
1932 animation = Animation::Create(
1933 curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
1934 controller_impl->AddAnimation(animation.Pass());
1936 EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
1937 EXPECT_EQ(6.f, max_scale);
1939 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1940 KeyframedTransformAnimationCurve::Create());
1942 TransformOperations operations3;
1943 curve3->AddKeyframe(TransformKeyframe::Create(
1944 0.0, operations3, scoped_ptr<TimingFunction>()));
1945 operations3.AppendPerspective(6.0);
1946 curve3->AddKeyframe(TransformKeyframe::Create(
1947 1.0, operations3, scoped_ptr<TimingFunction>()));
1949 animation = Animation::Create(
1950 curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1951 controller_impl->AddAnimation(animation.Pass());
1953 EXPECT_FALSE(controller_impl->MaximumScale(&max_scale));
1955 controller_impl->GetAnimation(3, Animation::Transform)
1956 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1957 controller_impl->GetAnimation(2, Animation::Transform)
1958 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1960 // Only unfinished animations should be considered by
1962 EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
1963 EXPECT_EQ(4.f, max_scale);
1966 TEST(LayerAnimationControllerTest, NewlyPushedAnimationWaitsForActivation) {
1967 scoped_ptr<AnimationEventsVector> events(
1968 make_scoped_ptr(new AnimationEventsVector));
1969 FakeLayerAnimationValueObserver dummy_impl;
1970 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
1971 scoped_refptr<LayerAnimationController> controller_impl(
1972 LayerAnimationController::Create(0));
1973 controller_impl->AddValueObserver(&dummy_impl);
1974 controller_impl->AddValueObserver(&pending_dummy_impl);
1975 FakeLayerAnimationValueObserver dummy;
1976 scoped_refptr<LayerAnimationController> controller(
1977 LayerAnimationController::Create(0));
1978 controller->AddValueObserver(&dummy);
1980 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1981 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, false);
1982 int group_id = controller->GetAnimation(Animation::Opacity)->group();
1983 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1985 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
1986 controller->PushAnimationUpdatesTo(controller_impl.get());
1987 EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
1989 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1991 Animation::WaitingForTargetAvailability,
1992 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
1993 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
1994 ->affects_pending_observers());
1995 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
1996 ->affects_active_observers());
1998 controller_impl->Animate(kInitialTickTime);
1999 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2000 controller_impl->UpdateState(true, events.get());
2002 // Since the animation hasn't been activated, it should still be Starting
2003 // rather than Running.
2005 Animation::Starting,
2006 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2008 // Since the animation hasn't been activated, only the pending observer
2009 // should have been ticked.
2010 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2011 EXPECT_EQ(0.f, dummy_impl.opacity());
2013 controller_impl->ActivateAnimations();
2014 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2015 ->affects_pending_observers());
2016 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2017 ->affects_active_observers());
2019 controller_impl->Animate(kInitialTickTime +
2020 TimeDelta::FromMilliseconds(1000));
2021 controller_impl->UpdateState(true, events.get());
2023 // Since the animation has been activated, it should have reached the
2024 // Running state and the active observer should start to get ticked.
2027 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2028 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2029 EXPECT_EQ(0.5f, dummy_impl.opacity());
2032 TEST(LayerAnimationControllerTest, ActivationBetweenAnimateAndUpdateState) {
2033 scoped_ptr<AnimationEventsVector> events(
2034 make_scoped_ptr(new AnimationEventsVector));
2035 FakeLayerAnimationValueObserver dummy_impl;
2036 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2037 scoped_refptr<LayerAnimationController> controller_impl(
2038 LayerAnimationController::Create(0));
2039 controller_impl->AddValueObserver(&dummy_impl);
2040 controller_impl->AddValueObserver(&pending_dummy_impl);
2041 FakeLayerAnimationValueObserver dummy;
2042 scoped_refptr<LayerAnimationController> controller(
2043 LayerAnimationController::Create(0));
2044 controller->AddValueObserver(&dummy);
2046 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2047 int group_id = controller->GetAnimation(Animation::Opacity)->group();
2049 controller->PushAnimationUpdatesTo(controller_impl.get());
2051 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
2053 Animation::WaitingForTargetAvailability,
2054 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2055 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2056 ->affects_pending_observers());
2057 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2058 ->affects_active_observers());
2060 controller_impl->Animate(kInitialTickTime);
2062 // Since the animation hasn't been activated, only the pending observer
2063 // should have been ticked.
2064 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2065 EXPECT_EQ(0.f, dummy_impl.opacity());
2067 controller_impl->ActivateAnimations();
2068 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2069 ->affects_pending_observers());
2070 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2071 ->affects_active_observers());
2073 controller_impl->UpdateState(true, events.get());
2075 // Since the animation has been activated, it should have reached the
2079 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2081 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2083 // Both observers should have been ticked.
2084 EXPECT_EQ(0.75f, pending_dummy_impl.opacity());
2085 EXPECT_EQ(0.75f, dummy_impl.opacity());
2088 TEST(LayerAnimationControllerTest, ClippedOpacityValues) {
2089 FakeLayerAnimationValueObserver dummy;
2090 scoped_refptr<LayerAnimationController> controller(
2091 LayerAnimationController::Create(0));
2092 controller->AddValueObserver(&dummy);
2094 AddOpacityTransitionToController(controller.get(), 1, 1.f, 2.f, true);
2096 controller->Animate(kInitialTickTime);
2097 EXPECT_EQ(1.f, dummy.opacity());
2099 // Opacity values are clipped [0,1]
2100 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2101 EXPECT_EQ(1.f, dummy.opacity());
2104 TEST(LayerAnimationControllerTest, ClippedNegativeOpacityValues) {
2105 FakeLayerAnimationValueObserver dummy;
2106 scoped_refptr<LayerAnimationController> controller(
2107 LayerAnimationController::Create(0));
2108 controller->AddValueObserver(&dummy);
2110 AddOpacityTransitionToController(controller.get(), 1, 0.f, -2.f, true);
2112 controller->Animate(kInitialTickTime);
2113 EXPECT_EQ(0.f, dummy.opacity());
2115 // Opacity values are clipped [0,1]
2116 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2117 EXPECT_EQ(0.f, dummy.opacity());
2120 TEST(LayerAnimationControllerTest, PushedDeletedAnimationWaitsForActivation) {
2121 scoped_ptr<AnimationEventsVector> events(
2122 make_scoped_ptr(new AnimationEventsVector));
2123 FakeLayerAnimationValueObserver dummy_impl;
2124 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2125 scoped_refptr<LayerAnimationController> controller_impl(
2126 LayerAnimationController::Create(0));
2127 controller_impl->AddValueObserver(&dummy_impl);
2128 controller_impl->AddValueObserver(&pending_dummy_impl);
2129 FakeLayerAnimationValueObserver dummy;
2130 scoped_refptr<LayerAnimationController> controller(
2131 LayerAnimationController::Create(0));
2132 controller->AddValueObserver(&dummy);
2134 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2135 int group_id = controller->GetAnimation(Animation::Opacity)->group();
2137 controller->PushAnimationUpdatesTo(controller_impl.get());
2138 controller_impl->ActivateAnimations();
2139 controller_impl->Animate(kInitialTickTime);
2140 controller_impl->UpdateState(true, events.get());
2143 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2144 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2145 EXPECT_EQ(0.5f, dummy_impl.opacity());
2147 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2148 ->affects_pending_observers());
2149 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2150 ->affects_active_observers());
2152 // Delete the animation on the main-thread controller.
2153 controller->RemoveAnimation(
2154 controller->GetAnimation(Animation::Opacity)->id());
2155 controller->PushAnimationUpdatesTo(controller_impl.get());
2157 // The animation should no longer affect pending observers.
2158 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2159 ->affects_pending_observers());
2160 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2161 ->affects_active_observers());
2163 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2164 controller_impl->UpdateState(true, events.get());
2166 // Only the active observer should have been ticked.
2167 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2168 EXPECT_EQ(0.75f, dummy_impl.opacity());
2170 controller_impl->ActivateAnimations();
2172 // Activation should cause the animation to be deleted.
2173 EXPECT_FALSE(controller_impl->has_any_animation());
2176 // Tests that an animation that affects only active observers won't block
2177 // an animation that affects only pending observers from starting.
2178 TEST(LayerAnimationControllerTest, StartAnimationsAffectingDifferentObservers) {
2179 scoped_ptr<AnimationEventsVector> events(
2180 make_scoped_ptr(new AnimationEventsVector));
2181 FakeLayerAnimationValueObserver dummy_impl;
2182 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2183 scoped_refptr<LayerAnimationController> controller_impl(
2184 LayerAnimationController::Create(0));
2185 controller_impl->AddValueObserver(&dummy_impl);
2186 controller_impl->AddValueObserver(&pending_dummy_impl);
2187 FakeLayerAnimationValueObserver dummy;
2188 scoped_refptr<LayerAnimationController> controller(
2189 LayerAnimationController::Create(0));
2190 controller->AddValueObserver(&dummy);
2192 AddOpacityTransitionToController(controller.get(), 1, 0.f, 1.f, true);
2193 int first_animation_group_id =
2194 controller->GetAnimation(Animation::Opacity)->group();
2196 controller->PushAnimationUpdatesTo(controller_impl.get());
2197 controller_impl->ActivateAnimations();
2198 controller_impl->Animate(kInitialTickTime);
2199 controller_impl->UpdateState(true, events.get());
2201 // Remove the first animation from the main-thread controller, and add a
2202 // new animation affecting the same property.
2203 controller->RemoveAnimation(
2204 controller->GetAnimation(Animation::Opacity)->id());
2205 AddOpacityTransitionToController(controller.get(), 1, 1.f, 0.5f, true);
2206 int second_animation_group_id =
2207 controller->GetAnimation(Animation::Opacity)->group();
2208 controller->PushAnimationUpdatesTo(controller_impl.get());
2210 // The original animation should only affect active observers, and the new
2211 // animation should only affect pending observers.
2212 EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
2214 ->affects_pending_observers());
2215 EXPECT_TRUE(controller_impl->GetAnimation(first_animation_group_id,
2217 ->affects_active_observers());
2218 EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2220 ->affects_pending_observers());
2221 EXPECT_FALSE(controller_impl->GetAnimation(second_animation_group_id,
2223 ->affects_active_observers());
2225 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2226 controller_impl->UpdateState(true, events.get());
2228 // The original animation should still be running, and the new animation
2229 // should be starting.
2230 EXPECT_EQ(Animation::Running,
2231 controller_impl->GetAnimation(first_animation_group_id,
2232 Animation::Opacity)->run_state());
2233 EXPECT_EQ(Animation::Starting,
2234 controller_impl->GetAnimation(second_animation_group_id,
2235 Animation::Opacity)->run_state());
2237 // The active observer should have been ticked by the original animation,
2238 // and the pending observer should have been ticked by the new animation.
2239 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2240 EXPECT_EQ(0.5f, dummy_impl.opacity());
2242 controller_impl->ActivateAnimations();
2244 // The original animation should have been deleted, and the new animation
2245 // should now affect both observers.
2246 EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
2247 Animation::Opacity));
2248 EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2250 ->affects_pending_observers());
2251 EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2253 ->affects_active_observers());
2255 controller_impl->Animate(kInitialTickTime +
2256 TimeDelta::FromMilliseconds(1000));
2257 controller_impl->UpdateState(true, events.get());
2259 // The new animation should be running, and the active observer should have
2260 // been ticked at the new animation's starting point.
2261 EXPECT_EQ(Animation::Running,
2262 controller_impl->GetAnimation(second_animation_group_id,
2263 Animation::Opacity)->run_state());
2264 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2265 EXPECT_EQ(1.f, dummy_impl.opacity());