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 template <class Target
>
94 int AddAnimatedFilter(Target
* target
,
96 float start_brightness
,
97 float end_brightness
) {
98 scoped_ptr
<KeyframedFilterAnimationCurve
>
99 curve(KeyframedFilterAnimationCurve::Create());
101 if (duration
> 0.0) {
102 FilterOperations start_filters
;
103 start_filters
.Append(
104 FilterOperation::CreateBrightnessFilter(start_brightness
));
105 curve
->AddKeyframe(FilterKeyframe::Create(
106 0.0, start_filters
, scoped_ptr
<cc::TimingFunction
>()));
109 FilterOperations filters
;
110 filters
.Append(FilterOperation::CreateBrightnessFilter(end_brightness
));
111 curve
->AddKeyframe(FilterKeyframe::Create(
112 duration
, filters
, scoped_ptr
<cc::TimingFunction
>()));
114 int id
= AnimationIdProvider::NextAnimationId();
116 scoped_ptr
<Animation
> animation(Animation::Create(
117 curve
.PassAs
<AnimationCurve
>(),
119 AnimationIdProvider::NextGroupId(),
121 animation
->set_needs_synchronized_start_time(true);
123 target
->AddAnimation(animation
.Pass());
127 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
130 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration
)
131 : duration_(duration
) {}
133 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
135 double FakeFloatAnimationCurve::Duration() const {
139 float FakeFloatAnimationCurve::GetValue(double now
) const {
143 scoped_ptr
<cc::AnimationCurve
> FakeFloatAnimationCurve::Clone() const {
144 return make_scoped_ptr(
145 new FakeFloatAnimationCurve
).PassAs
<cc::AnimationCurve
>();
148 FakeTransformTransition::FakeTransformTransition(double duration
)
149 : duration_(duration
) {}
151 FakeTransformTransition::~FakeTransformTransition() {}
153 double FakeTransformTransition::Duration() const {
157 gfx::Transform
FakeTransformTransition::GetValue(double time
) const {
158 return gfx::Transform();
161 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF
& box
,
162 gfx::BoxF
* bounds
) const {
166 scoped_ptr
<cc::AnimationCurve
> FakeTransformTransition::Clone() const {
167 return make_scoped_ptr(
168 new FakeTransformTransition(*this)).PassAs
<cc::AnimationCurve
>();
172 FakeFloatTransition::FakeFloatTransition(double duration
, float from
, float to
)
173 : duration_(duration
), from_(from
), to_(to
) {}
175 FakeFloatTransition::~FakeFloatTransition() {}
177 double FakeFloatTransition::Duration() const {
181 float FakeFloatTransition::GetValue(double time
) const {
185 return (1.0 - time
) * from_
+ time
* to_
;
188 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
190 animation_waiting_for_deletion_(false) {}
192 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
194 void FakeLayerAnimationValueObserver::OnFilterAnimated(
195 const FilterOperations
& filters
) {
199 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity
) {
203 void FakeLayerAnimationValueObserver::OnTransformAnimated(
204 const gfx::Transform
& transform
) {
205 transform_
= transform
;
208 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
209 animation_waiting_for_deletion_
= true;
212 bool FakeLayerAnimationValueObserver::IsActive() const {
216 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
220 scoped_ptr
<cc::AnimationCurve
> FakeFloatTransition::Clone() const {
221 return make_scoped_ptr(
222 new FakeFloatTransition(*this)).PassAs
<cc::AnimationCurve
>();
225 int AddOpacityTransitionToController(cc::LayerAnimationController
* controller
,
229 bool use_timing_function
) {
230 return AddOpacityTransition(controller
,
234 use_timing_function
);
237 int AddAnimatedTransformToController(cc::LayerAnimationController
* controller
,
241 return AddAnimatedTransform(controller
,
247 int AddAnimatedFilterToController(cc::LayerAnimationController
* controller
,
249 float start_brightness
,
250 float end_brightness
) {
251 return AddAnimatedFilter(
252 controller
, duration
, start_brightness
, end_brightness
);
255 int AddOpacityTransitionToLayer(cc::Layer
* layer
,
259 bool use_timing_function
) {
260 return AddOpacityTransition(layer
,
264 use_timing_function
);
267 int AddOpacityTransitionToLayer(cc::LayerImpl
* layer
,
271 bool use_timing_function
) {
272 return AddOpacityTransition(layer
->layer_animation_controller(),
276 use_timing_function
);
279 int AddAnimatedTransformToLayer(cc::Layer
* layer
,
283 return AddAnimatedTransform(layer
, duration
, delta_x
, delta_y
);
286 int AddAnimatedTransformToLayer(cc::LayerImpl
* layer
,
290 return AddAnimatedTransform(layer
->layer_animation_controller(),
296 int AddAnimatedFilterToLayer(cc::Layer
* layer
,
298 float start_brightness
,
299 float end_brightness
) {
300 return AddAnimatedFilter(layer
, duration
, start_brightness
, end_brightness
);
303 int AddAnimatedFilterToLayer(cc::LayerImpl
* layer
,
305 float start_brightness
,
306 float end_brightness
) {
307 return AddAnimatedFilter(layer
->layer_animation_controller(),