- add sources.
[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/keyframed_animation_curve.h"
11 #include "cc/animation/transform_operations.h"
12 #include "cc/test/animation_test_common.h"
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/box_f.h"
16 #include "ui/gfx/transform.h"
17
18 namespace cc {
19 namespace {
20
21 scoped_ptr<Animation> CreateAnimation(scoped_ptr<AnimationCurve> curve,
22                                       int id,
23                                       Animation::TargetProperty property) {
24   return Animation::Create(curve.Pass(), 0, id, property);
25 }
26
27 TEST(LayerAnimationControllerTest, SyncNewAnimation) {
28   FakeLayerAnimationValueObserver dummy_impl;
29   scoped_refptr<LayerAnimationController> controller_impl(
30       LayerAnimationController::Create(0));
31   controller_impl->AddValueObserver(&dummy_impl);
32   FakeLayerAnimationValueObserver dummy;
33   scoped_refptr<LayerAnimationController> controller(
34       LayerAnimationController::Create(0));
35   controller->AddValueObserver(&dummy);
36
37   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
38
39   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
40   int group_id = controller->GetAnimation(Animation::Opacity)->group();
41
42   controller->PushAnimationUpdatesTo(controller_impl.get());
43
44   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
45   EXPECT_EQ(Animation::WaitingForTargetAvailability,
46             controller_impl->GetAnimation(group_id,
47                                           Animation::Opacity)->run_state());
48 }
49
50 // If an animation is started on the impl thread before it is ticked on the main
51 // thread, we must be sure to respect the synchronized start time.
52 TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) {
53   FakeLayerAnimationValueObserver dummy_impl;
54   scoped_refptr<LayerAnimationController> controller_impl(
55       LayerAnimationController::Create(0));
56   controller_impl->AddValueObserver(&dummy_impl);
57   FakeLayerAnimationValueObserver dummy;
58   scoped_refptr<LayerAnimationController> controller(
59       LayerAnimationController::Create(0));
60   controller->AddValueObserver(&dummy);
61
62   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
63
64   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
65   int group_id = controller->GetAnimation(Animation::Opacity)->group();
66
67   controller->PushAnimationUpdatesTo(controller_impl.get());
68
69   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
70   EXPECT_EQ(Animation::WaitingForTargetAvailability,
71             controller_impl->GetAnimation(group_id,
72                                           Animation::Opacity)->run_state());
73
74   AnimationEventsVector events;
75   controller_impl->Animate(1.0);
76   controller_impl->UpdateState(true, &events);
77
78   // Synchronize the start times.
79   EXPECT_EQ(1u, events.size());
80   controller->NotifyAnimationStarted(events[0], 0.0);
81   EXPECT_EQ(controller->GetAnimation(group_id,
82                                      Animation::Opacity)->start_time(),
83             controller_impl->GetAnimation(group_id,
84                                           Animation::Opacity)->start_time());
85
86   // Start the animation on the main thread. Should not affect the start time.
87   controller->Animate(1.5);
88   controller->UpdateState(true, NULL);
89   EXPECT_EQ(controller->GetAnimation(group_id,
90                                      Animation::Opacity)->start_time(),
91             controller_impl->GetAnimation(group_id,
92                                           Animation::Opacity)->start_time());
93 }
94
95 TEST(LayerAnimationControllerTest, SyncPause) {
96   FakeLayerAnimationValueObserver dummy_impl;
97   scoped_refptr<LayerAnimationController> controller_impl(
98       LayerAnimationController::Create(0));
99   controller_impl->AddValueObserver(&dummy_impl);
100   FakeLayerAnimationValueObserver dummy;
101   scoped_refptr<LayerAnimationController> controller(
102       LayerAnimationController::Create(0));
103   controller->AddValueObserver(&dummy);
104
105   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
106
107   AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
108   int group_id = controller->GetAnimation(Animation::Opacity)->group();
109   int animation_id = controller->GetAnimation(Animation::Opacity)->id();
110
111   controller->PushAnimationUpdatesTo(controller_impl.get());
112
113   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
114   EXPECT_EQ(Animation::WaitingForTargetAvailability,
115             controller_impl->GetAnimation(group_id,
116                                           Animation::Opacity)->run_state());
117
118   // Start the animations on each controller.
119   AnimationEventsVector events;
120   controller_impl->Animate(0.0);
121   controller_impl->UpdateState(true, &events);
122   controller->Animate(0.0);
123   controller->UpdateState(true, NULL);
124   EXPECT_EQ(Animation::Running,
125             controller_impl->GetAnimation(group_id,
126                                           Animation::Opacity)->run_state());
127   EXPECT_EQ(Animation::Running,
128             controller->GetAnimation(group_id,
129                                      Animation::Opacity)->run_state());
130
131   // Pause the main-thread animation.
132   controller->PauseAnimation(animation_id, 1.0);
133   EXPECT_EQ(Animation::Paused,
134             controller->GetAnimation(group_id,
135                                      Animation::Opacity)->run_state());
136
137   // The pause run state change should make it to the impl thread controller.
138   controller->PushAnimationUpdatesTo(controller_impl.get());
139   EXPECT_EQ(Animation::Paused,
140             controller_impl->GetAnimation(group_id,
141                                           Animation::Opacity)->run_state());
142 }
143
144 TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
145   FakeLayerAnimationValueObserver dummy_impl;
146   scoped_refptr<LayerAnimationController> controller_impl(
147       LayerAnimationController::Create(0));
148   controller_impl->AddValueObserver(&dummy_impl);
149   FakeLayerAnimationValueObserver dummy;
150   scoped_refptr<LayerAnimationController> controller(
151       LayerAnimationController::Create(0));
152   controller->AddValueObserver(&dummy);
153
154   EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
155
156   int animation_id =
157       AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
158   int group_id = controller->GetAnimation(Animation::Opacity)->group();
159
160   controller->PushAnimationUpdatesTo(controller_impl.get());
161
162   EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
163   EXPECT_EQ(Animation::WaitingForTargetAvailability,
164             controller_impl->GetAnimation(group_id,
165                                           Animation::Opacity)->run_state());
166
167   // Notify main thread controller that the animation has started.
168   AnimationEvent animation_started_event(
169       AnimationEvent::Started, 0, group_id, Animation::Opacity, 0);
170   controller->NotifyAnimationStarted(animation_started_event, 0.0);
171
172   // Force animation to complete on impl thread.
173   controller_impl->RemoveAnimation(animation_id);
174
175   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
176
177   controller->PushAnimationUpdatesTo(controller_impl.get());
178
179   // Even though the main thread has a 'new' animation, it should not be pushed
180   // because the animation has already completed on the impl thread.
181   EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
182 }
183
184 // Ensure that a finished animation is eventually deleted by both the
185 // main-thread and the impl-thread controllers.
186 TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
187   FakeLayerAnimationValueObserver dummy;
188   FakeLayerAnimationValueObserver dummy_impl;
189   scoped_ptr<AnimationEventsVector> events(
190       make_scoped_ptr(new AnimationEventsVector));
191   scoped_refptr<LayerAnimationController> controller(
192       LayerAnimationController::Create(0));
193   scoped_refptr<LayerAnimationController> controller_impl(
194       LayerAnimationController::Create(0));
195   controller->AddValueObserver(&dummy);
196   controller_impl->AddValueObserver(&dummy_impl);
197
198   AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
199   controller->Animate(0.0);
200   controller->UpdateState(true, NULL);
201   controller->PushAnimationUpdatesTo(controller_impl.get());
202
203   controller_impl->Animate(0.5);
204   controller_impl->UpdateState(true, events.get());
205
206   // There should be a Started event for the animation.
207   EXPECT_EQ(1u, events->size());
208   EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
209   controller->NotifyAnimationStarted((*events)[0], 0.0);
210
211   controller->Animate(1.0);
212   controller->UpdateState(true, NULL);
213
214   events.reset(new AnimationEventsVector);
215   controller_impl->Animate(2.0);
216   controller_impl->UpdateState(true, events.get());
217
218   // There should be a Finished event for the animation.
219   EXPECT_EQ(1u, events->size());
220   EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
221
222   // Neither controller should have deleted the animation yet.
223   EXPECT_TRUE(controller->GetAnimation(Animation::Opacity));
224   EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity));
225
226   controller->NotifyAnimationFinished((*events)[0], 0.0);
227
228   controller->Animate(3.0);
229   controller->UpdateState(true, NULL);
230
231   controller->PushAnimationUpdatesTo(controller_impl.get());
232
233   // Both controllers should now have deleted the animation.
234   EXPECT_FALSE(controller->has_any_animation());
235   EXPECT_FALSE(controller_impl->has_any_animation());
236 }
237
238 // Tests that transitioning opacity from 0 to 1 works as expected.
239
240 static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
241     const AnimationEventsVector* events) {
242   const AnimationEvent* event = 0;
243   for (size_t i = 0; i < events->size(); ++i)
244     if ((*events)[i].type == AnimationEvent::PropertyUpdate)
245       event = &(*events)[i];
246
247   return event;
248 }
249
250 TEST(LayerAnimationControllerTest, TrivialTransition) {
251   scoped_ptr<AnimationEventsVector> events(
252       make_scoped_ptr(new AnimationEventsVector));
253   FakeLayerAnimationValueObserver dummy;
254   scoped_refptr<LayerAnimationController> controller(
255       LayerAnimationController::Create(0));
256   controller->AddValueObserver(&dummy);
257
258   scoped_ptr<Animation> to_add(CreateAnimation(
259       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
260       1,
261       Animation::Opacity));
262
263   controller->AddAnimation(to_add.Pass());
264   controller->Animate(0.0);
265   controller->UpdateState(true, events.get());
266   EXPECT_TRUE(controller->HasActiveAnimation());
267   EXPECT_EQ(0.f, dummy.opacity());
268   // A non-impl-only animation should not generate property updates.
269   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
270   EXPECT_FALSE(event);
271   controller->Animate(1.0);
272   controller->UpdateState(true, events.get());
273   EXPECT_EQ(1.f, dummy.opacity());
274   EXPECT_FALSE(controller->HasActiveAnimation());
275   event = GetMostRecentPropertyUpdateEvent(events.get());
276   EXPECT_FALSE(event);
277 }
278
279 TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
280   scoped_ptr<AnimationEventsVector> events(
281       make_scoped_ptr(new AnimationEventsVector));
282   FakeLayerAnimationValueObserver dummy_impl;
283   scoped_refptr<LayerAnimationController> controller_impl(
284       LayerAnimationController::Create(0));
285   controller_impl->AddValueObserver(&dummy_impl);
286
287   scoped_ptr<Animation> to_add(CreateAnimation(
288       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
289       1,
290       Animation::Opacity));
291   to_add->set_is_impl_only(true);
292
293   controller_impl->AddAnimation(to_add.Pass());
294   controller_impl->Animate(0.0);
295   controller_impl->UpdateState(true, events.get());
296   EXPECT_TRUE(controller_impl->HasActiveAnimation());
297   EXPECT_EQ(0.f, dummy_impl.opacity());
298   EXPECT_EQ(2u, events->size());
299   const AnimationEvent* start_opacity_event =
300       GetMostRecentPropertyUpdateEvent(events.get());
301   EXPECT_EQ(0.f, start_opacity_event->opacity);
302
303   controller_impl->Animate(1.0);
304   controller_impl->UpdateState(true, events.get());
305   EXPECT_EQ(1.f, dummy_impl.opacity());
306   EXPECT_FALSE(controller_impl->HasActiveAnimation());
307   EXPECT_EQ(4u, events->size());
308   const AnimationEvent* end_opacity_event =
309       GetMostRecentPropertyUpdateEvent(events.get());
310   EXPECT_EQ(1.f, end_opacity_event->opacity);
311 }
312
313 TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
314   scoped_ptr<AnimationEventsVector> events(
315       make_scoped_ptr(new AnimationEventsVector));
316   FakeLayerAnimationValueObserver dummy_impl;
317   scoped_refptr<LayerAnimationController> controller_impl(
318       LayerAnimationController::Create(0));
319   controller_impl->AddValueObserver(&dummy_impl);
320
321   // Choose different values for x and y to avoid coincidental values in the
322   // observed transforms.
323   const float delta_x = 3;
324   const float delta_y = 4;
325
326   scoped_ptr<KeyframedTransformAnimationCurve> curve(
327       KeyframedTransformAnimationCurve::Create());
328
329   // Create simple Transform animation.
330   TransformOperations operations;
331   curve->AddKeyframe(TransformKeyframe::Create(
332       0, operations, scoped_ptr<cc::TimingFunction>()));
333   operations.AppendTranslate(delta_x, delta_y, 0);
334   curve->AddKeyframe(TransformKeyframe::Create(
335       1, operations, scoped_ptr<cc::TimingFunction>()));
336
337   scoped_ptr<Animation> animation(Animation::Create(
338       curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform));
339   animation->set_is_impl_only(true);
340   controller_impl->AddAnimation(animation.Pass());
341
342   // Run animation.
343   controller_impl->Animate(0.0);
344   controller_impl->UpdateState(true, events.get());
345   EXPECT_TRUE(controller_impl->HasActiveAnimation());
346   EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
347   EXPECT_EQ(2u, events->size());
348   const AnimationEvent* start_transform_event =
349       GetMostRecentPropertyUpdateEvent(events.get());
350   ASSERT_TRUE(start_transform_event);
351   EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
352   EXPECT_TRUE(start_transform_event->is_impl_only);
353
354   gfx::Transform expected_transform;
355   expected_transform.Translate(delta_x, delta_y);
356
357   controller_impl->Animate(1.0);
358   controller_impl->UpdateState(true, events.get());
359   EXPECT_EQ(expected_transform, dummy_impl.transform());
360   EXPECT_FALSE(controller_impl->HasActiveAnimation());
361   EXPECT_EQ(4u, events->size());
362   const AnimationEvent* end_transform_event =
363       GetMostRecentPropertyUpdateEvent(events.get());
364   EXPECT_EQ(expected_transform, end_transform_event->transform);
365   EXPECT_TRUE(end_transform_event->is_impl_only);
366 }
367
368 TEST(LayerAnimationControllerTest, FilterTransition) {
369   scoped_ptr<AnimationEventsVector> events(
370       make_scoped_ptr(new AnimationEventsVector));
371   FakeLayerAnimationValueObserver dummy;
372   scoped_refptr<LayerAnimationController> controller(
373       LayerAnimationController::Create(0));
374   controller->AddValueObserver(&dummy);
375
376   scoped_ptr<KeyframedFilterAnimationCurve> curve(
377       KeyframedFilterAnimationCurve::Create());
378
379   FilterOperations start_filters;
380   start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
381   curve->AddKeyframe(FilterKeyframe::Create(
382       0, start_filters, scoped_ptr<cc::TimingFunction>()));
383   FilterOperations end_filters;
384   end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
385   curve->AddKeyframe(FilterKeyframe::Create(
386       1, end_filters, scoped_ptr<cc::TimingFunction>()));
387
388   scoped_ptr<Animation> animation(Animation::Create(
389       curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
390   controller->AddAnimation(animation.Pass());
391
392   controller->Animate(0.0);
393   controller->UpdateState(true, events.get());
394   EXPECT_TRUE(controller->HasActiveAnimation());
395   EXPECT_EQ(start_filters, dummy.filters());
396   // A non-impl-only animation should not generate property updates.
397   const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
398   EXPECT_FALSE(event);
399
400   controller->Animate(0.5);
401   controller->UpdateState(true, events.get());
402   EXPECT_EQ(1u, dummy.filters().size());
403   EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
404             dummy.filters().at(0));
405   event = GetMostRecentPropertyUpdateEvent(events.get());
406   EXPECT_FALSE(event);
407
408   controller->Animate(1.0);
409   controller->UpdateState(true, events.get());
410   EXPECT_EQ(end_filters, dummy.filters());
411   EXPECT_FALSE(controller->HasActiveAnimation());
412   event = GetMostRecentPropertyUpdateEvent(events.get());
413   EXPECT_FALSE(event);
414 }
415
416 TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
417   scoped_ptr<AnimationEventsVector> events(
418       make_scoped_ptr(new AnimationEventsVector));
419   FakeLayerAnimationValueObserver dummy_impl;
420   scoped_refptr<LayerAnimationController> controller_impl(
421       LayerAnimationController::Create(0));
422   controller_impl->AddValueObserver(&dummy_impl);
423
424   scoped_ptr<KeyframedFilterAnimationCurve> curve(
425       KeyframedFilterAnimationCurve::Create());
426
427   // Create simple Filter animation.
428   FilterOperations start_filters;
429   start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
430   curve->AddKeyframe(FilterKeyframe::Create(
431       0, start_filters, scoped_ptr<cc::TimingFunction>()));
432   FilterOperations end_filters;
433   end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
434   curve->AddKeyframe(FilterKeyframe::Create(
435       1, end_filters, scoped_ptr<cc::TimingFunction>()));
436
437   scoped_ptr<Animation> animation(Animation::Create(
438       curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
439   animation->set_is_impl_only(true);
440   controller_impl->AddAnimation(animation.Pass());
441
442   // Run animation.
443   controller_impl->Animate(0.0);
444   controller_impl->UpdateState(true, events.get());
445   EXPECT_TRUE(controller_impl->HasActiveAnimation());
446   EXPECT_EQ(start_filters, dummy_impl.filters());
447   EXPECT_EQ(2u, events->size());
448   const AnimationEvent* start_filter_event =
449       GetMostRecentPropertyUpdateEvent(events.get());
450   EXPECT_TRUE(start_filter_event);
451   EXPECT_EQ(start_filters, start_filter_event->filters);
452   EXPECT_TRUE(start_filter_event->is_impl_only);
453
454   controller_impl->Animate(1.0);
455   controller_impl->UpdateState(true, events.get());
456   EXPECT_EQ(end_filters, dummy_impl.filters());
457   EXPECT_FALSE(controller_impl->HasActiveAnimation());
458   EXPECT_EQ(4u, events->size());
459   const AnimationEvent* end_filter_event =
460       GetMostRecentPropertyUpdateEvent(events.get());
461   EXPECT_TRUE(end_filter_event);
462   EXPECT_EQ(end_filters, end_filter_event->filters);
463   EXPECT_TRUE(end_filter_event->is_impl_only);
464 }
465
466 class FakeAnimationDelegate : public AnimationDelegate {
467  public:
468   FakeAnimationDelegate()
469       : started_(false),
470         finished_(false) {}
471
472   virtual void NotifyAnimationStarted(double time) OVERRIDE {
473     started_ = true;
474   }
475
476   virtual void NotifyAnimationFinished(double time) OVERRIDE {
477     finished_ = true;
478   }
479
480   bool started() { return started_; }
481
482   bool finished() { return finished_; }
483
484  private:
485   bool started_;
486   bool finished_;
487 };
488
489 // Tests that impl-only animations lead to start and finished notifications
490 // being sent to the main thread controller's animation delegate.
491 TEST(LayerAnimationControllerTest,
492      NotificationsForImplOnlyAnimationsAreSentToMainThreadDelegate) {
493   FakeLayerAnimationValueObserver dummy_impl;
494   scoped_refptr<LayerAnimationController> controller_impl(
495       LayerAnimationController::Create(0));
496   controller_impl->AddValueObserver(&dummy_impl);
497   scoped_ptr<AnimationEventsVector> events(
498       make_scoped_ptr(new AnimationEventsVector));
499   FakeLayerAnimationValueObserver dummy;
500   scoped_refptr<LayerAnimationController> controller(
501       LayerAnimationController::Create(0));
502   controller->AddValueObserver(&dummy);
503   FakeAnimationDelegate delegate;
504   controller->set_layer_animation_delegate(&delegate);
505
506   scoped_ptr<Animation> to_add(CreateAnimation(
507       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
508       1,
509       Animation::Opacity));
510   to_add->set_is_impl_only(true);
511   controller_impl->AddAnimation(to_add.Pass());
512
513   controller_impl->Animate(0.0);
514   controller_impl->UpdateState(true, events.get());
515
516   // We should receive 2 events (a started notification and a property update).
517   EXPECT_EQ(2u, events->size());
518   EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
519   EXPECT_TRUE((*events)[0].is_impl_only);
520   EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type);
521   EXPECT_TRUE((*events)[1].is_impl_only);
522
523   // Passing on the start event to the main thread controller should cause the
524   // delegate to get notified.
525   EXPECT_FALSE(delegate.started());
526   controller->NotifyAnimationStarted((*events)[0], 0.0);
527   EXPECT_TRUE(delegate.started());
528
529   events.reset(new AnimationEventsVector);
530   controller_impl->Animate(1.0);
531   controller_impl->UpdateState(true, events.get());
532
533   // We should receive 2 events (a finished notification and a property update).
534   EXPECT_EQ(2u, events->size());
535   EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
536   EXPECT_TRUE((*events)[0].is_impl_only);
537   EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type);
538   EXPECT_TRUE((*events)[1].is_impl_only);
539
540   // Passing on the finished event to the main thread controller should cause
541   // the delegate to get notified.
542   EXPECT_FALSE(delegate.finished());
543   controller->NotifyAnimationFinished((*events)[0], 0.0);
544   EXPECT_TRUE(delegate.finished());
545 }
546
547 // Tests animations that are waiting for a synchronized start time do not
548 // finish.
549 TEST(LayerAnimationControllerTest,
550      AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
551   scoped_ptr<AnimationEventsVector> events(
552       make_scoped_ptr(new AnimationEventsVector));
553   FakeLayerAnimationValueObserver dummy;
554   scoped_refptr<LayerAnimationController> controller(
555       LayerAnimationController::Create(0));
556   controller->AddValueObserver(&dummy);
557
558   scoped_ptr<Animation> to_add(CreateAnimation(
559       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
560       1,
561       Animation::Opacity));
562   to_add->set_needs_synchronized_start_time(true);
563
564   // We should pause at the first keyframe indefinitely waiting for that
565   // animation to start.
566   controller->AddAnimation(to_add.Pass());
567   controller->Animate(0.0);
568   controller->UpdateState(true, events.get());
569   EXPECT_TRUE(controller->HasActiveAnimation());
570   EXPECT_EQ(0.f, dummy.opacity());
571   controller->Animate(1.0);
572   controller->UpdateState(true, events.get());
573   EXPECT_TRUE(controller->HasActiveAnimation());
574   EXPECT_EQ(0.f, dummy.opacity());
575   controller->Animate(2.0);
576   controller->UpdateState(true, events.get());
577   EXPECT_TRUE(controller->HasActiveAnimation());
578   EXPECT_EQ(0.f, dummy.opacity());
579
580   // Send the synchronized start time.
581   controller->NotifyAnimationStarted(
582       AnimationEvent(AnimationEvent::Started, 0, 1, Animation::Opacity, 2),
583       0.0);
584   controller->Animate(5.0);
585   controller->UpdateState(true, events.get());
586   EXPECT_EQ(1.f, dummy.opacity());
587   EXPECT_FALSE(controller->HasActiveAnimation());
588 }
589
590 // Tests that two queued animations affecting the same property run in sequence.
591 TEST(LayerAnimationControllerTest, TrivialQueuing) {
592   scoped_ptr<AnimationEventsVector> events(
593       make_scoped_ptr(new AnimationEventsVector));
594   FakeLayerAnimationValueObserver dummy;
595   scoped_refptr<LayerAnimationController> controller(
596       LayerAnimationController::Create(0));
597   controller->AddValueObserver(&dummy);
598
599   controller->AddAnimation(CreateAnimation(
600       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
601       1,
602       Animation::Opacity));
603   controller->AddAnimation(CreateAnimation(
604       scoped_ptr<AnimationCurve>(
605           new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
606       2,
607       Animation::Opacity));
608
609   controller->Animate(0.0);
610   controller->UpdateState(true, events.get());
611   EXPECT_TRUE(controller->HasActiveAnimation());
612   EXPECT_EQ(0.f, dummy.opacity());
613   controller->Animate(1.0);
614   controller->UpdateState(true, events.get());
615   EXPECT_TRUE(controller->HasActiveAnimation());
616   EXPECT_EQ(1.f, dummy.opacity());
617   controller->Animate(2.0);
618   controller->UpdateState(true, events.get());
619   EXPECT_EQ(0.5f, dummy.opacity());
620   EXPECT_FALSE(controller->HasActiveAnimation());
621 }
622
623 // Tests interrupting a transition with another transition.
624 TEST(LayerAnimationControllerTest, Interrupt) {
625   scoped_ptr<AnimationEventsVector> events(
626       make_scoped_ptr(new AnimationEventsVector));
627   FakeLayerAnimationValueObserver dummy;
628   scoped_refptr<LayerAnimationController> controller(
629       LayerAnimationController::Create(0));
630   controller->AddValueObserver(&dummy);
631   controller->AddAnimation(CreateAnimation(
632       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
633       1,
634       Animation::Opacity));
635   controller->Animate(0.0);
636   controller->UpdateState(true, events.get());
637   EXPECT_TRUE(controller->HasActiveAnimation());
638   EXPECT_EQ(0.f, dummy.opacity());
639
640   scoped_ptr<Animation> to_add(CreateAnimation(
641       scoped_ptr<AnimationCurve>(
642           new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
643       2,
644       Animation::Opacity));
645   to_add->SetRunState(Animation::WaitingForNextTick, 0);
646   controller->AddAnimation(to_add.Pass());
647
648   // Since the animation was in the WaitingForNextTick state, it should start
649   // right in this call to animate.
650   controller->Animate(0.5);
651   controller->UpdateState(true, events.get());
652   EXPECT_TRUE(controller->HasActiveAnimation());
653   EXPECT_EQ(1.f, dummy.opacity());
654   controller->Animate(1.5);
655   controller->UpdateState(true, events.get());
656   EXPECT_EQ(0.5f, dummy.opacity());
657   EXPECT_FALSE(controller->HasActiveAnimation());
658 }
659
660 // Tests scheduling two animations to run together when only one property is
661 // free.
662 TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
663   scoped_ptr<AnimationEventsVector> events(
664       make_scoped_ptr(new AnimationEventsVector));
665   FakeLayerAnimationValueObserver dummy;
666   scoped_refptr<LayerAnimationController> controller(
667       LayerAnimationController::Create(0));
668   controller->AddValueObserver(&dummy);
669
670   controller->AddAnimation(CreateAnimation(
671       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
672       1,
673       Animation::Transform));
674   controller->AddAnimation(CreateAnimation(
675       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
676       2,
677       Animation::Transform));
678   controller->AddAnimation(CreateAnimation(
679       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
680       2,
681       Animation::Opacity));
682
683   controller->Animate(0.0);
684   controller->UpdateState(true, events.get());
685   EXPECT_EQ(0.f, dummy.opacity());
686   EXPECT_TRUE(controller->HasActiveAnimation());
687   controller->Animate(1.0);
688   controller->UpdateState(true, events.get());
689   // Should not have started the float transition yet.
690   EXPECT_TRUE(controller->HasActiveAnimation());
691   EXPECT_EQ(0.f, dummy.opacity());
692   // The float animation should have started at time 1 and should be done.
693   controller->Animate(2.0);
694   controller->UpdateState(true, events.get());
695   EXPECT_EQ(1.f, dummy.opacity());
696   EXPECT_FALSE(controller->HasActiveAnimation());
697 }
698
699 // Tests scheduling two animations to run together with different lengths and
700 // another animation queued to start when the shorter animation finishes (should
701 // wait for both to finish).
702 TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
703   scoped_ptr<AnimationEventsVector> events(
704       make_scoped_ptr(new AnimationEventsVector));
705   FakeLayerAnimationValueObserver dummy;
706   scoped_refptr<LayerAnimationController> controller(
707       LayerAnimationController::Create(0));
708   controller->AddValueObserver(&dummy);
709
710   controller->AddAnimation(CreateAnimation(
711       scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
712       1,
713       Animation::Transform));
714   controller->AddAnimation(CreateAnimation(
715       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
716       1,
717       Animation::Opacity));
718   controller->AddAnimation(CreateAnimation(
719       scoped_ptr<AnimationCurve>(
720           new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
721       2,
722       Animation::Opacity));
723
724   // Animations with id 1 should both start now.
725   controller->Animate(0.0);
726   controller->UpdateState(true, events.get());
727   EXPECT_TRUE(controller->HasActiveAnimation());
728   EXPECT_EQ(0.f, dummy.opacity());
729   // The opacity animation should have finished at time 1, but the group
730   // of animations with id 1 don't finish until time 2 because of the length
731   // of the transform animation.
732   controller->Animate(2.0);
733   controller->UpdateState(true, events.get());
734   // Should not have started the float transition yet.
735   EXPECT_TRUE(controller->HasActiveAnimation());
736   EXPECT_EQ(1.f, dummy.opacity());
737
738   // The second opacity animation should start at time 2 and should be done by
739   // time 3.
740   controller->Animate(3.0);
741   controller->UpdateState(true, events.get());
742   EXPECT_EQ(0.5f, dummy.opacity());
743   EXPECT_FALSE(controller->HasActiveAnimation());
744 }
745
746 // Tests scheduling an animation to start in the future.
747 TEST(LayerAnimationControllerTest, ScheduleAnimation) {
748   scoped_ptr<AnimationEventsVector> events(
749       make_scoped_ptr(new AnimationEventsVector));
750   FakeLayerAnimationValueObserver dummy;
751   scoped_refptr<LayerAnimationController> controller(
752       LayerAnimationController::Create(0));
753   controller->AddValueObserver(&dummy);
754
755   scoped_ptr<Animation> to_add(CreateAnimation(
756       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
757       1,
758       Animation::Opacity));
759   to_add->SetRunState(Animation::WaitingForStartTime, 0);
760   to_add->set_start_time(1.f);
761   controller->AddAnimation(to_add.Pass());
762
763   controller->Animate(0.0);
764   controller->UpdateState(true, events.get());
765   EXPECT_TRUE(controller->HasActiveAnimation());
766   EXPECT_EQ(0.f, dummy.opacity());
767   controller->Animate(1.0);
768   controller->UpdateState(true, events.get());
769   EXPECT_TRUE(controller->HasActiveAnimation());
770   EXPECT_EQ(0.f, dummy.opacity());
771   controller->Animate(2.0);
772   controller->UpdateState(true, events.get());
773   EXPECT_EQ(1.f, dummy.opacity());
774   EXPECT_FALSE(controller->HasActiveAnimation());
775 }
776
777 // Tests scheduling an animation to start in the future that's interrupting a
778 // running animation.
779 TEST(LayerAnimationControllerTest,
780      ScheduledAnimationInterruptsRunningAnimation) {
781   scoped_ptr<AnimationEventsVector> events(
782       make_scoped_ptr(new AnimationEventsVector));
783   FakeLayerAnimationValueObserver dummy;
784   scoped_refptr<LayerAnimationController> controller(
785       LayerAnimationController::Create(0));
786   controller->AddValueObserver(&dummy);
787
788   controller->AddAnimation(CreateAnimation(
789       scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
790       1,
791       Animation::Opacity));
792
793   scoped_ptr<Animation> to_add(CreateAnimation(
794       scoped_ptr<AnimationCurve>(
795           new FakeFloatTransition(1.0, 0.5f, 0.f)).Pass(),
796       2,
797       Animation::Opacity));
798   to_add->SetRunState(Animation::WaitingForStartTime, 0);
799   to_add->set_start_time(1.f);
800   controller->AddAnimation(to_add.Pass());
801
802   // First 2s opacity transition should start immediately.
803   controller->Animate(0.0);
804   controller->UpdateState(true, events.get());
805   EXPECT_TRUE(controller->HasActiveAnimation());
806   EXPECT_EQ(0.f, dummy.opacity());
807   controller->Animate(0.5);
808   controller->UpdateState(true, events.get());
809   EXPECT_TRUE(controller->HasActiveAnimation());
810   EXPECT_EQ(0.25f, dummy.opacity());
811   controller->Animate(1.0);
812   controller->UpdateState(true, events.get());
813   EXPECT_TRUE(controller->HasActiveAnimation());
814   EXPECT_EQ(0.5f, dummy.opacity());
815   controller->Animate(2.0);
816   controller->UpdateState(true, events.get());
817   EXPECT_EQ(0.f, dummy.opacity());
818   EXPECT_FALSE(controller->HasActiveAnimation());
819 }
820
821 // Tests scheduling an animation to start in the future that interrupts a
822 // running animation and there is yet another animation queued to start later.
823 TEST(LayerAnimationControllerTest,
824      ScheduledAnimationInterruptsRunningAnimationWithAnimInQueue) {
825   scoped_ptr<AnimationEventsVector> events(
826       make_scoped_ptr(new AnimationEventsVector));
827   FakeLayerAnimationValueObserver dummy;
828   scoped_refptr<LayerAnimationController> controller(
829       LayerAnimationController::Create(0));
830   controller->AddValueObserver(&dummy);
831
832   controller->AddAnimation(CreateAnimation(
833       scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
834       1,
835       Animation::Opacity));
836
837   scoped_ptr<Animation> to_add(CreateAnimation(
838       scoped_ptr<AnimationCurve>(
839           new FakeFloatTransition(2.0, 0.5f, 0.f)).Pass(),
840       2,
841       Animation::Opacity));
842   to_add->SetRunState(Animation::WaitingForStartTime, 0);
843   to_add->set_start_time(1.f);
844   controller->AddAnimation(to_add.Pass());
845
846   controller->AddAnimation(CreateAnimation(
847       scoped_ptr<AnimationCurve>(
848           new FakeFloatTransition(1.0, 0.f, 0.75f)).Pass(),
849       3,
850       Animation::Opacity));
851
852   // First 2s opacity transition should start immediately.
853   controller->Animate(0.0);
854   controller->UpdateState(true, events.get());
855   EXPECT_TRUE(controller->HasActiveAnimation());
856   EXPECT_EQ(0.f, dummy.opacity());
857   controller->Animate(0.5);
858   controller->UpdateState(true, events.get());
859   EXPECT_TRUE(controller->HasActiveAnimation());
860   EXPECT_EQ(0.25f, dummy.opacity());
861   EXPECT_TRUE(controller->HasActiveAnimation());
862   controller->Animate(1.0);
863   controller->UpdateState(true, events.get());
864   EXPECT_TRUE(controller->HasActiveAnimation());
865   EXPECT_EQ(0.5f, dummy.opacity());
866   controller->Animate(3.0);
867   controller->UpdateState(true, events.get());
868   EXPECT_TRUE(controller->HasActiveAnimation());
869   EXPECT_EQ(0.f, dummy.opacity());
870   controller->Animate(4.0);
871   controller->UpdateState(true, events.get());
872   EXPECT_EQ(0.75f, dummy.opacity());
873   EXPECT_FALSE(controller->HasActiveAnimation());
874 }
875
876 // Test that a looping animation loops and for the correct number of iterations.
877 TEST(LayerAnimationControllerTest, TrivialLooping) {
878   scoped_ptr<AnimationEventsVector> events(
879       make_scoped_ptr(new AnimationEventsVector));
880   FakeLayerAnimationValueObserver dummy;
881   scoped_refptr<LayerAnimationController> controller(
882       LayerAnimationController::Create(0));
883   controller->AddValueObserver(&dummy);
884
885   scoped_ptr<Animation> to_add(CreateAnimation(
886       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
887       1,
888       Animation::Opacity));
889   to_add->set_iterations(3);
890   controller->AddAnimation(to_add.Pass());
891
892   controller->Animate(0.0);
893   controller->UpdateState(true, events.get());
894   EXPECT_TRUE(controller->HasActiveAnimation());
895   EXPECT_EQ(0.f, dummy.opacity());
896   controller->Animate(1.25);
897   controller->UpdateState(true, events.get());
898   EXPECT_TRUE(controller->HasActiveAnimation());
899   EXPECT_EQ(0.25f, dummy.opacity());
900   controller->Animate(1.75);
901   controller->UpdateState(true, events.get());
902   EXPECT_TRUE(controller->HasActiveAnimation());
903   EXPECT_EQ(0.75f, dummy.opacity());
904   controller->Animate(2.25);
905   controller->UpdateState(true, events.get());
906   EXPECT_TRUE(controller->HasActiveAnimation());
907   EXPECT_EQ(0.25f, dummy.opacity());
908   controller->Animate(2.75);
909   controller->UpdateState(true, events.get());
910   EXPECT_TRUE(controller->HasActiveAnimation());
911   EXPECT_EQ(0.75f, dummy.opacity());
912   controller->Animate(3.0);
913   controller->UpdateState(true, events.get());
914   EXPECT_FALSE(controller->HasActiveAnimation());
915   EXPECT_EQ(1.f, dummy.opacity());
916
917   // Just be extra sure.
918   controller->Animate(4.0);
919   controller->UpdateState(true, events.get());
920   EXPECT_EQ(1.f, dummy.opacity());
921 }
922
923 // Test that an infinitely looping animation does indeed go until aborted.
924 TEST(LayerAnimationControllerTest, InfiniteLooping) {
925   scoped_ptr<AnimationEventsVector> events(
926       make_scoped_ptr(new AnimationEventsVector));
927   FakeLayerAnimationValueObserver dummy;
928   scoped_refptr<LayerAnimationController> controller(
929       LayerAnimationController::Create(0));
930   controller->AddValueObserver(&dummy);
931
932   const int id = 1;
933   scoped_ptr<Animation> to_add(CreateAnimation(
934       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
935       id,
936       Animation::Opacity));
937   to_add->set_iterations(-1);
938   controller->AddAnimation(to_add.Pass());
939
940   controller->Animate(0.0);
941   controller->UpdateState(true, events.get());
942   EXPECT_TRUE(controller->HasActiveAnimation());
943   EXPECT_EQ(0.f, dummy.opacity());
944   controller->Animate(1.25);
945   controller->UpdateState(true, events.get());
946   EXPECT_TRUE(controller->HasActiveAnimation());
947   EXPECT_EQ(0.25f, dummy.opacity());
948   controller->Animate(1.75);
949   controller->UpdateState(true, events.get());
950   EXPECT_TRUE(controller->HasActiveAnimation());
951   EXPECT_EQ(0.75f, dummy.opacity());
952
953   controller->Animate(1073741824.25);
954   controller->UpdateState(true, events.get());
955   EXPECT_TRUE(controller->HasActiveAnimation());
956   EXPECT_EQ(0.25f, dummy.opacity());
957   controller->Animate(1073741824.75);
958   controller->UpdateState(true, events.get());
959   EXPECT_TRUE(controller->HasActiveAnimation());
960   EXPECT_EQ(0.75f, dummy.opacity());
961
962   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
963   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
964       Animation::Aborted, 0.75);
965   EXPECT_FALSE(controller->HasActiveAnimation());
966   EXPECT_EQ(0.75f, dummy.opacity());
967 }
968
969 // Test that pausing and resuming work as expected.
970 TEST(LayerAnimationControllerTest, PauseResume) {
971   scoped_ptr<AnimationEventsVector> events(
972       make_scoped_ptr(new AnimationEventsVector));
973   FakeLayerAnimationValueObserver dummy;
974   scoped_refptr<LayerAnimationController> controller(
975       LayerAnimationController::Create(0));
976   controller->AddValueObserver(&dummy);
977
978   const int id = 1;
979   controller->AddAnimation(CreateAnimation(
980       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
981       id,
982       Animation::Opacity));
983
984   controller->Animate(0.0);
985   controller->UpdateState(true, events.get());
986   EXPECT_TRUE(controller->HasActiveAnimation());
987   EXPECT_EQ(0.f, dummy.opacity());
988   controller->Animate(0.5);
989   controller->UpdateState(true, events.get());
990   EXPECT_TRUE(controller->HasActiveAnimation());
991   EXPECT_EQ(0.5f, dummy.opacity());
992
993   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
994   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
995       Animation::Paused, 0.5);
996
997   controller->Animate(1024);
998   controller->UpdateState(true, events.get());
999   EXPECT_TRUE(controller->HasActiveAnimation());
1000   EXPECT_EQ(0.5f, dummy.opacity());
1001
1002   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1003   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1004       Animation::Running, 1024);
1005
1006   controller->Animate(1024.25);
1007   controller->UpdateState(true, events.get());
1008   EXPECT_TRUE(controller->HasActiveAnimation());
1009   EXPECT_EQ(0.75f, dummy.opacity());
1010   controller->Animate(1024.5);
1011   controller->UpdateState(true, events.get());
1012   EXPECT_FALSE(controller->HasActiveAnimation());
1013   EXPECT_EQ(1.f, dummy.opacity());
1014 }
1015
1016 TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1017   scoped_ptr<AnimationEventsVector> events(
1018       make_scoped_ptr(new AnimationEventsVector));
1019   FakeLayerAnimationValueObserver dummy;
1020   scoped_refptr<LayerAnimationController> controller(
1021       LayerAnimationController::Create(0));
1022   controller->AddValueObserver(&dummy);
1023
1024   const int id = 1;
1025   controller->AddAnimation(CreateAnimation(
1026       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1027       id,
1028       Animation::Transform));
1029   controller->AddAnimation(CreateAnimation(
1030       scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1031       id,
1032       Animation::Opacity));
1033   controller->AddAnimation(CreateAnimation(
1034       scoped_ptr<AnimationCurve>(
1035           new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
1036       2,
1037       Animation::Opacity));
1038
1039   controller->Animate(0.0);
1040   controller->UpdateState(true, events.get());
1041   EXPECT_TRUE(controller->HasActiveAnimation());
1042   EXPECT_EQ(0.f, dummy.opacity());
1043   controller->Animate(1.0);
1044   controller->UpdateState(true, events.get());
1045   EXPECT_TRUE(controller->HasActiveAnimation());
1046   EXPECT_EQ(0.5f, dummy.opacity());
1047
1048   EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1049   controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1050       Animation::Aborted, 1);
1051   controller->Animate(1.0);
1052   controller->UpdateState(true, events.get());
1053   EXPECT_TRUE(controller->HasActiveAnimation());
1054   EXPECT_EQ(1.f, dummy.opacity());
1055   controller->Animate(2.0);
1056   controller->UpdateState(true, events.get());
1057   EXPECT_TRUE(!controller->HasActiveAnimation());
1058   EXPECT_EQ(0.75f, dummy.opacity());
1059 }
1060
1061 TEST(LayerAnimationControllerTest, ForceSyncWhenSynchronizedStartTimeNeeded) {
1062   FakeLayerAnimationValueObserver dummy_impl;
1063   scoped_refptr<LayerAnimationController> controller_impl(
1064       LayerAnimationController::Create(0));
1065   controller_impl->AddValueObserver(&dummy_impl);
1066   scoped_ptr<AnimationEventsVector> events(
1067       make_scoped_ptr(new AnimationEventsVector));
1068   FakeLayerAnimationValueObserver dummy;
1069   scoped_refptr<LayerAnimationController> controller(
1070       LayerAnimationController::Create(0));
1071   controller->AddValueObserver(&dummy);
1072
1073   scoped_ptr<Animation> to_add(CreateAnimation(
1074       scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1075       0,
1076       Animation::Opacity));
1077   to_add->set_needs_synchronized_start_time(true);
1078   controller->AddAnimation(to_add.Pass());
1079
1080   controller->Animate(0.0);
1081   controller->UpdateState(true, events.get());
1082   EXPECT_TRUE(controller->HasActiveAnimation());
1083   Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
1084   EXPECT_TRUE(active_animation);
1085   EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1086
1087   controller->set_force_sync();
1088
1089   controller->PushAnimationUpdatesTo(controller_impl.get());
1090
1091   active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
1092   EXPECT_TRUE(active_animation);
1093   EXPECT_EQ(Animation::WaitingForTargetAvailability,
1094             active_animation->run_state());
1095 }
1096
1097 // Tests that skipping a call to UpdateState works as expected.
1098 TEST(LayerAnimationControllerTest, SkipUpdateState) {
1099   scoped_ptr<AnimationEventsVector> events(
1100       make_scoped_ptr(new AnimationEventsVector));
1101   FakeLayerAnimationValueObserver dummy;
1102   scoped_refptr<LayerAnimationController> controller(
1103       LayerAnimationController::Create(0));
1104   controller->AddValueObserver(&dummy);
1105
1106   controller->AddAnimation(CreateAnimation(
1107       scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1108       1,
1109       Animation::Transform));
1110
1111   controller->Animate(0.0);
1112   controller->UpdateState(true, events.get());
1113
1114   controller->AddAnimation(CreateAnimation(
1115       scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1116       2,
1117       Animation::Opacity));
1118
1119   // Animate but don't UpdateState.
1120   controller->Animate(1.0);
1121
1122   controller->Animate(2.0);
1123   events.reset(new AnimationEventsVector);
1124   controller->UpdateState(true, events.get());
1125
1126   // Should have one Started event and one Finished event.
1127   EXPECT_EQ(2u, events->size());
1128   EXPECT_NE((*events)[0].type, (*events)[1].type);
1129
1130   // The float transition should still be at its starting point.
1131   EXPECT_TRUE(controller->HasActiveAnimation());
1132   EXPECT_EQ(0.f, dummy.opacity());
1133
1134   controller->Animate(3.0);
1135   controller->UpdateState(true, events.get());
1136
1137   // The float tranisition should now be done.
1138   EXPECT_EQ(1.f, dummy.opacity());
1139   EXPECT_FALSE(controller->HasActiveAnimation());
1140 }
1141
1142 // Tests that an animation controller with only an inactive observer gets ticked
1143 // but doesn't progress animations past the Starting state.
1144 TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1145   scoped_ptr<AnimationEventsVector> events(
1146       make_scoped_ptr(new AnimationEventsVector));
1147   FakeLayerAnimationValueObserver dummy;
1148   FakeInactiveLayerAnimationValueObserver inactive_dummy;
1149   scoped_refptr<LayerAnimationController> controller(
1150       LayerAnimationController::Create(0));
1151
1152   const int id = 1;
1153   controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>(
1154       new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(),
1155       id,
1156       Animation::Opacity));
1157
1158   // Without an observer, the animation shouldn't progress to the Starting
1159   // state.
1160   controller->Animate(0.0);
1161   controller->UpdateState(true, events.get());
1162   EXPECT_EQ(0u, events->size());
1163   EXPECT_EQ(Animation::WaitingForTargetAvailability,
1164             controller->GetAnimation(id, Animation::Opacity)->run_state());
1165
1166   controller->AddValueObserver(&inactive_dummy);
1167
1168   // With only an inactive observer, the animation should progress to the
1169   // Starting state and get ticked at its starting point, but should not
1170   // progress to Running.
1171   controller->Animate(1.0);
1172   controller->UpdateState(true, events.get());
1173   EXPECT_EQ(0u, events->size());
1174   EXPECT_EQ(Animation::Starting,
1175             controller->GetAnimation(id, Animation::Opacity)->run_state());
1176   EXPECT_EQ(0.5f, inactive_dummy.opacity());
1177
1178   // Even when already in the Starting state, the animation should stay
1179   // there, and shouldn't be ticked past its starting point.
1180   controller->Animate(2.0);
1181   controller->UpdateState(true, events.get());
1182   EXPECT_EQ(0u, events->size());
1183   EXPECT_EQ(Animation::Starting,
1184             controller->GetAnimation(id, Animation::Opacity)->run_state());
1185   EXPECT_EQ(0.5f, inactive_dummy.opacity());
1186
1187   controller->AddValueObserver(&dummy);
1188
1189   // Now that an active observer has been added, the animation should still
1190   // initially tick at its starting point, but should now progress to Running.
1191   controller->Animate(3.0);
1192   controller->UpdateState(true, events.get());
1193   EXPECT_EQ(1u, events->size());
1194   EXPECT_EQ(Animation::Running,
1195             controller->GetAnimation(id, Animation::Opacity)->run_state());
1196   EXPECT_EQ(0.5f, inactive_dummy.opacity());
1197   EXPECT_EQ(0.5f, dummy.opacity());
1198
1199   // The animation should now tick past its starting point.
1200   controller->Animate(3.5);
1201   EXPECT_NE(0.5f, inactive_dummy.opacity());
1202   EXPECT_NE(0.5f, dummy.opacity());
1203 }
1204
1205 TEST(LayerAnimationControllerTest, AnimatedBounds) {
1206   scoped_refptr<LayerAnimationController> controller_impl(
1207       LayerAnimationController::Create(0));
1208
1209   scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1210       KeyframedTransformAnimationCurve::Create());
1211
1212   TransformOperations operations1;
1213   curve1->AddKeyframe(TransformKeyframe::Create(
1214       0.0, operations1, scoped_ptr<TimingFunction>()));
1215   operations1.AppendTranslate(10.0, 15.0, 0.0);
1216   curve1->AddKeyframe(TransformKeyframe::Create(
1217       1.0, operations1, scoped_ptr<TimingFunction>()));
1218
1219   scoped_ptr<Animation> animation(Animation::Create(
1220       curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1221   controller_impl->AddAnimation(animation.Pass());
1222
1223   scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1224       KeyframedTransformAnimationCurve::Create());
1225
1226   TransformOperations operations2;
1227   curve2->AddKeyframe(TransformKeyframe::Create(
1228       0.0, operations2, scoped_ptr<TimingFunction>()));
1229   operations2.AppendScale(2.0, 3.0, 4.0);
1230   curve2->AddKeyframe(TransformKeyframe::Create(
1231       1.0, operations2, scoped_ptr<TimingFunction>()));
1232
1233   animation = Animation::Create(
1234       curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
1235   controller_impl->AddAnimation(animation.Pass());
1236
1237   gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1238   gfx::BoxF bounds;
1239
1240   EXPECT_TRUE(controller_impl->AnimatedBoundsForBox(box, &bounds));
1241   EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1242             bounds.ToString());
1243
1244   controller_impl->GetAnimation(1, Animation::Transform)->SetRunState(
1245       cc::Animation::Finished, 0.0);
1246
1247   // Only the unfinished animation should affect the animated bounds.
1248   EXPECT_TRUE(controller_impl->AnimatedBoundsForBox(box, &bounds));
1249   EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1250             bounds.ToString());
1251
1252   controller_impl->GetAnimation(2, Animation::Transform)->SetRunState(
1253       cc::Animation::Finished, 0.0);
1254
1255   // There are no longer any running animations.
1256   EXPECT_TRUE(controller_impl->AnimatedBoundsForBox(box, &bounds));
1257   EXPECT_EQ(gfx::BoxF().ToString(), bounds.ToString());
1258
1259   // Add an animation whose bounds we don't yet support computing.
1260   scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1261       KeyframedTransformAnimationCurve::Create());
1262   TransformOperations operations3;
1263   curve3->AddKeyframe(TransformKeyframe::Create(
1264       0.0, operations3, scoped_ptr<TimingFunction>()));
1265   operations3.AppendSkew(1.0, 2.0);
1266   curve3->AddKeyframe(TransformKeyframe::Create(
1267       1.0, operations3, scoped_ptr<TimingFunction>()));
1268   animation = Animation::Create(
1269       curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1270   controller_impl->AddAnimation(animation.Pass());
1271   EXPECT_FALSE(controller_impl->AnimatedBoundsForBox(box, &bounds));
1272 }
1273
1274 }  // namespace
1275 }  // namespace cc