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