Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / compositor / layer_animator_unittest.cc
blob9a3df2d28d00af2cf19f91d54b1c5fe61aff2cc6
1 // Copyright (c) 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 "ui/compositor/layer_animator.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/stringprintf.h"
11 #include "base/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/compositor/layer_animation_delegate.h"
14 #include "ui/compositor/layer_animation_element.h"
15 #include "ui/compositor/layer_animation_sequence.h"
16 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
17 #include "ui/compositor/scoped_layer_animation_settings.h"
18 #include "ui/compositor/test/layer_animator_test_controller.h"
19 #include "ui/compositor/test/test_layer_animation_delegate.h"
20 #include "ui/compositor/test/test_layer_animation_observer.h"
21 #include "ui/compositor/test/test_utils.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/transform.h"
25 namespace ui {
27 namespace {
29 // Converts |color| to a string. Each component of the color is separated by a
30 // space and the order if A R G B.
31 std::string ColorToString(SkColor color) {
32 return base::StringPrintf("%d %d %d %d", SkColorGetA(color),
33 SkColorGetR(color), SkColorGetG(color),
34 SkColorGetB(color));
37 // Creates vector with two LayerAnimationSequences, based on |first| and
38 // |second| layer animation elements.
39 std::vector<LayerAnimationSequence*> CreateMultiSequence(
40 LayerAnimationElement* first,
41 LayerAnimationElement* second) {
42 LayerAnimationSequence* first_sequence = new LayerAnimationSequence();
43 first_sequence->AddElement(first);
44 LayerAnimationSequence* second_sequence = new LayerAnimationSequence();
45 second_sequence->AddElement(second);
47 std::vector<ui::LayerAnimationSequence*> animations;
48 animations.push_back(first_sequence);
49 animations.push_back(second_sequence);
50 return animations;
53 class TestImplicitAnimationObserver : public ImplicitAnimationObserver {
54 public:
55 explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed)
56 : animations_completed_(false),
57 notify_when_animator_destructed_(notify_when_animator_destructed) {
60 bool animations_completed() const { return animations_completed_; }
61 void set_animations_completed(bool completed) {
62 animations_completed_ = completed;
65 private:
66 // ImplicitAnimationObserver implementation
67 virtual void OnImplicitAnimationsCompleted() OVERRIDE {
68 animations_completed_ = true;
71 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE {
72 return notify_when_animator_destructed_;
75 bool animations_completed_;
76 bool notify_when_animator_destructed_;
78 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver);
81 // When notified that an animation has ended, stops all other animations.
82 class DeletingLayerAnimationObserver : public LayerAnimationObserver {
83 public:
84 DeletingLayerAnimationObserver(LayerAnimator* animator,
85 LayerAnimationSequence* sequence)
86 : animator_(animator),
87 sequence_(sequence) {
90 virtual void OnLayerAnimationEnded(
91 LayerAnimationSequence* sequence) OVERRIDE {
92 animator_->StopAnimating();
95 virtual void OnLayerAnimationAborted(
96 LayerAnimationSequence* sequence) OVERRIDE {
99 virtual void OnLayerAnimationScheduled(
100 LayerAnimationSequence* sequence) OVERRIDE {
103 private:
104 LayerAnimator* animator_;
105 LayerAnimationSequence* sequence_;
107 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver);
110 class TestLayerAnimator : public LayerAnimator {
111 public:
112 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)) {}
114 protected:
115 virtual ~TestLayerAnimator() {}
117 virtual void ProgressAnimation(LayerAnimationSequence* sequence,
118 base::TimeTicks now) OVERRIDE {
119 EXPECT_TRUE(HasAnimation(sequence));
120 LayerAnimator::ProgressAnimation(sequence, now);
123 private:
124 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
127 // The test layer animation sequence updates a live instances count when it is
128 // created and destroyed.
129 class TestLayerAnimationSequence : public LayerAnimationSequence {
130 public:
131 TestLayerAnimationSequence(LayerAnimationElement* element,
132 int* num_live_instances)
133 : LayerAnimationSequence(element),
134 num_live_instances_(num_live_instances) {
135 (*num_live_instances_)++;
138 virtual ~TestLayerAnimationSequence() {
139 (*num_live_instances_)--;
142 private:
143 int* num_live_instances_;
145 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence);
148 } // namespace
150 // Checks that setting a property on an implicit animator causes an animation to
151 // happen.
152 TEST(LayerAnimatorTest, ImplicitAnimation) {
153 scoped_refptr<LayerAnimator> animator(
154 LayerAnimator::CreateImplicitAnimator());
155 AnimationContainerElement* element = animator.get();
156 animator->set_disable_timer_for_test(true);
157 TestLayerAnimationDelegate delegate;
158 animator->SetDelegate(&delegate);
159 base::TimeTicks now = base::TimeTicks::Now();
160 animator->SetBrightness(0.5);
161 EXPECT_TRUE(animator->is_animating());
162 element->Step(now + base::TimeDelta::FromSeconds(1));
163 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
166 // Checks that if the animator is a default animator, that implicit animations
167 // are not started.
168 TEST(LayerAnimatorTest, NoImplicitAnimation) {
169 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
170 animator->set_disable_timer_for_test(true);
171 TestLayerAnimationDelegate delegate;
172 animator->SetDelegate(&delegate);
173 animator->SetBrightness(0.5);
174 EXPECT_FALSE(animator->is_animating());
175 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
178 // Checks that StopAnimatingProperty stops animation for that property, and also
179 // skips the stopped animation to the end.
180 TEST(LayerAnimatorTest, StopAnimatingProperty) {
181 scoped_refptr<LayerAnimator> animator(
182 LayerAnimator::CreateImplicitAnimator());
183 animator->set_disable_timer_for_test(true);
184 TestLayerAnimationDelegate delegate;
185 animator->SetDelegate(&delegate);
186 double target_opacity(0.5);
187 gfx::Rect target_bounds(0, 0, 50, 50);
188 animator->SetOpacity(target_opacity);
189 animator->SetBounds(target_bounds);
190 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY);
191 EXPECT_TRUE(animator->is_animating());
192 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
193 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS);
194 EXPECT_FALSE(animator->is_animating());
195 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
198 // Checks that multiple running animation for separate properties can be stopped
199 // simultaneously and that all animations are advanced to their target values.
200 TEST(LayerAnimatorTest, StopAnimating) {
201 scoped_refptr<LayerAnimator> animator(
202 LayerAnimator::CreateImplicitAnimator());
203 animator->set_disable_timer_for_test(true);
204 TestLayerAnimationDelegate delegate;
205 animator->SetDelegate(&delegate);
206 double target_opacity(0.5);
207 gfx::Rect target_bounds(0, 0, 50, 50);
208 animator->SetOpacity(target_opacity);
209 animator->SetBounds(target_bounds);
210 EXPECT_TRUE(animator->is_animating());
211 animator->StopAnimating();
212 EXPECT_FALSE(animator->is_animating());
213 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
214 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
217 // Checks that multiple running animation for separate properties can be stopped
218 // simultaneously and that all animations are advanced to their target values.
219 TEST(LayerAnimatorTest, AbortAllAnimations) {
220 scoped_refptr<LayerAnimator> animator(
221 LayerAnimator::CreateImplicitAnimator());
222 animator->set_disable_timer_for_test(true);
223 TestLayerAnimationDelegate delegate;
224 double initial_opacity(1.0);
225 gfx::Rect initial_bounds(0, 0, 10, 10);
226 delegate.SetOpacityFromAnimation(initial_opacity);
227 delegate.SetBoundsFromAnimation(initial_bounds);
228 animator->SetDelegate(&delegate);
229 double target_opacity(0.5);
230 gfx::Rect target_bounds(0, 0, 50, 50);
231 animator->SetOpacity(target_opacity);
232 animator->SetBounds(target_bounds);
233 EXPECT_TRUE(animator->is_animating());
234 animator->AbortAllAnimations();
235 EXPECT_FALSE(animator->is_animating());
236 EXPECT_FLOAT_EQ(initial_opacity, delegate.GetOpacityForAnimation());
237 CheckApproximatelyEqual(initial_bounds, delegate.GetBoundsForAnimation());
240 // Schedule a non-threaded animation that can run immediately. This is the
241 // trivial case and should result in the animation being started immediately.
242 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) {
243 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
244 AnimationContainerElement* element = animator.get();
245 animator->set_disable_timer_for_test(true);
246 TestLayerAnimationDelegate delegate;
247 animator->SetDelegate(&delegate);
249 double start_brightness(0.0);
250 double middle_brightness(0.5);
251 double target_brightness(1.0);
253 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
255 delegate.SetBrightnessFromAnimation(start_brightness);
257 animator->ScheduleAnimation(
258 new LayerAnimationSequence(
259 LayerAnimationElement::CreateBrightnessElement(target_brightness,
260 delta)));
262 EXPECT_TRUE(animator->is_animating());
263 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
265 base::TimeTicks start_time = animator->last_step_time();
267 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
269 EXPECT_TRUE(animator->is_animating());
270 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
272 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
274 EXPECT_FALSE(animator->is_animating());
275 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
278 // Schedule a threaded animation that can run immediately.
279 TEST(LayerAnimatorTest, ScheduleThreadedAnimationThatCanRunImmediately) {
280 double epsilon = 0.00001;
281 LayerAnimatorTestController test_controller(
282 LayerAnimator::CreateDefaultAnimator());
283 AnimationContainerElement* element = test_controller.animator();
284 test_controller.animator()->set_disable_timer_for_test(true);
285 TestLayerAnimationDelegate delegate;
286 test_controller.animator()->SetDelegate(&delegate);
288 double start_opacity(0.0);
289 double target_opacity(1.0);
291 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
293 delegate.SetOpacityFromAnimation(start_opacity);
295 test_controller.animator()->ScheduleAnimation(
296 new LayerAnimationSequence(
297 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
299 EXPECT_TRUE(test_controller.animator()->is_animating());
300 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
302 base::TimeTicks start_time = test_controller.animator()->last_step_time();
303 base::TimeTicks effective_start = start_time + delta;
305 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
306 cc::AnimationEvent::Started,
308 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
309 animation_group_id(),
310 cc::Animation::Opacity,
311 (effective_start - base::TimeTicks()).InSecondsF()));
313 element->Step(effective_start + delta/2);
315 EXPECT_TRUE(test_controller.animator()->is_animating());
316 EXPECT_NEAR(
317 0.5,
318 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
319 last_progressed_fraction(),
320 epsilon);
322 element->Step(effective_start + delta);
324 EXPECT_FALSE(test_controller.animator()->is_animating());
325 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
328 // Schedule two non-threaded animations on separate properties. Both animations
329 // should start immediately and should progress in lock step.
330 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) {
331 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
332 AnimationContainerElement* element = animator.get();
333 animator->set_disable_timer_for_test(true);
334 TestLayerAnimationDelegate delegate;
335 animator->SetDelegate(&delegate);
337 double start_brightness(0.0);
338 double middle_brightness(0.5);
339 double target_brightness(1.0);
341 gfx::Rect start_bounds, target_bounds, middle_bounds;
342 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
343 start_bounds.set_x(-90);
344 target_bounds.set_x(90);
346 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
348 delegate.SetBrightnessFromAnimation(start_brightness);
349 delegate.SetBoundsFromAnimation(start_bounds);
351 animator->ScheduleAnimation(
352 new LayerAnimationSequence(
353 LayerAnimationElement::CreateBrightnessElement(target_brightness,
354 delta)));
356 animator->ScheduleAnimation(
357 new LayerAnimationSequence(
358 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
360 EXPECT_TRUE(animator->is_animating());
361 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
362 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
364 base::TimeTicks start_time = animator->last_step_time();
366 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
368 EXPECT_TRUE(animator->is_animating());
369 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
370 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
372 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
374 EXPECT_FALSE(animator->is_animating());
375 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
376 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
379 // Schedule a threaded and a non-threaded animation on separate properties. Both
380 // animations should progress in lock step.
381 TEST(LayerAnimatorTest, ScheduleThreadedAndNonThreadedAnimations) {
382 double epsilon = 0.00001;
383 LayerAnimatorTestController test_controller(
384 LayerAnimator::CreateDefaultAnimator());
385 AnimationContainerElement* element = test_controller.animator();
386 test_controller.animator()->set_disable_timer_for_test(true);
387 TestLayerAnimationDelegate delegate;
388 test_controller.animator()->SetDelegate(&delegate);
390 double start_opacity(0.0);
391 double target_opacity(1.0);
393 gfx::Rect start_bounds, target_bounds, middle_bounds;
394 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
395 start_bounds.set_x(-90);
396 target_bounds.set_x(90);
398 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
400 delegate.SetOpacityFromAnimation(start_opacity);
401 delegate.SetBoundsFromAnimation(start_bounds);
403 std::vector<LayerAnimationSequence*> animations;
404 animations.push_back(
405 new LayerAnimationSequence(
406 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
408 animations.push_back(
409 new LayerAnimationSequence(
410 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
412 test_controller.animator()->ScheduleTogether(animations);
414 EXPECT_TRUE(test_controller.animator()->is_animating());
415 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
416 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
418 base::TimeTicks start_time = test_controller.animator()->last_step_time();
419 base::TimeTicks effective_start = start_time + delta;
421 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
422 cc::AnimationEvent::Started,
424 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
425 animation_group_id(),
426 cc::Animation::Opacity,
427 (effective_start - base::TimeTicks()).InSecondsF()));
429 element->Step(effective_start + delta/2);
431 EXPECT_TRUE(test_controller.animator()->is_animating());
432 EXPECT_NEAR(
433 0.5,
434 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
435 last_progressed_fraction(),
436 epsilon);
437 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
439 element->Step(effective_start + delta);
441 EXPECT_FALSE(test_controller.animator()->is_animating());
442 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
443 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
446 // Schedule two animations on the same property. In this case, the two
447 // animations should run one after another.
448 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) {
449 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
450 AnimationContainerElement* element = animator.get();
451 animator->set_disable_timer_for_test(true);
452 TestLayerAnimationDelegate delegate;
453 animator->SetDelegate(&delegate);
455 double start_brightness(0.0);
456 double middle_brightness(0.5);
457 double target_brightness(1.0);
459 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
461 delegate.SetBrightnessFromAnimation(start_brightness);
463 animator->ScheduleAnimation(
464 new LayerAnimationSequence(
465 LayerAnimationElement::CreateBrightnessElement(target_brightness,
466 delta)));
468 animator->ScheduleAnimation(
469 new LayerAnimationSequence(
470 LayerAnimationElement::CreateBrightnessElement(start_brightness,
471 delta)));
473 EXPECT_TRUE(animator->is_animating());
474 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
476 base::TimeTicks start_time = animator->last_step_time();
478 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
480 EXPECT_TRUE(animator->is_animating());
481 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
483 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
485 EXPECT_TRUE(animator->is_animating());
486 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
488 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
490 EXPECT_TRUE(animator->is_animating());
491 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
493 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
495 EXPECT_FALSE(animator->is_animating());
496 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
499 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
500 // is, ensure that all animations targetting a particular property are run in
501 // order.
502 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) {
503 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
504 AnimationContainerElement* element = animator.get();
505 animator->set_disable_timer_for_test(true);
506 TestLayerAnimationDelegate delegate;
507 animator->SetDelegate(&delegate);
509 double start_grayscale(0.0);
510 double middle_grayscale(0.5);
511 double target_grayscale(1.0);
513 gfx::Rect start_bounds, target_bounds, middle_bounds;
514 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
515 start_bounds.set_x(-90);
516 target_bounds.set_x(90);
518 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
520 delegate.SetGrayscaleFromAnimation(start_grayscale);
521 delegate.SetBoundsFromAnimation(start_bounds);
523 animator->ScheduleAnimation(
524 new LayerAnimationSequence(
525 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
526 delta)));
528 scoped_ptr<LayerAnimationSequence> bounds_and_grayscale(
529 new LayerAnimationSequence(
530 LayerAnimationElement::CreateGrayscaleElement(start_grayscale,
531 delta)));
533 bounds_and_grayscale->AddElement(
534 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
536 animator->ScheduleAnimation(bounds_and_grayscale.release());
538 animator->ScheduleAnimation(
539 new LayerAnimationSequence(
540 LayerAnimationElement::CreateBoundsElement(start_bounds, delta)));
542 EXPECT_TRUE(animator->is_animating());
543 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
544 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
546 base::TimeTicks start_time = animator->last_step_time();
548 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
550 EXPECT_TRUE(animator->is_animating());
551 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
552 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
554 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
556 EXPECT_TRUE(animator->is_animating());
557 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
558 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
560 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
562 EXPECT_TRUE(animator->is_animating());
563 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
564 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
566 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
568 EXPECT_TRUE(animator->is_animating());
569 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
570 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
572 element->Step(start_time + base::TimeDelta::FromMilliseconds(4000));
574 EXPECT_FALSE(animator->is_animating());
575 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
576 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
579 // Schedule {g} and then schedule {g} and {b} together. In this case, since
580 // ScheduleTogether is being used, the bounds animation should not start until
581 // the second grayscale animation starts.
582 TEST(LayerAnimatorTest, ScheduleTogether) {
583 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
584 AnimationContainerElement* element = animator.get();
585 animator->set_disable_timer_for_test(true);
586 TestLayerAnimationDelegate delegate;
587 animator->SetDelegate(&delegate);
589 double start_grayscale(0.0);
590 double target_grayscale(1.0);
592 gfx::Rect start_bounds, target_bounds, middle_bounds;
593 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50);
594 start_bounds.set_x(-90);
595 target_bounds.set_x(90);
597 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
599 delegate.SetGrayscaleFromAnimation(start_grayscale);
600 delegate.SetBoundsFromAnimation(start_bounds);
602 animator->ScheduleAnimation(
603 new LayerAnimationSequence(
604 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
605 delta)));
607 std::vector<LayerAnimationSequence*> sequences;
608 sequences.push_back(new LayerAnimationSequence(
609 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta)));
610 sequences.push_back(new LayerAnimationSequence(
611 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
613 animator->ScheduleTogether(sequences);
615 EXPECT_TRUE(animator->is_animating());
616 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
617 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
619 base::TimeTicks start_time = animator->last_step_time();
621 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
623 EXPECT_TRUE(animator->is_animating());
624 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
625 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
627 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
629 EXPECT_FALSE(animator->is_animating());
630 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
631 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
634 // Start non-threaded animation (that can run immediately). This is the trivial
635 // case (see the trival case for ScheduleAnimation).
636 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) {
637 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
638 AnimationContainerElement* element = animator.get();
639 animator->set_disable_timer_for_test(true);
640 TestLayerAnimationDelegate delegate;
641 animator->SetDelegate(&delegate);
643 double start_brightness(0.0);
644 double middle_brightness(0.5);
645 double target_brightness(1.0);
647 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
649 delegate.SetBrightnessFromAnimation(start_brightness);
651 animator->StartAnimation(
652 new LayerAnimationSequence(
653 LayerAnimationElement::CreateBrightnessElement(target_brightness,
654 delta)));
656 EXPECT_TRUE(animator->is_animating());
657 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
659 base::TimeTicks start_time = animator->last_step_time();
661 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
663 EXPECT_TRUE(animator->is_animating());
664 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
666 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
668 EXPECT_FALSE(animator->is_animating());
669 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
672 // Start threaded animation (that can run immediately).
673 TEST(LayerAnimatorTest, StartThreadedAnimationThatCanRunImmediately) {
674 double epsilon = 0.00001;
675 LayerAnimatorTestController test_controller(
676 LayerAnimator::CreateDefaultAnimator());
677 AnimationContainerElement* element = test_controller.animator();
678 test_controller.animator()->set_disable_timer_for_test(true);
679 TestLayerAnimationDelegate delegate;
680 test_controller.animator()->SetDelegate(&delegate);
682 double start_opacity(0.0);
683 double target_opacity(1.0);
685 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
687 delegate.SetOpacityFromAnimation(start_opacity);
689 test_controller.animator()->StartAnimation(
690 new LayerAnimationSequence(
691 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
693 EXPECT_TRUE(test_controller.animator()->is_animating());
694 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
696 base::TimeTicks start_time = test_controller.animator()->last_step_time();
697 base::TimeTicks effective_start = start_time + delta;
699 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
700 cc::AnimationEvent::Started,
702 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
703 animation_group_id(),
704 cc::Animation::Opacity,
705 (effective_start - base::TimeTicks()).InSecondsF()));
707 element->Step(effective_start + delta/2);
709 EXPECT_TRUE(test_controller.animator()->is_animating());
710 EXPECT_NEAR(
711 0.5,
712 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
713 last_progressed_fraction(),
714 epsilon);
716 element->Step(effective_start + delta);
717 EXPECT_FALSE(test_controller.animator()->is_animating());
718 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
721 // Preempt by immediately setting new target.
722 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) {
723 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
724 animator->set_disable_timer_for_test(true);
725 TestLayerAnimationDelegate delegate;
726 animator->SetDelegate(&delegate);
728 double start_opacity(0.0);
729 double target_opacity(1.0);
731 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
733 delegate.SetOpacityFromAnimation(start_opacity);
735 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
737 animator->StartAnimation(
738 new LayerAnimationSequence(
739 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
741 animator->StartAnimation(
742 new LayerAnimationSequence(
743 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
745 EXPECT_FALSE(animator->is_animating());
746 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
749 // Preempt by animating to new target, with a non-threaded animation.
750 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) {
751 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
752 AnimationContainerElement* element = animator.get();
753 animator->set_disable_timer_for_test(true);
754 TestLayerAnimationDelegate delegate;
755 animator->SetDelegate(&delegate);
757 double start_brightness(0.0);
758 double middle_brightness(0.5);
759 double target_brightness(1.0);
761 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
763 delegate.SetBrightnessFromAnimation(start_brightness);
765 animator->set_preemption_strategy(
766 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
768 animator->StartAnimation(
769 new LayerAnimationSequence(
770 LayerAnimationElement::CreateBrightnessElement(target_brightness,
771 delta)));
773 base::TimeTicks start_time = animator->last_step_time();
775 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
777 animator->StartAnimation(
778 new LayerAnimationSequence(
779 LayerAnimationElement::CreateBrightnessElement(start_brightness,
780 delta)));
782 EXPECT_TRUE(animator->is_animating());
783 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
785 animator->StartAnimation(
786 new LayerAnimationSequence(
787 LayerAnimationElement::CreateBrightnessElement(start_brightness,
788 delta)));
790 EXPECT_TRUE(animator->is_animating());
792 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
794 EXPECT_TRUE(animator->is_animating());
795 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
796 0.5 * (start_brightness + middle_brightness));
798 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
800 EXPECT_FALSE(animator->is_animating());
801 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
804 // Preempt by animating to new target, with a threaded animation.
805 TEST(LayerAnimatorTest, PreemptThreadedByImmediatelyAnimatingToNewTarget) {
806 double epsilon = 0.00001;
807 LayerAnimatorTestController test_controller(
808 LayerAnimator::CreateDefaultAnimator());
809 AnimationContainerElement* element = test_controller.animator();
810 test_controller.animator()->set_disable_timer_for_test(true);
811 TestLayerAnimationDelegate delegate;
812 test_controller.animator()->SetDelegate(&delegate);
814 double start_opacity(0.0);
815 double middle_opacity(0.5);
816 double target_opacity(1.0);
818 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
820 delegate.SetOpacityFromAnimation(start_opacity);
822 test_controller.animator()->set_preemption_strategy(
823 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
825 test_controller.animator()->StartAnimation(
826 new LayerAnimationSequence(
827 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
829 base::TimeTicks start_time = test_controller.animator()->last_step_time();
830 base::TimeTicks effective_start = start_time + delta;
832 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
833 cc::AnimationEvent::Started,
835 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
836 animation_group_id(),
837 cc::Animation::Opacity,
838 (effective_start - base::TimeTicks()).InSecondsF()));
840 element->Step(effective_start + delta/2);
842 test_controller.animator()->StartAnimation(
843 new LayerAnimationSequence(
844 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
846 EXPECT_TRUE(test_controller.animator()->is_animating());
847 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
849 test_controller.animator()->StartAnimation(
850 new LayerAnimationSequence(
851 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
853 EXPECT_TRUE(test_controller.animator()->is_animating());
855 base::TimeTicks second_effective_start = effective_start + delta;
857 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
858 cc::AnimationEvent::Started,
860 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
861 animation_group_id(),
862 cc::Animation::Opacity,
863 (second_effective_start - base::TimeTicks()).InSecondsF()));
865 element->Step(second_effective_start + delta/2);
867 EXPECT_TRUE(test_controller.animator()->is_animating());
868 EXPECT_NEAR(
869 0.5,
870 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
871 last_progressed_fraction(),
872 epsilon);
874 element->Step(second_effective_start + delta);
876 EXPECT_FALSE(test_controller.animator()->is_animating());
877 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
880 // Preempt by enqueuing the new animation.
881 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) {
882 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
883 AnimationContainerElement* element = animator.get();
884 animator->set_disable_timer_for_test(true);
885 TestLayerAnimationDelegate delegate;
886 animator->SetDelegate(&delegate);
888 double start_brightness(0.0);
889 double middle_brightness(0.5);
890 double target_brightness(1.0);
892 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
894 delegate.SetBrightnessFromAnimation(start_brightness);
896 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
898 animator->StartAnimation(
899 new LayerAnimationSequence(
900 LayerAnimationElement::CreateBrightnessElement(target_brightness,
901 delta)));
903 base::TimeTicks start_time = animator->last_step_time();
905 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
907 animator->StartAnimation(
908 new LayerAnimationSequence(
909 LayerAnimationElement::CreateBrightnessElement(start_brightness,
910 delta)));
912 EXPECT_TRUE(animator->is_animating());
913 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
915 EXPECT_TRUE(animator->is_animating());
917 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
919 EXPECT_TRUE(animator->is_animating());
920 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
922 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
924 EXPECT_TRUE(animator->is_animating());
925 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
927 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
929 EXPECT_FALSE(animator->is_animating());
930 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
933 // Start an animation when there are sequences waiting in the queue. In this
934 // case, all pending and running animations should be finished, and the new
935 // animation started.
936 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) {
937 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
938 AnimationContainerElement* element = animator.get();
939 animator->set_disable_timer_for_test(true);
940 TestLayerAnimationDelegate delegate;
941 animator->SetDelegate(&delegate);
943 double start_brightness(0.0);
944 double middle_brightness(0.5);
945 double target_brightness(1.0);
947 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
949 delegate.SetBrightnessFromAnimation(start_brightness);
951 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
953 animator->StartAnimation(
954 new LayerAnimationSequence(
955 LayerAnimationElement::CreateBrightnessElement(target_brightness,
956 delta)));
958 base::TimeTicks start_time = animator->last_step_time();
960 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
962 animator->StartAnimation(
963 new LayerAnimationSequence(
964 LayerAnimationElement::CreateBrightnessElement(middle_brightness,
965 delta)));
967 // Queue should now have two animations. Starting a third should replace the
968 // second.
969 animator->StartAnimation(
970 new LayerAnimationSequence(
971 LayerAnimationElement::CreateBrightnessElement(start_brightness,
972 delta)));
974 EXPECT_TRUE(animator->is_animating());
975 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
977 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
979 EXPECT_TRUE(animator->is_animating());
980 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
982 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
984 EXPECT_TRUE(animator->is_animating());
985 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
987 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
989 EXPECT_FALSE(animator->is_animating());
990 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
993 TEST(LayerAnimatorTest, StartTogetherSetsLastStepTime) {
994 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
995 animator->set_disable_timer_for_test(true);
996 TestLayerAnimationDelegate delegate;
997 animator->SetDelegate(&delegate);
999 double start_grayscale(0.0);
1000 double target_grayscale(1.0);
1001 double start_brightness(0.1);
1002 double target_brightness(0.9);
1004 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1006 delegate.SetGrayscaleFromAnimation(start_grayscale);
1007 delegate.SetBrightnessFromAnimation(start_brightness);
1009 animator->set_preemption_strategy(
1010 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1012 animator->set_last_step_time(base::TimeTicks());
1014 animator->StartTogether(
1015 CreateMultiSequence(
1016 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1017 delta),
1018 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1019 delta)
1022 // If last step time was not set correctly, the resulting delta should be
1023 // miniscule (fractions of a millisecond). If set correctly, then the delta
1024 // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1025 // though a much smaller value would probably have sufficed.
1026 delta = base::TimeTicks::Now() - animator->last_step_time();
1027 EXPECT_GT(60.0, delta.InSecondsF());
1030 //-------------------------------------------------------
1031 // Preempt by immediately setting new target.
1032 TEST(LayerAnimatorTest, MultiPreemptBySettingNewTarget) {
1033 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1034 animator->set_disable_timer_for_test(true);
1035 TestLayerAnimationDelegate delegate;
1036 animator->SetDelegate(&delegate);
1038 double start_opacity(0.0);
1039 double target_opacity(1.0);
1040 double start_brightness(0.1);
1041 double target_brightness(0.9);
1043 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1045 delegate.SetOpacityFromAnimation(start_opacity);
1046 delegate.SetBrightnessFromAnimation(start_brightness);
1048 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1050 animator->StartTogether(
1051 CreateMultiSequence(
1052 LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1053 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1054 delta)
1057 animator->StartTogether(
1058 CreateMultiSequence(
1059 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1060 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1061 delta)
1064 EXPECT_FALSE(animator->is_animating());
1065 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1066 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1069 // Preempt by animating to new target.
1070 TEST(LayerAnimatorTest, MultiPreemptByImmediatelyAnimatingToNewTarget) {
1071 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1072 AnimationContainerElement* element = animator.get();
1073 animator->set_disable_timer_for_test(true);
1074 TestLayerAnimationDelegate delegate;
1075 animator->SetDelegate(&delegate);
1077 double start_grayscale(0.0);
1078 double middle_grayscale(0.5);
1079 double target_grayscale(1.0);
1081 double start_brightness(0.1);
1082 double middle_brightness(0.2);
1083 double target_brightness(0.3);
1085 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1087 delegate.SetGrayscaleFromAnimation(start_grayscale);
1088 delegate.SetBrightnessFromAnimation(start_brightness);
1090 animator->set_preemption_strategy(
1091 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1093 animator->StartTogether(
1094 CreateMultiSequence(
1095 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1096 delta),
1097 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1098 delta)
1101 base::TimeTicks start_time = animator->last_step_time();
1103 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1105 animator->StartTogether(
1106 CreateMultiSequence(
1107 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1108 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1109 delta)));
1111 EXPECT_TRUE(animator->is_animating());
1112 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1113 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1115 animator->StartTogether(
1116 CreateMultiSequence(
1117 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1118 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1119 delta)));
1121 EXPECT_TRUE(animator->is_animating());
1123 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1125 EXPECT_TRUE(animator->is_animating());
1126 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(),
1127 0.5 * (start_grayscale + middle_grayscale));
1128 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
1129 0.5 * (start_brightness + middle_brightness));
1131 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1133 EXPECT_FALSE(animator->is_animating());
1134 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1135 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1138 // Preempt a threaded animation by animating to new target.
1139 TEST(LayerAnimatorTest, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget) {
1140 double epsilon = 0.00001;
1141 LayerAnimatorTestController test_controller(
1142 LayerAnimator::CreateDefaultAnimator());
1143 AnimationContainerElement* element = test_controller.animator();
1144 test_controller.animator()->set_disable_timer_for_test(true);
1145 TestLayerAnimationDelegate delegate;
1146 test_controller.animator()->SetDelegate(&delegate);
1148 double start_opacity(0.0);
1149 double middle_opacity(0.5);
1150 double target_opacity(1.0);
1152 double start_brightness(0.1);
1153 double middle_brightness(0.2);
1154 double target_brightness(0.3);
1156 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1158 delegate.SetOpacityFromAnimation(start_opacity);
1159 delegate.SetBrightnessFromAnimation(start_brightness);
1161 test_controller.animator()->set_preemption_strategy(
1162 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1164 test_controller.animator()->StartTogether(
1165 CreateMultiSequence(
1166 LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1167 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1168 delta)
1171 base::TimeTicks start_time = test_controller.animator()->last_step_time();
1172 base::TimeTicks effective_start = start_time + delta;
1174 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1175 cc::AnimationEvent::Started,
1177 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1178 animation_group_id(),
1179 cc::Animation::Opacity,
1180 (effective_start - base::TimeTicks()).InSecondsF()));
1182 element->Step(effective_start + delta/2);
1184 test_controller.animator()->StartTogether(
1185 CreateMultiSequence(
1186 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1187 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1188 delta)));
1190 EXPECT_TRUE(test_controller.animator()->is_animating());
1191 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
1192 EXPECT_NEAR(delegate.GetBrightnessForAnimation(), middle_brightness, epsilon);
1194 test_controller.animator()->StartTogether(
1195 CreateMultiSequence(
1196 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1197 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1198 delta)));
1200 EXPECT_TRUE(test_controller.animator()->is_animating());
1202 base::TimeTicks second_effective_start = effective_start + delta;
1204 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1205 cc::AnimationEvent::Started,
1207 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1208 animation_group_id(),
1209 cc::Animation::Opacity,
1210 (second_effective_start - base::TimeTicks()).InSecondsF()));
1212 element->Step(second_effective_start + delta/2);
1214 EXPECT_TRUE(test_controller.animator()->is_animating());
1215 EXPECT_NEAR(
1216 0.5,
1217 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1218 last_progressed_fraction(),
1219 epsilon);
1220 EXPECT_NEAR(delegate.GetBrightnessForAnimation(),
1221 0.5 * (start_brightness + middle_brightness),
1222 epsilon);
1224 element->Step(second_effective_start + delta);
1226 EXPECT_FALSE(test_controller.animator()->is_animating());
1227 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1228 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1231 // Preempt by enqueuing the new animation.
1232 TEST(LayerAnimatorTest, MultiPreemptEnqueueNewAnimation) {
1233 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1234 AnimationContainerElement* element = animator.get();
1235 animator->set_disable_timer_for_test(true);
1236 TestLayerAnimationDelegate delegate;
1237 animator->SetDelegate(&delegate);
1239 double start_grayscale(0.0);
1240 double middle_grayscale(0.5);
1241 double target_grayscale(1.0);
1243 double start_brightness(0.1);
1244 double middle_brightness(0.2);
1245 double target_brightness(0.3);
1247 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1249 delegate.SetGrayscaleFromAnimation(start_grayscale);
1250 delegate.SetBrightnessFromAnimation(start_brightness);
1252 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1254 animator->StartTogether(
1255 CreateMultiSequence(
1256 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1257 delta),
1258 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1259 delta)));
1261 base::TimeTicks start_time = animator->last_step_time();
1263 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1265 animator->StartTogether(
1266 CreateMultiSequence(
1267 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1268 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1269 delta)));
1271 EXPECT_TRUE(animator->is_animating());
1272 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1273 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1275 EXPECT_TRUE(animator->is_animating());
1277 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1279 EXPECT_TRUE(animator->is_animating());
1280 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1281 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1283 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1285 EXPECT_TRUE(animator->is_animating());
1286 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1287 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1289 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1291 EXPECT_FALSE(animator->is_animating());
1292 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1293 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1296 // Start an animation when there are sequences waiting in the queue. In this
1297 // case, all pending and running animations should be finished, and the new
1298 // animation started.
1299 TEST(LayerAnimatorTest, MultiPreemptByReplacingQueuedAnimations) {
1300 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1301 AnimationContainerElement* element = animator.get();
1302 animator->set_disable_timer_for_test(true);
1303 TestLayerAnimationDelegate delegate;
1304 animator->SetDelegate(&delegate);
1306 double start_grayscale(0.0);
1307 double middle_grayscale(0.5);
1308 double target_grayscale(1.0);
1310 double start_brightness(0.1);
1311 double middle_brightness(0.2);
1312 double target_brightness(0.3);
1314 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1316 delegate.SetGrayscaleFromAnimation(start_grayscale);
1317 delegate.SetBrightnessFromAnimation(start_brightness);
1319 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
1321 animator->StartTogether(
1322 CreateMultiSequence(
1323 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1324 delta),
1325 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1326 delta)));
1328 base::TimeTicks start_time = animator->last_step_time();
1330 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1332 animator->StartTogether(
1333 CreateMultiSequence(
1334 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale,
1335 delta),
1336 LayerAnimationElement::CreateBrightnessElement(middle_brightness,
1337 delta)));
1339 // Queue should now have two animations. Starting a third should replace the
1340 // second.
1341 animator->StartTogether(
1342 CreateMultiSequence(
1343 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1344 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1345 delta)));
1347 EXPECT_TRUE(animator->is_animating());
1348 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1349 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1351 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1353 EXPECT_TRUE(animator->is_animating());
1354 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1355 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1357 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1359 EXPECT_TRUE(animator->is_animating());
1360 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1361 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1363 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1365 EXPECT_FALSE(animator->is_animating());
1366 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1367 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1369 //-------------------------------------------------------
1370 // Test that non-threaded cyclic sequences continue to animate.
1371 TEST(LayerAnimatorTest, CyclicSequences) {
1372 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1373 AnimationContainerElement* element = animator.get();
1374 animator->set_disable_timer_for_test(true);
1375 TestLayerAnimationDelegate delegate;
1376 animator->SetDelegate(&delegate);
1378 double start_brightness(0.0);
1379 double target_brightness(1.0);
1381 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1383 delegate.SetBrightnessFromAnimation(start_brightness);
1385 scoped_ptr<LayerAnimationSequence> sequence(
1386 new LayerAnimationSequence(
1387 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1388 delta)));
1390 sequence->AddElement(
1391 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
1393 sequence->set_is_cyclic(true);
1395 animator->StartAnimation(sequence.release());
1397 base::TimeTicks start_time = animator->last_step_time();
1399 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1401 EXPECT_TRUE(animator->is_animating());
1402 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1404 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1406 EXPECT_TRUE(animator->is_animating());
1407 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1409 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
1411 EXPECT_TRUE(animator->is_animating());
1412 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1414 // Skip ahead by a lot.
1415 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000));
1417 EXPECT_TRUE(animator->is_animating());
1418 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1420 // Skip ahead by a lot.
1421 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000));
1423 EXPECT_TRUE(animator->is_animating());
1424 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1426 animator->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS);
1428 EXPECT_FALSE(animator->is_animating());
1431 // Test that threaded cyclic sequences continue to animate.
1432 TEST(LayerAnimatorTest, ThreadedCyclicSequences) {
1433 LayerAnimatorTestController test_controller(
1434 LayerAnimator::CreateDefaultAnimator());
1435 AnimationContainerElement* element = test_controller.animator();
1436 test_controller.animator()->set_disable_timer_for_test(true);
1437 TestLayerAnimationDelegate delegate;
1438 test_controller.animator()->SetDelegate(&delegate);
1440 double start_opacity(0.0);
1441 double target_opacity(1.0);
1443 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1445 delegate.SetOpacityFromAnimation(start_opacity);
1447 scoped_ptr<LayerAnimationSequence> sequence(
1448 new LayerAnimationSequence(
1449 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
1451 sequence->AddElement(
1452 LayerAnimationElement::CreateOpacityElement(start_opacity, delta));
1454 sequence->set_is_cyclic(true);
1456 test_controller.animator()->StartAnimation(sequence.release());
1458 base::TimeTicks start_time = test_controller.animator()->last_step_time();
1459 base::TimeTicks effective_start = start_time + delta;
1461 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1462 cc::AnimationEvent::Started,
1464 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1465 animation_group_id(),
1466 cc::Animation::Opacity,
1467 (effective_start - base::TimeTicks()).InSecondsF()));
1469 element->Step(effective_start + delta);
1470 EXPECT_TRUE(test_controller.animator()->is_animating());
1471 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1473 base::TimeTicks second_effective_start = effective_start + 2 * delta;
1474 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1475 cc::AnimationEvent::Started,
1477 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1478 animation_group_id(),
1479 cc::Animation::Opacity,
1480 (second_effective_start - base::TimeTicks()).InSecondsF()));
1482 element->Step(second_effective_start + delta);
1484 EXPECT_TRUE(test_controller.animator()->is_animating());
1485 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1487 base::TimeTicks third_effective_start = second_effective_start + 2 * delta;
1488 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1489 cc::AnimationEvent::Started,
1491 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1492 animation_group_id(),
1493 cc::Animation::Opacity,
1494 (third_effective_start - base::TimeTicks()).InSecondsF()));
1496 element->Step(third_effective_start + delta);
1497 EXPECT_TRUE(test_controller.animator()->is_animating());
1498 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1500 base::TimeTicks fourth_effective_start = third_effective_start + 2 * delta;
1501 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1502 cc::AnimationEvent::Started,
1504 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1505 animation_group_id(),
1506 cc::Animation::Opacity,
1507 (fourth_effective_start - base::TimeTicks()).InSecondsF()));
1509 // Skip ahead by a lot.
1510 element->Step(fourth_effective_start + 1000 * delta);
1512 EXPECT_TRUE(test_controller.animator()->is_animating());
1513 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1515 base::TimeTicks fifth_effective_start = fourth_effective_start + 1001 * delta;
1516 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1517 cc::AnimationEvent::Started,
1519 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1520 animation_group_id(),
1521 cc::Animation::Opacity,
1522 (fifth_effective_start - base::TimeTicks()).InSecondsF()));
1524 // Skip ahead by a lot.
1525 element->Step(fifth_effective_start + 999 * delta);
1527 EXPECT_TRUE(test_controller.animator()->is_animating());
1528 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1530 test_controller.animator()->StopAnimatingProperty(
1531 LayerAnimationElement::OPACITY);
1533 EXPECT_FALSE(test_controller.animator()->is_animating());
1536 TEST(LayerAnimatorTest, AddObserverExplicit) {
1537 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1538 AnimationContainerElement* element = animator.get();
1539 animator->set_disable_timer_for_test(true);
1540 TestLayerAnimationObserver observer;
1541 TestLayerAnimationDelegate delegate;
1542 animator->SetDelegate(&delegate);
1543 animator->AddObserver(&observer);
1544 observer.set_requires_notification_when_animator_destroyed(true);
1546 EXPECT_TRUE(!observer.last_ended_sequence());
1548 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1550 delegate.SetBrightnessFromAnimation(0.0f);
1552 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1553 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1555 animator->StartAnimation(sequence);
1557 EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1559 base::TimeTicks start_time = animator->last_step_time();
1561 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1563 EXPECT_EQ(observer.last_ended_sequence(), sequence);
1565 // |sequence| has been destroyed. Recreate it to test abort.
1566 sequence = new LayerAnimationSequence(
1567 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1569 animator->StartAnimation(sequence);
1571 animator = NULL;
1573 EXPECT_EQ(observer.last_aborted_sequence(), sequence);
1576 // Tests that an observer added to a scoped settings object is still notified
1577 // when the object goes out of scope.
1578 TEST(LayerAnimatorTest, ImplicitAnimationObservers) {
1579 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1580 AnimationContainerElement* element = animator.get();
1581 animator->set_disable_timer_for_test(true);
1582 TestImplicitAnimationObserver observer(false);
1583 TestLayerAnimationDelegate delegate;
1584 animator->SetDelegate(&delegate);
1586 EXPECT_FALSE(observer.animations_completed());
1587 animator->SetBrightness(1.0f);
1590 ScopedLayerAnimationSettings settings(animator.get());
1591 settings.AddObserver(&observer);
1592 animator->SetBrightness(0.0f);
1595 EXPECT_FALSE(observer.animations_completed());
1596 base::TimeTicks start_time = animator->last_step_time();
1597 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1598 EXPECT_TRUE(observer.animations_completed());
1599 EXPECT_FLOAT_EQ(0.0f, delegate.GetBrightnessForAnimation());
1602 // Tests that an observer added to a scoped settings object is still notified
1603 // when the object goes out of scope due to the animation being interrupted.
1604 TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) {
1605 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1606 animator->set_disable_timer_for_test(true);
1607 TestImplicitAnimationObserver observer(false);
1608 TestLayerAnimationDelegate delegate;
1609 animator->SetDelegate(&delegate);
1611 EXPECT_FALSE(observer.animations_completed());
1612 animator->SetBrightness(1.0f);
1615 ScopedLayerAnimationSettings settings(animator.get());
1616 settings.AddObserver(&observer);
1617 animator->SetBrightness(0.0f);
1620 EXPECT_FALSE(observer.animations_completed());
1621 // This should interrupt the implicit animation causing the observer to be
1622 // notified immediately.
1623 animator->SetBrightness(1.0f);
1624 EXPECT_TRUE(observer.animations_completed());
1625 EXPECT_FLOAT_EQ(1.0f, delegate.GetBrightnessForAnimation());
1628 // Tests that an observer added to a scoped settings object is not notified
1629 // when the animator is destroyed unless explicitly requested.
1630 TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) {
1631 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1632 animator->set_disable_timer_for_test(true);
1633 TestImplicitAnimationObserver observer_notify(true);
1634 TestImplicitAnimationObserver observer_do_not_notify(false);
1635 TestLayerAnimationDelegate delegate;
1636 animator->SetDelegate(&delegate);
1638 EXPECT_FALSE(observer_notify.animations_completed());
1639 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1641 animator->SetBrightness(1.0f);
1644 ScopedLayerAnimationSettings settings(animator.get());
1645 settings.AddObserver(&observer_notify);
1646 settings.AddObserver(&observer_do_not_notify);
1647 animator->SetBrightness(0.0f);
1650 EXPECT_FALSE(observer_notify.animations_completed());
1651 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1652 animator = NULL;
1653 EXPECT_TRUE(observer_notify.animations_completed());
1654 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1658 TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) {
1659 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1660 AnimationContainerElement* element = animator.get();
1661 animator->set_disable_timer_for_test(true);
1662 TestLayerAnimationObserver observer;
1663 TestLayerAnimationObserver removed_observer;
1664 TestLayerAnimationDelegate delegate;
1665 animator->SetDelegate(&delegate);
1667 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1669 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1670 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1672 sequence->AddObserver(&observer);
1673 sequence->AddObserver(&removed_observer);
1675 animator->StartAnimation(sequence);
1677 EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1678 EXPECT_TRUE(!observer.last_ended_sequence());
1679 EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence);
1680 EXPECT_TRUE(!removed_observer.last_ended_sequence());
1682 // This should stop the observer from observing sequence.
1683 animator->RemoveObserver(&removed_observer);
1685 base::TimeTicks start_time = animator->last_step_time();
1687 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1689 EXPECT_EQ(observer.last_ended_sequence(), sequence);
1690 EXPECT_TRUE(!removed_observer.last_ended_sequence());
1693 TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) {
1694 TestLayerAnimationDelegate delegate;
1695 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1696 animator->set_disable_timer_for_test(true);
1698 scoped_ptr<TestLayerAnimationObserver> observer(
1699 new TestLayerAnimationObserver);
1700 animator->SetDelegate(&delegate);
1701 animator->AddObserver(observer.get());
1703 delegate.SetOpacityFromAnimation(0.0f);
1705 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1706 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1707 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
1709 animator->StartAnimation(sequence);
1711 // |observer| should be attached to |sequence|.
1712 EXPECT_EQ(static_cast<size_t>(1), sequence->observers_.size());
1714 // Now, release |observer|
1715 observer.reset();
1717 // And |sequence| should no longer be attached to |observer|.
1718 EXPECT_EQ(static_cast<size_t>(0), sequence->observers_.size());
1721 TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) {
1722 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1723 AnimationContainerElement* element = animator.get();
1724 animator->set_disable_timer_for_test(true);
1726 TestImplicitAnimationObserver observer(false);
1727 TestLayerAnimationDelegate delegate;
1728 animator->SetDelegate(&delegate);
1730 delegate.SetBrightnessFromAnimation(0.0f);
1733 ScopedLayerAnimationSettings setter(animator.get());
1735 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1736 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1737 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1739 animator->StartAnimation(sequence);
1740 base::TimeTicks start_time = animator->last_step_time();
1741 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1743 setter.AddObserver(&observer);
1745 // Start observing an in-flight animation.
1746 sequence->AddObserver(&observer);
1748 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1751 EXPECT_TRUE(observer.animations_completed());
1754 TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) {
1755 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1756 AnimationContainerElement* element = animator.get();
1757 animator->set_disable_timer_for_test(true);
1759 TestImplicitAnimationObserver observer(false);
1760 TestLayerAnimationDelegate delegate;
1761 animator->SetDelegate(&delegate);
1763 delegate.SetBrightnessFromAnimation(0.0f);
1764 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1765 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1766 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1769 ScopedLayerAnimationSettings setter(animator.get());
1770 setter.AddObserver(&observer);
1772 animator->StartAnimation(sequence);
1773 base::TimeTicks start_time = animator->last_step_time();
1774 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1777 EXPECT_FALSE(observer.animations_completed());
1779 // Stop observing an in-flight animation.
1780 sequence->RemoveObserver(&observer);
1782 EXPECT_TRUE(observer.animations_completed());
1785 // This checks that if an animation is deleted due to a callback, that the
1786 // animator does not try to use the deleted animation. For example, if we have
1787 // two running animations, and the first finishes and the resulting callback
1788 // causes the second to be deleted, we should not attempt to animate the second
1789 // animation.
1790 TEST(LayerAnimatorTest, ObserverDeletesAnimations) {
1791 ScopedAnimationDurationScaleMode normal_duration_mode(
1792 ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1793 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1794 AnimationContainerElement* element = animator.get();
1795 animator->set_disable_timer_for_test(true);
1796 TestLayerAnimationDelegate delegate;
1797 animator->SetDelegate(&delegate);
1799 double start_brightness(0.0);
1800 double target_brightness(1.0);
1802 gfx::Rect start_bounds(0, 0, 50, 50);
1803 gfx::Rect target_bounds(5, 5, 5, 5);
1805 delegate.SetBrightnessFromAnimation(start_brightness);
1806 delegate.SetBoundsFromAnimation(start_bounds);
1808 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
1809 base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2);
1810 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3);
1812 LayerAnimationSequence* to_delete = new LayerAnimationSequence(
1813 LayerAnimationElement::CreateBoundsElement(target_bounds, bounds_delta));
1815 scoped_ptr<DeletingLayerAnimationObserver> observer(
1816 new DeletingLayerAnimationObserver(animator.get(), to_delete));
1818 animator->AddObserver(observer.get());
1820 animator->StartAnimation(
1821 new LayerAnimationSequence(
1822 LayerAnimationElement::CreateBrightnessElement(
1823 target_brightness, brightness_delta)));
1825 animator->StartAnimation(to_delete);
1827 base::TimeTicks start_time = animator->last_step_time();
1828 element->Step(start_time + halfway_delta);
1830 animator->RemoveObserver(observer.get());
1833 // Check that setting a property during an animation with a default animator
1834 // cancels the original animation.
1835 TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) {
1836 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1837 animator->set_disable_timer_for_test(true);
1838 TestLayerAnimationDelegate delegate;
1839 animator->SetDelegate(&delegate);
1841 double start_opacity(0.0);
1842 double target_opacity(1.0);
1844 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1846 delegate.SetOpacityFromAnimation(start_opacity);
1848 scoped_ptr<LayerAnimationSequence> sequence(
1849 new LayerAnimationSequence(
1850 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
1852 animator->StartAnimation(sequence.release());
1854 animator->SetOpacity(0.5);
1856 EXPECT_FALSE(animator->is_animating());
1857 EXPECT_EQ(0.5, animator->GetTargetOpacity());
1860 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
1861 // second sequence to be leaked.
1862 TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) {
1863 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1864 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1865 animator->set_disable_timer_for_test(true);
1866 TestLayerAnimationDelegate delegate;
1867 animator->SetDelegate(&delegate);
1869 gfx::Rect start_bounds(0, 0, 50, 50);
1870 gfx::Rect middle_bounds(10, 10, 100, 100);
1871 gfx::Rect target_bounds(5, 5, 5, 5);
1873 delegate.SetBoundsFromAnimation(start_bounds);
1876 // start an implicit bounds animation.
1877 ScopedLayerAnimationSettings settings(animator.get());
1878 animator->SetBounds(middle_bounds);
1881 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1883 int num_live_instances = 0;
1884 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1885 scoped_ptr<TestLayerAnimationSequence> sequence(
1886 new TestLayerAnimationSequence(
1887 LayerAnimationElement::CreateBoundsElement(target_bounds, delta),
1888 &num_live_instances));
1890 EXPECT_EQ(1, num_live_instances);
1892 // This should interrupt the running sequence causing us to immediately set
1893 // the target value. The sequence should alse be destructed.
1894 animator->StartAnimation(sequence.release());
1896 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1897 EXPECT_EQ(0, num_live_instances);
1898 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
1901 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
1902 TEST(LayerAnimatorTest, GetTargetOpacity) {
1903 TestLayerAnimationDelegate delegate;
1904 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1905 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1906 animator->set_disable_timer_for_test(true);
1907 animator->SetDelegate(&delegate);
1909 delegate.SetOpacityFromAnimation(0.0);
1912 ScopedLayerAnimationSettings settings(animator.get());
1913 animator->SetOpacity(0.5);
1914 EXPECT_EQ(0.5, animator->GetTargetOpacity());
1916 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
1917 animator->SetOpacity(1.0);
1918 EXPECT_EQ(1.0, animator->GetTargetOpacity());
1922 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
1923 TEST(LayerAnimatorTest, GetTargetBrightness) {
1924 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1925 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1926 animator->set_disable_timer_for_test(true);
1927 TestLayerAnimationDelegate delegate;
1928 animator->SetDelegate(&delegate);
1930 delegate.SetBrightnessFromAnimation(0.0);
1933 ScopedLayerAnimationSettings settings(animator.get());
1934 animator->SetBrightness(0.5);
1935 EXPECT_EQ(0.5, animator->GetTargetBrightness());
1937 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
1938 animator->SetBrightness(1.0);
1939 EXPECT_EQ(1.0, animator->GetTargetBrightness());
1943 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
1944 TEST(LayerAnimatorTest, GetTargetGrayscale) {
1945 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1946 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1947 animator->set_disable_timer_for_test(true);
1948 TestLayerAnimationDelegate delegate;
1949 animator->SetDelegate(&delegate);
1951 delegate.SetGrayscaleFromAnimation(0.0);
1954 ScopedLayerAnimationSettings settings(animator.get());
1955 animator->SetGrayscale(0.5);
1956 EXPECT_EQ(0.5, animator->GetTargetGrayscale());
1958 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
1959 animator->SetGrayscale(1.0);
1960 EXPECT_EQ(1.0, animator->GetTargetGrayscale());
1964 // Verifies color property is modified appropriately.
1965 TEST(LayerAnimatorTest, Color) {
1966 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1967 AnimationContainerElement* element = animator.get();
1968 animator->set_disable_timer_for_test(true);
1969 TestLayerAnimationDelegate delegate;
1970 animator->SetDelegate(&delegate);
1972 SkColor start_color = SkColorSetARGB( 0, 20, 40, 60);
1973 SkColor middle_color = SkColorSetARGB(127, 30, 60, 100);
1974 SkColor target_color = SkColorSetARGB(254, 40, 80, 140);
1976 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1978 delegate.SetColorFromAnimation(start_color);
1980 animator->ScheduleAnimation(
1981 new LayerAnimationSequence(
1982 LayerAnimationElement::CreateColorElement(target_color, delta)));
1984 EXPECT_TRUE(animator->is_animating());
1985 EXPECT_EQ(ColorToString(start_color),
1986 ColorToString(delegate.GetColorForAnimation()));
1988 base::TimeTicks start_time = animator->last_step_time();
1990 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1992 EXPECT_TRUE(animator->is_animating());
1993 EXPECT_EQ(ColorToString(middle_color),
1994 ColorToString(delegate.GetColorForAnimation()));
1996 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1998 EXPECT_FALSE(animator->is_animating());
1999 EXPECT_EQ(ColorToString(target_color),
2000 ColorToString(delegate.GetColorForAnimation()));
2003 // Verifies SchedulePauseForProperties().
2004 TEST(LayerAnimatorTest, SchedulePauseForProperties) {
2005 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2006 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2007 animator->SchedulePauseForProperties(base::TimeDelta::FromMilliseconds(100),
2008 LayerAnimationElement::TRANSFORM,
2009 LayerAnimationElement::BOUNDS, -1);
2010 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM));
2011 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2012 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY));
2016 class AnimatorOwner {
2017 public:
2018 AnimatorOwner()
2019 : animator_(LayerAnimator::CreateDefaultAnimator()) {
2022 LayerAnimator* animator() { return animator_.get(); }
2024 private:
2025 scoped_refptr<LayerAnimator> animator_;
2027 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner);
2030 class DeletingObserver : public LayerAnimationObserver {
2031 public:
2032 DeletingObserver(bool* was_deleted)
2033 : animator_owner_(new AnimatorOwner),
2034 delete_on_animation_ended_(false),
2035 delete_on_animation_aborted_(false),
2036 delete_on_animation_scheduled_(false),
2037 was_deleted_(was_deleted) {
2038 animator()->AddObserver(this);
2041 virtual ~DeletingObserver() {
2042 animator()->RemoveObserver(this);
2043 *was_deleted_ = true;
2046 LayerAnimator* animator() { return animator_owner_->animator(); }
2048 bool delete_on_animation_ended() const {
2049 return delete_on_animation_ended_;
2051 void set_delete_on_animation_ended(bool enabled) {
2052 delete_on_animation_ended_ = enabled;
2055 bool delete_on_animation_aborted() const {
2056 return delete_on_animation_aborted_;
2058 void set_delete_on_animation_aborted(bool enabled) {
2059 delete_on_animation_aborted_ = enabled;
2062 bool delete_on_animation_scheduled() const {
2063 return delete_on_animation_scheduled_;
2065 void set_delete_on_animation_scheduled(bool enabled) {
2066 delete_on_animation_scheduled_ = enabled;
2069 // LayerAnimationObserver implementation.
2070 virtual void OnLayerAnimationEnded(
2071 LayerAnimationSequence* sequence) OVERRIDE {
2072 if (delete_on_animation_ended_)
2073 delete this;
2076 virtual void OnLayerAnimationAborted(
2077 LayerAnimationSequence* sequence) OVERRIDE {
2078 if (delete_on_animation_aborted_)
2079 delete this;
2082 virtual void OnLayerAnimationScheduled(
2083 LayerAnimationSequence* sequence) OVERRIDE {
2084 if (delete_on_animation_scheduled_)
2085 delete this;
2088 private:
2089 scoped_ptr<AnimatorOwner> animator_owner_;
2090 bool delete_on_animation_ended_;
2091 bool delete_on_animation_aborted_;
2092 bool delete_on_animation_scheduled_;
2093 bool* was_deleted_;
2095 DISALLOW_COPY_AND_ASSIGN(DeletingObserver);
2098 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) {
2099 bool observer_was_deleted = false;
2100 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2101 observer->set_delete_on_animation_ended(true);
2102 observer->set_delete_on_animation_aborted(true);
2103 LayerAnimator* animator = observer->animator();
2104 AnimationContainerElement* element = observer->animator();
2105 animator->set_disable_timer_for_test(true);
2106 TestLayerAnimationDelegate delegate;
2107 animator->SetDelegate(&delegate);
2109 delegate.SetBrightnessFromAnimation(0.0f);
2111 gfx::Rect start_bounds(0, 0, 50, 50);
2112 gfx::Rect target_bounds(10, 10, 100, 100);
2114 delegate.SetBoundsFromAnimation(start_bounds);
2116 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2117 LayerAnimationSequence* brightness_sequence = new LayerAnimationSequence(
2118 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
2119 animator->StartAnimation(brightness_sequence);
2121 delta = base::TimeDelta::FromSeconds(2);
2122 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2123 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2124 animator->StartAnimation(bounds_sequence);
2126 base::TimeTicks start_time = animator->last_step_time();
2127 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
2129 EXPECT_TRUE(observer_was_deleted);
2132 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterStoppingAnimating) {
2133 bool observer_was_deleted = false;
2134 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2135 observer->set_delete_on_animation_ended(true);
2136 observer->set_delete_on_animation_aborted(true);
2137 LayerAnimator* animator = observer->animator();
2138 animator->set_disable_timer_for_test(true);
2139 TestLayerAnimationDelegate delegate;
2140 animator->SetDelegate(&delegate);
2142 delegate.SetOpacityFromAnimation(0.0f);
2144 gfx::Rect start_bounds(0, 0, 50, 50);
2145 gfx::Rect target_bounds(10, 10, 100, 100);
2147 delegate.SetBoundsFromAnimation(start_bounds);
2149 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2150 LayerAnimationSequence* opacity_sequence = new LayerAnimationSequence(
2151 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
2152 animator->StartAnimation(opacity_sequence);
2154 delta = base::TimeDelta::FromSeconds(2);
2155 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2156 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2157 animator->StartAnimation(bounds_sequence);
2159 animator->StopAnimating();
2161 EXPECT_TRUE(observer_was_deleted);
2164 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterScheduling) {
2165 bool observer_was_deleted = false;
2166 TestLayerAnimationDelegate delegate;
2167 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2168 observer->set_delete_on_animation_scheduled(true);
2169 LayerAnimator* animator = observer->animator();
2170 animator->set_disable_timer_for_test(true);
2171 animator->SetDelegate(&delegate);
2173 delegate.SetOpacityFromAnimation(0.0f);
2175 gfx::Rect start_bounds(0, 0, 50, 50);
2176 gfx::Rect target_bounds(10, 10, 100, 100);
2178 delegate.SetBoundsFromAnimation(start_bounds);
2180 std::vector<LayerAnimationSequence*> to_start;
2182 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2183 to_start.push_back(new LayerAnimationSequence(
2184 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2186 delta = base::TimeDelta::FromSeconds(2);
2187 to_start.push_back(new LayerAnimationSequence(
2188 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2190 animator->ScheduleTogether(to_start);
2192 EXPECT_TRUE(observer_was_deleted);
2195 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterAborted) {
2196 bool observer_was_deleted = false;
2197 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2198 TestLayerAnimationDelegate delegate;
2199 observer->set_delete_on_animation_aborted(true);
2200 LayerAnimator* animator = observer->animator();
2201 animator->set_preemption_strategy(
2202 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2203 animator->set_disable_timer_for_test(true);
2204 animator->SetDelegate(&delegate);
2206 delegate.SetOpacityFromAnimation(0.0f);
2208 gfx::Rect start_bounds(0, 0, 50, 50);
2209 gfx::Rect target_bounds(10, 10, 100, 100);
2211 delegate.SetBoundsFromAnimation(start_bounds);
2213 std::vector<LayerAnimationSequence*> to_start;
2215 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2216 to_start.push_back(new LayerAnimationSequence(
2217 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2219 delta = base::TimeDelta::FromSeconds(2);
2220 to_start.push_back(new LayerAnimationSequence(
2221 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2223 animator->ScheduleTogether(to_start);
2225 EXPECT_FALSE(observer_was_deleted);
2227 animator->StartAnimation(new LayerAnimationSequence(
2228 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2230 EXPECT_TRUE(observer_was_deleted);
2234 TEST(LayerAnimatorTest, TestSetterRespectEnqueueStrategy) {
2235 TestLayerAnimationDelegate delegate;
2236 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2237 animator->set_disable_timer_for_test(true);
2239 animator->SetDelegate(&delegate);
2241 float start_opacity = 0.0f;
2242 float target_opacity = 1.0f;
2243 float magic_opacity = 0.123f;
2245 delegate.SetOpacityFromAnimation(start_opacity);
2247 ScopedLayerAnimationSettings settings(animator);
2248 settings.SetPreemptionStrategy(
2249 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2250 settings.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2251 animator->SetOpacity(target_opacity);
2253 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2255 settings.SetPreemptionStrategy(
2256 LayerAnimator::ENQUEUE_NEW_ANIMATION);
2257 settings.SetTransitionDuration(base::TimeDelta());
2258 animator->SetOpacity(magic_opacity);
2260 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2263 } // namespace ui