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