Home | History | Annotate | Download | only in test
      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/test/animation_test_common.h"
      6 
      7 #include "cc/animation/animation_id_provider.h"
      8 #include "cc/animation/keyframed_animation_curve.h"
      9 #include "cc/animation/layer_animation_controller.h"
     10 #include "cc/animation/transform_operations.h"
     11 #include "cc/layers/layer.h"
     12 #include "cc/layers/layer_impl.h"
     13 
     14 using cc::Animation;
     15 using cc::AnimationCurve;
     16 using cc::EaseTimingFunction;
     17 using cc::FloatKeyframe;
     18 using cc::KeyframedFloatAnimationCurve;
     19 using cc::KeyframedTransformAnimationCurve;
     20 using cc::TimingFunction;
     21 using cc::TransformKeyframe;
     22 
     23 namespace cc {
     24 
     25 template <class Target>
     26 int AddOpacityTransition(Target* target,
     27                          double duration,
     28                          float start_opacity,
     29                          float end_opacity,
     30                          bool use_timing_function) {
     31   scoped_ptr<KeyframedFloatAnimationCurve>
     32       curve(KeyframedFloatAnimationCurve::Create());
     33 
     34   scoped_ptr<TimingFunction> func;
     35   if (!use_timing_function)
     36     func = EaseTimingFunction::Create();
     37   if (duration > 0.0)
     38     curve->AddKeyframe(FloatKeyframe::Create(0.0, start_opacity, func.Pass()));
     39   curve->AddKeyframe(FloatKeyframe::Create(duration,
     40                                            end_opacity,
     41                                            scoped_ptr<cc::TimingFunction>()));
     42 
     43   int id = AnimationIdProvider::NextAnimationId();
     44 
     45   scoped_ptr<Animation> animation(Animation::Create(
     46       curve.PassAs<AnimationCurve>(),
     47       id,
     48       AnimationIdProvider::NextGroupId(),
     49       Animation::Opacity));
     50   animation->set_needs_synchronized_start_time(true);
     51 
     52   target->AddAnimation(animation.Pass());
     53   return id;
     54 }
     55 
     56 template <class Target>
     57 int AddAnimatedTransform(Target* target,
     58                          double duration,
     59                          int delta_x,
     60                          int delta_y) {
     61   scoped_ptr<KeyframedTransformAnimationCurve>
     62       curve(KeyframedTransformAnimationCurve::Create());
     63 
     64   if (duration > 0.0) {
     65     TransformOperations start_operations;
     66     start_operations.AppendTranslate(delta_x, delta_y, 0.0);
     67     curve->AddKeyframe(TransformKeyframe::Create(
     68         0.0,
     69         start_operations,
     70         scoped_ptr<cc::TimingFunction>()));
     71   }
     72 
     73   TransformOperations operations;
     74   operations.AppendTranslate(delta_x, delta_y, 0.0);
     75   curve->AddKeyframe(TransformKeyframe::Create(
     76       duration,
     77       operations,
     78       scoped_ptr<cc::TimingFunction>()));
     79 
     80   int id = AnimationIdProvider::NextAnimationId();
     81 
     82   scoped_ptr<Animation> animation(Animation::Create(
     83       curve.PassAs<AnimationCurve>(),
     84       id,
     85       AnimationIdProvider::NextGroupId(),
     86       Animation::Transform));
     87   animation->set_needs_synchronized_start_time(true);
     88 
     89   target->AddAnimation(animation.Pass());
     90   return id;
     91 }
     92 
     93 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
     94     : duration_(1.0) {}
     95 
     96 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
     97     : duration_(duration) {}
     98 
     99 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
    100 
    101 double FakeFloatAnimationCurve::Duration() const {
    102   return duration_;
    103 }
    104 
    105 float FakeFloatAnimationCurve::GetValue(double now) const {
    106   return 0.0f;
    107 }
    108 
    109 scoped_ptr<cc::AnimationCurve> FakeFloatAnimationCurve::Clone() const {
    110   return make_scoped_ptr(
    111       new FakeFloatAnimationCurve).PassAs<cc::AnimationCurve>();
    112 }
    113 
    114 FakeTransformTransition::FakeTransformTransition(double duration)
    115     : duration_(duration) {}
    116 
    117 FakeTransformTransition::~FakeTransformTransition() {}
    118 
    119 double FakeTransformTransition::Duration() const {
    120   return duration_;
    121 }
    122 
    123 gfx::Transform FakeTransformTransition::GetValue(double time) const {
    124   return gfx::Transform();
    125 }
    126 
    127 scoped_ptr<cc::AnimationCurve> FakeTransformTransition::Clone() const {
    128   return make_scoped_ptr(
    129       new FakeTransformTransition(*this)).PassAs<cc::AnimationCurve>();
    130 }
    131 
    132 
    133 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
    134     : duration_(duration), from_(from), to_(to) {}
    135 
    136 FakeFloatTransition::~FakeFloatTransition() {}
    137 
    138 double FakeFloatTransition::Duration() const {
    139   return duration_;
    140 }
    141 
    142 float FakeFloatTransition::GetValue(double time) const {
    143   time /= duration_;
    144   if (time >= 1.0)
    145     time = 1.0;
    146   return (1.0 - time) * from_ + time * to_;
    147 }
    148 
    149 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
    150     : opacity_(0.0f) {}
    151 
    152 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
    153 
    154 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
    155   opacity_ = opacity;
    156 }
    157 
    158 void FakeLayerAnimationValueObserver::OnTransformAnimated(
    159     const gfx::Transform& transform) {
    160   transform_ = transform;
    161 }
    162 
    163 bool FakeLayerAnimationValueObserver::IsActive() const {
    164   return true;
    165 }
    166 
    167 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
    168   return false;
    169 }
    170 
    171 scoped_ptr<cc::AnimationCurve> FakeFloatTransition::Clone() const {
    172   return make_scoped_ptr(
    173       new FakeFloatTransition(*this)).PassAs<cc::AnimationCurve>();
    174 }
    175 
    176 int AddOpacityTransitionToController(cc::LayerAnimationController* controller,
    177                                      double duration,
    178                                      float start_opacity,
    179                                      float end_opacity,
    180                                      bool use_timing_function) {
    181   return AddOpacityTransition(controller,
    182                               duration,
    183                               start_opacity,
    184                               end_opacity,
    185                               use_timing_function);
    186 }
    187 
    188 int AddAnimatedTransformToController(cc::LayerAnimationController* controller,
    189                                      double duration,
    190                                      int delta_x,
    191                                      int delta_y) {
    192   return AddAnimatedTransform(controller,
    193                               duration,
    194                               delta_x,
    195                               delta_y);
    196 }
    197 
    198 int AddOpacityTransitionToLayer(cc::Layer* layer,
    199                                 double duration,
    200                                 float start_opacity,
    201                                 float end_opacity,
    202                                 bool use_timing_function) {
    203   return AddOpacityTransition(layer,
    204                               duration,
    205                               start_opacity,
    206                               end_opacity,
    207                               use_timing_function);
    208 }
    209 
    210 int AddOpacityTransitionToLayer(cc::LayerImpl* layer,
    211                                 double duration,
    212                                 float start_opacity,
    213                                 float end_opacity,
    214                                 bool use_timing_function) {
    215   return AddOpacityTransition(layer->layer_animation_controller(),
    216                               duration,
    217                               start_opacity,
    218                               end_opacity,
    219                               use_timing_function);
    220 }
    221 
    222 int AddAnimatedTransformToLayer(cc::Layer* layer,
    223                                 double duration,
    224                                 int delta_x,
    225                                 int delta_y) {
    226   return AddAnimatedTransform(layer, duration, delta_x, delta_y);
    227 }
    228 
    229 int AddAnimatedTransformToLayer(cc::LayerImpl* layer,
    230                                 double duration,
    231                                 int delta_x,
    232                                 int delta_y) {
    233   return AddAnimatedTransform(layer->layer_animation_controller(),
    234                               duration,
    235                               delta_x,
    236                               delta_y);
    237 }
    238 
    239 }  // namespace cc
    240