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/frame_time.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/gfx/transform.h"
34 // Converts |color| to a string. Each component of the color is separated by a
35 // space and the order if A R G B.
36 std::string
ColorToString(SkColor color
) {
37 return base::StringPrintf("%d %d %d %d", SkColorGetA(color
),
38 SkColorGetR(color
), SkColorGetG(color
),
42 // Creates vector with two LayerAnimationSequences, based on |first| and
43 // |second| layer animation elements.
44 std::vector
<LayerAnimationSequence
*> CreateMultiSequence(
45 LayerAnimationElement
* first
,
46 LayerAnimationElement
* second
) {
47 LayerAnimationSequence
* first_sequence
= new LayerAnimationSequence();
48 first_sequence
->AddElement(first
);
49 LayerAnimationSequence
* second_sequence
= new LayerAnimationSequence();
50 second_sequence
->AddElement(second
);
52 std::vector
<ui::LayerAnimationSequence
*> animations
;
53 animations
.push_back(first_sequence
);
54 animations
.push_back(second_sequence
);
58 class TestImplicitAnimationObserver
: public ImplicitAnimationObserver
{
60 explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed
)
61 : animations_completed_(false),
62 notify_when_animator_destructed_(notify_when_animator_destructed
) {
65 bool animations_completed() const { return animations_completed_
; }
66 void set_animations_completed(bool completed
) {
67 animations_completed_
= completed
;
70 bool WasAnimationAbortedForProperty(
71 LayerAnimationElement::AnimatableProperty property
) const {
72 return ImplicitAnimationObserver::WasAnimationAbortedForProperty(property
);
75 bool WasAnimationCompletedForProperty(
76 LayerAnimationElement::AnimatableProperty property
) const {
77 return ImplicitAnimationObserver::WasAnimationCompletedForProperty(
82 // ImplicitAnimationObserver implementation
83 void OnImplicitAnimationsCompleted() override
{
84 animations_completed_
= true;
87 bool RequiresNotificationWhenAnimatorDestroyed() const override
{
88 return notify_when_animator_destructed_
;
91 bool animations_completed_
;
92 bool notify_when_animator_destructed_
;
94 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver
);
97 // When notified that an animation has ended, stops all other animations.
98 class DeletingLayerAnimationObserver
: public LayerAnimationObserver
{
100 DeletingLayerAnimationObserver(LayerAnimator
* animator
)
101 : animator_(animator
) {
104 void OnLayerAnimationEnded(LayerAnimationSequence
* sequence
) override
{
105 animator_
->StopAnimating();
108 void OnLayerAnimationAborted(LayerAnimationSequence
* sequence
) override
{
109 animator_
->StopAnimating();
112 void OnLayerAnimationScheduled(LayerAnimationSequence
* sequence
) override
{}
115 LayerAnimator
* animator_
;
117 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver
);
120 class LayerAnimatorDestructionObserver
{
122 LayerAnimatorDestructionObserver() : animator_deleted_(false) {}
123 virtual ~LayerAnimatorDestructionObserver() {}
125 void NotifyAnimatorDeleted() {
126 animator_deleted_
= true;
129 bool IsAnimatorDeleted() {
130 return animator_deleted_
;
134 bool animator_deleted_
;
136 DISALLOW_COPY_AND_ASSIGN(LayerAnimatorDestructionObserver
);
139 class TestLayerAnimator
: public LayerAnimator
{
141 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)),
142 destruction_observer_(NULL
) {}
144 void SetDestructionObserver(
145 LayerAnimatorDestructionObserver
* observer
) {
146 destruction_observer_
= observer
;
150 ~TestLayerAnimator() override
{
151 if (destruction_observer_
) {
152 destruction_observer_
->NotifyAnimatorDeleted();
156 void ProgressAnimation(LayerAnimationSequence
* sequence
,
157 base::TimeTicks now
) override
{
158 EXPECT_TRUE(HasAnimation(sequence
));
159 LayerAnimator::ProgressAnimation(sequence
, now
);
163 LayerAnimatorDestructionObserver
* destruction_observer_
;
165 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
168 // The test layer animation sequence updates a live instances count when it is
169 // created and destroyed.
170 class TestLayerAnimationSequence
: public LayerAnimationSequence
{
172 TestLayerAnimationSequence(LayerAnimationElement
* element
,
173 int* num_live_instances
)
174 : LayerAnimationSequence(element
),
175 num_live_instances_(num_live_instances
) {
176 (*num_live_instances_
)++;
179 ~TestLayerAnimationSequence() override
{ (*num_live_instances_
)--; }
182 int* num_live_instances_
;
184 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence
);
189 // Checks that setting a property on an implicit animator causes an animation to
191 TEST(LayerAnimatorTest
, ImplicitAnimation
) {
192 scoped_refptr
<LayerAnimator
> animator(
193 LayerAnimator::CreateImplicitAnimator());
194 animator
->set_disable_timer_for_test(true);
195 TestLayerAnimationDelegate delegate
;
196 animator
->SetDelegate(&delegate
);
197 base::TimeTicks now
= gfx::FrameTime::Now();
198 animator
->SetBrightness(0.5);
199 EXPECT_TRUE(animator
->is_animating());
200 animator
->Step(now
+ base::TimeDelta::FromSeconds(1));
201 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), 0.5);
204 // Checks that if the animator is a default animator, that implicit animations
206 TEST(LayerAnimatorTest
, NoImplicitAnimation
) {
207 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
208 animator
->set_disable_timer_for_test(true);
209 TestLayerAnimationDelegate delegate
;
210 animator
->SetDelegate(&delegate
);
211 animator
->SetBrightness(0.5);
212 EXPECT_FALSE(animator
->is_animating());
213 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), 0.5);
216 // Checks that StopAnimatingProperty stops animation for that property, and also
217 // skips the stopped animation to the end.
218 TEST(LayerAnimatorTest
, StopAnimatingProperty
) {
219 scoped_refptr
<LayerAnimator
> animator(
220 LayerAnimator::CreateImplicitAnimator());
221 animator
->set_disable_timer_for_test(true);
222 TestLayerAnimationDelegate delegate
;
223 animator
->SetDelegate(&delegate
);
224 double target_opacity(0.5);
225 gfx::Rect
target_bounds(0, 0, 50, 50);
226 animator
->SetOpacity(target_opacity
);
227 animator
->SetBounds(target_bounds
);
228 animator
->StopAnimatingProperty(LayerAnimationElement::OPACITY
);
229 EXPECT_TRUE(animator
->is_animating());
230 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), 0.5);
231 animator
->StopAnimatingProperty(LayerAnimationElement::BOUNDS
);
232 EXPECT_FALSE(animator
->is_animating());
233 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
236 // Checks that multiple running animation for separate properties can be stopped
237 // simultaneously and that all animations are advanced to their target values.
238 TEST(LayerAnimatorTest
, StopAnimating
) {
239 scoped_refptr
<LayerAnimator
> animator(
240 LayerAnimator::CreateImplicitAnimator());
241 animator
->set_disable_timer_for_test(true);
242 TestLayerAnimationDelegate delegate
;
243 animator
->SetDelegate(&delegate
);
244 double target_opacity(0.5);
245 gfx::Rect
target_bounds(0, 0, 50, 50);
246 animator
->SetOpacity(target_opacity
);
247 animator
->SetBounds(target_bounds
);
248 EXPECT_TRUE(animator
->is_animating());
249 animator
->StopAnimating();
250 EXPECT_FALSE(animator
->is_animating());
251 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), 0.5);
252 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
255 // Checks that multiple running animation for separate properties can be stopped
256 // simultaneously and that all animations are advanced to their target values.
257 TEST(LayerAnimatorTest
, AbortAllAnimations
) {
258 scoped_refptr
<LayerAnimator
> animator(
259 LayerAnimator::CreateImplicitAnimator());
260 animator
->set_disable_timer_for_test(true);
261 TestLayerAnimationDelegate delegate
;
262 double initial_opacity(1.0);
263 gfx::Rect
initial_bounds(0, 0, 10, 10);
264 delegate
.SetOpacityFromAnimation(initial_opacity
);
265 delegate
.SetBoundsFromAnimation(initial_bounds
);
266 animator
->SetDelegate(&delegate
);
267 double target_opacity(0.5);
268 gfx::Rect
target_bounds(0, 0, 50, 50);
269 animator
->SetOpacity(target_opacity
);
270 animator
->SetBounds(target_bounds
);
271 EXPECT_TRUE(animator
->is_animating());
272 animator
->AbortAllAnimations();
273 EXPECT_FALSE(animator
->is_animating());
274 EXPECT_FLOAT_EQ(initial_opacity
, delegate
.GetOpacityForAnimation());
275 CheckApproximatelyEqual(initial_bounds
, delegate
.GetBoundsForAnimation());
278 // Schedule a non-threaded animation that can run immediately. This is the
279 // trivial case and should result in the animation being started immediately.
280 TEST(LayerAnimatorTest
, ScheduleAnimationThatCanRunImmediately
) {
281 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
282 animator
->set_disable_timer_for_test(true);
283 TestLayerAnimationDelegate delegate
;
284 animator
->SetDelegate(&delegate
);
286 double start_brightness(0.0);
287 double middle_brightness(0.5);
288 double target_brightness(1.0);
290 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
292 delegate
.SetBrightnessFromAnimation(start_brightness
);
294 animator
->ScheduleAnimation(
295 new LayerAnimationSequence(
296 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
299 EXPECT_TRUE(animator
->is_animating());
300 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
302 base::TimeTicks start_time
= animator
->last_step_time();
304 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
306 EXPECT_TRUE(animator
->is_animating());
307 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
309 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
311 EXPECT_FALSE(animator
->is_animating());
312 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
315 // Schedule a threaded animation that can run immediately.
316 TEST(LayerAnimatorTest
, ScheduleThreadedAnimationThatCanRunImmediately
) {
317 double epsilon
= 0.00001;
318 LayerAnimatorTestController
test_controller(
319 LayerAnimator::CreateDefaultAnimator());
320 LayerAnimator
* animator
= test_controller
.animator();
321 test_controller
.animator()->set_disable_timer_for_test(true);
322 TestLayerAnimationDelegate delegate
;
323 test_controller
.animator()->SetDelegate(&delegate
);
325 double start_opacity(0.0);
326 double target_opacity(1.0);
328 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
330 delegate
.SetOpacityFromAnimation(start_opacity
);
332 test_controller
.animator()->ScheduleAnimation(
333 new LayerAnimationSequence(
334 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
336 EXPECT_TRUE(test_controller
.animator()->is_animating());
337 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
339 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
340 base::TimeTicks effective_start
= start_time
+ delta
;
342 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
343 cc::AnimationEvent::Started
,
345 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
346 ->animation_group_id(),
347 cc::Animation::Opacity
,
350 animator
->Step(effective_start
+ delta
/ 2);
352 EXPECT_TRUE(test_controller
.animator()->is_animating());
355 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
356 last_progressed_fraction(),
359 animator
->Step(effective_start
+ delta
);
361 EXPECT_FALSE(test_controller
.animator()->is_animating());
362 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
365 // Schedule two non-threaded animations on separate properties. Both animations
366 // should start immediately and should progress in lock step.
367 TEST(LayerAnimatorTest
, ScheduleTwoAnimationsThatCanRunImmediately
) {
368 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
369 animator
->set_disable_timer_for_test(true);
370 TestLayerAnimationDelegate delegate
;
371 animator
->SetDelegate(&delegate
);
373 double start_brightness(0.0);
374 double middle_brightness(0.5);
375 double target_brightness(1.0);
377 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
378 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
379 start_bounds
.set_x(-90);
380 target_bounds
.set_x(90);
382 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
384 delegate
.SetBrightnessFromAnimation(start_brightness
);
385 delegate
.SetBoundsFromAnimation(start_bounds
);
387 animator
->ScheduleAnimation(
388 new LayerAnimationSequence(
389 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
392 animator
->ScheduleAnimation(
393 new LayerAnimationSequence(
394 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
396 EXPECT_TRUE(animator
->is_animating());
397 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
398 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
400 base::TimeTicks start_time
= animator
->last_step_time();
402 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
404 EXPECT_TRUE(animator
->is_animating());
405 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
406 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), middle_bounds
);
408 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
410 EXPECT_FALSE(animator
->is_animating());
411 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
412 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
415 // Schedule a threaded and a non-threaded animation on separate properties. Both
416 // animations should progress in lock step.
417 TEST(LayerAnimatorTest
, ScheduleThreadedAndNonThreadedAnimations
) {
418 double epsilon
= 0.00001;
419 LayerAnimatorTestController
test_controller(
420 LayerAnimator::CreateDefaultAnimator());
421 LayerAnimator
* animator
= test_controller
.animator();
422 test_controller
.animator()->set_disable_timer_for_test(true);
423 TestLayerAnimationDelegate delegate
;
424 test_controller
.animator()->SetDelegate(&delegate
);
426 double start_opacity(0.0);
427 double target_opacity(1.0);
429 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
430 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
431 start_bounds
.set_x(-90);
432 target_bounds
.set_x(90);
434 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
436 delegate
.SetOpacityFromAnimation(start_opacity
);
437 delegate
.SetBoundsFromAnimation(start_bounds
);
439 std::vector
<LayerAnimationSequence
*> animations
;
440 animations
.push_back(
441 new LayerAnimationSequence(
442 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
444 animations
.push_back(
445 new LayerAnimationSequence(
446 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
448 test_controller
.animator()->ScheduleTogether(animations
);
450 EXPECT_TRUE(test_controller
.animator()->is_animating());
451 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
452 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
454 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
455 base::TimeTicks effective_start
= start_time
+ delta
;
457 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
458 cc::AnimationEvent::Started
,
460 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
461 ->animation_group_id(),
462 cc::Animation::Opacity
,
465 animator
->Step(effective_start
+ delta
/ 2);
467 EXPECT_TRUE(test_controller
.animator()->is_animating());
470 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
471 last_progressed_fraction(),
473 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), middle_bounds
);
475 animator
->Step(effective_start
+ delta
);
477 EXPECT_FALSE(test_controller
.animator()->is_animating());
478 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
479 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
482 // Schedule two animations on the same property. In this case, the two
483 // animations should run one after another.
484 TEST(LayerAnimatorTest
, ScheduleTwoAnimationsOnSameProperty
) {
485 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
486 animator
->set_disable_timer_for_test(true);
487 TestLayerAnimationDelegate delegate
;
488 animator
->SetDelegate(&delegate
);
490 double start_brightness(0.0);
491 double middle_brightness(0.5);
492 double target_brightness(1.0);
494 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
496 delegate
.SetBrightnessFromAnimation(start_brightness
);
498 animator
->ScheduleAnimation(
499 new LayerAnimationSequence(
500 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
503 animator
->ScheduleAnimation(
504 new LayerAnimationSequence(
505 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
508 EXPECT_TRUE(animator
->is_animating());
509 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
511 base::TimeTicks start_time
= animator
->last_step_time();
513 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
515 EXPECT_TRUE(animator
->is_animating());
516 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
518 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
520 EXPECT_TRUE(animator
->is_animating());
521 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
523 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
525 EXPECT_TRUE(animator
->is_animating());
526 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
528 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
530 EXPECT_FALSE(animator
->is_animating());
531 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
534 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
535 // is, ensure that all animations targetting a particular property are run in
537 TEST(LayerAnimatorTest
, ScheduleBlockedAnimation
) {
538 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
539 animator
->set_disable_timer_for_test(true);
540 TestLayerAnimationDelegate delegate
;
541 animator
->SetDelegate(&delegate
);
543 double start_grayscale(0.0);
544 double middle_grayscale(0.5);
545 double target_grayscale(1.0);
547 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
548 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
549 start_bounds
.set_x(-90);
550 target_bounds
.set_x(90);
552 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
554 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
555 delegate
.SetBoundsFromAnimation(start_bounds
);
557 animator
->ScheduleAnimation(
558 new LayerAnimationSequence(
559 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
562 scoped_ptr
<LayerAnimationSequence
> bounds_and_grayscale(
563 new LayerAnimationSequence(
564 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
,
567 bounds_and_grayscale
->AddElement(
568 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
570 animator
->ScheduleAnimation(bounds_and_grayscale
.release());
572 animator
->ScheduleAnimation(
573 new LayerAnimationSequence(
574 LayerAnimationElement::CreateBoundsElement(start_bounds
, delta
)));
576 EXPECT_TRUE(animator
->is_animating());
577 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
578 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
580 base::TimeTicks start_time
= animator
->last_step_time();
582 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
584 EXPECT_TRUE(animator
->is_animating());
585 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
586 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
588 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
590 EXPECT_TRUE(animator
->is_animating());
591 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
592 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
594 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
596 EXPECT_TRUE(animator
->is_animating());
597 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
598 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
600 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(3000));
602 EXPECT_TRUE(animator
->is_animating());
603 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
604 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
606 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(4000));
608 EXPECT_FALSE(animator
->is_animating());
609 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
610 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
613 // Schedule {g} and then schedule {g} and {b} together. In this case, since
614 // ScheduleTogether is being used, the bounds animation should not start until
615 // the second grayscale animation starts.
616 TEST(LayerAnimatorTest
, ScheduleTogether
) {
617 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
618 animator
->set_disable_timer_for_test(true);
619 TestLayerAnimationDelegate delegate
;
620 animator
->SetDelegate(&delegate
);
622 double start_grayscale(0.0);
623 double target_grayscale(1.0);
625 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
626 start_bounds
= target_bounds
= gfx::Rect(0, 0, 50, 50);
627 start_bounds
.set_x(-90);
628 target_bounds
.set_x(90);
630 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
632 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
633 delegate
.SetBoundsFromAnimation(start_bounds
);
635 animator
->ScheduleAnimation(
636 new LayerAnimationSequence(
637 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
640 std::vector
<LayerAnimationSequence
*> sequences
;
641 sequences
.push_back(new LayerAnimationSequence(
642 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
)));
643 sequences
.push_back(new LayerAnimationSequence(
644 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
646 animator
->ScheduleTogether(sequences
);
648 EXPECT_TRUE(animator
->is_animating());
649 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
650 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
652 base::TimeTicks start_time
= animator
->last_step_time();
654 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
656 EXPECT_TRUE(animator
->is_animating());
657 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
658 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
660 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
662 EXPECT_FALSE(animator
->is_animating());
663 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
664 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
667 // Start non-threaded animation (that can run immediately). This is the trivial
668 // case (see the trival case for ScheduleAnimation).
669 TEST(LayerAnimatorTest
, StartAnimationThatCanRunImmediately
) {
670 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
671 animator
->set_disable_timer_for_test(true);
672 TestLayerAnimationDelegate delegate
;
673 animator
->SetDelegate(&delegate
);
675 double start_brightness(0.0);
676 double middle_brightness(0.5);
677 double target_brightness(1.0);
679 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
681 delegate
.SetBrightnessFromAnimation(start_brightness
);
683 animator
->StartAnimation(
684 new LayerAnimationSequence(
685 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
688 EXPECT_TRUE(animator
->is_animating());
689 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
691 base::TimeTicks start_time
= animator
->last_step_time();
693 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
695 EXPECT_TRUE(animator
->is_animating());
696 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
698 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
700 EXPECT_FALSE(animator
->is_animating());
701 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
704 // Start threaded animation (that can run immediately).
705 TEST(LayerAnimatorTest
, StartThreadedAnimationThatCanRunImmediately
) {
706 double epsilon
= 0.00001;
707 LayerAnimatorTestController
test_controller(
708 LayerAnimator::CreateDefaultAnimator());
709 LayerAnimator
* animator
= test_controller
.animator();
710 test_controller
.animator()->set_disable_timer_for_test(true);
711 TestLayerAnimationDelegate delegate
;
712 test_controller
.animator()->SetDelegate(&delegate
);
714 double start_opacity(0.0);
715 double target_opacity(1.0);
717 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
719 delegate
.SetOpacityFromAnimation(start_opacity
);
721 test_controller
.animator()->StartAnimation(
722 new LayerAnimationSequence(
723 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
725 EXPECT_TRUE(test_controller
.animator()->is_animating());
726 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
728 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
729 base::TimeTicks effective_start
= start_time
+ delta
;
731 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
732 cc::AnimationEvent::Started
,
734 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
735 ->animation_group_id(),
736 cc::Animation::Opacity
,
739 animator
->Step(effective_start
+ delta
/ 2);
741 EXPECT_TRUE(test_controller
.animator()->is_animating());
744 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
745 last_progressed_fraction(),
748 animator
->Step(effective_start
+ delta
);
749 EXPECT_FALSE(test_controller
.animator()->is_animating());
750 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
753 // Preempt by immediately setting new target.
754 TEST(LayerAnimatorTest
, PreemptBySettingNewTarget
) {
755 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
756 animator
->set_disable_timer_for_test(true);
757 TestLayerAnimationDelegate delegate
;
758 animator
->SetDelegate(&delegate
);
760 double start_opacity(0.0);
761 double target_opacity(1.0);
763 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
765 delegate
.SetOpacityFromAnimation(start_opacity
);
767 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
769 animator
->StartAnimation(
770 new LayerAnimationSequence(
771 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
773 animator
->StartAnimation(
774 new LayerAnimationSequence(
775 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
777 EXPECT_FALSE(animator
->is_animating());
778 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
781 // Preempt by animating to new target, with a non-threaded animation.
782 TEST(LayerAnimatorTest
, PreemptByImmediatelyAnimatingToNewTarget
) {
783 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
784 animator
->set_disable_timer_for_test(true);
785 TestLayerAnimationDelegate delegate
;
786 animator
->SetDelegate(&delegate
);
788 double start_brightness(0.0);
789 double middle_brightness(0.5);
790 double target_brightness(1.0);
792 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
794 delegate
.SetBrightnessFromAnimation(start_brightness
);
796 animator
->set_preemption_strategy(
797 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
799 animator
->StartAnimation(
800 new LayerAnimationSequence(
801 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
804 base::TimeTicks start_time
= animator
->last_step_time();
806 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
808 animator
->StartAnimation(
809 new LayerAnimationSequence(
810 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
813 EXPECT_TRUE(animator
->is_animating());
814 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
816 animator
->StartAnimation(
817 new LayerAnimationSequence(
818 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
821 EXPECT_TRUE(animator
->is_animating());
823 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
825 EXPECT_TRUE(animator
->is_animating());
826 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
827 0.5 * (start_brightness
+ middle_brightness
));
829 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
831 EXPECT_FALSE(animator
->is_animating());
832 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
835 // Preempt by animating to new target, with a threaded animation.
836 TEST(LayerAnimatorTest
, PreemptThreadedByImmediatelyAnimatingToNewTarget
) {
837 double epsilon
= 0.00001;
838 LayerAnimatorTestController
test_controller(
839 LayerAnimator::CreateDefaultAnimator());
840 LayerAnimator
* animator
= test_controller
.animator();
841 test_controller
.animator()->set_disable_timer_for_test(true);
842 TestLayerAnimationDelegate delegate
;
843 test_controller
.animator()->SetDelegate(&delegate
);
845 double start_opacity(0.0);
846 double middle_opacity(0.5);
847 double target_opacity(1.0);
849 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
851 delegate
.SetOpacityFromAnimation(start_opacity
);
853 test_controller
.animator()->set_preemption_strategy(
854 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
856 test_controller
.animator()->StartAnimation(
857 new LayerAnimationSequence(
858 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
860 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
861 base::TimeTicks effective_start
= start_time
+ delta
;
863 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
864 cc::AnimationEvent::Started
,
866 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
867 ->animation_group_id(),
868 cc::Animation::Opacity
,
871 animator
->Step(effective_start
+ delta
/ 2);
873 test_controller
.animator()->StartAnimation(
874 new LayerAnimationSequence(
875 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
877 EXPECT_TRUE(test_controller
.animator()->is_animating());
878 EXPECT_NEAR(delegate
.GetOpacityForAnimation(), middle_opacity
, epsilon
);
880 test_controller
.animator()->StartAnimation(
881 new LayerAnimationSequence(
882 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
884 EXPECT_TRUE(test_controller
.animator()->is_animating());
886 base::TimeTicks second_effective_start
= effective_start
+ delta
;
888 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
889 cc::AnimationEvent::Started
,
891 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
892 ->animation_group_id(),
893 cc::Animation::Opacity
,
894 second_effective_start
));
896 animator
->Step(second_effective_start
+ delta
/ 2);
898 EXPECT_TRUE(test_controller
.animator()->is_animating());
901 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
902 last_progressed_fraction(),
905 animator
->Step(second_effective_start
+ delta
);
907 EXPECT_FALSE(test_controller
.animator()->is_animating());
908 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
911 // Preempt by enqueuing the new animation.
912 TEST(LayerAnimatorTest
, PreemptEnqueueNewAnimation
) {
913 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
914 animator
->set_disable_timer_for_test(true);
915 TestLayerAnimationDelegate delegate
;
916 animator
->SetDelegate(&delegate
);
918 double start_brightness(0.0);
919 double middle_brightness(0.5);
920 double target_brightness(1.0);
922 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
924 delegate
.SetBrightnessFromAnimation(start_brightness
);
926 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
928 animator
->StartAnimation(
929 new LayerAnimationSequence(
930 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
933 base::TimeTicks start_time
= animator
->last_step_time();
935 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
937 animator
->StartAnimation(
938 new LayerAnimationSequence(
939 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
942 EXPECT_TRUE(animator
->is_animating());
943 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
945 EXPECT_TRUE(animator
->is_animating());
947 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
949 EXPECT_TRUE(animator
->is_animating());
950 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
952 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
954 EXPECT_TRUE(animator
->is_animating());
955 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
957 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
959 EXPECT_FALSE(animator
->is_animating());
960 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
963 // Start an animation when there are sequences waiting in the queue. In this
964 // case, all pending and running animations should be finished, and the new
965 // animation started.
966 TEST(LayerAnimatorTest
, PreemptyByReplacingQueuedAnimations
) {
967 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
968 animator
->set_disable_timer_for_test(true);
969 TestLayerAnimationDelegate delegate
;
970 animator
->SetDelegate(&delegate
);
972 double start_brightness(0.0);
973 double middle_brightness(0.5);
974 double target_brightness(1.0);
976 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
978 delegate
.SetBrightnessFromAnimation(start_brightness
);
980 animator
->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
982 animator
->StartAnimation(
983 new LayerAnimationSequence(
984 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
987 base::TimeTicks start_time
= animator
->last_step_time();
989 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
991 animator
->StartAnimation(
992 new LayerAnimationSequence(
993 LayerAnimationElement::CreateBrightnessElement(middle_brightness
,
996 // Queue should now have two animations. Starting a third should replace the
998 animator
->StartAnimation(
999 new LayerAnimationSequence(
1000 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1003 EXPECT_TRUE(animator
->is_animating());
1004 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1006 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1008 EXPECT_TRUE(animator
->is_animating());
1009 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1011 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1013 EXPECT_TRUE(animator
->is_animating());
1014 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1016 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1018 EXPECT_FALSE(animator
->is_animating());
1019 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1022 TEST(LayerAnimatorTest
, StartTogetherSetsLastStepTime
) {
1023 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1024 animator
->set_disable_timer_for_test(true);
1025 TestLayerAnimationDelegate delegate
;
1026 animator
->SetDelegate(&delegate
);
1028 double start_grayscale(0.0);
1029 double target_grayscale(1.0);
1030 double start_brightness(0.1);
1031 double target_brightness(0.9);
1033 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1035 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1036 delegate
.SetBrightnessFromAnimation(start_brightness
);
1038 animator
->set_preemption_strategy(
1039 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1041 animator
->set_last_step_time(base::TimeTicks());
1043 animator
->StartTogether(
1044 CreateMultiSequence(
1045 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1047 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1051 // If last step time was not set correctly, the resulting delta should be
1052 // miniscule (fractions of a millisecond). If set correctly, then the delta
1053 // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1054 // though a much smaller value would probably have sufficed.
1055 delta
= gfx::FrameTime::Now() - animator
->last_step_time();
1056 EXPECT_GT(60.0, delta
.InSecondsF());
1059 //-------------------------------------------------------
1060 // Preempt by immediately setting new target.
1061 TEST(LayerAnimatorTest
, MultiPreemptBySettingNewTarget
) {
1062 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1063 animator
->set_disable_timer_for_test(true);
1064 TestLayerAnimationDelegate delegate
;
1065 animator
->SetDelegate(&delegate
);
1067 double start_opacity(0.0);
1068 double target_opacity(1.0);
1069 double start_brightness(0.1);
1070 double target_brightness(0.9);
1072 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1074 delegate
.SetOpacityFromAnimation(start_opacity
);
1075 delegate
.SetBrightnessFromAnimation(start_brightness
);
1077 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
1079 animator
->StartTogether(
1080 CreateMultiSequence(
1081 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
),
1082 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1086 animator
->StartTogether(
1087 CreateMultiSequence(
1088 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1089 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1093 EXPECT_FALSE(animator
->is_animating());
1094 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1095 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1098 // Preempt by animating to new target.
1099 TEST(LayerAnimatorTest
, MultiPreemptByImmediatelyAnimatingToNewTarget
) {
1100 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1101 animator
->set_disable_timer_for_test(true);
1102 TestLayerAnimationDelegate delegate
;
1103 animator
->SetDelegate(&delegate
);
1105 double start_grayscale(0.0);
1106 double middle_grayscale(0.5);
1107 double target_grayscale(1.0);
1109 double start_brightness(0.1);
1110 double middle_brightness(0.2);
1111 double target_brightness(0.3);
1113 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1115 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1116 delegate
.SetBrightnessFromAnimation(start_brightness
);
1118 animator
->set_preemption_strategy(
1119 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1121 animator
->StartTogether(
1122 CreateMultiSequence(
1123 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1125 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1129 base::TimeTicks start_time
= animator
->last_step_time();
1131 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1133 animator
->StartTogether(
1134 CreateMultiSequence(
1135 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1136 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1139 EXPECT_TRUE(animator
->is_animating());
1140 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1141 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1143 animator
->StartTogether(
1144 CreateMultiSequence(
1145 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1146 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1149 EXPECT_TRUE(animator
->is_animating());
1151 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1153 EXPECT_TRUE(animator
->is_animating());
1154 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(),
1155 0.5 * (start_grayscale
+ middle_grayscale
));
1156 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
1157 0.5 * (start_brightness
+ middle_brightness
));
1159 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1161 EXPECT_FALSE(animator
->is_animating());
1162 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1163 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1166 // Preempt a threaded animation by animating to new target.
1167 TEST(LayerAnimatorTest
, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget
) {
1168 double epsilon
= 0.00001;
1169 LayerAnimatorTestController
test_controller(
1170 LayerAnimator::CreateDefaultAnimator());
1171 LayerAnimator
* animator
= test_controller
.animator();
1172 test_controller
.animator()->set_disable_timer_for_test(true);
1173 TestLayerAnimationDelegate delegate
;
1174 test_controller
.animator()->SetDelegate(&delegate
);
1176 double start_opacity(0.0);
1177 double middle_opacity(0.5);
1178 double target_opacity(1.0);
1180 double start_brightness(0.1);
1181 double middle_brightness(0.2);
1182 double target_brightness(0.3);
1184 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1186 delegate
.SetOpacityFromAnimation(start_opacity
);
1187 delegate
.SetBrightnessFromAnimation(start_brightness
);
1189 test_controller
.animator()->set_preemption_strategy(
1190 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1192 test_controller
.animator()->StartTogether(
1193 CreateMultiSequence(
1194 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
),
1195 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1199 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
1200 base::TimeTicks effective_start
= start_time
+ delta
;
1202 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1203 cc::AnimationEvent::Started
,
1205 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1206 ->animation_group_id(),
1207 cc::Animation::Opacity
,
1210 animator
->Step(effective_start
+ delta
/ 2);
1212 test_controller
.animator()->StartTogether(
1213 CreateMultiSequence(
1214 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1215 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1218 EXPECT_TRUE(test_controller
.animator()->is_animating());
1219 EXPECT_NEAR(delegate
.GetOpacityForAnimation(), middle_opacity
, epsilon
);
1220 EXPECT_NEAR(delegate
.GetBrightnessForAnimation(), middle_brightness
, epsilon
);
1222 test_controller
.animator()->StartTogether(
1223 CreateMultiSequence(
1224 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1225 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1228 EXPECT_TRUE(test_controller
.animator()->is_animating());
1230 base::TimeTicks second_effective_start
= effective_start
+ delta
;
1232 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1233 cc::AnimationEvent::Started
,
1235 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1236 ->animation_group_id(),
1237 cc::Animation::Opacity
,
1238 second_effective_start
));
1240 animator
->Step(second_effective_start
+ delta
/ 2);
1242 EXPECT_TRUE(test_controller
.animator()->is_animating());
1245 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
1246 last_progressed_fraction(),
1248 EXPECT_NEAR(delegate
.GetBrightnessForAnimation(),
1249 0.5 * (start_brightness
+ middle_brightness
),
1252 animator
->Step(second_effective_start
+ delta
);
1254 EXPECT_FALSE(test_controller
.animator()->is_animating());
1255 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1256 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1259 // Preempt by enqueuing the new animation.
1260 TEST(LayerAnimatorTest
, MultiPreemptEnqueueNewAnimation
) {
1261 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1262 animator
->set_disable_timer_for_test(true);
1263 TestLayerAnimationDelegate delegate
;
1264 animator
->SetDelegate(&delegate
);
1266 double start_grayscale(0.0);
1267 double middle_grayscale(0.5);
1268 double target_grayscale(1.0);
1270 double start_brightness(0.1);
1271 double middle_brightness(0.2);
1272 double target_brightness(0.3);
1274 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1276 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1277 delegate
.SetBrightnessFromAnimation(start_brightness
);
1279 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
1281 animator
->StartTogether(
1282 CreateMultiSequence(
1283 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1285 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1288 base::TimeTicks start_time
= animator
->last_step_time();
1290 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1292 animator
->StartTogether(
1293 CreateMultiSequence(
1294 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1295 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1298 EXPECT_TRUE(animator
->is_animating());
1299 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1300 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1302 EXPECT_TRUE(animator
->is_animating());
1304 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1306 EXPECT_TRUE(animator
->is_animating());
1307 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
1308 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1310 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1312 EXPECT_TRUE(animator
->is_animating());
1313 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1314 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1316 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1318 EXPECT_FALSE(animator
->is_animating());
1319 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1320 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1323 // Start an animation when there are sequences waiting in the queue. In this
1324 // case, all pending and running animations should be finished, and the new
1325 // animation started.
1326 TEST(LayerAnimatorTest
, MultiPreemptByReplacingQueuedAnimations
) {
1327 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1328 animator
->set_disable_timer_for_test(true);
1329 TestLayerAnimationDelegate delegate
;
1330 animator
->SetDelegate(&delegate
);
1332 double start_grayscale(0.0);
1333 double middle_grayscale(0.5);
1334 double target_grayscale(1.0);
1336 double start_brightness(0.1);
1337 double middle_brightness(0.2);
1338 double target_brightness(0.3);
1340 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1342 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1343 delegate
.SetBrightnessFromAnimation(start_brightness
);
1345 animator
->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
1347 animator
->StartTogether(
1348 CreateMultiSequence(
1349 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1351 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1354 base::TimeTicks start_time
= animator
->last_step_time();
1356 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1358 animator
->StartTogether(
1359 CreateMultiSequence(
1360 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale
,
1362 LayerAnimationElement::CreateBrightnessElement(middle_brightness
,
1365 // Queue should now have two animations. Starting a third should replace the
1367 animator
->StartTogether(
1368 CreateMultiSequence(
1369 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1370 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1373 EXPECT_TRUE(animator
->is_animating());
1374 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1375 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1377 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1379 EXPECT_TRUE(animator
->is_animating());
1380 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
1381 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1383 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1385 EXPECT_TRUE(animator
->is_animating());
1386 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1387 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1389 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1391 EXPECT_FALSE(animator
->is_animating());
1392 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1393 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1395 //-------------------------------------------------------
1396 // Test that non-threaded cyclic sequences continue to animate.
1397 TEST(LayerAnimatorTest
, CyclicSequences
) {
1398 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1399 animator
->set_disable_timer_for_test(true);
1400 TestLayerAnimationDelegate delegate
;
1401 animator
->SetDelegate(&delegate
);
1403 double start_brightness(0.0);
1404 double target_brightness(1.0);
1406 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1408 delegate
.SetBrightnessFromAnimation(start_brightness
);
1410 scoped_ptr
<LayerAnimationSequence
> sequence(
1411 new LayerAnimationSequence(
1412 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1415 sequence
->AddElement(
1416 LayerAnimationElement::CreateBrightnessElement(start_brightness
, delta
));
1418 sequence
->set_is_cyclic(true);
1420 animator
->StartAnimation(sequence
.release());
1422 base::TimeTicks start_time
= animator
->last_step_time();
1424 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1426 EXPECT_TRUE(animator
->is_animating());
1427 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1429 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1431 EXPECT_TRUE(animator
->is_animating());
1432 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1434 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(3000));
1436 EXPECT_TRUE(animator
->is_animating());
1437 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1439 // Skip ahead by a lot.
1440 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000000000));
1442 EXPECT_TRUE(animator
->is_animating());
1443 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1445 // Skip ahead by a lot.
1446 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000001000));
1448 EXPECT_TRUE(animator
->is_animating());
1449 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1451 animator
->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS
);
1453 EXPECT_FALSE(animator
->is_animating());
1456 // Test that threaded cyclic sequences continue to animate.
1457 TEST(LayerAnimatorTest
, ThreadedCyclicSequences
) {
1458 LayerAnimatorTestController
test_controller(
1459 LayerAnimator::CreateDefaultAnimator());
1460 LayerAnimator
* animator
= test_controller
.animator();
1461 test_controller
.animator()->set_disable_timer_for_test(true);
1462 TestLayerAnimationDelegate delegate
;
1463 test_controller
.animator()->SetDelegate(&delegate
);
1465 double start_opacity(0.0);
1466 double target_opacity(1.0);
1468 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1470 delegate
.SetOpacityFromAnimation(start_opacity
);
1472 scoped_ptr
<LayerAnimationSequence
> sequence(
1473 new LayerAnimationSequence(
1474 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
1476 sequence
->AddElement(
1477 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
));
1479 sequence
->set_is_cyclic(true);
1481 test_controller
.animator()->StartAnimation(sequence
.release());
1483 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
1484 base::TimeTicks effective_start
= start_time
+ delta
;
1486 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1487 cc::AnimationEvent::Started
,
1489 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1490 ->animation_group_id(),
1491 cc::Animation::Opacity
,
1494 animator
->Step(effective_start
+ delta
);
1495 EXPECT_TRUE(test_controller
.animator()->is_animating());
1496 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1498 base::TimeTicks second_effective_start
= effective_start
+ 2 * delta
;
1499 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1500 cc::AnimationEvent::Started
,
1502 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1503 ->animation_group_id(),
1504 cc::Animation::Opacity
,
1505 second_effective_start
));
1507 animator
->Step(second_effective_start
+ delta
);
1509 EXPECT_TRUE(test_controller
.animator()->is_animating());
1510 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1512 base::TimeTicks third_effective_start
= second_effective_start
+ 2 * delta
;
1513 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1514 cc::AnimationEvent::Started
,
1516 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1517 ->animation_group_id(),
1518 cc::Animation::Opacity
,
1519 third_effective_start
));
1521 animator
->Step(third_effective_start
+ delta
);
1522 EXPECT_TRUE(test_controller
.animator()->is_animating());
1523 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1525 base::TimeTicks fourth_effective_start
= third_effective_start
+ 2 * delta
;
1526 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1527 cc::AnimationEvent::Started
,
1529 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1530 ->animation_group_id(),
1531 cc::Animation::Opacity
,
1532 fourth_effective_start
));
1534 // Skip ahead by a lot.
1535 animator
->Step(fourth_effective_start
+ 1000 * delta
);
1537 EXPECT_TRUE(test_controller
.animator()->is_animating());
1538 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1540 base::TimeTicks fifth_effective_start
= fourth_effective_start
+ 1001 * delta
;
1541 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1542 cc::AnimationEvent::Started
,
1544 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1545 ->animation_group_id(),
1546 cc::Animation::Opacity
,
1547 fifth_effective_start
));
1549 // Skip ahead by a lot.
1550 animator
->Step(fifth_effective_start
+ 999 * delta
);
1552 EXPECT_TRUE(test_controller
.animator()->is_animating());
1553 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1555 test_controller
.animator()->StopAnimatingProperty(
1556 LayerAnimationElement::OPACITY
);
1558 EXPECT_FALSE(test_controller
.animator()->is_animating());
1561 TEST(LayerAnimatorTest
, AddObserverExplicit
) {
1562 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1563 animator
->set_disable_timer_for_test(true);
1564 TestLayerAnimationObserver observer
;
1565 TestLayerAnimationDelegate delegate
;
1566 animator
->SetDelegate(&delegate
);
1567 animator
->AddObserver(&observer
);
1568 observer
.set_requires_notification_when_animator_destroyed(true);
1570 EXPECT_TRUE(!observer
.last_ended_sequence());
1572 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1574 delegate
.SetBrightnessFromAnimation(0.0f
);
1576 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1577 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1579 animator
->StartAnimation(sequence
);
1581 EXPECT_EQ(observer
.last_scheduled_sequence(), sequence
);
1583 base::TimeTicks start_time
= animator
->last_step_time();
1585 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1587 EXPECT_EQ(observer
.last_ended_sequence(), sequence
);
1589 // |sequence| has been destroyed. Recreate it to test abort.
1590 sequence
= new LayerAnimationSequence(
1591 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1593 animator
->StartAnimation(sequence
);
1597 EXPECT_EQ(observer
.last_aborted_sequence(), sequence
);
1600 // Tests that an observer added to a scoped settings object is still notified
1601 // when the object goes out of scope.
1602 TEST(LayerAnimatorTest
, ImplicitAnimationObservers
) {
1603 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1604 animator
->set_disable_timer_for_test(true);
1605 TestImplicitAnimationObserver
observer(false);
1606 TestLayerAnimationDelegate delegate
;
1607 animator
->SetDelegate(&delegate
);
1609 EXPECT_FALSE(observer
.animations_completed());
1610 animator
->SetBrightness(1.0f
);
1613 ScopedLayerAnimationSettings
settings(animator
.get());
1614 settings
.AddObserver(&observer
);
1615 animator
->SetBrightness(0.0f
);
1618 EXPECT_FALSE(observer
.animations_completed());
1619 base::TimeTicks start_time
= animator
->last_step_time();
1620 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1621 EXPECT_TRUE(observer
.animations_completed());
1622 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1623 LayerAnimationElement::BRIGHTNESS
));
1624 EXPECT_FLOAT_EQ(0.0f
, delegate
.GetBrightnessForAnimation());
1627 // Tests that an observer added to a scoped settings object is still notified
1628 // when the object goes out of scope due to the animation being interrupted.
1629 TEST(LayerAnimatorTest
, InterruptedImplicitAnimationObservers
) {
1630 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1631 animator
->set_disable_timer_for_test(true);
1632 TestImplicitAnimationObserver
observer(false);
1633 TestLayerAnimationDelegate delegate
;
1634 animator
->SetDelegate(&delegate
);
1636 EXPECT_FALSE(observer
.animations_completed());
1637 animator
->SetBrightness(1.0f
);
1640 ScopedLayerAnimationSettings
settings(animator
.get());
1641 settings
.AddObserver(&observer
);
1642 animator
->SetBrightness(0.0f
);
1645 EXPECT_FALSE(observer
.animations_completed());
1646 // This should interrupt the implicit animation causing the observer to be
1647 // notified immediately.
1648 animator
->SetBrightness(1.0f
);
1649 EXPECT_TRUE(observer
.animations_completed());
1650 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1651 LayerAnimationElement::BRIGHTNESS
));
1652 EXPECT_FLOAT_EQ(1.0f
, delegate
.GetBrightnessForAnimation());
1655 // Tests that LayerAnimator is not deleted after the animation completes as long
1656 // as there is a live ScopedLayerAnimationSettings object wrapped around it.
1657 TEST(LayerAnimatorTest
, AnimatorKeptAliveBySettings
) {
1658 // Note we are using a raw pointer unlike in other tests.
1659 TestLayerAnimator
* animator
= new TestLayerAnimator();
1660 LayerAnimatorDestructionObserver destruction_observer
;
1661 animator
->SetDestructionObserver(&destruction_observer
);
1662 animator
->set_disable_timer_for_test(true);
1663 TestLayerAnimationDelegate delegate
;
1664 animator
->SetDelegate(&delegate
);
1666 // ScopedLayerAnimationSettings should keep the Animator alive as long as
1667 // it is alive, even beyond the end of the animation.
1668 ScopedLayerAnimationSettings
settings(animator
);
1669 base::TimeTicks now
= gfx::FrameTime::Now();
1670 animator
->SetBrightness(0.5);
1671 animator
->Step(now
+ base::TimeDelta::FromSeconds(1));
1672 EXPECT_FALSE(destruction_observer
.IsAnimatorDeleted());
1674 // ScopedLayerAnimationSettings was destroyed, so Animator should be deleted.
1675 EXPECT_TRUE(destruction_observer
.IsAnimatorDeleted());
1678 // Tests that an observer added to a scoped settings object is not notified
1679 // when the animator is destroyed unless explicitly requested.
1680 TEST(LayerAnimatorTest
, ImplicitObserversAtAnimatorDestruction
) {
1681 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1682 animator
->set_disable_timer_for_test(true);
1683 TestImplicitAnimationObserver
observer_notify(true);
1684 TestImplicitAnimationObserver
observer_do_not_notify(false);
1685 TestLayerAnimationDelegate delegate
;
1686 animator
->SetDelegate(&delegate
);
1688 EXPECT_FALSE(observer_notify
.animations_completed());
1689 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1691 animator
->SetBrightness(1.0f
);
1694 ScopedLayerAnimationSettings
settings(animator
.get());
1695 settings
.AddObserver(&observer_notify
);
1696 settings
.AddObserver(&observer_do_not_notify
);
1697 animator
->SetBrightness(0.0f
);
1700 EXPECT_FALSE(observer_notify
.animations_completed());
1701 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1703 EXPECT_TRUE(observer_notify
.animations_completed());
1704 EXPECT_TRUE(observer_notify
.WasAnimationAbortedForProperty(
1705 LayerAnimationElement::BRIGHTNESS
));
1706 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1709 TEST(LayerAnimatorTest
, AbortedAnimationStatusInImplicitObservers
) {
1710 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1711 animator
->set_disable_timer_for_test(true);
1712 TestImplicitAnimationObserver
observer(false);
1713 TestLayerAnimationDelegate delegate
;
1714 animator
->SetDelegate(&delegate
);
1716 EXPECT_FALSE(observer
.animations_completed());
1717 animator
->SetBrightness(1.0f
);
1720 ScopedLayerAnimationSettings
settings(animator
.get());
1721 settings
.AddObserver(&observer
);
1722 animator
->SetBrightness(0.0f
);
1724 EXPECT_FALSE(observer
.animations_completed());
1726 animator
->AbortAllAnimations();
1727 EXPECT_TRUE(observer
.animations_completed());
1728 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1729 LayerAnimationElement::BRIGHTNESS
));
1730 EXPECT_FALSE(observer
.WasAnimationAbortedForProperty(
1731 LayerAnimationElement::OPACITY
));
1733 observer
.set_animations_completed(false);
1735 ScopedLayerAnimationSettings
settings(animator
.get());
1736 settings
.AddObserver(&observer
);
1737 animator
->SetOpacity(0.0f
);
1739 EXPECT_FALSE(observer
.animations_completed());
1741 animator
->AbortAllAnimations();
1742 EXPECT_TRUE(observer
.animations_completed());
1743 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1744 LayerAnimationElement::BRIGHTNESS
));
1745 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1746 LayerAnimationElement::OPACITY
));
1749 TEST(LayerAnimatorTest
, RemoveObserverShouldRemoveFromSequences
) {
1750 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1751 animator
->set_disable_timer_for_test(true);
1752 TestLayerAnimationObserver observer
;
1753 TestLayerAnimationObserver removed_observer
;
1754 TestLayerAnimationDelegate delegate
;
1755 animator
->SetDelegate(&delegate
);
1757 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1759 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1760 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1762 sequence
->AddObserver(&observer
);
1763 sequence
->AddObserver(&removed_observer
);
1765 animator
->StartAnimation(sequence
);
1767 EXPECT_EQ(observer
.last_scheduled_sequence(), sequence
);
1768 EXPECT_TRUE(!observer
.last_ended_sequence());
1769 EXPECT_EQ(removed_observer
.last_scheduled_sequence(), sequence
);
1770 EXPECT_TRUE(!removed_observer
.last_ended_sequence());
1772 // This should stop the observer from observing sequence.
1773 animator
->RemoveObserver(&removed_observer
);
1775 base::TimeTicks start_time
= animator
->last_step_time();
1777 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1779 EXPECT_EQ(observer
.last_ended_sequence(), sequence
);
1780 EXPECT_TRUE(!removed_observer
.last_ended_sequence());
1783 TEST(LayerAnimatorTest
, ObserverReleasedBeforeAnimationSequenceEnds
) {
1784 TestLayerAnimationDelegate delegate
;
1785 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1786 animator
->set_disable_timer_for_test(true);
1788 scoped_ptr
<TestLayerAnimationObserver
> observer(
1789 new TestLayerAnimationObserver
);
1790 animator
->SetDelegate(&delegate
);
1791 animator
->AddObserver(observer
.get());
1793 delegate
.SetOpacityFromAnimation(0.0f
);
1795 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1796 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1797 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
));
1799 animator
->StartAnimation(sequence
);
1801 // |observer| should be attached to |sequence|.
1802 EXPECT_TRUE(sequence
->observers_
.might_have_observers());
1804 // Now, release |observer|
1807 // And |sequence| should no longer be attached to |observer|.
1808 EXPECT_FALSE(sequence
->observers_
.might_have_observers());
1811 TEST(LayerAnimatorTest
, ObserverAttachedAfterAnimationStarted
) {
1812 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1813 animator
->set_disable_timer_for_test(true);
1815 TestImplicitAnimationObserver
observer(false);
1816 TestLayerAnimationDelegate delegate
;
1817 animator
->SetDelegate(&delegate
);
1819 delegate
.SetBrightnessFromAnimation(0.0f
);
1822 ScopedLayerAnimationSettings
setter(animator
.get());
1824 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1825 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1826 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1828 animator
->StartAnimation(sequence
);
1829 base::TimeTicks start_time
= animator
->last_step_time();
1830 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1832 setter
.AddObserver(&observer
);
1834 // Start observing an in-flight animation.
1835 sequence
->AddObserver(&observer
);
1837 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1840 EXPECT_TRUE(observer
.animations_completed());
1841 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1842 LayerAnimationElement::BRIGHTNESS
));
1845 TEST(LayerAnimatorTest
, ObserverDetachedBeforeAnimationFinished
) {
1846 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1847 animator
->set_disable_timer_for_test(true);
1849 TestImplicitAnimationObserver
observer(false);
1850 TestLayerAnimationDelegate delegate
;
1851 animator
->SetDelegate(&delegate
);
1853 delegate
.SetBrightnessFromAnimation(0.0f
);
1854 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1855 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1856 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1859 ScopedLayerAnimationSettings
setter(animator
.get());
1860 setter
.AddObserver(&observer
);
1862 animator
->StartAnimation(sequence
);
1863 base::TimeTicks start_time
= animator
->last_step_time();
1864 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1867 EXPECT_FALSE(observer
.animations_completed());
1869 // Stop observing an in-flight animation.
1870 sequence
->RemoveObserver(&observer
);
1872 EXPECT_TRUE(observer
.animations_completed());
1874 // The animation didn't complete, and neither was it aborted.
1875 EXPECT_FALSE(observer
.WasAnimationCompletedForProperty(
1876 LayerAnimationElement::BRIGHTNESS
));
1877 EXPECT_FALSE(observer
.WasAnimationAbortedForProperty(
1878 LayerAnimationElement::BRIGHTNESS
));
1881 // This checks that if an animation is deleted due to a callback, that the
1882 // animator does not try to use the deleted animation. For example, if we have
1883 // two running animations, and the first finishes and the resulting callback
1884 // causes the second to be deleted, we should not attempt to animate the second
1886 TEST(LayerAnimatorTest
, ObserverDeletesAnimationsOnEnd
) {
1887 ScopedAnimationDurationScaleMode
normal_duration_mode(
1888 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
1889 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1890 animator
->set_disable_timer_for_test(true);
1891 TestLayerAnimationDelegate delegate
;
1892 animator
->SetDelegate(&delegate
);
1894 double start_brightness(0.0);
1895 double target_brightness(1.0);
1897 gfx::Rect
start_bounds(0, 0, 50, 50);
1898 gfx::Rect
target_bounds(5, 5, 5, 5);
1900 delegate
.SetBrightnessFromAnimation(start_brightness
);
1901 delegate
.SetBoundsFromAnimation(start_bounds
);
1903 base::TimeDelta brightness_delta
= base::TimeDelta::FromSeconds(1);
1904 base::TimeDelta halfway_delta
= base::TimeDelta::FromSeconds(2);
1905 base::TimeDelta bounds_delta
= base::TimeDelta::FromSeconds(3);
1907 scoped_ptr
<DeletingLayerAnimationObserver
> observer(
1908 new DeletingLayerAnimationObserver(animator
.get()));
1910 animator
->AddObserver(observer
.get());
1912 animator
->StartAnimation(
1913 new LayerAnimationSequence(
1914 LayerAnimationElement::CreateBrightnessElement(
1915 target_brightness
, brightness_delta
)));
1917 animator
->StartAnimation(new LayerAnimationSequence(
1918 LayerAnimationElement::CreateBoundsElement(
1919 target_bounds
, bounds_delta
)));
1920 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1922 base::TimeTicks start_time
= animator
->last_step_time();
1923 animator
->Step(start_time
+ halfway_delta
);
1925 // Completing the brightness animation should have stopped the bounds
1927 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1929 animator
->RemoveObserver(observer
.get());
1932 // Ensure that stopping animation in a bounds change does not crash and that
1933 // animation gets stopped correctly.
1934 // This scenario is possible when animation is restarted from inside a
1935 // callback triggered by the animation progress.
1936 TEST(LayerAnimatorTest
, CallbackDeletesAnimationInProgress
) {
1938 class TestLayerAnimationDeletingDelegate
: public TestLayerAnimationDelegate
{
1940 TestLayerAnimationDeletingDelegate(LayerAnimator
* animator
, int max_width
)
1941 : animator_(animator
),
1942 max_width_(max_width
) {
1945 void SetBoundsFromAnimation(const gfx::Rect
& bounds
) override
{
1946 TestLayerAnimationDelegate::SetBoundsFromAnimation(bounds
);
1947 if (bounds
.width() > max_width_
)
1948 animator_
->StopAnimating();
1951 LayerAnimator
* animator_
;
1953 // Allow copy and assign.
1956 ScopedAnimationDurationScaleMode
normal_duration_mode(
1957 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
1958 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1959 animator
->set_disable_timer_for_test(true);
1960 TestLayerAnimationDeletingDelegate
delegate(animator
.get(), 30);
1961 animator
->SetDelegate(&delegate
);
1963 gfx::Rect
start_bounds(0, 0, 0, 0);
1964 gfx::Rect
target_bounds(5, 5, 50, 50);
1966 delegate
.SetBoundsFromAnimation(start_bounds
);
1968 base::TimeDelta bounds_delta1
= base::TimeDelta::FromMilliseconds(333);
1969 base::TimeDelta bounds_delta2
= base::TimeDelta::FromMilliseconds(666);
1970 base::TimeDelta bounds_delta
= base::TimeDelta::FromMilliseconds(1000);
1971 base::TimeDelta final_delta
= base::TimeDelta::FromMilliseconds(1500);
1973 animator
->StartAnimation(new LayerAnimationSequence(
1974 LayerAnimationElement::CreateBoundsElement(
1975 target_bounds
, bounds_delta
)));
1976 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1978 base::TimeTicks start_time
= animator
->last_step_time();
1979 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ bounds_delta1
));
1980 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1982 // The next step should change the animated bounds past the threshold and
1983 // cause the animaton to stop.
1984 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ bounds_delta2
));
1985 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1986 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ final_delta
));
1988 // Completing the animation should have stopped the bounds
1990 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1993 // Similar to the ObserverDeletesAnimationsOnEnd test above except that it
1994 // tests the behavior when the OnLayerAnimationAborted() callback causes
1995 // all of the animator's other animations to be deleted.
1996 TEST(LayerAnimatorTest
, ObserverDeletesAnimationsOnAbort
) {
1997 ScopedAnimationDurationScaleMode
test_duration_mode(
1998 ScopedAnimationDurationScaleMode::NON_ZERO_DURATION
);
1999 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
2000 animator
->set_disable_timer_for_test(true);
2001 TestLayerAnimationDelegate delegate
;
2002 animator
->SetDelegate(&delegate
);
2004 double start_brightness(0.0);
2005 double target_brightness(1.0);
2006 gfx::Rect
start_bounds(0, 0, 50, 50);
2007 gfx::Rect
target_bounds(5, 5, 5, 5);
2008 base::TimeDelta brightness_delta
= base::TimeDelta::FromSeconds(1);
2009 base::TimeDelta bounds_delta
= base::TimeDelta::FromSeconds(2);
2011 delegate
.SetBrightnessFromAnimation(start_brightness
);
2012 delegate
.SetBoundsFromAnimation(start_bounds
);
2014 scoped_ptr
<DeletingLayerAnimationObserver
> observer(
2015 new DeletingLayerAnimationObserver(animator
.get()));
2016 animator
->AddObserver(observer
.get());
2018 animator
->StartAnimation(
2019 new LayerAnimationSequence(
2020 LayerAnimationElement::CreateBrightnessElement(
2021 target_brightness
, brightness_delta
)));
2022 animator
->StartAnimation(new LayerAnimationSequence(
2023 LayerAnimationElement::CreateBoundsElement(
2024 target_bounds
, bounds_delta
)));
2025 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2027 animator
->set_preemption_strategy(
2028 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2029 animator
->StartAnimation(
2030 new LayerAnimationSequence(
2031 LayerAnimationElement::CreateBrightnessElement(
2032 target_brightness
, brightness_delta
)));
2034 // Starting the second brightness animation should have aborted the initial
2035 // brightness animation. |observer| should have stopped the bounds animation
2037 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2039 animator
->RemoveObserver(observer
.get());
2042 // Check that setting a property during an animation with a default animator
2043 // cancels the original animation.
2044 TEST(LayerAnimatorTest
, SettingPropertyDuringAnAnimation
) {
2045 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2046 animator
->set_disable_timer_for_test(true);
2047 TestLayerAnimationDelegate delegate
;
2048 animator
->SetDelegate(&delegate
);
2050 double start_opacity(0.0);
2051 double target_opacity(1.0);
2053 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2055 delegate
.SetOpacityFromAnimation(start_opacity
);
2057 scoped_ptr
<LayerAnimationSequence
> sequence(
2058 new LayerAnimationSequence(
2059 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
2061 animator
->StartAnimation(sequence
.release());
2063 animator
->SetOpacity(0.5);
2065 EXPECT_FALSE(animator
->is_animating());
2066 EXPECT_EQ(0.5, animator
->GetTargetOpacity());
2069 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
2070 // second sequence to be leaked.
2071 TEST(LayerAnimatorTest
, ImmediatelySettingNewTargetDoesNotLeak
) {
2072 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2073 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
2074 animator
->set_disable_timer_for_test(true);
2075 TestLayerAnimationDelegate delegate
;
2076 animator
->SetDelegate(&delegate
);
2078 gfx::Rect
start_bounds(0, 0, 50, 50);
2079 gfx::Rect
middle_bounds(10, 10, 100, 100);
2080 gfx::Rect
target_bounds(5, 5, 5, 5);
2082 delegate
.SetBoundsFromAnimation(start_bounds
);
2085 // start an implicit bounds animation.
2086 ScopedLayerAnimationSettings
settings(animator
.get());
2087 animator
->SetBounds(middle_bounds
);
2090 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2092 int num_live_instances
= 0;
2093 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2094 scoped_ptr
<TestLayerAnimationSequence
> sequence(
2095 new TestLayerAnimationSequence(
2096 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
),
2097 &num_live_instances
));
2099 EXPECT_EQ(1, num_live_instances
);
2101 // This should interrupt the running sequence causing us to immediately set
2102 // the target value. The sequence should alse be destructed.
2103 animator
->StartAnimation(sequence
.release());
2105 EXPECT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2106 EXPECT_EQ(0, num_live_instances
);
2107 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
2110 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
2111 TEST(LayerAnimatorTest
, GetTargetOpacity
) {
2112 TestLayerAnimationDelegate delegate
;
2113 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2114 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2115 animator
->set_disable_timer_for_test(true);
2116 animator
->SetDelegate(&delegate
);
2118 delegate
.SetOpacityFromAnimation(0.0);
2121 ScopedLayerAnimationSettings
settings(animator
.get());
2122 animator
->SetOpacity(0.5);
2123 EXPECT_EQ(0.5, animator
->GetTargetOpacity());
2125 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2126 animator
->SetOpacity(1.0);
2127 EXPECT_EQ(1.0, animator
->GetTargetOpacity());
2131 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
2132 TEST(LayerAnimatorTest
, GetTargetBrightness
) {
2133 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2134 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2135 animator
->set_disable_timer_for_test(true);
2136 TestLayerAnimationDelegate delegate
;
2137 animator
->SetDelegate(&delegate
);
2139 delegate
.SetBrightnessFromAnimation(0.0);
2142 ScopedLayerAnimationSettings
settings(animator
.get());
2143 animator
->SetBrightness(0.5);
2144 EXPECT_EQ(0.5, animator
->GetTargetBrightness());
2146 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2147 animator
->SetBrightness(1.0);
2148 EXPECT_EQ(1.0, animator
->GetTargetBrightness());
2152 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
2153 TEST(LayerAnimatorTest
, GetTargetGrayscale
) {
2154 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2155 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2156 animator
->set_disable_timer_for_test(true);
2157 TestLayerAnimationDelegate delegate
;
2158 animator
->SetDelegate(&delegate
);
2160 delegate
.SetGrayscaleFromAnimation(0.0);
2163 ScopedLayerAnimationSettings
settings(animator
.get());
2164 animator
->SetGrayscale(0.5);
2165 EXPECT_EQ(0.5, animator
->GetTargetGrayscale());
2167 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2168 animator
->SetGrayscale(1.0);
2169 EXPECT_EQ(1.0, animator
->GetTargetGrayscale());
2173 // Verifies color property is modified appropriately.
2174 TEST(LayerAnimatorTest
, Color
) {
2175 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2176 animator
->set_disable_timer_for_test(true);
2177 TestLayerAnimationDelegate delegate
;
2178 animator
->SetDelegate(&delegate
);
2180 SkColor start_color
= SkColorSetARGB( 64, 20, 40, 60);
2181 SkColor middle_color
= SkColorSetARGB(128, 35, 70, 120);
2182 SkColor target_color
= SkColorSetARGB(192, 40, 80, 140);
2184 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2186 delegate
.SetColorFromAnimation(start_color
);
2188 animator
->ScheduleAnimation(
2189 new LayerAnimationSequence(
2190 LayerAnimationElement::CreateColorElement(target_color
, delta
)));
2192 EXPECT_TRUE(animator
->is_animating());
2193 EXPECT_EQ(ColorToString(start_color
),
2194 ColorToString(delegate
.GetColorForAnimation()));
2196 base::TimeTicks start_time
= animator
->last_step_time();
2198 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
2200 EXPECT_TRUE(animator
->is_animating());
2201 EXPECT_EQ(ColorToString(middle_color
),
2202 ColorToString(delegate
.GetColorForAnimation()));
2204 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
2206 EXPECT_FALSE(animator
->is_animating());
2207 EXPECT_EQ(ColorToString(target_color
),
2208 ColorToString(delegate
.GetColorForAnimation()));
2211 // Verifies SchedulePauseForProperties().
2212 TEST(LayerAnimatorTest
, SchedulePauseForProperties
) {
2213 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2214 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2215 animator
->SchedulePauseForProperties(
2216 base::TimeDelta::FromMilliseconds(100),
2217 LayerAnimationElement::TRANSFORM
| LayerAnimationElement::BOUNDS
);
2218 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::TRANSFORM
));
2219 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2220 EXPECT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::OPACITY
));
2224 class AnimatorOwner
{
2227 : animator_(LayerAnimator::CreateDefaultAnimator()) {
2230 LayerAnimator
* animator() { return animator_
.get(); }
2233 scoped_refptr
<LayerAnimator
> animator_
;
2235 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner
);
2238 class DeletingObserver
: public LayerAnimationObserver
{
2240 DeletingObserver(bool* was_deleted
)
2241 : animator_owner_(new AnimatorOwner
),
2242 delete_on_animation_ended_(false),
2243 delete_on_animation_aborted_(false),
2244 delete_on_animation_scheduled_(false),
2245 was_deleted_(was_deleted
) {
2246 animator()->AddObserver(this);
2249 ~DeletingObserver() override
{
2250 animator()->RemoveObserver(this);
2251 *was_deleted_
= true;
2254 LayerAnimator
* animator() { return animator_owner_
->animator(); }
2256 bool delete_on_animation_ended() const {
2257 return delete_on_animation_ended_
;
2259 void set_delete_on_animation_ended(bool enabled
) {
2260 delete_on_animation_ended_
= enabled
;
2263 bool delete_on_animation_aborted() const {
2264 return delete_on_animation_aborted_
;
2266 void set_delete_on_animation_aborted(bool enabled
) {
2267 delete_on_animation_aborted_
= enabled
;
2270 bool delete_on_animation_scheduled() const {
2271 return delete_on_animation_scheduled_
;
2273 void set_delete_on_animation_scheduled(bool enabled
) {
2274 delete_on_animation_scheduled_
= enabled
;
2277 // LayerAnimationObserver implementation.
2278 void OnLayerAnimationEnded(LayerAnimationSequence
* sequence
) override
{
2279 if (delete_on_animation_ended_
)
2283 void OnLayerAnimationAborted(LayerAnimationSequence
* sequence
) override
{
2284 if (delete_on_animation_aborted_
)
2288 void OnLayerAnimationScheduled(LayerAnimationSequence
* sequence
) override
{
2289 if (delete_on_animation_scheduled_
)
2294 scoped_ptr
<AnimatorOwner
> animator_owner_
;
2295 bool delete_on_animation_ended_
;
2296 bool delete_on_animation_aborted_
;
2297 bool delete_on_animation_scheduled_
;
2300 DISALLOW_COPY_AND_ASSIGN(DeletingObserver
);
2303 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterFinishingAnimation
) {
2304 bool observer_was_deleted
= false;
2305 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2306 observer
->set_delete_on_animation_ended(true);
2307 observer
->set_delete_on_animation_aborted(true);
2308 LayerAnimator
* animator
= observer
->animator();
2309 animator
->set_disable_timer_for_test(true);
2310 TestLayerAnimationDelegate delegate
;
2311 animator
->SetDelegate(&delegate
);
2313 delegate
.SetBrightnessFromAnimation(0.0f
);
2315 gfx::Rect
start_bounds(0, 0, 50, 50);
2316 gfx::Rect
target_bounds(10, 10, 100, 100);
2318 delegate
.SetBoundsFromAnimation(start_bounds
);
2320 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2321 LayerAnimationSequence
* brightness_sequence
= new LayerAnimationSequence(
2322 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
2323 animator
->StartAnimation(brightness_sequence
);
2325 delta
= base::TimeDelta::FromSeconds(2);
2326 LayerAnimationSequence
* bounds_sequence
= new LayerAnimationSequence(
2327 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
2328 animator
->StartAnimation(bounds_sequence
);
2330 base::TimeTicks start_time
= animator
->last_step_time();
2331 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
2333 EXPECT_TRUE(observer_was_deleted
);
2336 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterStoppingAnimating
) {
2337 bool observer_was_deleted
= false;
2338 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2339 observer
->set_delete_on_animation_ended(true);
2340 observer
->set_delete_on_animation_aborted(true);
2341 LayerAnimator
* animator
= observer
->animator();
2342 animator
->set_disable_timer_for_test(true);
2343 TestLayerAnimationDelegate delegate
;
2344 animator
->SetDelegate(&delegate
);
2346 delegate
.SetOpacityFromAnimation(0.0f
);
2348 gfx::Rect
start_bounds(0, 0, 50, 50);
2349 gfx::Rect
target_bounds(10, 10, 100, 100);
2351 delegate
.SetBoundsFromAnimation(start_bounds
);
2353 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2354 LayerAnimationSequence
* opacity_sequence
= new LayerAnimationSequence(
2355 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
));
2356 animator
->StartAnimation(opacity_sequence
);
2358 delta
= base::TimeDelta::FromSeconds(2);
2359 LayerAnimationSequence
* bounds_sequence
= new LayerAnimationSequence(
2360 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
2361 animator
->StartAnimation(bounds_sequence
);
2363 animator
->StopAnimating();
2365 EXPECT_TRUE(observer_was_deleted
);
2368 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterScheduling
) {
2369 bool observer_was_deleted
= false;
2370 TestLayerAnimationDelegate delegate
;
2371 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2372 observer
->set_delete_on_animation_scheduled(true);
2373 LayerAnimator
* animator
= observer
->animator();
2374 animator
->set_disable_timer_for_test(true);
2375 animator
->SetDelegate(&delegate
);
2377 delegate
.SetOpacityFromAnimation(0.0f
);
2379 gfx::Rect
start_bounds(0, 0, 50, 50);
2380 gfx::Rect
target_bounds(10, 10, 100, 100);
2382 delegate
.SetBoundsFromAnimation(start_bounds
);
2384 std::vector
<LayerAnimationSequence
*> to_start
;
2386 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2387 to_start
.push_back(new LayerAnimationSequence(
2388 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2390 delta
= base::TimeDelta::FromSeconds(2);
2391 to_start
.push_back(new LayerAnimationSequence(
2392 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
2394 animator
->ScheduleTogether(to_start
);
2396 EXPECT_TRUE(observer_was_deleted
);
2399 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterAborted
) {
2400 bool observer_was_deleted
= false;
2401 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2402 TestLayerAnimationDelegate delegate
;
2403 observer
->set_delete_on_animation_aborted(true);
2404 LayerAnimator
* animator
= observer
->animator();
2405 animator
->set_preemption_strategy(
2406 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2407 animator
->set_disable_timer_for_test(true);
2408 animator
->SetDelegate(&delegate
);
2410 delegate
.SetOpacityFromAnimation(0.0f
);
2412 gfx::Rect
start_bounds(0, 0, 50, 50);
2413 gfx::Rect
target_bounds(10, 10, 100, 100);
2415 delegate
.SetBoundsFromAnimation(start_bounds
);
2417 std::vector
<LayerAnimationSequence
*> to_start
;
2419 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2420 to_start
.push_back(new LayerAnimationSequence(
2421 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2423 delta
= base::TimeDelta::FromSeconds(2);
2424 to_start
.push_back(new LayerAnimationSequence(
2425 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
2427 animator
->ScheduleTogether(to_start
);
2429 EXPECT_FALSE(observer_was_deleted
);
2431 animator
->StartAnimation(new LayerAnimationSequence(
2432 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2434 EXPECT_TRUE(observer_was_deleted
);
2438 TEST(LayerAnimatorTest
, TestSetterRespectEnqueueStrategy
) {
2439 TestLayerAnimationDelegate delegate
;
2440 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2441 animator
->set_disable_timer_for_test(true);
2443 animator
->SetDelegate(&delegate
);
2445 float start_opacity
= 0.0f
;
2446 float target_opacity
= 1.0f
;
2447 float magic_opacity
= 0.123f
;
2449 delegate
.SetOpacityFromAnimation(start_opacity
);
2451 ScopedLayerAnimationSettings
settings(animator
.get());
2452 settings
.SetPreemptionStrategy(
2453 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2454 settings
.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2455 animator
->SetOpacity(target_opacity
);
2457 EXPECT_EQ(start_opacity
, delegate
.GetOpacityForAnimation());
2459 settings
.SetPreemptionStrategy(
2460 LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2461 settings
.SetTransitionDuration(base::TimeDelta());
2462 animator
->SetOpacity(magic_opacity
);
2464 EXPECT_EQ(start_opacity
, delegate
.GetOpacityForAnimation());
2467 TEST(LayerAnimatorTest
, TestScopedCounterAnimation
) {
2468 Layer parent
, child
;
2471 gfx::Transform parent_begin
, parent_end
;
2473 parent_end
.Scale3d(2.0, 0.5, 1.0);
2475 // Parent animates from identity to the end value. The counter animation will
2476 // start at the end value and animate back to identity.
2477 gfx::Transform
child_begin(parent_end
);
2479 child
.SetTransform(child_begin
);
2480 parent
.SetTransform(parent_begin
);
2482 EXPECT_FALSE(child
.GetAnimator()->is_animating());
2484 ScopedLayerAnimationSettings
settings(parent
.GetAnimator());
2485 settings
.SetInverselyAnimatedBaseLayer(&parent
);
2486 settings
.AddInverselyAnimatedLayer(&child
);
2488 parent
.SetTransform(parent_end
);
2490 EXPECT_TRUE(child
.GetAnimator()->is_animating());
2491 EXPECT_TRUE(child
.GetTargetTransform().IsIdentity())
2492 << child
.GetTargetTransform().ToString();
2496 class CollectionLayerAnimationDelegate
: public TestLayerAnimationDelegate
{
2498 CollectionLayerAnimationDelegate() : collection(NULL
) {}
2499 ~CollectionLayerAnimationDelegate() override
{}
2501 // LayerAnimationDelegate:
2502 LayerAnimatorCollection
* GetLayerAnimatorCollection() override
{
2507 LayerAnimatorCollection collection
;
2510 TEST(LayerAnimatorTest
, LayerAnimatorCollectionTickTime
) {
2512 LayerAnimator
* animator
= layer
.GetAnimator();
2513 CollectionLayerAnimationDelegate delegate
;
2514 animator
->SetDelegate(&delegate
);
2516 LayerAnimatorCollection
* collection
= delegate
.GetLayerAnimatorCollection();
2517 base::TimeTicks null
;
2518 collection
->OnAnimationStep(null
);
2519 EXPECT_TRUE(collection
->last_tick_time().is_null());
2521 // Adding an animator to the collection should update the last tick time.
2522 collection
->StartAnimator(layer
.GetAnimator());
2523 EXPECT_TRUE(collection
->HasActiveAnimators());
2524 EXPECT_FALSE(collection
->last_tick_time().is_null());
2526 collection
->StopAnimator(layer
.GetAnimator());
2527 EXPECT_FALSE(collection
->HasActiveAnimators());
2530 TEST(LayerAnimatorTest
, AnimatorStartedCorrectly
) {
2532 LayerAnimatorTestController
test_controller(layer
.GetAnimator());
2533 LayerAnimator
* animator
= test_controller
.animator();
2534 ASSERT_FALSE(animator
->is_started_
);
2536 TestLayerAnimationDelegate test_delegate
;
2537 animator
->SetDelegate(&test_delegate
);
2538 double target_opacity
= 1.0;
2539 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2540 animator
->ScheduleAnimation(new LayerAnimationSequence(
2541 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2542 EXPECT_FALSE(animator
->is_started_
);
2544 CollectionLayerAnimationDelegate collection_delegate
;
2545 animator
->SetDelegate(&collection_delegate
);
2546 animator
->UpdateAnimationState();
2547 EXPECT_TRUE(animator
->is_started_
);
2548 animator
->SetDelegate(NULL
);
2551 TEST(LayerAnimatorTest
, AnimatorRemovedFromCollectionWhenLayerIsDestroyed
) {
2552 scoped_ptr
<Layer
> layer(new Layer(LAYER_TEXTURED
));
2553 LayerAnimatorTestController
test_controller(layer
->GetAnimator());
2554 scoped_refptr
<LayerAnimator
> animator
= test_controller
.animator();
2555 CollectionLayerAnimationDelegate collection_delegate
;
2556 animator
->SetDelegate(&collection_delegate
);
2558 double target_opacity
= 1.0;
2559 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2560 animator
->ScheduleAnimation(new LayerAnimationSequence(
2561 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2564 collection_delegate
.GetLayerAnimatorCollection()->HasActiveAnimators());
2567 EXPECT_EQ(NULL
, animator
->delegate());
2569 collection_delegate
.GetLayerAnimatorCollection()->HasActiveAnimators());
2572 TEST(LayerAnimatorTest
, LayerMovedBetweenCompositorsDuringAnimation
) {
2573 bool enable_pixel_output
= false;
2574 ui::ContextFactory
* context_factory
=
2575 InitializeContextFactoryForTests(enable_pixel_output
);
2576 const gfx::Rect
bounds(10, 10, 100, 100);
2577 scoped_ptr
<TestCompositorHost
> host_1(
2578 TestCompositorHost::Create(bounds
, context_factory
));
2579 scoped_ptr
<TestCompositorHost
> host_2(
2580 TestCompositorHost::Create(bounds
, context_factory
));
2584 Compositor
* compositor_1
= host_1
->GetCompositor();
2586 compositor_1
->SetRootLayer(&root_1
);
2588 Compositor
* compositor_2
= host_2
->GetCompositor();
2590 compositor_2
->SetRootLayer(&root_2
);
2592 // Verify that neither compositor has active animators.
2593 EXPECT_FALSE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2594 EXPECT_FALSE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2598 LayerAnimator
* animator
= layer
.GetAnimator();
2599 double target_opacity
= 1.0;
2600 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2601 animator
->ScheduleAnimation(new LayerAnimationSequence(
2602 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2603 EXPECT_TRUE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2604 EXPECT_FALSE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2607 EXPECT_FALSE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2608 EXPECT_TRUE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2611 TerminateContextFactoryForTests();
2614 class LayerOwnerAnimationObserver
: public LayerAnimationObserver
{
2616 LayerOwnerAnimationObserver(LayerAnimator
* animator
)
2617 : animator_layer_(new Layer(LAYER_TEXTURED
)) {
2618 animator_layer_
->SetAnimator(animator
);
2621 ~LayerOwnerAnimationObserver() override
{}
2623 void OnLayerAnimationEnded(LayerAnimationSequence
* sequence
) override
{
2624 ASSERT_TRUE(sequence
);
2625 animator_layer_
.reset();
2628 void OnLayerAnimationAborted(LayerAnimationSequence
* sequence
) override
{
2629 ASSERT_TRUE(sequence
);
2630 animator_layer_
.reset();
2633 LayerAnimationDelegate
* animator_layer() {
2634 return animator_layer_
.get();
2637 void OnLayerAnimationScheduled(LayerAnimationSequence
* sequence
) override
{}
2640 scoped_ptr
<Layer
> animator_layer_
;
2642 DISALLOW_COPY_AND_ASSIGN(LayerOwnerAnimationObserver
);
2645 TEST(LayerAnimatorTest
, ObserverDeletesLayerInStopAnimating
) {
2646 scoped_refptr
<LayerAnimator
> animator(
2647 LayerAnimator::CreateImplicitAnimator());
2648 animator
->set_disable_timer_for_test(true);
2649 LayerOwnerAnimationObserver
observer(animator
.get());
2650 LayerAnimationDelegate
* delegate
= observer
.animator_layer();
2652 const gfx::Rect
start_bounds(0, 0, 50, 50);
2653 const gfx::Rect
target_bounds(10, 10, 100, 100);
2654 const double target_opacity
= 1.0;
2656 delegate
->SetOpacityFromAnimation(0.0f
);
2657 delegate
->SetBoundsFromAnimation(start_bounds
);
2659 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2660 LayerAnimationSequence
* opacity
= new LayerAnimationSequence(
2661 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
));
2662 opacity
->AddObserver(&observer
);
2663 animator
->ScheduleAnimation(opacity
);
2664 time_delta
= base::TimeDelta::FromSeconds(2);
2665 LayerAnimationSequence
* move
= new LayerAnimationSequence(
2666 LayerAnimationElement::CreateBoundsElement(target_bounds
, time_delta
));
2667 animator
->ScheduleAnimation(move
);
2668 EXPECT_TRUE(animator
->is_animating());
2669 animator
->Step(animator
->last_step_time() +
2670 base::TimeDelta::FromMilliseconds(500));
2671 animator
->StopAnimating();
2673 EXPECT_EQ(nullptr, observer
.animator_layer());
2674 EXPECT_TRUE(animator
->is_animating());