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.
5 #include "cc/test/animation_test_common.h"
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"
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
;
25 template <class Target
>
26 int AddOpacityTransition(Target
* target
,
30 bool use_timing_function
) {
31 scoped_ptr
<KeyframedFloatAnimationCurve
>
32 curve(KeyframedFloatAnimationCurve::Create());
34 scoped_ptr
<TimingFunction
> func
;
35 if (!use_timing_function
)
36 func
= EaseTimingFunction::Create();
38 curve
->AddKeyframe(FloatKeyframe::Create(0.0, start_opacity
, func
.Pass()));
39 curve
->AddKeyframe(FloatKeyframe::Create(duration
,
41 scoped_ptr
<cc::TimingFunction
>()));
43 int id
= AnimationIdProvider::NextAnimationId();
45 scoped_ptr
<Animation
> animation(Animation::Create(
46 curve
.PassAs
<AnimationCurve
>(),
48 AnimationIdProvider::NextGroupId(),
50 animation
->set_needs_synchronized_start_time(true);
52 target
->AddAnimation(animation
.Pass());
56 template <class Target
>
57 int AddAnimatedTransform(Target
* target
,
61 scoped_ptr
<KeyframedTransformAnimationCurve
>
62 curve(KeyframedTransformAnimationCurve::Create());
65 TransformOperations start_operations
;
66 start_operations
.AppendTranslate(delta_x
, delta_y
, 0.0);
67 curve
->AddKeyframe(TransformKeyframe::Create(
70 scoped_ptr
<cc::TimingFunction
>()));
73 TransformOperations operations
;
74 operations
.AppendTranslate(delta_x
, delta_y
, 0.0);
75 curve
->AddKeyframe(TransformKeyframe::Create(
78 scoped_ptr
<cc::TimingFunction
>()));
80 int id
= AnimationIdProvider::NextAnimationId();
82 scoped_ptr
<Animation
> animation(Animation::Create(
83 curve
.PassAs
<AnimationCurve
>(),
85 AnimationIdProvider::NextGroupId(),
86 Animation::Transform
));
87 animation
->set_needs_synchronized_start_time(true);
89 target
->AddAnimation(animation
.Pass());
93 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
96 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration
)
97 : duration_(duration
) {}
99 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
101 double FakeFloatAnimationCurve::Duration() const {
105 float FakeFloatAnimationCurve::GetValue(double now
) const {
109 scoped_ptr
<cc::AnimationCurve
> FakeFloatAnimationCurve::Clone() const {
110 return make_scoped_ptr(
111 new FakeFloatAnimationCurve
).PassAs
<cc::AnimationCurve
>();
114 FakeTransformTransition::FakeTransformTransition(double duration
)
115 : duration_(duration
) {}
117 FakeTransformTransition::~FakeTransformTransition() {}
119 double FakeTransformTransition::Duration() const {
123 gfx::Transform
FakeTransformTransition::GetValue(double time
) const {
124 return gfx::Transform();
127 scoped_ptr
<cc::AnimationCurve
> FakeTransformTransition::Clone() const {
128 return make_scoped_ptr(
129 new FakeTransformTransition(*this)).PassAs
<cc::AnimationCurve
>();
133 FakeFloatTransition::FakeFloatTransition(double duration
, float from
, float to
)
134 : duration_(duration
), from_(from
), to_(to
) {}
136 FakeFloatTransition::~FakeFloatTransition() {}
138 double FakeFloatTransition::Duration() const {
142 float FakeFloatTransition::GetValue(double time
) const {
146 return (1.0 - time
) * from_
+ time
* to_
;
149 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
152 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
154 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity
) {
158 void FakeLayerAnimationValueObserver::OnTransformAnimated(
159 const gfx::Transform
& transform
) {
160 transform_
= transform
;
163 bool FakeLayerAnimationValueObserver::IsActive() const {
167 scoped_ptr
<cc::AnimationCurve
> FakeFloatTransition::Clone() const {
168 return make_scoped_ptr(
169 new FakeFloatTransition(*this)).PassAs
<cc::AnimationCurve
>();
172 int AddOpacityTransitionToController(cc::LayerAnimationController
* controller
,
176 bool use_timing_function
) {
177 return AddOpacityTransition(controller
,
181 use_timing_function
);
184 int AddAnimatedTransformToController(cc::LayerAnimationController
* controller
,
188 return AddAnimatedTransform(controller
,
194 int AddOpacityTransitionToLayer(cc::Layer
* layer
,
198 bool use_timing_function
) {
199 return AddOpacityTransition(layer
,
203 use_timing_function
);
206 int AddOpacityTransitionToLayer(cc::LayerImpl
* layer
,
210 bool use_timing_function
) {
211 return AddOpacityTransition(layer
->layer_animation_controller(),
215 use_timing_function
);
218 int AddAnimatedTransformToLayer(cc::Layer
* layer
,
222 return AddAnimatedTransform(layer
, duration
, delta_x
, delta_y
);
225 int AddAnimatedTransformToLayer(cc::LayerImpl
* layer
,
229 return AddAnimatedTransform(layer
->layer_animation_controller(),