1 // Copyright 2015 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/element_animations.h"
6 
7 #include "base/memory/ptr_util.h"
8 #include "cc/animation/animation.h"
9 #include "cc/animation/animation_delegate.h"
10 #include "cc/animation/animation_events.h"
11 #include "cc/animation/animation_host.h"
12 #include "cc/animation/animation_id_provider.h"
13 #include "cc/animation/animation_timeline.h"
14 #include "cc/animation/keyframe_effect.h"
15 #include "cc/animation/keyframed_animation_curve.h"
16 #include "cc/animation/scroll_offset_animation_curve.h"
17 #include "cc/animation/scroll_offset_animation_curve_factory.h"
18 #include "cc/animation/transform_operations.h"
19 #include "cc/test/animation_test_common.h"
20 #include "cc/test/animation_timelines_test_common.h"
21 #include "ui/gfx/geometry/box_f.h"
22 
23 namespace cc {
24 namespace {
25 
26 using base::TimeDelta;
27 using base::TimeTicks;
28 
TicksFromSecondsF(double seconds)29 static base::TimeTicks TicksFromSecondsF(double seconds) {
30   return base::TimeTicks() + base::TimeDelta::FromSecondsD(seconds);
31 }
32 
33 // An ElementAnimations cannot be ticked at 0.0, since an animation
34 // with start time 0.0 is treated as an animation whose start time has
35 // not yet been set.
36 const TimeTicks kInitialTickTime = TicksFromSecondsF(1.0);
37 
38 class ElementAnimationsTest : public AnimationTimelinesTest {
39  public:
40   ElementAnimationsTest() = default;
41   ~ElementAnimationsTest() override = default;
42 
SetUp()43   void SetUp() override {
44     AnimationTimelinesTest::SetUp();
45   }
46 
CreateImplTimelineAndAnimation()47   void CreateImplTimelineAndAnimation() override {
48     AnimationTimelinesTest::CreateImplTimelineAndAnimation();
49   }
50 
CreateEventsForTesting()51   std::unique_ptr<AnimationEvents> CreateEventsForTesting() {
52     auto mutator_events = host_impl_->CreateEvents();
53     return base::WrapUnique(
54         static_cast<AnimationEvents*>(mutator_events.release()));
55   }
56 };
57 
58 // See animation_unittest.cc for integration with Animation.
59 
TEST_F(ElementAnimationsTest,AttachToLayerInActiveTree)60 TEST_F(ElementAnimationsTest, AttachToLayerInActiveTree) {
61   // Set up the layer which is in active tree for main thread and not
62   // yet passed onto the impl thread.
63   client_.RegisterElementId(element_id_, ElementListType::ACTIVE);
64   client_impl_.RegisterElementId(element_id_, ElementListType::PENDING);
65 
66   EXPECT_TRUE(
67       client_.IsElementInPropertyTrees(element_id_, ElementListType::ACTIVE));
68   EXPECT_FALSE(
69       client_.IsElementInPropertyTrees(element_id_, ElementListType::PENDING));
70 
71   AttachTimelineAnimationLayer();
72 
73   EXPECT_TRUE(element_animations_->has_element_in_active_list());
74   EXPECT_FALSE(element_animations_->has_element_in_pending_list());
75 
76   PushProperties();
77 
78   GetImplTimelineAndAnimationByID();
79 
80   EXPECT_FALSE(element_animations_impl_->has_element_in_active_list());
81   EXPECT_TRUE(element_animations_impl_->has_element_in_pending_list());
82 
83   // Create the layer in the impl active tree.
84   client_impl_.RegisterElementId(element_id_, ElementListType::ACTIVE);
85   EXPECT_TRUE(element_animations_impl_->has_element_in_active_list());
86   EXPECT_TRUE(element_animations_impl_->has_element_in_pending_list());
87 
88   EXPECT_TRUE(client_impl_.IsElementInPropertyTrees(element_id_,
89                                                     ElementListType::ACTIVE));
90   EXPECT_TRUE(client_impl_.IsElementInPropertyTrees(element_id_,
91                                                     ElementListType::PENDING));
92 
93   // kill layer on main thread.
94   client_.UnregisterElementId(element_id_, ElementListType::ACTIVE);
95   EXPECT_EQ(element_animations_,
96             animation_->keyframe_effect()->element_animations());
97   EXPECT_FALSE(element_animations_->has_element_in_active_list());
98   EXPECT_FALSE(element_animations_->has_element_in_pending_list());
99 
100   // Sync doesn't detach LayerImpl.
101   PushProperties();
102   EXPECT_EQ(element_animations_impl_,
103             animation_impl_->keyframe_effect()->element_animations());
104   EXPECT_TRUE(element_animations_impl_->has_element_in_active_list());
105   EXPECT_TRUE(element_animations_impl_->has_element_in_pending_list());
106 
107   // Kill layer on impl thread in pending tree.
108   client_impl_.UnregisterElementId(element_id_, ElementListType::PENDING);
109   EXPECT_EQ(element_animations_impl_,
110             animation_impl_->keyframe_effect()->element_animations());
111   EXPECT_TRUE(element_animations_impl_->has_element_in_active_list());
112   EXPECT_FALSE(element_animations_impl_->has_element_in_pending_list());
113 
114   // Kill layer on impl thread in active tree.
115   client_impl_.UnregisterElementId(element_id_, ElementListType::ACTIVE);
116   EXPECT_EQ(element_animations_impl_,
117             animation_impl_->keyframe_effect()->element_animations());
118   EXPECT_FALSE(element_animations_impl_->has_element_in_active_list());
119   EXPECT_FALSE(element_animations_impl_->has_element_in_pending_list());
120 
121   // Sync doesn't change anything.
122   PushProperties();
123   EXPECT_EQ(element_animations_impl_,
124             animation_impl_->keyframe_effect()->element_animations());
125   EXPECT_FALSE(element_animations_impl_->has_element_in_active_list());
126   EXPECT_FALSE(element_animations_impl_->has_element_in_pending_list());
127 
128   animation_->DetachElement();
129   EXPECT_FALSE(animation_->keyframe_effect()->element_animations());
130 
131   // Release ptrs now to test the order of destruction.
132   ReleaseRefPtrs();
133 }
134 
TEST_F(ElementAnimationsTest,AttachToNotYetCreatedLayer)135 TEST_F(ElementAnimationsTest, AttachToNotYetCreatedLayer) {
136   host_->AddAnimationTimeline(timeline_);
137   timeline_->AttachAnimation(animation_);
138 
139   PushProperties();
140   GetImplTimelineAndAnimationByID();
141 
142   // Perform attachment separately.
143   animation_->AttachElement(element_id_);
144   element_animations_ = animation_->keyframe_effect()->element_animations();
145 
146   EXPECT_FALSE(element_animations_->has_element_in_active_list());
147   EXPECT_FALSE(element_animations_->has_element_in_pending_list());
148 
149   PushProperties();
150   element_animations_impl_ =
151       animation_impl_->keyframe_effect()->element_animations();
152 
153   EXPECT_FALSE(element_animations_impl_->has_element_in_active_list());
154   EXPECT_FALSE(element_animations_impl_->has_element_in_pending_list());
155 
156   // Create layer.
157   client_.RegisterElementId(element_id_, ElementListType::ACTIVE);
158   EXPECT_TRUE(element_animations_->has_element_in_active_list());
159   EXPECT_FALSE(element_animations_->has_element_in_pending_list());
160 
161   client_impl_.RegisterElementId(element_id_, ElementListType::PENDING);
162   EXPECT_FALSE(element_animations_impl_->has_element_in_active_list());
163   EXPECT_TRUE(element_animations_impl_->has_element_in_pending_list());
164 
165   client_impl_.RegisterElementId(element_id_, ElementListType::ACTIVE);
166   EXPECT_TRUE(element_animations_impl_->has_element_in_active_list());
167   EXPECT_TRUE(element_animations_impl_->has_element_in_pending_list());
168 }
169 
TEST_F(ElementAnimationsTest,AddRemoveAnimations)170 TEST_F(ElementAnimationsTest, AddRemoveAnimations) {
171   host_->AddAnimationTimeline(timeline_);
172   timeline_->AttachAnimation(animation_);
173   animation_->AttachElement(element_id_);
174 
175   scoped_refptr<ElementAnimations> element_animations =
176       animation_->keyframe_effect()->element_animations();
177   EXPECT_TRUE(element_animations);
178 
179   scoped_refptr<Animation> animation1 =
180       Animation::Create(AnimationIdProvider::NextAnimationId());
181   scoped_refptr<Animation> animation2 =
182       Animation::Create(AnimationIdProvider::NextAnimationId());
183 
184   timeline_->AttachAnimation(animation1);
185   timeline_->AttachAnimation(animation2);
186 
187   // Attach animations to the same layer.
188   animation1->AttachElement(element_id_);
189   animation2->AttachElement(element_id_);
190 
191   EXPECT_EQ(element_animations,
192             animation1->keyframe_effect()->element_animations());
193   EXPECT_EQ(element_animations,
194             animation2->keyframe_effect()->element_animations());
195 
196   PushProperties();
197   GetImplTimelineAndAnimationByID();
198 
199   scoped_refptr<ElementAnimations> element_animations_impl =
200       animation_impl_->keyframe_effect()->element_animations();
201   EXPECT_TRUE(element_animations_impl);
202 
203   EXPECT_EQ(3u, element_animations_impl_->CountKeyframesForTesting());
204 
205   animation2->DetachElement();
206   EXPECT_FALSE(animation2->keyframe_effect()->element_animations());
207   EXPECT_EQ(element_animations,
208             animation_->keyframe_effect()->element_animations());
209   EXPECT_EQ(element_animations,
210             animation1->keyframe_effect()->element_animations());
211 
212   PushProperties();
213   EXPECT_EQ(element_animations_impl,
214             animation_impl_->keyframe_effect()->element_animations());
215 
216   EXPECT_EQ(2u, element_animations_impl_->CountKeyframesForTesting());
217 }
218 
TEST_F(ElementAnimationsTest,SyncNewAnimation)219 TEST_F(ElementAnimationsTest, SyncNewAnimation) {
220   CreateTestLayer(true, false);
221   AttachTimelineAnimationLayer();
222   CreateImplTimelineAndAnimation();
223 
224   EXPECT_FALSE(animation_->GetKeyframeModel(TargetProperty::OPACITY));
225   EXPECT_FALSE(animation_impl_->GetKeyframeModel(TargetProperty::OPACITY));
226 
227   int keyframe_model_id =
228       AddOpacityTransitionToAnimation(animation_.get(), 1, 0, 1, false);
229   EXPECT_TRUE(
230       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
231   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
232             animation_->keyframe_effect()
233                 ->GetKeyframeModelById(keyframe_model_id)
234                 ->run_state());
235   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
236       keyframe_model_id));
237 
238   PushProperties();
239   animation_impl_->ActivateKeyframeModels();
240 
241   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
242       keyframe_model_id));
243   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
244             animation_impl_->keyframe_effect()
245                 ->GetKeyframeModelById(keyframe_model_id)
246                 ->run_state());
247 }
248 
TEST_F(ElementAnimationsTest,SyncScrollOffsetAnimationRespectsHasSetInitialValue)249 TEST_F(ElementAnimationsTest,
250        SyncScrollOffsetAnimationRespectsHasSetInitialValue) {
251   CreateTestLayer(true, false);
252   AttachTimelineAnimationLayer();
253   CreateImplTimelineAndAnimation();
254 
255   EXPECT_FALSE(animation_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET));
256   EXPECT_FALSE(
257       animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET));
258 
259   gfx::ScrollOffset initial_value(100.f, 300.f);
260   gfx::ScrollOffset provider_initial_value(150.f, 300.f);
261   gfx::ScrollOffset target_value(300.f, 200.f);
262 
263   client_impl_.SetScrollOffsetForAnimation(provider_initial_value);
264 
265   // Animation with initial value set.
266   std::unique_ptr<ScrollOffsetAnimationCurve> curve_fixed(
267       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
268           target_value));
269   curve_fixed->SetInitialValue(initial_value);
270   const int animation1_id = 1;
271   std::unique_ptr<KeyframeModel> animation_fixed(KeyframeModel::Create(
272       std::move(curve_fixed), animation1_id, 0, TargetProperty::SCROLL_OFFSET));
273   animation_->AddKeyframeModel(std::move(animation_fixed));
274   PushProperties();
275   EXPECT_VECTOR2DF_EQ(initial_value, animation_impl_->keyframe_effect()
276                                          ->GetKeyframeModelById(animation1_id)
277                                          ->curve()
278                                          ->ToScrollOffsetAnimationCurve()
279                                          ->GetValue(base::TimeDelta()));
280   animation_->RemoveKeyframeModel(animation1_id);
281 
282   // Animation without initial value set.
283   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
284       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
285           target_value));
286   const int animation2_id = 2;
287   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
288       std::move(curve), animation2_id, 1, TargetProperty::SCROLL_OFFSET));
289   animation_->AddKeyframeModel(std::move(keyframe_model));
290   PushProperties();
291   EXPECT_VECTOR2DF_EQ(provider_initial_value,
292                       animation_impl_->keyframe_effect()
293                           ->GetKeyframeModelById(animation2_id)
294                           ->curve()
295                           ->ToScrollOffsetAnimationCurve()
296                           ->GetValue(base::TimeDelta()));
297   animation_->RemoveKeyframeModel(animation2_id);
298 }
299 
300 class TestAnimationDelegateThatDestroysAnimation
301     : public TestAnimationDelegate {
302  public:
303   TestAnimationDelegateThatDestroysAnimation() = default;
304 
NotifyAnimationStarted(base::TimeTicks monotonic_time,int target_property,int group)305   void NotifyAnimationStarted(base::TimeTicks monotonic_time,
306                               int target_property,
307                               int group) override {
308     TestAnimationDelegate::NotifyAnimationStarted(monotonic_time,
309                                                   target_property, group);
310     // Detaching animation from the timeline ensures that the timeline doesn't
311     // hold a reference to the animation and the animation is destroyed.
312     timeline_->DetachAnimation(animation_);
313   }
314 
setTimelineAndAnimation(scoped_refptr<AnimationTimeline> timeline,scoped_refptr<Animation> animation)315   void setTimelineAndAnimation(scoped_refptr<AnimationTimeline> timeline,
316                                scoped_refptr<Animation> animation) {
317     timeline_ = timeline;
318     animation_ = animation;
319   }
320 
321  private:
322   scoped_refptr<AnimationTimeline> timeline_;
323   scoped_refptr<Animation> animation_;
324 };
325 
326 // Test that we don't crash if a animation is deleted while ElementAnimations is
327 // iterating through the list of animations (see crbug.com/631052). This test
328 // passes if it doesn't crash.
TEST_F(ElementAnimationsTest,AddedAnimationIsDestroyed)329 TEST_F(ElementAnimationsTest, AddedAnimationIsDestroyed) {
330   CreateTestLayer(true, false);
331   AttachTimelineAnimationLayer();
332   CreateImplTimelineAndAnimation();
333 
334   TestAnimationDelegateThatDestroysAnimation delegate;
335 
336   const int animation2_id = AnimationIdProvider::NextAnimationId();
337   scoped_refptr<Animation> animation2 = Animation::Create(animation2_id);
338   delegate.setTimelineAndAnimation(timeline_, animation2);
339 
340   timeline_->AttachAnimation(animation2);
341   animation2->AttachElement(element_id_);
342   animation2->set_animation_delegate(&delegate);
343 
344   int keyframe_model_id =
345       AddOpacityTransitionToAnimation(animation2.get(), 1.0, 0.f, 1.f, false);
346 
347   PushProperties();
348 
349   scoped_refptr<Animation> animation2_impl =
350       timeline_impl_->GetAnimationById(animation2_id);
351   DCHECK(animation2_impl);
352 
353   animation2_impl->ActivateKeyframeModels();
354   EXPECT_TRUE(animation2_impl->keyframe_effect()->GetKeyframeModelById(
355       keyframe_model_id));
356 
357   animation2_impl->Tick(kInitialTickTime);
358 
359   auto events = CreateEventsForTesting();
360   animation2_impl->UpdateState(true, events.get());
361   EXPECT_EQ(1u, events->events_.size());
362   EXPECT_EQ(AnimationEvent::STARTED, events->events_[0].type);
363 
364   // The actual detachment happens here, inside the callback
365   animation2->DispatchAndDelegateAnimationEvent(events->events_[0]);
366   EXPECT_TRUE(delegate.started());
367 }
368 
369 // If an animation is started on the impl thread before it is ticked on the main
370 // thread, we must be sure to respect the synchronized start time.
TEST_F(ElementAnimationsTest,DoNotClobberStartTimes)371 TEST_F(ElementAnimationsTest, DoNotClobberStartTimes) {
372   CreateTestLayer(true, false);
373   AttachTimelineAnimationLayer();
374   CreateImplTimelineAndAnimation();
375 
376   EXPECT_FALSE(animation_impl_->GetKeyframeModel(TargetProperty::OPACITY));
377 
378   int keyframe_model_id =
379       AddOpacityTransitionToAnimation(animation_.get(), 1, 0, 1, false);
380 
381   PushProperties();
382   animation_impl_->ActivateKeyframeModels();
383 
384   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
385       keyframe_model_id));
386   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
387             animation_impl_->keyframe_effect()
388                 ->GetKeyframeModelById(keyframe_model_id)
389                 ->run_state());
390 
391   auto events = CreateEventsForTesting();
392   animation_impl_->Tick(kInitialTickTime);
393   animation_impl_->UpdateState(true, events.get());
394 
395   // Synchronize the start times.
396   EXPECT_EQ(1u, events->events_.size());
397   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
398   EXPECT_EQ(animation_->keyframe_effect()
399                 ->GetKeyframeModelById(keyframe_model_id)
400                 ->start_time(),
401             animation_impl_->keyframe_effect()
402                 ->GetKeyframeModelById(keyframe_model_id)
403                 ->start_time());
404 
405   // Start the animation on the main thread. Should not affect the start time.
406   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
407   animation_impl_->UpdateState(true, nullptr);
408   EXPECT_EQ(animation_->keyframe_effect()
409                 ->GetKeyframeModelById(keyframe_model_id)
410                 ->start_time(),
411             animation_impl_->keyframe_effect()
412                 ->GetKeyframeModelById(keyframe_model_id)
413                 ->start_time());
414 }
415 
TEST_F(ElementAnimationsTest,UseSpecifiedStartTimes)416 TEST_F(ElementAnimationsTest, UseSpecifiedStartTimes) {
417   CreateTestLayer(true, false);
418   AttachTimelineAnimationLayer();
419   CreateImplTimelineAndAnimation();
420 
421   int keyframe_model_id =
422       AddOpacityTransitionToAnimation(animation_.get(), 1, 0, 1, false);
423 
424   const TimeTicks start_time = TicksFromSecondsF(123);
425   animation_->GetKeyframeModel(TargetProperty::OPACITY)
426       ->set_start_time(start_time);
427 
428   PushProperties();
429   animation_impl_->ActivateKeyframeModels();
430 
431   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
432       keyframe_model_id));
433   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
434             animation_impl_->keyframe_effect()
435                 ->GetKeyframeModelById(keyframe_model_id)
436                 ->run_state());
437 
438   auto events = CreateEventsForTesting();
439   animation_impl_->Tick(kInitialTickTime);
440   animation_impl_->UpdateState(true, events.get());
441 
442   // Synchronize the start times.
443   EXPECT_EQ(1u, events->events_.size());
444   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
445 
446   EXPECT_EQ(start_time, animation_->keyframe_effect()
447                             ->GetKeyframeModelById(keyframe_model_id)
448                             ->start_time());
449   EXPECT_EQ(animation_->keyframe_effect()
450                 ->GetKeyframeModelById(keyframe_model_id)
451                 ->start_time(),
452             animation_impl_->keyframe_effect()
453                 ->GetKeyframeModelById(keyframe_model_id)
454                 ->start_time());
455 
456   // Start the animation on the main thread. Should not affect the start time.
457   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
458   animation_->UpdateState(true, nullptr);
459   EXPECT_EQ(start_time, animation_->keyframe_effect()
460                             ->GetKeyframeModelById(keyframe_model_id)
461                             ->start_time());
462   EXPECT_EQ(animation_->keyframe_effect()
463                 ->GetKeyframeModelById(keyframe_model_id)
464                 ->start_time(),
465             animation_impl_->keyframe_effect()
466                 ->GetKeyframeModelById(keyframe_model_id)
467                 ->start_time());
468 }
469 
470 // Tests that animationss activate and deactivate as expected.
TEST_F(ElementAnimationsTest,Activation)471 TEST_F(ElementAnimationsTest, Activation) {
472   CreateTestLayer(true, false);
473   AttachTimelineAnimationLayer();
474   CreateImplTimelineAndAnimation();
475 
476   AnimationHost* host = client_.host();
477   AnimationHost* host_impl = client_impl_.host();
478 
479   auto events = CreateEventsForTesting();
480 
481   EXPECT_EQ(1u, host->element_animations_for_testing().size());
482   EXPECT_EQ(1u, host_impl->element_animations_for_testing().size());
483 
484   // Initially, both animationss should be inactive.
485   EXPECT_EQ(0u, host->ticking_animations_for_testing().size());
486   EXPECT_EQ(0u, host_impl->ticking_animations_for_testing().size());
487 
488   AddOpacityTransitionToAnimation(animation_.get(), 1, 0, 1, false);
489   // The main thread animations should now be active.
490   EXPECT_EQ(1u, host->ticking_animations_for_testing().size());
491 
492   PushProperties();
493   animation_impl_->ActivateKeyframeModels();
494   // Both animationss should now be active.
495   EXPECT_EQ(1u, host->ticking_animations_for_testing().size());
496   EXPECT_EQ(1u, host_impl->ticking_animations_for_testing().size());
497 
498   animation_impl_->Tick(kInitialTickTime);
499   animation_impl_->UpdateState(true, events.get());
500   EXPECT_EQ(1u, events->events_.size());
501   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
502 
503   EXPECT_EQ(1u, host->ticking_animations_for_testing().size());
504   EXPECT_EQ(1u, host_impl->ticking_animations_for_testing().size());
505 
506   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
507   animation_->UpdateState(true, nullptr);
508   EXPECT_EQ(1u, host->ticking_animations_for_testing().size());
509 
510   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
511   animation_->UpdateState(true, nullptr);
512   EXPECT_EQ(KeyframeModel::FINISHED,
513             animation_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
514   EXPECT_EQ(1u, host->ticking_animations_for_testing().size());
515 
516   events = CreateEventsForTesting();
517 
518   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
519   animation_impl_->UpdateState(true, events.get());
520 
521   EXPECT_EQ(
522       KeyframeModel::WAITING_FOR_DELETION,
523       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
524   // The impl thread animations should have de-activated.
525   EXPECT_EQ(0u, host_impl->ticking_animations_for_testing().size());
526 
527   EXPECT_EQ(1u, events->events_.size());
528   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
529   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
530   animation_->UpdateState(true, nullptr);
531 
532   EXPECT_EQ(KeyframeModel::WAITING_FOR_DELETION,
533             animation_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
534   // The main thread animations should have de-activated.
535   EXPECT_EQ(0u, host->ticking_animations_for_testing().size());
536 
537   PushProperties();
538   animation_impl_->ActivateKeyframeModels();
539   EXPECT_FALSE(animation_->keyframe_effect()->has_any_keyframe_model());
540   EXPECT_FALSE(animation_impl_->keyframe_effect()->has_any_keyframe_model());
541   EXPECT_EQ(0u, host->ticking_animations_for_testing().size());
542   EXPECT_EQ(0u, host_impl->ticking_animations_for_testing().size());
543 }
544 
TEST_F(ElementAnimationsTest,SyncPause)545 TEST_F(ElementAnimationsTest, SyncPause) {
546   CreateTestLayer(true, false);
547   AttachTimelineAnimationLayer();
548   CreateImplTimelineAndAnimation();
549 
550   EXPECT_FALSE(animation_impl_->GetKeyframeModel(TargetProperty::OPACITY));
551 
552   // Two steps, three ranges: [0-1) -> 0.2, [1-2) -> 0.3, [2-3] -> 0.4.
553   const double duration = 3.0;
554   const int keyframe_model_id =
555       AddOpacityStepsToAnimation(animation_.get(), duration, 0.2f, 0.4f, 2);
556 
557   // Set start offset to be at the beginning of the second range.
558   animation_->keyframe_effect()
559       ->GetKeyframeModelById(keyframe_model_id)
560       ->set_time_offset(TimeDelta::FromSecondsD(1.01));
561 
562   PushProperties();
563   animation_impl_->ActivateKeyframeModels();
564 
565   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
566       keyframe_model_id));
567   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
568             animation_impl_->keyframe_effect()
569                 ->GetKeyframeModelById(keyframe_model_id)
570                 ->run_state());
571 
572   TimeTicks time = kInitialTickTime;
573 
574   // Start the animations on each animations.
575   auto events = CreateEventsForTesting();
576   animation_impl_->Tick(time);
577   animation_impl_->UpdateState(true, events.get());
578   EXPECT_EQ(1u, events->events_.size());
579 
580   animation_->Tick(time);
581   animation_->UpdateState(true, nullptr);
582   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
583 
584   EXPECT_EQ(KeyframeModel::RUNNING,
585             animation_impl_->keyframe_effect()
586                 ->GetKeyframeModelById(keyframe_model_id)
587                 ->run_state());
588   EXPECT_EQ(KeyframeModel::RUNNING,
589             animation_->keyframe_effect()
590                 ->GetKeyframeModelById(keyframe_model_id)
591                 ->run_state());
592 
593   EXPECT_EQ(0.3f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
594   EXPECT_EQ(0.3f,
595             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
596 
597   EXPECT_EQ(kInitialTickTime, animation_->keyframe_effect()
598                                   ->GetKeyframeModelById(keyframe_model_id)
599                                   ->start_time());
600   EXPECT_EQ(kInitialTickTime, animation_impl_->keyframe_effect()
601                                   ->GetKeyframeModelById(keyframe_model_id)
602                                   ->start_time());
603 
604   // Pause the animation at the middle of the second range so the offset
605   // delays animation until the middle of the third range.
606   animation_->PauseKeyframeModel(keyframe_model_id,
607                                  base::TimeDelta::FromMilliseconds(1500));
608   EXPECT_EQ(KeyframeModel::PAUSED, animation_->keyframe_effect()
609                                        ->GetKeyframeModelById(keyframe_model_id)
610                                        ->run_state());
611 
612   // The pause run state change should make it to the impl thread animations.
613   PushProperties();
614   animation_impl_->ActivateKeyframeModels();
615 
616   // Advance time so it stays within the first range.
617   time += TimeDelta::FromMilliseconds(10);
618   animation_->Tick(time);
619   animation_impl_->Tick(time);
620 
621   EXPECT_EQ(KeyframeModel::PAUSED, animation_impl_->keyframe_effect()
622                                        ->GetKeyframeModelById(keyframe_model_id)
623                                        ->run_state());
624 
625   // Opacity value doesn't depend on time if paused at specified time offset.
626   EXPECT_EQ(0.4f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
627   EXPECT_EQ(0.4f,
628             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
629 }
630 
TEST_F(ElementAnimationsTest,DoNotSyncFinishedAnimation)631 TEST_F(ElementAnimationsTest, DoNotSyncFinishedAnimation) {
632   CreateTestLayer(true, false);
633   AttachTimelineAnimationLayer();
634   CreateImplTimelineAndAnimation();
635 
636   auto events = CreateEventsForTesting();
637 
638   EXPECT_FALSE(animation_impl_->GetKeyframeModel(TargetProperty::OPACITY));
639 
640   int keyframe_model_id =
641       AddOpacityTransitionToAnimation(animation_.get(), 1, 0, 1, false);
642 
643   PushProperties();
644   animation_impl_->ActivateKeyframeModels();
645 
646   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
647       keyframe_model_id));
648   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
649             animation_impl_->keyframe_effect()
650                 ->GetKeyframeModelById(keyframe_model_id)
651                 ->run_state());
652 
653   events = CreateEventsForTesting();
654   animation_impl_->Tick(kInitialTickTime);
655   animation_impl_->UpdateState(true, events.get());
656   EXPECT_EQ(1u, events->events_.size());
657   EXPECT_EQ(AnimationEvent::STARTED, events->events_[0].type);
658 
659   // Notify main thread animations that the animation has started.
660   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
661 
662   // Complete animation on impl thread.
663   events = CreateEventsForTesting();
664   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromSeconds(1));
665   animation_impl_->UpdateState(true, events.get());
666   EXPECT_EQ(1u, events->events_.size());
667   EXPECT_EQ(AnimationEvent::FINISHED, events->events_[0].type);
668 
669   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
670 
671   animation_->Tick(kInitialTickTime + TimeDelta::FromSeconds(2));
672   animation_->UpdateState(true, nullptr);
673 
674   PushProperties();
675   animation_impl_->ActivateKeyframeModels();
676   EXPECT_FALSE(
677       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
678   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
679       keyframe_model_id));
680 }
681 
682 // Ensure that a finished animation is eventually deleted by both the
683 // main-thread and the impl-thread animationss.
TEST_F(ElementAnimationsTest,AnimationsAreDeleted)684 TEST_F(ElementAnimationsTest, AnimationsAreDeleted) {
685   CreateTestLayer(true, false);
686   AttachTimelineAnimationLayer();
687   CreateImplTimelineAndAnimation();
688 
689   auto events = CreateEventsForTesting();
690 
691   AddOpacityTransitionToAnimation(animation_.get(), 1.0, 0.0f, 1.0f, false);
692   animation_->Tick(kInitialTickTime);
693   animation_->UpdateState(true, nullptr);
694   EXPECT_TRUE(animation_->keyframe_effect()->needs_push_properties());
695 
696   PushProperties();
697   EXPECT_FALSE(animation_->keyframe_effect()->needs_push_properties());
698 
699   EXPECT_FALSE(host_->needs_push_properties());
700   EXPECT_FALSE(host_impl_->needs_push_properties());
701 
702   animation_impl_->ActivateKeyframeModels();
703 
704   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
705   animation_impl_->UpdateState(true, events.get());
706 
707   // There should be a STARTED event for the animation.
708   EXPECT_EQ(1u, events->events_.size());
709   EXPECT_EQ(AnimationEvent::STARTED, events->events_[0].type);
710   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
711 
712   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
713   animation_->UpdateState(true, nullptr);
714 
715   EXPECT_FALSE(host_->needs_push_properties());
716   EXPECT_FALSE(host_impl_->needs_push_properties());
717 
718   events = CreateEventsForTesting();
719   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
720   animation_impl_->UpdateState(true, events.get());
721 
722   EXPECT_TRUE(host_impl_->needs_push_properties());
723 
724   // There should be a FINISHED event for the animation.
725   EXPECT_EQ(1u, events->events_.size());
726   EXPECT_EQ(AnimationEvent::FINISHED, events->events_[0].type);
727 
728   // Neither animations should have deleted the animation yet.
729   EXPECT_TRUE(animation_->GetKeyframeModel(TargetProperty::OPACITY));
730   EXPECT_TRUE(animation_impl_->GetKeyframeModel(TargetProperty::OPACITY));
731 
732   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
733 
734   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
735   animation_->UpdateState(true, nullptr);
736   EXPECT_TRUE(host_->needs_push_properties());
737 
738   PushProperties();
739 
740   // Both animationss should now have deleted the animation. The impl animations
741   // should have deleted the animation even though activation has not occurred,
742   // since the animation was already waiting for deletion when
743   // PushPropertiesTo was called.
744   EXPECT_FALSE(animation_->keyframe_effect()->has_any_keyframe_model());
745   EXPECT_FALSE(animation_impl_->keyframe_effect()->has_any_keyframe_model());
746 }
747 
748 // Tests that transitioning opacity from 0 to 1 works as expected.
749 
CreateKeyframeModel(std::unique_ptr<AnimationCurve> curve,int group_id,TargetProperty::Type property)750 static std::unique_ptr<KeyframeModel> CreateKeyframeModel(
751     std::unique_ptr<AnimationCurve> curve,
752     int group_id,
753     TargetProperty::Type property) {
754   return KeyframeModel::Create(std::move(curve), 0, group_id, property);
755 }
756 
TEST_F(ElementAnimationsTest,TrivialTransition)757 TEST_F(ElementAnimationsTest, TrivialTransition) {
758   CreateTestLayer(true, false);
759   AttachTimelineAnimationLayer();
760 
761   auto events = CreateEventsForTesting();
762 
763   std::unique_ptr<KeyframeModel> to_add(CreateKeyframeModel(
764       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
765       1, TargetProperty::OPACITY));
766   int keyframe_model_id = to_add->id();
767 
768   EXPECT_FALSE(
769       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
770   animation_->AddKeyframeModel(std::move(to_add));
771   EXPECT_TRUE(
772       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
773   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
774             animation_->keyframe_effect()
775                 ->GetKeyframeModelById(keyframe_model_id)
776                 ->run_state());
777   animation_->Tick(kInitialTickTime);
778   EXPECT_EQ(KeyframeModel::STARTING,
779             animation_->keyframe_effect()
780                 ->GetKeyframeModelById(keyframe_model_id)
781                 ->run_state());
782   animation_->UpdateState(true, events.get());
783   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
784   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
785 
786   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
787   animation_->UpdateState(true, events.get());
788   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
789   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
790 }
791 
TEST_F(ElementAnimationsTest,FilterTransition)792 TEST_F(ElementAnimationsTest, FilterTransition) {
793   CreateTestLayer(true, false);
794   AttachTimelineAnimationLayer();
795 
796   auto events = CreateEventsForTesting();
797 
798   std::unique_ptr<KeyframedFilterAnimationCurve> curve(
799       KeyframedFilterAnimationCurve::Create());
800 
801   FilterOperations start_filters;
802   start_filters.Append(FilterOperation::CreateBrightnessFilter(1.f));
803   curve->AddKeyframe(
804       FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
805   FilterOperations end_filters;
806   end_filters.Append(FilterOperation::CreateBrightnessFilter(2.f));
807   curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
808                                             end_filters, nullptr));
809 
810   std::unique_ptr<KeyframeModel> keyframe_model(
811       KeyframeModel::Create(std::move(curve), 1, 0, TargetProperty::FILTER));
812   animation_->AddKeyframeModel(std::move(keyframe_model));
813 
814   animation_->Tick(kInitialTickTime);
815   animation_->UpdateState(true, events.get());
816   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
817   EXPECT_EQ(start_filters,
818             client_.GetFilters(element_id_, ElementListType::ACTIVE));
819 
820   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
821   animation_->UpdateState(true, events.get());
822   EXPECT_EQ(1u,
823             client_.GetFilters(element_id_, ElementListType::ACTIVE).size());
824   EXPECT_EQ(FilterOperation::CreateBrightnessFilter(1.5f),
825             client_.GetFilters(element_id_, ElementListType::ACTIVE).at(0));
826 
827   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
828   animation_->UpdateState(true, events.get());
829   EXPECT_EQ(end_filters,
830             client_.GetFilters(element_id_, ElementListType::ACTIVE));
831   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
832 }
833 
TEST_F(ElementAnimationsTest,BackdropFilterTransition)834 TEST_F(ElementAnimationsTest, BackdropFilterTransition) {
835   CreateTestLayer(true, false);
836   AttachTimelineAnimationLayer();
837 
838   auto events = CreateEventsForTesting();
839 
840   std::unique_ptr<KeyframedFilterAnimationCurve> curve(
841       KeyframedFilterAnimationCurve::Create());
842 
843   FilterOperations start_filters;
844   start_filters.Append(FilterOperation::CreateInvertFilter(0.f));
845   curve->AddKeyframe(
846       FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr));
847   FilterOperations end_filters;
848   end_filters.Append(FilterOperation::CreateInvertFilter(1.f));
849   curve->AddKeyframe(FilterKeyframe::Create(base::TimeDelta::FromSecondsD(1.0),
850                                             end_filters, nullptr));
851 
852   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
853       std::move(curve), 1, 0, TargetProperty::BACKDROP_FILTER));
854   animation_->AddKeyframeModel(std::move(keyframe_model));
855 
856   animation_->Tick(kInitialTickTime);
857   animation_->UpdateState(true, events.get());
858   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
859   EXPECT_EQ(start_filters,
860             client_.GetBackdropFilters(element_id_, ElementListType::ACTIVE));
861 
862   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
863   animation_->UpdateState(true, events.get());
864   EXPECT_EQ(
865       1u,
866       client_.GetBackdropFilters(element_id_, ElementListType::ACTIVE).size());
867   EXPECT_EQ(
868       FilterOperation::CreateInvertFilter(0.5f),
869       client_.GetBackdropFilters(element_id_, ElementListType::ACTIVE).at(0));
870 
871   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
872   animation_->UpdateState(true, events.get());
873   EXPECT_EQ(end_filters,
874             client_.GetBackdropFilters(element_id_, ElementListType::ACTIVE));
875   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
876 }
877 
TEST_F(ElementAnimationsTest,ScrollOffsetTransition)878 TEST_F(ElementAnimationsTest, ScrollOffsetTransition) {
879   CreateTestLayer(true, false);
880   AttachTimelineAnimationLayer();
881   CreateImplTimelineAndAnimation();
882 
883   auto events = CreateEventsForTesting();
884 
885   gfx::ScrollOffset initial_value(100.f, 300.f);
886   gfx::ScrollOffset target_value(300.f, 200.f);
887   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
888       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
889           target_value));
890 
891   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
892       std::move(curve), 1, 0, TargetProperty::SCROLL_OFFSET));
893   keyframe_model->set_needs_synchronized_start_time(true);
894   animation_->AddKeyframeModel(std::move(keyframe_model));
895 
896   client_impl_.SetScrollOffsetForAnimation(initial_value);
897   PushProperties();
898   animation_impl_->ActivateKeyframeModels();
899   EXPECT_TRUE(animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET));
900   TimeDelta duration =
901       animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
902           ->curve()
903           ->Duration();
904   EXPECT_EQ(duration,
905             animation_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
906                 ->curve()
907                 ->Duration());
908 
909   animation_->Tick(kInitialTickTime);
910   animation_->UpdateState(true, nullptr);
911   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
912   EXPECT_EQ(initial_value,
913             client_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
914 
915   animation_impl_->Tick(kInitialTickTime);
916   animation_impl_->UpdateState(true, events.get());
917   EXPECT_TRUE(animation_impl_->keyframe_effect()->HasTickingKeyframeModel());
918   EXPECT_EQ(initial_value,
919             client_impl_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
920 
921   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
922   animation_->Tick(kInitialTickTime + duration / 2);
923   animation_->UpdateState(true, nullptr);
924   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
925   EXPECT_VECTOR2DF_EQ(
926       gfx::Vector2dF(200.f, 250.f),
927       client_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
928 
929   animation_impl_->Tick(kInitialTickTime + duration / 2);
930   animation_impl_->UpdateState(true, events.get());
931   EXPECT_VECTOR2DF_EQ(
932       gfx::Vector2dF(200.f, 250.f),
933       client_impl_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
934 
935   animation_impl_->Tick(kInitialTickTime + duration);
936   animation_impl_->UpdateState(true, events.get());
937   EXPECT_VECTOR2DF_EQ(target_value, client_impl_.GetScrollOffset(
938                                         element_id_, ElementListType::ACTIVE));
939   EXPECT_FALSE(animation_impl_->keyframe_effect()->HasTickingKeyframeModel());
940 
941   animation_->Tick(kInitialTickTime + duration);
942   animation_->UpdateState(true, nullptr);
943   EXPECT_VECTOR2DF_EQ(target_value, client_.GetScrollOffset(
944                                         element_id_, ElementListType::ACTIVE));
945   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
946 }
947 
TEST_F(ElementAnimationsTest,ScrollOffsetTransitionOnImplOnly)948 TEST_F(ElementAnimationsTest, ScrollOffsetTransitionOnImplOnly) {
949   CreateTestLayer(true, false);
950   AttachTimelineAnimationLayer();
951   CreateImplTimelineAndAnimation();
952 
953   auto events = CreateEventsForTesting();
954 
955   gfx::ScrollOffset initial_value(100.f, 300.f);
956   gfx::ScrollOffset target_value(300.f, 200.f);
957   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
958       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
959           target_value));
960   curve->SetInitialValue(initial_value);
961   double duration_in_seconds = curve->Duration().InSecondsF();
962 
963   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
964       std::move(curve), 1, 0, TargetProperty::SCROLL_OFFSET));
965   keyframe_model->SetIsImplOnly();
966   animation_impl_->AddKeyframeModel(std::move(keyframe_model));
967 
968   animation_impl_->Tick(kInitialTickTime);
969   animation_impl_->UpdateState(true, events.get());
970   EXPECT_TRUE(animation_impl_->keyframe_effect()->HasTickingKeyframeModel());
971   EXPECT_EQ(initial_value,
972             client_impl_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
973 
974   TimeDelta duration = TimeDelta::FromMicroseconds(
975       duration_in_seconds * base::Time::kMicrosecondsPerSecond);
976 
977   animation_impl_->Tick(kInitialTickTime + duration / 2);
978   animation_impl_->UpdateState(true, events.get());
979   EXPECT_VECTOR2DF_EQ(
980       gfx::Vector2dF(200.f, 250.f),
981       client_impl_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
982 
983   animation_impl_->Tick(kInitialTickTime + duration);
984   animation_impl_->UpdateState(true, events.get());
985   EXPECT_VECTOR2DF_EQ(target_value, client_impl_.GetScrollOffset(
986                                         element_id_, ElementListType::ACTIVE));
987   EXPECT_FALSE(animation_impl_->keyframe_effect()->HasTickingKeyframeModel());
988 }
989 
990 // This test verifies that if an animation is added after a layer is animated,
991 // it doesn't get promoted to be in the RUNNING state. This prevents cases where
992 // a start time gets set on an animation using the stale value of
993 // last_tick_time_.
TEST_F(ElementAnimationsTest,UpdateStateWithoutAnimate)994 TEST_F(ElementAnimationsTest, UpdateStateWithoutAnimate) {
995   CreateTestLayer(true, false);
996   AttachTimelineAnimationLayer();
997   CreateImplTimelineAndAnimation();
998 
999   auto events = CreateEventsForTesting();
1000 
1001   // Add first scroll offset animation.
1002   AddScrollOffsetAnimationToAnimation(animation_impl_.get(),
1003                                       gfx::ScrollOffset(100.f, 300.f),
1004                                       gfx::ScrollOffset(100.f, 200.f));
1005 
1006   // Calling UpdateState after Animate should promote the animation to running
1007   // state.
1008   animation_impl_->Tick(kInitialTickTime);
1009   animation_impl_->UpdateState(true, events.get());
1010   EXPECT_EQ(KeyframeModel::RUNNING,
1011             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
1012                 ->run_state());
1013 
1014   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1015   animation_impl_->UpdateState(true, events.get());
1016   EXPECT_EQ(nullptr,
1017             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET));
1018 
1019   // Add second scroll offset animation.
1020   AddScrollOffsetAnimationToAnimation(animation_impl_.get(),
1021                                       gfx::ScrollOffset(100.f, 200.f),
1022                                       gfx::ScrollOffset(100.f, 100.f));
1023 
1024   // Calling UpdateState without Animate should NOT promote the animation to
1025   // running state.
1026   animation_impl_->UpdateState(true, events.get());
1027   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
1028             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
1029                 ->run_state());
1030 
1031   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1032   animation_impl_->UpdateState(true, events.get());
1033 
1034   EXPECT_EQ(KeyframeModel::RUNNING,
1035             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
1036                 ->run_state());
1037   EXPECT_VECTOR2DF_EQ(
1038       gfx::ScrollOffset(100.f, 200.f),
1039       client_impl_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
1040 }
1041 
1042 // Ensure that when the impl animations doesn't have a value provider,
1043 // the main-thread animations's value provider is used to obtain the intial
1044 // scroll offset.
TEST_F(ElementAnimationsTest,ScrollOffsetTransitionNoImplProvider)1045 TEST_F(ElementAnimationsTest, ScrollOffsetTransitionNoImplProvider) {
1046   CreateTestLayer(false, false);
1047   CreateTestImplLayer(ElementListType::PENDING);
1048   AttachTimelineAnimationLayer();
1049   CreateImplTimelineAndAnimation();
1050 
1051   EXPECT_TRUE(element_animations_impl_->has_element_in_pending_list());
1052   EXPECT_FALSE(element_animations_impl_->has_element_in_active_list());
1053 
1054   auto events = CreateEventsForTesting();
1055 
1056   gfx::ScrollOffset initial_value(500.f, 100.f);
1057   gfx::ScrollOffset target_value(300.f, 200.f);
1058   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
1059       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
1060           target_value));
1061 
1062   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
1063       std::move(curve), 1, 0, TargetProperty::SCROLL_OFFSET));
1064   keyframe_model->set_needs_synchronized_start_time(true);
1065   animation_->AddKeyframeModel(std::move(keyframe_model));
1066 
1067   client_.SetScrollOffsetForAnimation(initial_value);
1068   PushProperties();
1069   animation_impl_->ActivateKeyframeModels();
1070   EXPECT_TRUE(animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET));
1071   TimeDelta duration =
1072       animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
1073           ->curve()
1074           ->Duration();
1075   EXPECT_EQ(duration,
1076             animation_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
1077                 ->curve()
1078                 ->Duration());
1079 
1080   animation_->Tick(kInitialTickTime);
1081   animation_->UpdateState(true, nullptr);
1082 
1083   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1084   EXPECT_EQ(initial_value,
1085             client_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
1086   EXPECT_EQ(gfx::ScrollOffset(), client_impl_.GetScrollOffset(
1087                                      element_id_, ElementListType::PENDING));
1088 
1089   animation_impl_->Tick(kInitialTickTime);
1090 
1091   EXPECT_TRUE(animation_impl_->keyframe_effect()->HasTickingKeyframeModel());
1092   EXPECT_EQ(initial_value, client_impl_.GetScrollOffset(
1093                                element_id_, ElementListType::PENDING));
1094 
1095   CreateTestImplLayer(ElementListType::ACTIVE);
1096 
1097   animation_impl_->UpdateState(true, events.get());
1098   DCHECK_EQ(1UL, events->events_.size());
1099 
1100   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
1101   animation_->Tick(kInitialTickTime + duration / 2);
1102   animation_->UpdateState(true, nullptr);
1103   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1104   EXPECT_VECTOR2DF_EQ(
1105       gfx::Vector2dF(400.f, 150.f),
1106       client_.GetScrollOffset(element_id_, ElementListType::ACTIVE));
1107 
1108   animation_impl_->Tick(kInitialTickTime + duration / 2);
1109   animation_impl_->UpdateState(true, events.get());
1110   EXPECT_VECTOR2DF_EQ(
1111       gfx::Vector2dF(400.f, 150.f),
1112       client_impl_.GetScrollOffset(element_id_, ElementListType::PENDING));
1113 
1114   animation_impl_->Tick(kInitialTickTime + duration);
1115   animation_impl_->UpdateState(true, events.get());
1116   EXPECT_VECTOR2DF_EQ(target_value, client_impl_.GetScrollOffset(
1117                                         element_id_, ElementListType::PENDING));
1118   EXPECT_FALSE(animation_impl_->keyframe_effect()->HasTickingKeyframeModel());
1119 
1120   animation_->Tick(kInitialTickTime + duration);
1121   animation_->UpdateState(true, nullptr);
1122   EXPECT_VECTOR2DF_EQ(target_value, client_.GetScrollOffset(
1123                                         element_id_, ElementListType::ACTIVE));
1124   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1125 }
1126 
TEST_F(ElementAnimationsTest,ScrollOffsetRemovalClearsScrollDelta)1127 TEST_F(ElementAnimationsTest, ScrollOffsetRemovalClearsScrollDelta) {
1128   CreateTestLayer(true, false);
1129   AttachTimelineAnimationLayer();
1130   CreateImplTimelineAndAnimation();
1131 
1132   auto events = CreateEventsForTesting();
1133 
1134   // First test the 1-argument version of RemoveKeyframeModel.
1135   gfx::ScrollOffset target_value(300.f, 200.f);
1136   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
1137       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
1138           target_value));
1139 
1140   int keyframe_model_id = 1;
1141   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
1142       std::move(curve), keyframe_model_id, 0, TargetProperty::SCROLL_OFFSET));
1143   keyframe_model->set_needs_synchronized_start_time(true);
1144   animation_->AddKeyframeModel(std::move(keyframe_model));
1145   PushProperties();
1146   animation_impl_->ActivateKeyframeModels();
1147   EXPECT_FALSE(
1148       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1149   EXPECT_FALSE(animation_impl_->keyframe_effect()
1150                    ->scroll_offset_animation_was_interrupted());
1151 
1152   animation_->RemoveKeyframeModel(keyframe_model_id);
1153   EXPECT_TRUE(
1154       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1155 
1156   PushProperties();
1157   EXPECT_TRUE(animation_impl_->keyframe_effect()
1158                   ->scroll_offset_animation_was_interrupted());
1159   EXPECT_FALSE(
1160       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1161 
1162   animation_impl_->ActivateKeyframeModels();
1163   EXPECT_FALSE(animation_impl_->keyframe_effect()
1164                    ->scroll_offset_animation_was_interrupted());
1165 
1166   // Now, test the 2-argument version of RemoveKeyframeModel.
1167   curve = ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
1168       target_value);
1169   keyframe_model = KeyframeModel::Create(std::move(curve), keyframe_model_id, 0,
1170                                          TargetProperty::SCROLL_OFFSET);
1171   keyframe_model->set_needs_synchronized_start_time(true);
1172   animation_->AddKeyframeModel(std::move(keyframe_model));
1173   PushProperties();
1174   animation_impl_->ActivateKeyframeModels();
1175   EXPECT_FALSE(
1176       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1177   EXPECT_FALSE(animation_impl_->keyframe_effect()
1178                    ->scroll_offset_animation_was_interrupted());
1179 
1180   animation_->RemoveKeyframeModel(keyframe_model_id);
1181   EXPECT_TRUE(
1182       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1183 
1184   PushProperties();
1185   EXPECT_TRUE(animation_impl_->keyframe_effect()
1186                   ->scroll_offset_animation_was_interrupted());
1187   EXPECT_FALSE(
1188       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1189 
1190   animation_impl_->ActivateKeyframeModels();
1191   EXPECT_FALSE(animation_impl_->keyframe_effect()
1192                    ->scroll_offset_animation_was_interrupted());
1193 
1194   // Check that removing non-scroll-offset animations does not cause
1195   // scroll_offset_animation_was_interrupted() to get set.
1196   keyframe_model_id =
1197       AddAnimatedTransformToAnimation(animation_.get(), 1.0, 1, 2);
1198   PushProperties();
1199   animation_impl_->ActivateKeyframeModels();
1200   EXPECT_FALSE(
1201       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1202   EXPECT_FALSE(animation_impl_->keyframe_effect()
1203                    ->scroll_offset_animation_was_interrupted());
1204 
1205   animation_->RemoveKeyframeModel(keyframe_model_id);
1206   EXPECT_FALSE(
1207       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1208 
1209   PushProperties();
1210   EXPECT_FALSE(animation_impl_->keyframe_effect()
1211                    ->scroll_offset_animation_was_interrupted());
1212   EXPECT_FALSE(
1213       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1214 
1215   animation_impl_->ActivateKeyframeModels();
1216   EXPECT_FALSE(animation_impl_->keyframe_effect()
1217                    ->scroll_offset_animation_was_interrupted());
1218 
1219   keyframe_model_id =
1220       AddAnimatedFilterToAnimation(animation_.get(), 1.0, 0.1f, 0.2f);
1221   PushProperties();
1222   animation_impl_->ActivateKeyframeModels();
1223   EXPECT_FALSE(
1224       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1225   EXPECT_FALSE(animation_impl_->keyframe_effect()
1226                    ->scroll_offset_animation_was_interrupted());
1227 
1228   animation_->RemoveKeyframeModel(keyframe_model_id);
1229   EXPECT_FALSE(
1230       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1231 
1232   PushProperties();
1233   EXPECT_FALSE(animation_impl_->keyframe_effect()
1234                    ->scroll_offset_animation_was_interrupted());
1235   EXPECT_FALSE(
1236       animation_->keyframe_effect()->scroll_offset_animation_was_interrupted());
1237 
1238   animation_impl_->ActivateKeyframeModels();
1239   EXPECT_FALSE(animation_impl_->keyframe_effect()
1240                    ->scroll_offset_animation_was_interrupted());
1241 }
1242 
1243 // Tests that impl-only animations lead to start and finished notifications
1244 // on the impl thread animations's animation delegate.
TEST_F(ElementAnimationsTest,NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate)1245 TEST_F(ElementAnimationsTest,
1246        NotificationsForImplOnlyAnimationsAreSentToImplThreadDelegate) {
1247   CreateTestLayer(true, false);
1248   AttachTimelineAnimationLayer();
1249   CreateImplTimelineAndAnimation();
1250 
1251   auto events = CreateEventsForTesting();
1252 
1253   TestAnimationDelegate delegate;
1254   animation_impl_->set_animation_delegate(&delegate);
1255 
1256   gfx::ScrollOffset initial_value(100.f, 300.f);
1257   gfx::ScrollOffset target_value(300.f, 200.f);
1258   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
1259       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
1260           target_value));
1261   curve->SetInitialValue(initial_value);
1262   TimeDelta duration = curve->Duration();
1263   std::unique_ptr<KeyframeModel> to_add(KeyframeModel::Create(
1264       std::move(curve), 1, 0, TargetProperty::SCROLL_OFFSET));
1265   to_add->SetIsImplOnly();
1266   animation_impl_->AddKeyframeModel(std::move(to_add));
1267 
1268   EXPECT_FALSE(delegate.started());
1269   EXPECT_FALSE(delegate.finished());
1270 
1271   animation_impl_->Tick(kInitialTickTime);
1272   animation_impl_->UpdateState(true, events.get());
1273 
1274   EXPECT_TRUE(delegate.started());
1275   EXPECT_FALSE(delegate.finished());
1276 
1277   events = CreateEventsForTesting();
1278   animation_impl_->Tick(kInitialTickTime + duration);
1279   EXPECT_EQ(duration,
1280             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
1281                 ->curve()
1282                 ->Duration());
1283   animation_impl_->UpdateState(true, events.get());
1284 
1285   EXPECT_TRUE(delegate.started());
1286   EXPECT_TRUE(delegate.finished());
1287 }
1288 
1289 // Tests that specified start times are sent to the main thread delegate
TEST_F(ElementAnimationsTest,SpecifiedStartTimesAreSentToMainThreadDelegate)1290 TEST_F(ElementAnimationsTest, SpecifiedStartTimesAreSentToMainThreadDelegate) {
1291   CreateTestLayer(true, false);
1292   AttachTimelineAnimationLayer();
1293   CreateImplTimelineAndAnimation();
1294 
1295   TestAnimationDelegate delegate;
1296   animation_->set_animation_delegate(&delegate);
1297 
1298   int keyframe_model_id =
1299       AddOpacityTransitionToAnimation(animation_.get(), 1, 0, 1, false);
1300 
1301   const TimeTicks start_time = TicksFromSecondsF(123);
1302   animation_->GetKeyframeModel(TargetProperty::OPACITY)
1303       ->set_start_time(start_time);
1304 
1305   PushProperties();
1306   animation_impl_->ActivateKeyframeModels();
1307 
1308   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
1309       keyframe_model_id));
1310   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
1311             animation_impl_->keyframe_effect()
1312                 ->GetKeyframeModelById(keyframe_model_id)
1313                 ->run_state());
1314 
1315   auto events = CreateEventsForTesting();
1316   animation_impl_->Tick(kInitialTickTime);
1317   animation_impl_->UpdateState(true, events.get());
1318 
1319   // Synchronize the start times.
1320   EXPECT_EQ(1u, events->events_.size());
1321   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
1322 
1323   // Validate start time on the main thread delegate.
1324   EXPECT_EQ(start_time, delegate.start_time());
1325 }
1326 
1327 // Tests animations that are waiting for a synchronized start time do not
1328 // finish.
TEST_F(ElementAnimationsTest,AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish)1329 TEST_F(ElementAnimationsTest,
1330        AnimationsWaitingForStartTimeDoNotFinishIfTheyOutwaitTheirFinish) {
1331   CreateTestLayer(false, false);
1332   AttachTimelineAnimationLayer();
1333 
1334   auto events = CreateEventsForTesting();
1335 
1336   std::unique_ptr<KeyframeModel> to_add(CreateKeyframeModel(
1337       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1338       1, TargetProperty::OPACITY));
1339   to_add->set_needs_synchronized_start_time(true);
1340   int keyframe_model_id = to_add->id();
1341 
1342   // We should pause at the first keyframe indefinitely waiting for that
1343   // animation to start.
1344   animation_->AddKeyframeModel(std::move(to_add));
1345   animation_->Tick(kInitialTickTime);
1346   animation_->UpdateState(true, events.get());
1347   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1348   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1349   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1350   animation_->UpdateState(true, events.get());
1351   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1352   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1353   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1354   animation_->UpdateState(true, events.get());
1355   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1356   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1357 
1358   // Send the synchronized start time.
1359   animation_->DispatchAndDelegateAnimationEvent(
1360       AnimationEvent(AnimationEvent::STARTED,
1361                      {animation_->animation_timeline()->id(), animation_->id(),
1362                       keyframe_model_id},
1363                      1, TargetProperty::OPACITY,
1364                      kInitialTickTime + TimeDelta::FromMilliseconds(2000)));
1365   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(5000));
1366   animation_->UpdateState(true, events.get());
1367   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1368   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1369 }
1370 
1371 // Tests that two queued animations affecting the same property run in sequence.
TEST_F(ElementAnimationsTest,TrivialQueuing)1372 TEST_F(ElementAnimationsTest, TrivialQueuing) {
1373   CreateTestLayer(false, false);
1374   AttachTimelineAnimationLayer();
1375 
1376   auto events = CreateEventsForTesting();
1377 
1378   int animation1_id = 1;
1379   int animation2_id = 2;
1380   animation_->AddKeyframeModel(KeyframeModel::Create(
1381       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1382       animation1_id, 1, TargetProperty::OPACITY));
1383   animation_->AddKeyframeModel(KeyframeModel::Create(
1384       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f)),
1385       animation2_id, 2, TargetProperty::OPACITY));
1386 
1387   animation_->Tick(kInitialTickTime);
1388 
1389   // The first animation should have been started.
1390   EXPECT_TRUE(
1391       animation_->keyframe_effect()->GetKeyframeModelById(animation1_id));
1392   EXPECT_EQ(KeyframeModel::STARTING, animation_->keyframe_effect()
1393                                          ->GetKeyframeModelById(animation1_id)
1394                                          ->run_state());
1395 
1396   // The second animation still needs to be started.
1397   EXPECT_TRUE(
1398       animation_->keyframe_effect()->GetKeyframeModelById(animation2_id));
1399   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
1400             animation_->keyframe_effect()
1401                 ->GetKeyframeModelById(animation2_id)
1402                 ->run_state());
1403 
1404   animation_->UpdateState(true, events.get());
1405   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1406   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1407 
1408   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1409   animation_->UpdateState(true, events.get());
1410 
1411   // Now the first should be finished, and the second started.
1412   EXPECT_TRUE(
1413       animation_->keyframe_effect()->GetKeyframeModelById(animation1_id));
1414   EXPECT_EQ(KeyframeModel::FINISHED, animation_->keyframe_effect()
1415                                          ->GetKeyframeModelById(animation1_id)
1416                                          ->run_state());
1417   EXPECT_TRUE(
1418       animation_->keyframe_effect()->GetKeyframeModelById(animation2_id));
1419   EXPECT_EQ(KeyframeModel::RUNNING, animation_->keyframe_effect()
1420                                         ->GetKeyframeModelById(animation2_id)
1421                                         ->run_state());
1422 
1423   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1424   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1425   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1426   animation_->UpdateState(true, events.get());
1427   EXPECT_EQ(0.5f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1428   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1429 }
1430 
1431 // Tests interrupting a transition with another transition.
TEST_F(ElementAnimationsTest,Interrupt)1432 TEST_F(ElementAnimationsTest, Interrupt) {
1433   CreateTestLayer(false, false);
1434   AttachTimelineAnimationLayer();
1435 
1436   auto events = CreateEventsForTesting();
1437 
1438   animation_->AddKeyframeModel(CreateKeyframeModel(
1439       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1440       1, TargetProperty::OPACITY));
1441   animation_->Tick(kInitialTickTime);
1442   animation_->UpdateState(true, events.get());
1443   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1444   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1445 
1446   std::unique_ptr<KeyframeModel> to_add(CreateKeyframeModel(
1447       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f)),
1448       2, TargetProperty::OPACITY));
1449   animation_->AbortKeyframeModelsWithProperty(TargetProperty::OPACITY, false);
1450   animation_->AddKeyframeModel(std::move(to_add));
1451 
1452   // Since the previous animation was aborted, the new animation should start
1453   // right in this call to animate.
1454   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1455   animation_->UpdateState(true, events.get());
1456   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1457   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1458   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1500));
1459   animation_->UpdateState(true, events.get());
1460   EXPECT_EQ(0.5f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1461   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1462 }
1463 
1464 // Tests scheduling two animations to run together when only one property is
1465 // free.
TEST_F(ElementAnimationsTest,ScheduleTogetherWhenAPropertyIsBlocked)1466 TEST_F(ElementAnimationsTest, ScheduleTogetherWhenAPropertyIsBlocked) {
1467   CreateTestLayer(false, false);
1468   AttachTimelineAnimationLayer();
1469 
1470   auto events = CreateEventsForTesting();
1471 
1472   animation_->AddKeyframeModel(CreateKeyframeModel(
1473       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1)), 1,
1474       TargetProperty::TRANSFORM));
1475   animation_->AddKeyframeModel(CreateKeyframeModel(
1476       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1)), 2,
1477       TargetProperty::TRANSFORM));
1478   animation_->AddKeyframeModel(CreateKeyframeModel(
1479       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1480       2, TargetProperty::OPACITY));
1481 
1482   animation_->Tick(kInitialTickTime);
1483   animation_->UpdateState(true, events.get());
1484   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1485   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1486   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1487   animation_->UpdateState(true, events.get());
1488   // Should not have started the float transition yet.
1489   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1490   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1491   // The float animation should have started at time 1 and should be done.
1492   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1493   animation_->UpdateState(true, events.get());
1494   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1495   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1496 }
1497 
1498 // Tests scheduling two animations to run together with different lengths and
1499 // another animation queued to start when the shorter animation finishes (should
1500 // wait for both to finish).
TEST_F(ElementAnimationsTest,ScheduleTogetherWithAnAnimWaiting)1501 TEST_F(ElementAnimationsTest, ScheduleTogetherWithAnAnimWaiting) {
1502   CreateTestLayer(false, false);
1503   AttachTimelineAnimationLayer();
1504 
1505   auto events = CreateEventsForTesting();
1506 
1507   animation_->AddKeyframeModel(CreateKeyframeModel(
1508       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(2)), 1,
1509       TargetProperty::TRANSFORM));
1510   animation_->AddKeyframeModel(CreateKeyframeModel(
1511       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1512       1, TargetProperty::OPACITY));
1513   animation_->AddKeyframeModel(CreateKeyframeModel(
1514       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f)),
1515       2, TargetProperty::OPACITY));
1516 
1517   // Animations with id 1 should both start now.
1518   animation_->Tick(kInitialTickTime);
1519   animation_->UpdateState(true, events.get());
1520   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1521   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1522   // The opacity animation should have finished at time 1, but the group
1523   // of animations with id 1 don't finish until time 2 because of the length
1524   // of the transform animation.
1525   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1526   animation_->UpdateState(true, events.get());
1527   // Should not have started the float transition yet.
1528   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1529   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1530 
1531   // The second opacity animation should start at time 2 and should be done by
1532   // time 3.
1533   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1534   animation_->UpdateState(true, events.get());
1535   EXPECT_EQ(0.5f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1536   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1537 }
1538 
1539 // Test that a looping animation loops and for the correct number of iterations.
TEST_F(ElementAnimationsTest,TrivialLooping)1540 TEST_F(ElementAnimationsTest, TrivialLooping) {
1541   CreateTestLayer(false, false);
1542   AttachTimelineAnimationLayer();
1543 
1544   auto events = CreateEventsForTesting();
1545 
1546   std::unique_ptr<KeyframeModel> to_add(CreateKeyframeModel(
1547       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1548       1, TargetProperty::OPACITY));
1549   to_add->set_iterations(3);
1550   animation_->AddKeyframeModel(std::move(to_add));
1551 
1552   animation_->Tick(kInitialTickTime);
1553   animation_->UpdateState(true, events.get());
1554   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1555   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1556   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1557   animation_->UpdateState(true, events.get());
1558   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1559   EXPECT_EQ(0.25f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1560   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1561   animation_->UpdateState(true, events.get());
1562   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1563   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1564   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2250));
1565   animation_->UpdateState(true, events.get());
1566   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1567   EXPECT_EQ(0.25f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1568   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2750));
1569   animation_->UpdateState(true, events.get());
1570   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1571   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1572   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1573   animation_->UpdateState(true, events.get());
1574   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1575   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1576 
1577   // Just be extra sure.
1578   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
1579   animation_->UpdateState(true, events.get());
1580   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1581 }
1582 
1583 // Test that an infinitely looping animation does indeed go until aborted.
TEST_F(ElementAnimationsTest,InfiniteLooping)1584 TEST_F(ElementAnimationsTest, InfiniteLooping) {
1585   CreateTestLayer(false, false);
1586   AttachTimelineAnimationLayer();
1587 
1588   auto events = CreateEventsForTesting();
1589 
1590   std::unique_ptr<KeyframeModel> to_add(CreateKeyframeModel(
1591       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1592       1, TargetProperty::OPACITY));
1593   to_add->set_iterations(std::numeric_limits<double>::infinity());
1594   animation_->AddKeyframeModel(std::move(to_add));
1595 
1596   animation_->Tick(kInitialTickTime);
1597   animation_->UpdateState(true, events.get());
1598   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1599   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1600   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1250));
1601   animation_->UpdateState(true, events.get());
1602   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1603   EXPECT_EQ(0.25f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1604   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1750));
1605   animation_->UpdateState(true, events.get());
1606   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1607   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1608 
1609   animation_->Tick(kInitialTickTime +
1610                    TimeDelta::FromMilliseconds(1073741824250));
1611   animation_->UpdateState(true, events.get());
1612   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1613   EXPECT_EQ(0.25f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1614   animation_->Tick(kInitialTickTime +
1615                    TimeDelta::FromMilliseconds(1073741824750));
1616   animation_->UpdateState(true, events.get());
1617   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1618   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1619 
1620   EXPECT_TRUE(animation_->GetKeyframeModel(TargetProperty::OPACITY));
1621   animation_->GetKeyframeModel(TargetProperty::OPACITY)
1622       ->SetRunState(KeyframeModel::ABORTED,
1623                     kInitialTickTime + TimeDelta::FromMilliseconds(750));
1624   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1625   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1626 }
1627 
1628 // Test that pausing and resuming work as expected.
TEST_F(ElementAnimationsTest,PauseResume)1629 TEST_F(ElementAnimationsTest, PauseResume) {
1630   CreateTestLayer(false, false);
1631   AttachTimelineAnimationLayer();
1632 
1633   auto events = CreateEventsForTesting();
1634 
1635   animation_->AddKeyframeModel(CreateKeyframeModel(
1636       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1637       1, TargetProperty::OPACITY));
1638 
1639   animation_->Tick(kInitialTickTime);
1640   animation_->UpdateState(true, events.get());
1641   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1642   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1643   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
1644   animation_->UpdateState(true, events.get());
1645   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1646   EXPECT_EQ(0.5f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1647 
1648   EXPECT_TRUE(animation_->GetKeyframeModel(TargetProperty::OPACITY));
1649   animation_->GetKeyframeModel(TargetProperty::OPACITY)
1650       ->SetRunState(KeyframeModel::PAUSED,
1651                     kInitialTickTime + TimeDelta::FromMilliseconds(500));
1652 
1653   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1654   animation_->UpdateState(true, events.get());
1655   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1656   EXPECT_EQ(0.5f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1657 
1658   EXPECT_TRUE(animation_->GetKeyframeModel(TargetProperty::OPACITY));
1659   animation_->GetKeyframeModel(TargetProperty::OPACITY)
1660       ->SetRunState(KeyframeModel::RUNNING,
1661                     kInitialTickTime + TimeDelta::FromMilliseconds(1024000));
1662   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1024250));
1663   animation_->UpdateState(true, events.get());
1664   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1665   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1666 
1667   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1024500));
1668   animation_->UpdateState(true, events.get());
1669   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1670   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1671 }
1672 
TEST_F(ElementAnimationsTest,AbortAGroupedAnimation)1673 TEST_F(ElementAnimationsTest, AbortAGroupedAnimation) {
1674   CreateTestLayer(false, false);
1675   AttachTimelineAnimationLayer();
1676 
1677   auto events = CreateEventsForTesting();
1678 
1679   const int keyframe_model_id = 2;
1680   animation_->AddKeyframeModel(KeyframeModel::Create(
1681       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1)), 1, 1,
1682       TargetProperty::TRANSFORM));
1683   animation_->AddKeyframeModel(KeyframeModel::Create(
1684       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)),
1685       keyframe_model_id, 1, TargetProperty::OPACITY));
1686   animation_->AddKeyframeModel(KeyframeModel::Create(
1687       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.75f)),
1688       3, 2, TargetProperty::OPACITY));
1689 
1690   animation_->Tick(kInitialTickTime);
1691   animation_->UpdateState(true, events.get());
1692   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1693   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1694   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1695   animation_->UpdateState(true, events.get());
1696   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1697   EXPECT_EQ(0.5f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1698 
1699   EXPECT_TRUE(
1700       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
1701   animation_->keyframe_effect()
1702       ->GetKeyframeModelById(keyframe_model_id)
1703       ->SetRunState(KeyframeModel::ABORTED,
1704                     kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1705   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1706   animation_->UpdateState(true, events.get());
1707   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1708   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1709   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1710   animation_->UpdateState(true, events.get());
1711   EXPECT_TRUE(!animation_->keyframe_effect()->HasTickingKeyframeModel());
1712   EXPECT_EQ(0.75f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1713 }
1714 
TEST_F(ElementAnimationsTest,PushUpdatesWhenSynchronizedStartTimeNeeded)1715 TEST_F(ElementAnimationsTest, PushUpdatesWhenSynchronizedStartTimeNeeded) {
1716   CreateTestLayer(true, false);
1717   AttachTimelineAnimationLayer();
1718   CreateImplTimelineAndAnimation();
1719 
1720   auto events = CreateEventsForTesting();
1721 
1722   std::unique_ptr<KeyframeModel> to_add(CreateKeyframeModel(
1723       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)),
1724       0, TargetProperty::OPACITY));
1725   to_add->set_needs_synchronized_start_time(true);
1726   animation_->AddKeyframeModel(std::move(to_add));
1727 
1728   animation_->Tick(kInitialTickTime);
1729   animation_->UpdateState(true, events.get());
1730   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1731   KeyframeModel* active_keyframe_model =
1732       animation_->GetKeyframeModel(TargetProperty::OPACITY);
1733   EXPECT_TRUE(active_keyframe_model);
1734   EXPECT_TRUE(active_keyframe_model->needs_synchronized_start_time());
1735 
1736   EXPECT_TRUE(animation_->keyframe_effect()->needs_push_properties());
1737   PushProperties();
1738   animation_impl_->ActivateKeyframeModels();
1739 
1740   active_keyframe_model =
1741       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY);
1742   EXPECT_TRUE(active_keyframe_model);
1743   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
1744             active_keyframe_model->run_state());
1745 }
1746 
1747 // Tests that skipping a call to UpdateState works as expected.
TEST_F(ElementAnimationsTest,SkipUpdateState)1748 TEST_F(ElementAnimationsTest, SkipUpdateState) {
1749   CreateTestLayer(true, false);
1750   AttachTimelineAnimationLayer();
1751 
1752   auto events = CreateEventsForTesting();
1753 
1754   std::unique_ptr<KeyframeModel> first_keyframe_model(CreateKeyframeModel(
1755       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1)), 1,
1756       TargetProperty::TRANSFORM));
1757   first_keyframe_model->set_is_controlling_instance_for_test(true);
1758   animation_->AddKeyframeModel(std::move(first_keyframe_model));
1759 
1760   animation_->Tick(kInitialTickTime);
1761   animation_->UpdateState(true, events.get());
1762 
1763   std::unique_ptr<KeyframeModel> second_keyframe_model(CreateKeyframeModel(
1764       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1765       2, TargetProperty::OPACITY));
1766   second_keyframe_model->set_is_controlling_instance_for_test(true);
1767   animation_->AddKeyframeModel(std::move(second_keyframe_model));
1768 
1769   // Animate but don't UpdateState.
1770   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1771 
1772   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1773   events = CreateEventsForTesting();
1774   animation_->UpdateState(true, events.get());
1775 
1776   // Should have one STARTED event and one FINISHED event.
1777   EXPECT_EQ(2u, events->events_.size());
1778   EXPECT_NE(events->events_[0].type, events->events_[1].type);
1779 
1780   // The float transition should still be at its starting point.
1781   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1782   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1783 
1784   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1785   animation_->UpdateState(true, events.get());
1786 
1787   // The float tranisition should now be done.
1788   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
1789   EXPECT_FALSE(animation_->keyframe_effect()->HasTickingKeyframeModel());
1790 }
1791 
1792 // Tests that an animation animations with only a pending observer gets ticked
1793 // but doesn't progress animations past the STARTING state.
TEST_F(ElementAnimationsTest,InactiveObserverGetsTicked)1794 TEST_F(ElementAnimationsTest, InactiveObserverGetsTicked) {
1795   AttachTimelineAnimationLayer();
1796   CreateImplTimelineAndAnimation();
1797 
1798   auto events = CreateEventsForTesting();
1799 
1800   const int id = 1;
1801   animation_impl_->AddKeyframeModel(CreateKeyframeModel(
1802       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.5f, 1.f)),
1803       id, TargetProperty::OPACITY));
1804   animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)
1805       ->set_affects_active_elements(false);
1806 
1807   // Without an observer, the animation shouldn't progress to the STARTING
1808   // state.
1809   animation_impl_->Tick(kInitialTickTime);
1810   animation_impl_->UpdateState(true, events.get());
1811   EXPECT_EQ(0u, events->events_.size());
1812   EXPECT_EQ(
1813       KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
1814       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1815 
1816   CreateTestImplLayer(ElementListType::PENDING);
1817 
1818   // With only a pending observer, the animation should progress to the
1819   // STARTING state and get ticked at its starting point, but should not
1820   // progress to RUNNING.
1821   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1822   animation_impl_->UpdateState(true, events.get());
1823   EXPECT_EQ(0u, events->events_.size());
1824   EXPECT_EQ(
1825       KeyframeModel::STARTING,
1826       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1827   EXPECT_EQ(0.5f,
1828             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
1829 
1830   // Even when already in the STARTING state, the animation should stay
1831   // there, and shouldn't be ticked past its starting point.
1832   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
1833   animation_impl_->UpdateState(true, events.get());
1834   EXPECT_EQ(0u, events->events_.size());
1835   EXPECT_EQ(
1836       KeyframeModel::STARTING,
1837       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1838   EXPECT_EQ(0.5f,
1839             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
1840 
1841   CreateTestImplLayer(ElementListType::ACTIVE);
1842   animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)
1843       ->set_affects_active_elements(true);
1844 
1845   // Now that an active observer has been added, the animation should still
1846   // initially tick at its starting point, but should now progress to RUNNING.
1847   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(3000));
1848   animation_impl_->UpdateState(true, events.get());
1849   EXPECT_EQ(1u, events->events_.size());
1850   EXPECT_EQ(
1851       KeyframeModel::RUNNING,
1852       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1853   EXPECT_EQ(0.5f,
1854             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
1855   EXPECT_EQ(0.5f,
1856             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
1857 
1858   // The animation should now tick past its starting point.
1859   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(3500));
1860   EXPECT_NE(0.5f,
1861             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
1862   EXPECT_NE(0.5f,
1863             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
1864 }
1865 
1866 // Tests that AbortKeyframeModelsWithProperty aborts all animations targeting
1867 // the specified property.
TEST_F(ElementAnimationsTest,AbortKeyframeModelsWithProperty)1868 TEST_F(ElementAnimationsTest, AbortKeyframeModelsWithProperty) {
1869   CreateTestLayer(false, false);
1870   AttachTimelineAnimationLayer();
1871 
1872   // Start with several animations, and allow some of them to reach the finished
1873   // state.
1874   animation_->AddKeyframeModel(KeyframeModel::Create(
1875       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1.0)), 1, 1,
1876       TargetProperty::TRANSFORM));
1877   animation_->AddKeyframeModel(KeyframeModel::Create(
1878       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1879       2, 2, TargetProperty::OPACITY));
1880   animation_->AddKeyframeModel(KeyframeModel::Create(
1881       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1.0)), 3, 3,
1882       TargetProperty::TRANSFORM));
1883   animation_->AddKeyframeModel(KeyframeModel::Create(
1884       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(2.0)), 4, 4,
1885       TargetProperty::TRANSFORM));
1886   animation_->AddKeyframeModel(KeyframeModel::Create(
1887       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
1888       5, 5, TargetProperty::OPACITY));
1889 
1890   animation_->Tick(kInitialTickTime);
1891   animation_->UpdateState(true, nullptr);
1892   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
1893   animation_->UpdateState(true, nullptr);
1894 
1895   EXPECT_EQ(
1896       KeyframeModel::FINISHED,
1897       animation_->keyframe_effect()->GetKeyframeModelById(1)->run_state());
1898   EXPECT_EQ(
1899       KeyframeModel::FINISHED,
1900       animation_->keyframe_effect()->GetKeyframeModelById(2)->run_state());
1901   EXPECT_EQ(
1902       KeyframeModel::RUNNING,
1903       animation_->keyframe_effect()->GetKeyframeModelById(3)->run_state());
1904   EXPECT_EQ(
1905       KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
1906       animation_->keyframe_effect()->GetKeyframeModelById(4)->run_state());
1907   EXPECT_EQ(
1908       KeyframeModel::RUNNING,
1909       animation_->keyframe_effect()->GetKeyframeModelById(5)->run_state());
1910 
1911   animation_->AbortKeyframeModelsWithProperty(TargetProperty::TRANSFORM, false);
1912 
1913   // Only un-finished TRANSFORM animations should have been aborted.
1914   EXPECT_EQ(
1915       KeyframeModel::FINISHED,
1916       animation_->keyframe_effect()->GetKeyframeModelById(1)->run_state());
1917   EXPECT_EQ(
1918       KeyframeModel::FINISHED,
1919       animation_->keyframe_effect()->GetKeyframeModelById(2)->run_state());
1920   EXPECT_EQ(
1921       KeyframeModel::ABORTED,
1922       animation_->keyframe_effect()->GetKeyframeModelById(3)->run_state());
1923   EXPECT_EQ(
1924       KeyframeModel::ABORTED,
1925       animation_->keyframe_effect()->GetKeyframeModelById(4)->run_state());
1926   EXPECT_EQ(
1927       KeyframeModel::RUNNING,
1928       animation_->keyframe_effect()->GetKeyframeModelById(5)->run_state());
1929 }
1930 
1931 // An animation aborted on the main thread should get deleted on both threads.
TEST_F(ElementAnimationsTest,MainThreadAbortedAnimationGetsDeleted)1932 TEST_F(ElementAnimationsTest, MainThreadAbortedAnimationGetsDeleted) {
1933   CreateTestLayer(true, false);
1934   AttachTimelineAnimationLayer();
1935   CreateImplTimelineAndAnimation();
1936 
1937   int keyframe_model_id =
1938       AddOpacityTransitionToAnimation(animation_.get(), 1.0, 0.f, 1.f, false);
1939   EXPECT_TRUE(host_->needs_push_properties());
1940 
1941   PushProperties();
1942 
1943   animation_impl_->ActivateKeyframeModels();
1944 
1945   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
1946       keyframe_model_id));
1947   EXPECT_FALSE(host_->needs_push_properties());
1948 
1949   animation_->AbortKeyframeModelsWithProperty(TargetProperty::OPACITY, false);
1950   EXPECT_EQ(KeyframeModel::ABORTED,
1951             animation_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1952   EXPECT_TRUE(host_->needs_push_properties());
1953 
1954   animation_->Tick(kInitialTickTime);
1955   animation_->UpdateState(true, nullptr);
1956   EXPECT_EQ(KeyframeModel::ABORTED,
1957             animation_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1958 
1959   EXPECT_TRUE(animation_->keyframe_effect()->needs_push_properties());
1960   EXPECT_TRUE(host_->needs_push_properties());
1961 
1962   PushProperties();
1963   EXPECT_FALSE(animation_->keyframe_effect()->needs_push_properties());
1964   EXPECT_FALSE(host_->needs_push_properties());
1965 
1966   EXPECT_FALSE(
1967       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
1968   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
1969       keyframe_model_id));
1970 }
1971 
1972 // An animation aborted on the impl thread should get deleted on both threads.
TEST_F(ElementAnimationsTest,ImplThreadAbortedAnimationGetsDeleted)1973 TEST_F(ElementAnimationsTest, ImplThreadAbortedAnimationGetsDeleted) {
1974   CreateTestLayer(true, false);
1975   AttachTimelineAnimationLayer();
1976   CreateImplTimelineAndAnimation();
1977 
1978   TestAnimationDelegate delegate;
1979   animation_->set_animation_delegate(&delegate);
1980 
1981   int keyframe_model_id =
1982       AddOpacityTransitionToAnimation(animation_.get(), 1.0, 0.f, 1.f, false);
1983 
1984   PushProperties();
1985   EXPECT_FALSE(host_->needs_push_properties());
1986 
1987   animation_impl_->ActivateKeyframeModels();
1988   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
1989       keyframe_model_id));
1990 
1991   animation_impl_->AbortKeyframeModelsWithProperty(TargetProperty::OPACITY,
1992                                                    false);
1993   EXPECT_EQ(
1994       KeyframeModel::ABORTED,
1995       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
1996   EXPECT_TRUE(host_impl_->needs_push_properties());
1997   EXPECT_TRUE(animation_impl_->keyframe_effect()->needs_push_properties());
1998 
1999   auto events = CreateEventsForTesting();
2000   animation_impl_->Tick(kInitialTickTime);
2001   animation_impl_->UpdateState(true, events.get());
2002   EXPECT_TRUE(host_impl_->needs_push_properties());
2003   EXPECT_EQ(1u, events->events_.size());
2004   EXPECT_EQ(AnimationEvent::ABORTED, events->events_[0].type);
2005   EXPECT_EQ(
2006       KeyframeModel::WAITING_FOR_DELETION,
2007       animation_impl_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
2008 
2009   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2010   EXPECT_EQ(KeyframeModel::ABORTED,
2011             animation_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
2012   EXPECT_TRUE(delegate.aborted());
2013 
2014   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2015   animation_->UpdateState(true, nullptr);
2016   EXPECT_TRUE(host_->needs_push_properties());
2017   EXPECT_EQ(KeyframeModel::WAITING_FOR_DELETION,
2018             animation_->GetKeyframeModel(TargetProperty::OPACITY)->run_state());
2019 
2020   PushProperties();
2021 
2022   animation_impl_->ActivateKeyframeModels();
2023   EXPECT_FALSE(
2024       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
2025   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
2026       keyframe_model_id));
2027 }
2028 
2029 // Test that an impl-only scroll offset animation that needs to be completed on
2030 // the main thread gets deleted.
TEST_F(ElementAnimationsTest,ImplThreadTakeoverAnimationGetsDeleted)2031 TEST_F(ElementAnimationsTest, ImplThreadTakeoverAnimationGetsDeleted) {
2032   CreateTestLayer(true, false);
2033   AttachTimelineAnimationLayer();
2034   CreateImplTimelineAndAnimation();
2035 
2036   TestAnimationDelegate delegate_impl;
2037   animation_impl_->set_animation_delegate(&delegate_impl);
2038   TestAnimationDelegate delegate;
2039   animation_->set_animation_delegate(&delegate);
2040 
2041   // Add impl-only scroll offset animation.
2042   const int keyframe_model_id = 1;
2043   gfx::ScrollOffset initial_value(100.f, 300.f);
2044   gfx::ScrollOffset target_value(300.f, 200.f);
2045   std::unique_ptr<ScrollOffsetAnimationCurve> curve(
2046       ScrollOffsetAnimationCurveFactory::CreateEaseInOutAnimationForTesting(
2047           target_value));
2048   curve->SetInitialValue(initial_value);
2049   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
2050       std::move(curve), keyframe_model_id, 0, TargetProperty::SCROLL_OFFSET));
2051   keyframe_model->set_start_time(TicksFromSecondsF(123));
2052   keyframe_model->SetIsImplOnly();
2053   animation_impl_->AddKeyframeModel(std::move(keyframe_model));
2054 
2055   PushProperties();
2056   EXPECT_FALSE(host_->needs_push_properties());
2057 
2058   animation_impl_->ActivateKeyframeModels();
2059   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
2060       keyframe_model_id));
2061 
2062   animation_impl_->AbortKeyframeModelsWithProperty(
2063       TargetProperty::SCROLL_OFFSET, true);
2064   EXPECT_TRUE(host_impl_->needs_push_properties());
2065   EXPECT_EQ(KeyframeModel::ABORTED_BUT_NEEDS_COMPLETION,
2066             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET)
2067                 ->run_state());
2068 
2069   auto events = CreateEventsForTesting();
2070   animation_impl_->Tick(kInitialTickTime);
2071   animation_impl_->UpdateState(true, events.get());
2072   EXPECT_TRUE(delegate_impl.finished());
2073   EXPECT_TRUE(host_impl_->needs_push_properties());
2074   EXPECT_EQ(1u, events->events_.size());
2075   EXPECT_EQ(AnimationEvent::TAKEOVER, events->events_[0].type);
2076   EXPECT_EQ(TicksFromSecondsF(123), events->events_[0].animation_start_time);
2077   EXPECT_EQ(
2078       target_value,
2079       events->events_[0].curve->ToScrollOffsetAnimationCurve()->target_value());
2080   EXPECT_EQ(nullptr,
2081             animation_impl_->GetKeyframeModel(TargetProperty::SCROLL_OFFSET));
2082 
2083   // MT receives the event to take over.
2084   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2085   EXPECT_TRUE(delegate.takeover());
2086 
2087   // Animation::NotifyAnimationTakeover requests SetNeedsPushProperties to purge
2088   // CT animations marked for deletion.
2089   EXPECT_TRUE(animation_->keyframe_effect()->needs_push_properties());
2090 
2091   // ElementAnimations::PurgeAnimationsMarkedForDeletion call happens only in
2092   // ElementAnimations::PushPropertiesTo.
2093   PushProperties();
2094 
2095   animation_impl_->ActivateKeyframeModels();
2096   EXPECT_FALSE(
2097       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
2098   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
2099       keyframe_model_id));
2100 }
2101 
2102 // Ensure that we only generate FINISHED events for animations in a group
2103 // once all animations in that group are finished.
TEST_F(ElementAnimationsTest,FinishedEventsForGroup)2104 TEST_F(ElementAnimationsTest, FinishedEventsForGroup) {
2105   CreateTestLayer(true, false);
2106   AttachTimelineAnimationLayer();
2107   CreateImplTimelineAndAnimation();
2108 
2109   auto events = CreateEventsForTesting();
2110 
2111   const int group_id = 1;
2112 
2113   // Add two animations with the same group id but different durations.
2114   std::unique_ptr<KeyframeModel> first_keyframe_model(KeyframeModel::Create(
2115       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(2.0)), 1,
2116       group_id, TargetProperty::TRANSFORM));
2117   first_keyframe_model->set_is_controlling_instance_for_test(true);
2118   animation_impl_->AddKeyframeModel(std::move(first_keyframe_model));
2119 
2120   std::unique_ptr<KeyframeModel> second_keyframe_model(KeyframeModel::Create(
2121       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
2122       2, group_id, TargetProperty::OPACITY));
2123   second_keyframe_model->set_is_controlling_instance_for_test(true);
2124   animation_impl_->AddKeyframeModel(std::move(second_keyframe_model));
2125 
2126   animation_impl_->Tick(kInitialTickTime);
2127   animation_impl_->UpdateState(true, events.get());
2128 
2129   // Both animations should have started.
2130   EXPECT_EQ(2u, events->events_.size());
2131   EXPECT_EQ(AnimationEvent::STARTED, events->events_[0].type);
2132   EXPECT_EQ(AnimationEvent::STARTED, events->events_[1].type);
2133 
2134   events = CreateEventsForTesting();
2135   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2136   animation_impl_->UpdateState(true, events.get());
2137 
2138   // The opacity animation should be finished, but should not have generated
2139   // a FINISHED event yet.
2140   EXPECT_EQ(0u, events->events_.size());
2141   EXPECT_EQ(
2142       KeyframeModel::FINISHED,
2143       animation_impl_->keyframe_effect()->GetKeyframeModelById(2)->run_state());
2144   EXPECT_EQ(
2145       KeyframeModel::RUNNING,
2146       animation_impl_->keyframe_effect()->GetKeyframeModelById(1)->run_state());
2147 
2148   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2149   animation_impl_->UpdateState(true, events.get());
2150 
2151   // Both animations should have generated FINISHED events.
2152   EXPECT_EQ(2u, events->events_.size());
2153   EXPECT_EQ(AnimationEvent::FINISHED, events->events_[0].type);
2154   EXPECT_EQ(AnimationEvent::FINISHED, events->events_[1].type);
2155 }
2156 
2157 // Ensure that when a group has a mix of aborted and finished animations,
2158 // we generate a FINISHED event for the finished animation and an ABORTED
2159 // event for the aborted animation.
TEST_F(ElementAnimationsTest,FinishedAndAbortedEventsForGroup)2160 TEST_F(ElementAnimationsTest, FinishedAndAbortedEventsForGroup) {
2161   CreateTestLayer(true, false);
2162   AttachTimelineAnimationLayer();
2163   CreateImplTimelineAndAnimation();
2164 
2165   auto events = CreateEventsForTesting();
2166 
2167   // Add two animations with the same group id.
2168   std::unique_ptr<KeyframeModel> first_keyframe_model(CreateKeyframeModel(
2169       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1.0)), 1,
2170       TargetProperty::TRANSFORM));
2171   first_keyframe_model->set_is_controlling_instance_for_test(true);
2172   animation_impl_->AddKeyframeModel(std::move(first_keyframe_model));
2173 
2174   std::unique_ptr<KeyframeModel> second_keyframe_model(CreateKeyframeModel(
2175       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
2176       1, TargetProperty::OPACITY));
2177   second_keyframe_model->set_is_controlling_instance_for_test(true);
2178   animation_impl_->AddKeyframeModel(std::move(second_keyframe_model));
2179 
2180   animation_impl_->Tick(kInitialTickTime);
2181   animation_impl_->UpdateState(true, events.get());
2182 
2183   // Both animations should have started.
2184   EXPECT_EQ(2u, events->events_.size());
2185   EXPECT_EQ(AnimationEvent::STARTED, events->events_[0].type);
2186   EXPECT_EQ(AnimationEvent::STARTED, events->events_[1].type);
2187 
2188   animation_impl_->AbortKeyframeModelsWithProperty(TargetProperty::OPACITY,
2189                                                    false);
2190 
2191   events = CreateEventsForTesting();
2192   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2193   animation_impl_->UpdateState(true, events.get());
2194 
2195   // We should have exactly 2 events: a FINISHED event for the tranform
2196   // animation, and an ABORTED event for the opacity animation.
2197   EXPECT_EQ(2u, events->events_.size());
2198   EXPECT_EQ(AnimationEvent::FINISHED, events->events_[0].type);
2199   EXPECT_EQ(TargetProperty::TRANSFORM, events->events_[0].target_property);
2200   EXPECT_EQ(AnimationEvent::ABORTED, events->events_[1].type);
2201   EXPECT_EQ(TargetProperty::OPACITY, events->events_[1].target_property);
2202 }
2203 
TEST_F(ElementAnimationsTest,GetAnimationScalesNotScaled)2204 TEST_F(ElementAnimationsTest, GetAnimationScalesNotScaled) {
2205   CreateTestLayer(true, false);
2206   AttachTimelineAnimationLayer();
2207   CreateImplTimelineAndAnimation();
2208 
2209   float max_scale = 999;
2210   float start_scale = 999;
2211   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2212       ElementListType::PENDING, &max_scale, &start_scale));
2213   EXPECT_EQ(kNotScaled, max_scale);
2214   EXPECT_EQ(kNotScaled, start_scale);
2215   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2216       ElementListType::ACTIVE, &max_scale, &start_scale));
2217   EXPECT_EQ(kNotScaled, max_scale);
2218   EXPECT_EQ(kNotScaled, start_scale);
2219 
2220   animation_impl_->AddKeyframeModel(CreateKeyframeModel(
2221       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
2222       1, TargetProperty::OPACITY));
2223 
2224   // Opacity animations aren't non-translation transforms.
2225   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2226       ElementListType::PENDING, &max_scale, &start_scale));
2227   EXPECT_EQ(kNotScaled, max_scale);
2228   EXPECT_EQ(kNotScaled, start_scale);
2229   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2230       ElementListType::ACTIVE, &max_scale, &start_scale));
2231   EXPECT_EQ(kNotScaled, max_scale);
2232   EXPECT_EQ(kNotScaled, start_scale);
2233 
2234   std::unique_ptr<KeyframedTransformAnimationCurve> curve1(
2235       KeyframedTransformAnimationCurve::Create());
2236 
2237   TransformOperations operations1;
2238   curve1->AddKeyframe(
2239       TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2240   operations1.AppendTranslate(10.0, 15.0, 0.0);
2241   curve1->AddKeyframe(TransformKeyframe::Create(
2242       base::TimeDelta::FromSecondsD(1.0), operations1, nullptr));
2243 
2244   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
2245       std::move(curve1), 2, 2, TargetProperty::TRANSFORM));
2246   animation_impl_->AddKeyframeModel(std::move(keyframe_model));
2247 
2248   // The only transform animation we've added is a translation.
2249   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2250       ElementListType::PENDING, &max_scale, &start_scale));
2251   EXPECT_EQ(kNotScaled, max_scale);
2252   EXPECT_EQ(kNotScaled, start_scale);
2253   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2254       ElementListType::ACTIVE, &max_scale, &start_scale));
2255   EXPECT_EQ(kNotScaled, max_scale);
2256   EXPECT_EQ(kNotScaled, start_scale);
2257 }
2258 
TEST_F(ElementAnimationsTest,GetAnimationScales)2259 TEST_F(ElementAnimationsTest, GetAnimationScales) {
2260   CreateTestLayer(true, false);
2261   AttachTimelineAnimationLayer();
2262   CreateImplTimelineAndAnimation();
2263 
2264   std::unique_ptr<KeyframedTransformAnimationCurve> curve1(
2265       KeyframedTransformAnimationCurve::Create());
2266 
2267   TransformOperations operations1a;
2268   operations1a.AppendScale(2.0, 3.0, 4.0);
2269   curve1->AddKeyframe(
2270       TransformKeyframe::Create(base::TimeDelta(), operations1a, nullptr));
2271   TransformOperations operations1b;
2272   operations1b.AppendScale(5.0, 4.0, 3.0);
2273   curve1->AddKeyframe(TransformKeyframe::Create(
2274       base::TimeDelta::FromSecondsD(1.0), operations1b, nullptr));
2275   std::unique_ptr<KeyframeModel> keyframe_model(KeyframeModel::Create(
2276       std::move(curve1), 1, 1, TargetProperty::TRANSFORM));
2277   keyframe_model->set_affects_active_elements(false);
2278   animation_impl_->AddKeyframeModel(std::move(keyframe_model));
2279 
2280   float max_scale = kNotScaled;
2281   float start_scale = kNotScaled;
2282   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2283       ElementListType::PENDING, &max_scale, &start_scale));
2284   EXPECT_EQ(5.f, max_scale);
2285   EXPECT_EQ(4.f, start_scale);
2286   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2287       ElementListType::ACTIVE, &max_scale, &start_scale));
2288   EXPECT_EQ(kNotScaled, max_scale);
2289   EXPECT_EQ(kNotScaled, start_scale);
2290 
2291   animation_impl_->ActivateKeyframeModels();
2292   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2293       ElementListType::PENDING, &max_scale, &start_scale));
2294   EXPECT_EQ(5.f, max_scale);
2295   EXPECT_EQ(4.f, start_scale);
2296   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2297       ElementListType::ACTIVE, &max_scale, &start_scale));
2298   EXPECT_EQ(5.f, max_scale);
2299   EXPECT_EQ(4.f, start_scale);
2300 
2301   std::unique_ptr<KeyframedTransformAnimationCurve> curve2(
2302       KeyframedTransformAnimationCurve::Create());
2303 
2304   TransformOperations operations2a;
2305   operations2a.AppendScale(1.0, 2.0, 3.0);
2306   curve2->AddKeyframe(
2307       TransformKeyframe::Create(base::TimeDelta(), operations2a, nullptr));
2308   TransformOperations operations2b;
2309   operations2b.AppendScale(6.0, 5.0, 4.0);
2310   curve2->AddKeyframe(TransformKeyframe::Create(
2311       base::TimeDelta::FromSecondsD(1.0), operations2b, nullptr));
2312 
2313   animation_impl_->RemoveKeyframeModel(1);
2314   keyframe_model =
2315       KeyframeModel::Create(std::move(curve2), 2, 2, TargetProperty::TRANSFORM);
2316 
2317   // Reverse Direction
2318   keyframe_model->set_direction(KeyframeModel::Direction::REVERSE);
2319   keyframe_model->set_affects_active_elements(false);
2320   animation_impl_->AddKeyframeModel(std::move(keyframe_model));
2321 
2322   std::unique_ptr<KeyframedTransformAnimationCurve> curve3(
2323       KeyframedTransformAnimationCurve::Create());
2324 
2325   TransformOperations operations3a;
2326   operations3a.AppendScale(5.0, 3.0, 1.0);
2327   curve3->AddKeyframe(
2328       TransformKeyframe::Create(base::TimeDelta(), operations3a, nullptr));
2329   TransformOperations operations3b;
2330   operations3b.AppendScale(1.5, 2.5, 3.5);
2331   curve3->AddKeyframe(TransformKeyframe::Create(
2332       base::TimeDelta::FromSecondsD(1.0), operations3b, nullptr));
2333 
2334   keyframe_model =
2335       KeyframeModel::Create(std::move(curve3), 3, 3, TargetProperty::TRANSFORM);
2336   keyframe_model->set_affects_active_elements(false);
2337   animation_impl_->AddKeyframeModel(std::move(keyframe_model));
2338 
2339   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2340       ElementListType::PENDING, &max_scale, &start_scale));
2341   EXPECT_EQ(3.5f, max_scale);
2342   EXPECT_EQ(6.f, start_scale);
2343   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2344       ElementListType::ACTIVE, &max_scale, &start_scale));
2345   EXPECT_EQ(kNotScaled, max_scale);
2346   EXPECT_EQ(kNotScaled, start_scale);
2347 
2348   animation_impl_->ActivateKeyframeModels();
2349   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2350       ElementListType::PENDING, &max_scale, &start_scale));
2351   EXPECT_EQ(3.5f, max_scale);
2352   EXPECT_EQ(6.f, start_scale);
2353   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2354       ElementListType::ACTIVE, &max_scale, &start_scale));
2355   EXPECT_EQ(3.5f, max_scale);
2356   EXPECT_EQ(6.f, start_scale);
2357 
2358   animation_impl_->keyframe_effect()->GetKeyframeModelById(2)->SetRunState(
2359       KeyframeModel::FINISHED, TicksFromSecondsF(0.0));
2360 
2361   // Only unfinished animations should be considered by GetAnimationScales.
2362   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2363       ElementListType::PENDING, &max_scale, &start_scale));
2364   EXPECT_EQ(3.5f, max_scale);
2365   EXPECT_EQ(5.f, start_scale);
2366   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2367       ElementListType::ACTIVE, &max_scale, &start_scale));
2368   EXPECT_EQ(3.5f, max_scale);
2369   EXPECT_EQ(5.f, start_scale);
2370 }
2371 
TEST_F(ElementAnimationsTest,GetAnimationScalesWithDirection)2372 TEST_F(ElementAnimationsTest, GetAnimationScalesWithDirection) {
2373   CreateTestLayer(true, false);
2374   AttachTimelineAnimationLayer();
2375   CreateImplTimelineAndAnimation();
2376 
2377   std::unique_ptr<KeyframedTransformAnimationCurve> curve1(
2378       KeyframedTransformAnimationCurve::Create());
2379   TransformOperations operations1;
2380   operations1.AppendScale(1.0, 2.0, 3.0);
2381   curve1->AddKeyframe(
2382       TransformKeyframe::Create(base::TimeDelta(), operations1, nullptr));
2383   TransformOperations operations2;
2384   operations2.AppendScale(4.0, 5.0, 6.0);
2385   curve1->AddKeyframe(TransformKeyframe::Create(
2386       base::TimeDelta::FromSecondsD(1.0), operations2, nullptr));
2387 
2388   std::unique_ptr<KeyframeModel> keyframe_model_owned(KeyframeModel::Create(
2389       std::move(curve1), 1, 1, TargetProperty::TRANSFORM));
2390   KeyframeModel* keyframe_model = keyframe_model_owned.get();
2391   animation_impl_->AddKeyframeModel(std::move(keyframe_model_owned));
2392 
2393   float max_scale = 999;
2394   float start_scale = 999;
2395 
2396   EXPECT_GT(keyframe_model->playback_rate(), 0.0);
2397 
2398   // NORMAL direction with positive playback rate.
2399   keyframe_model->set_direction(KeyframeModel::Direction::NORMAL);
2400   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2401       ElementListType::PENDING, &max_scale, &start_scale));
2402   EXPECT_EQ(6.f, max_scale);
2403   EXPECT_EQ(3.f, start_scale);
2404   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2405       ElementListType::ACTIVE, &max_scale, &start_scale));
2406   EXPECT_EQ(6.f, max_scale);
2407   EXPECT_EQ(3.f, start_scale);
2408 
2409   // ALTERNATE direction with positive playback rate.
2410   keyframe_model->set_direction(KeyframeModel::Direction::ALTERNATE_NORMAL);
2411   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2412       ElementListType::PENDING, &max_scale, &start_scale));
2413   EXPECT_EQ(6.f, max_scale);
2414   EXPECT_EQ(3.f, start_scale);
2415   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2416       ElementListType::ACTIVE, &max_scale, &start_scale));
2417   EXPECT_EQ(6.f, max_scale);
2418   EXPECT_EQ(3.f, start_scale);
2419 
2420   // REVERSE direction with positive playback rate.
2421   keyframe_model->set_direction(KeyframeModel::Direction::REVERSE);
2422   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2423       ElementListType::PENDING, &max_scale, &start_scale));
2424   EXPECT_EQ(3.f, max_scale);
2425   EXPECT_EQ(6.f, start_scale);
2426   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2427       ElementListType::ACTIVE, &max_scale, &start_scale));
2428   EXPECT_EQ(3.f, max_scale);
2429   EXPECT_EQ(6.f, start_scale);
2430 
2431   // ALTERNATE reverse direction.
2432   keyframe_model->set_direction(KeyframeModel::Direction::REVERSE);
2433   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2434       ElementListType::PENDING, &max_scale, &start_scale));
2435   EXPECT_EQ(3.f, max_scale);
2436   EXPECT_EQ(6.f, start_scale);
2437   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2438       ElementListType::ACTIVE, &max_scale, &start_scale));
2439   EXPECT_EQ(3.f, max_scale);
2440   EXPECT_EQ(6.f, start_scale);
2441 
2442   keyframe_model->set_playback_rate(-1.0);
2443 
2444   // NORMAL direction with negative playback rate.
2445   keyframe_model->set_direction(KeyframeModel::Direction::NORMAL);
2446   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2447       ElementListType::PENDING, &max_scale, &start_scale));
2448   EXPECT_EQ(3.f, max_scale);
2449   EXPECT_EQ(6.f, start_scale);
2450   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2451       ElementListType::ACTIVE, &max_scale, &start_scale));
2452   EXPECT_EQ(3.f, max_scale);
2453   EXPECT_EQ(6.f, start_scale);
2454 
2455   // ALTERNATE direction with negative playback rate.
2456   keyframe_model->set_direction(KeyframeModel::Direction::ALTERNATE_NORMAL);
2457   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2458       ElementListType::PENDING, &max_scale, &start_scale));
2459   EXPECT_EQ(3.f, max_scale);
2460   EXPECT_EQ(6.f, start_scale);
2461   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2462       ElementListType::ACTIVE, &max_scale, &start_scale));
2463   EXPECT_EQ(3.f, max_scale);
2464   EXPECT_EQ(6.f, start_scale);
2465 
2466   // REVERSE direction with negative playback rate.
2467   keyframe_model->set_direction(KeyframeModel::Direction::REVERSE);
2468   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2469       ElementListType::PENDING, &max_scale, &start_scale));
2470   EXPECT_EQ(6.f, max_scale);
2471   EXPECT_EQ(3.f, start_scale);
2472   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2473       ElementListType::ACTIVE, &max_scale, &start_scale));
2474   EXPECT_EQ(6.f, max_scale);
2475   EXPECT_EQ(3.f, start_scale);
2476 
2477   // ALTERNATE reverse direction with negative playback rate.
2478   keyframe_model->set_direction(KeyframeModel::Direction::REVERSE);
2479   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2480       ElementListType::PENDING, &max_scale, &start_scale));
2481   EXPECT_EQ(6.f, max_scale);
2482   EXPECT_EQ(3.f, start_scale);
2483   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetAnimationScales(
2484       ElementListType::ACTIVE, &max_scale, &start_scale));
2485   EXPECT_EQ(6.f, max_scale);
2486   EXPECT_EQ(3.f, start_scale);
2487 }
2488 
TEST_F(ElementAnimationsTest,NewlyPushedAnimationWaitsForActivation)2489 TEST_F(ElementAnimationsTest, NewlyPushedAnimationWaitsForActivation) {
2490   CreateTestLayer(true, true);
2491   AttachTimelineAnimationLayer();
2492   CreateImplTimelineAndAnimation();
2493 
2494   auto events = CreateEventsForTesting();
2495 
2496   int keyframe_model_id =
2497       AddOpacityTransitionToAnimation(animation_.get(), 1, 0.5f, 1.f, false);
2498   EXPECT_TRUE(
2499       animation_->keyframe_effect()->GetKeyframeModelById(keyframe_model_id));
2500   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
2501       keyframe_model_id));
2502 
2503   PushProperties();
2504 
2505   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
2506       keyframe_model_id));
2507   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
2508             animation_impl_->keyframe_effect()
2509                 ->GetKeyframeModelById(keyframe_model_id)
2510                 ->run_state());
2511   EXPECT_TRUE(animation_impl_->keyframe_effect()
2512                   ->GetKeyframeModelById(keyframe_model_id)
2513                   ->affects_pending_elements());
2514   EXPECT_FALSE(animation_impl_->keyframe_effect()
2515                    ->GetKeyframeModelById(keyframe_model_id)
2516                    ->affects_active_elements());
2517 
2518   animation_impl_->Tick(kInitialTickTime);
2519   EXPECT_EQ(KeyframeModel::STARTING,
2520             animation_impl_->keyframe_effect()
2521                 ->GetKeyframeModelById(keyframe_model_id)
2522                 ->run_state());
2523   animation_impl_->UpdateState(true, events.get());
2524 
2525   // Since the animation hasn't been activated, it should still be STARTING
2526   // rather than RUNNING.
2527   EXPECT_EQ(KeyframeModel::STARTING,
2528             animation_impl_->keyframe_effect()
2529                 ->GetKeyframeModelById(keyframe_model_id)
2530                 ->run_state());
2531 
2532   // Since the animation hasn't been activated, only the pending observer
2533   // should have been ticked.
2534   EXPECT_EQ(0.5f,
2535             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
2536   EXPECT_EQ(0.f, client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
2537 
2538   animation_impl_->ActivateKeyframeModels();
2539   EXPECT_TRUE(animation_impl_->keyframe_effect()
2540                   ->GetKeyframeModelById(keyframe_model_id)
2541                   ->affects_pending_elements());
2542   EXPECT_TRUE(animation_impl_->keyframe_effect()
2543                   ->GetKeyframeModelById(keyframe_model_id)
2544                   ->affects_active_elements());
2545 
2546   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2547   animation_impl_->UpdateState(true, events.get());
2548 
2549   // Since the animation has been activated, it should have reached the
2550   // RUNNING state and the active observer should start to get ticked.
2551   EXPECT_EQ(KeyframeModel::RUNNING,
2552             animation_impl_->keyframe_effect()
2553                 ->GetKeyframeModelById(keyframe_model_id)
2554                 ->run_state());
2555   EXPECT_EQ(0.5f,
2556             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
2557   EXPECT_EQ(0.5f,
2558             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
2559 }
2560 
TEST_F(ElementAnimationsTest,ActivationBetweenAnimateAndUpdateState)2561 TEST_F(ElementAnimationsTest, ActivationBetweenAnimateAndUpdateState) {
2562   CreateTestLayer(true, true);
2563   AttachTimelineAnimationLayer();
2564   CreateImplTimelineAndAnimation();
2565 
2566   auto events = CreateEventsForTesting();
2567 
2568   const int keyframe_model_id =
2569       AddOpacityTransitionToAnimation(animation_.get(), 1, 0.5f, 1.f, true);
2570 
2571   PushProperties();
2572 
2573   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(
2574       keyframe_model_id));
2575   EXPECT_EQ(KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY,
2576             animation_impl_->keyframe_effect()
2577                 ->GetKeyframeModelById(keyframe_model_id)
2578                 ->run_state());
2579   EXPECT_TRUE(animation_impl_->keyframe_effect()
2580                   ->GetKeyframeModelById(keyframe_model_id)
2581                   ->affects_pending_elements());
2582   EXPECT_FALSE(animation_impl_->keyframe_effect()
2583                    ->GetKeyframeModelById(keyframe_model_id)
2584                    ->affects_active_elements());
2585 
2586   animation_impl_->Tick(kInitialTickTime);
2587 
2588   // Since the animation hasn't been activated, only the pending observer
2589   // should have been ticked.
2590   EXPECT_EQ(0.5f,
2591             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
2592   EXPECT_EQ(0.f, client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
2593 
2594   animation_impl_->ActivateKeyframeModels();
2595   EXPECT_TRUE(animation_impl_->keyframe_effect()
2596                   ->GetKeyframeModelById(keyframe_model_id)
2597                   ->affects_pending_elements());
2598   EXPECT_TRUE(animation_impl_->keyframe_effect()
2599                   ->GetKeyframeModelById(keyframe_model_id)
2600                   ->affects_active_elements());
2601 
2602   animation_impl_->UpdateState(true, events.get());
2603 
2604   // Since the animation has been activated, it should have reached the
2605   // RUNNING state.
2606   EXPECT_EQ(KeyframeModel::RUNNING,
2607             animation_impl_->keyframe_effect()
2608                 ->GetKeyframeModelById(keyframe_model_id)
2609                 ->run_state());
2610 
2611   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
2612 
2613   // Both elements should have been ticked.
2614   EXPECT_EQ(0.75f,
2615             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
2616   EXPECT_EQ(0.75f,
2617             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
2618 }
2619 
TEST_F(ElementAnimationsTest,ObserverNotifiedWhenTransformAnimationChanges)2620 TEST_F(ElementAnimationsTest, ObserverNotifiedWhenTransformAnimationChanges) {
2621   CreateTestLayer(true, true);
2622   AttachTimelineAnimationLayer();
2623   CreateImplTimelineAndAnimation();
2624 
2625   auto events = CreateEventsForTesting();
2626 
2627   EXPECT_FALSE(client_.GetHasPotentialTransformAnimation(
2628       element_id_, ElementListType::ACTIVE));
2629   EXPECT_FALSE(client_.GetTransformIsCurrentlyAnimating(
2630       element_id_, ElementListType::ACTIVE));
2631   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2632       element_id_, ElementListType::PENDING));
2633   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2634       element_id_, ElementListType::PENDING));
2635   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2636       element_id_, ElementListType::ACTIVE));
2637   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2638       element_id_, ElementListType::ACTIVE));
2639 
2640   // Case 1: An animation that's allowed to run until its finish point.
2641   AddAnimatedTransformToAnimation(animation_.get(), 1.0, 1, 1);
2642   EXPECT_TRUE(client_.GetHasPotentialTransformAnimation(
2643       element_id_, ElementListType::ACTIVE));
2644   EXPECT_TRUE(client_.GetTransformIsCurrentlyAnimating(
2645       element_id_, ElementListType::ACTIVE));
2646 
2647   PushProperties();
2648   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2649       element_id_, ElementListType::PENDING));
2650   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2651       element_id_, ElementListType::PENDING));
2652   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2653       element_id_, ElementListType::ACTIVE));
2654   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2655       element_id_, ElementListType::ACTIVE));
2656 
2657   animation_impl_->ActivateKeyframeModels();
2658   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2659       element_id_, ElementListType::PENDING));
2660   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2661       element_id_, ElementListType::PENDING));
2662   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2663       element_id_, ElementListType::ACTIVE));
2664   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2665       element_id_, ElementListType::ACTIVE));
2666 
2667   animation_impl_->Tick(kInitialTickTime);
2668   animation_impl_->UpdateState(true, events.get());
2669 
2670   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2671   events->events_.clear();
2672 
2673   // Finish the animation.
2674   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2675   animation_->UpdateState(true, nullptr);
2676   EXPECT_FALSE(client_.GetHasPotentialTransformAnimation(
2677       element_id_, ElementListType::ACTIVE));
2678   EXPECT_FALSE(client_.GetTransformIsCurrentlyAnimating(
2679       element_id_, ElementListType::ACTIVE));
2680 
2681   PushProperties();
2682 
2683   // Finished animations are pushed, but animations_impl hasn't yet ticked
2684   // at/past the end of the animation.
2685   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2686       element_id_, ElementListType::PENDING));
2687   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2688       element_id_, ElementListType::PENDING));
2689   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2690       element_id_, ElementListType::ACTIVE));
2691   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2692       element_id_, ElementListType::ACTIVE));
2693 
2694   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2695   animation_impl_->UpdateState(true, events.get());
2696   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2697       element_id_, ElementListType::PENDING));
2698   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2699       element_id_, ElementListType::PENDING));
2700   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2701       element_id_, ElementListType::ACTIVE));
2702   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2703       element_id_, ElementListType::ACTIVE));
2704 
2705   // Case 2: An animation that's removed before it finishes.
2706   int keyframe_model_id =
2707       AddAnimatedTransformToAnimation(animation_.get(), 10.0, 2, 2);
2708   int animation2_id =
2709       AddAnimatedTransformToAnimation(animation_.get(), 10.0, 2, 1);
2710   animation_->keyframe_effect()
2711       ->GetKeyframeModelById(animation2_id)
2712       ->set_time_offset(base::TimeDelta::FromMilliseconds(-10000));
2713   animation_->keyframe_effect()
2714       ->GetKeyframeModelById(animation2_id)
2715       ->set_fill_mode(KeyframeModel::FillMode::NONE);
2716   EXPECT_TRUE(client_.GetHasPotentialTransformAnimation(
2717       element_id_, ElementListType::ACTIVE));
2718   EXPECT_TRUE(client_.GetTransformIsCurrentlyAnimating(
2719       element_id_, ElementListType::ACTIVE));
2720 
2721   PushProperties();
2722   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2723       element_id_, ElementListType::PENDING));
2724   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2725       element_id_, ElementListType::PENDING));
2726   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2727       element_id_, ElementListType::ACTIVE));
2728   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2729       element_id_, ElementListType::ACTIVE));
2730 
2731   animation_impl_->ActivateKeyframeModels();
2732   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2733       element_id_, ElementListType::ACTIVE));
2734   // animation1 is in effect currently and animation2 isn't. As the element has
2735   // atleast one animation that's in effect currently, client should be notified
2736   // that the transform is currently animating.
2737   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2738       element_id_, ElementListType::ACTIVE));
2739 
2740   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2741   animation_impl_->UpdateState(true, events.get());
2742 
2743   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2744   events->events_.clear();
2745 
2746   animation_->RemoveKeyframeModel(keyframe_model_id);
2747   animation_->RemoveKeyframeModel(animation2_id);
2748   EXPECT_FALSE(client_.GetHasPotentialTransformAnimation(
2749       element_id_, ElementListType::ACTIVE));
2750   EXPECT_FALSE(client_.GetTransformIsCurrentlyAnimating(
2751       element_id_, ElementListType::ACTIVE));
2752 
2753   PushProperties();
2754   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2755       element_id_, ElementListType::PENDING));
2756   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2757       element_id_, ElementListType::PENDING));
2758   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2759       element_id_, ElementListType::ACTIVE));
2760   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2761       element_id_, ElementListType::ACTIVE));
2762 
2763   animation_impl_->ActivateKeyframeModels();
2764   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2765       element_id_, ElementListType::ACTIVE));
2766   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2767       element_id_, ElementListType::ACTIVE));
2768 
2769   // Case 3: An animation that's aborted before it finishes.
2770   keyframe_model_id =
2771       AddAnimatedTransformToAnimation(animation_.get(), 10.0, 3, 3);
2772   EXPECT_TRUE(client_.GetHasPotentialTransformAnimation(
2773       element_id_, ElementListType::ACTIVE));
2774   EXPECT_TRUE(client_.GetTransformIsCurrentlyAnimating(
2775       element_id_, ElementListType::ACTIVE));
2776 
2777   PushProperties();
2778   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2779       element_id_, ElementListType::PENDING));
2780   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2781       element_id_, ElementListType::PENDING));
2782   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2783       element_id_, ElementListType::ACTIVE));
2784   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2785       element_id_, ElementListType::ACTIVE));
2786 
2787   animation_impl_->ActivateKeyframeModels();
2788   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2789       element_id_, ElementListType::ACTIVE));
2790   EXPECT_TRUE(client_impl_.GetTransformIsCurrentlyAnimating(
2791       element_id_, ElementListType::ACTIVE));
2792 
2793   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2794   animation_impl_->UpdateState(true, events.get());
2795 
2796   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2797   events->events_.clear();
2798 
2799   animation_impl_->AbortKeyframeModelsWithProperty(TargetProperty::TRANSFORM,
2800                                                    false);
2801   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2802       element_id_, ElementListType::PENDING));
2803   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2804       element_id_, ElementListType::PENDING));
2805   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2806       element_id_, ElementListType::ACTIVE));
2807   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2808       element_id_, ElementListType::ACTIVE));
2809 
2810   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
2811   animation_impl_->UpdateState(true, events.get());
2812 
2813   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2814   EXPECT_FALSE(client_.GetHasPotentialTransformAnimation(
2815       element_id_, ElementListType::ACTIVE));
2816   EXPECT_FALSE(client_.GetTransformIsCurrentlyAnimating(
2817       element_id_, ElementListType::ACTIVE));
2818 
2819   // Case 4 : An animation that's not in effect.
2820   keyframe_model_id =
2821       AddAnimatedTransformToAnimation(animation_.get(), 1.0, 1, 6);
2822   animation_->keyframe_effect()
2823       ->GetKeyframeModelById(keyframe_model_id)
2824       ->set_time_offset(base::TimeDelta::FromMilliseconds(-10000));
2825   animation_->keyframe_effect()
2826       ->GetKeyframeModelById(keyframe_model_id)
2827       ->set_fill_mode(KeyframeModel::FillMode::NONE);
2828 
2829   PushProperties();
2830   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2831       element_id_, ElementListType::PENDING));
2832   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2833       element_id_, ElementListType::PENDING));
2834   EXPECT_FALSE(client_impl_.GetHasPotentialTransformAnimation(
2835       element_id_, ElementListType::ACTIVE));
2836   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2837       element_id_, ElementListType::ACTIVE));
2838 
2839   animation_impl_->ActivateKeyframeModels();
2840   EXPECT_TRUE(client_impl_.GetHasPotentialTransformAnimation(
2841       element_id_, ElementListType::ACTIVE));
2842   EXPECT_FALSE(client_impl_.GetTransformIsCurrentlyAnimating(
2843       element_id_, ElementListType::ACTIVE));
2844 }
2845 
TEST_F(ElementAnimationsTest,ObserverNotifiedWhenOpacityAnimationChanges)2846 TEST_F(ElementAnimationsTest, ObserverNotifiedWhenOpacityAnimationChanges) {
2847   CreateTestLayer(true, true);
2848   AttachTimelineAnimationLayer();
2849   CreateImplTimelineAndAnimation();
2850 
2851   auto events = CreateEventsForTesting();
2852 
2853   EXPECT_FALSE(client_.GetHasPotentialOpacityAnimation(
2854       element_id_, ElementListType::ACTIVE));
2855   EXPECT_FALSE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
2856                                                       ElementListType::ACTIVE));
2857   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2858       element_id_, ElementListType::PENDING));
2859   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2860       element_id_, ElementListType::PENDING));
2861   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2862       element_id_, ElementListType::ACTIVE));
2863   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2864       element_id_, ElementListType::ACTIVE));
2865 
2866   // Case 1: An animation that's allowed to run until its finish point.
2867   AddOpacityTransitionToAnimation(animation_.get(), 1.0, 0.f, 1.f,
2868                                   false /*use_timing_function*/);
2869   EXPECT_TRUE(client_.GetHasPotentialOpacityAnimation(element_id_,
2870                                                       ElementListType::ACTIVE));
2871   EXPECT_TRUE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
2872                                                      ElementListType::ACTIVE));
2873 
2874   PushProperties();
2875   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2876       element_id_, ElementListType::PENDING));
2877   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2878       element_id_, ElementListType::PENDING));
2879   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2880       element_id_, ElementListType::ACTIVE));
2881   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2882       element_id_, ElementListType::ACTIVE));
2883 
2884   animation_impl_->ActivateKeyframeModels();
2885   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2886       element_id_, ElementListType::PENDING));
2887   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2888       element_id_, ElementListType::PENDING));
2889   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2890       element_id_, ElementListType::ACTIVE));
2891   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2892       element_id_, ElementListType::ACTIVE));
2893 
2894   animation_impl_->Tick(kInitialTickTime);
2895   animation_impl_->UpdateState(true, events.get());
2896 
2897   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2898   events->events_.clear();
2899 
2900   // Finish the animation.
2901   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2902   animation_->UpdateState(true, nullptr);
2903   EXPECT_FALSE(client_.GetHasPotentialOpacityAnimation(
2904       element_id_, ElementListType::ACTIVE));
2905   EXPECT_FALSE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
2906                                                       ElementListType::ACTIVE));
2907 
2908   PushProperties();
2909 
2910   // Finished animations are pushed, but animations_impl hasn't yet ticked
2911   // at/past the end of the animation.
2912   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2913       element_id_, ElementListType::PENDING));
2914   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2915       element_id_, ElementListType::PENDING));
2916   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2917       element_id_, ElementListType::ACTIVE));
2918   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2919       element_id_, ElementListType::ACTIVE));
2920 
2921   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
2922   animation_impl_->UpdateState(true, events.get());
2923   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2924       element_id_, ElementListType::PENDING));
2925   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2926       element_id_, ElementListType::PENDING));
2927   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2928       element_id_, ElementListType::ACTIVE));
2929   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2930       element_id_, ElementListType::ACTIVE));
2931 
2932   // Case 2: An animation that's removed before it finishes.
2933   int keyframe_model_id = AddOpacityTransitionToAnimation(
2934       animation_.get(), 10.0, 0.f, 1.f, false /*use_timing_function*/);
2935   EXPECT_TRUE(client_.GetHasPotentialOpacityAnimation(element_id_,
2936                                                       ElementListType::ACTIVE));
2937   EXPECT_TRUE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
2938                                                      ElementListType::ACTIVE));
2939 
2940   PushProperties();
2941   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2942       element_id_, ElementListType::PENDING));
2943   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2944       element_id_, ElementListType::PENDING));
2945   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2946       element_id_, ElementListType::ACTIVE));
2947   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2948       element_id_, ElementListType::ACTIVE));
2949 
2950   animation_impl_->ActivateKeyframeModels();
2951   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2952       element_id_, ElementListType::ACTIVE));
2953   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2954       element_id_, ElementListType::ACTIVE));
2955 
2956   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
2957   animation_impl_->UpdateState(true, events.get());
2958 
2959   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
2960   events->events_.clear();
2961 
2962   animation_->RemoveKeyframeModel(keyframe_model_id);
2963   EXPECT_FALSE(client_.GetHasPotentialOpacityAnimation(
2964       element_id_, ElementListType::ACTIVE));
2965   EXPECT_FALSE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
2966                                                       ElementListType::ACTIVE));
2967 
2968   PushProperties();
2969   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2970       element_id_, ElementListType::PENDING));
2971   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2972       element_id_, ElementListType::PENDING));
2973   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2974       element_id_, ElementListType::ACTIVE));
2975   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2976       element_id_, ElementListType::ACTIVE));
2977 
2978   animation_impl_->ActivateKeyframeModels();
2979   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2980       element_id_, ElementListType::ACTIVE));
2981   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
2982       element_id_, ElementListType::ACTIVE));
2983 
2984   // Case 3: An animation that's aborted before it finishes.
2985   keyframe_model_id = AddOpacityTransitionToAnimation(
2986       animation_.get(), 10.0, 0.f, 0.5f, false /*use_timing_function*/);
2987   EXPECT_TRUE(client_.GetHasPotentialOpacityAnimation(element_id_,
2988                                                       ElementListType::ACTIVE));
2989   EXPECT_TRUE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
2990                                                      ElementListType::ACTIVE));
2991 
2992   PushProperties();
2993   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
2994       element_id_, ElementListType::PENDING));
2995   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
2996       element_id_, ElementListType::PENDING));
2997   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
2998       element_id_, ElementListType::ACTIVE));
2999   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
3000       element_id_, ElementListType::ACTIVE));
3001 
3002   animation_impl_->ActivateKeyframeModels();
3003   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
3004       element_id_, ElementListType::ACTIVE));
3005   EXPECT_TRUE(client_impl_.GetOpacityIsCurrentlyAnimating(
3006       element_id_, ElementListType::ACTIVE));
3007 
3008   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
3009   animation_impl_->UpdateState(true, events.get());
3010 
3011   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3012   events->events_.clear();
3013 
3014   animation_impl_->AbortKeyframeModelsWithProperty(TargetProperty::OPACITY,
3015                                                    false);
3016   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
3017       element_id_, ElementListType::PENDING));
3018   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
3019       element_id_, ElementListType::PENDING));
3020   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
3021       element_id_, ElementListType::ACTIVE));
3022   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
3023       element_id_, ElementListType::ACTIVE));
3024 
3025   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
3026   animation_impl_->UpdateState(true, events.get());
3027 
3028   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3029   EXPECT_FALSE(client_.GetHasPotentialOpacityAnimation(
3030       element_id_, ElementListType::ACTIVE));
3031   EXPECT_FALSE(client_.GetOpacityIsCurrentlyAnimating(element_id_,
3032                                                       ElementListType::ACTIVE));
3033 
3034   // Case 4 : An animation that's not in effect.
3035   keyframe_model_id = AddOpacityTransitionToAnimation(
3036       animation_.get(), 1.0, 0.f, 0.5f, false /*use_timing_function*/);
3037   animation_->keyframe_effect()
3038       ->GetKeyframeModelById(keyframe_model_id)
3039       ->set_time_offset(base::TimeDelta::FromMilliseconds(-10000));
3040   animation_->keyframe_effect()
3041       ->GetKeyframeModelById(keyframe_model_id)
3042       ->set_fill_mode(KeyframeModel::FillMode::NONE);
3043 
3044   PushProperties();
3045   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
3046       element_id_, ElementListType::PENDING));
3047   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
3048       element_id_, ElementListType::PENDING));
3049   EXPECT_FALSE(client_impl_.GetHasPotentialOpacityAnimation(
3050       element_id_, ElementListType::ACTIVE));
3051   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
3052       element_id_, ElementListType::ACTIVE));
3053 
3054   animation_impl_->ActivateKeyframeModels();
3055   EXPECT_TRUE(client_impl_.GetHasPotentialOpacityAnimation(
3056       element_id_, ElementListType::ACTIVE));
3057   EXPECT_FALSE(client_impl_.GetOpacityIsCurrentlyAnimating(
3058       element_id_, ElementListType::ACTIVE));
3059 }
3060 
TEST_F(ElementAnimationsTest,ObserverNotifiedWhenFilterAnimationChanges)3061 TEST_F(ElementAnimationsTest, ObserverNotifiedWhenFilterAnimationChanges) {
3062   CreateTestLayer(true, true);
3063   AttachTimelineAnimationLayer();
3064   CreateImplTimelineAndAnimation();
3065 
3066   auto events = CreateEventsForTesting();
3067 
3068   EXPECT_FALSE(client_.GetHasPotentialFilterAnimation(element_id_,
3069                                                       ElementListType::ACTIVE));
3070   EXPECT_FALSE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3071                                                      ElementListType::ACTIVE));
3072   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3073       element_id_, ElementListType::PENDING));
3074   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3075       element_id_, ElementListType::PENDING));
3076   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3077       element_id_, ElementListType::ACTIVE));
3078   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3079       element_id_, ElementListType::ACTIVE));
3080 
3081   // Case 1: An animation that's allowed to run until its finish point.
3082   AddAnimatedFilterToAnimation(animation_.get(), 1.0, 0.f, 1.f);
3083   EXPECT_TRUE(client_.GetHasPotentialFilterAnimation(element_id_,
3084                                                      ElementListType::ACTIVE));
3085   EXPECT_TRUE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3086                                                     ElementListType::ACTIVE));
3087 
3088   PushProperties();
3089   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3090       element_id_, ElementListType::PENDING));
3091   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3092       element_id_, ElementListType::PENDING));
3093   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3094       element_id_, ElementListType::ACTIVE));
3095   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3096       element_id_, ElementListType::ACTIVE));
3097 
3098   animation_impl_->ActivateKeyframeModels();
3099   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3100       element_id_, ElementListType::PENDING));
3101   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3102       element_id_, ElementListType::PENDING));
3103   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3104       element_id_, ElementListType::ACTIVE));
3105   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3106       element_id_, ElementListType::ACTIVE));
3107 
3108   animation_impl_->Tick(kInitialTickTime);
3109   animation_impl_->UpdateState(true, events.get());
3110 
3111   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3112   events->events_.clear();
3113 
3114   // Finish the animation.
3115   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3116   animation_->UpdateState(true, nullptr);
3117   EXPECT_FALSE(client_.GetHasPotentialFilterAnimation(element_id_,
3118                                                       ElementListType::ACTIVE));
3119   EXPECT_FALSE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3120                                                      ElementListType::ACTIVE));
3121 
3122   PushProperties();
3123 
3124   // Finished animations are pushed, but animations_impl hasn't yet ticked
3125   // at/past the end of the animation.
3126   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3127       element_id_, ElementListType::PENDING));
3128   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3129       element_id_, ElementListType::PENDING));
3130   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3131       element_id_, ElementListType::ACTIVE));
3132   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3133       element_id_, ElementListType::ACTIVE));
3134 
3135   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3136   animation_impl_->UpdateState(true, events.get());
3137   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3138       element_id_, ElementListType::PENDING));
3139   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3140       element_id_, ElementListType::PENDING));
3141   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3142       element_id_, ElementListType::ACTIVE));
3143   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3144       element_id_, ElementListType::ACTIVE));
3145 
3146   // Case 2: An animation that's removed before it finishes.
3147   int keyframe_model_id =
3148       AddAnimatedFilterToAnimation(animation_.get(), 10.0, 0.f, 1.f);
3149   EXPECT_TRUE(client_.GetHasPotentialFilterAnimation(element_id_,
3150                                                      ElementListType::ACTIVE));
3151   EXPECT_TRUE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3152                                                     ElementListType::ACTIVE));
3153 
3154   PushProperties();
3155   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3156       element_id_, ElementListType::PENDING));
3157   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3158       element_id_, ElementListType::PENDING));
3159   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3160       element_id_, ElementListType::ACTIVE));
3161   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3162       element_id_, ElementListType::ACTIVE));
3163 
3164   animation_impl_->ActivateKeyframeModels();
3165   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3166       element_id_, ElementListType::ACTIVE));
3167   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3168       element_id_, ElementListType::ACTIVE));
3169 
3170   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
3171   animation_impl_->UpdateState(true, events.get());
3172 
3173   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3174   events->events_.clear();
3175 
3176   animation_->RemoveKeyframeModel(keyframe_model_id);
3177   EXPECT_FALSE(client_.GetHasPotentialFilterAnimation(element_id_,
3178                                                       ElementListType::ACTIVE));
3179   EXPECT_FALSE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3180                                                      ElementListType::ACTIVE));
3181 
3182   PushProperties();
3183   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3184       element_id_, ElementListType::PENDING));
3185   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3186       element_id_, ElementListType::PENDING));
3187   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3188       element_id_, ElementListType::ACTIVE));
3189   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3190       element_id_, ElementListType::ACTIVE));
3191 
3192   animation_impl_->ActivateKeyframeModels();
3193   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3194       element_id_, ElementListType::ACTIVE));
3195   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3196       element_id_, ElementListType::ACTIVE));
3197 
3198   // Case 3: An animation that's aborted before it finishes.
3199   keyframe_model_id =
3200       AddAnimatedFilterToAnimation(animation_.get(), 10.0, 0.f, 0.5f);
3201   EXPECT_TRUE(client_.GetHasPotentialFilterAnimation(element_id_,
3202                                                      ElementListType::ACTIVE));
3203   EXPECT_TRUE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3204                                                     ElementListType::ACTIVE));
3205 
3206   PushProperties();
3207   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3208       element_id_, ElementListType::PENDING));
3209   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3210       element_id_, ElementListType::PENDING));
3211   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3212       element_id_, ElementListType::ACTIVE));
3213   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3214       element_id_, ElementListType::ACTIVE));
3215 
3216   animation_impl_->ActivateKeyframeModels();
3217   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3218       element_id_, ElementListType::ACTIVE));
3219   EXPECT_TRUE(client_impl_.GetFilterIsCurrentlyAnimating(
3220       element_id_, ElementListType::ACTIVE));
3221 
3222   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
3223   animation_impl_->UpdateState(true, events.get());
3224 
3225   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3226   events->events_.clear();
3227 
3228   animation_impl_->AbortKeyframeModelsWithProperty(TargetProperty::FILTER,
3229                                                    false);
3230   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3231       element_id_, ElementListType::PENDING));
3232   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3233       element_id_, ElementListType::PENDING));
3234   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3235       element_id_, ElementListType::ACTIVE));
3236   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3237       element_id_, ElementListType::ACTIVE));
3238 
3239   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
3240   animation_impl_->UpdateState(true, events.get());
3241 
3242   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3243   EXPECT_FALSE(client_.GetHasPotentialFilterAnimation(element_id_,
3244                                                       ElementListType::ACTIVE));
3245   EXPECT_FALSE(client_.GetFilterIsCurrentlyAnimating(element_id_,
3246                                                      ElementListType::ACTIVE));
3247 
3248   // Case 4 : An animation that's not in effect.
3249   keyframe_model_id =
3250       AddAnimatedFilterToAnimation(animation_.get(), 1.0, 0.f, 0.5f);
3251   animation_->keyframe_effect()
3252       ->GetKeyframeModelById(keyframe_model_id)
3253       ->set_time_offset(base::TimeDelta::FromMilliseconds(-10000));
3254   animation_->keyframe_effect()
3255       ->GetKeyframeModelById(keyframe_model_id)
3256       ->set_fill_mode(KeyframeModel::FillMode::NONE);
3257 
3258   PushProperties();
3259   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3260       element_id_, ElementListType::PENDING));
3261   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3262       element_id_, ElementListType::PENDING));
3263   EXPECT_FALSE(client_impl_.GetHasPotentialFilterAnimation(
3264       element_id_, ElementListType::ACTIVE));
3265   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3266       element_id_, ElementListType::ACTIVE));
3267 
3268   animation_impl_->ActivateKeyframeModels();
3269   EXPECT_TRUE(client_impl_.GetHasPotentialFilterAnimation(
3270       element_id_, ElementListType::ACTIVE));
3271   EXPECT_FALSE(client_impl_.GetFilterIsCurrentlyAnimating(
3272       element_id_, ElementListType::ACTIVE));
3273 }
3274 
TEST_F(ElementAnimationsTest,ObserverNotifiedWhenBackdropFilterAnimationChanges)3275 TEST_F(ElementAnimationsTest,
3276        ObserverNotifiedWhenBackdropFilterAnimationChanges) {
3277   CreateTestLayer(true, true);
3278   AttachTimelineAnimationLayer();
3279   CreateImplTimelineAndAnimation();
3280 
3281   auto events = CreateEventsForTesting();
3282 
3283   EXPECT_FALSE(client_.GetHasPotentialBackdropFilterAnimation(
3284       element_id_, ElementListType::ACTIVE));
3285   EXPECT_FALSE(client_.GetBackdropFilterIsCurrentlyAnimating(
3286       element_id_, ElementListType::ACTIVE));
3287   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3288       element_id_, ElementListType::PENDING));
3289   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3290       element_id_, ElementListType::PENDING));
3291   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3292       element_id_, ElementListType::ACTIVE));
3293   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3294       element_id_, ElementListType::ACTIVE));
3295 
3296   // Case 1: An animation that's allowed to run until its finish point.
3297   AddAnimatedBackdropFilterToAnimation(animation_.get(), 1.0, 0.f, 1.f);
3298   EXPECT_TRUE(client_.GetHasPotentialBackdropFilterAnimation(
3299       element_id_, ElementListType::ACTIVE));
3300   EXPECT_TRUE(client_.GetBackdropFilterIsCurrentlyAnimating(
3301       element_id_, ElementListType::ACTIVE));
3302 
3303   PushProperties();
3304   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3305       element_id_, ElementListType::PENDING));
3306   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3307       element_id_, ElementListType::PENDING));
3308   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3309       element_id_, ElementListType::ACTIVE));
3310   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3311       element_id_, ElementListType::ACTIVE));
3312 
3313   animation_impl_->ActivateKeyframeModels();
3314   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3315       element_id_, ElementListType::PENDING));
3316   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3317       element_id_, ElementListType::PENDING));
3318   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3319       element_id_, ElementListType::ACTIVE));
3320   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3321       element_id_, ElementListType::ACTIVE));
3322 
3323   animation_impl_->Tick(kInitialTickTime);
3324   animation_impl_->UpdateState(true, events.get());
3325 
3326   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3327   events->events_.clear();
3328 
3329   // Finish the animation.
3330   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3331   animation_->UpdateState(true, nullptr);
3332   EXPECT_FALSE(client_.GetHasPotentialBackdropFilterAnimation(
3333       element_id_, ElementListType::ACTIVE));
3334   EXPECT_FALSE(client_.GetBackdropFilterIsCurrentlyAnimating(
3335       element_id_, ElementListType::ACTIVE));
3336 
3337   PushProperties();
3338 
3339   // Finished animations are pushed, but animations_impl hasn't yet ticked
3340   // at/past the end of the animation.
3341   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3342       element_id_, ElementListType::PENDING));
3343   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3344       element_id_, ElementListType::PENDING));
3345   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3346       element_id_, ElementListType::ACTIVE));
3347   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3348       element_id_, ElementListType::ACTIVE));
3349 
3350   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3351   animation_impl_->UpdateState(true, events.get());
3352   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3353       element_id_, ElementListType::PENDING));
3354   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3355       element_id_, ElementListType::PENDING));
3356   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3357       element_id_, ElementListType::ACTIVE));
3358   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3359       element_id_, ElementListType::ACTIVE));
3360 
3361   // Case 2: An animation that's removed before it finishes.
3362   int keyframe_model_id =
3363       AddAnimatedBackdropFilterToAnimation(animation_.get(), 10.0, 0.f, 1.f);
3364   EXPECT_TRUE(client_.GetHasPotentialBackdropFilterAnimation(
3365       element_id_, ElementListType::ACTIVE));
3366   EXPECT_TRUE(client_.GetBackdropFilterIsCurrentlyAnimating(
3367       element_id_, ElementListType::ACTIVE));
3368 
3369   PushProperties();
3370   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3371       element_id_, ElementListType::PENDING));
3372   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3373       element_id_, ElementListType::PENDING));
3374   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3375       element_id_, ElementListType::ACTIVE));
3376   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3377       element_id_, ElementListType::ACTIVE));
3378 
3379   animation_impl_->ActivateKeyframeModels();
3380   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3381       element_id_, ElementListType::ACTIVE));
3382   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3383       element_id_, ElementListType::ACTIVE));
3384 
3385   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
3386   animation_impl_->UpdateState(true, events.get());
3387 
3388   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3389   events->events_.clear();
3390 
3391   animation_->RemoveKeyframeModel(keyframe_model_id);
3392   EXPECT_FALSE(client_.GetHasPotentialBackdropFilterAnimation(
3393       element_id_, ElementListType::ACTIVE));
3394   EXPECT_FALSE(client_.GetBackdropFilterIsCurrentlyAnimating(
3395       element_id_, ElementListType::ACTIVE));
3396 
3397   PushProperties();
3398   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3399       element_id_, ElementListType::PENDING));
3400   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3401       element_id_, ElementListType::PENDING));
3402   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3403       element_id_, ElementListType::ACTIVE));
3404   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3405       element_id_, ElementListType::ACTIVE));
3406 
3407   animation_impl_->ActivateKeyframeModels();
3408   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3409       element_id_, ElementListType::ACTIVE));
3410   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3411       element_id_, ElementListType::ACTIVE));
3412 
3413   // Case 3: An animation that's aborted before it finishes.
3414   keyframe_model_id =
3415       AddAnimatedBackdropFilterToAnimation(animation_.get(), 10.0, 0.f, 0.5f);
3416   EXPECT_TRUE(client_.GetHasPotentialBackdropFilterAnimation(
3417       element_id_, ElementListType::ACTIVE));
3418   EXPECT_TRUE(client_.GetBackdropFilterIsCurrentlyAnimating(
3419       element_id_, ElementListType::ACTIVE));
3420 
3421   PushProperties();
3422   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3423       element_id_, ElementListType::PENDING));
3424   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3425       element_id_, ElementListType::PENDING));
3426   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3427       element_id_, ElementListType::ACTIVE));
3428   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3429       element_id_, ElementListType::ACTIVE));
3430 
3431   animation_impl_->ActivateKeyframeModels();
3432   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3433       element_id_, ElementListType::ACTIVE));
3434   EXPECT_TRUE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3435       element_id_, ElementListType::ACTIVE));
3436 
3437   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
3438   animation_impl_->UpdateState(true, events.get());
3439 
3440   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3441   events->events_.clear();
3442 
3443   animation_impl_->AbortKeyframeModelsWithProperty(
3444       TargetProperty::BACKDROP_FILTER, false);
3445   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3446       element_id_, ElementListType::PENDING));
3447   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3448       element_id_, ElementListType::PENDING));
3449   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3450       element_id_, ElementListType::ACTIVE));
3451   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3452       element_id_, ElementListType::ACTIVE));
3453 
3454   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
3455   animation_impl_->UpdateState(true, events.get());
3456 
3457   animation_->DispatchAndDelegateAnimationEvent(events->events_[0]);
3458   EXPECT_FALSE(client_.GetHasPotentialBackdropFilterAnimation(
3459       element_id_, ElementListType::ACTIVE));
3460   EXPECT_FALSE(client_.GetBackdropFilterIsCurrentlyAnimating(
3461       element_id_, ElementListType::ACTIVE));
3462 
3463   // Case 4 : An animation that's not in effect.
3464   keyframe_model_id =
3465       AddAnimatedBackdropFilterToAnimation(animation_.get(), 1.0, 0.f, 0.5f);
3466   animation_->keyframe_effect()
3467       ->GetKeyframeModelById(keyframe_model_id)
3468       ->set_time_offset(base::TimeDelta::FromMilliseconds(-10000));
3469   animation_->keyframe_effect()
3470       ->GetKeyframeModelById(keyframe_model_id)
3471       ->set_fill_mode(KeyframeModel::FillMode::NONE);
3472 
3473   PushProperties();
3474   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3475       element_id_, ElementListType::PENDING));
3476   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3477       element_id_, ElementListType::PENDING));
3478   EXPECT_FALSE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3479       element_id_, ElementListType::ACTIVE));
3480   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3481       element_id_, ElementListType::ACTIVE));
3482 
3483   animation_impl_->ActivateKeyframeModels();
3484   EXPECT_TRUE(client_impl_.GetHasPotentialBackdropFilterAnimation(
3485       element_id_, ElementListType::ACTIVE));
3486   EXPECT_FALSE(client_impl_.GetBackdropFilterIsCurrentlyAnimating(
3487       element_id_, ElementListType::ACTIVE));
3488 }
3489 
TEST_F(ElementAnimationsTest,ClippedOpacityValues)3490 TEST_F(ElementAnimationsTest, ClippedOpacityValues) {
3491   CreateTestLayer(false, false);
3492   AttachTimelineAnimationLayer();
3493 
3494   AddOpacityTransitionToAnimation(animation_.get(), 1, 1.f, 2.f, true);
3495 
3496   animation_->Tick(kInitialTickTime);
3497   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
3498 
3499   // Opacity values are clipped [0,1]
3500   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3501   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
3502 }
3503 
TEST_F(ElementAnimationsTest,ClippedNegativeOpacityValues)3504 TEST_F(ElementAnimationsTest, ClippedNegativeOpacityValues) {
3505   CreateTestLayer(false, false);
3506   AttachTimelineAnimationLayer();
3507 
3508   AddOpacityTransitionToAnimation(animation_.get(), 1, 0.f, -2.f, true);
3509 
3510   animation_->Tick(kInitialTickTime);
3511   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
3512 
3513   // Opacity values are clipped [0,1]
3514   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3515   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
3516 }
3517 
TEST_F(ElementAnimationsTest,PushedDeletedAnimationWaitsForActivation)3518 TEST_F(ElementAnimationsTest, PushedDeletedAnimationWaitsForActivation) {
3519   CreateTestLayer(true, true);
3520   AttachTimelineAnimationLayer();
3521   CreateImplTimelineAndAnimation();
3522 
3523   auto events = CreateEventsForTesting();
3524 
3525   const int keyframe_model_id =
3526       AddOpacityTransitionToAnimation(animation_.get(), 1, 0.5f, 1.f, true);
3527   PushProperties();
3528   animation_impl_->ActivateKeyframeModels();
3529   animation_impl_->Tick(kInitialTickTime);
3530   animation_impl_->UpdateState(true, events.get());
3531   EXPECT_EQ(KeyframeModel::RUNNING,
3532             animation_impl_->keyframe_effect()
3533                 ->GetKeyframeModelById(keyframe_model_id)
3534                 ->run_state());
3535   EXPECT_EQ(0.5f,
3536             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
3537   EXPECT_EQ(0.5f,
3538             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
3539 
3540   EXPECT_TRUE(animation_impl_->keyframe_effect()
3541                   ->GetKeyframeModelById(keyframe_model_id)
3542                   ->affects_pending_elements());
3543   EXPECT_TRUE(animation_impl_->keyframe_effect()
3544                   ->GetKeyframeModelById(keyframe_model_id)
3545                   ->affects_active_elements());
3546 
3547   // Delete the animation on the main-thread animations.
3548   animation_->RemoveKeyframeModel(
3549       animation_->GetKeyframeModel(TargetProperty::OPACITY)->id());
3550   PushProperties();
3551 
3552   // The animation should no longer affect pending elements.
3553   EXPECT_FALSE(animation_impl_->keyframe_effect()
3554                    ->GetKeyframeModelById(keyframe_model_id)
3555                    ->affects_pending_elements());
3556   EXPECT_TRUE(animation_impl_->keyframe_effect()
3557                   ->GetKeyframeModelById(keyframe_model_id)
3558                   ->affects_active_elements());
3559 
3560   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
3561   animation_impl_->UpdateState(true, events.get());
3562 
3563   // Only the active observer should have been ticked.
3564   EXPECT_EQ(0.5f,
3565             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
3566   EXPECT_EQ(0.75f,
3567             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
3568 
3569   animation_impl_->ActivateKeyframeModels();
3570   events = CreateEventsForTesting();
3571   animation_impl_->UpdateState(true, events.get());
3572 
3573   // After Activation the animation doesn't affect neither active nor pending
3574   // thread. UpdateState for this animation would put the animation to wait for
3575   // deletion state.
3576   EXPECT_EQ(KeyframeModel::WAITING_FOR_DELETION,
3577             animation_impl_->keyframe_effect()
3578                 ->GetKeyframeModelById(keyframe_model_id)
3579                 ->run_state());
3580   EXPECT_EQ(1u, events->events_.size());
3581 
3582   // The animation is finished on impl thread, and main thread will delete it
3583   // during commit.
3584   animation_->animation_host()->SetAnimationEvents(std::move(events));
3585   PushProperties();
3586   EXPECT_FALSE(animation_impl_->keyframe_effect()->has_any_keyframe_model());
3587 }
3588 
3589 // Tests that an animation that affects only active elements won't block
3590 // an animation that affects only pending elements from starting.
TEST_F(ElementAnimationsTest,StartAnimationsAffectingDifferentObservers)3591 TEST_F(ElementAnimationsTest, StartAnimationsAffectingDifferentObservers) {
3592   CreateTestLayer(true, true);
3593   AttachTimelineAnimationLayer();
3594   CreateImplTimelineAndAnimation();
3595 
3596   auto events = CreateEventsForTesting();
3597 
3598   const int first_keyframe_model_id =
3599       AddOpacityTransitionToAnimation(animation_.get(), 1, 0.f, 1.f, true);
3600 
3601   PushProperties();
3602   animation_impl_->ActivateKeyframeModels();
3603   animation_impl_->Tick(kInitialTickTime);
3604   animation_impl_->UpdateState(true, events.get());
3605 
3606   // Remove the first animation from the main-thread animations, and add a
3607   // new animation affecting the same property.
3608   animation_->RemoveKeyframeModel(
3609       animation_->GetKeyframeModel(TargetProperty::OPACITY)->id());
3610   const int second_keyframe_model_id =
3611       AddOpacityTransitionToAnimation(animation_.get(), 1, 1.f, 0.5f, true);
3612   PushProperties();
3613 
3614   // The original animation should only affect active elements, and the new
3615   // animation should only affect pending elements.
3616   EXPECT_FALSE(animation_impl_->keyframe_effect()
3617                    ->GetKeyframeModelById(first_keyframe_model_id)
3618                    ->affects_pending_elements());
3619   EXPECT_TRUE(animation_impl_->keyframe_effect()
3620                   ->GetKeyframeModelById(first_keyframe_model_id)
3621                   ->affects_active_elements());
3622   EXPECT_TRUE(animation_impl_->keyframe_effect()
3623                   ->GetKeyframeModelById(second_keyframe_model_id)
3624                   ->affects_pending_elements());
3625   EXPECT_FALSE(animation_impl_->keyframe_effect()
3626                    ->GetKeyframeModelById(second_keyframe_model_id)
3627                    ->affects_active_elements());
3628 
3629   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(500));
3630   animation_impl_->UpdateState(true, events.get());
3631 
3632   // The original animation should still be running, and the new animation
3633   // should be starting.
3634   EXPECT_EQ(KeyframeModel::RUNNING,
3635             animation_impl_->keyframe_effect()
3636                 ->GetKeyframeModelById(first_keyframe_model_id)
3637                 ->run_state());
3638   EXPECT_EQ(KeyframeModel::STARTING,
3639             animation_impl_->keyframe_effect()
3640                 ->GetKeyframeModelById(second_keyframe_model_id)
3641                 ->run_state());
3642 
3643   // The active observer should have been ticked by the original animation,
3644   // and the pending observer should have been ticked by the new animation.
3645   EXPECT_EQ(1.f,
3646             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
3647   EXPECT_EQ(0.5f,
3648             client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
3649 
3650   animation_impl_->ActivateKeyframeModels();
3651 
3652   // The original animation no longer affect either elements, and the new
3653   // animation should now affect both elements.
3654   EXPECT_FALSE(animation_impl_->keyframe_effect()
3655                    ->GetKeyframeModelById(first_keyframe_model_id)
3656                    ->affects_pending_elements());
3657   EXPECT_FALSE(animation_impl_->keyframe_effect()
3658                    ->GetKeyframeModelById(first_keyframe_model_id)
3659                    ->affects_active_elements());
3660   EXPECT_TRUE(animation_impl_->keyframe_effect()
3661                   ->GetKeyframeModelById(second_keyframe_model_id)
3662                   ->affects_pending_elements());
3663   EXPECT_TRUE(animation_impl_->keyframe_effect()
3664                   ->GetKeyframeModelById(second_keyframe_model_id)
3665                   ->affects_active_elements());
3666 
3667   animation_impl_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3668   animation_impl_->UpdateState(true, events.get());
3669 
3670   // The original animation should be marked for waiting for deletion.
3671   EXPECT_EQ(KeyframeModel::WAITING_FOR_DELETION,
3672             animation_impl_->keyframe_effect()
3673                 ->GetKeyframeModelById(first_keyframe_model_id)
3674                 ->run_state());
3675 
3676   // The new animation should be running, and the active observer should have
3677   // been ticked at the new animation's starting point.
3678   EXPECT_EQ(KeyframeModel::RUNNING,
3679             animation_impl_->keyframe_effect()
3680                 ->GetKeyframeModelById(second_keyframe_model_id)
3681                 ->run_state());
3682   EXPECT_EQ(1.f,
3683             client_impl_.GetOpacity(element_id_, ElementListType::PENDING));
3684   EXPECT_EQ(1.f, client_impl_.GetOpacity(element_id_, ElementListType::ACTIVE));
3685 }
3686 
TEST_F(ElementAnimationsTest,TestIsCurrentlyAnimatingProperty)3687 TEST_F(ElementAnimationsTest, TestIsCurrentlyAnimatingProperty) {
3688   CreateTestLayer(false, false);
3689   AttachTimelineAnimationLayer();
3690 
3691   // Create an animation that initially affects only pending elements.
3692   std::unique_ptr<KeyframeModel> keyframe_model(CreateKeyframeModel(
3693       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
3694       1, TargetProperty::OPACITY));
3695   keyframe_model->set_affects_active_elements(false);
3696 
3697   animation_->AddKeyframeModel(std::move(keyframe_model));
3698   animation_->Tick(kInitialTickTime);
3699   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3700       TargetProperty::OPACITY, ElementListType::PENDING));
3701   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3702       TargetProperty::OPACITY, ElementListType::ACTIVE));
3703   animation_->UpdateState(true, nullptr);
3704   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
3705 
3706   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3707       TargetProperty::OPACITY, ElementListType::PENDING));
3708   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3709       TargetProperty::OPACITY, ElementListType::ACTIVE));
3710   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3711       TargetProperty::FILTER, ElementListType::PENDING));
3712   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3713       TargetProperty::FILTER, ElementListType::ACTIVE));
3714 
3715   animation_->ActivateKeyframeModels();
3716 
3717   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3718       TargetProperty::OPACITY, ElementListType::PENDING));
3719   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3720       TargetProperty::OPACITY, ElementListType::ACTIVE));
3721   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3722       TargetProperty::FILTER, ElementListType::PENDING));
3723   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3724       TargetProperty::FILTER, ElementListType::ACTIVE));
3725 
3726   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(10));
3727   animation_->UpdateState(true, nullptr);
3728 
3729   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3730       TargetProperty::OPACITY, ElementListType::PENDING));
3731   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3732       TargetProperty::OPACITY, ElementListType::ACTIVE));
3733   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3734       TargetProperty::FILTER, ElementListType::PENDING));
3735   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3736       TargetProperty::FILTER, ElementListType::ACTIVE));
3737 
3738   EXPECT_EQ(0.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
3739 
3740   // Tick past the end of the animation.
3741   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1100));
3742   animation_->UpdateState(true, nullptr);
3743 
3744   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3745       TargetProperty::OPACITY, ElementListType::PENDING));
3746   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3747       TargetProperty::OPACITY, ElementListType::ACTIVE));
3748   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3749       TargetProperty::FILTER, ElementListType::PENDING));
3750   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3751       TargetProperty::FILTER, ElementListType::ACTIVE));
3752 
3753   EXPECT_EQ(1.f, client_.GetOpacity(element_id_, ElementListType::ACTIVE));
3754 }
3755 
TEST_F(ElementAnimationsTest,TestIsAnimatingPropertyTimeOffsetFillMode)3756 TEST_F(ElementAnimationsTest, TestIsAnimatingPropertyTimeOffsetFillMode) {
3757   CreateTestLayer(false, false);
3758   AttachTimelineAnimationLayer();
3759 
3760   // Create an animation that initially affects only pending elements, and has
3761   // a start delay of 2 seconds.
3762   std::unique_ptr<KeyframeModel> keyframe_model(CreateKeyframeModel(
3763       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 0.f, 1.f)),
3764       1, TargetProperty::OPACITY));
3765   keyframe_model->set_fill_mode(KeyframeModel::FillMode::NONE);
3766   keyframe_model->set_time_offset(TimeDelta::FromMilliseconds(-2000));
3767   keyframe_model->set_affects_active_elements(false);
3768 
3769   animation_->AddKeyframeModel(std::move(keyframe_model));
3770 
3771   animation_->Tick(kInitialTickTime);
3772 
3773   // Since the animation has a start delay, the elements it affects have a
3774   // potentially running transform animation but aren't currently animating
3775   // transform.
3776   EXPECT_TRUE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3777       TargetProperty::OPACITY, ElementListType::PENDING));
3778   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3779       TargetProperty::OPACITY, ElementListType::ACTIVE));
3780   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3781       TargetProperty::OPACITY, ElementListType::PENDING));
3782   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3783       TargetProperty::OPACITY, ElementListType::ACTIVE));
3784   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
3785   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3786       TargetProperty::FILTER, ElementListType::PENDING));
3787   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3788       TargetProperty::FILTER, ElementListType::ACTIVE));
3789 
3790   animation_->ActivateKeyframeModels();
3791 
3792   EXPECT_TRUE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3793       TargetProperty::OPACITY, ElementListType::PENDING));
3794   EXPECT_TRUE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3795       TargetProperty::OPACITY, ElementListType::ACTIVE));
3796   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3797       TargetProperty::OPACITY, ElementListType::PENDING));
3798   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3799       TargetProperty::OPACITY, ElementListType::ACTIVE));
3800   EXPECT_TRUE(animation_->keyframe_effect()->HasTickingKeyframeModel());
3801   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3802       TargetProperty::FILTER, ElementListType::PENDING));
3803   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3804       TargetProperty::FILTER, ElementListType::ACTIVE));
3805 
3806   animation_->UpdateState(true, nullptr);
3807 
3808   // Tick past the start delay.
3809   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(2000));
3810   animation_->UpdateState(true, nullptr);
3811   EXPECT_TRUE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3812       TargetProperty::OPACITY, ElementListType::PENDING));
3813   EXPECT_TRUE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3814       TargetProperty::OPACITY, ElementListType::ACTIVE));
3815   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3816       TargetProperty::OPACITY, ElementListType::PENDING));
3817   EXPECT_TRUE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3818       TargetProperty::OPACITY, ElementListType::ACTIVE));
3819 
3820   // After the animaton finishes, the elements it affects have neither a
3821   // potentially running transform animation nor a currently running transform
3822   // animation.
3823   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(4000));
3824   animation_->UpdateState(true, nullptr);
3825   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3826       TargetProperty::OPACITY, ElementListType::PENDING));
3827   EXPECT_FALSE(animation_->keyframe_effect()->IsPotentiallyAnimatingProperty(
3828       TargetProperty::OPACITY, ElementListType::ACTIVE));
3829   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3830       TargetProperty::OPACITY, ElementListType::PENDING));
3831   EXPECT_FALSE(animation_->keyframe_effect()->IsCurrentlyAnimatingProperty(
3832       TargetProperty::OPACITY, ElementListType::ACTIVE));
3833 }
3834 
TEST_F(ElementAnimationsTest,DestroyTestMainLayerBeforePushProperties)3835 TEST_F(ElementAnimationsTest, DestroyTestMainLayerBeforePushProperties) {
3836   CreateTestLayer(false, false);
3837   AttachTimelineAnimationLayer();
3838   EXPECT_EQ(0u, host_->ticking_animations_for_testing().size());
3839 
3840   animation_->AddKeyframeModel(CreateKeyframeModel(
3841       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f)),
3842       2, TargetProperty::OPACITY));
3843   EXPECT_EQ(1u, host_->ticking_animations_for_testing().size());
3844 
3845   DestroyTestMainLayer();
3846   host_->UpdateAnimationState(true, nullptr);
3847   EXPECT_EQ(0u, host_->ticking_animations_for_testing().size());
3848 
3849   PushProperties();
3850   host_impl_->ActivateAnimations(nullptr);
3851   EXPECT_EQ(0u, host_->ticking_animations_for_testing().size());
3852   EXPECT_EQ(0u, host_impl_->ticking_animations_for_testing().size());
3853 }
3854 
TEST_F(ElementAnimationsTest,RemoveAndReAddAnimationToTicking)3855 TEST_F(ElementAnimationsTest, RemoveAndReAddAnimationToTicking) {
3856   CreateTestLayer(false, false);
3857   AttachTimelineAnimationLayer();
3858   EXPECT_EQ(0u, host_->ticking_animations_for_testing().size());
3859 
3860   // Add an animation and ensure the animation is in the host's ticking
3861   // animations. Remove the animation using RemoveFromTicking().
3862   animation_->AddKeyframeModel(CreateKeyframeModel(
3863       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f)),
3864       1, TargetProperty::OPACITY));
3865   ASSERT_EQ(1u, host_->ticking_animations_for_testing().size());
3866   animation_->keyframe_effect()->RemoveFromTicking();
3867   ASSERT_EQ(0u, host_->ticking_animations_for_testing().size());
3868 
3869   // Ensure that adding a new animation will correctly update the ticking
3870   // animations list.
3871   animation_->AddKeyframeModel(CreateKeyframeModel(
3872       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(1.0, 1.f, 0.5f)),
3873       2, TargetProperty::OPACITY));
3874   EXPECT_EQ(1u, host_->ticking_animations_for_testing().size());
3875 }
3876 
3877 // This test verifies that finished keyframe models don't get copied over to
3878 // impl thread.
TEST_F(ElementAnimationsTest,FinishedKeyframeModelsNotCopiedToImpl)3879 TEST_F(ElementAnimationsTest, FinishedKeyframeModelsNotCopiedToImpl) {
3880   CreateTestLayer(false, false);
3881   AttachTimelineAnimationLayer();
3882   CreateImplTimelineAndAnimation();
3883 
3884   animation_->AddKeyframeModel(KeyframeModel::Create(
3885       std::unique_ptr<AnimationCurve>(new FakeTransformTransition(1.0)), 1, 1,
3886       TargetProperty::TRANSFORM));
3887   animation_->AddKeyframeModel(KeyframeModel::Create(
3888       std::unique_ptr<AnimationCurve>(new FakeFloatTransition(2.0, 0.f, 1.f)),
3889       2, 2, TargetProperty::OPACITY));
3890 
3891   // Finish the first keyframe model.
3892   animation_->Tick(kInitialTickTime);
3893   animation_->UpdateState(true, nullptr);
3894   animation_->Tick(kInitialTickTime + TimeDelta::FromMilliseconds(1000));
3895   animation_->UpdateState(true, nullptr);
3896 
3897   EXPECT_EQ(
3898       KeyframeModel::FINISHED,
3899       animation_->keyframe_effect()->GetKeyframeModelById(1)->run_state());
3900   EXPECT_EQ(
3901       KeyframeModel::RUNNING,
3902       animation_->keyframe_effect()->GetKeyframeModelById(2)->run_state());
3903 
3904   PushProperties();
3905 
3906   // Finished keyframe model doesn't get copied to impl thread.
3907   EXPECT_FALSE(animation_impl_->keyframe_effect()->GetKeyframeModelById(1));
3908   EXPECT_TRUE(animation_impl_->keyframe_effect()->GetKeyframeModelById(2));
3909 }
3910 
3911 }  // namespace
3912 }  // namespace cc
3913