Home | History | Annotate | Download | only in compositor
      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.
      4 
      5 #include "ui/compositor/layer_animator.h"
      6 
      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_animation_delegate.h"
     14 #include "ui/compositor/layer_animation_element.h"
     15 #include "ui/compositor/layer_animation_sequence.h"
     16 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
     17 #include "ui/compositor/scoped_layer_animation_settings.h"
     18 #include "ui/compositor/test/layer_animator_test_controller.h"
     19 #include "ui/compositor/test/test_layer_animation_delegate.h"
     20 #include "ui/compositor/test/test_layer_animation_observer.h"
     21 #include "ui/compositor/test/test_utils.h"
     22 #include "ui/gfx/rect.h"
     23 #include "ui/gfx/transform.h"
     24 
     25 namespace ui {
     26 
     27 namespace {
     28 
     29 // Converts |color| to a string. Each component of the color is separated by a
     30 // space and the order if A R G B.
     31 std::string ColorToString(SkColor color) {
     32   return base::StringPrintf("%d %d %d %d", SkColorGetA(color),
     33                             SkColorGetR(color), SkColorGetG(color),
     34                             SkColorGetB(color));
     35 }
     36 
     37 // Creates vector with two LayerAnimationSequences, based on |first| and
     38 // |second| layer animation elements.
     39 std::vector<LayerAnimationSequence*> CreateMultiSequence(
     40     LayerAnimationElement* first,
     41     LayerAnimationElement* second) {
     42   LayerAnimationSequence* first_sequence = new LayerAnimationSequence();
     43   first_sequence->AddElement(first);
     44   LayerAnimationSequence* second_sequence = new LayerAnimationSequence();
     45   second_sequence->AddElement(second);
     46 
     47   std::vector<ui::LayerAnimationSequence*> animations;
     48   animations.push_back(first_sequence);
     49   animations.push_back(second_sequence);
     50   return animations;
     51 }
     52 
     53 class TestImplicitAnimationObserver : public ImplicitAnimationObserver {
     54  public:
     55   explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed)
     56     : animations_completed_(false),
     57       notify_when_animator_destructed_(notify_when_animator_destructed) {
     58   }
     59 
     60   bool animations_completed() const { return animations_completed_; }
     61   void set_animations_completed(bool completed) {
     62     animations_completed_ = completed;
     63   }
     64 
     65   bool WasAnimationAbortedForProperty(
     66       LayerAnimationElement::AnimatableProperty property) const {
     67     return ImplicitAnimationObserver::WasAnimationAbortedForProperty(property);
     68   }
     69 
     70   bool WasAnimationCompletedForProperty(
     71       LayerAnimationElement::AnimatableProperty property) const {
     72     return ImplicitAnimationObserver::WasAnimationCompletedForProperty(
     73         property);
     74   }
     75 
     76  private:
     77   // ImplicitAnimationObserver implementation
     78   virtual void OnImplicitAnimationsCompleted() OVERRIDE {
     79     animations_completed_ = true;
     80   }
     81 
     82   virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE {
     83     return notify_when_animator_destructed_;
     84   }
     85 
     86   bool animations_completed_;
     87   bool notify_when_animator_destructed_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver);
     90 };
     91 
     92 // When notified that an animation has ended, stops all other animations.
     93 class DeletingLayerAnimationObserver : public LayerAnimationObserver {
     94  public:
     95   DeletingLayerAnimationObserver(LayerAnimator* animator)
     96     : animator_(animator) {
     97   }
     98 
     99   virtual void OnLayerAnimationEnded(
    100       LayerAnimationSequence* sequence) OVERRIDE {
    101     animator_->StopAnimating();
    102   }
    103 
    104   virtual void OnLayerAnimationAborted(
    105       LayerAnimationSequence* sequence) OVERRIDE {
    106     animator_->StopAnimating();
    107   }
    108 
    109   virtual void OnLayerAnimationScheduled(
    110       LayerAnimationSequence* sequence) OVERRIDE {
    111   }
    112 
    113  private:
    114   LayerAnimator* animator_;
    115   LayerAnimationSequence* sequence_;
    116 
    117   DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver);
    118 };
    119 
    120 class TestLayerAnimator : public LayerAnimator {
    121  public:
    122   TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)) {}
    123 
    124  protected:
    125   virtual ~TestLayerAnimator() {}
    126 
    127   virtual void ProgressAnimation(LayerAnimationSequence* sequence,
    128                                  base::TimeTicks now) OVERRIDE {
    129     EXPECT_TRUE(HasAnimation(sequence));
    130     LayerAnimator::ProgressAnimation(sequence, now);
    131   }
    132 
    133  private:
    134   DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
    135 };
    136 
    137 // The test layer animation sequence updates a live instances count when it is
    138 // created and destroyed.
    139 class TestLayerAnimationSequence : public LayerAnimationSequence {
    140  public:
    141   TestLayerAnimationSequence(LayerAnimationElement* element,
    142                              int* num_live_instances)
    143       : LayerAnimationSequence(element),
    144         num_live_instances_(num_live_instances) {
    145     (*num_live_instances_)++;
    146   }
    147 
    148   virtual ~TestLayerAnimationSequence() {
    149     (*num_live_instances_)--;
    150   }
    151 
    152  private:
    153   int* num_live_instances_;
    154 
    155   DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence);
    156 };
    157 
    158 } // namespace
    159 
    160 // Checks that setting a property on an implicit animator causes an animation to
    161 // happen.
    162 TEST(LayerAnimatorTest, ImplicitAnimation) {
    163   scoped_refptr<LayerAnimator> animator(
    164       LayerAnimator::CreateImplicitAnimator());
    165   AnimationContainerElement* element = animator.get();
    166   animator->set_disable_timer_for_test(true);
    167   TestLayerAnimationDelegate delegate;
    168   animator->SetDelegate(&delegate);
    169   base::TimeTicks now = base::TimeTicks::Now();
    170   animator->SetBrightness(0.5);
    171   EXPECT_TRUE(animator->is_animating());
    172   element->Step(now + base::TimeDelta::FromSeconds(1));
    173   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
    174 }
    175 
    176 // Checks that if the animator is a default animator, that implicit animations
    177 // are not started.
    178 TEST(LayerAnimatorTest, NoImplicitAnimation) {
    179   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    180   animator->set_disable_timer_for_test(true);
    181   TestLayerAnimationDelegate delegate;
    182   animator->SetDelegate(&delegate);
    183   animator->SetBrightness(0.5);
    184   EXPECT_FALSE(animator->is_animating());
    185   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
    186 }
    187 
    188 // Checks that StopAnimatingProperty stops animation for that property, and also
    189 // skips the stopped animation to the end.
    190 TEST(LayerAnimatorTest, StopAnimatingProperty) {
    191   scoped_refptr<LayerAnimator> animator(
    192       LayerAnimator::CreateImplicitAnimator());
    193   animator->set_disable_timer_for_test(true);
    194   TestLayerAnimationDelegate delegate;
    195   animator->SetDelegate(&delegate);
    196   double target_opacity(0.5);
    197   gfx::Rect target_bounds(0, 0, 50, 50);
    198   animator->SetOpacity(target_opacity);
    199   animator->SetBounds(target_bounds);
    200   animator->StopAnimatingProperty(LayerAnimationElement::OPACITY);
    201   EXPECT_TRUE(animator->is_animating());
    202   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
    203   animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS);
    204   EXPECT_FALSE(animator->is_animating());
    205   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
    206 }
    207 
    208 // Checks that multiple running animation for separate properties can be stopped
    209 // simultaneously and that all animations are advanced to their target values.
    210 TEST(LayerAnimatorTest, StopAnimating) {
    211   scoped_refptr<LayerAnimator> animator(
    212       LayerAnimator::CreateImplicitAnimator());
    213   animator->set_disable_timer_for_test(true);
    214   TestLayerAnimationDelegate delegate;
    215   animator->SetDelegate(&delegate);
    216   double target_opacity(0.5);
    217   gfx::Rect target_bounds(0, 0, 50, 50);
    218   animator->SetOpacity(target_opacity);
    219   animator->SetBounds(target_bounds);
    220   EXPECT_TRUE(animator->is_animating());
    221   animator->StopAnimating();
    222   EXPECT_FALSE(animator->is_animating());
    223   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
    224   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
    225 }
    226 
    227 // Checks that multiple running animation for separate properties can be stopped
    228 // simultaneously and that all animations are advanced to their target values.
    229 TEST(LayerAnimatorTest, AbortAllAnimations) {
    230   scoped_refptr<LayerAnimator> animator(
    231       LayerAnimator::CreateImplicitAnimator());
    232   animator->set_disable_timer_for_test(true);
    233   TestLayerAnimationDelegate delegate;
    234   double initial_opacity(1.0);
    235   gfx::Rect initial_bounds(0, 0, 10, 10);
    236   delegate.SetOpacityFromAnimation(initial_opacity);
    237   delegate.SetBoundsFromAnimation(initial_bounds);
    238   animator->SetDelegate(&delegate);
    239   double target_opacity(0.5);
    240   gfx::Rect target_bounds(0, 0, 50, 50);
    241   animator->SetOpacity(target_opacity);
    242   animator->SetBounds(target_bounds);
    243   EXPECT_TRUE(animator->is_animating());
    244   animator->AbortAllAnimations();
    245   EXPECT_FALSE(animator->is_animating());
    246   EXPECT_FLOAT_EQ(initial_opacity, delegate.GetOpacityForAnimation());
    247   CheckApproximatelyEqual(initial_bounds, delegate.GetBoundsForAnimation());
    248 }
    249 
    250 // Schedule a non-threaded animation that can run immediately. This is the
    251 // trivial case and should result in the animation being started immediately.
    252 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) {
    253   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    254   AnimationContainerElement* element = animator.get();
    255   animator->set_disable_timer_for_test(true);
    256   TestLayerAnimationDelegate delegate;
    257   animator->SetDelegate(&delegate);
    258 
    259   double start_brightness(0.0);
    260   double middle_brightness(0.5);
    261   double target_brightness(1.0);
    262 
    263   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    264 
    265   delegate.SetBrightnessFromAnimation(start_brightness);
    266 
    267   animator->ScheduleAnimation(
    268       new LayerAnimationSequence(
    269           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    270                                                          delta)));
    271 
    272   EXPECT_TRUE(animator->is_animating());
    273   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    274 
    275   base::TimeTicks start_time = animator->last_step_time();
    276 
    277   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    278 
    279   EXPECT_TRUE(animator->is_animating());
    280   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    281 
    282   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    283 
    284   EXPECT_FALSE(animator->is_animating());
    285   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
    286 }
    287 
    288 // Schedule a threaded animation that can run immediately.
    289 TEST(LayerAnimatorTest, ScheduleThreadedAnimationThatCanRunImmediately) {
    290   double epsilon = 0.00001;
    291   LayerAnimatorTestController test_controller(
    292       LayerAnimator::CreateDefaultAnimator());
    293   AnimationContainerElement* element = test_controller.animator();
    294   test_controller.animator()->set_disable_timer_for_test(true);
    295   TestLayerAnimationDelegate delegate;
    296   test_controller.animator()->SetDelegate(&delegate);
    297 
    298   double start_opacity(0.0);
    299   double target_opacity(1.0);
    300 
    301   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    302 
    303   delegate.SetOpacityFromAnimation(start_opacity);
    304 
    305   test_controller.animator()->ScheduleAnimation(
    306       new LayerAnimationSequence(
    307           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
    308 
    309   EXPECT_TRUE(test_controller.animator()->is_animating());
    310   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
    311 
    312   base::TimeTicks start_time = test_controller.animator()->last_step_time();
    313   base::TimeTicks effective_start = start_time + delta;
    314 
    315   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
    316       cc::AnimationEvent::Started,
    317       0,
    318       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    319           animation_group_id(),
    320       cc::Animation::Opacity,
    321       (effective_start - base::TimeTicks()).InSecondsF()));
    322 
    323   element->Step(effective_start + delta/2);
    324 
    325   EXPECT_TRUE(test_controller.animator()->is_animating());
    326   EXPECT_NEAR(
    327       0.5,
    328       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    329           last_progressed_fraction(),
    330       epsilon);
    331 
    332   element->Step(effective_start + delta);
    333 
    334   EXPECT_FALSE(test_controller.animator()->is_animating());
    335   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
    336 }
    337 
    338 // Schedule two non-threaded animations on separate properties. Both animations
    339 // should start immediately and should progress in lock step.
    340 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) {
    341   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    342   AnimationContainerElement* element = animator.get();
    343   animator->set_disable_timer_for_test(true);
    344   TestLayerAnimationDelegate delegate;
    345   animator->SetDelegate(&delegate);
    346 
    347   double start_brightness(0.0);
    348   double middle_brightness(0.5);
    349   double target_brightness(1.0);
    350 
    351   gfx::Rect start_bounds, target_bounds, middle_bounds;
    352   start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
    353   start_bounds.set_x(-90);
    354   target_bounds.set_x(90);
    355 
    356   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    357 
    358   delegate.SetBrightnessFromAnimation(start_brightness);
    359   delegate.SetBoundsFromAnimation(start_bounds);
    360 
    361   animator->ScheduleAnimation(
    362       new LayerAnimationSequence(
    363           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    364                                                          delta)));
    365 
    366   animator->ScheduleAnimation(
    367       new LayerAnimationSequence(
    368           LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
    369 
    370   EXPECT_TRUE(animator->is_animating());
    371   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    372   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    373 
    374   base::TimeTicks start_time = animator->last_step_time();
    375 
    376   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    377 
    378   EXPECT_TRUE(animator->is_animating());
    379   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    380   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
    381 
    382   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    383 
    384   EXPECT_FALSE(animator->is_animating());
    385   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
    386   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
    387 }
    388 
    389 // Schedule a threaded and a non-threaded animation on separate properties. Both
    390 // animations should progress in lock step.
    391 TEST(LayerAnimatorTest, ScheduleThreadedAndNonThreadedAnimations) {
    392   double epsilon = 0.00001;
    393   LayerAnimatorTestController test_controller(
    394       LayerAnimator::CreateDefaultAnimator());
    395   AnimationContainerElement* element = test_controller.animator();
    396   test_controller.animator()->set_disable_timer_for_test(true);
    397   TestLayerAnimationDelegate delegate;
    398   test_controller.animator()->SetDelegate(&delegate);
    399 
    400   double start_opacity(0.0);
    401   double target_opacity(1.0);
    402 
    403   gfx::Rect start_bounds, target_bounds, middle_bounds;
    404   start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
    405   start_bounds.set_x(-90);
    406   target_bounds.set_x(90);
    407 
    408   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    409 
    410   delegate.SetOpacityFromAnimation(start_opacity);
    411   delegate.SetBoundsFromAnimation(start_bounds);
    412 
    413   std::vector<LayerAnimationSequence*> animations;
    414   animations.push_back(
    415       new LayerAnimationSequence(
    416           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
    417 
    418   animations.push_back(
    419       new LayerAnimationSequence(
    420           LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
    421 
    422   test_controller.animator()->ScheduleTogether(animations);
    423 
    424   EXPECT_TRUE(test_controller.animator()->is_animating());
    425   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
    426   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    427 
    428   base::TimeTicks start_time = test_controller.animator()->last_step_time();
    429   base::TimeTicks effective_start = start_time + delta;
    430 
    431   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
    432       cc::AnimationEvent::Started,
    433       0,
    434       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    435           animation_group_id(),
    436       cc::Animation::Opacity,
    437       (effective_start - base::TimeTicks()).InSecondsF()));
    438 
    439   element->Step(effective_start + delta/2);
    440 
    441   EXPECT_TRUE(test_controller.animator()->is_animating());
    442   EXPECT_NEAR(
    443       0.5,
    444       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    445           last_progressed_fraction(),
    446       epsilon);
    447   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
    448 
    449   element->Step(effective_start + delta);
    450 
    451   EXPECT_FALSE(test_controller.animator()->is_animating());
    452   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
    453   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
    454 }
    455 
    456 // Schedule two animations on the same property. In this case, the two
    457 // animations should run one after another.
    458 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) {
    459   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    460   AnimationContainerElement* element = animator.get();
    461   animator->set_disable_timer_for_test(true);
    462   TestLayerAnimationDelegate delegate;
    463   animator->SetDelegate(&delegate);
    464 
    465   double start_brightness(0.0);
    466   double middle_brightness(0.5);
    467   double target_brightness(1.0);
    468 
    469   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    470 
    471   delegate.SetBrightnessFromAnimation(start_brightness);
    472 
    473   animator->ScheduleAnimation(
    474       new LayerAnimationSequence(
    475           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    476                                                          delta)));
    477 
    478   animator->ScheduleAnimation(
    479       new LayerAnimationSequence(
    480           LayerAnimationElement::CreateBrightnessElement(start_brightness,
    481                                                          delta)));
    482 
    483   EXPECT_TRUE(animator->is_animating());
    484   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    485 
    486   base::TimeTicks start_time = animator->last_step_time();
    487 
    488   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    489 
    490   EXPECT_TRUE(animator->is_animating());
    491   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    492 
    493   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    494 
    495   EXPECT_TRUE(animator->is_animating());
    496   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
    497 
    498   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
    499 
    500   EXPECT_TRUE(animator->is_animating());
    501   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    502 
    503   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
    504 
    505   EXPECT_FALSE(animator->is_animating());
    506   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    507 }
    508 
    509 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
    510 // is, ensure that all animations targetting a particular property are run in
    511 // order.
    512 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) {
    513   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    514   AnimationContainerElement* element = animator.get();
    515   animator->set_disable_timer_for_test(true);
    516   TestLayerAnimationDelegate delegate;
    517   animator->SetDelegate(&delegate);
    518 
    519   double start_grayscale(0.0);
    520   double middle_grayscale(0.5);
    521   double target_grayscale(1.0);
    522 
    523   gfx::Rect start_bounds, target_bounds, middle_bounds;
    524   start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
    525   start_bounds.set_x(-90);
    526   target_bounds.set_x(90);
    527 
    528   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    529 
    530   delegate.SetGrayscaleFromAnimation(start_grayscale);
    531   delegate.SetBoundsFromAnimation(start_bounds);
    532 
    533   animator->ScheduleAnimation(
    534       new LayerAnimationSequence(
    535           LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
    536                                                         delta)));
    537 
    538   scoped_ptr<LayerAnimationSequence> bounds_and_grayscale(
    539       new LayerAnimationSequence(
    540           LayerAnimationElement::CreateGrayscaleElement(start_grayscale,
    541                                                         delta)));
    542 
    543   bounds_and_grayscale->AddElement(
    544       LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
    545 
    546   animator->ScheduleAnimation(bounds_and_grayscale.release());
    547 
    548   animator->ScheduleAnimation(
    549       new LayerAnimationSequence(
    550           LayerAnimationElement::CreateBoundsElement(start_bounds, delta)));
    551 
    552   EXPECT_TRUE(animator->is_animating());
    553   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
    554   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    555 
    556   base::TimeTicks start_time = animator->last_step_time();
    557 
    558   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    559 
    560   EXPECT_TRUE(animator->is_animating());
    561   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
    562   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    563 
    564   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    565 
    566   EXPECT_TRUE(animator->is_animating());
    567   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
    568   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    569 
    570   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
    571 
    572   EXPECT_TRUE(animator->is_animating());
    573   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
    574   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    575 
    576   element->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
    577 
    578   EXPECT_TRUE(animator->is_animating());
    579   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
    580   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
    581 
    582   element->Step(start_time + base::TimeDelta::FromMilliseconds(4000));
    583 
    584   EXPECT_FALSE(animator->is_animating());
    585   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
    586   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    587 }
    588 
    589 // Schedule {g} and then schedule {g} and {b} together. In this case, since
    590 // ScheduleTogether is being used, the bounds animation should not start until
    591 // the second grayscale animation starts.
    592 TEST(LayerAnimatorTest, ScheduleTogether) {
    593   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    594   AnimationContainerElement* element = animator.get();
    595   animator->set_disable_timer_for_test(true);
    596   TestLayerAnimationDelegate delegate;
    597   animator->SetDelegate(&delegate);
    598 
    599   double start_grayscale(0.0);
    600   double target_grayscale(1.0);
    601 
    602   gfx::Rect start_bounds, target_bounds, middle_bounds;
    603   start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50);
    604   start_bounds.set_x(-90);
    605   target_bounds.set_x(90);
    606 
    607   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    608 
    609   delegate.SetGrayscaleFromAnimation(start_grayscale);
    610   delegate.SetBoundsFromAnimation(start_bounds);
    611 
    612   animator->ScheduleAnimation(
    613       new LayerAnimationSequence(
    614           LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
    615                                                         delta)));
    616 
    617   std::vector<LayerAnimationSequence*> sequences;
    618   sequences.push_back(new LayerAnimationSequence(
    619       LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta)));
    620   sequences.push_back(new LayerAnimationSequence(
    621       LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
    622 
    623   animator->ScheduleTogether(sequences);
    624 
    625   EXPECT_TRUE(animator->is_animating());
    626   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
    627   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    628 
    629   base::TimeTicks start_time = animator->last_step_time();
    630 
    631   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    632 
    633   EXPECT_TRUE(animator->is_animating());
    634   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
    635   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
    636 
    637   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
    638 
    639   EXPECT_FALSE(animator->is_animating());
    640   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
    641   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
    642 }
    643 
    644 // Start non-threaded animation (that can run immediately). This is the trivial
    645 // case (see the trival case for ScheduleAnimation).
    646 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) {
    647   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    648   AnimationContainerElement* element = animator.get();
    649   animator->set_disable_timer_for_test(true);
    650   TestLayerAnimationDelegate delegate;
    651   animator->SetDelegate(&delegate);
    652 
    653   double start_brightness(0.0);
    654   double middle_brightness(0.5);
    655   double target_brightness(1.0);
    656 
    657   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    658 
    659   delegate.SetBrightnessFromAnimation(start_brightness);
    660 
    661   animator->StartAnimation(
    662       new LayerAnimationSequence(
    663           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    664                                                          delta)));
    665 
    666   EXPECT_TRUE(animator->is_animating());
    667   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    668 
    669   base::TimeTicks start_time = animator->last_step_time();
    670 
    671   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    672 
    673   EXPECT_TRUE(animator->is_animating());
    674   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    675 
    676   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    677 
    678   EXPECT_FALSE(animator->is_animating());
    679   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
    680 }
    681 
    682 // Start threaded animation (that can run immediately).
    683 TEST(LayerAnimatorTest, StartThreadedAnimationThatCanRunImmediately) {
    684   double epsilon = 0.00001;
    685   LayerAnimatorTestController test_controller(
    686       LayerAnimator::CreateDefaultAnimator());
    687   AnimationContainerElement* element = test_controller.animator();
    688   test_controller.animator()->set_disable_timer_for_test(true);
    689   TestLayerAnimationDelegate delegate;
    690   test_controller.animator()->SetDelegate(&delegate);
    691 
    692   double start_opacity(0.0);
    693   double target_opacity(1.0);
    694 
    695   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    696 
    697   delegate.SetOpacityFromAnimation(start_opacity);
    698 
    699   test_controller.animator()->StartAnimation(
    700       new LayerAnimationSequence(
    701           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
    702 
    703   EXPECT_TRUE(test_controller.animator()->is_animating());
    704   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
    705 
    706   base::TimeTicks start_time = test_controller.animator()->last_step_time();
    707   base::TimeTicks effective_start = start_time + delta;
    708 
    709   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
    710       cc::AnimationEvent::Started,
    711       0,
    712       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    713           animation_group_id(),
    714       cc::Animation::Opacity,
    715       (effective_start - base::TimeTicks()).InSecondsF()));
    716 
    717   element->Step(effective_start + delta/2);
    718 
    719   EXPECT_TRUE(test_controller.animator()->is_animating());
    720   EXPECT_NEAR(
    721       0.5,
    722       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    723           last_progressed_fraction(),
    724       epsilon);
    725 
    726   element->Step(effective_start + delta);
    727   EXPECT_FALSE(test_controller.animator()->is_animating());
    728   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
    729 }
    730 
    731 // Preempt by immediately setting new target.
    732 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) {
    733   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    734   animator->set_disable_timer_for_test(true);
    735   TestLayerAnimationDelegate delegate;
    736   animator->SetDelegate(&delegate);
    737 
    738   double start_opacity(0.0);
    739   double target_opacity(1.0);
    740 
    741   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    742 
    743   delegate.SetOpacityFromAnimation(start_opacity);
    744 
    745   animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
    746 
    747   animator->StartAnimation(
    748       new LayerAnimationSequence(
    749           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
    750 
    751   animator->StartAnimation(
    752       new LayerAnimationSequence(
    753           LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
    754 
    755   EXPECT_FALSE(animator->is_animating());
    756   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
    757 }
    758 
    759 // Preempt by animating to new target, with a non-threaded animation.
    760 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) {
    761   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    762   AnimationContainerElement* element = animator.get();
    763   animator->set_disable_timer_for_test(true);
    764   TestLayerAnimationDelegate delegate;
    765   animator->SetDelegate(&delegate);
    766 
    767   double start_brightness(0.0);
    768   double middle_brightness(0.5);
    769   double target_brightness(1.0);
    770 
    771   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    772 
    773   delegate.SetBrightnessFromAnimation(start_brightness);
    774 
    775   animator->set_preemption_strategy(
    776       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
    777 
    778   animator->StartAnimation(
    779       new LayerAnimationSequence(
    780           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    781                                                          delta)));
    782 
    783   base::TimeTicks start_time = animator->last_step_time();
    784 
    785   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    786 
    787   animator->StartAnimation(
    788       new LayerAnimationSequence(
    789           LayerAnimationElement::CreateBrightnessElement(start_brightness,
    790                                                          delta)));
    791 
    792   EXPECT_TRUE(animator->is_animating());
    793   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    794 
    795   animator->StartAnimation(
    796       new LayerAnimationSequence(
    797           LayerAnimationElement::CreateBrightnessElement(start_brightness,
    798                                                          delta)));
    799 
    800   EXPECT_TRUE(animator->is_animating());
    801 
    802   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    803 
    804   EXPECT_TRUE(animator->is_animating());
    805   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
    806                   0.5 * (start_brightness + middle_brightness));
    807 
    808   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
    809 
    810   EXPECT_FALSE(animator->is_animating());
    811   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    812 }
    813 
    814 // Preempt by animating to new target, with a threaded animation.
    815 TEST(LayerAnimatorTest, PreemptThreadedByImmediatelyAnimatingToNewTarget) {
    816   double epsilon = 0.00001;
    817   LayerAnimatorTestController test_controller(
    818       LayerAnimator::CreateDefaultAnimator());
    819   AnimationContainerElement* element = test_controller.animator();
    820   test_controller.animator()->set_disable_timer_for_test(true);
    821   TestLayerAnimationDelegate delegate;
    822   test_controller.animator()->SetDelegate(&delegate);
    823 
    824   double start_opacity(0.0);
    825   double middle_opacity(0.5);
    826   double target_opacity(1.0);
    827 
    828   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    829 
    830   delegate.SetOpacityFromAnimation(start_opacity);
    831 
    832   test_controller.animator()->set_preemption_strategy(
    833       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
    834 
    835   test_controller.animator()->StartAnimation(
    836       new LayerAnimationSequence(
    837           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
    838 
    839   base::TimeTicks start_time = test_controller.animator()->last_step_time();
    840   base::TimeTicks effective_start = start_time + delta;
    841 
    842   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
    843       cc::AnimationEvent::Started,
    844       0,
    845       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    846           animation_group_id(),
    847       cc::Animation::Opacity,
    848       (effective_start - base::TimeTicks()).InSecondsF()));
    849 
    850   element->Step(effective_start + delta/2);
    851 
    852   test_controller.animator()->StartAnimation(
    853       new LayerAnimationSequence(
    854           LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
    855 
    856   EXPECT_TRUE(test_controller.animator()->is_animating());
    857   EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
    858 
    859   test_controller.animator()->StartAnimation(
    860       new LayerAnimationSequence(
    861           LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
    862 
    863   EXPECT_TRUE(test_controller.animator()->is_animating());
    864 
    865   base::TimeTicks second_effective_start = effective_start + delta;
    866 
    867   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
    868       cc::AnimationEvent::Started,
    869       0,
    870       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    871           animation_group_id(),
    872       cc::Animation::Opacity,
    873       (second_effective_start - base::TimeTicks()).InSecondsF()));
    874 
    875   element->Step(second_effective_start + delta/2);
    876 
    877   EXPECT_TRUE(test_controller.animator()->is_animating());
    878   EXPECT_NEAR(
    879       0.5,
    880       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
    881           last_progressed_fraction(),
    882       epsilon);
    883 
    884   element->Step(second_effective_start + delta);
    885 
    886   EXPECT_FALSE(test_controller.animator()->is_animating());
    887   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
    888 }
    889 
    890 // Preempt by enqueuing the new animation.
    891 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) {
    892   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    893   AnimationContainerElement* element = animator.get();
    894   animator->set_disable_timer_for_test(true);
    895   TestLayerAnimationDelegate delegate;
    896   animator->SetDelegate(&delegate);
    897 
    898   double start_brightness(0.0);
    899   double middle_brightness(0.5);
    900   double target_brightness(1.0);
    901 
    902   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    903 
    904   delegate.SetBrightnessFromAnimation(start_brightness);
    905 
    906   animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
    907 
    908   animator->StartAnimation(
    909       new LayerAnimationSequence(
    910           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    911                                                           delta)));
    912 
    913   base::TimeTicks start_time = animator->last_step_time();
    914 
    915   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    916 
    917   animator->StartAnimation(
    918       new LayerAnimationSequence(
    919           LayerAnimationElement::CreateBrightnessElement(start_brightness,
    920                                                          delta)));
    921 
    922   EXPECT_TRUE(animator->is_animating());
    923   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    924 
    925   EXPECT_TRUE(animator->is_animating());
    926 
    927   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    928 
    929   EXPECT_TRUE(animator->is_animating());
    930   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
    931 
    932   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
    933 
    934   EXPECT_TRUE(animator->is_animating());
    935   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    936 
    937   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
    938 
    939   EXPECT_FALSE(animator->is_animating());
    940   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
    941 }
    942 
    943 // Start an animation when there are sequences waiting in the queue. In this
    944 // case, all pending and running animations should be finished, and the new
    945 // animation started.
    946 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) {
    947   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
    948   AnimationContainerElement* element = animator.get();
    949   animator->set_disable_timer_for_test(true);
    950   TestLayerAnimationDelegate delegate;
    951   animator->SetDelegate(&delegate);
    952 
    953   double start_brightness(0.0);
    954   double middle_brightness(0.5);
    955   double target_brightness(1.0);
    956 
    957   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
    958 
    959   delegate.SetBrightnessFromAnimation(start_brightness);
    960 
    961   animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
    962 
    963   animator->StartAnimation(
    964       new LayerAnimationSequence(
    965           LayerAnimationElement::CreateBrightnessElement(target_brightness,
    966                                                          delta)));
    967 
    968   base::TimeTicks start_time = animator->last_step_time();
    969 
    970   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
    971 
    972   animator->StartAnimation(
    973       new LayerAnimationSequence(
    974           LayerAnimationElement::CreateBrightnessElement(middle_brightness,
    975                                                          delta)));
    976 
    977   // Queue should now have two animations. Starting a third should replace the
    978   // second.
    979   animator->StartAnimation(
    980       new LayerAnimationSequence(
    981           LayerAnimationElement::CreateBrightnessElement(start_brightness,
    982                                                          delta)));
    983 
    984   EXPECT_TRUE(animator->is_animating());
    985   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    986 
    987   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
    988 
    989   EXPECT_TRUE(animator->is_animating());
    990   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
    991 
    992   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
    993 
    994   EXPECT_TRUE(animator->is_animating());
    995   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
    996 
    997   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
    998 
    999   EXPECT_FALSE(animator->is_animating());
   1000   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1001 }
   1002 
   1003 TEST(LayerAnimatorTest, StartTogetherSetsLastStepTime) {
   1004   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1005   animator->set_disable_timer_for_test(true);
   1006   TestLayerAnimationDelegate delegate;
   1007   animator->SetDelegate(&delegate);
   1008 
   1009   double start_grayscale(0.0);
   1010   double target_grayscale(1.0);
   1011   double start_brightness(0.1);
   1012   double target_brightness(0.9);
   1013 
   1014   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1015 
   1016   delegate.SetGrayscaleFromAnimation(start_grayscale);
   1017   delegate.SetBrightnessFromAnimation(start_brightness);
   1018 
   1019   animator->set_preemption_strategy(
   1020       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
   1021 
   1022   animator->set_last_step_time(base::TimeTicks());
   1023 
   1024   animator->StartTogether(
   1025       CreateMultiSequence(
   1026           LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
   1027                                                         delta),
   1028           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1029                                                          delta)
   1030       ));
   1031 
   1032   // If last step time was not set correctly, the resulting delta should be
   1033   // miniscule (fractions of a millisecond). If set correctly, then the delta
   1034   // should be enormous. Arbitrarily choosing 1 minute as the threshold,
   1035   // though a much smaller value would probably have sufficed.
   1036   delta = base::TimeTicks::Now() - animator->last_step_time();
   1037   EXPECT_GT(60.0, delta.InSecondsF());
   1038 }
   1039 
   1040 //-------------------------------------------------------
   1041 // Preempt by immediately setting new target.
   1042 TEST(LayerAnimatorTest, MultiPreemptBySettingNewTarget) {
   1043   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1044   animator->set_disable_timer_for_test(true);
   1045   TestLayerAnimationDelegate delegate;
   1046   animator->SetDelegate(&delegate);
   1047 
   1048   double start_opacity(0.0);
   1049   double target_opacity(1.0);
   1050   double start_brightness(0.1);
   1051   double target_brightness(0.9);
   1052 
   1053   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1054 
   1055   delegate.SetOpacityFromAnimation(start_opacity);
   1056   delegate.SetBrightnessFromAnimation(start_brightness);
   1057 
   1058   animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
   1059 
   1060   animator->StartTogether(
   1061       CreateMultiSequence(
   1062           LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
   1063           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1064                                                          delta)
   1065       ));
   1066 
   1067   animator->StartTogether(
   1068       CreateMultiSequence(
   1069           LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
   1070           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1071                                                          delta)
   1072       ));
   1073 
   1074   EXPECT_FALSE(animator->is_animating());
   1075   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
   1076   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1077 }
   1078 
   1079 // Preempt by animating to new target.
   1080 TEST(LayerAnimatorTest, MultiPreemptByImmediatelyAnimatingToNewTarget) {
   1081   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1082   AnimationContainerElement* element = animator.get();
   1083   animator->set_disable_timer_for_test(true);
   1084   TestLayerAnimationDelegate delegate;
   1085   animator->SetDelegate(&delegate);
   1086 
   1087   double start_grayscale(0.0);
   1088   double middle_grayscale(0.5);
   1089   double target_grayscale(1.0);
   1090 
   1091   double start_brightness(0.1);
   1092   double middle_brightness(0.2);
   1093   double target_brightness(0.3);
   1094 
   1095   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1096 
   1097   delegate.SetGrayscaleFromAnimation(start_grayscale);
   1098   delegate.SetBrightnessFromAnimation(start_brightness);
   1099 
   1100   animator->set_preemption_strategy(
   1101       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
   1102 
   1103   animator->StartTogether(
   1104       CreateMultiSequence(
   1105           LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
   1106                                                         delta),
   1107           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1108                                                          delta)
   1109       ));
   1110 
   1111   base::TimeTicks start_time = animator->last_step_time();
   1112 
   1113   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
   1114 
   1115   animator->StartTogether(
   1116       CreateMultiSequence(
   1117           LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
   1118           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1119                                                          delta)));
   1120 
   1121   EXPECT_TRUE(animator->is_animating());
   1122   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
   1123   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
   1124 
   1125   animator->StartTogether(
   1126       CreateMultiSequence(
   1127           LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
   1128           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1129                                                          delta)));
   1130 
   1131   EXPECT_TRUE(animator->is_animating());
   1132 
   1133   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1134 
   1135   EXPECT_TRUE(animator->is_animating());
   1136   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(),
   1137                   0.5 * (start_grayscale + middle_grayscale));
   1138   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
   1139                   0.5 * (start_brightness + middle_brightness));
   1140 
   1141   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
   1142 
   1143   EXPECT_FALSE(animator->is_animating());
   1144   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
   1145   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1146 }
   1147 
   1148 // Preempt a threaded animation by animating to new target.
   1149 TEST(LayerAnimatorTest, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget) {
   1150   double epsilon = 0.00001;
   1151   LayerAnimatorTestController test_controller(
   1152       LayerAnimator::CreateDefaultAnimator());
   1153   AnimationContainerElement* element = test_controller.animator();
   1154   test_controller.animator()->set_disable_timer_for_test(true);
   1155   TestLayerAnimationDelegate delegate;
   1156   test_controller.animator()->SetDelegate(&delegate);
   1157 
   1158   double start_opacity(0.0);
   1159   double middle_opacity(0.5);
   1160   double target_opacity(1.0);
   1161 
   1162   double start_brightness(0.1);
   1163   double middle_brightness(0.2);
   1164   double target_brightness(0.3);
   1165 
   1166   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1167 
   1168   delegate.SetOpacityFromAnimation(start_opacity);
   1169   delegate.SetBrightnessFromAnimation(start_brightness);
   1170 
   1171   test_controller.animator()->set_preemption_strategy(
   1172       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
   1173 
   1174   test_controller.animator()->StartTogether(
   1175       CreateMultiSequence(
   1176           LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
   1177           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1178                                                          delta)
   1179       ));
   1180 
   1181   base::TimeTicks start_time = test_controller.animator()->last_step_time();
   1182   base::TimeTicks effective_start = start_time + delta;
   1183 
   1184   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1185       cc::AnimationEvent::Started,
   1186       0,
   1187       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1188           animation_group_id(),
   1189       cc::Animation::Opacity,
   1190       (effective_start - base::TimeTicks()).InSecondsF()));
   1191 
   1192   element->Step(effective_start + delta/2);
   1193 
   1194   test_controller.animator()->StartTogether(
   1195       CreateMultiSequence(
   1196           LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
   1197           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1198                                                          delta)));
   1199 
   1200   EXPECT_TRUE(test_controller.animator()->is_animating());
   1201   EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
   1202   EXPECT_NEAR(delegate.GetBrightnessForAnimation(), middle_brightness, epsilon);
   1203 
   1204   test_controller.animator()->StartTogether(
   1205       CreateMultiSequence(
   1206           LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
   1207           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1208                                                          delta)));
   1209 
   1210   EXPECT_TRUE(test_controller.animator()->is_animating());
   1211 
   1212   base::TimeTicks second_effective_start = effective_start + delta;
   1213 
   1214   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1215       cc::AnimationEvent::Started,
   1216       0,
   1217       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1218           animation_group_id(),
   1219       cc::Animation::Opacity,
   1220       (second_effective_start - base::TimeTicks()).InSecondsF()));
   1221 
   1222   element->Step(second_effective_start + delta/2);
   1223 
   1224   EXPECT_TRUE(test_controller.animator()->is_animating());
   1225   EXPECT_NEAR(
   1226       0.5,
   1227       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1228           last_progressed_fraction(),
   1229       epsilon);
   1230   EXPECT_NEAR(delegate.GetBrightnessForAnimation(),
   1231               0.5 * (start_brightness + middle_brightness),
   1232               epsilon);
   1233 
   1234   element->Step(second_effective_start + delta);
   1235 
   1236   EXPECT_FALSE(test_controller.animator()->is_animating());
   1237   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
   1238   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1239 }
   1240 
   1241 // Preempt by enqueuing the new animation.
   1242 TEST(LayerAnimatorTest, MultiPreemptEnqueueNewAnimation) {
   1243   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1244   AnimationContainerElement* element = animator.get();
   1245   animator->set_disable_timer_for_test(true);
   1246   TestLayerAnimationDelegate delegate;
   1247   animator->SetDelegate(&delegate);
   1248 
   1249   double start_grayscale(0.0);
   1250   double middle_grayscale(0.5);
   1251   double target_grayscale(1.0);
   1252 
   1253   double start_brightness(0.1);
   1254   double middle_brightness(0.2);
   1255   double target_brightness(0.3);
   1256 
   1257   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1258 
   1259   delegate.SetGrayscaleFromAnimation(start_grayscale);
   1260   delegate.SetBrightnessFromAnimation(start_brightness);
   1261 
   1262   animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
   1263 
   1264   animator->StartTogether(
   1265       CreateMultiSequence(
   1266           LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
   1267                                                         delta),
   1268           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1269                                                          delta)));
   1270 
   1271   base::TimeTicks start_time = animator->last_step_time();
   1272 
   1273   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
   1274 
   1275   animator->StartTogether(
   1276       CreateMultiSequence(
   1277           LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
   1278           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1279                                                          delta)));
   1280 
   1281   EXPECT_TRUE(animator->is_animating());
   1282   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
   1283   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
   1284 
   1285   EXPECT_TRUE(animator->is_animating());
   1286 
   1287   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1288 
   1289   EXPECT_TRUE(animator->is_animating());
   1290   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
   1291   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
   1292 
   1293   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
   1294 
   1295   EXPECT_TRUE(animator->is_animating());
   1296   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
   1297   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
   1298 
   1299   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
   1300 
   1301   EXPECT_FALSE(animator->is_animating());
   1302   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
   1303   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1304 }
   1305 
   1306 // Start an animation when there are sequences waiting in the queue. In this
   1307 // case, all pending and running animations should be finished, and the new
   1308 // animation started.
   1309 TEST(LayerAnimatorTest, MultiPreemptByReplacingQueuedAnimations) {
   1310   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1311   AnimationContainerElement* element = animator.get();
   1312   animator->set_disable_timer_for_test(true);
   1313   TestLayerAnimationDelegate delegate;
   1314   animator->SetDelegate(&delegate);
   1315 
   1316   double start_grayscale(0.0);
   1317   double middle_grayscale(0.5);
   1318   double target_grayscale(1.0);
   1319 
   1320   double start_brightness(0.1);
   1321   double middle_brightness(0.2);
   1322   double target_brightness(0.3);
   1323 
   1324   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1325 
   1326   delegate.SetGrayscaleFromAnimation(start_grayscale);
   1327   delegate.SetBrightnessFromAnimation(start_brightness);
   1328 
   1329   animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
   1330 
   1331   animator->StartTogether(
   1332       CreateMultiSequence(
   1333           LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
   1334                                                         delta),
   1335           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1336                                                          delta)));
   1337 
   1338   base::TimeTicks start_time = animator->last_step_time();
   1339 
   1340   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
   1341 
   1342   animator->StartTogether(
   1343       CreateMultiSequence(
   1344           LayerAnimationElement::CreateGrayscaleElement(middle_grayscale,
   1345                                                         delta),
   1346           LayerAnimationElement::CreateBrightnessElement(middle_brightness,
   1347                                                          delta)));
   1348 
   1349   // Queue should now have two animations. Starting a third should replace the
   1350   // second.
   1351   animator->StartTogether(
   1352       CreateMultiSequence(
   1353           LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
   1354           LayerAnimationElement::CreateBrightnessElement(start_brightness,
   1355                                                          delta)));
   1356 
   1357   EXPECT_TRUE(animator->is_animating());
   1358   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
   1359   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
   1360 
   1361   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1362 
   1363   EXPECT_TRUE(animator->is_animating());
   1364   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
   1365   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
   1366 
   1367   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
   1368 
   1369   EXPECT_TRUE(animator->is_animating());
   1370   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
   1371   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
   1372 
   1373   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
   1374 
   1375   EXPECT_FALSE(animator->is_animating());
   1376   EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
   1377   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1378 }
   1379 //-------------------------------------------------------
   1380 // Test that non-threaded cyclic sequences continue to animate.
   1381 TEST(LayerAnimatorTest, CyclicSequences) {
   1382   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1383   AnimationContainerElement* element = animator.get();
   1384   animator->set_disable_timer_for_test(true);
   1385   TestLayerAnimationDelegate delegate;
   1386   animator->SetDelegate(&delegate);
   1387 
   1388   double start_brightness(0.0);
   1389   double target_brightness(1.0);
   1390 
   1391   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1392 
   1393   delegate.SetBrightnessFromAnimation(start_brightness);
   1394 
   1395   scoped_ptr<LayerAnimationSequence> sequence(
   1396       new LayerAnimationSequence(
   1397           LayerAnimationElement::CreateBrightnessElement(target_brightness,
   1398                                                          delta)));
   1399 
   1400   sequence->AddElement(
   1401       LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
   1402 
   1403   sequence->set_is_cyclic(true);
   1404 
   1405   animator->StartAnimation(sequence.release());
   1406 
   1407   base::TimeTicks start_time = animator->last_step_time();
   1408 
   1409   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1410 
   1411   EXPECT_TRUE(animator->is_animating());
   1412   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
   1413 
   1414   element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
   1415 
   1416   EXPECT_TRUE(animator->is_animating());
   1417   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1418 
   1419   element->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
   1420 
   1421   EXPECT_TRUE(animator->is_animating());
   1422   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
   1423 
   1424   // Skip ahead by a lot.
   1425   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000));
   1426 
   1427   EXPECT_TRUE(animator->is_animating());
   1428   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
   1429 
   1430   // Skip ahead by a lot.
   1431   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000));
   1432 
   1433   EXPECT_TRUE(animator->is_animating());
   1434   EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
   1435 
   1436   animator->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS);
   1437 
   1438   EXPECT_FALSE(animator->is_animating());
   1439 }
   1440 
   1441 // Test that threaded cyclic sequences continue to animate.
   1442 TEST(LayerAnimatorTest, ThreadedCyclicSequences) {
   1443   LayerAnimatorTestController test_controller(
   1444       LayerAnimator::CreateDefaultAnimator());
   1445   AnimationContainerElement* element = test_controller.animator();
   1446   test_controller.animator()->set_disable_timer_for_test(true);
   1447   TestLayerAnimationDelegate delegate;
   1448   test_controller.animator()->SetDelegate(&delegate);
   1449 
   1450   double start_opacity(0.0);
   1451   double target_opacity(1.0);
   1452 
   1453   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1454 
   1455   delegate.SetOpacityFromAnimation(start_opacity);
   1456 
   1457   scoped_ptr<LayerAnimationSequence> sequence(
   1458       new LayerAnimationSequence(
   1459           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
   1460 
   1461   sequence->AddElement(
   1462       LayerAnimationElement::CreateOpacityElement(start_opacity, delta));
   1463 
   1464   sequence->set_is_cyclic(true);
   1465 
   1466   test_controller.animator()->StartAnimation(sequence.release());
   1467 
   1468   base::TimeTicks start_time = test_controller.animator()->last_step_time();
   1469   base::TimeTicks effective_start = start_time + delta;
   1470 
   1471   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1472       cc::AnimationEvent::Started,
   1473       0,
   1474       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1475           animation_group_id(),
   1476       cc::Animation::Opacity,
   1477       (effective_start - base::TimeTicks()).InSecondsF()));
   1478 
   1479   element->Step(effective_start + delta);
   1480   EXPECT_TRUE(test_controller.animator()->is_animating());
   1481   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
   1482 
   1483   base::TimeTicks second_effective_start = effective_start + 2 * delta;
   1484   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1485       cc::AnimationEvent::Started,
   1486       0,
   1487       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1488           animation_group_id(),
   1489       cc::Animation::Opacity,
   1490       (second_effective_start - base::TimeTicks()).InSecondsF()));
   1491 
   1492   element->Step(second_effective_start + delta);
   1493 
   1494   EXPECT_TRUE(test_controller.animator()->is_animating());
   1495   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
   1496 
   1497   base::TimeTicks third_effective_start = second_effective_start + 2 * delta;
   1498   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1499       cc::AnimationEvent::Started,
   1500       0,
   1501       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1502           animation_group_id(),
   1503       cc::Animation::Opacity,
   1504       (third_effective_start - base::TimeTicks()).InSecondsF()));
   1505 
   1506   element->Step(third_effective_start + delta);
   1507   EXPECT_TRUE(test_controller.animator()->is_animating());
   1508   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
   1509 
   1510   base::TimeTicks fourth_effective_start = third_effective_start + 2 * delta;
   1511   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1512       cc::AnimationEvent::Started,
   1513       0,
   1514       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1515           animation_group_id(),
   1516       cc::Animation::Opacity,
   1517       (fourth_effective_start - base::TimeTicks()).InSecondsF()));
   1518 
   1519   // Skip ahead by a lot.
   1520   element->Step(fourth_effective_start + 1000 * delta);
   1521 
   1522   EXPECT_TRUE(test_controller.animator()->is_animating());
   1523   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
   1524 
   1525   base::TimeTicks fifth_effective_start = fourth_effective_start + 1001 * delta;
   1526   test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
   1527       cc::AnimationEvent::Started,
   1528       0,
   1529       test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
   1530           animation_group_id(),
   1531       cc::Animation::Opacity,
   1532       (fifth_effective_start - base::TimeTicks()).InSecondsF()));
   1533 
   1534   // Skip ahead by a lot.
   1535   element->Step(fifth_effective_start + 999 * delta);
   1536 
   1537   EXPECT_TRUE(test_controller.animator()->is_animating());
   1538   EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
   1539 
   1540   test_controller.animator()->StopAnimatingProperty(
   1541       LayerAnimationElement::OPACITY);
   1542 
   1543   EXPECT_FALSE(test_controller.animator()->is_animating());
   1544 }
   1545 
   1546 TEST(LayerAnimatorTest, AddObserverExplicit) {
   1547   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1548   AnimationContainerElement* element = animator.get();
   1549   animator->set_disable_timer_for_test(true);
   1550   TestLayerAnimationObserver observer;
   1551   TestLayerAnimationDelegate delegate;
   1552   animator->SetDelegate(&delegate);
   1553   animator->AddObserver(&observer);
   1554   observer.set_requires_notification_when_animator_destroyed(true);
   1555 
   1556   EXPECT_TRUE(!observer.last_ended_sequence());
   1557 
   1558   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1559 
   1560   delegate.SetBrightnessFromAnimation(0.0f);
   1561 
   1562   LayerAnimationSequence* sequence = new LayerAnimationSequence(
   1563       LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
   1564 
   1565   animator->StartAnimation(sequence);
   1566 
   1567   EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
   1568 
   1569   base::TimeTicks start_time = animator->last_step_time();
   1570 
   1571   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1572 
   1573   EXPECT_EQ(observer.last_ended_sequence(), sequence);
   1574 
   1575   // |sequence| has been destroyed. Recreate it to test abort.
   1576   sequence = new LayerAnimationSequence(
   1577       LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
   1578 
   1579   animator->StartAnimation(sequence);
   1580 
   1581   animator = NULL;
   1582 
   1583   EXPECT_EQ(observer.last_aborted_sequence(), sequence);
   1584 }
   1585 
   1586 // Tests that an observer added to a scoped settings object is still notified
   1587 // when the object goes out of scope.
   1588 TEST(LayerAnimatorTest, ImplicitAnimationObservers) {
   1589   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1590   AnimationContainerElement* element = animator.get();
   1591   animator->set_disable_timer_for_test(true);
   1592   TestImplicitAnimationObserver observer(false);
   1593   TestLayerAnimationDelegate delegate;
   1594   animator->SetDelegate(&delegate);
   1595 
   1596   EXPECT_FALSE(observer.animations_completed());
   1597   animator->SetBrightness(1.0f);
   1598 
   1599   {
   1600     ScopedLayerAnimationSettings settings(animator.get());
   1601     settings.AddObserver(&observer);
   1602     animator->SetBrightness(0.0f);
   1603   }
   1604 
   1605   EXPECT_FALSE(observer.animations_completed());
   1606   base::TimeTicks start_time = animator->last_step_time();
   1607   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1608   EXPECT_TRUE(observer.animations_completed());
   1609   EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
   1610       LayerAnimationElement::BRIGHTNESS));
   1611   EXPECT_FLOAT_EQ(0.0f, delegate.GetBrightnessForAnimation());
   1612 }
   1613 
   1614 // Tests that an observer added to a scoped settings object is still notified
   1615 // when the object goes out of scope due to the animation being interrupted.
   1616 TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) {
   1617   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1618   animator->set_disable_timer_for_test(true);
   1619   TestImplicitAnimationObserver observer(false);
   1620   TestLayerAnimationDelegate delegate;
   1621   animator->SetDelegate(&delegate);
   1622 
   1623   EXPECT_FALSE(observer.animations_completed());
   1624   animator->SetBrightness(1.0f);
   1625 
   1626   {
   1627     ScopedLayerAnimationSettings settings(animator.get());
   1628     settings.AddObserver(&observer);
   1629     animator->SetBrightness(0.0f);
   1630   }
   1631 
   1632   EXPECT_FALSE(observer.animations_completed());
   1633   // This should interrupt the implicit animation causing the observer to be
   1634   // notified immediately.
   1635   animator->SetBrightness(1.0f);
   1636   EXPECT_TRUE(observer.animations_completed());
   1637   EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
   1638       LayerAnimationElement::BRIGHTNESS));
   1639   EXPECT_FLOAT_EQ(1.0f, delegate.GetBrightnessForAnimation());
   1640 }
   1641 
   1642 // Tests that an observer added to a scoped settings object is not notified
   1643 // when the animator is destroyed unless explicitly requested.
   1644 TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) {
   1645   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1646   animator->set_disable_timer_for_test(true);
   1647   TestImplicitAnimationObserver observer_notify(true);
   1648   TestImplicitAnimationObserver observer_do_not_notify(false);
   1649   TestLayerAnimationDelegate delegate;
   1650   animator->SetDelegate(&delegate);
   1651 
   1652   EXPECT_FALSE(observer_notify.animations_completed());
   1653   EXPECT_FALSE(observer_do_not_notify.animations_completed());
   1654 
   1655   animator->SetBrightness(1.0f);
   1656 
   1657   {
   1658     ScopedLayerAnimationSettings settings(animator.get());
   1659     settings.AddObserver(&observer_notify);
   1660     settings.AddObserver(&observer_do_not_notify);
   1661     animator->SetBrightness(0.0f);
   1662   }
   1663 
   1664   EXPECT_FALSE(observer_notify.animations_completed());
   1665   EXPECT_FALSE(observer_do_not_notify.animations_completed());
   1666   animator = NULL;
   1667   EXPECT_TRUE(observer_notify.animations_completed());
   1668   EXPECT_TRUE(observer_notify.WasAnimationAbortedForProperty(
   1669       LayerAnimationElement::BRIGHTNESS));
   1670   EXPECT_FALSE(observer_do_not_notify.animations_completed());
   1671 }
   1672 
   1673 TEST(LayerAnimatorTest, AbortedAnimationStatusInImplicitObservers) {
   1674   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1675   animator->set_disable_timer_for_test(true);
   1676   TestImplicitAnimationObserver observer(false);
   1677   TestLayerAnimationDelegate delegate;
   1678   animator->SetDelegate(&delegate);
   1679 
   1680   EXPECT_FALSE(observer.animations_completed());
   1681   animator->SetBrightness(1.0f);
   1682 
   1683   {
   1684     ScopedLayerAnimationSettings settings(animator.get());
   1685     settings.AddObserver(&observer);
   1686     animator->SetBrightness(0.0f);
   1687   }
   1688   EXPECT_FALSE(observer.animations_completed());
   1689 
   1690   animator->AbortAllAnimations();
   1691   EXPECT_TRUE(observer.animations_completed());
   1692   EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
   1693       LayerAnimationElement::BRIGHTNESS));
   1694   EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
   1695       LayerAnimationElement::OPACITY));
   1696 
   1697   observer.set_animations_completed(false);
   1698   {
   1699     ScopedLayerAnimationSettings settings(animator.get());
   1700     settings.AddObserver(&observer);
   1701     animator->SetOpacity(0.0f);
   1702   }
   1703   EXPECT_FALSE(observer.animations_completed());
   1704 
   1705   animator->AbortAllAnimations();
   1706   EXPECT_TRUE(observer.animations_completed());
   1707   EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
   1708       LayerAnimationElement::BRIGHTNESS));
   1709   EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
   1710       LayerAnimationElement::OPACITY));
   1711 }
   1712 
   1713 TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) {
   1714   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1715   AnimationContainerElement* element = animator.get();
   1716   animator->set_disable_timer_for_test(true);
   1717   TestLayerAnimationObserver observer;
   1718   TestLayerAnimationObserver removed_observer;
   1719   TestLayerAnimationDelegate delegate;
   1720   animator->SetDelegate(&delegate);
   1721 
   1722   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1723 
   1724   LayerAnimationSequence* sequence = new LayerAnimationSequence(
   1725       LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
   1726 
   1727   sequence->AddObserver(&observer);
   1728   sequence->AddObserver(&removed_observer);
   1729 
   1730   animator->StartAnimation(sequence);
   1731 
   1732   EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
   1733   EXPECT_TRUE(!observer.last_ended_sequence());
   1734   EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence);
   1735   EXPECT_TRUE(!removed_observer.last_ended_sequence());
   1736 
   1737   // This should stop the observer from observing sequence.
   1738   animator->RemoveObserver(&removed_observer);
   1739 
   1740   base::TimeTicks start_time = animator->last_step_time();
   1741 
   1742   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1743 
   1744   EXPECT_EQ(observer.last_ended_sequence(), sequence);
   1745   EXPECT_TRUE(!removed_observer.last_ended_sequence());
   1746 }
   1747 
   1748 TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) {
   1749   TestLayerAnimationDelegate delegate;
   1750   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1751   animator->set_disable_timer_for_test(true);
   1752 
   1753   scoped_ptr<TestLayerAnimationObserver> observer(
   1754       new TestLayerAnimationObserver);
   1755   animator->SetDelegate(&delegate);
   1756   animator->AddObserver(observer.get());
   1757 
   1758   delegate.SetOpacityFromAnimation(0.0f);
   1759 
   1760   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1761   LayerAnimationSequence* sequence = new LayerAnimationSequence(
   1762       LayerAnimationElement::CreateOpacityElement(1.0f, delta));
   1763 
   1764   animator->StartAnimation(sequence);
   1765 
   1766   // |observer| should be attached to |sequence|.
   1767   EXPECT_EQ(static_cast<size_t>(1), sequence->observers_.size());
   1768 
   1769   // Now, release |observer|
   1770   observer.reset();
   1771 
   1772   // And |sequence| should no longer be attached to |observer|.
   1773   EXPECT_EQ(static_cast<size_t>(0), sequence->observers_.size());
   1774 }
   1775 
   1776 TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) {
   1777   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1778   AnimationContainerElement* element = animator.get();
   1779   animator->set_disable_timer_for_test(true);
   1780 
   1781   TestImplicitAnimationObserver observer(false);
   1782   TestLayerAnimationDelegate delegate;
   1783   animator->SetDelegate(&delegate);
   1784 
   1785   delegate.SetBrightnessFromAnimation(0.0f);
   1786 
   1787   {
   1788     ScopedLayerAnimationSettings setter(animator.get());
   1789 
   1790     base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1791     LayerAnimationSequence* sequence = new LayerAnimationSequence(
   1792         LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
   1793 
   1794     animator->StartAnimation(sequence);
   1795     base::TimeTicks start_time = animator->last_step_time();
   1796     element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
   1797 
   1798     setter.AddObserver(&observer);
   1799 
   1800     // Start observing an in-flight animation.
   1801     sequence->AddObserver(&observer);
   1802 
   1803     element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   1804   }
   1805 
   1806   EXPECT_TRUE(observer.animations_completed());
   1807   EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
   1808                   LayerAnimationElement::BRIGHTNESS));
   1809 }
   1810 
   1811 TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) {
   1812   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1813   AnimationContainerElement* element = animator.get();
   1814   animator->set_disable_timer_for_test(true);
   1815 
   1816   TestImplicitAnimationObserver observer(false);
   1817   TestLayerAnimationDelegate delegate;
   1818   animator->SetDelegate(&delegate);
   1819 
   1820   delegate.SetBrightnessFromAnimation(0.0f);
   1821   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1822   LayerAnimationSequence* sequence = new LayerAnimationSequence(
   1823       LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
   1824 
   1825   {
   1826     ScopedLayerAnimationSettings setter(animator.get());
   1827     setter.AddObserver(&observer);
   1828 
   1829     animator->StartAnimation(sequence);
   1830     base::TimeTicks start_time = animator->last_step_time();
   1831     element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
   1832   }
   1833 
   1834   EXPECT_FALSE(observer.animations_completed());
   1835 
   1836   // Stop observing an in-flight animation.
   1837   sequence->RemoveObserver(&observer);
   1838 
   1839   EXPECT_TRUE(observer.animations_completed());
   1840 
   1841   // The animation didn't complete, and neither was it aborted.
   1842   EXPECT_FALSE(observer.WasAnimationCompletedForProperty(
   1843                   LayerAnimationElement::BRIGHTNESS));
   1844   EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
   1845                   LayerAnimationElement::BRIGHTNESS));
   1846 }
   1847 
   1848 // This checks that if an animation is deleted due to a callback, that the
   1849 // animator does not try to use the deleted animation. For example, if we have
   1850 // two running animations, and the first finishes and the resulting callback
   1851 // causes the second to be deleted, we should not attempt to animate the second
   1852 // animation.
   1853 TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnEnd) {
   1854   ScopedAnimationDurationScaleMode normal_duration_mode(
   1855       ScopedAnimationDurationScaleMode::NORMAL_DURATION);
   1856   scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
   1857   AnimationContainerElement* element = animator.get();
   1858   animator->set_disable_timer_for_test(true);
   1859   TestLayerAnimationDelegate delegate;
   1860   animator->SetDelegate(&delegate);
   1861 
   1862   double start_brightness(0.0);
   1863   double target_brightness(1.0);
   1864 
   1865   gfx::Rect start_bounds(0, 0, 50, 50);
   1866   gfx::Rect target_bounds(5, 5, 5, 5);
   1867 
   1868   delegate.SetBrightnessFromAnimation(start_brightness);
   1869   delegate.SetBoundsFromAnimation(start_bounds);
   1870 
   1871   base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
   1872   base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2);
   1873   base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3);
   1874 
   1875   scoped_ptr<DeletingLayerAnimationObserver> observer(
   1876       new DeletingLayerAnimationObserver(animator.get()));
   1877 
   1878   animator->AddObserver(observer.get());
   1879 
   1880   animator->StartAnimation(
   1881       new LayerAnimationSequence(
   1882           LayerAnimationElement::CreateBrightnessElement(
   1883               target_brightness, brightness_delta)));
   1884 
   1885   animator->StartAnimation(new LayerAnimationSequence(
   1886       LayerAnimationElement::CreateBoundsElement(
   1887           target_bounds, bounds_delta)));
   1888   ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   1889 
   1890   base::TimeTicks start_time = animator->last_step_time();
   1891   element->Step(start_time + halfway_delta);
   1892 
   1893   // Completing the brightness animation should have stopped the bounds
   1894   // animation.
   1895   ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   1896 
   1897   animator->RemoveObserver(observer.get());
   1898 }
   1899 
   1900 // Similar to the ObserverDeletesAnimationsOnEnd test above except that it
   1901 // tests the behavior when the OnLayerAnimationAborted() callback causes
   1902 // all of the animator's other animations to be deleted.
   1903 TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnAbort) {
   1904   ScopedAnimationDurationScaleMode normal_duration_mode(
   1905       ScopedAnimationDurationScaleMode::NORMAL_DURATION);
   1906   scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
   1907   animator->set_disable_timer_for_test(true);
   1908   TestLayerAnimationDelegate delegate;
   1909   animator->SetDelegate(&delegate);
   1910 
   1911   double start_brightness(0.0);
   1912   double target_brightness(1.0);
   1913   gfx::Rect start_bounds(0, 0, 50, 50);
   1914   gfx::Rect target_bounds(5, 5, 5, 5);
   1915   base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
   1916   base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(2);
   1917 
   1918   delegate.SetBrightnessFromAnimation(start_brightness);
   1919   delegate.SetBoundsFromAnimation(start_bounds);
   1920 
   1921   scoped_ptr<DeletingLayerAnimationObserver> observer(
   1922       new DeletingLayerAnimationObserver(animator.get()));
   1923   animator->AddObserver(observer.get());
   1924 
   1925   animator->StartAnimation(
   1926       new LayerAnimationSequence(
   1927           LayerAnimationElement::CreateBrightnessElement(
   1928               target_brightness, brightness_delta)));
   1929   animator->StartAnimation(new LayerAnimationSequence(
   1930       LayerAnimationElement::CreateBoundsElement(
   1931           target_bounds, bounds_delta)));
   1932   ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   1933 
   1934   animator->set_preemption_strategy(
   1935       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
   1936   animator->StartAnimation(
   1937       new LayerAnimationSequence(
   1938           LayerAnimationElement::CreateBrightnessElement(
   1939               target_brightness, brightness_delta)));
   1940 
   1941   // Starting the second brightness animation should have aborted the initial
   1942   // brightness animation. |observer| should have stopped the bounds animation
   1943   // as a result.
   1944   ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   1945 
   1946   animator->RemoveObserver(observer.get());
   1947 }
   1948 
   1949 // Check that setting a property during an animation with a default animator
   1950 // cancels the original animation.
   1951 TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) {
   1952   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1953   animator->set_disable_timer_for_test(true);
   1954   TestLayerAnimationDelegate delegate;
   1955   animator->SetDelegate(&delegate);
   1956 
   1957   double start_opacity(0.0);
   1958   double target_opacity(1.0);
   1959 
   1960   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   1961 
   1962   delegate.SetOpacityFromAnimation(start_opacity);
   1963 
   1964   scoped_ptr<LayerAnimationSequence> sequence(
   1965       new LayerAnimationSequence(
   1966           LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
   1967 
   1968   animator->StartAnimation(sequence.release());
   1969 
   1970   animator->SetOpacity(0.5);
   1971 
   1972   EXPECT_FALSE(animator->is_animating());
   1973   EXPECT_EQ(0.5, animator->GetTargetOpacity());
   1974 }
   1975 
   1976 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
   1977 // second sequence to be leaked.
   1978 TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) {
   1979   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   1980   animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
   1981   animator->set_disable_timer_for_test(true);
   1982   TestLayerAnimationDelegate delegate;
   1983   animator->SetDelegate(&delegate);
   1984 
   1985   gfx::Rect start_bounds(0, 0, 50, 50);
   1986   gfx::Rect middle_bounds(10, 10, 100, 100);
   1987   gfx::Rect target_bounds(5, 5, 5, 5);
   1988 
   1989   delegate.SetBoundsFromAnimation(start_bounds);
   1990 
   1991   {
   1992     // start an implicit bounds animation.
   1993     ScopedLayerAnimationSettings settings(animator.get());
   1994     animator->SetBounds(middle_bounds);
   1995   }
   1996 
   1997   EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   1998 
   1999   int num_live_instances = 0;
   2000   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   2001   scoped_ptr<TestLayerAnimationSequence> sequence(
   2002       new TestLayerAnimationSequence(
   2003           LayerAnimationElement::CreateBoundsElement(target_bounds, delta),
   2004           &num_live_instances));
   2005 
   2006   EXPECT_EQ(1, num_live_instances);
   2007 
   2008   // This should interrupt the running sequence causing us to immediately set
   2009   // the target value. The sequence should alse be destructed.
   2010   animator->StartAnimation(sequence.release());
   2011 
   2012   EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   2013   EXPECT_EQ(0, num_live_instances);
   2014   CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
   2015 }
   2016 
   2017 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
   2018 TEST(LayerAnimatorTest, GetTargetOpacity) {
   2019   TestLayerAnimationDelegate delegate;
   2020   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   2021   animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
   2022   animator->set_disable_timer_for_test(true);
   2023   animator->SetDelegate(&delegate);
   2024 
   2025   delegate.SetOpacityFromAnimation(0.0);
   2026 
   2027   {
   2028     ScopedLayerAnimationSettings settings(animator.get());
   2029     animator->SetOpacity(0.5);
   2030     EXPECT_EQ(0.5, animator->GetTargetOpacity());
   2031 
   2032     // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
   2033     animator->SetOpacity(1.0);
   2034     EXPECT_EQ(1.0, animator->GetTargetOpacity());
   2035   }
   2036 }
   2037 
   2038 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
   2039 TEST(LayerAnimatorTest, GetTargetBrightness) {
   2040   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   2041   animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
   2042   animator->set_disable_timer_for_test(true);
   2043   TestLayerAnimationDelegate delegate;
   2044   animator->SetDelegate(&delegate);
   2045 
   2046   delegate.SetBrightnessFromAnimation(0.0);
   2047 
   2048   {
   2049     ScopedLayerAnimationSettings settings(animator.get());
   2050     animator->SetBrightness(0.5);
   2051     EXPECT_EQ(0.5, animator->GetTargetBrightness());
   2052 
   2053     // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
   2054     animator->SetBrightness(1.0);
   2055     EXPECT_EQ(1.0, animator->GetTargetBrightness());
   2056   }
   2057 }
   2058 
   2059 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
   2060 TEST(LayerAnimatorTest, GetTargetGrayscale) {
   2061   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   2062   animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
   2063   animator->set_disable_timer_for_test(true);
   2064   TestLayerAnimationDelegate delegate;
   2065   animator->SetDelegate(&delegate);
   2066 
   2067   delegate.SetGrayscaleFromAnimation(0.0);
   2068 
   2069   {
   2070     ScopedLayerAnimationSettings settings(animator.get());
   2071     animator->SetGrayscale(0.5);
   2072     EXPECT_EQ(0.5, animator->GetTargetGrayscale());
   2073 
   2074     // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
   2075     animator->SetGrayscale(1.0);
   2076     EXPECT_EQ(1.0, animator->GetTargetGrayscale());
   2077   }
   2078 }
   2079 
   2080 // Verifies color property is modified appropriately.
   2081 TEST(LayerAnimatorTest, Color) {
   2082   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   2083   AnimationContainerElement* element = animator.get();
   2084   animator->set_disable_timer_for_test(true);
   2085   TestLayerAnimationDelegate delegate;
   2086   animator->SetDelegate(&delegate);
   2087 
   2088   SkColor start_color  = SkColorSetARGB(  0, 20, 40,  60);
   2089   SkColor middle_color = SkColorSetARGB(127, 30, 60, 100);
   2090   SkColor target_color = SkColorSetARGB(254, 40, 80, 140);
   2091 
   2092   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   2093 
   2094   delegate.SetColorFromAnimation(start_color);
   2095 
   2096   animator->ScheduleAnimation(
   2097       new LayerAnimationSequence(
   2098           LayerAnimationElement::CreateColorElement(target_color, delta)));
   2099 
   2100   EXPECT_TRUE(animator->is_animating());
   2101   EXPECT_EQ(ColorToString(start_color),
   2102             ColorToString(delegate.GetColorForAnimation()));
   2103 
   2104   base::TimeTicks start_time = animator->last_step_time();
   2105 
   2106   element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
   2107 
   2108   EXPECT_TRUE(animator->is_animating());
   2109   EXPECT_EQ(ColorToString(middle_color),
   2110             ColorToString(delegate.GetColorForAnimation()));
   2111 
   2112   element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
   2113 
   2114   EXPECT_FALSE(animator->is_animating());
   2115   EXPECT_EQ(ColorToString(target_color),
   2116             ColorToString(delegate.GetColorForAnimation()));
   2117 }
   2118 
   2119 // Verifies SchedulePauseForProperties().
   2120 TEST(LayerAnimatorTest, SchedulePauseForProperties) {
   2121   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   2122   animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
   2123   animator->SchedulePauseForProperties(base::TimeDelta::FromMilliseconds(100),
   2124                                        LayerAnimationElement::TRANSFORM,
   2125                                        LayerAnimationElement::BOUNDS, -1);
   2126   EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM));
   2127   EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
   2128   EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY));
   2129 }
   2130 
   2131 
   2132 class AnimatorOwner {
   2133 public:
   2134   AnimatorOwner()
   2135       : animator_(LayerAnimator::CreateDefaultAnimator()) {
   2136   }
   2137 
   2138   LayerAnimator* animator() { return animator_.get(); }
   2139 
   2140 private:
   2141   scoped_refptr<LayerAnimator> animator_;
   2142 
   2143   DISALLOW_COPY_AND_ASSIGN(AnimatorOwner);
   2144 };
   2145 
   2146 class DeletingObserver : public LayerAnimationObserver {
   2147 public:
   2148   DeletingObserver(bool* was_deleted)
   2149       : animator_owner_(new AnimatorOwner),
   2150         delete_on_animation_ended_(false),
   2151         delete_on_animation_aborted_(false),
   2152         delete_on_animation_scheduled_(false),
   2153         was_deleted_(was_deleted) {
   2154     animator()->AddObserver(this);
   2155   }
   2156 
   2157   virtual ~DeletingObserver() {
   2158     animator()->RemoveObserver(this);
   2159     *was_deleted_ = true;
   2160   }
   2161 
   2162   LayerAnimator* animator() { return animator_owner_->animator(); }
   2163 
   2164   bool delete_on_animation_ended() const {
   2165     return delete_on_animation_ended_;
   2166   }
   2167   void set_delete_on_animation_ended(bool enabled) {
   2168     delete_on_animation_ended_ = enabled;
   2169   }
   2170 
   2171   bool delete_on_animation_aborted() const {
   2172     return delete_on_animation_aborted_;
   2173   }
   2174   void set_delete_on_animation_aborted(bool enabled) {
   2175     delete_on_animation_aborted_ = enabled;
   2176   }
   2177 
   2178   bool delete_on_animation_scheduled() const {
   2179     return delete_on_animation_scheduled_;
   2180   }
   2181   void set_delete_on_animation_scheduled(bool enabled) {
   2182     delete_on_animation_scheduled_ = enabled;
   2183   }
   2184 
   2185   // LayerAnimationObserver implementation.
   2186   virtual void OnLayerAnimationEnded(
   2187       LayerAnimationSequence* sequence) OVERRIDE {
   2188     if (delete_on_animation_ended_)
   2189       delete this;
   2190   }
   2191 
   2192   virtual void OnLayerAnimationAborted(
   2193       LayerAnimationSequence* sequence) OVERRIDE {
   2194     if (delete_on_animation_aborted_)
   2195       delete this;
   2196   }
   2197 
   2198   virtual void OnLayerAnimationScheduled(
   2199       LayerAnimationSequence* sequence) OVERRIDE {
   2200     if (delete_on_animation_scheduled_)
   2201       delete this;
   2202   }
   2203 
   2204 private:
   2205   scoped_ptr<AnimatorOwner> animator_owner_;
   2206   bool delete_on_animation_ended_;
   2207   bool delete_on_animation_aborted_;
   2208   bool delete_on_animation_scheduled_;
   2209   bool* was_deleted_;
   2210 
   2211   DISALLOW_COPY_AND_ASSIGN(DeletingObserver);
   2212 };
   2213 
   2214 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) {
   2215   bool observer_was_deleted = false;
   2216   DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
   2217   observer->set_delete_on_animation_ended(true);
   2218   observer->set_delete_on_animation_aborted(true);
   2219   LayerAnimator* animator = observer->animator();
   2220   AnimationContainerElement* element = observer->animator();
   2221   animator->set_disable_timer_for_test(true);
   2222   TestLayerAnimationDelegate delegate;
   2223   animator->SetDelegate(&delegate);
   2224 
   2225   delegate.SetBrightnessFromAnimation(0.0f);
   2226 
   2227   gfx::Rect start_bounds(0, 0, 50, 50);
   2228   gfx::Rect target_bounds(10, 10, 100, 100);
   2229 
   2230   delegate.SetBoundsFromAnimation(start_bounds);
   2231 
   2232   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   2233   LayerAnimationSequence* brightness_sequence = new LayerAnimationSequence(
   2234       LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
   2235   animator->StartAnimation(brightness_sequence);
   2236 
   2237   delta = base::TimeDelta::FromSeconds(2);
   2238   LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
   2239       LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
   2240   animator->StartAnimation(bounds_sequence);
   2241 
   2242   base::TimeTicks start_time = animator->last_step_time();
   2243   element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
   2244 
   2245   EXPECT_TRUE(observer_was_deleted);
   2246 }
   2247 
   2248 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterStoppingAnimating) {
   2249   bool observer_was_deleted = false;
   2250   DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
   2251   observer->set_delete_on_animation_ended(true);
   2252   observer->set_delete_on_animation_aborted(true);
   2253   LayerAnimator* animator = observer->animator();
   2254   animator->set_disable_timer_for_test(true);
   2255   TestLayerAnimationDelegate delegate;
   2256   animator->SetDelegate(&delegate);
   2257 
   2258   delegate.SetOpacityFromAnimation(0.0f);
   2259 
   2260   gfx::Rect start_bounds(0, 0, 50, 50);
   2261   gfx::Rect target_bounds(10, 10, 100, 100);
   2262 
   2263   delegate.SetBoundsFromAnimation(start_bounds);
   2264 
   2265   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   2266   LayerAnimationSequence* opacity_sequence = new LayerAnimationSequence(
   2267       LayerAnimationElement::CreateOpacityElement(1.0f, delta));
   2268   animator->StartAnimation(opacity_sequence);
   2269 
   2270   delta = base::TimeDelta::FromSeconds(2);
   2271   LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
   2272       LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
   2273   animator->StartAnimation(bounds_sequence);
   2274 
   2275   animator->StopAnimating();
   2276 
   2277   EXPECT_TRUE(observer_was_deleted);
   2278 }
   2279 
   2280 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterScheduling) {
   2281   bool observer_was_deleted = false;
   2282   TestLayerAnimationDelegate delegate;
   2283   DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
   2284   observer->set_delete_on_animation_scheduled(true);
   2285   LayerAnimator* animator = observer->animator();
   2286   animator->set_disable_timer_for_test(true);
   2287   animator->SetDelegate(&delegate);
   2288 
   2289   delegate.SetOpacityFromAnimation(0.0f);
   2290 
   2291   gfx::Rect start_bounds(0, 0, 50, 50);
   2292   gfx::Rect target_bounds(10, 10, 100, 100);
   2293 
   2294   delegate.SetBoundsFromAnimation(start_bounds);
   2295 
   2296   std::vector<LayerAnimationSequence*> to_start;
   2297 
   2298   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   2299   to_start.push_back(new LayerAnimationSequence(
   2300       LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
   2301 
   2302   delta = base::TimeDelta::FromSeconds(2);
   2303   to_start.push_back(new LayerAnimationSequence(
   2304       LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
   2305 
   2306   animator->ScheduleTogether(to_start);
   2307 
   2308   EXPECT_TRUE(observer_was_deleted);
   2309 }
   2310 
   2311 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterAborted) {
   2312   bool observer_was_deleted = false;
   2313   DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
   2314   TestLayerAnimationDelegate delegate;
   2315   observer->set_delete_on_animation_aborted(true);
   2316   LayerAnimator* animator = observer->animator();
   2317   animator->set_preemption_strategy(
   2318       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
   2319   animator->set_disable_timer_for_test(true);
   2320   animator->SetDelegate(&delegate);
   2321 
   2322   delegate.SetOpacityFromAnimation(0.0f);
   2323 
   2324   gfx::Rect start_bounds(0, 0, 50, 50);
   2325   gfx::Rect target_bounds(10, 10, 100, 100);
   2326 
   2327   delegate.SetBoundsFromAnimation(start_bounds);
   2328 
   2329   std::vector<LayerAnimationSequence*> to_start;
   2330 
   2331   base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
   2332   to_start.push_back(new LayerAnimationSequence(
   2333       LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
   2334 
   2335   delta = base::TimeDelta::FromSeconds(2);
   2336   to_start.push_back(new LayerAnimationSequence(
   2337       LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
   2338 
   2339   animator->ScheduleTogether(to_start);
   2340 
   2341   EXPECT_FALSE(observer_was_deleted);
   2342 
   2343   animator->StartAnimation(new LayerAnimationSequence(
   2344       LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
   2345 
   2346   EXPECT_TRUE(observer_was_deleted);
   2347 }
   2348 
   2349 
   2350 TEST(LayerAnimatorTest, TestSetterRespectEnqueueStrategy) {
   2351   TestLayerAnimationDelegate delegate;
   2352   scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
   2353   animator->set_disable_timer_for_test(true);
   2354 
   2355   animator->SetDelegate(&delegate);
   2356 
   2357   float start_opacity = 0.0f;
   2358   float target_opacity = 1.0f;
   2359   float magic_opacity = 0.123f;
   2360 
   2361   delegate.SetOpacityFromAnimation(start_opacity);
   2362 
   2363   ScopedLayerAnimationSettings settings(animator.get());
   2364   settings.SetPreemptionStrategy(
   2365       LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
   2366   settings.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
   2367   animator->SetOpacity(target_opacity);
   2368 
   2369   EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
   2370 
   2371   settings.SetPreemptionStrategy(
   2372       LayerAnimator::ENQUEUE_NEW_ANIMATION);
   2373   settings.SetTransitionDuration(base::TimeDelta());
   2374   animator->SetOpacity(magic_opacity);
   2375 
   2376   EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
   2377 }
   2378 
   2379 }  // namespace ui
   2380