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.
5 #include "cc/layers/layer.h"
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/trees/layer_tree_host.h"
20 #include "cc/trees/single_thread_proxy.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/transform.h"
25 using ::testing::AnyNumber;
26 using ::testing::AtLeast;
27 using ::testing::Mock;
28 using ::testing::StrictMock;
31 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
33 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
35 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
41 class MockLayerTreeHost : public LayerTreeHost {
43 explicit MockLayerTreeHost(FakeLayerTreeHostClient* client)
44 : LayerTreeHost(client, nullptr, nullptr, LayerTreeSettings()) {
45 InitializeSingleThreaded(client, base::MessageLoopProxy::current());
48 MOCK_METHOD0(SetNeedsCommit, void());
49 MOCK_METHOD0(SetNeedsUpdateLayers, void());
50 MOCK_METHOD0(SetNeedsFullTreeSync, void());
53 class MockLayerPainter : public LayerPainter {
55 void Paint(SkCanvas* canvas, const gfx::Rect& content_rect) override {}
58 class LayerTest : public testing::Test {
61 : host_impl_(&proxy_, &shared_bitmap_manager_),
62 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
65 virtual void SetUp() override {
66 layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_));
69 virtual void TearDown() override {
70 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
71 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
76 grand_child1_ = nullptr;
77 grand_child2_ = nullptr;
78 grand_child3_ = nullptr;
80 layer_tree_host_->SetRootLayer(nullptr);
81 layer_tree_host_ = nullptr;
84 void VerifyTestTreeInitialState() const {
85 ASSERT_EQ(3U, parent_->children().size());
86 EXPECT_EQ(child1_, parent_->children()[0]);
87 EXPECT_EQ(child2_, parent_->children()[1]);
88 EXPECT_EQ(child3_, parent_->children()[2]);
89 EXPECT_EQ(parent_.get(), child1_->parent());
90 EXPECT_EQ(parent_.get(), child2_->parent());
91 EXPECT_EQ(parent_.get(), child3_->parent());
93 ASSERT_EQ(2U, child1_->children().size());
94 EXPECT_EQ(grand_child1_, child1_->children()[0]);
95 EXPECT_EQ(grand_child2_, child1_->children()[1]);
96 EXPECT_EQ(child1_.get(), grand_child1_->parent());
97 EXPECT_EQ(child1_.get(), grand_child2_->parent());
99 ASSERT_EQ(1U, child2_->children().size());
100 EXPECT_EQ(grand_child3_, child2_->children()[0]);
101 EXPECT_EQ(child2_.get(), grand_child3_->parent());
103 ASSERT_EQ(0U, child3_->children().size());
106 void CreateSimpleTestTree() {
107 parent_ = Layer::Create();
108 child1_ = Layer::Create();
109 child2_ = Layer::Create();
110 child3_ = Layer::Create();
111 grand_child1_ = Layer::Create();
112 grand_child2_ = Layer::Create();
113 grand_child3_ = Layer::Create();
115 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
116 layer_tree_host_->SetRootLayer(parent_);
118 parent_->AddChild(child1_);
119 parent_->AddChild(child2_);
120 parent_->AddChild(child3_);
121 child1_->AddChild(grand_child1_);
122 child1_->AddChild(grand_child2_);
123 child2_->AddChild(grand_child3_);
125 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
127 VerifyTestTreeInitialState();
130 FakeImplProxy proxy_;
131 TestSharedBitmapManager shared_bitmap_manager_;
132 FakeLayerTreeHostImpl host_impl_;
134 FakeLayerTreeHostClient fake_client_;
135 scoped_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
136 scoped_refptr<Layer> parent_;
137 scoped_refptr<Layer> child1_;
138 scoped_refptr<Layer> child2_;
139 scoped_refptr<Layer> child3_;
140 scoped_refptr<Layer> grand_child1_;
141 scoped_refptr<Layer> grand_child2_;
142 scoped_refptr<Layer> grand_child3_;
145 TEST_F(LayerTest, BasicCreateAndDestroy) {
146 scoped_refptr<Layer> test_layer = Layer::Create();
147 ASSERT_TRUE(test_layer.get());
149 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
150 test_layer->SetLayerTreeHost(layer_tree_host_.get());
151 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
153 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
154 test_layer->SetLayerTreeHost(nullptr);
157 TEST_F(LayerTest, AddAndRemoveChild) {
158 scoped_refptr<Layer> parent = Layer::Create();
159 scoped_refptr<Layer> child = Layer::Create();
161 // Upon creation, layers should not have children or parent.
162 ASSERT_EQ(0U, parent->children().size());
163 EXPECT_FALSE(child->parent());
165 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
166 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
168 ASSERT_EQ(1U, parent->children().size());
169 EXPECT_EQ(child.get(), parent->children()[0].get());
170 EXPECT_EQ(parent.get(), child->parent());
171 EXPECT_EQ(parent.get(), child->RootLayer());
173 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
176 TEST_F(LayerTest, AddSameChildTwice) {
177 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
179 scoped_refptr<Layer> parent = Layer::Create();
180 scoped_refptr<Layer> child = Layer::Create();
182 layer_tree_host_->SetRootLayer(parent);
184 ASSERT_EQ(0u, parent->children().size());
186 parent->AddChild(child);
187 ASSERT_EQ(1u, parent->children().size());
188 EXPECT_EQ(parent.get(), child->parent());
190 parent->AddChild(child);
191 ASSERT_EQ(1u, parent->children().size());
192 EXPECT_EQ(parent.get(), child->parent());
195 TEST_F(LayerTest, InsertChild) {
196 scoped_refptr<Layer> parent = Layer::Create();
197 scoped_refptr<Layer> child1 = Layer::Create();
198 scoped_refptr<Layer> child2 = Layer::Create();
199 scoped_refptr<Layer> child3 = Layer::Create();
200 scoped_refptr<Layer> child4 = Layer::Create();
202 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
204 ASSERT_EQ(0U, parent->children().size());
206 // Case 1: inserting to empty list.
207 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
208 ASSERT_EQ(1U, parent->children().size());
209 EXPECT_EQ(child3, parent->children()[0]);
210 EXPECT_EQ(parent.get(), child3->parent());
212 // Case 2: inserting to beginning of list
213 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
214 ASSERT_EQ(2U, parent->children().size());
215 EXPECT_EQ(child1, parent->children()[0]);
216 EXPECT_EQ(child3, parent->children()[1]);
217 EXPECT_EQ(parent.get(), child1->parent());
219 // Case 3: inserting to middle of list
220 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
221 ASSERT_EQ(3U, parent->children().size());
222 EXPECT_EQ(child1, parent->children()[0]);
223 EXPECT_EQ(child2, parent->children()[1]);
224 EXPECT_EQ(child3, parent->children()[2]);
225 EXPECT_EQ(parent.get(), child2->parent());
227 // Case 4: inserting to end of list
228 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
230 ASSERT_EQ(4U, parent->children().size());
231 EXPECT_EQ(child1, parent->children()[0]);
232 EXPECT_EQ(child2, parent->children()[1]);
233 EXPECT_EQ(child3, parent->children()[2]);
234 EXPECT_EQ(child4, parent->children()[3]);
235 EXPECT_EQ(parent.get(), child4->parent());
237 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
240 TEST_F(LayerTest, InsertChildPastEndOfList) {
241 scoped_refptr<Layer> parent = Layer::Create();
242 scoped_refptr<Layer> child1 = Layer::Create();
243 scoped_refptr<Layer> child2 = Layer::Create();
245 ASSERT_EQ(0U, parent->children().size());
247 // insert to an out-of-bounds index
248 parent->InsertChild(child1, 53);
250 ASSERT_EQ(1U, parent->children().size());
251 EXPECT_EQ(child1, parent->children()[0]);
253 // insert another child to out-of-bounds, when list is not already empty.
254 parent->InsertChild(child2, 2459);
256 ASSERT_EQ(2U, parent->children().size());
257 EXPECT_EQ(child1, parent->children()[0]);
258 EXPECT_EQ(child2, parent->children()[1]);
261 TEST_F(LayerTest, InsertSameChildTwice) {
262 scoped_refptr<Layer> parent = Layer::Create();
263 scoped_refptr<Layer> child1 = Layer::Create();
264 scoped_refptr<Layer> child2 = Layer::Create();
266 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
268 ASSERT_EQ(0U, parent->children().size());
270 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
271 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
273 ASSERT_EQ(2U, parent->children().size());
274 EXPECT_EQ(child1, parent->children()[0]);
275 EXPECT_EQ(child2, parent->children()[1]);
277 // Inserting the same child again should cause the child to be removed and
278 // re-inserted at the new location.
279 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
281 // child1 should now be at the end of the list.
282 ASSERT_EQ(2U, parent->children().size());
283 EXPECT_EQ(child2, parent->children()[0]);
284 EXPECT_EQ(child1, parent->children()[1]);
286 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
289 TEST_F(LayerTest, ReplaceChildWithNewChild) {
290 CreateSimpleTestTree();
291 scoped_refptr<Layer> child4 = Layer::Create();
293 EXPECT_FALSE(child4->parent());
295 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
296 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
297 EXPECT_FALSE(parent_->NeedsDisplayForTesting());
298 EXPECT_FALSE(child1_->NeedsDisplayForTesting());
299 EXPECT_FALSE(child2_->NeedsDisplayForTesting());
300 EXPECT_FALSE(child3_->NeedsDisplayForTesting());
301 EXPECT_FALSE(child4->NeedsDisplayForTesting());
303 ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
304 EXPECT_EQ(child1_, parent_->children()[0]);
305 EXPECT_EQ(child4, parent_->children()[1]);
306 EXPECT_EQ(child3_, parent_->children()[2]);
307 EXPECT_EQ(parent_.get(), child4->parent());
309 EXPECT_FALSE(child2_->parent());
312 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
313 CreateSimpleTestTree();
315 // create another simple tree with test_layer and child4.
316 scoped_refptr<Layer> test_layer = Layer::Create();
317 scoped_refptr<Layer> child4 = Layer::Create();
318 test_layer->AddChild(child4);
319 ASSERT_EQ(1U, test_layer->children().size());
320 EXPECT_EQ(child4, test_layer->children()[0]);
321 EXPECT_EQ(test_layer.get(), child4->parent());
323 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
324 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
326 ASSERT_EQ(3U, parent_->children().size());
327 EXPECT_EQ(child1_, parent_->children()[0]);
328 EXPECT_EQ(child4, parent_->children()[1]);
329 EXPECT_EQ(child3_, parent_->children()[2]);
330 EXPECT_EQ(parent_.get(), child4->parent());
332 // test_layer should no longer have child4,
333 // and child2 should no longer have a parent.
334 ASSERT_EQ(0U, test_layer->children().size());
335 EXPECT_FALSE(child2_->parent());
338 TEST_F(LayerTest, DeleteRemovedScrollParent) {
339 scoped_refptr<Layer> parent = Layer::Create();
340 scoped_refptr<Layer> child1 = Layer::Create();
341 scoped_refptr<Layer> child2 = Layer::Create();
343 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
345 ASSERT_EQ(0U, parent->children().size());
347 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
348 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
350 ASSERT_EQ(2U, parent->children().size());
351 EXPECT_EQ(child1, parent->children()[0]);
352 EXPECT_EQ(child2, parent->children()[1]);
354 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
356 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
358 child1->reset_needs_push_properties_for_testing();
360 EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
362 EXPECT_TRUE(child1->needs_push_properties());
364 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
367 TEST_F(LayerTest, DeleteRemovedScrollChild) {
368 scoped_refptr<Layer> parent = Layer::Create();
369 scoped_refptr<Layer> child1 = Layer::Create();
370 scoped_refptr<Layer> child2 = Layer::Create();
372 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
374 ASSERT_EQ(0U, parent->children().size());
376 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
377 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
379 ASSERT_EQ(2U, parent->children().size());
380 EXPECT_EQ(child1, parent->children()[0]);
381 EXPECT_EQ(child2, parent->children()[1]);
383 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
385 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
387 child2->reset_needs_push_properties_for_testing();
389 EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
391 EXPECT_TRUE(child2->needs_push_properties());
393 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
396 TEST_F(LayerTest, ReplaceChildWithSameChild) {
397 CreateSimpleTestTree();
399 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
401 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
402 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
403 parent_->ReplaceChild(child2_.get(), child2_);
405 VerifyTestTreeInitialState();
408 TEST_F(LayerTest, RemoveAllChildren) {
409 CreateSimpleTestTree();
411 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
413 ASSERT_EQ(0U, parent_->children().size());
414 EXPECT_FALSE(child1_->parent());
415 EXPECT_FALSE(child2_->parent());
416 EXPECT_FALSE(child3_->parent());
419 TEST_F(LayerTest, SetChildren) {
420 scoped_refptr<Layer> old_parent = Layer::Create();
421 scoped_refptr<Layer> new_parent = Layer::Create();
423 scoped_refptr<Layer> child1 = Layer::Create();
424 scoped_refptr<Layer> child2 = Layer::Create();
426 LayerList new_children;
427 new_children.push_back(child1);
428 new_children.push_back(child2);
430 // Set up and verify initial test conditions: child1 has a parent, child2 has
432 old_parent->AddChild(child1);
433 ASSERT_EQ(0U, new_parent->children().size());
434 EXPECT_EQ(old_parent.get(), child1->parent());
435 EXPECT_FALSE(child2->parent());
437 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
438 1, layer_tree_host_->SetRootLayer(new_parent));
440 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
441 AtLeast(1), new_parent->SetChildren(new_children));
443 ASSERT_EQ(2U, new_parent->children().size());
444 EXPECT_EQ(new_parent.get(), child1->parent());
445 EXPECT_EQ(new_parent.get(), child2->parent());
447 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
450 TEST_F(LayerTest, HasAncestor) {
451 scoped_refptr<Layer> parent = Layer::Create();
452 EXPECT_FALSE(parent->HasAncestor(parent.get()));
454 scoped_refptr<Layer> child = Layer::Create();
455 parent->AddChild(child);
457 EXPECT_FALSE(child->HasAncestor(child.get()));
458 EXPECT_TRUE(child->HasAncestor(parent.get()));
459 EXPECT_FALSE(parent->HasAncestor(child.get()));
461 scoped_refptr<Layer> child_child = Layer::Create();
462 child->AddChild(child_child);
464 EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
465 EXPECT_TRUE(child_child->HasAncestor(parent.get()));
466 EXPECT_TRUE(child_child->HasAncestor(child.get()));
467 EXPECT_FALSE(parent->HasAncestor(child.get()));
468 EXPECT_FALSE(parent->HasAncestor(child_child.get()));
471 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
472 CreateSimpleTestTree();
474 // For this test we don't care about SetNeedsFullTreeSync calls.
475 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
477 scoped_refptr<Layer> child4 = Layer::Create();
479 EXPECT_EQ(parent_.get(), parent_->RootLayer());
480 EXPECT_EQ(parent_.get(), child1_->RootLayer());
481 EXPECT_EQ(parent_.get(), child2_->RootLayer());
482 EXPECT_EQ(parent_.get(), child3_->RootLayer());
483 EXPECT_EQ(child4.get(), child4->RootLayer());
484 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
485 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
486 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
488 child1_->RemoveFromParent();
490 // |child1| and its children, grand_child1 and grand_child2 are now on a
492 EXPECT_EQ(parent_.get(), parent_->RootLayer());
493 EXPECT_EQ(child1_.get(), child1_->RootLayer());
494 EXPECT_EQ(parent_.get(), child2_->RootLayer());
495 EXPECT_EQ(parent_.get(), child3_->RootLayer());
496 EXPECT_EQ(child4.get(), child4->RootLayer());
497 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
498 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
499 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
501 grand_child3_->AddChild(child4);
503 EXPECT_EQ(parent_.get(), parent_->RootLayer());
504 EXPECT_EQ(child1_.get(), child1_->RootLayer());
505 EXPECT_EQ(parent_.get(), child2_->RootLayer());
506 EXPECT_EQ(parent_.get(), child3_->RootLayer());
507 EXPECT_EQ(parent_.get(), child4->RootLayer());
508 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
509 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
510 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
512 child2_->ReplaceChild(grand_child3_.get(), child1_);
514 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
515 // the tree under child2.
516 EXPECT_EQ(parent_.get(), parent_->RootLayer());
517 EXPECT_EQ(parent_.get(), child1_->RootLayer());
518 EXPECT_EQ(parent_.get(), child2_->RootLayer());
519 EXPECT_EQ(parent_.get(), child3_->RootLayer());
520 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
521 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
522 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
523 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
526 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
527 // The semantics for SetNeedsDisplay which are tested here:
528 // 1. sets NeedsDisplay flag appropriately.
529 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
532 scoped_refptr<Layer> test_layer = Layer::Create();
533 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
534 1, layer_tree_host_->SetRootLayer(test_layer));
535 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
537 gfx::Size test_bounds = gfx::Size(501, 508);
539 gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
540 gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
541 gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
543 // Before anything, test_layer should not be dirty.
544 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
546 // This is just initialization, but SetNeedsCommit behavior is verified anyway
547 // to avoid warnings.
548 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
549 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
551 // The real test begins here.
552 test_layer->ResetNeedsDisplayForTesting();
553 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
555 // Case 1: Layer should accept dirty rects that go beyond its bounds.
556 test_layer->ResetNeedsDisplayForTesting();
557 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
558 EXPECT_SET_NEEDS_UPDATE(
559 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
560 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
561 test_layer->ResetNeedsDisplayForTesting();
563 // Case 2: SetNeedsDisplay() without the dirty rect arg.
564 test_layer->ResetNeedsDisplayForTesting();
565 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
566 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
567 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
568 test_layer->ResetNeedsDisplayForTesting();
570 // Case 3: SetNeedsDisplay() with an empty rect.
571 test_layer->ResetNeedsDisplayForTesting();
572 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
573 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
574 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
576 // Case 4: SetNeedsDisplay() with a non-drawable layer
577 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
578 test_layer->ResetNeedsDisplayForTesting();
579 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
580 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
581 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
584 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
585 scoped_refptr<Layer> test_layer = Layer::Create();
586 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
587 1, layer_tree_host_->SetRootLayer(test_layer));
588 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
590 scoped_refptr<Layer> dummy_layer1 = Layer::Create();
591 scoped_refptr<Layer> dummy_layer2 = Layer::Create();
593 // sanity check of initial test condition
594 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
596 // Next, test properties that should call SetNeedsCommit (but not
597 // SetNeedsDisplay). All properties need to be set to new values in order for
598 // SetNeedsCommit to be called.
599 EXPECT_SET_NEEDS_COMMIT(
600 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
601 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
602 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
603 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
604 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
605 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
606 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
607 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
608 // We can use any layer pointer here since we aren't syncing for real.
609 EXPECT_SET_NEEDS_COMMIT(1,
610 test_layer->SetScrollClipLayerId(test_layer->id()));
611 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
612 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
613 gfx::ScrollOffset(10, 10)));
614 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
615 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
616 Region(gfx::Rect(1, 1, 2, 2))));
617 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
618 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
619 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
620 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
621 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
622 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
624 EXPECT_SET_NEEDS_COMMIT(
626 test_layer->SetDrawCheckerboardForMissingTiles(
627 !test_layer->draw_checkerboard_for_missing_tiles()));
628 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
629 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
631 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
632 dummy_layer1.get()));
633 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
634 dummy_layer2.get()));
636 // The above tests should not have caused a change to the needs_display flag.
637 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
639 // As layers are removed from the tree, they will cause a tree sync.
640 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
643 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
644 scoped_refptr<Layer> test_layer = Layer::Create();
645 scoped_ptr<LayerImpl> impl_layer =
646 LayerImpl::Create(host_impl_.active_tree(), 1);
648 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
649 layer_tree_host_->SetRootLayer(test_layer));
651 test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
652 test_layer->PushPropertiesTo(impl_layer.get());
653 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
654 impl_layer->update_rect());
656 // The LayerImpl's update_rect() should be accumulated here, since we did not
657 // do anything to clear it.
658 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
659 test_layer->PushPropertiesTo(impl_layer.get());
660 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
661 impl_layer->update_rect());
663 // If we do clear the LayerImpl side, then the next update_rect() should be
664 // fresh without accumulation.
665 impl_layer->ResetAllChangeTrackingForSubtree();
666 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
667 test_layer->PushPropertiesTo(impl_layer.get());
668 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
669 impl_layer->update_rect());
672 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
673 scoped_refptr<Layer> test_layer = Layer::Create();
674 scoped_ptr<LayerImpl> impl_layer =
675 LayerImpl::Create(host_impl_.active_tree(), 1);
677 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
678 layer_tree_host_->SetRootLayer(test_layer));
680 gfx::Transform transform;
681 transform.Rotate(45.0);
682 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
684 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
686 test_layer->PushPropertiesTo(impl_layer.get());
688 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
691 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
692 scoped_refptr<Layer> test_layer = Layer::Create();
693 scoped_ptr<LayerImpl> impl_layer =
694 LayerImpl::Create(host_impl_.active_tree(), 1);
696 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
697 layer_tree_host_->SetRootLayer(test_layer));
699 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
701 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
703 test_layer->PushPropertiesTo(impl_layer.get());
705 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
709 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
710 scoped_refptr<Layer> test_layer = Layer::Create();
711 scoped_ptr<LayerImpl> impl_layer =
712 LayerImpl::Create(host_impl_.active_tree(), 1);
714 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
715 layer_tree_host_->SetRootLayer(test_layer));
717 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
718 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
721 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
726 gfx::Transform transform;
727 transform.Rotate(45.0);
728 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
730 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
731 test_layer->PushPropertiesTo(impl_layer.get());
732 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
734 impl_layer->ResetAllChangeTrackingForSubtree();
735 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
739 impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)->
740 set_is_impl_only(true);
741 transform.Rotate(45.0);
742 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
744 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
745 test_layer->PushPropertiesTo(impl_layer.get());
746 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
750 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
751 scoped_refptr<Layer> test_layer = Layer::Create();
752 scoped_ptr<LayerImpl> impl_layer =
753 LayerImpl::Create(host_impl_.active_tree(), 1);
755 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
756 layer_tree_host_->SetRootLayer(test_layer));
758 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
759 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
762 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
768 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
770 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
771 test_layer->PushPropertiesTo(impl_layer.get());
772 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
774 impl_layer->ResetAllChangeTrackingForSubtree();
775 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
780 impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)->
781 set_is_impl_only(true);
782 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
784 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
785 test_layer->PushPropertiesTo(impl_layer.get());
786 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
790 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
791 scoped_refptr<Layer> test_layer = Layer::Create();
792 scoped_ptr<LayerImpl> impl_layer =
793 LayerImpl::Create(host_impl_.active_tree(), 1);
795 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
796 layer_tree_host_->SetRootLayer(test_layer));
798 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
799 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
802 AddAnimatedFilterToController(
803 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
805 FilterOperations filters;
806 filters.Append(FilterOperation::CreateBlurFilter(2.f));
807 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
809 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
810 test_layer->PushPropertiesTo(impl_layer.get());
811 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
813 impl_layer->ResetAllChangeTrackingForSubtree();
814 AddAnimatedFilterToController(
815 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
816 impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)->
817 set_is_impl_only(true);
818 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
819 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
821 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
822 test_layer->PushPropertiesTo(impl_layer.get());
823 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
826 TEST_F(LayerTest, MaskAndReplicaHasParent) {
827 scoped_refptr<Layer> parent = Layer::Create();
828 scoped_refptr<Layer> child = Layer::Create();
829 scoped_refptr<Layer> mask = Layer::Create();
830 scoped_refptr<Layer> replica = Layer::Create();
831 scoped_refptr<Layer> replica_mask = Layer::Create();
832 scoped_refptr<Layer> mask_replacement = Layer::Create();
833 scoped_refptr<Layer> replica_replacement = Layer::Create();
834 scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
836 parent->AddChild(child);
837 child->SetMaskLayer(mask.get());
838 child->SetReplicaLayer(replica.get());
839 replica->SetMaskLayer(replica_mask.get());
841 EXPECT_EQ(parent.get(), child->parent());
842 EXPECT_EQ(child.get(), mask->parent());
843 EXPECT_EQ(child.get(), replica->parent());
844 EXPECT_EQ(replica.get(), replica_mask->parent());
846 replica->SetMaskLayer(replica_mask_replacement.get());
847 EXPECT_EQ(nullptr, replica_mask->parent());
848 EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
850 child->SetMaskLayer(mask_replacement.get());
851 EXPECT_EQ(nullptr, mask->parent());
852 EXPECT_EQ(child.get(), mask_replacement->parent());
854 child->SetReplicaLayer(replica_replacement.get());
855 EXPECT_EQ(nullptr, replica->parent());
856 EXPECT_EQ(child.get(), replica_replacement->parent());
858 EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
861 TEST_F(LayerTest, CheckTranformIsInvertible) {
862 scoped_refptr<Layer> layer = Layer::Create();
863 scoped_ptr<LayerImpl> impl_layer =
864 LayerImpl::Create(host_impl_.active_tree(), 1);
865 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
866 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
867 layer_tree_host_->SetRootLayer(layer);
869 EXPECT_TRUE(layer->transform_is_invertible());
871 gfx::Transform singular_transform;
872 singular_transform.Scale3d(
873 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
875 layer->SetTransform(singular_transform);
876 layer->PushPropertiesTo(impl_layer.get());
878 EXPECT_FALSE(layer->transform_is_invertible());
879 EXPECT_FALSE(impl_layer->transform_is_invertible());
881 gfx::Transform rotation_transform;
882 rotation_transform.RotateAboutZAxis(-45.0);
884 layer->SetTransform(rotation_transform);
885 layer->PushPropertiesTo(impl_layer.get());
886 EXPECT_TRUE(layer->transform_is_invertible());
887 EXPECT_TRUE(impl_layer->transform_is_invertible());
889 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
892 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
893 scoped_refptr<Layer> layer = Layer::Create();
894 scoped_ptr<LayerImpl> impl_layer =
895 LayerImpl::Create(host_impl_.active_tree(), 1);
896 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
897 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
898 layer_tree_host_->SetRootLayer(layer);
900 EXPECT_TRUE(layer->transform_is_invertible());
902 gfx::Transform singular_transform;
903 singular_transform.Scale3d(
904 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
906 layer->SetTransform(singular_transform);
907 layer->PushPropertiesTo(impl_layer.get());
909 EXPECT_FALSE(layer->transform_is_invertible());
910 EXPECT_FALSE(impl_layer->transform_is_invertible());
912 gfx::Transform identity_transform;
914 layer->SetTransform(identity_transform);
915 static_cast<LayerAnimationValueObserver*>(layer.get())
916 ->OnTransformAnimated(singular_transform);
917 layer->PushPropertiesTo(impl_layer.get());
918 EXPECT_FALSE(layer->transform_is_invertible());
919 EXPECT_FALSE(impl_layer->transform_is_invertible());
921 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
924 class LayerTreeHostFactory {
926 LayerTreeHostFactory()
927 : client_(FakeLayerTreeHostClient::DIRECT_3D),
928 shared_bitmap_manager_(new TestSharedBitmapManager),
929 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager) {}
931 scoped_ptr<LayerTreeHost> Create() {
932 return LayerTreeHost::CreateSingleThreaded(
935 shared_bitmap_manager_.get(),
936 gpu_memory_buffer_manager_.get(),
938 base::MessageLoopProxy::current()).Pass();
941 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
942 return LayerTreeHost::CreateSingleThreaded(
945 shared_bitmap_manager_.get(),
946 gpu_memory_buffer_manager_.get(),
948 base::MessageLoopProxy::current()).Pass();
952 FakeLayerTreeHostClient client_;
953 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
954 scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
957 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
958 EXPECT_EQ(host, layer->layer_tree_host());
960 for (size_t i = 0; i < layer->children().size(); ++i)
961 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
963 if (layer->mask_layer())
964 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
966 if (layer->replica_layer())
967 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
970 TEST(LayerLayerTreeHostTest, EnteringTree) {
971 scoped_refptr<Layer> parent = Layer::Create();
972 scoped_refptr<Layer> child = Layer::Create();
973 scoped_refptr<Layer> mask = Layer::Create();
974 scoped_refptr<Layer> replica = Layer::Create();
975 scoped_refptr<Layer> replica_mask = Layer::Create();
977 // Set up a detached tree of layers. The host pointer should be nil for these
979 parent->AddChild(child);
980 child->SetMaskLayer(mask.get());
981 child->SetReplicaLayer(replica.get());
982 replica->SetMaskLayer(replica_mask.get());
984 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
986 LayerTreeHostFactory factory;
987 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
988 // Setting the root layer should set the host pointer for all layers in the
990 layer_tree_host->SetRootLayer(parent.get());
992 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
994 // Clearing the root layer should also clear out the host pointers for all
995 // layers in the tree.
996 layer_tree_host->SetRootLayer(nullptr);
998 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
1001 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
1002 scoped_refptr<Layer> parent = Layer::Create();
1003 LayerTreeHostFactory factory;
1004 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1006 layer_tree_host->SetRootLayer(parent.get());
1008 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
1010 // Adding a subtree to a layer already associated with a host should set the
1011 // host pointer on all layers in that subtree.
1012 scoped_refptr<Layer> child = Layer::Create();
1013 scoped_refptr<Layer> grand_child = Layer::Create();
1014 child->AddChild(grand_child);
1016 // Masks, replicas, and replica masks should pick up the new host too.
1017 scoped_refptr<Layer> child_mask = Layer::Create();
1018 child->SetMaskLayer(child_mask.get());
1019 scoped_refptr<Layer> child_replica = Layer::Create();
1020 child->SetReplicaLayer(child_replica.get());
1021 scoped_refptr<Layer> child_replica_mask = Layer::Create();
1022 child_replica->SetMaskLayer(child_replica_mask.get());
1024 parent->AddChild(child);
1025 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1027 layer_tree_host->SetRootLayer(nullptr);
1030 TEST(LayerLayerTreeHostTest, ChangeHost) {
1031 scoped_refptr<Layer> parent = Layer::Create();
1032 scoped_refptr<Layer> child = Layer::Create();
1033 scoped_refptr<Layer> mask = Layer::Create();
1034 scoped_refptr<Layer> replica = Layer::Create();
1035 scoped_refptr<Layer> replica_mask = Layer::Create();
1037 // Same setup as the previous test.
1038 parent->AddChild(child);
1039 child->SetMaskLayer(mask.get());
1040 child->SetReplicaLayer(replica.get());
1041 replica->SetMaskLayer(replica_mask.get());
1043 LayerTreeHostFactory factory;
1044 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1045 first_layer_tree_host->SetRootLayer(parent.get());
1047 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1048 first_layer_tree_host.get());
1050 // Now re-root the tree to a new host (simulating what we do on a context lost
1051 // event). This should update the host pointers for all layers in the tree.
1052 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1053 second_layer_tree_host->SetRootLayer(parent.get());
1055 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1056 second_layer_tree_host.get());
1058 second_layer_tree_host->SetRootLayer(nullptr);
1061 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1062 scoped_refptr<Layer> first_parent = Layer::Create();
1063 scoped_refptr<Layer> first_child = Layer::Create();
1064 scoped_refptr<Layer> second_parent = Layer::Create();
1065 scoped_refptr<Layer> second_child = Layer::Create();
1066 scoped_refptr<Layer> second_grand_child = Layer::Create();
1068 // First put all children under the first parent and set the first host.
1069 first_parent->AddChild(first_child);
1070 second_child->AddChild(second_grand_child);
1071 first_parent->AddChild(second_child);
1073 LayerTreeHostFactory factory;
1074 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1075 first_layer_tree_host->SetRootLayer(first_parent.get());
1077 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1078 first_layer_tree_host.get());
1080 // Now reparent the subtree starting at second_child to a layer in a different
1082 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1083 second_layer_tree_host->SetRootLayer(second_parent.get());
1085 second_parent->AddChild(second_child);
1087 // The moved layer and its children should point to the new host.
1088 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1089 EXPECT_EQ(second_layer_tree_host.get(),
1090 second_grand_child->layer_tree_host());
1092 // Test over, cleanup time.
1093 first_layer_tree_host->SetRootLayer(nullptr);
1094 second_layer_tree_host->SetRootLayer(nullptr);
1097 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1098 scoped_refptr<Layer> parent = Layer::Create();
1099 scoped_refptr<Layer> mask = Layer::Create();
1100 scoped_refptr<Layer> replica = Layer::Create();
1101 scoped_refptr<Layer> mask_child = Layer::Create();
1102 scoped_refptr<Layer> replica_child = Layer::Create();
1103 scoped_refptr<Layer> mask_replacement = Layer::Create();
1104 scoped_refptr<Layer> replica_replacement = Layer::Create();
1106 parent->SetMaskLayer(mask.get());
1107 parent->SetReplicaLayer(replica.get());
1108 mask->AddChild(mask_child);
1109 replica->AddChild(replica_child);
1111 LayerTreeHostFactory factory;
1112 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1113 layer_tree_host->SetRootLayer(parent.get());
1115 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1117 // Replacing the mask should clear out the old mask's subtree's host pointers.
1118 parent->SetMaskLayer(mask_replacement.get());
1119 EXPECT_EQ(nullptr, mask->layer_tree_host());
1120 EXPECT_EQ(nullptr, mask_child->layer_tree_host());
1122 // Same for replacing a replica layer.
1123 parent->SetReplicaLayer(replica_replacement.get());
1124 EXPECT_EQ(nullptr, replica->layer_tree_host());
1125 EXPECT_EQ(nullptr, replica_child->layer_tree_host());
1127 // Test over, cleanup time.
1128 layer_tree_host->SetRootLayer(nullptr);
1131 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1132 scoped_refptr<Layer> root = Layer::Create();
1133 scoped_refptr<Layer> child = Layer::Create();
1134 root->AddChild(child);
1135 LayerTreeHostFactory factory;
1136 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1137 layer_tree_host->SetRootLayer(root);
1140 static bool AddTestAnimation(Layer* layer) {
1141 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1142 KeyframedFloatAnimationCurve::Create();
1143 curve->AddKeyframe(FloatKeyframe::Create(0.0, 0.3f, nullptr));
1144 curve->AddKeyframe(FloatKeyframe::Create(1.0, 0.7f, nullptr));
1145 scoped_ptr<Animation> animation =
1146 Animation::Create(curve.Pass(), 0, 0, Animation::Opacity);
1148 return layer->AddAnimation(animation.Pass());
1151 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1152 scoped_refptr<Layer> layer = Layer::Create();
1154 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1155 // animation should not be accepted.
1156 EXPECT_FALSE(AddTestAnimation(layer.get()));
1158 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1159 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1161 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1162 EXPECT_TRUE(AddTestAnimation(layer.get()));
1164 LayerTreeSettings settings;
1165 settings.accelerated_animation_enabled = false;
1166 LayerTreeHostFactory factory;
1167 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1168 layer_tree_host->SetRootLayer(layer);
1169 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1171 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1172 // animation should be rejected.
1173 EXPECT_FALSE(AddTestAnimation(layer.get()));
1176 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1177 LayerTreeHostFactory factory;
1178 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1180 scoped_refptr<Layer> layer = Layer::Create();
1181 layer_tree_host->SetRootLayer(layer);
1183 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1184 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1185 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1186 layer->SetContentsOpaque(!!contents_opaque);
1187 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1188 : SK_ColorTRANSPARENT);
1189 layer_tree_host->set_background_color(
1190 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1192 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1193 if (contents_opaque) {
1194 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1195 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1196 << host_opaque << "\n";
1198 EXPECT_NE(SkColorGetA(safe_color), 255u)
1199 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1200 << host_opaque << "\n";
1207 class DrawsContentChangeLayer : public Layer {
1209 static scoped_refptr<DrawsContentChangeLayer> Create() {
1210 return make_scoped_refptr(new DrawsContentChangeLayer());
1213 void SetLayerTreeHost(LayerTreeHost* host) override {
1214 Layer::SetLayerTreeHost(host);
1215 SetFakeDrawsContent(!fake_draws_content_);
1218 bool HasDrawableContent() const override {
1219 return fake_draws_content_ && Layer::HasDrawableContent();
1222 void SetFakeDrawsContent(bool fake_draws_content) {
1223 fake_draws_content_ = fake_draws_content;
1224 UpdateDrawsContent(HasDrawableContent());
1228 DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1229 ~DrawsContentChangeLayer() override {}
1231 bool fake_draws_content_;
1234 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1235 scoped_refptr<Layer> root_layer = Layer::Create();
1236 scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1237 DrawsContentChangeLayer::Create();
1238 scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1239 DrawsContentChangeLayer::Create();
1240 root_layer->SetIsDrawable(true);
1241 becomes_not_draws_content->SetIsDrawable(true);
1242 becomes_not_draws_content->SetFakeDrawsContent(true);
1243 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1244 root_layer->AddChild(becomes_not_draws_content);
1245 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1247 becomes_draws_content->SetIsDrawable(true);
1248 root_layer->AddChild(becomes_draws_content);
1249 EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());