Add ICU message format support
[chromium-blink-merge.git] / cc / test / animation_test_common.cc
blobde1d01526b6f8dcf89b284f7979564a5b1284570
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/animation_player.h"
9 #include "cc/animation/keyframed_animation_curve.h"
10 #include "cc/animation/layer_animation_controller.h"
11 #include "cc/animation/transform_operations.h"
12 #include "cc/base/time_util.h"
13 #include "cc/layers/layer.h"
14 #include "cc/layers/layer_impl.h"
16 using cc::Animation;
17 using cc::AnimationCurve;
18 using cc::EaseTimingFunction;
19 using cc::FloatKeyframe;
20 using cc::KeyframedFloatAnimationCurve;
21 using cc::KeyframedTransformAnimationCurve;
22 using cc::TimingFunction;
23 using cc::TransformKeyframe;
25 namespace cc {
27 template <class Target>
28 int AddOpacityTransition(Target* target,
29 double duration,
30 float start_opacity,
31 float end_opacity,
32 bool use_timing_function) {
33 scoped_ptr<KeyframedFloatAnimationCurve>
34 curve(KeyframedFloatAnimationCurve::Create());
36 scoped_ptr<TimingFunction> func;
37 if (!use_timing_function)
38 func = EaseTimingFunction::Create();
39 if (duration > 0.0)
40 curve->AddKeyframe(
41 FloatKeyframe::Create(base::TimeDelta(), start_opacity, func.Pass()));
42 curve->AddKeyframe(FloatKeyframe::Create(
43 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr));
45 int id = AnimationIdProvider::NextAnimationId();
47 scoped_ptr<Animation> animation(
48 Animation::Create(curve.Pass(), id, AnimationIdProvider::NextGroupId(),
49 Animation::OPACITY));
50 animation->set_needs_synchronized_start_time(true);
52 target->AddAnimation(animation.Pass());
53 return id;
56 template <class Target>
57 int AddAnimatedTransform(Target* target,
58 double duration,
59 TransformOperations start_operations,
60 TransformOperations operations) {
61 scoped_ptr<KeyframedTransformAnimationCurve>
62 curve(KeyframedTransformAnimationCurve::Create());
64 if (duration > 0.0) {
65 curve->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(),
66 start_operations, nullptr));
69 curve->AddKeyframe(TransformKeyframe::Create(
70 base::TimeDelta::FromSecondsD(duration), operations, nullptr));
72 int id = AnimationIdProvider::NextAnimationId();
74 scoped_ptr<Animation> animation(
75 Animation::Create(curve.Pass(), id, AnimationIdProvider::NextGroupId(),
76 Animation::TRANSFORM));
77 animation->set_needs_synchronized_start_time(true);
79 target->AddAnimation(animation.Pass());
80 return id;
83 template <class Target>
84 int AddAnimatedTransform(Target* target,
85 double duration,
86 int delta_x,
87 int delta_y) {
88 TransformOperations start_operations;
89 if (duration > 0.0) {
90 start_operations.AppendTranslate(0, 0, 0.0);
93 TransformOperations operations;
94 operations.AppendTranslate(delta_x, delta_y, 0.0);
95 return AddAnimatedTransform(target, duration, start_operations, operations);
98 template <class Target>
99 int AddAnimatedFilter(Target* target,
100 double duration,
101 float start_brightness,
102 float end_brightness) {
103 scoped_ptr<KeyframedFilterAnimationCurve>
104 curve(KeyframedFilterAnimationCurve::Create());
106 if (duration > 0.0) {
107 FilterOperations start_filters;
108 start_filters.Append(
109 FilterOperation::CreateBrightnessFilter(start_brightness));
110 curve->AddKeyframe(
111 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
114 FilterOperations filters;
115 filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness));
116 curve->AddKeyframe(FilterKeyframe::Create(
117 base::TimeDelta::FromSecondsD(duration), filters, nullptr));
119 int id = AnimationIdProvider::NextAnimationId();
121 scoped_ptr<Animation> animation(Animation::Create(
122 curve.Pass(), id, AnimationIdProvider::NextGroupId(), Animation::FILTER));
123 animation->set_needs_synchronized_start_time(true);
125 target->AddAnimation(animation.Pass());
126 return id;
129 FakeFloatAnimationCurve::FakeFloatAnimationCurve()
130 : duration_(base::TimeDelta::FromSecondsD(1.0)) {
133 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration)
134 : duration_(base::TimeDelta::FromSecondsD(duration)) {
137 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {}
139 base::TimeDelta FakeFloatAnimationCurve::Duration() const {
140 return duration_;
143 float FakeFloatAnimationCurve::GetValue(base::TimeDelta now) const {
144 return 0.0f;
147 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const {
148 return make_scoped_ptr(new FakeFloatAnimationCurve);
151 FakeTransformTransition::FakeTransformTransition(double duration)
152 : duration_(base::TimeDelta::FromSecondsD(duration)) {
155 FakeTransformTransition::~FakeTransformTransition() {}
157 base::TimeDelta FakeTransformTransition::Duration() const {
158 return duration_;
161 gfx::Transform FakeTransformTransition::GetValue(base::TimeDelta time) const {
162 return gfx::Transform();
165 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box,
166 gfx::BoxF* bounds) const {
167 return false;
170 bool FakeTransformTransition::AffectsScale() const { return false; }
172 bool FakeTransformTransition::IsTranslation() const { return true; }
174 bool FakeTransformTransition::PreservesAxisAlignment() const {
175 return true;
178 bool FakeTransformTransition::AnimationStartScale(bool forward_direction,
179 float* start_scale) const {
180 *start_scale = 1.f;
181 return true;
184 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction,
185 float* max_scale) const {
186 *max_scale = 1.f;
187 return true;
190 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const {
191 return make_scoped_ptr(new FakeTransformTransition(*this));
194 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to)
195 : duration_(base::TimeDelta::FromSecondsD(duration)), from_(from), to_(to) {
198 FakeFloatTransition::~FakeFloatTransition() {}
200 base::TimeDelta FakeFloatTransition::Duration() const {
201 return duration_;
204 float FakeFloatTransition::GetValue(base::TimeDelta time) const {
205 double progress = TimeUtil::Divide(time, duration_);
206 if (progress >= 1.0)
207 progress = 1.0;
208 return (1.0 - progress) * from_ + progress * to_;
211 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver()
212 : opacity_(0.0f),
213 animation_waiting_for_deletion_(false),
214 transform_is_animating_(false) {}
216 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {}
218 void FakeLayerAnimationValueObserver::OnFilterAnimated(
219 const FilterOperations& filters) {
220 filters_ = filters;
223 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) {
224 opacity_ = opacity;
227 void FakeLayerAnimationValueObserver::OnTransformAnimated(
228 const gfx::Transform& transform) {
229 transform_ = transform;
232 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated(
233 const gfx::ScrollOffset& scroll_offset) {
234 scroll_offset_ = scroll_offset;
237 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() {
238 animation_waiting_for_deletion_ = true;
241 void FakeLayerAnimationValueObserver::OnTransformIsPotentiallyAnimatingChanged(
242 bool is_animating) {
243 transform_is_animating_ = is_animating;
246 bool FakeLayerAnimationValueObserver::IsActive() const {
247 return true;
250 bool FakeInactiveLayerAnimationValueObserver::IsActive() const {
251 return false;
254 gfx::ScrollOffset FakeLayerAnimationValueProvider::ScrollOffsetForAnimation()
255 const {
256 return scroll_offset_;
259 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const {
260 return make_scoped_ptr(new FakeFloatTransition(*this));
263 int AddOpacityTransitionToController(LayerAnimationController* controller,
264 double duration,
265 float start_opacity,
266 float end_opacity,
267 bool use_timing_function) {
268 return AddOpacityTransition(controller,
269 duration,
270 start_opacity,
271 end_opacity,
272 use_timing_function);
275 int AddAnimatedTransformToController(LayerAnimationController* controller,
276 double duration,
277 int delta_x,
278 int delta_y) {
279 return AddAnimatedTransform(controller,
280 duration,
281 delta_x,
282 delta_y);
285 int AddAnimatedFilterToController(LayerAnimationController* controller,
286 double duration,
287 float start_brightness,
288 float end_brightness) {
289 return AddAnimatedFilter(
290 controller, duration, start_brightness, end_brightness);
293 int AddOpacityTransitionToLayer(Layer* layer,
294 double duration,
295 float start_opacity,
296 float end_opacity,
297 bool use_timing_function) {
298 return AddOpacityTransition(layer,
299 duration,
300 start_opacity,
301 end_opacity,
302 use_timing_function);
305 int AddOpacityTransitionToLayer(LayerImpl* layer,
306 double duration,
307 float start_opacity,
308 float end_opacity,
309 bool use_timing_function) {
310 return AddOpacityTransition(layer->layer_animation_controller(),
311 duration,
312 start_opacity,
313 end_opacity,
314 use_timing_function);
317 int AddAnimatedTransformToLayer(Layer* layer,
318 double duration,
319 int delta_x,
320 int delta_y) {
321 return AddAnimatedTransform(layer, duration, delta_x, delta_y);
324 int AddAnimatedTransformToLayer(LayerImpl* layer,
325 double duration,
326 int delta_x,
327 int delta_y) {
328 return AddAnimatedTransform(layer->layer_animation_controller(),
329 duration,
330 delta_x,
331 delta_y);
334 int AddAnimatedTransformToLayer(Layer* layer,
335 double duration,
336 TransformOperations start_operations,
337 TransformOperations operations) {
338 return AddAnimatedTransform(layer, duration, start_operations, operations);
341 int AddAnimatedTransformToLayer(LayerImpl* layer,
342 double duration,
343 TransformOperations start_operations,
344 TransformOperations operations) {
345 return AddAnimatedTransform(layer->layer_animation_controller(),
346 duration,
347 start_operations,
348 operations);
351 int AddAnimatedFilterToLayer(Layer* layer,
352 double duration,
353 float start_brightness,
354 float end_brightness) {
355 return AddAnimatedFilter(layer, duration, start_brightness, end_brightness);
358 int AddAnimatedFilterToLayer(LayerImpl* layer,
359 double duration,
360 float start_brightness,
361 float end_brightness) {
362 return AddAnimatedFilter(layer->layer_animation_controller(),
363 duration,
364 start_brightness,
365 end_brightness);
368 int AddAnimatedTransformToPlayer(AnimationPlayer* player,
369 double duration,
370 int delta_x,
371 int delta_y) {
372 return AddAnimatedTransform(player, duration, delta_x, delta_y);
375 int AddOpacityTransitionToPlayer(AnimationPlayer* player,
376 double duration,
377 float start_opacity,
378 float end_opacity,
379 bool use_timing_function) {
380 return AddOpacityTransition(player, duration, start_opacity, end_opacity,
381 use_timing_function);
384 int AddAnimatedFilterToPlayer(AnimationPlayer* player,
385 double duration,
386 float start_brightness,
387 float end_brightness) {
388 return AddAnimatedFilter(player, duration, start_brightness, end_brightness);
391 int AddOpacityStepsToController(LayerAnimationController* target,
392 double duration,
393 float start_opacity,
394 float end_opacity,
395 int num_steps) {
396 scoped_ptr<KeyframedFloatAnimationCurve> curve(
397 KeyframedFloatAnimationCurve::Create());
399 scoped_ptr<TimingFunction> func =
400 StepsTimingFunction::Create(num_steps, 0.5f);
401 if (duration > 0.0)
402 curve->AddKeyframe(
403 FloatKeyframe::Create(base::TimeDelta(), start_opacity, func.Pass()));
404 curve->AddKeyframe(FloatKeyframe::Create(
405 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr));
407 int id = AnimationIdProvider::NextAnimationId();
409 scoped_ptr<Animation> animation(
410 Animation::Create(curve.Pass(), id, AnimationIdProvider::NextGroupId(),
411 Animation::OPACITY));
412 animation->set_needs_synchronized_start_time(true);
414 target->AddAnimation(animation.Pass());
415 return id;
418 } // namespace cc