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"
23 using base::TimeDelta
;
24 using base::TimeTicks
;
26 static base::TimeTicks
TicksFromSecondsF(double seconds
) {
27 return base::TimeTicks::FromInternalValue(seconds
*
28 base::Time::kMicrosecondsPerSecond
);
31 // A LayerAnimationController cannot be ticked at 0.0, since an animation
32 // with start time 0.0 is treated as an animation whose start time has
34 const TimeTicks kInitialTickTime
= TicksFromSecondsF(1.0);
36 scoped_ptr
<Animation
> CreateAnimation(scoped_ptr
<AnimationCurve
> curve
,
38 Animation::TargetProperty property
) {
39 return Animation::Create(curve
.Pass(), 0, group_id
, property
);
42 TEST(LayerAnimationControllerTest
, SyncNewAnimation
) {
43 FakeLayerAnimationValueObserver dummy_impl
;
44 scoped_refptr
<LayerAnimationController
> controller_impl(
45 LayerAnimationController::Create(0));
46 controller_impl
->AddValueObserver(&dummy_impl
);
47 FakeLayerAnimationValueObserver dummy
;
48 scoped_refptr
<LayerAnimationController
> controller(
49 LayerAnimationController::Create(0));
50 controller
->AddValueObserver(&dummy
);
52 EXPECT_FALSE(controller_impl
->GetAnimation(Animation::OPACITY
));
54 EXPECT_FALSE(controller
->needs_to_start_animations_for_testing());
55 EXPECT_FALSE(controller_impl
->needs_to_start_animations_for_testing());
58 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
59 EXPECT_TRUE(controller
->needs_to_start_animations_for_testing());
61 controller
->PushAnimationUpdatesTo(controller_impl
.get());
62 EXPECT_TRUE(controller_impl
->needs_to_start_animations_for_testing());
63 controller_impl
->ActivateAnimations();
65 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
66 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
67 controller_impl
->GetAnimationById(animation_id
)->run_state());
70 // If an animation is started on the impl thread before it is ticked on the main
71 // thread, we must be sure to respect the synchronized start time.
72 TEST(LayerAnimationControllerTest
, DoNotClobberStartTimes
) {
73 FakeLayerAnimationValueObserver dummy_impl
;
74 scoped_refptr
<LayerAnimationController
> controller_impl(
75 LayerAnimationController::Create(0));
76 controller_impl
->AddValueObserver(&dummy_impl
);
77 FakeLayerAnimationValueObserver dummy
;
78 scoped_refptr
<LayerAnimationController
> controller(
79 LayerAnimationController::Create(0));
80 controller
->AddValueObserver(&dummy
);
82 EXPECT_FALSE(controller_impl
->GetAnimation(Animation::OPACITY
));
85 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
87 controller
->PushAnimationUpdatesTo(controller_impl
.get());
88 controller_impl
->ActivateAnimations();
90 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
91 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
92 controller_impl
->GetAnimationById(animation_id
)->run_state());
94 AnimationEventsVector events
;
95 controller_impl
->Animate(kInitialTickTime
);
96 controller_impl
->UpdateState(true, &events
);
98 // Synchronize the start times.
99 EXPECT_EQ(1u, events
.size());
100 controller
->NotifyAnimationStarted(events
[0]);
101 EXPECT_EQ(controller
->GetAnimationById(animation_id
)->start_time(),
102 controller_impl
->GetAnimationById(animation_id
)->start_time());
104 // Start the animation on the main thread. Should not affect the start time.
105 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
106 controller
->UpdateState(true, nullptr);
107 EXPECT_EQ(controller
->GetAnimationById(animation_id
)->start_time(),
108 controller_impl
->GetAnimationById(animation_id
)->start_time());
111 TEST(LayerAnimationControllerTest
, UseSpecifiedStartTimes
) {
112 FakeLayerAnimationValueObserver dummy_impl
;
113 scoped_refptr
<LayerAnimationController
> controller_impl(
114 LayerAnimationController::Create(0));
115 controller_impl
->AddValueObserver(&dummy_impl
);
116 FakeLayerAnimationValueObserver dummy
;
117 scoped_refptr
<LayerAnimationController
> controller(
118 LayerAnimationController::Create(0));
119 controller
->AddValueObserver(&dummy
);
122 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
124 const TimeTicks start_time
= TicksFromSecondsF(123);
125 controller
->GetAnimation(Animation::OPACITY
)->set_start_time(start_time
);
127 controller
->PushAnimationUpdatesTo(controller_impl
.get());
128 controller_impl
->ActivateAnimations();
130 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
131 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
132 controller_impl
->GetAnimationById(animation_id
)->run_state());
134 AnimationEventsVector events
;
135 controller_impl
->Animate(kInitialTickTime
);
136 controller_impl
->UpdateState(true, &events
);
138 // Synchronize the start times.
139 EXPECT_EQ(1u, events
.size());
140 controller
->NotifyAnimationStarted(events
[0]);
142 EXPECT_EQ(start_time
,
143 controller
->GetAnimationById(animation_id
)->start_time());
144 EXPECT_EQ(controller
->GetAnimationById(animation_id
)->start_time(),
145 controller_impl
->GetAnimationById(animation_id
)->start_time());
147 // Start the animation on the main thread. Should not affect the start time.
148 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
149 controller
->UpdateState(true, nullptr);
150 EXPECT_EQ(start_time
,
151 controller
->GetAnimationById(animation_id
)->start_time());
152 EXPECT_EQ(controller
->GetAnimationById(animation_id
)->start_time(),
153 controller_impl
->GetAnimationById(animation_id
)->start_time());
156 // Tests that controllers activate and deactivate as expected.
157 TEST(LayerAnimationControllerTest
, Activation
) {
158 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
159 scoped_ptr
<AnimationRegistrar
> registrar_impl
= AnimationRegistrar::Create();
161 FakeLayerAnimationValueObserver dummy_impl
;
162 scoped_refptr
<LayerAnimationController
> controller_impl(
163 LayerAnimationController::Create(0));
164 controller_impl
->AddValueObserver(&dummy_impl
);
165 FakeLayerAnimationValueObserver dummy
;
166 scoped_refptr
<LayerAnimationController
> controller(
167 LayerAnimationController::Create(0));
168 controller
->AddValueObserver(&dummy
);
169 scoped_ptr
<AnimationEventsVector
> events(
170 make_scoped_ptr(new AnimationEventsVector
));
172 controller
->SetAnimationRegistrar(registrar
.get());
173 controller_impl
->SetAnimationRegistrar(registrar_impl
.get());
174 EXPECT_EQ(1u, registrar
->all_animation_controllers_for_testing().size());
175 EXPECT_EQ(1u, registrar_impl
->all_animation_controllers_for_testing().size());
177 // Initially, both controllers should be inactive.
178 EXPECT_EQ(0u, registrar
->active_animation_controllers_for_testing().size());
180 registrar_impl
->active_animation_controllers_for_testing().size());
182 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
183 // The main thread controller should now be active.
184 EXPECT_EQ(1u, registrar
->active_animation_controllers_for_testing().size());
186 controller
->PushAnimationUpdatesTo(controller_impl
.get());
187 controller_impl
->ActivateAnimations();
188 // Both controllers should now be active.
189 EXPECT_EQ(1u, registrar
->active_animation_controllers_for_testing().size());
191 registrar_impl
->active_animation_controllers_for_testing().size());
193 controller_impl
->Animate(kInitialTickTime
);
194 controller_impl
->UpdateState(true, events
.get());
195 EXPECT_EQ(1u, events
->size());
196 controller
->NotifyAnimationStarted((*events
)[0]);
198 EXPECT_EQ(1u, registrar
->active_animation_controllers_for_testing().size());
200 registrar_impl
->active_animation_controllers_for_testing().size());
202 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
203 controller
->UpdateState(true, nullptr);
204 EXPECT_EQ(1u, registrar
->active_animation_controllers_for_testing().size());
206 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
207 controller
->UpdateState(true, nullptr);
208 EXPECT_EQ(Animation::FINISHED
,
209 controller
->GetAnimation(Animation::OPACITY
)->run_state());
210 EXPECT_EQ(1u, registrar
->active_animation_controllers_for_testing().size());
212 events
.reset(new AnimationEventsVector
);
213 controller_impl
->Animate(kInitialTickTime
+
214 TimeDelta::FromMilliseconds(1500));
215 controller_impl
->UpdateState(true, events
.get());
217 EXPECT_EQ(Animation::WAITING_FOR_DELETION
,
218 controller_impl
->GetAnimation(Animation::OPACITY
)->run_state());
219 // The impl thread controller should have de-activated.
221 registrar_impl
->active_animation_controllers_for_testing().size());
223 EXPECT_EQ(1u, events
->size());
224 controller
->NotifyAnimationFinished((*events
)[0]);
225 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1500));
226 controller
->UpdateState(true, nullptr);
228 EXPECT_EQ(Animation::WAITING_FOR_DELETION
,
229 controller
->GetAnimation(Animation::OPACITY
)->run_state());
230 // The main thread controller should have de-activated.
231 EXPECT_EQ(0u, registrar
->active_animation_controllers_for_testing().size());
233 controller
->PushAnimationUpdatesTo(controller_impl
.get());
234 controller_impl
->ActivateAnimations();
235 EXPECT_FALSE(controller
->has_any_animation());
236 EXPECT_FALSE(controller_impl
->has_any_animation());
237 EXPECT_EQ(0u, registrar
->active_animation_controllers_for_testing().size());
239 registrar_impl
->active_animation_controllers_for_testing().size());
241 controller
->SetAnimationRegistrar(nullptr);
242 controller_impl
->SetAnimationRegistrar(nullptr);
245 TEST(LayerAnimationControllerTest
, SyncPause
) {
246 FakeLayerAnimationValueObserver dummy_impl
;
247 scoped_refptr
<LayerAnimationController
> controller_impl(
248 LayerAnimationController::Create(0));
249 controller_impl
->AddValueObserver(&dummy_impl
);
250 FakeLayerAnimationValueObserver dummy
;
251 scoped_refptr
<LayerAnimationController
> controller(
252 LayerAnimationController::Create(0));
253 controller
->AddValueObserver(&dummy
);
255 EXPECT_FALSE(controller_impl
->GetAnimation(Animation::OPACITY
));
257 // Two steps, three ranges: [0-1) -> 0.2, [1-2) -> 0.3, [2-3] -> 0.4.
258 const double duration
= 3.0;
259 const int animation_id
=
260 AddOpacityStepsToController(controller
.get(), duration
, 0.2f
, 0.4f
, 2);
262 // Set start offset to be at the beginning of the second range.
263 controller
->GetAnimationById(animation_id
)
264 ->set_time_offset(TimeDelta::FromSecondsD(1.01));
266 controller
->PushAnimationUpdatesTo(controller_impl
.get());
267 controller_impl
->ActivateAnimations();
269 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
270 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
271 controller_impl
->GetAnimationById(animation_id
)->run_state());
273 TimeTicks time
= kInitialTickTime
;
275 // Start the animations on each controller.
276 AnimationEventsVector events
;
277 controller_impl
->Animate(time
);
278 controller_impl
->UpdateState(true, &events
);
279 EXPECT_EQ(1u, events
.size());
281 controller
->Animate(time
);
282 controller
->UpdateState(true, nullptr);
283 controller
->NotifyAnimationStarted(events
[0]);
285 EXPECT_EQ(Animation::RUNNING
,
286 controller_impl
->GetAnimationById(animation_id
)->run_state());
287 EXPECT_EQ(Animation::RUNNING
,
288 controller
->GetAnimationById(animation_id
)->run_state());
290 EXPECT_EQ(0.3f
, dummy
.opacity());
291 EXPECT_EQ(0.3f
, dummy_impl
.opacity());
293 EXPECT_EQ(kInitialTickTime
,
294 controller
->GetAnimationById(animation_id
)->start_time());
295 EXPECT_EQ(kInitialTickTime
,
296 controller_impl
->GetAnimationById(animation_id
)->start_time());
298 // Pause the animation at the middle of the second range so the offset
299 // delays animation until the middle of the third range.
300 controller
->PauseAnimation(animation_id
, TimeDelta::FromSecondsD(1.5));
301 EXPECT_EQ(Animation::PAUSED
,
302 controller
->GetAnimationById(animation_id
)->run_state());
304 // The pause run state change should make it to the impl thread controller.
305 controller
->PushAnimationUpdatesTo(controller_impl
.get());
306 controller_impl
->ActivateAnimations();
308 // Advance time so it stays within the first range.
309 time
+= TimeDelta::FromMilliseconds(10);
310 controller
->Animate(time
);
311 controller_impl
->Animate(time
);
313 EXPECT_EQ(Animation::PAUSED
,
314 controller_impl
->GetAnimationById(animation_id
)->run_state());
316 // Opacity value doesn't depend on time if paused at specified time offset.
317 EXPECT_EQ(0.4f
, dummy
.opacity());
318 EXPECT_EQ(0.4f
, dummy_impl
.opacity());
321 TEST(LayerAnimationControllerTest
, DoNotSyncFinishedAnimation
) {
322 FakeLayerAnimationValueObserver dummy_impl
;
323 scoped_refptr
<LayerAnimationController
> controller_impl(
324 LayerAnimationController::Create(0));
325 controller_impl
->AddValueObserver(&dummy_impl
);
326 FakeLayerAnimationValueObserver dummy
;
327 scoped_refptr
<LayerAnimationController
> controller(
328 LayerAnimationController::Create(0));
329 controller
->AddValueObserver(&dummy
);
330 scoped_ptr
<AnimationEventsVector
> events(
331 make_scoped_ptr(new AnimationEventsVector
));
333 EXPECT_FALSE(controller_impl
->GetAnimation(Animation::OPACITY
));
336 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
338 controller
->PushAnimationUpdatesTo(controller_impl
.get());
339 controller_impl
->ActivateAnimations();
341 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
342 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
343 controller_impl
->GetAnimationById(animation_id
)->run_state());
345 events
.reset(new AnimationEventsVector
);
346 controller_impl
->Animate(kInitialTickTime
);
347 controller_impl
->UpdateState(true, events
.get());
348 EXPECT_EQ(1u, events
->size());
349 EXPECT_EQ(AnimationEvent::STARTED
, (*events
)[0].type
);
351 // Notify main thread controller that the animation has started.
352 controller
->NotifyAnimationStarted((*events
)[0]);
354 // Complete animation on impl thread.
355 events
.reset(new AnimationEventsVector
);
356 controller_impl
->Animate(kInitialTickTime
+ TimeDelta::FromSeconds(1));
357 controller_impl
->UpdateState(true, events
.get());
358 EXPECT_EQ(1u, events
->size());
359 EXPECT_EQ(AnimationEvent::FINISHED
, (*events
)[0].type
);
361 controller
->NotifyAnimationFinished((*events
)[0]);
363 controller
->Animate(kInitialTickTime
+ TimeDelta::FromSeconds(2));
364 controller
->UpdateState(true, nullptr);
366 controller
->PushAnimationUpdatesTo(controller_impl
.get());
367 controller_impl
->ActivateAnimations();
368 EXPECT_FALSE(controller
->GetAnimationById(animation_id
));
369 EXPECT_FALSE(controller_impl
->GetAnimationById(animation_id
));
372 // Ensure that a finished animation is eventually deleted by both the
373 // main-thread and the impl-thread controllers.
374 TEST(LayerAnimationControllerTest
, AnimationsAreDeleted
) {
375 FakeLayerAnimationValueObserver dummy
;
376 FakeLayerAnimationValueObserver dummy_impl
;
377 scoped_ptr
<AnimationEventsVector
> events(
378 make_scoped_ptr(new AnimationEventsVector
));
379 scoped_refptr
<LayerAnimationController
> controller(
380 LayerAnimationController::Create(0));
381 scoped_refptr
<LayerAnimationController
> controller_impl(
382 LayerAnimationController::Create(0));
383 controller
->AddValueObserver(&dummy
);
384 controller_impl
->AddValueObserver(&dummy_impl
);
386 AddOpacityTransitionToController(controller
.get(), 1.0, 0.0f
, 1.0f
, false);
387 controller
->Animate(kInitialTickTime
);
388 controller
->UpdateState(true, nullptr);
389 controller
->PushAnimationUpdatesTo(controller_impl
.get());
390 controller_impl
->ActivateAnimations();
392 controller_impl
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
393 controller_impl
->UpdateState(true, events
.get());
395 // There should be a STARTED event for the animation.
396 EXPECT_EQ(1u, events
->size());
397 EXPECT_EQ(AnimationEvent::STARTED
, (*events
)[0].type
);
398 controller
->NotifyAnimationStarted((*events
)[0]);
400 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
401 controller
->UpdateState(true, nullptr);
403 EXPECT_FALSE(dummy
.animation_waiting_for_deletion());
404 EXPECT_FALSE(dummy_impl
.animation_waiting_for_deletion());
406 events
.reset(new AnimationEventsVector
);
407 controller_impl
->Animate(kInitialTickTime
+
408 TimeDelta::FromMilliseconds(2000));
409 controller_impl
->UpdateState(true, events
.get());
411 EXPECT_TRUE(dummy_impl
.animation_waiting_for_deletion());
413 // There should be a FINISHED event for the animation.
414 EXPECT_EQ(1u, events
->size());
415 EXPECT_EQ(AnimationEvent::FINISHED
, (*events
)[0].type
);
417 // Neither controller should have deleted the animation yet.
418 EXPECT_TRUE(controller
->GetAnimation(Animation::OPACITY
));
419 EXPECT_TRUE(controller_impl
->GetAnimation(Animation::OPACITY
));
421 controller
->NotifyAnimationFinished((*events
)[0]);
423 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(3000));
424 controller
->UpdateState(true, nullptr);
425 EXPECT_TRUE(dummy
.animation_waiting_for_deletion());
427 controller
->PushAnimationUpdatesTo(controller_impl
.get());
429 // Both controllers should now have deleted the animation. The impl controller
430 // should have deleted the animation even though activation has not occurred,
431 // since the animation was already waiting for deletion when
432 // PushAnimationUpdatesTo was called.
433 EXPECT_FALSE(controller
->has_any_animation());
434 EXPECT_FALSE(controller_impl
->has_any_animation());
437 // Tests that transitioning opacity from 0 to 1 works as expected.
439 static const AnimationEvent
* GetMostRecentPropertyUpdateEvent(
440 const AnimationEventsVector
* events
) {
441 const AnimationEvent
* event
= 0;
442 for (size_t i
= 0; i
< events
->size(); ++i
)
443 if ((*events
)[i
].type
== AnimationEvent::PROPERTY_UPDATE
)
444 event
= &(*events
)[i
];
449 TEST(LayerAnimationControllerTest
, TrivialTransition
) {
450 scoped_ptr
<AnimationEventsVector
> events(
451 make_scoped_ptr(new AnimationEventsVector
));
452 FakeLayerAnimationValueObserver dummy
;
453 scoped_refptr
<LayerAnimationController
> controller(
454 LayerAnimationController::Create(0));
455 controller
->AddValueObserver(&dummy
);
457 scoped_ptr
<Animation
> to_add(CreateAnimation(
458 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
459 1, Animation::OPACITY
));
461 EXPECT_FALSE(controller
->needs_to_start_animations_for_testing());
462 controller
->AddAnimation(to_add
.Pass());
463 EXPECT_TRUE(controller
->needs_to_start_animations_for_testing());
464 controller
->Animate(kInitialTickTime
);
465 EXPECT_FALSE(controller
->needs_to_start_animations_for_testing());
466 controller
->UpdateState(true, events
.get());
467 EXPECT_TRUE(controller
->HasActiveAnimation());
468 EXPECT_EQ(0.f
, dummy
.opacity());
469 // A non-impl-only animation should not generate property updates.
470 const AnimationEvent
* event
= GetMostRecentPropertyUpdateEvent(events
.get());
472 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
473 controller
->UpdateState(true, events
.get());
474 EXPECT_EQ(1.f
, dummy
.opacity());
475 EXPECT_FALSE(controller
->HasActiveAnimation());
476 event
= GetMostRecentPropertyUpdateEvent(events
.get());
480 TEST(LayerAnimationControllerTest
, TrivialTransitionOnImpl
) {
481 scoped_ptr
<AnimationEventsVector
> events(
482 make_scoped_ptr(new AnimationEventsVector
));
483 FakeLayerAnimationValueObserver dummy_impl
;
484 scoped_refptr
<LayerAnimationController
> controller_impl(
485 LayerAnimationController::Create(0));
486 controller_impl
->AddValueObserver(&dummy_impl
);
488 scoped_ptr
<Animation
> to_add(CreateAnimation(
489 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
490 1, Animation::OPACITY
));
491 to_add
->set_is_impl_only(true);
493 controller_impl
->AddAnimation(to_add
.Pass());
494 controller_impl
->Animate(kInitialTickTime
);
495 controller_impl
->UpdateState(true, events
.get());
496 EXPECT_TRUE(controller_impl
->HasActiveAnimation());
497 EXPECT_EQ(0.f
, dummy_impl
.opacity());
498 EXPECT_EQ(1u, events
->size());
499 const AnimationEvent
* start_opacity_event
=
500 GetMostRecentPropertyUpdateEvent(events
.get());
501 EXPECT_EQ(0.f
, start_opacity_event
->opacity
);
503 controller_impl
->Animate(kInitialTickTime
+
504 TimeDelta::FromMilliseconds(1000));
505 controller_impl
->UpdateState(true, events
.get());
506 EXPECT_EQ(1.f
, dummy_impl
.opacity());
507 EXPECT_FALSE(controller_impl
->HasActiveAnimation());
508 EXPECT_EQ(2u, events
->size());
509 const AnimationEvent
* end_opacity_event
=
510 GetMostRecentPropertyUpdateEvent(events
.get());
511 EXPECT_EQ(1.f
, end_opacity_event
->opacity
);
514 TEST(LayerAnimationControllerTest
, TrivialTransformOnImpl
) {
515 scoped_ptr
<AnimationEventsVector
> events(
516 make_scoped_ptr(new AnimationEventsVector
));
517 FakeLayerAnimationValueObserver dummy_impl
;
518 scoped_refptr
<LayerAnimationController
> controller_impl(
519 LayerAnimationController::Create(0));
520 controller_impl
->AddValueObserver(&dummy_impl
);
522 // Choose different values for x and y to avoid coincidental values in the
523 // observed transforms.
524 const float delta_x
= 3;
525 const float delta_y
= 4;
527 scoped_ptr
<KeyframedTransformAnimationCurve
> curve(
528 KeyframedTransformAnimationCurve::Create());
530 // Create simple TRANSFORM animation.
531 TransformOperations operations
;
533 TransformKeyframe::Create(base::TimeDelta(), operations
, nullptr));
534 operations
.AppendTranslate(delta_x
, delta_y
, 0);
535 curve
->AddKeyframe(TransformKeyframe::Create(
536 base::TimeDelta::FromSecondsD(1.0), operations
, nullptr));
538 scoped_ptr
<Animation
> animation(
539 Animation::Create(curve
.Pass(), 1, 0, Animation::TRANSFORM
));
540 animation
->set_is_impl_only(true);
541 controller_impl
->AddAnimation(animation
.Pass());
544 controller_impl
->Animate(kInitialTickTime
);
545 controller_impl
->UpdateState(true, events
.get());
546 EXPECT_TRUE(controller_impl
->HasActiveAnimation());
547 EXPECT_EQ(gfx::Transform(), dummy_impl
.transform());
548 EXPECT_EQ(1u, events
->size());
549 const AnimationEvent
* start_transform_event
=
550 GetMostRecentPropertyUpdateEvent(events
.get());
551 ASSERT_TRUE(start_transform_event
);
552 EXPECT_EQ(gfx::Transform(), start_transform_event
->transform
);
553 EXPECT_TRUE(start_transform_event
->is_impl_only
);
555 gfx::Transform expected_transform
;
556 expected_transform
.Translate(delta_x
, delta_y
);
558 controller_impl
->Animate(kInitialTickTime
+
559 TimeDelta::FromMilliseconds(1000));
560 controller_impl
->UpdateState(true, events
.get());
561 EXPECT_EQ(expected_transform
, dummy_impl
.transform());
562 EXPECT_FALSE(controller_impl
->HasActiveAnimation());
563 EXPECT_EQ(2u, events
->size());
564 const AnimationEvent
* end_transform_event
=
565 GetMostRecentPropertyUpdateEvent(events
.get());
566 EXPECT_EQ(expected_transform
, end_transform_event
->transform
);
567 EXPECT_TRUE(end_transform_event
->is_impl_only
);
570 TEST(LayerAnimationControllerTest
, FilterTransition
) {
571 scoped_ptr
<AnimationEventsVector
> events(
572 make_scoped_ptr(new AnimationEventsVector
));
573 FakeLayerAnimationValueObserver dummy
;
574 scoped_refptr
<LayerAnimationController
> controller(
575 LayerAnimationController::Create(0));
576 controller
->AddValueObserver(&dummy
);
578 scoped_ptr
<KeyframedFilterAnimationCurve
> curve(
579 KeyframedFilterAnimationCurve::Create());
581 FilterOperations start_filters
;
582 start_filters
.Append(FilterOperation::CreateBrightnessFilter(1.f
));
584 FilterKeyframe::Create(base::TimeDelta(), start_filters
, nullptr));
585 FilterOperations end_filters
;
586 end_filters
.Append(FilterOperation::CreateBrightnessFilter(2.f
));
587 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
588 end_filters
, nullptr));
590 scoped_ptr
<Animation
> animation(
591 Animation::Create(curve
.Pass(), 1, 0, Animation::FILTER
));
592 controller
->AddAnimation(animation
.Pass());
594 controller
->Animate(kInitialTickTime
);
595 controller
->UpdateState(true, events
.get());
596 EXPECT_TRUE(controller
->HasActiveAnimation());
597 EXPECT_EQ(start_filters
, dummy
.filters());
598 // A non-impl-only animation should not generate property updates.
599 const AnimationEvent
* event
= GetMostRecentPropertyUpdateEvent(events
.get());
602 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
603 controller
->UpdateState(true, events
.get());
604 EXPECT_EQ(1u, dummy
.filters().size());
605 EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f
),
606 dummy
.filters().at(0));
607 event
= GetMostRecentPropertyUpdateEvent(events
.get());
610 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
611 controller
->UpdateState(true, events
.get());
612 EXPECT_EQ(end_filters
, dummy
.filters());
613 EXPECT_FALSE(controller
->HasActiveAnimation());
614 event
= GetMostRecentPropertyUpdateEvent(events
.get());
618 TEST(LayerAnimationControllerTest
, FilterTransitionOnImplOnly
) {
619 scoped_ptr
<AnimationEventsVector
> events(
620 make_scoped_ptr(new AnimationEventsVector
));
621 FakeLayerAnimationValueObserver dummy_impl
;
622 scoped_refptr
<LayerAnimationController
> controller_impl(
623 LayerAnimationController::Create(0));
624 controller_impl
->AddValueObserver(&dummy_impl
);
626 scoped_ptr
<KeyframedFilterAnimationCurve
> curve(
627 KeyframedFilterAnimationCurve::Create());
629 // Create simple FILTER animation.
630 FilterOperations start_filters
;
631 start_filters
.Append(FilterOperation::CreateBrightnessFilter(1.f
));
633 FilterKeyframe::Create(base::TimeDelta(), start_filters
, nullptr));
634 FilterOperations end_filters
;
635 end_filters
.Append(FilterOperation::CreateBrightnessFilter(2.f
));
636 curve
->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
637 end_filters
, nullptr));
639 scoped_ptr
<Animation
> animation(
640 Animation::Create(curve
.Pass(), 1, 0, Animation::FILTER
));
641 animation
->set_is_impl_only(true);
642 controller_impl
->AddAnimation(animation
.Pass());
645 controller_impl
->Animate(kInitialTickTime
);
646 controller_impl
->UpdateState(true, events
.get());
647 EXPECT_TRUE(controller_impl
->HasActiveAnimation());
648 EXPECT_EQ(start_filters
, dummy_impl
.filters());
649 EXPECT_EQ(1u, events
->size());
650 const AnimationEvent
* start_filter_event
=
651 GetMostRecentPropertyUpdateEvent(events
.get());
652 EXPECT_TRUE(start_filter_event
);
653 EXPECT_EQ(start_filters
, start_filter_event
->filters
);
654 EXPECT_TRUE(start_filter_event
->is_impl_only
);
656 controller_impl
->Animate(kInitialTickTime
+
657 TimeDelta::FromMilliseconds(1000));
658 controller_impl
->UpdateState(true, events
.get());
659 EXPECT_EQ(end_filters
, dummy_impl
.filters());
660 EXPECT_FALSE(controller_impl
->HasActiveAnimation());
661 EXPECT_EQ(2u, events
->size());
662 const AnimationEvent
* end_filter_event
=
663 GetMostRecentPropertyUpdateEvent(events
.get());
664 EXPECT_TRUE(end_filter_event
);
665 EXPECT_EQ(end_filters
, end_filter_event
->filters
);
666 EXPECT_TRUE(end_filter_event
->is_impl_only
);
669 TEST(LayerAnimationControllerTest
, ScrollOffsetTransition
) {
670 FakeLayerAnimationValueObserver dummy_impl
;
671 FakeLayerAnimationValueProvider dummy_provider_impl
;
672 scoped_refptr
<LayerAnimationController
> controller_impl(
673 LayerAnimationController::Create(0));
674 controller_impl
->AddValueObserver(&dummy_impl
);
675 controller_impl
->set_value_provider(&dummy_provider_impl
);
676 scoped_ptr
<AnimationEventsVector
> events(
677 make_scoped_ptr(new AnimationEventsVector
));
678 FakeLayerAnimationValueObserver dummy
;
679 FakeLayerAnimationValueProvider dummy_provider
;
680 scoped_refptr
<LayerAnimationController
> controller(
681 LayerAnimationController::Create(0));
682 controller
->AddValueObserver(&dummy
);
683 controller
->set_value_provider(&dummy_provider
);
685 gfx::ScrollOffset
initial_value(100.f
, 300.f
);
686 gfx::ScrollOffset
target_value(300.f
, 200.f
);
687 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
688 ScrollOffsetAnimationCurve::Create(
690 EaseInOutTimingFunction::Create().Pass()));
692 scoped_ptr
<Animation
> animation(
693 Animation::Create(curve
.Pass(), 1, 0, Animation::SCROLL_OFFSET
));
694 animation
->set_needs_synchronized_start_time(true);
695 controller
->AddAnimation(animation
.Pass());
697 dummy_provider_impl
.set_scroll_offset(initial_value
);
698 controller
->PushAnimationUpdatesTo(controller_impl
.get());
699 controller_impl
->ActivateAnimations();
700 EXPECT_TRUE(controller_impl
->GetAnimation(Animation::SCROLL_OFFSET
));
701 TimeDelta duration
= controller_impl
->GetAnimation(Animation::SCROLL_OFFSET
)
706 controller
->GetAnimation(Animation::SCROLL_OFFSET
)->curve()->Duration());
708 controller
->Animate(kInitialTickTime
);
709 controller
->UpdateState(true, nullptr);
710 EXPECT_TRUE(controller
->HasActiveAnimation());
711 EXPECT_EQ(initial_value
, dummy
.scroll_offset());
713 controller_impl
->Animate(kInitialTickTime
);
714 controller_impl
->UpdateState(true, events
.get());
715 EXPECT_TRUE(controller_impl
->HasActiveAnimation());
716 EXPECT_EQ(initial_value
, dummy_impl
.scroll_offset());
717 // Scroll offset animations should not generate property updates.
718 const AnimationEvent
* event
= GetMostRecentPropertyUpdateEvent(events
.get());
721 controller
->NotifyAnimationStarted((*events
)[0]);
722 controller
->Animate(kInitialTickTime
+ duration
/ 2);
723 controller
->UpdateState(true, nullptr);
724 EXPECT_TRUE(controller
->HasActiveAnimation());
725 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f
, 250.f
), dummy
.scroll_offset());
727 controller_impl
->Animate(kInitialTickTime
+ duration
/ 2);
728 controller_impl
->UpdateState(true, events
.get());
729 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f
, 250.f
),
730 dummy_impl
.scroll_offset());
731 event
= GetMostRecentPropertyUpdateEvent(events
.get());
734 controller_impl
->Animate(kInitialTickTime
+ duration
);
735 controller_impl
->UpdateState(true, events
.get());
736 EXPECT_VECTOR2DF_EQ(target_value
, dummy_impl
.scroll_offset());
737 EXPECT_FALSE(controller_impl
->HasActiveAnimation());
738 event
= GetMostRecentPropertyUpdateEvent(events
.get());
741 controller
->Animate(kInitialTickTime
+ duration
);
742 controller
->UpdateState(true, nullptr);
743 EXPECT_VECTOR2DF_EQ(target_value
, dummy
.scroll_offset());
744 EXPECT_FALSE(controller
->HasActiveAnimation());
747 // Ensure that when the impl controller doesn't have a value provider,
748 // the main-thread controller's value provider is used to obtain the intial
750 TEST(LayerAnimationControllerTest
, ScrollOffsetTransitionNoImplProvider
) {
751 FakeLayerAnimationValueObserver dummy_impl
;
752 scoped_refptr
<LayerAnimationController
> controller_impl(
753 LayerAnimationController::Create(0));
754 controller_impl
->AddValueObserver(&dummy_impl
);
755 scoped_ptr
<AnimationEventsVector
> events(
756 make_scoped_ptr(new AnimationEventsVector
));
757 FakeLayerAnimationValueObserver dummy
;
758 FakeLayerAnimationValueProvider dummy_provider
;
759 scoped_refptr
<LayerAnimationController
> controller(
760 LayerAnimationController::Create(0));
761 controller
->AddValueObserver(&dummy
);
762 controller
->set_value_provider(&dummy_provider
);
764 gfx::ScrollOffset
initial_value(500.f
, 100.f
);
765 gfx::ScrollOffset
target_value(300.f
, 200.f
);
766 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
767 ScrollOffsetAnimationCurve::Create(
769 EaseInOutTimingFunction::Create().Pass()));
771 scoped_ptr
<Animation
> animation(
772 Animation::Create(curve
.Pass(), 1, 0, Animation::SCROLL_OFFSET
));
773 animation
->set_needs_synchronized_start_time(true);
774 controller
->AddAnimation(animation
.Pass());
776 dummy_provider
.set_scroll_offset(initial_value
);
777 controller
->PushAnimationUpdatesTo(controller_impl
.get());
778 controller_impl
->ActivateAnimations();
779 EXPECT_TRUE(controller_impl
->GetAnimation(Animation::SCROLL_OFFSET
));
780 TimeDelta duration
= controller_impl
->GetAnimation(Animation::SCROLL_OFFSET
)
785 controller
->GetAnimation(Animation::SCROLL_OFFSET
)->curve()->Duration());
787 controller
->Animate(kInitialTickTime
);
788 controller
->UpdateState(true, nullptr);
789 EXPECT_TRUE(controller
->HasActiveAnimation());
790 EXPECT_EQ(initial_value
, dummy
.scroll_offset());
792 controller_impl
->Animate(kInitialTickTime
);
793 controller_impl
->UpdateState(true, events
.get());
794 EXPECT_TRUE(controller_impl
->HasActiveAnimation());
795 EXPECT_EQ(initial_value
, dummy_impl
.scroll_offset());
796 // Scroll offset animations should not generate property updates.
797 const AnimationEvent
* event
= GetMostRecentPropertyUpdateEvent(events
.get());
801 controller
->NotifyAnimationStarted((*events
)[0]);
802 controller
->Animate(kInitialTickTime
+ duration
/ 2);
803 controller
->UpdateState(true, nullptr);
804 EXPECT_TRUE(controller
->HasActiveAnimation());
805 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f
, 150.f
), dummy
.scroll_offset());
807 controller_impl
->Animate(kInitialTickTime
+ duration
/ 2);
808 controller_impl
->UpdateState(true, events
.get());
809 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f
, 150.f
),
810 dummy_impl
.scroll_offset());
811 event
= GetMostRecentPropertyUpdateEvent(events
.get());
814 controller_impl
->Animate(kInitialTickTime
+ duration
);
815 controller_impl
->UpdateState(true, events
.get());
816 EXPECT_VECTOR2DF_EQ(target_value
, dummy_impl
.scroll_offset());
817 EXPECT_FALSE(controller_impl
->HasActiveAnimation());
818 event
= GetMostRecentPropertyUpdateEvent(events
.get());
821 controller
->Animate(kInitialTickTime
+ duration
);
822 controller
->UpdateState(true, nullptr);
823 EXPECT_VECTOR2DF_EQ(target_value
, dummy
.scroll_offset());
824 EXPECT_FALSE(controller
->HasActiveAnimation());
827 TEST(LayerAnimationControllerTest
, ScrollOffsetTransitionOnImplOnly
) {
828 FakeLayerAnimationValueObserver dummy_impl
;
829 scoped_refptr
<LayerAnimationController
> controller_impl(
830 LayerAnimationController::Create(0));
831 controller_impl
->AddValueObserver(&dummy_impl
);
832 scoped_ptr
<AnimationEventsVector
> events(
833 make_scoped_ptr(new AnimationEventsVector
));
835 gfx::ScrollOffset
initial_value(100.f
, 300.f
);
836 gfx::ScrollOffset
target_value(300.f
, 200.f
);
837 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
838 ScrollOffsetAnimationCurve::Create(
840 EaseInOutTimingFunction::Create().Pass()));
841 curve
->SetInitialValue(initial_value
);
842 double duration_in_seconds
= curve
->Duration().InSecondsF();
844 scoped_ptr
<Animation
> animation(
845 Animation::Create(curve
.Pass(), 1, 0, Animation::SCROLL_OFFSET
));
846 animation
->set_is_impl_only(true);
847 controller_impl
->AddAnimation(animation
.Pass());
849 controller_impl
->Animate(kInitialTickTime
);
850 controller_impl
->UpdateState(true, events
.get());
851 EXPECT_TRUE(controller_impl
->HasActiveAnimation());
852 EXPECT_EQ(initial_value
, dummy_impl
.scroll_offset());
853 // Scroll offset animations should not generate property updates.
854 const AnimationEvent
* event
= GetMostRecentPropertyUpdateEvent(events
.get());
857 TimeDelta duration
= TimeDelta::FromMicroseconds(
858 duration_in_seconds
* base::Time::kMicrosecondsPerSecond
);
860 controller_impl
->Animate(kInitialTickTime
+ duration
/ 2);
861 controller_impl
->UpdateState(true, events
.get());
862 EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f
, 250.f
),
863 dummy_impl
.scroll_offset());
864 event
= GetMostRecentPropertyUpdateEvent(events
.get());
867 controller_impl
->Animate(kInitialTickTime
+ duration
);
868 controller_impl
->UpdateState(true, events
.get());
869 EXPECT_VECTOR2DF_EQ(target_value
, dummy_impl
.scroll_offset());
870 EXPECT_FALSE(controller_impl
->HasActiveAnimation());
871 event
= GetMostRecentPropertyUpdateEvent(events
.get());
875 TEST(LayerAnimationControllerTest
, ScrollOffsetRemovalClearsScrollDelta
) {
876 FakeLayerAnimationValueObserver dummy_impl
;
877 FakeLayerAnimationValueProvider dummy_provider_impl
;
878 scoped_refptr
<LayerAnimationController
> controller_impl(
879 LayerAnimationController::Create(0));
880 controller_impl
->AddValueObserver(&dummy_impl
);
881 controller_impl
->set_value_provider(&dummy_provider_impl
);
882 scoped_ptr
<AnimationEventsVector
> events(
883 make_scoped_ptr(new AnimationEventsVector
));
884 FakeLayerAnimationValueObserver dummy
;
885 FakeLayerAnimationValueProvider dummy_provider
;
886 scoped_refptr
<LayerAnimationController
> controller(
887 LayerAnimationController::Create(0));
888 controller
->AddValueObserver(&dummy
);
889 controller
->set_value_provider(&dummy_provider
);
891 // First test the 1-argument version of RemoveAnimation.
892 gfx::ScrollOffset
target_value(300.f
, 200.f
);
893 scoped_ptr
<ScrollOffsetAnimationCurve
> curve(
894 ScrollOffsetAnimationCurve::Create(
895 target_value
, EaseInOutTimingFunction::Create().Pass()));
897 int animation_id
= 1;
898 scoped_ptr
<Animation
> animation(Animation::Create(
899 curve
.Pass(), animation_id
, 0, Animation::SCROLL_OFFSET
));
900 animation
->set_needs_synchronized_start_time(true);
901 controller
->AddAnimation(animation
.Pass());
902 controller
->PushAnimationUpdatesTo(controller_impl
.get());
903 controller_impl
->ActivateAnimations();
904 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
905 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
907 controller
->RemoveAnimation(animation_id
);
908 EXPECT_TRUE(controller
->scroll_offset_animation_was_interrupted());
910 controller
->PushAnimationUpdatesTo(controller_impl
.get());
911 EXPECT_TRUE(controller_impl
->scroll_offset_animation_was_interrupted());
912 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
914 controller_impl
->ActivateAnimations();
915 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
917 // Now, test the 2-argument version of RemoveAnimation.
918 curve
= ScrollOffsetAnimationCurve::Create(
919 target_value
, EaseInOutTimingFunction::Create().Pass());
920 animation
= Animation::Create(curve
.Pass(), animation_id
, 0,
921 Animation::SCROLL_OFFSET
);
922 animation
->set_needs_synchronized_start_time(true);
923 controller
->AddAnimation(animation
.Pass());
924 controller
->PushAnimationUpdatesTo(controller_impl
.get());
925 controller_impl
->ActivateAnimations();
926 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
927 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
929 controller
->RemoveAnimation(animation_id
);
930 EXPECT_TRUE(controller
->scroll_offset_animation_was_interrupted());
932 controller
->PushAnimationUpdatesTo(controller_impl
.get());
933 EXPECT_TRUE(controller_impl
->scroll_offset_animation_was_interrupted());
934 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
936 controller_impl
->ActivateAnimations();
937 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
939 // Check that removing non-scroll-offset animations does not cause
940 // scroll_offset_animation_was_interrupted() to get set.
941 animation_id
= AddAnimatedTransformToController(controller
.get(), 1.0, 1, 2);
942 controller
->PushAnimationUpdatesTo(controller_impl
.get());
943 controller_impl
->ActivateAnimations();
944 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
945 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
947 controller
->RemoveAnimation(animation_id
);
948 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
950 controller
->PushAnimationUpdatesTo(controller_impl
.get());
951 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
952 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
954 controller_impl
->ActivateAnimations();
955 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
958 AddAnimatedFilterToController(controller
.get(), 1.0, 0.1f
, 0.2f
);
959 controller
->PushAnimationUpdatesTo(controller_impl
.get());
960 controller_impl
->ActivateAnimations();
961 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
962 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
964 controller
->RemoveAnimation(animation_id
);
965 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
967 controller
->PushAnimationUpdatesTo(controller_impl
.get());
968 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
969 EXPECT_FALSE(controller
->scroll_offset_animation_was_interrupted());
971 controller_impl
->ActivateAnimations();
972 EXPECT_FALSE(controller_impl
->scroll_offset_animation_was_interrupted());
975 class FakeAnimationDelegate
: public AnimationDelegate
{
977 FakeAnimationDelegate()
978 : started_(false), finished_(false), start_time_(base::TimeTicks()) {}
980 void NotifyAnimationStarted(TimeTicks monotonic_time
,
981 Animation::TargetProperty target_property
,
982 int group
) override
{
984 start_time_
= monotonic_time
;
987 void NotifyAnimationFinished(TimeTicks monotonic_time
,
988 Animation::TargetProperty target_property
,
989 int group
) override
{
993 bool started() { return started_
; }
995 bool finished() { return finished_
; }
997 TimeTicks
start_time() { return start_time_
; }
1002 TimeTicks start_time_
;
1005 // Tests that impl-only animations lead to start and finished notifications
1006 // on the impl thread controller's animation delegate.
1007 TEST(LayerAnimationControllerTest
,
1008 NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate
) {
1009 FakeLayerAnimationValueObserver dummy_impl
;
1010 scoped_refptr
<LayerAnimationController
> controller_impl(
1011 LayerAnimationController::Create(0));
1012 controller_impl
->AddValueObserver(&dummy_impl
);
1013 scoped_ptr
<AnimationEventsVector
> events(
1014 make_scoped_ptr(new AnimationEventsVector
));
1015 FakeAnimationDelegate delegate
;
1016 controller_impl
->set_layer_animation_delegate(&delegate
);
1018 scoped_ptr
<Animation
> to_add(CreateAnimation(
1019 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1020 1, Animation::OPACITY
));
1021 to_add
->set_is_impl_only(true);
1022 controller_impl
->AddAnimation(to_add
.Pass());
1024 EXPECT_FALSE(delegate
.started());
1025 EXPECT_FALSE(delegate
.finished());
1027 controller_impl
->Animate(kInitialTickTime
);
1028 controller_impl
->UpdateState(true, events
.get());
1030 EXPECT_TRUE(delegate
.started());
1031 EXPECT_FALSE(delegate
.finished());
1033 events
.reset(new AnimationEventsVector
);
1034 controller_impl
->Animate(kInitialTickTime
+
1035 TimeDelta::FromMilliseconds(1000));
1036 controller_impl
->UpdateState(true, events
.get());
1038 EXPECT_TRUE(delegate
.started());
1039 EXPECT_TRUE(delegate
.finished());
1042 // Tests that specified start times are sent to the main thread delegate
1043 TEST(LayerAnimationControllerTest
,
1044 SpecifiedStartTimesAreSentToMainThreadDelegate
) {
1045 FakeLayerAnimationValueObserver dummy_impl
;
1046 scoped_refptr
<LayerAnimationController
> controller_impl(
1047 LayerAnimationController::Create(0));
1048 controller_impl
->AddValueObserver(&dummy_impl
);
1049 FakeLayerAnimationValueObserver dummy
;
1050 scoped_refptr
<LayerAnimationController
> controller(
1051 LayerAnimationController::Create(0));
1052 controller
->AddValueObserver(&dummy
);
1053 FakeAnimationDelegate delegate
;
1054 controller
->set_layer_animation_delegate(&delegate
);
1057 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
1059 const TimeTicks start_time
= TicksFromSecondsF(123);
1060 controller
->GetAnimation(Animation::OPACITY
)->set_start_time(start_time
);
1062 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1063 controller_impl
->ActivateAnimations();
1065 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
1066 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
1067 controller_impl
->GetAnimationById(animation_id
)->run_state());
1069 AnimationEventsVector events
;
1070 controller_impl
->Animate(kInitialTickTime
);
1071 controller_impl
->UpdateState(true, &events
);
1073 // Synchronize the start times.
1074 EXPECT_EQ(1u, events
.size());
1075 controller
->NotifyAnimationStarted(events
[0]);
1077 // Validate start time on the main thread delegate.
1078 EXPECT_EQ(start_time
, delegate
.start_time());
1081 class FakeLayerAnimationEventObserver
: public LayerAnimationEventObserver
{
1083 FakeLayerAnimationEventObserver() : start_time_(base::TimeTicks()) {}
1085 void OnAnimationStarted(const AnimationEvent
& event
) override
{
1086 start_time_
= event
.monotonic_time
;
1089 TimeTicks
start_time() { return start_time_
; }
1092 TimeTicks start_time_
;
1095 // Tests that specified start times are sent to the event observers
1096 TEST(LayerAnimationControllerTest
, SpecifiedStartTimesAreSentToEventObservers
) {
1097 FakeLayerAnimationValueObserver dummy_impl
;
1098 scoped_refptr
<LayerAnimationController
> controller_impl(
1099 LayerAnimationController::Create(0));
1100 controller_impl
->AddValueObserver(&dummy_impl
);
1101 FakeLayerAnimationValueObserver dummy
;
1102 scoped_refptr
<LayerAnimationController
> controller(
1103 LayerAnimationController::Create(0));
1104 controller
->AddValueObserver(&dummy
);
1105 FakeLayerAnimationEventObserver observer
;
1106 controller
->AddEventObserver(&observer
);
1109 AddOpacityTransitionToController(controller
.get(), 1, 0, 1, false);
1111 const TimeTicks start_time
= TicksFromSecondsF(123);
1112 controller
->GetAnimation(Animation::OPACITY
)->set_start_time(start_time
);
1114 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1115 controller_impl
->ActivateAnimations();
1117 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
1118 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
1119 controller_impl
->GetAnimationById(animation_id
)->run_state());
1121 AnimationEventsVector events
;
1122 controller_impl
->Animate(kInitialTickTime
);
1123 controller_impl
->UpdateState(true, &events
);
1125 // Synchronize the start times.
1126 EXPECT_EQ(1u, events
.size());
1127 controller
->NotifyAnimationStarted(events
[0]);
1129 // Validate start time on the event observer.
1130 EXPECT_EQ(start_time
, observer
.start_time());
1133 // Tests animations that are waiting for a synchronized start time do not
1135 TEST(LayerAnimationControllerTest
,
1136 AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish
) {
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(),
1146 1, Animation::OPACITY
));
1147 to_add
->set_needs_synchronized_start_time(true);
1149 // We should pause at the first keyframe indefinitely waiting for that
1150 // animation to start.
1151 controller
->AddAnimation(to_add
.Pass());
1152 controller
->Animate(kInitialTickTime
);
1153 controller
->UpdateState(true, events
.get());
1154 EXPECT_TRUE(controller
->HasActiveAnimation());
1155 EXPECT_EQ(0.f
, dummy
.opacity());
1156 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1157 controller
->UpdateState(true, events
.get());
1158 EXPECT_TRUE(controller
->HasActiveAnimation());
1159 EXPECT_EQ(0.f
, dummy
.opacity());
1160 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1161 controller
->UpdateState(true, events
.get());
1162 EXPECT_TRUE(controller
->HasActiveAnimation());
1163 EXPECT_EQ(0.f
, dummy
.opacity());
1165 // Send the synchronized start time.
1166 controller
->NotifyAnimationStarted(
1167 AnimationEvent(AnimationEvent::STARTED
, 0, 1, Animation::OPACITY
,
1168 kInitialTickTime
+ TimeDelta::FromMilliseconds(2000)));
1169 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(5000));
1170 controller
->UpdateState(true, events
.get());
1171 EXPECT_EQ(1.f
, dummy
.opacity());
1172 EXPECT_FALSE(controller
->HasActiveAnimation());
1175 // Tests that two queued animations affecting the same property run in sequence.
1176 TEST(LayerAnimationControllerTest
, TrivialQueuing
) {
1177 scoped_ptr
<AnimationEventsVector
> events(
1178 make_scoped_ptr(new AnimationEventsVector
));
1179 FakeLayerAnimationValueObserver dummy
;
1180 scoped_refptr
<LayerAnimationController
> controller(
1181 LayerAnimationController::Create(0));
1182 controller
->AddValueObserver(&dummy
);
1184 EXPECT_FALSE(controller
->needs_to_start_animations_for_testing());
1186 controller
->AddAnimation(CreateAnimation(
1187 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1188 1, Animation::OPACITY
));
1189 controller
->AddAnimation(CreateAnimation(
1190 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 1.f
, 0.5f
))
1192 2, Animation::OPACITY
));
1194 EXPECT_TRUE(controller
->needs_to_start_animations_for_testing());
1196 controller
->Animate(kInitialTickTime
);
1198 // The second animation still needs to be started.
1199 EXPECT_TRUE(controller
->needs_to_start_animations_for_testing());
1201 controller
->UpdateState(true, events
.get());
1202 EXPECT_TRUE(controller
->HasActiveAnimation());
1203 EXPECT_EQ(0.f
, dummy
.opacity());
1205 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1206 EXPECT_TRUE(controller
->needs_to_start_animations_for_testing());
1207 controller
->UpdateState(true, events
.get());
1208 EXPECT_FALSE(controller
->needs_to_start_animations_for_testing());
1210 EXPECT_TRUE(controller
->HasActiveAnimation());
1211 EXPECT_EQ(1.f
, dummy
.opacity());
1212 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1213 controller
->UpdateState(true, events
.get());
1214 EXPECT_EQ(0.5f
, dummy
.opacity());
1215 EXPECT_FALSE(controller
->HasActiveAnimation());
1218 // Tests interrupting a transition with another transition.
1219 TEST(LayerAnimationControllerTest
, Interrupt
) {
1220 scoped_ptr
<AnimationEventsVector
> events(
1221 make_scoped_ptr(new AnimationEventsVector
));
1222 FakeLayerAnimationValueObserver dummy
;
1223 scoped_refptr
<LayerAnimationController
> controller(
1224 LayerAnimationController::Create(0));
1225 controller
->AddValueObserver(&dummy
);
1226 controller
->AddAnimation(CreateAnimation(
1227 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1228 1, Animation::OPACITY
));
1229 controller
->Animate(kInitialTickTime
);
1230 controller
->UpdateState(true, events
.get());
1231 EXPECT_TRUE(controller
->HasActiveAnimation());
1232 EXPECT_EQ(0.f
, dummy
.opacity());
1234 scoped_ptr
<Animation
> to_add(CreateAnimation(
1235 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 1.f
, 0.5f
))
1237 2, Animation::OPACITY
));
1238 controller
->AbortAnimations(Animation::OPACITY
);
1239 controller
->AddAnimation(to_add
.Pass());
1241 // Since the previous animation was aborted, the new animation should start
1242 // right in this call to animate.
1243 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
1244 controller
->UpdateState(true, events
.get());
1245 EXPECT_TRUE(controller
->HasActiveAnimation());
1246 EXPECT_EQ(1.f
, dummy
.opacity());
1247 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1500));
1248 controller
->UpdateState(true, events
.get());
1249 EXPECT_EQ(0.5f
, dummy
.opacity());
1250 EXPECT_FALSE(controller
->HasActiveAnimation());
1253 // Tests scheduling two animations to run together when only one property is
1255 TEST(LayerAnimationControllerTest
, ScheduleTogetherWhenAPropertyIsBlocked
) {
1256 scoped_ptr
<AnimationEventsVector
> events(
1257 make_scoped_ptr(new AnimationEventsVector
));
1258 FakeLayerAnimationValueObserver dummy
;
1259 scoped_refptr
<LayerAnimationController
> controller(
1260 LayerAnimationController::Create(0));
1261 controller
->AddValueObserver(&dummy
);
1263 controller
->AddAnimation(CreateAnimation(
1264 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1)).Pass(), 1,
1265 Animation::TRANSFORM
));
1266 controller
->AddAnimation(CreateAnimation(
1267 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1)).Pass(), 2,
1268 Animation::TRANSFORM
));
1269 controller
->AddAnimation(CreateAnimation(
1270 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1271 2, Animation::OPACITY
));
1273 controller
->Animate(kInitialTickTime
);
1274 controller
->UpdateState(true, events
.get());
1275 EXPECT_EQ(0.f
, dummy
.opacity());
1276 EXPECT_TRUE(controller
->HasActiveAnimation());
1277 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1278 controller
->UpdateState(true, events
.get());
1279 // Should not have started the float transition yet.
1280 EXPECT_TRUE(controller
->HasActiveAnimation());
1281 EXPECT_EQ(0.f
, dummy
.opacity());
1282 // The float animation should have started at time 1 and should be done.
1283 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1284 controller
->UpdateState(true, events
.get());
1285 EXPECT_EQ(1.f
, dummy
.opacity());
1286 EXPECT_FALSE(controller
->HasActiveAnimation());
1289 // Tests scheduling two animations to run together with different lengths and
1290 // another animation queued to start when the shorter animation finishes (should
1291 // wait for both to finish).
1292 TEST(LayerAnimationControllerTest
, ScheduleTogetherWithAnAnimWaiting
) {
1293 scoped_ptr
<AnimationEventsVector
> events(
1294 make_scoped_ptr(new AnimationEventsVector
));
1295 FakeLayerAnimationValueObserver dummy
;
1296 scoped_refptr
<LayerAnimationController
> controller(
1297 LayerAnimationController::Create(0));
1298 controller
->AddValueObserver(&dummy
);
1300 controller
->AddAnimation(CreateAnimation(
1301 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(2)).Pass(), 1,
1302 Animation::TRANSFORM
));
1303 controller
->AddAnimation(CreateAnimation(
1304 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1305 1, Animation::OPACITY
));
1306 controller
->AddAnimation(CreateAnimation(
1307 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 1.f
, 0.5f
))
1309 2, Animation::OPACITY
));
1311 // Animations with id 1 should both start now.
1312 controller
->Animate(kInitialTickTime
);
1313 controller
->UpdateState(true, events
.get());
1314 EXPECT_TRUE(controller
->HasActiveAnimation());
1315 EXPECT_EQ(0.f
, dummy
.opacity());
1316 // The opacity animation should have finished at time 1, but the group
1317 // of animations with id 1 don't finish until time 2 because of the length
1318 // of the transform animation.
1319 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1320 controller
->UpdateState(true, events
.get());
1321 // Should not have started the float transition yet.
1322 EXPECT_TRUE(controller
->HasActiveAnimation());
1323 EXPECT_EQ(1.f
, dummy
.opacity());
1325 // The second opacity animation should start at time 2 and should be done by
1327 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(3000));
1328 controller
->UpdateState(true, events
.get());
1329 EXPECT_EQ(0.5f
, dummy
.opacity());
1330 EXPECT_FALSE(controller
->HasActiveAnimation());
1333 // Test that a looping animation loops and for the correct number of iterations.
1334 TEST(LayerAnimationControllerTest
, TrivialLooping
) {
1335 scoped_ptr
<AnimationEventsVector
> events(
1336 make_scoped_ptr(new AnimationEventsVector
));
1337 FakeLayerAnimationValueObserver dummy
;
1338 scoped_refptr
<LayerAnimationController
> controller(
1339 LayerAnimationController::Create(0));
1340 controller
->AddValueObserver(&dummy
);
1342 scoped_ptr
<Animation
> to_add(CreateAnimation(
1343 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1344 1, Animation::OPACITY
));
1345 to_add
->set_iterations(3);
1346 controller
->AddAnimation(to_add
.Pass());
1348 controller
->Animate(kInitialTickTime
);
1349 controller
->UpdateState(true, events
.get());
1350 EXPECT_TRUE(controller
->HasActiveAnimation());
1351 EXPECT_EQ(0.f
, dummy
.opacity());
1352 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1250));
1353 controller
->UpdateState(true, events
.get());
1354 EXPECT_TRUE(controller
->HasActiveAnimation());
1355 EXPECT_EQ(0.25f
, dummy
.opacity());
1356 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1750));
1357 controller
->UpdateState(true, events
.get());
1358 EXPECT_TRUE(controller
->HasActiveAnimation());
1359 EXPECT_EQ(0.75f
, dummy
.opacity());
1360 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2250));
1361 controller
->UpdateState(true, events
.get());
1362 EXPECT_TRUE(controller
->HasActiveAnimation());
1363 EXPECT_EQ(0.25f
, dummy
.opacity());
1364 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2750));
1365 controller
->UpdateState(true, events
.get());
1366 EXPECT_TRUE(controller
->HasActiveAnimation());
1367 EXPECT_EQ(0.75f
, dummy
.opacity());
1368 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(3000));
1369 controller
->UpdateState(true, events
.get());
1370 EXPECT_FALSE(controller
->HasActiveAnimation());
1371 EXPECT_EQ(1.f
, dummy
.opacity());
1373 // Just be extra sure.
1374 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(4000));
1375 controller
->UpdateState(true, events
.get());
1376 EXPECT_EQ(1.f
, dummy
.opacity());
1379 // Test that an infinitely looping animation does indeed go until aborted.
1380 TEST(LayerAnimationControllerTest
, InfiniteLooping
) {
1381 scoped_ptr
<AnimationEventsVector
> events(
1382 make_scoped_ptr(new AnimationEventsVector
));
1383 FakeLayerAnimationValueObserver dummy
;
1384 scoped_refptr
<LayerAnimationController
> controller(
1385 LayerAnimationController::Create(0));
1386 controller
->AddValueObserver(&dummy
);
1388 scoped_ptr
<Animation
> to_add(CreateAnimation(
1389 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1390 1, Animation::OPACITY
));
1391 to_add
->set_iterations(-1);
1392 controller
->AddAnimation(to_add
.Pass());
1394 controller
->Animate(kInitialTickTime
);
1395 controller
->UpdateState(true, events
.get());
1396 EXPECT_TRUE(controller
->HasActiveAnimation());
1397 EXPECT_EQ(0.f
, dummy
.opacity());
1398 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1250));
1399 controller
->UpdateState(true, events
.get());
1400 EXPECT_TRUE(controller
->HasActiveAnimation());
1401 EXPECT_EQ(0.25f
, dummy
.opacity());
1402 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1750));
1403 controller
->UpdateState(true, events
.get());
1404 EXPECT_TRUE(controller
->HasActiveAnimation());
1405 EXPECT_EQ(0.75f
, dummy
.opacity());
1407 controller
->Animate(kInitialTickTime
+
1408 TimeDelta::FromMilliseconds(1073741824250));
1409 controller
->UpdateState(true, events
.get());
1410 EXPECT_TRUE(controller
->HasActiveAnimation());
1411 EXPECT_EQ(0.25f
, dummy
.opacity());
1412 controller
->Animate(kInitialTickTime
+
1413 TimeDelta::FromMilliseconds(1073741824750));
1414 controller
->UpdateState(true, events
.get());
1415 EXPECT_TRUE(controller
->HasActiveAnimation());
1416 EXPECT_EQ(0.75f
, dummy
.opacity());
1418 EXPECT_TRUE(controller
->GetAnimation(Animation::OPACITY
));
1419 controller
->GetAnimation(Animation::OPACITY
)
1420 ->SetRunState(Animation::ABORTED
,
1421 kInitialTickTime
+ TimeDelta::FromMilliseconds(750));
1422 EXPECT_FALSE(controller
->HasActiveAnimation());
1423 EXPECT_EQ(0.75f
, dummy
.opacity());
1426 // Test that pausing and resuming work as expected.
1427 TEST(LayerAnimationControllerTest
, PauseResume
) {
1428 scoped_ptr
<AnimationEventsVector
> events(
1429 make_scoped_ptr(new AnimationEventsVector
));
1430 FakeLayerAnimationValueObserver dummy
;
1431 scoped_refptr
<LayerAnimationController
> controller(
1432 LayerAnimationController::Create(0));
1433 controller
->AddValueObserver(&dummy
);
1435 controller
->AddAnimation(CreateAnimation(
1436 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1437 1, Animation::OPACITY
));
1439 controller
->Animate(kInitialTickTime
);
1440 controller
->UpdateState(true, events
.get());
1441 EXPECT_TRUE(controller
->HasActiveAnimation());
1442 EXPECT_EQ(0.f
, dummy
.opacity());
1443 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
1444 controller
->UpdateState(true, events
.get());
1445 EXPECT_TRUE(controller
->HasActiveAnimation());
1446 EXPECT_EQ(0.5f
, dummy
.opacity());
1448 EXPECT_TRUE(controller
->GetAnimation(Animation::OPACITY
));
1449 controller
->GetAnimation(Animation::OPACITY
)
1450 ->SetRunState(Animation::PAUSED
,
1451 kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
1453 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1024000));
1454 controller
->UpdateState(true, events
.get());
1455 EXPECT_TRUE(controller
->HasActiveAnimation());
1456 EXPECT_EQ(0.5f
, dummy
.opacity());
1458 EXPECT_TRUE(controller
->GetAnimation(Animation::OPACITY
));
1459 controller
->GetAnimation(Animation::OPACITY
)
1460 ->SetRunState(Animation::RUNNING
,
1461 kInitialTickTime
+ TimeDelta::FromMilliseconds(1024000));
1462 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1024250));
1463 controller
->UpdateState(true, events
.get());
1464 EXPECT_TRUE(controller
->HasActiveAnimation());
1465 EXPECT_EQ(0.75f
, dummy
.opacity());
1467 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1024500));
1468 controller
->UpdateState(true, events
.get());
1469 EXPECT_FALSE(controller
->HasActiveAnimation());
1470 EXPECT_EQ(1.f
, dummy
.opacity());
1473 TEST(LayerAnimationControllerTest
, AbortAGroupedAnimation
) {
1474 scoped_ptr
<AnimationEventsVector
> events(
1475 make_scoped_ptr(new AnimationEventsVector
));
1476 FakeLayerAnimationValueObserver dummy
;
1477 scoped_refptr
<LayerAnimationController
> controller(
1478 LayerAnimationController::Create(0));
1479 controller
->AddValueObserver(&dummy
);
1481 const int animation_id
= 2;
1482 controller
->AddAnimation(Animation::Create(
1483 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1)).Pass(), 1, 1,
1484 Animation::TRANSFORM
));
1485 controller
->AddAnimation(Animation::Create(
1486 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(2.0, 0.f
, 1.f
)).Pass(),
1487 animation_id
, 1, Animation::OPACITY
));
1488 controller
->AddAnimation(Animation::Create(
1489 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 1.f
, 0.75f
))
1491 3, 2, Animation::OPACITY
));
1493 controller
->Animate(kInitialTickTime
);
1494 controller
->UpdateState(true, events
.get());
1495 EXPECT_TRUE(controller
->HasActiveAnimation());
1496 EXPECT_EQ(0.f
, dummy
.opacity());
1497 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1498 controller
->UpdateState(true, events
.get());
1499 EXPECT_TRUE(controller
->HasActiveAnimation());
1500 EXPECT_EQ(0.5f
, dummy
.opacity());
1502 EXPECT_TRUE(controller
->GetAnimationById(animation_id
));
1503 controller
->GetAnimationById(animation_id
)
1504 ->SetRunState(Animation::ABORTED
,
1505 kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1506 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1507 controller
->UpdateState(true, events
.get());
1508 EXPECT_TRUE(controller
->HasActiveAnimation());
1509 EXPECT_EQ(1.f
, dummy
.opacity());
1510 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1511 controller
->UpdateState(true, events
.get());
1512 EXPECT_TRUE(!controller
->HasActiveAnimation());
1513 EXPECT_EQ(0.75f
, dummy
.opacity());
1516 TEST(LayerAnimationControllerTest
, PushUpdatesWhenSynchronizedStartTimeNeeded
) {
1517 FakeLayerAnimationValueObserver dummy_impl
;
1518 scoped_refptr
<LayerAnimationController
> controller_impl(
1519 LayerAnimationController::Create(0));
1520 controller_impl
->AddValueObserver(&dummy_impl
);
1521 scoped_ptr
<AnimationEventsVector
> events(
1522 make_scoped_ptr(new AnimationEventsVector
));
1523 FakeLayerAnimationValueObserver dummy
;
1524 scoped_refptr
<LayerAnimationController
> controller(
1525 LayerAnimationController::Create(0));
1526 controller
->AddValueObserver(&dummy
);
1528 scoped_ptr
<Animation
> to_add(CreateAnimation(
1529 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(2.0, 0.f
, 1.f
)).Pass(),
1530 0, Animation::OPACITY
));
1531 to_add
->set_needs_synchronized_start_time(true);
1532 controller
->AddAnimation(to_add
.Pass());
1534 controller
->Animate(kInitialTickTime
);
1535 controller
->UpdateState(true, events
.get());
1536 EXPECT_TRUE(controller
->HasActiveAnimation());
1537 Animation
* active_animation
= controller
->GetAnimation(Animation::OPACITY
);
1538 EXPECT_TRUE(active_animation
);
1539 EXPECT_TRUE(active_animation
->needs_synchronized_start_time());
1541 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1542 controller_impl
->ActivateAnimations();
1544 active_animation
= controller_impl
->GetAnimation(Animation::OPACITY
);
1545 EXPECT_TRUE(active_animation
);
1546 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
1547 active_animation
->run_state());
1550 // Tests that skipping a call to UpdateState works as expected.
1551 TEST(LayerAnimationControllerTest
, SkipUpdateState
) {
1552 scoped_ptr
<AnimationEventsVector
> events(
1553 make_scoped_ptr(new AnimationEventsVector
));
1554 FakeLayerAnimationValueObserver dummy
;
1555 scoped_refptr
<LayerAnimationController
> controller(
1556 LayerAnimationController::Create(0));
1557 controller
->AddValueObserver(&dummy
);
1559 scoped_ptr
<Animation
> first_animation(CreateAnimation(
1560 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1)).Pass(), 1,
1561 Animation::TRANSFORM
));
1562 first_animation
->set_is_controlling_instance_for_test(true);
1563 controller
->AddAnimation(first_animation
.Pass());
1565 controller
->Animate(kInitialTickTime
);
1566 controller
->UpdateState(true, events
.get());
1568 scoped_ptr
<Animation
> second_animation(CreateAnimation(
1569 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1570 2, Animation::OPACITY
));
1571 second_animation
->set_is_controlling_instance_for_test(true);
1572 controller
->AddAnimation(second_animation
.Pass());
1574 // Animate but don't UpdateState.
1575 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1577 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1578 events
.reset(new AnimationEventsVector
);
1579 controller
->UpdateState(true, events
.get());
1581 // Should have one STARTED event and one FINISHED event.
1582 EXPECT_EQ(2u, events
->size());
1583 EXPECT_NE((*events
)[0].type
, (*events
)[1].type
);
1585 // The float transition should still be at its starting point.
1586 EXPECT_TRUE(controller
->HasActiveAnimation());
1587 EXPECT_EQ(0.f
, dummy
.opacity());
1589 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(3000));
1590 controller
->UpdateState(true, events
.get());
1592 // The float tranisition should now be done.
1593 EXPECT_EQ(1.f
, dummy
.opacity());
1594 EXPECT_FALSE(controller
->HasActiveAnimation());
1597 // Tests that an animation controller with only a pending observer gets ticked
1598 // but doesn't progress animations past the STARTING state.
1599 TEST(LayerAnimationControllerTest
, InactiveObserverGetsTicked
) {
1600 scoped_ptr
<AnimationEventsVector
> events(
1601 make_scoped_ptr(new AnimationEventsVector
));
1602 FakeLayerAnimationValueObserver dummy
;
1603 FakeInactiveLayerAnimationValueObserver pending_dummy
;
1604 scoped_refptr
<LayerAnimationController
> controller(
1605 LayerAnimationController::Create(0));
1608 controller
->AddAnimation(CreateAnimation(
1609 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.5f
, 1.f
))
1611 id
, Animation::OPACITY
));
1613 // Without an observer, the animation shouldn't progress to the STARTING
1615 controller
->Animate(kInitialTickTime
);
1616 controller
->UpdateState(true, events
.get());
1617 EXPECT_EQ(0u, events
->size());
1618 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
1619 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1621 controller
->AddValueObserver(&pending_dummy
);
1623 // With only a pending observer, the animation should progress to the
1624 // STARTING state and get ticked at its starting point, but should not
1625 // progress to RUNNING.
1626 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1627 controller
->UpdateState(true, events
.get());
1628 EXPECT_EQ(0u, events
->size());
1629 EXPECT_EQ(Animation::STARTING
,
1630 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1631 EXPECT_EQ(0.5f
, pending_dummy
.opacity());
1633 // Even when already in the STARTING state, the animation should stay
1634 // there, and shouldn't be ticked past its starting point.
1635 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
1636 controller
->UpdateState(true, events
.get());
1637 EXPECT_EQ(0u, events
->size());
1638 EXPECT_EQ(Animation::STARTING
,
1639 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1640 EXPECT_EQ(0.5f
, pending_dummy
.opacity());
1642 controller
->AddValueObserver(&dummy
);
1644 // Now that an active observer has been added, the animation should still
1645 // initially tick at its starting point, but should now progress to RUNNING.
1646 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(3000));
1647 controller
->UpdateState(true, events
.get());
1648 EXPECT_EQ(1u, events
->size());
1649 EXPECT_EQ(Animation::RUNNING
,
1650 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1651 EXPECT_EQ(0.5f
, pending_dummy
.opacity());
1652 EXPECT_EQ(0.5f
, dummy
.opacity());
1654 // The animation should now tick past its starting point.
1655 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(3500));
1656 EXPECT_NE(0.5f
, pending_dummy
.opacity());
1657 EXPECT_NE(0.5f
, dummy
.opacity());
1660 TEST(LayerAnimationControllerTest
, TransformAnimationBounds
) {
1661 scoped_refptr
<LayerAnimationController
> controller_impl(
1662 LayerAnimationController::Create(0));
1664 scoped_ptr
<KeyframedTransformAnimationCurve
> curve1(
1665 KeyframedTransformAnimationCurve::Create());
1667 TransformOperations operations1
;
1668 curve1
->AddKeyframe(
1669 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
1670 operations1
.AppendTranslate(10.0, 15.0, 0.0);
1671 curve1
->AddKeyframe(TransformKeyframe::Create(
1672 base::TimeDelta::FromSecondsD(1.0), operations1
, nullptr));
1674 scoped_ptr
<Animation
> animation(
1675 Animation::Create(curve1
.Pass(), 1, 1, Animation::TRANSFORM
));
1676 controller_impl
->AddAnimation(animation
.Pass());
1678 scoped_ptr
<KeyframedTransformAnimationCurve
> curve2(
1679 KeyframedTransformAnimationCurve::Create());
1681 TransformOperations operations2
;
1682 curve2
->AddKeyframe(
1683 TransformKeyframe::Create(base::TimeDelta(), operations2
, nullptr));
1684 operations2
.AppendScale(2.0, 3.0, 4.0);
1685 curve2
->AddKeyframe(TransformKeyframe::Create(
1686 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
1688 animation
= Animation::Create(curve2
.Pass(), 2, 2, Animation::TRANSFORM
);
1689 controller_impl
->AddAnimation(animation
.Pass());
1691 gfx::BoxF
box(1.f
, 2.f
, -1.f
, 3.f
, 4.f
, 5.f
);
1694 EXPECT_TRUE(controller_impl
->TransformAnimationBoundsForBox(box
, &bounds
));
1695 EXPECT_EQ(gfx::BoxF(1.f
, 2.f
, -4.f
, 13.f
, 19.f
, 20.f
).ToString(),
1698 controller_impl
->GetAnimationById(1)
1699 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
1701 // Only the unfinished animation should affect the animated bounds.
1702 EXPECT_TRUE(controller_impl
->TransformAnimationBoundsForBox(box
, &bounds
));
1703 EXPECT_EQ(gfx::BoxF(1.f
, 2.f
, -4.f
, 7.f
, 16.f
, 20.f
).ToString(),
1706 controller_impl
->GetAnimationById(2)
1707 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
1709 // There are no longer any running animations.
1710 EXPECT_FALSE(controller_impl
->HasTransformAnimationThatInflatesBounds());
1712 // Add an animation whose bounds we don't yet support computing.
1713 scoped_ptr
<KeyframedTransformAnimationCurve
> curve3(
1714 KeyframedTransformAnimationCurve::Create());
1715 TransformOperations operations3
;
1716 gfx::Transform transform3
;
1717 transform3
.Scale3d(1.0, 2.0, 3.0);
1718 curve3
->AddKeyframe(
1719 TransformKeyframe::Create(base::TimeDelta(), operations3
, nullptr));
1720 operations3
.AppendMatrix(transform3
);
1721 curve3
->AddKeyframe(TransformKeyframe::Create(
1722 base::TimeDelta::FromSecondsD(1.0), operations3
, nullptr));
1723 animation
= Animation::Create(curve3
.Pass(), 3, 3, Animation::TRANSFORM
);
1724 controller_impl
->AddAnimation(animation
.Pass());
1725 EXPECT_FALSE(controller_impl
->TransformAnimationBoundsForBox(box
, &bounds
));
1728 // Tests that AbortAnimations aborts all animations targeting the specified
1730 TEST(LayerAnimationControllerTest
, AbortAnimations
) {
1731 FakeLayerAnimationValueObserver dummy
;
1732 scoped_refptr
<LayerAnimationController
> controller(
1733 LayerAnimationController::Create(0));
1734 controller
->AddValueObserver(&dummy
);
1736 // Start with several animations, and allow some of them to reach the finished
1738 controller
->AddAnimation(Animation::Create(
1739 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1.0)).Pass(), 1, 1,
1740 Animation::TRANSFORM
));
1741 controller
->AddAnimation(Animation::Create(
1742 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1743 2, 2, Animation::OPACITY
));
1744 controller
->AddAnimation(Animation::Create(
1745 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1.0)).Pass(), 3, 3,
1746 Animation::TRANSFORM
));
1747 controller
->AddAnimation(Animation::Create(
1748 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(2.0)).Pass(), 4, 4,
1749 Animation::TRANSFORM
));
1750 controller
->AddAnimation(Animation::Create(
1751 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1752 5, 5, Animation::OPACITY
));
1754 controller
->Animate(kInitialTickTime
);
1755 controller
->UpdateState(true, nullptr);
1756 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
1757 controller
->UpdateState(true, nullptr);
1759 EXPECT_EQ(Animation::FINISHED
, controller
->GetAnimationById(1)->run_state());
1760 EXPECT_EQ(Animation::FINISHED
, controller
->GetAnimationById(2)->run_state());
1761 EXPECT_EQ(Animation::RUNNING
, controller
->GetAnimationById(3)->run_state());
1762 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
1763 controller
->GetAnimationById(4)->run_state());
1764 EXPECT_EQ(Animation::RUNNING
, controller
->GetAnimationById(5)->run_state());
1766 controller
->AbortAnimations(Animation::TRANSFORM
);
1768 // Only un-finished TRANSFORM animations should have been aborted.
1769 EXPECT_EQ(Animation::FINISHED
, controller
->GetAnimationById(1)->run_state());
1770 EXPECT_EQ(Animation::FINISHED
, controller
->GetAnimationById(2)->run_state());
1771 EXPECT_EQ(Animation::ABORTED
, controller
->GetAnimationById(3)->run_state());
1772 EXPECT_EQ(Animation::ABORTED
, controller
->GetAnimationById(4)->run_state());
1773 EXPECT_EQ(Animation::RUNNING
, controller
->GetAnimationById(5)->run_state());
1776 // An animation aborted on the main thread should get deleted on both threads.
1777 TEST(LayerAnimationControllerTest
, MainThreadAbortedAnimationGetsDeleted
) {
1778 FakeLayerAnimationValueObserver dummy_impl
;
1779 scoped_refptr
<LayerAnimationController
> controller_impl(
1780 LayerAnimationController::Create(0));
1781 controller_impl
->AddValueObserver(&dummy_impl
);
1782 FakeLayerAnimationValueObserver dummy
;
1783 scoped_refptr
<LayerAnimationController
> controller(
1784 LayerAnimationController::Create(0));
1785 controller
->AddValueObserver(&dummy
);
1788 AddOpacityTransitionToController(controller
.get(), 1.0, 0.f
, 1.f
, false);
1790 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1791 controller_impl
->ActivateAnimations();
1792 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
1794 controller
->AbortAnimations(Animation::OPACITY
);
1795 EXPECT_EQ(Animation::ABORTED
,
1796 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1797 EXPECT_FALSE(dummy
.animation_waiting_for_deletion());
1798 EXPECT_FALSE(dummy_impl
.animation_waiting_for_deletion());
1800 controller
->Animate(kInitialTickTime
);
1801 controller
->UpdateState(true, nullptr);
1802 EXPECT_TRUE(dummy
.animation_waiting_for_deletion());
1803 EXPECT_EQ(Animation::WAITING_FOR_DELETION
,
1804 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1806 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1807 controller_impl
->ActivateAnimations();
1808 EXPECT_FALSE(controller
->GetAnimationById(animation_id
));
1809 EXPECT_FALSE(controller_impl
->GetAnimationById(animation_id
));
1812 // An animation aborted on the impl thread should get deleted on both threads.
1813 TEST(LayerAnimationControllerTest
, ImplThreadAbortedAnimationGetsDeleted
) {
1814 FakeLayerAnimationValueObserver dummy_impl
;
1815 scoped_refptr
<LayerAnimationController
> controller_impl(
1816 LayerAnimationController::Create(0));
1817 controller_impl
->AddValueObserver(&dummy_impl
);
1818 FakeLayerAnimationValueObserver dummy
;
1819 scoped_refptr
<LayerAnimationController
> controller(
1820 LayerAnimationController::Create(0));
1821 controller
->AddValueObserver(&dummy
);
1824 AddOpacityTransitionToController(controller
.get(), 1.0, 0.f
, 1.f
, false);
1826 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1827 controller_impl
->ActivateAnimations();
1828 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
1830 controller_impl
->AbortAnimations(Animation::OPACITY
);
1831 EXPECT_EQ(Animation::ABORTED
,
1832 controller_impl
->GetAnimation(Animation::OPACITY
)->run_state());
1833 EXPECT_FALSE(dummy
.animation_waiting_for_deletion());
1834 EXPECT_FALSE(dummy_impl
.animation_waiting_for_deletion());
1836 AnimationEventsVector events
;
1837 controller_impl
->Animate(kInitialTickTime
);
1838 controller_impl
->UpdateState(true, &events
);
1839 EXPECT_TRUE(dummy_impl
.animation_waiting_for_deletion());
1840 EXPECT_EQ(1u, events
.size());
1841 EXPECT_EQ(AnimationEvent::ABORTED
, events
[0].type
);
1842 EXPECT_EQ(Animation::WAITING_FOR_DELETION
,
1843 controller_impl
->GetAnimation(Animation::OPACITY
)->run_state());
1845 controller
->NotifyAnimationAborted(events
[0]);
1846 EXPECT_EQ(Animation::ABORTED
,
1847 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1849 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
1850 controller
->UpdateState(true, nullptr);
1851 EXPECT_TRUE(dummy
.animation_waiting_for_deletion());
1852 EXPECT_EQ(Animation::WAITING_FOR_DELETION
,
1853 controller
->GetAnimation(Animation::OPACITY
)->run_state());
1855 controller
->PushAnimationUpdatesTo(controller_impl
.get());
1856 controller_impl
->ActivateAnimations();
1857 EXPECT_FALSE(controller
->GetAnimationById(animation_id
));
1858 EXPECT_FALSE(controller_impl
->GetAnimationById(animation_id
));
1861 // Ensure that we only generate FINISHED events for animations in a group
1862 // once all animations in that group are finished.
1863 TEST(LayerAnimationControllerTest
, FinishedEventsForGroup
) {
1864 scoped_ptr
<AnimationEventsVector
> events(
1865 make_scoped_ptr(new AnimationEventsVector
));
1866 FakeLayerAnimationValueObserver dummy_impl
;
1867 scoped_refptr
<LayerAnimationController
> controller_impl(
1868 LayerAnimationController::Create(0));
1869 controller_impl
->AddValueObserver(&dummy_impl
);
1871 const int group_id
= 1;
1873 // Add two animations with the same group id but different durations.
1874 scoped_ptr
<Animation
> first_animation(Animation::Create(
1875 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(2.0)).Pass(), 1,
1876 group_id
, Animation::TRANSFORM
));
1877 first_animation
->set_is_controlling_instance_for_test(true);
1878 controller_impl
->AddAnimation(first_animation
.Pass());
1880 scoped_ptr
<Animation
> second_animation(Animation::Create(
1881 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1882 2, group_id
, Animation::OPACITY
));
1883 second_animation
->set_is_controlling_instance_for_test(true);
1884 controller_impl
->AddAnimation(second_animation
.Pass());
1886 controller_impl
->Animate(kInitialTickTime
);
1887 controller_impl
->UpdateState(true, events
.get());
1889 // Both animations should have started.
1890 EXPECT_EQ(2u, events
->size());
1891 EXPECT_EQ(AnimationEvent::STARTED
, (*events
)[0].type
);
1892 EXPECT_EQ(AnimationEvent::STARTED
, (*events
)[1].type
);
1894 events
.reset(new AnimationEventsVector
);
1895 controller_impl
->Animate(kInitialTickTime
+
1896 TimeDelta::FromMilliseconds(1000));
1897 controller_impl
->UpdateState(true, events
.get());
1899 // The opacity animation should be finished, but should not have generated
1900 // a FINISHED event yet.
1901 EXPECT_EQ(0u, events
->size());
1902 EXPECT_EQ(Animation::FINISHED
,
1903 controller_impl
->GetAnimationById(2)->run_state());
1904 EXPECT_EQ(Animation::RUNNING
,
1905 controller_impl
->GetAnimationById(1)->run_state());
1907 controller_impl
->Animate(kInitialTickTime
+
1908 TimeDelta::FromMilliseconds(2000));
1909 controller_impl
->UpdateState(true, events
.get());
1911 // Both animations should have generated FINISHED events.
1912 EXPECT_EQ(2u, events
->size());
1913 EXPECT_EQ(AnimationEvent::FINISHED
, (*events
)[0].type
);
1914 EXPECT_EQ(AnimationEvent::FINISHED
, (*events
)[1].type
);
1917 // Ensure that when a group has a mix of aborted and finished animations,
1918 // we generate a FINISHED event for the finished animation and an ABORTED
1919 // event for the aborted animation.
1920 TEST(LayerAnimationControllerTest
, FinishedAndAbortedEventsForGroup
) {
1921 scoped_ptr
<AnimationEventsVector
> events(
1922 make_scoped_ptr(new AnimationEventsVector
));
1923 FakeLayerAnimationValueObserver dummy_impl
;
1924 scoped_refptr
<LayerAnimationController
> controller_impl(
1925 LayerAnimationController::Create(0));
1926 controller_impl
->AddValueObserver(&dummy_impl
);
1928 // Add two animations with the same group id.
1929 scoped_ptr
<Animation
> first_animation(CreateAnimation(
1930 scoped_ptr
<AnimationCurve
>(new FakeTransformTransition(1.0)).Pass(), 1,
1931 Animation::TRANSFORM
));
1932 first_animation
->set_is_controlling_instance_for_test(true);
1933 controller_impl
->AddAnimation(first_animation
.Pass());
1935 scoped_ptr
<Animation
> second_animation(CreateAnimation(
1936 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1937 1, Animation::OPACITY
));
1938 second_animation
->set_is_controlling_instance_for_test(true);
1939 controller_impl
->AddAnimation(second_animation
.Pass());
1941 controller_impl
->Animate(kInitialTickTime
);
1942 controller_impl
->UpdateState(true, events
.get());
1944 // Both animations should have started.
1945 EXPECT_EQ(2u, events
->size());
1946 EXPECT_EQ(AnimationEvent::STARTED
, (*events
)[0].type
);
1947 EXPECT_EQ(AnimationEvent::STARTED
, (*events
)[1].type
);
1949 controller_impl
->AbortAnimations(Animation::OPACITY
);
1951 events
.reset(new AnimationEventsVector
);
1952 controller_impl
->Animate(kInitialTickTime
+
1953 TimeDelta::FromMilliseconds(1000));
1954 controller_impl
->UpdateState(true, events
.get());
1956 // We should have exactly 2 events: a FINISHED event for the tranform
1957 // animation, and an ABORTED event for the opacity animation.
1958 EXPECT_EQ(2u, events
->size());
1959 EXPECT_EQ(AnimationEvent::FINISHED
, (*events
)[0].type
);
1960 EXPECT_EQ(Animation::TRANSFORM
, (*events
)[0].target_property
);
1961 EXPECT_EQ(AnimationEvent::ABORTED
, (*events
)[1].type
);
1962 EXPECT_EQ(Animation::OPACITY
, (*events
)[1].target_property
);
1965 TEST(LayerAnimationControllerTest
, HasAnimationThatAffectsScale
) {
1966 scoped_refptr
<LayerAnimationController
> controller_impl(
1967 LayerAnimationController::Create(0));
1969 EXPECT_FALSE(controller_impl
->HasAnimationThatAffectsScale());
1971 controller_impl
->AddAnimation(CreateAnimation(
1972 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
1973 1, Animation::OPACITY
));
1975 // Opacity animations don't affect scale.
1976 EXPECT_FALSE(controller_impl
->HasAnimationThatAffectsScale());
1978 scoped_ptr
<KeyframedTransformAnimationCurve
> curve1(
1979 KeyframedTransformAnimationCurve::Create());
1981 TransformOperations operations1
;
1982 curve1
->AddKeyframe(
1983 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
1984 operations1
.AppendTranslate(10.0, 15.0, 0.0);
1985 curve1
->AddKeyframe(TransformKeyframe::Create(
1986 base::TimeDelta::FromSecondsD(1.0), operations1
, nullptr));
1988 scoped_ptr
<Animation
> animation(
1989 Animation::Create(curve1
.Pass(), 2, 2, Animation::TRANSFORM
));
1990 controller_impl
->AddAnimation(animation
.Pass());
1992 // Translations don't affect scale.
1993 EXPECT_FALSE(controller_impl
->HasAnimationThatAffectsScale());
1995 scoped_ptr
<KeyframedTransformAnimationCurve
> curve2(
1996 KeyframedTransformAnimationCurve::Create());
1998 TransformOperations operations2
;
1999 curve2
->AddKeyframe(
2000 TransformKeyframe::Create(base::TimeDelta(), operations2
, nullptr));
2001 operations2
.AppendScale(2.0, 3.0, 4.0);
2002 curve2
->AddKeyframe(TransformKeyframe::Create(
2003 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
2005 animation
= Animation::Create(curve2
.Pass(), 3, 3, Animation::TRANSFORM
);
2006 controller_impl
->AddAnimation(animation
.Pass());
2008 EXPECT_TRUE(controller_impl
->HasAnimationThatAffectsScale());
2010 controller_impl
->GetAnimationById(3)
2011 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
2013 // Only unfinished animations should be considered by
2014 // HasAnimationThatAffectsScale.
2015 EXPECT_FALSE(controller_impl
->HasAnimationThatAffectsScale());
2018 TEST(LayerAnimationControllerTest
, HasOnlyTranslationTransforms
) {
2019 scoped_refptr
<LayerAnimationController
> controller_impl(
2020 LayerAnimationController::Create(0));
2022 EXPECT_TRUE(controller_impl
->HasOnlyTranslationTransforms());
2024 controller_impl
->AddAnimation(CreateAnimation(
2025 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
2026 1, Animation::OPACITY
));
2028 // Opacity animations aren't non-translation transforms.
2029 EXPECT_TRUE(controller_impl
->HasOnlyTranslationTransforms());
2031 scoped_ptr
<KeyframedTransformAnimationCurve
> curve1(
2032 KeyframedTransformAnimationCurve::Create());
2034 TransformOperations operations1
;
2035 curve1
->AddKeyframe(
2036 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
2037 operations1
.AppendTranslate(10.0, 15.0, 0.0);
2038 curve1
->AddKeyframe(TransformKeyframe::Create(
2039 base::TimeDelta::FromSecondsD(1.0), operations1
, nullptr));
2041 scoped_ptr
<Animation
> animation(
2042 Animation::Create(curve1
.Pass(), 2, 2, Animation::TRANSFORM
));
2043 controller_impl
->AddAnimation(animation
.Pass());
2045 // The only transform animation we've added is a translation.
2046 EXPECT_TRUE(controller_impl
->HasOnlyTranslationTransforms());
2048 scoped_ptr
<KeyframedTransformAnimationCurve
> curve2(
2049 KeyframedTransformAnimationCurve::Create());
2051 TransformOperations operations2
;
2052 curve2
->AddKeyframe(
2053 TransformKeyframe::Create(base::TimeDelta(), operations2
, nullptr));
2054 operations2
.AppendScale(2.0, 3.0, 4.0);
2055 curve2
->AddKeyframe(TransformKeyframe::Create(
2056 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
2058 animation
= Animation::Create(curve2
.Pass(), 3, 3, Animation::TRANSFORM
);
2059 controller_impl
->AddAnimation(animation
.Pass());
2061 // A scale animation is not a translation.
2062 EXPECT_FALSE(controller_impl
->HasOnlyTranslationTransforms());
2064 controller_impl
->GetAnimationById(3)
2065 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
2067 // Only unfinished animations should be considered by
2068 // HasOnlyTranslationTransforms.
2069 EXPECT_TRUE(controller_impl
->HasOnlyTranslationTransforms());
2072 TEST(LayerAnimationControllerTest
, AnimationStartScale
) {
2073 scoped_refptr
<LayerAnimationController
> controller_impl(
2074 LayerAnimationController::Create(0));
2075 scoped_ptr
<KeyframedTransformAnimationCurve
> curve1(
2076 KeyframedTransformAnimationCurve::Create());
2078 TransformOperations operations1
;
2079 operations1
.AppendScale(2.0, 3.0, 4.0);
2080 curve1
->AddKeyframe(
2081 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
2082 TransformOperations operations2
;
2083 curve1
->AddKeyframe(TransformKeyframe::Create(
2084 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
2085 scoped_ptr
<Animation
> animation(
2086 Animation::Create(curve1
.Pass(), 1, 1, Animation::TRANSFORM
));
2087 controller_impl
->AddAnimation(animation
.Pass());
2089 float start_scale
= 0.f
;
2090 EXPECT_TRUE(controller_impl
->AnimationStartScale(&start_scale
));
2091 EXPECT_EQ(4.f
, start_scale
);
2093 scoped_ptr
<KeyframedTransformAnimationCurve
> curve2(
2094 KeyframedTransformAnimationCurve::Create());
2096 TransformOperations operations3
;
2097 curve2
->AddKeyframe(
2098 TransformKeyframe::Create(base::TimeDelta(), operations3
, nullptr));
2099 operations3
.AppendScale(6.0, 5.0, 4.0);
2100 curve2
->AddKeyframe(TransformKeyframe::Create(
2101 base::TimeDelta::FromSecondsD(1.0), operations3
, nullptr));
2103 controller_impl
->RemoveAnimation(1);
2104 animation
= Animation::Create(curve2
.Pass(), 2, 2, Animation::TRANSFORM
);
2106 // Reverse Direction
2107 animation
->set_direction(Animation::DIRECTION_REVERSE
);
2108 controller_impl
->AddAnimation(animation
.Pass());
2110 scoped_ptr
<KeyframedTransformAnimationCurve
> curve3(
2111 KeyframedTransformAnimationCurve::Create());
2113 TransformOperations operations4
;
2114 operations4
.AppendScale(5.0, 3.0, 1.0);
2115 curve3
->AddKeyframe(
2116 TransformKeyframe::Create(base::TimeDelta(), operations4
, nullptr));
2117 TransformOperations operations5
;
2118 curve3
->AddKeyframe(TransformKeyframe::Create(
2119 base::TimeDelta::FromSecondsD(1.0), operations5
, nullptr));
2121 animation
= Animation::Create(curve3
.Pass(), 3, 3, Animation::TRANSFORM
);
2122 controller_impl
->AddAnimation(animation
.Pass());
2124 EXPECT_TRUE(controller_impl
->AnimationStartScale(&start_scale
));
2125 EXPECT_EQ(6.f
, start_scale
);
2127 controller_impl
->GetAnimationById(2)
2128 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
2130 // Only unfinished animations should be considered by
2131 // AnimationStartScale.
2132 EXPECT_TRUE(controller_impl
->AnimationStartScale(&start_scale
));
2133 EXPECT_EQ(5.f
, start_scale
);
2136 TEST(LayerAnimationControllerTest
, MaximumTargetScale
) {
2137 scoped_refptr
<LayerAnimationController
> controller_impl(
2138 LayerAnimationController::Create(0));
2140 float max_scale
= 0.f
;
2141 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2142 EXPECT_EQ(0.f
, max_scale
);
2144 scoped_ptr
<KeyframedTransformAnimationCurve
> curve1(
2145 KeyframedTransformAnimationCurve::Create());
2147 TransformOperations operations1
;
2148 curve1
->AddKeyframe(
2149 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
2150 operations1
.AppendScale(2.0, 3.0, 4.0);
2151 curve1
->AddKeyframe(TransformKeyframe::Create(
2152 base::TimeDelta::FromSecondsD(1.0), operations1
, nullptr));
2154 scoped_ptr
<Animation
> animation(
2155 Animation::Create(curve1
.Pass(), 1, 1, Animation::TRANSFORM
));
2156 controller_impl
->AddAnimation(animation
.Pass());
2158 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2159 EXPECT_EQ(4.f
, max_scale
);
2161 scoped_ptr
<KeyframedTransformAnimationCurve
> curve2(
2162 KeyframedTransformAnimationCurve::Create());
2164 TransformOperations operations2
;
2165 curve2
->AddKeyframe(
2166 TransformKeyframe::Create(base::TimeDelta(), operations2
, nullptr));
2167 operations2
.AppendScale(6.0, 5.0, 4.0);
2168 curve2
->AddKeyframe(TransformKeyframe::Create(
2169 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
2171 animation
= Animation::Create(curve2
.Pass(), 2, 2, Animation::TRANSFORM
);
2172 controller_impl
->AddAnimation(animation
.Pass());
2174 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2175 EXPECT_EQ(6.f
, max_scale
);
2177 scoped_ptr
<KeyframedTransformAnimationCurve
> curve3(
2178 KeyframedTransformAnimationCurve::Create());
2180 TransformOperations operations3
;
2181 curve3
->AddKeyframe(
2182 TransformKeyframe::Create(base::TimeDelta(), operations3
, nullptr));
2183 operations3
.AppendPerspective(6.0);
2184 curve3
->AddKeyframe(TransformKeyframe::Create(
2185 base::TimeDelta::FromSecondsD(1.0), operations3
, nullptr));
2187 animation
= Animation::Create(curve3
.Pass(), 3, 3, Animation::TRANSFORM
);
2188 controller_impl
->AddAnimation(animation
.Pass());
2190 EXPECT_FALSE(controller_impl
->MaximumTargetScale(&max_scale
));
2192 controller_impl
->GetAnimationById(3)
2193 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
2194 controller_impl
->GetAnimationById(2)
2195 ->SetRunState(Animation::FINISHED
, TicksFromSecondsF(0.0));
2197 // Only unfinished animations should be considered by
2198 // MaximumTargetScale.
2199 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2200 EXPECT_EQ(4.f
, max_scale
);
2203 TEST(LayerAnimationControllerTest
, MaximumTargetScaleWithDirection
) {
2204 scoped_refptr
<LayerAnimationController
> controller_impl(
2205 LayerAnimationController::Create(0));
2207 scoped_ptr
<KeyframedTransformAnimationCurve
> curve1(
2208 KeyframedTransformAnimationCurve::Create());
2209 TransformOperations operations1
;
2210 operations1
.AppendScale(1.0, 2.0, 3.0);
2211 curve1
->AddKeyframe(
2212 TransformKeyframe::Create(base::TimeDelta(), operations1
, nullptr));
2213 TransformOperations operations2
;
2214 operations2
.AppendScale(4.0, 5.0, 6.0);
2215 curve1
->AddKeyframe(TransformKeyframe::Create(
2216 base::TimeDelta::FromSecondsD(1.0), operations2
, nullptr));
2218 scoped_ptr
<Animation
> animation_owned(
2219 Animation::Create(curve1
.Pass(), 1, 1, Animation::TRANSFORM
));
2220 Animation
* animation
= animation_owned
.get();
2221 controller_impl
->AddAnimation(animation_owned
.Pass());
2223 float max_scale
= 0.f
;
2225 EXPECT_GT(animation
->playback_rate(), 0.0);
2227 // NORMAL direction with positive playback rate.
2228 animation
->set_direction(Animation::DIRECTION_NORMAL
);
2229 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2230 EXPECT_EQ(6.f
, max_scale
);
2232 // ALTERNATE direction with positive playback rate.
2233 animation
->set_direction(Animation::DIRECTION_ALTERNATE
);
2234 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2235 EXPECT_EQ(6.f
, max_scale
);
2237 // REVERSE direction with positive playback rate.
2238 animation
->set_direction(Animation::DIRECTION_REVERSE
);
2239 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2240 EXPECT_EQ(3.f
, max_scale
);
2242 // ALTERNATE reverse direction.
2243 animation
->set_direction(Animation::DIRECTION_REVERSE
);
2244 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2245 EXPECT_EQ(3.f
, max_scale
);
2247 animation
->set_playback_rate(-1.0);
2249 // NORMAL direction with negative playback rate.
2250 animation
->set_direction(Animation::DIRECTION_NORMAL
);
2251 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2252 EXPECT_EQ(3.f
, max_scale
);
2254 // ALTERNATE direction with negative playback rate.
2255 animation
->set_direction(Animation::DIRECTION_ALTERNATE
);
2256 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2257 EXPECT_EQ(3.f
, max_scale
);
2259 // REVERSE direction with negative playback rate.
2260 animation
->set_direction(Animation::DIRECTION_REVERSE
);
2261 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2262 EXPECT_EQ(6.f
, max_scale
);
2264 // ALTERNATE reverse direction with negative playback rate.
2265 animation
->set_direction(Animation::DIRECTION_REVERSE
);
2266 EXPECT_TRUE(controller_impl
->MaximumTargetScale(&max_scale
));
2267 EXPECT_EQ(6.f
, max_scale
);
2270 TEST(LayerAnimationControllerTest
, NewlyPushedAnimationWaitsForActivation
) {
2271 scoped_ptr
<AnimationEventsVector
> events(
2272 make_scoped_ptr(new AnimationEventsVector
));
2273 FakeLayerAnimationValueObserver dummy_impl
;
2274 FakeInactiveLayerAnimationValueObserver pending_dummy_impl
;
2275 scoped_refptr
<LayerAnimationController
> controller_impl(
2276 LayerAnimationController::Create(0));
2277 controller_impl
->AddValueObserver(&dummy_impl
);
2278 controller_impl
->AddValueObserver(&pending_dummy_impl
);
2279 FakeLayerAnimationValueObserver dummy
;
2280 scoped_refptr
<LayerAnimationController
> controller(
2281 LayerAnimationController::Create(0));
2282 controller
->AddValueObserver(&dummy
);
2284 EXPECT_FALSE(controller
->needs_to_start_animations_for_testing());
2286 AddOpacityTransitionToController(controller
.get(), 1, 0.5f
, 1.f
, false);
2287 EXPECT_TRUE(controller
->needs_to_start_animations_for_testing());
2289 EXPECT_FALSE(controller_impl
->needs_to_start_animations_for_testing());
2290 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2291 EXPECT_TRUE(controller_impl
->needs_to_start_animations_for_testing());
2293 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
2294 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
2295 controller_impl
->GetAnimationById(animation_id
)->run_state());
2296 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2297 ->affects_pending_observers());
2298 EXPECT_FALSE(controller_impl
->GetAnimationById(animation_id
)
2299 ->affects_active_observers());
2301 controller_impl
->Animate(kInitialTickTime
);
2302 EXPECT_FALSE(controller_impl
->needs_to_start_animations_for_testing());
2303 controller_impl
->UpdateState(true, events
.get());
2305 // Since the animation hasn't been activated, it should still be STARTING
2306 // rather than RUNNING.
2307 EXPECT_EQ(Animation::STARTING
,
2308 controller_impl
->GetAnimationById(animation_id
)->run_state());
2310 // Since the animation hasn't been activated, only the pending observer
2311 // should have been ticked.
2312 EXPECT_EQ(0.5f
, pending_dummy_impl
.opacity());
2313 EXPECT_EQ(0.f
, dummy_impl
.opacity());
2315 controller_impl
->ActivateAnimations();
2316 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2317 ->affects_pending_observers());
2318 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2319 ->affects_active_observers());
2321 controller_impl
->Animate(kInitialTickTime
+
2322 TimeDelta::FromMilliseconds(1000));
2323 controller_impl
->UpdateState(true, events
.get());
2325 // Since the animation has been activated, it should have reached the
2326 // RUNNING state and the active observer should start to get ticked.
2327 EXPECT_EQ(Animation::RUNNING
,
2328 controller_impl
->GetAnimationById(animation_id
)->run_state());
2329 EXPECT_EQ(0.5f
, pending_dummy_impl
.opacity());
2330 EXPECT_EQ(0.5f
, dummy_impl
.opacity());
2333 TEST(LayerAnimationControllerTest
, ActivationBetweenAnimateAndUpdateState
) {
2334 scoped_ptr
<AnimationEventsVector
> events(
2335 make_scoped_ptr(new AnimationEventsVector
));
2336 FakeLayerAnimationValueObserver dummy_impl
;
2337 FakeInactiveLayerAnimationValueObserver pending_dummy_impl
;
2338 scoped_refptr
<LayerAnimationController
> controller_impl(
2339 LayerAnimationController::Create(0));
2340 controller_impl
->AddValueObserver(&dummy_impl
);
2341 controller_impl
->AddValueObserver(&pending_dummy_impl
);
2342 FakeLayerAnimationValueObserver dummy
;
2343 scoped_refptr
<LayerAnimationController
> controller(
2344 LayerAnimationController::Create(0));
2345 controller
->AddValueObserver(&dummy
);
2348 AddOpacityTransitionToController(controller
.get(), 1, 0.5f
, 1.f
, true);
2350 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2352 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
));
2353 EXPECT_EQ(Animation::WAITING_FOR_TARGET_AVAILABILITY
,
2354 controller_impl
->GetAnimationById(animation_id
)->run_state());
2355 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2356 ->affects_pending_observers());
2357 EXPECT_FALSE(controller_impl
->GetAnimationById(animation_id
)
2358 ->affects_active_observers());
2360 controller_impl
->Animate(kInitialTickTime
);
2362 // Since the animation hasn't been activated, only the pending observer
2363 // should have been ticked.
2364 EXPECT_EQ(0.5f
, pending_dummy_impl
.opacity());
2365 EXPECT_EQ(0.f
, dummy_impl
.opacity());
2367 controller_impl
->ActivateAnimations();
2368 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2369 ->affects_pending_observers());
2370 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2371 ->affects_active_observers());
2373 controller_impl
->UpdateState(true, events
.get());
2375 // Since the animation has been activated, it should have reached the
2377 EXPECT_EQ(Animation::RUNNING
,
2378 controller_impl
->GetAnimationById(animation_id
)->run_state());
2380 controller_impl
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
2382 // Both observers should have been ticked.
2383 EXPECT_EQ(0.75f
, pending_dummy_impl
.opacity());
2384 EXPECT_EQ(0.75f
, dummy_impl
.opacity());
2387 TEST(LayerAnimationControllerTest
,
2388 ObserverNotifiedWhenTransformIsPotentiallyAnimatingChanges
) {
2389 AnimationEventsVector events
;
2390 FakeLayerAnimationValueObserver active_dummy_impl
;
2391 FakeInactiveLayerAnimationValueObserver pending_dummy_impl
;
2392 scoped_refptr
<LayerAnimationController
> controller_impl(
2393 LayerAnimationController::Create(0));
2394 controller_impl
->AddValueObserver(&active_dummy_impl
);
2395 controller_impl
->AddValueObserver(&pending_dummy_impl
);
2396 FakeLayerAnimationValueObserver dummy
;
2397 scoped_refptr
<LayerAnimationController
> controller(
2398 LayerAnimationController::Create(0));
2399 controller
->AddValueObserver(&dummy
);
2401 EXPECT_FALSE(dummy
.transform_is_animating());
2402 EXPECT_FALSE(pending_dummy_impl
.transform_is_animating());
2403 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2405 // Case 1: An animation that's allowed to run until its finish point.
2406 AddAnimatedTransformToController(controller
.get(), 1.0, 1, 1);
2407 EXPECT_TRUE(dummy
.transform_is_animating());
2409 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2410 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2411 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2413 controller_impl
->ActivateAnimations();
2414 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2415 EXPECT_TRUE(active_dummy_impl
.transform_is_animating());
2417 controller_impl
->Animate(kInitialTickTime
);
2418 controller_impl
->UpdateState(true, &events
);
2420 controller
->NotifyAnimationStarted(events
[0]);
2423 // Finish the animation.
2424 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
2425 controller
->UpdateState(true, nullptr);
2426 EXPECT_FALSE(dummy
.transform_is_animating());
2428 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2430 // controller_impl hasn't yet ticked at/past the end of the animation.
2431 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2432 EXPECT_TRUE(active_dummy_impl
.transform_is_animating());
2434 controller_impl
->Animate(kInitialTickTime
+
2435 TimeDelta::FromMilliseconds(1000));
2436 controller_impl
->UpdateState(true, &events
);
2437 EXPECT_FALSE(pending_dummy_impl
.transform_is_animating());
2438 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2440 controller
->NotifyAnimationFinished(events
[0]);
2443 // Case 2: An animation that's removed before it finishes.
2445 AddAnimatedTransformToController(controller
.get(), 10.0, 2, 2);
2446 EXPECT_TRUE(dummy
.transform_is_animating());
2448 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2449 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2450 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2452 controller_impl
->ActivateAnimations();
2453 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2454 EXPECT_TRUE(active_dummy_impl
.transform_is_animating());
2456 controller_impl
->Animate(kInitialTickTime
+
2457 TimeDelta::FromMilliseconds(2000));
2458 controller_impl
->UpdateState(true, &events
);
2460 controller
->NotifyAnimationStarted(events
[0]);
2463 controller
->RemoveAnimation(animation_id
);
2464 EXPECT_FALSE(dummy
.transform_is_animating());
2466 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2467 EXPECT_FALSE(pending_dummy_impl
.transform_is_animating());
2468 EXPECT_TRUE(active_dummy_impl
.transform_is_animating());
2470 controller_impl
->ActivateAnimations();
2471 EXPECT_FALSE(pending_dummy_impl
.transform_is_animating());
2472 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2474 // Case 3: An animation that's aborted before it finishes.
2475 animation_id
= AddAnimatedTransformToController(controller
.get(), 10.0, 3, 3);
2476 EXPECT_TRUE(dummy
.transform_is_animating());
2478 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2479 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2480 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2482 controller_impl
->ActivateAnimations();
2483 EXPECT_TRUE(pending_dummy_impl
.transform_is_animating());
2484 EXPECT_TRUE(active_dummy_impl
.transform_is_animating());
2486 controller_impl
->Animate(kInitialTickTime
+
2487 TimeDelta::FromMilliseconds(3000));
2488 controller_impl
->UpdateState(true, &events
);
2490 controller
->NotifyAnimationStarted(events
[0]);
2493 controller_impl
->AbortAnimations(Animation::TRANSFORM
);
2494 EXPECT_FALSE(pending_dummy_impl
.transform_is_animating());
2495 EXPECT_FALSE(active_dummy_impl
.transform_is_animating());
2497 controller_impl
->Animate(kInitialTickTime
+
2498 TimeDelta::FromMilliseconds(4000));
2499 controller_impl
->UpdateState(true, &events
);
2501 controller
->NotifyAnimationAborted(events
[0]);
2502 EXPECT_FALSE(dummy
.transform_is_animating());
2505 TEST(LayerAnimationControllerTest
, ClippedOpacityValues
) {
2506 FakeLayerAnimationValueObserver dummy
;
2507 scoped_refptr
<LayerAnimationController
> controller(
2508 LayerAnimationController::Create(0));
2509 controller
->AddValueObserver(&dummy
);
2511 AddOpacityTransitionToController(controller
.get(), 1, 1.f
, 2.f
, true);
2513 controller
->Animate(kInitialTickTime
);
2514 EXPECT_EQ(1.f
, dummy
.opacity());
2516 // Opacity values are clipped [0,1]
2517 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
2518 EXPECT_EQ(1.f
, dummy
.opacity());
2521 TEST(LayerAnimationControllerTest
, ClippedNegativeOpacityValues
) {
2522 FakeLayerAnimationValueObserver dummy
;
2523 scoped_refptr
<LayerAnimationController
> controller(
2524 LayerAnimationController::Create(0));
2525 controller
->AddValueObserver(&dummy
);
2527 AddOpacityTransitionToController(controller
.get(), 1, 0.f
, -2.f
, true);
2529 controller
->Animate(kInitialTickTime
);
2530 EXPECT_EQ(0.f
, dummy
.opacity());
2532 // Opacity values are clipped [0,1]
2533 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1000));
2534 EXPECT_EQ(0.f
, dummy
.opacity());
2537 TEST(LayerAnimationControllerTest
, PushedDeletedAnimationWaitsForActivation
) {
2538 scoped_ptr
<AnimationEventsVector
> events(
2539 make_scoped_ptr(new AnimationEventsVector
));
2540 FakeLayerAnimationValueObserver dummy_impl
;
2541 FakeInactiveLayerAnimationValueObserver pending_dummy_impl
;
2542 scoped_refptr
<LayerAnimationController
> controller_impl(
2543 LayerAnimationController::Create(0));
2544 controller_impl
->AddValueObserver(&dummy_impl
);
2545 controller_impl
->AddValueObserver(&pending_dummy_impl
);
2546 FakeLayerAnimationValueObserver dummy
;
2547 scoped_refptr
<LayerAnimationController
> controller(
2548 LayerAnimationController::Create(0));
2549 controller
->AddValueObserver(&dummy
);
2552 AddOpacityTransitionToController(controller
.get(), 1, 0.5f
, 1.f
, true);
2554 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2555 controller_impl
->ActivateAnimations();
2556 controller_impl
->Animate(kInitialTickTime
);
2557 controller_impl
->UpdateState(true, events
.get());
2558 EXPECT_EQ(Animation::RUNNING
,
2559 controller_impl
->GetAnimationById(animation_id
)->run_state());
2560 EXPECT_EQ(0.5f
, pending_dummy_impl
.opacity());
2561 EXPECT_EQ(0.5f
, dummy_impl
.opacity());
2563 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2564 ->affects_pending_observers());
2565 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2566 ->affects_active_observers());
2568 // Delete the animation on the main-thread controller.
2569 controller
->RemoveAnimation(
2570 controller
->GetAnimation(Animation::OPACITY
)->id());
2571 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2573 // The animation should no longer affect pending observers.
2574 EXPECT_FALSE(controller_impl
->GetAnimationById(animation_id
)
2575 ->affects_pending_observers());
2576 EXPECT_TRUE(controller_impl
->GetAnimationById(animation_id
)
2577 ->affects_active_observers());
2579 controller_impl
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
2580 controller_impl
->UpdateState(true, events
.get());
2582 // Only the active observer should have been ticked.
2583 EXPECT_EQ(0.5f
, pending_dummy_impl
.opacity());
2584 EXPECT_EQ(0.75f
, dummy_impl
.opacity());
2586 controller_impl
->ActivateAnimations();
2588 // Activation should cause the animation to be deleted.
2589 EXPECT_FALSE(controller_impl
->has_any_animation());
2592 // Tests that an animation that affects only active observers won't block
2593 // an animation that affects only pending observers from starting.
2594 TEST(LayerAnimationControllerTest
, StartAnimationsAffectingDifferentObservers
) {
2595 scoped_ptr
<AnimationEventsVector
> events(
2596 make_scoped_ptr(new AnimationEventsVector
));
2597 FakeLayerAnimationValueObserver dummy_impl
;
2598 FakeInactiveLayerAnimationValueObserver pending_dummy_impl
;
2599 scoped_refptr
<LayerAnimationController
> controller_impl(
2600 LayerAnimationController::Create(0));
2601 controller_impl
->AddValueObserver(&dummy_impl
);
2602 controller_impl
->AddValueObserver(&pending_dummy_impl
);
2603 FakeLayerAnimationValueObserver dummy
;
2604 scoped_refptr
<LayerAnimationController
> controller(
2605 LayerAnimationController::Create(0));
2606 controller
->AddValueObserver(&dummy
);
2608 int first_animation_id
=
2609 AddOpacityTransitionToController(controller
.get(), 1, 0.f
, 1.f
, true);
2611 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2612 controller_impl
->ActivateAnimations();
2613 controller_impl
->Animate(kInitialTickTime
);
2614 controller_impl
->UpdateState(true, events
.get());
2616 // Remove the first animation from the main-thread controller, and add a
2617 // new animation affecting the same property.
2618 controller
->RemoveAnimation(
2619 controller
->GetAnimation(Animation::OPACITY
)->id());
2620 int second_animation_id
=
2621 AddOpacityTransitionToController(controller
.get(), 1, 1.f
, 0.5f
, true);
2622 controller
->PushAnimationUpdatesTo(controller_impl
.get());
2624 // The original animation should only affect active observers, and the new
2625 // animation should only affect pending observers.
2626 EXPECT_FALSE(controller_impl
->GetAnimationById(first_animation_id
)
2627 ->affects_pending_observers());
2628 EXPECT_TRUE(controller_impl
->GetAnimationById(first_animation_id
)
2629 ->affects_active_observers());
2630 EXPECT_TRUE(controller_impl
->GetAnimationById(second_animation_id
)
2631 ->affects_pending_observers());
2632 EXPECT_FALSE(controller_impl
->GetAnimationById(second_animation_id
)
2633 ->affects_active_observers());
2635 controller_impl
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(500));
2636 controller_impl
->UpdateState(true, events
.get());
2638 // The original animation should still be running, and the new animation
2639 // should be starting.
2640 EXPECT_EQ(Animation::RUNNING
,
2641 controller_impl
->GetAnimationById(first_animation_id
)->run_state());
2643 Animation::STARTING
,
2644 controller_impl
->GetAnimationById(second_animation_id
)->run_state());
2646 // The active observer should have been ticked by the original animation,
2647 // and the pending observer should have been ticked by the new animation.
2648 EXPECT_EQ(1.f
, pending_dummy_impl
.opacity());
2649 EXPECT_EQ(0.5f
, dummy_impl
.opacity());
2651 controller_impl
->ActivateAnimations();
2653 // The original animation should have been deleted, and the new animation
2654 // should now affect both observers.
2655 EXPECT_FALSE(controller_impl
->GetAnimationById(first_animation_id
));
2656 EXPECT_TRUE(controller_impl
->GetAnimationById(second_animation_id
)
2657 ->affects_pending_observers());
2658 EXPECT_TRUE(controller_impl
->GetAnimationById(second_animation_id
)
2659 ->affects_active_observers());
2661 controller_impl
->Animate(kInitialTickTime
+
2662 TimeDelta::FromMilliseconds(1000));
2663 controller_impl
->UpdateState(true, events
.get());
2665 // The new animation should be running, and the active observer should have
2666 // been ticked at the new animation's starting point.
2669 controller_impl
->GetAnimationById(second_animation_id
)->run_state());
2670 EXPECT_EQ(1.f
, pending_dummy_impl
.opacity());
2671 EXPECT_EQ(1.f
, dummy_impl
.opacity());
2674 TEST(LayerAnimationControllerTest
, TestIsCurrentlyAnimatingProperty
) {
2675 FakeLayerAnimationValueObserver dummy
;
2676 scoped_refptr
<LayerAnimationController
> controller(
2677 LayerAnimationController::Create(0));
2678 controller
->AddValueObserver(&dummy
);
2680 // Create an animation that initially affects only pending observers.
2681 scoped_ptr
<Animation
> animation(CreateAnimation(
2682 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
2683 1, Animation::OPACITY
));
2684 animation
->set_affects_active_observers(false);
2686 controller
->AddAnimation(animation
.Pass());
2687 controller
->Animate(kInitialTickTime
);
2688 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2689 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2690 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2691 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2692 controller
->UpdateState(true, nullptr);
2693 EXPECT_TRUE(controller
->HasActiveAnimation());
2695 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2696 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2697 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2698 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2699 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2700 Animation::FILTER
, LayerAnimationController::ObserverType::PENDING
));
2701 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2702 Animation::FILTER
, LayerAnimationController::ObserverType::ACTIVE
));
2704 controller
->ActivateAnimations();
2706 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2707 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2708 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2709 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2710 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2711 Animation::FILTER
, LayerAnimationController::ObserverType::PENDING
));
2712 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2713 Animation::FILTER
, LayerAnimationController::ObserverType::ACTIVE
));
2715 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(10));
2716 controller
->UpdateState(true, nullptr);
2718 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2719 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2720 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2721 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2722 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2723 Animation::FILTER
, LayerAnimationController::ObserverType::PENDING
));
2724 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2725 Animation::FILTER
, LayerAnimationController::ObserverType::ACTIVE
));
2727 EXPECT_EQ(0.f
, dummy
.opacity());
2729 // Tick past the end of the animation.
2730 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(1100));
2731 controller
->UpdateState(true, nullptr);
2733 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2734 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2735 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2736 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2737 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2738 Animation::FILTER
, LayerAnimationController::ObserverType::PENDING
));
2739 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2740 Animation::FILTER
, LayerAnimationController::ObserverType::ACTIVE
));
2742 EXPECT_EQ(1.f
, dummy
.opacity());
2745 TEST(LayerAnimationControllerTest
, TestIsAnimatingPropertyTimeOffsetFillMode
) {
2746 FakeLayerAnimationValueObserver dummy
;
2747 scoped_refptr
<LayerAnimationController
> controller(
2748 LayerAnimationController::Create(0));
2749 controller
->AddValueObserver(&dummy
);
2751 // Create an animation that initially affects only pending observers, and has
2752 // a start delay of 2 seconds.
2753 scoped_ptr
<Animation
> animation(CreateAnimation(
2754 scoped_ptr
<AnimationCurve
>(new FakeFloatTransition(1.0, 0.f
, 1.f
)).Pass(),
2755 1, Animation::OPACITY
));
2756 animation
->set_fill_mode(Animation::FILL_MODE_NONE
);
2757 animation
->set_time_offset(TimeDelta::FromMilliseconds(-2000));
2758 animation
->set_affects_active_observers(false);
2760 controller
->AddAnimation(animation
.Pass());
2762 controller
->Animate(kInitialTickTime
);
2764 // Since the animation has a start delay, the observers it affects have a
2765 // potentially running transform animation but aren't currently animating
2767 EXPECT_TRUE(controller
->IsPotentiallyAnimatingProperty(
2768 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2769 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2770 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2771 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2772 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2773 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2774 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2775 EXPECT_TRUE(controller
->HasActiveAnimation());
2776 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2777 Animation::FILTER
, LayerAnimationController::ObserverType::PENDING
));
2778 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2779 Animation::FILTER
, LayerAnimationController::ObserverType::ACTIVE
));
2781 controller
->ActivateAnimations();
2783 EXPECT_TRUE(controller
->IsPotentiallyAnimatingProperty(
2784 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2785 EXPECT_TRUE(controller
->IsPotentiallyAnimatingProperty(
2786 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2787 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2788 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2789 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2790 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2791 EXPECT_TRUE(controller
->HasActiveAnimation());
2792 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2793 Animation::FILTER
, LayerAnimationController::ObserverType::PENDING
));
2794 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2795 Animation::FILTER
, LayerAnimationController::ObserverType::ACTIVE
));
2797 controller
->UpdateState(true, nullptr);
2799 // Tick past the start delay.
2800 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(2000));
2801 controller
->UpdateState(true, nullptr);
2802 EXPECT_TRUE(controller
->IsPotentiallyAnimatingProperty(
2803 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2804 EXPECT_TRUE(controller
->IsPotentiallyAnimatingProperty(
2805 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2806 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2807 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2808 EXPECT_TRUE(controller
->IsCurrentlyAnimatingProperty(
2809 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2811 // After the animaton finishes, the observers it affects have neither a
2812 // potentially running transform animation nor a currently running transform
2814 controller
->Animate(kInitialTickTime
+ TimeDelta::FromMilliseconds(4000));
2815 controller
->UpdateState(true, nullptr);
2816 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2817 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2818 EXPECT_FALSE(controller
->IsPotentiallyAnimatingProperty(
2819 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));
2820 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2821 Animation::OPACITY
, LayerAnimationController::ObserverType::PENDING
));
2822 EXPECT_FALSE(controller
->IsCurrentlyAnimatingProperty(
2823 Animation::OPACITY
, LayerAnimationController::ObserverType::ACTIVE
));