Ozone: Temporarily remove DCHECK in BufferFactory.
[chromium-blink-merge.git] / cc / animation / layer_animation_controller_unittest.cc
blob464a973704e6db4fac9e0d38578dfea94e02b3ea
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/animation/layer_animation_controller.h"
7 #include "cc/animation/animation.h"
8 #include "cc/animation/animation_curve.h"
9 #include "cc/animation/animation_delegate.h"
10 #include "cc/animation/animation_registrar.h"
11 #include "cc/animation/keyframed_animation_curve.h"
12 #include "cc/animation/scroll_offset_animation_curve.h"
13 #include "cc/animation/transform_operations.h"
14 #include "cc/test/animation_test_common.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/box_f.h"
18 #include "ui/gfx/transform.h"
20 namespace cc {
21 namespace {
23 using base::TimeDelta;
24 using base::TimeTicks;
26 static base::TimeTicks TicksFromSecondsF(double seconds) {
27 return base::TimeTicks::FromInternalValue(seconds *
28 base::Time::kMicrosecondsPerSecond);
31 // A LayerAnimationController cannot be ticked at 0.0, since an animation
32 // with start time 0.0 is treated as an animation whose start time has
33 // not yet been set.
34 const TimeTicks kInitialTickTime = TicksFromSecondsF(1.0);
36 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve,
37 int id,
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, nullptr);
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, nullptr);
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, nullptr);
214 EXPECT_EQ(1u, registrar->active_animation_controllers().size());
216 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
217 controller->UpdateState(true, nullptr);
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, nullptr);
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(nullptr);
250 controller_impl->SetAnimationRegistrar(nullptr);
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, nullptr);
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(
292 animation_id,
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));
318 int animation_id =
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,
333 group_id,
334 Animation::Opacity,
335 kInitialTickTime);
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, nullptr);
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, nullptr);
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, nullptr);
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];
425 return event;
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());
451 EXPECT_FALSE(event);
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());
457 EXPECT_FALSE(event);
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;
513 curve->AddKeyframe(
514 TransformKeyframe::Create(base::TimeDelta(), operations, nullptr));
515 operations.AppendTranslate(delta_x, delta_y, 0);
516 curve->AddKeyframe(TransformKeyframe::Create(
517 base::TimeDelta::FromSecondsD(1.0), operations, nullptr));
519 scoped_ptr<Animation> animation(
520 Animation::Create(curve.Pass(), 1, 0, Animation::Transform));
521 animation->set_is_impl_only(true);
522 controller_impl->AddAnimation(animation.Pass());
524 // Run animation.
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));
564 curve->AddKeyframe(
565 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
566 FilterOperations end_filters;
567 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
568 curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
569 end_filters, nullptr));
571 scoped_ptr<Animation> animation(
572 Animation::Create(curve.Pass(), 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());
581 EXPECT_FALSE(event);
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());
589 EXPECT_FALSE(event);
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());
596 EXPECT_FALSE(event);
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));
613 curve->AddKeyframe(
614 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
615 FilterOperations end_filters;
616 end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
617 curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
618 end_filters, nullptr));
620 scoped_ptr<Animation> animation(
621 Animation::Create(curve.Pass(), 1, 0, Animation::Filter));
622 animation->set_is_impl_only(true);
623 controller_impl->AddAnimation(animation.Pass());
625 // Run animation.
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::ScrollOffset initial_value(100.f, 300.f);
667 gfx::ScrollOffset target_value(300.f, 200.f);
668 scoped_ptr<ScrollOffsetAnimationCurve> curve(
669 ScrollOffsetAnimationCurve::Create(
670 target_value,
671 EaseInOutTimingFunction::Create().Pass()));
673 scoped_ptr<Animation> animation(
674 Animation::Create(curve.Pass(), 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 TimeDelta duration = controller_impl->GetAnimation(Animation::ScrollOffset)
683 ->curve()
684 ->Duration();
685 EXPECT_EQ(
686 duration,
687 controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
689 controller->Animate(kInitialTickTime);
690 controller->UpdateState(true, nullptr);
691 EXPECT_TRUE(controller->HasActiveAnimation());
692 EXPECT_EQ(initial_value, dummy.scroll_offset());
694 controller_impl->Animate(kInitialTickTime);
695 controller_impl->UpdateState(true, events.get());
696 EXPECT_TRUE(controller_impl->HasActiveAnimation());
697 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
698 // Scroll offset animations should not generate property updates.
699 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
700 EXPECT_FALSE(event);
702 controller->NotifyAnimationStarted((*events)[0]);
703 controller->Animate(kInitialTickTime + duration / 2);
704 controller->UpdateState(true, nullptr);
705 EXPECT_TRUE(controller->HasActiveAnimation());
706 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
708 controller_impl->Animate(kInitialTickTime + duration / 2);
709 controller_impl->UpdateState(true, events.get());
710 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
711 dummy_impl.scroll_offset());
712 event = GetMostRecentPropertyUpdateEvent(events.get());
713 EXPECT_FALSE(event);
715 controller_impl->Animate(kInitialTickTime + duration);
716 controller_impl->UpdateState(true, events.get());
717 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
718 EXPECT_FALSE(controller_impl->HasActiveAnimation());
719 event = GetMostRecentPropertyUpdateEvent(events.get());
720 EXPECT_FALSE(event);
722 controller->Animate(kInitialTickTime + duration);
723 controller->UpdateState(true, nullptr);
724 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
725 EXPECT_FALSE(controller->HasActiveAnimation());
728 // Ensure that when the impl controller doesn't have a value provider,
729 // the main-thread controller's value provider is used to obtain the intial
730 // scroll offset.
731 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
732 FakeLayerAnimationValueObserver dummy_impl;
733 scoped_refptr<LayerAnimationController> controller_impl(
734 LayerAnimationController::Create(0));
735 controller_impl->AddValueObserver(&dummy_impl);
736 scoped_ptr<AnimationEventsVector> events(
737 make_scoped_ptr(new AnimationEventsVector));
738 FakeLayerAnimationValueObserver dummy;
739 FakeLayerAnimationValueProvider dummy_provider;
740 scoped_refptr<LayerAnimationController> controller(
741 LayerAnimationController::Create(0));
742 controller->AddValueObserver(&dummy);
743 controller->set_value_provider(&dummy_provider);
745 gfx::ScrollOffset initial_value(500.f, 100.f);
746 gfx::ScrollOffset target_value(300.f, 200.f);
747 scoped_ptr<ScrollOffsetAnimationCurve> curve(
748 ScrollOffsetAnimationCurve::Create(
749 target_value,
750 EaseInOutTimingFunction::Create().Pass()));
752 scoped_ptr<Animation> animation(
753 Animation::Create(curve.Pass(), 1, 0, Animation::ScrollOffset));
754 animation->set_needs_synchronized_start_time(true);
755 controller->AddAnimation(animation.Pass());
757 dummy_provider.set_scroll_offset(initial_value);
758 controller->PushAnimationUpdatesTo(controller_impl.get());
759 controller_impl->ActivateAnimations();
760 EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
761 TimeDelta duration = controller_impl->GetAnimation(Animation::ScrollOffset)
762 ->curve()
763 ->Duration();
764 EXPECT_EQ(
765 duration,
766 controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
768 controller->Animate(kInitialTickTime);
769 controller->UpdateState(true, nullptr);
770 EXPECT_TRUE(controller->HasActiveAnimation());
771 EXPECT_EQ(initial_value, dummy.scroll_offset());
773 controller_impl->Animate(kInitialTickTime);
774 controller_impl->UpdateState(true, events.get());
775 EXPECT_TRUE(controller_impl->HasActiveAnimation());
776 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
777 // Scroll offset animations should not generate property updates.
778 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
779 EXPECT_FALSE(event);
782 controller->NotifyAnimationStarted((*events)[0]);
783 controller->Animate(kInitialTickTime + duration / 2);
784 controller->UpdateState(true, nullptr);
785 EXPECT_TRUE(controller->HasActiveAnimation());
786 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
788 controller_impl->Animate(kInitialTickTime + duration / 2);
789 controller_impl->UpdateState(true, events.get());
790 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
791 dummy_impl.scroll_offset());
792 event = GetMostRecentPropertyUpdateEvent(events.get());
793 EXPECT_FALSE(event);
795 controller_impl->Animate(kInitialTickTime + duration);
796 controller_impl->UpdateState(true, events.get());
797 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
798 EXPECT_FALSE(controller_impl->HasActiveAnimation());
799 event = GetMostRecentPropertyUpdateEvent(events.get());
800 EXPECT_FALSE(event);
802 controller->Animate(kInitialTickTime + duration);
803 controller->UpdateState(true, nullptr);
804 EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
805 EXPECT_FALSE(controller->HasActiveAnimation());
808 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
809 FakeLayerAnimationValueObserver dummy_impl;
810 scoped_refptr<LayerAnimationController> controller_impl(
811 LayerAnimationController::Create(0));
812 controller_impl->AddValueObserver(&dummy_impl);
813 scoped_ptr<AnimationEventsVector> events(
814 make_scoped_ptr(new AnimationEventsVector));
816 gfx::ScrollOffset initial_value(100.f, 300.f);
817 gfx::ScrollOffset target_value(300.f, 200.f);
818 scoped_ptr<ScrollOffsetAnimationCurve> curve(
819 ScrollOffsetAnimationCurve::Create(
820 target_value,
821 EaseInOutTimingFunction::Create().Pass()));
822 curve->SetInitialValue(initial_value);
823 double duration_in_seconds = curve->Duration().InSecondsF();
825 scoped_ptr<Animation> animation(
826 Animation::Create(curve.Pass(), 1, 0, Animation::ScrollOffset));
827 animation->set_is_impl_only(true);
828 controller_impl->AddAnimation(animation.Pass());
830 controller_impl->Animate(kInitialTickTime);
831 controller_impl->UpdateState(true, events.get());
832 EXPECT_TRUE(controller_impl->HasActiveAnimation());
833 EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
834 // Scroll offset animations should not generate property updates.
835 const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
836 EXPECT_FALSE(event);
838 TimeDelta duration = TimeDelta::FromMicroseconds(
839 duration_in_seconds * base::Time::kMicrosecondsPerSecond);
841 controller_impl->Animate(kInitialTickTime + duration / 2);
842 controller_impl->UpdateState(true, events.get());
843 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
844 dummy_impl.scroll_offset());
845 event = GetMostRecentPropertyUpdateEvent(events.get());
846 EXPECT_FALSE(event);
848 controller_impl->Animate(kInitialTickTime + duration);
849 controller_impl->UpdateState(true, events.get());
850 EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
851 EXPECT_FALSE(controller_impl->HasActiveAnimation());
852 event = GetMostRecentPropertyUpdateEvent(events.get());
853 EXPECT_FALSE(event);
856 class FakeAnimationDelegate : public AnimationDelegate {
857 public:
858 FakeAnimationDelegate()
859 : started_(false),
860 finished_(false) {}
862 void NotifyAnimationStarted(TimeTicks monotonic_time,
863 Animation::TargetProperty target_property,
864 int group) override {
865 started_ = true;
868 void NotifyAnimationFinished(TimeTicks monotonic_time,
869 Animation::TargetProperty target_property,
870 int group) override {
871 finished_ = true;
874 bool started() { return started_; }
876 bool finished() { return finished_; }
878 private:
879 bool started_;
880 bool finished_;
883 // Tests that impl-only animations lead to start and finished notifications
884 // on the impl thread controller's animation delegate.
885 TEST(LayerAnimationControllerTest,
886 NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate) {
887 FakeLayerAnimationValueObserver dummy_impl;
888 scoped_refptr<LayerAnimationController> controller_impl(
889 LayerAnimationController::Create(0));
890 controller_impl->AddValueObserver(&dummy_impl);
891 scoped_ptr<AnimationEventsVector> events(
892 make_scoped_ptr(new AnimationEventsVector));
893 FakeAnimationDelegate delegate;
894 controller_impl->set_layer_animation_delegate(&delegate);
896 scoped_ptr<Animation> to_add(CreateAnimation(
897 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
899 Animation::Opacity));
900 to_add->set_is_impl_only(true);
901 controller_impl->AddAnimation(to_add.Pass());
903 EXPECT_FALSE(delegate.started());
904 EXPECT_FALSE(delegate.finished());
906 controller_impl->Animate(kInitialTickTime);
907 controller_impl->UpdateState(true, events.get());
909 EXPECT_TRUE(delegate.started());
910 EXPECT_FALSE(delegate.finished());
912 events.reset(new AnimationEventsVector);
913 controller_impl->Animate(kInitialTickTime +
914 TimeDelta::FromMilliseconds(1000));
915 controller_impl->UpdateState(true, events.get());
917 EXPECT_TRUE(delegate.started());
918 EXPECT_TRUE(delegate.finished());
921 // Tests animations that are waiting for a synchronized start time do not
922 // finish.
923 TEST(LayerAnimationControllerTest,
924 AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
925 scoped_ptr<AnimationEventsVector> events(
926 make_scoped_ptr(new AnimationEventsVector));
927 FakeLayerAnimationValueObserver dummy;
928 scoped_refptr<LayerAnimationController> controller(
929 LayerAnimationController::Create(0));
930 controller->AddValueObserver(&dummy);
932 scoped_ptr<Animation> to_add(CreateAnimation(
933 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
935 Animation::Opacity));
936 to_add->set_needs_synchronized_start_time(true);
938 // We should pause at the first keyframe indefinitely waiting for that
939 // animation to start.
940 controller->AddAnimation(to_add.Pass());
941 controller->Animate(kInitialTickTime);
942 controller->UpdateState(true, events.get());
943 EXPECT_TRUE(controller->HasActiveAnimation());
944 EXPECT_EQ(0.f, dummy.opacity());
945 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
946 controller->UpdateState(true, events.get());
947 EXPECT_TRUE(controller->HasActiveAnimation());
948 EXPECT_EQ(0.f, dummy.opacity());
949 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
950 controller->UpdateState(true, events.get());
951 EXPECT_TRUE(controller->HasActiveAnimation());
952 EXPECT_EQ(0.f, dummy.opacity());
954 // Send the synchronized start time.
955 controller->NotifyAnimationStarted(
956 AnimationEvent(AnimationEvent::Started,
959 Animation::Opacity,
960 kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
961 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
962 controller->UpdateState(true, events.get());
963 EXPECT_EQ(1.f, dummy.opacity());
964 EXPECT_FALSE(controller->HasActiveAnimation());
967 // Tests that two queued animations affecting the same property run in sequence.
968 TEST(LayerAnimationControllerTest, TrivialQueuing) {
969 scoped_ptr<AnimationEventsVector> events(
970 make_scoped_ptr(new AnimationEventsVector));
971 FakeLayerAnimationValueObserver dummy;
972 scoped_refptr<LayerAnimationController> controller(
973 LayerAnimationController::Create(0));
974 controller->AddValueObserver(&dummy);
976 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
978 controller->AddAnimation(CreateAnimation(
979 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
981 Animation::Opacity));
982 controller->AddAnimation(CreateAnimation(
983 scoped_ptr<AnimationCurve>(
984 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
986 Animation::Opacity));
988 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
990 controller->Animate(kInitialTickTime);
992 // The second animation still needs to be started.
993 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
995 controller->UpdateState(true, events.get());
996 EXPECT_TRUE(controller->HasActiveAnimation());
997 EXPECT_EQ(0.f, dummy.opacity());
999 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1000 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1001 controller->UpdateState(true, events.get());
1002 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1004 EXPECT_TRUE(controller->HasActiveAnimation());
1005 EXPECT_EQ(1.f, dummy.opacity());
1006 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1007 controller->UpdateState(true, events.get());
1008 EXPECT_EQ(0.5f, dummy.opacity());
1009 EXPECT_FALSE(controller->HasActiveAnimation());
1012 // Tests interrupting a transition with another transition.
1013 TEST(LayerAnimationControllerTest, Interrupt) {
1014 scoped_ptr<AnimationEventsVector> events(
1015 make_scoped_ptr(new AnimationEventsVector));
1016 FakeLayerAnimationValueObserver dummy;
1017 scoped_refptr<LayerAnimationController> controller(
1018 LayerAnimationController::Create(0));
1019 controller->AddValueObserver(&dummy);
1020 controller->AddAnimation(CreateAnimation(
1021 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1023 Animation::Opacity));
1024 controller->Animate(kInitialTickTime);
1025 controller->UpdateState(true, events.get());
1026 EXPECT_TRUE(controller->HasActiveAnimation());
1027 EXPECT_EQ(0.f, dummy.opacity());
1029 scoped_ptr<Animation> to_add(CreateAnimation(
1030 scoped_ptr<AnimationCurve>(
1031 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1033 Animation::Opacity));
1034 controller->AbortAnimations(Animation::Opacity);
1035 controller->AddAnimation(to_add.Pass());
1037 // Since the previous animation was aborted, the new animation should start
1038 // right in this call to animate.
1039 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1040 controller->UpdateState(true, events.get());
1041 EXPECT_TRUE(controller->HasActiveAnimation());
1042 EXPECT_EQ(1.f, dummy.opacity());
1043 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1044 controller->UpdateState(true, events.get());
1045 EXPECT_EQ(0.5f, dummy.opacity());
1046 EXPECT_FALSE(controller->HasActiveAnimation());
1049 // Tests scheduling two animations to run together when only one property is
1050 // free.
1051 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
1052 scoped_ptr<AnimationEventsVector> events(
1053 make_scoped_ptr(new AnimationEventsVector));
1054 FakeLayerAnimationValueObserver dummy;
1055 scoped_refptr<LayerAnimationController> controller(
1056 LayerAnimationController::Create(0));
1057 controller->AddValueObserver(&dummy);
1059 controller->AddAnimation(CreateAnimation(
1060 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1062 Animation::Transform));
1063 controller->AddAnimation(CreateAnimation(
1064 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1066 Animation::Transform));
1067 controller->AddAnimation(CreateAnimation(
1068 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1070 Animation::Opacity));
1072 controller->Animate(kInitialTickTime);
1073 controller->UpdateState(true, events.get());
1074 EXPECT_EQ(0.f, dummy.opacity());
1075 EXPECT_TRUE(controller->HasActiveAnimation());
1076 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1077 controller->UpdateState(true, events.get());
1078 // Should not have started the float transition yet.
1079 EXPECT_TRUE(controller->HasActiveAnimation());
1080 EXPECT_EQ(0.f, dummy.opacity());
1081 // The float animation should have started at time 1 and should be done.
1082 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1083 controller->UpdateState(true, events.get());
1084 EXPECT_EQ(1.f, dummy.opacity());
1085 EXPECT_FALSE(controller->HasActiveAnimation());
1088 // Tests scheduling two animations to run together with different lengths and
1089 // another animation queued to start when the shorter animation finishes (should
1090 // wait for both to finish).
1091 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
1092 scoped_ptr<AnimationEventsVector> events(
1093 make_scoped_ptr(new AnimationEventsVector));
1094 FakeLayerAnimationValueObserver dummy;
1095 scoped_refptr<LayerAnimationController> controller(
1096 LayerAnimationController::Create(0));
1097 controller->AddValueObserver(&dummy);
1099 controller->AddAnimation(CreateAnimation(
1100 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
1102 Animation::Transform));
1103 controller->AddAnimation(CreateAnimation(
1104 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1106 Animation::Opacity));
1107 controller->AddAnimation(CreateAnimation(
1108 scoped_ptr<AnimationCurve>(
1109 new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1111 Animation::Opacity));
1113 // Animations with id 1 should both start now.
1114 controller->Animate(kInitialTickTime);
1115 controller->UpdateState(true, events.get());
1116 EXPECT_TRUE(controller->HasActiveAnimation());
1117 EXPECT_EQ(0.f, dummy.opacity());
1118 // The opacity animation should have finished at time 1, but the group
1119 // of animations with id 1 don't finish until time 2 because of the length
1120 // of the transform animation.
1121 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1122 controller->UpdateState(true, events.get());
1123 // Should not have started the float transition yet.
1124 EXPECT_TRUE(controller->HasActiveAnimation());
1125 EXPECT_EQ(1.f, dummy.opacity());
1127 // The second opacity animation should start at time 2 and should be done by
1128 // time 3.
1129 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1130 controller->UpdateState(true, events.get());
1131 EXPECT_EQ(0.5f, dummy.opacity());
1132 EXPECT_FALSE(controller->HasActiveAnimation());
1135 // Test that a looping animation loops and for the correct number of iterations.
1136 TEST(LayerAnimationControllerTest, TrivialLooping) {
1137 scoped_ptr<AnimationEventsVector> events(
1138 make_scoped_ptr(new AnimationEventsVector));
1139 FakeLayerAnimationValueObserver dummy;
1140 scoped_refptr<LayerAnimationController> controller(
1141 LayerAnimationController::Create(0));
1142 controller->AddValueObserver(&dummy);
1144 scoped_ptr<Animation> to_add(CreateAnimation(
1145 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1147 Animation::Opacity));
1148 to_add->set_iterations(3);
1149 controller->AddAnimation(to_add.Pass());
1151 controller->Animate(kInitialTickTime);
1152 controller->UpdateState(true, events.get());
1153 EXPECT_TRUE(controller->HasActiveAnimation());
1154 EXPECT_EQ(0.f, dummy.opacity());
1155 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1156 controller->UpdateState(true, events.get());
1157 EXPECT_TRUE(controller->HasActiveAnimation());
1158 EXPECT_EQ(0.25f, dummy.opacity());
1159 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1160 controller->UpdateState(true, events.get());
1161 EXPECT_TRUE(controller->HasActiveAnimation());
1162 EXPECT_EQ(0.75f, dummy.opacity());
1163 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
1164 controller->UpdateState(true, events.get());
1165 EXPECT_TRUE(controller->HasActiveAnimation());
1166 EXPECT_EQ(0.25f, dummy.opacity());
1167 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
1168 controller->UpdateState(true, events.get());
1169 EXPECT_TRUE(controller->HasActiveAnimation());
1170 EXPECT_EQ(0.75f, dummy.opacity());
1171 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1172 controller->UpdateState(true, events.get());
1173 EXPECT_FALSE(controller->HasActiveAnimation());
1174 EXPECT_EQ(1.f, dummy.opacity());
1176 // Just be extra sure.
1177 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
1178 controller->UpdateState(true, events.get());
1179 EXPECT_EQ(1.f, dummy.opacity());
1182 // Test that an infinitely looping animation does indeed go until aborted.
1183 TEST(LayerAnimationControllerTest, InfiniteLooping) {
1184 scoped_ptr<AnimationEventsVector> events(
1185 make_scoped_ptr(new AnimationEventsVector));
1186 FakeLayerAnimationValueObserver dummy;
1187 scoped_refptr<LayerAnimationController> controller(
1188 LayerAnimationController::Create(0));
1189 controller->AddValueObserver(&dummy);
1191 const int id = 1;
1192 scoped_ptr<Animation> to_add(CreateAnimation(
1193 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1195 Animation::Opacity));
1196 to_add->set_iterations(-1);
1197 controller->AddAnimation(to_add.Pass());
1199 controller->Animate(kInitialTickTime);
1200 controller->UpdateState(true, events.get());
1201 EXPECT_TRUE(controller->HasActiveAnimation());
1202 EXPECT_EQ(0.f, dummy.opacity());
1203 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1204 controller->UpdateState(true, events.get());
1205 EXPECT_TRUE(controller->HasActiveAnimation());
1206 EXPECT_EQ(0.25f, dummy.opacity());
1207 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1208 controller->UpdateState(true, events.get());
1209 EXPECT_TRUE(controller->HasActiveAnimation());
1210 EXPECT_EQ(0.75f, dummy.opacity());
1212 controller->Animate(kInitialTickTime +
1213 TimeDelta::FromMilliseconds(1073741824250));
1214 controller->UpdateState(true, events.get());
1215 EXPECT_TRUE(controller->HasActiveAnimation());
1216 EXPECT_EQ(0.25f, dummy.opacity());
1217 controller->Animate(kInitialTickTime +
1218 TimeDelta::FromMilliseconds(1073741824750));
1219 controller->UpdateState(true, events.get());
1220 EXPECT_TRUE(controller->HasActiveAnimation());
1221 EXPECT_EQ(0.75f, dummy.opacity());
1223 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1224 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1225 Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(750));
1226 EXPECT_FALSE(controller->HasActiveAnimation());
1227 EXPECT_EQ(0.75f, dummy.opacity());
1230 // Test that pausing and resuming work as expected.
1231 TEST(LayerAnimationControllerTest, PauseResume) {
1232 scoped_ptr<AnimationEventsVector> events(
1233 make_scoped_ptr(new AnimationEventsVector));
1234 FakeLayerAnimationValueObserver dummy;
1235 scoped_refptr<LayerAnimationController> controller(
1236 LayerAnimationController::Create(0));
1237 controller->AddValueObserver(&dummy);
1239 const int id = 1;
1240 controller->AddAnimation(CreateAnimation(
1241 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1243 Animation::Opacity));
1245 controller->Animate(kInitialTickTime);
1246 controller->UpdateState(true, events.get());
1247 EXPECT_TRUE(controller->HasActiveAnimation());
1248 EXPECT_EQ(0.f, dummy.opacity());
1249 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1250 controller->UpdateState(true, events.get());
1251 EXPECT_TRUE(controller->HasActiveAnimation());
1252 EXPECT_EQ(0.5f, dummy.opacity());
1254 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1255 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1256 Animation::Paused, kInitialTickTime + TimeDelta::FromMilliseconds(500));
1258 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1259 controller->UpdateState(true, events.get());
1260 EXPECT_TRUE(controller->HasActiveAnimation());
1261 EXPECT_EQ(0.5f, dummy.opacity());
1263 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1264 controller->GetAnimation(id, Animation::Opacity)
1265 ->SetRunState(Animation::Running,
1266 kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1267 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
1268 controller->UpdateState(true, events.get());
1269 EXPECT_TRUE(controller->HasActiveAnimation());
1270 EXPECT_EQ(0.75f, dummy.opacity());
1272 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
1273 controller->UpdateState(true, events.get());
1274 EXPECT_FALSE(controller->HasActiveAnimation());
1275 EXPECT_EQ(1.f, dummy.opacity());
1278 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1279 scoped_ptr<AnimationEventsVector> events(
1280 make_scoped_ptr(new AnimationEventsVector));
1281 FakeLayerAnimationValueObserver dummy;
1282 scoped_refptr<LayerAnimationController> controller(
1283 LayerAnimationController::Create(0));
1284 controller->AddValueObserver(&dummy);
1286 const int id = 1;
1287 controller->AddAnimation(CreateAnimation(
1288 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1290 Animation::Transform));
1291 controller->AddAnimation(CreateAnimation(
1292 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1294 Animation::Opacity));
1295 controller->AddAnimation(CreateAnimation(
1296 scoped_ptr<AnimationCurve>(
1297 new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
1299 Animation::Opacity));
1301 controller->Animate(kInitialTickTime);
1302 controller->UpdateState(true, events.get());
1303 EXPECT_TRUE(controller->HasActiveAnimation());
1304 EXPECT_EQ(0.f, dummy.opacity());
1305 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1306 controller->UpdateState(true, events.get());
1307 EXPECT_TRUE(controller->HasActiveAnimation());
1308 EXPECT_EQ(0.5f, dummy.opacity());
1310 EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1311 controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1312 Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1313 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1314 controller->UpdateState(true, events.get());
1315 EXPECT_TRUE(controller->HasActiveAnimation());
1316 EXPECT_EQ(1.f, dummy.opacity());
1317 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1318 controller->UpdateState(true, events.get());
1319 EXPECT_TRUE(!controller->HasActiveAnimation());
1320 EXPECT_EQ(0.75f, dummy.opacity());
1323 TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1324 FakeLayerAnimationValueObserver dummy_impl;
1325 scoped_refptr<LayerAnimationController> controller_impl(
1326 LayerAnimationController::Create(0));
1327 controller_impl->AddValueObserver(&dummy_impl);
1328 scoped_ptr<AnimationEventsVector> events(
1329 make_scoped_ptr(new AnimationEventsVector));
1330 FakeLayerAnimationValueObserver dummy;
1331 scoped_refptr<LayerAnimationController> controller(
1332 LayerAnimationController::Create(0));
1333 controller->AddValueObserver(&dummy);
1335 scoped_ptr<Animation> to_add(CreateAnimation(
1336 scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1338 Animation::Opacity));
1339 to_add->set_needs_synchronized_start_time(true);
1340 controller->AddAnimation(to_add.Pass());
1342 controller->Animate(kInitialTickTime);
1343 controller->UpdateState(true, events.get());
1344 EXPECT_TRUE(controller->HasActiveAnimation());
1345 Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
1346 EXPECT_TRUE(active_animation);
1347 EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1349 controller->PushAnimationUpdatesTo(controller_impl.get());
1350 controller_impl->ActivateAnimations();
1352 active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
1353 EXPECT_TRUE(active_animation);
1354 EXPECT_EQ(Animation::WaitingForTargetAvailability,
1355 active_animation->run_state());
1358 // Tests that skipping a call to UpdateState works as expected.
1359 TEST(LayerAnimationControllerTest, SkipUpdateState) {
1360 scoped_ptr<AnimationEventsVector> events(
1361 make_scoped_ptr(new AnimationEventsVector));
1362 FakeLayerAnimationValueObserver dummy;
1363 scoped_refptr<LayerAnimationController> controller(
1364 LayerAnimationController::Create(0));
1365 controller->AddValueObserver(&dummy);
1367 controller->AddAnimation(CreateAnimation(
1368 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1370 Animation::Transform));
1372 controller->Animate(kInitialTickTime);
1373 controller->UpdateState(true, events.get());
1375 controller->AddAnimation(CreateAnimation(
1376 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1378 Animation::Opacity));
1380 // Animate but don't UpdateState.
1381 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1383 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1384 events.reset(new AnimationEventsVector);
1385 controller->UpdateState(true, events.get());
1387 // Should have one Started event and one Finished event.
1388 EXPECT_EQ(2u, events->size());
1389 EXPECT_NE((*events)[0].type, (*events)[1].type);
1391 // The float transition should still be at its starting point.
1392 EXPECT_TRUE(controller->HasActiveAnimation());
1393 EXPECT_EQ(0.f, dummy.opacity());
1395 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1396 controller->UpdateState(true, events.get());
1398 // The float tranisition should now be done.
1399 EXPECT_EQ(1.f, dummy.opacity());
1400 EXPECT_FALSE(controller->HasActiveAnimation());
1403 // Tests that an animation controller with only a pending observer gets ticked
1404 // but doesn't progress animations past the Starting state.
1405 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1406 scoped_ptr<AnimationEventsVector> events(
1407 make_scoped_ptr(new AnimationEventsVector));
1408 FakeLayerAnimationValueObserver dummy;
1409 FakeInactiveLayerAnimationValueObserver pending_dummy;
1410 scoped_refptr<LayerAnimationController> controller(
1411 LayerAnimationController::Create(0));
1413 const int id = 1;
1414 controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>(
1415 new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(),
1417 Animation::Opacity));
1419 // Without an observer, the animation shouldn't progress to the Starting
1420 // state.
1421 controller->Animate(kInitialTickTime);
1422 controller->UpdateState(true, events.get());
1423 EXPECT_EQ(0u, events->size());
1424 EXPECT_EQ(Animation::WaitingForTargetAvailability,
1425 controller->GetAnimation(id, Animation::Opacity)->run_state());
1427 controller->AddValueObserver(&pending_dummy);
1429 // With only a pending observer, the animation should progress to the
1430 // Starting state and get ticked at its starting point, but should not
1431 // progress to Running.
1432 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1433 controller->UpdateState(true, events.get());
1434 EXPECT_EQ(0u, events->size());
1435 EXPECT_EQ(Animation::Starting,
1436 controller->GetAnimation(id, Animation::Opacity)->run_state());
1437 EXPECT_EQ(0.5f, pending_dummy.opacity());
1439 // Even when already in the Starting state, the animation should stay
1440 // there, and shouldn't be ticked past its starting point.
1441 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1442 controller->UpdateState(true, events.get());
1443 EXPECT_EQ(0u, events->size());
1444 EXPECT_EQ(Animation::Starting,
1445 controller->GetAnimation(id, Animation::Opacity)->run_state());
1446 EXPECT_EQ(0.5f, pending_dummy.opacity());
1448 controller->AddValueObserver(&dummy);
1450 // Now that an active observer has been added, the animation should still
1451 // initially tick at its starting point, but should now progress to Running.
1452 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1453 controller->UpdateState(true, events.get());
1454 EXPECT_EQ(1u, events->size());
1455 EXPECT_EQ(Animation::Running,
1456 controller->GetAnimation(id, Animation::Opacity)->run_state());
1457 EXPECT_EQ(0.5f, pending_dummy.opacity());
1458 EXPECT_EQ(0.5f, dummy.opacity());
1460 // The animation should now tick past its starting point.
1461 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
1462 EXPECT_NE(0.5f, pending_dummy.opacity());
1463 EXPECT_NE(0.5f, dummy.opacity());
1466 TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
1467 scoped_refptr<LayerAnimationController> controller_impl(
1468 LayerAnimationController::Create(0));
1470 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1471 KeyframedTransformAnimationCurve::Create());
1473 TransformOperations operations1;
1474 curve1->AddKeyframe(
1475 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1476 operations1.AppendTranslate(10.0, 15.0, 0.0);
1477 curve1->AddKeyframe(TransformKeyframe::Create(
1478 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1480 scoped_ptr<Animation> animation(
1481 Animation::Create(curve1.Pass(), 1, 1, Animation::Transform));
1482 controller_impl->AddAnimation(animation.Pass());
1484 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1485 KeyframedTransformAnimationCurve::Create());
1487 TransformOperations operations2;
1488 curve2->AddKeyframe(
1489 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1490 operations2.AppendScale(2.0, 3.0, 4.0);
1491 curve2->AddKeyframe(TransformKeyframe::Create(
1492 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1494 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::Transform);
1495 controller_impl->AddAnimation(animation.Pass());
1497 gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1498 gfx::BoxF bounds;
1500 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1501 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1502 bounds.ToString());
1504 controller_impl->GetAnimation(1, Animation::Transform)
1505 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1507 // Only the unfinished animation should affect the animated bounds.
1508 EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1509 EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1510 bounds.ToString());
1512 controller_impl->GetAnimation(2, Animation::Transform)
1513 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1515 // There are no longer any running animations.
1516 EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
1518 // Add an animation whose bounds we don't yet support computing.
1519 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1520 KeyframedTransformAnimationCurve::Create());
1521 TransformOperations operations3;
1522 gfx::Transform transform3;
1523 transform3.Scale3d(1.0, 2.0, 3.0);
1524 curve3->AddKeyframe(
1525 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
1526 operations3.AppendMatrix(transform3);
1527 curve3->AddKeyframe(TransformKeyframe::Create(
1528 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
1529 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::Transform);
1530 controller_impl->AddAnimation(animation.Pass());
1531 EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1534 // Tests that AbortAnimations aborts all animations targeting the specified
1535 // property.
1536 TEST(LayerAnimationControllerTest, AbortAnimations) {
1537 FakeLayerAnimationValueObserver dummy;
1538 scoped_refptr<LayerAnimationController> controller(
1539 LayerAnimationController::Create(0));
1540 controller->AddValueObserver(&dummy);
1542 // Start with several animations, and allow some of them to reach the finished
1543 // state.
1544 controller->AddAnimation(CreateAnimation(
1545 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1547 Animation::Transform));
1548 controller->AddAnimation(CreateAnimation(
1549 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1551 Animation::Opacity));
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 FakeTransformTransition(2.0)).Pass(),
1559 Animation::Transform));
1560 controller->AddAnimation(CreateAnimation(
1561 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1563 Animation::Opacity));
1565 controller->Animate(kInitialTickTime);
1566 controller->UpdateState(true, nullptr);
1567 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1568 controller->UpdateState(true, nullptr);
1570 EXPECT_EQ(Animation::Finished,
1571 controller->GetAnimation(1, Animation::Transform)->run_state());
1572 EXPECT_EQ(Animation::Finished,
1573 controller->GetAnimation(2, Animation::Opacity)->run_state());
1574 EXPECT_EQ(Animation::Running,
1575 controller->GetAnimation(3, Animation::Transform)->run_state());
1576 EXPECT_EQ(Animation::WaitingForTargetAvailability,
1577 controller->GetAnimation(4, Animation::Transform)->run_state());
1578 EXPECT_EQ(Animation::Running,
1579 controller->GetAnimation(5, Animation::Opacity)->run_state());
1581 controller->AbortAnimations(Animation::Transform);
1583 // Only un-finished Transform animations should have been aborted.
1584 EXPECT_EQ(Animation::Finished,
1585 controller->GetAnimation(1, Animation::Transform)->run_state());
1586 EXPECT_EQ(Animation::Finished,
1587 controller->GetAnimation(2, Animation::Opacity)->run_state());
1588 EXPECT_EQ(Animation::Aborted,
1589 controller->GetAnimation(3, Animation::Transform)->run_state());
1590 EXPECT_EQ(Animation::Aborted,
1591 controller->GetAnimation(4, Animation::Transform)->run_state());
1592 EXPECT_EQ(Animation::Running,
1593 controller->GetAnimation(5, Animation::Opacity)->run_state());
1596 // An animation aborted on the main thread should get deleted on both threads.
1597 TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
1598 FakeLayerAnimationValueObserver dummy_impl;
1599 scoped_refptr<LayerAnimationController> controller_impl(
1600 LayerAnimationController::Create(0));
1601 controller_impl->AddValueObserver(&dummy_impl);
1602 FakeLayerAnimationValueObserver dummy;
1603 scoped_refptr<LayerAnimationController> controller(
1604 LayerAnimationController::Create(0));
1605 controller->AddValueObserver(&dummy);
1607 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1608 int group_id = controller->GetAnimation(Animation::Opacity)->group();
1610 controller->PushAnimationUpdatesTo(controller_impl.get());
1611 controller_impl->ActivateAnimations();
1612 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1614 controller->AbortAnimations(Animation::Opacity);
1615 EXPECT_EQ(Animation::Aborted,
1616 controller->GetAnimation(Animation::Opacity)->run_state());
1617 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1618 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1620 controller->Animate(kInitialTickTime);
1621 controller->UpdateState(true, nullptr);
1622 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1623 EXPECT_EQ(Animation::WaitingForDeletion,
1624 controller->GetAnimation(Animation::Opacity)->run_state());
1626 controller->PushAnimationUpdatesTo(controller_impl.get());
1627 controller_impl->ActivateAnimations();
1628 EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1629 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1632 // An animation aborted on the impl thread should get deleted on both threads.
1633 TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
1634 FakeLayerAnimationValueObserver dummy_impl;
1635 scoped_refptr<LayerAnimationController> controller_impl(
1636 LayerAnimationController::Create(0));
1637 controller_impl->AddValueObserver(&dummy_impl);
1638 FakeLayerAnimationValueObserver dummy;
1639 scoped_refptr<LayerAnimationController> controller(
1640 LayerAnimationController::Create(0));
1641 controller->AddValueObserver(&dummy);
1643 AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1644 int group_id = controller->GetAnimation(Animation::Opacity)->group();
1646 controller->PushAnimationUpdatesTo(controller_impl.get());
1647 controller_impl->ActivateAnimations();
1648 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1650 controller_impl->AbortAnimations(Animation::Opacity);
1651 EXPECT_EQ(Animation::Aborted,
1652 controller_impl->GetAnimation(Animation::Opacity)->run_state());
1653 EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1654 EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1656 AnimationEventsVector events;
1657 controller_impl->Animate(kInitialTickTime);
1658 controller_impl->UpdateState(true, &events);
1659 EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
1660 EXPECT_EQ(1u, events.size());
1661 EXPECT_EQ(AnimationEvent::Aborted, events[0].type);
1662 EXPECT_EQ(Animation::WaitingForDeletion,
1663 controller_impl->GetAnimation(Animation::Opacity)->run_state());
1665 controller->NotifyAnimationAborted(events[0]);
1666 EXPECT_EQ(Animation::Aborted,
1667 controller->GetAnimation(Animation::Opacity)->run_state());
1669 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1670 controller->UpdateState(true, nullptr);
1671 EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1672 EXPECT_EQ(Animation::WaitingForDeletion,
1673 controller->GetAnimation(Animation::Opacity)->run_state());
1675 controller->PushAnimationUpdatesTo(controller_impl.get());
1676 controller_impl->ActivateAnimations();
1677 EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1678 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1681 // Ensure that we only generate Finished events for animations in a group
1682 // once all animations in that group are finished.
1683 TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
1684 scoped_ptr<AnimationEventsVector> events(
1685 make_scoped_ptr(new AnimationEventsVector));
1686 FakeLayerAnimationValueObserver dummy_impl;
1687 scoped_refptr<LayerAnimationController> controller_impl(
1688 LayerAnimationController::Create(0));
1689 controller_impl->AddValueObserver(&dummy_impl);
1691 // Add two animations with the same group id but different durations.
1692 controller_impl->AddAnimation(CreateAnimation(
1693 scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1695 Animation::Transform));
1696 controller_impl->AddAnimation(CreateAnimation(
1697 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1699 Animation::Opacity));
1701 controller_impl->Animate(kInitialTickTime);
1702 controller_impl->UpdateState(true, events.get());
1704 // Both animations should have started.
1705 EXPECT_EQ(2u, events->size());
1706 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1707 EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1709 events.reset(new AnimationEventsVector);
1710 controller_impl->Animate(kInitialTickTime +
1711 TimeDelta::FromMilliseconds(1000));
1712 controller_impl->UpdateState(true, events.get());
1714 // The opacity animation should be finished, but should not have generated
1715 // a Finished event yet.
1716 EXPECT_EQ(0u, events->size());
1717 EXPECT_EQ(Animation::Finished,
1718 controller_impl->GetAnimation(1, Animation::Opacity)->run_state());
1719 EXPECT_EQ(Animation::Running,
1720 controller_impl->GetAnimation(1,
1721 Animation::Transform)->run_state());
1723 controller_impl->Animate(kInitialTickTime +
1724 TimeDelta::FromMilliseconds(2000));
1725 controller_impl->UpdateState(true, events.get());
1727 // Both animations should have generated Finished events.
1728 EXPECT_EQ(2u, events->size());
1729 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1730 EXPECT_EQ(AnimationEvent::Finished, (*events)[1].type);
1733 // Ensure that when a group has a mix of aborted and finished animations,
1734 // we generate a Finished event for the finished animation and an Aborted
1735 // event for the aborted animation.
1736 TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
1737 scoped_ptr<AnimationEventsVector> events(
1738 make_scoped_ptr(new AnimationEventsVector));
1739 FakeLayerAnimationValueObserver dummy_impl;
1740 scoped_refptr<LayerAnimationController> controller_impl(
1741 LayerAnimationController::Create(0));
1742 controller_impl->AddValueObserver(&dummy_impl);
1744 // Add two animations with the same group id.
1745 controller_impl->AddAnimation(CreateAnimation(
1746 scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1748 Animation::Transform));
1749 controller_impl->AddAnimation(CreateAnimation(
1750 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1752 Animation::Opacity));
1754 controller_impl->Animate(kInitialTickTime);
1755 controller_impl->UpdateState(true, events.get());
1757 // Both animations should have started.
1758 EXPECT_EQ(2u, events->size());
1759 EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1760 EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1762 controller_impl->AbortAnimations(Animation::Opacity);
1764 events.reset(new AnimationEventsVector);
1765 controller_impl->Animate(kInitialTickTime +
1766 TimeDelta::FromMilliseconds(1000));
1767 controller_impl->UpdateState(true, events.get());
1769 // We should have exactly 2 events: a Finished event for the tranform
1770 // animation, and an Aborted event for the opacity animation.
1771 EXPECT_EQ(2u, events->size());
1772 EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1773 EXPECT_EQ(Animation::Transform, (*events)[0].target_property);
1774 EXPECT_EQ(AnimationEvent::Aborted, (*events)[1].type);
1775 EXPECT_EQ(Animation::Opacity, (*events)[1].target_property);
1778 TEST(LayerAnimationControllerTest, HasAnimationThatAffectsScale) {
1779 scoped_refptr<LayerAnimationController> controller_impl(
1780 LayerAnimationController::Create(0));
1782 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1784 controller_impl->AddAnimation(CreateAnimation(
1785 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1787 Animation::Opacity));
1789 // Opacity animations don't affect scale.
1790 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1792 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1793 KeyframedTransformAnimationCurve::Create());
1795 TransformOperations operations1;
1796 curve1->AddKeyframe(
1797 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1798 operations1.AppendTranslate(10.0, 15.0, 0.0);
1799 curve1->AddKeyframe(TransformKeyframe::Create(
1800 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1802 scoped_ptr<Animation> animation(
1803 Animation::Create(curve1.Pass(), 2, 2, Animation::Transform));
1804 controller_impl->AddAnimation(animation.Pass());
1806 // Translations don't affect scale.
1807 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1809 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1810 KeyframedTransformAnimationCurve::Create());
1812 TransformOperations operations2;
1813 curve2->AddKeyframe(
1814 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1815 operations2.AppendScale(2.0, 3.0, 4.0);
1816 curve2->AddKeyframe(TransformKeyframe::Create(
1817 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1819 animation = Animation::Create(curve2.Pass(), 3, 3, Animation::Transform);
1820 controller_impl->AddAnimation(animation.Pass());
1822 EXPECT_TRUE(controller_impl->HasAnimationThatAffectsScale());
1824 controller_impl->GetAnimation(3, Animation::Transform)
1825 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1827 // Only unfinished animations should be considered by
1828 // HasAnimationThatAffectsScale.
1829 EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1832 TEST(LayerAnimationControllerTest, HasOnlyTranslationTransforms) {
1833 scoped_refptr<LayerAnimationController> controller_impl(
1834 LayerAnimationController::Create(0));
1836 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1838 controller_impl->AddAnimation(CreateAnimation(
1839 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1841 Animation::Opacity));
1843 // Opacity animations aren't non-translation transforms.
1844 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1846 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1847 KeyframedTransformAnimationCurve::Create());
1849 TransformOperations operations1;
1850 curve1->AddKeyframe(
1851 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1852 operations1.AppendTranslate(10.0, 15.0, 0.0);
1853 curve1->AddKeyframe(TransformKeyframe::Create(
1854 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1856 scoped_ptr<Animation> animation(
1857 Animation::Create(curve1.Pass(), 2, 2, Animation::Transform));
1858 controller_impl->AddAnimation(animation.Pass());
1860 // The only transform animation we've added is a translation.
1861 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1863 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1864 KeyframedTransformAnimationCurve::Create());
1866 TransformOperations operations2;
1867 curve2->AddKeyframe(
1868 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1869 operations2.AppendScale(2.0, 3.0, 4.0);
1870 curve2->AddKeyframe(TransformKeyframe::Create(
1871 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1873 animation = Animation::Create(curve2.Pass(), 3, 3, Animation::Transform);
1874 controller_impl->AddAnimation(animation.Pass());
1876 // A scale animation is not a translation.
1877 EXPECT_FALSE(controller_impl->HasOnlyTranslationTransforms());
1879 controller_impl->GetAnimation(3, Animation::Transform)
1880 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1882 // Only unfinished animations should be considered by
1883 // HasOnlyTranslationTransforms.
1884 EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1887 TEST(LayerAnimationControllerTest, MaximumTargetScale) {
1888 scoped_refptr<LayerAnimationController> controller_impl(
1889 LayerAnimationController::Create(0));
1891 float max_scale = 0.f;
1892 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1893 EXPECT_EQ(0.f, max_scale);
1895 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1896 KeyframedTransformAnimationCurve::Create());
1898 TransformOperations operations1;
1899 curve1->AddKeyframe(
1900 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1901 operations1.AppendScale(2.0, 3.0, 4.0);
1902 curve1->AddKeyframe(TransformKeyframe::Create(
1903 base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
1905 scoped_ptr<Animation> animation(
1906 Animation::Create(curve1.Pass(), 1, 1, Animation::Transform));
1907 controller_impl->AddAnimation(animation.Pass());
1909 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1910 EXPECT_EQ(4.f, max_scale);
1912 scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1913 KeyframedTransformAnimationCurve::Create());
1915 TransformOperations operations2;
1916 curve2->AddKeyframe(
1917 TransformKeyframe::Create(base::TimeDelta(), operations2, nullptr));
1918 operations2.AppendScale(6.0, 5.0, 4.0);
1919 curve2->AddKeyframe(TransformKeyframe::Create(
1920 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1922 animation = Animation::Create(curve2.Pass(), 2, 2, Animation::Transform);
1923 controller_impl->AddAnimation(animation.Pass());
1925 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1926 EXPECT_EQ(6.f, max_scale);
1928 scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1929 KeyframedTransformAnimationCurve::Create());
1931 TransformOperations operations3;
1932 curve3->AddKeyframe(
1933 TransformKeyframe::Create(base::TimeDelta(), operations3, nullptr));
1934 operations3.AppendPerspective(6.0);
1935 curve3->AddKeyframe(TransformKeyframe::Create(
1936 base::TimeDelta::FromSecondsD(1.0), operations3, nullptr));
1938 animation = Animation::Create(curve3.Pass(), 3, 3, Animation::Transform);
1939 controller_impl->AddAnimation(animation.Pass());
1941 EXPECT_FALSE(controller_impl->MaximumTargetScale(&max_scale));
1943 controller_impl->GetAnimation(3, Animation::Transform)
1944 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1945 controller_impl->GetAnimation(2, Animation::Transform)
1946 ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1948 // Only unfinished animations should be considered by
1949 // MaximumTargetScale.
1950 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1951 EXPECT_EQ(4.f, max_scale);
1954 TEST(LayerAnimationControllerTest, MaximumTargetScaleWithDirection) {
1955 scoped_refptr<LayerAnimationController> controller_impl(
1956 LayerAnimationController::Create(0));
1958 scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1959 KeyframedTransformAnimationCurve::Create());
1960 TransformOperations operations1;
1961 operations1.AppendScale(1.0, 2.0, 3.0);
1962 curve1->AddKeyframe(
1963 TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
1964 TransformOperations operations2;
1965 operations2.AppendScale(4.0, 5.0, 6.0);
1966 curve1->AddKeyframe(TransformKeyframe::Create(
1967 base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
1969 scoped_ptr<Animation> animation_owned(
1970 Animation::Create(curve1.Pass(), 1, 1, Animation::Transform));
1971 Animation* animation = animation_owned.get();
1972 controller_impl->AddAnimation(animation_owned.Pass());
1974 float max_scale = 0.f;
1976 EXPECT_GT(animation->playback_rate(), 0.0);
1978 // Normal direction with positive playback rate.
1979 animation->set_direction(Animation::Normal);
1980 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1981 EXPECT_EQ(6.f, max_scale);
1983 // Alternate direction with positive playback rate.
1984 animation->set_direction(Animation::Alternate);
1985 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1986 EXPECT_EQ(6.f, max_scale);
1988 // Reverse direction with positive playback rate.
1989 animation->set_direction(Animation::Reverse);
1990 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1991 EXPECT_EQ(3.f, max_scale);
1993 // Alternate reverse direction.
1994 animation->set_direction(Animation::Reverse);
1995 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1996 EXPECT_EQ(3.f, max_scale);
1998 animation->set_playback_rate(-1.0);
2000 // Normal direction with negative playback rate.
2001 animation->set_direction(Animation::Normal);
2002 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2003 EXPECT_EQ(3.f, max_scale);
2005 // Alternate direction with negative playback rate.
2006 animation->set_direction(Animation::Alternate);
2007 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2008 EXPECT_EQ(3.f, max_scale);
2010 // Reverse direction with negative playback rate.
2011 animation->set_direction(Animation::Reverse);
2012 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2013 EXPECT_EQ(6.f, max_scale);
2015 // Alternate reverse direction with negative playback rate.
2016 animation->set_direction(Animation::Reverse);
2017 EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2018 EXPECT_EQ(6.f, max_scale);
2021 TEST(LayerAnimationControllerTest, NewlyPushedAnimationWaitsForActivation) {
2022 scoped_ptr<AnimationEventsVector> events(
2023 make_scoped_ptr(new AnimationEventsVector));
2024 FakeLayerAnimationValueObserver dummy_impl;
2025 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2026 scoped_refptr<LayerAnimationController> controller_impl(
2027 LayerAnimationController::Create(0));
2028 controller_impl->AddValueObserver(&dummy_impl);
2029 controller_impl->AddValueObserver(&pending_dummy_impl);
2030 FakeLayerAnimationValueObserver dummy;
2031 scoped_refptr<LayerAnimationController> controller(
2032 LayerAnimationController::Create(0));
2033 controller->AddValueObserver(&dummy);
2035 EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
2036 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, false);
2037 int group_id = controller->GetAnimation(Animation::Opacity)->group();
2038 EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
2040 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2041 controller->PushAnimationUpdatesTo(controller_impl.get());
2042 EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
2044 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
2045 EXPECT_EQ(
2046 Animation::WaitingForTargetAvailability,
2047 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2048 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2049 ->affects_pending_observers());
2050 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2051 ->affects_active_observers());
2053 controller_impl->Animate(kInitialTickTime);
2054 EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2055 controller_impl->UpdateState(true, events.get());
2057 // Since the animation hasn't been activated, it should still be Starting
2058 // rather than Running.
2059 EXPECT_EQ(
2060 Animation::Starting,
2061 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2063 // Since the animation hasn't been activated, only the pending observer
2064 // should have been ticked.
2065 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2066 EXPECT_EQ(0.f, dummy_impl.opacity());
2068 controller_impl->ActivateAnimations();
2069 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2070 ->affects_pending_observers());
2071 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2072 ->affects_active_observers());
2074 controller_impl->Animate(kInitialTickTime +
2075 TimeDelta::FromMilliseconds(1000));
2076 controller_impl->UpdateState(true, events.get());
2078 // Since the animation has been activated, it should have reached the
2079 // Running state and the active observer should start to get ticked.
2080 EXPECT_EQ(
2081 Animation::Running,
2082 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2083 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2084 EXPECT_EQ(0.5f, dummy_impl.opacity());
2087 TEST(LayerAnimationControllerTest, ActivationBetweenAnimateAndUpdateState) {
2088 scoped_ptr<AnimationEventsVector> events(
2089 make_scoped_ptr(new AnimationEventsVector));
2090 FakeLayerAnimationValueObserver dummy_impl;
2091 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2092 scoped_refptr<LayerAnimationController> controller_impl(
2093 LayerAnimationController::Create(0));
2094 controller_impl->AddValueObserver(&dummy_impl);
2095 controller_impl->AddValueObserver(&pending_dummy_impl);
2096 FakeLayerAnimationValueObserver dummy;
2097 scoped_refptr<LayerAnimationController> controller(
2098 LayerAnimationController::Create(0));
2099 controller->AddValueObserver(&dummy);
2101 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2102 int group_id = controller->GetAnimation(Animation::Opacity)->group();
2104 controller->PushAnimationUpdatesTo(controller_impl.get());
2106 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
2107 EXPECT_EQ(
2108 Animation::WaitingForTargetAvailability,
2109 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2110 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2111 ->affects_pending_observers());
2112 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2113 ->affects_active_observers());
2115 controller_impl->Animate(kInitialTickTime);
2117 // Since the animation hasn't been activated, only the pending observer
2118 // should have been ticked.
2119 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2120 EXPECT_EQ(0.f, dummy_impl.opacity());
2122 controller_impl->ActivateAnimations();
2123 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2124 ->affects_pending_observers());
2125 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2126 ->affects_active_observers());
2128 controller_impl->UpdateState(true, events.get());
2130 // Since the animation has been activated, it should have reached the
2131 // Running state.
2132 EXPECT_EQ(
2133 Animation::Running,
2134 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2136 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2138 // Both observers should have been ticked.
2139 EXPECT_EQ(0.75f, pending_dummy_impl.opacity());
2140 EXPECT_EQ(0.75f, dummy_impl.opacity());
2143 TEST(LayerAnimationControllerTest, ClippedOpacityValues) {
2144 FakeLayerAnimationValueObserver dummy;
2145 scoped_refptr<LayerAnimationController> controller(
2146 LayerAnimationController::Create(0));
2147 controller->AddValueObserver(&dummy);
2149 AddOpacityTransitionToController(controller.get(), 1, 1.f, 2.f, true);
2151 controller->Animate(kInitialTickTime);
2152 EXPECT_EQ(1.f, dummy.opacity());
2154 // Opacity values are clipped [0,1]
2155 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2156 EXPECT_EQ(1.f, dummy.opacity());
2159 TEST(LayerAnimationControllerTest, ClippedNegativeOpacityValues) {
2160 FakeLayerAnimationValueObserver dummy;
2161 scoped_refptr<LayerAnimationController> controller(
2162 LayerAnimationController::Create(0));
2163 controller->AddValueObserver(&dummy);
2165 AddOpacityTransitionToController(controller.get(), 1, 0.f, -2.f, true);
2167 controller->Animate(kInitialTickTime);
2168 EXPECT_EQ(0.f, dummy.opacity());
2170 // Opacity values are clipped [0,1]
2171 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2172 EXPECT_EQ(0.f, dummy.opacity());
2175 TEST(LayerAnimationControllerTest, PushedDeletedAnimationWaitsForActivation) {
2176 scoped_ptr<AnimationEventsVector> events(
2177 make_scoped_ptr(new AnimationEventsVector));
2178 FakeLayerAnimationValueObserver dummy_impl;
2179 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2180 scoped_refptr<LayerAnimationController> controller_impl(
2181 LayerAnimationController::Create(0));
2182 controller_impl->AddValueObserver(&dummy_impl);
2183 controller_impl->AddValueObserver(&pending_dummy_impl);
2184 FakeLayerAnimationValueObserver dummy;
2185 scoped_refptr<LayerAnimationController> controller(
2186 LayerAnimationController::Create(0));
2187 controller->AddValueObserver(&dummy);
2189 AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2190 int group_id = controller->GetAnimation(Animation::Opacity)->group();
2192 controller->PushAnimationUpdatesTo(controller_impl.get());
2193 controller_impl->ActivateAnimations();
2194 controller_impl->Animate(kInitialTickTime);
2195 controller_impl->UpdateState(true, events.get());
2196 EXPECT_EQ(
2197 Animation::Running,
2198 controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2199 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2200 EXPECT_EQ(0.5f, dummy_impl.opacity());
2202 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2203 ->affects_pending_observers());
2204 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2205 ->affects_active_observers());
2207 // Delete the animation on the main-thread controller.
2208 controller->RemoveAnimation(
2209 controller->GetAnimation(Animation::Opacity)->id());
2210 controller->PushAnimationUpdatesTo(controller_impl.get());
2212 // The animation should no longer affect pending observers.
2213 EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2214 ->affects_pending_observers());
2215 EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2216 ->affects_active_observers());
2218 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2219 controller_impl->UpdateState(true, events.get());
2221 // Only the active observer should have been ticked.
2222 EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2223 EXPECT_EQ(0.75f, dummy_impl.opacity());
2225 controller_impl->ActivateAnimations();
2227 // Activation should cause the animation to be deleted.
2228 EXPECT_FALSE(controller_impl->has_any_animation());
2231 // Tests that an animation that affects only active observers won't block
2232 // an animation that affects only pending observers from starting.
2233 TEST(LayerAnimationControllerTest, StartAnimationsAffectingDifferentObservers) {
2234 scoped_ptr<AnimationEventsVector> events(
2235 make_scoped_ptr(new AnimationEventsVector));
2236 FakeLayerAnimationValueObserver dummy_impl;
2237 FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2238 scoped_refptr<LayerAnimationController> controller_impl(
2239 LayerAnimationController::Create(0));
2240 controller_impl->AddValueObserver(&dummy_impl);
2241 controller_impl->AddValueObserver(&pending_dummy_impl);
2242 FakeLayerAnimationValueObserver dummy;
2243 scoped_refptr<LayerAnimationController> controller(
2244 LayerAnimationController::Create(0));
2245 controller->AddValueObserver(&dummy);
2247 AddOpacityTransitionToController(controller.get(), 1, 0.f, 1.f, true);
2248 int first_animation_group_id =
2249 controller->GetAnimation(Animation::Opacity)->group();
2251 controller->PushAnimationUpdatesTo(controller_impl.get());
2252 controller_impl->ActivateAnimations();
2253 controller_impl->Animate(kInitialTickTime);
2254 controller_impl->UpdateState(true, events.get());
2256 // Remove the first animation from the main-thread controller, and add a
2257 // new animation affecting the same property.
2258 controller->RemoveAnimation(
2259 controller->GetAnimation(Animation::Opacity)->id());
2260 AddOpacityTransitionToController(controller.get(), 1, 1.f, 0.5f, true);
2261 int second_animation_group_id =
2262 controller->GetAnimation(Animation::Opacity)->group();
2263 controller->PushAnimationUpdatesTo(controller_impl.get());
2265 // The original animation should only affect active observers, and the new
2266 // animation should only affect pending observers.
2267 EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
2268 Animation::Opacity)
2269 ->affects_pending_observers());
2270 EXPECT_TRUE(controller_impl->GetAnimation(first_animation_group_id,
2271 Animation::Opacity)
2272 ->affects_active_observers());
2273 EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2274 Animation::Opacity)
2275 ->affects_pending_observers());
2276 EXPECT_FALSE(controller_impl->GetAnimation(second_animation_group_id,
2277 Animation::Opacity)
2278 ->affects_active_observers());
2280 controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2281 controller_impl->UpdateState(true, events.get());
2283 // The original animation should still be running, and the new animation
2284 // should be starting.
2285 EXPECT_EQ(Animation::Running,
2286 controller_impl->GetAnimation(first_animation_group_id,
2287 Animation::Opacity)->run_state());
2288 EXPECT_EQ(Animation::Starting,
2289 controller_impl->GetAnimation(second_animation_group_id,
2290 Animation::Opacity)->run_state());
2292 // The active observer should have been ticked by the original animation,
2293 // and the pending observer should have been ticked by the new animation.
2294 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2295 EXPECT_EQ(0.5f, dummy_impl.opacity());
2297 controller_impl->ActivateAnimations();
2299 // The original animation should have been deleted, and the new animation
2300 // should now affect both observers.
2301 EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
2302 Animation::Opacity));
2303 EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2304 Animation::Opacity)
2305 ->affects_pending_observers());
2306 EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2307 Animation::Opacity)
2308 ->affects_active_observers());
2310 controller_impl->Animate(kInitialTickTime +
2311 TimeDelta::FromMilliseconds(1000));
2312 controller_impl->UpdateState(true, events.get());
2314 // The new animation should be running, and the active observer should have
2315 // been ticked at the new animation's starting point.
2316 EXPECT_EQ(Animation::Running,
2317 controller_impl->GetAnimation(second_animation_group_id,
2318 Animation::Opacity)->run_state());
2319 EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2320 EXPECT_EQ(1.f, dummy_impl.opacity());
2323 TEST(LayerAnimationControllerTest, TestIsAnimatingProperty) {
2324 FakeLayerAnimationValueObserver dummy;
2325 scoped_refptr<LayerAnimationController> controller(
2326 LayerAnimationController::Create(0));
2327 controller->AddValueObserver(&dummy);
2329 scoped_ptr<Animation> animation(CreateAnimation(
2330 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2332 Animation::Opacity));
2333 controller->AddAnimation(animation.Pass());
2334 controller->Animate(kInitialTickTime);
2335 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::Opacity));
2336 controller->UpdateState(true, nullptr);
2337 EXPECT_TRUE(controller->HasActiveAnimation());
2338 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::Opacity));
2339 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Filter));
2340 EXPECT_EQ(0.f, dummy.opacity());
2343 TEST(LayerAnimationControllerTest, TestIsAnimatingPropertyTimeOffsetFillMode) {
2344 FakeLayerAnimationValueObserver dummy;
2345 scoped_refptr<LayerAnimationController> controller(
2346 LayerAnimationController::Create(0));
2347 controller->AddValueObserver(&dummy);
2349 scoped_ptr<Animation> animation(CreateAnimation(
2350 scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2352 Animation::Opacity));
2353 animation->set_fill_mode(Animation::FillModeNone);
2354 animation->set_time_offset(TimeDelta::FromMilliseconds(-2000));
2355 controller->AddAnimation(animation.Pass());
2357 controller->Animate(kInitialTickTime);
2358 controller->UpdateState(true, nullptr);
2359 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Opacity));
2360 EXPECT_TRUE(controller->HasActiveAnimation());
2361 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Opacity));
2362 EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Filter));
2364 controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2365 controller->UpdateState(true, nullptr);
2366 EXPECT_TRUE(controller->IsAnimatingProperty(Animation::Opacity));
2369 } // namespace
2370 } // namespace cc