Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / cc / layers / layer_unittest.cc
blob460726f55335658ec76239bc5608fe9ea0e9314e
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;
29 using ::testing::_;
31 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
32 do { \
33 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
34 code_to_test; \
35 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
36 } while (false)
38 namespace cc {
39 namespace {
41 class MockLayerTreeHost : public LayerTreeHost {
42 public:
43 explicit MockLayerTreeHost(FakeLayerTreeHostClient* client)
44 : LayerTreeHost(client, nullptr, nullptr, LayerTreeSettings()) {
45 InitializeSingleThreaded(client,
46 base::MessageLoopProxy::current(),
47 nullptr);
50 MOCK_METHOD0(SetNeedsCommit, void());
51 MOCK_METHOD0(SetNeedsUpdateLayers, void());
52 MOCK_METHOD0(SetNeedsFullTreeSync, void());
55 class MockLayerPainter : public LayerPainter {
56 public:
57 void Paint(SkCanvas* canvas, const gfx::Rect& content_rect) override {}
60 class LayerTest : public testing::Test {
61 public:
62 LayerTest()
63 : host_impl_(&proxy_, &shared_bitmap_manager_),
64 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
66 protected:
67 void SetUp() override {
68 layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_));
71 void TearDown() override {
72 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
73 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
74 parent_ = nullptr;
75 child1_ = nullptr;
76 child2_ = nullptr;
77 child3_ = nullptr;
78 grand_child1_ = nullptr;
79 grand_child2_ = nullptr;
80 grand_child3_ = nullptr;
82 layer_tree_host_->SetRootLayer(nullptr);
83 layer_tree_host_ = nullptr;
86 void VerifyTestTreeInitialState() const {
87 ASSERT_EQ(3U, parent_->children().size());
88 EXPECT_EQ(child1_, parent_->children()[0]);
89 EXPECT_EQ(child2_, parent_->children()[1]);
90 EXPECT_EQ(child3_, parent_->children()[2]);
91 EXPECT_EQ(parent_.get(), child1_->parent());
92 EXPECT_EQ(parent_.get(), child2_->parent());
93 EXPECT_EQ(parent_.get(), child3_->parent());
95 ASSERT_EQ(2U, child1_->children().size());
96 EXPECT_EQ(grand_child1_, child1_->children()[0]);
97 EXPECT_EQ(grand_child2_, child1_->children()[1]);
98 EXPECT_EQ(child1_.get(), grand_child1_->parent());
99 EXPECT_EQ(child1_.get(), grand_child2_->parent());
101 ASSERT_EQ(1U, child2_->children().size());
102 EXPECT_EQ(grand_child3_, child2_->children()[0]);
103 EXPECT_EQ(child2_.get(), grand_child3_->parent());
105 ASSERT_EQ(0U, child3_->children().size());
108 void CreateSimpleTestTree() {
109 parent_ = Layer::Create();
110 child1_ = Layer::Create();
111 child2_ = Layer::Create();
112 child3_ = Layer::Create();
113 grand_child1_ = Layer::Create();
114 grand_child2_ = Layer::Create();
115 grand_child3_ = Layer::Create();
117 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
118 layer_tree_host_->SetRootLayer(parent_);
120 parent_->AddChild(child1_);
121 parent_->AddChild(child2_);
122 parent_->AddChild(child3_);
123 child1_->AddChild(grand_child1_);
124 child1_->AddChild(grand_child2_);
125 child2_->AddChild(grand_child3_);
127 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
129 VerifyTestTreeInitialState();
132 FakeImplProxy proxy_;
133 TestSharedBitmapManager shared_bitmap_manager_;
134 FakeLayerTreeHostImpl host_impl_;
136 FakeLayerTreeHostClient fake_client_;
137 scoped_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
138 scoped_refptr<Layer> parent_;
139 scoped_refptr<Layer> child1_;
140 scoped_refptr<Layer> child2_;
141 scoped_refptr<Layer> child3_;
142 scoped_refptr<Layer> grand_child1_;
143 scoped_refptr<Layer> grand_child2_;
144 scoped_refptr<Layer> grand_child3_;
147 TEST_F(LayerTest, BasicCreateAndDestroy) {
148 scoped_refptr<Layer> test_layer = Layer::Create();
149 ASSERT_TRUE(test_layer.get());
151 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
152 test_layer->SetLayerTreeHost(layer_tree_host_.get());
153 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
155 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
156 test_layer->SetLayerTreeHost(nullptr);
159 TEST_F(LayerTest, AddAndRemoveChild) {
160 scoped_refptr<Layer> parent = Layer::Create();
161 scoped_refptr<Layer> child = Layer::Create();
163 // Upon creation, layers should not have children or parent.
164 ASSERT_EQ(0U, parent->children().size());
165 EXPECT_FALSE(child->parent());
167 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
168 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
170 ASSERT_EQ(1U, parent->children().size());
171 EXPECT_EQ(child.get(), parent->children()[0].get());
172 EXPECT_EQ(parent.get(), child->parent());
173 EXPECT_EQ(parent.get(), child->RootLayer());
175 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
178 TEST_F(LayerTest, AddSameChildTwice) {
179 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
181 scoped_refptr<Layer> parent = Layer::Create();
182 scoped_refptr<Layer> child = Layer::Create();
184 layer_tree_host_->SetRootLayer(parent);
186 ASSERT_EQ(0u, parent->children().size());
188 parent->AddChild(child);
189 ASSERT_EQ(1u, parent->children().size());
190 EXPECT_EQ(parent.get(), child->parent());
192 parent->AddChild(child);
193 ASSERT_EQ(1u, parent->children().size());
194 EXPECT_EQ(parent.get(), child->parent());
197 TEST_F(LayerTest, InsertChild) {
198 scoped_refptr<Layer> parent = Layer::Create();
199 scoped_refptr<Layer> child1 = Layer::Create();
200 scoped_refptr<Layer> child2 = Layer::Create();
201 scoped_refptr<Layer> child3 = Layer::Create();
202 scoped_refptr<Layer> child4 = Layer::Create();
204 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
206 ASSERT_EQ(0U, parent->children().size());
208 // Case 1: inserting to empty list.
209 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
210 ASSERT_EQ(1U, parent->children().size());
211 EXPECT_EQ(child3, parent->children()[0]);
212 EXPECT_EQ(parent.get(), child3->parent());
214 // Case 2: inserting to beginning of list
215 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
216 ASSERT_EQ(2U, parent->children().size());
217 EXPECT_EQ(child1, parent->children()[0]);
218 EXPECT_EQ(child3, parent->children()[1]);
219 EXPECT_EQ(parent.get(), child1->parent());
221 // Case 3: inserting to middle of list
222 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
223 ASSERT_EQ(3U, parent->children().size());
224 EXPECT_EQ(child1, parent->children()[0]);
225 EXPECT_EQ(child2, parent->children()[1]);
226 EXPECT_EQ(child3, parent->children()[2]);
227 EXPECT_EQ(parent.get(), child2->parent());
229 // Case 4: inserting to end of list
230 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
232 ASSERT_EQ(4U, parent->children().size());
233 EXPECT_EQ(child1, parent->children()[0]);
234 EXPECT_EQ(child2, parent->children()[1]);
235 EXPECT_EQ(child3, parent->children()[2]);
236 EXPECT_EQ(child4, parent->children()[3]);
237 EXPECT_EQ(parent.get(), child4->parent());
239 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
242 TEST_F(LayerTest, InsertChildPastEndOfList) {
243 scoped_refptr<Layer> parent = Layer::Create();
244 scoped_refptr<Layer> child1 = Layer::Create();
245 scoped_refptr<Layer> child2 = Layer::Create();
247 ASSERT_EQ(0U, parent->children().size());
249 // insert to an out-of-bounds index
250 parent->InsertChild(child1, 53);
252 ASSERT_EQ(1U, parent->children().size());
253 EXPECT_EQ(child1, parent->children()[0]);
255 // insert another child to out-of-bounds, when list is not already empty.
256 parent->InsertChild(child2, 2459);
258 ASSERT_EQ(2U, parent->children().size());
259 EXPECT_EQ(child1, parent->children()[0]);
260 EXPECT_EQ(child2, parent->children()[1]);
263 TEST_F(LayerTest, InsertSameChildTwice) {
264 scoped_refptr<Layer> parent = Layer::Create();
265 scoped_refptr<Layer> child1 = Layer::Create();
266 scoped_refptr<Layer> child2 = Layer::Create();
268 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
270 ASSERT_EQ(0U, parent->children().size());
272 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
273 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
275 ASSERT_EQ(2U, parent->children().size());
276 EXPECT_EQ(child1, parent->children()[0]);
277 EXPECT_EQ(child2, parent->children()[1]);
279 // Inserting the same child again should cause the child to be removed and
280 // re-inserted at the new location.
281 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
283 // child1 should now be at the end of the list.
284 ASSERT_EQ(2U, parent->children().size());
285 EXPECT_EQ(child2, parent->children()[0]);
286 EXPECT_EQ(child1, parent->children()[1]);
288 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
291 TEST_F(LayerTest, ReplaceChildWithNewChild) {
292 CreateSimpleTestTree();
293 scoped_refptr<Layer> child4 = Layer::Create();
295 EXPECT_FALSE(child4->parent());
297 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
298 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
299 EXPECT_FALSE(parent_->NeedsDisplayForTesting());
300 EXPECT_FALSE(child1_->NeedsDisplayForTesting());
301 EXPECT_FALSE(child2_->NeedsDisplayForTesting());
302 EXPECT_FALSE(child3_->NeedsDisplayForTesting());
303 EXPECT_FALSE(child4->NeedsDisplayForTesting());
305 ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
306 EXPECT_EQ(child1_, parent_->children()[0]);
307 EXPECT_EQ(child4, parent_->children()[1]);
308 EXPECT_EQ(child3_, parent_->children()[2]);
309 EXPECT_EQ(parent_.get(), child4->parent());
311 EXPECT_FALSE(child2_->parent());
314 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
315 CreateSimpleTestTree();
317 // create another simple tree with test_layer and child4.
318 scoped_refptr<Layer> test_layer = Layer::Create();
319 scoped_refptr<Layer> child4 = Layer::Create();
320 test_layer->AddChild(child4);
321 ASSERT_EQ(1U, test_layer->children().size());
322 EXPECT_EQ(child4, test_layer->children()[0]);
323 EXPECT_EQ(test_layer.get(), child4->parent());
325 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
326 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
328 ASSERT_EQ(3U, parent_->children().size());
329 EXPECT_EQ(child1_, parent_->children()[0]);
330 EXPECT_EQ(child4, parent_->children()[1]);
331 EXPECT_EQ(child3_, parent_->children()[2]);
332 EXPECT_EQ(parent_.get(), child4->parent());
334 // test_layer should no longer have child4,
335 // and child2 should no longer have a parent.
336 ASSERT_EQ(0U, test_layer->children().size());
337 EXPECT_FALSE(child2_->parent());
340 TEST_F(LayerTest, DeleteRemovedScrollParent) {
341 scoped_refptr<Layer> parent = Layer::Create();
342 scoped_refptr<Layer> child1 = Layer::Create();
343 scoped_refptr<Layer> child2 = Layer::Create();
345 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
347 ASSERT_EQ(0U, parent->children().size());
349 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
350 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
352 ASSERT_EQ(2U, parent->children().size());
353 EXPECT_EQ(child1, parent->children()[0]);
354 EXPECT_EQ(child2, parent->children()[1]);
356 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
358 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
360 child1->reset_needs_push_properties_for_testing();
362 EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
364 EXPECT_TRUE(child1->needs_push_properties());
366 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
369 TEST_F(LayerTest, DeleteRemovedScrollChild) {
370 scoped_refptr<Layer> parent = Layer::Create();
371 scoped_refptr<Layer> child1 = Layer::Create();
372 scoped_refptr<Layer> child2 = Layer::Create();
374 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
376 ASSERT_EQ(0U, parent->children().size());
378 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
379 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
381 ASSERT_EQ(2U, parent->children().size());
382 EXPECT_EQ(child1, parent->children()[0]);
383 EXPECT_EQ(child2, parent->children()[1]);
385 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
387 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
389 child2->reset_needs_push_properties_for_testing();
391 EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
393 EXPECT_TRUE(child2->needs_push_properties());
395 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
398 TEST_F(LayerTest, ReplaceChildWithSameChild) {
399 CreateSimpleTestTree();
401 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
402 // same child.
403 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
404 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
405 parent_->ReplaceChild(child2_.get(), child2_);
407 VerifyTestTreeInitialState();
410 TEST_F(LayerTest, RemoveAllChildren) {
411 CreateSimpleTestTree();
413 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
415 ASSERT_EQ(0U, parent_->children().size());
416 EXPECT_FALSE(child1_->parent());
417 EXPECT_FALSE(child2_->parent());
418 EXPECT_FALSE(child3_->parent());
421 TEST_F(LayerTest, SetChildren) {
422 scoped_refptr<Layer> old_parent = Layer::Create();
423 scoped_refptr<Layer> new_parent = Layer::Create();
425 scoped_refptr<Layer> child1 = Layer::Create();
426 scoped_refptr<Layer> child2 = Layer::Create();
428 LayerList new_children;
429 new_children.push_back(child1);
430 new_children.push_back(child2);
432 // Set up and verify initial test conditions: child1 has a parent, child2 has
433 // no parent.
434 old_parent->AddChild(child1);
435 ASSERT_EQ(0U, new_parent->children().size());
436 EXPECT_EQ(old_parent.get(), child1->parent());
437 EXPECT_FALSE(child2->parent());
439 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
440 1, layer_tree_host_->SetRootLayer(new_parent));
442 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
443 AtLeast(1), new_parent->SetChildren(new_children));
445 ASSERT_EQ(2U, new_parent->children().size());
446 EXPECT_EQ(new_parent.get(), child1->parent());
447 EXPECT_EQ(new_parent.get(), child2->parent());
449 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
452 TEST_F(LayerTest, HasAncestor) {
453 scoped_refptr<Layer> parent = Layer::Create();
454 EXPECT_FALSE(parent->HasAncestor(parent.get()));
456 scoped_refptr<Layer> child = Layer::Create();
457 parent->AddChild(child);
459 EXPECT_FALSE(child->HasAncestor(child.get()));
460 EXPECT_TRUE(child->HasAncestor(parent.get()));
461 EXPECT_FALSE(parent->HasAncestor(child.get()));
463 scoped_refptr<Layer> child_child = Layer::Create();
464 child->AddChild(child_child);
466 EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
467 EXPECT_TRUE(child_child->HasAncestor(parent.get()));
468 EXPECT_TRUE(child_child->HasAncestor(child.get()));
469 EXPECT_FALSE(parent->HasAncestor(child.get()));
470 EXPECT_FALSE(parent->HasAncestor(child_child.get()));
473 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
474 CreateSimpleTestTree();
476 // For this test we don't care about SetNeedsFullTreeSync calls.
477 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
479 scoped_refptr<Layer> child4 = Layer::Create();
481 EXPECT_EQ(parent_.get(), parent_->RootLayer());
482 EXPECT_EQ(parent_.get(), child1_->RootLayer());
483 EXPECT_EQ(parent_.get(), child2_->RootLayer());
484 EXPECT_EQ(parent_.get(), child3_->RootLayer());
485 EXPECT_EQ(child4.get(), child4->RootLayer());
486 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
487 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
488 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
490 child1_->RemoveFromParent();
492 // |child1| and its children, grand_child1 and grand_child2 are now on a
493 // separate subtree.
494 EXPECT_EQ(parent_.get(), parent_->RootLayer());
495 EXPECT_EQ(child1_.get(), child1_->RootLayer());
496 EXPECT_EQ(parent_.get(), child2_->RootLayer());
497 EXPECT_EQ(parent_.get(), child3_->RootLayer());
498 EXPECT_EQ(child4.get(), child4->RootLayer());
499 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
500 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
501 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
503 grand_child3_->AddChild(child4);
505 EXPECT_EQ(parent_.get(), parent_->RootLayer());
506 EXPECT_EQ(child1_.get(), child1_->RootLayer());
507 EXPECT_EQ(parent_.get(), child2_->RootLayer());
508 EXPECT_EQ(parent_.get(), child3_->RootLayer());
509 EXPECT_EQ(parent_.get(), child4->RootLayer());
510 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
511 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
512 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
514 child2_->ReplaceChild(grand_child3_.get(), child1_);
516 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
517 // the tree under child2.
518 EXPECT_EQ(parent_.get(), parent_->RootLayer());
519 EXPECT_EQ(parent_.get(), child1_->RootLayer());
520 EXPECT_EQ(parent_.get(), child2_->RootLayer());
521 EXPECT_EQ(parent_.get(), child3_->RootLayer());
522 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
523 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
524 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
525 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
528 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
529 // The semantics for SetNeedsDisplay which are tested here:
530 // 1. sets NeedsDisplay flag appropriately.
531 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
532 // SetNeedsDisplay.
534 scoped_refptr<Layer> test_layer = Layer::Create();
535 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
536 1, layer_tree_host_->SetRootLayer(test_layer));
537 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
539 gfx::Size test_bounds = gfx::Size(501, 508);
541 gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
542 gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
543 gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
545 // Before anything, test_layer should not be dirty.
546 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
548 // This is just initialization, but SetNeedsCommit behavior is verified anyway
549 // to avoid warnings.
550 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
551 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
553 // The real test begins here.
554 test_layer->ResetNeedsDisplayForTesting();
555 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
557 // Case 1: Layer should accept dirty rects that go beyond its bounds.
558 test_layer->ResetNeedsDisplayForTesting();
559 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
560 EXPECT_SET_NEEDS_UPDATE(
561 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
562 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
563 test_layer->ResetNeedsDisplayForTesting();
565 // Case 2: SetNeedsDisplay() without the dirty rect arg.
566 test_layer->ResetNeedsDisplayForTesting();
567 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
568 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
569 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
570 test_layer->ResetNeedsDisplayForTesting();
572 // Case 3: SetNeedsDisplay() with an empty rect.
573 test_layer->ResetNeedsDisplayForTesting();
574 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
575 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
576 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
578 // Case 4: SetNeedsDisplay() with a non-drawable layer
579 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
580 test_layer->ResetNeedsDisplayForTesting();
581 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
582 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
583 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
586 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
587 scoped_refptr<Layer> test_layer = Layer::Create();
588 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
589 1, layer_tree_host_->SetRootLayer(test_layer));
590 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
592 scoped_refptr<Layer> dummy_layer1 = Layer::Create();
593 scoped_refptr<Layer> dummy_layer2 = Layer::Create();
595 // sanity check of initial test condition
596 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
598 // Next, test properties that should call SetNeedsCommit (but not
599 // SetNeedsDisplay). All properties need to be set to new values in order for
600 // SetNeedsCommit to be called.
601 EXPECT_SET_NEEDS_COMMIT(
602 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
603 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
604 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
605 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
606 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
607 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
608 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
609 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
610 // We can use any layer pointer here since we aren't syncing for real.
611 EXPECT_SET_NEEDS_COMMIT(1,
612 test_layer->SetScrollClipLayerId(test_layer->id()));
613 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
614 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
615 gfx::ScrollOffset(10, 10)));
616 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
617 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
618 Region(gfx::Rect(1, 1, 2, 2))));
619 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
620 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
621 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
622 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
624 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
625 gfx::Rect(10, 10)));
626 EXPECT_SET_NEEDS_COMMIT(
628 test_layer->SetDrawCheckerboardForMissingTiles(
629 !test_layer->draw_checkerboard_for_missing_tiles()));
630 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
631 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
633 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
634 dummy_layer1.get()));
635 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
636 dummy_layer2.get()));
638 // The above tests should not have caused a change to the needs_display flag.
639 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
641 // As layers are removed from the tree, they will cause a tree sync.
642 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
645 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
646 scoped_refptr<Layer> test_layer = Layer::Create();
647 scoped_ptr<LayerImpl> impl_layer =
648 LayerImpl::Create(host_impl_.active_tree(), 1);
650 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
651 layer_tree_host_->SetRootLayer(test_layer));
653 test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
654 test_layer->PushPropertiesTo(impl_layer.get());
655 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
656 impl_layer->update_rect());
658 // The LayerImpl's update_rect() should be accumulated here, since we did not
659 // do anything to clear it.
660 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
661 test_layer->PushPropertiesTo(impl_layer.get());
662 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
663 impl_layer->update_rect());
665 // If we do clear the LayerImpl side, then the next update_rect() should be
666 // fresh without accumulation.
667 impl_layer->ResetAllChangeTrackingForSubtree();
668 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
669 test_layer->PushPropertiesTo(impl_layer.get());
670 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
671 impl_layer->update_rect());
674 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
675 scoped_refptr<Layer> test_layer = Layer::Create();
676 scoped_ptr<LayerImpl> impl_layer =
677 LayerImpl::Create(host_impl_.active_tree(), 1);
679 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
680 layer_tree_host_->SetRootLayer(test_layer));
682 gfx::Transform transform;
683 transform.Rotate(45.0);
684 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
686 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
688 test_layer->PushPropertiesTo(impl_layer.get());
690 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
693 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
694 scoped_refptr<Layer> test_layer = Layer::Create();
695 scoped_ptr<LayerImpl> impl_layer =
696 LayerImpl::Create(host_impl_.active_tree(), 1);
698 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
699 layer_tree_host_->SetRootLayer(test_layer));
701 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
703 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
705 test_layer->PushPropertiesTo(impl_layer.get());
707 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
710 TEST_F(LayerTest,
711 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
712 scoped_refptr<Layer> test_layer = Layer::Create();
713 scoped_ptr<LayerImpl> impl_layer =
714 LayerImpl::Create(host_impl_.active_tree(), 1);
716 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
717 layer_tree_host_->SetRootLayer(test_layer));
719 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
720 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
721 registrar.get());
723 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
724 1.0,
726 100);
728 gfx::Transform transform;
729 transform.Rotate(45.0);
730 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
732 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
733 test_layer->PushPropertiesTo(impl_layer.get());
734 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
736 impl_layer->ResetAllChangeTrackingForSubtree();
737 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
738 1.0,
740 100);
741 impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)->
742 set_is_impl_only(true);
743 transform.Rotate(45.0);
744 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
746 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
747 test_layer->PushPropertiesTo(impl_layer.get());
748 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
751 TEST_F(LayerTest,
752 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
753 scoped_refptr<Layer> test_layer = Layer::Create();
754 scoped_ptr<LayerImpl> impl_layer =
755 LayerImpl::Create(host_impl_.active_tree(), 1);
757 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
758 layer_tree_host_->SetRootLayer(test_layer));
760 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
761 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
762 registrar.get());
764 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
765 1.0,
766 0.3f,
767 0.7f,
768 false);
770 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
772 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
773 test_layer->PushPropertiesTo(impl_layer.get());
774 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
776 impl_layer->ResetAllChangeTrackingForSubtree();
777 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
778 1.0,
779 0.3f,
780 0.7f,
781 false);
782 impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)->
783 set_is_impl_only(true);
784 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
786 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
787 test_layer->PushPropertiesTo(impl_layer.get());
788 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
791 TEST_F(LayerTest,
792 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
793 scoped_refptr<Layer> test_layer = Layer::Create();
794 scoped_ptr<LayerImpl> impl_layer =
795 LayerImpl::Create(host_impl_.active_tree(), 1);
797 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
798 layer_tree_host_->SetRootLayer(test_layer));
800 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
801 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
802 registrar.get());
804 AddAnimatedFilterToController(
805 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
807 FilterOperations filters;
808 filters.Append(FilterOperation::CreateBlurFilter(2.f));
809 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
811 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
812 test_layer->PushPropertiesTo(impl_layer.get());
813 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
815 impl_layer->ResetAllChangeTrackingForSubtree();
816 AddAnimatedFilterToController(
817 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
818 impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)->
819 set_is_impl_only(true);
820 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
821 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
823 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
824 test_layer->PushPropertiesTo(impl_layer.get());
825 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
828 TEST_F(LayerTest, MaskAndReplicaHasParent) {
829 scoped_refptr<Layer> parent = Layer::Create();
830 scoped_refptr<Layer> child = Layer::Create();
831 scoped_refptr<Layer> mask = Layer::Create();
832 scoped_refptr<Layer> replica = Layer::Create();
833 scoped_refptr<Layer> replica_mask = Layer::Create();
834 scoped_refptr<Layer> mask_replacement = Layer::Create();
835 scoped_refptr<Layer> replica_replacement = Layer::Create();
836 scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
838 parent->AddChild(child);
839 child->SetMaskLayer(mask.get());
840 child->SetReplicaLayer(replica.get());
841 replica->SetMaskLayer(replica_mask.get());
843 EXPECT_EQ(parent.get(), child->parent());
844 EXPECT_EQ(child.get(), mask->parent());
845 EXPECT_EQ(child.get(), replica->parent());
846 EXPECT_EQ(replica.get(), replica_mask->parent());
848 replica->SetMaskLayer(replica_mask_replacement.get());
849 EXPECT_EQ(nullptr, replica_mask->parent());
850 EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
852 child->SetMaskLayer(mask_replacement.get());
853 EXPECT_EQ(nullptr, mask->parent());
854 EXPECT_EQ(child.get(), mask_replacement->parent());
856 child->SetReplicaLayer(replica_replacement.get());
857 EXPECT_EQ(nullptr, replica->parent());
858 EXPECT_EQ(child.get(), replica_replacement->parent());
860 EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
863 TEST_F(LayerTest, CheckTranformIsInvertible) {
864 scoped_refptr<Layer> layer = Layer::Create();
865 scoped_ptr<LayerImpl> impl_layer =
866 LayerImpl::Create(host_impl_.active_tree(), 1);
867 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
868 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
869 layer_tree_host_->SetRootLayer(layer);
871 EXPECT_TRUE(layer->transform_is_invertible());
873 gfx::Transform singular_transform;
874 singular_transform.Scale3d(
875 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
877 layer->SetTransform(singular_transform);
878 layer->PushPropertiesTo(impl_layer.get());
880 EXPECT_FALSE(layer->transform_is_invertible());
881 EXPECT_FALSE(impl_layer->transform_is_invertible());
883 gfx::Transform rotation_transform;
884 rotation_transform.RotateAboutZAxis(-45.0);
886 layer->SetTransform(rotation_transform);
887 layer->PushPropertiesTo(impl_layer.get());
888 EXPECT_TRUE(layer->transform_is_invertible());
889 EXPECT_TRUE(impl_layer->transform_is_invertible());
891 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
894 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
895 scoped_refptr<Layer> layer = Layer::Create();
896 scoped_ptr<LayerImpl> impl_layer =
897 LayerImpl::Create(host_impl_.active_tree(), 1);
898 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
899 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
900 layer_tree_host_->SetRootLayer(layer);
902 EXPECT_TRUE(layer->transform_is_invertible());
904 gfx::Transform singular_transform;
905 singular_transform.Scale3d(
906 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
908 layer->SetTransform(singular_transform);
909 layer->PushPropertiesTo(impl_layer.get());
911 EXPECT_FALSE(layer->transform_is_invertible());
912 EXPECT_FALSE(impl_layer->transform_is_invertible());
914 gfx::Transform identity_transform;
916 layer->SetTransform(identity_transform);
917 static_cast<LayerAnimationValueObserver*>(layer.get())
918 ->OnTransformAnimated(singular_transform);
919 layer->PushPropertiesTo(impl_layer.get());
920 EXPECT_FALSE(layer->transform_is_invertible());
921 EXPECT_FALSE(impl_layer->transform_is_invertible());
923 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
926 class LayerTreeHostFactory {
927 public:
928 LayerTreeHostFactory()
929 : client_(FakeLayerTreeHostClient::DIRECT_3D),
930 shared_bitmap_manager_(new TestSharedBitmapManager),
931 gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager) {}
933 scoped_ptr<LayerTreeHost> Create() {
934 return LayerTreeHost::CreateSingleThreaded(
935 &client_,
936 &client_,
937 shared_bitmap_manager_.get(),
938 gpu_memory_buffer_manager_.get(),
939 LayerTreeSettings(),
940 base::MessageLoopProxy::current(),
941 nullptr);
944 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
945 return LayerTreeHost::CreateSingleThreaded(
946 &client_,
947 &client_,
948 shared_bitmap_manager_.get(),
949 gpu_memory_buffer_manager_.get(),
950 settings,
951 base::MessageLoopProxy::current(),
952 nullptr);
955 private:
956 FakeLayerTreeHostClient client_;
957 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
958 scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
961 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
962 EXPECT_EQ(host, layer->layer_tree_host());
964 for (size_t i = 0; i < layer->children().size(); ++i)
965 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
967 if (layer->mask_layer())
968 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
970 if (layer->replica_layer())
971 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
974 TEST(LayerLayerTreeHostTest, EnteringTree) {
975 scoped_refptr<Layer> parent = Layer::Create();
976 scoped_refptr<Layer> child = Layer::Create();
977 scoped_refptr<Layer> mask = Layer::Create();
978 scoped_refptr<Layer> replica = Layer::Create();
979 scoped_refptr<Layer> replica_mask = Layer::Create();
981 // Set up a detached tree of layers. The host pointer should be nil for these
982 // layers.
983 parent->AddChild(child);
984 child->SetMaskLayer(mask.get());
985 child->SetReplicaLayer(replica.get());
986 replica->SetMaskLayer(replica_mask.get());
988 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
990 LayerTreeHostFactory factory;
991 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
992 // Setting the root layer should set the host pointer for all layers in the
993 // tree.
994 layer_tree_host->SetRootLayer(parent.get());
996 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
998 // Clearing the root layer should also clear out the host pointers for all
999 // layers in the tree.
1000 layer_tree_host->SetRootLayer(nullptr);
1002 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
1005 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
1006 scoped_refptr<Layer> parent = Layer::Create();
1007 LayerTreeHostFactory factory;
1008 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1010 layer_tree_host->SetRootLayer(parent.get());
1012 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
1014 // Adding a subtree to a layer already associated with a host should set the
1015 // host pointer on all layers in that subtree.
1016 scoped_refptr<Layer> child = Layer::Create();
1017 scoped_refptr<Layer> grand_child = Layer::Create();
1018 child->AddChild(grand_child);
1020 // Masks, replicas, and replica masks should pick up the new host too.
1021 scoped_refptr<Layer> child_mask = Layer::Create();
1022 child->SetMaskLayer(child_mask.get());
1023 scoped_refptr<Layer> child_replica = Layer::Create();
1024 child->SetReplicaLayer(child_replica.get());
1025 scoped_refptr<Layer> child_replica_mask = Layer::Create();
1026 child_replica->SetMaskLayer(child_replica_mask.get());
1028 parent->AddChild(child);
1029 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1031 layer_tree_host->SetRootLayer(nullptr);
1034 TEST(LayerLayerTreeHostTest, ChangeHost) {
1035 scoped_refptr<Layer> parent = Layer::Create();
1036 scoped_refptr<Layer> child = Layer::Create();
1037 scoped_refptr<Layer> mask = Layer::Create();
1038 scoped_refptr<Layer> replica = Layer::Create();
1039 scoped_refptr<Layer> replica_mask = Layer::Create();
1041 // Same setup as the previous test.
1042 parent->AddChild(child);
1043 child->SetMaskLayer(mask.get());
1044 child->SetReplicaLayer(replica.get());
1045 replica->SetMaskLayer(replica_mask.get());
1047 LayerTreeHostFactory factory;
1048 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1049 first_layer_tree_host->SetRootLayer(parent.get());
1051 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1052 first_layer_tree_host.get());
1054 // Now re-root the tree to a new host (simulating what we do on a context lost
1055 // event). This should update the host pointers for all layers in the tree.
1056 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1057 second_layer_tree_host->SetRootLayer(parent.get());
1059 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1060 second_layer_tree_host.get());
1062 second_layer_tree_host->SetRootLayer(nullptr);
1065 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1066 scoped_refptr<Layer> first_parent = Layer::Create();
1067 scoped_refptr<Layer> first_child = Layer::Create();
1068 scoped_refptr<Layer> second_parent = Layer::Create();
1069 scoped_refptr<Layer> second_child = Layer::Create();
1070 scoped_refptr<Layer> second_grand_child = Layer::Create();
1072 // First put all children under the first parent and set the first host.
1073 first_parent->AddChild(first_child);
1074 second_child->AddChild(second_grand_child);
1075 first_parent->AddChild(second_child);
1077 LayerTreeHostFactory factory;
1078 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1079 first_layer_tree_host->SetRootLayer(first_parent.get());
1081 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1082 first_layer_tree_host.get());
1084 // Now reparent the subtree starting at second_child to a layer in a different
1085 // tree.
1086 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1087 second_layer_tree_host->SetRootLayer(second_parent.get());
1089 second_parent->AddChild(second_child);
1091 // The moved layer and its children should point to the new host.
1092 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1093 EXPECT_EQ(second_layer_tree_host.get(),
1094 second_grand_child->layer_tree_host());
1096 // Test over, cleanup time.
1097 first_layer_tree_host->SetRootLayer(nullptr);
1098 second_layer_tree_host->SetRootLayer(nullptr);
1101 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1102 scoped_refptr<Layer> parent = Layer::Create();
1103 scoped_refptr<Layer> mask = Layer::Create();
1104 scoped_refptr<Layer> replica = Layer::Create();
1105 scoped_refptr<Layer> mask_child = Layer::Create();
1106 scoped_refptr<Layer> replica_child = Layer::Create();
1107 scoped_refptr<Layer> mask_replacement = Layer::Create();
1108 scoped_refptr<Layer> replica_replacement = Layer::Create();
1110 parent->SetMaskLayer(mask.get());
1111 parent->SetReplicaLayer(replica.get());
1112 mask->AddChild(mask_child);
1113 replica->AddChild(replica_child);
1115 LayerTreeHostFactory factory;
1116 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1117 layer_tree_host->SetRootLayer(parent.get());
1119 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1121 // Replacing the mask should clear out the old mask's subtree's host pointers.
1122 parent->SetMaskLayer(mask_replacement.get());
1123 EXPECT_EQ(nullptr, mask->layer_tree_host());
1124 EXPECT_EQ(nullptr, mask_child->layer_tree_host());
1126 // Same for replacing a replica layer.
1127 parent->SetReplicaLayer(replica_replacement.get());
1128 EXPECT_EQ(nullptr, replica->layer_tree_host());
1129 EXPECT_EQ(nullptr, replica_child->layer_tree_host());
1131 // Test over, cleanup time.
1132 layer_tree_host->SetRootLayer(nullptr);
1135 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1136 scoped_refptr<Layer> root = Layer::Create();
1137 scoped_refptr<Layer> child = Layer::Create();
1138 root->AddChild(child);
1139 LayerTreeHostFactory factory;
1140 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1141 layer_tree_host->SetRootLayer(root);
1144 static bool AddTestAnimation(Layer* layer) {
1145 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1146 KeyframedFloatAnimationCurve::Create();
1147 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f, nullptr));
1148 curve->AddKeyframe(
1149 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f, nullptr));
1150 scoped_ptr<Animation> animation =
1151 Animation::Create(curve.Pass(), 0, 0, Animation::Opacity);
1153 return layer->AddAnimation(animation.Pass());
1156 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1157 scoped_refptr<Layer> layer = Layer::Create();
1159 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1160 // animation should not be accepted.
1161 EXPECT_FALSE(AddTestAnimation(layer.get()));
1163 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1164 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1166 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1167 EXPECT_TRUE(AddTestAnimation(layer.get()));
1169 LayerTreeSettings settings;
1170 settings.accelerated_animation_enabled = false;
1171 LayerTreeHostFactory factory;
1172 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1173 layer_tree_host->SetRootLayer(layer);
1174 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1176 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1177 // animation should be rejected.
1178 EXPECT_FALSE(AddTestAnimation(layer.get()));
1181 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1182 LayerTreeHostFactory factory;
1183 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1185 scoped_refptr<Layer> layer = Layer::Create();
1186 layer_tree_host->SetRootLayer(layer);
1188 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1189 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1190 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1191 layer->SetContentsOpaque(!!contents_opaque);
1192 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1193 : SK_ColorTRANSPARENT);
1194 layer_tree_host->set_background_color(
1195 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1197 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1198 if (contents_opaque) {
1199 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1200 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1201 << host_opaque << "\n";
1202 } else {
1203 EXPECT_NE(SkColorGetA(safe_color), 255u)
1204 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1205 << host_opaque << "\n";
1212 class DrawsContentChangeLayer : public Layer {
1213 public:
1214 static scoped_refptr<DrawsContentChangeLayer> Create() {
1215 return make_scoped_refptr(new DrawsContentChangeLayer());
1218 void SetLayerTreeHost(LayerTreeHost* host) override {
1219 Layer::SetLayerTreeHost(host);
1220 SetFakeDrawsContent(!fake_draws_content_);
1223 bool HasDrawableContent() const override {
1224 return fake_draws_content_ && Layer::HasDrawableContent();
1227 void SetFakeDrawsContent(bool fake_draws_content) {
1228 fake_draws_content_ = fake_draws_content;
1229 UpdateDrawsContent(HasDrawableContent());
1232 private:
1233 DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1234 ~DrawsContentChangeLayer() override {}
1236 bool fake_draws_content_;
1239 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1240 scoped_refptr<Layer> root_layer = Layer::Create();
1241 scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1242 DrawsContentChangeLayer::Create();
1243 scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1244 DrawsContentChangeLayer::Create();
1245 root_layer->SetIsDrawable(true);
1246 becomes_not_draws_content->SetIsDrawable(true);
1247 becomes_not_draws_content->SetFakeDrawsContent(true);
1248 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1249 root_layer->AddChild(becomes_not_draws_content);
1250 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1252 becomes_draws_content->SetIsDrawable(true);
1253 root_layer->AddChild(becomes_draws_content);
1254 EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
1257 } // namespace
1258 } // namespace cc