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(
40 duration, end_opacity, scoped_ptr<TimingFunction>()));
42 int id = AnimationIdProvider::NextAnimationId();
44 scoped_ptr<Animation> animation(Animation::Create(
45 curve.PassAs<AnimationCurve>(),
47 AnimationIdProvider::NextGroupId(),
49 animation->set_needs_synchronized_start_time(true);
51 target->AddAnimation(animation.Pass());
55 template <class Target>
56 int AddAnimatedTransform(Target* target,
58 TransformOperations start_operations,
59 TransformOperations operations) {
60 scoped_ptr<KeyframedTransformAnimationCurve>
61 curve(KeyframedTransformAnimationCurve::Create());
64 curve->AddKeyframe(TransformKeyframe::Create(
65 0.0, start_operations, scoped_ptr<TimingFunction>()));
68 curve->AddKeyframe(TransformKeyframe::Create(
69 duration, operations, scoped_ptr<TimingFunction>()));
71 int id = AnimationIdProvider::NextAnimationId();
73 scoped_ptr<Animation> animation(Animation::Create(
74 curve.PassAs<AnimationCurve>(),
76 AnimationIdProvider::NextGroupId(),
77 Animation::Transform));
78 animation->set_needs_synchronized_start_time(true);
80 target->AddAnimation(animation.Pass());
84 template <class Target>
85 int AddAnimatedTransform(Target* target,
89 TransformOperations start_operations;
91 start_operations.AppendTranslate(delta_x, delta_y, 0.0);
94 TransformOperations operations;
95 operations.AppendTranslate(delta_x, delta_y, 0.0);
96 return AddAnimatedTransform(target, duration, start_operations, operations);
99 template <class Target>
100 int AddAnimatedFilter(Target* target,
102 float start_brightness,
103 float end_brightness) {
104 scoped_ptr<KeyframedFilterAnimationCurve>
105 curve(KeyframedFilterAnimationCurve::Create());
107 if (duration > 0.0) {
108 FilterOperations start_filters;
109 start_filters.Append(
110 FilterOperation::CreateBrightnessFilter(start_brightness));
111 curve->AddKeyframe(FilterKeyframe::Create(
112 0.0, start_filters, scoped_ptr<TimingFunction>()));
115 FilterOperations filters;
116 filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness));
118 FilterKeyframe::Create(duration, filters, scoped_ptr<TimingFunction>()));
120 int id = AnimationIdProvider::NextAnimationId();
122 scoped_ptr<Animation> animation(Animation::Create(
123 curve.PassAs<AnimationCurve>(),
125 AnimationIdProvider::NextGroupId(),
127 animation->set_needs_synchronized_start_time(true);
129 target->AddAnimation(animation.Pass());
133 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
136 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
137 : duration_(duration) {}
139 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
141 double FakeFloatAnimationCurve::Duration() const {
145 float FakeFloatAnimationCurve::GetValue(double now) const {
149 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const {
150 return make_scoped_ptr(new FakeFloatAnimationCurve).PassAs<AnimationCurve>();
153 FakeTransformTransition::FakeTransformTransition(double duration)
154 : duration_(duration) {}
156 FakeTransformTransition::~FakeTransformTransition() {}
158 double FakeTransformTransition::Duration() const {
162 gfx::Transform FakeTransformTransition::GetValue(double time) const {
163 return gfx::Transform();
166 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box,
167 gfx::BoxF* bounds) const {
171 bool FakeTransformTransition::AffectsScale() const { return false; }
173 bool FakeTransformTransition::IsTranslation() const { return true; }
175 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction,
176 float* max_scale) const {
181 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const {
182 return make_scoped_ptr(new FakeTransformTransition(*this))
183 .PassAs<AnimationCurve>();
187 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
188 : duration_(duration), from_(from), to_(to) {}
190 FakeFloatTransition::~FakeFloatTransition() {}
192 double FakeFloatTransition::Duration() const {
196 float FakeFloatTransition::GetValue(double time) const {
200 return (1.0 - time) * from_ + time * to_;
203 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
205 animation_waiting_for_deletion_(false) {}
207 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
209 void FakeLayerAnimationValueObserver::OnFilterAnimated(
210 const FilterOperations& filters) {
214 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
218 void FakeLayerAnimationValueObserver::OnTransformAnimated(
219 const gfx::Transform& transform) {
220 transform_ = transform;
223 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
224 const gfx::Vector2dF& scroll_offset) {
225 scroll_offset_ = scroll_offset;
228 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
229 animation_waiting_for_deletion_ = true;
232 bool FakeLayerAnimationValueObserver::IsActive() const {
236 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
240 gfx::Vector2dF FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
242 return scroll_offset_;
245 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const {
246 return make_scoped_ptr(new FakeFloatTransition(*this))
247 .PassAs<AnimationCurve>();
250 int AddOpacityTransitionToController(LayerAnimationController* controller,
254 bool use_timing_function) {
255 return AddOpacityTransition(controller,
259 use_timing_function);
262 int AddAnimatedTransformToController(LayerAnimationController* controller,
266 return AddAnimatedTransform(controller,
272 int AddAnimatedFilterToController(LayerAnimationController* controller,
274 float start_brightness,
275 float end_brightness) {
276 return AddAnimatedFilter(
277 controller, duration, start_brightness, end_brightness);
280 int AddOpacityTransitionToLayer(Layer* layer,
284 bool use_timing_function) {
285 return AddOpacityTransition(layer,
289 use_timing_function);
292 int AddOpacityTransitionToLayer(LayerImpl* layer,
296 bool use_timing_function) {
297 return AddOpacityTransition(layer->layer_animation_controller(),
301 use_timing_function);
304 int AddAnimatedTransformToLayer(Layer* layer,
308 return AddAnimatedTransform(layer, duration, delta_x, delta_y);
311 int AddAnimatedTransformToLayer(LayerImpl* layer,
315 return AddAnimatedTransform(layer->layer_animation_controller(),
321 int AddAnimatedTransformToLayer(Layer* layer,
323 TransformOperations start_operations,
324 TransformOperations operations) {
325 return AddAnimatedTransform(layer, duration, start_operations, operations);
328 int AddAnimatedTransformToLayer(LayerImpl* layer,
330 TransformOperations start_operations,
331 TransformOperations operations) {
332 return AddAnimatedTransform(layer->layer_animation_controller(),
338 int AddAnimatedFilterToLayer(Layer* layer,
340 float start_brightness,
341 float end_brightness) {
342 return AddAnimatedFilter(layer, duration, start_brightness, end_brightness);
345 int AddAnimatedFilterToLayer(LayerImpl* layer,
347 float start_brightness,
348 float end_brightness) {
349 return AddAnimatedFilter(layer->layer_animation_controller(),