layer_animation_controller_unittest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "cc/animation/layer_animation_controller.h"
6
7#include "cc/animation/animation.h"
8#include "cc/animation/animation_curve.h"
9#include "cc/animation/animation_delegate.h"
10#include "cc/animation/animation_registrar.h"
11#include "cc/animation/keyframed_animation_curve.h"
12#include "cc/animation/scroll_offset_animation_curve.h"
13#include "cc/animation/transform_operations.h"
14#include "cc/test/animation_test_common.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17#include "ui/gfx/box_f.h"
18#include "ui/gfx/transform.h"
19
20namespace cc {
21namespace {
22
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.
26const double kInitialTickTime = 1.0;
27
28scoped_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
34TEST(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  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
47  int group_id = controller->GetAnimation(Animation::Opacity)->group();
48
49  controller->PushAnimationUpdatesTo(controller_impl.get());
50
51  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
52  EXPECT_EQ(Animation::WaitingForTargetAvailability,
53            controller_impl->GetAnimation(group_id,
54                                          Animation::Opacity)->run_state());
55}
56
57// If an animation is started on the impl thread before it is ticked on the main
58// thread, we must be sure to respect the synchronized start time.
59TEST(LayerAnimationControllerTest, DoNotClobberStartTimes) {
60  FakeLayerAnimationValueObserver dummy_impl;
61  scoped_refptr<LayerAnimationController> controller_impl(
62      LayerAnimationController::Create(0));
63  controller_impl->AddValueObserver(&dummy_impl);
64  FakeLayerAnimationValueObserver dummy;
65  scoped_refptr<LayerAnimationController> controller(
66      LayerAnimationController::Create(0));
67  controller->AddValueObserver(&dummy);
68
69  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
70
71  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
72  int group_id = controller->GetAnimation(Animation::Opacity)->group();
73
74  controller->PushAnimationUpdatesTo(controller_impl.get());
75
76  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
77  EXPECT_EQ(Animation::WaitingForTargetAvailability,
78            controller_impl->GetAnimation(group_id,
79                                          Animation::Opacity)->run_state());
80
81  AnimationEventsVector events;
82  controller_impl->Animate(kInitialTickTime);
83  controller_impl->UpdateState(true, &events);
84
85  // Synchronize the start times.
86  EXPECT_EQ(1u, events.size());
87  controller->NotifyAnimationStarted(events[0]);
88  EXPECT_EQ(controller->GetAnimation(group_id,
89                                     Animation::Opacity)->start_time(),
90            controller_impl->GetAnimation(group_id,
91                                          Animation::Opacity)->start_time());
92
93  // Start the animation on the main thread. Should not affect the start time.
94  controller->Animate(kInitialTickTime + 0.5);
95  controller->UpdateState(true, NULL);
96  EXPECT_EQ(controller->GetAnimation(group_id,
97                                     Animation::Opacity)->start_time(),
98            controller_impl->GetAnimation(group_id,
99                                          Animation::Opacity)->start_time());
100}
101
102// Tests that controllers activate and deactivate as expected.
103TEST(LayerAnimationControllerTest, Activation) {
104  scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
105  scoped_ptr<AnimationRegistrar> registrar_impl = AnimationRegistrar::Create();
106
107  FakeLayerAnimationValueObserver dummy_impl;
108  scoped_refptr<LayerAnimationController> controller_impl(
109      LayerAnimationController::Create(0));
110  controller_impl->AddValueObserver(&dummy_impl);
111  FakeLayerAnimationValueObserver dummy;
112  scoped_refptr<LayerAnimationController> controller(
113      LayerAnimationController::Create(0));
114  controller->AddValueObserver(&dummy);
115  scoped_ptr<AnimationEventsVector> events(
116      make_scoped_ptr(new AnimationEventsVector));
117
118  controller->SetAnimationRegistrar(registrar.get());
119  controller_impl->SetAnimationRegistrar(registrar_impl.get());
120  EXPECT_EQ(1u, registrar->all_animation_controllers().size());
121  EXPECT_EQ(1u, registrar_impl->all_animation_controllers().size());
122
123  // Initially, both controllers should be inactive.
124  EXPECT_EQ(0u, registrar->active_animation_controllers().size());
125  EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
126
127  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
128  // The main thread controller should now be active.
129  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
130
131  controller->PushAnimationUpdatesTo(controller_impl.get());
132  // Both controllers should now be active.
133  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
134  EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
135
136  controller_impl->Animate(kInitialTickTime);
137  controller_impl->UpdateState(true, events.get());
138  EXPECT_EQ(1u, events->size());
139  controller->NotifyAnimationStarted((*events)[0]);
140
141  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
142  EXPECT_EQ(1u, registrar_impl->active_animation_controllers().size());
143
144  controller->Animate(kInitialTickTime + 0.5);
145  controller->UpdateState(true, NULL);
146  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
147
148  controller->Animate(kInitialTickTime + 1.0);
149  controller->UpdateState(true, NULL);
150  EXPECT_EQ(Animation::Finished,
151            controller->GetAnimation(Animation::Opacity)->run_state());
152  EXPECT_EQ(1u, registrar->active_animation_controllers().size());
153
154  events.reset(new AnimationEventsVector);
155  controller_impl->Animate(kInitialTickTime + 1.5);
156  controller_impl->UpdateState(true, events.get());
157
158  EXPECT_EQ(Animation::WaitingForDeletion,
159            controller_impl->GetAnimation(Animation::Opacity)->run_state());
160  // The impl thread controller should have de-activated.
161  EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
162
163  EXPECT_EQ(1u, events->size());
164  controller->NotifyAnimationFinished((*events)[0]);
165  controller->Animate(kInitialTickTime + 1.5);
166  controller->UpdateState(true, NULL);
167
168  EXPECT_EQ(Animation::WaitingForDeletion,
169            controller->GetAnimation(Animation::Opacity)->run_state());
170  // The main thread controller should have de-activated.
171  EXPECT_EQ(0u, registrar->active_animation_controllers().size());
172
173  controller->PushAnimationUpdatesTo(controller_impl.get());
174  EXPECT_FALSE(controller->has_any_animation());
175  EXPECT_FALSE(controller_impl->has_any_animation());
176  EXPECT_EQ(0u, registrar->active_animation_controllers().size());
177  EXPECT_EQ(0u, registrar_impl->active_animation_controllers().size());
178
179  controller->SetAnimationRegistrar(NULL);
180  controller_impl->SetAnimationRegistrar(NULL);
181}
182
183TEST(LayerAnimationControllerTest, SyncPause) {
184  FakeLayerAnimationValueObserver dummy_impl;
185  scoped_refptr<LayerAnimationController> controller_impl(
186      LayerAnimationController::Create(0));
187  controller_impl->AddValueObserver(&dummy_impl);
188  FakeLayerAnimationValueObserver dummy;
189  scoped_refptr<LayerAnimationController> controller(
190      LayerAnimationController::Create(0));
191  controller->AddValueObserver(&dummy);
192
193  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
194
195  AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
196  int group_id = controller->GetAnimation(Animation::Opacity)->group();
197  int animation_id = controller->GetAnimation(Animation::Opacity)->id();
198
199  controller->PushAnimationUpdatesTo(controller_impl.get());
200
201  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
202  EXPECT_EQ(Animation::WaitingForTargetAvailability,
203            controller_impl->GetAnimation(group_id,
204                                          Animation::Opacity)->run_state());
205
206  // Start the animations on each controller.
207  AnimationEventsVector events;
208  controller_impl->Animate(kInitialTickTime);
209  controller_impl->UpdateState(true, &events);
210  controller->Animate(kInitialTickTime);
211  controller->UpdateState(true, NULL);
212  EXPECT_EQ(Animation::Running,
213            controller_impl->GetAnimation(group_id,
214                                          Animation::Opacity)->run_state());
215  EXPECT_EQ(Animation::Running,
216            controller->GetAnimation(group_id,
217                                     Animation::Opacity)->run_state());
218
219  // Pause the main-thread animation.
220  controller->PauseAnimation(animation_id, kInitialTickTime + 1.0);
221  EXPECT_EQ(Animation::Paused,
222            controller->GetAnimation(group_id,
223                                     Animation::Opacity)->run_state());
224
225  // The pause run state change should make it to the impl thread controller.
226  controller->PushAnimationUpdatesTo(controller_impl.get());
227  EXPECT_EQ(Animation::Paused,
228            controller_impl->GetAnimation(group_id,
229                                          Animation::Opacity)->run_state());
230}
231
232TEST(LayerAnimationControllerTest, DoNotSyncFinishedAnimation) {
233  FakeLayerAnimationValueObserver dummy_impl;
234  scoped_refptr<LayerAnimationController> controller_impl(
235      LayerAnimationController::Create(0));
236  controller_impl->AddValueObserver(&dummy_impl);
237  FakeLayerAnimationValueObserver dummy;
238  scoped_refptr<LayerAnimationController> controller(
239      LayerAnimationController::Create(0));
240  controller->AddValueObserver(&dummy);
241
242  EXPECT_FALSE(controller_impl->GetAnimation(Animation::Opacity));
243
244  int animation_id =
245      AddOpacityTransitionToController(controller.get(), 1, 0, 1, false);
246  int group_id = controller->GetAnimation(Animation::Opacity)->group();
247
248  controller->PushAnimationUpdatesTo(controller_impl.get());
249
250  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
251  EXPECT_EQ(Animation::WaitingForTargetAvailability,
252            controller_impl->GetAnimation(group_id,
253                                          Animation::Opacity)->run_state());
254
255  // Notify main thread controller that the animation has started.
256  AnimationEvent animation_started_event(AnimationEvent::Started,
257                                         0,
258                                         group_id,
259                                         Animation::Opacity,
260                                         kInitialTickTime);
261  controller->NotifyAnimationStarted(animation_started_event);
262
263  // Force animation to complete on impl thread.
264  controller_impl->RemoveAnimation(animation_id);
265
266  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
267
268  controller->PushAnimationUpdatesTo(controller_impl.get());
269
270  // Even though the main thread has a 'new' animation, it should not be pushed
271  // because the animation has already completed on the impl thread.
272  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
273}
274
275// Ensure that a finished animation is eventually deleted by both the
276// main-thread and the impl-thread controllers.
277TEST(LayerAnimationControllerTest, AnimationsAreDeleted) {
278  FakeLayerAnimationValueObserver dummy;
279  FakeLayerAnimationValueObserver dummy_impl;
280  scoped_ptr<AnimationEventsVector> events(
281      make_scoped_ptr(new AnimationEventsVector));
282  scoped_refptr<LayerAnimationController> controller(
283      LayerAnimationController::Create(0));
284  scoped_refptr<LayerAnimationController> controller_impl(
285      LayerAnimationController::Create(0));
286  controller->AddValueObserver(&dummy);
287  controller_impl->AddValueObserver(&dummy_impl);
288
289  AddOpacityTransitionToController(controller.get(), 1.0, 0.0f, 1.0f, false);
290  controller->Animate(kInitialTickTime);
291  controller->UpdateState(true, NULL);
292  controller->PushAnimationUpdatesTo(controller_impl.get());
293
294  controller_impl->Animate(kInitialTickTime + 0.5);
295  controller_impl->UpdateState(true, events.get());
296
297  // There should be a Started event for the animation.
298  EXPECT_EQ(1u, events->size());
299  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
300  controller->NotifyAnimationStarted((*events)[0]);
301
302  controller->Animate(kInitialTickTime + 1.0);
303  controller->UpdateState(true, NULL);
304
305  EXPECT_FALSE(dummy.animation_waiting_for_deletion());
306  EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
307
308  events.reset(new AnimationEventsVector);
309  controller_impl->Animate(kInitialTickTime + 2.0);
310  controller_impl->UpdateState(true, events.get());
311
312  EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
313
314  // There should be a Finished event for the animation.
315  EXPECT_EQ(1u, events->size());
316  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
317
318  // Neither controller should have deleted the animation yet.
319  EXPECT_TRUE(controller->GetAnimation(Animation::Opacity));
320  EXPECT_TRUE(controller_impl->GetAnimation(Animation::Opacity));
321
322  controller->NotifyAnimationFinished((*events)[0]);
323
324  controller->Animate(kInitialTickTime + 3.0);
325  controller->UpdateState(true, NULL);
326  EXPECT_TRUE(dummy.animation_waiting_for_deletion());
327
328  controller->PushAnimationUpdatesTo(controller_impl.get());
329
330  // Both controllers should now have deleted the animation.
331  EXPECT_FALSE(controller->has_any_animation());
332  EXPECT_FALSE(controller_impl->has_any_animation());
333}
334
335// Tests that transitioning opacity from 0 to 1 works as expected.
336
337static const AnimationEvent* GetMostRecentPropertyUpdateEvent(
338    const AnimationEventsVector* events) {
339  const AnimationEvent* event = 0;
340  for (size_t i = 0; i < events->size(); ++i)
341    if ((*events)[i].type == AnimationEvent::PropertyUpdate)
342      event = &(*events)[i];
343
344  return event;
345}
346
347TEST(LayerAnimationControllerTest, TrivialTransition) {
348  scoped_ptr<AnimationEventsVector> events(
349      make_scoped_ptr(new AnimationEventsVector));
350  FakeLayerAnimationValueObserver dummy;
351  scoped_refptr<LayerAnimationController> controller(
352      LayerAnimationController::Create(0));
353  controller->AddValueObserver(&dummy);
354
355  scoped_ptr<Animation> to_add(CreateAnimation(
356      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
357      1,
358      Animation::Opacity));
359
360  controller->AddAnimation(to_add.Pass());
361  controller->Animate(kInitialTickTime);
362  controller->UpdateState(true, events.get());
363  EXPECT_TRUE(controller->HasActiveAnimation());
364  EXPECT_EQ(0.f, dummy.opacity());
365  // A non-impl-only animation should not generate property updates.
366  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
367  EXPECT_FALSE(event);
368  controller->Animate(kInitialTickTime + 1.0);
369  controller->UpdateState(true, events.get());
370  EXPECT_EQ(1.f, dummy.opacity());
371  EXPECT_FALSE(controller->HasActiveAnimation());
372  event = GetMostRecentPropertyUpdateEvent(events.get());
373  EXPECT_FALSE(event);
374}
375
376TEST(LayerAnimationControllerTest, TrivialTransitionOnImpl) {
377  scoped_ptr<AnimationEventsVector> events(
378      make_scoped_ptr(new AnimationEventsVector));
379  FakeLayerAnimationValueObserver dummy_impl;
380  scoped_refptr<LayerAnimationController> controller_impl(
381      LayerAnimationController::Create(0));
382  controller_impl->AddValueObserver(&dummy_impl);
383
384  scoped_ptr<Animation> to_add(CreateAnimation(
385      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
386      1,
387      Animation::Opacity));
388  to_add->set_is_impl_only(true);
389
390  controller_impl->AddAnimation(to_add.Pass());
391  controller_impl->Animate(kInitialTickTime);
392  controller_impl->UpdateState(true, events.get());
393  EXPECT_TRUE(controller_impl->HasActiveAnimation());
394  EXPECT_EQ(0.f, dummy_impl.opacity());
395  EXPECT_EQ(2u, events->size());
396  const AnimationEvent* start_opacity_event =
397      GetMostRecentPropertyUpdateEvent(events.get());
398  EXPECT_EQ(0.f, start_opacity_event->opacity);
399
400  controller_impl->Animate(kInitialTickTime + 1.0);
401  controller_impl->UpdateState(true, events.get());
402  EXPECT_EQ(1.f, dummy_impl.opacity());
403  EXPECT_FALSE(controller_impl->HasActiveAnimation());
404  EXPECT_EQ(4u, events->size());
405  const AnimationEvent* end_opacity_event =
406      GetMostRecentPropertyUpdateEvent(events.get());
407  EXPECT_EQ(1.f, end_opacity_event->opacity);
408}
409
410TEST(LayerAnimationControllerTest, TrivialTransformOnImpl) {
411  scoped_ptr<AnimationEventsVector> events(
412      make_scoped_ptr(new AnimationEventsVector));
413  FakeLayerAnimationValueObserver dummy_impl;
414  scoped_refptr<LayerAnimationController> controller_impl(
415      LayerAnimationController::Create(0));
416  controller_impl->AddValueObserver(&dummy_impl);
417
418  // Choose different values for x and y to avoid coincidental values in the
419  // observed transforms.
420  const float delta_x = 3;
421  const float delta_y = 4;
422
423  scoped_ptr<KeyframedTransformAnimationCurve> curve(
424      KeyframedTransformAnimationCurve::Create());
425
426  // Create simple Transform animation.
427  TransformOperations operations;
428  curve->AddKeyframe(
429      TransformKeyframe::Create(0, operations, scoped_ptr<TimingFunction>()));
430  operations.AppendTranslate(delta_x, delta_y, 0);
431  curve->AddKeyframe(
432      TransformKeyframe::Create(1, operations, scoped_ptr<TimingFunction>()));
433
434  scoped_ptr<Animation> animation(Animation::Create(
435      curve.PassAs<AnimationCurve>(), 1, 0, Animation::Transform));
436  animation->set_is_impl_only(true);
437  controller_impl->AddAnimation(animation.Pass());
438
439  // Run animation.
440  controller_impl->Animate(kInitialTickTime);
441  controller_impl->UpdateState(true, events.get());
442  EXPECT_TRUE(controller_impl->HasActiveAnimation());
443  EXPECT_EQ(gfx::Transform(), dummy_impl.transform());
444  EXPECT_EQ(2u, events->size());
445  const AnimationEvent* start_transform_event =
446      GetMostRecentPropertyUpdateEvent(events.get());
447  ASSERT_TRUE(start_transform_event);
448  EXPECT_EQ(gfx::Transform(), start_transform_event->transform);
449  EXPECT_TRUE(start_transform_event->is_impl_only);
450
451  gfx::Transform expected_transform;
452  expected_transform.Translate(delta_x, delta_y);
453
454  controller_impl->Animate(kInitialTickTime + 1.0);
455  controller_impl->UpdateState(true, events.get());
456  EXPECT_EQ(expected_transform, dummy_impl.transform());
457  EXPECT_FALSE(controller_impl->HasActiveAnimation());
458  EXPECT_EQ(4u, events->size());
459  const AnimationEvent* end_transform_event =
460      GetMostRecentPropertyUpdateEvent(events.get());
461  EXPECT_EQ(expected_transform, end_transform_event->transform);
462  EXPECT_TRUE(end_transform_event->is_impl_only);
463}
464
465TEST(LayerAnimationControllerTest, FilterTransition) {
466  scoped_ptr<AnimationEventsVector> events(
467      make_scoped_ptr(new AnimationEventsVector));
468  FakeLayerAnimationValueObserver dummy;
469  scoped_refptr<LayerAnimationController> controller(
470      LayerAnimationController::Create(0));
471  controller->AddValueObserver(&dummy);
472
473  scoped_ptr<KeyframedFilterAnimationCurve> curve(
474      KeyframedFilterAnimationCurve::Create());
475
476  FilterOperations start_filters;
477  start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
478  curve->AddKeyframe(
479      FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
480  FilterOperations end_filters;
481  end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
482  curve->AddKeyframe(
483      FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
484
485  scoped_ptr<Animation> animation(Animation::Create(
486      curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
487  controller->AddAnimation(animation.Pass());
488
489  controller->Animate(kInitialTickTime);
490  controller->UpdateState(true, events.get());
491  EXPECT_TRUE(controller->HasActiveAnimation());
492  EXPECT_EQ(start_filters, dummy.filters());
493  // A non-impl-only animation should not generate property updates.
494  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
495  EXPECT_FALSE(event);
496
497  controller->Animate(kInitialTickTime + 0.5);
498  controller->UpdateState(true, events.get());
499  EXPECT_EQ(1u, dummy.filters().size());
500  EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
501            dummy.filters().at(0));
502  event = GetMostRecentPropertyUpdateEvent(events.get());
503  EXPECT_FALSE(event);
504
505  controller->Animate(kInitialTickTime + 1.0);
506  controller->UpdateState(true, events.get());
507  EXPECT_EQ(end_filters, dummy.filters());
508  EXPECT_FALSE(controller->HasActiveAnimation());
509  event = GetMostRecentPropertyUpdateEvent(events.get());
510  EXPECT_FALSE(event);
511}
512
513TEST(LayerAnimationControllerTest, FilterTransitionOnImplOnly) {
514  scoped_ptr<AnimationEventsVector> events(
515      make_scoped_ptr(new AnimationEventsVector));
516  FakeLayerAnimationValueObserver dummy_impl;
517  scoped_refptr<LayerAnimationController> controller_impl(
518      LayerAnimationController::Create(0));
519  controller_impl->AddValueObserver(&dummy_impl);
520
521  scoped_ptr<KeyframedFilterAnimationCurve> curve(
522      KeyframedFilterAnimationCurve::Create());
523
524  // Create simple Filter animation.
525  FilterOperations start_filters;
526  start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
527  curve->AddKeyframe(
528      FilterKeyframe::Create(0, start_filters, scoped_ptr<TimingFunction>()));
529  FilterOperations end_filters;
530  end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
531  curve->AddKeyframe(
532      FilterKeyframe::Create(1, end_filters, scoped_ptr<TimingFunction>()));
533
534  scoped_ptr<Animation> animation(Animation::Create(
535      curve.PassAs<AnimationCurve>(), 1, 0, Animation::Filter));
536  animation->set_is_impl_only(true);
537  controller_impl->AddAnimation(animation.Pass());
538
539  // Run animation.
540  controller_impl->Animate(kInitialTickTime);
541  controller_impl->UpdateState(true, events.get());
542  EXPECT_TRUE(controller_impl->HasActiveAnimation());
543  EXPECT_EQ(start_filters, dummy_impl.filters());
544  EXPECT_EQ(2u, events->size());
545  const AnimationEvent* start_filter_event =
546      GetMostRecentPropertyUpdateEvent(events.get());
547  EXPECT_TRUE(start_filter_event);
548  EXPECT_EQ(start_filters, start_filter_event->filters);
549  EXPECT_TRUE(start_filter_event->is_impl_only);
550
551  controller_impl->Animate(kInitialTickTime + 1.0);
552  controller_impl->UpdateState(true, events.get());
553  EXPECT_EQ(end_filters, dummy_impl.filters());
554  EXPECT_FALSE(controller_impl->HasActiveAnimation());
555  EXPECT_EQ(4u, events->size());
556  const AnimationEvent* end_filter_event =
557      GetMostRecentPropertyUpdateEvent(events.get());
558  EXPECT_TRUE(end_filter_event);
559  EXPECT_EQ(end_filters, end_filter_event->filters);
560  EXPECT_TRUE(end_filter_event->is_impl_only);
561}
562
563TEST(LayerAnimationControllerTest, ScrollOffsetTransition) {
564  FakeLayerAnimationValueObserver dummy_impl;
565  FakeLayerAnimationValueProvider dummy_provider_impl;
566  scoped_refptr<LayerAnimationController> controller_impl(
567      LayerAnimationController::Create(0));
568  controller_impl->AddValueObserver(&dummy_impl);
569  controller_impl->set_value_provider(&dummy_provider_impl);
570  scoped_ptr<AnimationEventsVector> events(
571      make_scoped_ptr(new AnimationEventsVector));
572  FakeLayerAnimationValueObserver dummy;
573  FakeLayerAnimationValueProvider dummy_provider;
574  scoped_refptr<LayerAnimationController> controller(
575      LayerAnimationController::Create(0));
576  controller->AddValueObserver(&dummy);
577  controller->set_value_provider(&dummy_provider);
578
579  gfx::Vector2dF initial_value(100.f, 300.f);
580  gfx::Vector2dF target_value(300.f, 200.f);
581  scoped_ptr<ScrollOffsetAnimationCurve> curve(
582      ScrollOffsetAnimationCurve::Create(
583          target_value,
584          EaseInOutTimingFunction::Create().Pass()));
585
586  scoped_ptr<Animation> animation(Animation::Create(
587      curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
588  animation->set_needs_synchronized_start_time(true);
589  controller->AddAnimation(animation.Pass());
590
591  dummy_provider_impl.set_scroll_offset(initial_value);
592  controller->PushAnimationUpdatesTo(controller_impl.get());
593  EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
594  double duration = controller_impl->GetAnimation(
595      Animation::ScrollOffset)->curve()->Duration();
596
597  EXPECT_EQ(
598      duration,
599      controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
600
601  controller->Animate(kInitialTickTime);
602  controller->UpdateState(true, NULL);
603  EXPECT_TRUE(controller->HasActiveAnimation());
604  EXPECT_EQ(initial_value, dummy.scroll_offset());
605
606  controller_impl->Animate(kInitialTickTime);
607  controller_impl->UpdateState(true, events.get());
608  EXPECT_TRUE(controller_impl->HasActiveAnimation());
609  EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
610  // Scroll offset animations should not generate property updates.
611  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
612  EXPECT_FALSE(event);
613
614  controller->NotifyAnimationStarted((*events)[0]);
615  controller->Animate(kInitialTickTime + duration/2.0);
616  controller->UpdateState(true, NULL);
617  EXPECT_TRUE(controller->HasActiveAnimation());
618  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f), dummy.scroll_offset());
619
620  controller_impl->Animate(kInitialTickTime + duration/2.0);
621  controller_impl->UpdateState(true, events.get());
622  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
623                      dummy_impl.scroll_offset());
624  event = GetMostRecentPropertyUpdateEvent(events.get());
625  EXPECT_FALSE(event);
626
627  controller_impl->Animate(kInitialTickTime + duration);
628  controller_impl->UpdateState(true, events.get());
629  EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
630  EXPECT_FALSE(controller_impl->HasActiveAnimation());
631  event = GetMostRecentPropertyUpdateEvent(events.get());
632  EXPECT_FALSE(event);
633
634  controller->Animate(kInitialTickTime + duration);
635  controller->UpdateState(true, NULL);
636  EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
637  EXPECT_FALSE(controller->HasActiveAnimation());
638}
639
640// Ensure that when the impl controller doesn't have a value provider,
641// the main-thread controller's value provider is used to obtain the intial
642// scroll offset.
643TEST(LayerAnimationControllerTest, ScrollOffsetTransitionNoImplProvider) {
644  FakeLayerAnimationValueObserver dummy_impl;
645  scoped_refptr<LayerAnimationController> controller_impl(
646      LayerAnimationController::Create(0));
647  controller_impl->AddValueObserver(&dummy_impl);
648  scoped_ptr<AnimationEventsVector> events(
649      make_scoped_ptr(new AnimationEventsVector));
650  FakeLayerAnimationValueObserver dummy;
651  FakeLayerAnimationValueProvider dummy_provider;
652  scoped_refptr<LayerAnimationController> controller(
653      LayerAnimationController::Create(0));
654  controller->AddValueObserver(&dummy);
655  controller->set_value_provider(&dummy_provider);
656
657  gfx::Vector2dF initial_value(500.f, 100.f);
658  gfx::Vector2dF target_value(300.f, 200.f);
659  scoped_ptr<ScrollOffsetAnimationCurve> curve(
660      ScrollOffsetAnimationCurve::Create(
661          target_value,
662          EaseInOutTimingFunction::Create().Pass()));
663
664  scoped_ptr<Animation> animation(Animation::Create(
665      curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
666  animation->set_needs_synchronized_start_time(true);
667  controller->AddAnimation(animation.Pass());
668
669  dummy_provider.set_scroll_offset(initial_value);
670  controller->PushAnimationUpdatesTo(controller_impl.get());
671  EXPECT_TRUE(controller_impl->GetAnimation(Animation::ScrollOffset));
672  double duration = controller_impl->GetAnimation(
673      Animation::ScrollOffset)->curve()->Duration();
674
675  EXPECT_EQ(
676      duration,
677      controller->GetAnimation(Animation::ScrollOffset)->curve()->Duration());
678
679  controller->Animate(kInitialTickTime);
680  controller->UpdateState(true, NULL);
681  EXPECT_TRUE(controller->HasActiveAnimation());
682  EXPECT_EQ(initial_value, dummy.scroll_offset());
683
684  controller_impl->Animate(kInitialTickTime);
685  controller_impl->UpdateState(true, events.get());
686  EXPECT_TRUE(controller_impl->HasActiveAnimation());
687  EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
688  // Scroll offset animations should not generate property updates.
689  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
690  EXPECT_FALSE(event);
691
692  controller->NotifyAnimationStarted((*events)[0]);
693  controller->Animate(kInitialTickTime + duration/2.0);
694  controller->UpdateState(true, NULL);
695  EXPECT_TRUE(controller->HasActiveAnimation());
696  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f), dummy.scroll_offset());
697
698  controller_impl->Animate(kInitialTickTime + duration/2.0);
699  controller_impl->UpdateState(true, events.get());
700  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(400.f, 150.f),
701                      dummy_impl.scroll_offset());
702  event = GetMostRecentPropertyUpdateEvent(events.get());
703  EXPECT_FALSE(event);
704
705  controller_impl->Animate(kInitialTickTime + duration);
706  controller_impl->UpdateState(true, events.get());
707  EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
708  EXPECT_FALSE(controller_impl->HasActiveAnimation());
709  event = GetMostRecentPropertyUpdateEvent(events.get());
710  EXPECT_FALSE(event);
711
712  controller->Animate(kInitialTickTime + duration);
713  controller->UpdateState(true, NULL);
714  EXPECT_VECTOR2DF_EQ(target_value, dummy.scroll_offset());
715  EXPECT_FALSE(controller->HasActiveAnimation());
716}
717
718TEST(LayerAnimationControllerTest, ScrollOffsetTransitionOnImplOnly) {
719  FakeLayerAnimationValueObserver dummy_impl;
720  scoped_refptr<LayerAnimationController> controller_impl(
721      LayerAnimationController::Create(0));
722  controller_impl->AddValueObserver(&dummy_impl);
723  scoped_ptr<AnimationEventsVector> events(
724      make_scoped_ptr(new AnimationEventsVector));
725
726  gfx::Vector2dF initial_value(100.f, 300.f);
727  gfx::Vector2dF target_value(300.f, 200.f);
728  scoped_ptr<ScrollOffsetAnimationCurve> curve(
729      ScrollOffsetAnimationCurve::Create(
730          target_value,
731          EaseInOutTimingFunction::Create().Pass()));
732  curve->SetInitialValue(initial_value);
733  double duration = curve->Duration();
734
735  scoped_ptr<Animation> animation(Animation::Create(
736      curve.PassAs<AnimationCurve>(), 1, 0, Animation::ScrollOffset));
737  animation->set_is_impl_only(true);
738  controller_impl->AddAnimation(animation.Pass());
739
740  controller_impl->Animate(kInitialTickTime);
741  controller_impl->UpdateState(true, events.get());
742  EXPECT_TRUE(controller_impl->HasActiveAnimation());
743  EXPECT_EQ(initial_value, dummy_impl.scroll_offset());
744  // Scroll offset animations should not generate property updates.
745  const AnimationEvent* event = GetMostRecentPropertyUpdateEvent(events.get());
746  EXPECT_FALSE(event);
747
748  controller_impl->Animate(kInitialTickTime + duration/2.0);
749  controller_impl->UpdateState(true, events.get());
750  EXPECT_VECTOR2DF_EQ(gfx::Vector2dF(200.f, 250.f),
751                      dummy_impl.scroll_offset());
752  event = GetMostRecentPropertyUpdateEvent(events.get());
753  EXPECT_FALSE(event);
754
755  controller_impl->Animate(kInitialTickTime + duration);
756  controller_impl->UpdateState(true, events.get());
757  EXPECT_VECTOR2DF_EQ(target_value, dummy_impl.scroll_offset());
758  EXPECT_FALSE(controller_impl->HasActiveAnimation());
759  event = GetMostRecentPropertyUpdateEvent(events.get());
760  EXPECT_FALSE(event);
761}
762
763class FakeAnimationDelegate : public AnimationDelegate {
764 public:
765  FakeAnimationDelegate()
766      : started_(false),
767        finished_(false) {}
768
769  virtual void NotifyAnimationStarted(
770      base::TimeTicks monotonic_time,
771      Animation::TargetProperty target_property) OVERRIDE {
772    started_ = true;
773  }
774
775  virtual void NotifyAnimationFinished(
776      base::TimeTicks monotonic_time,
777      Animation::TargetProperty target_property) OVERRIDE {
778    finished_ = true;
779  }
780
781  bool started() { return started_; }
782
783  bool finished() { return finished_; }
784
785 private:
786  bool started_;
787  bool finished_;
788};
789
790// Tests that impl-only animations lead to start and finished notifications
791// being sent to the main thread controller's animation delegate.
792TEST(LayerAnimationControllerTest,
793     NotificationsForImplOnlyAnimationsAreSentToMainThreadDelegate) {
794  FakeLayerAnimationValueObserver dummy_impl;
795  scoped_refptr<LayerAnimationController> controller_impl(
796      LayerAnimationController::Create(0));
797  controller_impl->AddValueObserver(&dummy_impl);
798  scoped_ptr<AnimationEventsVector> events(
799      make_scoped_ptr(new AnimationEventsVector));
800  FakeLayerAnimationValueObserver dummy;
801  scoped_refptr<LayerAnimationController> controller(
802      LayerAnimationController::Create(0));
803  controller->AddValueObserver(&dummy);
804  FakeAnimationDelegate delegate;
805  controller->set_layer_animation_delegate(&delegate);
806
807  scoped_ptr<Animation> to_add(CreateAnimation(
808      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
809      1,
810      Animation::Opacity));
811  to_add->set_is_impl_only(true);
812  controller_impl->AddAnimation(to_add.Pass());
813
814  controller_impl->Animate(kInitialTickTime);
815  controller_impl->UpdateState(true, events.get());
816
817  // We should receive 2 events (a started notification and a property update).
818  EXPECT_EQ(2u, events->size());
819  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
820  EXPECT_TRUE((*events)[0].is_impl_only);
821  EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type);
822  EXPECT_TRUE((*events)[1].is_impl_only);
823
824  // Passing on the start event to the main thread controller should cause the
825  // delegate to get notified.
826  EXPECT_FALSE(delegate.started());
827  controller->NotifyAnimationStarted((*events)[0]);
828  EXPECT_TRUE(delegate.started());
829
830  events.reset(new AnimationEventsVector);
831  controller_impl->Animate(kInitialTickTime + 1.0);
832  controller_impl->UpdateState(true, events.get());
833
834  // We should receive 2 events (a finished notification and a property update).
835  EXPECT_EQ(2u, events->size());
836  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
837  EXPECT_TRUE((*events)[0].is_impl_only);
838  EXPECT_EQ(AnimationEvent::PropertyUpdate, (*events)[1].type);
839  EXPECT_TRUE((*events)[1].is_impl_only);
840
841  // Passing on the finished event to the main thread controller should cause
842  // the delegate to get notified.
843  EXPECT_FALSE(delegate.finished());
844  controller->NotifyAnimationFinished((*events)[0]);
845  EXPECT_TRUE(delegate.finished());
846}
847
848// Tests animations that are waiting for a synchronized start time do not
849// finish.
850TEST(LayerAnimationControllerTest,
851     AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
852  scoped_ptr<AnimationEventsVector> events(
853      make_scoped_ptr(new AnimationEventsVector));
854  FakeLayerAnimationValueObserver dummy;
855  scoped_refptr<LayerAnimationController> controller(
856      LayerAnimationController::Create(0));
857  controller->AddValueObserver(&dummy);
858
859  scoped_ptr<Animation> to_add(CreateAnimation(
860      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
861      1,
862      Animation::Opacity));
863  to_add->set_needs_synchronized_start_time(true);
864
865  // We should pause at the first keyframe indefinitely waiting for that
866  // animation to start.
867  controller->AddAnimation(to_add.Pass());
868  controller->Animate(kInitialTickTime);
869  controller->UpdateState(true, events.get());
870  EXPECT_TRUE(controller->HasActiveAnimation());
871  EXPECT_EQ(0.f, dummy.opacity());
872  controller->Animate(kInitialTickTime + 1.0);
873  controller->UpdateState(true, events.get());
874  EXPECT_TRUE(controller->HasActiveAnimation());
875  EXPECT_EQ(0.f, dummy.opacity());
876  controller->Animate(kInitialTickTime + 2.0);
877  controller->UpdateState(true, events.get());
878  EXPECT_TRUE(controller->HasActiveAnimation());
879  EXPECT_EQ(0.f, dummy.opacity());
880
881  // Send the synchronized start time.
882  controller->NotifyAnimationStarted(AnimationEvent(
883      AnimationEvent::Started, 0, 1, Animation::Opacity, kInitialTickTime + 2));
884  controller->Animate(kInitialTickTime + 5.0);
885  controller->UpdateState(true, events.get());
886  EXPECT_EQ(1.f, dummy.opacity());
887  EXPECT_FALSE(controller->HasActiveAnimation());
888}
889
890// Tests that two queued animations affecting the same property run in sequence.
891TEST(LayerAnimationControllerTest, TrivialQueuing) {
892  scoped_ptr<AnimationEventsVector> events(
893      make_scoped_ptr(new AnimationEventsVector));
894  FakeLayerAnimationValueObserver dummy;
895  scoped_refptr<LayerAnimationController> controller(
896      LayerAnimationController::Create(0));
897  controller->AddValueObserver(&dummy);
898
899  controller->AddAnimation(CreateAnimation(
900      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
901      1,
902      Animation::Opacity));
903  controller->AddAnimation(CreateAnimation(
904      scoped_ptr<AnimationCurve>(
905          new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
906      2,
907      Animation::Opacity));
908
909  controller->Animate(kInitialTickTime);
910  controller->UpdateState(true, events.get());
911  EXPECT_TRUE(controller->HasActiveAnimation());
912  EXPECT_EQ(0.f, dummy.opacity());
913  controller->Animate(kInitialTickTime + 1.0);
914  controller->UpdateState(true, events.get());
915  EXPECT_TRUE(controller->HasActiveAnimation());
916  EXPECT_EQ(1.f, dummy.opacity());
917  controller->Animate(kInitialTickTime + 2.0);
918  controller->UpdateState(true, events.get());
919  EXPECT_EQ(0.5f, dummy.opacity());
920  EXPECT_FALSE(controller->HasActiveAnimation());
921}
922
923// Tests interrupting a transition with another transition.
924TEST(LayerAnimationControllerTest, Interrupt) {
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  controller->AddAnimation(CreateAnimation(
932      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
933      1,
934      Animation::Opacity));
935  controller->Animate(kInitialTickTime);
936  controller->UpdateState(true, events.get());
937  EXPECT_TRUE(controller->HasActiveAnimation());
938  EXPECT_EQ(0.f, dummy.opacity());
939
940  scoped_ptr<Animation> to_add(CreateAnimation(
941      scoped_ptr<AnimationCurve>(
942          new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
943      2,
944      Animation::Opacity));
945  controller->AbortAnimations(Animation::Opacity);
946  controller->AddAnimation(to_add.Pass());
947
948  // Since the previous animation was aborted, the new animation should start
949  // right in this call to animate.
950  controller->Animate(kInitialTickTime + 0.5);
951  controller->UpdateState(true, events.get());
952  EXPECT_TRUE(controller->HasActiveAnimation());
953  EXPECT_EQ(1.f, dummy.opacity());
954  controller->Animate(kInitialTickTime + 1.5);
955  controller->UpdateState(true, events.get());
956  EXPECT_EQ(0.5f, dummy.opacity());
957  EXPECT_FALSE(controller->HasActiveAnimation());
958}
959
960// Tests scheduling two animations to run together when only one property is
961// free.
962TEST(LayerAnimationControllerTest, ScheduleTogetherWhenAPropertyIsBlocked) {
963  scoped_ptr<AnimationEventsVector> events(
964      make_scoped_ptr(new AnimationEventsVector));
965  FakeLayerAnimationValueObserver dummy;
966  scoped_refptr<LayerAnimationController> controller(
967      LayerAnimationController::Create(0));
968  controller->AddValueObserver(&dummy);
969
970  controller->AddAnimation(CreateAnimation(
971      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
972      1,
973      Animation::Transform));
974  controller->AddAnimation(CreateAnimation(
975      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
976      2,
977      Animation::Transform));
978  controller->AddAnimation(CreateAnimation(
979      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
980      2,
981      Animation::Opacity));
982
983  controller->Animate(kInitialTickTime);
984  controller->UpdateState(true, events.get());
985  EXPECT_EQ(0.f, dummy.opacity());
986  EXPECT_TRUE(controller->HasActiveAnimation());
987  controller->Animate(kInitialTickTime + 1.0);
988  controller->UpdateState(true, events.get());
989  // Should not have started the float transition yet.
990  EXPECT_TRUE(controller->HasActiveAnimation());
991  EXPECT_EQ(0.f, dummy.opacity());
992  // The float animation should have started at time 1 and should be done.
993  controller->Animate(kInitialTickTime + 2.0);
994  controller->UpdateState(true, events.get());
995  EXPECT_EQ(1.f, dummy.opacity());
996  EXPECT_FALSE(controller->HasActiveAnimation());
997}
998
999// Tests scheduling two animations to run together with different lengths and
1000// another animation queued to start when the shorter animation finishes (should
1001// wait for both to finish).
1002TEST(LayerAnimationControllerTest, ScheduleTogetherWithAnAnimWaiting) {
1003  scoped_ptr<AnimationEventsVector> events(
1004      make_scoped_ptr(new AnimationEventsVector));
1005  FakeLayerAnimationValueObserver dummy;
1006  scoped_refptr<LayerAnimationController> controller(
1007      LayerAnimationController::Create(0));
1008  controller->AddValueObserver(&dummy);
1009
1010  controller->AddAnimation(CreateAnimation(
1011      scoped_ptr<AnimationCurve>(new FakeTransformTransition(2)).Pass(),
1012      1,
1013      Animation::Transform));
1014  controller->AddAnimation(CreateAnimation(
1015      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1016      1,
1017      Animation::Opacity));
1018  controller->AddAnimation(CreateAnimation(
1019      scoped_ptr<AnimationCurve>(
1020          new FakeFloatTransition(1.0, 1.f, 0.5f)).Pass(),
1021      2,
1022      Animation::Opacity));
1023
1024  // Animations with id 1 should both start now.
1025  controller->Animate(kInitialTickTime);
1026  controller->UpdateState(true, events.get());
1027  EXPECT_TRUE(controller->HasActiveAnimation());
1028  EXPECT_EQ(0.f, dummy.opacity());
1029  // The opacity animation should have finished at time 1, but the group
1030  // of animations with id 1 don't finish until time 2 because of the length
1031  // of the transform animation.
1032  controller->Animate(kInitialTickTime + 2.0);
1033  controller->UpdateState(true, events.get());
1034  // Should not have started the float transition yet.
1035  EXPECT_TRUE(controller->HasActiveAnimation());
1036  EXPECT_EQ(1.f, dummy.opacity());
1037
1038  // The second opacity animation should start at time 2 and should be done by
1039  // time 3.
1040  controller->Animate(kInitialTickTime + 3.0);
1041  controller->UpdateState(true, events.get());
1042  EXPECT_EQ(0.5f, dummy.opacity());
1043  EXPECT_FALSE(controller->HasActiveAnimation());
1044}
1045
1046// Test that a looping animation loops and for the correct number of iterations.
1047TEST(LayerAnimationControllerTest, TrivialLooping) {
1048  scoped_ptr<AnimationEventsVector> events(
1049      make_scoped_ptr(new AnimationEventsVector));
1050  FakeLayerAnimationValueObserver dummy;
1051  scoped_refptr<LayerAnimationController> controller(
1052      LayerAnimationController::Create(0));
1053  controller->AddValueObserver(&dummy);
1054
1055  scoped_ptr<Animation> to_add(CreateAnimation(
1056      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1057      1,
1058      Animation::Opacity));
1059  to_add->set_iterations(3);
1060  controller->AddAnimation(to_add.Pass());
1061
1062  controller->Animate(kInitialTickTime);
1063  controller->UpdateState(true, events.get());
1064  EXPECT_TRUE(controller->HasActiveAnimation());
1065  EXPECT_EQ(0.f, dummy.opacity());
1066  controller->Animate(kInitialTickTime + 1.25);
1067  controller->UpdateState(true, events.get());
1068  EXPECT_TRUE(controller->HasActiveAnimation());
1069  EXPECT_EQ(0.25f, dummy.opacity());
1070  controller->Animate(kInitialTickTime + 1.75);
1071  controller->UpdateState(true, events.get());
1072  EXPECT_TRUE(controller->HasActiveAnimation());
1073  EXPECT_EQ(0.75f, dummy.opacity());
1074  controller->Animate(kInitialTickTime + 2.25);
1075  controller->UpdateState(true, events.get());
1076  EXPECT_TRUE(controller->HasActiveAnimation());
1077  EXPECT_EQ(0.25f, dummy.opacity());
1078  controller->Animate(kInitialTickTime + 2.75);
1079  controller->UpdateState(true, events.get());
1080  EXPECT_TRUE(controller->HasActiveAnimation());
1081  EXPECT_EQ(0.75f, dummy.opacity());
1082  controller->Animate(kInitialTickTime + 3.0);
1083  controller->UpdateState(true, events.get());
1084  EXPECT_FALSE(controller->HasActiveAnimation());
1085  EXPECT_EQ(1.f, dummy.opacity());
1086
1087  // Just be extra sure.
1088  controller->Animate(kInitialTickTime + 4.0);
1089  controller->UpdateState(true, events.get());
1090  EXPECT_EQ(1.f, dummy.opacity());
1091}
1092
1093// Test that an infinitely looping animation does indeed go until aborted.
1094TEST(LayerAnimationControllerTest, InfiniteLooping) {
1095  scoped_ptr<AnimationEventsVector> events(
1096      make_scoped_ptr(new AnimationEventsVector));
1097  FakeLayerAnimationValueObserver dummy;
1098  scoped_refptr<LayerAnimationController> controller(
1099      LayerAnimationController::Create(0));
1100  controller->AddValueObserver(&dummy);
1101
1102  const int id = 1;
1103  scoped_ptr<Animation> to_add(CreateAnimation(
1104      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1105      id,
1106      Animation::Opacity));
1107  to_add->set_iterations(-1);
1108  controller->AddAnimation(to_add.Pass());
1109
1110  controller->Animate(kInitialTickTime);
1111  controller->UpdateState(true, events.get());
1112  EXPECT_TRUE(controller->HasActiveAnimation());
1113  EXPECT_EQ(0.f, dummy.opacity());
1114  controller->Animate(kInitialTickTime + 1.25);
1115  controller->UpdateState(true, events.get());
1116  EXPECT_TRUE(controller->HasActiveAnimation());
1117  EXPECT_EQ(0.25f, dummy.opacity());
1118  controller->Animate(kInitialTickTime + 1.75);
1119  controller->UpdateState(true, events.get());
1120  EXPECT_TRUE(controller->HasActiveAnimation());
1121  EXPECT_EQ(0.75f, dummy.opacity());
1122
1123  controller->Animate(kInitialTickTime + 1073741824.25);
1124  controller->UpdateState(true, events.get());
1125  EXPECT_TRUE(controller->HasActiveAnimation());
1126  EXPECT_EQ(0.25f, dummy.opacity());
1127  controller->Animate(kInitialTickTime + 1073741824.75);
1128  controller->UpdateState(true, events.get());
1129  EXPECT_TRUE(controller->HasActiveAnimation());
1130  EXPECT_EQ(0.75f, dummy.opacity());
1131
1132  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1133  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1134      Animation::Aborted, kInitialTickTime + 0.75);
1135  EXPECT_FALSE(controller->HasActiveAnimation());
1136  EXPECT_EQ(0.75f, dummy.opacity());
1137}
1138
1139// Test that pausing and resuming work as expected.
1140TEST(LayerAnimationControllerTest, PauseResume) {
1141  scoped_ptr<AnimationEventsVector> events(
1142      make_scoped_ptr(new AnimationEventsVector));
1143  FakeLayerAnimationValueObserver dummy;
1144  scoped_refptr<LayerAnimationController> controller(
1145      LayerAnimationController::Create(0));
1146  controller->AddValueObserver(&dummy);
1147
1148  const int id = 1;
1149  controller->AddAnimation(CreateAnimation(
1150      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1151      id,
1152      Animation::Opacity));
1153
1154  controller->Animate(kInitialTickTime);
1155  controller->UpdateState(true, events.get());
1156  EXPECT_TRUE(controller->HasActiveAnimation());
1157  EXPECT_EQ(0.f, dummy.opacity());
1158  controller->Animate(kInitialTickTime + 0.5);
1159  controller->UpdateState(true, events.get());
1160  EXPECT_TRUE(controller->HasActiveAnimation());
1161  EXPECT_EQ(0.5f, dummy.opacity());
1162
1163  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1164  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1165      Animation::Paused, kInitialTickTime + 0.5);
1166
1167  controller->Animate(kInitialTickTime + 1024.0);
1168  controller->UpdateState(true, events.get());
1169  EXPECT_TRUE(controller->HasActiveAnimation());
1170  EXPECT_EQ(0.5f, dummy.opacity());
1171
1172  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1173  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1174      Animation::Running, kInitialTickTime + 1024);
1175
1176  controller->Animate(kInitialTickTime + 1024.25);
1177  controller->UpdateState(true, events.get());
1178  EXPECT_TRUE(controller->HasActiveAnimation());
1179  EXPECT_EQ(0.75f, dummy.opacity());
1180  controller->Animate(kInitialTickTime + 1024.5);
1181  controller->UpdateState(true, events.get());
1182  EXPECT_FALSE(controller->HasActiveAnimation());
1183  EXPECT_EQ(1.f, dummy.opacity());
1184}
1185
1186TEST(LayerAnimationControllerTest, AbortAGroupedAnimation) {
1187  scoped_ptr<AnimationEventsVector> events(
1188      make_scoped_ptr(new AnimationEventsVector));
1189  FakeLayerAnimationValueObserver dummy;
1190  scoped_refptr<LayerAnimationController> controller(
1191      LayerAnimationController::Create(0));
1192  controller->AddValueObserver(&dummy);
1193
1194  const int id = 1;
1195  controller->AddAnimation(CreateAnimation(
1196      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1197      id,
1198      Animation::Transform));
1199  controller->AddAnimation(CreateAnimation(
1200      scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1201      id,
1202      Animation::Opacity));
1203  controller->AddAnimation(CreateAnimation(
1204      scoped_ptr<AnimationCurve>(
1205          new FakeFloatTransition(1.0, 1.f, 0.75f)).Pass(),
1206      2,
1207      Animation::Opacity));
1208
1209  controller->Animate(kInitialTickTime);
1210  controller->UpdateState(true, events.get());
1211  EXPECT_TRUE(controller->HasActiveAnimation());
1212  EXPECT_EQ(0.f, dummy.opacity());
1213  controller->Animate(kInitialTickTime + 1.0);
1214  controller->UpdateState(true, events.get());
1215  EXPECT_TRUE(controller->HasActiveAnimation());
1216  EXPECT_EQ(0.5f, dummy.opacity());
1217
1218  EXPECT_TRUE(controller->GetAnimation(id, Animation::Opacity));
1219  controller->GetAnimation(id, Animation::Opacity)->SetRunState(
1220      Animation::Aborted, kInitialTickTime + 1.0);
1221  controller->Animate(kInitialTickTime + 1.0);
1222  controller->UpdateState(true, events.get());
1223  EXPECT_TRUE(controller->HasActiveAnimation());
1224  EXPECT_EQ(1.f, dummy.opacity());
1225  controller->Animate(kInitialTickTime + 2.0);
1226  controller->UpdateState(true, events.get());
1227  EXPECT_TRUE(!controller->HasActiveAnimation());
1228  EXPECT_EQ(0.75f, dummy.opacity());
1229}
1230
1231TEST(LayerAnimationControllerTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1232  FakeLayerAnimationValueObserver dummy_impl;
1233  scoped_refptr<LayerAnimationController> controller_impl(
1234      LayerAnimationController::Create(0));
1235  controller_impl->AddValueObserver(&dummy_impl);
1236  scoped_ptr<AnimationEventsVector> events(
1237      make_scoped_ptr(new AnimationEventsVector));
1238  FakeLayerAnimationValueObserver dummy;
1239  scoped_refptr<LayerAnimationController> controller(
1240      LayerAnimationController::Create(0));
1241  controller->AddValueObserver(&dummy);
1242
1243  scoped_ptr<Animation> to_add(CreateAnimation(
1244      scoped_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)).Pass(),
1245      0,
1246      Animation::Opacity));
1247  to_add->set_needs_synchronized_start_time(true);
1248  controller->AddAnimation(to_add.Pass());
1249
1250  controller->Animate(kInitialTickTime);
1251  controller->UpdateState(true, events.get());
1252  EXPECT_TRUE(controller->HasActiveAnimation());
1253  Animation* active_animation = controller->GetAnimation(0, Animation::Opacity);
1254  EXPECT_TRUE(active_animation);
1255  EXPECT_TRUE(active_animation->needs_synchronized_start_time());
1256
1257  controller->PushAnimationUpdatesTo(controller_impl.get());
1258
1259  active_animation = controller_impl->GetAnimation(0, Animation::Opacity);
1260  EXPECT_TRUE(active_animation);
1261  EXPECT_EQ(Animation::WaitingForTargetAvailability,
1262            active_animation->run_state());
1263}
1264
1265// Tests that skipping a call to UpdateState works as expected.
1266TEST(LayerAnimationControllerTest, SkipUpdateState) {
1267  scoped_ptr<AnimationEventsVector> events(
1268      make_scoped_ptr(new AnimationEventsVector));
1269  FakeLayerAnimationValueObserver dummy;
1270  scoped_refptr<LayerAnimationController> controller(
1271      LayerAnimationController::Create(0));
1272  controller->AddValueObserver(&dummy);
1273
1274  controller->AddAnimation(CreateAnimation(
1275      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1)).Pass(),
1276      1,
1277      Animation::Transform));
1278
1279  controller->Animate(kInitialTickTime);
1280  controller->UpdateState(true, events.get());
1281
1282  controller->AddAnimation(CreateAnimation(
1283      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1284      2,
1285      Animation::Opacity));
1286
1287  // Animate but don't UpdateState.
1288  controller->Animate(kInitialTickTime + 1.0);
1289
1290  controller->Animate(kInitialTickTime + 2.0);
1291  events.reset(new AnimationEventsVector);
1292  controller->UpdateState(true, events.get());
1293
1294  // Should have one Started event and one Finished event.
1295  EXPECT_EQ(2u, events->size());
1296  EXPECT_NE((*events)[0].type, (*events)[1].type);
1297
1298  // The float transition should still be at its starting point.
1299  EXPECT_TRUE(controller->HasActiveAnimation());
1300  EXPECT_EQ(0.f, dummy.opacity());
1301
1302  controller->Animate(kInitialTickTime + 3.0);
1303  controller->UpdateState(true, events.get());
1304
1305  // The float tranisition should now be done.
1306  EXPECT_EQ(1.f, dummy.opacity());
1307  EXPECT_FALSE(controller->HasActiveAnimation());
1308}
1309
1310// Tests that an animation controller with only an inactive observer gets ticked
1311// but doesn't progress animations past the Starting state.
1312TEST(LayerAnimationControllerTest, InactiveObserverGetsTicked) {
1313  scoped_ptr<AnimationEventsVector> events(
1314      make_scoped_ptr(new AnimationEventsVector));
1315  FakeLayerAnimationValueObserver dummy;
1316  FakeInactiveLayerAnimationValueObserver inactive_dummy;
1317  scoped_refptr<LayerAnimationController> controller(
1318      LayerAnimationController::Create(0));
1319
1320  const int id = 1;
1321  controller->AddAnimation(CreateAnimation(scoped_ptr<AnimationCurve>(
1322      new FakeFloatTransition(1.0, 0.5f, 1.f)).Pass(),
1323      id,
1324      Animation::Opacity));
1325
1326  // Without an observer, the animation shouldn't progress to the Starting
1327  // state.
1328  controller->Animate(kInitialTickTime);
1329  controller->UpdateState(true, events.get());
1330  EXPECT_EQ(0u, events->size());
1331  EXPECT_EQ(Animation::WaitingForTargetAvailability,
1332            controller->GetAnimation(id, Animation::Opacity)->run_state());
1333
1334  controller->AddValueObserver(&inactive_dummy);
1335
1336  // With only an inactive observer, the animation should progress to the
1337  // Starting state and get ticked at its starting point, but should not
1338  // progress to Running.
1339  controller->Animate(kInitialTickTime + 1.0);
1340  controller->UpdateState(true, events.get());
1341  EXPECT_EQ(0u, events->size());
1342  EXPECT_EQ(Animation::Starting,
1343            controller->GetAnimation(id, Animation::Opacity)->run_state());
1344  EXPECT_EQ(0.5f, inactive_dummy.opacity());
1345
1346  // Even when already in the Starting state, the animation should stay
1347  // there, and shouldn't be ticked past its starting point.
1348  controller->Animate(kInitialTickTime + 2.0);
1349  controller->UpdateState(true, events.get());
1350  EXPECT_EQ(0u, events->size());
1351  EXPECT_EQ(Animation::Starting,
1352            controller->GetAnimation(id, Animation::Opacity)->run_state());
1353  EXPECT_EQ(0.5f, inactive_dummy.opacity());
1354
1355  controller->AddValueObserver(&dummy);
1356
1357  // Now that an active observer has been added, the animation should still
1358  // initially tick at its starting point, but should now progress to Running.
1359  controller->Animate(kInitialTickTime + 3.0);
1360  controller->UpdateState(true, events.get());
1361  EXPECT_EQ(1u, events->size());
1362  EXPECT_EQ(Animation::Running,
1363            controller->GetAnimation(id, Animation::Opacity)->run_state());
1364  EXPECT_EQ(0.5f, inactive_dummy.opacity());
1365  EXPECT_EQ(0.5f, dummy.opacity());
1366
1367  // The animation should now tick past its starting point.
1368  controller->Animate(kInitialTickTime + 3.5);
1369  EXPECT_NE(0.5f, inactive_dummy.opacity());
1370  EXPECT_NE(0.5f, dummy.opacity());
1371}
1372
1373TEST(LayerAnimationControllerTest, TransformAnimationBounds) {
1374  scoped_refptr<LayerAnimationController> controller_impl(
1375      LayerAnimationController::Create(0));
1376
1377  scoped_ptr<KeyframedTransformAnimationCurve> curve1(
1378      KeyframedTransformAnimationCurve::Create());
1379
1380  TransformOperations operations1;
1381  curve1->AddKeyframe(TransformKeyframe::Create(
1382      0.0, operations1, scoped_ptr<TimingFunction>()));
1383  operations1.AppendTranslate(10.0, 15.0, 0.0);
1384  curve1->AddKeyframe(TransformKeyframe::Create(
1385      1.0, operations1, scoped_ptr<TimingFunction>()));
1386
1387  scoped_ptr<Animation> animation(Animation::Create(
1388      curve1.PassAs<AnimationCurve>(), 1, 1, Animation::Transform));
1389  controller_impl->AddAnimation(animation.Pass());
1390
1391  scoped_ptr<KeyframedTransformAnimationCurve> curve2(
1392      KeyframedTransformAnimationCurve::Create());
1393
1394  TransformOperations operations2;
1395  curve2->AddKeyframe(TransformKeyframe::Create(
1396      0.0, operations2, scoped_ptr<TimingFunction>()));
1397  operations2.AppendScale(2.0, 3.0, 4.0);
1398  curve2->AddKeyframe(TransformKeyframe::Create(
1399      1.0, operations2, scoped_ptr<TimingFunction>()));
1400
1401  animation = Animation::Create(
1402      curve2.PassAs<AnimationCurve>(), 2, 2, Animation::Transform);
1403  controller_impl->AddAnimation(animation.Pass());
1404
1405  gfx::BoxF box(1.f, 2.f, -1.f, 3.f, 4.f, 5.f);
1406  gfx::BoxF bounds;
1407
1408  EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1409  EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 13.f, 19.f, 20.f).ToString(),
1410            bounds.ToString());
1411
1412  controller_impl->GetAnimation(1, Animation::Transform)
1413      ->SetRunState(Animation::Finished, 0.0);
1414
1415  // Only the unfinished animation should affect the animated bounds.
1416  EXPECT_TRUE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1417  EXPECT_EQ(gfx::BoxF(1.f, 2.f, -4.f, 7.f, 16.f, 20.f).ToString(),
1418            bounds.ToString());
1419
1420  controller_impl->GetAnimation(2, Animation::Transform)
1421      ->SetRunState(Animation::Finished, 0.0);
1422
1423  // There are no longer any running animations.
1424  EXPECT_FALSE(controller_impl->HasTransformAnimationThatInflatesBounds());
1425
1426  // Add an animation whose bounds we don't yet support computing.
1427  scoped_ptr<KeyframedTransformAnimationCurve> curve3(
1428      KeyframedTransformAnimationCurve::Create());
1429  TransformOperations operations3;
1430  gfx::Transform transform3;
1431  transform3.Scale3d(1.0, 2.0, 3.0);
1432  curve3->AddKeyframe(TransformKeyframe::Create(
1433      0.0, operations3, scoped_ptr<TimingFunction>()));
1434  operations3.AppendMatrix(transform3);
1435  curve3->AddKeyframe(TransformKeyframe::Create(
1436      1.0, operations3, scoped_ptr<TimingFunction>()));
1437  animation = Animation::Create(
1438      curve3.PassAs<AnimationCurve>(), 3, 3, Animation::Transform);
1439  controller_impl->AddAnimation(animation.Pass());
1440  EXPECT_FALSE(controller_impl->TransformAnimationBoundsForBox(box, &bounds));
1441}
1442
1443// Tests that AbortAnimations aborts all animations targeting the specified
1444// property.
1445TEST(LayerAnimationControllerTest, AbortAnimations) {
1446  FakeLayerAnimationValueObserver dummy;
1447  scoped_refptr<LayerAnimationController> controller(
1448      LayerAnimationController::Create(0));
1449  controller->AddValueObserver(&dummy);
1450
1451  // Start with several animations, and allow some of them to reach the finished
1452  // state.
1453  controller->AddAnimation(CreateAnimation(
1454      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1455      1,
1456      Animation::Transform));
1457  controller->AddAnimation(CreateAnimation(
1458      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1459      2,
1460      Animation::Opacity));
1461  controller->AddAnimation(CreateAnimation(
1462      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1463      3,
1464      Animation::Transform));
1465  controller->AddAnimation(CreateAnimation(
1466      scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1467      4,
1468      Animation::Transform));
1469  controller->AddAnimation(CreateAnimation(
1470      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1471      5,
1472      Animation::Opacity));
1473
1474  controller->Animate(kInitialTickTime);
1475  controller->UpdateState(true, NULL);
1476  controller->Animate(kInitialTickTime + 1.0);
1477  controller->UpdateState(true, NULL);
1478
1479  EXPECT_EQ(Animation::Finished,
1480            controller->GetAnimation(1, Animation::Transform)->run_state());
1481  EXPECT_EQ(Animation::Finished,
1482            controller->GetAnimation(2, Animation::Opacity)->run_state());
1483  EXPECT_EQ(Animation::Running,
1484            controller->GetAnimation(3, Animation::Transform)->run_state());
1485  EXPECT_EQ(Animation::WaitingForTargetAvailability,
1486            controller->GetAnimation(4, Animation::Transform)->run_state());
1487  EXPECT_EQ(Animation::Running,
1488            controller->GetAnimation(5, Animation::Opacity)->run_state());
1489
1490  controller->AbortAnimations(Animation::Transform);
1491
1492  // Only un-finished Transform animations should have been aborted.
1493  EXPECT_EQ(Animation::Finished,
1494            controller->GetAnimation(1, Animation::Transform)->run_state());
1495  EXPECT_EQ(Animation::Finished,
1496            controller->GetAnimation(2, Animation::Opacity)->run_state());
1497  EXPECT_EQ(Animation::Aborted,
1498            controller->GetAnimation(3, Animation::Transform)->run_state());
1499  EXPECT_EQ(Animation::Aborted,
1500            controller->GetAnimation(4, Animation::Transform)->run_state());
1501  EXPECT_EQ(Animation::Running,
1502            controller->GetAnimation(5, Animation::Opacity)->run_state());
1503}
1504
1505// An animation aborted on the main thread should get deleted on both threads.
1506TEST(LayerAnimationControllerTest, MainThreadAbortedAnimationGetsDeleted) {
1507  FakeLayerAnimationValueObserver dummy_impl;
1508  scoped_refptr<LayerAnimationController> controller_impl(
1509      LayerAnimationController::Create(0));
1510  controller_impl->AddValueObserver(&dummy_impl);
1511  FakeLayerAnimationValueObserver dummy;
1512  scoped_refptr<LayerAnimationController> controller(
1513      LayerAnimationController::Create(0));
1514  controller->AddValueObserver(&dummy);
1515
1516  AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1517  int group_id = controller->GetAnimation(Animation::Opacity)->group();
1518
1519  controller->PushAnimationUpdatesTo(controller_impl.get());
1520  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1521
1522  controller->AbortAnimations(Animation::Opacity);
1523  EXPECT_EQ(Animation::Aborted,
1524            controller->GetAnimation(Animation::Opacity)->run_state());
1525  EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1526  EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1527
1528  controller->Animate(kInitialTickTime);
1529  controller->UpdateState(true, NULL);
1530  EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1531  EXPECT_EQ(Animation::WaitingForDeletion,
1532            controller->GetAnimation(Animation::Opacity)->run_state());
1533
1534  controller->PushAnimationUpdatesTo(controller_impl.get());
1535  EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1536  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1537}
1538
1539// An animation aborted on the impl thread should get deleted on both threads.
1540TEST(LayerAnimationControllerTest, ImplThreadAbortedAnimationGetsDeleted) {
1541  FakeLayerAnimationValueObserver dummy_impl;
1542  scoped_refptr<LayerAnimationController> controller_impl(
1543      LayerAnimationController::Create(0));
1544  controller_impl->AddValueObserver(&dummy_impl);
1545  FakeLayerAnimationValueObserver dummy;
1546  scoped_refptr<LayerAnimationController> controller(
1547      LayerAnimationController::Create(0));
1548  controller->AddValueObserver(&dummy);
1549
1550  AddOpacityTransitionToController(controller.get(), 1.0, 0.f, 1.f, false);
1551  int group_id = controller->GetAnimation(Animation::Opacity)->group();
1552
1553  controller->PushAnimationUpdatesTo(controller_impl.get());
1554  EXPECT_TRUE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1555
1556  controller_impl->AbortAnimations(Animation::Opacity);
1557  EXPECT_EQ(Animation::Aborted,
1558            controller_impl->GetAnimation(Animation::Opacity)->run_state());
1559  EXPECT_FALSE(dummy.animation_waiting_for_deletion());
1560  EXPECT_FALSE(dummy_impl.animation_waiting_for_deletion());
1561
1562  AnimationEventsVector events;
1563  controller_impl->Animate(kInitialTickTime);
1564  controller_impl->UpdateState(true, &events);
1565  EXPECT_TRUE(dummy_impl.animation_waiting_for_deletion());
1566  EXPECT_EQ(1u, events.size());
1567  EXPECT_EQ(AnimationEvent::Aborted, events[0].type);
1568  EXPECT_EQ(Animation::WaitingForDeletion,
1569            controller_impl->GetAnimation(Animation::Opacity)->run_state());
1570
1571  controller->NotifyAnimationAborted(events[0]);
1572  EXPECT_EQ(Animation::Aborted,
1573            controller->GetAnimation(Animation::Opacity)->run_state());
1574
1575  controller->Animate(kInitialTickTime + 0.5);
1576  controller->UpdateState(true, NULL);
1577  EXPECT_TRUE(dummy.animation_waiting_for_deletion());
1578  EXPECT_EQ(Animation::WaitingForDeletion,
1579            controller->GetAnimation(Animation::Opacity)->run_state());
1580
1581  controller->PushAnimationUpdatesTo(controller_impl.get());
1582  EXPECT_FALSE(controller->GetAnimation(group_id, Animation::Opacity));
1583  EXPECT_FALSE(controller_impl->GetAnimation(group_id, Animation::Opacity));
1584}
1585
1586// Ensure that we only generate Finished events for animations in a group
1587// once all animations in that group are finished.
1588TEST(LayerAnimationControllerTest, FinishedEventsForGroup) {
1589  scoped_ptr<AnimationEventsVector> events(
1590      make_scoped_ptr(new AnimationEventsVector));
1591  FakeLayerAnimationValueObserver dummy_impl;
1592  scoped_refptr<LayerAnimationController> controller_impl(
1593      LayerAnimationController::Create(0));
1594  controller_impl->AddValueObserver(&dummy_impl);
1595
1596  // Add two animations with the same group id but different durations.
1597  controller_impl->AddAnimation(CreateAnimation(
1598      scoped_ptr<AnimationCurve>(new FakeTransformTransition(2.0)).Pass(),
1599      1,
1600      Animation::Transform));
1601  controller_impl->AddAnimation(CreateAnimation(
1602      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1603      1,
1604      Animation::Opacity));
1605
1606  controller_impl->Animate(kInitialTickTime);
1607  controller_impl->UpdateState(true, events.get());
1608
1609  // Both animations should have started.
1610  EXPECT_EQ(2u, events->size());
1611  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1612  EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1613
1614  events.reset(new AnimationEventsVector);
1615  controller_impl->Animate(kInitialTickTime + 1.0);
1616  controller_impl->UpdateState(true, events.get());
1617
1618  // The opacity animation should be finished, but should not have generated
1619  // a Finished event yet.
1620  EXPECT_EQ(0u, events->size());
1621  EXPECT_EQ(Animation::Finished,
1622            controller_impl->GetAnimation(1, Animation::Opacity)->run_state());
1623  EXPECT_EQ(Animation::Running,
1624            controller_impl->GetAnimation(1,
1625                                          Animation::Transform)->run_state());
1626
1627  controller_impl->Animate(kInitialTickTime + 2.0);
1628  controller_impl->UpdateState(true, events.get());
1629
1630  // Both animations should have generated Finished events.
1631  EXPECT_EQ(2u, events->size());
1632  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1633  EXPECT_EQ(AnimationEvent::Finished, (*events)[1].type);
1634}
1635
1636// Ensure that when a group has a mix of aborted and finished animations,
1637// we generate a Finished event for the finished animation and an Aborted
1638// event for the aborted animation.
1639TEST(LayerAnimationControllerTest, FinishedAndAbortedEventsForGroup) {
1640  scoped_ptr<AnimationEventsVector> events(
1641      make_scoped_ptr(new AnimationEventsVector));
1642  FakeLayerAnimationValueObserver dummy_impl;
1643  scoped_refptr<LayerAnimationController> controller_impl(
1644      LayerAnimationController::Create(0));
1645  controller_impl->AddValueObserver(&dummy_impl);
1646
1647  // Add two animations with the same group id.
1648  controller_impl->AddAnimation(CreateAnimation(
1649      scoped_ptr<AnimationCurve>(new FakeTransformTransition(1.0)).Pass(),
1650      1,
1651      Animation::Transform));
1652  controller_impl->AddAnimation(CreateAnimation(
1653      scoped_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)).Pass(),
1654      1,
1655      Animation::Opacity));
1656
1657  controller_impl->Animate(kInitialTickTime);
1658  controller_impl->UpdateState(true, events.get());
1659
1660  // Both animations should have started.
1661  EXPECT_EQ(2u, events->size());
1662  EXPECT_EQ(AnimationEvent::Started, (*events)[0].type);
1663  EXPECT_EQ(AnimationEvent::Started, (*events)[1].type);
1664
1665  controller_impl->AbortAnimations(Animation::Opacity);
1666
1667  events.reset(new AnimationEventsVector);
1668  controller_impl->Animate(kInitialTickTime + 1.0);
1669  controller_impl->UpdateState(true, events.get());
1670
1671  // We should have exactly 2 events: a Finished event for the tranform
1672  // animation, and an Aborted event for the opacity animation.
1673  EXPECT_EQ(2u, events->size());
1674  EXPECT_EQ(AnimationEvent::Finished, (*events)[0].type);
1675  EXPECT_EQ(Animation::Transform, (*events)[0].target_property);
1676  EXPECT_EQ(AnimationEvent::Aborted, (*events)[1].type);
1677  EXPECT_EQ(Animation::Opacity, (*events)[1].target_property);
1678}
1679
1680}  // namespace
1681}  // namespace cc
1682