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