Home | History | Annotate | Download | only in animation
      1 // Copyright 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 "cc/animation/layer_animation_controller.h"
      6 
      7 #include "cc/animation/animation.h"
      8 #include "cc/animation/animation_curve.h"
      9 #include "cc/animation/animation_delegate.h"
     10 #include "cc/animation/animation_registrar.h"
     11 #include "cc/animation/keyframed_animation_curve.h"
     12 #include "cc/animation/scroll_offset_animation_curve.h"
     13 #include "cc/animation/transform_operations.h"
     14 #include "cc/test/animation_test_common.h"
     15 #include "testing/gmock/include/gmock/gmock.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 #include "ui/gfx/box_f.h"
     18 #include "ui/gfx/transform.h"
     19 
     20 namespace cc {
     21 namespace {
     22 
     23 using base::TimeDelta;
     24 using base::TimeTicks;
     25 
     26 static base::TimeTicks TicksFromSecondsF(double seconds) {
     27   return base::TimeTicks::FromInternalValue(seconds *
     28                                             base::Time::kMicrosecondsPerSecond);
     29 }
     30 
     31 // A LayerAnimationController cannot be ticked at 0.0, since an animation
     32 // with start time 0.0 is treated as an animation whose start time has
     33 // not yet been set.
     34 const TimeTicks kInitialTickTime = TicksFromSecondsF(1.0);
     35 
     36 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve,
     37                                       int id,
     38                                       Animation::TargetProperty property) {
     39   return Animation::Create(curve.Pass(), 0, id, property);
     40 }
     41 
     42 TEST(LayerAnimationControllerTest, SyncNewAnimation) {
     43   FakeLayerAnimationValueObserver dummy_impl;
     44   scoped_refptr<LayerAnimationController> controller_impl(
     45       LayerAnimationController::Create(0));
     46   controller_impl->AddValueObserver(&dummy_impl);
     47   FakeLayerAnimationValueObserver dummy;
     48   scoped_refptr<LayerAnimationController> controller(
     49       LayerAnimationController::Create(0));
     50   controller->AddValueObserver(&dummy);
     51 
     52   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
     53 
     54   EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
     55   EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
     56 
     57   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
     58   EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
     59   int group_id = controller->GetAnimation(Animation::Opacity)->group();
     60 
     61   controller->PushAnimationUpdatesTo(controller_impl.get());
     62   EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
     63   controller_impl->ActivateAnimations();
     64 
     65   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
     66   EXPECT_EQ(Animation::WaitingForTargetAvailability,
     67             controller_impl->GetAnimation(group_id,
     68                                           Animation::Opacity)->run_state());
     69 }
     70 
     71 // If an animation is started on the impl thread before it is ticked on the main
     72 // thread, we must be sure to respect the synchronized start time.
     73 TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) {
     74   FakeLayerAnimationValueObserver dummy_impl;
     75   scoped_refptr<LayerAnimationController> controller_impl(
     76       LayerAnimationController::Create(0));
     77   controller_impl->AddValueObserver(&dummy_impl);
     78   FakeLayerAnimationValueObserver dummy;
     79   scoped_refptr<LayerAnimationController> controller(
     80       LayerAnimationController::Create(0));
     81   controller->AddValueObserver(&dummy);
     82 
     83   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
     84 
     85   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
     86   int group_id = controller->GetAnimation(Animation::Opacity)->group();
     87 
     88   controller->PushAnimationUpdatesTo(controller_impl.get());
     89   controller_impl->ActivateAnimations();
     90 
     91   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
     92   EXPECT_EQ(Animation::WaitingForTargetAvailability,
     93             controller_impl->GetAnimation(group_id,
     94                                           Animation::Opacity)->run_state());
     95 
     96   AnimationEventsVector events;
     97   controller_impl->Animate(kInitialTickTime);
     98   controller_impl->UpdateState(true, &events);
     99 
    100   // Synchronize the start times.
    101   EXPECT_EQ(1u, events.size());
    102   controller->NotifyAnimationStarted(events[0]);
    103   EXPECT_EQ(controller->GetAnimation(group_id,
    104                                      Animation::Opacity)->start_time(),
    105             controller_impl->GetAnimation(group_id,
    106                                           Animation::Opacity)->start_time());
    107 
    108   // Start the animation on the main thread. Should not affect the start time.
    109   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
    110   controller->UpdateState(true, NULL);
    111   EXPECT_EQ(controller->GetAnimation(group_id,
    112                                      Animation::Opacity)->start_time(),
    113             controller_impl->GetAnimation(group_id,
    114                                           Animation::Opacity)->start_time());
    115 }
    116 
    117 TEST(LayerAnimationControllerTest, UseSpecifiedStartTimes) {
    118   FakeLayerAnimationValueObserver dummy_impl;
    119   scoped_refptr<LayerAnimationController> controller_impl(
    120       LayerAnimationController::Create(0));
    121   controller_impl->AddValueObserver(&dummy_impl);
    122   FakeLayerAnimationValueObserver dummy;
    123   scoped_refptr<LayerAnimationController> controller(
    124       LayerAnimationController::Create(0));
    125   controller->AddValueObserver(&dummy);
    126 
    127   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
    128   int group_id = controller->GetAnimation(Animation::Opacity)->group();
    129 
    130   const TimeTicks start_time = TicksFromSecondsF(123);
    131   controller->GetAnimation(Animation::Opacity)->set_start_time(start_time);
    132 
    133   controller->PushAnimationUpdatesTo(controller_impl.get());
    134   controller_impl->ActivateAnimations();
    135 
    136   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
    137   EXPECT_EQ(Animation::WaitingForTargetAvailability,
    138             controller_impl->GetAnimation(group_id,
    139                                           Animation::Opacity)->run_state());
    140 
    141   AnimationEventsVector events;
    142   controller_impl->Animate(kInitialTickTime);
    143   controller_impl->UpdateState(true, &events);
    144 
    145   // Synchronize the start times.
    146   EXPECT_EQ(1u, events.size());
    147   controller->NotifyAnimationStarted(events[0]);
    148 
    149   EXPECT_EQ(start_time,
    150             controller->GetAnimation(group_id,
    151                                      Animation::Opacity)->start_time());
    152   EXPECT_EQ(controller->GetAnimation(group_id,
    153                                      Animation::Opacity)->start_time(),
    154             controller_impl->GetAnimation(group_id,
    155                                           Animation::Opacity)->start_time());
    156 
    157   // Start the animation on the main thread. Should not affect the start time.
    158   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
    159   controller->UpdateState(true, NULL);
    160   EXPECT_EQ(start_time,
    161             controller->GetAnimation(group_id,
    162                                      Animation::Opacity)->start_time());
    163   EXPECT_EQ(controller->GetAnimation(group_id,
    164                                      Animation::Opacity)->start_time(),
    165             controller_impl->GetAnimation(group_id,
    166                                           Animation::Opacity)->start_time());
    167 }
    168 
    169 // Tests that controllers activate and deactivate as expected.
    170 TEST(LayerAnimationControllerTest, Activation) {
    171   scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
    172   scoped_ptr<AnimationRegistrar> registrar_impl = AnimationRegistrar::Create();
    173 
    174   FakeLayerAnimationValueObserver dummy_impl;
    175   scoped_refptr<LayerAnimationController> controller_impl(
    176       LayerAnimationController::Create(0));
    177   controller_impl->AddValueObserver(&dummy_impl);
    178   FakeLayerAnimationValueObserver dummy;
    179   scoped_refptr<LayerAnimationController> controller(
    180       LayerAnimationController::Create(0));
    181   controller->AddValueObserver(&dummy);
    182   scoped_ptr<AnimationEventsVector> events(
    183       make_scoped_ptr(new AnimationEventsVector));
    184 
    185   controller->SetAnimationRegistrar(registrar.get());
    186   controller_impl->SetAnimationRegistrar(registrar_impl.get());
    187   EXPECT_EQ(1u, registrar->all_animation_controllers().size());
    188   EXPECT_EQ(1u, registrar_impl->all_animation_controllers().size());
    189 
    190   // Initially, both controllers should be inactive.
    191   EXPECT_EQ(0u, registrar->active_animation_controllers().size());
    192   EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
    193 
    194   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
    195   // The main thread controller should now be active.
    196   EXPECT_EQ(1u, registrar->active_animation_controllers().size());
    197 
    198   controller->PushAnimationUpdatesTo(controller_impl.get());
    199   controller_impl->ActivateAnimations();
    200   // Both controllers should now be active.
    201   EXPECT_EQ(1u, registrar->active_animation_controllers().size());
    202   EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
    203 
    204   controller_impl->Animate(kInitialTickTime);
    205   controller_impl->UpdateState(true, events.get());
    206   EXPECT_EQ(1u, events->size());
    207   controller->NotifyAnimationStarted((*events)[0]);
    208 
    209   EXPECT_EQ(1u, registrar->active_animation_controllers().size());
    210   EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
    211 
    212   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
    213   controller->UpdateState(true, NULL);
    214   EXPECT_EQ(1u, registrar->active_animation_controllers().size());
    215 
    216   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
    217   controller->UpdateState(true, NULL);
    218   EXPECT_EQ(Animation::Finished,
    219             controller->GetAnimation(Animation::Opacity)->run_state());
    220   EXPECT_EQ(1u, registrar->active_animation_controllers().size());
    221 
    222   events.reset(new AnimationEventsVector);
    223   controller_impl->Animate(kInitialTickTime +
    224                            TimeDelta::FromMilliseconds(1500));
    225   controller_impl->UpdateState(true, events.get());
    226 
    227   EXPECT_EQ(Animation::WaitingForDeletion,
    228             controller_impl->GetAnimation(Animation::Opacity)->run_state());
    229   // The impl thread controller should have de-activated.
    230   EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
    231 
    232   EXPECT_EQ(1u, events->size());
    233   controller->NotifyAnimationFinished((*events)[0]);
    234   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
    235   controller->UpdateState(true, NULL);
    236 
    237   EXPECT_EQ(Animation::WaitingForDeletion,
    238             controller->GetAnimation(Animation::Opacity)->run_state());
    239   // The main thread controller should have de-activated.
    240   EXPECT_EQ(0u, registrar->active_animation_controllers().size());
    241 
    242   controller->PushAnimationUpdatesTo(controller_impl.get());
    243   controller_impl->ActivateAnimations();
    244   EXPECT_FALSE(controller->has_any_animation());
    245   EXPECT_FALSE(controller_impl->has_any_animation());
    246   EXPECT_EQ(0u, registrar->active_animation_controllers().size());
    247   EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
    248 
    249   controller->SetAnimationRegistrar(NULL);
    250   controller_impl->SetAnimationRegistrar(NULL);
    251 }
    252 
    253 TEST(LayerAnimationControllerTest, SyncPause) {
    254   FakeLayerAnimationValueObserver dummy_impl;
    255   scoped_refptr<LayerAnimationController> controller_impl(
    256       LayerAnimationController::Create(0));
    257   controller_impl->AddValueObserver(&dummy_impl);
    258   FakeLayerAnimationValueObserver dummy;
    259   scoped_refptr<LayerAnimationController> controller(
    260       LayerAnimationController::Create(0));
    261   controller->AddValueObserver(&dummy);
    262 
    263   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
    264 
    265   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
    266   int group_id = controller->GetAnimation(Animation::Opacity)->group();
    267   int animation_id = controller->GetAnimation(Animation::Opacity)->id();
    268 
    269   controller->PushAnimationUpdatesTo(controller_impl.get());
    270   controller_impl->ActivateAnimations();
    271 
    272   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
    273   EXPECT_EQ(Animation::WaitingForTargetAvailability,
    274             controller_impl->GetAnimation(group_id,
    275                                           Animation::Opacity)->run_state());
    276 
    277   // Start the animations on each controller.
    278   AnimationEventsVector events;
    279   controller_impl->Animate(kInitialTickTime);
    280   controller_impl->UpdateState(true, &events);
    281   controller->Animate(kInitialTickTime);
    282   controller->UpdateState(true, NULL);
    283   EXPECT_EQ(Animation::Running,
    284             controller_impl->GetAnimation(group_id,
    285                                           Animation::Opacity)->run_state());
    286   EXPECT_EQ(Animation::Running,
    287             controller->GetAnimation(group_id,
    288                                      Animation::Opacity)->run_state());
    289 
    290   // Pause the main-thread animation.
    291   controller->PauseAnimation(
    292       animation_id,
    293       TimeDelta::FromMilliseconds(1000) + TimeDelta::FromMilliseconds(1000));
    294   EXPECT_EQ(Animation::Paused,
    295             controller->GetAnimation(group_id,
    296                                      Animation::Opacity)->run_state());
    297 
    298   // The pause run state change should make it to the impl thread controller.
    299   controller->PushAnimationUpdatesTo(controller_impl.get());
    300   controller_impl->ActivateAnimations();
    301   EXPECT_EQ(Animation::Paused,
    302             controller_impl->GetAnimation(group_id,
    303                                           Animation::Opacity)->run_state());
    304 }
    305 
    306 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
    307   FakeLayerAnimationValueObserver dummy_impl;
    308   scoped_refptr<LayerAnimationController> controller_impl(
    309       LayerAnimationController::Create(0));
    310   controller_impl->AddValueObserver(&dummy_impl);
    311   FakeLayerAnimationValueObserver dummy;
    312   scoped_refptr<LayerAnimationController> controller(
    313       LayerAnimationController::Create(0));
    314   controller->AddValueObserver(&dummy);
    315 
    316   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
    317 
    318   int animation_id =
    319       AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
    320   int group_id = controller->GetAnimation(Animation::Opacity)->group();
    321 
    322   controller->PushAnimationUpdatesTo(controller_impl.get());
    323   controller_impl->ActivateAnimations();
    324 
    325   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
    326   EXPECT_EQ(Animation::WaitingForTargetAvailability,
    327             controller_impl->GetAnimation(group_id,
    328                                           Animation::Opacity)->run_state());
    329 
    330   // Notify main thread controller that the animation has started.
    331   AnimationEvent animation_started_event(AnimationEvent::Started,
    332                                          0,
    333                                          group_id,
    334                                          Animation::Opacity,
    335                                          kInitialTickTime);
    336   controller->NotifyAnimationStarted(animation_started_event);
    337 
    338   // Force animation to complete on impl thread.
    339   controller_impl->RemoveAnimation(animation_id);
    340 
    341   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
    342 
    343   controller->PushAnimationUpdatesTo(controller_impl.get());
    344   controller_impl->ActivateAnimations();
    345 
    346   // Even though the main thread has a 'new' animation, it should not be pushed
    347   // because the animation has already completed on the impl thread.
    348   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
    349 }
    350 
    351 // Ensure that a finished animation is eventually deleted by both the
    352 // main-thread and the impl-thread controllers.
    353 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
    354   FakeLayerAnimationValueObserver dummy;
    355   FakeLayerAnimationValueObserver dummy_impl;
    356   scoped_ptr<AnimationEventsVector> events(
    357       make_scoped_ptr(new AnimationEventsVector));
    358   scoped_refptr<LayerAnimationController> controller(
    359       LayerAnimationController::Create(0));
    360   scoped_refptr<LayerAnimationController> controller_impl(
    361       LayerAnimationController::Create(0));
    362   controller->AddValueObserver(&dummy);
    363   controller_impl->AddValueObserver(&dummy_impl);
    364 
    365   AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
    366   controller->Animate(kInitialTickTime);
    367   controller->UpdateState(true, NULL);
    368   controller->PushAnimationUpdatesTo(controller_impl.get());
    369   controller_impl->ActivateAnimations();
    370 
    371   controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
    372   controller_impl->UpdateState(true, events.get());
    373 
    374   // There should be a Started event for the animation.
    375   EXPECT_EQ(1u, events->size());
    376   EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
    377   controller->NotifyAnimationStarted((*events)[0]);
    378 
    379   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
    380   controller->UpdateState(true, NULL);
    381 
    382   EXPECT_FALSE(dummy.animation_waiting_for_deletion());
    383   EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
    384 
    385   events.reset(new AnimationEventsVector);
    386   controller_impl->Animate(kInitialTickTime +
    387                            TimeDelta::FromMilliseconds(2000));
    388   controller_impl->UpdateState(true, events.get());
    389 
    390   EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
    391 
    392   // There should be a Finished event for the animation.
    393   EXPECT_EQ(1u, events->size());
    394   EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
    395 
    396   // Neither controller should have deleted the animation yet.
    397   EXPECT_TRUE(controller->GetAnimation(Animation::Opacity));
    398   EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity));
    399 
    400   controller->NotifyAnimationFinished((*events)[0]);
    401 
    402   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
    403   controller->UpdateState(true, NULL);
    404   EXPECT_TRUE(dummy.animation_waiting_for_deletion());
    405 
    406   controller->PushAnimationUpdatesTo(controller_impl.get());
    407 
    408   // Both controllers should now have deleted the animation. The impl controller
    409   // should have deleted the animation even though activation has not occurred,
    410   // since the animation was already waiting for deletion when
    411   // PushAnimationUpdatesTo was called.
    412   EXPECT_FALSE(controller->has_any_animation());
    413   EXPECT_FALSE(controller_impl->has_any_animation());
    414 }
    415 
    416 // Tests that transitioning opacity from 0 to 1 works as expected.
    417 
    418 static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
    419     const AnimationEventsVector* events) {
    420   const AnimationEvent* event = 0;
    421   for (size_t i = 0; i < events->size(); ++i)
    422     if ((*events)[i].type == AnimationEvent::PropertyUpdate)
    423       event = &(*events)[i];
    424 
    425   return event;
    426 }
    427 
    428 TEST(LayerAnimationControllerTest, TrivialTransition) {
    429   scoped_ptr<AnimationEventsVector> events(
    430       make_scoped_ptr(new AnimationEventsVector));
    431   FakeLayerAnimationValueObserver dummy;
    432   scoped_refptr<LayerAnimationController> controller(
    433       LayerAnimationController::Create(0));
    434   controller->AddValueObserver(&dummy);
    435 
    436   scoped_ptr<Animation> to_add(CreateAnimation(
    437       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
    438       1,
    439       Animation::Opacity));
    440 
    441   EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
    442   controller->AddAnimation(to_add.Pass());
    443   EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
    444   controller->Animate(kInitialTickTime);
    445   EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
    446   controller->UpdateState(true, events.get());
    447   EXPECT_TRUE(controller->HasActiveAnimation());
    448   EXPECT_EQ(0.f, dummy.opacity());
    449   // A non-impl-only animation should not generate property updates.
    450   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
    451   EXPECT_FALSE(event);
    452   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
    453   controller->UpdateState(true, events.get());
    454   EXPECT_EQ(1.f, dummy.opacity());
    455   EXPECT_FALSE(controller->HasActiveAnimation());
    456   event = GetMostRecentPropertyUpdateEvent(events.get());
    457   EXPECT_FALSE(event);
    458 }
    459 
    460 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
    461   scoped_ptr<AnimationEventsVector> events(
    462       make_scoped_ptr(new AnimationEventsVector));
    463   FakeLayerAnimationValueObserver dummy_impl;
    464   scoped_refptr<LayerAnimationController> controller_impl(
    465       LayerAnimationController::Create(0));
    466   controller_impl->AddValueObserver(&dummy_impl);
    467 
    468   scoped_ptr<Animation> to_add(CreateAnimation(
    469       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
    470       1,
    471       Animation::Opacity));
    472   to_add->set_is_impl_only(true);
    473 
    474   controller_impl->AddAnimation(to_add.Pass());
    475   controller_impl->Animate(kInitialTickTime);
    476   controller_impl->UpdateState(true, events.get());
    477   EXPECT_TRUE(controller_impl->HasActiveAnimation());
    478   EXPECT_EQ(0.f, dummy_impl.opacity());
    479   EXPECT_EQ(2u, events->size());
    480   const AnimationEvent* start_opacity_event =
    481       GetMostRecentPropertyUpdateEvent(events.get());
    482   EXPECT_EQ(0.f, start_opacity_event->opacity);
    483 
    484   controller_impl->Animate(kInitialTickTime +
    485                            TimeDelta::FromMilliseconds(1000));
    486   controller_impl->UpdateState(true, events.get());
    487   EXPECT_EQ(1.f, dummy_impl.opacity());
    488   EXPECT_FALSE(controller_impl->HasActiveAnimation());
    489   EXPECT_EQ(4u, events->size());
    490   const AnimationEvent* end_opacity_event =
    491       GetMostRecentPropertyUpdateEvent(events.get());
    492   EXPECT_EQ(1.f, end_opacity_event->opacity);
    493 }
    494 
    495 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
    496   scoped_ptr<AnimationEventsVector> events(
    497       make_scoped_ptr(new AnimationEventsVector));
    498   FakeLayerAnimationValueObserver dummy_impl;
    499   scoped_refptr<LayerAnimationController> controller_impl(
    500       LayerAnimationController::Create(0));
    501   controller_impl->AddValueObserver(&dummy_impl);
    502 
    503   // Choose different values for x and y to avoid coincidental values in the
    504   // observed transforms.
    505   const float delta_x = 3;
    506   const float delta_y = 4;
    507 
    508   scoped_ptr<KeyframedTransformAnimationCurve> curve(
    509       KeyframedTransformAnimationCurve::Create());
    510 
    511   // Create simple Transform animation.
    512   TransformOperations operations;
    513   curve->AddKeyframe(
    514       TransformKeyframe::Create(0, operations, scoped_ptr<TimingFunction>()));
    515   operations.AppendTranslate(delta_x, delta_y, 0);
    516   curve->AddKeyframe(
    517       TransformKeyframe::Create(1, operations, scoped_ptr<TimingFunction>()));
    518 
    519   scoped_ptr<Animation> animation(Animation::Create(
    520       curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform));
    521   animation->set_is_impl_only(true);
    522   controller_impl->AddAnimation(animation.Pass());
    523 
    524   // Run animation.
    525   controller_impl->Animate(kInitialTickTime);
    526   controller_impl->UpdateState(true, events.get());
    527   EXPECT_TRUE(controller_impl->HasActiveAnimation());
    528   EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
    529   EXPECT_EQ(2u, events->size());
    530   const AnimationEvent* start_transform_event =
    531       GetMostRecentPropertyUpdateEvent(events.get());
    532   ASSERT_TRUE(start_transform_event);
    533   EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
    534   EXPECT_TRUE(start_transform_event->is_impl_only);
    535 
    536   gfx::Transform expected_transform;
    537   expected_transform.Translate(delta_x, delta_y);
    538 
    539   controller_impl->Animate(kInitialTickTime +
    540                            TimeDelta::FromMilliseconds(1000));
    541   controller_impl->UpdateState(true, events.get());
    542   EXPECT_EQ(expected_transform, dummy_impl.transform());
    543   EXPECT_FALSE(controller_impl->HasActiveAnimation());
    544   EXPECT_EQ(4u, events->size());
    545   const AnimationEvent* end_transform_event =
    546       GetMostRecentPropertyUpdateEvent(events.get());
    547   EXPECT_EQ(expected_transform, end_transform_event->transform);
    548   EXPECT_TRUE(end_transform_event->is_impl_only);
    549 }
    550 
    551 TEST(LayerAnimationControllerTest, FilterTransition) {
    552   scoped_ptr<AnimationEventsVector> events(
    553       make_scoped_ptr(new AnimationEventsVector));
    554   FakeLayerAnimationValueObserver dummy;
    555   scoped_refptr<LayerAnimationController> controller(
    556       LayerAnimationController::Create(0));
    557   controller->AddValueObserver(&dummy);
    558 
    559   scoped_ptr<KeyframedFilterAnimationCurve> curve(
    560       KeyframedFilterAnimationCurve::Create());
    561 
    562   FilterOperations start_filters;
    563   start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
    564   curve->AddKeyframe(
    565       FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
    566   FilterOperations end_filters;
    567   end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
    568   curve->AddKeyframe(
    569       FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
    570 
    571   scoped_ptr<Animation> animation(Animation::Create(
    572       curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
    573   controller->AddAnimation(animation.Pass());
    574 
    575   controller->Animate(kInitialTickTime);
    576   controller->UpdateState(true, events.get());
    577   EXPECT_TRUE(controller->HasActiveAnimation());
    578   EXPECT_EQ(start_filters, dummy.filters());
    579   // A non-impl-only animation should not generate property updates.
    580   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
    581   EXPECT_FALSE(event);
    582 
    583   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
    584   controller->UpdateState(true, events.get());
    585   EXPECT_EQ(1u, dummy.filters().size());
    586   EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
    587             dummy.filters().at(0));
    588   event = GetMostRecentPropertyUpdateEvent(events.get());
    589   EXPECT_FALSE(event);
    590 
    591   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
    592   controller->UpdateState(true, events.get());
    593   EXPECT_EQ(end_filters, dummy.filters());
    594   EXPECT_FALSE(controller->HasActiveAnimation());
    595   event = GetMostRecentPropertyUpdateEvent(events.get());
    596   EXPECT_FALSE(event);
    597 }
    598 
    599 TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
    600   scoped_ptr<AnimationEventsVector> events(
    601       make_scoped_ptr(new AnimationEventsVector));
    602   FakeLayerAnimationValueObserver dummy_impl;
    603   scoped_refptr<LayerAnimationController> controller_impl(
    604       LayerAnimationController::Create(0));
    605   controller_impl->AddValueObserver(&dummy_impl);
    606 
    607   scoped_ptr<KeyframedFilterAnimationCurve> curve(
    608       KeyframedFilterAnimationCurve::Create());
    609 
    610   // Create simple Filter animation.
    611   FilterOperations start_filters;
    612   start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
    613   curve->AddKeyframe(
    614       FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
    615   FilterOperations end_filters;
    616   end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
    617   curve->AddKeyframe(
    618       FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
    619 
    620   scoped_ptr<Animation> animation(Animation::Create(
    621       curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
    622   animation->set_is_impl_only(true);
    623   controller_impl->AddAnimation(animation.Pass());
    624 
    625   // Run animation.
    626   controller_impl->Animate(kInitialTickTime);
    627   controller_impl->UpdateState(true, events.get());
    628   EXPECT_TRUE(controller_impl->HasActiveAnimation());
    629   EXPECT_EQ(start_filters, dummy_impl.filters());
    630   EXPECT_EQ(2u, events->size());
    631   const AnimationEvent* start_filter_event =
    632       GetMostRecentPropertyUpdateEvent(events.get());
    633   EXPECT_TRUE(start_filter_event);
    634   EXPECT_EQ(start_filters, start_filter_event->filters);
    635   EXPECT_TRUE(start_filter_event->is_impl_only);
    636 
    637   controller_impl->Animate(kInitialTickTime +
    638                            TimeDelta::FromMilliseconds(1000));
    639   controller_impl->UpdateState(true, events.get());
    640   EXPECT_EQ(end_filters, dummy_impl.filters());
    641   EXPECT_FALSE(controller_impl->HasActiveAnimation());
    642   EXPECT_EQ(4u, events->size());
    643   const AnimationEvent* end_filter_event =
    644       GetMostRecentPropertyUpdateEvent(events.get());
    645   EXPECT_TRUE(end_filter_event);
    646   EXPECT_EQ(end_filters, end_filter_event->filters);
    647   EXPECT_TRUE(end_filter_event->is_impl_only);
    648 }
    649 
    650 TEST(LayerAnimationControllerTest, ScrollOffsetTransition) {
    651   FakeLayerAnimationValueObserver dummy_impl;
    652   FakeLayerAnimationValueProvider dummy_provider_impl;
    653   scoped_refptr<LayerAnimationController> controller_impl(
    654       LayerAnimationController::Create(0));
    655   controller_impl->AddValueObserver(&dummy_impl);
    656   controller_impl->set_value_provider(&dummy_provider_impl);
    657   scoped_ptr<AnimationEventsVector> events(
    658       make_scoped_ptr(new AnimationEventsVector));
    659   FakeLayerAnimationValueObserver dummy;
    660   FakeLayerAnimationValueProvider dummy_provider;
    661   scoped_refptr<LayerAnimationController> controller(
    662       LayerAnimationController::Create(0));
    663   controller->AddValueObserver(&dummy);
    664   controller->set_value_provider(&dummy_provider);
    665 
    666   gfx::Vector2dF initial_value(100.f, 300.f);
    667   gfx::Vector2dF target_value(300.f, 200.f);
    668   scoped_ptr<ScrollOffsetAnimationCurve> curve(
    669       ScrollOffsetAnimationCurve::Create(
    670           target_value,
    671           EaseInOutTimingFunction::Create().Pass()));
    672 
    673   scoped_ptr<Animation> animation(Animation::Create(
    674       curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
    675   animation->set_needs_synchronized_start_time(true);
    676   controller->AddAnimation(animation.Pass());
    677 
    678   dummy_provider_impl.set_scroll_offset(initial_value);
    679   controller->PushAnimationUpdatesTo(controller_impl.get());
    680   controller_impl->ActivateAnimations();
    681   EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
    682   double duration_in_seconds =
    683       controller_impl->GetAnimation(Animation::ScrollOffset)
    684           ->curve()
    685           ->Duration();
    686   TimeDelta duration = TimeDelta::FromMicroseconds(
    687       duration_in_seconds * base::Time::kMicrosecondsPerSecond);
    688   EXPECT_EQ(
    689       duration_in_seconds,
    690       controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
    691 
    692   controller->Animate(kInitialTickTime);
    693   controller->UpdateState(true, NULL);
    694   EXPECT_TRUE(controller->HasActiveAnimation());
    695   EXPECT_EQ(initial_value, dummy.scroll_offset());
    696 
    697   controller_impl->Animate(kInitialTickTime);
    698   controller_impl->UpdateState(true, events.get());
    699   EXPECT_TRUE(controller_impl->HasActiveAnimation());
    700   EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
    701   // Scroll offset animations should not generate property updates.
    702   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
    703   EXPECT_FALSE(event);
    704 
    705   controller->NotifyAnimationStarted((*events)[0]);
    706   controller->Animate(kInitialTickTime + duration / 2);
    707   controller->UpdateState(true, NULL);
    708   EXPECT_TRUE(controller->HasActiveAnimation());
    709   EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
    710 
    711   controller_impl->Animate(kInitialTickTime + duration / 2);
    712   controller_impl->UpdateState(true, events.get());
    713   EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
    714                       dummy_impl.scroll_offset());
    715   event = GetMostRecentPropertyUpdateEvent(events.get());
    716   EXPECT_FALSE(event);
    717 
    718   controller_impl->Animate(kInitialTickTime + duration);
    719   controller_impl->UpdateState(true, events.get());
    720   EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
    721   EXPECT_FALSE(controller_impl->HasActiveAnimation());
    722   event = GetMostRecentPropertyUpdateEvent(events.get());
    723   EXPECT_FALSE(event);
    724 
    725   controller->Animate(kInitialTickTime + duration);
    726   controller->UpdateState(true, NULL);
    727   EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
    728   EXPECT_FALSE(controller->HasActiveAnimation());
    729 }
    730 
    731 // Ensure that when the impl controller doesn't have a value provider,
    732 // the main-thread controller's value provider is used to obtain the intial
    733 // scroll offset.
    734 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
    735   FakeLayerAnimationValueObserver dummy_impl;
    736   scoped_refptr<LayerAnimationController> controller_impl(
    737       LayerAnimationController::Create(0));
    738   controller_impl->AddValueObserver(&dummy_impl);
    739   scoped_ptr<AnimationEventsVector> events(
    740       make_scoped_ptr(new AnimationEventsVector));
    741   FakeLayerAnimationValueObserver dummy;
    742   FakeLayerAnimationValueProvider dummy_provider;
    743   scoped_refptr<LayerAnimationController> controller(
    744       LayerAnimationController::Create(0));
    745   controller->AddValueObserver(&dummy);
    746   controller->set_value_provider(&dummy_provider);
    747 
    748   gfx::Vector2dF initial_value(500.f, 100.f);
    749   gfx::Vector2dF target_value(300.f, 200.f);
    750   scoped_ptr<ScrollOffsetAnimationCurve> curve(
    751       ScrollOffsetAnimationCurve::Create(
    752           target_value,
    753           EaseInOutTimingFunction::Create().Pass()));
    754 
    755   scoped_ptr<Animation> animation(Animation::Create(
    756       curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
    757   animation->set_needs_synchronized_start_time(true);
    758   controller->AddAnimation(animation.Pass());
    759 
    760   dummy_provider.set_scroll_offset(initial_value);
    761   controller->PushAnimationUpdatesTo(controller_impl.get());
    762   controller_impl->ActivateAnimations();
    763   EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
    764   double duration_in_seconds =
    765       controller_impl->GetAnimation(Animation::ScrollOffset)
    766           ->curve()
    767           ->Duration();
    768   EXPECT_EQ(
    769       duration_in_seconds,
    770       controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
    771 
    772   controller->Animate(kInitialTickTime);
    773   controller->UpdateState(true, NULL);
    774   EXPECT_TRUE(controller->HasActiveAnimation());
    775   EXPECT_EQ(initial_value, dummy.scroll_offset());
    776 
    777   controller_impl->Animate(kInitialTickTime);
    778   controller_impl->UpdateState(true, events.get());
    779   EXPECT_TRUE(controller_impl->HasActiveAnimation());
    780   EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
    781   // Scroll offset animations should not generate property updates.
    782   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
    783   EXPECT_FALSE(event);
    784 
    785   TimeDelta duration = TimeDelta::FromMicroseconds(
    786       duration_in_seconds * base::Time::kMicrosecondsPerSecond);
    787 
    788   controller->NotifyAnimationStarted((*events)[0]);
    789   controller->Animate(kInitialTickTime + duration / 2);
    790   controller->UpdateState(true, NULL);
    791   EXPECT_TRUE(controller->HasActiveAnimation());
    792   EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
    793 
    794   controller_impl->Animate(kInitialTickTime + duration / 2);
    795   controller_impl->UpdateState(true, events.get());
    796   EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
    797                       dummy_impl.scroll_offset());
    798   event = GetMostRecentPropertyUpdateEvent(events.get());
    799   EXPECT_FALSE(event);
    800 
    801   controller_impl->Animate(kInitialTickTime + duration);
    802   controller_impl->UpdateState(true, events.get());
    803   EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
    804   EXPECT_FALSE(controller_impl->HasActiveAnimation());
    805   event = GetMostRecentPropertyUpdateEvent(events.get());
    806   EXPECT_FALSE(event);
    807 
    808   controller->Animate(kInitialTickTime + duration);
    809   controller->UpdateState(true, NULL);
    810   EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
    811   EXPECT_FALSE(controller->HasActiveAnimation());
    812 }
    813 
    814 TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
    815   FakeLayerAnimationValueObserver dummy_impl;
    816   scoped_refptr<LayerAnimationController> controller_impl(
    817       LayerAnimationController::Create(0));
    818   controller_impl->AddValueObserver(&dummy_impl);
    819   scoped_ptr<AnimationEventsVector> events(
    820       make_scoped_ptr(new AnimationEventsVector));
    821 
    822   gfx::Vector2dF initial_value(100.f, 300.f);
    823   gfx::Vector2dF target_value(300.f, 200.f);
    824   scoped_ptr<ScrollOffsetAnimationCurve> curve(
    825       ScrollOffsetAnimationCurve::Create(
    826           target_value,
    827           EaseInOutTimingFunction::Create().Pass()));
    828   curve->SetInitialValue(initial_value);
    829   double duration_in_seconds = curve->Duration();
    830 
    831   scoped_ptr<Animation> animation(Animation::Create(
    832       curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
    833   animation->set_is_impl_only(true);
    834   controller_impl->AddAnimation(animation.Pass());
    835 
    836   controller_impl->Animate(kInitialTickTime);
    837   controller_impl->UpdateState(true, events.get());
    838   EXPECT_TRUE(controller_impl->HasActiveAnimation());
    839   EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
    840   // Scroll offset animations should not generate property updates.
    841   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
    842   EXPECT_FALSE(event);
    843 
    844   TimeDelta duration = TimeDelta::FromMicroseconds(
    845       duration_in_seconds * base::Time::kMicrosecondsPerSecond);
    846 
    847   controller_impl->Animate(kInitialTickTime + duration / 2);
    848   controller_impl->UpdateState(true, events.get());
    849   EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
    850                       dummy_impl.scroll_offset());
    851   event = GetMostRecentPropertyUpdateEvent(events.get());
    852   EXPECT_FALSE(event);
    853 
    854   controller_impl->Animate(kInitialTickTime + duration);
    855   controller_impl->UpdateState(true, events.get());
    856   EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
    857   EXPECT_FALSE(controller_impl->HasActiveAnimation());
    858   event = GetMostRecentPropertyUpdateEvent(events.get());
    859   EXPECT_FALSE(event);
    860 }
    861 
    862 class FakeAnimationDelegate : public AnimationDelegate {
    863  public:
    864   FakeAnimationDelegate()
    865       : started_(false),
    866         finished_(false) {}
    867 
    868   virtual void NotifyAnimationStarted(
    869       TimeTicks monotonic_time,
    870       Animation::TargetProperty target_property) OVERRIDE {
    871     started_ = true;
    872   }
    873 
    874   virtual void NotifyAnimationFinished(
    875       TimeTicks monotonic_time,
    876       Animation::TargetProperty target_property) OVERRIDE {
    877     finished_ = true;
    878   }
    879 
    880   bool started() { return started_; }
    881 
    882   bool finished() { return finished_; }
    883 
    884  private:
    885   bool started_;
    886   bool finished_;
    887 };
    888 
    889 // Tests that impl-only animations lead to start and finished notifications
    890 // being sent to the main thread controller's animation delegate.
    891 TEST(LayerAnimationControllerTest,
    892      NotificationsForImplOnlyAnimationsAreSentToMainThreadDelegate) {
    893   FakeLayerAnimationValueObserver dummy_impl;
    894   scoped_refptr<LayerAnimationController> controller_impl(
    895       LayerAnimationController::Create(0));
    896   controller_impl->AddValueObserver(&dummy_impl);
    897   scoped_ptr<AnimationEventsVector> events(
    898       make_scoped_ptr(new AnimationEventsVector));
    899   FakeLayerAnimationValueObserver dummy;
    900   scoped_refptr<LayerAnimationController> controller(
    901       LayerAnimationController::Create(0));
    902   controller->AddValueObserver(&dummy);
    903   FakeAnimationDelegate delegate;
    904   controller->set_layer_animation_delegate(&delegate);
    905 
    906   scoped_ptr<Animation> to_add(CreateAnimation(
    907       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
    908       1,
    909       Animation::Opacity));
    910   to_add->set_is_impl_only(true);
    911   controller_impl->AddAnimation(to_add.Pass());
    912 
    913   controller_impl->Animate(kInitialTickTime);
    914   controller_impl->UpdateState(true, events.get());
    915 
    916   // We should receive 2 events (a started notification and a property update).
    917   EXPECT_EQ(2u, events->size());
    918   EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
    919   EXPECT_TRUE((*events)[0].is_impl_only);
    920   EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type);
    921   EXPECT_TRUE((*events)[1].is_impl_only);
    922 
    923   // Passing on the start event to the main thread controller should cause the
    924   // delegate to get notified.
    925   EXPECT_FALSE(delegate.started());
    926   controller->NotifyAnimationStarted((*events)[0]);
    927   EXPECT_TRUE(delegate.started());
    928 
    929   events.reset(new AnimationEventsVector);
    930   controller_impl->Animate(kInitialTickTime +
    931                            TimeDelta::FromMilliseconds(1000));
    932   controller_impl->UpdateState(true, events.get());
    933 
    934   // We should receive 2 events (a finished notification and a property update).
    935   EXPECT_EQ(2u, events->size());
    936   EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
    937   EXPECT_TRUE((*events)[0].is_impl_only);
    938   EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type);
    939   EXPECT_TRUE((*events)[1].is_impl_only);
    940 
    941   // Passing on the finished event to the main thread controller should cause
    942   // the delegate to get notified.
    943   EXPECT_FALSE(delegate.finished());
    944   controller->NotifyAnimationFinished((*events)[0]);
    945   EXPECT_TRUE(delegate.finished());
    946 }
    947 
    948 // Tests animations that are waiting for a synchronized start time do not
    949 // finish.
    950 TEST(LayerAnimationControllerTest,
    951      AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
    952   scoped_ptr<AnimationEventsVector> events(
    953       make_scoped_ptr(new AnimationEventsVector));
    954   FakeLayerAnimationValueObserver dummy;
    955   scoped_refptr<LayerAnimationController> controller(
    956       LayerAnimationController::Create(0));
    957   controller->AddValueObserver(&dummy);
    958 
    959   scoped_ptr<Animation> to_add(CreateAnimation(
    960       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
    961       1,
    962       Animation::Opacity));
    963   to_add->set_needs_synchronized_start_time(true);
    964 
    965   // We should pause at the first keyframe indefinitely waiting for that
    966   // animation to start.
    967   controller->AddAnimation(to_add.Pass());
    968   controller->Animate(kInitialTickTime);
    969   controller->UpdateState(true, events.get());
    970   EXPECT_TRUE(controller->HasActiveAnimation());
    971   EXPECT_EQ(0.f, dummy.opacity());
    972   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
    973   controller->UpdateState(true, events.get());
    974   EXPECT_TRUE(controller->HasActiveAnimation());
    975   EXPECT_EQ(0.f, dummy.opacity());
    976   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
    977   controller->UpdateState(true, events.get());
    978   EXPECT_TRUE(controller->HasActiveAnimation());
    979   EXPECT_EQ(0.f, dummy.opacity());
    980 
    981   // Send the synchronized start time.
    982   controller->NotifyAnimationStarted(
    983       AnimationEvent(AnimationEvent::Started,
    984                      0,
    985                      1,
    986                      Animation::Opacity,
    987                      kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
    988   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
    989   controller->UpdateState(true, events.get());
    990   EXPECT_EQ(1.f, dummy.opacity());
    991   EXPECT_FALSE(controller->HasActiveAnimation());
    992 }
    993 
    994 // Tests that two queued animations affecting the same property run in sequence.
    995 TEST(LayerAnimationControllerTest, TrivialQueuing) {
    996   scoped_ptr<AnimationEventsVector> events(
    997       make_scoped_ptr(new AnimationEventsVector));
    998   FakeLayerAnimationValueObserver dummy;
    999   scoped_refptr<LayerAnimationController> controller(
   1000       LayerAnimationController::Create(0));
   1001   controller->AddValueObserver(&dummy);
   1002 
   1003   EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
   1004 
   1005   controller->AddAnimation(CreateAnimation(
   1006       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1007       1,
   1008       Animation::Opacity));
   1009   controller->AddAnimation(CreateAnimation(
   1010       scoped_ptr<AnimationCurve>(
   1011           new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
   1012       2,
   1013       Animation::Opacity));
   1014 
   1015   EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
   1016 
   1017   controller->Animate(kInitialTickTime);
   1018 
   1019   // The second animation still needs to be started.
   1020   EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
   1021 
   1022   controller->UpdateState(true, events.get());
   1023   EXPECT_TRUE(controller->HasActiveAnimation());
   1024   EXPECT_EQ(0.f, dummy.opacity());
   1025 
   1026   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1027   EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
   1028   controller->UpdateState(true, events.get());
   1029   EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
   1030 
   1031   EXPECT_TRUE(controller->HasActiveAnimation());
   1032   EXPECT_EQ(1.f, dummy.opacity());
   1033   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
   1034   controller->UpdateState(true, events.get());
   1035   EXPECT_EQ(0.5f, dummy.opacity());
   1036   EXPECT_FALSE(controller->HasActiveAnimation());
   1037 }
   1038 
   1039 // Tests interrupting a transition with another transition.
   1040 TEST(LayerAnimationControllerTest, Interrupt) {
   1041   scoped_ptr<AnimationEventsVector> events(
   1042       make_scoped_ptr(new AnimationEventsVector));
   1043   FakeLayerAnimationValueObserver dummy;
   1044   scoped_refptr<LayerAnimationController> controller(
   1045       LayerAnimationController::Create(0));
   1046   controller->AddValueObserver(&dummy);
   1047   controller->AddAnimation(CreateAnimation(
   1048       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1049       1,
   1050       Animation::Opacity));
   1051   controller->Animate(kInitialTickTime);
   1052   controller->UpdateState(true, events.get());
   1053   EXPECT_TRUE(controller->HasActiveAnimation());
   1054   EXPECT_EQ(0.f, dummy.opacity());
   1055 
   1056   scoped_ptr<Animation> to_add(CreateAnimation(
   1057       scoped_ptr<AnimationCurve>(
   1058           new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
   1059       2,
   1060       Animation::Opacity));
   1061   controller->AbortAnimations(Animation::Opacity);
   1062   controller->AddAnimation(to_add.Pass());
   1063 
   1064   // Since the previous animation was aborted, the new animation should start
   1065   // right in this call to animate.
   1066   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
   1067   controller->UpdateState(true, events.get());
   1068   EXPECT_TRUE(controller->HasActiveAnimation());
   1069   EXPECT_EQ(1.f, dummy.opacity());
   1070   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
   1071   controller->UpdateState(true, events.get());
   1072   EXPECT_EQ(0.5f, dummy.opacity());
   1073   EXPECT_FALSE(controller->HasActiveAnimation());
   1074 }
   1075 
   1076 // Tests scheduling two animations to run together when only one property is
   1077 // free.
   1078 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
   1079   scoped_ptr<AnimationEventsVector> events(
   1080       make_scoped_ptr(new AnimationEventsVector));
   1081   FakeLayerAnimationValueObserver dummy;
   1082   scoped_refptr<LayerAnimationController> controller(
   1083       LayerAnimationController::Create(0));
   1084   controller->AddValueObserver(&dummy);
   1085 
   1086   controller->AddAnimation(CreateAnimation(
   1087       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
   1088       1,
   1089       Animation::Transform));
   1090   controller->AddAnimation(CreateAnimation(
   1091       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
   1092       2,
   1093       Animation::Transform));
   1094   controller->AddAnimation(CreateAnimation(
   1095       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1096       2,
   1097       Animation::Opacity));
   1098 
   1099   controller->Animate(kInitialTickTime);
   1100   controller->UpdateState(true, events.get());
   1101   EXPECT_EQ(0.f, dummy.opacity());
   1102   EXPECT_TRUE(controller->HasActiveAnimation());
   1103   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1104   controller->UpdateState(true, events.get());
   1105   // Should not have started the float transition yet.
   1106   EXPECT_TRUE(controller->HasActiveAnimation());
   1107   EXPECT_EQ(0.f, dummy.opacity());
   1108   // The float animation should have started at time 1 and should be done.
   1109   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
   1110   controller->UpdateState(true, events.get());
   1111   EXPECT_EQ(1.f, dummy.opacity());
   1112   EXPECT_FALSE(controller->HasActiveAnimation());
   1113 }
   1114 
   1115 // Tests scheduling two animations to run together with different lengths and
   1116 // another animation queued to start when the shorter animation finishes (should
   1117 // wait for both to finish).
   1118 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
   1119   scoped_ptr<AnimationEventsVector> events(
   1120       make_scoped_ptr(new AnimationEventsVector));
   1121   FakeLayerAnimationValueObserver dummy;
   1122   scoped_refptr<LayerAnimationController> controller(
   1123       LayerAnimationController::Create(0));
   1124   controller->AddValueObserver(&dummy);
   1125 
   1126   controller->AddAnimation(CreateAnimation(
   1127       scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
   1128       1,
   1129       Animation::Transform));
   1130   controller->AddAnimation(CreateAnimation(
   1131       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1132       1,
   1133       Animation::Opacity));
   1134   controller->AddAnimation(CreateAnimation(
   1135       scoped_ptr<AnimationCurve>(
   1136           new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
   1137       2,
   1138       Animation::Opacity));
   1139 
   1140   // Animations with id 1 should both start now.
   1141   controller->Animate(kInitialTickTime);
   1142   controller->UpdateState(true, events.get());
   1143   EXPECT_TRUE(controller->HasActiveAnimation());
   1144   EXPECT_EQ(0.f, dummy.opacity());
   1145   // The opacity animation should have finished at time 1, but the group
   1146   // of animations with id 1 don't finish until time 2 because of the length
   1147   // of the transform animation.
   1148   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
   1149   controller->UpdateState(true, events.get());
   1150   // Should not have started the float transition yet.
   1151   EXPECT_TRUE(controller->HasActiveAnimation());
   1152   EXPECT_EQ(1.f, dummy.opacity());
   1153 
   1154   // The second opacity animation should start at time 2 and should be done by
   1155   // time 3.
   1156   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
   1157   controller->UpdateState(true, events.get());
   1158   EXPECT_EQ(0.5f, dummy.opacity());
   1159   EXPECT_FALSE(controller->HasActiveAnimation());
   1160 }
   1161 
   1162 // Test that a looping animation loops and for the correct number of iterations.
   1163 TEST(LayerAnimationControllerTest, TrivialLooping) {
   1164   scoped_ptr<AnimationEventsVector> events(
   1165       make_scoped_ptr(new AnimationEventsVector));
   1166   FakeLayerAnimationValueObserver dummy;
   1167   scoped_refptr<LayerAnimationController> controller(
   1168       LayerAnimationController::Create(0));
   1169   controller->AddValueObserver(&dummy);
   1170 
   1171   scoped_ptr<Animation> to_add(CreateAnimation(
   1172       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1173       1,
   1174       Animation::Opacity));
   1175   to_add->set_iterations(3);
   1176   controller->AddAnimation(to_add.Pass());
   1177 
   1178   controller->Animate(kInitialTickTime);
   1179   controller->UpdateState(true, events.get());
   1180   EXPECT_TRUE(controller->HasActiveAnimation());
   1181   EXPECT_EQ(0.f, dummy.opacity());
   1182   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
   1183   controller->UpdateState(true, events.get());
   1184   EXPECT_TRUE(controller->HasActiveAnimation());
   1185   EXPECT_EQ(0.25f, dummy.opacity());
   1186   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
   1187   controller->UpdateState(true, events.get());
   1188   EXPECT_TRUE(controller->HasActiveAnimation());
   1189   EXPECT_EQ(0.75f, dummy.opacity());
   1190   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
   1191   controller->UpdateState(true, events.get());
   1192   EXPECT_TRUE(controller->HasActiveAnimation());
   1193   EXPECT_EQ(0.25f, dummy.opacity());
   1194   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
   1195   controller->UpdateState(true, events.get());
   1196   EXPECT_TRUE(controller->HasActiveAnimation());
   1197   EXPECT_EQ(0.75f, dummy.opacity());
   1198   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
   1199   controller->UpdateState(true, events.get());
   1200   EXPECT_FALSE(controller->HasActiveAnimation());
   1201   EXPECT_EQ(1.f, dummy.opacity());
   1202 
   1203   // Just be extra sure.
   1204   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
   1205   controller->UpdateState(true, events.get());
   1206   EXPECT_EQ(1.f, dummy.opacity());
   1207 }
   1208 
   1209 // Test that an infinitely looping animation does indeed go until aborted.
   1210 TEST(LayerAnimationControllerTest, InfiniteLooping) {
   1211   scoped_ptr<AnimationEventsVector> events(
   1212       make_scoped_ptr(new AnimationEventsVector));
   1213   FakeLayerAnimationValueObserver dummy;
   1214   scoped_refptr<LayerAnimationController> controller(
   1215       LayerAnimationController::Create(0));
   1216   controller->AddValueObserver(&dummy);
   1217 
   1218   const int id = 1;
   1219   scoped_ptr<Animation> to_add(CreateAnimation(
   1220       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1221       id,
   1222       Animation::Opacity));
   1223   to_add->set_iterations(-1);
   1224   controller->AddAnimation(to_add.Pass());
   1225 
   1226   controller->Animate(kInitialTickTime);
   1227   controller->UpdateState(true, events.get());
   1228   EXPECT_TRUE(controller->HasActiveAnimation());
   1229   EXPECT_EQ(0.f, dummy.opacity());
   1230   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
   1231   controller->UpdateState(true, events.get());
   1232   EXPECT_TRUE(controller->HasActiveAnimation());
   1233   EXPECT_EQ(0.25f, dummy.opacity());
   1234   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
   1235   controller->UpdateState(true, events.get());
   1236   EXPECT_TRUE(controller->HasActiveAnimation());
   1237   EXPECT_EQ(0.75f, dummy.opacity());
   1238 
   1239   controller->Animate(kInitialTickTime +
   1240                       TimeDelta::FromMilliseconds(1073741824250));
   1241   controller->UpdateState(true, events.get());
   1242   EXPECT_TRUE(controller->HasActiveAnimation());
   1243   EXPECT_EQ(0.25f, dummy.opacity());
   1244   controller->Animate(kInitialTickTime +
   1245                       TimeDelta::FromMilliseconds(1073741824750));
   1246   controller->UpdateState(true, events.get());
   1247   EXPECT_TRUE(controller->HasActiveAnimation());
   1248   EXPECT_EQ(0.75f, dummy.opacity());
   1249 
   1250   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
   1251   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
   1252       Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(750));
   1253   EXPECT_FALSE(controller->HasActiveAnimation());
   1254   EXPECT_EQ(0.75f, dummy.opacity());
   1255 }
   1256 
   1257 // Test that pausing and resuming work as expected.
   1258 TEST(LayerAnimationControllerTest, PauseResume) {
   1259   scoped_ptr<AnimationEventsVector> events(
   1260       make_scoped_ptr(new AnimationEventsVector));
   1261   FakeLayerAnimationValueObserver dummy;
   1262   scoped_refptr<LayerAnimationController> controller(
   1263       LayerAnimationController::Create(0));
   1264   controller->AddValueObserver(&dummy);
   1265 
   1266   const int id = 1;
   1267   controller->AddAnimation(CreateAnimation(
   1268       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1269       id,
   1270       Animation::Opacity));
   1271 
   1272   controller->Animate(kInitialTickTime);
   1273   controller->UpdateState(true, events.get());
   1274   EXPECT_TRUE(controller->HasActiveAnimation());
   1275   EXPECT_EQ(0.f, dummy.opacity());
   1276   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
   1277   controller->UpdateState(true, events.get());
   1278   EXPECT_TRUE(controller->HasActiveAnimation());
   1279   EXPECT_EQ(0.5f, dummy.opacity());
   1280 
   1281   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
   1282   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
   1283       Animation::Paused, kInitialTickTime + TimeDelta::FromMilliseconds(500));
   1284 
   1285   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
   1286   controller->UpdateState(true, events.get());
   1287   EXPECT_TRUE(controller->HasActiveAnimation());
   1288   EXPECT_EQ(0.5f, dummy.opacity());
   1289 
   1290   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
   1291   controller->GetAnimation(id, Animation::Opacity)
   1292       ->SetRunState(Animation::Running,
   1293                     kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
   1294   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
   1295   controller->UpdateState(true, events.get());
   1296   EXPECT_TRUE(controller->HasActiveAnimation());
   1297   EXPECT_EQ(0.75f, dummy.opacity());
   1298 
   1299   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
   1300   controller->UpdateState(true, events.get());
   1301   EXPECT_FALSE(controller->HasActiveAnimation());
   1302   EXPECT_EQ(1.f, dummy.opacity());
   1303 }
   1304 
   1305 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
   1306   scoped_ptr<AnimationEventsVector> events(
   1307       make_scoped_ptr(new AnimationEventsVector));
   1308   FakeLayerAnimationValueObserver dummy;
   1309   scoped_refptr<LayerAnimationController> controller(
   1310       LayerAnimationController::Create(0));
   1311   controller->AddValueObserver(&dummy);
   1312 
   1313   const int id = 1;
   1314   controller->AddAnimation(CreateAnimation(
   1315       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
   1316       id,
   1317       Animation::Transform));
   1318   controller->AddAnimation(CreateAnimation(
   1319       scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
   1320       id,
   1321       Animation::Opacity));
   1322   controller->AddAnimation(CreateAnimation(
   1323       scoped_ptr<AnimationCurve>(
   1324           new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
   1325       2,
   1326       Animation::Opacity));
   1327 
   1328   controller->Animate(kInitialTickTime);
   1329   controller->UpdateState(true, events.get());
   1330   EXPECT_TRUE(controller->HasActiveAnimation());
   1331   EXPECT_EQ(0.f, dummy.opacity());
   1332   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1333   controller->UpdateState(true, events.get());
   1334   EXPECT_TRUE(controller->HasActiveAnimation());
   1335   EXPECT_EQ(0.5f, dummy.opacity());
   1336 
   1337   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
   1338   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
   1339       Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1340   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1341   controller->UpdateState(true, events.get());
   1342   EXPECT_TRUE(controller->HasActiveAnimation());
   1343   EXPECT_EQ(1.f, dummy.opacity());
   1344   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
   1345   controller->UpdateState(true, events.get());
   1346   EXPECT_TRUE(!controller->HasActiveAnimation());
   1347   EXPECT_EQ(0.75f, dummy.opacity());
   1348 }
   1349 
   1350 TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
   1351   FakeLayerAnimationValueObserver dummy_impl;
   1352   scoped_refptr<LayerAnimationController> controller_impl(
   1353       LayerAnimationController::Create(0));
   1354   controller_impl->AddValueObserver(&dummy_impl);
   1355   scoped_ptr<AnimationEventsVector> events(
   1356       make_scoped_ptr(new AnimationEventsVector));
   1357   FakeLayerAnimationValueObserver dummy;
   1358   scoped_refptr<LayerAnimationController> controller(
   1359       LayerAnimationController::Create(0));
   1360   controller->AddValueObserver(&dummy);
   1361 
   1362   scoped_ptr<Animation> to_add(CreateAnimation(
   1363       scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
   1364       0,
   1365       Animation::Opacity));
   1366   to_add->set_needs_synchronized_start_time(true);
   1367   controller->AddAnimation(to_add.Pass());
   1368 
   1369   controller->Animate(kInitialTickTime);
   1370   controller->UpdateState(true, events.get());
   1371   EXPECT_TRUE(controller->HasActiveAnimation());
   1372   Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
   1373   EXPECT_TRUE(active_animation);
   1374   EXPECT_TRUE(active_animation->needs_synchronized_start_time());
   1375 
   1376   controller->PushAnimationUpdatesTo(controller_impl.get());
   1377   controller_impl->ActivateAnimations();
   1378 
   1379   active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
   1380   EXPECT_TRUE(active_animation);
   1381   EXPECT_EQ(Animation::WaitingForTargetAvailability,
   1382             active_animation->run_state());
   1383 }
   1384 
   1385 // Tests that skipping a call to UpdateState works as expected.
   1386 TEST(LayerAnimationControllerTest, SkipUpdateState) {
   1387   scoped_ptr<AnimationEventsVector> events(
   1388       make_scoped_ptr(new AnimationEventsVector));
   1389   FakeLayerAnimationValueObserver dummy;
   1390   scoped_refptr<LayerAnimationController> controller(
   1391       LayerAnimationController::Create(0));
   1392   controller->AddValueObserver(&dummy);
   1393 
   1394   controller->AddAnimation(CreateAnimation(
   1395       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
   1396       1,
   1397       Animation::Transform));
   1398 
   1399   controller->Animate(kInitialTickTime);
   1400   controller->UpdateState(true, events.get());
   1401 
   1402   controller->AddAnimation(CreateAnimation(
   1403       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1404       2,
   1405       Animation::Opacity));
   1406 
   1407   // Animate but don't UpdateState.
   1408   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1409 
   1410   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
   1411   events.reset(new AnimationEventsVector);
   1412   controller->UpdateState(true, events.get());
   1413 
   1414   // Should have one Started event and one Finished event.
   1415   EXPECT_EQ(2u, events->size());
   1416   EXPECT_NE((*events)[0].type, (*events)[1].type);
   1417 
   1418   // The float transition should still be at its starting point.
   1419   EXPECT_TRUE(controller->HasActiveAnimation());
   1420   EXPECT_EQ(0.f, dummy.opacity());
   1421 
   1422   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
   1423   controller->UpdateState(true, events.get());
   1424 
   1425   // The float tranisition should now be done.
   1426   EXPECT_EQ(1.f, dummy.opacity());
   1427   EXPECT_FALSE(controller->HasActiveAnimation());
   1428 }
   1429 
   1430 // Tests that an animation controller with only a pending observer gets ticked
   1431 // but doesn't progress animations past the Starting state.
   1432 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
   1433   scoped_ptr<AnimationEventsVector> events(
   1434       make_scoped_ptr(new AnimationEventsVector));
   1435   FakeLayerAnimationValueObserver dummy;
   1436   FakeInactiveLayerAnimationValueObserver pending_dummy;
   1437   scoped_refptr<LayerAnimationController> controller(
   1438       LayerAnimationController::Create(0));
   1439 
   1440   const int id = 1;
   1441   controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>(
   1442       new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(),
   1443       id,
   1444       Animation::Opacity));
   1445 
   1446   // Without an observer, the animation shouldn't progress to the Starting
   1447   // state.
   1448   controller->Animate(kInitialTickTime);
   1449   controller->UpdateState(true, events.get());
   1450   EXPECT_EQ(0u, events->size());
   1451   EXPECT_EQ(Animation::WaitingForTargetAvailability,
   1452             controller->GetAnimation(id, Animation::Opacity)->run_state());
   1453 
   1454   controller->AddValueObserver(&pending_dummy);
   1455 
   1456   // With only a pending observer, the animation should progress to the
   1457   // Starting state and get ticked at its starting point, but should not
   1458   // progress to Running.
   1459   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1460   controller->UpdateState(true, events.get());
   1461   EXPECT_EQ(0u, events->size());
   1462   EXPECT_EQ(Animation::Starting,
   1463             controller->GetAnimation(id, Animation::Opacity)->run_state());
   1464   EXPECT_EQ(0.5f, pending_dummy.opacity());
   1465 
   1466   // Even when already in the Starting state, the animation should stay
   1467   // there, and shouldn't be ticked past its starting point.
   1468   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
   1469   controller->UpdateState(true, events.get());
   1470   EXPECT_EQ(0u, events->size());
   1471   EXPECT_EQ(Animation::Starting,
   1472             controller->GetAnimation(id, Animation::Opacity)->run_state());
   1473   EXPECT_EQ(0.5f, pending_dummy.opacity());
   1474 
   1475   controller->AddValueObserver(&dummy);
   1476 
   1477   // Now that an active observer has been added, the animation should still
   1478   // initially tick at its starting point, but should now progress to Running.
   1479   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
   1480   controller->UpdateState(true, events.get());
   1481   EXPECT_EQ(1u, events->size());
   1482   EXPECT_EQ(Animation::Running,
   1483             controller->GetAnimation(id, Animation::Opacity)->run_state());
   1484   EXPECT_EQ(0.5f, pending_dummy.opacity());
   1485   EXPECT_EQ(0.5f, dummy.opacity());
   1486 
   1487   // The animation should now tick past its starting point.
   1488   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
   1489   EXPECT_NE(0.5f, pending_dummy.opacity());
   1490   EXPECT_NE(0.5f, dummy.opacity());
   1491 }
   1492 
   1493 TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
   1494   scoped_refptr<LayerAnimationController> controller_impl(
   1495       LayerAnimationController::Create(0));
   1496 
   1497   scoped_ptr<KeyframedTransformAnimationCurve> curve1(
   1498       KeyframedTransformAnimationCurve::Create());
   1499 
   1500   TransformOperations operations1;
   1501   curve1->AddKeyframe(TransformKeyframe::Create(
   1502       0.0, operations1, scoped_ptr<TimingFunction>()));
   1503   operations1.AppendTranslate(10.0, 15.0, 0.0);
   1504   curve1->AddKeyframe(TransformKeyframe::Create(
   1505       1.0, operations1, scoped_ptr<TimingFunction>()));
   1506 
   1507   scoped_ptr<Animation> animation(Animation::Create(
   1508       curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
   1509   controller_impl->AddAnimation(animation.Pass());
   1510 
   1511   scoped_ptr<KeyframedTransformAnimationCurve> curve2(
   1512       KeyframedTransformAnimationCurve::Create());
   1513 
   1514   TransformOperations operations2;
   1515   curve2->AddKeyframe(TransformKeyframe::Create(
   1516       0.0, operations2, scoped_ptr<TimingFunction>()));
   1517   operations2.AppendScale(2.0, 3.0, 4.0);
   1518   curve2->AddKeyframe(TransformKeyframe::Create(
   1519       1.0, operations2, scoped_ptr<TimingFunction>()));
   1520 
   1521   animation = Animation::Create(
   1522       curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
   1523   controller_impl->AddAnimation(animation.Pass());
   1524 
   1525   gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
   1526   gfx::BoxF bounds;
   1527 
   1528   EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
   1529   EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
   1530             bounds.ToString());
   1531 
   1532   controller_impl->GetAnimation(1, Animation::Transform)
   1533       ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
   1534 
   1535   // Only the unfinished animation should affect the animated bounds.
   1536   EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
   1537   EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
   1538             bounds.ToString());
   1539 
   1540   controller_impl->GetAnimation(2, Animation::Transform)
   1541       ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
   1542 
   1543   // There are no longer any running animations.
   1544   EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
   1545 
   1546   // Add an animation whose bounds we don't yet support computing.
   1547   scoped_ptr<KeyframedTransformAnimationCurve> curve3(
   1548       KeyframedTransformAnimationCurve::Create());
   1549   TransformOperations operations3;
   1550   gfx::Transform transform3;
   1551   transform3.Scale3d(1.0, 2.0, 3.0);
   1552   curve3->AddKeyframe(TransformKeyframe::Create(
   1553       0.0, operations3, scoped_ptr<TimingFunction>()));
   1554   operations3.AppendMatrix(transform3);
   1555   curve3->AddKeyframe(TransformKeyframe::Create(
   1556       1.0, operations3, scoped_ptr<TimingFunction>()));
   1557   animation = Animation::Create(
   1558       curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
   1559   controller_impl->AddAnimation(animation.Pass());
   1560   EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
   1561 }
   1562 
   1563 // Tests that AbortAnimations aborts all animations targeting the specified
   1564 // property.
   1565 TEST(LayerAnimationControllerTest, AbortAnimations) {
   1566   FakeLayerAnimationValueObserver dummy;
   1567   scoped_refptr<LayerAnimationController> controller(
   1568       LayerAnimationController::Create(0));
   1569   controller->AddValueObserver(&dummy);
   1570 
   1571   // Start with several animations, and allow some of them to reach the finished
   1572   // state.
   1573   controller->AddAnimation(CreateAnimation(
   1574       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
   1575       1,
   1576       Animation::Transform));
   1577   controller->AddAnimation(CreateAnimation(
   1578       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1579       2,
   1580       Animation::Opacity));
   1581   controller->AddAnimation(CreateAnimation(
   1582       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
   1583       3,
   1584       Animation::Transform));
   1585   controller->AddAnimation(CreateAnimation(
   1586       scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
   1587       4,
   1588       Animation::Transform));
   1589   controller->AddAnimation(CreateAnimation(
   1590       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1591       5,
   1592       Animation::Opacity));
   1593 
   1594   controller->Animate(kInitialTickTime);
   1595   controller->UpdateState(true, NULL);
   1596   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
   1597   controller->UpdateState(true, NULL);
   1598 
   1599   EXPECT_EQ(Animation::Finished,
   1600             controller->GetAnimation(1, Animation::Transform)->run_state());
   1601   EXPECT_EQ(Animation::Finished,
   1602             controller->GetAnimation(2, Animation::Opacity)->run_state());
   1603   EXPECT_EQ(Animation::Running,
   1604             controller->GetAnimation(3, Animation::Transform)->run_state());
   1605   EXPECT_EQ(Animation::WaitingForTargetAvailability,
   1606             controller->GetAnimation(4, Animation::Transform)->run_state());
   1607   EXPECT_EQ(Animation::Running,
   1608             controller->GetAnimation(5, Animation::Opacity)->run_state());
   1609 
   1610   controller->AbortAnimations(Animation::Transform);
   1611 
   1612   // Only un-finished Transform animations should have been aborted.
   1613   EXPECT_EQ(Animation::Finished,
   1614             controller->GetAnimation(1, Animation::Transform)->run_state());
   1615   EXPECT_EQ(Animation::Finished,
   1616             controller->GetAnimation(2, Animation::Opacity)->run_state());
   1617   EXPECT_EQ(Animation::Aborted,
   1618             controller->GetAnimation(3, Animation::Transform)->run_state());
   1619   EXPECT_EQ(Animation::Aborted,
   1620             controller->GetAnimation(4, Animation::Transform)->run_state());
   1621   EXPECT_EQ(Animation::Running,
   1622             controller->GetAnimation(5, Animation::Opacity)->run_state());
   1623 }
   1624 
   1625 // An animation aborted on the main thread should get deleted on both threads.
   1626 TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
   1627   FakeLayerAnimationValueObserver dummy_impl;
   1628   scoped_refptr<LayerAnimationController> controller_impl(
   1629       LayerAnimationController::Create(0));
   1630   controller_impl->AddValueObserver(&dummy_impl);
   1631   FakeLayerAnimationValueObserver dummy;
   1632   scoped_refptr<LayerAnimationController> controller(
   1633       LayerAnimationController::Create(0));
   1634   controller->AddValueObserver(&dummy);
   1635 
   1636   AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
   1637   int group_id = controller->GetAnimation(Animation::Opacity)->group();
   1638 
   1639   controller->PushAnimationUpdatesTo(controller_impl.get());
   1640   controller_impl->ActivateAnimations();
   1641   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
   1642 
   1643   controller->AbortAnimations(Animation::Opacity);
   1644   EXPECT_EQ(Animation::Aborted,
   1645             controller->GetAnimation(Animation::Opacity)->run_state());
   1646   EXPECT_FALSE(dummy.animation_waiting_for_deletion());
   1647   EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
   1648 
   1649   controller->Animate(kInitialTickTime);
   1650   controller->UpdateState(true, NULL);
   1651   EXPECT_TRUE(dummy.animation_waiting_for_deletion());
   1652   EXPECT_EQ(Animation::WaitingForDeletion,
   1653             controller->GetAnimation(Animation::Opacity)->run_state());
   1654 
   1655   controller->PushAnimationUpdatesTo(controller_impl.get());
   1656   controller_impl->ActivateAnimations();
   1657   EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
   1658   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
   1659 }
   1660 
   1661 // An animation aborted on the impl thread should get deleted on both threads.
   1662 TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
   1663   FakeLayerAnimationValueObserver dummy_impl;
   1664   scoped_refptr<LayerAnimationController> controller_impl(
   1665       LayerAnimationController::Create(0));
   1666   controller_impl->AddValueObserver(&dummy_impl);
   1667   FakeLayerAnimationValueObserver dummy;
   1668   scoped_refptr<LayerAnimationController> controller(
   1669       LayerAnimationController::Create(0));
   1670   controller->AddValueObserver(&dummy);
   1671 
   1672   AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
   1673   int group_id = controller->GetAnimation(Animation::Opacity)->group();
   1674 
   1675   controller->PushAnimationUpdatesTo(controller_impl.get());
   1676   controller_impl->ActivateAnimations();
   1677   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
   1678 
   1679   controller_impl->AbortAnimations(Animation::Opacity);
   1680   EXPECT_EQ(Animation::Aborted,
   1681             controller_impl->GetAnimation(Animation::Opacity)->run_state());
   1682   EXPECT_FALSE(dummy.animation_waiting_for_deletion());
   1683   EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
   1684 
   1685   AnimationEventsVector events;
   1686   controller_impl->Animate(kInitialTickTime);
   1687   controller_impl->UpdateState(true, &events);
   1688   EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
   1689   EXPECT_EQ(1u, events.size());
   1690   EXPECT_EQ(AnimationEvent::Aborted, events[0].type);
   1691   EXPECT_EQ(Animation::WaitingForDeletion,
   1692             controller_impl->GetAnimation(Animation::Opacity)->run_state());
   1693 
   1694   controller->NotifyAnimationAborted(events[0]);
   1695   EXPECT_EQ(Animation::Aborted,
   1696             controller->GetAnimation(Animation::Opacity)->run_state());
   1697 
   1698   controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
   1699   controller->UpdateState(true, NULL);
   1700   EXPECT_TRUE(dummy.animation_waiting_for_deletion());
   1701   EXPECT_EQ(Animation::WaitingForDeletion,
   1702             controller->GetAnimation(Animation::Opacity)->run_state());
   1703 
   1704   controller->PushAnimationUpdatesTo(controller_impl.get());
   1705   controller_impl->ActivateAnimations();
   1706   EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
   1707   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
   1708 }
   1709 
   1710 // Ensure that we only generate Finished events for animations in a group
   1711 // once all animations in that group are finished.
   1712 TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
   1713   scoped_ptr<AnimationEventsVector> events(
   1714       make_scoped_ptr(new AnimationEventsVector));
   1715   FakeLayerAnimationValueObserver dummy_impl;
   1716   scoped_refptr<LayerAnimationController> controller_impl(
   1717       LayerAnimationController::Create(0));
   1718   controller_impl->AddValueObserver(&dummy_impl);
   1719 
   1720   // Add two animations with the same group id but different durations.
   1721   controller_impl->AddAnimation(CreateAnimation(
   1722       scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
   1723       1,
   1724       Animation::Transform));
   1725   controller_impl->AddAnimation(CreateAnimation(
   1726       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1727       1,
   1728       Animation::Opacity));
   1729 
   1730   controller_impl->Animate(kInitialTickTime);
   1731   controller_impl->UpdateState(true, events.get());
   1732 
   1733   // Both animations should have started.
   1734   EXPECT_EQ(2u, events->size());
   1735   EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
   1736   EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
   1737 
   1738   events.reset(new AnimationEventsVector);
   1739   controller_impl->Animate(kInitialTickTime +
   1740                            TimeDelta::FromMilliseconds(1000));
   1741   controller_impl->UpdateState(true, events.get());
   1742 
   1743   // The opacity animation should be finished, but should not have generated
   1744   // a Finished event yet.
   1745   EXPECT_EQ(0u, events->size());
   1746   EXPECT_EQ(Animation::Finished,
   1747             controller_impl->GetAnimation(1, Animation::Opacity)->run_state());
   1748   EXPECT_EQ(Animation::Running,
   1749             controller_impl->GetAnimation(1,
   1750                                           Animation::Transform)->run_state());
   1751 
   1752   controller_impl->Animate(kInitialTickTime +
   1753                            TimeDelta::FromMilliseconds(2000));
   1754   controller_impl->UpdateState(true, events.get());
   1755 
   1756   // Both animations should have generated Finished events.
   1757   EXPECT_EQ(2u, events->size());
   1758   EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
   1759   EXPECT_EQ(AnimationEvent::Finished, (*events)[1].type);
   1760 }
   1761 
   1762 // Ensure that when a group has a mix of aborted and finished animations,
   1763 // we generate a Finished event for the finished animation and an Aborted
   1764 // event for the aborted animation.
   1765 TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
   1766   scoped_ptr<AnimationEventsVector> events(
   1767       make_scoped_ptr(new AnimationEventsVector));
   1768   FakeLayerAnimationValueObserver dummy_impl;
   1769   scoped_refptr<LayerAnimationController> controller_impl(
   1770       LayerAnimationController::Create(0));
   1771   controller_impl->AddValueObserver(&dummy_impl);
   1772 
   1773   // Add two animations with the same group id.
   1774   controller_impl->AddAnimation(CreateAnimation(
   1775       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
   1776       1,
   1777       Animation::Transform));
   1778   controller_impl->AddAnimation(CreateAnimation(
   1779       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1780       1,
   1781       Animation::Opacity));
   1782 
   1783   controller_impl->Animate(kInitialTickTime);
   1784   controller_impl->UpdateState(true, events.get());
   1785 
   1786   // Both animations should have started.
   1787   EXPECT_EQ(2u, events->size());
   1788   EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
   1789   EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
   1790 
   1791   controller_impl->AbortAnimations(Animation::Opacity);
   1792 
   1793   events.reset(new AnimationEventsVector);
   1794   controller_impl->Animate(kInitialTickTime +
   1795                            TimeDelta::FromMilliseconds(1000));
   1796   controller_impl->UpdateState(true, events.get());
   1797 
   1798   // We should have exactly 2 events: a Finished event for the tranform
   1799   // animation, and an Aborted event for the opacity animation.
   1800   EXPECT_EQ(2u, events->size());
   1801   EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
   1802   EXPECT_EQ(Animation::Transform, (*events)[0].target_property);
   1803   EXPECT_EQ(AnimationEvent::Aborted, (*events)[1].type);
   1804   EXPECT_EQ(Animation::Opacity, (*events)[1].target_property);
   1805 }
   1806 
   1807 TEST(LayerAnimationControllerTest, HasAnimationThatAffectsScale) {
   1808   scoped_refptr<LayerAnimationController> controller_impl(
   1809       LayerAnimationController::Create(0));
   1810 
   1811   EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
   1812 
   1813   controller_impl->AddAnimation(CreateAnimation(
   1814       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1815       1,
   1816       Animation::Opacity));
   1817 
   1818   // Opacity animations don't affect scale.
   1819   EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
   1820 
   1821   scoped_ptr<KeyframedTransformAnimationCurve> curve1(
   1822       KeyframedTransformAnimationCurve::Create());
   1823 
   1824   TransformOperations operations1;
   1825   curve1->AddKeyframe(TransformKeyframe::Create(
   1826       0.0, operations1, scoped_ptr<TimingFunction>()));
   1827   operations1.AppendTranslate(10.0, 15.0, 0.0);
   1828   curve1->AddKeyframe(TransformKeyframe::Create(
   1829       1.0, operations1, scoped_ptr<TimingFunction>()));
   1830 
   1831   scoped_ptr<Animation> animation(Animation::Create(
   1832       curve1.PassAs<AnimationCurve>(), 2, 2, Animation::Transform));
   1833   controller_impl->AddAnimation(animation.Pass());
   1834 
   1835   // Translations don't affect scale.
   1836   EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
   1837 
   1838   scoped_ptr<KeyframedTransformAnimationCurve> curve2(
   1839       KeyframedTransformAnimationCurve::Create());
   1840 
   1841   TransformOperations operations2;
   1842   curve2->AddKeyframe(TransformKeyframe::Create(
   1843       0.0, operations2, scoped_ptr<TimingFunction>()));
   1844   operations2.AppendScale(2.0, 3.0, 4.0);
   1845   curve2->AddKeyframe(TransformKeyframe::Create(
   1846       1.0, operations2, scoped_ptr<TimingFunction>()));
   1847 
   1848   animation = Animation::Create(
   1849       curve2.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
   1850   controller_impl->AddAnimation(animation.Pass());
   1851 
   1852   EXPECT_TRUE(controller_impl->HasAnimationThatAffectsScale());
   1853 
   1854   controller_impl->GetAnimation(3, Animation::Transform)
   1855       ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
   1856 
   1857   // Only unfinished animations should be considered by
   1858   // HasAnimationThatAffectsScale.
   1859   EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
   1860 }
   1861 
   1862 TEST(LayerAnimationControllerTest, HasOnlyTranslationTransforms) {
   1863   scoped_refptr<LayerAnimationController> controller_impl(
   1864       LayerAnimationController::Create(0));
   1865 
   1866   EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
   1867 
   1868   controller_impl->AddAnimation(CreateAnimation(
   1869       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
   1870       1,
   1871       Animation::Opacity));
   1872 
   1873   // Opacity animations aren't non-translation transforms.
   1874   EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
   1875 
   1876   scoped_ptr<KeyframedTransformAnimationCurve> curve1(
   1877       KeyframedTransformAnimationCurve::Create());
   1878 
   1879   TransformOperations operations1;
   1880   curve1->AddKeyframe(TransformKeyframe::Create(
   1881       0.0, operations1, scoped_ptr<TimingFunction>()));
   1882   operations1.AppendTranslate(10.0, 15.0, 0.0);
   1883   curve1->AddKeyframe(TransformKeyframe::Create(
   1884       1.0, operations1, scoped_ptr<TimingFunction>()));
   1885 
   1886   scoped_ptr<Animation> animation(Animation::Create(
   1887       curve1.PassAs<AnimationCurve>(), 2, 2, Animation::Transform));
   1888   controller_impl->AddAnimation(animation.Pass());
   1889 
   1890   // The only transform animation we've added is a translation.
   1891   EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
   1892 
   1893   scoped_ptr<KeyframedTransformAnimationCurve> curve2(
   1894       KeyframedTransformAnimationCurve::Create());
   1895 
   1896   TransformOperations operations2;
   1897   curve2->AddKeyframe(TransformKeyframe::Create(
   1898       0.0, operations2, scoped_ptr<TimingFunction>()));
   1899   operations2.AppendScale(2.0, 3.0, 4.0);
   1900   curve2->AddKeyframe(TransformKeyframe::Create(
   1901       1.0, operations2, scoped_ptr<TimingFunction>()));
   1902 
   1903   animation = Animation::Create(
   1904       curve2.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
   1905   controller_impl->AddAnimation(animation.Pass());
   1906 
   1907   // A scale animation is not a translation.
   1908   EXPECT_FALSE(controller_impl->HasOnlyTranslationTransforms());
   1909 
   1910   controller_impl->GetAnimation(3, Animation::Transform)
   1911       ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
   1912 
   1913   // Only unfinished animations should be considered by
   1914   // HasOnlyTranslationTransforms.
   1915   EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
   1916 }
   1917 
   1918 TEST(LayerAnimationControllerTest, MaximumScale) {
   1919   scoped_refptr<LayerAnimationController> controller_impl(
   1920       LayerAnimationController::Create(0));
   1921 
   1922   float max_scale = 0.f;
   1923   EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
   1924   EXPECT_EQ(0.f, max_scale);
   1925 
   1926   scoped_ptr<KeyframedTransformAnimationCurve> curve1(
   1927       KeyframedTransformAnimationCurve::Create());
   1928 
   1929   TransformOperations operations1;
   1930   curve1->AddKeyframe(TransformKeyframe::Create(
   1931       0.0, operations1, scoped_ptr<TimingFunction>()));
   1932   operations1.AppendScale(2.0, 3.0, 4.0);
   1933   curve1->AddKeyframe(TransformKeyframe::Create(
   1934       1.0, operations1, scoped_ptr<TimingFunction>()));
   1935 
   1936   scoped_ptr<Animation> animation(Animation::Create(
   1937       curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
   1938   controller_impl->AddAnimation(animation.Pass());
   1939 
   1940   EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
   1941   EXPECT_EQ(4.f, max_scale);
   1942 
   1943   scoped_ptr<KeyframedTransformAnimationCurve> curve2(
   1944       KeyframedTransformAnimationCurve::Create());
   1945 
   1946   TransformOperations operations2;
   1947   curve2->AddKeyframe(TransformKeyframe::Create(
   1948       0.0, operations2, scoped_ptr<TimingFunction>()));
   1949   operations2.AppendScale(6.0, 5.0, 4.0);
   1950   curve2->AddKeyframe(TransformKeyframe::Create(
   1951       1.0, operations2, scoped_ptr<TimingFunction>()));
   1952 
   1953   animation = Animation::Create(
   1954       curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
   1955   controller_impl->AddAnimation(animation.Pass());
   1956 
   1957   EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
   1958   EXPECT_EQ(6.f, max_scale);
   1959 
   1960   scoped_ptr<KeyframedTransformAnimationCurve> curve3(
   1961       KeyframedTransformAnimationCurve::Create());
   1962 
   1963   TransformOperations operations3;
   1964   curve3->AddKeyframe(TransformKeyframe::Create(
   1965       0.0, operations3, scoped_ptr<TimingFunction>()));
   1966   operations3.AppendPerspective(6.0);
   1967   curve3->AddKeyframe(TransformKeyframe::Create(
   1968       1.0, operations3, scoped_ptr<TimingFunction>()));
   1969 
   1970   animation = Animation::Create(
   1971       curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
   1972   controller_impl->AddAnimation(animation.Pass());
   1973 
   1974   EXPECT_FALSE(controller_impl->MaximumScale(&max_scale));
   1975 
   1976   controller_impl->GetAnimation(3, Animation::Transform)
   1977       ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
   1978   controller_impl->GetAnimation(2, Animation::Transform)
   1979       ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
   1980 
   1981   // Only unfinished animations should be considered by
   1982   // MaximumScale.
   1983   EXPECT_TRUE(controller_impl->MaximumScale(&max_scale));
   1984   EXPECT_EQ(4.f, max_scale);
   1985 }
   1986 
   1987 TEST(LayerAnimationControllerTest, NewlyPushedAnimationWaitsForActivation) {
   1988   scoped_ptr<AnimationEventsVector> events(
   1989       make_scoped_ptr(new AnimationEventsVector));
   1990   FakeLayerAnimationValueObserver dummy_impl;
   1991   FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
   1992   scoped_refptr<LayerAnimationController> controller_impl(
   1993       LayerAnimationController::Create(0));
   1994   controller_impl->AddValueObserver(&dummy_impl);
   1995   controller_impl->AddValueObserver(&pending_dummy_impl);
   1996   FakeLayerAnimationValueObserver dummy;
   1997   scoped_refptr<LayerAnimationController> controller(
   1998       LayerAnimationController::Create(0));
   1999   controller->AddValueObserver(&dummy);
   2000 
   2001   EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
   2002   AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, false);
   2003   int group_id = controller->GetAnimation(Animation::Opacity)->group();
   2004   EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
   2005 
   2006   EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
   2007   controller->PushAnimationUpdatesTo(controller_impl.get());
   2008   EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
   2009 
   2010   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
   2011   EXPECT_EQ(
   2012       Animation::WaitingForTargetAvailability,
   2013       controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
   2014   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2015                   ->affects_pending_observers());
   2016   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2017                    ->affects_active_observers());
   2018 
   2019   controller_impl->Animate(kInitialTickTime);
   2020   EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
   2021   controller_impl->UpdateState(true, events.get());
   2022 
   2023   // Since the animation hasn't been activated, it should still be Starting
   2024   // rather than Running.
   2025   EXPECT_EQ(
   2026       Animation::Starting,
   2027       controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
   2028 
   2029   // Since the animation hasn't been activated, only the pending observer
   2030   // should have been ticked.
   2031   EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
   2032   EXPECT_EQ(0.f, dummy_impl.opacity());
   2033 
   2034   controller_impl->ActivateAnimations();
   2035   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2036                   ->affects_pending_observers());
   2037   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2038                   ->affects_active_observers());
   2039 
   2040   controller_impl->Animate(kInitialTickTime +
   2041                            TimeDelta::FromMilliseconds(1000));
   2042   controller_impl->UpdateState(true, events.get());
   2043 
   2044   // Since the animation has been activated, it should have reached the
   2045   // Running state and the active observer should start to get ticked.
   2046   EXPECT_EQ(
   2047       Animation::Running,
   2048       controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
   2049   EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
   2050   EXPECT_EQ(0.5f, dummy_impl.opacity());
   2051 }
   2052 
   2053 TEST(LayerAnimationControllerTest, ActivationBetweenAnimateAndUpdateState) {
   2054   scoped_ptr<AnimationEventsVector> events(
   2055       make_scoped_ptr(new AnimationEventsVector));
   2056   FakeLayerAnimationValueObserver dummy_impl;
   2057   FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
   2058   scoped_refptr<LayerAnimationController> controller_impl(
   2059       LayerAnimationController::Create(0));
   2060   controller_impl->AddValueObserver(&dummy_impl);
   2061   controller_impl->AddValueObserver(&pending_dummy_impl);
   2062   FakeLayerAnimationValueObserver dummy;
   2063   scoped_refptr<LayerAnimationController> controller(
   2064       LayerAnimationController::Create(0));
   2065   controller->AddValueObserver(&dummy);
   2066 
   2067   AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
   2068   int group_id = controller->GetAnimation(Animation::Opacity)->group();
   2069 
   2070   controller->PushAnimationUpdatesTo(controller_impl.get());
   2071 
   2072   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
   2073   EXPECT_EQ(
   2074       Animation::WaitingForTargetAvailability,
   2075       controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
   2076   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2077                   ->affects_pending_observers());
   2078   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2079                    ->affects_active_observers());
   2080 
   2081   controller_impl->Animate(kInitialTickTime);
   2082 
   2083   // Since the animation hasn't been activated, only the pending observer
   2084   // should have been ticked.
   2085   EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
   2086   EXPECT_EQ(0.f, dummy_impl.opacity());
   2087 
   2088   controller_impl->ActivateAnimations();
   2089   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2090                   ->affects_pending_observers());
   2091   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2092                   ->affects_active_observers());
   2093 
   2094   controller_impl->UpdateState(true, events.get());
   2095 
   2096   // Since the animation has been activated, it should have reached the
   2097   // Running state.
   2098   EXPECT_EQ(
   2099       Animation::Running,
   2100       controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
   2101 
   2102   controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
   2103 
   2104   // Both observers should have been ticked.
   2105   EXPECT_EQ(0.75f, pending_dummy_impl.opacity());
   2106   EXPECT_EQ(0.75f, dummy_impl.opacity());
   2107 }
   2108 
   2109 TEST(LayerAnimationControllerTest, PushedDeletedAnimationWaitsForActivation) {
   2110   scoped_ptr<AnimationEventsVector> events(
   2111       make_scoped_ptr(new AnimationEventsVector));
   2112   FakeLayerAnimationValueObserver dummy_impl;
   2113   FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
   2114   scoped_refptr<LayerAnimationController> controller_impl(
   2115       LayerAnimationController::Create(0));
   2116   controller_impl->AddValueObserver(&dummy_impl);
   2117   controller_impl->AddValueObserver(&pending_dummy_impl);
   2118   FakeLayerAnimationValueObserver dummy;
   2119   scoped_refptr<LayerAnimationController> controller(
   2120       LayerAnimationController::Create(0));
   2121   controller->AddValueObserver(&dummy);
   2122 
   2123   AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
   2124   int group_id = controller->GetAnimation(Animation::Opacity)->group();
   2125 
   2126   controller->PushAnimationUpdatesTo(controller_impl.get());
   2127   controller_impl->ActivateAnimations();
   2128   controller_impl->Animate(kInitialTickTime);
   2129   controller_impl->UpdateState(true, events.get());
   2130   EXPECT_EQ(
   2131       Animation::Running,
   2132       controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
   2133   EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
   2134   EXPECT_EQ(0.5f, dummy_impl.opacity());
   2135 
   2136   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2137                   ->affects_pending_observers());
   2138   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2139                   ->affects_active_observers());
   2140 
   2141   // Delete the animation on the main-thread controller.
   2142   controller->RemoveAnimation(
   2143       controller->GetAnimation(Animation::Opacity)->id());
   2144   controller->PushAnimationUpdatesTo(controller_impl.get());
   2145 
   2146   // The animation should no longer affect pending observers.
   2147   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2148                    ->affects_pending_observers());
   2149   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
   2150                   ->affects_active_observers());
   2151 
   2152   controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
   2153   controller_impl->UpdateState(true, events.get());
   2154 
   2155   // Only the active observer should have been ticked.
   2156   EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
   2157   EXPECT_EQ(0.75f, dummy_impl.opacity());
   2158 
   2159   controller_impl->ActivateAnimations();
   2160 
   2161   // Activation should cause the animation to be deleted.
   2162   EXPECT_FALSE(controller_impl->has_any_animation());
   2163 }
   2164 
   2165 // Tests that an animation that affects only active observers won't block
   2166 // an animation that affects only pending observers from starting.
   2167 TEST(LayerAnimationControllerTest, StartAnimationsAffectingDifferentObservers) {
   2168   scoped_ptr<AnimationEventsVector> events(
   2169       make_scoped_ptr(new AnimationEventsVector));
   2170   FakeLayerAnimationValueObserver dummy_impl;
   2171   FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
   2172   scoped_refptr<LayerAnimationController> controller_impl(
   2173       LayerAnimationController::Create(0));
   2174   controller_impl->AddValueObserver(&dummy_impl);
   2175   controller_impl->AddValueObserver(&pending_dummy_impl);
   2176   FakeLayerAnimationValueObserver dummy;
   2177   scoped_refptr<LayerAnimationController> controller(
   2178       LayerAnimationController::Create(0));
   2179   controller->AddValueObserver(&dummy);
   2180 
   2181   AddOpacityTransitionToController(controller.get(), 1, 0.f, 1.f, true);
   2182   int first_animation_group_id =
   2183       controller->GetAnimation(Animation::Opacity)->group();
   2184 
   2185   controller->PushAnimationUpdatesTo(controller_impl.get());
   2186   controller_impl->ActivateAnimations();
   2187   controller_impl->Animate(kInitialTickTime);
   2188   controller_impl->UpdateState(true, events.get());
   2189 
   2190   // Remove the first animation from the main-thread controller, and add a
   2191   // new animation affecting the same property.
   2192   controller->RemoveAnimation(
   2193       controller->GetAnimation(Animation::Opacity)->id());
   2194   AddOpacityTransitionToController(controller.get(), 1, 1.f, 0.5f, true);
   2195   int second_animation_group_id =
   2196       controller->GetAnimation(Animation::Opacity)->group();
   2197   controller->PushAnimationUpdatesTo(controller_impl.get());
   2198 
   2199   // The original animation should only affect active observers, and the new
   2200   // animation should only affect pending observers.
   2201   EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
   2202                                              Animation::Opacity)
   2203                    ->affects_pending_observers());
   2204   EXPECT_TRUE(controller_impl->GetAnimation(first_animation_group_id,
   2205                                             Animation::Opacity)
   2206                   ->affects_active_observers());
   2207   EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
   2208                                             Animation::Opacity)
   2209                   ->affects_pending_observers());
   2210   EXPECT_FALSE(controller_impl->GetAnimation(second_animation_group_id,
   2211                                              Animation::Opacity)
   2212                    ->affects_active_observers());
   2213 
   2214   controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
   2215   controller_impl->UpdateState(true, events.get());
   2216 
   2217   // The original animation should still be running, and the new animation
   2218   // should be starting.
   2219   EXPECT_EQ(Animation::Running,
   2220             controller_impl->GetAnimation(first_animation_group_id,
   2221                                           Animation::Opacity)->run_state());
   2222   EXPECT_EQ(Animation::Starting,
   2223             controller_impl->GetAnimation(second_animation_group_id,
   2224                                           Animation::Opacity)->run_state());
   2225 
   2226   // The active observer should have been ticked by the original animation,
   2227   // and the pending observer should have been ticked by the new animation.
   2228   EXPECT_EQ(1.f, pending_dummy_impl.opacity());
   2229   EXPECT_EQ(0.5f, dummy_impl.opacity());
   2230 
   2231   controller_impl->ActivateAnimations();
   2232 
   2233   // The original animation should have been deleted, and the new animation
   2234   // should now affect both observers.
   2235   EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
   2236                                              Animation::Opacity));
   2237   EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
   2238                                             Animation::Opacity)
   2239                   ->affects_pending_observers());
   2240   EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
   2241                                             Animation::Opacity)
   2242                   ->affects_active_observers());
   2243 
   2244   controller_impl->Animate(kInitialTickTime +
   2245                            TimeDelta::FromMilliseconds(1000));
   2246   controller_impl->UpdateState(true, events.get());
   2247 
   2248   // The new animation should be running, and the active observer should have
   2249   // been ticked at the new animation's starting point.
   2250   EXPECT_EQ(Animation::Running,
   2251             controller_impl->GetAnimation(second_animation_group_id,
   2252                                           Animation::Opacity)->run_state());
   2253   EXPECT_EQ(1.f, pending_dummy_impl.opacity());
   2254   EXPECT_EQ(1.f, dummy_impl.opacity());
   2255 }
   2256 
   2257 }  // namespace
   2258 }  // namespace cc
   2259