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.
5 #include "cc/animation/animation_player.h"
7 #include "cc/animation/animation_delegate.h"
8 #include "cc/animation/animation_host.h"
9 #include "cc/animation/animation_id_provider.h"
10 #include "cc/animation/animation_registrar.h"
11 #include "cc/animation/animation_timeline.h"
12 #include "cc/animation/element_animations.h"
13 #include "cc/test/animation_test_common.h"
14 #include "cc/test/animation_timelines_test_common.h"
19 class AnimationPlayerTest
: public AnimationTimelinesTest
{
21 AnimationPlayerTest() {}
22 ~AnimationPlayerTest() override
{}
25 // See element_animations_unittest.cc for active/pending observers tests.
27 TEST_F(AnimationPlayerTest
, AttachDetachLayerIfTimelineAttached
) {
28 host_
->AddAnimationTimeline(timeline_
);
29 timeline_
->AttachPlayer(player_
);
30 EXPECT_FALSE(player_
->element_animations());
31 EXPECT_FALSE(player_
->layer_id());
33 host_
->PushPropertiesTo(host_impl_
);
35 EXPECT_FALSE(GetImplPlayerForLayerId(layer_id_
));
37 GetImplTimelineAndPlayerByID();
39 EXPECT_FALSE(player_impl_
->element_animations());
40 EXPECT_FALSE(player_impl_
->layer_id());
42 player_
->AttachLayer(layer_id_
);
43 EXPECT_EQ(player_
, GetPlayerForLayerId(layer_id_
));
44 EXPECT_TRUE(player_
->element_animations());
45 EXPECT_EQ(player_
->layer_id(), layer_id_
);
47 host_
->PushPropertiesTo(host_impl_
);
49 EXPECT_EQ(player_impl_
, GetImplPlayerForLayerId(layer_id_
));
50 EXPECT_TRUE(player_impl_
->element_animations());
51 EXPECT_EQ(player_impl_
->layer_id(), layer_id_
);
53 player_
->DetachLayer();
54 EXPECT_FALSE(GetPlayerForLayerId(layer_id_
));
55 EXPECT_FALSE(player_
->element_animations());
56 EXPECT_FALSE(player_
->layer_id());
58 host_
->PushPropertiesTo(host_impl_
);
60 EXPECT_FALSE(GetImplPlayerForLayerId(layer_id_
));
61 EXPECT_FALSE(player_impl_
->element_animations());
62 EXPECT_FALSE(player_impl_
->layer_id());
64 timeline_
->DetachPlayer(player_
);
65 EXPECT_FALSE(player_
->animation_timeline());
66 EXPECT_FALSE(player_
->element_animations());
67 EXPECT_FALSE(player_
->layer_id());
70 TEST_F(AnimationPlayerTest
, AttachDetachTimelineIfLayerAttached
) {
71 host_
->AddAnimationTimeline(timeline_
);
73 EXPECT_FALSE(player_
->element_animations());
74 EXPECT_FALSE(player_
->layer_id());
76 player_
->AttachLayer(layer_id_
);
77 EXPECT_FALSE(player_
->animation_timeline());
78 EXPECT_FALSE(GetPlayerForLayerId(layer_id_
));
79 EXPECT_FALSE(player_
->element_animations());
80 EXPECT_EQ(player_
->layer_id(), layer_id_
);
82 timeline_
->AttachPlayer(player_
);
83 EXPECT_EQ(timeline_
, player_
->animation_timeline());
84 EXPECT_EQ(player_
, GetPlayerForLayerId(layer_id_
));
85 EXPECT_TRUE(player_
->element_animations());
86 EXPECT_EQ(player_
->layer_id(), layer_id_
);
88 // Removing player from timeline detaches layer.
89 timeline_
->DetachPlayer(player_
);
90 EXPECT_FALSE(player_
->animation_timeline());
91 EXPECT_FALSE(GetPlayerForLayerId(layer_id_
));
92 EXPECT_FALSE(player_
->element_animations());
93 EXPECT_FALSE(player_
->layer_id());
96 TEST_F(AnimationPlayerTest
, PropertiesMutate
) {
97 client_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
98 client_impl_
.RegisterLayer(layer_id_
, LayerTreeType::PENDING
);
99 client_impl_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
101 host_
->AddAnimationTimeline(timeline_
);
102 timeline_
->AttachPlayer(player_
);
103 player_
->AttachLayer(layer_id_
);
105 const float start_opacity
= .7f
;
106 const float end_opacity
= .3f
;
108 const float start_brightness
= .6f
;
109 const float end_brightness
= .4f
;
111 const int transform_x
= 10;
112 const int transform_y
= 20;
114 const double duration
= 1.;
116 AddOpacityTransitionToPlayer(player_
.get(), duration
, start_opacity
,
118 AddAnimatedTransformToPlayer(player_
.get(), duration
, transform_x
,
120 AddAnimatedFilterToPlayer(player_
.get(), duration
, start_brightness
,
123 host_
->PushPropertiesTo(host_impl_
);
125 EXPECT_FALSE(client_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
126 Animation::OPACITY
));
127 EXPECT_FALSE(client_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
128 Animation::TRANSFORM
));
129 EXPECT_FALSE(client_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
132 EXPECT_FALSE(client_impl_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
133 Animation::OPACITY
));
134 EXPECT_FALSE(client_impl_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
135 Animation::TRANSFORM
));
136 EXPECT_FALSE(client_impl_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
139 host_impl_
->animation_registrar()->ActivateAnimations();
141 base::TimeTicks time
;
142 time
+= base::TimeDelta::FromSecondsD(0.1);
143 AnimateLayersTransferEvents(time
, 3u);
145 time
+= base::TimeDelta::FromSecondsD(duration
);
146 AnimateLayersTransferEvents(time
, 3u);
148 client_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
150 client_
.ExpectTransformPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
151 transform_x
, transform_y
);
152 client_
.ExpectFilterPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
155 client_impl_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
157 client_impl_
.ExpectTransformPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
158 transform_x
, transform_y
);
159 client_impl_
.ExpectFilterPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
162 client_impl_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::PENDING
,
164 client_impl_
.ExpectTransformPropertyMutated(layer_id_
, LayerTreeType::PENDING
,
165 transform_x
, transform_y
);
166 client_impl_
.ExpectFilterPropertyMutated(layer_id_
, LayerTreeType::PENDING
,
170 TEST_F(AnimationPlayerTest
, AttachTwoPlayersToOneLayer
) {
171 TestAnimationDelegate delegate1
;
172 TestAnimationDelegate delegate2
;
174 client_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
175 client_impl_
.RegisterLayer(layer_id_
, LayerTreeType::PENDING
);
176 client_impl_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
178 scoped_refptr
<AnimationPlayer
> player1
=
179 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId());
180 scoped_refptr
<AnimationPlayer
> player2
=
181 AnimationPlayer::Create(AnimationIdProvider::NextPlayerId());
183 host_
->AddAnimationTimeline(timeline_
);
184 timeline_
->AttachPlayer(player1
);
185 timeline_
->AttachPlayer(player2
);
187 player1
->set_layer_animation_delegate(&delegate1
);
188 player2
->set_layer_animation_delegate(&delegate2
);
190 // Attach players to the same layer.
191 player1
->AttachLayer(layer_id_
);
192 player2
->AttachLayer(layer_id_
);
194 const float start_opacity
= .7f
;
195 const float end_opacity
= .3f
;
197 const int transform_x
= 10;
198 const int transform_y
= 20;
200 const double duration
= 1.;
202 AddOpacityTransitionToPlayer(player1
.get(), duration
, start_opacity
,
204 AddAnimatedTransformToPlayer(player2
.get(), duration
, transform_x
,
207 host_
->PushPropertiesTo(host_impl_
);
208 host_impl_
->animation_registrar()->ActivateAnimations();
210 EXPECT_FALSE(delegate1
.started_
);
211 EXPECT_FALSE(delegate1
.finished_
);
213 EXPECT_FALSE(delegate2
.started_
);
214 EXPECT_FALSE(delegate2
.finished_
);
216 base::TimeTicks time
;
217 time
+= base::TimeDelta::FromSecondsD(0.1);
218 AnimateLayersTransferEvents(time
, 2u);
220 EXPECT_TRUE(delegate1
.started_
);
221 EXPECT_FALSE(delegate1
.finished_
);
223 EXPECT_TRUE(delegate2
.started_
);
224 EXPECT_FALSE(delegate2
.finished_
);
226 time
+= base::TimeDelta::FromSecondsD(duration
);
227 AnimateLayersTransferEvents(time
, 2u);
229 EXPECT_TRUE(delegate1
.finished_
);
230 EXPECT_TRUE(delegate2
.finished_
);
232 client_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
234 client_
.ExpectTransformPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
235 transform_x
, transform_y
);
237 client_impl_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
239 client_impl_
.ExpectTransformPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
240 transform_x
, transform_y
);
242 client_impl_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::PENDING
,
244 client_impl_
.ExpectTransformPropertyMutated(layer_id_
, LayerTreeType::PENDING
,
245 transform_x
, transform_y
);
248 TEST_F(AnimationPlayerTest
, AddRemoveAnimationToNonAttachedPlayer
) {
249 client_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
250 client_impl_
.RegisterLayer(layer_id_
, LayerTreeType::PENDING
);
251 client_impl_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
253 const double duration
= 1.;
254 const float start_opacity
= .7f
;
255 const float end_opacity
= .3f
;
257 const int filter_id
=
258 AddAnimatedFilterToPlayer(player_
.get(), duration
, 0.1f
, 0.9f
);
259 const int opacity_id
= AddOpacityTransitionToPlayer(
260 player_
.get(), duration
, start_opacity
, end_opacity
, false);
262 host_
->AddAnimationTimeline(timeline_
);
263 timeline_
->AttachPlayer(player_
);
265 EXPECT_FALSE(player_
->element_animations());
266 player_
->RemoveAnimation(filter_id
);
268 player_
->AttachLayer(layer_id_
);
270 EXPECT_TRUE(player_
->element_animations());
271 EXPECT_FALSE(player_
->element_animations()
272 ->layer_animation_controller()
273 ->GetAnimationById(filter_id
));
274 EXPECT_TRUE(player_
->element_animations()
275 ->layer_animation_controller()
276 ->GetAnimationById(opacity_id
));
278 host_
->PushPropertiesTo(host_impl_
);
280 EXPECT_FALSE(client_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
281 Animation::OPACITY
));
282 EXPECT_FALSE(client_impl_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
283 Animation::OPACITY
));
285 EXPECT_FALSE(client_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
287 EXPECT_FALSE(client_impl_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
290 host_impl_
->animation_registrar()->ActivateAnimations();
292 base::TimeTicks time
;
293 time
+= base::TimeDelta::FromSecondsD(0.1);
294 AnimateLayersTransferEvents(time
, 1u);
296 time
+= base::TimeDelta::FromSecondsD(duration
);
297 AnimateLayersTransferEvents(time
, 1u);
299 client_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
301 client_impl_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
303 client_impl_
.ExpectOpacityPropertyMutated(layer_id_
, LayerTreeType::PENDING
,
306 EXPECT_FALSE(client_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
308 EXPECT_FALSE(client_impl_
.IsPropertyMutated(layer_id_
, LayerTreeType::ACTIVE
,
312 TEST_F(AnimationPlayerTest
, AddRemoveAnimationCausesSetNeedsCommit
) {
313 client_
.RegisterLayer(layer_id_
, LayerTreeType::ACTIVE
);
314 host_
->AddAnimationTimeline(timeline_
);
315 timeline_
->AttachPlayer(player_
);
316 player_
->AttachLayer(layer_id_
);
318 EXPECT_FALSE(client_
.mutators_need_commit());
320 const int animation_id
=
321 AddOpacityTransitionToPlayer(player_
.get(), 1., .7f
, .3f
, false);
323 EXPECT_TRUE(client_
.mutators_need_commit());
324 client_
.set_mutators_need_commit(false);
326 player_
->PauseAnimation(animation_id
, 1.);
327 EXPECT_TRUE(client_
.mutators_need_commit());
328 client_
.set_mutators_need_commit(false);
330 player_
->RemoveAnimation(animation_id
);
331 EXPECT_TRUE(client_
.mutators_need_commit());
332 client_
.set_mutators_need_commit(false);