OLD | NEW |
---|---|
(Empty) | |
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/animation_player.h" | |
6 | |
7 #include "base/containers/hash_tables.h" | |
8 #include "cc/animation/animation_delegate.h" | |
9 #include "cc/animation/animation_host.h" | |
10 #include "cc/animation/animation_registrar.h" | |
11 #include "cc/animation/animation_timeline.h" | |
12 #include "cc/test/animation_test_common.h" | |
13 #include "cc/trees/layer_tree_mutators_client.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace cc { | |
18 namespace { | |
19 | |
20 struct LayerTest { | |
21 LayerTest() { ClearMutatedProperties(); } | |
22 | |
23 void ClearMutatedProperties() { | |
24 for (int i = 0; i <= Animation::LAST_TARGET_PROPERTY; ++i) | |
25 mutated_properties_[i] = false; | |
26 } | |
27 | |
28 bool mutated_properties_[Animation::LAST_TARGET_PROPERTY + 1]; | |
29 }; | |
30 | |
31 class LayerTreeMutatorsClientTest : public LayerTreeMutatorsClient { | |
32 public: | |
33 explicit LayerTreeMutatorsClientTest(bool is_impl_instance) | |
34 : host_(AnimationHost::Create(is_impl_instance)), | |
35 mutators_need_commit_(false) { | |
36 host_->SetLayerTreeMutatorsClient(this); | |
37 } | |
38 | |
39 ~LayerTreeMutatorsClientTest() { | |
40 for (auto& kv : layers_in_pending_tree_) | |
41 delete kv.second; | |
42 for (auto& kv : layers_in_active_tree_) | |
43 delete kv.second; | |
44 host_->SetLayerTreeMutatorsClient(nullptr); | |
45 } | |
46 | |
47 void ClearMutatedProperties() { | |
48 for (auto& kv : layers_in_pending_tree_) | |
49 kv.second->ClearMutatedProperties(); | |
50 for (auto& kv : layers_in_active_tree_) | |
51 kv.second->ClearMutatedProperties(); | |
52 } | |
53 | |
54 bool IsLayerInActiveTree(int layer_id) const override { | |
55 return layers_in_active_tree_.count(layer_id); | |
56 } | |
57 | |
58 bool IsLayerInPendingTree(int layer_id) const override { | |
59 return layers_in_pending_tree_.count(layer_id); | |
60 } | |
61 | |
62 void SetMutatorsNeedCommit() override { mutators_need_commit_ = true; } | |
63 | |
64 void SetLayerFilterMutated(int layer_id, | |
65 bool active_tree, | |
66 const FilterOperations& filters) override { | |
67 SetLayerMutated(layer_id, active_tree, Animation::FILTER); | |
68 } | |
69 | |
70 void SetLayerOpacityMutated(int layer_id, | |
71 bool active_tree, | |
72 float opacity) override { | |
73 SetLayerMutated(layer_id, active_tree, Animation::OPACITY); | |
74 } | |
75 | |
76 void SetLayerTransformMutated(int layer_id, | |
77 bool active_tree, | |
78 const gfx::Transform& transform) override { | |
79 SetLayerMutated(layer_id, active_tree, Animation::TRANSFORM); | |
80 } | |
81 | |
82 void SetLayerScrollOffsetMutated( | |
83 int layer_id, | |
84 bool active_tree, | |
85 const gfx::ScrollOffset& scroll_offset) override { | |
86 SetLayerMutated(layer_id, active_tree, Animation::SCROLL_OFFSET); | |
87 } | |
88 | |
89 bool mutators_need_commit() const { return mutators_need_commit_; } | |
90 void set_mutators_need_commit(bool need) { mutators_need_commit_ = need; } | |
91 | |
92 void RegisterLayer(int layer_id, bool active_tree) { | |
93 LayerIdToTestLayer& layers_in_tree = | |
94 active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; | |
95 DCHECK(layers_in_tree.find(layer_id) == layers_in_tree.end()); | |
96 layers_in_tree.insert(std::make_pair(layer_id, new LayerTest())); | |
97 | |
98 DCHECK(host_); | |
99 host_->RegisterLayer(layer_id, active_tree); | |
100 } | |
101 | |
102 void UnregisterLayer(int layer_id, bool active_tree) { | |
103 DCHECK(host_); | |
104 host_->UnregisterLayer(layer_id, active_tree); | |
105 | |
106 LayerIdToTestLayer& layers_in_tree = | |
107 active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; | |
108 auto kv = layers_in_tree.find(layer_id); | |
109 DCHECK(kv != layers_in_tree.end()); | |
110 delete kv->second; | |
111 layers_in_tree.erase(kv); | |
112 } | |
113 | |
114 AnimationHost* host() { | |
115 DCHECK(host_); | |
116 return host_.get(); | |
117 } | |
118 | |
119 bool IsPropertyMutated(int layer_id, | |
120 bool active_tree, | |
121 Animation::TargetProperty property) { | |
122 LayerIdToTestLayer& layers_in_tree = | |
123 active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; | |
124 auto kv = layers_in_tree.find(layer_id); | |
125 DCHECK(kv != layers_in_tree.end()); | |
126 return kv->second->mutated_properties_[property]; | |
127 } | |
128 | |
129 private: | |
130 void SetLayerMutated(int layer_id, | |
131 bool active_tree, | |
132 Animation::TargetProperty property) { | |
133 LayerIdToTestLayer& layers_in_tree = | |
134 active_tree ? layers_in_active_tree_ : layers_in_pending_tree_; | |
135 auto kv = layers_in_tree.find(layer_id); | |
136 DCHECK(kv != layers_in_tree.end()); | |
137 kv->second->mutated_properties_[property] = true; | |
138 } | |
139 | |
140 scoped_ptr<AnimationHost> host_; | |
141 | |
142 typedef base::hash_map<int, LayerTest*> LayerIdToTestLayer; | |
143 LayerIdToTestLayer layers_in_active_tree_; | |
144 LayerIdToTestLayer layers_in_pending_tree_; | |
145 | |
146 bool mutators_need_commit_; | |
147 }; | |
148 | |
149 class AnimationDelegateTest : public AnimationDelegate { | |
150 public: | |
151 AnimationDelegateTest() : started_(false), finished_(false) {} | |
152 | |
153 void NotifyAnimationStarted(base::TimeTicks monotonic_time, | |
154 Animation::TargetProperty target_property, | |
155 int group) override { | |
156 started_ = true; | |
157 } | |
158 void NotifyAnimationFinished(base::TimeTicks monotonic_time, | |
159 Animation::TargetProperty target_property, | |
160 int group) override { | |
161 finished_ = true; | |
162 } | |
163 | |
164 bool started_; | |
165 bool finished_; | |
166 }; | |
167 | |
168 class AnimationPlayerTest : public testing::Test { | |
169 public: | |
170 AnimationPlayerTest() | |
171 : client_(false), | |
172 client_impl_(true), | |
173 timeline_id_(1), | |
174 player_id_(2), | |
175 layer_id_(3) { | |
176 host_ = client_.host(); | |
177 host_impl_ = client_impl_.host(); | |
178 } | |
179 | |
180 protected: | |
181 void SetUp() override { | |
182 timeline_ = AnimationTimeline::Create(timeline_id_); | |
183 player_ = AnimationPlayer::Create(player_id_); | |
184 } | |
185 | |
186 void GetImplObjectsByIDs() { | |
187 timeline_impl_ = host_impl_->GetTimelineById(timeline_id_); | |
188 EXPECT_TRUE(timeline_impl_); | |
189 player_impl_ = timeline_impl_->GetPlayerById(player_id_); | |
190 EXPECT_TRUE(player_impl_); | |
191 } | |
192 | |
193 void ReleaseRefPtrs() { | |
194 player_ = nullptr; | |
195 timeline_ = nullptr; | |
196 player_impl_ = nullptr; | |
197 timeline_impl_ = nullptr; | |
198 } | |
199 | |
200 LayerTreeMutatorsClientTest client_; | |
201 LayerTreeMutatorsClientTest client_impl_; | |
202 | |
203 AnimationHost* host_; | |
204 AnimationHost* host_impl_; | |
205 | |
206 const int timeline_id_; | |
207 const int player_id_; | |
208 const int layer_id_; | |
209 | |
210 scoped_refptr<AnimationTimeline> timeline_; | |
211 scoped_refptr<AnimationPlayer> player_; | |
212 | |
213 scoped_refptr<AnimationTimeline> timeline_impl_; | |
214 scoped_refptr<AnimationPlayer> player_impl_; | |
215 }; | |
216 | |
217 TEST_F(AnimationPlayerTest, AttachDetachLayerIfTimelineAttached) { | |
218 host_->AddAnimationTimeline(timeline_.get()); | |
219 timeline_->AttachPlayer(player_.get()); | |
220 EXPECT_FALSE(player_->layer_animation_controller()); | |
221 EXPECT_FALSE(player_->layer_id()); | |
222 | |
223 host_->PushPropertiesTo(host_impl_); | |
224 | |
225 EXPECT_FALSE(host_impl_->GetPlayerForLayerId(layer_id_)); | |
226 | |
227 GetImplObjectsByIDs(); | |
228 | |
229 EXPECT_FALSE(player_impl_->layer_animation_controller()); | |
230 EXPECT_FALSE(player_impl_->layer_id()); | |
231 | |
232 player_->AttachLayer(layer_id_); | |
233 EXPECT_EQ(player_, host_->GetPlayerForLayerId(layer_id_)); | |
234 EXPECT_TRUE(player_->layer_animation_controller()); | |
235 EXPECT_EQ(player_->layer_id(), layer_id_); | |
236 | |
237 host_->PushPropertiesTo(host_impl_); | |
238 | |
239 EXPECT_EQ(player_impl_, host_impl_->GetPlayerForLayerId(layer_id_)); | |
240 EXPECT_TRUE(player_impl_->layer_animation_controller()); | |
241 EXPECT_EQ(player_impl_->layer_id(), layer_id_); | |
242 | |
243 player_->DetachLayer(); | |
244 EXPECT_FALSE(host_->GetPlayerForLayerId(layer_id_)); | |
245 EXPECT_FALSE(player_->layer_animation_controller()); | |
246 EXPECT_FALSE(player_->layer_id()); | |
247 | |
248 host_->PushPropertiesTo(host_impl_); | |
249 | |
250 EXPECT_FALSE(host_impl_->GetPlayerForLayerId(layer_id_)); | |
251 EXPECT_FALSE(player_impl_->layer_animation_controller()); | |
252 EXPECT_FALSE(player_impl_->layer_id()); | |
253 | |
254 timeline_->DetachPlayer(player_.get()); | |
255 EXPECT_FALSE(player_->animation_timeline()); | |
256 EXPECT_FALSE(player_->layer_animation_controller()); | |
257 EXPECT_FALSE(player_->layer_id()); | |
258 } | |
259 | |
260 TEST_F(AnimationPlayerTest, AttachDetachTimelineIfLayerAttached) { | |
261 host_->AddAnimationTimeline(timeline_.get()); | |
262 | |
263 EXPECT_FALSE(player_->layer_animation_controller()); | |
264 EXPECT_FALSE(player_->layer_id()); | |
265 | |
266 player_->AttachLayer(layer_id_); | |
267 EXPECT_FALSE(player_->animation_timeline()); | |
268 EXPECT_FALSE(host_->GetPlayerForLayerId(layer_id_)); | |
269 EXPECT_FALSE(player_->layer_animation_controller()); | |
270 EXPECT_EQ(player_->layer_id(), layer_id_); | |
271 | |
272 timeline_->AttachPlayer(player_.get()); | |
273 EXPECT_EQ(timeline_, player_->animation_timeline()); | |
274 EXPECT_EQ(player_, host_->GetPlayerForLayerId(layer_id_)); | |
275 EXPECT_TRUE(player_->layer_animation_controller()); | |
276 EXPECT_EQ(player_->layer_id(), layer_id_); | |
277 | |
278 // removing player from timeline detaches layer. | |
ajuma
2015/04/22 15:47:18
Style nit: Start comments with a capital letter (h
loyso (OOO)
2015/04/30 06:40:21
Acknowledged.
| |
279 timeline_->DetachPlayer(player_.get()); | |
280 EXPECT_FALSE(player_->animation_timeline()); | |
281 EXPECT_FALSE(host_->GetPlayerForLayerId(layer_id_)); | |
282 EXPECT_FALSE(player_->layer_animation_controller()); | |
283 EXPECT_FALSE(player_->layer_id()); | |
284 } | |
285 | |
286 TEST_F(AnimationPlayerTest, AttachToLayerInActiveTree) { | |
287 // always and only in active tree for main thread. | |
ajuma
2015/04/22 15:47:18
Please re-phrase this comment to make it clearer.
loyso (OOO)
2015/04/30 06:40:21
Done.
| |
288 client_.RegisterLayer(layer_id_, true); | |
289 client_impl_.RegisterLayer(layer_id_, false); | |
290 | |
291 EXPECT_TRUE(client_.IsLayerInActiveTree(layer_id_)); | |
292 EXPECT_FALSE(client_.IsLayerInPendingTree(layer_id_)); | |
293 | |
294 host_->AddAnimationTimeline(timeline_.get()); | |
295 | |
296 timeline_->AttachPlayer(player_.get()); | |
297 player_->AttachLayer(layer_id_); | |
298 EXPECT_TRUE(player_->has_active_value_observer_for_testing()); | |
299 EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); | |
300 EXPECT_TRUE(player_->layer_animation_controller()); | |
301 | |
302 host_->PushPropertiesTo(host_impl_); | |
303 | |
304 GetImplObjectsByIDs(); | |
305 | |
306 EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); | |
307 EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); | |
308 | |
309 client_impl_.RegisterLayer(layer_id_, true); | |
310 EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); | |
311 EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); | |
312 | |
313 EXPECT_TRUE(client_impl_.IsLayerInActiveTree(layer_id_)); | |
314 EXPECT_TRUE(client_impl_.IsLayerInPendingTree(layer_id_)); | |
315 | |
316 // kill layer on main thread. | |
317 client_.UnregisterLayer(layer_id_, true); | |
318 EXPECT_FALSE(player_->has_active_value_observer_for_testing()); | |
319 EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); | |
320 EXPECT_TRUE(player_->layer_animation_controller()); | |
321 | |
322 // sync doesn't detach LayerImpl. | |
323 host_->PushPropertiesTo(host_impl_); | |
324 EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); | |
325 EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); | |
326 EXPECT_TRUE(player_impl_->layer_animation_controller()); | |
327 | |
328 // kill layer on impl thread in pending tree. | |
329 client_impl_.UnregisterLayer(layer_id_, false); | |
330 EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); | |
331 EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); | |
332 EXPECT_TRUE(player_impl_->layer_animation_controller()); | |
333 | |
334 // kill layer on impl thread in active tree. | |
335 client_impl_.UnregisterLayer(layer_id_, true); | |
336 EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); | |
337 EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); | |
338 EXPECT_TRUE(player_impl_->layer_animation_controller()); | |
339 | |
340 // sync doesn't change anything. | |
341 host_->PushPropertiesTo(host_impl_); | |
342 EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); | |
343 EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); | |
344 EXPECT_TRUE(player_impl_->layer_animation_controller()); | |
345 | |
346 player_->DetachLayer(); | |
347 EXPECT_FALSE(player_->layer_animation_controller()); | |
348 | |
349 // release ptrs now to test the order of destruction. | |
350 ReleaseRefPtrs(); | |
351 } | |
352 | |
353 TEST_F(AnimationPlayerTest, AttachToNotYetCreatedLayer) { | |
354 host_->AddAnimationTimeline(timeline_.get()); | |
355 timeline_->AttachPlayer(player_.get()); | |
356 | |
357 host_->PushPropertiesTo(host_impl_); | |
358 | |
359 GetImplObjectsByIDs(); | |
360 | |
361 player_->AttachLayer(layer_id_); | |
362 EXPECT_FALSE(player_->has_active_value_observer_for_testing()); | |
363 EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); | |
364 EXPECT_TRUE(player_->layer_animation_controller()); | |
365 | |
366 host_->PushPropertiesTo(host_impl_); | |
367 EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); | |
368 EXPECT_FALSE(player_impl_->has_pending_value_observer_for_testing()); | |
369 EXPECT_TRUE(player_impl_->layer_animation_controller()); | |
370 | |
371 // create layer. | |
372 client_.RegisterLayer(layer_id_, true); | |
373 EXPECT_TRUE(player_->has_active_value_observer_for_testing()); | |
374 EXPECT_FALSE(player_->has_pending_value_observer_for_testing()); | |
375 | |
376 client_impl_.RegisterLayer(layer_id_, false); | |
377 EXPECT_FALSE(player_impl_->has_active_value_observer_for_testing()); | |
378 EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); | |
379 | |
380 client_impl_.RegisterLayer(layer_id_, true); | |
381 EXPECT_TRUE(player_impl_->has_active_value_observer_for_testing()); | |
382 EXPECT_TRUE(player_impl_->has_pending_value_observer_for_testing()); | |
383 } | |
384 | |
385 TEST_F(AnimationPlayerTest, PropertiesMutate) { | |
386 client_.RegisterLayer(layer_id_, true); | |
387 client_impl_.RegisterLayer(layer_id_, false); | |
388 client_impl_.RegisterLayer(layer_id_, true); | |
389 | |
390 host_->AddAnimationTimeline(timeline_.get()); | |
391 timeline_->AttachPlayer(player_.get()); | |
392 player_->AttachLayer(layer_id_); | |
393 | |
394 AddOpacityTransitionToPlayer(player_.get(), 1., .7f, .3f, false); | |
395 AddAnimatedTransformToPlayer(player_.get(), 1., 1, 1); | |
396 AddAnimatedFilterToPlayer(player_.get(), 1., .6f, .4f); | |
397 | |
398 host_->PushPropertiesTo(host_impl_); | |
399 | |
400 EXPECT_FALSE(client_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); | |
401 EXPECT_FALSE( | |
402 client_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); | |
403 EXPECT_FALSE(client_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); | |
404 | |
405 EXPECT_FALSE( | |
406 client_impl_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); | |
407 EXPECT_FALSE( | |
408 client_impl_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); | |
409 EXPECT_FALSE( | |
410 client_impl_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); | |
411 | |
412 const base::TimeTicks sec = | |
413 base::TimeTicks::FromInternalValue(base::Time::kMicrosecondsPerSecond); | |
414 | |
415 host_->animation_registrar()->AnimateLayers(sec); | |
416 host_impl_->animation_registrar()->ActivateAnimations(); | |
417 host_impl_->animation_registrar()->AnimateLayers(sec); | |
418 | |
419 EXPECT_TRUE(client_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); | |
420 EXPECT_TRUE(client_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); | |
421 EXPECT_TRUE(client_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); | |
422 | |
423 EXPECT_TRUE( | |
424 client_impl_.IsPropertyMutated(layer_id_, true, Animation::OPACITY)); | |
425 EXPECT_TRUE( | |
426 client_impl_.IsPropertyMutated(layer_id_, true, Animation::TRANSFORM)); | |
427 EXPECT_TRUE( | |
428 client_impl_.IsPropertyMutated(layer_id_, true, Animation::FILTER)); | |
429 | |
430 EXPECT_TRUE( | |
431 client_impl_.IsPropertyMutated(layer_id_, false, Animation::OPACITY)); | |
432 EXPECT_TRUE( | |
433 client_impl_.IsPropertyMutated(layer_id_, false, Animation::TRANSFORM)); | |
434 EXPECT_TRUE( | |
435 client_impl_.IsPropertyMutated(layer_id_, false, Animation::FILTER)); | |
436 } | |
437 | |
438 TEST_F(AnimationPlayerTest, LayerAnimationDelegate) { | |
439 AnimationDelegateTest delegate; | |
440 | |
441 client_.RegisterLayer(layer_id_, true); | |
442 host_->AddAnimationTimeline(timeline_.get()); | |
443 timeline_->AttachPlayer(player_.get()); | |
444 | |
445 // set delegate before controller created. | |
446 player_->set_layer_animation_delegate(&delegate); | |
447 player_->AttachLayer(layer_id_); | |
448 | |
449 AddOpacityTransitionToPlayer(player_.get(), 1., .7f, .3f, false); | |
450 | |
451 EXPECT_FALSE(delegate.started_); | |
452 host_->animation_registrar()->AnimateLayers( | |
453 base::TimeTicks::FromInternalValue(base::Time::kMicrosecondsPerSecond)); | |
454 | |
455 scoped_ptr<AnimationEventsVector> events = | |
456 host_->animation_registrar()->CreateEvents(); | |
457 host_->animation_registrar()->UpdateAnimationState(true, events.get()); | |
458 host_->animation_registrar()->SetAnimationEvents(events.Pass()); | |
459 | |
460 EXPECT_TRUE(delegate.started_); | |
461 } | |
462 | |
463 TEST_F(AnimationPlayerTest, AddRemoveAnimationCausesSetNeedsCommit) { | |
464 client_.RegisterLayer(layer_id_, true); | |
465 host_->AddAnimationTimeline(timeline_.get()); | |
466 timeline_->AttachPlayer(player_.get()); | |
467 player_->AttachLayer(layer_id_); | |
468 | |
469 EXPECT_FALSE(client_.mutators_need_commit()); | |
470 | |
471 const int animation_id = | |
472 AddOpacityTransitionToPlayer(player_.get(), 1., .7f, .3f, false); | |
473 | |
474 EXPECT_TRUE(client_.mutators_need_commit()); | |
475 client_.set_mutators_need_commit(false); | |
476 | |
477 player_->PauseAnimation(animation_id, 1.); | |
478 EXPECT_TRUE(client_.mutators_need_commit()); | |
479 client_.set_mutators_need_commit(false); | |
480 | |
481 player_->RemoveAnimation(animation_id); | |
482 EXPECT_TRUE(client_.mutators_need_commit()); | |
483 client_.set_mutators_need_commit(false); | |
484 } | |
485 | |
486 } // namespace | |
487 } // namespace cc | |
OLD | NEW |