Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: cc/animation/animation_player_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698