1// Copyright 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 "cc/animation/layer_animation_controller.h"
6
7#include "cc/animation/animation.h"
8#include "cc/animation/animation_curve.h"
9#include "cc/animation/animation_delegate.h"
10#include "cc/animation/animation_registrar.h"
11#include "cc/animation/keyframed_animation_curve.h"
12#include "cc/animation/scroll_offset_animation_curve.h"
13#include "cc/animation/transform_operations.h"
14#include "cc/test/animation_test_common.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17#include "ui/gfx/box_f.h"
18#include "ui/gfx/transform.h"
19
20namespace cc {
21namespace {
22
23using base::TimeDelta;
24using base::TimeTicks;
25
26static base::TimeTicks TicksFromSecondsF(double seconds) {
27  return base::TimeTicks::FromInternalValue(seconds *
28                                            base::Time::kMicrosecondsPerSecond);
29}
30
31// A LayerAnimationController cannot be ticked at 0.0, since an animation
32// with start time 0.0 is treated as an animation whose start time has
33// not yet been set.
34const TimeTicks kInitialTickTime = TicksFromSecondsF(1.0);
35
36scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve,
37                                      int id,
38                                      Animation::TargetProperty property) {
39  return Animation::Create(curve.Pass(), 0, id, property);
40}
41
42TEST(LayerAnimationControllerTest, SyncNewAnimation) {
43  FakeLayerAnimationValueObserver dummy_impl;
44  scoped_refptr<LayerAnimationController> controller_impl(
45      LayerAnimationController::Create(0));
46  controller_impl->AddValueObserver(&dummy_impl);
47  FakeLayerAnimationValueObserver dummy;
48  scoped_refptr<LayerAnimationController> controller(
49      LayerAnimationController::Create(0));
50  controller->AddValueObserver(&dummy);
51
52  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
53
54  EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
55  EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
56
57  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
58  EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
59  int group_id = controller->GetAnimation(Animation::Opacity)->group();
60
61  controller->PushAnimationUpdatesTo(controller_impl.get());
62  EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
63  controller_impl->ActivateAnimations();
64
65  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
66  EXPECT_EQ(Animation::WaitingForTargetAvailability,
67            controller_impl->GetAnimation(group_id,
68                                          Animation::Opacity)->run_state());
69}
70
71// If an animation is started on the impl thread before it is ticked on the main
72// thread, we must be sure to respect the synchronized start time.
73TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) {
74  FakeLayerAnimationValueObserver dummy_impl;
75  scoped_refptr<LayerAnimationController> controller_impl(
76      LayerAnimationController::Create(0));
77  controller_impl->AddValueObserver(&dummy_impl);
78  FakeLayerAnimationValueObserver dummy;
79  scoped_refptr<LayerAnimationController> controller(
80      LayerAnimationController::Create(0));
81  controller->AddValueObserver(&dummy);
82
83  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
84
85  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
86  int group_id = controller->GetAnimation(Animation::Opacity)->group();
87
88  controller->PushAnimationUpdatesTo(controller_impl.get());
89  controller_impl->ActivateAnimations();
90
91  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
92  EXPECT_EQ(Animation::WaitingForTargetAvailability,
93            controller_impl->GetAnimation(group_id,
94                                          Animation::Opacity)->run_state());
95
96  AnimationEventsVector events;
97  controller_impl->Animate(kInitialTickTime);
98  controller_impl->UpdateState(true, &events);
99
100  // Synchronize the start times.
101  EXPECT_EQ(1u, events.size());
102  controller->NotifyAnimationStarted(events[0]);
103  EXPECT_EQ(controller->GetAnimation(group_id,
104                                     Animation::Opacity)->start_time(),
105            controller_impl->GetAnimation(group_id,
106                                          Animation::Opacity)->start_time());
107
108  // Start the animation on the main thread. Should not affect the start time.
109  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
110  controller->UpdateState(true, NULL);
111  EXPECT_EQ(controller->GetAnimation(group_id,
112                                     Animation::Opacity)->start_time(),
113            controller_impl->GetAnimation(group_id,
114                                          Animation::Opacity)->start_time());
115}
116
117TEST(LayerAnimationControllerTest, UseSpecifiedStartTimes) {
118  FakeLayerAnimationValueObserver dummy_impl;
119  scoped_refptr<LayerAnimationController> controller_impl(
120      LayerAnimationController::Create(0));
121  controller_impl->AddValueObserver(&dummy_impl);
122  FakeLayerAnimationValueObserver dummy;
123  scoped_refptr<LayerAnimationController> controller(
124      LayerAnimationController::Create(0));
125  controller->AddValueObserver(&dummy);
126
127  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
128  int group_id = controller->GetAnimation(Animation::Opacity)->group();
129
130  const TimeTicks start_time = TicksFromSecondsF(123);
131  controller->GetAnimation(Animation::Opacity)->set_start_time(start_time);
132
133  controller->PushAnimationUpdatesTo(controller_impl.get());
134  controller_impl->ActivateAnimations();
135
136  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
137  EXPECT_EQ(Animation::WaitingForTargetAvailability,
138            controller_impl->GetAnimation(group_id,
139                                          Animation::Opacity)->run_state());
140
141  AnimationEventsVector events;
142  controller_impl->Animate(kInitialTickTime);
143  controller_impl->UpdateState(true, &events);
144
145  // Synchronize the start times.
146  EXPECT_EQ(1u, events.size());
147  controller->NotifyAnimationStarted(events[0]);
148
149  EXPECT_EQ(start_time,
150            controller->GetAnimation(group_id,
151                                     Animation::Opacity)->start_time());
152  EXPECT_EQ(controller->GetAnimation(group_id,
153                                     Animation::Opacity)->start_time(),
154            controller_impl->GetAnimation(group_id,
155                                          Animation::Opacity)->start_time());
156
157  // Start the animation on the main thread. Should not affect the start time.
158  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
159  controller->UpdateState(true, NULL);
160  EXPECT_EQ(start_time,
161            controller->GetAnimation(group_id,
162                                     Animation::Opacity)->start_time());
163  EXPECT_EQ(controller->GetAnimation(group_id,
164                                     Animation::Opacity)->start_time(),
165            controller_impl->GetAnimation(group_id,
166                                          Animation::Opacity)->start_time());
167}
168
169// Tests that controllers activate and deactivate as expected.
170TEST(LayerAnimationControllerTest, Activation) {
171  scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
172  scoped_ptr<AnimationRegistrar> registrar_impl = AnimationRegistrar::Create();
173
174  FakeLayerAnimationValueObserver dummy_impl;
175  scoped_refptr<LayerAnimationController> controller_impl(
176      LayerAnimationController::Create(0));
177  controller_impl->AddValueObserver(&dummy_impl);
178  FakeLayerAnimationValueObserver dummy;
179  scoped_refptr<LayerAnimationController> controller(
180      LayerAnimationController::Create(0));
181  controller->AddValueObserver(&dummy);
182  scoped_ptr<AnimationEventsVector> events(
183      make_scoped_ptr(new AnimationEventsVector));
184
185  controller->SetAnimationRegistrar(registrar.get());
186  controller_impl->SetAnimationRegistrar(registrar_impl.get());
187  EXPECT_EQ(1u, registrar->all_animation_controllers().size());
188  EXPECT_EQ(1u, registrar_impl->all_animation_controllers().size());
189
190  // Initially, both controllers should be inactive.
191  EXPECT_EQ(0u, registrar->active_animation_controllers().size());
192  EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
193
194  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
195  // The main thread controller should now be active.
196  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
197
198  controller->PushAnimationUpdatesTo(controller_impl.get());
199  controller_impl->ActivateAnimations();
200  // Both controllers should now be active.
201  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
202  EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
203
204  controller_impl->Animate(kInitialTickTime);
205  controller_impl->UpdateState(true, events.get());
206  EXPECT_EQ(1u, events->size());
207  controller->NotifyAnimationStarted((*events)[0]);
208
209  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
210  EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
211
212  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
213  controller->UpdateState(true, NULL);
214  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
215
216  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
217  controller->UpdateState(true, NULL);
218  EXPECT_EQ(Animation::Finished,
219            controller->GetAnimation(Animation::Opacity)->run_state());
220  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
221
222  events.reset(new AnimationEventsVector);
223  controller_impl->Animate(kInitialTickTime +
224                           TimeDelta::FromMilliseconds(1500));
225  controller_impl->UpdateState(true, events.get());
226
227  EXPECT_EQ(Animation::WaitingForDeletion,
228            controller_impl->GetAnimation(Animation::Opacity)->run_state());
229  // The impl thread controller should have de-activated.
230  EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
231
232  EXPECT_EQ(1u, events->size());
233  controller->NotifyAnimationFinished((*events)[0]);
234  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
235  controller->UpdateState(true, NULL);
236
237  EXPECT_EQ(Animation::WaitingForDeletion,
238            controller->GetAnimation(Animation::Opacity)->run_state());
239  // The main thread controller should have de-activated.
240  EXPECT_EQ(0u, registrar->active_animation_controllers().size());
241
242  controller->PushAnimationUpdatesTo(controller_impl.get());
243  controller_impl->ActivateAnimations();
244  EXPECT_FALSE(controller->has_any_animation());
245  EXPECT_FALSE(controller_impl->has_any_animation());
246  EXPECT_EQ(0u, registrar->active_animation_controllers().size());
247  EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
248
249  controller->SetAnimationRegistrar(NULL);
250  controller_impl->SetAnimationRegistrar(NULL);
251}
252
253TEST(LayerAnimationControllerTest, SyncPause) {
254  FakeLayerAnimationValueObserver dummy_impl;
255  scoped_refptr<LayerAnimationController> controller_impl(
256      LayerAnimationController::Create(0));
257  controller_impl->AddValueObserver(&dummy_impl);
258  FakeLayerAnimationValueObserver dummy;
259  scoped_refptr<LayerAnimationController> controller(
260      LayerAnimationController::Create(0));
261  controller->AddValueObserver(&dummy);
262
263  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
264
265  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
266  int group_id = controller->GetAnimation(Animation::Opacity)->group();
267  int animation_id = controller->GetAnimation(Animation::Opacity)->id();
268
269  controller->PushAnimationUpdatesTo(controller_impl.get());
270  controller_impl->ActivateAnimations();
271
272  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
273  EXPECT_EQ(Animation::WaitingForTargetAvailability,
274            controller_impl->GetAnimation(group_id,
275                                          Animation::Opacity)->run_state());
276
277  // Start the animations on each controller.
278  AnimationEventsVector events;
279  controller_impl->Animate(kInitialTickTime);
280  controller_impl->UpdateState(true, &events);
281  controller->Animate(kInitialTickTime);
282  controller->UpdateState(true, NULL);
283  EXPECT_EQ(Animation::Running,
284            controller_impl->GetAnimation(group_id,
285                                          Animation::Opacity)->run_state());
286  EXPECT_EQ(Animation::Running,
287            controller->GetAnimation(group_id,
288                                     Animation::Opacity)->run_state());
289
290  // Pause the main-thread animation.
291  controller->PauseAnimation(
292      animation_id,
293      TimeDelta::FromMilliseconds(1000) + TimeDelta::FromMilliseconds(1000));
294  EXPECT_EQ(Animation::Paused,
295            controller->GetAnimation(group_id,
296                                     Animation::Opacity)->run_state());
297
298  // The pause run state change should make it to the impl thread controller.
299  controller->PushAnimationUpdatesTo(controller_impl.get());
300  controller_impl->ActivateAnimations();
301  EXPECT_EQ(Animation::Paused,
302            controller_impl->GetAnimation(group_id,
303                                          Animation::Opacity)->run_state());
304}
305
306TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
307  FakeLayerAnimationValueObserver dummy_impl;
308  scoped_refptr<LayerAnimationController> controller_impl(
309      LayerAnimationController::Create(0));
310  controller_impl->AddValueObserver(&dummy_impl);
311  FakeLayerAnimationValueObserver dummy;
312  scoped_refptr<LayerAnimationController> controller(
313      LayerAnimationController::Create(0));
314  controller->AddValueObserver(&dummy);
315
316  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
317
318  int animation_id =
319      AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
320  int group_id = controller->GetAnimation(Animation::Opacity)->group();
321
322  controller->PushAnimationUpdatesTo(controller_impl.get());
323  controller_impl->ActivateAnimations();
324
325  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
326  EXPECT_EQ(Animation::WaitingForTargetAvailability,
327            controller_impl->GetAnimation(group_id,
328                                          Animation::Opacity)->run_state());
329
330  // Notify main thread controller that the animation has started.
331  AnimationEvent animation_started_event(AnimationEvent::Started,
332                                         0,
333                                         group_id,
334                                         Animation::Opacity,
335                                         kInitialTickTime);
336  controller->NotifyAnimationStarted(animation_started_event);
337
338  // Force animation to complete on impl thread.
339  controller_impl->RemoveAnimation(animation_id);
340
341  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
342
343  controller->PushAnimationUpdatesTo(controller_impl.get());
344  controller_impl->ActivateAnimations();
345
346  // Even though the main thread has a 'new' animation, it should not be pushed
347  // because the animation has already completed on the impl thread.
348  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
349}
350
351// Ensure that a finished animation is eventually deleted by both the
352// main-thread and the impl-thread controllers.
353TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
354  FakeLayerAnimationValueObserver dummy;
355  FakeLayerAnimationValueObserver dummy_impl;
356  scoped_ptr<AnimationEventsVector> events(
357      make_scoped_ptr(new AnimationEventsVector));
358  scoped_refptr<LayerAnimationController> controller(
359      LayerAnimationController::Create(0));
360  scoped_refptr<LayerAnimationController> controller_impl(
361      LayerAnimationController::Create(0));
362  controller->AddValueObserver(&dummy);
363  controller_impl->AddValueObserver(&dummy_impl);
364
365  AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
366  controller->Animate(kInitialTickTime);
367  controller->UpdateState(true, NULL);
368  controller->PushAnimationUpdatesTo(controller_impl.get());
369  controller_impl->ActivateAnimations();
370
371  controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
372  controller_impl->UpdateState(true, events.get());
373
374  // There should be a Started event for the animation.
375  EXPECT_EQ(1u, events->size());
376  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
377  controller->NotifyAnimationStarted((*events)[0]);
378
379  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
380  controller->UpdateState(true, NULL);
381
382  EXPECT_FALSE(dummy.animation_waiting_for_deletion());
383  EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
384
385  events.reset(new AnimationEventsVector);
386  controller_impl->Animate(kInitialTickTime +
387                           TimeDelta::FromMilliseconds(2000));
388  controller_impl->UpdateState(true, events.get());
389
390  EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
391
392  // There should be a Finished event for the animation.
393  EXPECT_EQ(1u, events->size());
394  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
395
396  // Neither controller should have deleted the animation yet.
397  EXPECT_TRUE(controller->GetAnimation(Animation::Opacity));
398  EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity));
399
400  controller->NotifyAnimationFinished((*events)[0]);
401
402  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
403  controller->UpdateState(true, NULL);
404  EXPECT_TRUE(dummy.animation_waiting_for_deletion());
405
406  controller->PushAnimationUpdatesTo(controller_impl.get());
407
408  // Both controllers should now have deleted the animation. The impl controller
409  // should have deleted the animation even though activation has not occurred,
410  // since the animation was already waiting for deletion when
411  // PushAnimationUpdatesTo was called.
412  EXPECT_FALSE(controller->has_any_animation());
413  EXPECT_FALSE(controller_impl->has_any_animation());
414}
415
416// Tests that transitioning opacity from 0 to 1 works as expected.
417
418static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
419    const AnimationEventsVector* events) {
420  const AnimationEvent* event = 0;
421  for (size_t i = 0; i < events->size(); ++i)
422    if ((*events)[i].type == AnimationEvent::PropertyUpdate)
423      event = &(*events)[i];
424
425  return event;
426}
427
428TEST(LayerAnimationControllerTest, TrivialTransition) {
429  scoped_ptr<AnimationEventsVector> events(
430      make_scoped_ptr(new AnimationEventsVector));
431  FakeLayerAnimationValueObserver dummy;
432  scoped_refptr<LayerAnimationController> controller(
433      LayerAnimationController::Create(0));
434  controller->AddValueObserver(&dummy);
435
436  scoped_ptr<Animation> to_add(CreateAnimation(
437      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
438      1,
439      Animation::Opacity));
440
441  EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
442  controller->AddAnimation(to_add.Pass());
443  EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
444  controller->Animate(kInitialTickTime);
445  EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
446  controller->UpdateState(true, events.get());
447  EXPECT_TRUE(controller->HasActiveAnimation());
448  EXPECT_EQ(0.f, dummy.opacity());
449  // A non-impl-only animation should not generate property updates.
450  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
451  EXPECT_FALSE(event);
452  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
453  controller->UpdateState(true, events.get());
454  EXPECT_EQ(1.f, dummy.opacity());
455  EXPECT_FALSE(controller->HasActiveAnimation());
456  event = GetMostRecentPropertyUpdateEvent(events.get());
457  EXPECT_FALSE(event);
458}
459
460TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
461  scoped_ptr<AnimationEventsVector> events(
462      make_scoped_ptr(new AnimationEventsVector));
463  FakeLayerAnimationValueObserver dummy_impl;
464  scoped_refptr<LayerAnimationController> controller_impl(
465      LayerAnimationController::Create(0));
466  controller_impl->AddValueObserver(&dummy_impl);
467
468  scoped_ptr<Animation> to_add(CreateAnimation(
469      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
470      1,
471      Animation::Opacity));
472  to_add->set_is_impl_only(true);
473
474  controller_impl->AddAnimation(to_add.Pass());
475  controller_impl->Animate(kInitialTickTime);
476  controller_impl->UpdateState(true, events.get());
477  EXPECT_TRUE(controller_impl->HasActiveAnimation());
478  EXPECT_EQ(0.f, dummy_impl.opacity());
479  EXPECT_EQ(1u, events->size());
480  const AnimationEvent* start_opacity_event =
481      GetMostRecentPropertyUpdateEvent(events.get());
482  EXPECT_EQ(0.f, start_opacity_event->opacity);
483
484  controller_impl->Animate(kInitialTickTime +
485                           TimeDelta::FromMilliseconds(1000));
486  controller_impl->UpdateState(true, events.get());
487  EXPECT_EQ(1.f, dummy_impl.opacity());
488  EXPECT_FALSE(controller_impl->HasActiveAnimation());
489  EXPECT_EQ(2u, events->size());
490  const AnimationEvent* end_opacity_event =
491      GetMostRecentPropertyUpdateEvent(events.get());
492  EXPECT_EQ(1.f, end_opacity_event->opacity);
493}
494
495TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
496  scoped_ptr<AnimationEventsVector> events(
497      make_scoped_ptr(new AnimationEventsVector));
498  FakeLayerAnimationValueObserver dummy_impl;
499  scoped_refptr<LayerAnimationController> controller_impl(
500      LayerAnimationController::Create(0));
501  controller_impl->AddValueObserver(&dummy_impl);
502
503  // Choose different values for x and y to avoid coincidental values in the
504  // observed transforms.
505  const float delta_x = 3;
506  const float delta_y = 4;
507
508  scoped_ptr<KeyframedTransformAnimationCurve> curve(
509      KeyframedTransformAnimationCurve::Create());
510
511  // Create simple Transform animation.
512  TransformOperations operations;
513  curve->AddKeyframe(
514      TransformKeyframe::Create(0, operations, scoped_ptr<TimingFunction>()));
515  operations.AppendTranslate(delta_x, delta_y, 0);
516  curve->AddKeyframe(
517      TransformKeyframe::Create(1, operations, scoped_ptr<TimingFunction>()));
518
519  scoped_ptr<Animation> animation(Animation::Create(
520      curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform));
521  animation->set_is_impl_only(true);
522  controller_impl->AddAnimation(animation.Pass());
523
524  // Run animation.
525  controller_impl->Animate(kInitialTickTime);
526  controller_impl->UpdateState(true, events.get());
527  EXPECT_TRUE(controller_impl->HasActiveAnimation());
528  EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
529  EXPECT_EQ(1u, events->size());
530  const AnimationEvent* start_transform_event =
531      GetMostRecentPropertyUpdateEvent(events.get());
532  ASSERT_TRUE(start_transform_event);
533  EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
534  EXPECT_TRUE(start_transform_event->is_impl_only);
535
536  gfx::Transform expected_transform;
537  expected_transform.Translate(delta_x, delta_y);
538
539  controller_impl->Animate(kInitialTickTime +
540                           TimeDelta::FromMilliseconds(1000));
541  controller_impl->UpdateState(true, events.get());
542  EXPECT_EQ(expected_transform, dummy_impl.transform());
543  EXPECT_FALSE(controller_impl->HasActiveAnimation());
544  EXPECT_EQ(2u, events->size());
545  const AnimationEvent* end_transform_event =
546      GetMostRecentPropertyUpdateEvent(events.get());
547  EXPECT_EQ(expected_transform, end_transform_event->transform);
548  EXPECT_TRUE(end_transform_event->is_impl_only);
549}
550
551TEST(LayerAnimationControllerTest, FilterTransition) {
552  scoped_ptr<AnimationEventsVector> events(
553      make_scoped_ptr(new AnimationEventsVector));
554  FakeLayerAnimationValueObserver dummy;
555  scoped_refptr<LayerAnimationController> controller(
556      LayerAnimationController::Create(0));
557  controller->AddValueObserver(&dummy);
558
559  scoped_ptr<KeyframedFilterAnimationCurve> curve(
560      KeyframedFilterAnimationCurve::Create());
561
562  FilterOperations start_filters;
563  start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
564  curve->AddKeyframe(
565      FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
566  FilterOperations end_filters;
567  end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
568  curve->AddKeyframe(
569      FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
570
571  scoped_ptr<Animation> animation(Animation::Create(
572      curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
573  controller->AddAnimation(animation.Pass());
574
575  controller->Animate(kInitialTickTime);
576  controller->UpdateState(true, events.get());
577  EXPECT_TRUE(controller->HasActiveAnimation());
578  EXPECT_EQ(start_filters, dummy.filters());
579  // A non-impl-only animation should not generate property updates.
580  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
581  EXPECT_FALSE(event);
582
583  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
584  controller->UpdateState(true, events.get());
585  EXPECT_EQ(1u, dummy.filters().size());
586  EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
587            dummy.filters().at(0));
588  event = GetMostRecentPropertyUpdateEvent(events.get());
589  EXPECT_FALSE(event);
590
591  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
592  controller->UpdateState(true, events.get());
593  EXPECT_EQ(end_filters, dummy.filters());
594  EXPECT_FALSE(controller->HasActiveAnimation());
595  event = GetMostRecentPropertyUpdateEvent(events.get());
596  EXPECT_FALSE(event);
597}
598
599TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
600  scoped_ptr<AnimationEventsVector> events(
601      make_scoped_ptr(new AnimationEventsVector));
602  FakeLayerAnimationValueObserver dummy_impl;
603  scoped_refptr<LayerAnimationController> controller_impl(
604      LayerAnimationController::Create(0));
605  controller_impl->AddValueObserver(&dummy_impl);
606
607  scoped_ptr<KeyframedFilterAnimationCurve> curve(
608      KeyframedFilterAnimationCurve::Create());
609
610  // Create simple Filter animation.
611  FilterOperations start_filters;
612  start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
613  curve->AddKeyframe(
614      FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
615  FilterOperations end_filters;
616  end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
617  curve->AddKeyframe(
618      FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
619
620  scoped_ptr<Animation> animation(Animation::Create(
621      curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
622  animation->set_is_impl_only(true);
623  controller_impl->AddAnimation(animation.Pass());
624
625  // Run animation.
626  controller_impl->Animate(kInitialTickTime);
627  controller_impl->UpdateState(true, events.get());
628  EXPECT_TRUE(controller_impl->HasActiveAnimation());
629  EXPECT_EQ(start_filters, dummy_impl.filters());
630  EXPECT_EQ(1u, events->size());
631  const AnimationEvent* start_filter_event =
632      GetMostRecentPropertyUpdateEvent(events.get());
633  EXPECT_TRUE(start_filter_event);
634  EXPECT_EQ(start_filters, start_filter_event->filters);
635  EXPECT_TRUE(start_filter_event->is_impl_only);
636
637  controller_impl->Animate(kInitialTickTime +
638                           TimeDelta::FromMilliseconds(1000));
639  controller_impl->UpdateState(true, events.get());
640  EXPECT_EQ(end_filters, dummy_impl.filters());
641  EXPECT_FALSE(controller_impl->HasActiveAnimation());
642  EXPECT_EQ(2u, events->size());
643  const AnimationEvent* end_filter_event =
644      GetMostRecentPropertyUpdateEvent(events.get());
645  EXPECT_TRUE(end_filter_event);
646  EXPECT_EQ(end_filters, end_filter_event->filters);
647  EXPECT_TRUE(end_filter_event->is_impl_only);
648}
649
650TEST(LayerAnimationControllerTest, ScrollOffsetTransition) {
651  FakeLayerAnimationValueObserver dummy_impl;
652  FakeLayerAnimationValueProvider dummy_provider_impl;
653  scoped_refptr<LayerAnimationController> controller_impl(
654      LayerAnimationController::Create(0));
655  controller_impl->AddValueObserver(&dummy_impl);
656  controller_impl->set_value_provider(&dummy_provider_impl);
657  scoped_ptr<AnimationEventsVector> events(
658      make_scoped_ptr(new AnimationEventsVector));
659  FakeLayerAnimationValueObserver dummy;
660  FakeLayerAnimationValueProvider dummy_provider;
661  scoped_refptr<LayerAnimationController> controller(
662      LayerAnimationController::Create(0));
663  controller->AddValueObserver(&dummy);
664  controller->set_value_provider(&dummy_provider);
665
666  gfx::Vector2dF initial_value(100.f, 300.f);
667  gfx::Vector2dF target_value(300.f, 200.f);
668  scoped_ptr<ScrollOffsetAnimationCurve> curve(
669      ScrollOffsetAnimationCurve::Create(
670          target_value,
671          EaseInOutTimingFunction::Create().Pass()));
672
673  scoped_ptr<Animation> animation(Animation::Create(
674      curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
675  animation->set_needs_synchronized_start_time(true);
676  controller->AddAnimation(animation.Pass());
677
678  dummy_provider_impl.set_scroll_offset(initial_value);
679  controller->PushAnimationUpdatesTo(controller_impl.get());
680  controller_impl->ActivateAnimations();
681  EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
682  double duration_in_seconds =
683      controller_impl->GetAnimation(Animation::ScrollOffset)
684          ->curve()
685          ->Duration();
686  TimeDelta duration = TimeDelta::FromMicroseconds(
687      duration_in_seconds * base::Time::kMicrosecondsPerSecond);
688  EXPECT_EQ(
689      duration_in_seconds,
690      controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
691
692  controller->Animate(kInitialTickTime);
693  controller->UpdateState(true, NULL);
694  EXPECT_TRUE(controller->HasActiveAnimation());
695  EXPECT_EQ(initial_value, dummy.scroll_offset());
696
697  controller_impl->Animate(kInitialTickTime);
698  controller_impl->UpdateState(true, events.get());
699  EXPECT_TRUE(controller_impl->HasActiveAnimation());
700  EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
701  // Scroll offset animations should not generate property updates.
702  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
703  EXPECT_FALSE(event);
704
705  controller->NotifyAnimationStarted((*events)[0]);
706  controller->Animate(kInitialTickTime + duration / 2);
707  controller->UpdateState(true, NULL);
708  EXPECT_TRUE(controller->HasActiveAnimation());
709  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
710
711  controller_impl->Animate(kInitialTickTime + duration / 2);
712  controller_impl->UpdateState(true, events.get());
713  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
714                      dummy_impl.scroll_offset());
715  event = GetMostRecentPropertyUpdateEvent(events.get());
716  EXPECT_FALSE(event);
717
718  controller_impl->Animate(kInitialTickTime + duration);
719  controller_impl->UpdateState(true, events.get());
720  EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
721  EXPECT_FALSE(controller_impl->HasActiveAnimation());
722  event = GetMostRecentPropertyUpdateEvent(events.get());
723  EXPECT_FALSE(event);
724
725  controller->Animate(kInitialTickTime + duration);
726  controller->UpdateState(true, NULL);
727  EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
728  EXPECT_FALSE(controller->HasActiveAnimation());
729}
730
731// Ensure that when the impl controller doesn't have a value provider,
732// the main-thread controller's value provider is used to obtain the intial
733// scroll offset.
734TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
735  FakeLayerAnimationValueObserver dummy_impl;
736  scoped_refptr<LayerAnimationController> controller_impl(
737      LayerAnimationController::Create(0));
738  controller_impl->AddValueObserver(&dummy_impl);
739  scoped_ptr<AnimationEventsVector> events(
740      make_scoped_ptr(new AnimationEventsVector));
741  FakeLayerAnimationValueObserver dummy;
742  FakeLayerAnimationValueProvider dummy_provider;
743  scoped_refptr<LayerAnimationController> controller(
744      LayerAnimationController::Create(0));
745  controller->AddValueObserver(&dummy);
746  controller->set_value_provider(&dummy_provider);
747
748  gfx::Vector2dF initial_value(500.f, 100.f);
749  gfx::Vector2dF target_value(300.f, 200.f);
750  scoped_ptr<ScrollOffsetAnimationCurve> curve(
751      ScrollOffsetAnimationCurve::Create(
752          target_value,
753          EaseInOutTimingFunction::Create().Pass()));
754
755  scoped_ptr<Animation> animation(Animation::Create(
756      curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
757  animation->set_needs_synchronized_start_time(true);
758  controller->AddAnimation(animation.Pass());
759
760  dummy_provider.set_scroll_offset(initial_value);
761  controller->PushAnimationUpdatesTo(controller_impl.get());
762  controller_impl->ActivateAnimations();
763  EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
764  double duration_in_seconds =
765      controller_impl->GetAnimation(Animation::ScrollOffset)
766          ->curve()
767          ->Duration();
768  EXPECT_EQ(
769      duration_in_seconds,
770      controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
771
772  controller->Animate(kInitialTickTime);
773  controller->UpdateState(true, NULL);
774  EXPECT_TRUE(controller->HasActiveAnimation());
775  EXPECT_EQ(initial_value, dummy.scroll_offset());
776
777  controller_impl->Animate(kInitialTickTime);
778  controller_impl->UpdateState(true, events.get());
779  EXPECT_TRUE(controller_impl->HasActiveAnimation());
780  EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
781  // Scroll offset animations should not generate property updates.
782  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
783  EXPECT_FALSE(event);
784
785  TimeDelta duration = TimeDelta::FromMicroseconds(
786      duration_in_seconds * base::Time::kMicrosecondsPerSecond);
787
788  controller->NotifyAnimationStarted((*events)[0]);
789  controller->Animate(kInitialTickTime + duration / 2);
790  controller->UpdateState(true, NULL);
791  EXPECT_TRUE(controller->HasActiveAnimation());
792  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
793
794  controller_impl->Animate(kInitialTickTime + duration / 2);
795  controller_impl->UpdateState(true, events.get());
796  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
797                      dummy_impl.scroll_offset());
798  event = GetMostRecentPropertyUpdateEvent(events.get());
799  EXPECT_FALSE(event);
800
801  controller_impl->Animate(kInitialTickTime + duration);
802  controller_impl->UpdateState(true, events.get());
803  EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
804  EXPECT_FALSE(controller_impl->HasActiveAnimation());
805  event = GetMostRecentPropertyUpdateEvent(events.get());
806  EXPECT_FALSE(event);
807
808  controller->Animate(kInitialTickTime + duration);
809  controller->UpdateState(true, NULL);
810  EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
811  EXPECT_FALSE(controller->HasActiveAnimation());
812}
813
814TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
815  FakeLayerAnimationValueObserver dummy_impl;
816  scoped_refptr<LayerAnimationController> controller_impl(
817      LayerAnimationController::Create(0));
818  controller_impl->AddValueObserver(&dummy_impl);
819  scoped_ptr<AnimationEventsVector> events(
820      make_scoped_ptr(new AnimationEventsVector));
821
822  gfx::Vector2dF initial_value(100.f, 300.f);
823  gfx::Vector2dF target_value(300.f, 200.f);
824  scoped_ptr<ScrollOffsetAnimationCurve> curve(
825      ScrollOffsetAnimationCurve::Create(
826          target_value,
827          EaseInOutTimingFunction::Create().Pass()));
828  curve->SetInitialValue(initial_value);
829  double duration_in_seconds = curve->Duration();
830
831  scoped_ptr<Animation> animation(Animation::Create(
832      curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
833  animation->set_is_impl_only(true);
834  controller_impl->AddAnimation(animation.Pass());
835
836  controller_impl->Animate(kInitialTickTime);
837  controller_impl->UpdateState(true, events.get());
838  EXPECT_TRUE(controller_impl->HasActiveAnimation());
839  EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
840  // Scroll offset animations should not generate property updates.
841  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
842  EXPECT_FALSE(event);
843
844  TimeDelta duration = TimeDelta::FromMicroseconds(
845      duration_in_seconds * base::Time::kMicrosecondsPerSecond);
846
847  controller_impl->Animate(kInitialTickTime + duration / 2);
848  controller_impl->UpdateState(true, events.get());
849  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
850                      dummy_impl.scroll_offset());
851  event = GetMostRecentPropertyUpdateEvent(events.get());
852  EXPECT_FALSE(event);
853
854  controller_impl->Animate(kInitialTickTime + duration);
855  controller_impl->UpdateState(true, events.get());
856  EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
857  EXPECT_FALSE(controller_impl->HasActiveAnimation());
858  event = GetMostRecentPropertyUpdateEvent(events.get());
859  EXPECT_FALSE(event);
860}
861
862class FakeAnimationDelegate : public AnimationDelegate {
863 public:
864  FakeAnimationDelegate()
865      : started_(false),
866        finished_(false) {}
867
868  virtual void NotifyAnimationStarted(
869      TimeTicks monotonic_time,
870      Animation::TargetProperty target_property) OVERRIDE {
871    started_ = true;
872  }
873
874  virtual void NotifyAnimationFinished(
875      TimeTicks monotonic_time,
876      Animation::TargetProperty target_property) OVERRIDE {
877    finished_ = true;
878  }
879
880  bool started() { return started_; }
881
882  bool finished() { return finished_; }
883
884 private:
885  bool started_;
886  bool finished_;
887};
888
889// Tests that impl-only animations lead to start and finished notifications
890// on the impl thread controller's animation delegate.
891TEST(LayerAnimationControllerTest,
892     NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate) {
893  FakeLayerAnimationValueObserver dummy_impl;
894  scoped_refptr<LayerAnimationController> controller_impl(
895      LayerAnimationController::Create(0));
896  controller_impl->AddValueObserver(&dummy_impl);
897  scoped_ptr<AnimationEventsVector> events(
898      make_scoped_ptr(new AnimationEventsVector));
899  FakeAnimationDelegate delegate;
900  controller_impl->set_layer_animation_delegate(&delegate);
901
902  scoped_ptr<Animation> to_add(CreateAnimation(
903      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
904      1,
905      Animation::Opacity));
906  to_add->set_is_impl_only(true);
907  controller_impl->AddAnimation(to_add.Pass());
908
909  EXPECT_FALSE(delegate.started());
910  EXPECT_FALSE(delegate.finished());
911
912  controller_impl->Animate(kInitialTickTime);
913  controller_impl->UpdateState(true, events.get());
914
915  EXPECT_TRUE(delegate.started());
916  EXPECT_FALSE(delegate.finished());
917
918  events.reset(new AnimationEventsVector);
919  controller_impl->Animate(kInitialTickTime +
920                           TimeDelta::FromMilliseconds(1000));
921  controller_impl->UpdateState(true, events.get());
922
923  EXPECT_TRUE(delegate.started());
924  EXPECT_TRUE(delegate.finished());
925}
926
927// Tests animations that are waiting for a synchronized start time do not
928// finish.
929TEST(LayerAnimationControllerTest,
930     AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
931  scoped_ptr<AnimationEventsVector> events(
932      make_scoped_ptr(new AnimationEventsVector));
933  FakeLayerAnimationValueObserver dummy;
934  scoped_refptr<LayerAnimationController> controller(
935      LayerAnimationController::Create(0));
936  controller->AddValueObserver(&dummy);
937
938  scoped_ptr<Animation> to_add(CreateAnimation(
939      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
940      1,
941      Animation::Opacity));
942  to_add->set_needs_synchronized_start_time(true);
943
944  // We should pause at the first keyframe indefinitely waiting for that
945  // animation to start.
946  controller->AddAnimation(to_add.Pass());
947  controller->Animate(kInitialTickTime);
948  controller->UpdateState(true, events.get());
949  EXPECT_TRUE(controller->HasActiveAnimation());
950  EXPECT_EQ(0.f, dummy.opacity());
951  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
952  controller->UpdateState(true, events.get());
953  EXPECT_TRUE(controller->HasActiveAnimation());
954  EXPECT_EQ(0.f, dummy.opacity());
955  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
956  controller->UpdateState(true, events.get());
957  EXPECT_TRUE(controller->HasActiveAnimation());
958  EXPECT_EQ(0.f, dummy.opacity());
959
960  // Send the synchronized start time.
961  controller->NotifyAnimationStarted(
962      AnimationEvent(AnimationEvent::Started,
963                     0,
964                     1,
965                     Animation::Opacity,
966                     kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
967  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
968  controller->UpdateState(true, events.get());
969  EXPECT_EQ(1.f, dummy.opacity());
970  EXPECT_FALSE(controller->HasActiveAnimation());
971}
972
973// Tests that two queued animations affecting the same property run in sequence.
974TEST(LayerAnimationControllerTest, TrivialQueuing) {
975  scoped_ptr<AnimationEventsVector> events(
976      make_scoped_ptr(new AnimationEventsVector));
977  FakeLayerAnimationValueObserver dummy;
978  scoped_refptr<LayerAnimationController> controller(
979      LayerAnimationController::Create(0));
980  controller->AddValueObserver(&dummy);
981
982  EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
983
984  controller->AddAnimation(CreateAnimation(
985      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
986      1,
987      Animation::Opacity));
988  controller->AddAnimation(CreateAnimation(
989      scoped_ptr<AnimationCurve>(
990          new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
991      2,
992      Animation::Opacity));
993
994  EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
995
996  controller->Animate(kInitialTickTime);
997
998  // The second animation still needs to be started.
999  EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1000
1001  controller->UpdateState(true, events.get());
1002  EXPECT_TRUE(controller->HasActiveAnimation());
1003  EXPECT_EQ(0.f, dummy.opacity());
1004
1005  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1006  EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
1007  controller->UpdateState(true, events.get());
1008  EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
1009
1010  EXPECT_TRUE(controller->HasActiveAnimation());
1011  EXPECT_EQ(1.f, dummy.opacity());
1012  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1013  controller->UpdateState(true, events.get());
1014  EXPECT_EQ(0.5f, dummy.opacity());
1015  EXPECT_FALSE(controller->HasActiveAnimation());
1016}
1017
1018// Tests interrupting a transition with another transition.
1019TEST(LayerAnimationControllerTest, Interrupt) {
1020  scoped_ptr<AnimationEventsVector> events(
1021      make_scoped_ptr(new AnimationEventsVector));
1022  FakeLayerAnimationValueObserver dummy;
1023  scoped_refptr<LayerAnimationController> controller(
1024      LayerAnimationController::Create(0));
1025  controller->AddValueObserver(&dummy);
1026  controller->AddAnimation(CreateAnimation(
1027      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1028      1,
1029      Animation::Opacity));
1030  controller->Animate(kInitialTickTime);
1031  controller->UpdateState(true, events.get());
1032  EXPECT_TRUE(controller->HasActiveAnimation());
1033  EXPECT_EQ(0.f, dummy.opacity());
1034
1035  scoped_ptr<Animation> to_add(CreateAnimation(
1036      scoped_ptr<AnimationCurve>(
1037          new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1038      2,
1039      Animation::Opacity));
1040  controller->AbortAnimations(Animation::Opacity);
1041  controller->AddAnimation(to_add.Pass());
1042
1043  // Since the previous animation was aborted, the new animation should start
1044  // right in this call to animate.
1045  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1046  controller->UpdateState(true, events.get());
1047  EXPECT_TRUE(controller->HasActiveAnimation());
1048  EXPECT_EQ(1.f, dummy.opacity());
1049  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1050  controller->UpdateState(true, events.get());
1051  EXPECT_EQ(0.5f, dummy.opacity());
1052  EXPECT_FALSE(controller->HasActiveAnimation());
1053}
1054
1055// Tests scheduling two animations to run together when only one property is
1056// free.
1057TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
1058  scoped_ptr<AnimationEventsVector> events(
1059      make_scoped_ptr(new AnimationEventsVector));
1060  FakeLayerAnimationValueObserver dummy;
1061  scoped_refptr<LayerAnimationController> controller(
1062      LayerAnimationController::Create(0));
1063  controller->AddValueObserver(&dummy);
1064
1065  controller->AddAnimation(CreateAnimation(
1066      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1067      1,
1068      Animation::Transform));
1069  controller->AddAnimation(CreateAnimation(
1070      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1071      2,
1072      Animation::Transform));
1073  controller->AddAnimation(CreateAnimation(
1074      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1075      2,
1076      Animation::Opacity));
1077
1078  controller->Animate(kInitialTickTime);
1079  controller->UpdateState(true, events.get());
1080  EXPECT_EQ(0.f, dummy.opacity());
1081  EXPECT_TRUE(controller->HasActiveAnimation());
1082  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1083  controller->UpdateState(true, events.get());
1084  // Should not have started the float transition yet.
1085  EXPECT_TRUE(controller->HasActiveAnimation());
1086  EXPECT_EQ(0.f, dummy.opacity());
1087  // The float animation should have started at time 1 and should be done.
1088  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1089  controller->UpdateState(true, events.get());
1090  EXPECT_EQ(1.f, dummy.opacity());
1091  EXPECT_FALSE(controller->HasActiveAnimation());
1092}
1093
1094// Tests scheduling two animations to run together with different lengths and
1095// another animation queued to start when the shorter animation finishes (should
1096// wait for both to finish).
1097TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
1098  scoped_ptr<AnimationEventsVector> events(
1099      make_scoped_ptr(new AnimationEventsVector));
1100  FakeLayerAnimationValueObserver dummy;
1101  scoped_refptr<LayerAnimationController> controller(
1102      LayerAnimationController::Create(0));
1103  controller->AddValueObserver(&dummy);
1104
1105  controller->AddAnimation(CreateAnimation(
1106      scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
1107      1,
1108      Animation::Transform));
1109  controller->AddAnimation(CreateAnimation(
1110      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1111      1,
1112      Animation::Opacity));
1113  controller->AddAnimation(CreateAnimation(
1114      scoped_ptr<AnimationCurve>(
1115          new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1116      2,
1117      Animation::Opacity));
1118
1119  // Animations with id 1 should both start now.
1120  controller->Animate(kInitialTickTime);
1121  controller->UpdateState(true, events.get());
1122  EXPECT_TRUE(controller->HasActiveAnimation());
1123  EXPECT_EQ(0.f, dummy.opacity());
1124  // The opacity animation should have finished at time 1, but the group
1125  // of animations with id 1 don't finish until time 2 because of the length
1126  // of the transform animation.
1127  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1128  controller->UpdateState(true, events.get());
1129  // Should not have started the float transition yet.
1130  EXPECT_TRUE(controller->HasActiveAnimation());
1131  EXPECT_EQ(1.f, dummy.opacity());
1132
1133  // The second opacity animation should start at time 2 and should be done by
1134  // time 3.
1135  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1136  controller->UpdateState(true, events.get());
1137  EXPECT_EQ(0.5f, dummy.opacity());
1138  EXPECT_FALSE(controller->HasActiveAnimation());
1139}
1140
1141// Test that a looping animation loops and for the correct number of iterations.
1142TEST(LayerAnimationControllerTest, TrivialLooping) {
1143  scoped_ptr<AnimationEventsVector> events(
1144      make_scoped_ptr(new AnimationEventsVector));
1145  FakeLayerAnimationValueObserver dummy;
1146  scoped_refptr<LayerAnimationController> controller(
1147      LayerAnimationController::Create(0));
1148  controller->AddValueObserver(&dummy);
1149
1150  scoped_ptr<Animation> to_add(CreateAnimation(
1151      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1152      1,
1153      Animation::Opacity));
1154  to_add->set_iterations(3);
1155  controller->AddAnimation(to_add.Pass());
1156
1157  controller->Animate(kInitialTickTime);
1158  controller->UpdateState(true, events.get());
1159  EXPECT_TRUE(controller->HasActiveAnimation());
1160  EXPECT_EQ(0.f, dummy.opacity());
1161  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1162  controller->UpdateState(true, events.get());
1163  EXPECT_TRUE(controller->HasActiveAnimation());
1164  EXPECT_EQ(0.25f, dummy.opacity());
1165  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1166  controller->UpdateState(true, events.get());
1167  EXPECT_TRUE(controller->HasActiveAnimation());
1168  EXPECT_EQ(0.75f, dummy.opacity());
1169  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
1170  controller->UpdateState(true, events.get());
1171  EXPECT_TRUE(controller->HasActiveAnimation());
1172  EXPECT_EQ(0.25f, dummy.opacity());
1173  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
1174  controller->UpdateState(true, events.get());
1175  EXPECT_TRUE(controller->HasActiveAnimation());
1176  EXPECT_EQ(0.75f, dummy.opacity());
1177  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1178  controller->UpdateState(true, events.get());
1179  EXPECT_FALSE(controller->HasActiveAnimation());
1180  EXPECT_EQ(1.f, dummy.opacity());
1181
1182  // Just be extra sure.
1183  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
1184  controller->UpdateState(true, events.get());
1185  EXPECT_EQ(1.f, dummy.opacity());
1186}
1187
1188// Test that an infinitely looping animation does indeed go until aborted.
1189TEST(LayerAnimationControllerTest, InfiniteLooping) {
1190  scoped_ptr<AnimationEventsVector> events(
1191      make_scoped_ptr(new AnimationEventsVector));
1192  FakeLayerAnimationValueObserver dummy;
1193  scoped_refptr<LayerAnimationController> controller(
1194      LayerAnimationController::Create(0));
1195  controller->AddValueObserver(&dummy);
1196
1197  const int id = 1;
1198  scoped_ptr<Animation> to_add(CreateAnimation(
1199      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1200      id,
1201      Animation::Opacity));
1202  to_add->set_iterations(-1);
1203  controller->AddAnimation(to_add.Pass());
1204
1205  controller->Animate(kInitialTickTime);
1206  controller->UpdateState(true, events.get());
1207  EXPECT_TRUE(controller->HasActiveAnimation());
1208  EXPECT_EQ(0.f, dummy.opacity());
1209  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1210  controller->UpdateState(true, events.get());
1211  EXPECT_TRUE(controller->HasActiveAnimation());
1212  EXPECT_EQ(0.25f, dummy.opacity());
1213  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1214  controller->UpdateState(true, events.get());
1215  EXPECT_TRUE(controller->HasActiveAnimation());
1216  EXPECT_EQ(0.75f, dummy.opacity());
1217
1218  controller->Animate(kInitialTickTime +
1219                      TimeDelta::FromMilliseconds(1073741824250));
1220  controller->UpdateState(true, events.get());
1221  EXPECT_TRUE(controller->HasActiveAnimation());
1222  EXPECT_EQ(0.25f, dummy.opacity());
1223  controller->Animate(kInitialTickTime +
1224                      TimeDelta::FromMilliseconds(1073741824750));
1225  controller->UpdateState(true, events.get());
1226  EXPECT_TRUE(controller->HasActiveAnimation());
1227  EXPECT_EQ(0.75f, dummy.opacity());
1228
1229  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1230  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1231      Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(750));
1232  EXPECT_FALSE(controller->HasActiveAnimation());
1233  EXPECT_EQ(0.75f, dummy.opacity());
1234}
1235
1236// Test that pausing and resuming work as expected.
1237TEST(LayerAnimationControllerTest, PauseResume) {
1238  scoped_ptr<AnimationEventsVector> events(
1239      make_scoped_ptr(new AnimationEventsVector));
1240  FakeLayerAnimationValueObserver dummy;
1241  scoped_refptr<LayerAnimationController> controller(
1242      LayerAnimationController::Create(0));
1243  controller->AddValueObserver(&dummy);
1244
1245  const int id = 1;
1246  controller->AddAnimation(CreateAnimation(
1247      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1248      id,
1249      Animation::Opacity));
1250
1251  controller->Animate(kInitialTickTime);
1252  controller->UpdateState(true, events.get());
1253  EXPECT_TRUE(controller->HasActiveAnimation());
1254  EXPECT_EQ(0.f, dummy.opacity());
1255  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1256  controller->UpdateState(true, events.get());
1257  EXPECT_TRUE(controller->HasActiveAnimation());
1258  EXPECT_EQ(0.5f, dummy.opacity());
1259
1260  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1261  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1262      Animation::Paused, kInitialTickTime + TimeDelta::FromMilliseconds(500));
1263
1264  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1265  controller->UpdateState(true, events.get());
1266  EXPECT_TRUE(controller->HasActiveAnimation());
1267  EXPECT_EQ(0.5f, dummy.opacity());
1268
1269  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1270  controller->GetAnimation(id, Animation::Opacity)
1271      ->SetRunState(Animation::Running,
1272                    kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1273  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
1274  controller->UpdateState(true, events.get());
1275  EXPECT_TRUE(controller->HasActiveAnimation());
1276  EXPECT_EQ(0.75f, dummy.opacity());
1277
1278  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
1279  controller->UpdateState(true, events.get());
1280  EXPECT_FALSE(controller->HasActiveAnimation());
1281  EXPECT_EQ(1.f, dummy.opacity());
1282}
1283
1284TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1285  scoped_ptr<AnimationEventsVector> events(
1286      make_scoped_ptr(new AnimationEventsVector));
1287  FakeLayerAnimationValueObserver dummy;
1288  scoped_refptr<LayerAnimationController> controller(
1289      LayerAnimationController::Create(0));
1290  controller->AddValueObserver(&dummy);
1291
1292  const int id = 1;
1293  controller->AddAnimation(CreateAnimation(
1294      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1295      id,
1296      Animation::Transform));
1297  controller->AddAnimation(CreateAnimation(
1298      scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1299      id,
1300      Animation::Opacity));
1301  controller->AddAnimation(CreateAnimation(
1302      scoped_ptr<AnimationCurve>(
1303          new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
1304      2,
1305      Animation::Opacity));
1306
1307  controller->Animate(kInitialTickTime);
1308  controller->UpdateState(true, events.get());
1309  EXPECT_TRUE(controller->HasActiveAnimation());
1310  EXPECT_EQ(0.f, dummy.opacity());
1311  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1312  controller->UpdateState(true, events.get());
1313  EXPECT_TRUE(controller->HasActiveAnimation());
1314  EXPECT_EQ(0.5f, dummy.opacity());
1315
1316  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1317  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1318      Animation::Aborted, kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1319  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1320  controller->UpdateState(true, events.get());
1321  EXPECT_TRUE(controller->HasActiveAnimation());
1322  EXPECT_EQ(1.f, dummy.opacity());
1323  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1324  controller->UpdateState(true, events.get());
1325  EXPECT_TRUE(!controller->HasActiveAnimation());
1326  EXPECT_EQ(0.75f, dummy.opacity());
1327}
1328
1329TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1330  FakeLayerAnimationValueObserver dummy_impl;
1331  scoped_refptr<LayerAnimationController> controller_impl(
1332      LayerAnimationController::Create(0));
1333  controller_impl->AddValueObserver(&dummy_impl);
1334  scoped_ptr<AnimationEventsVector> events(
1335      make_scoped_ptr(new AnimationEventsVector));
1336  FakeLayerAnimationValueObserver dummy;
1337  scoped_refptr<LayerAnimationController> controller(
1338      LayerAnimationController::Create(0));
1339  controller->AddValueObserver(&dummy);
1340
1341  scoped_ptr<Animation> to_add(CreateAnimation(
1342      scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1343      0,
1344      Animation::Opacity));
1345  to_add->set_needs_synchronized_start_time(true);
1346  controller->AddAnimation(to_add.Pass());
1347
1348  controller->Animate(kInitialTickTime);
1349  controller->UpdateState(true, events.get());
1350  EXPECT_TRUE(controller->HasActiveAnimation());
1351  Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
1352  EXPECT_TRUE(active_animation);
1353  EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1354
1355  controller->PushAnimationUpdatesTo(controller_impl.get());
1356  controller_impl->ActivateAnimations();
1357
1358  active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
1359  EXPECT_TRUE(active_animation);
1360  EXPECT_EQ(Animation::WaitingForTargetAvailability,
1361            active_animation->run_state());
1362}
1363
1364// Tests that skipping a call to UpdateState works as expected.
1365TEST(LayerAnimationControllerTest, SkipUpdateState) {
1366  scoped_ptr<AnimationEventsVector> events(
1367      make_scoped_ptr(new AnimationEventsVector));
1368  FakeLayerAnimationValueObserver dummy;
1369  scoped_refptr<LayerAnimationController> controller(
1370      LayerAnimationController::Create(0));
1371  controller->AddValueObserver(&dummy);
1372
1373  controller->AddAnimation(CreateAnimation(
1374      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1375      1,
1376      Animation::Transform));
1377
1378  controller->Animate(kInitialTickTime);
1379  controller->UpdateState(true, events.get());
1380
1381  controller->AddAnimation(CreateAnimation(
1382      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1383      2,
1384      Animation::Opacity));
1385
1386  // Animate but don't UpdateState.
1387  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1388
1389  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1390  events.reset(new AnimationEventsVector);
1391  controller->UpdateState(true, events.get());
1392
1393  // Should have one Started event and one Finished event.
1394  EXPECT_EQ(2u, events->size());
1395  EXPECT_NE((*events)[0].type, (*events)[1].type);
1396
1397  // The float transition should still be at its starting point.
1398  EXPECT_TRUE(controller->HasActiveAnimation());
1399  EXPECT_EQ(0.f, dummy.opacity());
1400
1401  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1402  controller->UpdateState(true, events.get());
1403
1404  // The float tranisition should now be done.
1405  EXPECT_EQ(1.f, dummy.opacity());
1406  EXPECT_FALSE(controller->HasActiveAnimation());
1407}
1408
1409// Tests that an animation controller with only a pending observer gets ticked
1410// but doesn't progress animations past the Starting state.
1411TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1412  scoped_ptr<AnimationEventsVector> events(
1413      make_scoped_ptr(new AnimationEventsVector));
1414  FakeLayerAnimationValueObserver dummy;
1415  FakeInactiveLayerAnimationValueObserver pending_dummy;
1416  scoped_refptr<LayerAnimationController> controller(
1417      LayerAnimationController::Create(0));
1418
1419  const int id = 1;
1420  controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>(
1421      new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(),
1422      id,
1423      Animation::Opacity));
1424
1425  // Without an observer, the animation shouldn't progress to the Starting
1426  // state.
1427  controller->Animate(kInitialTickTime);
1428  controller->UpdateState(true, events.get());
1429  EXPECT_EQ(0u, events->size());
1430  EXPECT_EQ(Animation::WaitingForTargetAvailability,
1431            controller->GetAnimation(id, Animation::Opacity)->run_state());
1432
1433  controller->AddValueObserver(&pending_dummy);
1434
1435  // With only a pending observer, the animation should progress to the
1436  // Starting state and get ticked at its starting point, but should not
1437  // progress to Running.
1438  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1439  controller->UpdateState(true, events.get());
1440  EXPECT_EQ(0u, events->size());
1441  EXPECT_EQ(Animation::Starting,
1442            controller->GetAnimation(id, Animation::Opacity)->run_state());
1443  EXPECT_EQ(0.5f, pending_dummy.opacity());
1444
1445  // Even when already in the Starting state, the animation should stay
1446  // there, and shouldn't be ticked past its starting point.
1447  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1448  controller->UpdateState(true, events.get());
1449  EXPECT_EQ(0u, events->size());
1450  EXPECT_EQ(Animation::Starting,
1451            controller->GetAnimation(id, Animation::Opacity)->run_state());
1452  EXPECT_EQ(0.5f, pending_dummy.opacity());
1453
1454  controller->AddValueObserver(&dummy);
1455
1456  // Now that an active observer has been added, the animation should still
1457  // initially tick at its starting point, but should now progress to Running.
1458  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1459  controller->UpdateState(true, events.get());
1460  EXPECT_EQ(1u, events->size());
1461  EXPECT_EQ(Animation::Running,
1462            controller->GetAnimation(id, Animation::Opacity)->run_state());
1463  EXPECT_EQ(0.5f, pending_dummy.opacity());
1464  EXPECT_EQ(0.5f, dummy.opacity());
1465
1466  // The animation should now tick past its starting point.
1467  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
1468  EXPECT_NE(0.5f, pending_dummy.opacity());
1469  EXPECT_NE(0.5f, dummy.opacity());
1470}
1471
1472TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
1473  scoped_refptr<LayerAnimationController> controller_impl(
1474      LayerAnimationController::Create(0));
1475
1476  scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1477      KeyframedTransformAnimationCurve::Create());
1478
1479  TransformOperations operations1;
1480  curve1->AddKeyframe(TransformKeyframe::Create(
1481      0.0, operations1, scoped_ptr<TimingFunction>()));
1482  operations1.AppendTranslate(10.0, 15.0, 0.0);
1483  curve1->AddKeyframe(TransformKeyframe::Create(
1484      1.0, operations1, scoped_ptr<TimingFunction>()));
1485
1486  scoped_ptr<Animation> animation(Animation::Create(
1487      curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1488  controller_impl->AddAnimation(animation.Pass());
1489
1490  scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1491      KeyframedTransformAnimationCurve::Create());
1492
1493  TransformOperations operations2;
1494  curve2->AddKeyframe(TransformKeyframe::Create(
1495      0.0, operations2, scoped_ptr<TimingFunction>()));
1496  operations2.AppendScale(2.0, 3.0, 4.0);
1497  curve2->AddKeyframe(TransformKeyframe::Create(
1498      1.0, operations2, scoped_ptr<TimingFunction>()));
1499
1500  animation = Animation::Create(
1501      curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
1502  controller_impl->AddAnimation(animation.Pass());
1503
1504  gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1505  gfx::BoxF bounds;
1506
1507  EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1508  EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1509            bounds.ToString());
1510
1511  controller_impl->GetAnimation(1, Animation::Transform)
1512      ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1513
1514  // Only the unfinished animation should affect the animated bounds.
1515  EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1516  EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1517            bounds.ToString());
1518
1519  controller_impl->GetAnimation(2, Animation::Transform)
1520      ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1521
1522  // There are no longer any running animations.
1523  EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
1524
1525  // Add an animation whose bounds we don't yet support computing.
1526  scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1527      KeyframedTransformAnimationCurve::Create());
1528  TransformOperations operations3;
1529  gfx::Transform transform3;
1530  transform3.Scale3d(1.0, 2.0, 3.0);
1531  curve3->AddKeyframe(TransformKeyframe::Create(
1532      0.0, operations3, scoped_ptr<TimingFunction>()));
1533  operations3.AppendMatrix(transform3);
1534  curve3->AddKeyframe(TransformKeyframe::Create(
1535      1.0, operations3, scoped_ptr<TimingFunction>()));
1536  animation = Animation::Create(
1537      curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1538  controller_impl->AddAnimation(animation.Pass());
1539  EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1540}
1541
1542// Tests that AbortAnimations aborts all animations targeting the specified
1543// property.
1544TEST(LayerAnimationControllerTest, AbortAnimations) {
1545  FakeLayerAnimationValueObserver dummy;
1546  scoped_refptr<LayerAnimationController> controller(
1547      LayerAnimationController::Create(0));
1548  controller->AddValueObserver(&dummy);
1549
1550  // Start with several animations, and allow some of them to reach the finished
1551  // state.
1552  controller->AddAnimation(CreateAnimation(
1553      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1554      1,
1555      Animation::Transform));
1556  controller->AddAnimation(CreateAnimation(
1557      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1558      2,
1559      Animation::Opacity));
1560  controller->AddAnimation(CreateAnimation(
1561      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1562      3,
1563      Animation::Transform));
1564  controller->AddAnimation(CreateAnimation(
1565      scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1566      4,
1567      Animation::Transform));
1568  controller->AddAnimation(CreateAnimation(
1569      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1570      5,
1571      Animation::Opacity));
1572
1573  controller->Animate(kInitialTickTime);
1574  controller->UpdateState(true, NULL);
1575  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1576  controller->UpdateState(true, NULL);
1577
1578  EXPECT_EQ(Animation::Finished,
1579            controller->GetAnimation(1, Animation::Transform)->run_state());
1580  EXPECT_EQ(Animation::Finished,
1581            controller->GetAnimation(2, Animation::Opacity)->run_state());
1582  EXPECT_EQ(Animation::Running,
1583            controller->GetAnimation(3, Animation::Transform)->run_state());
1584  EXPECT_EQ(Animation::WaitingForTargetAvailability,
1585            controller->GetAnimation(4, Animation::Transform)->run_state());
1586  EXPECT_EQ(Animation::Running,
1587            controller->GetAnimation(5, Animation::Opacity)->run_state());
1588
1589  controller->AbortAnimations(Animation::Transform);
1590
1591  // Only un-finished Transform animations should have been aborted.
1592  EXPECT_EQ(Animation::Finished,
1593            controller->GetAnimation(1, Animation::Transform)->run_state());
1594  EXPECT_EQ(Animation::Finished,
1595            controller->GetAnimation(2, Animation::Opacity)->run_state());
1596  EXPECT_EQ(Animation::Aborted,
1597            controller->GetAnimation(3, Animation::Transform)->run_state());
1598  EXPECT_EQ(Animation::Aborted,
1599            controller->GetAnimation(4, Animation::Transform)->run_state());
1600  EXPECT_EQ(Animation::Running,
1601            controller->GetAnimation(5, Animation::Opacity)->run_state());
1602}
1603
1604// An animation aborted on the main thread should get deleted on both threads.
1605TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
1606  FakeLayerAnimationValueObserver dummy_impl;
1607  scoped_refptr<LayerAnimationController> controller_impl(
1608      LayerAnimationController::Create(0));
1609  controller_impl->AddValueObserver(&dummy_impl);
1610  FakeLayerAnimationValueObserver dummy;
1611  scoped_refptr<LayerAnimationController> controller(
1612      LayerAnimationController::Create(0));
1613  controller->AddValueObserver(&dummy);
1614
1615  AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1616  int group_id = controller->GetAnimation(Animation::Opacity)->group();
1617
1618  controller->PushAnimationUpdatesTo(controller_impl.get());
1619  controller_impl->ActivateAnimations();
1620  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1621
1622  controller->AbortAnimations(Animation::Opacity);
1623  EXPECT_EQ(Animation::Aborted,
1624            controller->GetAnimation(Animation::Opacity)->run_state());
1625  EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1626  EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1627
1628  controller->Animate(kInitialTickTime);
1629  controller->UpdateState(true, NULL);
1630  EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1631  EXPECT_EQ(Animation::WaitingForDeletion,
1632            controller->GetAnimation(Animation::Opacity)->run_state());
1633
1634  controller->PushAnimationUpdatesTo(controller_impl.get());
1635  controller_impl->ActivateAnimations();
1636  EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1637  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1638}
1639
1640// An animation aborted on the impl thread should get deleted on both threads.
1641TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
1642  FakeLayerAnimationValueObserver dummy_impl;
1643  scoped_refptr<LayerAnimationController> controller_impl(
1644      LayerAnimationController::Create(0));
1645  controller_impl->AddValueObserver(&dummy_impl);
1646  FakeLayerAnimationValueObserver dummy;
1647  scoped_refptr<LayerAnimationController> controller(
1648      LayerAnimationController::Create(0));
1649  controller->AddValueObserver(&dummy);
1650
1651  AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1652  int group_id = controller->GetAnimation(Animation::Opacity)->group();
1653
1654  controller->PushAnimationUpdatesTo(controller_impl.get());
1655  controller_impl->ActivateAnimations();
1656  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1657
1658  controller_impl->AbortAnimations(Animation::Opacity);
1659  EXPECT_EQ(Animation::Aborted,
1660            controller_impl->GetAnimation(Animation::Opacity)->run_state());
1661  EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1662  EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1663
1664  AnimationEventsVector events;
1665  controller_impl->Animate(kInitialTickTime);
1666  controller_impl->UpdateState(true, &events);
1667  EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
1668  EXPECT_EQ(1u, events.size());
1669  EXPECT_EQ(AnimationEvent::Aborted, events[0].type);
1670  EXPECT_EQ(Animation::WaitingForDeletion,
1671            controller_impl->GetAnimation(Animation::Opacity)->run_state());
1672
1673  controller->NotifyAnimationAborted(events[0]);
1674  EXPECT_EQ(Animation::Aborted,
1675            controller->GetAnimation(Animation::Opacity)->run_state());
1676
1677  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1678  controller->UpdateState(true, NULL);
1679  EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1680  EXPECT_EQ(Animation::WaitingForDeletion,
1681            controller->GetAnimation(Animation::Opacity)->run_state());
1682
1683  controller->PushAnimationUpdatesTo(controller_impl.get());
1684  controller_impl->ActivateAnimations();
1685  EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1686  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1687}
1688
1689// Ensure that we only generate Finished events for animations in a group
1690// once all animations in that group are finished.
1691TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
1692  scoped_ptr<AnimationEventsVector> events(
1693      make_scoped_ptr(new AnimationEventsVector));
1694  FakeLayerAnimationValueObserver dummy_impl;
1695  scoped_refptr<LayerAnimationController> controller_impl(
1696      LayerAnimationController::Create(0));
1697  controller_impl->AddValueObserver(&dummy_impl);
1698
1699  // Add two animations with the same group id but different durations.
1700  controller_impl->AddAnimation(CreateAnimation(
1701      scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1702      1,
1703      Animation::Transform));
1704  controller_impl->AddAnimation(CreateAnimation(
1705      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1706      1,
1707      Animation::Opacity));
1708
1709  controller_impl->Animate(kInitialTickTime);
1710  controller_impl->UpdateState(true, events.get());
1711
1712  // Both animations should have started.
1713  EXPECT_EQ(2u, events->size());
1714  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1715  EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1716
1717  events.reset(new AnimationEventsVector);
1718  controller_impl->Animate(kInitialTickTime +
1719                           TimeDelta::FromMilliseconds(1000));
1720  controller_impl->UpdateState(true, events.get());
1721
1722  // The opacity animation should be finished, but should not have generated
1723  // a Finished event yet.
1724  EXPECT_EQ(0u, events->size());
1725  EXPECT_EQ(Animation::Finished,
1726            controller_impl->GetAnimation(1, Animation::Opacity)->run_state());
1727  EXPECT_EQ(Animation::Running,
1728            controller_impl->GetAnimation(1,
1729                                          Animation::Transform)->run_state());
1730
1731  controller_impl->Animate(kInitialTickTime +
1732                           TimeDelta::FromMilliseconds(2000));
1733  controller_impl->UpdateState(true, events.get());
1734
1735  // Both animations should have generated Finished events.
1736  EXPECT_EQ(2u, events->size());
1737  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1738  EXPECT_EQ(AnimationEvent::Finished, (*events)[1].type);
1739}
1740
1741// Ensure that when a group has a mix of aborted and finished animations,
1742// we generate a Finished event for the finished animation and an Aborted
1743// event for the aborted animation.
1744TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
1745  scoped_ptr<AnimationEventsVector> events(
1746      make_scoped_ptr(new AnimationEventsVector));
1747  FakeLayerAnimationValueObserver dummy_impl;
1748  scoped_refptr<LayerAnimationController> controller_impl(
1749      LayerAnimationController::Create(0));
1750  controller_impl->AddValueObserver(&dummy_impl);
1751
1752  // Add two animations with the same group id.
1753  controller_impl->AddAnimation(CreateAnimation(
1754      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1755      1,
1756      Animation::Transform));
1757  controller_impl->AddAnimation(CreateAnimation(
1758      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1759      1,
1760      Animation::Opacity));
1761
1762  controller_impl->Animate(kInitialTickTime);
1763  controller_impl->UpdateState(true, events.get());
1764
1765  // Both animations should have started.
1766  EXPECT_EQ(2u, events->size());
1767  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1768  EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1769
1770  controller_impl->AbortAnimations(Animation::Opacity);
1771
1772  events.reset(new AnimationEventsVector);
1773  controller_impl->Animate(kInitialTickTime +
1774                           TimeDelta::FromMilliseconds(1000));
1775  controller_impl->UpdateState(true, events.get());
1776
1777  // We should have exactly 2 events: a Finished event for the tranform
1778  // animation, and an Aborted event for the opacity animation.
1779  EXPECT_EQ(2u, events->size());
1780  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1781  EXPECT_EQ(Animation::Transform, (*events)[0].target_property);
1782  EXPECT_EQ(AnimationEvent::Aborted, (*events)[1].type);
1783  EXPECT_EQ(Animation::Opacity, (*events)[1].target_property);
1784}
1785
1786TEST(LayerAnimationControllerTest, HasAnimationThatAffectsScale) {
1787  scoped_refptr<LayerAnimationController> controller_impl(
1788      LayerAnimationController::Create(0));
1789
1790  EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1791
1792  controller_impl->AddAnimation(CreateAnimation(
1793      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1794      1,
1795      Animation::Opacity));
1796
1797  // Opacity animations don't affect scale.
1798  EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1799
1800  scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1801      KeyframedTransformAnimationCurve::Create());
1802
1803  TransformOperations operations1;
1804  curve1->AddKeyframe(TransformKeyframe::Create(
1805      0.0, operations1, scoped_ptr<TimingFunction>()));
1806  operations1.AppendTranslate(10.0, 15.0, 0.0);
1807  curve1->AddKeyframe(TransformKeyframe::Create(
1808      1.0, operations1, scoped_ptr<TimingFunction>()));
1809
1810  scoped_ptr<Animation> animation(Animation::Create(
1811      curve1.PassAs<AnimationCurve>(), 2, 2, Animation::Transform));
1812  controller_impl->AddAnimation(animation.Pass());
1813
1814  // Translations don't affect scale.
1815  EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1816
1817  scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1818      KeyframedTransformAnimationCurve::Create());
1819
1820  TransformOperations operations2;
1821  curve2->AddKeyframe(TransformKeyframe::Create(
1822      0.0, operations2, scoped_ptr<TimingFunction>()));
1823  operations2.AppendScale(2.0, 3.0, 4.0);
1824  curve2->AddKeyframe(TransformKeyframe::Create(
1825      1.0, operations2, scoped_ptr<TimingFunction>()));
1826
1827  animation = Animation::Create(
1828      curve2.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1829  controller_impl->AddAnimation(animation.Pass());
1830
1831  EXPECT_TRUE(controller_impl->HasAnimationThatAffectsScale());
1832
1833  controller_impl->GetAnimation(3, Animation::Transform)
1834      ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1835
1836  // Only unfinished animations should be considered by
1837  // HasAnimationThatAffectsScale.
1838  EXPECT_FALSE(controller_impl->HasAnimationThatAffectsScale());
1839}
1840
1841TEST(LayerAnimationControllerTest, HasOnlyTranslationTransforms) {
1842  scoped_refptr<LayerAnimationController> controller_impl(
1843      LayerAnimationController::Create(0));
1844
1845  EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1846
1847  controller_impl->AddAnimation(CreateAnimation(
1848      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1849      1,
1850      Animation::Opacity));
1851
1852  // Opacity animations aren't non-translation transforms.
1853  EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1854
1855  scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1856      KeyframedTransformAnimationCurve::Create());
1857
1858  TransformOperations operations1;
1859  curve1->AddKeyframe(TransformKeyframe::Create(
1860      0.0, operations1, scoped_ptr<TimingFunction>()));
1861  operations1.AppendTranslate(10.0, 15.0, 0.0);
1862  curve1->AddKeyframe(TransformKeyframe::Create(
1863      1.0, operations1, scoped_ptr<TimingFunction>()));
1864
1865  scoped_ptr<Animation> animation(Animation::Create(
1866      curve1.PassAs<AnimationCurve>(), 2, 2, Animation::Transform));
1867  controller_impl->AddAnimation(animation.Pass());
1868
1869  // The only transform animation we've added is a translation.
1870  EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1871
1872  scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1873      KeyframedTransformAnimationCurve::Create());
1874
1875  TransformOperations operations2;
1876  curve2->AddKeyframe(TransformKeyframe::Create(
1877      0.0, operations2, scoped_ptr<TimingFunction>()));
1878  operations2.AppendScale(2.0, 3.0, 4.0);
1879  curve2->AddKeyframe(TransformKeyframe::Create(
1880      1.0, operations2, scoped_ptr<TimingFunction>()));
1881
1882  animation = Animation::Create(
1883      curve2.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1884  controller_impl->AddAnimation(animation.Pass());
1885
1886  // A scale animation is not a translation.
1887  EXPECT_FALSE(controller_impl->HasOnlyTranslationTransforms());
1888
1889  controller_impl->GetAnimation(3, Animation::Transform)
1890      ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1891
1892  // Only unfinished animations should be considered by
1893  // HasOnlyTranslationTransforms.
1894  EXPECT_TRUE(controller_impl->HasOnlyTranslationTransforms());
1895}
1896
1897TEST(LayerAnimationControllerTest, MaximumTargetScale) {
1898  scoped_refptr<LayerAnimationController> controller_impl(
1899      LayerAnimationController::Create(0));
1900
1901  float max_scale = 0.f;
1902  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1903  EXPECT_EQ(0.f, max_scale);
1904
1905  scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1906      KeyframedTransformAnimationCurve::Create());
1907
1908  TransformOperations operations1;
1909  curve1->AddKeyframe(TransformKeyframe::Create(
1910      0.0, operations1, scoped_ptr<TimingFunction>()));
1911  operations1.AppendScale(2.0, 3.0, 4.0);
1912  curve1->AddKeyframe(TransformKeyframe::Create(
1913      1.0, operations1, scoped_ptr<TimingFunction>()));
1914
1915  scoped_ptr<Animation> animation(Animation::Create(
1916      curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1917  controller_impl->AddAnimation(animation.Pass());
1918
1919  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1920  EXPECT_EQ(4.f, max_scale);
1921
1922  scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1923      KeyframedTransformAnimationCurve::Create());
1924
1925  TransformOperations operations2;
1926  curve2->AddKeyframe(TransformKeyframe::Create(
1927      0.0, operations2, scoped_ptr<TimingFunction>()));
1928  operations2.AppendScale(6.0, 5.0, 4.0);
1929  curve2->AddKeyframe(TransformKeyframe::Create(
1930      1.0, operations2, scoped_ptr<TimingFunction>()));
1931
1932  animation = Animation::Create(
1933      curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
1934  controller_impl->AddAnimation(animation.Pass());
1935
1936  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1937  EXPECT_EQ(6.f, max_scale);
1938
1939  scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1940      KeyframedTransformAnimationCurve::Create());
1941
1942  TransformOperations operations3;
1943  curve3->AddKeyframe(TransformKeyframe::Create(
1944      0.0, operations3, scoped_ptr<TimingFunction>()));
1945  operations3.AppendPerspective(6.0);
1946  curve3->AddKeyframe(TransformKeyframe::Create(
1947      1.0, operations3, scoped_ptr<TimingFunction>()));
1948
1949  animation = Animation::Create(
1950      curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1951  controller_impl->AddAnimation(animation.Pass());
1952
1953  EXPECT_FALSE(controller_impl->MaximumTargetScale(&max_scale));
1954
1955  controller_impl->GetAnimation(3, Animation::Transform)
1956      ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1957  controller_impl->GetAnimation(2, Animation::Transform)
1958      ->SetRunState(Animation::Finished, TicksFromSecondsF(0.0));
1959
1960  // Only unfinished animations should be considered by
1961  // MaximumTargetScale.
1962  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1963  EXPECT_EQ(4.f, max_scale);
1964}
1965
1966TEST(LayerAnimationControllerTest, MaximumTargetScaleWithDirection) {
1967  scoped_refptr<LayerAnimationController> controller_impl(
1968      LayerAnimationController::Create(0));
1969
1970  scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1971      KeyframedTransformAnimationCurve::Create());
1972  TransformOperations operations1;
1973  operations1.AppendScale(1.0, 2.0, 3.0);
1974  curve1->AddKeyframe(TransformKeyframe::Create(
1975      0.0, operations1, scoped_ptr<TimingFunction>()));
1976  TransformOperations operations2;
1977  operations2.AppendScale(4.0, 5.0, 6.0);
1978  curve1->AddKeyframe(TransformKeyframe::Create(
1979      1.0, operations2, scoped_ptr<TimingFunction>()));
1980
1981  scoped_ptr<Animation> animation_owned(Animation::Create(
1982      curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1983  Animation* animation = animation_owned.get();
1984  controller_impl->AddAnimation(animation_owned.Pass());
1985
1986  float max_scale = 0.f;
1987
1988  EXPECT_GT(animation->playback_rate(), 0.0);
1989
1990  // Normal direction with positive playback rate.
1991  animation->set_direction(Animation::Normal);
1992  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1993  EXPECT_EQ(6.f, max_scale);
1994
1995  // Alternate direction with positive playback rate.
1996  animation->set_direction(Animation::Alternate);
1997  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
1998  EXPECT_EQ(6.f, max_scale);
1999
2000  // Reverse direction with positive playback rate.
2001  animation->set_direction(Animation::Reverse);
2002  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2003  EXPECT_EQ(3.f, max_scale);
2004
2005  // Alternate reverse direction.
2006  animation->set_direction(Animation::Reverse);
2007  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2008  EXPECT_EQ(3.f, max_scale);
2009
2010  animation->set_playback_rate(-1.0);
2011
2012  // Normal direction with negative playback rate.
2013  animation->set_direction(Animation::Normal);
2014  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2015  EXPECT_EQ(3.f, max_scale);
2016
2017  // Alternate direction with negative playback rate.
2018  animation->set_direction(Animation::Alternate);
2019  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2020  EXPECT_EQ(3.f, max_scale);
2021
2022  // Reverse direction with negative playback rate.
2023  animation->set_direction(Animation::Reverse);
2024  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2025  EXPECT_EQ(6.f, max_scale);
2026
2027  // Alternate reverse direction with negative playback rate.
2028  animation->set_direction(Animation::Reverse);
2029  EXPECT_TRUE(controller_impl->MaximumTargetScale(&max_scale));
2030  EXPECT_EQ(6.f, max_scale);
2031}
2032
2033TEST(LayerAnimationControllerTest, NewlyPushedAnimationWaitsForActivation) {
2034  scoped_ptr<AnimationEventsVector> events(
2035      make_scoped_ptr(new AnimationEventsVector));
2036  FakeLayerAnimationValueObserver dummy_impl;
2037  FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2038  scoped_refptr<LayerAnimationController> controller_impl(
2039      LayerAnimationController::Create(0));
2040  controller_impl->AddValueObserver(&dummy_impl);
2041  controller_impl->AddValueObserver(&pending_dummy_impl);
2042  FakeLayerAnimationValueObserver dummy;
2043  scoped_refptr<LayerAnimationController> controller(
2044      LayerAnimationController::Create(0));
2045  controller->AddValueObserver(&dummy);
2046
2047  EXPECT_FALSE(controller->needs_to_start_animations_for_testing());
2048  AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, false);
2049  int group_id = controller->GetAnimation(Animation::Opacity)->group();
2050  EXPECT_TRUE(controller->needs_to_start_animations_for_testing());
2051
2052  EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2053  controller->PushAnimationUpdatesTo(controller_impl.get());
2054  EXPECT_TRUE(controller_impl->needs_to_start_animations_for_testing());
2055
2056  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
2057  EXPECT_EQ(
2058      Animation::WaitingForTargetAvailability,
2059      controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2060  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2061                  ->affects_pending_observers());
2062  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2063                   ->affects_active_observers());
2064
2065  controller_impl->Animate(kInitialTickTime);
2066  EXPECT_FALSE(controller_impl->needs_to_start_animations_for_testing());
2067  controller_impl->UpdateState(true, events.get());
2068
2069  // Since the animation hasn't been activated, it should still be Starting
2070  // rather than Running.
2071  EXPECT_EQ(
2072      Animation::Starting,
2073      controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2074
2075  // Since the animation hasn't been activated, only the pending observer
2076  // should have been ticked.
2077  EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2078  EXPECT_EQ(0.f, dummy_impl.opacity());
2079
2080  controller_impl->ActivateAnimations();
2081  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2082                  ->affects_pending_observers());
2083  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2084                  ->affects_active_observers());
2085
2086  controller_impl->Animate(kInitialTickTime +
2087                           TimeDelta::FromMilliseconds(1000));
2088  controller_impl->UpdateState(true, events.get());
2089
2090  // Since the animation has been activated, it should have reached the
2091  // Running state and the active observer should start to get ticked.
2092  EXPECT_EQ(
2093      Animation::Running,
2094      controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2095  EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2096  EXPECT_EQ(0.5f, dummy_impl.opacity());
2097}
2098
2099TEST(LayerAnimationControllerTest, ActivationBetweenAnimateAndUpdateState) {
2100  scoped_ptr<AnimationEventsVector> events(
2101      make_scoped_ptr(new AnimationEventsVector));
2102  FakeLayerAnimationValueObserver dummy_impl;
2103  FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2104  scoped_refptr<LayerAnimationController> controller_impl(
2105      LayerAnimationController::Create(0));
2106  controller_impl->AddValueObserver(&dummy_impl);
2107  controller_impl->AddValueObserver(&pending_dummy_impl);
2108  FakeLayerAnimationValueObserver dummy;
2109  scoped_refptr<LayerAnimationController> controller(
2110      LayerAnimationController::Create(0));
2111  controller->AddValueObserver(&dummy);
2112
2113  AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2114  int group_id = controller->GetAnimation(Animation::Opacity)->group();
2115
2116  controller->PushAnimationUpdatesTo(controller_impl.get());
2117
2118  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
2119  EXPECT_EQ(
2120      Animation::WaitingForTargetAvailability,
2121      controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2122  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2123                  ->affects_pending_observers());
2124  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2125                   ->affects_active_observers());
2126
2127  controller_impl->Animate(kInitialTickTime);
2128
2129  // Since the animation hasn't been activated, only the pending observer
2130  // should have been ticked.
2131  EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2132  EXPECT_EQ(0.f, dummy_impl.opacity());
2133
2134  controller_impl->ActivateAnimations();
2135  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2136                  ->affects_pending_observers());
2137  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2138                  ->affects_active_observers());
2139
2140  controller_impl->UpdateState(true, events.get());
2141
2142  // Since the animation has been activated, it should have reached the
2143  // Running state.
2144  EXPECT_EQ(
2145      Animation::Running,
2146      controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2147
2148  controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2149
2150  // Both observers should have been ticked.
2151  EXPECT_EQ(0.75f, pending_dummy_impl.opacity());
2152  EXPECT_EQ(0.75f, dummy_impl.opacity());
2153}
2154
2155TEST(LayerAnimationControllerTest, ClippedOpacityValues) {
2156  FakeLayerAnimationValueObserver dummy;
2157  scoped_refptr<LayerAnimationController> controller(
2158      LayerAnimationController::Create(0));
2159  controller->AddValueObserver(&dummy);
2160
2161  AddOpacityTransitionToController(controller.get(), 1, 1.f, 2.f, true);
2162
2163  controller->Animate(kInitialTickTime);
2164  EXPECT_EQ(1.f, dummy.opacity());
2165
2166  // Opacity values are clipped [0,1]
2167  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2168  EXPECT_EQ(1.f, dummy.opacity());
2169}
2170
2171TEST(LayerAnimationControllerTest, ClippedNegativeOpacityValues) {
2172  FakeLayerAnimationValueObserver dummy;
2173  scoped_refptr<LayerAnimationController> controller(
2174      LayerAnimationController::Create(0));
2175  controller->AddValueObserver(&dummy);
2176
2177  AddOpacityTransitionToController(controller.get(), 1, 0.f, -2.f, true);
2178
2179  controller->Animate(kInitialTickTime);
2180  EXPECT_EQ(0.f, dummy.opacity());
2181
2182  // Opacity values are clipped [0,1]
2183  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2184  EXPECT_EQ(0.f, dummy.opacity());
2185}
2186
2187TEST(LayerAnimationControllerTest, PushedDeletedAnimationWaitsForActivation) {
2188  scoped_ptr<AnimationEventsVector> events(
2189      make_scoped_ptr(new AnimationEventsVector));
2190  FakeLayerAnimationValueObserver dummy_impl;
2191  FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2192  scoped_refptr<LayerAnimationController> controller_impl(
2193      LayerAnimationController::Create(0));
2194  controller_impl->AddValueObserver(&dummy_impl);
2195  controller_impl->AddValueObserver(&pending_dummy_impl);
2196  FakeLayerAnimationValueObserver dummy;
2197  scoped_refptr<LayerAnimationController> controller(
2198      LayerAnimationController::Create(0));
2199  controller->AddValueObserver(&dummy);
2200
2201  AddOpacityTransitionToController(controller.get(), 1, 0.5f, 1.f, true);
2202  int group_id = controller->GetAnimation(Animation::Opacity)->group();
2203
2204  controller->PushAnimationUpdatesTo(controller_impl.get());
2205  controller_impl->ActivateAnimations();
2206  controller_impl->Animate(kInitialTickTime);
2207  controller_impl->UpdateState(true, events.get());
2208  EXPECT_EQ(
2209      Animation::Running,
2210      controller_impl->GetAnimation(group_id, Animation::Opacity)->run_state());
2211  EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2212  EXPECT_EQ(0.5f, dummy_impl.opacity());
2213
2214  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2215                  ->affects_pending_observers());
2216  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2217                  ->affects_active_observers());
2218
2219  // Delete the animation on the main-thread controller.
2220  controller->RemoveAnimation(
2221      controller->GetAnimation(Animation::Opacity)->id());
2222  controller->PushAnimationUpdatesTo(controller_impl.get());
2223
2224  // The animation should no longer affect pending observers.
2225  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2226                   ->affects_pending_observers());
2227  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity)
2228                  ->affects_active_observers());
2229
2230  controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2231  controller_impl->UpdateState(true, events.get());
2232
2233  // Only the active observer should have been ticked.
2234  EXPECT_EQ(0.5f, pending_dummy_impl.opacity());
2235  EXPECT_EQ(0.75f, dummy_impl.opacity());
2236
2237  controller_impl->ActivateAnimations();
2238
2239  // Activation should cause the animation to be deleted.
2240  EXPECT_FALSE(controller_impl->has_any_animation());
2241}
2242
2243// Tests that an animation that affects only active observers won't block
2244// an animation that affects only pending observers from starting.
2245TEST(LayerAnimationControllerTest, StartAnimationsAffectingDifferentObservers) {
2246  scoped_ptr<AnimationEventsVector> events(
2247      make_scoped_ptr(new AnimationEventsVector));
2248  FakeLayerAnimationValueObserver dummy_impl;
2249  FakeInactiveLayerAnimationValueObserver pending_dummy_impl;
2250  scoped_refptr<LayerAnimationController> controller_impl(
2251      LayerAnimationController::Create(0));
2252  controller_impl->AddValueObserver(&dummy_impl);
2253  controller_impl->AddValueObserver(&pending_dummy_impl);
2254  FakeLayerAnimationValueObserver dummy;
2255  scoped_refptr<LayerAnimationController> controller(
2256      LayerAnimationController::Create(0));
2257  controller->AddValueObserver(&dummy);
2258
2259  AddOpacityTransitionToController(controller.get(), 1, 0.f, 1.f, true);
2260  int first_animation_group_id =
2261      controller->GetAnimation(Animation::Opacity)->group();
2262
2263  controller->PushAnimationUpdatesTo(controller_impl.get());
2264  controller_impl->ActivateAnimations();
2265  controller_impl->Animate(kInitialTickTime);
2266  controller_impl->UpdateState(true, events.get());
2267
2268  // Remove the first animation from the main-thread controller, and add a
2269  // new animation affecting the same property.
2270  controller->RemoveAnimation(
2271      controller->GetAnimation(Animation::Opacity)->id());
2272  AddOpacityTransitionToController(controller.get(), 1, 1.f, 0.5f, true);
2273  int second_animation_group_id =
2274      controller->GetAnimation(Animation::Opacity)->group();
2275  controller->PushAnimationUpdatesTo(controller_impl.get());
2276
2277  // The original animation should only affect active observers, and the new
2278  // animation should only affect pending observers.
2279  EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
2280                                             Animation::Opacity)
2281                   ->affects_pending_observers());
2282  EXPECT_TRUE(controller_impl->GetAnimation(first_animation_group_id,
2283                                            Animation::Opacity)
2284                  ->affects_active_observers());
2285  EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2286                                            Animation::Opacity)
2287                  ->affects_pending_observers());
2288  EXPECT_FALSE(controller_impl->GetAnimation(second_animation_group_id,
2289                                             Animation::Opacity)
2290                   ->affects_active_observers());
2291
2292  controller_impl->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2293  controller_impl->UpdateState(true, events.get());
2294
2295  // The original animation should still be running, and the new animation
2296  // should be starting.
2297  EXPECT_EQ(Animation::Running,
2298            controller_impl->GetAnimation(first_animation_group_id,
2299                                          Animation::Opacity)->run_state());
2300  EXPECT_EQ(Animation::Starting,
2301            controller_impl->GetAnimation(second_animation_group_id,
2302                                          Animation::Opacity)->run_state());
2303
2304  // The active observer should have been ticked by the original animation,
2305  // and the pending observer should have been ticked by the new animation.
2306  EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2307  EXPECT_EQ(0.5f, dummy_impl.opacity());
2308
2309  controller_impl->ActivateAnimations();
2310
2311  // The original animation should have been deleted, and the new animation
2312  // should now affect both observers.
2313  EXPECT_FALSE(controller_impl->GetAnimation(first_animation_group_id,
2314                                             Animation::Opacity));
2315  EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2316                                            Animation::Opacity)
2317                  ->affects_pending_observers());
2318  EXPECT_TRUE(controller_impl->GetAnimation(second_animation_group_id,
2319                                            Animation::Opacity)
2320                  ->affects_active_observers());
2321
2322  controller_impl->Animate(kInitialTickTime +
2323                           TimeDelta::FromMilliseconds(1000));
2324  controller_impl->UpdateState(true, events.get());
2325
2326  // The new animation should be running, and the active observer should have
2327  // been ticked at the new animation's starting point.
2328  EXPECT_EQ(Animation::Running,
2329            controller_impl->GetAnimation(second_animation_group_id,
2330                                          Animation::Opacity)->run_state());
2331  EXPECT_EQ(1.f, pending_dummy_impl.opacity());
2332  EXPECT_EQ(1.f, dummy_impl.opacity());
2333}
2334
2335TEST(LayerAnimationControllerTest, TestIsAnimatingProperty) {
2336  FakeLayerAnimationValueObserver dummy;
2337  scoped_refptr<LayerAnimationController> controller(
2338      LayerAnimationController::Create(0));
2339  controller->AddValueObserver(&dummy);
2340
2341  scoped_ptr<Animation> animation(CreateAnimation(
2342      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2343      1,
2344      Animation::Opacity));
2345  controller->AddAnimation(animation.Pass());
2346  controller->Animate(kInitialTickTime);
2347  EXPECT_TRUE(controller->IsAnimatingProperty(Animation::Opacity));
2348  controller->UpdateState(true, nullptr);
2349  EXPECT_TRUE(controller->HasActiveAnimation());
2350  EXPECT_TRUE(controller->IsAnimatingProperty(Animation::Opacity));
2351  EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Filter));
2352  EXPECT_EQ(0.f, dummy.opacity());
2353}
2354
2355TEST(LayerAnimationControllerTest, TestIsAnimatingPropertyTimeOffsetFillMode) {
2356  FakeLayerAnimationValueObserver dummy;
2357  scoped_refptr<LayerAnimationController> controller(
2358      LayerAnimationController::Create(0));
2359  controller->AddValueObserver(&dummy);
2360
2361  scoped_ptr<Animation> animation(CreateAnimation(
2362      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
2363      1,
2364      Animation::Opacity));
2365  animation->set_fill_mode(Animation::FillModeNone);
2366  animation->set_time_offset(TimeDelta::FromMilliseconds(-2000));
2367  controller->AddAnimation(animation.Pass());
2368
2369  controller->Animate(kInitialTickTime);
2370  controller->UpdateState(true, nullptr);
2371  EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Opacity));
2372  EXPECT_TRUE(controller->HasActiveAnimation());
2373  EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Opacity));
2374  EXPECT_FALSE(controller->IsAnimatingProperty(Animation::Filter));
2375
2376  controller->Animate(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2377  controller->UpdateState(true, nullptr);
2378  EXPECT_TRUE(controller->IsAnimatingProperty(Animation::Opacity));
2379}
2380
2381}  // namespace
2382}  // namespace cc
2383