Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / cc / test / animation_test_common.cc
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(
40       duration, end_opacity, scoped_ptr<TimingFunction>()));
41
42   int id = AnimationIdProvider::NextAnimationId();
43
44   scoped_ptr<Animation> animation(Animation::Create(
45       curve.PassAs<AnimationCurve>(),
46       id,
47       AnimationIdProvider::NextGroupId(),
48       Animation::Opacity));
49   animation->set_needs_synchronized_start_time(true);
50
51   target->AddAnimation(animation.Pass());
52   return id;
53 }
54
55 template <class Target>
56 int AddAnimatedTransform(Target* target,
57                          double duration,
58                          TransformOperations start_operations,
59                          TransformOperations operations) {
60   scoped_ptr<KeyframedTransformAnimationCurve>
61       curve(KeyframedTransformAnimationCurve::Create());
62
63   if (duration > 0.0) {
64     curve->AddKeyframe(TransformKeyframe::Create(
65         0.0, start_operations, scoped_ptr<TimingFunction>()));
66   }
67
68   curve->AddKeyframe(TransformKeyframe::Create(
69       duration, operations, scoped_ptr<TimingFunction>()));
70
71   int id = AnimationIdProvider::NextAnimationId();
72
73   scoped_ptr<Animation> animation(Animation::Create(
74       curve.PassAs<AnimationCurve>(),
75       id,
76       AnimationIdProvider::NextGroupId(),
77       Animation::Transform));
78   animation->set_needs_synchronized_start_time(true);
79
80   target->AddAnimation(animation.Pass());
81   return id;
82 }
83
84 template <class Target>
85 int AddAnimatedTransform(Target* target,
86                          double duration,
87                          int delta_x,
88                          int delta_y) {
89   TransformOperations start_operations;
90   if (duration > 0.0) {
91     start_operations.AppendTranslate(delta_x, delta_y, 0.0);
92   }
93
94   TransformOperations operations;
95   operations.AppendTranslate(delta_x, delta_y, 0.0);
96   return AddAnimatedTransform(target, duration, start_operations, operations);
97 }
98
99 template <class Target>
100 int AddAnimatedFilter(Target* target,
101                       double duration,
102                       float start_brightness,
103                       float end_brightness) {
104   scoped_ptr<KeyframedFilterAnimationCurve>
105       curve(KeyframedFilterAnimationCurve::Create());
106
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>()));
113   }
114
115   FilterOperations filters;
116   filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness));
117   curve->AddKeyframe(
118       FilterKeyframe::Create(duration, filters, scoped_ptr<TimingFunction>()));
119
120   int id = AnimationIdProvider::NextAnimationId();
121
122   scoped_ptr<Animation> animation(Animation::Create(
123       curve.PassAs<AnimationCurve>(),
124       id,
125       AnimationIdProvider::NextGroupId(),
126       Animation::Filter));
127   animation->set_needs_synchronized_start_time(true);
128
129   target->AddAnimation(animation.Pass());
130   return id;
131 }
132
133 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
134     : duration_(1.0) {}
135
136 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
137     : duration_(duration) {}
138
139 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
140
141 double FakeFloatAnimationCurve::Duration() const {
142   return duration_;
143 }
144
145 float FakeFloatAnimationCurve::GetValue(double now) const {
146   return 0.0f;
147 }
148
149 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const {
150   return make_scoped_ptr(new FakeFloatAnimationCurve).PassAs<AnimationCurve>();
151 }
152
153 FakeTransformTransition::FakeTransformTransition(double duration)
154     : duration_(duration) {}
155
156 FakeTransformTransition::~FakeTransformTransition() {}
157
158 double FakeTransformTransition::Duration() const {
159   return duration_;
160 }
161
162 gfx::Transform FakeTransformTransition::GetValue(double time) const {
163   return gfx::Transform();
164 }
165
166 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box,
167                                                    gfx::BoxF* bounds) const {
168   return false;
169 }
170
171 bool FakeTransformTransition::AffectsScale() const { return false; }
172
173 bool FakeTransformTransition::IsTranslation() const { return true; }
174
175 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction,
176                                                  float* max_scale) const {
177   *max_scale = 1.f;
178   return true;
179 }
180
181 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const {
182   return make_scoped_ptr(new FakeTransformTransition(*this))
183       .PassAs<AnimationCurve>();
184 }
185
186
187 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
188     : duration_(duration), from_(from), to_(to) {}
189
190 FakeFloatTransition::~FakeFloatTransition() {}
191
192 double FakeFloatTransition::Duration() const {
193   return duration_;
194 }
195
196 float FakeFloatTransition::GetValue(double time) const {
197   time /= duration_;
198   if (time >= 1.0)
199     time = 1.0;
200   return (1.0 - time) * from_ + time * to_;
201 }
202
203 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
204     : opacity_(0.0f),
205       animation_waiting_for_deletion_(false) {}
206
207 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
208
209 void FakeLayerAnimationValueObserver::OnFilterAnimated(
210     const FilterOperations& filters) {
211   filters_ = filters;
212 }
213
214 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
215   opacity_ = opacity;
216 }
217
218 void FakeLayerAnimationValueObserver::OnTransformAnimated(
219     const gfx::Transform& transform) {
220   transform_ = transform;
221 }
222
223 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
224     const gfx::Vector2dF& scroll_offset) {
225   scroll_offset_ = scroll_offset;
226 }
227
228 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
229   animation_waiting_for_deletion_ = true;
230 }
231
232 bool FakeLayerAnimationValueObserver::IsActive() const {
233   return true;
234 }
235
236 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
237   return false;
238 }
239
240 gfx::Vector2dF FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
241     const {
242   return scroll_offset_;
243 }
244
245 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const {
246   return make_scoped_ptr(new FakeFloatTransition(*this))
247       .PassAs<AnimationCurve>();
248 }
249
250 int AddOpacityTransitionToController(LayerAnimationController* controller,
251                                      double duration,
252                                      float start_opacity,
253                                      float end_opacity,
254                                      bool use_timing_function) {
255   return AddOpacityTransition(controller,
256                               duration,
257                               start_opacity,
258                               end_opacity,
259                               use_timing_function);
260 }
261
262 int AddAnimatedTransformToController(LayerAnimationController* controller,
263                                      double duration,
264                                      int delta_x,
265                                      int delta_y) {
266   return AddAnimatedTransform(controller,
267                               duration,
268                               delta_x,
269                               delta_y);
270 }
271
272 int AddAnimatedFilterToController(LayerAnimationController* controller,
273                                   double duration,
274                                   float start_brightness,
275                                   float end_brightness) {
276   return AddAnimatedFilter(
277       controller, duration, start_brightness, end_brightness);
278 }
279
280 int AddOpacityTransitionToLayer(Layer* layer,
281                                 double duration,
282                                 float start_opacity,
283                                 float end_opacity,
284                                 bool use_timing_function) {
285   return AddOpacityTransition(layer,
286                               duration,
287                               start_opacity,
288                               end_opacity,
289                               use_timing_function);
290 }
291
292 int AddOpacityTransitionToLayer(LayerImpl* layer,
293                                 double duration,
294                                 float start_opacity,
295                                 float end_opacity,
296                                 bool use_timing_function) {
297   return AddOpacityTransition(layer->layer_animation_controller(),
298                               duration,
299                               start_opacity,
300                               end_opacity,
301                               use_timing_function);
302 }
303
304 int AddAnimatedTransformToLayer(Layer* layer,
305                                 double duration,
306                                 int delta_x,
307                                 int delta_y) {
308   return AddAnimatedTransform(layer, duration, delta_x, delta_y);
309 }
310
311 int AddAnimatedTransformToLayer(LayerImpl* layer,
312                                 double duration,
313                                 int delta_x,
314                                 int delta_y) {
315   return AddAnimatedTransform(layer->layer_animation_controller(),
316                               duration,
317                               delta_x,
318                               delta_y);
319 }
320
321 int AddAnimatedTransformToLayer(Layer* layer,
322                                 double duration,
323                                 TransformOperations start_operations,
324                                 TransformOperations operations) {
325   return AddAnimatedTransform(layer, duration, start_operations, operations);
326 }
327
328 int AddAnimatedTransformToLayer(LayerImpl* layer,
329                                 double duration,
330                                 TransformOperations start_operations,
331                                 TransformOperations operations) {
332   return AddAnimatedTransform(layer->layer_animation_controller(),
333                               duration,
334                               start_operations,
335                               operations);
336 }
337
338 int AddAnimatedFilterToLayer(Layer* layer,
339                              double duration,
340                              float start_brightness,
341                              float end_brightness) {
342   return AddAnimatedFilter(layer, duration, start_brightness, end_brightness);
343 }
344
345 int AddAnimatedFilterToLayer(LayerImpl* layer,
346                              double duration,
347                              float start_brightness,
348                              float end_brightness) {
349   return AddAnimatedFilter(layer->layer_animation_controller(),
350                            duration,
351                            start_brightness,
352                            end_brightness);
353 }
354
355 }  // namespace cc