Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / compositor / layer_animator_unittest.cc
blob613eb1cd2ade796921a5c71dc869af48ba5d2de4
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/compositor/layer_animator.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/compositor/layer_animation_delegate.h"
15 #include "ui/compositor/layer_animation_element.h"
16 #include "ui/compositor/layer_animation_sequence.h"
17 #include "ui/compositor/layer_animator_collection.h"
18 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h"
20 #include "ui/compositor/test/context_factories_for_test.h"
21 #include "ui/compositor/test/layer_animator_test_controller.h"
22 #include "ui/compositor/test/test_compositor_host.h"
23 #include "ui/compositor/test/test_layer_animation_delegate.h"
24 #include "ui/compositor/test/test_layer_animation_observer.h"
25 #include "ui/compositor/test/test_utils.h"
26 #include "ui/gfx/frame_time.h"
27 #include "ui/gfx/geometry/rect.h"
28 #include "ui/gfx/transform.h"
30 namespace ui {
32 namespace {
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),
39 SkColorGetB(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);
55 return animations;
58 class TestImplicitAnimationObserver : public ImplicitAnimationObserver {
59 public:
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(
78 property);
81 private:
82 // ImplicitAnimationObserver implementation
83 void OnImplicitAnimationsCompleted() override {
84 animations_completed_ = true;
87 bool RequiresNotificationWhenAnimatorDestroyed() const override {
88 return notify_when_animator_destructed_;
91 bool animations_completed_;
92 bool notify_when_animator_destructed_;
94 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver);
97 // When notified that an animation has ended, stops all other animations.
98 class DeletingLayerAnimationObserver : public LayerAnimationObserver {
99 public:
100 DeletingLayerAnimationObserver(LayerAnimator* animator)
101 : animator_(animator) {
104 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
105 animator_->StopAnimating();
108 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {
109 animator_->StopAnimating();
112 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {}
114 private:
115 LayerAnimator* animator_;
117 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver);
120 class LayerAnimatorDestructionObserver {
121 public:
122 LayerAnimatorDestructionObserver() : animator_deleted_(false) {}
123 virtual ~LayerAnimatorDestructionObserver() {}
125 void NotifyAnimatorDeleted() {
126 animator_deleted_ = true;
129 bool IsAnimatorDeleted() {
130 return animator_deleted_;
133 private:
134 bool animator_deleted_;
136 DISALLOW_COPY_AND_ASSIGN(LayerAnimatorDestructionObserver);
139 class TestLayerAnimator : public LayerAnimator {
140 public:
141 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)),
142 destruction_observer_(NULL) {}
144 void SetDestructionObserver(
145 LayerAnimatorDestructionObserver* observer) {
146 destruction_observer_ = observer;
149 protected:
150 ~TestLayerAnimator() override {
151 if (destruction_observer_) {
152 destruction_observer_->NotifyAnimatorDeleted();
156 void ProgressAnimation(LayerAnimationSequence* sequence,
157 base::TimeTicks now) override {
158 EXPECT_TRUE(HasAnimation(sequence));
159 LayerAnimator::ProgressAnimation(sequence, now);
162 private:
163 LayerAnimatorDestructionObserver* destruction_observer_;
165 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
168 // The test layer animation sequence updates a live instances count when it is
169 // created and destroyed.
170 class TestLayerAnimationSequence : public LayerAnimationSequence {
171 public:
172 TestLayerAnimationSequence(LayerAnimationElement* element,
173 int* num_live_instances)
174 : LayerAnimationSequence(element),
175 num_live_instances_(num_live_instances) {
176 (*num_live_instances_)++;
179 ~TestLayerAnimationSequence() override { (*num_live_instances_)--; }
181 private:
182 int* num_live_instances_;
184 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence);
187 } // namespace
189 // Checks that setting a property on an implicit animator causes an animation to
190 // happen.
191 TEST(LayerAnimatorTest, ImplicitAnimation) {
192 scoped_refptr<LayerAnimator> animator(
193 LayerAnimator::CreateImplicitAnimator());
194 animator->set_disable_timer_for_test(true);
195 TestLayerAnimationDelegate delegate;
196 animator->SetDelegate(&delegate);
197 base::TimeTicks now = gfx::FrameTime::Now();
198 animator->SetBrightness(0.5);
199 EXPECT_TRUE(animator->is_animating());
200 animator->Step(now + base::TimeDelta::FromSeconds(1));
201 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
204 // Checks that if the animator is a default animator, that implicit animations
205 // are not started.
206 TEST(LayerAnimatorTest, NoImplicitAnimation) {
207 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
208 animator->set_disable_timer_for_test(true);
209 TestLayerAnimationDelegate delegate;
210 animator->SetDelegate(&delegate);
211 animator->SetBrightness(0.5);
212 EXPECT_FALSE(animator->is_animating());
213 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
216 // Checks that StopAnimatingProperty stops animation for that property, and also
217 // skips the stopped animation to the end.
218 TEST(LayerAnimatorTest, StopAnimatingProperty) {
219 scoped_refptr<LayerAnimator> animator(
220 LayerAnimator::CreateImplicitAnimator());
221 animator->set_disable_timer_for_test(true);
222 TestLayerAnimationDelegate delegate;
223 animator->SetDelegate(&delegate);
224 double target_opacity(0.5);
225 gfx::Rect target_bounds(0, 0, 50, 50);
226 animator->SetOpacity(target_opacity);
227 animator->SetBounds(target_bounds);
228 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY);
229 EXPECT_TRUE(animator->is_animating());
230 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
231 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS);
232 EXPECT_FALSE(animator->is_animating());
233 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
236 // Checks that multiple running animation for separate properties can be stopped
237 // simultaneously and that all animations are advanced to their target values.
238 TEST(LayerAnimatorTest, StopAnimating) {
239 scoped_refptr<LayerAnimator> animator(
240 LayerAnimator::CreateImplicitAnimator());
241 animator->set_disable_timer_for_test(true);
242 TestLayerAnimationDelegate delegate;
243 animator->SetDelegate(&delegate);
244 double target_opacity(0.5);
245 gfx::Rect target_bounds(0, 0, 50, 50);
246 animator->SetOpacity(target_opacity);
247 animator->SetBounds(target_bounds);
248 EXPECT_TRUE(animator->is_animating());
249 animator->StopAnimating();
250 EXPECT_FALSE(animator->is_animating());
251 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
252 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
255 // Checks that multiple running animation for separate properties can be stopped
256 // simultaneously and that all animations are advanced to their target values.
257 TEST(LayerAnimatorTest, AbortAllAnimations) {
258 scoped_refptr<LayerAnimator> animator(
259 LayerAnimator::CreateImplicitAnimator());
260 animator->set_disable_timer_for_test(true);
261 TestLayerAnimationDelegate delegate;
262 double initial_opacity(1.0);
263 gfx::Rect initial_bounds(0, 0, 10, 10);
264 delegate.SetOpacityFromAnimation(initial_opacity);
265 delegate.SetBoundsFromAnimation(initial_bounds);
266 animator->SetDelegate(&delegate);
267 double target_opacity(0.5);
268 gfx::Rect target_bounds(0, 0, 50, 50);
269 animator->SetOpacity(target_opacity);
270 animator->SetBounds(target_bounds);
271 EXPECT_TRUE(animator->is_animating());
272 animator->AbortAllAnimations();
273 EXPECT_FALSE(animator->is_animating());
274 EXPECT_FLOAT_EQ(initial_opacity, delegate.GetOpacityForAnimation());
275 CheckApproximatelyEqual(initial_bounds, delegate.GetBoundsForAnimation());
278 // Schedule a non-threaded animation that can run immediately. This is the
279 // trivial case and should result in the animation being started immediately.
280 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) {
281 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
282 animator->set_disable_timer_for_test(true);
283 TestLayerAnimationDelegate delegate;
284 animator->SetDelegate(&delegate);
286 double start_brightness(0.0);
287 double middle_brightness(0.5);
288 double target_brightness(1.0);
290 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
292 delegate.SetBrightnessFromAnimation(start_brightness);
294 animator->ScheduleAnimation(
295 new LayerAnimationSequence(
296 LayerAnimationElement::CreateBrightnessElement(target_brightness,
297 delta)));
299 EXPECT_TRUE(animator->is_animating());
300 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
302 base::TimeTicks start_time = animator->last_step_time();
304 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
306 EXPECT_TRUE(animator->is_animating());
307 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
309 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
311 EXPECT_FALSE(animator->is_animating());
312 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
315 // Schedule a threaded animation that can run immediately.
316 TEST(LayerAnimatorTest, ScheduleThreadedAnimationThatCanRunImmediately) {
317 double epsilon = 0.00001;
318 LayerAnimatorTestController test_controller(
319 LayerAnimator::CreateDefaultAnimator());
320 LayerAnimator* animator = test_controller.animator();
321 test_controller.animator()->set_disable_timer_for_test(true);
322 TestLayerAnimationDelegate delegate;
323 test_controller.animator()->SetDelegate(&delegate);
325 double start_opacity(0.0);
326 double target_opacity(1.0);
328 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
330 delegate.SetOpacityFromAnimation(start_opacity);
332 test_controller.animator()->ScheduleAnimation(
333 new LayerAnimationSequence(
334 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
336 EXPECT_TRUE(test_controller.animator()->is_animating());
337 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
339 base::TimeTicks start_time = test_controller.animator()->last_step_time();
340 base::TimeTicks effective_start = start_time + delta;
342 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
343 cc::AnimationEvent::STARTED, 0,
344 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
345 ->animation_group_id(),
346 cc::Animation::OPACITY, effective_start));
348 animator->Step(effective_start + delta / 2);
350 EXPECT_TRUE(test_controller.animator()->is_animating());
351 EXPECT_NEAR(
352 0.5,
353 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
354 last_progressed_fraction(),
355 epsilon);
357 animator->Step(effective_start + delta);
359 EXPECT_FALSE(test_controller.animator()->is_animating());
360 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
363 // Schedule two non-threaded animations on separate properties. Both animations
364 // should start immediately and should progress in lock step.
365 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) {
366 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
367 animator->set_disable_timer_for_test(true);
368 TestLayerAnimationDelegate delegate;
369 animator->SetDelegate(&delegate);
371 double start_brightness(0.0);
372 double middle_brightness(0.5);
373 double target_brightness(1.0);
375 gfx::Rect start_bounds, target_bounds, middle_bounds;
376 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
377 start_bounds.set_x(-90);
378 target_bounds.set_x(90);
380 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
382 delegate.SetBrightnessFromAnimation(start_brightness);
383 delegate.SetBoundsFromAnimation(start_bounds);
385 animator->ScheduleAnimation(
386 new LayerAnimationSequence(
387 LayerAnimationElement::CreateBrightnessElement(target_brightness,
388 delta)));
390 animator->ScheduleAnimation(
391 new LayerAnimationSequence(
392 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
394 EXPECT_TRUE(animator->is_animating());
395 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
396 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
398 base::TimeTicks start_time = animator->last_step_time();
400 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
402 EXPECT_TRUE(animator->is_animating());
403 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
404 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
406 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
408 EXPECT_FALSE(animator->is_animating());
409 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
410 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
413 // Schedule a threaded and a non-threaded animation on separate properties. Both
414 // animations should progress in lock step.
415 TEST(LayerAnimatorTest, ScheduleThreadedAndNonThreadedAnimations) {
416 double epsilon = 0.00001;
417 LayerAnimatorTestController test_controller(
418 LayerAnimator::CreateDefaultAnimator());
419 LayerAnimator* animator = test_controller.animator();
420 test_controller.animator()->set_disable_timer_for_test(true);
421 TestLayerAnimationDelegate delegate;
422 test_controller.animator()->SetDelegate(&delegate);
424 double start_opacity(0.0);
425 double target_opacity(1.0);
427 gfx::Rect start_bounds, target_bounds, middle_bounds;
428 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
429 start_bounds.set_x(-90);
430 target_bounds.set_x(90);
432 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
434 delegate.SetOpacityFromAnimation(start_opacity);
435 delegate.SetBoundsFromAnimation(start_bounds);
437 std::vector<LayerAnimationSequence*> animations;
438 animations.push_back(
439 new LayerAnimationSequence(
440 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
442 animations.push_back(
443 new LayerAnimationSequence(
444 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
446 test_controller.animator()->ScheduleTogether(animations);
448 EXPECT_TRUE(test_controller.animator()->is_animating());
449 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
450 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
452 base::TimeTicks start_time = test_controller.animator()->last_step_time();
453 base::TimeTicks effective_start = start_time + delta;
455 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
456 cc::AnimationEvent::STARTED, 0,
457 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
458 ->animation_group_id(),
459 cc::Animation::OPACITY, effective_start));
461 animator->Step(effective_start + delta / 2);
463 EXPECT_TRUE(test_controller.animator()->is_animating());
464 EXPECT_NEAR(
465 0.5,
466 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
467 last_progressed_fraction(),
468 epsilon);
469 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
471 animator->Step(effective_start + delta);
473 EXPECT_FALSE(test_controller.animator()->is_animating());
474 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
475 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
478 // Schedule two animations on the same property. In this case, the two
479 // animations should run one after another.
480 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) {
481 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
482 animator->set_disable_timer_for_test(true);
483 TestLayerAnimationDelegate delegate;
484 animator->SetDelegate(&delegate);
486 double start_brightness(0.0);
487 double middle_brightness(0.5);
488 double target_brightness(1.0);
490 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
492 delegate.SetBrightnessFromAnimation(start_brightness);
494 animator->ScheduleAnimation(
495 new LayerAnimationSequence(
496 LayerAnimationElement::CreateBrightnessElement(target_brightness,
497 delta)));
499 animator->ScheduleAnimation(
500 new LayerAnimationSequence(
501 LayerAnimationElement::CreateBrightnessElement(start_brightness,
502 delta)));
504 EXPECT_TRUE(animator->is_animating());
505 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
507 base::TimeTicks start_time = animator->last_step_time();
509 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
511 EXPECT_TRUE(animator->is_animating());
512 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
514 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
516 EXPECT_TRUE(animator->is_animating());
517 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
519 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
521 EXPECT_TRUE(animator->is_animating());
522 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
524 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
526 EXPECT_FALSE(animator->is_animating());
527 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
530 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
531 // is, ensure that all animations targetting a particular property are run in
532 // order.
533 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) {
534 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
535 animator->set_disable_timer_for_test(true);
536 TestLayerAnimationDelegate delegate;
537 animator->SetDelegate(&delegate);
539 double start_grayscale(0.0);
540 double middle_grayscale(0.5);
541 double target_grayscale(1.0);
543 gfx::Rect start_bounds, target_bounds, middle_bounds;
544 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
545 start_bounds.set_x(-90);
546 target_bounds.set_x(90);
548 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
550 delegate.SetGrayscaleFromAnimation(start_grayscale);
551 delegate.SetBoundsFromAnimation(start_bounds);
553 animator->ScheduleAnimation(
554 new LayerAnimationSequence(
555 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
556 delta)));
558 scoped_ptr<LayerAnimationSequence> bounds_and_grayscale(
559 new LayerAnimationSequence(
560 LayerAnimationElement::CreateGrayscaleElement(start_grayscale,
561 delta)));
563 bounds_and_grayscale->AddElement(
564 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
566 animator->ScheduleAnimation(bounds_and_grayscale.release());
568 animator->ScheduleAnimation(
569 new LayerAnimationSequence(
570 LayerAnimationElement::CreateBoundsElement(start_bounds, delta)));
572 EXPECT_TRUE(animator->is_animating());
573 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
574 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
576 base::TimeTicks start_time = animator->last_step_time();
578 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
580 EXPECT_TRUE(animator->is_animating());
581 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
582 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
584 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
586 EXPECT_TRUE(animator->is_animating());
587 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
588 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
590 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
592 EXPECT_TRUE(animator->is_animating());
593 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
594 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
596 animator->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
598 EXPECT_TRUE(animator->is_animating());
599 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
600 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
602 animator->Step(start_time + base::TimeDelta::FromMilliseconds(4000));
604 EXPECT_FALSE(animator->is_animating());
605 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
606 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
609 // Schedule {g} and then schedule {g} and {b} together. In this case, since
610 // ScheduleTogether is being used, the bounds animation should not start until
611 // the second grayscale animation starts.
612 TEST(LayerAnimatorTest, ScheduleTogether) {
613 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
614 animator->set_disable_timer_for_test(true);
615 TestLayerAnimationDelegate delegate;
616 animator->SetDelegate(&delegate);
618 double start_grayscale(0.0);
619 double target_grayscale(1.0);
621 gfx::Rect start_bounds, target_bounds, middle_bounds;
622 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50);
623 start_bounds.set_x(-90);
624 target_bounds.set_x(90);
626 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
628 delegate.SetGrayscaleFromAnimation(start_grayscale);
629 delegate.SetBoundsFromAnimation(start_bounds);
631 animator->ScheduleAnimation(
632 new LayerAnimationSequence(
633 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
634 delta)));
636 std::vector<LayerAnimationSequence*> sequences;
637 sequences.push_back(new LayerAnimationSequence(
638 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta)));
639 sequences.push_back(new LayerAnimationSequence(
640 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
642 animator->ScheduleTogether(sequences);
644 EXPECT_TRUE(animator->is_animating());
645 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
646 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
648 base::TimeTicks start_time = animator->last_step_time();
650 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
652 EXPECT_TRUE(animator->is_animating());
653 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
654 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
656 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
658 EXPECT_FALSE(animator->is_animating());
659 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
660 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
663 // Start non-threaded animation (that can run immediately). This is the trivial
664 // case (see the trival case for ScheduleAnimation).
665 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) {
666 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
667 animator->set_disable_timer_for_test(true);
668 TestLayerAnimationDelegate delegate;
669 animator->SetDelegate(&delegate);
671 double start_brightness(0.0);
672 double middle_brightness(0.5);
673 double target_brightness(1.0);
675 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
677 delegate.SetBrightnessFromAnimation(start_brightness);
679 animator->StartAnimation(
680 new LayerAnimationSequence(
681 LayerAnimationElement::CreateBrightnessElement(target_brightness,
682 delta)));
684 EXPECT_TRUE(animator->is_animating());
685 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
687 base::TimeTicks start_time = animator->last_step_time();
689 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
691 EXPECT_TRUE(animator->is_animating());
692 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
694 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
696 EXPECT_FALSE(animator->is_animating());
697 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
700 // Start threaded animation (that can run immediately).
701 TEST(LayerAnimatorTest, StartThreadedAnimationThatCanRunImmediately) {
702 double epsilon = 0.00001;
703 LayerAnimatorTestController test_controller(
704 LayerAnimator::CreateDefaultAnimator());
705 LayerAnimator* animator = test_controller.animator();
706 test_controller.animator()->set_disable_timer_for_test(true);
707 TestLayerAnimationDelegate delegate;
708 test_controller.animator()->SetDelegate(&delegate);
710 double start_opacity(0.0);
711 double target_opacity(1.0);
713 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
715 delegate.SetOpacityFromAnimation(start_opacity);
717 test_controller.animator()->StartAnimation(
718 new LayerAnimationSequence(
719 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
721 EXPECT_TRUE(test_controller.animator()->is_animating());
722 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
724 base::TimeTicks start_time = test_controller.animator()->last_step_time();
725 base::TimeTicks effective_start = start_time + delta;
727 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
728 cc::AnimationEvent::STARTED, 0,
729 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
730 ->animation_group_id(),
731 cc::Animation::OPACITY, effective_start));
733 animator->Step(effective_start + delta / 2);
735 EXPECT_TRUE(test_controller.animator()->is_animating());
736 EXPECT_NEAR(
737 0.5,
738 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
739 last_progressed_fraction(),
740 epsilon);
742 animator->Step(effective_start + delta);
743 EXPECT_FALSE(test_controller.animator()->is_animating());
744 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
747 // Preempt by immediately setting new target.
748 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) {
749 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
750 animator->set_disable_timer_for_test(true);
751 TestLayerAnimationDelegate delegate;
752 animator->SetDelegate(&delegate);
754 double start_opacity(0.0);
755 double target_opacity(1.0);
757 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
759 delegate.SetOpacityFromAnimation(start_opacity);
761 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
763 animator->StartAnimation(
764 new LayerAnimationSequence(
765 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
767 animator->StartAnimation(
768 new LayerAnimationSequence(
769 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
771 EXPECT_FALSE(animator->is_animating());
772 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
775 // Preempt by animating to new target, with a non-threaded animation.
776 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) {
777 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
778 animator->set_disable_timer_for_test(true);
779 TestLayerAnimationDelegate delegate;
780 animator->SetDelegate(&delegate);
782 double start_brightness(0.0);
783 double middle_brightness(0.5);
784 double target_brightness(1.0);
786 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
788 delegate.SetBrightnessFromAnimation(start_brightness);
790 animator->set_preemption_strategy(
791 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
793 animator->StartAnimation(
794 new LayerAnimationSequence(
795 LayerAnimationElement::CreateBrightnessElement(target_brightness,
796 delta)));
798 base::TimeTicks start_time = animator->last_step_time();
800 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
802 animator->StartAnimation(
803 new LayerAnimationSequence(
804 LayerAnimationElement::CreateBrightnessElement(start_brightness,
805 delta)));
807 EXPECT_TRUE(animator->is_animating());
808 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
810 animator->StartAnimation(
811 new LayerAnimationSequence(
812 LayerAnimationElement::CreateBrightnessElement(start_brightness,
813 delta)));
815 EXPECT_TRUE(animator->is_animating());
817 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
819 EXPECT_TRUE(animator->is_animating());
820 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
821 0.5 * (start_brightness + middle_brightness));
823 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
825 EXPECT_FALSE(animator->is_animating());
826 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
829 // Preempt by animating to new target, with a threaded animation.
830 TEST(LayerAnimatorTest, PreemptThreadedByImmediatelyAnimatingToNewTarget) {
831 double epsilon = 0.00001;
832 LayerAnimatorTestController test_controller(
833 LayerAnimator::CreateDefaultAnimator());
834 LayerAnimator* animator = test_controller.animator();
835 test_controller.animator()->set_disable_timer_for_test(true);
836 TestLayerAnimationDelegate delegate;
837 test_controller.animator()->SetDelegate(&delegate);
839 double start_opacity(0.0);
840 double middle_opacity(0.5);
841 double target_opacity(1.0);
843 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
845 delegate.SetOpacityFromAnimation(start_opacity);
847 test_controller.animator()->set_preemption_strategy(
848 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
850 test_controller.animator()->StartAnimation(
851 new LayerAnimationSequence(
852 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
854 base::TimeTicks start_time = test_controller.animator()->last_step_time();
855 base::TimeTicks effective_start = start_time + delta;
857 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
858 cc::AnimationEvent::STARTED, 0,
859 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
860 ->animation_group_id(),
861 cc::Animation::OPACITY, effective_start));
863 animator->Step(effective_start + delta / 2);
865 test_controller.animator()->StartAnimation(
866 new LayerAnimationSequence(
867 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
869 EXPECT_TRUE(test_controller.animator()->is_animating());
870 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
872 test_controller.animator()->StartAnimation(
873 new LayerAnimationSequence(
874 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
876 EXPECT_TRUE(test_controller.animator()->is_animating());
878 base::TimeTicks second_effective_start = effective_start + delta;
880 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
881 cc::AnimationEvent::STARTED, 0,
882 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
883 ->animation_group_id(),
884 cc::Animation::OPACITY, second_effective_start));
886 animator->Step(second_effective_start + delta / 2);
888 EXPECT_TRUE(test_controller.animator()->is_animating());
889 EXPECT_NEAR(
890 0.5,
891 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
892 last_progressed_fraction(),
893 epsilon);
895 animator->Step(second_effective_start + delta);
897 EXPECT_FALSE(test_controller.animator()->is_animating());
898 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
901 // Preempt by enqueuing the new animation.
902 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) {
903 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
904 animator->set_disable_timer_for_test(true);
905 TestLayerAnimationDelegate delegate;
906 animator->SetDelegate(&delegate);
908 double start_brightness(0.0);
909 double middle_brightness(0.5);
910 double target_brightness(1.0);
912 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
914 delegate.SetBrightnessFromAnimation(start_brightness);
916 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
918 animator->StartAnimation(
919 new LayerAnimationSequence(
920 LayerAnimationElement::CreateBrightnessElement(target_brightness,
921 delta)));
923 base::TimeTicks start_time = animator->last_step_time();
925 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
927 animator->StartAnimation(
928 new LayerAnimationSequence(
929 LayerAnimationElement::CreateBrightnessElement(start_brightness,
930 delta)));
932 EXPECT_TRUE(animator->is_animating());
933 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
935 EXPECT_TRUE(animator->is_animating());
937 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
939 EXPECT_TRUE(animator->is_animating());
940 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
942 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
944 EXPECT_TRUE(animator->is_animating());
945 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
947 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
949 EXPECT_FALSE(animator->is_animating());
950 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
953 // Start an animation when there are sequences waiting in the queue. In this
954 // case, all pending and running animations should be finished, and the new
955 // animation started.
956 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) {
957 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
958 animator->set_disable_timer_for_test(true);
959 TestLayerAnimationDelegate delegate;
960 animator->SetDelegate(&delegate);
962 double start_brightness(0.0);
963 double middle_brightness(0.5);
964 double target_brightness(1.0);
966 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
968 delegate.SetBrightnessFromAnimation(start_brightness);
970 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
972 animator->StartAnimation(
973 new LayerAnimationSequence(
974 LayerAnimationElement::CreateBrightnessElement(target_brightness,
975 delta)));
977 base::TimeTicks start_time = animator->last_step_time();
979 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
981 animator->StartAnimation(
982 new LayerAnimationSequence(
983 LayerAnimationElement::CreateBrightnessElement(middle_brightness,
984 delta)));
986 // Queue should now have two animations. Starting a third should replace the
987 // second.
988 animator->StartAnimation(
989 new LayerAnimationSequence(
990 LayerAnimationElement::CreateBrightnessElement(start_brightness,
991 delta)));
993 EXPECT_TRUE(animator->is_animating());
994 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
996 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
998 EXPECT_TRUE(animator->is_animating());
999 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1001 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1003 EXPECT_TRUE(animator->is_animating());
1004 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1006 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1008 EXPECT_FALSE(animator->is_animating());
1009 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1012 TEST(LayerAnimatorTest, StartTogetherSetsLastStepTime) {
1013 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1014 animator->set_disable_timer_for_test(true);
1015 TestLayerAnimationDelegate delegate;
1016 animator->SetDelegate(&delegate);
1018 double start_grayscale(0.0);
1019 double target_grayscale(1.0);
1020 double start_brightness(0.1);
1021 double target_brightness(0.9);
1023 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1025 delegate.SetGrayscaleFromAnimation(start_grayscale);
1026 delegate.SetBrightnessFromAnimation(start_brightness);
1028 animator->set_preemption_strategy(
1029 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1031 animator->set_last_step_time(base::TimeTicks());
1033 animator->StartTogether(
1034 CreateMultiSequence(
1035 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1036 delta),
1037 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1038 delta)
1041 // If last step time was not set correctly, the resulting delta should be
1042 // miniscule (fractions of a millisecond). If set correctly, then the delta
1043 // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1044 // though a much smaller value would probably have sufficed.
1045 delta = gfx::FrameTime::Now() - animator->last_step_time();
1046 EXPECT_GT(60.0, delta.InSecondsF());
1049 //-------------------------------------------------------
1050 // Preempt by immediately setting new target.
1051 TEST(LayerAnimatorTest, MultiPreemptBySettingNewTarget) {
1052 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1053 animator->set_disable_timer_for_test(true);
1054 TestLayerAnimationDelegate delegate;
1055 animator->SetDelegate(&delegate);
1057 double start_opacity(0.0);
1058 double target_opacity(1.0);
1059 double start_brightness(0.1);
1060 double target_brightness(0.9);
1062 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1064 delegate.SetOpacityFromAnimation(start_opacity);
1065 delegate.SetBrightnessFromAnimation(start_brightness);
1067 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1069 animator->StartTogether(
1070 CreateMultiSequence(
1071 LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1072 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1073 delta)
1076 animator->StartTogether(
1077 CreateMultiSequence(
1078 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1079 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1080 delta)
1083 EXPECT_FALSE(animator->is_animating());
1084 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1085 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1088 // Preempt by animating to new target.
1089 TEST(LayerAnimatorTest, MultiPreemptByImmediatelyAnimatingToNewTarget) {
1090 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1091 animator->set_disable_timer_for_test(true);
1092 TestLayerAnimationDelegate delegate;
1093 animator->SetDelegate(&delegate);
1095 double start_grayscale(0.0);
1096 double middle_grayscale(0.5);
1097 double target_grayscale(1.0);
1099 double start_brightness(0.1);
1100 double middle_brightness(0.2);
1101 double target_brightness(0.3);
1103 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1105 delegate.SetGrayscaleFromAnimation(start_grayscale);
1106 delegate.SetBrightnessFromAnimation(start_brightness);
1108 animator->set_preemption_strategy(
1109 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1111 animator->StartTogether(
1112 CreateMultiSequence(
1113 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1114 delta),
1115 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1116 delta)
1119 base::TimeTicks start_time = animator->last_step_time();
1121 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1123 animator->StartTogether(
1124 CreateMultiSequence(
1125 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1126 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1127 delta)));
1129 EXPECT_TRUE(animator->is_animating());
1130 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1131 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1133 animator->StartTogether(
1134 CreateMultiSequence(
1135 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1136 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1137 delta)));
1139 EXPECT_TRUE(animator->is_animating());
1141 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1143 EXPECT_TRUE(animator->is_animating());
1144 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(),
1145 0.5 * (start_grayscale + middle_grayscale));
1146 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
1147 0.5 * (start_brightness + middle_brightness));
1149 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1151 EXPECT_FALSE(animator->is_animating());
1152 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1153 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1156 // Preempt a threaded animation by animating to new target.
1157 TEST(LayerAnimatorTest, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget) {
1158 double epsilon = 0.00001;
1159 LayerAnimatorTestController test_controller(
1160 LayerAnimator::CreateDefaultAnimator());
1161 LayerAnimator* animator = test_controller.animator();
1162 test_controller.animator()->set_disable_timer_for_test(true);
1163 TestLayerAnimationDelegate delegate;
1164 test_controller.animator()->SetDelegate(&delegate);
1166 double start_opacity(0.0);
1167 double middle_opacity(0.5);
1168 double target_opacity(1.0);
1170 double start_brightness(0.1);
1171 double middle_brightness(0.2);
1172 double target_brightness(0.3);
1174 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1176 delegate.SetOpacityFromAnimation(start_opacity);
1177 delegate.SetBrightnessFromAnimation(start_brightness);
1179 test_controller.animator()->set_preemption_strategy(
1180 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1182 test_controller.animator()->StartTogether(
1183 CreateMultiSequence(
1184 LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1185 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1186 delta)
1189 base::TimeTicks start_time = test_controller.animator()->last_step_time();
1190 base::TimeTicks effective_start = start_time + delta;
1192 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1193 cc::AnimationEvent::STARTED, 0,
1194 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1195 ->animation_group_id(),
1196 cc::Animation::OPACITY, effective_start));
1198 animator->Step(effective_start + delta / 2);
1200 test_controller.animator()->StartTogether(
1201 CreateMultiSequence(
1202 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1203 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1204 delta)));
1206 EXPECT_TRUE(test_controller.animator()->is_animating());
1207 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
1208 EXPECT_NEAR(delegate.GetBrightnessForAnimation(), middle_brightness, epsilon);
1210 test_controller.animator()->StartTogether(
1211 CreateMultiSequence(
1212 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1213 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1214 delta)));
1216 EXPECT_TRUE(test_controller.animator()->is_animating());
1218 base::TimeTicks second_effective_start = effective_start + delta;
1220 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1221 cc::AnimationEvent::STARTED, 0,
1222 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1223 ->animation_group_id(),
1224 cc::Animation::OPACITY, second_effective_start));
1226 animator->Step(second_effective_start + delta / 2);
1228 EXPECT_TRUE(test_controller.animator()->is_animating());
1229 EXPECT_NEAR(
1230 0.5,
1231 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1232 last_progressed_fraction(),
1233 epsilon);
1234 EXPECT_NEAR(delegate.GetBrightnessForAnimation(),
1235 0.5 * (start_brightness + middle_brightness),
1236 epsilon);
1238 animator->Step(second_effective_start + delta);
1240 EXPECT_FALSE(test_controller.animator()->is_animating());
1241 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1242 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1245 // Preempt by enqueuing the new animation.
1246 TEST(LayerAnimatorTest, MultiPreemptEnqueueNewAnimation) {
1247 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1248 animator->set_disable_timer_for_test(true);
1249 TestLayerAnimationDelegate delegate;
1250 animator->SetDelegate(&delegate);
1252 double start_grayscale(0.0);
1253 double middle_grayscale(0.5);
1254 double target_grayscale(1.0);
1256 double start_brightness(0.1);
1257 double middle_brightness(0.2);
1258 double target_brightness(0.3);
1260 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1262 delegate.SetGrayscaleFromAnimation(start_grayscale);
1263 delegate.SetBrightnessFromAnimation(start_brightness);
1265 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1267 animator->StartTogether(
1268 CreateMultiSequence(
1269 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1270 delta),
1271 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1272 delta)));
1274 base::TimeTicks start_time = animator->last_step_time();
1276 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1278 animator->StartTogether(
1279 CreateMultiSequence(
1280 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1281 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1282 delta)));
1284 EXPECT_TRUE(animator->is_animating());
1285 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1286 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1288 EXPECT_TRUE(animator->is_animating());
1290 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1292 EXPECT_TRUE(animator->is_animating());
1293 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1294 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1296 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1298 EXPECT_TRUE(animator->is_animating());
1299 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1300 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1302 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1304 EXPECT_FALSE(animator->is_animating());
1305 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1306 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1309 // Start an animation when there are sequences waiting in the queue. In this
1310 // case, all pending and running animations should be finished, and the new
1311 // animation started.
1312 TEST(LayerAnimatorTest, MultiPreemptByReplacingQueuedAnimations) {
1313 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1314 animator->set_disable_timer_for_test(true);
1315 TestLayerAnimationDelegate delegate;
1316 animator->SetDelegate(&delegate);
1318 double start_grayscale(0.0);
1319 double middle_grayscale(0.5);
1320 double target_grayscale(1.0);
1322 double start_brightness(0.1);
1323 double middle_brightness(0.2);
1324 double target_brightness(0.3);
1326 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1328 delegate.SetGrayscaleFromAnimation(start_grayscale);
1329 delegate.SetBrightnessFromAnimation(start_brightness);
1331 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
1333 animator->StartTogether(
1334 CreateMultiSequence(
1335 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1336 delta),
1337 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1338 delta)));
1340 base::TimeTicks start_time = animator->last_step_time();
1342 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1344 animator->StartTogether(
1345 CreateMultiSequence(
1346 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale,
1347 delta),
1348 LayerAnimationElement::CreateBrightnessElement(middle_brightness,
1349 delta)));
1351 // Queue should now have two animations. Starting a third should replace the
1352 // second.
1353 animator->StartTogether(
1354 CreateMultiSequence(
1355 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1356 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1357 delta)));
1359 EXPECT_TRUE(animator->is_animating());
1360 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1361 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1363 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1365 EXPECT_TRUE(animator->is_animating());
1366 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1367 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1369 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1371 EXPECT_TRUE(animator->is_animating());
1372 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1373 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1375 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1377 EXPECT_FALSE(animator->is_animating());
1378 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1379 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1381 //-------------------------------------------------------
1382 // Test that non-threaded cyclic sequences continue to animate.
1383 TEST(LayerAnimatorTest, CyclicSequences) {
1384 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1385 animator->set_disable_timer_for_test(true);
1386 TestLayerAnimationDelegate delegate;
1387 animator->SetDelegate(&delegate);
1389 double start_brightness(0.0);
1390 double target_brightness(1.0);
1392 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1394 delegate.SetBrightnessFromAnimation(start_brightness);
1396 scoped_ptr<LayerAnimationSequence> sequence(
1397 new LayerAnimationSequence(
1398 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1399 delta)));
1401 sequence->AddElement(
1402 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
1404 sequence->set_is_cyclic(true);
1406 animator->StartAnimation(sequence.release());
1408 base::TimeTicks start_time = animator->last_step_time();
1410 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1412 EXPECT_TRUE(animator->is_animating());
1413 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1415 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1417 EXPECT_TRUE(animator->is_animating());
1418 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1420 animator->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
1422 EXPECT_TRUE(animator->is_animating());
1423 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1425 // Skip ahead by a lot.
1426 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000));
1428 EXPECT_TRUE(animator->is_animating());
1429 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1431 // Skip ahead by a lot.
1432 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000));
1434 EXPECT_TRUE(animator->is_animating());
1435 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1437 animator->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS);
1439 EXPECT_FALSE(animator->is_animating());
1442 // Test that threaded cyclic sequences continue to animate.
1443 TEST(LayerAnimatorTest, ThreadedCyclicSequences) {
1444 LayerAnimatorTestController test_controller(
1445 LayerAnimator::CreateDefaultAnimator());
1446 LayerAnimator* animator = test_controller.animator();
1447 test_controller.animator()->set_disable_timer_for_test(true);
1448 TestLayerAnimationDelegate delegate;
1449 test_controller.animator()->SetDelegate(&delegate);
1451 double start_opacity(0.0);
1452 double target_opacity(1.0);
1454 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1456 delegate.SetOpacityFromAnimation(start_opacity);
1458 scoped_ptr<LayerAnimationSequence> sequence(
1459 new LayerAnimationSequence(
1460 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
1462 sequence->AddElement(
1463 LayerAnimationElement::CreateOpacityElement(start_opacity, delta));
1465 sequence->set_is_cyclic(true);
1467 test_controller.animator()->StartAnimation(sequence.release());
1469 base::TimeTicks start_time = test_controller.animator()->last_step_time();
1470 base::TimeTicks effective_start = start_time + delta;
1472 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1473 cc::AnimationEvent::STARTED, 0,
1474 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1475 ->animation_group_id(),
1476 cc::Animation::OPACITY, effective_start));
1478 animator->Step(effective_start + delta);
1479 EXPECT_TRUE(test_controller.animator()->is_animating());
1480 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1482 base::TimeTicks second_effective_start = effective_start + 2 * delta;
1483 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1484 cc::AnimationEvent::STARTED, 0,
1485 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1486 ->animation_group_id(),
1487 cc::Animation::OPACITY, second_effective_start));
1489 animator->Step(second_effective_start + delta);
1491 EXPECT_TRUE(test_controller.animator()->is_animating());
1492 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1494 base::TimeTicks third_effective_start = second_effective_start + 2 * delta;
1495 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1496 cc::AnimationEvent::STARTED, 0,
1497 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1498 ->animation_group_id(),
1499 cc::Animation::OPACITY, third_effective_start));
1501 animator->Step(third_effective_start + delta);
1502 EXPECT_TRUE(test_controller.animator()->is_animating());
1503 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1505 base::TimeTicks fourth_effective_start = third_effective_start + 2 * delta;
1506 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1507 cc::AnimationEvent::STARTED, 0,
1508 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1509 ->animation_group_id(),
1510 cc::Animation::OPACITY, fourth_effective_start));
1512 // Skip ahead by a lot.
1513 animator->Step(fourth_effective_start + 1000 * delta);
1515 EXPECT_TRUE(test_controller.animator()->is_animating());
1516 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1518 base::TimeTicks fifth_effective_start = fourth_effective_start + 1001 * delta;
1519 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1520 cc::AnimationEvent::STARTED, 0,
1521 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1522 ->animation_group_id(),
1523 cc::Animation::OPACITY, fifth_effective_start));
1525 // Skip ahead by a lot.
1526 animator->Step(fifth_effective_start + 999 * delta);
1528 EXPECT_TRUE(test_controller.animator()->is_animating());
1529 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1531 test_controller.animator()->StopAnimatingProperty(
1532 LayerAnimationElement::OPACITY);
1534 EXPECT_FALSE(test_controller.animator()->is_animating());
1537 TEST(LayerAnimatorTest, AddObserverExplicit) {
1538 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1539 animator->set_disable_timer_for_test(true);
1540 TestLayerAnimationObserver observer;
1541 TestLayerAnimationDelegate delegate;
1542 animator->SetDelegate(&delegate);
1543 animator->AddObserver(&observer);
1544 observer.set_requires_notification_when_animator_destroyed(true);
1546 EXPECT_TRUE(!observer.last_ended_sequence());
1548 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1550 delegate.SetBrightnessFromAnimation(0.0f);
1552 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1553 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1555 animator->StartAnimation(sequence);
1557 EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1559 base::TimeTicks start_time = animator->last_step_time();
1561 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1563 EXPECT_EQ(observer.last_ended_sequence(), sequence);
1565 // |sequence| has been destroyed. Recreate it to test abort.
1566 sequence = new LayerAnimationSequence(
1567 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1569 animator->StartAnimation(sequence);
1571 animator = NULL;
1573 EXPECT_EQ(observer.last_aborted_sequence(), sequence);
1576 // Tests that an observer added to a scoped settings object is still notified
1577 // when the object goes out of scope.
1578 TEST(LayerAnimatorTest, ImplicitAnimationObservers) {
1579 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1580 animator->set_disable_timer_for_test(true);
1581 TestImplicitAnimationObserver observer(false);
1582 TestLayerAnimationDelegate delegate;
1583 animator->SetDelegate(&delegate);
1585 EXPECT_FALSE(observer.animations_completed());
1586 animator->SetBrightness(1.0f);
1589 ScopedLayerAnimationSettings settings(animator.get());
1590 settings.AddObserver(&observer);
1591 animator->SetBrightness(0.0f);
1594 EXPECT_FALSE(observer.animations_completed());
1595 base::TimeTicks start_time = animator->last_step_time();
1596 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1597 EXPECT_TRUE(observer.animations_completed());
1598 EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1599 LayerAnimationElement::BRIGHTNESS));
1600 EXPECT_FLOAT_EQ(0.0f, delegate.GetBrightnessForAnimation());
1603 // Tests that an observer added to a scoped settings object is still notified
1604 // when the object goes out of scope due to the animation being interrupted.
1605 TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) {
1606 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1607 animator->set_disable_timer_for_test(true);
1608 TestImplicitAnimationObserver observer(false);
1609 TestLayerAnimationDelegate delegate;
1610 animator->SetDelegate(&delegate);
1612 EXPECT_FALSE(observer.animations_completed());
1613 animator->SetBrightness(1.0f);
1616 ScopedLayerAnimationSettings settings(animator.get());
1617 settings.AddObserver(&observer);
1618 animator->SetBrightness(0.0f);
1621 EXPECT_FALSE(observer.animations_completed());
1622 // This should interrupt the implicit animation causing the observer to be
1623 // notified immediately.
1624 animator->SetBrightness(1.0f);
1625 EXPECT_TRUE(observer.animations_completed());
1626 EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1627 LayerAnimationElement::BRIGHTNESS));
1628 EXPECT_FLOAT_EQ(1.0f, delegate.GetBrightnessForAnimation());
1631 // Tests that LayerAnimator is not deleted after the animation completes as long
1632 // as there is a live ScopedLayerAnimationSettings object wrapped around it.
1633 TEST(LayerAnimatorTest, AnimatorKeptAliveBySettings) {
1634 // Note we are using a raw pointer unlike in other tests.
1635 TestLayerAnimator* animator = new TestLayerAnimator();
1636 LayerAnimatorDestructionObserver destruction_observer;
1637 animator->SetDestructionObserver(&destruction_observer);
1638 animator->set_disable_timer_for_test(true);
1639 TestLayerAnimationDelegate delegate;
1640 animator->SetDelegate(&delegate);
1642 // ScopedLayerAnimationSettings should keep the Animator alive as long as
1643 // it is alive, even beyond the end of the animation.
1644 ScopedLayerAnimationSettings settings(animator);
1645 base::TimeTicks now = gfx::FrameTime::Now();
1646 animator->SetBrightness(0.5);
1647 animator->Step(now + base::TimeDelta::FromSeconds(1));
1648 EXPECT_FALSE(destruction_observer.IsAnimatorDeleted());
1650 // ScopedLayerAnimationSettings was destroyed, so Animator should be deleted.
1651 EXPECT_TRUE(destruction_observer.IsAnimatorDeleted());
1654 // Tests that an observer added to a scoped settings object is not notified
1655 // when the animator is destroyed unless explicitly requested.
1656 TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) {
1657 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1658 animator->set_disable_timer_for_test(true);
1659 TestImplicitAnimationObserver observer_notify(true);
1660 TestImplicitAnimationObserver observer_do_not_notify(false);
1661 TestLayerAnimationDelegate delegate;
1662 animator->SetDelegate(&delegate);
1664 EXPECT_FALSE(observer_notify.animations_completed());
1665 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1667 animator->SetBrightness(1.0f);
1670 ScopedLayerAnimationSettings settings(animator.get());
1671 settings.AddObserver(&observer_notify);
1672 settings.AddObserver(&observer_do_not_notify);
1673 animator->SetBrightness(0.0f);
1676 EXPECT_FALSE(observer_notify.animations_completed());
1677 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1678 animator = NULL;
1679 EXPECT_TRUE(observer_notify.animations_completed());
1680 EXPECT_TRUE(observer_notify.WasAnimationAbortedForProperty(
1681 LayerAnimationElement::BRIGHTNESS));
1682 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1685 TEST(LayerAnimatorTest, AbortedAnimationStatusInImplicitObservers) {
1686 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1687 animator->set_disable_timer_for_test(true);
1688 TestImplicitAnimationObserver observer(false);
1689 TestLayerAnimationDelegate delegate;
1690 animator->SetDelegate(&delegate);
1692 EXPECT_FALSE(observer.animations_completed());
1693 animator->SetBrightness(1.0f);
1696 ScopedLayerAnimationSettings settings(animator.get());
1697 settings.AddObserver(&observer);
1698 animator->SetBrightness(0.0f);
1700 EXPECT_FALSE(observer.animations_completed());
1702 animator->AbortAllAnimations();
1703 EXPECT_TRUE(observer.animations_completed());
1704 EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1705 LayerAnimationElement::BRIGHTNESS));
1706 EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
1707 LayerAnimationElement::OPACITY));
1709 observer.set_animations_completed(false);
1711 ScopedLayerAnimationSettings settings(animator.get());
1712 settings.AddObserver(&observer);
1713 animator->SetOpacity(0.0f);
1715 EXPECT_FALSE(observer.animations_completed());
1717 animator->AbortAllAnimations();
1718 EXPECT_TRUE(observer.animations_completed());
1719 EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1720 LayerAnimationElement::BRIGHTNESS));
1721 EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1722 LayerAnimationElement::OPACITY));
1725 TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) {
1726 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1727 animator->set_disable_timer_for_test(true);
1728 TestLayerAnimationObserver observer;
1729 TestLayerAnimationObserver removed_observer;
1730 TestLayerAnimationDelegate delegate;
1731 animator->SetDelegate(&delegate);
1733 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1735 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1736 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1738 sequence->AddObserver(&observer);
1739 sequence->AddObserver(&removed_observer);
1741 animator->StartAnimation(sequence);
1743 EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1744 EXPECT_TRUE(!observer.last_ended_sequence());
1745 EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence);
1746 EXPECT_TRUE(!removed_observer.last_ended_sequence());
1748 // This should stop the observer from observing sequence.
1749 animator->RemoveObserver(&removed_observer);
1751 base::TimeTicks start_time = animator->last_step_time();
1753 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1755 EXPECT_EQ(observer.last_ended_sequence(), sequence);
1756 EXPECT_TRUE(!removed_observer.last_ended_sequence());
1759 TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) {
1760 TestLayerAnimationDelegate delegate;
1761 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1762 animator->set_disable_timer_for_test(true);
1764 scoped_ptr<TestLayerAnimationObserver> observer(
1765 new TestLayerAnimationObserver);
1766 animator->SetDelegate(&delegate);
1767 animator->AddObserver(observer.get());
1769 delegate.SetOpacityFromAnimation(0.0f);
1771 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1772 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1773 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
1775 animator->StartAnimation(sequence);
1777 // |observer| should be attached to |sequence|.
1778 EXPECT_TRUE(sequence->observers_.might_have_observers());
1780 // Now, release |observer|
1781 observer.reset();
1783 // And |sequence| should no longer be attached to |observer|.
1784 EXPECT_FALSE(sequence->observers_.might_have_observers());
1787 TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) {
1788 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1789 animator->set_disable_timer_for_test(true);
1791 TestImplicitAnimationObserver observer(false);
1792 TestLayerAnimationDelegate delegate;
1793 animator->SetDelegate(&delegate);
1795 delegate.SetBrightnessFromAnimation(0.0f);
1798 ScopedLayerAnimationSettings setter(animator.get());
1800 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1801 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1802 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1804 animator->StartAnimation(sequence);
1805 base::TimeTicks start_time = animator->last_step_time();
1806 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1808 setter.AddObserver(&observer);
1810 // Start observing an in-flight animation.
1811 sequence->AddObserver(&observer);
1813 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1816 EXPECT_TRUE(observer.animations_completed());
1817 EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1818 LayerAnimationElement::BRIGHTNESS));
1821 TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) {
1822 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1823 animator->set_disable_timer_for_test(true);
1825 TestImplicitAnimationObserver observer(false);
1826 TestLayerAnimationDelegate delegate;
1827 animator->SetDelegate(&delegate);
1829 delegate.SetBrightnessFromAnimation(0.0f);
1830 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1831 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1832 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1835 ScopedLayerAnimationSettings setter(animator.get());
1836 setter.AddObserver(&observer);
1838 animator->StartAnimation(sequence);
1839 base::TimeTicks start_time = animator->last_step_time();
1840 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1843 EXPECT_FALSE(observer.animations_completed());
1845 // Stop observing an in-flight animation.
1846 sequence->RemoveObserver(&observer);
1848 EXPECT_TRUE(observer.animations_completed());
1850 // The animation didn't complete, and neither was it aborted.
1851 EXPECT_FALSE(observer.WasAnimationCompletedForProperty(
1852 LayerAnimationElement::BRIGHTNESS));
1853 EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
1854 LayerAnimationElement::BRIGHTNESS));
1857 // This checks that if an animation is deleted due to a callback, that the
1858 // animator does not try to use the deleted animation. For example, if we have
1859 // two running animations, and the first finishes and the resulting callback
1860 // causes the second to be deleted, we should not attempt to animate the second
1861 // animation.
1862 TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnEnd) {
1863 ScopedAnimationDurationScaleMode normal_duration_mode(
1864 ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1865 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1866 animator->set_disable_timer_for_test(true);
1867 TestLayerAnimationDelegate delegate;
1868 animator->SetDelegate(&delegate);
1870 double start_brightness(0.0);
1871 double target_brightness(1.0);
1873 gfx::Rect start_bounds(0, 0, 50, 50);
1874 gfx::Rect target_bounds(5, 5, 5, 5);
1876 delegate.SetBrightnessFromAnimation(start_brightness);
1877 delegate.SetBoundsFromAnimation(start_bounds);
1879 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
1880 base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2);
1881 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3);
1883 scoped_ptr<DeletingLayerAnimationObserver> observer(
1884 new DeletingLayerAnimationObserver(animator.get()));
1886 animator->AddObserver(observer.get());
1888 animator->StartAnimation(
1889 new LayerAnimationSequence(
1890 LayerAnimationElement::CreateBrightnessElement(
1891 target_brightness, brightness_delta)));
1893 animator->StartAnimation(new LayerAnimationSequence(
1894 LayerAnimationElement::CreateBoundsElement(
1895 target_bounds, bounds_delta)));
1896 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1898 base::TimeTicks start_time = animator->last_step_time();
1899 animator->Step(start_time + halfway_delta);
1901 // Completing the brightness animation should have stopped the bounds
1902 // animation.
1903 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1905 animator->RemoveObserver(observer.get());
1908 // Ensure that stopping animation in a bounds change does not crash and that
1909 // animation gets stopped correctly.
1910 // This scenario is possible when animation is restarted from inside a
1911 // callback triggered by the animation progress.
1912 TEST(LayerAnimatorTest, CallbackDeletesAnimationInProgress) {
1914 class TestLayerAnimationDeletingDelegate : public TestLayerAnimationDelegate {
1915 public:
1916 TestLayerAnimationDeletingDelegate(LayerAnimator* animator, int max_width)
1917 : animator_(animator),
1918 max_width_(max_width) {
1921 void SetBoundsFromAnimation(const gfx::Rect& bounds) override {
1922 TestLayerAnimationDelegate::SetBoundsFromAnimation(bounds);
1923 if (bounds.width() > max_width_)
1924 animator_->StopAnimating();
1926 private:
1927 LayerAnimator* animator_;
1928 int max_width_;
1929 // Allow copy and assign.
1932 ScopedAnimationDurationScaleMode normal_duration_mode(
1933 ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1934 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1935 animator->set_disable_timer_for_test(true);
1936 TestLayerAnimationDeletingDelegate delegate(animator.get(), 30);
1937 animator->SetDelegate(&delegate);
1939 gfx::Rect start_bounds(0, 0, 0, 0);
1940 gfx::Rect target_bounds(5, 5, 50, 50);
1942 delegate.SetBoundsFromAnimation(start_bounds);
1944 base::TimeDelta bounds_delta1 = base::TimeDelta::FromMilliseconds(333);
1945 base::TimeDelta bounds_delta2 = base::TimeDelta::FromMilliseconds(666);
1946 base::TimeDelta bounds_delta = base::TimeDelta::FromMilliseconds(1000);
1947 base::TimeDelta final_delta = base::TimeDelta::FromMilliseconds(1500);
1949 animator->StartAnimation(new LayerAnimationSequence(
1950 LayerAnimationElement::CreateBoundsElement(
1951 target_bounds, bounds_delta)));
1952 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1954 base::TimeTicks start_time = animator->last_step_time();
1955 ASSERT_NO_FATAL_FAILURE(animator->Step(start_time + bounds_delta1));
1956 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1958 // The next step should change the animated bounds past the threshold and
1959 // cause the animaton to stop.
1960 ASSERT_NO_FATAL_FAILURE(animator->Step(start_time + bounds_delta2));
1961 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1962 ASSERT_NO_FATAL_FAILURE(animator->Step(start_time + final_delta));
1964 // Completing the animation should have stopped the bounds
1965 // animation.
1966 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1969 // Similar to the ObserverDeletesAnimationsOnEnd test above except that it
1970 // tests the behavior when the OnLayerAnimationAborted() callback causes
1971 // all of the animator's other animations to be deleted.
1972 TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnAbort) {
1973 ScopedAnimationDurationScaleMode test_duration_mode(
1974 ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
1975 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1976 animator->set_disable_timer_for_test(true);
1977 TestLayerAnimationDelegate delegate;
1978 animator->SetDelegate(&delegate);
1980 double start_brightness(0.0);
1981 double target_brightness(1.0);
1982 gfx::Rect start_bounds(0, 0, 50, 50);
1983 gfx::Rect target_bounds(5, 5, 5, 5);
1984 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
1985 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(2);
1987 delegate.SetBrightnessFromAnimation(start_brightness);
1988 delegate.SetBoundsFromAnimation(start_bounds);
1990 scoped_ptr<DeletingLayerAnimationObserver> observer(
1991 new DeletingLayerAnimationObserver(animator.get()));
1992 animator->AddObserver(observer.get());
1994 animator->StartAnimation(
1995 new LayerAnimationSequence(
1996 LayerAnimationElement::CreateBrightnessElement(
1997 target_brightness, brightness_delta)));
1998 animator->StartAnimation(new LayerAnimationSequence(
1999 LayerAnimationElement::CreateBoundsElement(
2000 target_bounds, bounds_delta)));
2001 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2003 animator->set_preemption_strategy(
2004 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2005 animator->StartAnimation(
2006 new LayerAnimationSequence(
2007 LayerAnimationElement::CreateBrightnessElement(
2008 target_brightness, brightness_delta)));
2010 // Starting the second brightness animation should have aborted the initial
2011 // brightness animation. |observer| should have stopped the bounds animation
2012 // as a result.
2013 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2015 animator->RemoveObserver(observer.get());
2018 // Check that setting a property during an animation with a default animator
2019 // cancels the original animation.
2020 TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) {
2021 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2022 animator->set_disable_timer_for_test(true);
2023 TestLayerAnimationDelegate delegate;
2024 animator->SetDelegate(&delegate);
2026 double start_opacity(0.0);
2027 double target_opacity(1.0);
2029 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2031 delegate.SetOpacityFromAnimation(start_opacity);
2033 scoped_ptr<LayerAnimationSequence> sequence(
2034 new LayerAnimationSequence(
2035 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
2037 animator->StartAnimation(sequence.release());
2039 animator->SetOpacity(0.5);
2041 EXPECT_FALSE(animator->is_animating());
2042 EXPECT_EQ(0.5, animator->GetTargetOpacity());
2045 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
2046 // second sequence to be leaked.
2047 TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) {
2048 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2049 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
2050 animator->set_disable_timer_for_test(true);
2051 TestLayerAnimationDelegate delegate;
2052 animator->SetDelegate(&delegate);
2054 gfx::Rect start_bounds(0, 0, 50, 50);
2055 gfx::Rect middle_bounds(10, 10, 100, 100);
2056 gfx::Rect target_bounds(5, 5, 5, 5);
2058 delegate.SetBoundsFromAnimation(start_bounds);
2061 // start an implicit bounds animation.
2062 ScopedLayerAnimationSettings settings(animator.get());
2063 animator->SetBounds(middle_bounds);
2066 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2068 int num_live_instances = 0;
2069 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2070 scoped_ptr<TestLayerAnimationSequence> sequence(
2071 new TestLayerAnimationSequence(
2072 LayerAnimationElement::CreateBoundsElement(target_bounds, delta),
2073 &num_live_instances));
2075 EXPECT_EQ(1, num_live_instances);
2077 // This should interrupt the running sequence causing us to immediately set
2078 // the target value. The sequence should alse be destructed.
2079 animator->StartAnimation(sequence.release());
2081 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2082 EXPECT_EQ(0, num_live_instances);
2083 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
2086 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
2087 TEST(LayerAnimatorTest, GetTargetOpacity) {
2088 TestLayerAnimationDelegate delegate;
2089 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2090 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2091 animator->set_disable_timer_for_test(true);
2092 animator->SetDelegate(&delegate);
2094 delegate.SetOpacityFromAnimation(0.0);
2097 ScopedLayerAnimationSettings settings(animator.get());
2098 animator->SetOpacity(0.5);
2099 EXPECT_EQ(0.5, animator->GetTargetOpacity());
2101 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2102 animator->SetOpacity(1.0);
2103 EXPECT_EQ(1.0, animator->GetTargetOpacity());
2107 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
2108 TEST(LayerAnimatorTest, GetTargetBrightness) {
2109 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2110 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2111 animator->set_disable_timer_for_test(true);
2112 TestLayerAnimationDelegate delegate;
2113 animator->SetDelegate(&delegate);
2115 delegate.SetBrightnessFromAnimation(0.0);
2118 ScopedLayerAnimationSettings settings(animator.get());
2119 animator->SetBrightness(0.5);
2120 EXPECT_EQ(0.5, animator->GetTargetBrightness());
2122 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2123 animator->SetBrightness(1.0);
2124 EXPECT_EQ(1.0, animator->GetTargetBrightness());
2128 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
2129 TEST(LayerAnimatorTest, GetTargetGrayscale) {
2130 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2131 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2132 animator->set_disable_timer_for_test(true);
2133 TestLayerAnimationDelegate delegate;
2134 animator->SetDelegate(&delegate);
2136 delegate.SetGrayscaleFromAnimation(0.0);
2139 ScopedLayerAnimationSettings settings(animator.get());
2140 animator->SetGrayscale(0.5);
2141 EXPECT_EQ(0.5, animator->GetTargetGrayscale());
2143 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2144 animator->SetGrayscale(1.0);
2145 EXPECT_EQ(1.0, animator->GetTargetGrayscale());
2149 // Verifies color property is modified appropriately.
2150 TEST(LayerAnimatorTest, Color) {
2151 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2152 animator->set_disable_timer_for_test(true);
2153 TestLayerAnimationDelegate delegate;
2154 animator->SetDelegate(&delegate);
2156 SkColor start_color = SkColorSetARGB( 64, 20, 40, 60);
2157 SkColor middle_color = SkColorSetARGB(128, 35, 70, 120);
2158 SkColor target_color = SkColorSetARGB(192, 40, 80, 140);
2160 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2162 delegate.SetColorFromAnimation(start_color);
2164 animator->ScheduleAnimation(
2165 new LayerAnimationSequence(
2166 LayerAnimationElement::CreateColorElement(target_color, delta)));
2168 EXPECT_TRUE(animator->is_animating());
2169 EXPECT_EQ(ColorToString(start_color),
2170 ColorToString(delegate.GetColorForAnimation()));
2172 base::TimeTicks start_time = animator->last_step_time();
2174 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
2176 EXPECT_TRUE(animator->is_animating());
2177 EXPECT_EQ(ColorToString(middle_color),
2178 ColorToString(delegate.GetColorForAnimation()));
2180 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2182 EXPECT_FALSE(animator->is_animating());
2183 EXPECT_EQ(ColorToString(target_color),
2184 ColorToString(delegate.GetColorForAnimation()));
2187 // Verifies SchedulePauseForProperties().
2188 TEST(LayerAnimatorTest, SchedulePauseForProperties) {
2189 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2190 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2191 animator->SchedulePauseForProperties(
2192 base::TimeDelta::FromMilliseconds(100),
2193 LayerAnimationElement::TRANSFORM | LayerAnimationElement::BOUNDS);
2194 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM));
2195 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2196 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY));
2200 class AnimatorOwner {
2201 public:
2202 AnimatorOwner()
2203 : animator_(LayerAnimator::CreateDefaultAnimator()) {
2206 LayerAnimator* animator() { return animator_.get(); }
2208 private:
2209 scoped_refptr<LayerAnimator> animator_;
2211 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner);
2214 class DeletingObserver : public LayerAnimationObserver {
2215 public:
2216 DeletingObserver(bool* was_deleted)
2217 : animator_owner_(new AnimatorOwner),
2218 delete_on_animation_ended_(false),
2219 delete_on_animation_aborted_(false),
2220 delete_on_animation_scheduled_(false),
2221 was_deleted_(was_deleted) {
2222 animator()->AddObserver(this);
2225 ~DeletingObserver() override {
2226 animator()->RemoveObserver(this);
2227 *was_deleted_ = true;
2230 LayerAnimator* animator() { return animator_owner_->animator(); }
2232 bool delete_on_animation_ended() const {
2233 return delete_on_animation_ended_;
2235 void set_delete_on_animation_ended(bool enabled) {
2236 delete_on_animation_ended_ = enabled;
2239 bool delete_on_animation_aborted() const {
2240 return delete_on_animation_aborted_;
2242 void set_delete_on_animation_aborted(bool enabled) {
2243 delete_on_animation_aborted_ = enabled;
2246 bool delete_on_animation_scheduled() const {
2247 return delete_on_animation_scheduled_;
2249 void set_delete_on_animation_scheduled(bool enabled) {
2250 delete_on_animation_scheduled_ = enabled;
2253 // LayerAnimationObserver implementation.
2254 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
2255 if (delete_on_animation_ended_)
2256 delete this;
2259 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {
2260 if (delete_on_animation_aborted_)
2261 delete this;
2264 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {
2265 if (delete_on_animation_scheduled_)
2266 delete this;
2269 private:
2270 scoped_ptr<AnimatorOwner> animator_owner_;
2271 bool delete_on_animation_ended_;
2272 bool delete_on_animation_aborted_;
2273 bool delete_on_animation_scheduled_;
2274 bool* was_deleted_;
2276 DISALLOW_COPY_AND_ASSIGN(DeletingObserver);
2279 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) {
2280 bool observer_was_deleted = false;
2281 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2282 observer->set_delete_on_animation_ended(true);
2283 observer->set_delete_on_animation_aborted(true);
2284 LayerAnimator* animator = observer->animator();
2285 animator->set_disable_timer_for_test(true);
2286 TestLayerAnimationDelegate delegate;
2287 animator->SetDelegate(&delegate);
2289 delegate.SetBrightnessFromAnimation(0.0f);
2291 gfx::Rect start_bounds(0, 0, 50, 50);
2292 gfx::Rect target_bounds(10, 10, 100, 100);
2294 delegate.SetBoundsFromAnimation(start_bounds);
2296 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2297 LayerAnimationSequence* brightness_sequence = new LayerAnimationSequence(
2298 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
2299 animator->StartAnimation(brightness_sequence);
2301 delta = base::TimeDelta::FromSeconds(2);
2302 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2303 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2304 animator->StartAnimation(bounds_sequence);
2306 base::TimeTicks start_time = animator->last_step_time();
2307 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
2309 EXPECT_TRUE(observer_was_deleted);
2312 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterStoppingAnimating) {
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.SetOpacityFromAnimation(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* opacity_sequence = new LayerAnimationSequence(
2331 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
2332 animator->StartAnimation(opacity_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 animator->StopAnimating();
2341 EXPECT_TRUE(observer_was_deleted);
2344 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterScheduling) {
2345 bool observer_was_deleted = false;
2346 TestLayerAnimationDelegate delegate;
2347 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2348 observer->set_delete_on_animation_scheduled(true);
2349 LayerAnimator* animator = observer->animator();
2350 animator->set_disable_timer_for_test(true);
2351 animator->SetDelegate(&delegate);
2353 delegate.SetOpacityFromAnimation(0.0f);
2355 gfx::Rect start_bounds(0, 0, 50, 50);
2356 gfx::Rect target_bounds(10, 10, 100, 100);
2358 delegate.SetBoundsFromAnimation(start_bounds);
2360 std::vector<LayerAnimationSequence*> to_start;
2362 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2363 to_start.push_back(new LayerAnimationSequence(
2364 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2366 delta = base::TimeDelta::FromSeconds(2);
2367 to_start.push_back(new LayerAnimationSequence(
2368 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2370 animator->ScheduleTogether(to_start);
2372 EXPECT_TRUE(observer_was_deleted);
2375 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterAborted) {
2376 bool observer_was_deleted = false;
2377 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2378 TestLayerAnimationDelegate delegate;
2379 observer->set_delete_on_animation_aborted(true);
2380 LayerAnimator* animator = observer->animator();
2381 animator->set_preemption_strategy(
2382 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
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_FALSE(observer_was_deleted);
2407 animator->StartAnimation(new LayerAnimationSequence(
2408 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2410 EXPECT_TRUE(observer_was_deleted);
2414 TEST(LayerAnimatorTest, TestSetterRespectEnqueueStrategy) {
2415 TestLayerAnimationDelegate delegate;
2416 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2417 animator->set_disable_timer_for_test(true);
2419 animator->SetDelegate(&delegate);
2421 float start_opacity = 0.0f;
2422 float target_opacity = 1.0f;
2423 float magic_opacity = 0.123f;
2425 delegate.SetOpacityFromAnimation(start_opacity);
2427 ScopedLayerAnimationSettings settings(animator.get());
2428 settings.SetPreemptionStrategy(
2429 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2430 settings.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2431 animator->SetOpacity(target_opacity);
2433 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2435 settings.SetPreemptionStrategy(
2436 LayerAnimator::ENQUEUE_NEW_ANIMATION);
2437 settings.SetTransitionDuration(base::TimeDelta());
2438 animator->SetOpacity(magic_opacity);
2440 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2443 TEST(LayerAnimatorTest, TestScopedCounterAnimation) {
2444 Layer parent, child;
2445 parent.Add(&child);
2447 gfx::Transform parent_begin, parent_end;
2449 parent_end.Scale3d(2.0, 0.5, 1.0);
2451 // Parent animates from identity to the end value. The counter animation will
2452 // start at the end value and animate back to identity.
2453 gfx::Transform child_begin(parent_end);
2455 child.SetTransform(child_begin);
2456 parent.SetTransform(parent_begin);
2458 EXPECT_FALSE(child.GetAnimator()->is_animating());
2460 ScopedLayerAnimationSettings settings(parent.GetAnimator());
2461 settings.SetInverselyAnimatedBaseLayer(&parent);
2462 settings.AddInverselyAnimatedLayer(&child);
2464 parent.SetTransform(parent_end);
2466 EXPECT_TRUE(child.GetAnimator()->is_animating());
2467 EXPECT_TRUE(child.GetTargetTransform().IsIdentity())
2468 << child.GetTargetTransform().ToString();
2472 class CollectionLayerAnimationDelegate : public TestLayerAnimationDelegate {
2473 public:
2474 CollectionLayerAnimationDelegate() : collection(NULL) {}
2475 ~CollectionLayerAnimationDelegate() override {}
2477 // LayerAnimationDelegate:
2478 LayerAnimatorCollection* GetLayerAnimatorCollection() override {
2479 return &collection;
2482 private:
2483 LayerAnimatorCollection collection;
2486 TEST(LayerAnimatorTest, LayerAnimatorCollectionTickTime) {
2487 Layer layer;
2488 LayerAnimator* animator = layer.GetAnimator();
2489 CollectionLayerAnimationDelegate delegate;
2490 animator->SetDelegate(&delegate);
2492 LayerAnimatorCollection* collection = delegate.GetLayerAnimatorCollection();
2493 base::TimeTicks null;
2494 collection->OnAnimationStep(null);
2495 EXPECT_TRUE(collection->last_tick_time().is_null());
2497 // Adding an animator to the collection should update the last tick time.
2498 collection->StartAnimator(layer.GetAnimator());
2499 EXPECT_TRUE(collection->HasActiveAnimators());
2500 EXPECT_FALSE(collection->last_tick_time().is_null());
2502 collection->StopAnimator(layer.GetAnimator());
2503 EXPECT_FALSE(collection->HasActiveAnimators());
2506 TEST(LayerAnimatorTest, AnimatorStartedCorrectly) {
2507 Layer layer;
2508 LayerAnimatorTestController test_controller(layer.GetAnimator());
2509 LayerAnimator* animator = test_controller.animator();
2510 ASSERT_FALSE(animator->is_started_);
2512 TestLayerAnimationDelegate test_delegate;
2513 animator->SetDelegate(&test_delegate);
2514 double target_opacity = 1.0;
2515 base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2516 animator->ScheduleAnimation(new LayerAnimationSequence(
2517 LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta)));
2518 EXPECT_FALSE(animator->is_started_);
2520 CollectionLayerAnimationDelegate collection_delegate;
2521 animator->SetDelegate(&collection_delegate);
2522 animator->UpdateAnimationState();
2523 EXPECT_TRUE(animator->is_started_);
2524 animator->SetDelegate(NULL);
2527 TEST(LayerAnimatorTest, AnimatorRemovedFromCollectionWhenLayerIsDestroyed) {
2528 scoped_ptr<Layer> layer(new Layer(LAYER_TEXTURED));
2529 LayerAnimatorTestController test_controller(layer->GetAnimator());
2530 scoped_refptr<LayerAnimator> animator = test_controller.animator();
2531 CollectionLayerAnimationDelegate collection_delegate;
2532 animator->SetDelegate(&collection_delegate);
2534 double target_opacity = 1.0;
2535 base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2536 animator->ScheduleAnimation(new LayerAnimationSequence(
2537 LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta)));
2539 EXPECT_TRUE(
2540 collection_delegate.GetLayerAnimatorCollection()->HasActiveAnimators());
2542 layer.reset();
2543 EXPECT_EQ(NULL, animator->delegate());
2544 EXPECT_FALSE(
2545 collection_delegate.GetLayerAnimatorCollection()->HasActiveAnimators());
2548 TEST(LayerAnimatorTest, LayerMovedBetweenCompositorsDuringAnimation) {
2549 bool enable_pixel_output = false;
2550 ui::ContextFactory* context_factory =
2551 InitializeContextFactoryForTests(enable_pixel_output);
2552 const gfx::Rect bounds(10, 10, 100, 100);
2553 scoped_ptr<TestCompositorHost> host_1(
2554 TestCompositorHost::Create(bounds, context_factory));
2555 scoped_ptr<TestCompositorHost> host_2(
2556 TestCompositorHost::Create(bounds, context_factory));
2557 host_1->Show();
2558 host_2->Show();
2560 Compositor* compositor_1 = host_1->GetCompositor();
2561 Layer root_1;
2562 compositor_1->SetRootLayer(&root_1);
2564 Compositor* compositor_2 = host_2->GetCompositor();
2565 Layer root_2;
2566 compositor_2->SetRootLayer(&root_2);
2568 // Verify that neither compositor has active animators.
2569 EXPECT_FALSE(compositor_1->layer_animator_collection()->HasActiveAnimators());
2570 EXPECT_FALSE(compositor_2->layer_animator_collection()->HasActiveAnimators());
2572 Layer layer;
2573 root_1.Add(&layer);
2574 LayerAnimator* animator = layer.GetAnimator();
2575 double target_opacity = 1.0;
2576 base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2577 animator->ScheduleAnimation(new LayerAnimationSequence(
2578 LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta)));
2579 EXPECT_TRUE(compositor_1->layer_animator_collection()->HasActiveAnimators());
2580 EXPECT_FALSE(compositor_2->layer_animator_collection()->HasActiveAnimators());
2582 root_2.Add(&layer);
2583 EXPECT_FALSE(compositor_1->layer_animator_collection()->HasActiveAnimators());
2584 EXPECT_TRUE(compositor_2->layer_animator_collection()->HasActiveAnimators());
2585 host_2.reset();
2586 host_1.reset();
2587 TerminateContextFactoryForTests();
2590 class LayerOwnerAnimationObserver : public LayerAnimationObserver {
2591 public:
2592 LayerOwnerAnimationObserver(LayerAnimator* animator)
2593 : animator_layer_(new Layer(LAYER_TEXTURED)) {
2594 animator_layer_->SetAnimator(animator);
2597 ~LayerOwnerAnimationObserver() override {}
2599 void OnLayerAnimationEnded(LayerAnimationSequence* sequence) override {
2600 ASSERT_TRUE(sequence);
2601 animator_layer_.reset();
2604 void OnLayerAnimationAborted(LayerAnimationSequence* sequence) override {
2605 ASSERT_TRUE(sequence);
2606 animator_layer_.reset();
2609 LayerAnimationDelegate* animator_layer() {
2610 return animator_layer_.get();
2613 void OnLayerAnimationScheduled(LayerAnimationSequence* sequence) override {}
2615 private:
2616 scoped_ptr<Layer> animator_layer_;
2618 DISALLOW_COPY_AND_ASSIGN(LayerOwnerAnimationObserver);
2621 TEST(LayerAnimatorTest, ObserverDeletesLayerInStopAnimating) {
2622 scoped_refptr<LayerAnimator> animator(
2623 LayerAnimator::CreateImplicitAnimator());
2624 animator->set_disable_timer_for_test(true);
2625 LayerOwnerAnimationObserver observer(animator.get());
2626 LayerAnimationDelegate* delegate = observer.animator_layer();
2628 const gfx::Rect start_bounds(0, 0, 50, 50);
2629 const gfx::Rect target_bounds(10, 10, 100, 100);
2630 const double target_opacity = 1.0;
2632 delegate->SetOpacityFromAnimation(0.0f);
2633 delegate->SetBoundsFromAnimation(start_bounds);
2635 base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2636 LayerAnimationSequence* opacity = new LayerAnimationSequence(
2637 LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta));
2638 opacity->AddObserver(&observer);
2639 animator->ScheduleAnimation(opacity);
2640 time_delta = base::TimeDelta::FromSeconds(2);
2641 LayerAnimationSequence* move = new LayerAnimationSequence(
2642 LayerAnimationElement::CreateBoundsElement(target_bounds, time_delta));
2643 animator->ScheduleAnimation(move);
2644 EXPECT_TRUE(animator->is_animating());
2645 animator->Step(animator->last_step_time() +
2646 base::TimeDelta::FromMilliseconds(500));
2647 animator->StopAnimating();
2649 EXPECT_EQ(nullptr, observer.animator_layer());
2650 EXPECT_TRUE(animator->is_animating());
2653 } // namespace ui