OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 "base/basictypes.h" | |
6 #include "base/compiler_specific.h" | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/time.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 #include "ui/gfx/rect.h" | |
11 #include "ui/gfx/transform.h" | |
12 #include "ui/gfx/compositor/layer_animation_delegate.h" | |
13 #include "ui/gfx/compositor/layer_animation_element.h" | |
14 #include "ui/gfx/compositor/layer_animation_sequence.h" | |
15 #include "ui/gfx/compositor/layer_animator.h" | |
16 | |
17 namespace ui { | |
18 | |
19 namespace { | |
20 | |
21 void CheckApproximatelyEqual(const Transform& lhs, | |
22 const Transform& rhs) { | |
23 for (int i = 0; i < 4; ++i) { | |
24 for (int j = 0; j < 4; ++j) { | |
25 EXPECT_FLOAT_EQ(lhs.matrix().get(i, j), rhs.matrix().get(i, j)); | |
26 } | |
27 } | |
28 } | |
29 | |
30 void CheckApproximatelyEqual(const gfx::Rect& lhs, | |
31 const gfx::Rect& rhs) { | |
32 EXPECT_FLOAT_EQ(lhs.x(), rhs.x()); | |
33 EXPECT_FLOAT_EQ(lhs.y(), rhs.y()); | |
34 EXPECT_FLOAT_EQ(lhs.width(), rhs.width()); | |
35 EXPECT_FLOAT_EQ(lhs.height(), rhs.height()); | |
36 } | |
37 | |
38 class DummyLayerAnimationDelegate : public LayerAnimationDelegate { | |
39 public: | |
40 DummyLayerAnimationDelegate() : opacity_(1.0f) { | |
41 } | |
42 | |
43 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE { | |
44 bounds_ = bounds; | |
45 } | |
46 virtual void SetTransformFromAnimation(const Transform& transform) OVERRIDE { | |
47 transform_ = transform; | |
48 } | |
49 virtual void SetOpacityFromAnimation(float opacity) OVERRIDE { | |
50 opacity_ = opacity; | |
51 } | |
52 virtual void ScheduleDrawForAnimation() OVERRIDE { | |
53 } | |
54 virtual const gfx::Rect& GetBoundsForAnimation() const OVERRIDE { | |
55 return bounds_; | |
56 } | |
57 virtual const Transform& GetTransformForAnimation() const OVERRIDE { | |
58 return transform_; | |
59 } | |
60 virtual const float GetOpacityForAnimation() const OVERRIDE { | |
61 return opacity_; | |
62 } | |
63 | |
64 private: | |
65 gfx::Rect bounds_; | |
66 Transform transform_; | |
67 float opacity_; | |
68 }; | |
69 | |
70 TEST(LayerAnimationElementTest, TransformElement) { | |
sky
2011/10/19 00:06:33
It's better form to create _unittest for each of t
| |
71 DummyLayerAnimationDelegate delegate; | |
72 Transform start_transform, target_transform, middle_transform; | |
73 start_transform.SetRotate(-90); | |
74 target_transform.SetRotate(90); | |
75 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
76 | |
77 scoped_ptr<LayerAnimationElement> element( | |
78 LayerAnimationElement::CreateTransformElement(target_transform, delta)); | |
79 | |
80 for (int i = 0; i < 2; ++i) { | |
81 delegate.SetTransformFromAnimation(start_transform); | |
82 element->Progress(0.0, &delegate); | |
83 CheckApproximatelyEqual(start_transform, | |
84 delegate.GetTransformForAnimation()); | |
85 element->Progress(0.5, &delegate); | |
86 CheckApproximatelyEqual(middle_transform, | |
87 delegate.GetTransformForAnimation()); | |
88 element->Progress(1.0, &delegate); | |
89 CheckApproximatelyEqual(target_transform, | |
90 delegate.GetTransformForAnimation()); | |
91 } | |
92 | |
93 EXPECT_EQ(delta, element->Duration()); | |
94 } | |
95 | |
96 TEST(LayerAnimationElementTest, BoundsElement) { | |
sky
2011/10/19 00:06:33
Add descriptions for each of your tests.
| |
97 DummyLayerAnimationDelegate delegate; | |
98 gfx::Rect start, target, middle; | |
99 start = target = middle = gfx::Rect(0, 0, 50, 50); | |
100 start.set_x(-90); | |
101 target.set_x(90); | |
102 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
103 | |
104 scoped_ptr<LayerAnimationElement> element( | |
105 LayerAnimationElement::CreateBoundsElement(target, delta)); | |
106 | |
107 for (int i = 0; i < 2; ++i) { | |
108 delegate.SetBoundsFromAnimation(start); | |
109 element->Progress(0.0, &delegate); | |
110 CheckApproximatelyEqual(start, delegate.GetBoundsForAnimation()); | |
111 element->Progress(0.5, &delegate); | |
112 CheckApproximatelyEqual(middle, delegate.GetBoundsForAnimation()); | |
113 element->Progress(1.0, &delegate); | |
114 CheckApproximatelyEqual(target, delegate.GetBoundsForAnimation()); | |
115 } | |
116 | |
117 EXPECT_EQ(delta, element->Duration()); | |
118 } | |
119 | |
120 TEST(LayerAnimationElementTest, OpacityElement) { | |
121 DummyLayerAnimationDelegate delegate; | |
122 float start = 0.0; | |
123 float middle = 0.5; | |
124 float target = 1.0; | |
125 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
126 scoped_ptr<LayerAnimationElement> element( | |
127 LayerAnimationElement::CreateOpacityElement(target, delta)); | |
128 | |
129 for (int i = 0; i < 2; ++i) { | |
130 delegate.SetOpacityFromAnimation(start); | |
131 element->Progress(0.0, &delegate); | |
132 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | |
133 element->Progress(0.5, &delegate); | |
134 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | |
135 element->Progress(1.0, &delegate); | |
136 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | |
137 } | |
138 | |
139 EXPECT_EQ(delta, element->Duration()); | |
140 } | |
141 | |
142 TEST(LayerAnimationElementTest, PauseElement) { | |
143 LayerAnimationElement::AnimatableProperties properties; | |
144 properties.insert(LayerAnimationElement::TRANSFORM); | |
145 properties.insert(LayerAnimationElement::BOUNDS); | |
146 properties.insert(LayerAnimationElement::OPACITY); | |
147 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
148 | |
149 scoped_ptr<LayerAnimationElement> element( | |
150 LayerAnimationElement::CreatePauseElement(properties, delta)); | |
151 | |
152 DummyLayerAnimationDelegate delegate; | |
153 DummyLayerAnimationDelegate copy = delegate; | |
154 | |
155 element->Progress(1.0, &delegate); | |
156 | |
157 // Nothing should have changed. | |
158 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | |
159 copy.GetBoundsForAnimation()); | |
160 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | |
161 copy.GetTransformForAnimation()); | |
162 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | |
163 copy.GetOpacityForAnimation()); | |
164 | |
165 // Pause should last for |delta|. | |
166 EXPECT_EQ(delta, element->Duration()); | |
167 } | |
168 | |
169 TEST(LayerAnimationSequenceTest, NoElement) { | |
170 LayerAnimationSequence sequence; | |
171 EXPECT_EQ(sequence.duration(), base::TimeDelta()); | |
172 EXPECT_TRUE(sequence.properties().size() == 0); | |
173 LayerAnimationElement::AnimatableProperties properties; | |
174 EXPECT_FALSE(sequence.HasCommonProperty(properties)); | |
175 } | |
176 | |
177 TEST(LayerAnimationSequenceTest, SingleElement) { | |
178 LayerAnimationSequence sequence; | |
179 DummyLayerAnimationDelegate delegate; | |
180 float start = 0.0; | |
181 float middle = 0.5; | |
182 float target = 1.0; | |
183 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
184 sequence.AddElement( | |
185 LayerAnimationElement::CreateOpacityElement(target, delta)); | |
186 | |
187 for (int i = 0; i < 2; ++i) { | |
188 delegate.SetOpacityFromAnimation(start); | |
189 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); | |
190 EXPECT_FLOAT_EQ(start, delegate.GetOpacityForAnimation()); | |
191 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); | |
192 EXPECT_FLOAT_EQ(middle, delegate.GetOpacityForAnimation()); | |
193 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); | |
194 EXPECT_FLOAT_EQ(target, delegate.GetOpacityForAnimation()); | |
195 } | |
196 | |
197 EXPECT_TRUE(sequence.properties().size() == 1); | |
198 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | |
199 sequence.properties().end()); | |
200 EXPECT_EQ(delta, sequence.duration()); | |
201 } | |
202 | |
203 TEST(LayerAnimationSequenceTest, MultipleElement) { | |
204 LayerAnimationSequence sequence; | |
205 DummyLayerAnimationDelegate delegate; | |
206 float start_opacity = 0.0; | |
207 float middle_opacity = 0.5; | |
208 float target_opacity = 1.0; | |
209 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
210 sequence.AddElement( | |
211 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)); | |
212 | |
213 // Pause bounds for a second. | |
214 LayerAnimationElement::AnimatableProperties properties; | |
215 properties.insert(LayerAnimationElement::BOUNDS); | |
216 | |
217 sequence.AddElement( | |
218 LayerAnimationElement::CreatePauseElement(properties, delta)); | |
219 | |
220 Transform start_transform, target_transform, middle_transform; | |
221 start_transform.SetRotate(-90); | |
222 target_transform.SetRotate(90); | |
223 | |
224 sequence.AddElement( | |
225 LayerAnimationElement::CreateTransformElement(target_transform, delta)); | |
226 | |
227 for (int i = 0; i < 2; ++i) { | |
228 delegate.SetOpacityFromAnimation(start_opacity); | |
229 delegate.SetTransformFromAnimation(start_transform); | |
230 | |
231 sequence.Progress(base::TimeDelta::FromMilliseconds(0), &delegate); | |
232 EXPECT_FLOAT_EQ(start_opacity, delegate.GetOpacityForAnimation()); | |
233 sequence.Progress(base::TimeDelta::FromMilliseconds(500), &delegate); | |
234 EXPECT_FLOAT_EQ(middle_opacity, delegate.GetOpacityForAnimation()); | |
235 sequence.Progress(base::TimeDelta::FromMilliseconds(1000), &delegate); | |
236 EXPECT_FLOAT_EQ(target_opacity, delegate.GetOpacityForAnimation()); | |
237 DummyLayerAnimationDelegate copy = delegate; | |
238 | |
239 // In the middle of the pause -- nothing should have changed. | |
240 sequence.Progress(base::TimeDelta::FromMilliseconds(1500), &delegate); | |
241 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), | |
242 copy.GetBoundsForAnimation()); | |
243 CheckApproximatelyEqual(delegate.GetTransformForAnimation(), | |
244 copy.GetTransformForAnimation()); | |
245 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | |
246 copy.GetOpacityForAnimation()); | |
247 | |
248 | |
249 sequence.Progress(base::TimeDelta::FromMilliseconds(2000), &delegate); | |
250 CheckApproximatelyEqual(start_transform, | |
251 delegate.GetTransformForAnimation()); | |
252 sequence.Progress(base::TimeDelta::FromMilliseconds(2500), &delegate); | |
253 CheckApproximatelyEqual(middle_transform, | |
254 delegate.GetTransformForAnimation()); | |
255 sequence.Progress(base::TimeDelta::FromMilliseconds(3000), &delegate); | |
256 CheckApproximatelyEqual(target_transform, | |
257 delegate.GetTransformForAnimation()); | |
258 } | |
259 | |
260 EXPECT_TRUE(sequence.properties().size() == 3); | |
261 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::OPACITY) != | |
262 sequence.properties().end()); | |
263 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::TRANSFORM) != | |
264 sequence.properties().end()); | |
265 EXPECT_TRUE(sequence.properties().find(LayerAnimationElement::BOUNDS) != | |
266 sequence.properties().end()); | |
267 EXPECT_EQ(delta + delta + delta, sequence.duration()); | |
268 } | |
269 | |
270 TEST(LayerAnimatorTest, ImplicitAnimation) { | |
271 scoped_ptr<LayerAnimator> animator( | |
272 LayerAnimator::CreateImplicitAnimator()); | |
273 animator->SetAnimationForTest(NULL); | |
274 DummyLayerAnimationDelegate delegate; | |
275 animator->SetDelegate(&delegate); | |
276 base::TimeTicks now = base::TimeTicks::Now(); | |
277 animator->SetOpacity(0.5); | |
278 EXPECT_TRUE(animator->IsAnimating()); | |
279 animator->Step(now + base::TimeDelta::FromSeconds(1)); | |
280 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
281 } | |
282 | |
283 TEST(LayerAnimatorTest, NoImplicitAnimation) { | |
284 scoped_ptr<LayerAnimator> animator( | |
285 LayerAnimator::CreateDefaultAnimator()); | |
286 animator->SetAnimationForTest(NULL); | |
287 DummyLayerAnimationDelegate delegate; | |
288 animator->SetDelegate(&delegate); | |
289 base::TimeTicks now = base::TimeTicks::Now(); | |
290 animator->SetOpacity(0.5); | |
291 EXPECT_FALSE(animator->IsAnimating()); | |
292 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
293 } | |
294 | |
295 TEST(LayerAnimatorTest, StopAnimatingProperty) { | |
296 scoped_ptr<LayerAnimator> animator( | |
297 LayerAnimator::CreateImplicitAnimator()); | |
298 animator->SetAnimationForTest(NULL); | |
299 DummyLayerAnimationDelegate delegate; | |
300 animator->SetDelegate(&delegate); | |
301 base::TimeTicks now = base::TimeTicks::Now(); | |
302 double target_opacity(0.5); | |
303 gfx::Rect target_bounds(0, 0, 50, 50); | |
304 animator->SetOpacity(target_opacity); | |
305 animator->SetBounds(target_bounds); | |
306 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); | |
307 EXPECT_TRUE(animator->IsAnimating()); | |
308 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
309 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS); | |
310 EXPECT_FALSE(animator->IsAnimating()); | |
311 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
312 } | |
313 | |
314 TEST(LayerAnimatorTest, StopAnimating) { | |
315 scoped_ptr<LayerAnimator> animator( | |
316 LayerAnimator::CreateImplicitAnimator()); | |
317 animator->SetAnimationForTest(NULL); | |
318 DummyLayerAnimationDelegate delegate; | |
319 animator->SetDelegate(&delegate); | |
320 base::TimeTicks now = base::TimeTicks::Now(); | |
321 double target_opacity(0.5); | |
322 gfx::Rect target_bounds(0, 0, 50, 50); | |
323 animator->SetOpacity(target_opacity); | |
324 animator->SetBounds(target_bounds); | |
325 EXPECT_TRUE(animator->IsAnimating()); | |
326 animator->StopAnimating(); | |
327 EXPECT_FALSE(animator->IsAnimating()); | |
328 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5); | |
329 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
330 } | |
331 | |
332 // schedule animation (that can run immediately) | |
333 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) { | |
334 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
335 animator->SetAnimationForTest(NULL); | |
336 DummyLayerAnimationDelegate delegate; | |
337 animator->SetDelegate(&delegate); | |
338 | |
339 double start_opacity(0.0); | |
340 double middle_opacity(0.5); | |
341 double target_opacity(1.0); | |
342 | |
343 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
344 | |
345 delegate.SetOpacityFromAnimation(start_opacity); | |
346 | |
347 animator->ScheduleAnimation( | |
348 new LayerAnimationSequence( | |
349 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
350 | |
351 EXPECT_TRUE(animator->IsAnimating()); | |
352 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
353 | |
354 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
355 | |
356 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
357 | |
358 EXPECT_TRUE(animator->IsAnimating()); | |
359 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
360 | |
361 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
362 | |
363 EXPECT_FALSE(animator->IsAnimating()); | |
364 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
365 } | |
366 | |
367 // schedule two animations on separate properties | |
368 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) { | |
369 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
370 animator->SetAnimationForTest(NULL); | |
371 DummyLayerAnimationDelegate delegate; | |
372 animator->SetDelegate(&delegate); | |
373 | |
374 double start_opacity(0.0); | |
375 double middle_opacity(0.5); | |
376 double target_opacity(1.0); | |
377 | |
378 gfx::Rect start_bounds, target_bounds, middle_bounds; | |
379 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); | |
380 start_bounds.set_x(-90); | |
381 target_bounds.set_x(90); | |
382 | |
383 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
384 | |
385 delegate.SetOpacityFromAnimation(start_opacity); | |
386 delegate.SetBoundsFromAnimation(start_bounds); | |
387 | |
388 animator->ScheduleAnimation( | |
389 new LayerAnimationSequence( | |
390 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
391 | |
392 animator->ScheduleAnimation( | |
393 new LayerAnimationSequence( | |
394 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); | |
395 | |
396 EXPECT_TRUE(animator->IsAnimating()); | |
397 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
398 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
399 | |
400 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
401 | |
402 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
403 | |
404 EXPECT_TRUE(animator->IsAnimating()); | |
405 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
406 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds); | |
407 | |
408 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
409 | |
410 EXPECT_FALSE(animator->IsAnimating()); | |
411 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
412 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
413 } | |
414 | |
415 // schedule two animations on the same property | |
416 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) { | |
417 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
418 animator->SetAnimationForTest(NULL); | |
419 DummyLayerAnimationDelegate delegate; | |
420 animator->SetDelegate(&delegate); | |
421 | |
422 double start_opacity(0.0); | |
423 double middle_opacity(0.5); | |
424 double target_opacity(1.0); | |
425 | |
426 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
427 | |
428 delegate.SetOpacityFromAnimation(start_opacity); | |
429 | |
430 animator->ScheduleAnimation( | |
431 new LayerAnimationSequence( | |
432 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
433 | |
434 animator->ScheduleAnimation( | |
435 new LayerAnimationSequence( | |
436 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
437 | |
438 EXPECT_TRUE(animator->IsAnimating()); | |
439 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
440 | |
441 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
442 | |
443 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
444 | |
445 EXPECT_TRUE(animator->IsAnimating()); | |
446 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
447 | |
448 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
449 | |
450 EXPECT_TRUE(animator->IsAnimating()); | |
451 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
452 | |
453 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
454 | |
455 EXPECT_TRUE(animator->IsAnimating()); | |
456 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
457 | |
458 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
459 | |
460 EXPECT_FALSE(animator->IsAnimating()); | |
461 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
462 } | |
463 | |
464 // schedule {o}, {o,b}, {b} and ensure that {b} doesn't run right away. | |
465 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) { | |
466 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
467 animator->SetAnimationForTest(NULL); | |
468 DummyLayerAnimationDelegate delegate; | |
469 animator->SetDelegate(&delegate); | |
470 | |
471 double start_opacity(0.0); | |
472 double middle_opacity(0.5); | |
473 double target_opacity(1.0); | |
474 | |
475 gfx::Rect start_bounds, target_bounds, middle_bounds; | |
476 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50); | |
477 start_bounds.set_x(-90); | |
478 target_bounds.set_x(90); | |
479 | |
480 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
481 | |
482 delegate.SetOpacityFromAnimation(start_opacity); | |
483 delegate.SetBoundsFromAnimation(start_bounds); | |
484 | |
485 animator->ScheduleAnimation( | |
486 new LayerAnimationSequence( | |
487 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
488 | |
489 scoped_ptr<LayerAnimationSequence> bounds_and_opacity( | |
490 new LayerAnimationSequence( | |
491 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
492 | |
493 bounds_and_opacity->AddElement( | |
494 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)); | |
495 | |
496 animator->ScheduleAnimation(bounds_and_opacity.release()); | |
497 | |
498 animator->ScheduleAnimation( | |
499 new LayerAnimationSequence( | |
500 LayerAnimationElement::CreateBoundsElement(start_bounds, delta))); | |
501 | |
502 EXPECT_TRUE(animator->IsAnimating()); | |
503 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
504 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
505 | |
506 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
507 | |
508 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
509 | |
510 EXPECT_TRUE(animator->IsAnimating()); | |
511 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
512 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
513 | |
514 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
515 | |
516 EXPECT_TRUE(animator->IsAnimating()); | |
517 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
518 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
519 | |
520 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
521 | |
522 EXPECT_TRUE(animator->IsAnimating()); | |
523 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
524 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
525 | |
526 animator->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); | |
527 | |
528 EXPECT_TRUE(animator->IsAnimating()); | |
529 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
530 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
531 | |
532 animator->Step(start_time + base::TimeDelta::FromMilliseconds(4000)); | |
533 | |
534 EXPECT_FALSE(animator->IsAnimating()); | |
535 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
536 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
537 } | |
538 | |
539 // schedule {o} and then schedule {o} and {b} together | |
540 TEST(LayerAnimatorTest, ScheduleTogether) { | |
541 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
542 animator->SetAnimationForTest(NULL); | |
543 DummyLayerAnimationDelegate delegate; | |
544 animator->SetDelegate(&delegate); | |
545 | |
546 double start_opacity(0.0); | |
547 double target_opacity(1.0); | |
548 | |
549 gfx::Rect start_bounds, target_bounds, middle_bounds; | |
550 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50); | |
551 start_bounds.set_x(-90); | |
552 target_bounds.set_x(90); | |
553 | |
554 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
555 | |
556 delegate.SetOpacityFromAnimation(start_opacity); | |
557 delegate.SetBoundsFromAnimation(start_bounds); | |
558 | |
559 animator->ScheduleAnimation( | |
560 new LayerAnimationSequence( | |
561 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
562 | |
563 std::vector<LayerAnimationSequence*> sequences; | |
564 sequences.push_back(new LayerAnimationSequence( | |
565 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
566 sequences.push_back(new LayerAnimationSequence( | |
567 LayerAnimationElement::CreateBoundsElement(target_bounds, delta))); | |
568 | |
569 animator->ScheduleTogether(sequences); | |
570 | |
571 EXPECT_TRUE(animator->IsAnimating()); | |
572 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
573 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
574 | |
575 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
576 | |
577 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
578 | |
579 EXPECT_TRUE(animator->IsAnimating()); | |
580 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
581 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds); | |
582 | |
583 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
584 | |
585 EXPECT_FALSE(animator->IsAnimating()); | |
586 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
587 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds); | |
588 } | |
589 | |
590 // start animation (that can run immediately) | |
591 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) { | |
592 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
593 animator->SetAnimationForTest(NULL); | |
594 DummyLayerAnimationDelegate delegate; | |
595 animator->SetDelegate(&delegate); | |
596 | |
597 double start_opacity(0.0); | |
598 double middle_opacity(0.5); | |
599 double target_opacity(1.0); | |
600 | |
601 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
602 | |
603 delegate.SetOpacityFromAnimation(start_opacity); | |
604 | |
605 animator->StartAnimation( | |
606 new LayerAnimationSequence( | |
607 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
608 | |
609 EXPECT_TRUE(animator->IsAnimating()); | |
610 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
611 | |
612 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
613 | |
614 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
615 | |
616 EXPECT_TRUE(animator->IsAnimating()); | |
617 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
618 | |
619 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
620 | |
621 EXPECT_FALSE(animator->IsAnimating()); | |
622 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
623 } | |
624 | |
625 // preempt by immediately setting new target | |
626 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) { | |
627 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
628 animator->SetAnimationForTest(NULL); | |
629 DummyLayerAnimationDelegate delegate; | |
630 animator->SetDelegate(&delegate); | |
631 | |
632 double start_opacity(0.0); | |
633 double target_opacity(1.0); | |
634 | |
635 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
636 | |
637 delegate.SetOpacityFromAnimation(start_opacity); | |
638 | |
639 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); | |
640 | |
641 animator->StartAnimation( | |
642 new LayerAnimationSequence( | |
643 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
644 | |
645 animator->StartAnimation( | |
646 new LayerAnimationSequence( | |
647 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
648 | |
649 EXPECT_FALSE(animator->IsAnimating()); | |
650 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
651 } | |
652 | |
653 // preempt by animating to new target | |
654 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) { | |
655 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
656 animator->SetAnimationForTest(NULL); | |
657 DummyLayerAnimationDelegate delegate; | |
658 animator->SetDelegate(&delegate); | |
659 | |
660 double start_opacity(0.0); | |
661 double middle_opacity(0.5); | |
662 double target_opacity(1.0); | |
663 | |
664 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
665 | |
666 delegate.SetOpacityFromAnimation(start_opacity); | |
667 | |
668 animator->set_preemption_strategy( | |
669 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); | |
670 | |
671 animator->StartAnimation( | |
672 new LayerAnimationSequence( | |
673 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
674 | |
675 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
676 | |
677 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
678 | |
679 animator->StartAnimation( | |
680 new LayerAnimationSequence( | |
681 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
682 | |
683 EXPECT_TRUE(animator->IsAnimating()); | |
684 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
685 | |
686 animator->StartAnimation( | |
687 new LayerAnimationSequence( | |
688 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
689 | |
690 EXPECT_TRUE(animator->IsAnimating()); | |
691 | |
692 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
693 | |
694 EXPECT_TRUE(animator->IsAnimating()); | |
695 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), | |
696 0.5 * (start_opacity + middle_opacity)); | |
697 | |
698 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
699 | |
700 EXPECT_FALSE(animator->IsAnimating()); | |
701 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
702 } | |
703 | |
704 // preempt by enqueuing the new animation | |
705 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) { | |
706 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
707 animator->SetAnimationForTest(NULL); | |
708 DummyLayerAnimationDelegate delegate; | |
709 animator->SetDelegate(&delegate); | |
710 | |
711 double start_opacity(0.0); | |
712 double middle_opacity(0.5); | |
713 double target_opacity(1.0); | |
714 | |
715 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
716 | |
717 delegate.SetOpacityFromAnimation(start_opacity); | |
718 | |
719 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION); | |
720 | |
721 animator->StartAnimation( | |
722 new LayerAnimationSequence( | |
723 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
724 | |
725 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
726 | |
727 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
728 | |
729 animator->StartAnimation( | |
730 new LayerAnimationSequence( | |
731 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
732 | |
733 EXPECT_TRUE(animator->IsAnimating()); | |
734 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
735 | |
736 EXPECT_TRUE(animator->IsAnimating()); | |
737 | |
738 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
739 | |
740 EXPECT_TRUE(animator->IsAnimating()); | |
741 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
742 | |
743 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
744 | |
745 EXPECT_TRUE(animator->IsAnimating()); | |
746 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
747 | |
748 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
749 | |
750 EXPECT_FALSE(animator->IsAnimating()); | |
751 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
752 } | |
753 | |
754 // Start an animation when there are sequences waiting in the queue. In this | |
755 // case, all pending and running animations should be finished, and the new | |
756 // animation started. | |
757 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) { | |
758 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
759 animator->SetAnimationForTest(NULL); | |
760 DummyLayerAnimationDelegate delegate; | |
761 animator->SetDelegate(&delegate); | |
762 | |
763 double start_opacity(0.0); | |
764 double middle_opacity(0.5); | |
765 double target_opacity(1.0); | |
766 | |
767 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
768 | |
769 delegate.SetOpacityFromAnimation(start_opacity); | |
770 | |
771 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS); | |
772 | |
773 animator->StartAnimation( | |
774 new LayerAnimationSequence( | |
775 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
776 | |
777 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
778 | |
779 animator->Step(start_time + base::TimeDelta::FromMilliseconds(500)); | |
780 | |
781 animator->StartAnimation( | |
782 new LayerAnimationSequence( | |
783 LayerAnimationElement::CreateOpacityElement(middle_opacity, delta))); | |
784 | |
785 // Queue should now have two animations. Starting a third should replace the | |
786 // second. | |
787 animator->StartAnimation( | |
788 new LayerAnimationSequence( | |
789 LayerAnimationElement::CreateOpacityElement(start_opacity, delta))); | |
790 | |
791 EXPECT_TRUE(animator->IsAnimating()); | |
792 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
793 | |
794 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
795 | |
796 EXPECT_TRUE(animator->IsAnimating()); | |
797 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
798 | |
799 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500)); | |
800 | |
801 EXPECT_TRUE(animator->IsAnimating()); | |
802 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), middle_opacity); | |
803 | |
804 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
805 | |
806 EXPECT_FALSE(animator->IsAnimating()); | |
807 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
808 } | |
809 | |
810 // cyclic sequence | |
811 TEST(LayerAnimatorTest, CyclicSequences) { | |
812 scoped_ptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator()); | |
813 animator->SetAnimationForTest(NULL); | |
814 DummyLayerAnimationDelegate delegate; | |
815 animator->SetDelegate(&delegate); | |
816 | |
817 double start_opacity(0.0); | |
818 double target_opacity(1.0); | |
819 | |
820 base::TimeDelta delta = base::TimeDelta::FromSeconds(1); | |
821 | |
822 delegate.SetOpacityFromAnimation(start_opacity); | |
823 | |
824 scoped_ptr<LayerAnimationSequence> sequence( | |
825 new LayerAnimationSequence( | |
826 LayerAnimationElement::CreateOpacityElement(target_opacity, delta))); | |
827 | |
828 sequence->AddElement( | |
829 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)); | |
830 | |
831 sequence->set_is_cyclic(true); | |
832 | |
833 animator->StartAnimation(sequence.release()); | |
834 | |
835 base::TimeTicks start_time = animator->get_last_step_time_for_test(); | |
836 | |
837 animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000)); | |
838 | |
839 EXPECT_TRUE(animator->IsAnimating()); | |
840 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
841 | |
842 animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000)); | |
843 | |
844 EXPECT_TRUE(animator->IsAnimating()); | |
845 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity); | |
846 | |
847 animator->Step(start_time + base::TimeDelta::FromMilliseconds(3000)); | |
848 | |
849 EXPECT_TRUE(animator->IsAnimating()); | |
850 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity); | |
851 | |
852 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY); | |
853 | |
854 EXPECT_FALSE(animator->IsAnimating()); | |
855 } | |
856 | |
857 } // namespace | |
858 | |
859 } // namespace ui | |
OLD | NEW |