Don't send touch move events within the slop region in Aura.
[chromium-blink-merge.git] / ui / compositor / layer_animator_unittest.cc
blobed739656e56be3bee3d82a7a38f072e330678f91
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/strings/stringprintf.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/compositor/layer.h"
14 #include "ui/compositor/layer_animation_delegate.h"
15 #include "ui/compositor/layer_animation_element.h"
16 #include "ui/compositor/layer_animation_sequence.h"
17 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/compositor/test/layer_animator_test_controller.h"
20 #include "ui/compositor/test/test_layer_animation_delegate.h"
21 #include "ui/compositor/test/test_layer_animation_observer.h"
22 #include "ui/compositor/test/test_utils.h"
23 #include "ui/gfx/frame_time.h"
24 #include "ui/gfx/rect.h"
25 #include "ui/gfx/transform.h"
27 using gfx::AnimationContainerElement;
29 namespace ui {
31 namespace {
33 // Converts |color| to a string. Each component of the color is separated by a
34 // space and the order if A R G B.
35 std::string ColorToString(SkColor color) {
36 return base::StringPrintf("%d %d %d %d", SkColorGetA(color),
37 SkColorGetR(color), SkColorGetG(color),
38 SkColorGetB(color));
41 // Creates vector with two LayerAnimationSequences, based on |first| and
42 // |second| layer animation elements.
43 std::vector<LayerAnimationSequence*> CreateMultiSequence(
44 LayerAnimationElement* first,
45 LayerAnimationElement* second) {
46 LayerAnimationSequence* first_sequence = new LayerAnimationSequence();
47 first_sequence->AddElement(first);
48 LayerAnimationSequence* second_sequence = new LayerAnimationSequence();
49 second_sequence->AddElement(second);
51 std::vector<ui::LayerAnimationSequence*> animations;
52 animations.push_back(first_sequence);
53 animations.push_back(second_sequence);
54 return animations;
57 class TestImplicitAnimationObserver : public ImplicitAnimationObserver {
58 public:
59 explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed)
60 : animations_completed_(false),
61 notify_when_animator_destructed_(notify_when_animator_destructed) {
64 bool animations_completed() const { return animations_completed_; }
65 void set_animations_completed(bool completed) {
66 animations_completed_ = completed;
69 bool WasAnimationAbortedForProperty(
70 LayerAnimationElement::AnimatableProperty property) const {
71 return ImplicitAnimationObserver::WasAnimationAbortedForProperty(property);
74 bool WasAnimationCompletedForProperty(
75 LayerAnimationElement::AnimatableProperty property) const {
76 return ImplicitAnimationObserver::WasAnimationCompletedForProperty(
77 property);
80 private:
81 // ImplicitAnimationObserver implementation
82 virtual void OnImplicitAnimationsCompleted() OVERRIDE {
83 animations_completed_ = true;
86 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE {
87 return notify_when_animator_destructed_;
90 bool animations_completed_;
91 bool notify_when_animator_destructed_;
93 DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver);
96 // When notified that an animation has ended, stops all other animations.
97 class DeletingLayerAnimationObserver : public LayerAnimationObserver {
98 public:
99 DeletingLayerAnimationObserver(LayerAnimator* animator)
100 : animator_(animator) {
103 virtual void OnLayerAnimationEnded(
104 LayerAnimationSequence* sequence) OVERRIDE {
105 animator_->StopAnimating();
108 virtual void OnLayerAnimationAborted(
109 LayerAnimationSequence* sequence) OVERRIDE {
110 animator_->StopAnimating();
113 virtual void OnLayerAnimationScheduled(
114 LayerAnimationSequence* sequence) OVERRIDE {
117 private:
118 LayerAnimator* animator_;
120 DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver);
123 class TestLayerAnimator : public LayerAnimator {
124 public:
125 TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)) {}
127 protected:
128 virtual ~TestLayerAnimator() {}
130 virtual void ProgressAnimation(LayerAnimationSequence* sequence,
131 base::TimeTicks now) OVERRIDE {
132 EXPECT_TRUE(HasAnimation(sequence));
133 LayerAnimator::ProgressAnimation(sequence, now);
136 private:
137 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
140 // The test layer animation sequence updates a live instances count when it is
141 // created and destroyed.
142 class TestLayerAnimationSequence : public LayerAnimationSequence {
143 public:
144 TestLayerAnimationSequence(LayerAnimationElement* element,
145 int* num_live_instances)
146 : LayerAnimationSequence(element),
147 num_live_instances_(num_live_instances) {
148 (*num_live_instances_)++;
151 virtual ~TestLayerAnimationSequence() {
152 (*num_live_instances_)--;
155 private:
156 int* num_live_instances_;
158 DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence);
161 } // namespace
163 // Checks that setting a property on an implicit animator causes an animation to
164 // happen.
165 TEST(LayerAnimatorTest, ImplicitAnimation) {
166 scoped_refptr<LayerAnimator> animator(
167 LayerAnimator::CreateImplicitAnimator());
168 AnimationContainerElement* element = animator.get();
169 animator->set_disable_timer_for_test(true);
170 TestLayerAnimationDelegate delegate;
171 animator->SetDelegate(&delegate);
172 base::TimeTicks now = gfx::FrameTime::Now();
173 animator->SetBrightness(0.5);
174 EXPECT_TRUE(animator->is_animating());
175 element->Step(now + base::TimeDelta::FromSeconds(1));
176 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
179 // Checks that if the animator is a default animator, that implicit animations
180 // are not started.
181 TEST(LayerAnimatorTest, NoImplicitAnimation) {
182 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
183 animator->set_disable_timer_for_test(true);
184 TestLayerAnimationDelegate delegate;
185 animator->SetDelegate(&delegate);
186 animator->SetBrightness(0.5);
187 EXPECT_FALSE(animator->is_animating());
188 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
191 // Checks that StopAnimatingProperty stops animation for that property, and also
192 // skips the stopped animation to the end.
193 TEST(LayerAnimatorTest, StopAnimatingProperty) {
194 scoped_refptr<LayerAnimator> animator(
195 LayerAnimator::CreateImplicitAnimator());
196 animator->set_disable_timer_for_test(true);
197 TestLayerAnimationDelegate delegate;
198 animator->SetDelegate(&delegate);
199 double target_opacity(0.5);
200 gfx::Rect target_bounds(0, 0, 50, 50);
201 animator->SetOpacity(target_opacity);
202 animator->SetBounds(target_bounds);
203 animator->StopAnimatingProperty(LayerAnimationElement::OPACITY);
204 EXPECT_TRUE(animator->is_animating());
205 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
206 animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS);
207 EXPECT_FALSE(animator->is_animating());
208 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
211 // Checks that multiple running animation for separate properties can be stopped
212 // simultaneously and that all animations are advanced to their target values.
213 TEST(LayerAnimatorTest, StopAnimating) {
214 scoped_refptr<LayerAnimator> animator(
215 LayerAnimator::CreateImplicitAnimator());
216 animator->set_disable_timer_for_test(true);
217 TestLayerAnimationDelegate delegate;
218 animator->SetDelegate(&delegate);
219 double target_opacity(0.5);
220 gfx::Rect target_bounds(0, 0, 50, 50);
221 animator->SetOpacity(target_opacity);
222 animator->SetBounds(target_bounds);
223 EXPECT_TRUE(animator->is_animating());
224 animator->StopAnimating();
225 EXPECT_FALSE(animator->is_animating());
226 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
227 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
230 // Checks that multiple running animation for separate properties can be stopped
231 // simultaneously and that all animations are advanced to their target values.
232 TEST(LayerAnimatorTest, AbortAllAnimations) {
233 scoped_refptr<LayerAnimator> animator(
234 LayerAnimator::CreateImplicitAnimator());
235 animator->set_disable_timer_for_test(true);
236 TestLayerAnimationDelegate delegate;
237 double initial_opacity(1.0);
238 gfx::Rect initial_bounds(0, 0, 10, 10);
239 delegate.SetOpacityFromAnimation(initial_opacity);
240 delegate.SetBoundsFromAnimation(initial_bounds);
241 animator->SetDelegate(&delegate);
242 double target_opacity(0.5);
243 gfx::Rect target_bounds(0, 0, 50, 50);
244 animator->SetOpacity(target_opacity);
245 animator->SetBounds(target_bounds);
246 EXPECT_TRUE(animator->is_animating());
247 animator->AbortAllAnimations();
248 EXPECT_FALSE(animator->is_animating());
249 EXPECT_FLOAT_EQ(initial_opacity, delegate.GetOpacityForAnimation());
250 CheckApproximatelyEqual(initial_bounds, delegate.GetBoundsForAnimation());
253 // Schedule a non-threaded animation that can run immediately. This is the
254 // trivial case and should result in the animation being started immediately.
255 TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) {
256 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
257 AnimationContainerElement* element = animator.get();
258 animator->set_disable_timer_for_test(true);
259 TestLayerAnimationDelegate delegate;
260 animator->SetDelegate(&delegate);
262 double start_brightness(0.0);
263 double middle_brightness(0.5);
264 double target_brightness(1.0);
266 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
268 delegate.SetBrightnessFromAnimation(start_brightness);
270 animator->ScheduleAnimation(
271 new LayerAnimationSequence(
272 LayerAnimationElement::CreateBrightnessElement(target_brightness,
273 delta)));
275 EXPECT_TRUE(animator->is_animating());
276 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
278 base::TimeTicks start_time = animator->last_step_time();
280 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
282 EXPECT_TRUE(animator->is_animating());
283 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
285 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
287 EXPECT_FALSE(animator->is_animating());
288 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
291 // Schedule a threaded animation that can run immediately.
292 TEST(LayerAnimatorTest, ScheduleThreadedAnimationThatCanRunImmediately) {
293 double epsilon = 0.00001;
294 LayerAnimatorTestController test_controller(
295 LayerAnimator::CreateDefaultAnimator());
296 AnimationContainerElement* element = test_controller.animator();
297 test_controller.animator()->set_disable_timer_for_test(true);
298 TestLayerAnimationDelegate delegate;
299 test_controller.animator()->SetDelegate(&delegate);
301 double start_opacity(0.0);
302 double target_opacity(1.0);
304 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
306 delegate.SetOpacityFromAnimation(start_opacity);
308 test_controller.animator()->ScheduleAnimation(
309 new LayerAnimationSequence(
310 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
312 EXPECT_TRUE(test_controller.animator()->is_animating());
313 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
315 base::TimeTicks start_time = test_controller.animator()->last_step_time();
316 base::TimeTicks effective_start = start_time + delta;
318 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
319 cc::AnimationEvent::Started,
321 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
322 animation_group_id(),
323 cc::Animation::Opacity,
324 (effective_start - base::TimeTicks()).InSecondsF()));
326 element->Step(effective_start + delta/2);
328 EXPECT_TRUE(test_controller.animator()->is_animating());
329 EXPECT_NEAR(
330 0.5,
331 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
332 last_progressed_fraction(),
333 epsilon);
335 element->Step(effective_start + delta);
337 EXPECT_FALSE(test_controller.animator()->is_animating());
338 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
341 // Schedule two non-threaded animations on separate properties. Both animations
342 // should start immediately and should progress in lock step.
343 TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) {
344 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
345 AnimationContainerElement* element = animator.get();
346 animator->set_disable_timer_for_test(true);
347 TestLayerAnimationDelegate delegate;
348 animator->SetDelegate(&delegate);
350 double start_brightness(0.0);
351 double middle_brightness(0.5);
352 double target_brightness(1.0);
354 gfx::Rect start_bounds, target_bounds, middle_bounds;
355 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
356 start_bounds.set_x(-90);
357 target_bounds.set_x(90);
359 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
361 delegate.SetBrightnessFromAnimation(start_brightness);
362 delegate.SetBoundsFromAnimation(start_bounds);
364 animator->ScheduleAnimation(
365 new LayerAnimationSequence(
366 LayerAnimationElement::CreateBrightnessElement(target_brightness,
367 delta)));
369 animator->ScheduleAnimation(
370 new LayerAnimationSequence(
371 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
373 EXPECT_TRUE(animator->is_animating());
374 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
375 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
377 base::TimeTicks start_time = animator->last_step_time();
379 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
381 EXPECT_TRUE(animator->is_animating());
382 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
383 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
385 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
387 EXPECT_FALSE(animator->is_animating());
388 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
389 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
392 // Schedule a threaded and a non-threaded animation on separate properties. Both
393 // animations should progress in lock step.
394 TEST(LayerAnimatorTest, ScheduleThreadedAndNonThreadedAnimations) {
395 double epsilon = 0.00001;
396 LayerAnimatorTestController test_controller(
397 LayerAnimator::CreateDefaultAnimator());
398 AnimationContainerElement* element = test_controller.animator();
399 test_controller.animator()->set_disable_timer_for_test(true);
400 TestLayerAnimationDelegate delegate;
401 test_controller.animator()->SetDelegate(&delegate);
403 double start_opacity(0.0);
404 double target_opacity(1.0);
406 gfx::Rect start_bounds, target_bounds, middle_bounds;
407 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
408 start_bounds.set_x(-90);
409 target_bounds.set_x(90);
411 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
413 delegate.SetOpacityFromAnimation(start_opacity);
414 delegate.SetBoundsFromAnimation(start_bounds);
416 std::vector<LayerAnimationSequence*> animations;
417 animations.push_back(
418 new LayerAnimationSequence(
419 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
421 animations.push_back(
422 new LayerAnimationSequence(
423 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
425 test_controller.animator()->ScheduleTogether(animations);
427 EXPECT_TRUE(test_controller.animator()->is_animating());
428 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
429 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
431 base::TimeTicks start_time = test_controller.animator()->last_step_time();
432 base::TimeTicks effective_start = start_time + delta;
434 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
435 cc::AnimationEvent::Started,
437 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
438 animation_group_id(),
439 cc::Animation::Opacity,
440 (effective_start - base::TimeTicks()).InSecondsF()));
442 element->Step(effective_start + delta/2);
444 EXPECT_TRUE(test_controller.animator()->is_animating());
445 EXPECT_NEAR(
446 0.5,
447 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
448 last_progressed_fraction(),
449 epsilon);
450 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
452 element->Step(effective_start + delta);
454 EXPECT_FALSE(test_controller.animator()->is_animating());
455 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
456 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
459 // Schedule two animations on the same property. In this case, the two
460 // animations should run one after another.
461 TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) {
462 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
463 AnimationContainerElement* element = animator.get();
464 animator->set_disable_timer_for_test(true);
465 TestLayerAnimationDelegate delegate;
466 animator->SetDelegate(&delegate);
468 double start_brightness(0.0);
469 double middle_brightness(0.5);
470 double target_brightness(1.0);
472 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
474 delegate.SetBrightnessFromAnimation(start_brightness);
476 animator->ScheduleAnimation(
477 new LayerAnimationSequence(
478 LayerAnimationElement::CreateBrightnessElement(target_brightness,
479 delta)));
481 animator->ScheduleAnimation(
482 new LayerAnimationSequence(
483 LayerAnimationElement::CreateBrightnessElement(start_brightness,
484 delta)));
486 EXPECT_TRUE(animator->is_animating());
487 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
489 base::TimeTicks start_time = animator->last_step_time();
491 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
493 EXPECT_TRUE(animator->is_animating());
494 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
496 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
498 EXPECT_TRUE(animator->is_animating());
499 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
501 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
503 EXPECT_TRUE(animator->is_animating());
504 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
506 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
508 EXPECT_FALSE(animator->is_animating());
509 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
512 // Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
513 // is, ensure that all animations targetting a particular property are run in
514 // order.
515 TEST(LayerAnimatorTest, ScheduleBlockedAnimation) {
516 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
517 AnimationContainerElement* element = animator.get();
518 animator->set_disable_timer_for_test(true);
519 TestLayerAnimationDelegate delegate;
520 animator->SetDelegate(&delegate);
522 double start_grayscale(0.0);
523 double middle_grayscale(0.5);
524 double target_grayscale(1.0);
526 gfx::Rect start_bounds, target_bounds, middle_bounds;
527 start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
528 start_bounds.set_x(-90);
529 target_bounds.set_x(90);
531 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
533 delegate.SetGrayscaleFromAnimation(start_grayscale);
534 delegate.SetBoundsFromAnimation(start_bounds);
536 animator->ScheduleAnimation(
537 new LayerAnimationSequence(
538 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
539 delta)));
541 scoped_ptr<LayerAnimationSequence> bounds_and_grayscale(
542 new LayerAnimationSequence(
543 LayerAnimationElement::CreateGrayscaleElement(start_grayscale,
544 delta)));
546 bounds_and_grayscale->AddElement(
547 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
549 animator->ScheduleAnimation(bounds_and_grayscale.release());
551 animator->ScheduleAnimation(
552 new LayerAnimationSequence(
553 LayerAnimationElement::CreateBoundsElement(start_bounds, delta)));
555 EXPECT_TRUE(animator->is_animating());
556 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
557 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
559 base::TimeTicks start_time = animator->last_step_time();
561 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
563 EXPECT_TRUE(animator->is_animating());
564 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
565 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
567 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
569 EXPECT_TRUE(animator->is_animating());
570 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
571 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
573 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
575 EXPECT_TRUE(animator->is_animating());
576 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
577 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
579 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
581 EXPECT_TRUE(animator->is_animating());
582 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
583 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
585 element->Step(start_time + base::TimeDelta::FromMilliseconds(4000));
587 EXPECT_FALSE(animator->is_animating());
588 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
589 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
592 // Schedule {g} and then schedule {g} and {b} together. In this case, since
593 // ScheduleTogether is being used, the bounds animation should not start until
594 // the second grayscale animation starts.
595 TEST(LayerAnimatorTest, ScheduleTogether) {
596 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
597 AnimationContainerElement* element = animator.get();
598 animator->set_disable_timer_for_test(true);
599 TestLayerAnimationDelegate delegate;
600 animator->SetDelegate(&delegate);
602 double start_grayscale(0.0);
603 double target_grayscale(1.0);
605 gfx::Rect start_bounds, target_bounds, middle_bounds;
606 start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50);
607 start_bounds.set_x(-90);
608 target_bounds.set_x(90);
610 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
612 delegate.SetGrayscaleFromAnimation(start_grayscale);
613 delegate.SetBoundsFromAnimation(start_bounds);
615 animator->ScheduleAnimation(
616 new LayerAnimationSequence(
617 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
618 delta)));
620 std::vector<LayerAnimationSequence*> sequences;
621 sequences.push_back(new LayerAnimationSequence(
622 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta)));
623 sequences.push_back(new LayerAnimationSequence(
624 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
626 animator->ScheduleTogether(sequences);
628 EXPECT_TRUE(animator->is_animating());
629 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
630 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
632 base::TimeTicks start_time = animator->last_step_time();
634 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
636 EXPECT_TRUE(animator->is_animating());
637 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
638 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
640 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
642 EXPECT_FALSE(animator->is_animating());
643 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
644 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
647 // Start non-threaded animation (that can run immediately). This is the trivial
648 // case (see the trival case for ScheduleAnimation).
649 TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) {
650 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
651 AnimationContainerElement* element = animator.get();
652 animator->set_disable_timer_for_test(true);
653 TestLayerAnimationDelegate delegate;
654 animator->SetDelegate(&delegate);
656 double start_brightness(0.0);
657 double middle_brightness(0.5);
658 double target_brightness(1.0);
660 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
662 delegate.SetBrightnessFromAnimation(start_brightness);
664 animator->StartAnimation(
665 new LayerAnimationSequence(
666 LayerAnimationElement::CreateBrightnessElement(target_brightness,
667 delta)));
669 EXPECT_TRUE(animator->is_animating());
670 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
672 base::TimeTicks start_time = animator->last_step_time();
674 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
676 EXPECT_TRUE(animator->is_animating());
677 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
679 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
681 EXPECT_FALSE(animator->is_animating());
682 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
685 // Start threaded animation (that can run immediately).
686 TEST(LayerAnimatorTest, StartThreadedAnimationThatCanRunImmediately) {
687 double epsilon = 0.00001;
688 LayerAnimatorTestController test_controller(
689 LayerAnimator::CreateDefaultAnimator());
690 AnimationContainerElement* element = test_controller.animator();
691 test_controller.animator()->set_disable_timer_for_test(true);
692 TestLayerAnimationDelegate delegate;
693 test_controller.animator()->SetDelegate(&delegate);
695 double start_opacity(0.0);
696 double target_opacity(1.0);
698 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
700 delegate.SetOpacityFromAnimation(start_opacity);
702 test_controller.animator()->StartAnimation(
703 new LayerAnimationSequence(
704 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
706 EXPECT_TRUE(test_controller.animator()->is_animating());
707 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
709 base::TimeTicks start_time = test_controller.animator()->last_step_time();
710 base::TimeTicks effective_start = start_time + delta;
712 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
713 cc::AnimationEvent::Started,
715 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
716 animation_group_id(),
717 cc::Animation::Opacity,
718 (effective_start - base::TimeTicks()).InSecondsF()));
720 element->Step(effective_start + delta/2);
722 EXPECT_TRUE(test_controller.animator()->is_animating());
723 EXPECT_NEAR(
724 0.5,
725 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
726 last_progressed_fraction(),
727 epsilon);
729 element->Step(effective_start + delta);
730 EXPECT_FALSE(test_controller.animator()->is_animating());
731 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
734 // Preempt by immediately setting new target.
735 TEST(LayerAnimatorTest, PreemptBySettingNewTarget) {
736 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
737 animator->set_disable_timer_for_test(true);
738 TestLayerAnimationDelegate delegate;
739 animator->SetDelegate(&delegate);
741 double start_opacity(0.0);
742 double target_opacity(1.0);
744 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
746 delegate.SetOpacityFromAnimation(start_opacity);
748 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
750 animator->StartAnimation(
751 new LayerAnimationSequence(
752 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
754 animator->StartAnimation(
755 new LayerAnimationSequence(
756 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
758 EXPECT_FALSE(animator->is_animating());
759 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
762 // Preempt by animating to new target, with a non-threaded animation.
763 TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) {
764 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
765 AnimationContainerElement* element = animator.get();
766 animator->set_disable_timer_for_test(true);
767 TestLayerAnimationDelegate delegate;
768 animator->SetDelegate(&delegate);
770 double start_brightness(0.0);
771 double middle_brightness(0.5);
772 double target_brightness(1.0);
774 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
776 delegate.SetBrightnessFromAnimation(start_brightness);
778 animator->set_preemption_strategy(
779 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
781 animator->StartAnimation(
782 new LayerAnimationSequence(
783 LayerAnimationElement::CreateBrightnessElement(target_brightness,
784 delta)));
786 base::TimeTicks start_time = animator->last_step_time();
788 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
790 animator->StartAnimation(
791 new LayerAnimationSequence(
792 LayerAnimationElement::CreateBrightnessElement(start_brightness,
793 delta)));
795 EXPECT_TRUE(animator->is_animating());
796 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
798 animator->StartAnimation(
799 new LayerAnimationSequence(
800 LayerAnimationElement::CreateBrightnessElement(start_brightness,
801 delta)));
803 EXPECT_TRUE(animator->is_animating());
805 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
807 EXPECT_TRUE(animator->is_animating());
808 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
809 0.5 * (start_brightness + middle_brightness));
811 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
813 EXPECT_FALSE(animator->is_animating());
814 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
817 // Preempt by animating to new target, with a threaded animation.
818 TEST(LayerAnimatorTest, PreemptThreadedByImmediatelyAnimatingToNewTarget) {
819 double epsilon = 0.00001;
820 LayerAnimatorTestController test_controller(
821 LayerAnimator::CreateDefaultAnimator());
822 AnimationContainerElement* element = test_controller.animator();
823 test_controller.animator()->set_disable_timer_for_test(true);
824 TestLayerAnimationDelegate delegate;
825 test_controller.animator()->SetDelegate(&delegate);
827 double start_opacity(0.0);
828 double middle_opacity(0.5);
829 double target_opacity(1.0);
831 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
833 delegate.SetOpacityFromAnimation(start_opacity);
835 test_controller.animator()->set_preemption_strategy(
836 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
838 test_controller.animator()->StartAnimation(
839 new LayerAnimationSequence(
840 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
842 base::TimeTicks start_time = test_controller.animator()->last_step_time();
843 base::TimeTicks effective_start = start_time + delta;
845 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
846 cc::AnimationEvent::Started,
848 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
849 animation_group_id(),
850 cc::Animation::Opacity,
851 (effective_start - base::TimeTicks()).InSecondsF()));
853 element->Step(effective_start + delta/2);
855 test_controller.animator()->StartAnimation(
856 new LayerAnimationSequence(
857 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
859 EXPECT_TRUE(test_controller.animator()->is_animating());
860 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
862 test_controller.animator()->StartAnimation(
863 new LayerAnimationSequence(
864 LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
866 EXPECT_TRUE(test_controller.animator()->is_animating());
868 base::TimeTicks second_effective_start = effective_start + delta;
870 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
871 cc::AnimationEvent::Started,
873 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
874 animation_group_id(),
875 cc::Animation::Opacity,
876 (second_effective_start - base::TimeTicks()).InSecondsF()));
878 element->Step(second_effective_start + delta/2);
880 EXPECT_TRUE(test_controller.animator()->is_animating());
881 EXPECT_NEAR(
882 0.5,
883 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
884 last_progressed_fraction(),
885 epsilon);
887 element->Step(second_effective_start + delta);
889 EXPECT_FALSE(test_controller.animator()->is_animating());
890 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
893 // Preempt by enqueuing the new animation.
894 TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) {
895 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
896 AnimationContainerElement* element = animator.get();
897 animator->set_disable_timer_for_test(true);
898 TestLayerAnimationDelegate delegate;
899 animator->SetDelegate(&delegate);
901 double start_brightness(0.0);
902 double middle_brightness(0.5);
903 double target_brightness(1.0);
905 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
907 delegate.SetBrightnessFromAnimation(start_brightness);
909 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
911 animator->StartAnimation(
912 new LayerAnimationSequence(
913 LayerAnimationElement::CreateBrightnessElement(target_brightness,
914 delta)));
916 base::TimeTicks start_time = animator->last_step_time();
918 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
920 animator->StartAnimation(
921 new LayerAnimationSequence(
922 LayerAnimationElement::CreateBrightnessElement(start_brightness,
923 delta)));
925 EXPECT_TRUE(animator->is_animating());
926 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
928 EXPECT_TRUE(animator->is_animating());
930 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
932 EXPECT_TRUE(animator->is_animating());
933 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
935 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
937 EXPECT_TRUE(animator->is_animating());
938 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
940 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
942 EXPECT_FALSE(animator->is_animating());
943 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
946 // Start an animation when there are sequences waiting in the queue. In this
947 // case, all pending and running animations should be finished, and the new
948 // animation started.
949 TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) {
950 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
951 AnimationContainerElement* element = animator.get();
952 animator->set_disable_timer_for_test(true);
953 TestLayerAnimationDelegate delegate;
954 animator->SetDelegate(&delegate);
956 double start_brightness(0.0);
957 double middle_brightness(0.5);
958 double target_brightness(1.0);
960 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
962 delegate.SetBrightnessFromAnimation(start_brightness);
964 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
966 animator->StartAnimation(
967 new LayerAnimationSequence(
968 LayerAnimationElement::CreateBrightnessElement(target_brightness,
969 delta)));
971 base::TimeTicks start_time = animator->last_step_time();
973 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
975 animator->StartAnimation(
976 new LayerAnimationSequence(
977 LayerAnimationElement::CreateBrightnessElement(middle_brightness,
978 delta)));
980 // Queue should now have two animations. Starting a third should replace the
981 // second.
982 animator->StartAnimation(
983 new LayerAnimationSequence(
984 LayerAnimationElement::CreateBrightnessElement(start_brightness,
985 delta)));
987 EXPECT_TRUE(animator->is_animating());
988 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
990 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
992 EXPECT_TRUE(animator->is_animating());
993 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
995 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
997 EXPECT_TRUE(animator->is_animating());
998 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1000 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1002 EXPECT_FALSE(animator->is_animating());
1003 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1006 TEST(LayerAnimatorTest, StartTogetherSetsLastStepTime) {
1007 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1008 animator->set_disable_timer_for_test(true);
1009 TestLayerAnimationDelegate delegate;
1010 animator->SetDelegate(&delegate);
1012 double start_grayscale(0.0);
1013 double target_grayscale(1.0);
1014 double start_brightness(0.1);
1015 double target_brightness(0.9);
1017 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1019 delegate.SetGrayscaleFromAnimation(start_grayscale);
1020 delegate.SetBrightnessFromAnimation(start_brightness);
1022 animator->set_preemption_strategy(
1023 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1025 animator->set_last_step_time(base::TimeTicks());
1027 animator->StartTogether(
1028 CreateMultiSequence(
1029 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1030 delta),
1031 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1032 delta)
1035 // If last step time was not set correctly, the resulting delta should be
1036 // miniscule (fractions of a millisecond). If set correctly, then the delta
1037 // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1038 // though a much smaller value would probably have sufficed.
1039 delta = gfx::FrameTime::Now() - animator->last_step_time();
1040 EXPECT_GT(60.0, delta.InSecondsF());
1043 //-------------------------------------------------------
1044 // Preempt by immediately setting new target.
1045 TEST(LayerAnimatorTest, MultiPreemptBySettingNewTarget) {
1046 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1047 animator->set_disable_timer_for_test(true);
1048 TestLayerAnimationDelegate delegate;
1049 animator->SetDelegate(&delegate);
1051 double start_opacity(0.0);
1052 double target_opacity(1.0);
1053 double start_brightness(0.1);
1054 double target_brightness(0.9);
1056 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1058 delegate.SetOpacityFromAnimation(start_opacity);
1059 delegate.SetBrightnessFromAnimation(start_brightness);
1061 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1063 animator->StartTogether(
1064 CreateMultiSequence(
1065 LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1066 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1067 delta)
1070 animator->StartTogether(
1071 CreateMultiSequence(
1072 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1073 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1074 delta)
1077 EXPECT_FALSE(animator->is_animating());
1078 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1079 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1082 // Preempt by animating to new target.
1083 TEST(LayerAnimatorTest, MultiPreemptByImmediatelyAnimatingToNewTarget) {
1084 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1085 AnimationContainerElement* element = animator.get();
1086 animator->set_disable_timer_for_test(true);
1087 TestLayerAnimationDelegate delegate;
1088 animator->SetDelegate(&delegate);
1090 double start_grayscale(0.0);
1091 double middle_grayscale(0.5);
1092 double target_grayscale(1.0);
1094 double start_brightness(0.1);
1095 double middle_brightness(0.2);
1096 double target_brightness(0.3);
1098 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1100 delegate.SetGrayscaleFromAnimation(start_grayscale);
1101 delegate.SetBrightnessFromAnimation(start_brightness);
1103 animator->set_preemption_strategy(
1104 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1106 animator->StartTogether(
1107 CreateMultiSequence(
1108 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1109 delta),
1110 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1111 delta)
1114 base::TimeTicks start_time = animator->last_step_time();
1116 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1118 animator->StartTogether(
1119 CreateMultiSequence(
1120 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1121 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1122 delta)));
1124 EXPECT_TRUE(animator->is_animating());
1125 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1126 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1128 animator->StartTogether(
1129 CreateMultiSequence(
1130 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1131 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1132 delta)));
1134 EXPECT_TRUE(animator->is_animating());
1136 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1138 EXPECT_TRUE(animator->is_animating());
1139 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(),
1140 0.5 * (start_grayscale + middle_grayscale));
1141 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
1142 0.5 * (start_brightness + middle_brightness));
1144 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1146 EXPECT_FALSE(animator->is_animating());
1147 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1148 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1151 // Preempt a threaded animation by animating to new target.
1152 TEST(LayerAnimatorTest, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget) {
1153 double epsilon = 0.00001;
1154 LayerAnimatorTestController test_controller(
1155 LayerAnimator::CreateDefaultAnimator());
1156 AnimationContainerElement* element = test_controller.animator();
1157 test_controller.animator()->set_disable_timer_for_test(true);
1158 TestLayerAnimationDelegate delegate;
1159 test_controller.animator()->SetDelegate(&delegate);
1161 double start_opacity(0.0);
1162 double middle_opacity(0.5);
1163 double target_opacity(1.0);
1165 double start_brightness(0.1);
1166 double middle_brightness(0.2);
1167 double target_brightness(0.3);
1169 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1171 delegate.SetOpacityFromAnimation(start_opacity);
1172 delegate.SetBrightnessFromAnimation(start_brightness);
1174 test_controller.animator()->set_preemption_strategy(
1175 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1177 test_controller.animator()->StartTogether(
1178 CreateMultiSequence(
1179 LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1180 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1181 delta)
1184 base::TimeTicks start_time = test_controller.animator()->last_step_time();
1185 base::TimeTicks effective_start = start_time + delta;
1187 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1188 cc::AnimationEvent::Started,
1190 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1191 animation_group_id(),
1192 cc::Animation::Opacity,
1193 (effective_start - base::TimeTicks()).InSecondsF()));
1195 element->Step(effective_start + delta/2);
1197 test_controller.animator()->StartTogether(
1198 CreateMultiSequence(
1199 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1200 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1201 delta)));
1203 EXPECT_TRUE(test_controller.animator()->is_animating());
1204 EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
1205 EXPECT_NEAR(delegate.GetBrightnessForAnimation(), middle_brightness, epsilon);
1207 test_controller.animator()->StartTogether(
1208 CreateMultiSequence(
1209 LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1210 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1211 delta)));
1213 EXPECT_TRUE(test_controller.animator()->is_animating());
1215 base::TimeTicks second_effective_start = effective_start + delta;
1217 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1218 cc::AnimationEvent::Started,
1220 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1221 animation_group_id(),
1222 cc::Animation::Opacity,
1223 (second_effective_start - base::TimeTicks()).InSecondsF()));
1225 element->Step(second_effective_start + delta/2);
1227 EXPECT_TRUE(test_controller.animator()->is_animating());
1228 EXPECT_NEAR(
1229 0.5,
1230 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1231 last_progressed_fraction(),
1232 epsilon);
1233 EXPECT_NEAR(delegate.GetBrightnessForAnimation(),
1234 0.5 * (start_brightness + middle_brightness),
1235 epsilon);
1237 element->Step(second_effective_start + delta);
1239 EXPECT_FALSE(test_controller.animator()->is_animating());
1240 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1241 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1244 // Preempt by enqueuing the new animation.
1245 TEST(LayerAnimatorTest, MultiPreemptEnqueueNewAnimation) {
1246 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1247 AnimationContainerElement* element = animator.get();
1248 animator->set_disable_timer_for_test(true);
1249 TestLayerAnimationDelegate delegate;
1250 animator->SetDelegate(&delegate);
1252 double start_grayscale(0.0);
1253 double middle_grayscale(0.5);
1254 double target_grayscale(1.0);
1256 double start_brightness(0.1);
1257 double middle_brightness(0.2);
1258 double target_brightness(0.3);
1260 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1262 delegate.SetGrayscaleFromAnimation(start_grayscale);
1263 delegate.SetBrightnessFromAnimation(start_brightness);
1265 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1267 animator->StartTogether(
1268 CreateMultiSequence(
1269 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1270 delta),
1271 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1272 delta)));
1274 base::TimeTicks start_time = animator->last_step_time();
1276 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1278 animator->StartTogether(
1279 CreateMultiSequence(
1280 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1281 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1282 delta)));
1284 EXPECT_TRUE(animator->is_animating());
1285 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1286 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1288 EXPECT_TRUE(animator->is_animating());
1290 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1292 EXPECT_TRUE(animator->is_animating());
1293 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1294 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1296 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1298 EXPECT_TRUE(animator->is_animating());
1299 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1300 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1302 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1304 EXPECT_FALSE(animator->is_animating());
1305 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1306 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1309 // Start an animation when there are sequences waiting in the queue. In this
1310 // case, all pending and running animations should be finished, and the new
1311 // animation started.
1312 TEST(LayerAnimatorTest, MultiPreemptByReplacingQueuedAnimations) {
1313 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1314 AnimationContainerElement* element = animator.get();
1315 animator->set_disable_timer_for_test(true);
1316 TestLayerAnimationDelegate delegate;
1317 animator->SetDelegate(&delegate);
1319 double start_grayscale(0.0);
1320 double middle_grayscale(0.5);
1321 double target_grayscale(1.0);
1323 double start_brightness(0.1);
1324 double middle_brightness(0.2);
1325 double target_brightness(0.3);
1327 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1329 delegate.SetGrayscaleFromAnimation(start_grayscale);
1330 delegate.SetBrightnessFromAnimation(start_brightness);
1332 animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
1334 animator->StartTogether(
1335 CreateMultiSequence(
1336 LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1337 delta),
1338 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1339 delta)));
1341 base::TimeTicks start_time = animator->last_step_time();
1343 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1345 animator->StartTogether(
1346 CreateMultiSequence(
1347 LayerAnimationElement::CreateGrayscaleElement(middle_grayscale,
1348 delta),
1349 LayerAnimationElement::CreateBrightnessElement(middle_brightness,
1350 delta)));
1352 // Queue should now have two animations. Starting a third should replace the
1353 // second.
1354 animator->StartTogether(
1355 CreateMultiSequence(
1356 LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1357 LayerAnimationElement::CreateBrightnessElement(start_brightness,
1358 delta)));
1360 EXPECT_TRUE(animator->is_animating());
1361 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1362 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1364 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1366 EXPECT_TRUE(animator->is_animating());
1367 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1368 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1370 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1372 EXPECT_TRUE(animator->is_animating());
1373 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1374 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1376 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1378 EXPECT_FALSE(animator->is_animating());
1379 EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1380 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1382 //-------------------------------------------------------
1383 // Test that non-threaded cyclic sequences continue to animate.
1384 TEST(LayerAnimatorTest, CyclicSequences) {
1385 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1386 AnimationContainerElement* element = animator.get();
1387 animator->set_disable_timer_for_test(true);
1388 TestLayerAnimationDelegate delegate;
1389 animator->SetDelegate(&delegate);
1391 double start_brightness(0.0);
1392 double target_brightness(1.0);
1394 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1396 delegate.SetBrightnessFromAnimation(start_brightness);
1398 scoped_ptr<LayerAnimationSequence> sequence(
1399 new LayerAnimationSequence(
1400 LayerAnimationElement::CreateBrightnessElement(target_brightness,
1401 delta)));
1403 sequence->AddElement(
1404 LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
1406 sequence->set_is_cyclic(true);
1408 animator->StartAnimation(sequence.release());
1410 base::TimeTicks start_time = animator->last_step_time();
1412 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1414 EXPECT_TRUE(animator->is_animating());
1415 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1417 element->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1419 EXPECT_TRUE(animator->is_animating());
1420 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1422 element->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
1424 EXPECT_TRUE(animator->is_animating());
1425 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1427 // Skip ahead by a lot.
1428 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000));
1430 EXPECT_TRUE(animator->is_animating());
1431 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1433 // Skip ahead by a lot.
1434 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000));
1436 EXPECT_TRUE(animator->is_animating());
1437 EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1439 animator->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS);
1441 EXPECT_FALSE(animator->is_animating());
1444 // Test that threaded cyclic sequences continue to animate.
1445 TEST(LayerAnimatorTest, ThreadedCyclicSequences) {
1446 LayerAnimatorTestController test_controller(
1447 LayerAnimator::CreateDefaultAnimator());
1448 AnimationContainerElement* element = test_controller.animator();
1449 test_controller.animator()->set_disable_timer_for_test(true);
1450 TestLayerAnimationDelegate delegate;
1451 test_controller.animator()->SetDelegate(&delegate);
1453 double start_opacity(0.0);
1454 double target_opacity(1.0);
1456 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1458 delegate.SetOpacityFromAnimation(start_opacity);
1460 scoped_ptr<LayerAnimationSequence> sequence(
1461 new LayerAnimationSequence(
1462 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
1464 sequence->AddElement(
1465 LayerAnimationElement::CreateOpacityElement(start_opacity, delta));
1467 sequence->set_is_cyclic(true);
1469 test_controller.animator()->StartAnimation(sequence.release());
1471 base::TimeTicks start_time = test_controller.animator()->last_step_time();
1472 base::TimeTicks effective_start = start_time + 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 (effective_start - base::TimeTicks()).InSecondsF()));
1482 element->Step(effective_start + delta);
1483 EXPECT_TRUE(test_controller.animator()->is_animating());
1484 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1486 base::TimeTicks second_effective_start = effective_start + 2 * delta;
1487 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1488 cc::AnimationEvent::Started,
1490 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1491 animation_group_id(),
1492 cc::Animation::Opacity,
1493 (second_effective_start - base::TimeTicks()).InSecondsF()));
1495 element->Step(second_effective_start + delta);
1497 EXPECT_TRUE(test_controller.animator()->is_animating());
1498 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1500 base::TimeTicks third_effective_start = second_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 (third_effective_start - base::TimeTicks()).InSecondsF()));
1509 element->Step(third_effective_start + delta);
1510 EXPECT_TRUE(test_controller.animator()->is_animating());
1511 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1513 base::TimeTicks fourth_effective_start = third_effective_start + 2 * delta;
1514 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1515 cc::AnimationEvent::Started,
1517 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1518 animation_group_id(),
1519 cc::Animation::Opacity,
1520 (fourth_effective_start - base::TimeTicks()).InSecondsF()));
1522 // Skip ahead by a lot.
1523 element->Step(fourth_effective_start + 1000 * delta);
1525 EXPECT_TRUE(test_controller.animator()->is_animating());
1526 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1528 base::TimeTicks fifth_effective_start = fourth_effective_start + 1001 * delta;
1529 test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1530 cc::AnimationEvent::Started,
1532 test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1533 animation_group_id(),
1534 cc::Animation::Opacity,
1535 (fifth_effective_start - base::TimeTicks()).InSecondsF()));
1537 // Skip ahead by a lot.
1538 element->Step(fifth_effective_start + 999 * delta);
1540 EXPECT_TRUE(test_controller.animator()->is_animating());
1541 EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1543 test_controller.animator()->StopAnimatingProperty(
1544 LayerAnimationElement::OPACITY);
1546 EXPECT_FALSE(test_controller.animator()->is_animating());
1549 TEST(LayerAnimatorTest, AddObserverExplicit) {
1550 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1551 AnimationContainerElement* element = animator.get();
1552 animator->set_disable_timer_for_test(true);
1553 TestLayerAnimationObserver observer;
1554 TestLayerAnimationDelegate delegate;
1555 animator->SetDelegate(&delegate);
1556 animator->AddObserver(&observer);
1557 observer.set_requires_notification_when_animator_destroyed(true);
1559 EXPECT_TRUE(!observer.last_ended_sequence());
1561 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1563 delegate.SetBrightnessFromAnimation(0.0f);
1565 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1566 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1568 animator->StartAnimation(sequence);
1570 EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1572 base::TimeTicks start_time = animator->last_step_time();
1574 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1576 EXPECT_EQ(observer.last_ended_sequence(), sequence);
1578 // |sequence| has been destroyed. Recreate it to test abort.
1579 sequence = new LayerAnimationSequence(
1580 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1582 animator->StartAnimation(sequence);
1584 animator = NULL;
1586 EXPECT_EQ(observer.last_aborted_sequence(), sequence);
1589 // Tests that an observer added to a scoped settings object is still notified
1590 // when the object goes out of scope.
1591 TEST(LayerAnimatorTest, ImplicitAnimationObservers) {
1592 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1593 AnimationContainerElement* element = animator.get();
1594 animator->set_disable_timer_for_test(true);
1595 TestImplicitAnimationObserver observer(false);
1596 TestLayerAnimationDelegate delegate;
1597 animator->SetDelegate(&delegate);
1599 EXPECT_FALSE(observer.animations_completed());
1600 animator->SetBrightness(1.0f);
1603 ScopedLayerAnimationSettings settings(animator.get());
1604 settings.AddObserver(&observer);
1605 animator->SetBrightness(0.0f);
1608 EXPECT_FALSE(observer.animations_completed());
1609 base::TimeTicks start_time = animator->last_step_time();
1610 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1611 EXPECT_TRUE(observer.animations_completed());
1612 EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1613 LayerAnimationElement::BRIGHTNESS));
1614 EXPECT_FLOAT_EQ(0.0f, delegate.GetBrightnessForAnimation());
1617 // Tests that an observer added to a scoped settings object is still notified
1618 // when the object goes out of scope due to the animation being interrupted.
1619 TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) {
1620 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1621 animator->set_disable_timer_for_test(true);
1622 TestImplicitAnimationObserver observer(false);
1623 TestLayerAnimationDelegate delegate;
1624 animator->SetDelegate(&delegate);
1626 EXPECT_FALSE(observer.animations_completed());
1627 animator->SetBrightness(1.0f);
1630 ScopedLayerAnimationSettings settings(animator.get());
1631 settings.AddObserver(&observer);
1632 animator->SetBrightness(0.0f);
1635 EXPECT_FALSE(observer.animations_completed());
1636 // This should interrupt the implicit animation causing the observer to be
1637 // notified immediately.
1638 animator->SetBrightness(1.0f);
1639 EXPECT_TRUE(observer.animations_completed());
1640 EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1641 LayerAnimationElement::BRIGHTNESS));
1642 EXPECT_FLOAT_EQ(1.0f, delegate.GetBrightnessForAnimation());
1645 // Tests that an observer added to a scoped settings object is not notified
1646 // when the animator is destroyed unless explicitly requested.
1647 TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) {
1648 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1649 animator->set_disable_timer_for_test(true);
1650 TestImplicitAnimationObserver observer_notify(true);
1651 TestImplicitAnimationObserver observer_do_not_notify(false);
1652 TestLayerAnimationDelegate delegate;
1653 animator->SetDelegate(&delegate);
1655 EXPECT_FALSE(observer_notify.animations_completed());
1656 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1658 animator->SetBrightness(1.0f);
1661 ScopedLayerAnimationSettings settings(animator.get());
1662 settings.AddObserver(&observer_notify);
1663 settings.AddObserver(&observer_do_not_notify);
1664 animator->SetBrightness(0.0f);
1667 EXPECT_FALSE(observer_notify.animations_completed());
1668 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1669 animator = NULL;
1670 EXPECT_TRUE(observer_notify.animations_completed());
1671 EXPECT_TRUE(observer_notify.WasAnimationAbortedForProperty(
1672 LayerAnimationElement::BRIGHTNESS));
1673 EXPECT_FALSE(observer_do_not_notify.animations_completed());
1676 TEST(LayerAnimatorTest, AbortedAnimationStatusInImplicitObservers) {
1677 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1678 animator->set_disable_timer_for_test(true);
1679 TestImplicitAnimationObserver observer(false);
1680 TestLayerAnimationDelegate delegate;
1681 animator->SetDelegate(&delegate);
1683 EXPECT_FALSE(observer.animations_completed());
1684 animator->SetBrightness(1.0f);
1687 ScopedLayerAnimationSettings settings(animator.get());
1688 settings.AddObserver(&observer);
1689 animator->SetBrightness(0.0f);
1691 EXPECT_FALSE(observer.animations_completed());
1693 animator->AbortAllAnimations();
1694 EXPECT_TRUE(observer.animations_completed());
1695 EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1696 LayerAnimationElement::BRIGHTNESS));
1697 EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
1698 LayerAnimationElement::OPACITY));
1700 observer.set_animations_completed(false);
1702 ScopedLayerAnimationSettings settings(animator.get());
1703 settings.AddObserver(&observer);
1704 animator->SetOpacity(0.0f);
1706 EXPECT_FALSE(observer.animations_completed());
1708 animator->AbortAllAnimations();
1709 EXPECT_TRUE(observer.animations_completed());
1710 EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1711 LayerAnimationElement::BRIGHTNESS));
1712 EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1713 LayerAnimationElement::OPACITY));
1716 TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) {
1717 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1718 AnimationContainerElement* element = animator.get();
1719 animator->set_disable_timer_for_test(true);
1720 TestLayerAnimationObserver observer;
1721 TestLayerAnimationObserver removed_observer;
1722 TestLayerAnimationDelegate delegate;
1723 animator->SetDelegate(&delegate);
1725 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1727 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1728 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1730 sequence->AddObserver(&observer);
1731 sequence->AddObserver(&removed_observer);
1733 animator->StartAnimation(sequence);
1735 EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1736 EXPECT_TRUE(!observer.last_ended_sequence());
1737 EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence);
1738 EXPECT_TRUE(!removed_observer.last_ended_sequence());
1740 // This should stop the observer from observing sequence.
1741 animator->RemoveObserver(&removed_observer);
1743 base::TimeTicks start_time = animator->last_step_time();
1745 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1747 EXPECT_EQ(observer.last_ended_sequence(), sequence);
1748 EXPECT_TRUE(!removed_observer.last_ended_sequence());
1751 TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) {
1752 TestLayerAnimationDelegate delegate;
1753 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1754 animator->set_disable_timer_for_test(true);
1756 scoped_ptr<TestLayerAnimationObserver> observer(
1757 new TestLayerAnimationObserver);
1758 animator->SetDelegate(&delegate);
1759 animator->AddObserver(observer.get());
1761 delegate.SetOpacityFromAnimation(0.0f);
1763 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1764 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1765 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
1767 animator->StartAnimation(sequence);
1769 // |observer| should be attached to |sequence|.
1770 EXPECT_TRUE(sequence->observers_.might_have_observers());
1772 // Now, release |observer|
1773 observer.reset();
1775 // And |sequence| should no longer be attached to |observer|.
1776 EXPECT_FALSE(sequence->observers_.might_have_observers());
1779 TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) {
1780 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1781 AnimationContainerElement* element = animator.get();
1782 animator->set_disable_timer_for_test(true);
1784 TestImplicitAnimationObserver observer(false);
1785 TestLayerAnimationDelegate delegate;
1786 animator->SetDelegate(&delegate);
1788 delegate.SetBrightnessFromAnimation(0.0f);
1791 ScopedLayerAnimationSettings setter(animator.get());
1793 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1794 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1795 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1797 animator->StartAnimation(sequence);
1798 base::TimeTicks start_time = animator->last_step_time();
1799 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1801 setter.AddObserver(&observer);
1803 // Start observing an in-flight animation.
1804 sequence->AddObserver(&observer);
1806 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1809 EXPECT_TRUE(observer.animations_completed());
1810 EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1811 LayerAnimationElement::BRIGHTNESS));
1814 TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) {
1815 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1816 AnimationContainerElement* element = animator.get();
1817 animator->set_disable_timer_for_test(true);
1819 TestImplicitAnimationObserver observer(false);
1820 TestLayerAnimationDelegate delegate;
1821 animator->SetDelegate(&delegate);
1823 delegate.SetBrightnessFromAnimation(0.0f);
1824 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1825 LayerAnimationSequence* sequence = new LayerAnimationSequence(
1826 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1829 ScopedLayerAnimationSettings setter(animator.get());
1830 setter.AddObserver(&observer);
1832 animator->StartAnimation(sequence);
1833 base::TimeTicks start_time = animator->last_step_time();
1834 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1837 EXPECT_FALSE(observer.animations_completed());
1839 // Stop observing an in-flight animation.
1840 sequence->RemoveObserver(&observer);
1842 EXPECT_TRUE(observer.animations_completed());
1844 // The animation didn't complete, and neither was it aborted.
1845 EXPECT_FALSE(observer.WasAnimationCompletedForProperty(
1846 LayerAnimationElement::BRIGHTNESS));
1847 EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
1848 LayerAnimationElement::BRIGHTNESS));
1851 // This checks that if an animation is deleted due to a callback, that the
1852 // animator does not try to use the deleted animation. For example, if we have
1853 // two running animations, and the first finishes and the resulting callback
1854 // causes the second to be deleted, we should not attempt to animate the second
1855 // animation.
1856 TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnEnd) {
1857 ScopedAnimationDurationScaleMode normal_duration_mode(
1858 ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1859 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1860 AnimationContainerElement* element = animator.get();
1861 animator->set_disable_timer_for_test(true);
1862 TestLayerAnimationDelegate delegate;
1863 animator->SetDelegate(&delegate);
1865 double start_brightness(0.0);
1866 double target_brightness(1.0);
1868 gfx::Rect start_bounds(0, 0, 50, 50);
1869 gfx::Rect target_bounds(5, 5, 5, 5);
1871 delegate.SetBrightnessFromAnimation(start_brightness);
1872 delegate.SetBoundsFromAnimation(start_bounds);
1874 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
1875 base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2);
1876 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3);
1878 scoped_ptr<DeletingLayerAnimationObserver> observer(
1879 new DeletingLayerAnimationObserver(animator.get()));
1881 animator->AddObserver(observer.get());
1883 animator->StartAnimation(
1884 new LayerAnimationSequence(
1885 LayerAnimationElement::CreateBrightnessElement(
1886 target_brightness, brightness_delta)));
1888 animator->StartAnimation(new LayerAnimationSequence(
1889 LayerAnimationElement::CreateBoundsElement(
1890 target_bounds, bounds_delta)));
1891 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1893 base::TimeTicks start_time = animator->last_step_time();
1894 element->Step(start_time + halfway_delta);
1896 // Completing the brightness animation should have stopped the bounds
1897 // animation.
1898 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1900 animator->RemoveObserver(observer.get());
1903 // Ensure that stopping animation in a bounds change does not crash and that
1904 // animation gets stopped correctly.
1905 // This scenario is possible when animation is restarted from inside a
1906 // callback triggered by the animation progress.
1907 TEST(LayerAnimatorTest, CallbackDeletesAnimationInProgress) {
1909 class TestLayerAnimationDeletingDelegate : public TestLayerAnimationDelegate {
1910 public:
1911 TestLayerAnimationDeletingDelegate(LayerAnimator* animator, int max_width)
1912 : animator_(animator),
1913 max_width_(max_width) {
1916 virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE {
1917 TestLayerAnimationDelegate::SetBoundsFromAnimation(bounds);
1918 if (bounds.width() > max_width_)
1919 animator_->StopAnimating();
1921 private:
1922 LayerAnimator* animator_;
1923 int max_width_;
1924 // Allow copy and assign.
1927 ScopedAnimationDurationScaleMode normal_duration_mode(
1928 ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1929 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1930 AnimationContainerElement* element = animator.get();
1931 animator->set_disable_timer_for_test(true);
1932 TestLayerAnimationDeletingDelegate delegate(animator.get(), 30);
1933 animator->SetDelegate(&delegate);
1935 gfx::Rect start_bounds(0, 0, 0, 0);
1936 gfx::Rect target_bounds(5, 5, 50, 50);
1938 delegate.SetBoundsFromAnimation(start_bounds);
1940 base::TimeDelta bounds_delta1 = base::TimeDelta::FromMilliseconds(333);
1941 base::TimeDelta bounds_delta2 = base::TimeDelta::FromMilliseconds(666);
1942 base::TimeDelta bounds_delta = base::TimeDelta::FromMilliseconds(1000);
1943 base::TimeDelta final_delta = base::TimeDelta::FromMilliseconds(1500);
1945 animator->StartAnimation(new LayerAnimationSequence(
1946 LayerAnimationElement::CreateBoundsElement(
1947 target_bounds, bounds_delta)));
1948 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1950 base::TimeTicks start_time = animator->last_step_time();
1951 ASSERT_NO_FATAL_FAILURE(element->Step(start_time + bounds_delta1));
1952 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1954 // The next step should change the animated bounds past the threshold and
1955 // cause the animaton to stop.
1956 ASSERT_NO_FATAL_FAILURE(element->Step(start_time + bounds_delta2));
1957 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1958 ASSERT_NO_FATAL_FAILURE(element->Step(start_time + final_delta));
1960 // Completing the animation should have stopped the bounds
1961 // animation.
1962 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1965 // Similar to the ObserverDeletesAnimationsOnEnd test above except that it
1966 // tests the behavior when the OnLayerAnimationAborted() callback causes
1967 // all of the animator's other animations to be deleted.
1968 TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnAbort) {
1969 ScopedAnimationDurationScaleMode normal_duration_mode(
1970 ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1971 scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1972 animator->set_disable_timer_for_test(true);
1973 TestLayerAnimationDelegate delegate;
1974 animator->SetDelegate(&delegate);
1976 double start_brightness(0.0);
1977 double target_brightness(1.0);
1978 gfx::Rect start_bounds(0, 0, 50, 50);
1979 gfx::Rect target_bounds(5, 5, 5, 5);
1980 base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
1981 base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(2);
1983 delegate.SetBrightnessFromAnimation(start_brightness);
1984 delegate.SetBoundsFromAnimation(start_bounds);
1986 scoped_ptr<DeletingLayerAnimationObserver> observer(
1987 new DeletingLayerAnimationObserver(animator.get()));
1988 animator->AddObserver(observer.get());
1990 animator->StartAnimation(
1991 new LayerAnimationSequence(
1992 LayerAnimationElement::CreateBrightnessElement(
1993 target_brightness, brightness_delta)));
1994 animator->StartAnimation(new LayerAnimationSequence(
1995 LayerAnimationElement::CreateBoundsElement(
1996 target_bounds, bounds_delta)));
1997 ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1999 animator->set_preemption_strategy(
2000 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2001 animator->StartAnimation(
2002 new LayerAnimationSequence(
2003 LayerAnimationElement::CreateBrightnessElement(
2004 target_brightness, brightness_delta)));
2006 // Starting the second brightness animation should have aborted the initial
2007 // brightness animation. |observer| should have stopped the bounds animation
2008 // as a result.
2009 ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2011 animator->RemoveObserver(observer.get());
2014 // Check that setting a property during an animation with a default animator
2015 // cancels the original animation.
2016 TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) {
2017 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2018 animator->set_disable_timer_for_test(true);
2019 TestLayerAnimationDelegate delegate;
2020 animator->SetDelegate(&delegate);
2022 double start_opacity(0.0);
2023 double target_opacity(1.0);
2025 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2027 delegate.SetOpacityFromAnimation(start_opacity);
2029 scoped_ptr<LayerAnimationSequence> sequence(
2030 new LayerAnimationSequence(
2031 LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
2033 animator->StartAnimation(sequence.release());
2035 animator->SetOpacity(0.5);
2037 EXPECT_FALSE(animator->is_animating());
2038 EXPECT_EQ(0.5, animator->GetTargetOpacity());
2041 // Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
2042 // second sequence to be leaked.
2043 TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) {
2044 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2045 animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
2046 animator->set_disable_timer_for_test(true);
2047 TestLayerAnimationDelegate delegate;
2048 animator->SetDelegate(&delegate);
2050 gfx::Rect start_bounds(0, 0, 50, 50);
2051 gfx::Rect middle_bounds(10, 10, 100, 100);
2052 gfx::Rect target_bounds(5, 5, 5, 5);
2054 delegate.SetBoundsFromAnimation(start_bounds);
2057 // start an implicit bounds animation.
2058 ScopedLayerAnimationSettings settings(animator.get());
2059 animator->SetBounds(middle_bounds);
2062 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2064 int num_live_instances = 0;
2065 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2066 scoped_ptr<TestLayerAnimationSequence> sequence(
2067 new TestLayerAnimationSequence(
2068 LayerAnimationElement::CreateBoundsElement(target_bounds, delta),
2069 &num_live_instances));
2071 EXPECT_EQ(1, num_live_instances);
2073 // This should interrupt the running sequence causing us to immediately set
2074 // the target value. The sequence should alse be destructed.
2075 animator->StartAnimation(sequence.release());
2077 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2078 EXPECT_EQ(0, num_live_instances);
2079 CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
2082 // Verifies GetTargetOpacity() works when multiple sequences are scheduled.
2083 TEST(LayerAnimatorTest, GetTargetOpacity) {
2084 TestLayerAnimationDelegate delegate;
2085 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2086 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2087 animator->set_disable_timer_for_test(true);
2088 animator->SetDelegate(&delegate);
2090 delegate.SetOpacityFromAnimation(0.0);
2093 ScopedLayerAnimationSettings settings(animator.get());
2094 animator->SetOpacity(0.5);
2095 EXPECT_EQ(0.5, animator->GetTargetOpacity());
2097 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2098 animator->SetOpacity(1.0);
2099 EXPECT_EQ(1.0, animator->GetTargetOpacity());
2103 // Verifies GetTargetBrightness() works when multiple sequences are scheduled.
2104 TEST(LayerAnimatorTest, GetTargetBrightness) {
2105 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2106 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2107 animator->set_disable_timer_for_test(true);
2108 TestLayerAnimationDelegate delegate;
2109 animator->SetDelegate(&delegate);
2111 delegate.SetBrightnessFromAnimation(0.0);
2114 ScopedLayerAnimationSettings settings(animator.get());
2115 animator->SetBrightness(0.5);
2116 EXPECT_EQ(0.5, animator->GetTargetBrightness());
2118 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2119 animator->SetBrightness(1.0);
2120 EXPECT_EQ(1.0, animator->GetTargetBrightness());
2124 // Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
2125 TEST(LayerAnimatorTest, GetTargetGrayscale) {
2126 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2127 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2128 animator->set_disable_timer_for_test(true);
2129 TestLayerAnimationDelegate delegate;
2130 animator->SetDelegate(&delegate);
2132 delegate.SetGrayscaleFromAnimation(0.0);
2135 ScopedLayerAnimationSettings settings(animator.get());
2136 animator->SetGrayscale(0.5);
2137 EXPECT_EQ(0.5, animator->GetTargetGrayscale());
2139 // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2140 animator->SetGrayscale(1.0);
2141 EXPECT_EQ(1.0, animator->GetTargetGrayscale());
2145 // Verifies color property is modified appropriately.
2146 TEST(LayerAnimatorTest, Color) {
2147 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2148 AnimationContainerElement* element = animator.get();
2149 animator->set_disable_timer_for_test(true);
2150 TestLayerAnimationDelegate delegate;
2151 animator->SetDelegate(&delegate);
2153 SkColor start_color = SkColorSetARGB( 64, 20, 40, 60);
2154 SkColor middle_color = SkColorSetARGB(128, 35, 70, 120);
2155 SkColor target_color = SkColorSetARGB(192, 40, 80, 140);
2157 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2159 delegate.SetColorFromAnimation(start_color);
2161 animator->ScheduleAnimation(
2162 new LayerAnimationSequence(
2163 LayerAnimationElement::CreateColorElement(target_color, delta)));
2165 EXPECT_TRUE(animator->is_animating());
2166 EXPECT_EQ(ColorToString(start_color),
2167 ColorToString(delegate.GetColorForAnimation()));
2169 base::TimeTicks start_time = animator->last_step_time();
2171 element->Step(start_time + base::TimeDelta::FromMilliseconds(500));
2173 EXPECT_TRUE(animator->is_animating());
2174 EXPECT_EQ(ColorToString(middle_color),
2175 ColorToString(delegate.GetColorForAnimation()));
2177 element->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2179 EXPECT_FALSE(animator->is_animating());
2180 EXPECT_EQ(ColorToString(target_color),
2181 ColorToString(delegate.GetColorForAnimation()));
2184 // Verifies SchedulePauseForProperties().
2185 TEST(LayerAnimatorTest, SchedulePauseForProperties) {
2186 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2187 animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2188 animator->SchedulePauseForProperties(
2189 base::TimeDelta::FromMilliseconds(100),
2190 LayerAnimationElement::TRANSFORM | LayerAnimationElement::BOUNDS);
2191 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM));
2192 EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2193 EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY));
2197 class AnimatorOwner {
2198 public:
2199 AnimatorOwner()
2200 : animator_(LayerAnimator::CreateDefaultAnimator()) {
2203 LayerAnimator* animator() { return animator_.get(); }
2205 private:
2206 scoped_refptr<LayerAnimator> animator_;
2208 DISALLOW_COPY_AND_ASSIGN(AnimatorOwner);
2211 class DeletingObserver : public LayerAnimationObserver {
2212 public:
2213 DeletingObserver(bool* was_deleted)
2214 : animator_owner_(new AnimatorOwner),
2215 delete_on_animation_ended_(false),
2216 delete_on_animation_aborted_(false),
2217 delete_on_animation_scheduled_(false),
2218 was_deleted_(was_deleted) {
2219 animator()->AddObserver(this);
2222 virtual ~DeletingObserver() {
2223 animator()->RemoveObserver(this);
2224 *was_deleted_ = true;
2227 LayerAnimator* animator() { return animator_owner_->animator(); }
2229 bool delete_on_animation_ended() const {
2230 return delete_on_animation_ended_;
2232 void set_delete_on_animation_ended(bool enabled) {
2233 delete_on_animation_ended_ = enabled;
2236 bool delete_on_animation_aborted() const {
2237 return delete_on_animation_aborted_;
2239 void set_delete_on_animation_aborted(bool enabled) {
2240 delete_on_animation_aborted_ = enabled;
2243 bool delete_on_animation_scheduled() const {
2244 return delete_on_animation_scheduled_;
2246 void set_delete_on_animation_scheduled(bool enabled) {
2247 delete_on_animation_scheduled_ = enabled;
2250 // LayerAnimationObserver implementation.
2251 virtual void OnLayerAnimationEnded(
2252 LayerAnimationSequence* sequence) OVERRIDE {
2253 if (delete_on_animation_ended_)
2254 delete this;
2257 virtual void OnLayerAnimationAborted(
2258 LayerAnimationSequence* sequence) OVERRIDE {
2259 if (delete_on_animation_aborted_)
2260 delete this;
2263 virtual void OnLayerAnimationScheduled(
2264 LayerAnimationSequence* sequence) OVERRIDE {
2265 if (delete_on_animation_scheduled_)
2266 delete this;
2269 private:
2270 scoped_ptr<AnimatorOwner> animator_owner_;
2271 bool delete_on_animation_ended_;
2272 bool delete_on_animation_aborted_;
2273 bool delete_on_animation_scheduled_;
2274 bool* was_deleted_;
2276 DISALLOW_COPY_AND_ASSIGN(DeletingObserver);
2279 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) {
2280 bool observer_was_deleted = false;
2281 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2282 observer->set_delete_on_animation_ended(true);
2283 observer->set_delete_on_animation_aborted(true);
2284 LayerAnimator* animator = observer->animator();
2285 AnimationContainerElement* element = observer->animator();
2286 animator->set_disable_timer_for_test(true);
2287 TestLayerAnimationDelegate delegate;
2288 animator->SetDelegate(&delegate);
2290 delegate.SetBrightnessFromAnimation(0.0f);
2292 gfx::Rect start_bounds(0, 0, 50, 50);
2293 gfx::Rect target_bounds(10, 10, 100, 100);
2295 delegate.SetBoundsFromAnimation(start_bounds);
2297 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2298 LayerAnimationSequence* brightness_sequence = new LayerAnimationSequence(
2299 LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
2300 animator->StartAnimation(brightness_sequence);
2302 delta = base::TimeDelta::FromSeconds(2);
2303 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2304 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2305 animator->StartAnimation(bounds_sequence);
2307 base::TimeTicks start_time = animator->last_step_time();
2308 element->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
2310 EXPECT_TRUE(observer_was_deleted);
2313 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterStoppingAnimating) {
2314 bool observer_was_deleted = false;
2315 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2316 observer->set_delete_on_animation_ended(true);
2317 observer->set_delete_on_animation_aborted(true);
2318 LayerAnimator* animator = observer->animator();
2319 animator->set_disable_timer_for_test(true);
2320 TestLayerAnimationDelegate delegate;
2321 animator->SetDelegate(&delegate);
2323 delegate.SetOpacityFromAnimation(0.0f);
2325 gfx::Rect start_bounds(0, 0, 50, 50);
2326 gfx::Rect target_bounds(10, 10, 100, 100);
2328 delegate.SetBoundsFromAnimation(start_bounds);
2330 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2331 LayerAnimationSequence* opacity_sequence = new LayerAnimationSequence(
2332 LayerAnimationElement::CreateOpacityElement(1.0f, delta));
2333 animator->StartAnimation(opacity_sequence);
2335 delta = base::TimeDelta::FromSeconds(2);
2336 LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2337 LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2338 animator->StartAnimation(bounds_sequence);
2340 animator->StopAnimating();
2342 EXPECT_TRUE(observer_was_deleted);
2345 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterScheduling) {
2346 bool observer_was_deleted = false;
2347 TestLayerAnimationDelegate delegate;
2348 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2349 observer->set_delete_on_animation_scheduled(true);
2350 LayerAnimator* animator = observer->animator();
2351 animator->set_disable_timer_for_test(true);
2352 animator->SetDelegate(&delegate);
2354 delegate.SetOpacityFromAnimation(0.0f);
2356 gfx::Rect start_bounds(0, 0, 50, 50);
2357 gfx::Rect target_bounds(10, 10, 100, 100);
2359 delegate.SetBoundsFromAnimation(start_bounds);
2361 std::vector<LayerAnimationSequence*> to_start;
2363 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2364 to_start.push_back(new LayerAnimationSequence(
2365 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2367 delta = base::TimeDelta::FromSeconds(2);
2368 to_start.push_back(new LayerAnimationSequence(
2369 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2371 animator->ScheduleTogether(to_start);
2373 EXPECT_TRUE(observer_was_deleted);
2376 TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterAborted) {
2377 bool observer_was_deleted = false;
2378 DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2379 TestLayerAnimationDelegate delegate;
2380 observer->set_delete_on_animation_aborted(true);
2381 LayerAnimator* animator = observer->animator();
2382 animator->set_preemption_strategy(
2383 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2384 animator->set_disable_timer_for_test(true);
2385 animator->SetDelegate(&delegate);
2387 delegate.SetOpacityFromAnimation(0.0f);
2389 gfx::Rect start_bounds(0, 0, 50, 50);
2390 gfx::Rect target_bounds(10, 10, 100, 100);
2392 delegate.SetBoundsFromAnimation(start_bounds);
2394 std::vector<LayerAnimationSequence*> to_start;
2396 base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2397 to_start.push_back(new LayerAnimationSequence(
2398 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2400 delta = base::TimeDelta::FromSeconds(2);
2401 to_start.push_back(new LayerAnimationSequence(
2402 LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2404 animator->ScheduleTogether(to_start);
2406 EXPECT_FALSE(observer_was_deleted);
2408 animator->StartAnimation(new LayerAnimationSequence(
2409 LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2411 EXPECT_TRUE(observer_was_deleted);
2415 TEST(LayerAnimatorTest, TestSetterRespectEnqueueStrategy) {
2416 TestLayerAnimationDelegate delegate;
2417 scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2418 animator->set_disable_timer_for_test(true);
2420 animator->SetDelegate(&delegate);
2422 float start_opacity = 0.0f;
2423 float target_opacity = 1.0f;
2424 float magic_opacity = 0.123f;
2426 delegate.SetOpacityFromAnimation(start_opacity);
2428 ScopedLayerAnimationSettings settings(animator.get());
2429 settings.SetPreemptionStrategy(
2430 LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2431 settings.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2432 animator->SetOpacity(target_opacity);
2434 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2436 settings.SetPreemptionStrategy(
2437 LayerAnimator::ENQUEUE_NEW_ANIMATION);
2438 settings.SetTransitionDuration(base::TimeDelta());
2439 animator->SetOpacity(magic_opacity);
2441 EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2444 TEST(LayerAnimatorTest, TestScopedCounterAnimation) {
2445 Layer parent, child;
2446 parent.Add(&child);
2448 gfx::Transform parent_begin, parent_end;
2450 parent_end.Scale3d(2.0, 0.5, 1.0);
2452 // Parent animates from identity to the end value. The counter animation will
2453 // start at the end value and animate back to identity.
2454 gfx::Transform child_begin(parent_end);
2456 child.SetTransform(child_begin);
2457 parent.SetTransform(parent_begin);
2459 EXPECT_FALSE(child.GetAnimator()->is_animating());
2461 ScopedLayerAnimationSettings settings(parent.GetAnimator());
2462 settings.SetInverselyAnimatedBaseLayer(&parent);
2463 settings.AddInverselyAnimatedLayer(&child);
2465 parent.SetTransform(parent_end);
2467 EXPECT_TRUE(child.GetAnimator()->is_animating());
2468 EXPECT_TRUE(child.GetTargetTransform().IsIdentity())
2469 << child.GetTargetTransform().ToString();
2473 } // namespace ui