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

Side by Side Diff: cc/layers/layer_unittest.cc

Issue 1057283003: Remove parts of //cc we aren't using (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 8 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
« no previous file with comments | « cc/layers/layer_position_constraint_unittest.cc ('k') | cc/layers/layer_utils.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2011 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/layers/layer.h"
6
7 #include "cc/animation/keyframed_animation_curve.h"
8 #include "cc/base/math_util.h"
9 #include "cc/layers/layer_impl.h"
10 #include "cc/resources/layer_painter.h"
11 #include "cc/test/animation_test_common.h"
12 #include "cc/test/fake_impl_proxy.h"
13 #include "cc/test/fake_layer_tree_host_client.h"
14 #include "cc/test/fake_layer_tree_host_impl.h"
15 #include "cc/test/geometry_test_utils.h"
16 #include "cc/test/layer_test_common.h"
17 #include "cc/test/test_gpu_memory_buffer_manager.h"
18 #include "cc/test/test_shared_bitmap_manager.h"
19 #include "cc/test/test_task_graph_runner.h"
20 #include "cc/trees/layer_tree_host.h"
21 #include "cc/trees/single_thread_proxy.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "ui/gfx/transform.h"
25
26 using ::testing::AnyNumber;
27 using ::testing::AtLeast;
28 using ::testing::Mock;
29 using ::testing::StrictMock;
30 using ::testing::_;
31
32 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
33 do { \
34 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
35 code_to_test; \
36 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
37 } while (false)
38
39 namespace cc {
40 namespace {
41
42 class MockLayerTreeHost : public LayerTreeHost {
43 public:
44 explicit MockLayerTreeHost(FakeLayerTreeHostClient* client)
45 : LayerTreeHost(client, nullptr, nullptr, nullptr, LayerTreeSettings()) {
46 InitializeSingleThreaded(client,
47 base::MessageLoopProxy::current(),
48 nullptr);
49 }
50
51 MOCK_METHOD0(SetNeedsCommit, void());
52 MOCK_METHOD0(SetNeedsUpdateLayers, void());
53 MOCK_METHOD0(SetNeedsFullTreeSync, void());
54 };
55
56 class MockLayerPainter : public LayerPainter {
57 public:
58 void Paint(SkCanvas* canvas, const gfx::Rect& content_rect) override {}
59 };
60
61 class LayerTest : public testing::Test {
62 public:
63 LayerTest()
64 : host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
65 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
66
67 protected:
68 void SetUp() override {
69 layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_));
70 }
71
72 void TearDown() override {
73 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
74 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
75 parent_ = nullptr;
76 child1_ = nullptr;
77 child2_ = nullptr;
78 child3_ = nullptr;
79 grand_child1_ = nullptr;
80 grand_child2_ = nullptr;
81 grand_child3_ = nullptr;
82
83 layer_tree_host_->SetRootLayer(nullptr);
84 layer_tree_host_ = nullptr;
85 }
86
87 void VerifyTestTreeInitialState() const {
88 ASSERT_EQ(3U, parent_->children().size());
89 EXPECT_EQ(child1_, parent_->children()[0]);
90 EXPECT_EQ(child2_, parent_->children()[1]);
91 EXPECT_EQ(child3_, parent_->children()[2]);
92 EXPECT_EQ(parent_.get(), child1_->parent());
93 EXPECT_EQ(parent_.get(), child2_->parent());
94 EXPECT_EQ(parent_.get(), child3_->parent());
95
96 ASSERT_EQ(2U, child1_->children().size());
97 EXPECT_EQ(grand_child1_, child1_->children()[0]);
98 EXPECT_EQ(grand_child2_, child1_->children()[1]);
99 EXPECT_EQ(child1_.get(), grand_child1_->parent());
100 EXPECT_EQ(child1_.get(), grand_child2_->parent());
101
102 ASSERT_EQ(1U, child2_->children().size());
103 EXPECT_EQ(grand_child3_, child2_->children()[0]);
104 EXPECT_EQ(child2_.get(), grand_child3_->parent());
105
106 ASSERT_EQ(0U, child3_->children().size());
107 }
108
109 void CreateSimpleTestTree() {
110 parent_ = Layer::Create();
111 child1_ = Layer::Create();
112 child2_ = Layer::Create();
113 child3_ = Layer::Create();
114 grand_child1_ = Layer::Create();
115 grand_child2_ = Layer::Create();
116 grand_child3_ = Layer::Create();
117
118 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
119 layer_tree_host_->SetRootLayer(parent_);
120
121 parent_->AddChild(child1_);
122 parent_->AddChild(child2_);
123 parent_->AddChild(child3_);
124 child1_->AddChild(grand_child1_);
125 child1_->AddChild(grand_child2_);
126 child2_->AddChild(grand_child3_);
127
128 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
129
130 VerifyTestTreeInitialState();
131 }
132
133 FakeImplProxy proxy_;
134 TestSharedBitmapManager shared_bitmap_manager_;
135 TestTaskGraphRunner task_graph_runner_;
136 FakeLayerTreeHostImpl host_impl_;
137
138 FakeLayerTreeHostClient fake_client_;
139 scoped_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
140 scoped_refptr<Layer> parent_;
141 scoped_refptr<Layer> child1_;
142 scoped_refptr<Layer> child2_;
143 scoped_refptr<Layer> child3_;
144 scoped_refptr<Layer> grand_child1_;
145 scoped_refptr<Layer> grand_child2_;
146 scoped_refptr<Layer> grand_child3_;
147 };
148
149 TEST_F(LayerTest, BasicCreateAndDestroy) {
150 scoped_refptr<Layer> test_layer = Layer::Create();
151 ASSERT_TRUE(test_layer.get());
152
153 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
154 test_layer->SetLayerTreeHost(layer_tree_host_.get());
155 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
156
157 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
158 test_layer->SetLayerTreeHost(nullptr);
159 }
160
161 TEST_F(LayerTest, AddAndRemoveChild) {
162 scoped_refptr<Layer> parent = Layer::Create();
163 scoped_refptr<Layer> child = Layer::Create();
164
165 // Upon creation, layers should not have children or parent.
166 ASSERT_EQ(0U, parent->children().size());
167 EXPECT_FALSE(child->parent());
168
169 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
170 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
171
172 ASSERT_EQ(1U, parent->children().size());
173 EXPECT_EQ(child.get(), parent->children()[0].get());
174 EXPECT_EQ(parent.get(), child->parent());
175 EXPECT_EQ(parent.get(), child->RootLayer());
176
177 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
178 }
179
180 TEST_F(LayerTest, AddSameChildTwice) {
181 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
182
183 scoped_refptr<Layer> parent = Layer::Create();
184 scoped_refptr<Layer> child = Layer::Create();
185
186 layer_tree_host_->SetRootLayer(parent);
187
188 ASSERT_EQ(0u, parent->children().size());
189
190 parent->AddChild(child);
191 ASSERT_EQ(1u, parent->children().size());
192 EXPECT_EQ(parent.get(), child->parent());
193
194 parent->AddChild(child);
195 ASSERT_EQ(1u, parent->children().size());
196 EXPECT_EQ(parent.get(), child->parent());
197 }
198
199 TEST_F(LayerTest, InsertChild) {
200 scoped_refptr<Layer> parent = Layer::Create();
201 scoped_refptr<Layer> child1 = Layer::Create();
202 scoped_refptr<Layer> child2 = Layer::Create();
203 scoped_refptr<Layer> child3 = Layer::Create();
204 scoped_refptr<Layer> child4 = Layer::Create();
205
206 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
207
208 ASSERT_EQ(0U, parent->children().size());
209
210 // Case 1: inserting to empty list.
211 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
212 ASSERT_EQ(1U, parent->children().size());
213 EXPECT_EQ(child3, parent->children()[0]);
214 EXPECT_EQ(parent.get(), child3->parent());
215
216 // Case 2: inserting to beginning of list
217 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
218 ASSERT_EQ(2U, parent->children().size());
219 EXPECT_EQ(child1, parent->children()[0]);
220 EXPECT_EQ(child3, parent->children()[1]);
221 EXPECT_EQ(parent.get(), child1->parent());
222
223 // Case 3: inserting to middle of list
224 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
225 ASSERT_EQ(3U, parent->children().size());
226 EXPECT_EQ(child1, parent->children()[0]);
227 EXPECT_EQ(child2, parent->children()[1]);
228 EXPECT_EQ(child3, parent->children()[2]);
229 EXPECT_EQ(parent.get(), child2->parent());
230
231 // Case 4: inserting to end of list
232 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
233
234 ASSERT_EQ(4U, parent->children().size());
235 EXPECT_EQ(child1, parent->children()[0]);
236 EXPECT_EQ(child2, parent->children()[1]);
237 EXPECT_EQ(child3, parent->children()[2]);
238 EXPECT_EQ(child4, parent->children()[3]);
239 EXPECT_EQ(parent.get(), child4->parent());
240
241 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
242 }
243
244 TEST_F(LayerTest, InsertChildPastEndOfList) {
245 scoped_refptr<Layer> parent = Layer::Create();
246 scoped_refptr<Layer> child1 = Layer::Create();
247 scoped_refptr<Layer> child2 = Layer::Create();
248
249 ASSERT_EQ(0U, parent->children().size());
250
251 // insert to an out-of-bounds index
252 parent->InsertChild(child1, 53);
253
254 ASSERT_EQ(1U, parent->children().size());
255 EXPECT_EQ(child1, parent->children()[0]);
256
257 // insert another child to out-of-bounds, when list is not already empty.
258 parent->InsertChild(child2, 2459);
259
260 ASSERT_EQ(2U, parent->children().size());
261 EXPECT_EQ(child1, parent->children()[0]);
262 EXPECT_EQ(child2, parent->children()[1]);
263 }
264
265 TEST_F(LayerTest, InsertSameChildTwice) {
266 scoped_refptr<Layer> parent = Layer::Create();
267 scoped_refptr<Layer> child1 = Layer::Create();
268 scoped_refptr<Layer> child2 = Layer::Create();
269
270 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
271
272 ASSERT_EQ(0U, parent->children().size());
273
274 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
275 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
276
277 ASSERT_EQ(2U, parent->children().size());
278 EXPECT_EQ(child1, parent->children()[0]);
279 EXPECT_EQ(child2, parent->children()[1]);
280
281 // Inserting the same child again should cause the child to be removed and
282 // re-inserted at the new location.
283 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
284
285 // child1 should now be at the end of the list.
286 ASSERT_EQ(2U, parent->children().size());
287 EXPECT_EQ(child2, parent->children()[0]);
288 EXPECT_EQ(child1, parent->children()[1]);
289
290 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
291 }
292
293 TEST_F(LayerTest, ReplaceChildWithNewChild) {
294 CreateSimpleTestTree();
295 scoped_refptr<Layer> child4 = Layer::Create();
296
297 EXPECT_FALSE(child4->parent());
298
299 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
300 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
301 EXPECT_FALSE(parent_->NeedsDisplayForTesting());
302 EXPECT_FALSE(child1_->NeedsDisplayForTesting());
303 EXPECT_FALSE(child2_->NeedsDisplayForTesting());
304 EXPECT_FALSE(child3_->NeedsDisplayForTesting());
305 EXPECT_FALSE(child4->NeedsDisplayForTesting());
306
307 ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
308 EXPECT_EQ(child1_, parent_->children()[0]);
309 EXPECT_EQ(child4, parent_->children()[1]);
310 EXPECT_EQ(child3_, parent_->children()[2]);
311 EXPECT_EQ(parent_.get(), child4->parent());
312
313 EXPECT_FALSE(child2_->parent());
314 }
315
316 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
317 CreateSimpleTestTree();
318
319 // create another simple tree with test_layer and child4.
320 scoped_refptr<Layer> test_layer = Layer::Create();
321 scoped_refptr<Layer> child4 = Layer::Create();
322 test_layer->AddChild(child4);
323 ASSERT_EQ(1U, test_layer->children().size());
324 EXPECT_EQ(child4, test_layer->children()[0]);
325 EXPECT_EQ(test_layer.get(), child4->parent());
326
327 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
328 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
329
330 ASSERT_EQ(3U, parent_->children().size());
331 EXPECT_EQ(child1_, parent_->children()[0]);
332 EXPECT_EQ(child4, parent_->children()[1]);
333 EXPECT_EQ(child3_, parent_->children()[2]);
334 EXPECT_EQ(parent_.get(), child4->parent());
335
336 // test_layer should no longer have child4,
337 // and child2 should no longer have a parent.
338 ASSERT_EQ(0U, test_layer->children().size());
339 EXPECT_FALSE(child2_->parent());
340 }
341
342 TEST_F(LayerTest, DeleteRemovedScrollParent) {
343 scoped_refptr<Layer> parent = Layer::Create();
344 scoped_refptr<Layer> child1 = Layer::Create();
345 scoped_refptr<Layer> child2 = Layer::Create();
346
347 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
348
349 ASSERT_EQ(0U, parent->children().size());
350
351 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
352 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
353
354 ASSERT_EQ(2U, parent->children().size());
355 EXPECT_EQ(child1, parent->children()[0]);
356 EXPECT_EQ(child2, parent->children()[1]);
357
358 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
359
360 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
361
362 child1->reset_needs_push_properties_for_testing();
363
364 EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
365
366 EXPECT_TRUE(child1->needs_push_properties());
367
368 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
369 }
370
371 TEST_F(LayerTest, DeleteRemovedScrollChild) {
372 scoped_refptr<Layer> parent = Layer::Create();
373 scoped_refptr<Layer> child1 = Layer::Create();
374 scoped_refptr<Layer> child2 = Layer::Create();
375
376 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
377
378 ASSERT_EQ(0U, parent->children().size());
379
380 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
381 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
382
383 ASSERT_EQ(2U, parent->children().size());
384 EXPECT_EQ(child1, parent->children()[0]);
385 EXPECT_EQ(child2, parent->children()[1]);
386
387 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
388
389 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
390
391 child2->reset_needs_push_properties_for_testing();
392
393 EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
394
395 EXPECT_TRUE(child2->needs_push_properties());
396
397 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
398 }
399
400 TEST_F(LayerTest, ReplaceChildWithSameChild) {
401 CreateSimpleTestTree();
402
403 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
404 // same child.
405 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
406 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
407 parent_->ReplaceChild(child2_.get(), child2_);
408
409 VerifyTestTreeInitialState();
410 }
411
412 TEST_F(LayerTest, RemoveAllChildren) {
413 CreateSimpleTestTree();
414
415 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
416
417 ASSERT_EQ(0U, parent_->children().size());
418 EXPECT_FALSE(child1_->parent());
419 EXPECT_FALSE(child2_->parent());
420 EXPECT_FALSE(child3_->parent());
421 }
422
423 TEST_F(LayerTest, SetChildren) {
424 scoped_refptr<Layer> old_parent = Layer::Create();
425 scoped_refptr<Layer> new_parent = Layer::Create();
426
427 scoped_refptr<Layer> child1 = Layer::Create();
428 scoped_refptr<Layer> child2 = Layer::Create();
429
430 LayerList new_children;
431 new_children.push_back(child1);
432 new_children.push_back(child2);
433
434 // Set up and verify initial test conditions: child1 has a parent, child2 has
435 // no parent.
436 old_parent->AddChild(child1);
437 ASSERT_EQ(0U, new_parent->children().size());
438 EXPECT_EQ(old_parent.get(), child1->parent());
439 EXPECT_FALSE(child2->parent());
440
441 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
442 1, layer_tree_host_->SetRootLayer(new_parent));
443
444 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
445 AtLeast(1), new_parent->SetChildren(new_children));
446
447 ASSERT_EQ(2U, new_parent->children().size());
448 EXPECT_EQ(new_parent.get(), child1->parent());
449 EXPECT_EQ(new_parent.get(), child2->parent());
450
451 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
452 }
453
454 TEST_F(LayerTest, HasAncestor) {
455 scoped_refptr<Layer> parent = Layer::Create();
456 EXPECT_FALSE(parent->HasAncestor(parent.get()));
457
458 scoped_refptr<Layer> child = Layer::Create();
459 parent->AddChild(child);
460
461 EXPECT_FALSE(child->HasAncestor(child.get()));
462 EXPECT_TRUE(child->HasAncestor(parent.get()));
463 EXPECT_FALSE(parent->HasAncestor(child.get()));
464
465 scoped_refptr<Layer> child_child = Layer::Create();
466 child->AddChild(child_child);
467
468 EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
469 EXPECT_TRUE(child_child->HasAncestor(parent.get()));
470 EXPECT_TRUE(child_child->HasAncestor(child.get()));
471 EXPECT_FALSE(parent->HasAncestor(child.get()));
472 EXPECT_FALSE(parent->HasAncestor(child_child.get()));
473 }
474
475 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
476 CreateSimpleTestTree();
477
478 // For this test we don't care about SetNeedsFullTreeSync calls.
479 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
480
481 scoped_refptr<Layer> child4 = Layer::Create();
482
483 EXPECT_EQ(parent_.get(), parent_->RootLayer());
484 EXPECT_EQ(parent_.get(), child1_->RootLayer());
485 EXPECT_EQ(parent_.get(), child2_->RootLayer());
486 EXPECT_EQ(parent_.get(), child3_->RootLayer());
487 EXPECT_EQ(child4.get(), child4->RootLayer());
488 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
489 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
490 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
491
492 child1_->RemoveFromParent();
493
494 // |child1| and its children, grand_child1 and grand_child2 are now on a
495 // separate subtree.
496 EXPECT_EQ(parent_.get(), parent_->RootLayer());
497 EXPECT_EQ(child1_.get(), child1_->RootLayer());
498 EXPECT_EQ(parent_.get(), child2_->RootLayer());
499 EXPECT_EQ(parent_.get(), child3_->RootLayer());
500 EXPECT_EQ(child4.get(), child4->RootLayer());
501 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
502 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
503 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
504
505 grand_child3_->AddChild(child4);
506
507 EXPECT_EQ(parent_.get(), parent_->RootLayer());
508 EXPECT_EQ(child1_.get(), child1_->RootLayer());
509 EXPECT_EQ(parent_.get(), child2_->RootLayer());
510 EXPECT_EQ(parent_.get(), child3_->RootLayer());
511 EXPECT_EQ(parent_.get(), child4->RootLayer());
512 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
513 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
514 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
515
516 child2_->ReplaceChild(grand_child3_.get(), child1_);
517
518 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
519 // the tree under child2.
520 EXPECT_EQ(parent_.get(), parent_->RootLayer());
521 EXPECT_EQ(parent_.get(), child1_->RootLayer());
522 EXPECT_EQ(parent_.get(), child2_->RootLayer());
523 EXPECT_EQ(parent_.get(), child3_->RootLayer());
524 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
525 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
526 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
527 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
528 }
529
530 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
531 // The semantics for SetNeedsDisplay which are tested here:
532 // 1. sets NeedsDisplay flag appropriately.
533 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
534 // SetNeedsDisplay.
535
536 scoped_refptr<Layer> test_layer = Layer::Create();
537 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
538 1, layer_tree_host_->SetRootLayer(test_layer));
539 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
540
541 gfx::Size test_bounds = gfx::Size(501, 508);
542
543 gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
544 gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
545 gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
546
547 // Before anything, test_layer should not be dirty.
548 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
549
550 // This is just initialization, but SetNeedsCommit behavior is verified anyway
551 // to avoid warnings.
552 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
553 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
554
555 // The real test begins here.
556 test_layer->ResetNeedsDisplayForTesting();
557 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
558
559 // Case 1: Layer should accept dirty rects that go beyond its bounds.
560 test_layer->ResetNeedsDisplayForTesting();
561 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
562 EXPECT_SET_NEEDS_UPDATE(
563 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
564 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
565 test_layer->ResetNeedsDisplayForTesting();
566
567 // Case 2: SetNeedsDisplay() without the dirty rect arg.
568 test_layer->ResetNeedsDisplayForTesting();
569 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
570 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
571 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
572 test_layer->ResetNeedsDisplayForTesting();
573
574 // Case 3: SetNeedsDisplay() with an empty rect.
575 test_layer->ResetNeedsDisplayForTesting();
576 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
577 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
578 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
579
580 // Case 4: SetNeedsDisplay() with a non-drawable layer
581 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
582 test_layer->ResetNeedsDisplayForTesting();
583 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
584 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
585 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
586 }
587
588 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
589 scoped_refptr<Layer> test_layer = Layer::Create();
590 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
591 1, layer_tree_host_->SetRootLayer(test_layer));
592 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
593
594 scoped_refptr<Layer> dummy_layer1 = Layer::Create();
595 scoped_refptr<Layer> dummy_layer2 = Layer::Create();
596
597 // sanity check of initial test condition
598 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
599
600 // Next, test properties that should call SetNeedsCommit (but not
601 // SetNeedsDisplay). All properties need to be set to new values in order for
602 // SetNeedsCommit to be called.
603 EXPECT_SET_NEEDS_COMMIT(
604 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
605 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
606 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
607 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
608 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
609 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
610 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
611 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
612 // We can use any layer pointer here since we aren't syncing for real.
613 EXPECT_SET_NEEDS_COMMIT(1,
614 test_layer->SetScrollClipLayerId(test_layer->id()));
615 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
616 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
617 gfx::ScrollOffset(10, 10)));
618 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
619 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
620 Region(gfx::Rect(1, 1, 2, 2))));
621 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
622 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
624 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
625 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
626 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
627 gfx::Rect(10, 10)));
628 EXPECT_SET_NEEDS_COMMIT(
629 1,
630 test_layer->SetDrawCheckerboardForMissingTiles(
631 !test_layer->draw_checkerboard_for_missing_tiles()));
632 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
633 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
634
635 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
636 dummy_layer1.get()));
637 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
638 dummy_layer2.get()));
639
640 // The above tests should not have caused a change to the needs_display flag.
641 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
642
643 // As layers are removed from the tree, they will cause a tree sync.
644 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
645 }
646
647 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
648 scoped_refptr<Layer> test_layer = Layer::Create();
649 scoped_ptr<LayerImpl> impl_layer =
650 LayerImpl::Create(host_impl_.active_tree(), 1);
651
652 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
653 layer_tree_host_->SetRootLayer(test_layer));
654
655 test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
656 test_layer->PushPropertiesTo(impl_layer.get());
657 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
658 impl_layer->update_rect());
659
660 // The LayerImpl's update_rect() should be accumulated here, since we did not
661 // do anything to clear it.
662 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
663 test_layer->PushPropertiesTo(impl_layer.get());
664 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
665 impl_layer->update_rect());
666
667 // If we do clear the LayerImpl side, then the next update_rect() should be
668 // fresh without accumulation.
669 impl_layer->ResetAllChangeTrackingForSubtree();
670 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
671 test_layer->PushPropertiesTo(impl_layer.get());
672 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
673 impl_layer->update_rect());
674 }
675
676 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
677 scoped_refptr<Layer> test_layer = Layer::Create();
678 scoped_ptr<LayerImpl> impl_layer =
679 LayerImpl::Create(host_impl_.active_tree(), 1);
680
681 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
682 layer_tree_host_->SetRootLayer(test_layer));
683
684 gfx::Transform transform;
685 transform.Rotate(45.0);
686 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
687
688 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
689
690 test_layer->PushPropertiesTo(impl_layer.get());
691
692 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
693 }
694
695 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
696 scoped_refptr<Layer> test_layer = Layer::Create();
697 scoped_ptr<LayerImpl> impl_layer =
698 LayerImpl::Create(host_impl_.active_tree(), 1);
699
700 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
701 layer_tree_host_->SetRootLayer(test_layer));
702
703 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
704
705 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
706
707 test_layer->PushPropertiesTo(impl_layer.get());
708
709 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
710 }
711
712 TEST_F(LayerTest,
713 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
714 scoped_refptr<Layer> test_layer = Layer::Create();
715 scoped_ptr<LayerImpl> impl_layer =
716 LayerImpl::Create(host_impl_.active_tree(), 1);
717
718 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
719 layer_tree_host_->SetRootLayer(test_layer));
720
721 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
722 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
723 registrar.get());
724
725 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
726 1.0,
727 0,
728 100);
729
730 gfx::Transform transform;
731 transform.Rotate(45.0);
732 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
733
734 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
735 test_layer->PushPropertiesTo(impl_layer.get());
736 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
737
738 impl_layer->ResetAllChangeTrackingForSubtree();
739 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
740 1.0,
741 0,
742 100);
743 impl_layer->layer_animation_controller()
744 ->GetAnimation(Animation::TRANSFORM)
745 ->set_is_impl_only(true);
746 transform.Rotate(45.0);
747 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
748
749 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
750 test_layer->PushPropertiesTo(impl_layer.get());
751 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
752 }
753
754 TEST_F(LayerTest,
755 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
756 scoped_refptr<Layer> test_layer = Layer::Create();
757 scoped_ptr<LayerImpl> impl_layer =
758 LayerImpl::Create(host_impl_.active_tree(), 1);
759
760 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
761 layer_tree_host_->SetRootLayer(test_layer));
762
763 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
764 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
765 registrar.get());
766
767 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
768 1.0,
769 0.3f,
770 0.7f,
771 false);
772
773 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
774
775 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
776 test_layer->PushPropertiesTo(impl_layer.get());
777 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
778
779 impl_layer->ResetAllChangeTrackingForSubtree();
780 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
781 1.0,
782 0.3f,
783 0.7f,
784 false);
785 impl_layer->layer_animation_controller()
786 ->GetAnimation(Animation::OPACITY)
787 ->set_is_impl_only(true);
788 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
789
790 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
791 test_layer->PushPropertiesTo(impl_layer.get());
792 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
793 }
794
795 TEST_F(LayerTest,
796 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
797 scoped_refptr<Layer> test_layer = Layer::Create();
798 scoped_ptr<LayerImpl> impl_layer =
799 LayerImpl::Create(host_impl_.active_tree(), 1);
800
801 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
802 layer_tree_host_->SetRootLayer(test_layer));
803
804 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
805 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
806 registrar.get());
807
808 AddAnimatedFilterToController(
809 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
810
811 FilterOperations filters;
812 filters.Append(FilterOperation::CreateBlurFilter(2.f));
813 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
814
815 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
816 test_layer->PushPropertiesTo(impl_layer.get());
817 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
818
819 impl_layer->ResetAllChangeTrackingForSubtree();
820 AddAnimatedFilterToController(
821 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
822 impl_layer->layer_animation_controller()
823 ->GetAnimation(Animation::FILTER)
824 ->set_is_impl_only(true);
825 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
826 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
827
828 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
829 test_layer->PushPropertiesTo(impl_layer.get());
830 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
831 }
832
833 TEST_F(LayerTest, MaskAndReplicaHasParent) {
834 scoped_refptr<Layer> parent = Layer::Create();
835 scoped_refptr<Layer> child = Layer::Create();
836 scoped_refptr<Layer> mask = Layer::Create();
837 scoped_refptr<Layer> replica = Layer::Create();
838 scoped_refptr<Layer> replica_mask = Layer::Create();
839 scoped_refptr<Layer> mask_replacement = Layer::Create();
840 scoped_refptr<Layer> replica_replacement = Layer::Create();
841 scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
842
843 parent->AddChild(child);
844 child->SetMaskLayer(mask.get());
845 child->SetReplicaLayer(replica.get());
846 replica->SetMaskLayer(replica_mask.get());
847
848 EXPECT_EQ(parent.get(), child->parent());
849 EXPECT_EQ(child.get(), mask->parent());
850 EXPECT_EQ(child.get(), replica->parent());
851 EXPECT_EQ(replica.get(), replica_mask->parent());
852
853 replica->SetMaskLayer(replica_mask_replacement.get());
854 EXPECT_EQ(nullptr, replica_mask->parent());
855 EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
856
857 child->SetMaskLayer(mask_replacement.get());
858 EXPECT_EQ(nullptr, mask->parent());
859 EXPECT_EQ(child.get(), mask_replacement->parent());
860
861 child->SetReplicaLayer(replica_replacement.get());
862 EXPECT_EQ(nullptr, replica->parent());
863 EXPECT_EQ(child.get(), replica_replacement->parent());
864
865 EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
866 }
867
868 TEST_F(LayerTest, CheckTranformIsInvertible) {
869 scoped_refptr<Layer> layer = Layer::Create();
870 scoped_ptr<LayerImpl> impl_layer =
871 LayerImpl::Create(host_impl_.active_tree(), 1);
872 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
873 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
874 layer_tree_host_->SetRootLayer(layer);
875
876 EXPECT_TRUE(layer->transform_is_invertible());
877
878 gfx::Transform singular_transform;
879 singular_transform.Scale3d(
880 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
881
882 layer->SetTransform(singular_transform);
883 layer->PushPropertiesTo(impl_layer.get());
884
885 EXPECT_FALSE(layer->transform_is_invertible());
886 EXPECT_FALSE(impl_layer->transform_is_invertible());
887
888 gfx::Transform rotation_transform;
889 rotation_transform.RotateAboutZAxis(-45.0);
890
891 layer->SetTransform(rotation_transform);
892 layer->PushPropertiesTo(impl_layer.get());
893 EXPECT_TRUE(layer->transform_is_invertible());
894 EXPECT_TRUE(impl_layer->transform_is_invertible());
895
896 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
897 }
898
899 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
900 scoped_refptr<Layer> layer = Layer::Create();
901 scoped_ptr<LayerImpl> impl_layer =
902 LayerImpl::Create(host_impl_.active_tree(), 1);
903 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
904 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
905 layer_tree_host_->SetRootLayer(layer);
906
907 EXPECT_TRUE(layer->transform_is_invertible());
908
909 gfx::Transform singular_transform;
910 singular_transform.Scale3d(
911 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
912
913 layer->SetTransform(singular_transform);
914 layer->PushPropertiesTo(impl_layer.get());
915
916 EXPECT_FALSE(layer->transform_is_invertible());
917 EXPECT_FALSE(impl_layer->transform_is_invertible());
918
919 gfx::Transform identity_transform;
920
921 layer->SetTransform(identity_transform);
922 static_cast<LayerAnimationValueObserver*>(layer.get())
923 ->OnTransformAnimated(singular_transform);
924 layer->PushPropertiesTo(impl_layer.get());
925 EXPECT_FALSE(layer->transform_is_invertible());
926 EXPECT_FALSE(impl_layer->transform_is_invertible());
927
928 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
929 }
930
931 class LayerTreeHostFactory {
932 public:
933 LayerTreeHostFactory()
934 : client_(FakeLayerTreeHostClient::DIRECT_3D),
935 shared_bitmap_manager_(new TestSharedBitmapManager),
936 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager) {}
937
938 scoped_ptr<LayerTreeHost> Create() {
939 return LayerTreeHost::CreateSingleThreaded(
940 &client_, &client_, shared_bitmap_manager_.get(),
941 gpu_memory_buffer_manager_.get(), nullptr, LayerTreeSettings(),
942 base::MessageLoopProxy::current(), nullptr);
943 }
944
945 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
946 return LayerTreeHost::CreateSingleThreaded(
947 &client_, &client_, shared_bitmap_manager_.get(),
948 gpu_memory_buffer_manager_.get(), nullptr, settings,
949 base::MessageLoopProxy::current(), nullptr);
950 }
951
952 private:
953 FakeLayerTreeHostClient client_;
954 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
955 scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
956 };
957
958 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
959 EXPECT_EQ(host, layer->layer_tree_host());
960
961 for (size_t i = 0; i < layer->children().size(); ++i)
962 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
963
964 if (layer->mask_layer())
965 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
966
967 if (layer->replica_layer())
968 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
969 }
970
971 TEST(LayerLayerTreeHostTest, EnteringTree) {
972 scoped_refptr<Layer> parent = Layer::Create();
973 scoped_refptr<Layer> child = Layer::Create();
974 scoped_refptr<Layer> mask = Layer::Create();
975 scoped_refptr<Layer> replica = Layer::Create();
976 scoped_refptr<Layer> replica_mask = Layer::Create();
977
978 // Set up a detached tree of layers. The host pointer should be nil for these
979 // layers.
980 parent->AddChild(child);
981 child->SetMaskLayer(mask.get());
982 child->SetReplicaLayer(replica.get());
983 replica->SetMaskLayer(replica_mask.get());
984
985 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
986
987 LayerTreeHostFactory factory;
988 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
989 // Setting the root layer should set the host pointer for all layers in the
990 // tree.
991 layer_tree_host->SetRootLayer(parent.get());
992
993 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
994
995 // Clearing the root layer should also clear out the host pointers for all
996 // layers in the tree.
997 layer_tree_host->SetRootLayer(nullptr);
998
999 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
1000 }
1001
1002 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
1003 scoped_refptr<Layer> parent = Layer::Create();
1004 LayerTreeHostFactory factory;
1005 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1006
1007 layer_tree_host->SetRootLayer(parent.get());
1008
1009 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
1010
1011 // Adding a subtree to a layer already associated with a host should set the
1012 // host pointer on all layers in that subtree.
1013 scoped_refptr<Layer> child = Layer::Create();
1014 scoped_refptr<Layer> grand_child = Layer::Create();
1015 child->AddChild(grand_child);
1016
1017 // Masks, replicas, and replica masks should pick up the new host too.
1018 scoped_refptr<Layer> child_mask = Layer::Create();
1019 child->SetMaskLayer(child_mask.get());
1020 scoped_refptr<Layer> child_replica = Layer::Create();
1021 child->SetReplicaLayer(child_replica.get());
1022 scoped_refptr<Layer> child_replica_mask = Layer::Create();
1023 child_replica->SetMaskLayer(child_replica_mask.get());
1024
1025 parent->AddChild(child);
1026 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1027
1028 layer_tree_host->SetRootLayer(nullptr);
1029 }
1030
1031 TEST(LayerLayerTreeHostTest, ChangeHost) {
1032 scoped_refptr<Layer> parent = Layer::Create();
1033 scoped_refptr<Layer> child = Layer::Create();
1034 scoped_refptr<Layer> mask = Layer::Create();
1035 scoped_refptr<Layer> replica = Layer::Create();
1036 scoped_refptr<Layer> replica_mask = Layer::Create();
1037
1038 // Same setup as the previous test.
1039 parent->AddChild(child);
1040 child->SetMaskLayer(mask.get());
1041 child->SetReplicaLayer(replica.get());
1042 replica->SetMaskLayer(replica_mask.get());
1043
1044 LayerTreeHostFactory factory;
1045 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1046 first_layer_tree_host->SetRootLayer(parent.get());
1047
1048 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1049 first_layer_tree_host.get());
1050
1051 // Now re-root the tree to a new host (simulating what we do on a context lost
1052 // event). This should update the host pointers for all layers in the tree.
1053 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1054 second_layer_tree_host->SetRootLayer(parent.get());
1055
1056 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1057 second_layer_tree_host.get());
1058
1059 second_layer_tree_host->SetRootLayer(nullptr);
1060 }
1061
1062 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1063 scoped_refptr<Layer> first_parent = Layer::Create();
1064 scoped_refptr<Layer> first_child = Layer::Create();
1065 scoped_refptr<Layer> second_parent = Layer::Create();
1066 scoped_refptr<Layer> second_child = Layer::Create();
1067 scoped_refptr<Layer> second_grand_child = Layer::Create();
1068
1069 // First put all children under the first parent and set the first host.
1070 first_parent->AddChild(first_child);
1071 second_child->AddChild(second_grand_child);
1072 first_parent->AddChild(second_child);
1073
1074 LayerTreeHostFactory factory;
1075 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1076 first_layer_tree_host->SetRootLayer(first_parent.get());
1077
1078 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1079 first_layer_tree_host.get());
1080
1081 // Now reparent the subtree starting at second_child to a layer in a different
1082 // tree.
1083 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1084 second_layer_tree_host->SetRootLayer(second_parent.get());
1085
1086 second_parent->AddChild(second_child);
1087
1088 // The moved layer and its children should point to the new host.
1089 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1090 EXPECT_EQ(second_layer_tree_host.get(),
1091 second_grand_child->layer_tree_host());
1092
1093 // Test over, cleanup time.
1094 first_layer_tree_host->SetRootLayer(nullptr);
1095 second_layer_tree_host->SetRootLayer(nullptr);
1096 }
1097
1098 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1099 scoped_refptr<Layer> parent = Layer::Create();
1100 scoped_refptr<Layer> mask = Layer::Create();
1101 scoped_refptr<Layer> replica = Layer::Create();
1102 scoped_refptr<Layer> mask_child = Layer::Create();
1103 scoped_refptr<Layer> replica_child = Layer::Create();
1104 scoped_refptr<Layer> mask_replacement = Layer::Create();
1105 scoped_refptr<Layer> replica_replacement = Layer::Create();
1106
1107 parent->SetMaskLayer(mask.get());
1108 parent->SetReplicaLayer(replica.get());
1109 mask->AddChild(mask_child);
1110 replica->AddChild(replica_child);
1111
1112 LayerTreeHostFactory factory;
1113 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1114 layer_tree_host->SetRootLayer(parent.get());
1115
1116 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1117
1118 // Replacing the mask should clear out the old mask's subtree's host pointers.
1119 parent->SetMaskLayer(mask_replacement.get());
1120 EXPECT_EQ(nullptr, mask->layer_tree_host());
1121 EXPECT_EQ(nullptr, mask_child->layer_tree_host());
1122
1123 // Same for replacing a replica layer.
1124 parent->SetReplicaLayer(replica_replacement.get());
1125 EXPECT_EQ(nullptr, replica->layer_tree_host());
1126 EXPECT_EQ(nullptr, replica_child->layer_tree_host());
1127
1128 // Test over, cleanup time.
1129 layer_tree_host->SetRootLayer(nullptr);
1130 }
1131
1132 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1133 scoped_refptr<Layer> root = Layer::Create();
1134 scoped_refptr<Layer> child = Layer::Create();
1135 root->AddChild(child);
1136 LayerTreeHostFactory factory;
1137 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1138 layer_tree_host->SetRootLayer(root);
1139 }
1140
1141 static bool AddTestAnimation(Layer* layer) {
1142 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1143 KeyframedFloatAnimationCurve::Create();
1144 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f, nullptr));
1145 curve->AddKeyframe(
1146 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f, nullptr));
1147 scoped_ptr<Animation> animation =
1148 Animation::Create(curve.Pass(), 0, 0, Animation::OPACITY);
1149
1150 return layer->AddAnimation(animation.Pass());
1151 }
1152
1153 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1154 scoped_refptr<Layer> layer = Layer::Create();
1155
1156 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1157 // animation should not be accepted.
1158 EXPECT_FALSE(AddTestAnimation(layer.get()));
1159
1160 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1161 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1162
1163 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1164 EXPECT_TRUE(AddTestAnimation(layer.get()));
1165
1166 LayerTreeSettings settings;
1167 settings.accelerated_animation_enabled = false;
1168 LayerTreeHostFactory factory;
1169 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1170 layer_tree_host->SetRootLayer(layer);
1171 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1172
1173 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1174 // animation should be rejected.
1175 EXPECT_FALSE(AddTestAnimation(layer.get()));
1176 }
1177
1178 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1179 LayerTreeHostFactory factory;
1180 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1181
1182 scoped_refptr<Layer> layer = Layer::Create();
1183 layer_tree_host->SetRootLayer(layer);
1184
1185 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1186 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1187 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1188 layer->SetContentsOpaque(!!contents_opaque);
1189 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1190 : SK_ColorTRANSPARENT);
1191 layer_tree_host->set_background_color(
1192 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1193
1194 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1195 if (contents_opaque) {
1196 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1197 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1198 << host_opaque << "\n";
1199 } else {
1200 EXPECT_NE(SkColorGetA(safe_color), 255u)
1201 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1202 << host_opaque << "\n";
1203 }
1204 }
1205 }
1206 }
1207 }
1208
1209 class DrawsContentChangeLayer : public Layer {
1210 public:
1211 static scoped_refptr<DrawsContentChangeLayer> Create() {
1212 return make_scoped_refptr(new DrawsContentChangeLayer());
1213 }
1214
1215 void SetLayerTreeHost(LayerTreeHost* host) override {
1216 Layer::SetLayerTreeHost(host);
1217 SetFakeDrawsContent(!fake_draws_content_);
1218 }
1219
1220 bool HasDrawableContent() const override {
1221 return fake_draws_content_ && Layer::HasDrawableContent();
1222 }
1223
1224 void SetFakeDrawsContent(bool fake_draws_content) {
1225 fake_draws_content_ = fake_draws_content;
1226 UpdateDrawsContent(HasDrawableContent());
1227 }
1228
1229 private:
1230 DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1231 ~DrawsContentChangeLayer() override {}
1232
1233 bool fake_draws_content_;
1234 };
1235
1236 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1237 scoped_refptr<Layer> root_layer = Layer::Create();
1238 scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1239 DrawsContentChangeLayer::Create();
1240 scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1241 DrawsContentChangeLayer::Create();
1242 root_layer->SetIsDrawable(true);
1243 becomes_not_draws_content->SetIsDrawable(true);
1244 becomes_not_draws_content->SetFakeDrawsContent(true);
1245 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1246 root_layer->AddChild(becomes_not_draws_content);
1247 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1248
1249 becomes_draws_content->SetIsDrawable(true);
1250 root_layer->AddChild(becomes_draws_content);
1251 EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
1252 }
1253
1254 } // namespace
1255 } // namespace cc
OLDNEW
« no previous file with comments | « cc/layers/layer_position_constraint_unittest.cc ('k') | cc/layers/layer_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698