1 // Copyright (c) 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 "ui/compositor/layer_animator.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/compositor/layer_animation_delegate.h"
15 #include "ui/compositor/layer_animation_element.h"
16 #include "ui/compositor/layer_animation_sequence.h"
17 #include "ui/compositor/layer_animator_collection.h"
18 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/compositor/test/context_factories_for_test.h"
21 #include "ui/compositor/test/layer_animator_test_controller.h"
22 #include "ui/compositor/test/test_compositor_host.h"
23 #include "ui/compositor/test/test_layer_animation_delegate.h"
24 #include "ui/compositor/test/test_layer_animation_observer.h"
25 #include "ui/compositor/test/test_utils.h"
26 #include "ui/gfx/geometry/rect.h"
27 #include "ui/gfx/transform.h"
33 // Converts |color| to a string. Each component of the color is separated by a
34 // space and the order if A R G B.
35 std::string
ColorToString(SkColor color
) {
36 return base::StringPrintf("%d %d %d %d", SkColorGetA(color
),
37 SkColorGetR(color
), SkColorGetG(color
),
41 // Creates vector with two LayerAnimationSequences, based on |first| and
42 // |second| layer animation elements.
43 std::vector
<LayerAnimationSequence
*> CreateMultiSequence(
44 LayerAnimationElement
* first
,
45 LayerAnimationElement
* second
) {
46 LayerAnimationSequence
* first_sequence
= new LayerAnimationSequence();
47 first_sequence
->AddElement(first
);
48 LayerAnimationSequence
* second_sequence
= new LayerAnimationSequence();
49 second_sequence
->AddElement(second
);
51 std::vector
<ui::LayerAnimationSequence
*> animations
;
52 animations
.push_back(first_sequence
);
53 animations
.push_back(second_sequence
);
57 class TestImplicitAnimationObserver
: public ImplicitAnimationObserver
{
59 explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed
)
60 : animations_completed_(false),
61 notify_when_animator_destructed_(notify_when_animator_destructed
) {
64 bool animations_completed() const { return animations_completed_
; }
65 void set_animations_completed(bool completed
) {
66 animations_completed_
= completed
;
69 bool WasAnimationAbortedForProperty(
70 LayerAnimationElement::AnimatableProperty property
) const {
71 return ImplicitAnimationObserver::WasAnimationAbortedForProperty(property
);
74 bool WasAnimationCompletedForProperty(
75 LayerAnimationElement::AnimatableProperty property
) const {
76 return ImplicitAnimationObserver::WasAnimationCompletedForProperty(
81 // ImplicitAnimationObserver implementation
82 void OnImplicitAnimationsCompleted() override
{
83 animations_completed_
= true;
86 bool RequiresNotificationWhenAnimatorDestroyed() const override
{
87 return notify_when_animator_destructed_
;
90 bool animations_completed_
;
91 bool notify_when_animator_destructed_
;
93 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver
);
96 // When notified that an animation has ended, stops all other animations.
97 class DeletingLayerAnimationObserver
: public LayerAnimationObserver
{
99 DeletingLayerAnimationObserver(LayerAnimator
* animator
)
100 : animator_(animator
) {
103 void OnLayerAnimationEnded(LayerAnimationSequence
* sequence
) override
{
104 animator_
->StopAnimating();
107 void OnLayerAnimationAborted(LayerAnimationSequence
* sequence
) override
{
108 animator_
->StopAnimating();
111 void OnLayerAnimationScheduled(LayerAnimationSequence
* sequence
) override
{}
114 LayerAnimator
* animator_
;
116 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver
);
119 class LayerAnimatorDestructionObserver
{
121 LayerAnimatorDestructionObserver() : animator_deleted_(false) {}
122 virtual ~LayerAnimatorDestructionObserver() {}
124 void NotifyAnimatorDeleted() {
125 animator_deleted_
= true;
128 bool IsAnimatorDeleted() {
129 return animator_deleted_
;
133 bool animator_deleted_
;
135 DISALLOW_COPY_AND_ASSIGN(LayerAnimatorDestructionObserver
);
138 class TestLayerAnimator
: public LayerAnimator
{
140 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)),
141 destruction_observer_(NULL
) {}
143 void SetDestructionObserver(
144 LayerAnimatorDestructionObserver
* observer
) {
145 destruction_observer_
= observer
;
149 ~TestLayerAnimator() override
{
150 if (destruction_observer_
) {
151 destruction_observer_
->NotifyAnimatorDeleted();
155 void ProgressAnimation(LayerAnimationSequence
* sequence
,
156 base::TimeTicks now
) override
{
157 EXPECT_TRUE(HasAnimation(sequence
));
158 LayerAnimator::ProgressAnimation(sequence
, now
);
162 LayerAnimatorDestructionObserver
* destruction_observer_
;
164 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
167 // The test layer animation sequence updates a live instances count when it is
168 // created and destroyed.
169 class TestLayerAnimationSequence
: public LayerAnimationSequence
{
171 TestLayerAnimationSequence(LayerAnimationElement
* element
,
172 int* num_live_instances
)
173 : LayerAnimationSequence(element
),
174 num_live_instances_(num_live_instances
) {
175 (*num_live_instances_
)++;
178 ~TestLayerAnimationSequence() override
{ (*num_live_instances_
)--; }
181 int* num_live_instances_
;
183 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence
);
188 // Checks that setting a property on an implicit animator causes an animation to
190 TEST(LayerAnimatorTest
, ImplicitAnimation
) {
191 scoped_refptr
<LayerAnimator
> animator(
192 LayerAnimator::CreateImplicitAnimator());
193 animator
->set_disable_timer_for_test(true);
194 TestLayerAnimationDelegate delegate
;
195 animator
->SetDelegate(&delegate
);
196 base::TimeTicks now
= base::TimeTicks::Now();
197 animator
->SetBrightness(0.5);
198 EXPECT_TRUE(animator
->is_animating());
199 animator
->Step(now
+ base::TimeDelta::FromSeconds(1));
200 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), 0.5);
203 // Checks that if the animator is a default animator, that implicit animations
205 TEST(LayerAnimatorTest
, NoImplicitAnimation
) {
206 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
207 animator
->set_disable_timer_for_test(true);
208 TestLayerAnimationDelegate delegate
;
209 animator
->SetDelegate(&delegate
);
210 animator
->SetBrightness(0.5);
211 EXPECT_FALSE(animator
->is_animating());
212 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), 0.5);
215 // Checks that StopAnimatingProperty stops animation for that property, and also
216 // skips the stopped animation to the end.
217 TEST(LayerAnimatorTest
, StopAnimatingProperty
) {
218 scoped_refptr
<LayerAnimator
> animator(
219 LayerAnimator::CreateImplicitAnimator());
220 animator
->set_disable_timer_for_test(true);
221 TestLayerAnimationDelegate delegate
;
222 animator
->SetDelegate(&delegate
);
223 double target_opacity(0.5);
224 gfx::Rect
target_bounds(0, 0, 50, 50);
225 animator
->SetOpacity(target_opacity
);
226 animator
->SetBounds(target_bounds
);
227 animator
->StopAnimatingProperty(LayerAnimationElement::OPACITY
);
228 EXPECT_TRUE(animator
->is_animating());
229 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), 0.5);
230 animator
->StopAnimatingProperty(LayerAnimationElement::BOUNDS
);
231 EXPECT_FALSE(animator
->is_animating());
232 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
235 // Checks that multiple running animation for separate properties can be stopped
236 // simultaneously and that all animations are advanced to their target values.
237 TEST(LayerAnimatorTest
, StopAnimating
) {
238 scoped_refptr
<LayerAnimator
> animator(
239 LayerAnimator::CreateImplicitAnimator());
240 animator
->set_disable_timer_for_test(true);
241 TestLayerAnimationDelegate delegate
;
242 animator
->SetDelegate(&delegate
);
243 double target_opacity(0.5);
244 gfx::Rect
target_bounds(0, 0, 50, 50);
245 animator
->SetOpacity(target_opacity
);
246 animator
->SetBounds(target_bounds
);
247 EXPECT_TRUE(animator
->is_animating());
248 animator
->StopAnimating();
249 EXPECT_FALSE(animator
->is_animating());
250 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), 0.5);
251 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
254 // Checks that multiple running animation for separate properties can be stopped
255 // simultaneously and that all animations are advanced to their target values.
256 TEST(LayerAnimatorTest
, AbortAllAnimations
) {
257 scoped_refptr
<LayerAnimator
> animator(
258 LayerAnimator::CreateImplicitAnimator());
259 animator
->set_disable_timer_for_test(true);
260 TestLayerAnimationDelegate delegate
;
261 double initial_opacity(1.0);
262 gfx::Rect
initial_bounds(0, 0, 10, 10);
263 delegate
.SetOpacityFromAnimation(initial_opacity
);
264 delegate
.SetBoundsFromAnimation(initial_bounds
);
265 animator
->SetDelegate(&delegate
);
266 double target_opacity(0.5);
267 gfx::Rect
target_bounds(0, 0, 50, 50);
268 animator
->SetOpacity(target_opacity
);
269 animator
->SetBounds(target_bounds
);
270 EXPECT_TRUE(animator
->is_animating());
271 animator
->AbortAllAnimations();
272 EXPECT_FALSE(animator
->is_animating());
273 EXPECT_FLOAT_EQ(initial_opacity
, delegate
.GetOpacityForAnimation());
274 CheckApproximatelyEqual(initial_bounds
, delegate
.GetBoundsForAnimation());
277 // Schedule a non-threaded animation that can run immediately. This is the
278 // trivial case and should result in the animation being started immediately.
279 TEST(LayerAnimatorTest
, ScheduleAnimationThatCanRunImmediately
) {
280 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
281 animator
->set_disable_timer_for_test(true);
282 TestLayerAnimationDelegate delegate
;
283 animator
->SetDelegate(&delegate
);
285 double start_brightness(0.0);
286 double middle_brightness(0.5);
287 double target_brightness(1.0);
289 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
291 delegate
.SetBrightnessFromAnimation(start_brightness
);
293 animator
->ScheduleAnimation(
294 new LayerAnimationSequence(
295 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
298 EXPECT_TRUE(animator
->is_animating());
299 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
301 base::TimeTicks start_time
= animator
->last_step_time();
303 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
305 EXPECT_TRUE(animator
->is_animating());
306 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
308 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
310 EXPECT_FALSE(animator
->is_animating());
311 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
314 // Schedule a threaded animation that can run immediately.
315 TEST(LayerAnimatorTest
, ScheduleThreadedAnimationThatCanRunImmediately
) {
316 double epsilon
= 0.00001;
317 LayerAnimatorTestController
test_controller(
318 LayerAnimator::CreateDefaultAnimator());
319 LayerAnimator
* animator
= test_controller
.animator();
320 test_controller
.animator()->set_disable_timer_for_test(true);
321 TestLayerAnimationDelegate delegate
;
322 test_controller
.animator()->SetDelegate(&delegate
);
324 double start_opacity(0.0);
325 double target_opacity(1.0);
327 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
329 delegate
.SetOpacityFromAnimation(start_opacity
);
331 test_controller
.animator()->ScheduleAnimation(
332 new LayerAnimationSequence(
333 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
335 EXPECT_TRUE(test_controller
.animator()->is_animating());
336 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
338 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
339 base::TimeTicks effective_start
= start_time
+ delta
;
341 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
342 cc::AnimationEvent::STARTED
, 0,
343 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
344 ->animation_group_id(),
345 cc::Animation::OPACITY
, effective_start
));
347 animator
->Step(effective_start
+ delta
/ 2);
349 EXPECT_TRUE(test_controller
.animator()->is_animating());
352 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
353 last_progressed_fraction(),
356 animator
->Step(effective_start
+ delta
);
358 EXPECT_FALSE(test_controller
.animator()->is_animating());
359 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
362 // Schedule two non-threaded animations on separate properties. Both animations
363 // should start immediately and should progress in lock step.
364 TEST(LayerAnimatorTest
, ScheduleTwoAnimationsThatCanRunImmediately
) {
365 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
366 animator
->set_disable_timer_for_test(true);
367 TestLayerAnimationDelegate delegate
;
368 animator
->SetDelegate(&delegate
);
370 double start_brightness(0.0);
371 double middle_brightness(0.5);
372 double target_brightness(1.0);
374 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
375 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
376 start_bounds
.set_x(-90);
377 target_bounds
.set_x(90);
379 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
381 delegate
.SetBrightnessFromAnimation(start_brightness
);
382 delegate
.SetBoundsFromAnimation(start_bounds
);
384 animator
->ScheduleAnimation(
385 new LayerAnimationSequence(
386 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
389 animator
->ScheduleAnimation(
390 new LayerAnimationSequence(
391 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
393 EXPECT_TRUE(animator
->is_animating());
394 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
395 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
397 base::TimeTicks start_time
= animator
->last_step_time();
399 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
401 EXPECT_TRUE(animator
->is_animating());
402 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
403 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), middle_bounds
);
405 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
407 EXPECT_FALSE(animator
->is_animating());
408 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
409 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
412 // Schedule a threaded and a non-threaded animation on separate properties. Both
413 // animations should progress in lock step.
414 TEST(LayerAnimatorTest
, ScheduleThreadedAndNonThreadedAnimations
) {
415 double epsilon
= 0.00001;
416 LayerAnimatorTestController
test_controller(
417 LayerAnimator::CreateDefaultAnimator());
418 LayerAnimator
* animator
= test_controller
.animator();
419 test_controller
.animator()->set_disable_timer_for_test(true);
420 TestLayerAnimationDelegate delegate
;
421 test_controller
.animator()->SetDelegate(&delegate
);
423 double start_opacity(0.0);
424 double target_opacity(1.0);
426 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
427 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
428 start_bounds
.set_x(-90);
429 target_bounds
.set_x(90);
431 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
433 delegate
.SetOpacityFromAnimation(start_opacity
);
434 delegate
.SetBoundsFromAnimation(start_bounds
);
436 std::vector
<LayerAnimationSequence
*> animations
;
437 animations
.push_back(
438 new LayerAnimationSequence(
439 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
441 animations
.push_back(
442 new LayerAnimationSequence(
443 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
445 test_controller
.animator()->ScheduleTogether(animations
);
447 EXPECT_TRUE(test_controller
.animator()->is_animating());
448 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
449 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
451 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
452 base::TimeTicks effective_start
= start_time
+ delta
;
454 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
455 cc::AnimationEvent::STARTED
, 0,
456 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
457 ->animation_group_id(),
458 cc::Animation::OPACITY
, effective_start
));
460 animator
->Step(effective_start
+ delta
/ 2);
462 EXPECT_TRUE(test_controller
.animator()->is_animating());
465 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
466 last_progressed_fraction(),
468 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), middle_bounds
);
470 animator
->Step(effective_start
+ delta
);
472 EXPECT_FALSE(test_controller
.animator()->is_animating());
473 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
474 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
477 // Schedule two animations on the same property. In this case, the two
478 // animations should run one after another.
479 TEST(LayerAnimatorTest
, ScheduleTwoAnimationsOnSameProperty
) {
480 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
481 animator
->set_disable_timer_for_test(true);
482 TestLayerAnimationDelegate delegate
;
483 animator
->SetDelegate(&delegate
);
485 double start_brightness(0.0);
486 double middle_brightness(0.5);
487 double target_brightness(1.0);
489 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
491 delegate
.SetBrightnessFromAnimation(start_brightness
);
493 animator
->ScheduleAnimation(
494 new LayerAnimationSequence(
495 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
498 animator
->ScheduleAnimation(
499 new LayerAnimationSequence(
500 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
503 EXPECT_TRUE(animator
->is_animating());
504 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
506 base::TimeTicks start_time
= animator
->last_step_time();
508 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
510 EXPECT_TRUE(animator
->is_animating());
511 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
513 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
515 EXPECT_TRUE(animator
->is_animating());
516 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
518 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
520 EXPECT_TRUE(animator
->is_animating());
521 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
523 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
525 EXPECT_FALSE(animator
->is_animating());
526 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
529 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
530 // is, ensure that all animations targetting a particular property are run in
532 TEST(LayerAnimatorTest
, ScheduleBlockedAnimation
) {
533 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
534 animator
->set_disable_timer_for_test(true);
535 TestLayerAnimationDelegate delegate
;
536 animator
->SetDelegate(&delegate
);
538 double start_grayscale(0.0);
539 double middle_grayscale(0.5);
540 double target_grayscale(1.0);
542 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
543 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
544 start_bounds
.set_x(-90);
545 target_bounds
.set_x(90);
547 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
549 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
550 delegate
.SetBoundsFromAnimation(start_bounds
);
552 animator
->ScheduleAnimation(
553 new LayerAnimationSequence(
554 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
557 scoped_ptr
<LayerAnimationSequence
> bounds_and_grayscale(
558 new LayerAnimationSequence(
559 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
,
562 bounds_and_grayscale
->AddElement(
563 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
565 animator
->ScheduleAnimation(bounds_and_grayscale
.release());
567 animator
->ScheduleAnimation(
568 new LayerAnimationSequence(
569 LayerAnimationElement::CreateBoundsElement(start_bounds
, delta
)));
571 EXPECT_TRUE(animator
->is_animating());
572 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
573 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
575 base::TimeTicks start_time
= animator
->last_step_time();
577 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
579 EXPECT_TRUE(animator
->is_animating());
580 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
581 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
583 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
585 EXPECT_TRUE(animator
->is_animating());
586 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
587 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
589 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
591 EXPECT_TRUE(animator
->is_animating());
592 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
593 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
595 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(3000));
597 EXPECT_TRUE(animator
->is_animating());
598 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
599 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
601 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(4000));
603 EXPECT_FALSE(animator
->is_animating());
604 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
605 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
608 // Schedule {g} and then schedule {g} and {b} together. In this case, since
609 // ScheduleTogether is being used, the bounds animation should not start until
610 // the second grayscale animation starts.
611 TEST(LayerAnimatorTest
, ScheduleTogether
) {
612 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
613 animator
->set_disable_timer_for_test(true);
614 TestLayerAnimationDelegate delegate
;
615 animator
->SetDelegate(&delegate
);
617 double start_grayscale(0.0);
618 double target_grayscale(1.0);
620 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
621 start_bounds
= target_bounds
= gfx::Rect(0, 0, 50, 50);
622 start_bounds
.set_x(-90);
623 target_bounds
.set_x(90);
625 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
627 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
628 delegate
.SetBoundsFromAnimation(start_bounds
);
630 animator
->ScheduleAnimation(
631 new LayerAnimationSequence(
632 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
635 std::vector
<LayerAnimationSequence
*> sequences
;
636 sequences
.push_back(new LayerAnimationSequence(
637 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
)));
638 sequences
.push_back(new LayerAnimationSequence(
639 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
641 animator
->ScheduleTogether(sequences
);
643 EXPECT_TRUE(animator
->is_animating());
644 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
645 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
647 base::TimeTicks start_time
= animator
->last_step_time();
649 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
651 EXPECT_TRUE(animator
->is_animating());
652 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
653 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
655 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
657 EXPECT_FALSE(animator
->is_animating());
658 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
659 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
662 // Start non-threaded animation (that can run immediately). This is the trivial
663 // case (see the trival case for ScheduleAnimation).
664 TEST(LayerAnimatorTest
, StartAnimationThatCanRunImmediately
) {
665 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
666 animator
->set_disable_timer_for_test(true);
667 TestLayerAnimationDelegate delegate
;
668 animator
->SetDelegate(&delegate
);
670 double start_brightness(0.0);
671 double middle_brightness(0.5);
672 double target_brightness(1.0);
674 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
676 delegate
.SetBrightnessFromAnimation(start_brightness
);
678 animator
->StartAnimation(
679 new LayerAnimationSequence(
680 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
683 EXPECT_TRUE(animator
->is_animating());
684 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
686 base::TimeTicks start_time
= animator
->last_step_time();
688 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
690 EXPECT_TRUE(animator
->is_animating());
691 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
693 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
695 EXPECT_FALSE(animator
->is_animating());
696 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
699 // Start threaded animation (that can run immediately).
700 TEST(LayerAnimatorTest
, StartThreadedAnimationThatCanRunImmediately
) {
701 double epsilon
= 0.00001;
702 LayerAnimatorTestController
test_controller(
703 LayerAnimator::CreateDefaultAnimator());
704 LayerAnimator
* animator
= test_controller
.animator();
705 test_controller
.animator()->set_disable_timer_for_test(true);
706 TestLayerAnimationDelegate delegate
;
707 test_controller
.animator()->SetDelegate(&delegate
);
709 double start_opacity(0.0);
710 double target_opacity(1.0);
712 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
714 delegate
.SetOpacityFromAnimation(start_opacity
);
716 test_controller
.animator()->StartAnimation(
717 new LayerAnimationSequence(
718 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
720 EXPECT_TRUE(test_controller
.animator()->is_animating());
721 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
723 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
724 base::TimeTicks effective_start
= start_time
+ delta
;
726 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
727 cc::AnimationEvent::STARTED
, 0,
728 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
729 ->animation_group_id(),
730 cc::Animation::OPACITY
, effective_start
));
732 animator
->Step(effective_start
+ delta
/ 2);
734 EXPECT_TRUE(test_controller
.animator()->is_animating());
737 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
738 last_progressed_fraction(),
741 animator
->Step(effective_start
+ delta
);
742 EXPECT_FALSE(test_controller
.animator()->is_animating());
743 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
746 // Preempt by immediately setting new target.
747 TEST(LayerAnimatorTest
, PreemptBySettingNewTarget
) {
748 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
749 animator
->set_disable_timer_for_test(true);
750 TestLayerAnimationDelegate delegate
;
751 animator
->SetDelegate(&delegate
);
753 double start_opacity(0.0);
754 double target_opacity(1.0);
756 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
758 delegate
.SetOpacityFromAnimation(start_opacity
);
760 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
762 animator
->StartAnimation(
763 new LayerAnimationSequence(
764 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
766 animator
->StartAnimation(
767 new LayerAnimationSequence(
768 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
770 EXPECT_FALSE(animator
->is_animating());
771 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
774 // Preempt by animating to new target, with a non-threaded animation.
775 TEST(LayerAnimatorTest
, PreemptByImmediatelyAnimatingToNewTarget
) {
776 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
777 animator
->set_disable_timer_for_test(true);
778 TestLayerAnimationDelegate delegate
;
779 animator
->SetDelegate(&delegate
);
781 double start_brightness(0.0);
782 double middle_brightness(0.5);
783 double target_brightness(1.0);
785 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
787 delegate
.SetBrightnessFromAnimation(start_brightness
);
789 animator
->set_preemption_strategy(
790 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
792 animator
->StartAnimation(
793 new LayerAnimationSequence(
794 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
797 base::TimeTicks start_time
= animator
->last_step_time();
799 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
801 animator
->StartAnimation(
802 new LayerAnimationSequence(
803 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
806 EXPECT_TRUE(animator
->is_animating());
807 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
809 animator
->StartAnimation(
810 new LayerAnimationSequence(
811 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
814 EXPECT_TRUE(animator
->is_animating());
816 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
818 EXPECT_TRUE(animator
->is_animating());
819 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
820 0.5 * (start_brightness
+ middle_brightness
));
822 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
824 EXPECT_FALSE(animator
->is_animating());
825 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
828 // Preempt by animating to new target, with a threaded animation.
829 TEST(LayerAnimatorTest
, PreemptThreadedByImmediatelyAnimatingToNewTarget
) {
830 double epsilon
= 0.00001;
831 LayerAnimatorTestController
test_controller(
832 LayerAnimator::CreateDefaultAnimator());
833 LayerAnimator
* animator
= test_controller
.animator();
834 test_controller
.animator()->set_disable_timer_for_test(true);
835 TestLayerAnimationDelegate delegate
;
836 test_controller
.animator()->SetDelegate(&delegate
);
838 double start_opacity(0.0);
839 double middle_opacity(0.5);
840 double target_opacity(1.0);
842 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
844 delegate
.SetOpacityFromAnimation(start_opacity
);
846 test_controller
.animator()->set_preemption_strategy(
847 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
849 test_controller
.animator()->StartAnimation(
850 new LayerAnimationSequence(
851 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
853 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
854 base::TimeTicks effective_start
= start_time
+ delta
;
856 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
857 cc::AnimationEvent::STARTED
, 0,
858 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
859 ->animation_group_id(),
860 cc::Animation::OPACITY
, effective_start
));
862 animator
->Step(effective_start
+ delta
/ 2);
864 test_controller
.animator()->StartAnimation(
865 new LayerAnimationSequence(
866 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
868 EXPECT_TRUE(test_controller
.animator()->is_animating());
869 EXPECT_NEAR(delegate
.GetOpacityForAnimation(), middle_opacity
, epsilon
);
871 test_controller
.animator()->StartAnimation(
872 new LayerAnimationSequence(
873 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
875 EXPECT_TRUE(test_controller
.animator()->is_animating());
877 base::TimeTicks second_effective_start
= effective_start
+ delta
;
879 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
880 cc::AnimationEvent::STARTED
, 0,
881 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
882 ->animation_group_id(),
883 cc::Animation::OPACITY
, second_effective_start
));
885 animator
->Step(second_effective_start
+ delta
/ 2);
887 EXPECT_TRUE(test_controller
.animator()->is_animating());
890 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
891 last_progressed_fraction(),
894 animator
->Step(second_effective_start
+ delta
);
896 EXPECT_FALSE(test_controller
.animator()->is_animating());
897 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
900 // Preempt by enqueuing the new animation.
901 TEST(LayerAnimatorTest
, PreemptEnqueueNewAnimation
) {
902 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
903 animator
->set_disable_timer_for_test(true);
904 TestLayerAnimationDelegate delegate
;
905 animator
->SetDelegate(&delegate
);
907 double start_brightness(0.0);
908 double middle_brightness(0.5);
909 double target_brightness(1.0);
911 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
913 delegate
.SetBrightnessFromAnimation(start_brightness
);
915 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
917 animator
->StartAnimation(
918 new LayerAnimationSequence(
919 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
922 base::TimeTicks start_time
= animator
->last_step_time();
924 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
926 animator
->StartAnimation(
927 new LayerAnimationSequence(
928 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
931 EXPECT_TRUE(animator
->is_animating());
932 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
934 EXPECT_TRUE(animator
->is_animating());
936 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
938 EXPECT_TRUE(animator
->is_animating());
939 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
941 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
943 EXPECT_TRUE(animator
->is_animating());
944 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
946 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
948 EXPECT_FALSE(animator
->is_animating());
949 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
952 // Start an animation when there are sequences waiting in the queue. In this
953 // case, all pending and running animations should be finished, and the new
954 // animation started.
955 TEST(LayerAnimatorTest
, PreemptyByReplacingQueuedAnimations
) {
956 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
957 animator
->set_disable_timer_for_test(true);
958 TestLayerAnimationDelegate delegate
;
959 animator
->SetDelegate(&delegate
);
961 double start_brightness(0.0);
962 double middle_brightness(0.5);
963 double target_brightness(1.0);
965 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
967 delegate
.SetBrightnessFromAnimation(start_brightness
);
969 animator
->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
971 animator
->StartAnimation(
972 new LayerAnimationSequence(
973 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
976 base::TimeTicks start_time
= animator
->last_step_time();
978 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
980 animator
->StartAnimation(
981 new LayerAnimationSequence(
982 LayerAnimationElement::CreateBrightnessElement(middle_brightness
,
985 // Queue should now have two animations. Starting a third should replace the
987 animator
->StartAnimation(
988 new LayerAnimationSequence(
989 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
992 EXPECT_TRUE(animator
->is_animating());
993 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
995 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
997 EXPECT_TRUE(animator
->is_animating());
998 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1000 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1002 EXPECT_TRUE(animator
->is_animating());
1003 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1005 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1007 EXPECT_FALSE(animator
->is_animating());
1008 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1011 TEST(LayerAnimatorTest
, StartTogetherSetsLastStepTime
) {
1012 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1013 animator
->set_disable_timer_for_test(true);
1014 TestLayerAnimationDelegate delegate
;
1015 animator
->SetDelegate(&delegate
);
1017 double start_grayscale(0.0);
1018 double target_grayscale(1.0);
1019 double start_brightness(0.1);
1020 double target_brightness(0.9);
1022 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1024 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1025 delegate
.SetBrightnessFromAnimation(start_brightness
);
1027 animator
->set_preemption_strategy(
1028 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1030 animator
->set_last_step_time(base::TimeTicks());
1032 animator
->StartTogether(
1033 CreateMultiSequence(
1034 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1036 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1040 // If last step time was not set correctly, the resulting delta should be
1041 // miniscule (fractions of a millisecond). If set correctly, then the delta
1042 // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1043 // though a much smaller value would probably have sufficed.
1044 delta
= base::TimeTicks::Now() - animator
->last_step_time();
1045 EXPECT_GT(60.0, delta
.InSecondsF());
1048 //-------------------------------------------------------
1049 // Preempt by immediately setting new target.
1050 TEST(LayerAnimatorTest
, MultiPreemptBySettingNewTarget
) {
1051 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1052 animator
->set_disable_timer_for_test(true);
1053 TestLayerAnimationDelegate delegate
;
1054 animator
->SetDelegate(&delegate
);
1056 double start_opacity(0.0);
1057 double target_opacity(1.0);
1058 double start_brightness(0.1);
1059 double target_brightness(0.9);
1061 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1063 delegate
.SetOpacityFromAnimation(start_opacity
);
1064 delegate
.SetBrightnessFromAnimation(start_brightness
);
1066 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
1068 animator
->StartTogether(
1069 CreateMultiSequence(
1070 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
),
1071 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1075 animator
->StartTogether(
1076 CreateMultiSequence(
1077 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1078 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1082 EXPECT_FALSE(animator
->is_animating());
1083 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1084 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1087 // Preempt by animating to new target.
1088 TEST(LayerAnimatorTest
, MultiPreemptByImmediatelyAnimatingToNewTarget
) {
1089 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1090 animator
->set_disable_timer_for_test(true);
1091 TestLayerAnimationDelegate delegate
;
1092 animator
->SetDelegate(&delegate
);
1094 double start_grayscale(0.0);
1095 double middle_grayscale(0.5);
1096 double target_grayscale(1.0);
1098 double start_brightness(0.1);
1099 double middle_brightness(0.2);
1100 double target_brightness(0.3);
1102 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1104 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1105 delegate
.SetBrightnessFromAnimation(start_brightness
);
1107 animator
->set_preemption_strategy(
1108 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1110 animator
->StartTogether(
1111 CreateMultiSequence(
1112 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1114 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1118 base::TimeTicks start_time
= animator
->last_step_time();
1120 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1122 animator
->StartTogether(
1123 CreateMultiSequence(
1124 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1125 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1128 EXPECT_TRUE(animator
->is_animating());
1129 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1130 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1132 animator
->StartTogether(
1133 CreateMultiSequence(
1134 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1135 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1138 EXPECT_TRUE(animator
->is_animating());
1140 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1142 EXPECT_TRUE(animator
->is_animating());
1143 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(),
1144 0.5 * (start_grayscale
+ middle_grayscale
));
1145 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
1146 0.5 * (start_brightness
+ middle_brightness
));
1148 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1150 EXPECT_FALSE(animator
->is_animating());
1151 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1152 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1155 // Preempt a threaded animation by animating to new target.
1156 TEST(LayerAnimatorTest
, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget
) {
1157 double epsilon
= 0.00001;
1158 LayerAnimatorTestController
test_controller(
1159 LayerAnimator::CreateDefaultAnimator());
1160 LayerAnimator
* animator
= test_controller
.animator();
1161 test_controller
.animator()->set_disable_timer_for_test(true);
1162 TestLayerAnimationDelegate delegate
;
1163 test_controller
.animator()->SetDelegate(&delegate
);
1165 double start_opacity(0.0);
1166 double middle_opacity(0.5);
1167 double target_opacity(1.0);
1169 double start_brightness(0.1);
1170 double middle_brightness(0.2);
1171 double target_brightness(0.3);
1173 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1175 delegate
.SetOpacityFromAnimation(start_opacity
);
1176 delegate
.SetBrightnessFromAnimation(start_brightness
);
1178 test_controller
.animator()->set_preemption_strategy(
1179 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1181 test_controller
.animator()->StartTogether(
1182 CreateMultiSequence(
1183 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
),
1184 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1188 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
1189 base::TimeTicks effective_start
= start_time
+ delta
;
1191 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1192 cc::AnimationEvent::STARTED
, 0,
1193 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1194 ->animation_group_id(),
1195 cc::Animation::OPACITY
, effective_start
));
1197 animator
->Step(effective_start
+ delta
/ 2);
1199 test_controller
.animator()->StartTogether(
1200 CreateMultiSequence(
1201 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1202 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1205 EXPECT_TRUE(test_controller
.animator()->is_animating());
1206 EXPECT_NEAR(delegate
.GetOpacityForAnimation(), middle_opacity
, epsilon
);
1207 EXPECT_NEAR(delegate
.GetBrightnessForAnimation(), middle_brightness
, epsilon
);
1209 test_controller
.animator()->StartTogether(
1210 CreateMultiSequence(
1211 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1212 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1215 EXPECT_TRUE(test_controller
.animator()->is_animating());
1217 base::TimeTicks second_effective_start
= effective_start
+ delta
;
1219 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1220 cc::AnimationEvent::STARTED
, 0,
1221 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1222 ->animation_group_id(),
1223 cc::Animation::OPACITY
, second_effective_start
));
1225 animator
->Step(second_effective_start
+ delta
/ 2);
1227 EXPECT_TRUE(test_controller
.animator()->is_animating());
1230 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
1231 last_progressed_fraction(),
1233 EXPECT_NEAR(delegate
.GetBrightnessForAnimation(),
1234 0.5 * (start_brightness
+ middle_brightness
),
1237 animator
->Step(second_effective_start
+ delta
);
1239 EXPECT_FALSE(test_controller
.animator()->is_animating());
1240 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1241 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1244 // Preempt by enqueuing the new animation.
1245 TEST(LayerAnimatorTest
, MultiPreemptEnqueueNewAnimation
) {
1246 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1247 animator
->set_disable_timer_for_test(true);
1248 TestLayerAnimationDelegate delegate
;
1249 animator
->SetDelegate(&delegate
);
1251 double start_grayscale(0.0);
1252 double middle_grayscale(0.5);
1253 double target_grayscale(1.0);
1255 double start_brightness(0.1);
1256 double middle_brightness(0.2);
1257 double target_brightness(0.3);
1259 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1261 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1262 delegate
.SetBrightnessFromAnimation(start_brightness
);
1264 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
1266 animator
->StartTogether(
1267 CreateMultiSequence(
1268 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1270 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1273 base::TimeTicks start_time
= animator
->last_step_time();
1275 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1277 animator
->StartTogether(
1278 CreateMultiSequence(
1279 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1280 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1283 EXPECT_TRUE(animator
->is_animating());
1284 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1285 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1287 EXPECT_TRUE(animator
->is_animating());
1289 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1291 EXPECT_TRUE(animator
->is_animating());
1292 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
1293 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1295 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1297 EXPECT_TRUE(animator
->is_animating());
1298 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1299 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1301 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1303 EXPECT_FALSE(animator
->is_animating());
1304 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1305 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1308 // Start an animation when there are sequences waiting in the queue. In this
1309 // case, all pending and running animations should be finished, and the new
1310 // animation started.
1311 TEST(LayerAnimatorTest
, MultiPreemptByReplacingQueuedAnimations
) {
1312 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1313 animator
->set_disable_timer_for_test(true);
1314 TestLayerAnimationDelegate delegate
;
1315 animator
->SetDelegate(&delegate
);
1317 double start_grayscale(0.0);
1318 double middle_grayscale(0.5);
1319 double target_grayscale(1.0);
1321 double start_brightness(0.1);
1322 double middle_brightness(0.2);
1323 double target_brightness(0.3);
1325 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1327 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1328 delegate
.SetBrightnessFromAnimation(start_brightness
);
1330 animator
->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
1332 animator
->StartTogether(
1333 CreateMultiSequence(
1334 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1336 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1339 base::TimeTicks start_time
= animator
->last_step_time();
1341 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1343 animator
->StartTogether(
1344 CreateMultiSequence(
1345 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale
,
1347 LayerAnimationElement::CreateBrightnessElement(middle_brightness
,
1350 // Queue should now have two animations. Starting a third should replace the
1352 animator
->StartTogether(
1353 CreateMultiSequence(
1354 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1355 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1358 EXPECT_TRUE(animator
->is_animating());
1359 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1360 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1362 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1364 EXPECT_TRUE(animator
->is_animating());
1365 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
1366 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1368 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1370 EXPECT_TRUE(animator
->is_animating());
1371 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1372 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1374 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1376 EXPECT_FALSE(animator
->is_animating());
1377 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1378 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1380 //-------------------------------------------------------
1381 // Test that non-threaded cyclic sequences continue to animate.
1382 TEST(LayerAnimatorTest
, CyclicSequences
) {
1383 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1384 animator
->set_disable_timer_for_test(true);
1385 TestLayerAnimationDelegate delegate
;
1386 animator
->SetDelegate(&delegate
);
1388 double start_brightness(0.0);
1389 double target_brightness(1.0);
1391 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1393 delegate
.SetBrightnessFromAnimation(start_brightness
);
1395 scoped_ptr
<LayerAnimationSequence
> sequence(
1396 new LayerAnimationSequence(
1397 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1400 sequence
->AddElement(
1401 LayerAnimationElement::CreateBrightnessElement(start_brightness
, delta
));
1403 sequence
->set_is_cyclic(true);
1405 animator
->StartAnimation(sequence
.release());
1407 base::TimeTicks start_time
= animator
->last_step_time();
1409 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1411 EXPECT_TRUE(animator
->is_animating());
1412 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1414 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1416 EXPECT_TRUE(animator
->is_animating());
1417 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1419 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(3000));
1421 EXPECT_TRUE(animator
->is_animating());
1422 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1424 // Skip ahead by a lot.
1425 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000000000));
1427 EXPECT_TRUE(animator
->is_animating());
1428 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1430 // Skip ahead by a lot.
1431 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000001000));
1433 EXPECT_TRUE(animator
->is_animating());
1434 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1436 animator
->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS
);
1438 EXPECT_FALSE(animator
->is_animating());
1441 // Test that threaded cyclic sequences continue to animate.
1442 TEST(LayerAnimatorTest
, ThreadedCyclicSequences
) {
1443 LayerAnimatorTestController
test_controller(
1444 LayerAnimator::CreateDefaultAnimator());
1445 LayerAnimator
* animator
= test_controller
.animator();
1446 test_controller
.animator()->set_disable_timer_for_test(true);
1447 TestLayerAnimationDelegate delegate
;
1448 test_controller
.animator()->SetDelegate(&delegate
);
1450 double start_opacity(0.0);
1451 double target_opacity(1.0);
1453 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1455 delegate
.SetOpacityFromAnimation(start_opacity
);
1457 scoped_ptr
<LayerAnimationSequence
> sequence(
1458 new LayerAnimationSequence(
1459 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
1461 sequence
->AddElement(
1462 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
));
1464 sequence
->set_is_cyclic(true);
1466 test_controller
.animator()->StartAnimation(sequence
.release());
1468 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
1469 base::TimeTicks effective_start
= start_time
+ delta
;
1471 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1472 cc::AnimationEvent::STARTED
, 0,
1473 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1474 ->animation_group_id(),
1475 cc::Animation::OPACITY
, effective_start
));
1477 animator
->Step(effective_start
+ delta
);
1478 EXPECT_TRUE(test_controller
.animator()->is_animating());
1479 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1481 base::TimeTicks second_effective_start
= effective_start
+ 2 * delta
;
1482 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1483 cc::AnimationEvent::STARTED
, 0,
1484 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1485 ->animation_group_id(),
1486 cc::Animation::OPACITY
, second_effective_start
));
1488 animator
->Step(second_effective_start
+ delta
);
1490 EXPECT_TRUE(test_controller
.animator()->is_animating());
1491 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1493 base::TimeTicks third_effective_start
= second_effective_start
+ 2 * delta
;
1494 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1495 cc::AnimationEvent::STARTED
, 0,
1496 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1497 ->animation_group_id(),
1498 cc::Animation::OPACITY
, third_effective_start
));
1500 animator
->Step(third_effective_start
+ delta
);
1501 EXPECT_TRUE(test_controller
.animator()->is_animating());
1502 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1504 base::TimeTicks fourth_effective_start
= third_effective_start
+ 2 * delta
;
1505 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1506 cc::AnimationEvent::STARTED
, 0,
1507 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1508 ->animation_group_id(),
1509 cc::Animation::OPACITY
, fourth_effective_start
));
1511 // Skip ahead by a lot.
1512 animator
->Step(fourth_effective_start
+ 1000 * delta
);
1514 EXPECT_TRUE(test_controller
.animator()->is_animating());
1515 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1517 base::TimeTicks fifth_effective_start
= fourth_effective_start
+ 1001 * delta
;
1518 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1519 cc::AnimationEvent::STARTED
, 0,
1520 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1521 ->animation_group_id(),
1522 cc::Animation::OPACITY
, fifth_effective_start
));
1524 // Skip ahead by a lot.
1525 animator
->Step(fifth_effective_start
+ 999 * delta
);
1527 EXPECT_TRUE(test_controller
.animator()->is_animating());
1528 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1530 test_controller
.animator()->StopAnimatingProperty(
1531 LayerAnimationElement::OPACITY
);
1533 EXPECT_FALSE(test_controller
.animator()->is_animating());
1536 TEST(LayerAnimatorTest
, AddObserverExplicit
) {
1537 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1538 animator
->set_disable_timer_for_test(true);
1539 TestLayerAnimationObserver observer
;
1540 TestLayerAnimationDelegate delegate
;
1541 animator
->SetDelegate(&delegate
);
1542 animator
->AddObserver(&observer
);
1543 observer
.set_requires_notification_when_animator_destroyed(true);
1545 EXPECT_TRUE(!observer
.last_ended_sequence());
1547 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1549 delegate
.SetBrightnessFromAnimation(0.0f
);
1551 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1552 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1554 animator
->StartAnimation(sequence
);
1556 EXPECT_EQ(observer
.last_scheduled_sequence(), sequence
);
1558 base::TimeTicks start_time
= animator
->last_step_time();
1560 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1562 EXPECT_EQ(observer
.last_ended_sequence(), sequence
);
1564 // |sequence| has been destroyed. Recreate it to test abort.
1565 sequence
= new LayerAnimationSequence(
1566 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1568 animator
->StartAnimation(sequence
);
1572 EXPECT_EQ(observer
.last_aborted_sequence(), sequence
);
1575 // Tests that an observer added to a scoped settings object is still notified
1576 // when the object goes out of scope.
1577 TEST(LayerAnimatorTest
, ImplicitAnimationObservers
) {
1578 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1579 animator
->set_disable_timer_for_test(true);
1580 TestImplicitAnimationObserver
observer(false);
1581 TestLayerAnimationDelegate delegate
;
1582 animator
->SetDelegate(&delegate
);
1584 EXPECT_FALSE(observer
.animations_completed());
1585 animator
->SetBrightness(1.0f
);
1588 ScopedLayerAnimationSettings
settings(animator
.get());
1589 settings
.AddObserver(&observer
);
1590 animator
->SetBrightness(0.0f
);
1593 EXPECT_FALSE(observer
.animations_completed());
1594 base::TimeTicks start_time
= animator
->last_step_time();
1595 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1596 EXPECT_TRUE(observer
.animations_completed());
1597 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1598 LayerAnimationElement::BRIGHTNESS
));
1599 EXPECT_FLOAT_EQ(0.0f
, delegate
.GetBrightnessForAnimation());
1602 // Tests that an observer added to a scoped settings object is still notified
1603 // when the object goes out of scope due to the animation being interrupted.
1604 TEST(LayerAnimatorTest
, InterruptedImplicitAnimationObservers
) {
1605 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1606 animator
->set_disable_timer_for_test(true);
1607 TestImplicitAnimationObserver
observer(false);
1608 TestLayerAnimationDelegate delegate
;
1609 animator
->SetDelegate(&delegate
);
1611 EXPECT_FALSE(observer
.animations_completed());
1612 animator
->SetBrightness(1.0f
);
1615 ScopedLayerAnimationSettings
settings(animator
.get());
1616 settings
.AddObserver(&observer
);
1617 animator
->SetBrightness(0.0f
);
1620 EXPECT_FALSE(observer
.animations_completed());
1621 // This should interrupt the implicit animation causing the observer to be
1622 // notified immediately.
1623 animator
->SetBrightness(1.0f
);
1624 EXPECT_TRUE(observer
.animations_completed());
1625 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1626 LayerAnimationElement::BRIGHTNESS
));
1627 EXPECT_FLOAT_EQ(1.0f
, delegate
.GetBrightnessForAnimation());
1630 // Tests that LayerAnimator is not deleted after the animation completes as long
1631 // as there is a live ScopedLayerAnimationSettings object wrapped around it.
1632 TEST(LayerAnimatorTest
, AnimatorKeptAliveBySettings
) {
1633 // Note we are using a raw pointer unlike in other tests.
1634 TestLayerAnimator
* animator
= new TestLayerAnimator();
1635 LayerAnimatorDestructionObserver destruction_observer
;
1636 animator
->SetDestructionObserver(&destruction_observer
);
1637 animator
->set_disable_timer_for_test(true);
1638 TestLayerAnimationDelegate delegate
;
1639 animator
->SetDelegate(&delegate
);
1641 // ScopedLayerAnimationSettings should keep the Animator alive as long as
1642 // it is alive, even beyond the end of the animation.
1643 ScopedLayerAnimationSettings
settings(animator
);
1644 base::TimeTicks now
= base::TimeTicks::Now();
1645 animator
->SetBrightness(0.5);
1646 animator
->Step(now
+ base::TimeDelta::FromSeconds(1));
1647 EXPECT_FALSE(destruction_observer
.IsAnimatorDeleted());
1649 // ScopedLayerAnimationSettings was destroyed, so Animator should be deleted.
1650 EXPECT_TRUE(destruction_observer
.IsAnimatorDeleted());
1653 // Tests that an observer added to a scoped settings object is not notified
1654 // when the animator is destroyed unless explicitly requested.
1655 TEST(LayerAnimatorTest
, ImplicitObserversAtAnimatorDestruction
) {
1656 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1657 animator
->set_disable_timer_for_test(true);
1658 TestImplicitAnimationObserver
observer_notify(true);
1659 TestImplicitAnimationObserver
observer_do_not_notify(false);
1660 TestLayerAnimationDelegate delegate
;
1661 animator
->SetDelegate(&delegate
);
1663 EXPECT_FALSE(observer_notify
.animations_completed());
1664 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1666 animator
->SetBrightness(1.0f
);
1669 ScopedLayerAnimationSettings
settings(animator
.get());
1670 settings
.AddObserver(&observer_notify
);
1671 settings
.AddObserver(&observer_do_not_notify
);
1672 animator
->SetBrightness(0.0f
);
1675 EXPECT_FALSE(observer_notify
.animations_completed());
1676 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1678 EXPECT_TRUE(observer_notify
.animations_completed());
1679 EXPECT_TRUE(observer_notify
.WasAnimationAbortedForProperty(
1680 LayerAnimationElement::BRIGHTNESS
));
1681 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1684 TEST(LayerAnimatorTest
, AbortedAnimationStatusInImplicitObservers
) {
1685 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1686 animator
->set_disable_timer_for_test(true);
1687 TestImplicitAnimationObserver
observer(false);
1688 TestLayerAnimationDelegate delegate
;
1689 animator
->SetDelegate(&delegate
);
1691 EXPECT_FALSE(observer
.animations_completed());
1692 animator
->SetBrightness(1.0f
);
1695 ScopedLayerAnimationSettings
settings(animator
.get());
1696 settings
.AddObserver(&observer
);
1697 animator
->SetBrightness(0.0f
);
1699 EXPECT_FALSE(observer
.animations_completed());
1701 animator
->AbortAllAnimations();
1702 EXPECT_TRUE(observer
.animations_completed());
1703 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1704 LayerAnimationElement::BRIGHTNESS
));
1705 EXPECT_FALSE(observer
.WasAnimationAbortedForProperty(
1706 LayerAnimationElement::OPACITY
));
1708 observer
.set_animations_completed(false);
1710 ScopedLayerAnimationSettings
settings(animator
.get());
1711 settings
.AddObserver(&observer
);
1712 animator
->SetOpacity(0.0f
);
1714 EXPECT_FALSE(observer
.animations_completed());
1716 animator
->AbortAllAnimations();
1717 EXPECT_TRUE(observer
.animations_completed());
1718 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1719 LayerAnimationElement::BRIGHTNESS
));
1720 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1721 LayerAnimationElement::OPACITY
));
1724 TEST(LayerAnimatorTest
, RemoveObserverShouldRemoveFromSequences
) {
1725 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1726 animator
->set_disable_timer_for_test(true);
1727 TestLayerAnimationObserver observer
;
1728 TestLayerAnimationObserver removed_observer
;
1729 TestLayerAnimationDelegate delegate
;
1730 animator
->SetDelegate(&delegate
);
1732 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1734 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1735 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1737 sequence
->AddObserver(&observer
);
1738 sequence
->AddObserver(&removed_observer
);
1740 animator
->StartAnimation(sequence
);
1742 EXPECT_EQ(observer
.last_scheduled_sequence(), sequence
);
1743 EXPECT_TRUE(!observer
.last_ended_sequence());
1744 EXPECT_EQ(removed_observer
.last_scheduled_sequence(), sequence
);
1745 EXPECT_TRUE(!removed_observer
.last_ended_sequence());
1747 // This should stop the observer from observing sequence.
1748 animator
->RemoveObserver(&removed_observer
);
1750 base::TimeTicks start_time
= animator
->last_step_time();
1752 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1754 EXPECT_EQ(observer
.last_ended_sequence(), sequence
);
1755 EXPECT_TRUE(!removed_observer
.last_ended_sequence());
1758 TEST(LayerAnimatorTest
, ObserverReleasedBeforeAnimationSequenceEnds
) {
1759 TestLayerAnimationDelegate delegate
;
1760 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1761 animator
->set_disable_timer_for_test(true);
1763 scoped_ptr
<TestLayerAnimationObserver
> observer(
1764 new TestLayerAnimationObserver
);
1765 animator
->SetDelegate(&delegate
);
1766 animator
->AddObserver(observer
.get());
1768 delegate
.SetOpacityFromAnimation(0.0f
);
1770 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1771 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1772 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
));
1774 animator
->StartAnimation(sequence
);
1776 // |observer| should be attached to |sequence|.
1777 EXPECT_TRUE(sequence
->observers_
.might_have_observers());
1779 // Now, release |observer|
1782 // And |sequence| should no longer be attached to |observer|.
1783 EXPECT_FALSE(sequence
->observers_
.might_have_observers());
1786 TEST(LayerAnimatorTest
, ObserverAttachedAfterAnimationStarted
) {
1787 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1788 animator
->set_disable_timer_for_test(true);
1790 TestImplicitAnimationObserver
observer(false);
1791 TestLayerAnimationDelegate delegate
;
1792 animator
->SetDelegate(&delegate
);
1794 delegate
.SetBrightnessFromAnimation(0.0f
);
1797 ScopedLayerAnimationSettings
setter(animator
.get());
1799 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1800 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1801 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1803 animator
->StartAnimation(sequence
);
1804 base::TimeTicks start_time
= animator
->last_step_time();
1805 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1807 setter
.AddObserver(&observer
);
1809 // Start observing an in-flight animation.
1810 sequence
->AddObserver(&observer
);
1812 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1815 EXPECT_TRUE(observer
.animations_completed());
1816 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1817 LayerAnimationElement::BRIGHTNESS
));
1820 TEST(LayerAnimatorTest
, ObserverDetachedBeforeAnimationFinished
) {
1821 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1822 animator
->set_disable_timer_for_test(true);
1824 TestImplicitAnimationObserver
observer(false);
1825 TestLayerAnimationDelegate delegate
;
1826 animator
->SetDelegate(&delegate
);
1828 delegate
.SetBrightnessFromAnimation(0.0f
);
1829 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1830 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1831 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1834 ScopedLayerAnimationSettings
setter(animator
.get());
1835 setter
.AddObserver(&observer
);
1837 animator
->StartAnimation(sequence
);
1838 base::TimeTicks start_time
= animator
->last_step_time();
1839 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1842 EXPECT_FALSE(observer
.animations_completed());
1844 // Stop observing an in-flight animation.
1845 sequence
->RemoveObserver(&observer
);
1847 EXPECT_TRUE(observer
.animations_completed());
1849 // The animation didn't complete, and neither was it aborted.
1850 EXPECT_FALSE(observer
.WasAnimationCompletedForProperty(
1851 LayerAnimationElement::BRIGHTNESS
));
1852 EXPECT_FALSE(observer
.WasAnimationAbortedForProperty(
1853 LayerAnimationElement::BRIGHTNESS
));
1856 // This checks that if an animation is deleted due to a callback, that the
1857 // animator does not try to use the deleted animation. For example, if we have
1858 // two running animations, and the first finishes and the resulting callback
1859 // causes the second to be deleted, we should not attempt to animate the second
1861 TEST(LayerAnimatorTest
, ObserverDeletesAnimationsOnEnd
) {
1862 ScopedAnimationDurationScaleMode
normal_duration_mode(
1863 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
1864 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1865 animator
->set_disable_timer_for_test(true);
1866 TestLayerAnimationDelegate delegate
;
1867 animator
->SetDelegate(&delegate
);
1869 double start_brightness(0.0);
1870 double target_brightness(1.0);
1872 gfx::Rect
start_bounds(0, 0, 50, 50);
1873 gfx::Rect
target_bounds(5, 5, 5, 5);
1875 delegate
.SetBrightnessFromAnimation(start_brightness
);
1876 delegate
.SetBoundsFromAnimation(start_bounds
);
1878 base::TimeDelta brightness_delta
= base::TimeDelta::FromSeconds(1);
1879 base::TimeDelta halfway_delta
= base::TimeDelta::FromSeconds(2);
1880 base::TimeDelta bounds_delta
= base::TimeDelta::FromSeconds(3);
1882 scoped_ptr
<DeletingLayerAnimationObserver
> observer(
1883 new DeletingLayerAnimationObserver(animator
.get()));
1885 animator
->AddObserver(observer
.get());
1887 animator
->StartAnimation(
1888 new LayerAnimationSequence(
1889 LayerAnimationElement::CreateBrightnessElement(
1890 target_brightness
, brightness_delta
)));
1892 animator
->StartAnimation(new LayerAnimationSequence(
1893 LayerAnimationElement::CreateBoundsElement(
1894 target_bounds
, bounds_delta
)));
1895 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1897 base::TimeTicks start_time
= animator
->last_step_time();
1898 animator
->Step(start_time
+ halfway_delta
);
1900 // Completing the brightness animation should have stopped the bounds
1902 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1904 animator
->RemoveObserver(observer
.get());
1907 // Ensure that stopping animation in a bounds change does not crash and that
1908 // animation gets stopped correctly.
1909 // This scenario is possible when animation is restarted from inside a
1910 // callback triggered by the animation progress.
1911 TEST(LayerAnimatorTest
, CallbackDeletesAnimationInProgress
) {
1913 class TestLayerAnimationDeletingDelegate
: public TestLayerAnimationDelegate
{
1915 TestLayerAnimationDeletingDelegate(LayerAnimator
* animator
, int max_width
)
1916 : animator_(animator
),
1917 max_width_(max_width
) {
1920 void SetBoundsFromAnimation(const gfx::Rect
& bounds
) override
{
1921 TestLayerAnimationDelegate::SetBoundsFromAnimation(bounds
);
1922 if (bounds
.width() > max_width_
)
1923 animator_
->StopAnimating();
1926 LayerAnimator
* animator_
;
1928 // Allow copy and assign.
1931 ScopedAnimationDurationScaleMode
normal_duration_mode(
1932 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
1933 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1934 animator
->set_disable_timer_for_test(true);
1935 TestLayerAnimationDeletingDelegate
delegate(animator
.get(), 30);
1936 animator
->SetDelegate(&delegate
);
1938 gfx::Rect
start_bounds(0, 0, 0, 0);
1939 gfx::Rect
target_bounds(5, 5, 50, 50);
1941 delegate
.SetBoundsFromAnimation(start_bounds
);
1943 base::TimeDelta bounds_delta1
= base::TimeDelta::FromMilliseconds(333);
1944 base::TimeDelta bounds_delta2
= base::TimeDelta::FromMilliseconds(666);
1945 base::TimeDelta bounds_delta
= base::TimeDelta::FromMilliseconds(1000);
1946 base::TimeDelta final_delta
= base::TimeDelta::FromMilliseconds(1500);
1948 animator
->StartAnimation(new LayerAnimationSequence(
1949 LayerAnimationElement::CreateBoundsElement(
1950 target_bounds
, bounds_delta
)));
1951 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1953 base::TimeTicks start_time
= animator
->last_step_time();
1954 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ bounds_delta1
));
1955 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1957 // The next step should change the animated bounds past the threshold and
1958 // cause the animaton to stop.
1959 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ bounds_delta2
));
1960 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1961 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ final_delta
));
1963 // Completing the animation should have stopped the bounds
1965 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1968 // Similar to the ObserverDeletesAnimationsOnEnd test above except that it
1969 // tests the behavior when the OnLayerAnimationAborted() callback causes
1970 // all of the animator's other animations to be deleted.
1971 TEST(LayerAnimatorTest
, ObserverDeletesAnimationsOnAbort
) {
1972 ScopedAnimationDurationScaleMode
test_duration_mode(
1973 ScopedAnimationDurationScaleMode::NON_ZERO_DURATION
);
1974 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1975 animator
->set_disable_timer_for_test(true);
1976 TestLayerAnimationDelegate delegate
;
1977 animator
->SetDelegate(&delegate
);
1979 double start_brightness(0.0);
1980 double target_brightness(1.0);
1981 gfx::Rect
start_bounds(0, 0, 50, 50);
1982 gfx::Rect
target_bounds(5, 5, 5, 5);
1983 base::TimeDelta brightness_delta
= base::TimeDelta::FromSeconds(1);
1984 base::TimeDelta bounds_delta
= base::TimeDelta::FromSeconds(2);
1986 delegate
.SetBrightnessFromAnimation(start_brightness
);
1987 delegate
.SetBoundsFromAnimation(start_bounds
);
1989 scoped_ptr
<DeletingLayerAnimationObserver
> observer(
1990 new DeletingLayerAnimationObserver(animator
.get()));
1991 animator
->AddObserver(observer
.get());
1993 animator
->StartAnimation(
1994 new LayerAnimationSequence(
1995 LayerAnimationElement::CreateBrightnessElement(
1996 target_brightness
, brightness_delta
)));
1997 animator
->StartAnimation(new LayerAnimationSequence(
1998 LayerAnimationElement::CreateBoundsElement(
1999 target_bounds
, bounds_delta
)));
2000 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2002 animator
->set_preemption_strategy(
2003 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2004 animator
->StartAnimation(
2005 new LayerAnimationSequence(
2006 LayerAnimationElement::CreateBrightnessElement(
2007 target_brightness
, brightness_delta
)));
2009 // Starting the second brightness animation should have aborted the initial
2010 // brightness animation. |observer| should have stopped the bounds animation
2012 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2014 animator
->RemoveObserver(observer
.get());
2017 // Check that setting a property during an animation with a default animator
2018 // cancels the original animation.
2019 TEST(LayerAnimatorTest
, SettingPropertyDuringAnAnimation
) {
2020 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2021 animator
->set_disable_timer_for_test(true);
2022 TestLayerAnimationDelegate delegate
;
2023 animator
->SetDelegate(&delegate
);
2025 double start_opacity(0.0);
2026 double target_opacity(1.0);
2028 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2030 delegate
.SetOpacityFromAnimation(start_opacity
);
2032 scoped_ptr
<LayerAnimationSequence
> sequence(
2033 new LayerAnimationSequence(
2034 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
2036 animator
->StartAnimation(sequence
.release());
2038 animator
->SetOpacity(0.5);
2040 EXPECT_FALSE(animator
->is_animating());
2041 EXPECT_EQ(0.5, animator
->GetTargetOpacity());
2044 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
2045 // second sequence to be leaked.
2046 TEST(LayerAnimatorTest
, ImmediatelySettingNewTargetDoesNotLeak
) {
2047 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2048 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
2049 animator
->set_disable_timer_for_test(true);
2050 TestLayerAnimationDelegate delegate
;
2051 animator
->SetDelegate(&delegate
);
2053 gfx::Rect
start_bounds(0, 0, 50, 50);
2054 gfx::Rect
middle_bounds(10, 10, 100, 100);
2055 gfx::Rect
target_bounds(5, 5, 5, 5);
2057 delegate
.SetBoundsFromAnimation(start_bounds
);
2060 // start an implicit bounds animation.
2061 ScopedLayerAnimationSettings
settings(animator
.get());
2062 animator
->SetBounds(middle_bounds
);
2065 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2067 int num_live_instances
= 0;
2068 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2069 scoped_ptr
<TestLayerAnimationSequence
> sequence(
2070 new TestLayerAnimationSequence(
2071 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
),
2072 &num_live_instances
));
2074 EXPECT_EQ(1, num_live_instances
);
2076 // This should interrupt the running sequence causing us to immediately set
2077 // the target value. The sequence should alse be destructed.
2078 animator
->StartAnimation(sequence
.release());
2080 EXPECT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2081 EXPECT_EQ(0, num_live_instances
);
2082 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
2085 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
2086 TEST(LayerAnimatorTest
, GetTargetOpacity
) {
2087 TestLayerAnimationDelegate delegate
;
2088 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2089 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2090 animator
->set_disable_timer_for_test(true);
2091 animator
->SetDelegate(&delegate
);
2093 delegate
.SetOpacityFromAnimation(0.0);
2096 ScopedLayerAnimationSettings
settings(animator
.get());
2097 animator
->SetOpacity(0.5);
2098 EXPECT_EQ(0.5, animator
->GetTargetOpacity());
2100 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2101 animator
->SetOpacity(1.0);
2102 EXPECT_EQ(1.0, animator
->GetTargetOpacity());
2106 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
2107 TEST(LayerAnimatorTest
, GetTargetBrightness
) {
2108 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2109 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2110 animator
->set_disable_timer_for_test(true);
2111 TestLayerAnimationDelegate delegate
;
2112 animator
->SetDelegate(&delegate
);
2114 delegate
.SetBrightnessFromAnimation(0.0);
2117 ScopedLayerAnimationSettings
settings(animator
.get());
2118 animator
->SetBrightness(0.5);
2119 EXPECT_EQ(0.5, animator
->GetTargetBrightness());
2121 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2122 animator
->SetBrightness(1.0);
2123 EXPECT_EQ(1.0, animator
->GetTargetBrightness());
2127 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
2128 TEST(LayerAnimatorTest
, GetTargetGrayscale
) {
2129 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2130 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2131 animator
->set_disable_timer_for_test(true);
2132 TestLayerAnimationDelegate delegate
;
2133 animator
->SetDelegate(&delegate
);
2135 delegate
.SetGrayscaleFromAnimation(0.0);
2138 ScopedLayerAnimationSettings
settings(animator
.get());
2139 animator
->SetGrayscale(0.5);
2140 EXPECT_EQ(0.5, animator
->GetTargetGrayscale());
2142 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2143 animator
->SetGrayscale(1.0);
2144 EXPECT_EQ(1.0, animator
->GetTargetGrayscale());
2148 // Verifies color property is modified appropriately.
2149 TEST(LayerAnimatorTest
, Color
) {
2150 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2151 animator
->set_disable_timer_for_test(true);
2152 TestLayerAnimationDelegate delegate
;
2153 animator
->SetDelegate(&delegate
);
2155 SkColor start_color
= SkColorSetARGB( 64, 20, 40, 60);
2156 SkColor middle_color
= SkColorSetARGB(128, 35, 70, 120);
2157 SkColor target_color
= SkColorSetARGB(192, 40, 80, 140);
2159 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2161 delegate
.SetColorFromAnimation(start_color
);
2163 animator
->ScheduleAnimation(
2164 new LayerAnimationSequence(
2165 LayerAnimationElement::CreateColorElement(target_color
, delta
)));
2167 EXPECT_TRUE(animator
->is_animating());
2168 EXPECT_EQ(ColorToString(start_color
),
2169 ColorToString(delegate
.GetColorForAnimation()));
2171 base::TimeTicks start_time
= animator
->last_step_time();
2173 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
2175 EXPECT_TRUE(animator
->is_animating());
2176 EXPECT_EQ(ColorToString(middle_color
),
2177 ColorToString(delegate
.GetColorForAnimation()));
2179 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
2181 EXPECT_FALSE(animator
->is_animating());
2182 EXPECT_EQ(ColorToString(target_color
),
2183 ColorToString(delegate
.GetColorForAnimation()));
2186 // Verifies SchedulePauseForProperties().
2187 TEST(LayerAnimatorTest
, SchedulePauseForProperties
) {
2188 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2189 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2190 animator
->SchedulePauseForProperties(
2191 base::TimeDelta::FromMilliseconds(100),
2192 LayerAnimationElement::TRANSFORM
| LayerAnimationElement::BOUNDS
);
2193 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::TRANSFORM
));
2194 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2195 EXPECT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::OPACITY
));
2199 class AnimatorOwner
{
2202 : animator_(LayerAnimator::CreateDefaultAnimator()) {
2205 LayerAnimator
* animator() { return animator_
.get(); }
2208 scoped_refptr
<LayerAnimator
> animator_
;
2210 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner
);
2213 class DeletingObserver
: public LayerAnimationObserver
{
2215 DeletingObserver(bool* was_deleted
)
2216 : animator_owner_(new AnimatorOwner
),
2217 delete_on_animation_ended_(false),
2218 delete_on_animation_aborted_(false),
2219 delete_on_animation_scheduled_(false),
2220 was_deleted_(was_deleted
) {
2221 animator()->AddObserver(this);
2224 ~DeletingObserver() override
{
2225 animator()->RemoveObserver(this);
2226 *was_deleted_
= true;
2229 LayerAnimator
* animator() { return animator_owner_
->animator(); }
2231 bool delete_on_animation_ended() const {
2232 return delete_on_animation_ended_
;
2234 void set_delete_on_animation_ended(bool enabled
) {
2235 delete_on_animation_ended_
= enabled
;
2238 bool delete_on_animation_aborted() const {
2239 return delete_on_animation_aborted_
;
2241 void set_delete_on_animation_aborted(bool enabled
) {
2242 delete_on_animation_aborted_
= enabled
;
2245 bool delete_on_animation_scheduled() const {
2246 return delete_on_animation_scheduled_
;
2248 void set_delete_on_animation_scheduled(bool enabled
) {
2249 delete_on_animation_scheduled_
= enabled
;
2252 // LayerAnimationObserver implementation.
2253 void OnLayerAnimationEnded(LayerAnimationSequence
* sequence
) override
{
2254 if (delete_on_animation_ended_
)
2258 void OnLayerAnimationAborted(LayerAnimationSequence
* sequence
) override
{
2259 if (delete_on_animation_aborted_
)
2263 void OnLayerAnimationScheduled(LayerAnimationSequence
* sequence
) override
{
2264 if (delete_on_animation_scheduled_
)
2269 scoped_ptr
<AnimatorOwner
> animator_owner_
;
2270 bool delete_on_animation_ended_
;
2271 bool delete_on_animation_aborted_
;
2272 bool delete_on_animation_scheduled_
;
2275 DISALLOW_COPY_AND_ASSIGN(DeletingObserver
);
2278 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterFinishingAnimation
) {
2279 bool observer_was_deleted
= false;
2280 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2281 observer
->set_delete_on_animation_ended(true);
2282 observer
->set_delete_on_animation_aborted(true);
2283 LayerAnimator
* animator
= observer
->animator();
2284 animator
->set_disable_timer_for_test(true);
2285 TestLayerAnimationDelegate delegate
;
2286 animator
->SetDelegate(&delegate
);
2288 delegate
.SetBrightnessFromAnimation(0.0f
);
2290 gfx::Rect
start_bounds(0, 0, 50, 50);
2291 gfx::Rect
target_bounds(10, 10, 100, 100);
2293 delegate
.SetBoundsFromAnimation(start_bounds
);
2295 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2296 LayerAnimationSequence
* brightness_sequence
= new LayerAnimationSequence(
2297 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
2298 animator
->StartAnimation(brightness_sequence
);
2300 delta
= base::TimeDelta::FromSeconds(2);
2301 LayerAnimationSequence
* bounds_sequence
= new LayerAnimationSequence(
2302 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
2303 animator
->StartAnimation(bounds_sequence
);
2305 base::TimeTicks start_time
= animator
->last_step_time();
2306 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
2308 EXPECT_TRUE(observer_was_deleted
);
2311 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterStoppingAnimating
) {
2312 bool observer_was_deleted
= false;
2313 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2314 observer
->set_delete_on_animation_ended(true);
2315 observer
->set_delete_on_animation_aborted(true);
2316 LayerAnimator
* animator
= observer
->animator();
2317 animator
->set_disable_timer_for_test(true);
2318 TestLayerAnimationDelegate delegate
;
2319 animator
->SetDelegate(&delegate
);
2321 delegate
.SetOpacityFromAnimation(0.0f
);
2323 gfx::Rect
start_bounds(0, 0, 50, 50);
2324 gfx::Rect
target_bounds(10, 10, 100, 100);
2326 delegate
.SetBoundsFromAnimation(start_bounds
);
2328 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2329 LayerAnimationSequence
* opacity_sequence
= new LayerAnimationSequence(
2330 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
));
2331 animator
->StartAnimation(opacity_sequence
);
2333 delta
= base::TimeDelta::FromSeconds(2);
2334 LayerAnimationSequence
* bounds_sequence
= new LayerAnimationSequence(
2335 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
2336 animator
->StartAnimation(bounds_sequence
);
2338 animator
->StopAnimating();
2340 EXPECT_TRUE(observer_was_deleted
);
2343 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterScheduling
) {
2344 bool observer_was_deleted
= false;
2345 TestLayerAnimationDelegate delegate
;
2346 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2347 observer
->set_delete_on_animation_scheduled(true);
2348 LayerAnimator
* animator
= observer
->animator();
2349 animator
->set_disable_timer_for_test(true);
2350 animator
->SetDelegate(&delegate
);
2352 delegate
.SetOpacityFromAnimation(0.0f
);
2354 gfx::Rect
start_bounds(0, 0, 50, 50);
2355 gfx::Rect
target_bounds(10, 10, 100, 100);
2357 delegate
.SetBoundsFromAnimation(start_bounds
);
2359 std::vector
<LayerAnimationSequence
*> to_start
;
2361 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2362 to_start
.push_back(new LayerAnimationSequence(
2363 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2365 delta
= base::TimeDelta::FromSeconds(2);
2366 to_start
.push_back(new LayerAnimationSequence(
2367 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
2369 animator
->ScheduleTogether(to_start
);
2371 EXPECT_TRUE(observer_was_deleted
);
2374 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterAborted
) {
2375 bool observer_was_deleted
= false;
2376 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2377 TestLayerAnimationDelegate delegate
;
2378 observer
->set_delete_on_animation_aborted(true);
2379 LayerAnimator
* animator
= observer
->animator();
2380 animator
->set_preemption_strategy(
2381 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2382 animator
->set_disable_timer_for_test(true);
2383 animator
->SetDelegate(&delegate
);
2385 delegate
.SetOpacityFromAnimation(0.0f
);
2387 gfx::Rect
start_bounds(0, 0, 50, 50);
2388 gfx::Rect
target_bounds(10, 10, 100, 100);
2390 delegate
.SetBoundsFromAnimation(start_bounds
);
2392 std::vector
<LayerAnimationSequence
*> to_start
;
2394 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2395 to_start
.push_back(new LayerAnimationSequence(
2396 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2398 delta
= base::TimeDelta::FromSeconds(2);
2399 to_start
.push_back(new LayerAnimationSequence(
2400 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
2402 animator
->ScheduleTogether(to_start
);
2404 EXPECT_FALSE(observer_was_deleted
);
2406 animator
->StartAnimation(new LayerAnimationSequence(
2407 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2409 EXPECT_TRUE(observer_was_deleted
);
2413 TEST(LayerAnimatorTest
, TestSetterRespectEnqueueStrategy
) {
2414 TestLayerAnimationDelegate delegate
;
2415 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2416 animator
->set_disable_timer_for_test(true);
2418 animator
->SetDelegate(&delegate
);
2420 float start_opacity
= 0.0f
;
2421 float target_opacity
= 1.0f
;
2422 float magic_opacity
= 0.123f
;
2424 delegate
.SetOpacityFromAnimation(start_opacity
);
2426 ScopedLayerAnimationSettings
settings(animator
.get());
2427 settings
.SetPreemptionStrategy(
2428 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2429 settings
.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2430 animator
->SetOpacity(target_opacity
);
2432 EXPECT_EQ(start_opacity
, delegate
.GetOpacityForAnimation());
2434 settings
.SetPreemptionStrategy(
2435 LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2436 settings
.SetTransitionDuration(base::TimeDelta());
2437 animator
->SetOpacity(magic_opacity
);
2439 EXPECT_EQ(start_opacity
, delegate
.GetOpacityForAnimation());
2442 TEST(LayerAnimatorTest
, TestScopedCounterAnimation
) {
2443 Layer parent
, child
;
2446 gfx::Transform parent_begin
, parent_end
;
2448 parent_end
.Scale3d(2.0, 0.5, 1.0);
2450 // Parent animates from identity to the end value. The counter animation will
2451 // start at the end value and animate back to identity.
2452 gfx::Transform
child_begin(parent_end
);
2454 child
.SetTransform(child_begin
);
2455 parent
.SetTransform(parent_begin
);
2457 EXPECT_FALSE(child
.GetAnimator()->is_animating());
2459 ScopedLayerAnimationSettings
settings(parent
.GetAnimator());
2460 settings
.SetInverselyAnimatedBaseLayer(&parent
);
2461 settings
.AddInverselyAnimatedLayer(&child
);
2463 parent
.SetTransform(parent_end
);
2465 EXPECT_TRUE(child
.GetAnimator()->is_animating());
2466 EXPECT_TRUE(child
.GetTargetTransform().IsIdentity())
2467 << child
.GetTargetTransform().ToString();
2471 class CollectionLayerAnimationDelegate
: public TestLayerAnimationDelegate
{
2473 CollectionLayerAnimationDelegate() : collection(NULL
) {}
2474 ~CollectionLayerAnimationDelegate() override
{}
2476 // LayerAnimationDelegate:
2477 LayerAnimatorCollection
* GetLayerAnimatorCollection() override
{
2482 LayerAnimatorCollection collection
;
2485 TEST(LayerAnimatorTest
, LayerAnimatorCollectionTickTime
) {
2487 LayerAnimator
* animator
= layer
.GetAnimator();
2488 CollectionLayerAnimationDelegate delegate
;
2489 animator
->SetDelegate(&delegate
);
2491 LayerAnimatorCollection
* collection
= delegate
.GetLayerAnimatorCollection();
2492 base::TimeTicks null
;
2493 collection
->OnAnimationStep(null
);
2494 EXPECT_TRUE(collection
->last_tick_time().is_null());
2496 // Adding an animator to the collection should update the last tick time.
2497 collection
->StartAnimator(layer
.GetAnimator());
2498 EXPECT_TRUE(collection
->HasActiveAnimators());
2499 EXPECT_FALSE(collection
->last_tick_time().is_null());
2501 collection
->StopAnimator(layer
.GetAnimator());
2502 EXPECT_FALSE(collection
->HasActiveAnimators());
2505 TEST(LayerAnimatorTest
, AnimatorStartedCorrectly
) {
2507 LayerAnimatorTestController
test_controller(layer
.GetAnimator());
2508 LayerAnimator
* animator
= test_controller
.animator();
2509 ASSERT_FALSE(animator
->is_started_
);
2511 TestLayerAnimationDelegate test_delegate
;
2512 animator
->SetDelegate(&test_delegate
);
2513 double target_opacity
= 1.0;
2514 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2515 animator
->ScheduleAnimation(new LayerAnimationSequence(
2516 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2517 EXPECT_FALSE(animator
->is_started_
);
2519 CollectionLayerAnimationDelegate collection_delegate
;
2520 animator
->SetDelegate(&collection_delegate
);
2521 animator
->UpdateAnimationState();
2522 EXPECT_TRUE(animator
->is_started_
);
2523 animator
->SetDelegate(NULL
);
2526 TEST(LayerAnimatorTest
, AnimatorRemovedFromCollectionWhenLayerIsDestroyed
) {
2527 scoped_ptr
<Layer
> layer(new Layer(LAYER_TEXTURED
));
2528 LayerAnimatorTestController
test_controller(layer
->GetAnimator());
2529 scoped_refptr
<LayerAnimator
> animator
= test_controller
.animator();
2530 CollectionLayerAnimationDelegate collection_delegate
;
2531 animator
->SetDelegate(&collection_delegate
);
2533 double target_opacity
= 1.0;
2534 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2535 animator
->ScheduleAnimation(new LayerAnimationSequence(
2536 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2539 collection_delegate
.GetLayerAnimatorCollection()->HasActiveAnimators());
2542 EXPECT_EQ(NULL
, animator
->delegate());
2544 collection_delegate
.GetLayerAnimatorCollection()->HasActiveAnimators());
2547 TEST(LayerAnimatorTest
, LayerMovedBetweenCompositorsDuringAnimation
) {
2548 bool enable_pixel_output
= false;
2549 ui::ContextFactory
* context_factory
=
2550 InitializeContextFactoryForTests(enable_pixel_output
);
2551 const gfx::Rect
bounds(10, 10, 100, 100);
2552 scoped_ptr
<TestCompositorHost
> host_1(
2553 TestCompositorHost::Create(bounds
, context_factory
));
2554 scoped_ptr
<TestCompositorHost
> host_2(
2555 TestCompositorHost::Create(bounds
, context_factory
));
2559 Compositor
* compositor_1
= host_1
->GetCompositor();
2561 compositor_1
->SetRootLayer(&root_1
);
2563 Compositor
* compositor_2
= host_2
->GetCompositor();
2565 compositor_2
->SetRootLayer(&root_2
);
2567 // Verify that neither compositor has active animators.
2568 EXPECT_FALSE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2569 EXPECT_FALSE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2573 LayerAnimator
* animator
= layer
.GetAnimator();
2574 double target_opacity
= 1.0;
2575 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2576 animator
->ScheduleAnimation(new LayerAnimationSequence(
2577 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2578 EXPECT_TRUE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2579 EXPECT_FALSE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2582 EXPECT_FALSE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2583 EXPECT_TRUE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2586 TerminateContextFactoryForTests();
2589 class LayerOwnerAnimationObserver
: public LayerAnimationObserver
{
2591 LayerOwnerAnimationObserver(LayerAnimator
* animator
)
2592 : animator_layer_(new Layer(LAYER_TEXTURED
)) {
2593 animator_layer_
->SetAnimator(animator
);
2596 ~LayerOwnerAnimationObserver() override
{}
2598 void OnLayerAnimationEnded(LayerAnimationSequence
* sequence
) override
{
2599 ASSERT_TRUE(sequence
);
2600 animator_layer_
.reset();
2603 void OnLayerAnimationAborted(LayerAnimationSequence
* sequence
) override
{
2604 ASSERT_TRUE(sequence
);
2605 animator_layer_
.reset();
2608 LayerAnimationDelegate
* animator_layer() {
2609 return animator_layer_
.get();
2612 void OnLayerAnimationScheduled(LayerAnimationSequence
* sequence
) override
{}
2615 scoped_ptr
<Layer
> animator_layer_
;
2617 DISALLOW_COPY_AND_ASSIGN(LayerOwnerAnimationObserver
);
2620 TEST(LayerAnimatorTest
, ObserverDeletesLayerInStopAnimating
) {
2621 scoped_refptr
<LayerAnimator
> animator(
2622 LayerAnimator::CreateImplicitAnimator());
2623 animator
->set_disable_timer_for_test(true);
2624 LayerOwnerAnimationObserver
observer(animator
.get());
2625 LayerAnimationDelegate
* delegate
= observer
.animator_layer();
2627 const gfx::Rect
start_bounds(0, 0, 50, 50);
2628 const gfx::Rect
target_bounds(10, 10, 100, 100);
2629 const double target_opacity
= 1.0;
2631 delegate
->SetOpacityFromAnimation(0.0f
);
2632 delegate
->SetBoundsFromAnimation(start_bounds
);
2634 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2635 LayerAnimationSequence
* opacity
= new LayerAnimationSequence(
2636 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
));
2637 opacity
->AddObserver(&observer
);
2638 animator
->ScheduleAnimation(opacity
);
2639 time_delta
= base::TimeDelta::FromSeconds(2);
2640 LayerAnimationSequence
* move
= new LayerAnimationSequence(
2641 LayerAnimationElement::CreateBoundsElement(target_bounds
, time_delta
));
2642 animator
->ScheduleAnimation(move
);
2643 EXPECT_TRUE(animator
->is_animating());
2644 animator
->Step(animator
->last_step_time() +
2645 base::TimeDelta::FromMilliseconds(500));
2646 animator
->StopAnimating();
2648 EXPECT_EQ(nullptr, observer
.animator_layer());
2649 EXPECT_TRUE(animator
->is_animating());