layer_animator_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.
4
5#include "ui/compositor/layer_animator.h"
6
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/layer_animator_collection.h"
18#include "ui/compositor/scoped_animation_duration_scale_mode.h"
19#include "ui/compositor/scoped_layer_animation_settings.h"
20#include "ui/compositor/test/context_factories_for_test.h"
21#include "ui/compositor/test/layer_animator_test_controller.h"
22#include "ui/compositor/test/test_compositor_host.h"
23#include "ui/compositor/test/test_layer_animation_delegate.h"
24#include "ui/compositor/test/test_layer_animation_observer.h"
25#include "ui/compositor/test/test_utils.h"
26#include "ui/gfx/frame_time.h"
27#include "ui/gfx/rect.h"
28#include "ui/gfx/transform.h"
29
30namespace ui {
31
32namespace {
33
34// Converts |color| to a string. Each component of the color is separated by a
35// space and the order if A R G B.
36std::string ColorToString(SkColor color) {
37  return base::StringPrintf("%d %d %d %d", SkColorGetA(color),
38                            SkColorGetR(color), SkColorGetG(color),
39                            SkColorGetB(color));
40}
41
42// Creates vector with two LayerAnimationSequences, based on |first| and
43// |second| layer animation elements.
44std::vector<LayerAnimationSequence*> CreateMultiSequence(
45    LayerAnimationElement* first,
46    LayerAnimationElement* second) {
47  LayerAnimationSequence* first_sequence = new LayerAnimationSequence();
48  first_sequence->AddElement(first);
49  LayerAnimationSequence* second_sequence = new LayerAnimationSequence();
50  second_sequence->AddElement(second);
51
52  std::vector<ui::LayerAnimationSequence*> animations;
53  animations.push_back(first_sequence);
54  animations.push_back(second_sequence);
55  return animations;
56}
57
58class TestImplicitAnimationObserver : public ImplicitAnimationObserver {
59 public:
60  explicit TestImplicitAnimationObserver(bool notify_when_animator_destructed)
61    : animations_completed_(false),
62      notify_when_animator_destructed_(notify_when_animator_destructed) {
63  }
64
65  bool animations_completed() const { return animations_completed_; }
66  void set_animations_completed(bool completed) {
67    animations_completed_ = completed;
68  }
69
70  bool WasAnimationAbortedForProperty(
71      LayerAnimationElement::AnimatableProperty property) const {
72    return ImplicitAnimationObserver::WasAnimationAbortedForProperty(property);
73  }
74
75  bool WasAnimationCompletedForProperty(
76      LayerAnimationElement::AnimatableProperty property) const {
77    return ImplicitAnimationObserver::WasAnimationCompletedForProperty(
78        property);
79  }
80
81 private:
82  // ImplicitAnimationObserver implementation
83  virtual void OnImplicitAnimationsCompleted() OVERRIDE {
84    animations_completed_ = true;
85  }
86
87  virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE {
88    return notify_when_animator_destructed_;
89  }
90
91  bool animations_completed_;
92  bool notify_when_animator_destructed_;
93
94  DISALLOW_COPY_AND_ASSIGN(TestImplicitAnimationObserver);
95};
96
97// When notified that an animation has ended, stops all other animations.
98class DeletingLayerAnimationObserver : public LayerAnimationObserver {
99 public:
100  DeletingLayerAnimationObserver(LayerAnimator* animator)
101    : animator_(animator) {
102  }
103
104  virtual void OnLayerAnimationEnded(
105      LayerAnimationSequence* sequence) OVERRIDE {
106    animator_->StopAnimating();
107  }
108
109  virtual void OnLayerAnimationAborted(
110      LayerAnimationSequence* sequence) OVERRIDE {
111    animator_->StopAnimating();
112  }
113
114  virtual void OnLayerAnimationScheduled(
115      LayerAnimationSequence* sequence) OVERRIDE {
116  }
117
118 private:
119  LayerAnimator* animator_;
120
121  DISALLOW_COPY_AND_ASSIGN(DeletingLayerAnimationObserver);
122};
123
124class LayerAnimatorDestructionObserver {
125 public:
126  LayerAnimatorDestructionObserver() : animator_deleted_(false) {}
127  virtual ~LayerAnimatorDestructionObserver() {}
128
129  void NotifyAnimatorDeleted() {
130    animator_deleted_ = true;
131  }
132
133  bool IsAnimatorDeleted() {
134    return animator_deleted_;
135  }
136
137 private:
138  bool animator_deleted_;
139
140  DISALLOW_COPY_AND_ASSIGN(LayerAnimatorDestructionObserver);
141};
142
143class TestLayerAnimator : public LayerAnimator {
144 public:
145  TestLayerAnimator() : LayerAnimator(base::TimeDelta::FromSeconds(0)),
146      destruction_observer_(NULL) {}
147
148  void SetDestructionObserver(
149      LayerAnimatorDestructionObserver* observer) {
150    destruction_observer_ = observer;
151  }
152
153 protected:
154  virtual ~TestLayerAnimator() {
155    if (destruction_observer_) {
156      destruction_observer_->NotifyAnimatorDeleted();
157    }
158  }
159
160  virtual void ProgressAnimation(LayerAnimationSequence* sequence,
161                                 base::TimeTicks now) OVERRIDE {
162    EXPECT_TRUE(HasAnimation(sequence));
163    LayerAnimator::ProgressAnimation(sequence, now);
164  }
165
166 private:
167  LayerAnimatorDestructionObserver* destruction_observer_;
168
169  DISALLOW_COPY_AND_ASSIGN(TestLayerAnimator);
170};
171
172// The test layer animation sequence updates a live instances count when it is
173// created and destroyed.
174class TestLayerAnimationSequence : public LayerAnimationSequence {
175 public:
176  TestLayerAnimationSequence(LayerAnimationElement* element,
177                             int* num_live_instances)
178      : LayerAnimationSequence(element),
179        num_live_instances_(num_live_instances) {
180    (*num_live_instances_)++;
181  }
182
183  virtual ~TestLayerAnimationSequence() {
184    (*num_live_instances_)--;
185  }
186
187 private:
188  int* num_live_instances_;
189
190  DISALLOW_COPY_AND_ASSIGN(TestLayerAnimationSequence);
191};
192
193} // namespace
194
195// Checks that setting a property on an implicit animator causes an animation to
196// happen.
197TEST(LayerAnimatorTest, ImplicitAnimation) {
198  scoped_refptr<LayerAnimator> animator(
199      LayerAnimator::CreateImplicitAnimator());
200  animator->set_disable_timer_for_test(true);
201  TestLayerAnimationDelegate delegate;
202  animator->SetDelegate(&delegate);
203  base::TimeTicks now = gfx::FrameTime::Now();
204  animator->SetBrightness(0.5);
205  EXPECT_TRUE(animator->is_animating());
206  animator->Step(now + base::TimeDelta::FromSeconds(1));
207  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
208}
209
210// Checks that if the animator is a default animator, that implicit animations
211// are not started.
212TEST(LayerAnimatorTest, NoImplicitAnimation) {
213  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
214  animator->set_disable_timer_for_test(true);
215  TestLayerAnimationDelegate delegate;
216  animator->SetDelegate(&delegate);
217  animator->SetBrightness(0.5);
218  EXPECT_FALSE(animator->is_animating());
219  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), 0.5);
220}
221
222// Checks that StopAnimatingProperty stops animation for that property, and also
223// skips the stopped animation to the end.
224TEST(LayerAnimatorTest, StopAnimatingProperty) {
225  scoped_refptr<LayerAnimator> animator(
226      LayerAnimator::CreateImplicitAnimator());
227  animator->set_disable_timer_for_test(true);
228  TestLayerAnimationDelegate delegate;
229  animator->SetDelegate(&delegate);
230  double target_opacity(0.5);
231  gfx::Rect target_bounds(0, 0, 50, 50);
232  animator->SetOpacity(target_opacity);
233  animator->SetBounds(target_bounds);
234  animator->StopAnimatingProperty(LayerAnimationElement::OPACITY);
235  EXPECT_TRUE(animator->is_animating());
236  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
237  animator->StopAnimatingProperty(LayerAnimationElement::BOUNDS);
238  EXPECT_FALSE(animator->is_animating());
239  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
240}
241
242// Checks that multiple running animation for separate properties can be stopped
243// simultaneously and that all animations are advanced to their target values.
244TEST(LayerAnimatorTest, StopAnimating) {
245  scoped_refptr<LayerAnimator> animator(
246      LayerAnimator::CreateImplicitAnimator());
247  animator->set_disable_timer_for_test(true);
248  TestLayerAnimationDelegate delegate;
249  animator->SetDelegate(&delegate);
250  double target_opacity(0.5);
251  gfx::Rect target_bounds(0, 0, 50, 50);
252  animator->SetOpacity(target_opacity);
253  animator->SetBounds(target_bounds);
254  EXPECT_TRUE(animator->is_animating());
255  animator->StopAnimating();
256  EXPECT_FALSE(animator->is_animating());
257  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), 0.5);
258  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
259}
260
261// Checks that multiple running animation for separate properties can be stopped
262// simultaneously and that all animations are advanced to their target values.
263TEST(LayerAnimatorTest, AbortAllAnimations) {
264  scoped_refptr<LayerAnimator> animator(
265      LayerAnimator::CreateImplicitAnimator());
266  animator->set_disable_timer_for_test(true);
267  TestLayerAnimationDelegate delegate;
268  double initial_opacity(1.0);
269  gfx::Rect initial_bounds(0, 0, 10, 10);
270  delegate.SetOpacityFromAnimation(initial_opacity);
271  delegate.SetBoundsFromAnimation(initial_bounds);
272  animator->SetDelegate(&delegate);
273  double target_opacity(0.5);
274  gfx::Rect target_bounds(0, 0, 50, 50);
275  animator->SetOpacity(target_opacity);
276  animator->SetBounds(target_bounds);
277  EXPECT_TRUE(animator->is_animating());
278  animator->AbortAllAnimations();
279  EXPECT_FALSE(animator->is_animating());
280  EXPECT_FLOAT_EQ(initial_opacity, delegate.GetOpacityForAnimation());
281  CheckApproximatelyEqual(initial_bounds, delegate.GetBoundsForAnimation());
282}
283
284// Schedule a non-threaded animation that can run immediately. This is the
285// trivial case and should result in the animation being started immediately.
286TEST(LayerAnimatorTest, ScheduleAnimationThatCanRunImmediately) {
287  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
288  animator->set_disable_timer_for_test(true);
289  TestLayerAnimationDelegate delegate;
290  animator->SetDelegate(&delegate);
291
292  double start_brightness(0.0);
293  double middle_brightness(0.5);
294  double target_brightness(1.0);
295
296  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
297
298  delegate.SetBrightnessFromAnimation(start_brightness);
299
300  animator->ScheduleAnimation(
301      new LayerAnimationSequence(
302          LayerAnimationElement::CreateBrightnessElement(target_brightness,
303                                                         delta)));
304
305  EXPECT_TRUE(animator->is_animating());
306  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
307
308  base::TimeTicks start_time = animator->last_step_time();
309
310  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
311
312  EXPECT_TRUE(animator->is_animating());
313  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
314
315  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
316
317  EXPECT_FALSE(animator->is_animating());
318  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
319}
320
321// Schedule a threaded animation that can run immediately.
322TEST(LayerAnimatorTest, ScheduleThreadedAnimationThatCanRunImmediately) {
323  double epsilon = 0.00001;
324  LayerAnimatorTestController test_controller(
325      LayerAnimator::CreateDefaultAnimator());
326  LayerAnimator* animator = test_controller.animator();
327  test_controller.animator()->set_disable_timer_for_test(true);
328  TestLayerAnimationDelegate delegate;
329  test_controller.animator()->SetDelegate(&delegate);
330
331  double start_opacity(0.0);
332  double target_opacity(1.0);
333
334  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
335
336  delegate.SetOpacityFromAnimation(start_opacity);
337
338  test_controller.animator()->ScheduleAnimation(
339      new LayerAnimationSequence(
340          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
341
342  EXPECT_TRUE(test_controller.animator()->is_animating());
343  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
344
345  base::TimeTicks start_time = test_controller.animator()->last_step_time();
346  base::TimeTicks effective_start = start_time + delta;
347
348  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
349      cc::AnimationEvent::Started,
350      0,
351      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
352          ->animation_group_id(),
353      cc::Animation::Opacity,
354      effective_start));
355
356  animator->Step(effective_start + delta / 2);
357
358  EXPECT_TRUE(test_controller.animator()->is_animating());
359  EXPECT_NEAR(
360      0.5,
361      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
362          last_progressed_fraction(),
363      epsilon);
364
365  animator->Step(effective_start + delta);
366
367  EXPECT_FALSE(test_controller.animator()->is_animating());
368  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
369}
370
371// Schedule two non-threaded animations on separate properties. Both animations
372// should start immediately and should progress in lock step.
373TEST(LayerAnimatorTest, ScheduleTwoAnimationsThatCanRunImmediately) {
374  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
375  animator->set_disable_timer_for_test(true);
376  TestLayerAnimationDelegate delegate;
377  animator->SetDelegate(&delegate);
378
379  double start_brightness(0.0);
380  double middle_brightness(0.5);
381  double target_brightness(1.0);
382
383  gfx::Rect start_bounds, target_bounds, middle_bounds;
384  start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
385  start_bounds.set_x(-90);
386  target_bounds.set_x(90);
387
388  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
389
390  delegate.SetBrightnessFromAnimation(start_brightness);
391  delegate.SetBoundsFromAnimation(start_bounds);
392
393  animator->ScheduleAnimation(
394      new LayerAnimationSequence(
395          LayerAnimationElement::CreateBrightnessElement(target_brightness,
396                                                         delta)));
397
398  animator->ScheduleAnimation(
399      new LayerAnimationSequence(
400          LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
401
402  EXPECT_TRUE(animator->is_animating());
403  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
404  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
405
406  base::TimeTicks start_time = animator->last_step_time();
407
408  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
409
410  EXPECT_TRUE(animator->is_animating());
411  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
412  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
413
414  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
415
416  EXPECT_FALSE(animator->is_animating());
417  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
418  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
419}
420
421// Schedule a threaded and a non-threaded animation on separate properties. Both
422// animations should progress in lock step.
423TEST(LayerAnimatorTest, ScheduleThreadedAndNonThreadedAnimations) {
424  double epsilon = 0.00001;
425  LayerAnimatorTestController test_controller(
426      LayerAnimator::CreateDefaultAnimator());
427  LayerAnimator* animator = test_controller.animator();
428  test_controller.animator()->set_disable_timer_for_test(true);
429  TestLayerAnimationDelegate delegate;
430  test_controller.animator()->SetDelegate(&delegate);
431
432  double start_opacity(0.0);
433  double target_opacity(1.0);
434
435  gfx::Rect start_bounds, target_bounds, middle_bounds;
436  start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
437  start_bounds.set_x(-90);
438  target_bounds.set_x(90);
439
440  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
441
442  delegate.SetOpacityFromAnimation(start_opacity);
443  delegate.SetBoundsFromAnimation(start_bounds);
444
445  std::vector<LayerAnimationSequence*> animations;
446  animations.push_back(
447      new LayerAnimationSequence(
448          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
449
450  animations.push_back(
451      new LayerAnimationSequence(
452          LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
453
454  test_controller.animator()->ScheduleTogether(animations);
455
456  EXPECT_TRUE(test_controller.animator()->is_animating());
457  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
458  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
459
460  base::TimeTicks start_time = test_controller.animator()->last_step_time();
461  base::TimeTicks effective_start = start_time + delta;
462
463  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
464      cc::AnimationEvent::Started,
465      0,
466      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
467          ->animation_group_id(),
468      cc::Animation::Opacity,
469      effective_start));
470
471  animator->Step(effective_start + delta / 2);
472
473  EXPECT_TRUE(test_controller.animator()->is_animating());
474  EXPECT_NEAR(
475      0.5,
476      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
477          last_progressed_fraction(),
478      epsilon);
479  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), middle_bounds);
480
481  animator->Step(effective_start + delta);
482
483  EXPECT_FALSE(test_controller.animator()->is_animating());
484  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
485  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
486}
487
488// Schedule two animations on the same property. In this case, the two
489// animations should run one after another.
490TEST(LayerAnimatorTest, ScheduleTwoAnimationsOnSameProperty) {
491  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
492  animator->set_disable_timer_for_test(true);
493  TestLayerAnimationDelegate delegate;
494  animator->SetDelegate(&delegate);
495
496  double start_brightness(0.0);
497  double middle_brightness(0.5);
498  double target_brightness(1.0);
499
500  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
501
502  delegate.SetBrightnessFromAnimation(start_brightness);
503
504  animator->ScheduleAnimation(
505      new LayerAnimationSequence(
506          LayerAnimationElement::CreateBrightnessElement(target_brightness,
507                                                         delta)));
508
509  animator->ScheduleAnimation(
510      new LayerAnimationSequence(
511          LayerAnimationElement::CreateBrightnessElement(start_brightness,
512                                                         delta)));
513
514  EXPECT_TRUE(animator->is_animating());
515  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
516
517  base::TimeTicks start_time = animator->last_step_time();
518
519  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
520
521  EXPECT_TRUE(animator->is_animating());
522  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
523
524  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
525
526  EXPECT_TRUE(animator->is_animating());
527  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
528
529  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
530
531  EXPECT_TRUE(animator->is_animating());
532  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
533
534  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
535
536  EXPECT_FALSE(animator->is_animating());
537  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
538}
539
540// Schedule [{g}, {g,b}, {b}] and ensure that {b} doesn't run right away. That
541// is, ensure that all animations targetting a particular property are run in
542// order.
543TEST(LayerAnimatorTest, ScheduleBlockedAnimation) {
544  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
545  animator->set_disable_timer_for_test(true);
546  TestLayerAnimationDelegate delegate;
547  animator->SetDelegate(&delegate);
548
549  double start_grayscale(0.0);
550  double middle_grayscale(0.5);
551  double target_grayscale(1.0);
552
553  gfx::Rect start_bounds, target_bounds, middle_bounds;
554  start_bounds = target_bounds = middle_bounds = gfx::Rect(0, 0, 50, 50);
555  start_bounds.set_x(-90);
556  target_bounds.set_x(90);
557
558  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
559
560  delegate.SetGrayscaleFromAnimation(start_grayscale);
561  delegate.SetBoundsFromAnimation(start_bounds);
562
563  animator->ScheduleAnimation(
564      new LayerAnimationSequence(
565          LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
566                                                        delta)));
567
568  scoped_ptr<LayerAnimationSequence> bounds_and_grayscale(
569      new LayerAnimationSequence(
570          LayerAnimationElement::CreateGrayscaleElement(start_grayscale,
571                                                        delta)));
572
573  bounds_and_grayscale->AddElement(
574      LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
575
576  animator->ScheduleAnimation(bounds_and_grayscale.release());
577
578  animator->ScheduleAnimation(
579      new LayerAnimationSequence(
580          LayerAnimationElement::CreateBoundsElement(start_bounds, delta)));
581
582  EXPECT_TRUE(animator->is_animating());
583  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
584  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
585
586  base::TimeTicks start_time = animator->last_step_time();
587
588  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
589
590  EXPECT_TRUE(animator->is_animating());
591  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
592  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
593
594  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
595
596  EXPECT_TRUE(animator->is_animating());
597  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
598  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
599
600  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
601
602  EXPECT_TRUE(animator->is_animating());
603  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
604  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
605
606  animator->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
607
608  EXPECT_TRUE(animator->is_animating());
609  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
610  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
611
612  animator->Step(start_time + base::TimeDelta::FromMilliseconds(4000));
613
614  EXPECT_FALSE(animator->is_animating());
615  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
616  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
617}
618
619// Schedule {g} and then schedule {g} and {b} together. In this case, since
620// ScheduleTogether is being used, the bounds animation should not start until
621// the second grayscale animation starts.
622TEST(LayerAnimatorTest, ScheduleTogether) {
623  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
624  animator->set_disable_timer_for_test(true);
625  TestLayerAnimationDelegate delegate;
626  animator->SetDelegate(&delegate);
627
628  double start_grayscale(0.0);
629  double target_grayscale(1.0);
630
631  gfx::Rect start_bounds, target_bounds, middle_bounds;
632  start_bounds = target_bounds = gfx::Rect(0, 0, 50, 50);
633  start_bounds.set_x(-90);
634  target_bounds.set_x(90);
635
636  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
637
638  delegate.SetGrayscaleFromAnimation(start_grayscale);
639  delegate.SetBoundsFromAnimation(start_bounds);
640
641  animator->ScheduleAnimation(
642      new LayerAnimationSequence(
643          LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
644                                                        delta)));
645
646  std::vector<LayerAnimationSequence*> sequences;
647  sequences.push_back(new LayerAnimationSequence(
648      LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta)));
649  sequences.push_back(new LayerAnimationSequence(
650      LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
651
652  animator->ScheduleTogether(sequences);
653
654  EXPECT_TRUE(animator->is_animating());
655  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
656  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
657
658  base::TimeTicks start_time = animator->last_step_time();
659
660  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
661
662  EXPECT_TRUE(animator->is_animating());
663  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
664  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), start_bounds);
665
666  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
667
668  EXPECT_FALSE(animator->is_animating());
669  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
670  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
671}
672
673// Start non-threaded animation (that can run immediately). This is the trivial
674// case (see the trival case for ScheduleAnimation).
675TEST(LayerAnimatorTest, StartAnimationThatCanRunImmediately) {
676  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
677  animator->set_disable_timer_for_test(true);
678  TestLayerAnimationDelegate delegate;
679  animator->SetDelegate(&delegate);
680
681  double start_brightness(0.0);
682  double middle_brightness(0.5);
683  double target_brightness(1.0);
684
685  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
686
687  delegate.SetBrightnessFromAnimation(start_brightness);
688
689  animator->StartAnimation(
690      new LayerAnimationSequence(
691          LayerAnimationElement::CreateBrightnessElement(target_brightness,
692                                                         delta)));
693
694  EXPECT_TRUE(animator->is_animating());
695  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
696
697  base::TimeTicks start_time = animator->last_step_time();
698
699  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
700
701  EXPECT_TRUE(animator->is_animating());
702  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
703
704  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
705
706  EXPECT_FALSE(animator->is_animating());
707  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
708}
709
710// Start threaded animation (that can run immediately).
711TEST(LayerAnimatorTest, StartThreadedAnimationThatCanRunImmediately) {
712  double epsilon = 0.00001;
713  LayerAnimatorTestController test_controller(
714      LayerAnimator::CreateDefaultAnimator());
715  LayerAnimator* animator = test_controller.animator();
716  test_controller.animator()->set_disable_timer_for_test(true);
717  TestLayerAnimationDelegate delegate;
718  test_controller.animator()->SetDelegate(&delegate);
719
720  double start_opacity(0.0);
721  double target_opacity(1.0);
722
723  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
724
725  delegate.SetOpacityFromAnimation(start_opacity);
726
727  test_controller.animator()->StartAnimation(
728      new LayerAnimationSequence(
729          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
730
731  EXPECT_TRUE(test_controller.animator()->is_animating());
732  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
733
734  base::TimeTicks start_time = test_controller.animator()->last_step_time();
735  base::TimeTicks effective_start = start_time + delta;
736
737  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
738      cc::AnimationEvent::Started,
739      0,
740      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
741          ->animation_group_id(),
742      cc::Animation::Opacity,
743      effective_start));
744
745  animator->Step(effective_start + delta / 2);
746
747  EXPECT_TRUE(test_controller.animator()->is_animating());
748  EXPECT_NEAR(
749      0.5,
750      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
751          last_progressed_fraction(),
752      epsilon);
753
754  animator->Step(effective_start + delta);
755  EXPECT_FALSE(test_controller.animator()->is_animating());
756  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
757}
758
759// Preempt by immediately setting new target.
760TEST(LayerAnimatorTest, PreemptBySettingNewTarget) {
761  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
762  animator->set_disable_timer_for_test(true);
763  TestLayerAnimationDelegate delegate;
764  animator->SetDelegate(&delegate);
765
766  double start_opacity(0.0);
767  double target_opacity(1.0);
768
769  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
770
771  delegate.SetOpacityFromAnimation(start_opacity);
772
773  animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
774
775  animator->StartAnimation(
776      new LayerAnimationSequence(
777          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
778
779  animator->StartAnimation(
780      new LayerAnimationSequence(
781          LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
782
783  EXPECT_FALSE(animator->is_animating());
784  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
785}
786
787// Preempt by animating to new target, with a non-threaded animation.
788TEST(LayerAnimatorTest, PreemptByImmediatelyAnimatingToNewTarget) {
789  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
790  animator->set_disable_timer_for_test(true);
791  TestLayerAnimationDelegate delegate;
792  animator->SetDelegate(&delegate);
793
794  double start_brightness(0.0);
795  double middle_brightness(0.5);
796  double target_brightness(1.0);
797
798  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
799
800  delegate.SetBrightnessFromAnimation(start_brightness);
801
802  animator->set_preemption_strategy(
803      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
804
805  animator->StartAnimation(
806      new LayerAnimationSequence(
807          LayerAnimationElement::CreateBrightnessElement(target_brightness,
808                                                         delta)));
809
810  base::TimeTicks start_time = animator->last_step_time();
811
812  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
813
814  animator->StartAnimation(
815      new LayerAnimationSequence(
816          LayerAnimationElement::CreateBrightnessElement(start_brightness,
817                                                         delta)));
818
819  EXPECT_TRUE(animator->is_animating());
820  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
821
822  animator->StartAnimation(
823      new LayerAnimationSequence(
824          LayerAnimationElement::CreateBrightnessElement(start_brightness,
825                                                         delta)));
826
827  EXPECT_TRUE(animator->is_animating());
828
829  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
830
831  EXPECT_TRUE(animator->is_animating());
832  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
833                  0.5 * (start_brightness + middle_brightness));
834
835  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
836
837  EXPECT_FALSE(animator->is_animating());
838  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
839}
840
841// Preempt by animating to new target, with a threaded animation.
842TEST(LayerAnimatorTest, PreemptThreadedByImmediatelyAnimatingToNewTarget) {
843  double epsilon = 0.00001;
844  LayerAnimatorTestController test_controller(
845      LayerAnimator::CreateDefaultAnimator());
846  LayerAnimator* animator = test_controller.animator();
847  test_controller.animator()->set_disable_timer_for_test(true);
848  TestLayerAnimationDelegate delegate;
849  test_controller.animator()->SetDelegate(&delegate);
850
851  double start_opacity(0.0);
852  double middle_opacity(0.5);
853  double target_opacity(1.0);
854
855  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
856
857  delegate.SetOpacityFromAnimation(start_opacity);
858
859  test_controller.animator()->set_preemption_strategy(
860      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
861
862  test_controller.animator()->StartAnimation(
863      new LayerAnimationSequence(
864          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
865
866  base::TimeTicks start_time = test_controller.animator()->last_step_time();
867  base::TimeTicks effective_start = start_time + delta;
868
869  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
870      cc::AnimationEvent::Started,
871      0,
872      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
873          ->animation_group_id(),
874      cc::Animation::Opacity,
875      effective_start));
876
877  animator->Step(effective_start + delta / 2);
878
879  test_controller.animator()->StartAnimation(
880      new LayerAnimationSequence(
881          LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
882
883  EXPECT_TRUE(test_controller.animator()->is_animating());
884  EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
885
886  test_controller.animator()->StartAnimation(
887      new LayerAnimationSequence(
888          LayerAnimationElement::CreateOpacityElement(start_opacity, delta)));
889
890  EXPECT_TRUE(test_controller.animator()->is_animating());
891
892  base::TimeTicks second_effective_start = effective_start + delta;
893
894  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
895      cc::AnimationEvent::Started,
896      0,
897      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
898          ->animation_group_id(),
899      cc::Animation::Opacity,
900      second_effective_start));
901
902  animator->Step(second_effective_start + delta / 2);
903
904  EXPECT_TRUE(test_controller.animator()->is_animating());
905  EXPECT_NEAR(
906      0.5,
907      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
908          last_progressed_fraction(),
909      epsilon);
910
911  animator->Step(second_effective_start + delta);
912
913  EXPECT_FALSE(test_controller.animator()->is_animating());
914  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
915}
916
917// Preempt by enqueuing the new animation.
918TEST(LayerAnimatorTest, PreemptEnqueueNewAnimation) {
919  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
920  animator->set_disable_timer_for_test(true);
921  TestLayerAnimationDelegate delegate;
922  animator->SetDelegate(&delegate);
923
924  double start_brightness(0.0);
925  double middle_brightness(0.5);
926  double target_brightness(1.0);
927
928  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
929
930  delegate.SetBrightnessFromAnimation(start_brightness);
931
932  animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
933
934  animator->StartAnimation(
935      new LayerAnimationSequence(
936          LayerAnimationElement::CreateBrightnessElement(target_brightness,
937                                                          delta)));
938
939  base::TimeTicks start_time = animator->last_step_time();
940
941  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
942
943  animator->StartAnimation(
944      new LayerAnimationSequence(
945          LayerAnimationElement::CreateBrightnessElement(start_brightness,
946                                                         delta)));
947
948  EXPECT_TRUE(animator->is_animating());
949  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
950
951  EXPECT_TRUE(animator->is_animating());
952
953  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
954
955  EXPECT_TRUE(animator->is_animating());
956  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
957
958  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
959
960  EXPECT_TRUE(animator->is_animating());
961  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
962
963  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
964
965  EXPECT_FALSE(animator->is_animating());
966  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
967}
968
969// Start an animation when there are sequences waiting in the queue. In this
970// case, all pending and running animations should be finished, and the new
971// animation started.
972TEST(LayerAnimatorTest, PreemptyByReplacingQueuedAnimations) {
973  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
974  animator->set_disable_timer_for_test(true);
975  TestLayerAnimationDelegate delegate;
976  animator->SetDelegate(&delegate);
977
978  double start_brightness(0.0);
979  double middle_brightness(0.5);
980  double target_brightness(1.0);
981
982  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
983
984  delegate.SetBrightnessFromAnimation(start_brightness);
985
986  animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
987
988  animator->StartAnimation(
989      new LayerAnimationSequence(
990          LayerAnimationElement::CreateBrightnessElement(target_brightness,
991                                                         delta)));
992
993  base::TimeTicks start_time = animator->last_step_time();
994
995  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
996
997  animator->StartAnimation(
998      new LayerAnimationSequence(
999          LayerAnimationElement::CreateBrightnessElement(middle_brightness,
1000                                                         delta)));
1001
1002  // Queue should now have two animations. Starting a third should replace the
1003  // second.
1004  animator->StartAnimation(
1005      new LayerAnimationSequence(
1006          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1007                                                         delta)));
1008
1009  EXPECT_TRUE(animator->is_animating());
1010  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1011
1012  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1013
1014  EXPECT_TRUE(animator->is_animating());
1015  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1016
1017  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1018
1019  EXPECT_TRUE(animator->is_animating());
1020  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1021
1022  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1023
1024  EXPECT_FALSE(animator->is_animating());
1025  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1026}
1027
1028TEST(LayerAnimatorTest, StartTogetherSetsLastStepTime) {
1029  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1030  animator->set_disable_timer_for_test(true);
1031  TestLayerAnimationDelegate delegate;
1032  animator->SetDelegate(&delegate);
1033
1034  double start_grayscale(0.0);
1035  double target_grayscale(1.0);
1036  double start_brightness(0.1);
1037  double target_brightness(0.9);
1038
1039  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1040
1041  delegate.SetGrayscaleFromAnimation(start_grayscale);
1042  delegate.SetBrightnessFromAnimation(start_brightness);
1043
1044  animator->set_preemption_strategy(
1045      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1046
1047  animator->set_last_step_time(base::TimeTicks());
1048
1049  animator->StartTogether(
1050      CreateMultiSequence(
1051          LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1052                                                        delta),
1053          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1054                                                         delta)
1055      ));
1056
1057  // If last step time was not set correctly, the resulting delta should be
1058  // miniscule (fractions of a millisecond). If set correctly, then the delta
1059  // should be enormous. Arbitrarily choosing 1 minute as the threshold,
1060  // though a much smaller value would probably have sufficed.
1061  delta = gfx::FrameTime::Now() - animator->last_step_time();
1062  EXPECT_GT(60.0, delta.InSecondsF());
1063}
1064
1065//-------------------------------------------------------
1066// Preempt by immediately setting new target.
1067TEST(LayerAnimatorTest, MultiPreemptBySettingNewTarget) {
1068  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1069  animator->set_disable_timer_for_test(true);
1070  TestLayerAnimationDelegate delegate;
1071  animator->SetDelegate(&delegate);
1072
1073  double start_opacity(0.0);
1074  double target_opacity(1.0);
1075  double start_brightness(0.1);
1076  double target_brightness(0.9);
1077
1078  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1079
1080  delegate.SetOpacityFromAnimation(start_opacity);
1081  delegate.SetBrightnessFromAnimation(start_brightness);
1082
1083  animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
1084
1085  animator->StartTogether(
1086      CreateMultiSequence(
1087          LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1088          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1089                                                         delta)
1090      ));
1091
1092  animator->StartTogether(
1093      CreateMultiSequence(
1094          LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1095          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1096                                                         delta)
1097      ));
1098
1099  EXPECT_FALSE(animator->is_animating());
1100  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1101  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1102}
1103
1104// Preempt by animating to new target.
1105TEST(LayerAnimatorTest, MultiPreemptByImmediatelyAnimatingToNewTarget) {
1106  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1107  animator->set_disable_timer_for_test(true);
1108  TestLayerAnimationDelegate delegate;
1109  animator->SetDelegate(&delegate);
1110
1111  double start_grayscale(0.0);
1112  double middle_grayscale(0.5);
1113  double target_grayscale(1.0);
1114
1115  double start_brightness(0.1);
1116  double middle_brightness(0.2);
1117  double target_brightness(0.3);
1118
1119  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1120
1121  delegate.SetGrayscaleFromAnimation(start_grayscale);
1122  delegate.SetBrightnessFromAnimation(start_brightness);
1123
1124  animator->set_preemption_strategy(
1125      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1126
1127  animator->StartTogether(
1128      CreateMultiSequence(
1129          LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1130                                                        delta),
1131          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1132                                                         delta)
1133      ));
1134
1135  base::TimeTicks start_time = animator->last_step_time();
1136
1137  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1138
1139  animator->StartTogether(
1140      CreateMultiSequence(
1141          LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1142          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1143                                                         delta)));
1144
1145  EXPECT_TRUE(animator->is_animating());
1146  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1147  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1148
1149  animator->StartTogether(
1150      CreateMultiSequence(
1151          LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1152          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1153                                                         delta)));
1154
1155  EXPECT_TRUE(animator->is_animating());
1156
1157  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1158
1159  EXPECT_TRUE(animator->is_animating());
1160  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(),
1161                  0.5 * (start_grayscale + middle_grayscale));
1162  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(),
1163                  0.5 * (start_brightness + middle_brightness));
1164
1165  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1166
1167  EXPECT_FALSE(animator->is_animating());
1168  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1169  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1170}
1171
1172// Preempt a threaded animation by animating to new target.
1173TEST(LayerAnimatorTest, MultiPreemptThreadedByImmediatelyAnimatingToNewTarget) {
1174  double epsilon = 0.00001;
1175  LayerAnimatorTestController test_controller(
1176      LayerAnimator::CreateDefaultAnimator());
1177  LayerAnimator* animator = test_controller.animator();
1178  test_controller.animator()->set_disable_timer_for_test(true);
1179  TestLayerAnimationDelegate delegate;
1180  test_controller.animator()->SetDelegate(&delegate);
1181
1182  double start_opacity(0.0);
1183  double middle_opacity(0.5);
1184  double target_opacity(1.0);
1185
1186  double start_brightness(0.1);
1187  double middle_brightness(0.2);
1188  double target_brightness(0.3);
1189
1190  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1191
1192  delegate.SetOpacityFromAnimation(start_opacity);
1193  delegate.SetBrightnessFromAnimation(start_brightness);
1194
1195  test_controller.animator()->set_preemption_strategy(
1196      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
1197
1198  test_controller.animator()->StartTogether(
1199      CreateMultiSequence(
1200          LayerAnimationElement::CreateOpacityElement(target_opacity, delta),
1201          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1202                                                         delta)
1203      ));
1204
1205  base::TimeTicks start_time = test_controller.animator()->last_step_time();
1206  base::TimeTicks effective_start = start_time + delta;
1207
1208  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1209      cc::AnimationEvent::Started,
1210      0,
1211      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1212          ->animation_group_id(),
1213      cc::Animation::Opacity,
1214      effective_start));
1215
1216  animator->Step(effective_start + delta / 2);
1217
1218  test_controller.animator()->StartTogether(
1219      CreateMultiSequence(
1220          LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1221          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1222                                                         delta)));
1223
1224  EXPECT_TRUE(test_controller.animator()->is_animating());
1225  EXPECT_NEAR(delegate.GetOpacityForAnimation(), middle_opacity, epsilon);
1226  EXPECT_NEAR(delegate.GetBrightnessForAnimation(), middle_brightness, epsilon);
1227
1228  test_controller.animator()->StartTogether(
1229      CreateMultiSequence(
1230          LayerAnimationElement::CreateOpacityElement(start_opacity, delta),
1231          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1232                                                         delta)));
1233
1234  EXPECT_TRUE(test_controller.animator()->is_animating());
1235
1236  base::TimeTicks second_effective_start = effective_start + delta;
1237
1238  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1239      cc::AnimationEvent::Started,
1240      0,
1241      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1242          ->animation_group_id(),
1243      cc::Animation::Opacity,
1244      second_effective_start));
1245
1246  animator->Step(second_effective_start + delta / 2);
1247
1248  EXPECT_TRUE(test_controller.animator()->is_animating());
1249  EXPECT_NEAR(
1250      0.5,
1251      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)->
1252          last_progressed_fraction(),
1253      epsilon);
1254  EXPECT_NEAR(delegate.GetBrightnessForAnimation(),
1255              0.5 * (start_brightness + middle_brightness),
1256              epsilon);
1257
1258  animator->Step(second_effective_start + delta);
1259
1260  EXPECT_FALSE(test_controller.animator()->is_animating());
1261  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1262  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1263}
1264
1265// Preempt by enqueuing the new animation.
1266TEST(LayerAnimatorTest, MultiPreemptEnqueueNewAnimation) {
1267  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1268  animator->set_disable_timer_for_test(true);
1269  TestLayerAnimationDelegate delegate;
1270  animator->SetDelegate(&delegate);
1271
1272  double start_grayscale(0.0);
1273  double middle_grayscale(0.5);
1274  double target_grayscale(1.0);
1275
1276  double start_brightness(0.1);
1277  double middle_brightness(0.2);
1278  double target_brightness(0.3);
1279
1280  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1281
1282  delegate.SetGrayscaleFromAnimation(start_grayscale);
1283  delegate.SetBrightnessFromAnimation(start_brightness);
1284
1285  animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
1286
1287  animator->StartTogether(
1288      CreateMultiSequence(
1289          LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1290                                                        delta),
1291          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1292                                                         delta)));
1293
1294  base::TimeTicks start_time = animator->last_step_time();
1295
1296  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1297
1298  animator->StartTogether(
1299      CreateMultiSequence(
1300          LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1301          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1302                                                         delta)));
1303
1304  EXPECT_TRUE(animator->is_animating());
1305  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1306  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1307
1308  EXPECT_TRUE(animator->is_animating());
1309
1310  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1311
1312  EXPECT_TRUE(animator->is_animating());
1313  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1314  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1315
1316  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1317
1318  EXPECT_TRUE(animator->is_animating());
1319  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1320  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1321
1322  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1323
1324  EXPECT_FALSE(animator->is_animating());
1325  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1326  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1327}
1328
1329// Start an animation when there are sequences waiting in the queue. In this
1330// case, all pending and running animations should be finished, and the new
1331// animation started.
1332TEST(LayerAnimatorTest, MultiPreemptByReplacingQueuedAnimations) {
1333  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1334  animator->set_disable_timer_for_test(true);
1335  TestLayerAnimationDelegate delegate;
1336  animator->SetDelegate(&delegate);
1337
1338  double start_grayscale(0.0);
1339  double middle_grayscale(0.5);
1340  double target_grayscale(1.0);
1341
1342  double start_brightness(0.1);
1343  double middle_brightness(0.2);
1344  double target_brightness(0.3);
1345
1346  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1347
1348  delegate.SetGrayscaleFromAnimation(start_grayscale);
1349  delegate.SetBrightnessFromAnimation(start_brightness);
1350
1351  animator->set_preemption_strategy(LayerAnimator::REPLACE_QUEUED_ANIMATIONS);
1352
1353  animator->StartTogether(
1354      CreateMultiSequence(
1355          LayerAnimationElement::CreateGrayscaleElement(target_grayscale,
1356                                                        delta),
1357          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1358                                                         delta)));
1359
1360  base::TimeTicks start_time = animator->last_step_time();
1361
1362  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1363
1364  animator->StartTogether(
1365      CreateMultiSequence(
1366          LayerAnimationElement::CreateGrayscaleElement(middle_grayscale,
1367                                                        delta),
1368          LayerAnimationElement::CreateBrightnessElement(middle_brightness,
1369                                                         delta)));
1370
1371  // Queue should now have two animations. Starting a third should replace the
1372  // second.
1373  animator->StartTogether(
1374      CreateMultiSequence(
1375          LayerAnimationElement::CreateGrayscaleElement(start_grayscale, delta),
1376          LayerAnimationElement::CreateBrightnessElement(start_brightness,
1377                                                         delta)));
1378
1379  EXPECT_TRUE(animator->is_animating());
1380  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1381  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1382
1383  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1384
1385  EXPECT_TRUE(animator->is_animating());
1386  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), target_grayscale);
1387  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1388
1389  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
1390
1391  EXPECT_TRUE(animator->is_animating());
1392  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), middle_grayscale);
1393  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), middle_brightness);
1394
1395  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1396
1397  EXPECT_FALSE(animator->is_animating());
1398  EXPECT_FLOAT_EQ(delegate.GetGrayscaleForAnimation(), start_grayscale);
1399  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1400}
1401//-------------------------------------------------------
1402// Test that non-threaded cyclic sequences continue to animate.
1403TEST(LayerAnimatorTest, CyclicSequences) {
1404  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1405  animator->set_disable_timer_for_test(true);
1406  TestLayerAnimationDelegate delegate;
1407  animator->SetDelegate(&delegate);
1408
1409  double start_brightness(0.0);
1410  double target_brightness(1.0);
1411
1412  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1413
1414  delegate.SetBrightnessFromAnimation(start_brightness);
1415
1416  scoped_ptr<LayerAnimationSequence> sequence(
1417      new LayerAnimationSequence(
1418          LayerAnimationElement::CreateBrightnessElement(target_brightness,
1419                                                         delta)));
1420
1421  sequence->AddElement(
1422      LayerAnimationElement::CreateBrightnessElement(start_brightness, delta));
1423
1424  sequence->set_is_cyclic(true);
1425
1426  animator->StartAnimation(sequence.release());
1427
1428  base::TimeTicks start_time = animator->last_step_time();
1429
1430  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1431
1432  EXPECT_TRUE(animator->is_animating());
1433  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1434
1435  animator->Step(start_time + base::TimeDelta::FromMilliseconds(2000));
1436
1437  EXPECT_TRUE(animator->is_animating());
1438  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1439
1440  animator->Step(start_time + base::TimeDelta::FromMilliseconds(3000));
1441
1442  EXPECT_TRUE(animator->is_animating());
1443  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1444
1445  // Skip ahead by a lot.
1446  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000000000));
1447
1448  EXPECT_TRUE(animator->is_animating());
1449  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), start_brightness);
1450
1451  // Skip ahead by a lot.
1452  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000001000));
1453
1454  EXPECT_TRUE(animator->is_animating());
1455  EXPECT_FLOAT_EQ(delegate.GetBrightnessForAnimation(), target_brightness);
1456
1457  animator->StopAnimatingProperty(LayerAnimationElement::BRIGHTNESS);
1458
1459  EXPECT_FALSE(animator->is_animating());
1460}
1461
1462// Test that threaded cyclic sequences continue to animate.
1463TEST(LayerAnimatorTest, ThreadedCyclicSequences) {
1464  LayerAnimatorTestController test_controller(
1465      LayerAnimator::CreateDefaultAnimator());
1466  LayerAnimator* animator = test_controller.animator();
1467  test_controller.animator()->set_disable_timer_for_test(true);
1468  TestLayerAnimationDelegate delegate;
1469  test_controller.animator()->SetDelegate(&delegate);
1470
1471  double start_opacity(0.0);
1472  double target_opacity(1.0);
1473
1474  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1475
1476  delegate.SetOpacityFromAnimation(start_opacity);
1477
1478  scoped_ptr<LayerAnimationSequence> sequence(
1479      new LayerAnimationSequence(
1480          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
1481
1482  sequence->AddElement(
1483      LayerAnimationElement::CreateOpacityElement(start_opacity, delta));
1484
1485  sequence->set_is_cyclic(true);
1486
1487  test_controller.animator()->StartAnimation(sequence.release());
1488
1489  base::TimeTicks start_time = test_controller.animator()->last_step_time();
1490  base::TimeTicks effective_start = start_time + delta;
1491
1492  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1493      cc::AnimationEvent::Started,
1494      0,
1495      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1496          ->animation_group_id(),
1497      cc::Animation::Opacity,
1498      effective_start));
1499
1500  animator->Step(effective_start + delta);
1501  EXPECT_TRUE(test_controller.animator()->is_animating());
1502  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1503
1504  base::TimeTicks second_effective_start = effective_start + 2 * delta;
1505  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1506      cc::AnimationEvent::Started,
1507      0,
1508      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1509          ->animation_group_id(),
1510      cc::Animation::Opacity,
1511      second_effective_start));
1512
1513  animator->Step(second_effective_start + delta);
1514
1515  EXPECT_TRUE(test_controller.animator()->is_animating());
1516  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1517
1518  base::TimeTicks third_effective_start = second_effective_start + 2 * delta;
1519  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1520      cc::AnimationEvent::Started,
1521      0,
1522      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1523          ->animation_group_id(),
1524      cc::Animation::Opacity,
1525      third_effective_start));
1526
1527  animator->Step(third_effective_start + delta);
1528  EXPECT_TRUE(test_controller.animator()->is_animating());
1529  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1530
1531  base::TimeTicks fourth_effective_start = third_effective_start + 2 * delta;
1532  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1533      cc::AnimationEvent::Started,
1534      0,
1535      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1536          ->animation_group_id(),
1537      cc::Animation::Opacity,
1538      fourth_effective_start));
1539
1540  // Skip ahead by a lot.
1541  animator->Step(fourth_effective_start + 1000 * delta);
1542
1543  EXPECT_TRUE(test_controller.animator()->is_animating());
1544  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), target_opacity);
1545
1546  base::TimeTicks fifth_effective_start = fourth_effective_start + 1001 * delta;
1547  test_controller.animator()->OnThreadedAnimationStarted(cc::AnimationEvent(
1548      cc::AnimationEvent::Started,
1549      0,
1550      test_controller.GetRunningSequence(LayerAnimationElement::OPACITY)
1551          ->animation_group_id(),
1552      cc::Animation::Opacity,
1553      fifth_effective_start));
1554
1555  // Skip ahead by a lot.
1556  animator->Step(fifth_effective_start + 999 * delta);
1557
1558  EXPECT_TRUE(test_controller.animator()->is_animating());
1559  EXPECT_FLOAT_EQ(delegate.GetOpacityForAnimation(), start_opacity);
1560
1561  test_controller.animator()->StopAnimatingProperty(
1562      LayerAnimationElement::OPACITY);
1563
1564  EXPECT_FALSE(test_controller.animator()->is_animating());
1565}
1566
1567TEST(LayerAnimatorTest, AddObserverExplicit) {
1568  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1569  animator->set_disable_timer_for_test(true);
1570  TestLayerAnimationObserver observer;
1571  TestLayerAnimationDelegate delegate;
1572  animator->SetDelegate(&delegate);
1573  animator->AddObserver(&observer);
1574  observer.set_requires_notification_when_animator_destroyed(true);
1575
1576  EXPECT_TRUE(!observer.last_ended_sequence());
1577
1578  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1579
1580  delegate.SetBrightnessFromAnimation(0.0f);
1581
1582  LayerAnimationSequence* sequence = new LayerAnimationSequence(
1583      LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1584
1585  animator->StartAnimation(sequence);
1586
1587  EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1588
1589  base::TimeTicks start_time = animator->last_step_time();
1590
1591  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1592
1593  EXPECT_EQ(observer.last_ended_sequence(), sequence);
1594
1595  // |sequence| has been destroyed. Recreate it to test abort.
1596  sequence = new LayerAnimationSequence(
1597      LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1598
1599  animator->StartAnimation(sequence);
1600
1601  animator = NULL;
1602
1603  EXPECT_EQ(observer.last_aborted_sequence(), sequence);
1604}
1605
1606// Tests that an observer added to a scoped settings object is still notified
1607// when the object goes out of scope.
1608TEST(LayerAnimatorTest, ImplicitAnimationObservers) {
1609  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1610  animator->set_disable_timer_for_test(true);
1611  TestImplicitAnimationObserver observer(false);
1612  TestLayerAnimationDelegate delegate;
1613  animator->SetDelegate(&delegate);
1614
1615  EXPECT_FALSE(observer.animations_completed());
1616  animator->SetBrightness(1.0f);
1617
1618  {
1619    ScopedLayerAnimationSettings settings(animator.get());
1620    settings.AddObserver(&observer);
1621    animator->SetBrightness(0.0f);
1622  }
1623
1624  EXPECT_FALSE(observer.animations_completed());
1625  base::TimeTicks start_time = animator->last_step_time();
1626  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1627  EXPECT_TRUE(observer.animations_completed());
1628  EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1629      LayerAnimationElement::BRIGHTNESS));
1630  EXPECT_FLOAT_EQ(0.0f, delegate.GetBrightnessForAnimation());
1631}
1632
1633// Tests that an observer added to a scoped settings object is still notified
1634// when the object goes out of scope due to the animation being interrupted.
1635TEST(LayerAnimatorTest, InterruptedImplicitAnimationObservers) {
1636  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1637  animator->set_disable_timer_for_test(true);
1638  TestImplicitAnimationObserver observer(false);
1639  TestLayerAnimationDelegate delegate;
1640  animator->SetDelegate(&delegate);
1641
1642  EXPECT_FALSE(observer.animations_completed());
1643  animator->SetBrightness(1.0f);
1644
1645  {
1646    ScopedLayerAnimationSettings settings(animator.get());
1647    settings.AddObserver(&observer);
1648    animator->SetBrightness(0.0f);
1649  }
1650
1651  EXPECT_FALSE(observer.animations_completed());
1652  // This should interrupt the implicit animation causing the observer to be
1653  // notified immediately.
1654  animator->SetBrightness(1.0f);
1655  EXPECT_TRUE(observer.animations_completed());
1656  EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1657      LayerAnimationElement::BRIGHTNESS));
1658  EXPECT_FLOAT_EQ(1.0f, delegate.GetBrightnessForAnimation());
1659}
1660
1661// Tests that LayerAnimator is not deleted after the animation completes as long
1662// as there is a live ScopedLayerAnimationSettings object wrapped around it.
1663TEST(LayerAnimatorTest, AnimatorKeptAliveBySettings) {
1664  // Note we are using a raw pointer unlike in other tests.
1665  TestLayerAnimator* animator = new TestLayerAnimator();
1666  LayerAnimatorDestructionObserver destruction_observer;
1667  animator->SetDestructionObserver(&destruction_observer);
1668  animator->set_disable_timer_for_test(true);
1669  TestLayerAnimationDelegate delegate;
1670  animator->SetDelegate(&delegate);
1671  {
1672    // ScopedLayerAnimationSettings should keep the Animator alive as long as
1673    // it is alive, even beyond the end of the animation.
1674    ScopedLayerAnimationSettings settings(animator);
1675    base::TimeTicks now = gfx::FrameTime::Now();
1676    animator->SetBrightness(0.5);
1677    animator->Step(now + base::TimeDelta::FromSeconds(1));
1678    EXPECT_FALSE(destruction_observer.IsAnimatorDeleted());
1679  }
1680  // ScopedLayerAnimationSettings was destroyed, so Animator should be deleted.
1681  EXPECT_TRUE(destruction_observer.IsAnimatorDeleted());
1682}
1683
1684// Tests that an observer added to a scoped settings object is not notified
1685// when the animator is destroyed unless explicitly requested.
1686TEST(LayerAnimatorTest, ImplicitObserversAtAnimatorDestruction) {
1687  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1688  animator->set_disable_timer_for_test(true);
1689  TestImplicitAnimationObserver observer_notify(true);
1690  TestImplicitAnimationObserver observer_do_not_notify(false);
1691  TestLayerAnimationDelegate delegate;
1692  animator->SetDelegate(&delegate);
1693
1694  EXPECT_FALSE(observer_notify.animations_completed());
1695  EXPECT_FALSE(observer_do_not_notify.animations_completed());
1696
1697  animator->SetBrightness(1.0f);
1698
1699  {
1700    ScopedLayerAnimationSettings settings(animator.get());
1701    settings.AddObserver(&observer_notify);
1702    settings.AddObserver(&observer_do_not_notify);
1703    animator->SetBrightness(0.0f);
1704  }
1705
1706  EXPECT_FALSE(observer_notify.animations_completed());
1707  EXPECT_FALSE(observer_do_not_notify.animations_completed());
1708  animator = NULL;
1709  EXPECT_TRUE(observer_notify.animations_completed());
1710  EXPECT_TRUE(observer_notify.WasAnimationAbortedForProperty(
1711      LayerAnimationElement::BRIGHTNESS));
1712  EXPECT_FALSE(observer_do_not_notify.animations_completed());
1713}
1714
1715TEST(LayerAnimatorTest, AbortedAnimationStatusInImplicitObservers) {
1716  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1717  animator->set_disable_timer_for_test(true);
1718  TestImplicitAnimationObserver observer(false);
1719  TestLayerAnimationDelegate delegate;
1720  animator->SetDelegate(&delegate);
1721
1722  EXPECT_FALSE(observer.animations_completed());
1723  animator->SetBrightness(1.0f);
1724
1725  {
1726    ScopedLayerAnimationSettings settings(animator.get());
1727    settings.AddObserver(&observer);
1728    animator->SetBrightness(0.0f);
1729  }
1730  EXPECT_FALSE(observer.animations_completed());
1731
1732  animator->AbortAllAnimations();
1733  EXPECT_TRUE(observer.animations_completed());
1734  EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1735      LayerAnimationElement::BRIGHTNESS));
1736  EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
1737      LayerAnimationElement::OPACITY));
1738
1739  observer.set_animations_completed(false);
1740  {
1741    ScopedLayerAnimationSettings settings(animator.get());
1742    settings.AddObserver(&observer);
1743    animator->SetOpacity(0.0f);
1744  }
1745  EXPECT_FALSE(observer.animations_completed());
1746
1747  animator->AbortAllAnimations();
1748  EXPECT_TRUE(observer.animations_completed());
1749  EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1750      LayerAnimationElement::BRIGHTNESS));
1751  EXPECT_TRUE(observer.WasAnimationAbortedForProperty(
1752      LayerAnimationElement::OPACITY));
1753}
1754
1755TEST(LayerAnimatorTest, RemoveObserverShouldRemoveFromSequences) {
1756  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1757  animator->set_disable_timer_for_test(true);
1758  TestLayerAnimationObserver observer;
1759  TestLayerAnimationObserver removed_observer;
1760  TestLayerAnimationDelegate delegate;
1761  animator->SetDelegate(&delegate);
1762
1763  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1764
1765  LayerAnimationSequence* sequence = new LayerAnimationSequence(
1766      LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1767
1768  sequence->AddObserver(&observer);
1769  sequence->AddObserver(&removed_observer);
1770
1771  animator->StartAnimation(sequence);
1772
1773  EXPECT_EQ(observer.last_scheduled_sequence(), sequence);
1774  EXPECT_TRUE(!observer.last_ended_sequence());
1775  EXPECT_EQ(removed_observer.last_scheduled_sequence(), sequence);
1776  EXPECT_TRUE(!removed_observer.last_ended_sequence());
1777
1778  // This should stop the observer from observing sequence.
1779  animator->RemoveObserver(&removed_observer);
1780
1781  base::TimeTicks start_time = animator->last_step_time();
1782
1783  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1784
1785  EXPECT_EQ(observer.last_ended_sequence(), sequence);
1786  EXPECT_TRUE(!removed_observer.last_ended_sequence());
1787}
1788
1789TEST(LayerAnimatorTest, ObserverReleasedBeforeAnimationSequenceEnds) {
1790  TestLayerAnimationDelegate delegate;
1791  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1792  animator->set_disable_timer_for_test(true);
1793
1794  scoped_ptr<TestLayerAnimationObserver> observer(
1795      new TestLayerAnimationObserver);
1796  animator->SetDelegate(&delegate);
1797  animator->AddObserver(observer.get());
1798
1799  delegate.SetOpacityFromAnimation(0.0f);
1800
1801  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1802  LayerAnimationSequence* sequence = new LayerAnimationSequence(
1803      LayerAnimationElement::CreateOpacityElement(1.0f, delta));
1804
1805  animator->StartAnimation(sequence);
1806
1807  // |observer| should be attached to |sequence|.
1808  EXPECT_TRUE(sequence->observers_.might_have_observers());
1809
1810  // Now, release |observer|
1811  observer.reset();
1812
1813  // And |sequence| should no longer be attached to |observer|.
1814  EXPECT_FALSE(sequence->observers_.might_have_observers());
1815}
1816
1817TEST(LayerAnimatorTest, ObserverAttachedAfterAnimationStarted) {
1818  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1819  animator->set_disable_timer_for_test(true);
1820
1821  TestImplicitAnimationObserver observer(false);
1822  TestLayerAnimationDelegate delegate;
1823  animator->SetDelegate(&delegate);
1824
1825  delegate.SetBrightnessFromAnimation(0.0f);
1826
1827  {
1828    ScopedLayerAnimationSettings setter(animator.get());
1829
1830    base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1831    LayerAnimationSequence* sequence = new LayerAnimationSequence(
1832        LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1833
1834    animator->StartAnimation(sequence);
1835    base::TimeTicks start_time = animator->last_step_time();
1836    animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1837
1838    setter.AddObserver(&observer);
1839
1840    // Start observing an in-flight animation.
1841    sequence->AddObserver(&observer);
1842
1843    animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
1844  }
1845
1846  EXPECT_TRUE(observer.animations_completed());
1847  EXPECT_TRUE(observer.WasAnimationCompletedForProperty(
1848                  LayerAnimationElement::BRIGHTNESS));
1849}
1850
1851TEST(LayerAnimatorTest, ObserverDetachedBeforeAnimationFinished) {
1852  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
1853  animator->set_disable_timer_for_test(true);
1854
1855  TestImplicitAnimationObserver observer(false);
1856  TestLayerAnimationDelegate delegate;
1857  animator->SetDelegate(&delegate);
1858
1859  delegate.SetBrightnessFromAnimation(0.0f);
1860  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
1861  LayerAnimationSequence* sequence = new LayerAnimationSequence(
1862      LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
1863
1864  {
1865    ScopedLayerAnimationSettings setter(animator.get());
1866    setter.AddObserver(&observer);
1867
1868    animator->StartAnimation(sequence);
1869    base::TimeTicks start_time = animator->last_step_time();
1870    animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
1871  }
1872
1873  EXPECT_FALSE(observer.animations_completed());
1874
1875  // Stop observing an in-flight animation.
1876  sequence->RemoveObserver(&observer);
1877
1878  EXPECT_TRUE(observer.animations_completed());
1879
1880  // The animation didn't complete, and neither was it aborted.
1881  EXPECT_FALSE(observer.WasAnimationCompletedForProperty(
1882                  LayerAnimationElement::BRIGHTNESS));
1883  EXPECT_FALSE(observer.WasAnimationAbortedForProperty(
1884                  LayerAnimationElement::BRIGHTNESS));
1885}
1886
1887// This checks that if an animation is deleted due to a callback, that the
1888// animator does not try to use the deleted animation. For example, if we have
1889// two running animations, and the first finishes and the resulting callback
1890// causes the second to be deleted, we should not attempt to animate the second
1891// animation.
1892TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnEnd) {
1893  ScopedAnimationDurationScaleMode normal_duration_mode(
1894      ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1895  scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1896  animator->set_disable_timer_for_test(true);
1897  TestLayerAnimationDelegate delegate;
1898  animator->SetDelegate(&delegate);
1899
1900  double start_brightness(0.0);
1901  double target_brightness(1.0);
1902
1903  gfx::Rect start_bounds(0, 0, 50, 50);
1904  gfx::Rect target_bounds(5, 5, 5, 5);
1905
1906  delegate.SetBrightnessFromAnimation(start_brightness);
1907  delegate.SetBoundsFromAnimation(start_bounds);
1908
1909  base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
1910  base::TimeDelta halfway_delta = base::TimeDelta::FromSeconds(2);
1911  base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(3);
1912
1913  scoped_ptr<DeletingLayerAnimationObserver> observer(
1914      new DeletingLayerAnimationObserver(animator.get()));
1915
1916  animator->AddObserver(observer.get());
1917
1918  animator->StartAnimation(
1919      new LayerAnimationSequence(
1920          LayerAnimationElement::CreateBrightnessElement(
1921              target_brightness, brightness_delta)));
1922
1923  animator->StartAnimation(new LayerAnimationSequence(
1924      LayerAnimationElement::CreateBoundsElement(
1925          target_bounds, bounds_delta)));
1926  ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1927
1928  base::TimeTicks start_time = animator->last_step_time();
1929  animator->Step(start_time + halfway_delta);
1930
1931  // Completing the brightness animation should have stopped the bounds
1932  // animation.
1933  ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1934
1935  animator->RemoveObserver(observer.get());
1936}
1937
1938// Ensure that stopping animation in a bounds change does not crash and that
1939// animation gets stopped correctly.
1940// This scenario is possible when animation is restarted from inside a
1941// callback triggered by the animation progress.
1942TEST(LayerAnimatorTest, CallbackDeletesAnimationInProgress) {
1943
1944  class TestLayerAnimationDeletingDelegate : public TestLayerAnimationDelegate {
1945   public:
1946    TestLayerAnimationDeletingDelegate(LayerAnimator* animator, int max_width)
1947      : animator_(animator),
1948        max_width_(max_width) {
1949    }
1950
1951    virtual void SetBoundsFromAnimation(const gfx::Rect& bounds) OVERRIDE {
1952      TestLayerAnimationDelegate::SetBoundsFromAnimation(bounds);
1953      if (bounds.width() > max_width_)
1954        animator_->StopAnimating();
1955    }
1956   private:
1957    LayerAnimator* animator_;
1958    int max_width_;
1959    // Allow copy and assign.
1960  };
1961
1962  ScopedAnimationDurationScaleMode normal_duration_mode(
1963      ScopedAnimationDurationScaleMode::NORMAL_DURATION);
1964  scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
1965  animator->set_disable_timer_for_test(true);
1966  TestLayerAnimationDeletingDelegate delegate(animator.get(), 30);
1967  animator->SetDelegate(&delegate);
1968
1969  gfx::Rect start_bounds(0, 0, 0, 0);
1970  gfx::Rect target_bounds(5, 5, 50, 50);
1971
1972  delegate.SetBoundsFromAnimation(start_bounds);
1973
1974  base::TimeDelta bounds_delta1 = base::TimeDelta::FromMilliseconds(333);
1975  base::TimeDelta bounds_delta2 = base::TimeDelta::FromMilliseconds(666);
1976  base::TimeDelta bounds_delta = base::TimeDelta::FromMilliseconds(1000);
1977  base::TimeDelta final_delta = base::TimeDelta::FromMilliseconds(1500);
1978
1979  animator->StartAnimation(new LayerAnimationSequence(
1980      LayerAnimationElement::CreateBoundsElement(
1981          target_bounds, bounds_delta)));
1982  ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1983
1984  base::TimeTicks start_time = animator->last_step_time();
1985  ASSERT_NO_FATAL_FAILURE(animator->Step(start_time + bounds_delta1));
1986  ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1987
1988  // The next step should change the animated bounds past the threshold and
1989  // cause the animaton to stop.
1990  ASSERT_NO_FATAL_FAILURE(animator->Step(start_time + bounds_delta2));
1991  ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1992  ASSERT_NO_FATAL_FAILURE(animator->Step(start_time + final_delta));
1993
1994  // Completing the animation should have stopped the bounds
1995  // animation.
1996  ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
1997}
1998
1999// Similar to the ObserverDeletesAnimationsOnEnd test above except that it
2000// tests the behavior when the OnLayerAnimationAborted() callback causes
2001// all of the animator's other animations to be deleted.
2002TEST(LayerAnimatorTest, ObserverDeletesAnimationsOnAbort) {
2003  ScopedAnimationDurationScaleMode test_duration_mode(
2004      ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
2005  scoped_refptr<LayerAnimator> animator(new TestLayerAnimator());
2006  animator->set_disable_timer_for_test(true);
2007  TestLayerAnimationDelegate delegate;
2008  animator->SetDelegate(&delegate);
2009
2010  double start_brightness(0.0);
2011  double target_brightness(1.0);
2012  gfx::Rect start_bounds(0, 0, 50, 50);
2013  gfx::Rect target_bounds(5, 5, 5, 5);
2014  base::TimeDelta brightness_delta = base::TimeDelta::FromSeconds(1);
2015  base::TimeDelta bounds_delta = base::TimeDelta::FromSeconds(2);
2016
2017  delegate.SetBrightnessFromAnimation(start_brightness);
2018  delegate.SetBoundsFromAnimation(start_bounds);
2019
2020  scoped_ptr<DeletingLayerAnimationObserver> observer(
2021      new DeletingLayerAnimationObserver(animator.get()));
2022  animator->AddObserver(observer.get());
2023
2024  animator->StartAnimation(
2025      new LayerAnimationSequence(
2026          LayerAnimationElement::CreateBrightnessElement(
2027              target_brightness, brightness_delta)));
2028  animator->StartAnimation(new LayerAnimationSequence(
2029      LayerAnimationElement::CreateBoundsElement(
2030          target_bounds, bounds_delta)));
2031  ASSERT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2032
2033  animator->set_preemption_strategy(
2034      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2035  animator->StartAnimation(
2036      new LayerAnimationSequence(
2037          LayerAnimationElement::CreateBrightnessElement(
2038              target_brightness, brightness_delta)));
2039
2040  // Starting the second brightness animation should have aborted the initial
2041  // brightness animation. |observer| should have stopped the bounds animation
2042  // as a result.
2043  ASSERT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2044
2045  animator->RemoveObserver(observer.get());
2046}
2047
2048// Check that setting a property during an animation with a default animator
2049// cancels the original animation.
2050TEST(LayerAnimatorTest, SettingPropertyDuringAnAnimation) {
2051  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2052  animator->set_disable_timer_for_test(true);
2053  TestLayerAnimationDelegate delegate;
2054  animator->SetDelegate(&delegate);
2055
2056  double start_opacity(0.0);
2057  double target_opacity(1.0);
2058
2059  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2060
2061  delegate.SetOpacityFromAnimation(start_opacity);
2062
2063  scoped_ptr<LayerAnimationSequence> sequence(
2064      new LayerAnimationSequence(
2065          LayerAnimationElement::CreateOpacityElement(target_opacity, delta)));
2066
2067  animator->StartAnimation(sequence.release());
2068
2069  animator->SetOpacity(0.5);
2070
2071  EXPECT_FALSE(animator->is_animating());
2072  EXPECT_EQ(0.5, animator->GetTargetOpacity());
2073}
2074
2075// Tests that the preemption mode IMMEDIATELY_SET_NEW_TARGET, doesn't cause the
2076// second sequence to be leaked.
2077TEST(LayerAnimatorTest, ImmediatelySettingNewTargetDoesNotLeak) {
2078  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2079  animator->set_preemption_strategy(LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
2080  animator->set_disable_timer_for_test(true);
2081  TestLayerAnimationDelegate delegate;
2082  animator->SetDelegate(&delegate);
2083
2084  gfx::Rect start_bounds(0, 0, 50, 50);
2085  gfx::Rect middle_bounds(10, 10, 100, 100);
2086  gfx::Rect target_bounds(5, 5, 5, 5);
2087
2088  delegate.SetBoundsFromAnimation(start_bounds);
2089
2090  {
2091    // start an implicit bounds animation.
2092    ScopedLayerAnimationSettings settings(animator.get());
2093    animator->SetBounds(middle_bounds);
2094  }
2095
2096  EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2097
2098  int num_live_instances = 0;
2099  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2100  scoped_ptr<TestLayerAnimationSequence> sequence(
2101      new TestLayerAnimationSequence(
2102          LayerAnimationElement::CreateBoundsElement(target_bounds, delta),
2103          &num_live_instances));
2104
2105  EXPECT_EQ(1, num_live_instances);
2106
2107  // This should interrupt the running sequence causing us to immediately set
2108  // the target value. The sequence should alse be destructed.
2109  animator->StartAnimation(sequence.release());
2110
2111  EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2112  EXPECT_EQ(0, num_live_instances);
2113  CheckApproximatelyEqual(delegate.GetBoundsForAnimation(), target_bounds);
2114}
2115
2116// Verifies GetTargetOpacity() works when multiple sequences are scheduled.
2117TEST(LayerAnimatorTest, GetTargetOpacity) {
2118  TestLayerAnimationDelegate delegate;
2119  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2120  animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2121  animator->set_disable_timer_for_test(true);
2122  animator->SetDelegate(&delegate);
2123
2124  delegate.SetOpacityFromAnimation(0.0);
2125
2126  {
2127    ScopedLayerAnimationSettings settings(animator.get());
2128    animator->SetOpacity(0.5);
2129    EXPECT_EQ(0.5, animator->GetTargetOpacity());
2130
2131    // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2132    animator->SetOpacity(1.0);
2133    EXPECT_EQ(1.0, animator->GetTargetOpacity());
2134  }
2135}
2136
2137// Verifies GetTargetBrightness() works when multiple sequences are scheduled.
2138TEST(LayerAnimatorTest, GetTargetBrightness) {
2139  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2140  animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2141  animator->set_disable_timer_for_test(true);
2142  TestLayerAnimationDelegate delegate;
2143  animator->SetDelegate(&delegate);
2144
2145  delegate.SetBrightnessFromAnimation(0.0);
2146
2147  {
2148    ScopedLayerAnimationSettings settings(animator.get());
2149    animator->SetBrightness(0.5);
2150    EXPECT_EQ(0.5, animator->GetTargetBrightness());
2151
2152    // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2153    animator->SetBrightness(1.0);
2154    EXPECT_EQ(1.0, animator->GetTargetBrightness());
2155  }
2156}
2157
2158// Verifies GetTargetGrayscale() works when multiple sequences are scheduled.
2159TEST(LayerAnimatorTest, GetTargetGrayscale) {
2160  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2161  animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2162  animator->set_disable_timer_for_test(true);
2163  TestLayerAnimationDelegate delegate;
2164  animator->SetDelegate(&delegate);
2165
2166  delegate.SetGrayscaleFromAnimation(0.0);
2167
2168  {
2169    ScopedLayerAnimationSettings settings(animator.get());
2170    animator->SetGrayscale(0.5);
2171    EXPECT_EQ(0.5, animator->GetTargetGrayscale());
2172
2173    // Because the strategy is ENQUEUE_NEW_ANIMATION the target should now be 1.
2174    animator->SetGrayscale(1.0);
2175    EXPECT_EQ(1.0, animator->GetTargetGrayscale());
2176  }
2177}
2178
2179// Verifies color property is modified appropriately.
2180TEST(LayerAnimatorTest, Color) {
2181  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2182  animator->set_disable_timer_for_test(true);
2183  TestLayerAnimationDelegate delegate;
2184  animator->SetDelegate(&delegate);
2185
2186  SkColor start_color  = SkColorSetARGB( 64, 20, 40,  60);
2187  SkColor middle_color = SkColorSetARGB(128, 35, 70, 120);
2188  SkColor target_color = SkColorSetARGB(192, 40, 80, 140);
2189
2190  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2191
2192  delegate.SetColorFromAnimation(start_color);
2193
2194  animator->ScheduleAnimation(
2195      new LayerAnimationSequence(
2196          LayerAnimationElement::CreateColorElement(target_color, delta)));
2197
2198  EXPECT_TRUE(animator->is_animating());
2199  EXPECT_EQ(ColorToString(start_color),
2200            ColorToString(delegate.GetColorForAnimation()));
2201
2202  base::TimeTicks start_time = animator->last_step_time();
2203
2204  animator->Step(start_time + base::TimeDelta::FromMilliseconds(500));
2205
2206  EXPECT_TRUE(animator->is_animating());
2207  EXPECT_EQ(ColorToString(middle_color),
2208            ColorToString(delegate.GetColorForAnimation()));
2209
2210  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1000));
2211
2212  EXPECT_FALSE(animator->is_animating());
2213  EXPECT_EQ(ColorToString(target_color),
2214            ColorToString(delegate.GetColorForAnimation()));
2215}
2216
2217// Verifies SchedulePauseForProperties().
2218TEST(LayerAnimatorTest, SchedulePauseForProperties) {
2219  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2220  animator->set_preemption_strategy(LayerAnimator::ENQUEUE_NEW_ANIMATION);
2221  animator->SchedulePauseForProperties(
2222      base::TimeDelta::FromMilliseconds(100),
2223      LayerAnimationElement::TRANSFORM | LayerAnimationElement::BOUNDS);
2224  EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::TRANSFORM));
2225  EXPECT_TRUE(animator->IsAnimatingProperty(LayerAnimationElement::BOUNDS));
2226  EXPECT_FALSE(animator->IsAnimatingProperty(LayerAnimationElement::OPACITY));
2227}
2228
2229
2230class AnimatorOwner {
2231public:
2232  AnimatorOwner()
2233      : animator_(LayerAnimator::CreateDefaultAnimator()) {
2234  }
2235
2236  LayerAnimator* animator() { return animator_.get(); }
2237
2238private:
2239  scoped_refptr<LayerAnimator> animator_;
2240
2241  DISALLOW_COPY_AND_ASSIGN(AnimatorOwner);
2242};
2243
2244class DeletingObserver : public LayerAnimationObserver {
2245public:
2246  DeletingObserver(bool* was_deleted)
2247      : animator_owner_(new AnimatorOwner),
2248        delete_on_animation_ended_(false),
2249        delete_on_animation_aborted_(false),
2250        delete_on_animation_scheduled_(false),
2251        was_deleted_(was_deleted) {
2252    animator()->AddObserver(this);
2253  }
2254
2255  virtual ~DeletingObserver() {
2256    animator()->RemoveObserver(this);
2257    *was_deleted_ = true;
2258  }
2259
2260  LayerAnimator* animator() { return animator_owner_->animator(); }
2261
2262  bool delete_on_animation_ended() const {
2263    return delete_on_animation_ended_;
2264  }
2265  void set_delete_on_animation_ended(bool enabled) {
2266    delete_on_animation_ended_ = enabled;
2267  }
2268
2269  bool delete_on_animation_aborted() const {
2270    return delete_on_animation_aborted_;
2271  }
2272  void set_delete_on_animation_aborted(bool enabled) {
2273    delete_on_animation_aborted_ = enabled;
2274  }
2275
2276  bool delete_on_animation_scheduled() const {
2277    return delete_on_animation_scheduled_;
2278  }
2279  void set_delete_on_animation_scheduled(bool enabled) {
2280    delete_on_animation_scheduled_ = enabled;
2281  }
2282
2283  // LayerAnimationObserver implementation.
2284  virtual void OnLayerAnimationEnded(
2285      LayerAnimationSequence* sequence) OVERRIDE {
2286    if (delete_on_animation_ended_)
2287      delete this;
2288  }
2289
2290  virtual void OnLayerAnimationAborted(
2291      LayerAnimationSequence* sequence) OVERRIDE {
2292    if (delete_on_animation_aborted_)
2293      delete this;
2294  }
2295
2296  virtual void OnLayerAnimationScheduled(
2297      LayerAnimationSequence* sequence) OVERRIDE {
2298    if (delete_on_animation_scheduled_)
2299      delete this;
2300  }
2301
2302private:
2303  scoped_ptr<AnimatorOwner> animator_owner_;
2304  bool delete_on_animation_ended_;
2305  bool delete_on_animation_aborted_;
2306  bool delete_on_animation_scheduled_;
2307  bool* was_deleted_;
2308
2309  DISALLOW_COPY_AND_ASSIGN(DeletingObserver);
2310};
2311
2312TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterFinishingAnimation) {
2313  bool observer_was_deleted = false;
2314  DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2315  observer->set_delete_on_animation_ended(true);
2316  observer->set_delete_on_animation_aborted(true);
2317  LayerAnimator* animator = observer->animator();
2318  animator->set_disable_timer_for_test(true);
2319  TestLayerAnimationDelegate delegate;
2320  animator->SetDelegate(&delegate);
2321
2322  delegate.SetBrightnessFromAnimation(0.0f);
2323
2324  gfx::Rect start_bounds(0, 0, 50, 50);
2325  gfx::Rect target_bounds(10, 10, 100, 100);
2326
2327  delegate.SetBoundsFromAnimation(start_bounds);
2328
2329  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2330  LayerAnimationSequence* brightness_sequence = new LayerAnimationSequence(
2331      LayerAnimationElement::CreateBrightnessElement(1.0f, delta));
2332  animator->StartAnimation(brightness_sequence);
2333
2334  delta = base::TimeDelta::FromSeconds(2);
2335  LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2336      LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2337  animator->StartAnimation(bounds_sequence);
2338
2339  base::TimeTicks start_time = animator->last_step_time();
2340  animator->Step(start_time + base::TimeDelta::FromMilliseconds(1500));
2341
2342  EXPECT_TRUE(observer_was_deleted);
2343}
2344
2345TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterStoppingAnimating) {
2346  bool observer_was_deleted = false;
2347  DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2348  observer->set_delete_on_animation_ended(true);
2349  observer->set_delete_on_animation_aborted(true);
2350  LayerAnimator* animator = observer->animator();
2351  animator->set_disable_timer_for_test(true);
2352  TestLayerAnimationDelegate delegate;
2353  animator->SetDelegate(&delegate);
2354
2355  delegate.SetOpacityFromAnimation(0.0f);
2356
2357  gfx::Rect start_bounds(0, 0, 50, 50);
2358  gfx::Rect target_bounds(10, 10, 100, 100);
2359
2360  delegate.SetBoundsFromAnimation(start_bounds);
2361
2362  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2363  LayerAnimationSequence* opacity_sequence = new LayerAnimationSequence(
2364      LayerAnimationElement::CreateOpacityElement(1.0f, delta));
2365  animator->StartAnimation(opacity_sequence);
2366
2367  delta = base::TimeDelta::FromSeconds(2);
2368  LayerAnimationSequence* bounds_sequence = new LayerAnimationSequence(
2369      LayerAnimationElement::CreateBoundsElement(target_bounds, delta));
2370  animator->StartAnimation(bounds_sequence);
2371
2372  animator->StopAnimating();
2373
2374  EXPECT_TRUE(observer_was_deleted);
2375}
2376
2377TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterScheduling) {
2378  bool observer_was_deleted = false;
2379  TestLayerAnimationDelegate delegate;
2380  DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2381  observer->set_delete_on_animation_scheduled(true);
2382  LayerAnimator* animator = observer->animator();
2383  animator->set_disable_timer_for_test(true);
2384  animator->SetDelegate(&delegate);
2385
2386  delegate.SetOpacityFromAnimation(0.0f);
2387
2388  gfx::Rect start_bounds(0, 0, 50, 50);
2389  gfx::Rect target_bounds(10, 10, 100, 100);
2390
2391  delegate.SetBoundsFromAnimation(start_bounds);
2392
2393  std::vector<LayerAnimationSequence*> to_start;
2394
2395  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2396  to_start.push_back(new LayerAnimationSequence(
2397      LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2398
2399  delta = base::TimeDelta::FromSeconds(2);
2400  to_start.push_back(new LayerAnimationSequence(
2401      LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2402
2403  animator->ScheduleTogether(to_start);
2404
2405  EXPECT_TRUE(observer_was_deleted);
2406}
2407
2408TEST(LayerAnimatorTest, ObserverDeletesAnimatorAfterAborted) {
2409  bool observer_was_deleted = false;
2410  DeletingObserver* observer = new DeletingObserver(&observer_was_deleted);
2411  TestLayerAnimationDelegate delegate;
2412  observer->set_delete_on_animation_aborted(true);
2413  LayerAnimator* animator = observer->animator();
2414  animator->set_preemption_strategy(
2415      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2416  animator->set_disable_timer_for_test(true);
2417  animator->SetDelegate(&delegate);
2418
2419  delegate.SetOpacityFromAnimation(0.0f);
2420
2421  gfx::Rect start_bounds(0, 0, 50, 50);
2422  gfx::Rect target_bounds(10, 10, 100, 100);
2423
2424  delegate.SetBoundsFromAnimation(start_bounds);
2425
2426  std::vector<LayerAnimationSequence*> to_start;
2427
2428  base::TimeDelta delta = base::TimeDelta::FromSeconds(1);
2429  to_start.push_back(new LayerAnimationSequence(
2430      LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2431
2432  delta = base::TimeDelta::FromSeconds(2);
2433  to_start.push_back(new LayerAnimationSequence(
2434      LayerAnimationElement::CreateBoundsElement(target_bounds, delta)));
2435
2436  animator->ScheduleTogether(to_start);
2437
2438  EXPECT_FALSE(observer_was_deleted);
2439
2440  animator->StartAnimation(new LayerAnimationSequence(
2441      LayerAnimationElement::CreateOpacityElement(1.0f, delta)));
2442
2443  EXPECT_TRUE(observer_was_deleted);
2444}
2445
2446
2447TEST(LayerAnimatorTest, TestSetterRespectEnqueueStrategy) {
2448  TestLayerAnimationDelegate delegate;
2449  scoped_refptr<LayerAnimator> animator(LayerAnimator::CreateDefaultAnimator());
2450  animator->set_disable_timer_for_test(true);
2451
2452  animator->SetDelegate(&delegate);
2453
2454  float start_opacity = 0.0f;
2455  float target_opacity = 1.0f;
2456  float magic_opacity = 0.123f;
2457
2458  delegate.SetOpacityFromAnimation(start_opacity);
2459
2460  ScopedLayerAnimationSettings settings(animator.get());
2461  settings.SetPreemptionStrategy(
2462      LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
2463  settings.SetTransitionDuration(base::TimeDelta::FromSeconds(1));
2464  animator->SetOpacity(target_opacity);
2465
2466  EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2467
2468  settings.SetPreemptionStrategy(
2469      LayerAnimator::ENQUEUE_NEW_ANIMATION);
2470  settings.SetTransitionDuration(base::TimeDelta());
2471  animator->SetOpacity(magic_opacity);
2472
2473  EXPECT_EQ(start_opacity, delegate.GetOpacityForAnimation());
2474}
2475
2476TEST(LayerAnimatorTest, TestScopedCounterAnimation) {
2477  Layer parent, child;
2478  parent.Add(&child);
2479
2480  gfx::Transform parent_begin, parent_end;
2481
2482  parent_end.Scale3d(2.0, 0.5, 1.0);
2483
2484  // Parent animates from identity to the end value. The counter animation will
2485  // start at the end value and animate back to identity.
2486  gfx::Transform child_begin(parent_end);
2487
2488  child.SetTransform(child_begin);
2489  parent.SetTransform(parent_begin);
2490
2491  EXPECT_FALSE(child.GetAnimator()->is_animating());
2492
2493  ScopedLayerAnimationSettings settings(parent.GetAnimator());
2494  settings.SetInverselyAnimatedBaseLayer(&parent);
2495  settings.AddInverselyAnimatedLayer(&child);
2496
2497  parent.SetTransform(parent_end);
2498
2499  EXPECT_TRUE(child.GetAnimator()->is_animating());
2500  EXPECT_TRUE(child.GetTargetTransform().IsIdentity())
2501    << child.GetTargetTransform().ToString();
2502
2503}
2504
2505class CollectionLayerAnimationDelegate : public TestLayerAnimationDelegate {
2506 public:
2507  CollectionLayerAnimationDelegate() : collection(NULL) {}
2508  virtual ~CollectionLayerAnimationDelegate() {}
2509
2510  // LayerAnimationDelegate:
2511  virtual LayerAnimatorCollection* GetLayerAnimatorCollection() OVERRIDE {
2512    return &collection;
2513  }
2514
2515 private:
2516  LayerAnimatorCollection collection;
2517};
2518
2519TEST(LayerAnimatorTest, LayerAnimatorCollectionTickTime) {
2520  Layer layer;
2521  LayerAnimator* animator = layer.GetAnimator();
2522  CollectionLayerAnimationDelegate delegate;
2523  animator->SetDelegate(&delegate);
2524
2525  LayerAnimatorCollection* collection = delegate.GetLayerAnimatorCollection();
2526  base::TimeTicks null;
2527  collection->OnAnimationStep(null);
2528  EXPECT_TRUE(collection->last_tick_time().is_null());
2529
2530  // Adding an animator to the collection should update the last tick time.
2531  collection->StartAnimator(layer.GetAnimator());
2532  EXPECT_TRUE(collection->HasActiveAnimators());
2533  EXPECT_FALSE(collection->last_tick_time().is_null());
2534
2535  collection->StopAnimator(layer.GetAnimator());
2536  EXPECT_FALSE(collection->HasActiveAnimators());
2537}
2538
2539TEST(LayerAnimatorTest, AnimatorStartedCorrectly) {
2540  Layer layer;
2541  LayerAnimatorTestController test_controller(layer.GetAnimator());
2542  LayerAnimator* animator = test_controller.animator();
2543  ASSERT_FALSE(animator->is_started_);
2544
2545  TestLayerAnimationDelegate test_delegate;
2546  animator->SetDelegate(&test_delegate);
2547  double target_opacity = 1.0;
2548  base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2549  animator->ScheduleAnimation(new LayerAnimationSequence(
2550      LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta)));
2551  EXPECT_FALSE(animator->is_started_);
2552
2553  CollectionLayerAnimationDelegate collection_delegate;
2554  animator->SetDelegate(&collection_delegate);
2555  animator->UpdateAnimationState();
2556  EXPECT_TRUE(animator->is_started_);
2557  animator->SetDelegate(NULL);
2558}
2559
2560TEST(LayerAnimatorTest, AnimatorRemovedFromCollectionWhenLayerIsDestroyed) {
2561  scoped_ptr<Layer> layer(new Layer(LAYER_TEXTURED));
2562  LayerAnimatorTestController test_controller(layer->GetAnimator());
2563  scoped_refptr<LayerAnimator> animator = test_controller.animator();
2564  CollectionLayerAnimationDelegate collection_delegate;
2565  animator->SetDelegate(&collection_delegate);
2566
2567  double target_opacity = 1.0;
2568  base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2569  animator->ScheduleAnimation(new LayerAnimationSequence(
2570      LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta)));
2571
2572  EXPECT_TRUE(
2573      collection_delegate.GetLayerAnimatorCollection()->HasActiveAnimators());
2574
2575  layer.reset();
2576  EXPECT_EQ(NULL, animator->delegate());
2577  EXPECT_FALSE(
2578      collection_delegate.GetLayerAnimatorCollection()->HasActiveAnimators());
2579}
2580
2581TEST(LayerAnimatorTest, LayerMovedBetweenCompositorsDuringAnimation) {
2582  bool enable_pixel_output = false;
2583  ui::ContextFactory* context_factory =
2584      InitializeContextFactoryForTests(enable_pixel_output);
2585  const gfx::Rect bounds(10, 10, 100, 100);
2586  scoped_ptr<TestCompositorHost> host_1(
2587      TestCompositorHost::Create(bounds, context_factory));
2588  scoped_ptr<TestCompositorHost> host_2(
2589      TestCompositorHost::Create(bounds, context_factory));
2590  host_1->Show();
2591  host_2->Show();
2592
2593  Compositor* compositor_1 = host_1->GetCompositor();
2594  Layer root_1;
2595  compositor_1->SetRootLayer(&root_1);
2596
2597  Compositor* compositor_2 = host_2->GetCompositor();
2598  Layer root_2;
2599  compositor_2->SetRootLayer(&root_2);
2600
2601  // Verify that neither compositor has active animators.
2602  EXPECT_FALSE(compositor_1->layer_animator_collection()->HasActiveAnimators());
2603  EXPECT_FALSE(compositor_2->layer_animator_collection()->HasActiveAnimators());
2604
2605  Layer layer;
2606  root_1.Add(&layer);
2607  LayerAnimator* animator = layer.GetAnimator();
2608  double target_opacity = 1.0;
2609  base::TimeDelta time_delta = base::TimeDelta::FromSeconds(1);
2610  animator->ScheduleAnimation(new LayerAnimationSequence(
2611      LayerAnimationElement::CreateOpacityElement(target_opacity, time_delta)));
2612  EXPECT_TRUE(compositor_1->layer_animator_collection()->HasActiveAnimators());
2613  EXPECT_FALSE(compositor_2->layer_animator_collection()->HasActiveAnimators());
2614
2615  root_2.Add(&layer);
2616  EXPECT_FALSE(compositor_1->layer_animator_collection()->HasActiveAnimators());
2617  EXPECT_TRUE(compositor_2->layer_animator_collection()->HasActiveAnimators());
2618  host_2.reset();
2619  host_1.reset();
2620  TerminateContextFactoryForTests();
2621}
2622
2623}  // namespace ui
2624