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/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 virtual void OnImplicitAnimationsCompleted() OVERRIDE
{
84 animations_completed_
= true;
87 virtual 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 virtual void OnLayerAnimationEnded(
105 LayerAnimationSequence
* sequence
) OVERRIDE
{
106 animator_
->StopAnimating();
109 virtual void OnLayerAnimationAborted(
110 LayerAnimationSequence
* sequence
) OVERRIDE
{
111 animator_
->StopAnimating();
114 virtual void OnLayerAnimationScheduled(
115 LayerAnimationSequence
* sequence
) OVERRIDE
{
119 LayerAnimator
* animator_
;
121 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver
);
124 class LayerAnimatorDestructionObserver
{
126 LayerAnimatorDestructionObserver() : animator_deleted_(false) {}
127 virtual ~LayerAnimatorDestructionObserver() {}
129 void NotifyAnimatorDeleted() {
130 animator_deleted_
= true;
133 bool IsAnimatorDeleted() {
134 return animator_deleted_
;
138 bool animator_deleted_
;
140 DISALLOW_COPY_AND_ASSIGN(LayerAnimatorDestructionObserver
);
143 class TestLayerAnimator
: public LayerAnimator
{
145 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)),
146 destruction_observer_(NULL
) {}
148 void SetDestructionObserver(
149 LayerAnimatorDestructionObserver
* observer
) {
150 destruction_observer_
= observer
;
154 virtual ~TestLayerAnimator() {
155 if (destruction_observer_
) {
156 destruction_observer_
->NotifyAnimatorDeleted();
160 virtual void ProgressAnimation(LayerAnimationSequence
* sequence
,
161 base::TimeTicks now
) OVERRIDE
{
162 EXPECT_TRUE(HasAnimation(sequence
));
163 LayerAnimator::ProgressAnimation(sequence
, now
);
167 LayerAnimatorDestructionObserver
* destruction_observer_
;
169 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator
);
172 // The test layer animation sequence updates a live instances count when it is
173 // created and destroyed.
174 class TestLayerAnimationSequence
: public LayerAnimationSequence
{
176 TestLayerAnimationSequence(LayerAnimationElement
* element
,
177 int* num_live_instances
)
178 : LayerAnimationSequence(element
),
179 num_live_instances_(num_live_instances
) {
180 (*num_live_instances_
)++;
183 virtual ~TestLayerAnimationSequence() {
184 (*num_live_instances_
)--;
188 int* num_live_instances_
;
190 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence
);
195 // Checks that setting a property on an implicit animator causes an animation to
197 TEST(LayerAnimatorTest
, ImplicitAnimation
) {
198 scoped_refptr
<LayerAnimator
> animator(
199 LayerAnimator::CreateImplicitAnimator());
200 animator
->set_disable_timer_for_test(true);
201 TestLayerAnimationDelegate delegate
;
202 animator
->SetDelegate(&delegate
);
203 base::TimeTicks now
= gfx::FrameTime::Now();
204 animator
->SetBrightness(0.5);
205 EXPECT_TRUE(animator
->is_animating());
206 animator
->Step(now
+ base::TimeDelta::FromSeconds(1));
207 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), 0.5);
210 // Checks that if the animator is a default animator, that implicit animations
212 TEST(LayerAnimatorTest
, NoImplicitAnimation
) {
213 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
214 animator
->set_disable_timer_for_test(true);
215 TestLayerAnimationDelegate delegate
;
216 animator
->SetDelegate(&delegate
);
217 animator
->SetBrightness(0.5);
218 EXPECT_FALSE(animator
->is_animating());
219 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), 0.5);
222 // Checks that StopAnimatingProperty stops animation for that property, and also
223 // skips the stopped animation to the end.
224 TEST(LayerAnimatorTest
, StopAnimatingProperty
) {
225 scoped_refptr
<LayerAnimator
> animator(
226 LayerAnimator::CreateImplicitAnimator());
227 animator
->set_disable_timer_for_test(true);
228 TestLayerAnimationDelegate delegate
;
229 animator
->SetDelegate(&delegate
);
230 double target_opacity(0.5);
231 gfx::Rect
target_bounds(0, 0, 50, 50);
232 animator
->SetOpacity(target_opacity
);
233 animator
->SetBounds(target_bounds
);
234 animator
->StopAnimatingProperty(LayerAnimationElement::OPACITY
);
235 EXPECT_TRUE(animator
->is_animating());
236 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), 0.5);
237 animator
->StopAnimatingProperty(LayerAnimationElement::BOUNDS
);
238 EXPECT_FALSE(animator
->is_animating());
239 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
242 // Checks that multiple running animation for separate properties can be stopped
243 // simultaneously and that all animations are advanced to their target values.
244 TEST(LayerAnimatorTest
, StopAnimating
) {
245 scoped_refptr
<LayerAnimator
> animator(
246 LayerAnimator::CreateImplicitAnimator());
247 animator
->set_disable_timer_for_test(true);
248 TestLayerAnimationDelegate delegate
;
249 animator
->SetDelegate(&delegate
);
250 double target_opacity(0.5);
251 gfx::Rect
target_bounds(0, 0, 50, 50);
252 animator
->SetOpacity(target_opacity
);
253 animator
->SetBounds(target_bounds
);
254 EXPECT_TRUE(animator
->is_animating());
255 animator
->StopAnimating();
256 EXPECT_FALSE(animator
->is_animating());
257 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), 0.5);
258 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
261 // Checks that multiple running animation for separate properties can be stopped
262 // simultaneously and that all animations are advanced to their target values.
263 TEST(LayerAnimatorTest
, AbortAllAnimations
) {
264 scoped_refptr
<LayerAnimator
> animator(
265 LayerAnimator::CreateImplicitAnimator());
266 animator
->set_disable_timer_for_test(true);
267 TestLayerAnimationDelegate delegate
;
268 double initial_opacity(1.0);
269 gfx::Rect
initial_bounds(0, 0, 10, 10);
270 delegate
.SetOpacityFromAnimation(initial_opacity
);
271 delegate
.SetBoundsFromAnimation(initial_bounds
);
272 animator
->SetDelegate(&delegate
);
273 double target_opacity(0.5);
274 gfx::Rect
target_bounds(0, 0, 50, 50);
275 animator
->SetOpacity(target_opacity
);
276 animator
->SetBounds(target_bounds
);
277 EXPECT_TRUE(animator
->is_animating());
278 animator
->AbortAllAnimations();
279 EXPECT_FALSE(animator
->is_animating());
280 EXPECT_FLOAT_EQ(initial_opacity
, delegate
.GetOpacityForAnimation());
281 CheckApproximatelyEqual(initial_bounds
, delegate
.GetBoundsForAnimation());
284 // Schedule a non-threaded animation that can run immediately. This is the
285 // trivial case and should result in the animation being started immediately.
286 TEST(LayerAnimatorTest
, ScheduleAnimationThatCanRunImmediately
) {
287 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
288 animator
->set_disable_timer_for_test(true);
289 TestLayerAnimationDelegate delegate
;
290 animator
->SetDelegate(&delegate
);
292 double start_brightness(0.0);
293 double middle_brightness(0.5);
294 double target_brightness(1.0);
296 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
298 delegate
.SetBrightnessFromAnimation(start_brightness
);
300 animator
->ScheduleAnimation(
301 new LayerAnimationSequence(
302 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
305 EXPECT_TRUE(animator
->is_animating());
306 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
308 base::TimeTicks start_time
= animator
->last_step_time();
310 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
312 EXPECT_TRUE(animator
->is_animating());
313 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
315 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
317 EXPECT_FALSE(animator
->is_animating());
318 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
321 // Schedule a threaded animation that can run immediately.
322 TEST(LayerAnimatorTest
, ScheduleThreadedAnimationThatCanRunImmediately
) {
323 double epsilon
= 0.00001;
324 LayerAnimatorTestController
test_controller(
325 LayerAnimator::CreateDefaultAnimator());
326 LayerAnimator
* animator
= test_controller
.animator();
327 test_controller
.animator()->set_disable_timer_for_test(true);
328 TestLayerAnimationDelegate delegate
;
329 test_controller
.animator()->SetDelegate(&delegate
);
331 double start_opacity(0.0);
332 double target_opacity(1.0);
334 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
336 delegate
.SetOpacityFromAnimation(start_opacity
);
338 test_controller
.animator()->ScheduleAnimation(
339 new LayerAnimationSequence(
340 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
342 EXPECT_TRUE(test_controller
.animator()->is_animating());
343 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
345 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
346 base::TimeTicks effective_start
= start_time
+ delta
;
348 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
349 cc::AnimationEvent::Started
,
351 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
352 ->animation_group_id(),
353 cc::Animation::Opacity
,
356 animator
->Step(effective_start
+ delta
/ 2);
358 EXPECT_TRUE(test_controller
.animator()->is_animating());
361 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
362 last_progressed_fraction(),
365 animator
->Step(effective_start
+ delta
);
367 EXPECT_FALSE(test_controller
.animator()->is_animating());
368 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
371 // Schedule two non-threaded animations on separate properties. Both animations
372 // should start immediately and should progress in lock step.
373 TEST(LayerAnimatorTest
, ScheduleTwoAnimationsThatCanRunImmediately
) {
374 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
375 animator
->set_disable_timer_for_test(true);
376 TestLayerAnimationDelegate delegate
;
377 animator
->SetDelegate(&delegate
);
379 double start_brightness(0.0);
380 double middle_brightness(0.5);
381 double target_brightness(1.0);
383 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
384 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
385 start_bounds
.set_x(-90);
386 target_bounds
.set_x(90);
388 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
390 delegate
.SetBrightnessFromAnimation(start_brightness
);
391 delegate
.SetBoundsFromAnimation(start_bounds
);
393 animator
->ScheduleAnimation(
394 new LayerAnimationSequence(
395 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
398 animator
->ScheduleAnimation(
399 new LayerAnimationSequence(
400 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
402 EXPECT_TRUE(animator
->is_animating());
403 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
404 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
406 base::TimeTicks start_time
= animator
->last_step_time();
408 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
410 EXPECT_TRUE(animator
->is_animating());
411 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
412 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), middle_bounds
);
414 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
416 EXPECT_FALSE(animator
->is_animating());
417 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
418 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
421 // Schedule a threaded and a non-threaded animation on separate properties. Both
422 // animations should progress in lock step.
423 TEST(LayerAnimatorTest
, ScheduleThreadedAndNonThreadedAnimations
) {
424 double epsilon
= 0.00001;
425 LayerAnimatorTestController
test_controller(
426 LayerAnimator::CreateDefaultAnimator());
427 LayerAnimator
* animator
= test_controller
.animator();
428 test_controller
.animator()->set_disable_timer_for_test(true);
429 TestLayerAnimationDelegate delegate
;
430 test_controller
.animator()->SetDelegate(&delegate
);
432 double start_opacity(0.0);
433 double target_opacity(1.0);
435 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
436 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
437 start_bounds
.set_x(-90);
438 target_bounds
.set_x(90);
440 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
442 delegate
.SetOpacityFromAnimation(start_opacity
);
443 delegate
.SetBoundsFromAnimation(start_bounds
);
445 std::vector
<LayerAnimationSequence
*> animations
;
446 animations
.push_back(
447 new LayerAnimationSequence(
448 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
450 animations
.push_back(
451 new LayerAnimationSequence(
452 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
454 test_controller
.animator()->ScheduleTogether(animations
);
456 EXPECT_TRUE(test_controller
.animator()->is_animating());
457 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
458 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
460 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
461 base::TimeTicks effective_start
= start_time
+ delta
;
463 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
464 cc::AnimationEvent::Started
,
466 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
467 ->animation_group_id(),
468 cc::Animation::Opacity
,
471 animator
->Step(effective_start
+ delta
/ 2);
473 EXPECT_TRUE(test_controller
.animator()->is_animating());
476 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
477 last_progressed_fraction(),
479 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), middle_bounds
);
481 animator
->Step(effective_start
+ delta
);
483 EXPECT_FALSE(test_controller
.animator()->is_animating());
484 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
485 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
488 // Schedule two animations on the same property. In this case, the two
489 // animations should run one after another.
490 TEST(LayerAnimatorTest
, ScheduleTwoAnimationsOnSameProperty
) {
491 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
492 animator
->set_disable_timer_for_test(true);
493 TestLayerAnimationDelegate delegate
;
494 animator
->SetDelegate(&delegate
);
496 double start_brightness(0.0);
497 double middle_brightness(0.5);
498 double target_brightness(1.0);
500 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
502 delegate
.SetBrightnessFromAnimation(start_brightness
);
504 animator
->ScheduleAnimation(
505 new LayerAnimationSequence(
506 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
509 animator
->ScheduleAnimation(
510 new LayerAnimationSequence(
511 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
514 EXPECT_TRUE(animator
->is_animating());
515 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
517 base::TimeTicks start_time
= animator
->last_step_time();
519 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
521 EXPECT_TRUE(animator
->is_animating());
522 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
524 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
526 EXPECT_TRUE(animator
->is_animating());
527 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
529 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
531 EXPECT_TRUE(animator
->is_animating());
532 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
534 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
536 EXPECT_FALSE(animator
->is_animating());
537 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
540 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
541 // is, ensure that all animations targetting a particular property are run in
543 TEST(LayerAnimatorTest
, ScheduleBlockedAnimation
) {
544 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
545 animator
->set_disable_timer_for_test(true);
546 TestLayerAnimationDelegate delegate
;
547 animator
->SetDelegate(&delegate
);
549 double start_grayscale(0.0);
550 double middle_grayscale(0.5);
551 double target_grayscale(1.0);
553 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
554 start_bounds
= target_bounds
= middle_bounds
= gfx::Rect(0, 0, 50, 50);
555 start_bounds
.set_x(-90);
556 target_bounds
.set_x(90);
558 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
560 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
561 delegate
.SetBoundsFromAnimation(start_bounds
);
563 animator
->ScheduleAnimation(
564 new LayerAnimationSequence(
565 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
568 scoped_ptr
<LayerAnimationSequence
> bounds_and_grayscale(
569 new LayerAnimationSequence(
570 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
,
573 bounds_and_grayscale
->AddElement(
574 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
576 animator
->ScheduleAnimation(bounds_and_grayscale
.release());
578 animator
->ScheduleAnimation(
579 new LayerAnimationSequence(
580 LayerAnimationElement::CreateBoundsElement(start_bounds
, delta
)));
582 EXPECT_TRUE(animator
->is_animating());
583 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
584 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
586 base::TimeTicks start_time
= animator
->last_step_time();
588 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
590 EXPECT_TRUE(animator
->is_animating());
591 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
592 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
594 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
596 EXPECT_TRUE(animator
->is_animating());
597 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
598 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
600 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
602 EXPECT_TRUE(animator
->is_animating());
603 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
604 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
606 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(3000));
608 EXPECT_TRUE(animator
->is_animating());
609 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
610 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
612 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(4000));
614 EXPECT_FALSE(animator
->is_animating());
615 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
616 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
619 // Schedule {g} and then schedule {g} and {b} together. In this case, since
620 // ScheduleTogether is being used, the bounds animation should not start until
621 // the second grayscale animation starts.
622 TEST(LayerAnimatorTest
, ScheduleTogether
) {
623 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
624 animator
->set_disable_timer_for_test(true);
625 TestLayerAnimationDelegate delegate
;
626 animator
->SetDelegate(&delegate
);
628 double start_grayscale(0.0);
629 double target_grayscale(1.0);
631 gfx::Rect start_bounds
, target_bounds
, middle_bounds
;
632 start_bounds
= target_bounds
= gfx::Rect(0, 0, 50, 50);
633 start_bounds
.set_x(-90);
634 target_bounds
.set_x(90);
636 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
638 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
639 delegate
.SetBoundsFromAnimation(start_bounds
);
641 animator
->ScheduleAnimation(
642 new LayerAnimationSequence(
643 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
646 std::vector
<LayerAnimationSequence
*> sequences
;
647 sequences
.push_back(new LayerAnimationSequence(
648 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
)));
649 sequences
.push_back(new LayerAnimationSequence(
650 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
652 animator
->ScheduleTogether(sequences
);
654 EXPECT_TRUE(animator
->is_animating());
655 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
656 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
658 base::TimeTicks start_time
= animator
->last_step_time();
660 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
662 EXPECT_TRUE(animator
->is_animating());
663 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
664 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), start_bounds
);
666 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
668 EXPECT_FALSE(animator
->is_animating());
669 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
670 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
673 // Start non-threaded animation (that can run immediately). This is the trivial
674 // case (see the trival case for ScheduleAnimation).
675 TEST(LayerAnimatorTest
, StartAnimationThatCanRunImmediately
) {
676 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
677 animator
->set_disable_timer_for_test(true);
678 TestLayerAnimationDelegate delegate
;
679 animator
->SetDelegate(&delegate
);
681 double start_brightness(0.0);
682 double middle_brightness(0.5);
683 double target_brightness(1.0);
685 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
687 delegate
.SetBrightnessFromAnimation(start_brightness
);
689 animator
->StartAnimation(
690 new LayerAnimationSequence(
691 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
694 EXPECT_TRUE(animator
->is_animating());
695 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
697 base::TimeTicks start_time
= animator
->last_step_time();
699 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
701 EXPECT_TRUE(animator
->is_animating());
702 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
704 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
706 EXPECT_FALSE(animator
->is_animating());
707 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
710 // Start threaded animation (that can run immediately).
711 TEST(LayerAnimatorTest
, StartThreadedAnimationThatCanRunImmediately
) {
712 double epsilon
= 0.00001;
713 LayerAnimatorTestController
test_controller(
714 LayerAnimator::CreateDefaultAnimator());
715 LayerAnimator
* animator
= test_controller
.animator();
716 test_controller
.animator()->set_disable_timer_for_test(true);
717 TestLayerAnimationDelegate delegate
;
718 test_controller
.animator()->SetDelegate(&delegate
);
720 double start_opacity(0.0);
721 double target_opacity(1.0);
723 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
725 delegate
.SetOpacityFromAnimation(start_opacity
);
727 test_controller
.animator()->StartAnimation(
728 new LayerAnimationSequence(
729 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
731 EXPECT_TRUE(test_controller
.animator()->is_animating());
732 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
734 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
735 base::TimeTicks effective_start
= start_time
+ delta
;
737 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
738 cc::AnimationEvent::Started
,
740 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
741 ->animation_group_id(),
742 cc::Animation::Opacity
,
745 animator
->Step(effective_start
+ delta
/ 2);
747 EXPECT_TRUE(test_controller
.animator()->is_animating());
750 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
751 last_progressed_fraction(),
754 animator
->Step(effective_start
+ delta
);
755 EXPECT_FALSE(test_controller
.animator()->is_animating());
756 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
759 // Preempt by immediately setting new target.
760 TEST(LayerAnimatorTest
, PreemptBySettingNewTarget
) {
761 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
762 animator
->set_disable_timer_for_test(true);
763 TestLayerAnimationDelegate delegate
;
764 animator
->SetDelegate(&delegate
);
766 double start_opacity(0.0);
767 double target_opacity(1.0);
769 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
771 delegate
.SetOpacityFromAnimation(start_opacity
);
773 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
775 animator
->StartAnimation(
776 new LayerAnimationSequence(
777 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
779 animator
->StartAnimation(
780 new LayerAnimationSequence(
781 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
783 EXPECT_FALSE(animator
->is_animating());
784 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
787 // Preempt by animating to new target, with a non-threaded animation.
788 TEST(LayerAnimatorTest
, PreemptByImmediatelyAnimatingToNewTarget
) {
789 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
790 animator
->set_disable_timer_for_test(true);
791 TestLayerAnimationDelegate delegate
;
792 animator
->SetDelegate(&delegate
);
794 double start_brightness(0.0);
795 double middle_brightness(0.5);
796 double target_brightness(1.0);
798 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
800 delegate
.SetBrightnessFromAnimation(start_brightness
);
802 animator
->set_preemption_strategy(
803 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
805 animator
->StartAnimation(
806 new LayerAnimationSequence(
807 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
810 base::TimeTicks start_time
= animator
->last_step_time();
812 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
814 animator
->StartAnimation(
815 new LayerAnimationSequence(
816 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
819 EXPECT_TRUE(animator
->is_animating());
820 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
822 animator
->StartAnimation(
823 new LayerAnimationSequence(
824 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
827 EXPECT_TRUE(animator
->is_animating());
829 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
831 EXPECT_TRUE(animator
->is_animating());
832 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
833 0.5 * (start_brightness
+ middle_brightness
));
835 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
837 EXPECT_FALSE(animator
->is_animating());
838 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
841 // Preempt by animating to new target, with a threaded animation.
842 TEST(LayerAnimatorTest
, PreemptThreadedByImmediatelyAnimatingToNewTarget
) {
843 double epsilon
= 0.00001;
844 LayerAnimatorTestController
test_controller(
845 LayerAnimator::CreateDefaultAnimator());
846 LayerAnimator
* animator
= test_controller
.animator();
847 test_controller
.animator()->set_disable_timer_for_test(true);
848 TestLayerAnimationDelegate delegate
;
849 test_controller
.animator()->SetDelegate(&delegate
);
851 double start_opacity(0.0);
852 double middle_opacity(0.5);
853 double target_opacity(1.0);
855 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
857 delegate
.SetOpacityFromAnimation(start_opacity
);
859 test_controller
.animator()->set_preemption_strategy(
860 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
862 test_controller
.animator()->StartAnimation(
863 new LayerAnimationSequence(
864 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
866 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
867 base::TimeTicks effective_start
= start_time
+ delta
;
869 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
870 cc::AnimationEvent::Started
,
872 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
873 ->animation_group_id(),
874 cc::Animation::Opacity
,
877 animator
->Step(effective_start
+ delta
/ 2);
879 test_controller
.animator()->StartAnimation(
880 new LayerAnimationSequence(
881 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
883 EXPECT_TRUE(test_controller
.animator()->is_animating());
884 EXPECT_NEAR(delegate
.GetOpacityForAnimation(), middle_opacity
, epsilon
);
886 test_controller
.animator()->StartAnimation(
887 new LayerAnimationSequence(
888 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
)));
890 EXPECT_TRUE(test_controller
.animator()->is_animating());
892 base::TimeTicks second_effective_start
= effective_start
+ delta
;
894 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
895 cc::AnimationEvent::Started
,
897 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
898 ->animation_group_id(),
899 cc::Animation::Opacity
,
900 second_effective_start
));
902 animator
->Step(second_effective_start
+ delta
/ 2);
904 EXPECT_TRUE(test_controller
.animator()->is_animating());
907 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
908 last_progressed_fraction(),
911 animator
->Step(second_effective_start
+ delta
);
913 EXPECT_FALSE(test_controller
.animator()->is_animating());
914 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
917 // Preempt by enqueuing the new animation.
918 TEST(LayerAnimatorTest
, PreemptEnqueueNewAnimation
) {
919 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
920 animator
->set_disable_timer_for_test(true);
921 TestLayerAnimationDelegate delegate
;
922 animator
->SetDelegate(&delegate
);
924 double start_brightness(0.0);
925 double middle_brightness(0.5);
926 double target_brightness(1.0);
928 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
930 delegate
.SetBrightnessFromAnimation(start_brightness
);
932 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
934 animator
->StartAnimation(
935 new LayerAnimationSequence(
936 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
939 base::TimeTicks start_time
= animator
->last_step_time();
941 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
943 animator
->StartAnimation(
944 new LayerAnimationSequence(
945 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
948 EXPECT_TRUE(animator
->is_animating());
949 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
951 EXPECT_TRUE(animator
->is_animating());
953 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
955 EXPECT_TRUE(animator
->is_animating());
956 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
958 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
960 EXPECT_TRUE(animator
->is_animating());
961 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
963 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
965 EXPECT_FALSE(animator
->is_animating());
966 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
969 // Start an animation when there are sequences waiting in the queue. In this
970 // case, all pending and running animations should be finished, and the new
971 // animation started.
972 TEST(LayerAnimatorTest
, PreemptyByReplacingQueuedAnimations
) {
973 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
974 animator
->set_disable_timer_for_test(true);
975 TestLayerAnimationDelegate delegate
;
976 animator
->SetDelegate(&delegate
);
978 double start_brightness(0.0);
979 double middle_brightness(0.5);
980 double target_brightness(1.0);
982 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
984 delegate
.SetBrightnessFromAnimation(start_brightness
);
986 animator
->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
988 animator
->StartAnimation(
989 new LayerAnimationSequence(
990 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
993 base::TimeTicks start_time
= animator
->last_step_time();
995 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
997 animator
->StartAnimation(
998 new LayerAnimationSequence(
999 LayerAnimationElement::CreateBrightnessElement(middle_brightness
,
1002 // Queue should now have two animations. Starting a third should replace the
1004 animator
->StartAnimation(
1005 new LayerAnimationSequence(
1006 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1009 EXPECT_TRUE(animator
->is_animating());
1010 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1012 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1014 EXPECT_TRUE(animator
->is_animating());
1015 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1017 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1019 EXPECT_TRUE(animator
->is_animating());
1020 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1022 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1024 EXPECT_FALSE(animator
->is_animating());
1025 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1028 TEST(LayerAnimatorTest
, StartTogetherSetsLastStepTime
) {
1029 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1030 animator
->set_disable_timer_for_test(true);
1031 TestLayerAnimationDelegate delegate
;
1032 animator
->SetDelegate(&delegate
);
1034 double start_grayscale(0.0);
1035 double target_grayscale(1.0);
1036 double start_brightness(0.1);
1037 double target_brightness(0.9);
1039 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1041 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1042 delegate
.SetBrightnessFromAnimation(start_brightness
);
1044 animator
->set_preemption_strategy(
1045 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1047 animator
->set_last_step_time(base::TimeTicks());
1049 animator
->StartTogether(
1050 CreateMultiSequence(
1051 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1053 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1057 // If last step time was not set correctly, the resulting delta should be
1058 // miniscule (fractions of a millisecond). If set correctly, then the delta
1059 // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1060 // though a much smaller value would probably have sufficed.
1061 delta
= gfx::FrameTime::Now() - animator
->last_step_time();
1062 EXPECT_GT(60.0, delta
.InSecondsF());
1065 //-------------------------------------------------------
1066 // Preempt by immediately setting new target.
1067 TEST(LayerAnimatorTest
, MultiPreemptBySettingNewTarget
) {
1068 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1069 animator
->set_disable_timer_for_test(true);
1070 TestLayerAnimationDelegate delegate
;
1071 animator
->SetDelegate(&delegate
);
1073 double start_opacity(0.0);
1074 double target_opacity(1.0);
1075 double start_brightness(0.1);
1076 double target_brightness(0.9);
1078 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1080 delegate
.SetOpacityFromAnimation(start_opacity
);
1081 delegate
.SetBrightnessFromAnimation(start_brightness
);
1083 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
1085 animator
->StartTogether(
1086 CreateMultiSequence(
1087 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
),
1088 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1092 animator
->StartTogether(
1093 CreateMultiSequence(
1094 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1095 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1099 EXPECT_FALSE(animator
->is_animating());
1100 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1101 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1104 // Preempt by animating to new target.
1105 TEST(LayerAnimatorTest
, MultiPreemptByImmediatelyAnimatingToNewTarget
) {
1106 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1107 animator
->set_disable_timer_for_test(true);
1108 TestLayerAnimationDelegate delegate
;
1109 animator
->SetDelegate(&delegate
);
1111 double start_grayscale(0.0);
1112 double middle_grayscale(0.5);
1113 double target_grayscale(1.0);
1115 double start_brightness(0.1);
1116 double middle_brightness(0.2);
1117 double target_brightness(0.3);
1119 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1121 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1122 delegate
.SetBrightnessFromAnimation(start_brightness
);
1124 animator
->set_preemption_strategy(
1125 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1127 animator
->StartTogether(
1128 CreateMultiSequence(
1129 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1131 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1135 base::TimeTicks start_time
= animator
->last_step_time();
1137 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1139 animator
->StartTogether(
1140 CreateMultiSequence(
1141 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1142 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1145 EXPECT_TRUE(animator
->is_animating());
1146 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1147 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1149 animator
->StartTogether(
1150 CreateMultiSequence(
1151 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1152 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1155 EXPECT_TRUE(animator
->is_animating());
1157 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1159 EXPECT_TRUE(animator
->is_animating());
1160 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(),
1161 0.5 * (start_grayscale
+ middle_grayscale
));
1162 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(),
1163 0.5 * (start_brightness
+ middle_brightness
));
1165 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1167 EXPECT_FALSE(animator
->is_animating());
1168 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1169 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1172 // Preempt a threaded animation by animating to new target.
1173 TEST(LayerAnimatorTest
, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget
) {
1174 double epsilon
= 0.00001;
1175 LayerAnimatorTestController
test_controller(
1176 LayerAnimator::CreateDefaultAnimator());
1177 LayerAnimator
* animator
= test_controller
.animator();
1178 test_controller
.animator()->set_disable_timer_for_test(true);
1179 TestLayerAnimationDelegate delegate
;
1180 test_controller
.animator()->SetDelegate(&delegate
);
1182 double start_opacity(0.0);
1183 double middle_opacity(0.5);
1184 double target_opacity(1.0);
1186 double start_brightness(0.1);
1187 double middle_brightness(0.2);
1188 double target_brightness(0.3);
1190 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1192 delegate
.SetOpacityFromAnimation(start_opacity
);
1193 delegate
.SetBrightnessFromAnimation(start_brightness
);
1195 test_controller
.animator()->set_preemption_strategy(
1196 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
1198 test_controller
.animator()->StartTogether(
1199 CreateMultiSequence(
1200 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
),
1201 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1205 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
1206 base::TimeTicks effective_start
= start_time
+ delta
;
1208 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1209 cc::AnimationEvent::Started
,
1211 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1212 ->animation_group_id(),
1213 cc::Animation::Opacity
,
1216 animator
->Step(effective_start
+ delta
/ 2);
1218 test_controller
.animator()->StartTogether(
1219 CreateMultiSequence(
1220 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1221 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1224 EXPECT_TRUE(test_controller
.animator()->is_animating());
1225 EXPECT_NEAR(delegate
.GetOpacityForAnimation(), middle_opacity
, epsilon
);
1226 EXPECT_NEAR(delegate
.GetBrightnessForAnimation(), middle_brightness
, epsilon
);
1228 test_controller
.animator()->StartTogether(
1229 CreateMultiSequence(
1230 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
),
1231 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1234 EXPECT_TRUE(test_controller
.animator()->is_animating());
1236 base::TimeTicks second_effective_start
= effective_start
+ delta
;
1238 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1239 cc::AnimationEvent::Started
,
1241 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1242 ->animation_group_id(),
1243 cc::Animation::Opacity
,
1244 second_effective_start
));
1246 animator
->Step(second_effective_start
+ delta
/ 2);
1248 EXPECT_TRUE(test_controller
.animator()->is_animating());
1251 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)->
1252 last_progressed_fraction(),
1254 EXPECT_NEAR(delegate
.GetBrightnessForAnimation(),
1255 0.5 * (start_brightness
+ middle_brightness
),
1258 animator
->Step(second_effective_start
+ delta
);
1260 EXPECT_FALSE(test_controller
.animator()->is_animating());
1261 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1262 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1265 // Preempt by enqueuing the new animation.
1266 TEST(LayerAnimatorTest
, MultiPreemptEnqueueNewAnimation
) {
1267 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1268 animator
->set_disable_timer_for_test(true);
1269 TestLayerAnimationDelegate delegate
;
1270 animator
->SetDelegate(&delegate
);
1272 double start_grayscale(0.0);
1273 double middle_grayscale(0.5);
1274 double target_grayscale(1.0);
1276 double start_brightness(0.1);
1277 double middle_brightness(0.2);
1278 double target_brightness(0.3);
1280 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1282 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1283 delegate
.SetBrightnessFromAnimation(start_brightness
);
1285 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
1287 animator
->StartTogether(
1288 CreateMultiSequence(
1289 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1291 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1294 base::TimeTicks start_time
= animator
->last_step_time();
1296 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1298 animator
->StartTogether(
1299 CreateMultiSequence(
1300 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1301 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1304 EXPECT_TRUE(animator
->is_animating());
1305 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1306 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1308 EXPECT_TRUE(animator
->is_animating());
1310 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1312 EXPECT_TRUE(animator
->is_animating());
1313 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
1314 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1316 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1318 EXPECT_TRUE(animator
->is_animating());
1319 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1320 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1322 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1324 EXPECT_FALSE(animator
->is_animating());
1325 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1326 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1329 // Start an animation when there are sequences waiting in the queue. In this
1330 // case, all pending and running animations should be finished, and the new
1331 // animation started.
1332 TEST(LayerAnimatorTest
, MultiPreemptByReplacingQueuedAnimations
) {
1333 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1334 animator
->set_disable_timer_for_test(true);
1335 TestLayerAnimationDelegate delegate
;
1336 animator
->SetDelegate(&delegate
);
1338 double start_grayscale(0.0);
1339 double middle_grayscale(0.5);
1340 double target_grayscale(1.0);
1342 double start_brightness(0.1);
1343 double middle_brightness(0.2);
1344 double target_brightness(0.3);
1346 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1348 delegate
.SetGrayscaleFromAnimation(start_grayscale
);
1349 delegate
.SetBrightnessFromAnimation(start_brightness
);
1351 animator
->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS
);
1353 animator
->StartTogether(
1354 CreateMultiSequence(
1355 LayerAnimationElement::CreateGrayscaleElement(target_grayscale
,
1357 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1360 base::TimeTicks start_time
= animator
->last_step_time();
1362 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1364 animator
->StartTogether(
1365 CreateMultiSequence(
1366 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale
,
1368 LayerAnimationElement::CreateBrightnessElement(middle_brightness
,
1371 // Queue should now have two animations. Starting a third should replace the
1373 animator
->StartTogether(
1374 CreateMultiSequence(
1375 LayerAnimationElement::CreateGrayscaleElement(start_grayscale
, delta
),
1376 LayerAnimationElement::CreateBrightnessElement(start_brightness
,
1379 EXPECT_TRUE(animator
->is_animating());
1380 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1381 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1383 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1385 EXPECT_TRUE(animator
->is_animating());
1386 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), target_grayscale
);
1387 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1389 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
1391 EXPECT_TRUE(animator
->is_animating());
1392 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), middle_grayscale
);
1393 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), middle_brightness
);
1395 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1397 EXPECT_FALSE(animator
->is_animating());
1398 EXPECT_FLOAT_EQ(delegate
.GetGrayscaleForAnimation(), start_grayscale
);
1399 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1401 //-------------------------------------------------------
1402 // Test that non-threaded cyclic sequences continue to animate.
1403 TEST(LayerAnimatorTest
, CyclicSequences
) {
1404 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1405 animator
->set_disable_timer_for_test(true);
1406 TestLayerAnimationDelegate delegate
;
1407 animator
->SetDelegate(&delegate
);
1409 double start_brightness(0.0);
1410 double target_brightness(1.0);
1412 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1414 delegate
.SetBrightnessFromAnimation(start_brightness
);
1416 scoped_ptr
<LayerAnimationSequence
> sequence(
1417 new LayerAnimationSequence(
1418 LayerAnimationElement::CreateBrightnessElement(target_brightness
,
1421 sequence
->AddElement(
1422 LayerAnimationElement::CreateBrightnessElement(start_brightness
, delta
));
1424 sequence
->set_is_cyclic(true);
1426 animator
->StartAnimation(sequence
.release());
1428 base::TimeTicks start_time
= animator
->last_step_time();
1430 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1432 EXPECT_TRUE(animator
->is_animating());
1433 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1435 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(2000));
1437 EXPECT_TRUE(animator
->is_animating());
1438 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1440 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(3000));
1442 EXPECT_TRUE(animator
->is_animating());
1443 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1445 // Skip ahead by a lot.
1446 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000000000));
1448 EXPECT_TRUE(animator
->is_animating());
1449 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), start_brightness
);
1451 // Skip ahead by a lot.
1452 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000001000));
1454 EXPECT_TRUE(animator
->is_animating());
1455 EXPECT_FLOAT_EQ(delegate
.GetBrightnessForAnimation(), target_brightness
);
1457 animator
->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS
);
1459 EXPECT_FALSE(animator
->is_animating());
1462 // Test that threaded cyclic sequences continue to animate.
1463 TEST(LayerAnimatorTest
, ThreadedCyclicSequences
) {
1464 LayerAnimatorTestController
test_controller(
1465 LayerAnimator::CreateDefaultAnimator());
1466 LayerAnimator
* animator
= test_controller
.animator();
1467 test_controller
.animator()->set_disable_timer_for_test(true);
1468 TestLayerAnimationDelegate delegate
;
1469 test_controller
.animator()->SetDelegate(&delegate
);
1471 double start_opacity(0.0);
1472 double target_opacity(1.0);
1474 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1476 delegate
.SetOpacityFromAnimation(start_opacity
);
1478 scoped_ptr
<LayerAnimationSequence
> sequence(
1479 new LayerAnimationSequence(
1480 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
1482 sequence
->AddElement(
1483 LayerAnimationElement::CreateOpacityElement(start_opacity
, delta
));
1485 sequence
->set_is_cyclic(true);
1487 test_controller
.animator()->StartAnimation(sequence
.release());
1489 base::TimeTicks start_time
= test_controller
.animator()->last_step_time();
1490 base::TimeTicks effective_start
= start_time
+ delta
;
1492 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1493 cc::AnimationEvent::Started
,
1495 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1496 ->animation_group_id(),
1497 cc::Animation::Opacity
,
1500 animator
->Step(effective_start
+ delta
);
1501 EXPECT_TRUE(test_controller
.animator()->is_animating());
1502 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1504 base::TimeTicks second_effective_start
= effective_start
+ 2 * delta
;
1505 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1506 cc::AnimationEvent::Started
,
1508 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1509 ->animation_group_id(),
1510 cc::Animation::Opacity
,
1511 second_effective_start
));
1513 animator
->Step(second_effective_start
+ delta
);
1515 EXPECT_TRUE(test_controller
.animator()->is_animating());
1516 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1518 base::TimeTicks third_effective_start
= second_effective_start
+ 2 * delta
;
1519 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1520 cc::AnimationEvent::Started
,
1522 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1523 ->animation_group_id(),
1524 cc::Animation::Opacity
,
1525 third_effective_start
));
1527 animator
->Step(third_effective_start
+ delta
);
1528 EXPECT_TRUE(test_controller
.animator()->is_animating());
1529 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1531 base::TimeTicks fourth_effective_start
= third_effective_start
+ 2 * delta
;
1532 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1533 cc::AnimationEvent::Started
,
1535 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1536 ->animation_group_id(),
1537 cc::Animation::Opacity
,
1538 fourth_effective_start
));
1540 // Skip ahead by a lot.
1541 animator
->Step(fourth_effective_start
+ 1000 * delta
);
1543 EXPECT_TRUE(test_controller
.animator()->is_animating());
1544 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), target_opacity
);
1546 base::TimeTicks fifth_effective_start
= fourth_effective_start
+ 1001 * delta
;
1547 test_controller
.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1548 cc::AnimationEvent::Started
,
1550 test_controller
.GetRunningSequence(LayerAnimationElement::OPACITY
)
1551 ->animation_group_id(),
1552 cc::Animation::Opacity
,
1553 fifth_effective_start
));
1555 // Skip ahead by a lot.
1556 animator
->Step(fifth_effective_start
+ 999 * delta
);
1558 EXPECT_TRUE(test_controller
.animator()->is_animating());
1559 EXPECT_FLOAT_EQ(delegate
.GetOpacityForAnimation(), start_opacity
);
1561 test_controller
.animator()->StopAnimatingProperty(
1562 LayerAnimationElement::OPACITY
);
1564 EXPECT_FALSE(test_controller
.animator()->is_animating());
1567 TEST(LayerAnimatorTest
, AddObserverExplicit
) {
1568 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1569 animator
->set_disable_timer_for_test(true);
1570 TestLayerAnimationObserver observer
;
1571 TestLayerAnimationDelegate delegate
;
1572 animator
->SetDelegate(&delegate
);
1573 animator
->AddObserver(&observer
);
1574 observer
.set_requires_notification_when_animator_destroyed(true);
1576 EXPECT_TRUE(!observer
.last_ended_sequence());
1578 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1580 delegate
.SetBrightnessFromAnimation(0.0f
);
1582 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1583 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1585 animator
->StartAnimation(sequence
);
1587 EXPECT_EQ(observer
.last_scheduled_sequence(), sequence
);
1589 base::TimeTicks start_time
= animator
->last_step_time();
1591 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1593 EXPECT_EQ(observer
.last_ended_sequence(), sequence
);
1595 // |sequence| has been destroyed. Recreate it to test abort.
1596 sequence
= new LayerAnimationSequence(
1597 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1599 animator
->StartAnimation(sequence
);
1603 EXPECT_EQ(observer
.last_aborted_sequence(), sequence
);
1606 // Tests that an observer added to a scoped settings object is still notified
1607 // when the object goes out of scope.
1608 TEST(LayerAnimatorTest
, ImplicitAnimationObservers
) {
1609 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1610 animator
->set_disable_timer_for_test(true);
1611 TestImplicitAnimationObserver
observer(false);
1612 TestLayerAnimationDelegate delegate
;
1613 animator
->SetDelegate(&delegate
);
1615 EXPECT_FALSE(observer
.animations_completed());
1616 animator
->SetBrightness(1.0f
);
1619 ScopedLayerAnimationSettings
settings(animator
.get());
1620 settings
.AddObserver(&observer
);
1621 animator
->SetBrightness(0.0f
);
1624 EXPECT_FALSE(observer
.animations_completed());
1625 base::TimeTicks start_time
= animator
->last_step_time();
1626 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1627 EXPECT_TRUE(observer
.animations_completed());
1628 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1629 LayerAnimationElement::BRIGHTNESS
));
1630 EXPECT_FLOAT_EQ(0.0f
, delegate
.GetBrightnessForAnimation());
1633 // Tests that an observer added to a scoped settings object is still notified
1634 // when the object goes out of scope due to the animation being interrupted.
1635 TEST(LayerAnimatorTest
, InterruptedImplicitAnimationObservers
) {
1636 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1637 animator
->set_disable_timer_for_test(true);
1638 TestImplicitAnimationObserver
observer(false);
1639 TestLayerAnimationDelegate delegate
;
1640 animator
->SetDelegate(&delegate
);
1642 EXPECT_FALSE(observer
.animations_completed());
1643 animator
->SetBrightness(1.0f
);
1646 ScopedLayerAnimationSettings
settings(animator
.get());
1647 settings
.AddObserver(&observer
);
1648 animator
->SetBrightness(0.0f
);
1651 EXPECT_FALSE(observer
.animations_completed());
1652 // This should interrupt the implicit animation causing the observer to be
1653 // notified immediately.
1654 animator
->SetBrightness(1.0f
);
1655 EXPECT_TRUE(observer
.animations_completed());
1656 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1657 LayerAnimationElement::BRIGHTNESS
));
1658 EXPECT_FLOAT_EQ(1.0f
, delegate
.GetBrightnessForAnimation());
1661 // Tests that LayerAnimator is not deleted after the animation completes as long
1662 // as there is a live ScopedLayerAnimationSettings object wrapped around it.
1663 TEST(LayerAnimatorTest
, AnimatorKeptAliveBySettings
) {
1664 // Note we are using a raw pointer unlike in other tests.
1665 TestLayerAnimator
* animator
= new TestLayerAnimator();
1666 LayerAnimatorDestructionObserver destruction_observer
;
1667 animator
->SetDestructionObserver(&destruction_observer
);
1668 animator
->set_disable_timer_for_test(true);
1669 TestLayerAnimationDelegate delegate
;
1670 animator
->SetDelegate(&delegate
);
1672 // ScopedLayerAnimationSettings should keep the Animator alive as long as
1673 // it is alive, even beyond the end of the animation.
1674 ScopedLayerAnimationSettings
settings(animator
);
1675 base::TimeTicks now
= gfx::FrameTime::Now();
1676 animator
->SetBrightness(0.5);
1677 animator
->Step(now
+ base::TimeDelta::FromSeconds(1));
1678 EXPECT_FALSE(destruction_observer
.IsAnimatorDeleted());
1680 // ScopedLayerAnimationSettings was destroyed, so Animator should be deleted.
1681 EXPECT_TRUE(destruction_observer
.IsAnimatorDeleted());
1684 // Tests that an observer added to a scoped settings object is not notified
1685 // when the animator is destroyed unless explicitly requested.
1686 TEST(LayerAnimatorTest
, ImplicitObserversAtAnimatorDestruction
) {
1687 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1688 animator
->set_disable_timer_for_test(true);
1689 TestImplicitAnimationObserver
observer_notify(true);
1690 TestImplicitAnimationObserver
observer_do_not_notify(false);
1691 TestLayerAnimationDelegate delegate
;
1692 animator
->SetDelegate(&delegate
);
1694 EXPECT_FALSE(observer_notify
.animations_completed());
1695 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1697 animator
->SetBrightness(1.0f
);
1700 ScopedLayerAnimationSettings
settings(animator
.get());
1701 settings
.AddObserver(&observer_notify
);
1702 settings
.AddObserver(&observer_do_not_notify
);
1703 animator
->SetBrightness(0.0f
);
1706 EXPECT_FALSE(observer_notify
.animations_completed());
1707 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1709 EXPECT_TRUE(observer_notify
.animations_completed());
1710 EXPECT_TRUE(observer_notify
.WasAnimationAbortedForProperty(
1711 LayerAnimationElement::BRIGHTNESS
));
1712 EXPECT_FALSE(observer_do_not_notify
.animations_completed());
1715 TEST(LayerAnimatorTest
, AbortedAnimationStatusInImplicitObservers
) {
1716 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1717 animator
->set_disable_timer_for_test(true);
1718 TestImplicitAnimationObserver
observer(false);
1719 TestLayerAnimationDelegate delegate
;
1720 animator
->SetDelegate(&delegate
);
1722 EXPECT_FALSE(observer
.animations_completed());
1723 animator
->SetBrightness(1.0f
);
1726 ScopedLayerAnimationSettings
settings(animator
.get());
1727 settings
.AddObserver(&observer
);
1728 animator
->SetBrightness(0.0f
);
1730 EXPECT_FALSE(observer
.animations_completed());
1732 animator
->AbortAllAnimations();
1733 EXPECT_TRUE(observer
.animations_completed());
1734 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1735 LayerAnimationElement::BRIGHTNESS
));
1736 EXPECT_FALSE(observer
.WasAnimationAbortedForProperty(
1737 LayerAnimationElement::OPACITY
));
1739 observer
.set_animations_completed(false);
1741 ScopedLayerAnimationSettings
settings(animator
.get());
1742 settings
.AddObserver(&observer
);
1743 animator
->SetOpacity(0.0f
);
1745 EXPECT_FALSE(observer
.animations_completed());
1747 animator
->AbortAllAnimations();
1748 EXPECT_TRUE(observer
.animations_completed());
1749 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1750 LayerAnimationElement::BRIGHTNESS
));
1751 EXPECT_TRUE(observer
.WasAnimationAbortedForProperty(
1752 LayerAnimationElement::OPACITY
));
1755 TEST(LayerAnimatorTest
, RemoveObserverShouldRemoveFromSequences
) {
1756 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1757 animator
->set_disable_timer_for_test(true);
1758 TestLayerAnimationObserver observer
;
1759 TestLayerAnimationObserver removed_observer
;
1760 TestLayerAnimationDelegate delegate
;
1761 animator
->SetDelegate(&delegate
);
1763 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1765 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1766 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1768 sequence
->AddObserver(&observer
);
1769 sequence
->AddObserver(&removed_observer
);
1771 animator
->StartAnimation(sequence
);
1773 EXPECT_EQ(observer
.last_scheduled_sequence(), sequence
);
1774 EXPECT_TRUE(!observer
.last_ended_sequence());
1775 EXPECT_EQ(removed_observer
.last_scheduled_sequence(), sequence
);
1776 EXPECT_TRUE(!removed_observer
.last_ended_sequence());
1778 // This should stop the observer from observing sequence.
1779 animator
->RemoveObserver(&removed_observer
);
1781 base::TimeTicks start_time
= animator
->last_step_time();
1783 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1785 EXPECT_EQ(observer
.last_ended_sequence(), sequence
);
1786 EXPECT_TRUE(!removed_observer
.last_ended_sequence());
1789 TEST(LayerAnimatorTest
, ObserverReleasedBeforeAnimationSequenceEnds
) {
1790 TestLayerAnimationDelegate delegate
;
1791 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1792 animator
->set_disable_timer_for_test(true);
1794 scoped_ptr
<TestLayerAnimationObserver
> observer(
1795 new TestLayerAnimationObserver
);
1796 animator
->SetDelegate(&delegate
);
1797 animator
->AddObserver(observer
.get());
1799 delegate
.SetOpacityFromAnimation(0.0f
);
1801 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1802 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1803 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
));
1805 animator
->StartAnimation(sequence
);
1807 // |observer| should be attached to |sequence|.
1808 EXPECT_TRUE(sequence
->observers_
.might_have_observers());
1810 // Now, release |observer|
1813 // And |sequence| should no longer be attached to |observer|.
1814 EXPECT_FALSE(sequence
->observers_
.might_have_observers());
1817 TEST(LayerAnimatorTest
, ObserverAttachedAfterAnimationStarted
) {
1818 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1819 animator
->set_disable_timer_for_test(true);
1821 TestImplicitAnimationObserver
observer(false);
1822 TestLayerAnimationDelegate delegate
;
1823 animator
->SetDelegate(&delegate
);
1825 delegate
.SetBrightnessFromAnimation(0.0f
);
1828 ScopedLayerAnimationSettings
setter(animator
.get());
1830 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1831 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1832 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1834 animator
->StartAnimation(sequence
);
1835 base::TimeTicks start_time
= animator
->last_step_time();
1836 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1838 setter
.AddObserver(&observer
);
1840 // Start observing an in-flight animation.
1841 sequence
->AddObserver(&observer
);
1843 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
1846 EXPECT_TRUE(observer
.animations_completed());
1847 EXPECT_TRUE(observer
.WasAnimationCompletedForProperty(
1848 LayerAnimationElement::BRIGHTNESS
));
1851 TEST(LayerAnimatorTest
, ObserverDetachedBeforeAnimationFinished
) {
1852 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
1853 animator
->set_disable_timer_for_test(true);
1855 TestImplicitAnimationObserver
observer(false);
1856 TestLayerAnimationDelegate delegate
;
1857 animator
->SetDelegate(&delegate
);
1859 delegate
.SetBrightnessFromAnimation(0.0f
);
1860 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
1861 LayerAnimationSequence
* sequence
= new LayerAnimationSequence(
1862 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
1865 ScopedLayerAnimationSettings
setter(animator
.get());
1866 setter
.AddObserver(&observer
);
1868 animator
->StartAnimation(sequence
);
1869 base::TimeTicks start_time
= animator
->last_step_time();
1870 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
1873 EXPECT_FALSE(observer
.animations_completed());
1875 // Stop observing an in-flight animation.
1876 sequence
->RemoveObserver(&observer
);
1878 EXPECT_TRUE(observer
.animations_completed());
1880 // The animation didn't complete, and neither was it aborted.
1881 EXPECT_FALSE(observer
.WasAnimationCompletedForProperty(
1882 LayerAnimationElement::BRIGHTNESS
));
1883 EXPECT_FALSE(observer
.WasAnimationAbortedForProperty(
1884 LayerAnimationElement::BRIGHTNESS
));
1887 // This checks that if an animation is deleted due to a callback, that the
1888 // animator does not try to use the deleted animation. For example, if we have
1889 // two running animations, and the first finishes and the resulting callback
1890 // causes the second to be deleted, we should not attempt to animate the second
1892 TEST(LayerAnimatorTest
, ObserverDeletesAnimationsOnEnd
) {
1893 ScopedAnimationDurationScaleMode
normal_duration_mode(
1894 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
1895 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1896 animator
->set_disable_timer_for_test(true);
1897 TestLayerAnimationDelegate delegate
;
1898 animator
->SetDelegate(&delegate
);
1900 double start_brightness(0.0);
1901 double target_brightness(1.0);
1903 gfx::Rect
start_bounds(0, 0, 50, 50);
1904 gfx::Rect
target_bounds(5, 5, 5, 5);
1906 delegate
.SetBrightnessFromAnimation(start_brightness
);
1907 delegate
.SetBoundsFromAnimation(start_bounds
);
1909 base::TimeDelta brightness_delta
= base::TimeDelta::FromSeconds(1);
1910 base::TimeDelta halfway_delta
= base::TimeDelta::FromSeconds(2);
1911 base::TimeDelta bounds_delta
= base::TimeDelta::FromSeconds(3);
1913 scoped_ptr
<DeletingLayerAnimationObserver
> observer(
1914 new DeletingLayerAnimationObserver(animator
.get()));
1916 animator
->AddObserver(observer
.get());
1918 animator
->StartAnimation(
1919 new LayerAnimationSequence(
1920 LayerAnimationElement::CreateBrightnessElement(
1921 target_brightness
, brightness_delta
)));
1923 animator
->StartAnimation(new LayerAnimationSequence(
1924 LayerAnimationElement::CreateBoundsElement(
1925 target_bounds
, bounds_delta
)));
1926 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1928 base::TimeTicks start_time
= animator
->last_step_time();
1929 animator
->Step(start_time
+ halfway_delta
);
1931 // Completing the brightness animation should have stopped the bounds
1933 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1935 animator
->RemoveObserver(observer
.get());
1938 // Ensure that stopping animation in a bounds change does not crash and that
1939 // animation gets stopped correctly.
1940 // This scenario is possible when animation is restarted from inside a
1941 // callback triggered by the animation progress.
1942 TEST(LayerAnimatorTest
, CallbackDeletesAnimationInProgress
) {
1944 class TestLayerAnimationDeletingDelegate
: public TestLayerAnimationDelegate
{
1946 TestLayerAnimationDeletingDelegate(LayerAnimator
* animator
, int max_width
)
1947 : animator_(animator
),
1948 max_width_(max_width
) {
1951 virtual void SetBoundsFromAnimation(const gfx::Rect
& bounds
) OVERRIDE
{
1952 TestLayerAnimationDelegate::SetBoundsFromAnimation(bounds
);
1953 if (bounds
.width() > max_width_
)
1954 animator_
->StopAnimating();
1957 LayerAnimator
* animator_
;
1959 // Allow copy and assign.
1962 ScopedAnimationDurationScaleMode
normal_duration_mode(
1963 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
1964 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
1965 animator
->set_disable_timer_for_test(true);
1966 TestLayerAnimationDeletingDelegate
delegate(animator
.get(), 30);
1967 animator
->SetDelegate(&delegate
);
1969 gfx::Rect
start_bounds(0, 0, 0, 0);
1970 gfx::Rect
target_bounds(5, 5, 50, 50);
1972 delegate
.SetBoundsFromAnimation(start_bounds
);
1974 base::TimeDelta bounds_delta1
= base::TimeDelta::FromMilliseconds(333);
1975 base::TimeDelta bounds_delta2
= base::TimeDelta::FromMilliseconds(666);
1976 base::TimeDelta bounds_delta
= base::TimeDelta::FromMilliseconds(1000);
1977 base::TimeDelta final_delta
= base::TimeDelta::FromMilliseconds(1500);
1979 animator
->StartAnimation(new LayerAnimationSequence(
1980 LayerAnimationElement::CreateBoundsElement(
1981 target_bounds
, bounds_delta
)));
1982 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1984 base::TimeTicks start_time
= animator
->last_step_time();
1985 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ bounds_delta1
));
1986 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1988 // The next step should change the animated bounds past the threshold and
1989 // cause the animaton to stop.
1990 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ bounds_delta2
));
1991 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1992 ASSERT_NO_FATAL_FAILURE(animator
->Step(start_time
+ final_delta
));
1994 // Completing the animation should have stopped the bounds
1996 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
1999 // Similar to the ObserverDeletesAnimationsOnEnd test above except that it
2000 // tests the behavior when the OnLayerAnimationAborted() callback causes
2001 // all of the animator's other animations to be deleted.
2002 TEST(LayerAnimatorTest
, ObserverDeletesAnimationsOnAbort
) {
2003 ScopedAnimationDurationScaleMode
normal_duration_mode(
2004 ScopedAnimationDurationScaleMode::NORMAL_DURATION
);
2005 scoped_refptr
<LayerAnimator
> animator(new TestLayerAnimator());
2006 animator
->set_disable_timer_for_test(true);
2007 TestLayerAnimationDelegate delegate
;
2008 animator
->SetDelegate(&delegate
);
2010 double start_brightness(0.0);
2011 double target_brightness(1.0);
2012 gfx::Rect
start_bounds(0, 0, 50, 50);
2013 gfx::Rect
target_bounds(5, 5, 5, 5);
2014 base::TimeDelta brightness_delta
= base::TimeDelta::FromSeconds(1);
2015 base::TimeDelta bounds_delta
= base::TimeDelta::FromSeconds(2);
2017 delegate
.SetBrightnessFromAnimation(start_brightness
);
2018 delegate
.SetBoundsFromAnimation(start_bounds
);
2020 scoped_ptr
<DeletingLayerAnimationObserver
> observer(
2021 new DeletingLayerAnimationObserver(animator
.get()));
2022 animator
->AddObserver(observer
.get());
2024 animator
->StartAnimation(
2025 new LayerAnimationSequence(
2026 LayerAnimationElement::CreateBrightnessElement(
2027 target_brightness
, brightness_delta
)));
2028 animator
->StartAnimation(new LayerAnimationSequence(
2029 LayerAnimationElement::CreateBoundsElement(
2030 target_bounds
, bounds_delta
)));
2031 ASSERT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2033 animator
->set_preemption_strategy(
2034 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2035 animator
->StartAnimation(
2036 new LayerAnimationSequence(
2037 LayerAnimationElement::CreateBrightnessElement(
2038 target_brightness
, brightness_delta
)));
2040 // Starting the second brightness animation should have aborted the initial
2041 // brightness animation. |observer| should have stopped the bounds animation
2043 ASSERT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2045 animator
->RemoveObserver(observer
.get());
2048 // Check that setting a property during an animation with a default animator
2049 // cancels the original animation.
2050 TEST(LayerAnimatorTest
, SettingPropertyDuringAnAnimation
) {
2051 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2052 animator
->set_disable_timer_for_test(true);
2053 TestLayerAnimationDelegate delegate
;
2054 animator
->SetDelegate(&delegate
);
2056 double start_opacity(0.0);
2057 double target_opacity(1.0);
2059 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2061 delegate
.SetOpacityFromAnimation(start_opacity
);
2063 scoped_ptr
<LayerAnimationSequence
> sequence(
2064 new LayerAnimationSequence(
2065 LayerAnimationElement::CreateOpacityElement(target_opacity
, delta
)));
2067 animator
->StartAnimation(sequence
.release());
2069 animator
->SetOpacity(0.5);
2071 EXPECT_FALSE(animator
->is_animating());
2072 EXPECT_EQ(0.5, animator
->GetTargetOpacity());
2075 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
2076 // second sequence to be leaked.
2077 TEST(LayerAnimatorTest
, ImmediatelySettingNewTargetDoesNotLeak
) {
2078 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2079 animator
->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
);
2080 animator
->set_disable_timer_for_test(true);
2081 TestLayerAnimationDelegate delegate
;
2082 animator
->SetDelegate(&delegate
);
2084 gfx::Rect
start_bounds(0, 0, 50, 50);
2085 gfx::Rect
middle_bounds(10, 10, 100, 100);
2086 gfx::Rect
target_bounds(5, 5, 5, 5);
2088 delegate
.SetBoundsFromAnimation(start_bounds
);
2091 // start an implicit bounds animation.
2092 ScopedLayerAnimationSettings
settings(animator
.get());
2093 animator
->SetBounds(middle_bounds
);
2096 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2098 int num_live_instances
= 0;
2099 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2100 scoped_ptr
<TestLayerAnimationSequence
> sequence(
2101 new TestLayerAnimationSequence(
2102 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
),
2103 &num_live_instances
));
2105 EXPECT_EQ(1, num_live_instances
);
2107 // This should interrupt the running sequence causing us to immediately set
2108 // the target value. The sequence should alse be destructed.
2109 animator
->StartAnimation(sequence
.release());
2111 EXPECT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2112 EXPECT_EQ(0, num_live_instances
);
2113 CheckApproximatelyEqual(delegate
.GetBoundsForAnimation(), target_bounds
);
2116 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
2117 TEST(LayerAnimatorTest
, GetTargetOpacity
) {
2118 TestLayerAnimationDelegate delegate
;
2119 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2120 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2121 animator
->set_disable_timer_for_test(true);
2122 animator
->SetDelegate(&delegate
);
2124 delegate
.SetOpacityFromAnimation(0.0);
2127 ScopedLayerAnimationSettings
settings(animator
.get());
2128 animator
->SetOpacity(0.5);
2129 EXPECT_EQ(0.5, animator
->GetTargetOpacity());
2131 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2132 animator
->SetOpacity(1.0);
2133 EXPECT_EQ(1.0, animator
->GetTargetOpacity());
2137 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
2138 TEST(LayerAnimatorTest
, GetTargetBrightness
) {
2139 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2140 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2141 animator
->set_disable_timer_for_test(true);
2142 TestLayerAnimationDelegate delegate
;
2143 animator
->SetDelegate(&delegate
);
2145 delegate
.SetBrightnessFromAnimation(0.0);
2148 ScopedLayerAnimationSettings
settings(animator
.get());
2149 animator
->SetBrightness(0.5);
2150 EXPECT_EQ(0.5, animator
->GetTargetBrightness());
2152 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2153 animator
->SetBrightness(1.0);
2154 EXPECT_EQ(1.0, animator
->GetTargetBrightness());
2158 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
2159 TEST(LayerAnimatorTest
, GetTargetGrayscale
) {
2160 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2161 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2162 animator
->set_disable_timer_for_test(true);
2163 TestLayerAnimationDelegate delegate
;
2164 animator
->SetDelegate(&delegate
);
2166 delegate
.SetGrayscaleFromAnimation(0.0);
2169 ScopedLayerAnimationSettings
settings(animator
.get());
2170 animator
->SetGrayscale(0.5);
2171 EXPECT_EQ(0.5, animator
->GetTargetGrayscale());
2173 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2174 animator
->SetGrayscale(1.0);
2175 EXPECT_EQ(1.0, animator
->GetTargetGrayscale());
2179 // Verifies color property is modified appropriately.
2180 TEST(LayerAnimatorTest
, Color
) {
2181 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2182 animator
->set_disable_timer_for_test(true);
2183 TestLayerAnimationDelegate delegate
;
2184 animator
->SetDelegate(&delegate
);
2186 SkColor start_color
= SkColorSetARGB( 64, 20, 40, 60);
2187 SkColor middle_color
= SkColorSetARGB(128, 35, 70, 120);
2188 SkColor target_color
= SkColorSetARGB(192, 40, 80, 140);
2190 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2192 delegate
.SetColorFromAnimation(start_color
);
2194 animator
->ScheduleAnimation(
2195 new LayerAnimationSequence(
2196 LayerAnimationElement::CreateColorElement(target_color
, delta
)));
2198 EXPECT_TRUE(animator
->is_animating());
2199 EXPECT_EQ(ColorToString(start_color
),
2200 ColorToString(delegate
.GetColorForAnimation()));
2202 base::TimeTicks start_time
= animator
->last_step_time();
2204 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(500));
2206 EXPECT_TRUE(animator
->is_animating());
2207 EXPECT_EQ(ColorToString(middle_color
),
2208 ColorToString(delegate
.GetColorForAnimation()));
2210 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1000));
2212 EXPECT_FALSE(animator
->is_animating());
2213 EXPECT_EQ(ColorToString(target_color
),
2214 ColorToString(delegate
.GetColorForAnimation()));
2217 // Verifies SchedulePauseForProperties().
2218 TEST(LayerAnimatorTest
, SchedulePauseForProperties
) {
2219 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2220 animator
->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2221 animator
->SchedulePauseForProperties(
2222 base::TimeDelta::FromMilliseconds(100),
2223 LayerAnimationElement::TRANSFORM
| LayerAnimationElement::BOUNDS
);
2224 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::TRANSFORM
));
2225 EXPECT_TRUE(animator
->IsAnimatingProperty(LayerAnimationElement::BOUNDS
));
2226 EXPECT_FALSE(animator
->IsAnimatingProperty(LayerAnimationElement::OPACITY
));
2230 class AnimatorOwner
{
2233 : animator_(LayerAnimator::CreateDefaultAnimator()) {
2236 LayerAnimator
* animator() { return animator_
.get(); }
2239 scoped_refptr
<LayerAnimator
> animator_
;
2241 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner
);
2244 class DeletingObserver
: public LayerAnimationObserver
{
2246 DeletingObserver(bool* was_deleted
)
2247 : animator_owner_(new AnimatorOwner
),
2248 delete_on_animation_ended_(false),
2249 delete_on_animation_aborted_(false),
2250 delete_on_animation_scheduled_(false),
2251 was_deleted_(was_deleted
) {
2252 animator()->AddObserver(this);
2255 virtual ~DeletingObserver() {
2256 animator()->RemoveObserver(this);
2257 *was_deleted_
= true;
2260 LayerAnimator
* animator() { return animator_owner_
->animator(); }
2262 bool delete_on_animation_ended() const {
2263 return delete_on_animation_ended_
;
2265 void set_delete_on_animation_ended(bool enabled
) {
2266 delete_on_animation_ended_
= enabled
;
2269 bool delete_on_animation_aborted() const {
2270 return delete_on_animation_aborted_
;
2272 void set_delete_on_animation_aborted(bool enabled
) {
2273 delete_on_animation_aborted_
= enabled
;
2276 bool delete_on_animation_scheduled() const {
2277 return delete_on_animation_scheduled_
;
2279 void set_delete_on_animation_scheduled(bool enabled
) {
2280 delete_on_animation_scheduled_
= enabled
;
2283 // LayerAnimationObserver implementation.
2284 virtual void OnLayerAnimationEnded(
2285 LayerAnimationSequence
* sequence
) OVERRIDE
{
2286 if (delete_on_animation_ended_
)
2290 virtual void OnLayerAnimationAborted(
2291 LayerAnimationSequence
* sequence
) OVERRIDE
{
2292 if (delete_on_animation_aborted_
)
2296 virtual void OnLayerAnimationScheduled(
2297 LayerAnimationSequence
* sequence
) OVERRIDE
{
2298 if (delete_on_animation_scheduled_
)
2303 scoped_ptr
<AnimatorOwner
> animator_owner_
;
2304 bool delete_on_animation_ended_
;
2305 bool delete_on_animation_aborted_
;
2306 bool delete_on_animation_scheduled_
;
2309 DISALLOW_COPY_AND_ASSIGN(DeletingObserver
);
2312 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterFinishingAnimation
) {
2313 bool observer_was_deleted
= false;
2314 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2315 observer
->set_delete_on_animation_ended(true);
2316 observer
->set_delete_on_animation_aborted(true);
2317 LayerAnimator
* animator
= observer
->animator();
2318 animator
->set_disable_timer_for_test(true);
2319 TestLayerAnimationDelegate delegate
;
2320 animator
->SetDelegate(&delegate
);
2322 delegate
.SetBrightnessFromAnimation(0.0f
);
2324 gfx::Rect
start_bounds(0, 0, 50, 50);
2325 gfx::Rect
target_bounds(10, 10, 100, 100);
2327 delegate
.SetBoundsFromAnimation(start_bounds
);
2329 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2330 LayerAnimationSequence
* brightness_sequence
= new LayerAnimationSequence(
2331 LayerAnimationElement::CreateBrightnessElement(1.0f
, delta
));
2332 animator
->StartAnimation(brightness_sequence
);
2334 delta
= base::TimeDelta::FromSeconds(2);
2335 LayerAnimationSequence
* bounds_sequence
= new LayerAnimationSequence(
2336 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
2337 animator
->StartAnimation(bounds_sequence
);
2339 base::TimeTicks start_time
= animator
->last_step_time();
2340 animator
->Step(start_time
+ base::TimeDelta::FromMilliseconds(1500));
2342 EXPECT_TRUE(observer_was_deleted
);
2345 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterStoppingAnimating
) {
2346 bool observer_was_deleted
= false;
2347 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2348 observer
->set_delete_on_animation_ended(true);
2349 observer
->set_delete_on_animation_aborted(true);
2350 LayerAnimator
* animator
= observer
->animator();
2351 animator
->set_disable_timer_for_test(true);
2352 TestLayerAnimationDelegate delegate
;
2353 animator
->SetDelegate(&delegate
);
2355 delegate
.SetOpacityFromAnimation(0.0f
);
2357 gfx::Rect
start_bounds(0, 0, 50, 50);
2358 gfx::Rect
target_bounds(10, 10, 100, 100);
2360 delegate
.SetBoundsFromAnimation(start_bounds
);
2362 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2363 LayerAnimationSequence
* opacity_sequence
= new LayerAnimationSequence(
2364 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
));
2365 animator
->StartAnimation(opacity_sequence
);
2367 delta
= base::TimeDelta::FromSeconds(2);
2368 LayerAnimationSequence
* bounds_sequence
= new LayerAnimationSequence(
2369 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
));
2370 animator
->StartAnimation(bounds_sequence
);
2372 animator
->StopAnimating();
2374 EXPECT_TRUE(observer_was_deleted
);
2377 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterScheduling
) {
2378 bool observer_was_deleted
= false;
2379 TestLayerAnimationDelegate delegate
;
2380 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2381 observer
->set_delete_on_animation_scheduled(true);
2382 LayerAnimator
* animator
= observer
->animator();
2383 animator
->set_disable_timer_for_test(true);
2384 animator
->SetDelegate(&delegate
);
2386 delegate
.SetOpacityFromAnimation(0.0f
);
2388 gfx::Rect
start_bounds(0, 0, 50, 50);
2389 gfx::Rect
target_bounds(10, 10, 100, 100);
2391 delegate
.SetBoundsFromAnimation(start_bounds
);
2393 std::vector
<LayerAnimationSequence
*> to_start
;
2395 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2396 to_start
.push_back(new LayerAnimationSequence(
2397 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2399 delta
= base::TimeDelta::FromSeconds(2);
2400 to_start
.push_back(new LayerAnimationSequence(
2401 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
2403 animator
->ScheduleTogether(to_start
);
2405 EXPECT_TRUE(observer_was_deleted
);
2408 TEST(LayerAnimatorTest
, ObserverDeletesAnimatorAfterAborted
) {
2409 bool observer_was_deleted
= false;
2410 DeletingObserver
* observer
= new DeletingObserver(&observer_was_deleted
);
2411 TestLayerAnimationDelegate delegate
;
2412 observer
->set_delete_on_animation_aborted(true);
2413 LayerAnimator
* animator
= observer
->animator();
2414 animator
->set_preemption_strategy(
2415 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2416 animator
->set_disable_timer_for_test(true);
2417 animator
->SetDelegate(&delegate
);
2419 delegate
.SetOpacityFromAnimation(0.0f
);
2421 gfx::Rect
start_bounds(0, 0, 50, 50);
2422 gfx::Rect
target_bounds(10, 10, 100, 100);
2424 delegate
.SetBoundsFromAnimation(start_bounds
);
2426 std::vector
<LayerAnimationSequence
*> to_start
;
2428 base::TimeDelta delta
= base::TimeDelta::FromSeconds(1);
2429 to_start
.push_back(new LayerAnimationSequence(
2430 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2432 delta
= base::TimeDelta::FromSeconds(2);
2433 to_start
.push_back(new LayerAnimationSequence(
2434 LayerAnimationElement::CreateBoundsElement(target_bounds
, delta
)));
2436 animator
->ScheduleTogether(to_start
);
2438 EXPECT_FALSE(observer_was_deleted
);
2440 animator
->StartAnimation(new LayerAnimationSequence(
2441 LayerAnimationElement::CreateOpacityElement(1.0f
, delta
)));
2443 EXPECT_TRUE(observer_was_deleted
);
2447 TEST(LayerAnimatorTest
, TestSetterRespectEnqueueStrategy
) {
2448 TestLayerAnimationDelegate delegate
;
2449 scoped_refptr
<LayerAnimator
> animator(LayerAnimator::CreateDefaultAnimator());
2450 animator
->set_disable_timer_for_test(true);
2452 animator
->SetDelegate(&delegate
);
2454 float start_opacity
= 0.0f
;
2455 float target_opacity
= 1.0f
;
2456 float magic_opacity
= 0.123f
;
2458 delegate
.SetOpacityFromAnimation(start_opacity
);
2460 ScopedLayerAnimationSettings
settings(animator
.get());
2461 settings
.SetPreemptionStrategy(
2462 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
2463 settings
.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2464 animator
->SetOpacity(target_opacity
);
2466 EXPECT_EQ(start_opacity
, delegate
.GetOpacityForAnimation());
2468 settings
.SetPreemptionStrategy(
2469 LayerAnimator::ENQUEUE_NEW_ANIMATION
);
2470 settings
.SetTransitionDuration(base::TimeDelta());
2471 animator
->SetOpacity(magic_opacity
);
2473 EXPECT_EQ(start_opacity
, delegate
.GetOpacityForAnimation());
2476 TEST(LayerAnimatorTest
, TestScopedCounterAnimation
) {
2477 Layer parent
, child
;
2480 gfx::Transform parent_begin
, parent_end
;
2482 parent_end
.Scale3d(2.0, 0.5, 1.0);
2484 // Parent animates from identity to the end value. The counter animation will
2485 // start at the end value and animate back to identity.
2486 gfx::Transform
child_begin(parent_end
);
2488 child
.SetTransform(child_begin
);
2489 parent
.SetTransform(parent_begin
);
2491 EXPECT_FALSE(child
.GetAnimator()->is_animating());
2493 ScopedLayerAnimationSettings
settings(parent
.GetAnimator());
2494 settings
.SetInverselyAnimatedBaseLayer(&parent
);
2495 settings
.AddInverselyAnimatedLayer(&child
);
2497 parent
.SetTransform(parent_end
);
2499 EXPECT_TRUE(child
.GetAnimator()->is_animating());
2500 EXPECT_TRUE(child
.GetTargetTransform().IsIdentity())
2501 << child
.GetTargetTransform().ToString();
2505 class CollectionLayerAnimationDelegate
: public TestLayerAnimationDelegate
{
2507 CollectionLayerAnimationDelegate() : collection(NULL
) {}
2508 virtual ~CollectionLayerAnimationDelegate() {}
2510 // LayerAnimationDelegate:
2511 virtual LayerAnimatorCollection
* GetLayerAnimatorCollection() OVERRIDE
{
2516 LayerAnimatorCollection collection
;
2519 TEST(LayerAnimatorTest
, LayerAnimatorCollectionTickTime
) {
2521 LayerAnimator
* animator
= layer
.GetAnimator();
2522 CollectionLayerAnimationDelegate delegate
;
2523 animator
->SetDelegate(&delegate
);
2525 LayerAnimatorCollection
* collection
= delegate
.GetLayerAnimatorCollection();
2526 base::TimeTicks null
;
2527 collection
->Progress(null
);
2528 EXPECT_TRUE(collection
->last_tick_time().is_null());
2530 // Adding an animator to the collection should update the last tick time.
2531 collection
->StartAnimator(layer
.GetAnimator());
2532 EXPECT_TRUE(collection
->HasActiveAnimators());
2533 EXPECT_FALSE(collection
->last_tick_time().is_null());
2535 collection
->StopAnimator(layer
.GetAnimator());
2536 EXPECT_FALSE(collection
->HasActiveAnimators());
2539 TEST(LayerAnimatorTest
, AnimatorStartedCorrectly
) {
2541 LayerAnimatorTestController
test_controller(layer
.GetAnimator());
2542 LayerAnimator
* animator
= test_controller
.animator();
2543 ASSERT_FALSE(animator
->is_started_
);
2545 TestLayerAnimationDelegate test_delegate
;
2546 animator
->SetDelegate(&test_delegate
);
2547 double target_opacity
= 1.0;
2548 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2549 animator
->ScheduleAnimation(new LayerAnimationSequence(
2550 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2551 EXPECT_FALSE(animator
->is_started_
);
2553 CollectionLayerAnimationDelegate collection_delegate
;
2554 animator
->SetDelegate(&collection_delegate
);
2555 animator
->UpdateAnimationState();
2556 EXPECT_TRUE(animator
->is_started_
);
2557 animator
->SetDelegate(NULL
);
2560 TEST(LayerAnimatorTest
, AnimatorRemovedFromCollectionWhenLayerIsDestroyed
) {
2561 scoped_ptr
<Layer
> layer(new Layer(LAYER_TEXTURED
));
2562 LayerAnimatorTestController
test_controller(layer
->GetAnimator());
2563 scoped_refptr
<LayerAnimator
> animator
= test_controller
.animator();
2564 CollectionLayerAnimationDelegate collection_delegate
;
2565 animator
->SetDelegate(&collection_delegate
);
2567 double target_opacity
= 1.0;
2568 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2569 animator
->ScheduleAnimation(new LayerAnimationSequence(
2570 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2573 collection_delegate
.GetLayerAnimatorCollection()->HasActiveAnimators());
2576 EXPECT_EQ(NULL
, animator
->delegate());
2578 collection_delegate
.GetLayerAnimatorCollection()->HasActiveAnimators());
2581 TEST(LayerAnimatorTest
, LayerMovedBetweenCompositorsDuringAnimation
) {
2582 bool enable_pixel_output
= false;
2583 ui::ContextFactory
* context_factory
=
2584 InitializeContextFactoryForTests(enable_pixel_output
);
2585 const gfx::Rect
bounds(10, 10, 100, 100);
2586 scoped_ptr
<TestCompositorHost
> host_1(
2587 TestCompositorHost::Create(bounds
, context_factory
));
2588 scoped_ptr
<TestCompositorHost
> host_2(
2589 TestCompositorHost::Create(bounds
, context_factory
));
2593 Compositor
* compositor_1
= host_1
->GetCompositor();
2595 compositor_1
->SetRootLayer(&root_1
);
2597 Compositor
* compositor_2
= host_2
->GetCompositor();
2599 compositor_2
->SetRootLayer(&root_2
);
2601 // Verify that neither compositor has active animators.
2602 EXPECT_FALSE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2603 EXPECT_FALSE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2607 LayerAnimator
* animator
= layer
.GetAnimator();
2608 double target_opacity
= 1.0;
2609 base::TimeDelta time_delta
= base::TimeDelta::FromSeconds(1);
2610 animator
->ScheduleAnimation(new LayerAnimationSequence(
2611 LayerAnimationElement::CreateOpacityElement(target_opacity
, time_delta
)));
2612 EXPECT_TRUE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2613 EXPECT_FALSE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2616 EXPECT_FALSE(compositor_1
->layer_animator_collection()->HasActiveAnimators());
2617 EXPECT_TRUE(compositor_2
->layer_animator_collection()->HasActiveAnimators());
2620 TerminateContextFactoryForTests();