Popular sites on the NTP: Favicon improvements
[chromium-blink-merge.git] / cc / layers / layer_unittest.cc
blob10b1d147eafab228cf20f981c80020b66e96a9f3
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 "base/thread_task_runner_handle.h"
8 #include "cc/animation/keyframed_animation_curve.h"
9 #include "cc/base/math_util.h"
10 #include "cc/layers/layer_impl.h"
11 #include "cc/output/copy_output_request.h"
12 #include "cc/output/copy_output_result.h"
13 #include "cc/test/animation_test_common.h"
14 #include "cc/test/fake_impl_proxy.h"
15 #include "cc/test/fake_layer_tree_host_client.h"
16 #include "cc/test/fake_layer_tree_host_impl.h"
17 #include "cc/test/geometry_test_utils.h"
18 #include "cc/test/layer_test_common.h"
19 #include "cc/test/test_gpu_memory_buffer_manager.h"
20 #include "cc/test/test_shared_bitmap_manager.h"
21 #include "cc/test/test_task_graph_runner.h"
22 #include "cc/trees/layer_tree_host.h"
23 #include "cc/trees/single_thread_proxy.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "ui/gfx/transform.h"
28 using ::testing::AnyNumber;
29 using ::testing::AtLeast;
30 using ::testing::Mock;
31 using ::testing::StrictMock;
32 using ::testing::_;
34 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
35 do { \
36 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
37 code_to_test; \
38 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
39 } while (false)
41 namespace cc {
42 namespace {
44 class MockLayerTreeHost : public LayerTreeHost {
45 public:
46 MockLayerTreeHost(LayerTreeHostSingleThreadClient* single_thread_client,
47 LayerTreeHost::InitParams* params)
48 : LayerTreeHost(params) {
49 InitializeSingleThreaded(single_thread_client,
50 base::ThreadTaskRunnerHandle::Get(), nullptr);
53 MOCK_METHOD0(SetNeedsCommit, void());
54 MOCK_METHOD0(SetNeedsUpdateLayers, void());
55 MOCK_METHOD0(SetNeedsFullTreeSync, void());
58 class LayerTest : public testing::Test {
59 public:
60 LayerTest()
61 : host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
62 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
64 protected:
65 void SetUp() override {
66 LayerTreeHost::InitParams params;
67 LayerTreeSettings settings;
68 params.client = &fake_client_;
69 params.settings = &settings;
70 params.task_graph_runner = &task_graph_runner_;
71 layer_tree_host_.reset(
72 new StrictMock<MockLayerTreeHost>(&fake_client_, &params));
75 void TearDown() override {
76 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
77 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
78 parent_ = nullptr;
79 child1_ = nullptr;
80 child2_ = nullptr;
81 child3_ = nullptr;
82 grand_child1_ = nullptr;
83 grand_child2_ = nullptr;
84 grand_child3_ = nullptr;
86 layer_tree_host_->SetRootLayer(nullptr);
87 layer_tree_host_ = nullptr;
90 void VerifyTestTreeInitialState() const {
91 ASSERT_EQ(3U, parent_->children().size());
92 EXPECT_EQ(child1_, parent_->children()[0]);
93 EXPECT_EQ(child2_, parent_->children()[1]);
94 EXPECT_EQ(child3_, parent_->children()[2]);
95 EXPECT_EQ(parent_.get(), child1_->parent());
96 EXPECT_EQ(parent_.get(), child2_->parent());
97 EXPECT_EQ(parent_.get(), child3_->parent());
99 ASSERT_EQ(2U, child1_->children().size());
100 EXPECT_EQ(grand_child1_, child1_->children()[0]);
101 EXPECT_EQ(grand_child2_, child1_->children()[1]);
102 EXPECT_EQ(child1_.get(), grand_child1_->parent());
103 EXPECT_EQ(child1_.get(), grand_child2_->parent());
105 ASSERT_EQ(1U, child2_->children().size());
106 EXPECT_EQ(grand_child3_, child2_->children()[0]);
107 EXPECT_EQ(child2_.get(), grand_child3_->parent());
109 ASSERT_EQ(0U, child3_->children().size());
112 void CreateSimpleTestTree() {
113 parent_ = Layer::Create(layer_settings_);
114 child1_ = Layer::Create(layer_settings_);
115 child2_ = Layer::Create(layer_settings_);
116 child3_ = Layer::Create(layer_settings_);
117 grand_child1_ = Layer::Create(layer_settings_);
118 grand_child2_ = Layer::Create(layer_settings_);
119 grand_child3_ = Layer::Create(layer_settings_);
121 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
122 layer_tree_host_->SetRootLayer(parent_);
124 parent_->AddChild(child1_);
125 parent_->AddChild(child2_);
126 parent_->AddChild(child3_);
127 child1_->AddChild(grand_child1_);
128 child1_->AddChild(grand_child2_);
129 child2_->AddChild(grand_child3_);
131 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
133 VerifyTestTreeInitialState();
136 FakeImplProxy proxy_;
137 TestSharedBitmapManager shared_bitmap_manager_;
138 TestTaskGraphRunner task_graph_runner_;
139 FakeLayerTreeHostImpl host_impl_;
141 FakeLayerTreeHostClient fake_client_;
142 scoped_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
143 scoped_refptr<Layer> parent_;
144 scoped_refptr<Layer> child1_;
145 scoped_refptr<Layer> child2_;
146 scoped_refptr<Layer> child3_;
147 scoped_refptr<Layer> grand_child1_;
148 scoped_refptr<Layer> grand_child2_;
149 scoped_refptr<Layer> grand_child3_;
151 LayerSettings layer_settings_;
154 TEST_F(LayerTest, BasicCreateAndDestroy) {
155 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
156 ASSERT_TRUE(test_layer.get());
158 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
159 test_layer->SetLayerTreeHost(layer_tree_host_.get());
160 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
162 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
163 test_layer->SetLayerTreeHost(nullptr);
166 TEST_F(LayerTest, AddAndRemoveChild) {
167 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
168 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
170 // Upon creation, layers should not have children or parent.
171 ASSERT_EQ(0U, parent->children().size());
172 EXPECT_FALSE(child->parent());
174 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
175 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
177 ASSERT_EQ(1U, parent->children().size());
178 EXPECT_EQ(child.get(), parent->children()[0].get());
179 EXPECT_EQ(parent.get(), child->parent());
180 EXPECT_EQ(parent.get(), child->RootLayer());
182 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
185 TEST_F(LayerTest, AddSameChildTwice) {
186 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
188 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
189 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
191 layer_tree_host_->SetRootLayer(parent);
193 ASSERT_EQ(0u, parent->children().size());
195 parent->AddChild(child);
196 ASSERT_EQ(1u, parent->children().size());
197 EXPECT_EQ(parent.get(), child->parent());
199 parent->AddChild(child);
200 ASSERT_EQ(1u, parent->children().size());
201 EXPECT_EQ(parent.get(), child->parent());
204 TEST_F(LayerTest, InsertChild) {
205 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
206 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
207 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
208 scoped_refptr<Layer> child3 = Layer::Create(layer_settings_);
209 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
211 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
213 ASSERT_EQ(0U, parent->children().size());
215 // Case 1: inserting to empty list.
216 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
217 ASSERT_EQ(1U, parent->children().size());
218 EXPECT_EQ(child3, parent->children()[0]);
219 EXPECT_EQ(parent.get(), child3->parent());
221 // Case 2: inserting to beginning of list
222 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
223 ASSERT_EQ(2U, parent->children().size());
224 EXPECT_EQ(child1, parent->children()[0]);
225 EXPECT_EQ(child3, parent->children()[1]);
226 EXPECT_EQ(parent.get(), child1->parent());
228 // Case 3: inserting to middle of list
229 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
230 ASSERT_EQ(3U, 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(parent.get(), child2->parent());
236 // Case 4: inserting to end of list
237 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
239 ASSERT_EQ(4U, parent->children().size());
240 EXPECT_EQ(child1, parent->children()[0]);
241 EXPECT_EQ(child2, parent->children()[1]);
242 EXPECT_EQ(child3, parent->children()[2]);
243 EXPECT_EQ(child4, parent->children()[3]);
244 EXPECT_EQ(parent.get(), child4->parent());
246 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
249 TEST_F(LayerTest, InsertChildPastEndOfList) {
250 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
251 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
252 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
254 ASSERT_EQ(0U, parent->children().size());
256 // insert to an out-of-bounds index
257 parent->InsertChild(child1, 53);
259 ASSERT_EQ(1U, parent->children().size());
260 EXPECT_EQ(child1, parent->children()[0]);
262 // insert another child to out-of-bounds, when list is not already empty.
263 parent->InsertChild(child2, 2459);
265 ASSERT_EQ(2U, parent->children().size());
266 EXPECT_EQ(child1, parent->children()[0]);
267 EXPECT_EQ(child2, parent->children()[1]);
270 TEST_F(LayerTest, InsertSameChildTwice) {
271 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
272 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
273 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
275 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
277 ASSERT_EQ(0U, parent->children().size());
279 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
280 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
282 ASSERT_EQ(2U, parent->children().size());
283 EXPECT_EQ(child1, parent->children()[0]);
284 EXPECT_EQ(child2, parent->children()[1]);
286 // Inserting the same child again should cause the child to be removed and
287 // re-inserted at the new location.
288 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
290 // child1 should now be at the end of the list.
291 ASSERT_EQ(2U, parent->children().size());
292 EXPECT_EQ(child2, parent->children()[0]);
293 EXPECT_EQ(child1, parent->children()[1]);
295 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
298 TEST_F(LayerTest, ReplaceChildWithNewChild) {
299 CreateSimpleTestTree();
300 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
302 EXPECT_FALSE(child4->parent());
304 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
305 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
306 EXPECT_FALSE(parent_->NeedsDisplayForTesting());
307 EXPECT_FALSE(child1_->NeedsDisplayForTesting());
308 EXPECT_FALSE(child2_->NeedsDisplayForTesting());
309 EXPECT_FALSE(child3_->NeedsDisplayForTesting());
310 EXPECT_FALSE(child4->NeedsDisplayForTesting());
312 ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
313 EXPECT_EQ(child1_, parent_->children()[0]);
314 EXPECT_EQ(child4, parent_->children()[1]);
315 EXPECT_EQ(child3_, parent_->children()[2]);
316 EXPECT_EQ(parent_.get(), child4->parent());
318 EXPECT_FALSE(child2_->parent());
321 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
322 CreateSimpleTestTree();
324 // create another simple tree with test_layer and child4.
325 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
326 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
327 test_layer->AddChild(child4);
328 ASSERT_EQ(1U, test_layer->children().size());
329 EXPECT_EQ(child4, test_layer->children()[0]);
330 EXPECT_EQ(test_layer.get(), child4->parent());
332 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
333 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
335 ASSERT_EQ(3U, parent_->children().size());
336 EXPECT_EQ(child1_, parent_->children()[0]);
337 EXPECT_EQ(child4, parent_->children()[1]);
338 EXPECT_EQ(child3_, parent_->children()[2]);
339 EXPECT_EQ(parent_.get(), child4->parent());
341 // test_layer should no longer have child4,
342 // and child2 should no longer have a parent.
343 ASSERT_EQ(0U, test_layer->children().size());
344 EXPECT_FALSE(child2_->parent());
347 TEST_F(LayerTest, DeleteRemovedScrollParent) {
348 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
349 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
350 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
352 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
354 ASSERT_EQ(0U, parent->children().size());
356 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
357 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
359 ASSERT_EQ(2U, parent->children().size());
360 EXPECT_EQ(child1, parent->children()[0]);
361 EXPECT_EQ(child2, parent->children()[1]);
363 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
365 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
367 child1->reset_needs_push_properties_for_testing();
369 EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
371 EXPECT_TRUE(child1->needs_push_properties());
373 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
376 TEST_F(LayerTest, DeleteRemovedScrollChild) {
377 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
378 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
379 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
381 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
383 ASSERT_EQ(0U, parent->children().size());
385 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
386 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
388 ASSERT_EQ(2U, parent->children().size());
389 EXPECT_EQ(child1, parent->children()[0]);
390 EXPECT_EQ(child2, parent->children()[1]);
392 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
394 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
396 child2->reset_needs_push_properties_for_testing();
398 EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
400 EXPECT_TRUE(child2->needs_push_properties());
402 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
405 TEST_F(LayerTest, ReplaceChildWithSameChild) {
406 CreateSimpleTestTree();
408 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
409 // same child.
410 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
411 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
412 parent_->ReplaceChild(child2_.get(), child2_);
414 VerifyTestTreeInitialState();
417 TEST_F(LayerTest, RemoveAllChildren) {
418 CreateSimpleTestTree();
420 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
422 ASSERT_EQ(0U, parent_->children().size());
423 EXPECT_FALSE(child1_->parent());
424 EXPECT_FALSE(child2_->parent());
425 EXPECT_FALSE(child3_->parent());
428 TEST_F(LayerTest, SetChildren) {
429 scoped_refptr<Layer> old_parent = Layer::Create(layer_settings_);
430 scoped_refptr<Layer> new_parent = Layer::Create(layer_settings_);
432 scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
433 scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
435 LayerList new_children;
436 new_children.push_back(child1);
437 new_children.push_back(child2);
439 // Set up and verify initial test conditions: child1 has a parent, child2 has
440 // no parent.
441 old_parent->AddChild(child1);
442 ASSERT_EQ(0U, new_parent->children().size());
443 EXPECT_EQ(old_parent.get(), child1->parent());
444 EXPECT_FALSE(child2->parent());
446 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
447 1, layer_tree_host_->SetRootLayer(new_parent));
449 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
450 AtLeast(1), new_parent->SetChildren(new_children));
452 ASSERT_EQ(2U, new_parent->children().size());
453 EXPECT_EQ(new_parent.get(), child1->parent());
454 EXPECT_EQ(new_parent.get(), child2->parent());
456 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
459 TEST_F(LayerTest, HasAncestor) {
460 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
461 EXPECT_FALSE(parent->HasAncestor(parent.get()));
463 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
464 parent->AddChild(child);
466 EXPECT_FALSE(child->HasAncestor(child.get()));
467 EXPECT_TRUE(child->HasAncestor(parent.get()));
468 EXPECT_FALSE(parent->HasAncestor(child.get()));
470 scoped_refptr<Layer> child_child = Layer::Create(layer_settings_);
471 child->AddChild(child_child);
473 EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
474 EXPECT_TRUE(child_child->HasAncestor(parent.get()));
475 EXPECT_TRUE(child_child->HasAncestor(child.get()));
476 EXPECT_FALSE(parent->HasAncestor(child.get()));
477 EXPECT_FALSE(parent->HasAncestor(child_child.get()));
480 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
481 CreateSimpleTestTree();
483 // For this test we don't care about SetNeedsFullTreeSync calls.
484 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
486 scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
488 EXPECT_EQ(parent_.get(), parent_->RootLayer());
489 EXPECT_EQ(parent_.get(), child1_->RootLayer());
490 EXPECT_EQ(parent_.get(), child2_->RootLayer());
491 EXPECT_EQ(parent_.get(), child3_->RootLayer());
492 EXPECT_EQ(child4.get(), child4->RootLayer());
493 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
494 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
495 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
497 child1_->RemoveFromParent();
499 // |child1| and its children, grand_child1 and grand_child2 are now on a
500 // separate subtree.
501 EXPECT_EQ(parent_.get(), parent_->RootLayer());
502 EXPECT_EQ(child1_.get(), child1_->RootLayer());
503 EXPECT_EQ(parent_.get(), child2_->RootLayer());
504 EXPECT_EQ(parent_.get(), child3_->RootLayer());
505 EXPECT_EQ(child4.get(), child4->RootLayer());
506 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
507 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
508 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
510 grand_child3_->AddChild(child4);
512 EXPECT_EQ(parent_.get(), parent_->RootLayer());
513 EXPECT_EQ(child1_.get(), child1_->RootLayer());
514 EXPECT_EQ(parent_.get(), child2_->RootLayer());
515 EXPECT_EQ(parent_.get(), child3_->RootLayer());
516 EXPECT_EQ(parent_.get(), child4->RootLayer());
517 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
518 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
519 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
521 child2_->ReplaceChild(grand_child3_.get(), child1_);
523 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
524 // the tree under child2.
525 EXPECT_EQ(parent_.get(), parent_->RootLayer());
526 EXPECT_EQ(parent_.get(), child1_->RootLayer());
527 EXPECT_EQ(parent_.get(), child2_->RootLayer());
528 EXPECT_EQ(parent_.get(), child3_->RootLayer());
529 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
530 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
531 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
532 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
535 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
536 // The semantics for SetNeedsDisplay which are tested here:
537 // 1. sets NeedsDisplay flag appropriately.
538 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
539 // SetNeedsDisplay.
541 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
542 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
543 1, layer_tree_host_->SetRootLayer(test_layer));
544 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
546 gfx::Size test_bounds = gfx::Size(501, 508);
548 gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
549 gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
550 gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
552 // Before anything, test_layer should not be dirty.
553 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
555 // This is just initialization, but SetNeedsCommit behavior is verified anyway
556 // to avoid warnings.
557 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
558 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
560 // The real test begins here.
561 test_layer->ResetNeedsDisplayForTesting();
562 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
564 // Case 1: Layer should accept dirty rects that go beyond its bounds.
565 test_layer->ResetNeedsDisplayForTesting();
566 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
567 EXPECT_SET_NEEDS_UPDATE(
568 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
569 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
570 test_layer->ResetNeedsDisplayForTesting();
572 // Case 2: SetNeedsDisplay() without the dirty rect arg.
573 test_layer->ResetNeedsDisplayForTesting();
574 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
575 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
576 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
577 test_layer->ResetNeedsDisplayForTesting();
579 // Case 3: SetNeedsDisplay() with an empty rect.
580 test_layer->ResetNeedsDisplayForTesting();
581 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
582 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
583 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
585 // Case 4: SetNeedsDisplay() with a non-drawable layer
586 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
587 test_layer->ResetNeedsDisplayForTesting();
588 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
589 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
590 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
593 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
594 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
595 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
596 1, layer_tree_host_->SetRootLayer(test_layer));
597 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
599 scoped_refptr<Layer> dummy_layer1 = Layer::Create(layer_settings_);
600 scoped_refptr<Layer> dummy_layer2 = Layer::Create(layer_settings_);
602 // sanity check of initial test condition
603 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
605 // Next, test properties that should call SetNeedsCommit (but not
606 // SetNeedsDisplay). All properties need to be set to new values in order for
607 // SetNeedsCommit to be called.
608 EXPECT_SET_NEEDS_COMMIT(
609 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
610 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
611 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
612 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
613 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
614 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
615 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
616 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
617 // We can use any layer pointer here since we aren't syncing for real.
618 EXPECT_SET_NEEDS_COMMIT(1,
619 test_layer->SetScrollClipLayerId(test_layer->id()));
620 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
621 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
622 gfx::ScrollOffset(10, 10)));
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
624 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
625 Region(gfx::Rect(1, 1, 2, 2))));
626 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
627 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
628 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
629 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
630 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
631 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
632 gfx::Rect(10, 10)));
633 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
634 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
636 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
637 dummy_layer1.get()));
638 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
639 dummy_layer2.get()));
641 // The above tests should not have caused a change to the needs_display flag.
642 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
644 // As layers are removed from the tree, they will cause a tree sync.
645 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
648 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
649 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
650 scoped_ptr<LayerImpl> impl_layer =
651 LayerImpl::Create(host_impl_.active_tree(), 1);
653 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
654 layer_tree_host_->SetRootLayer(test_layer));
656 test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
657 test_layer->PushPropertiesTo(impl_layer.get());
658 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
659 impl_layer->update_rect());
661 // The LayerImpl's update_rect() should be accumulated here, since we did not
662 // do anything to clear it.
663 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
664 test_layer->PushPropertiesTo(impl_layer.get());
665 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
666 impl_layer->update_rect());
668 // If we do clear the LayerImpl side, then the next update_rect() should be
669 // fresh without accumulation.
670 impl_layer->ResetAllChangeTrackingForSubtree();
671 test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
672 test_layer->PushPropertiesTo(impl_layer.get());
673 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
674 impl_layer->update_rect());
677 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
678 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
679 scoped_ptr<LayerImpl> impl_layer =
680 LayerImpl::Create(host_impl_.active_tree(), 1);
682 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
683 layer_tree_host_->SetRootLayer(test_layer));
685 gfx::Transform transform;
686 transform.Rotate(45.0);
687 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
689 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
691 test_layer->PushPropertiesTo(impl_layer.get());
693 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
696 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
697 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
698 scoped_ptr<LayerImpl> impl_layer =
699 LayerImpl::Create(host_impl_.active_tree(), 1);
701 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
702 layer_tree_host_->SetRootLayer(test_layer));
704 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
706 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
708 test_layer->PushPropertiesTo(impl_layer.get());
710 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
713 TEST_F(LayerTest,
714 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
715 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
716 scoped_ptr<LayerImpl> impl_layer =
717 LayerImpl::Create(host_impl_.active_tree(), 1);
719 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
720 layer_tree_host_->SetRootLayer(test_layer));
722 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
723 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
724 registrar.get());
726 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
727 1.0,
729 100);
731 gfx::Transform transform;
732 transform.Rotate(45.0);
733 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
735 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
736 test_layer->PushPropertiesTo(impl_layer.get());
737 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
739 impl_layer->ResetAllChangeTrackingForSubtree();
740 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
741 1.0,
743 100);
744 impl_layer->layer_animation_controller()
745 ->GetAnimation(Animation::TRANSFORM)
746 ->set_is_impl_only(true);
747 transform.Rotate(45.0);
748 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
750 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
751 test_layer->PushPropertiesTo(impl_layer.get());
752 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
755 TEST_F(LayerTest,
756 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
757 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
758 scoped_ptr<LayerImpl> impl_layer =
759 LayerImpl::Create(host_impl_.active_tree(), 1);
761 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
762 layer_tree_host_->SetRootLayer(test_layer));
764 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
765 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
766 registrar.get());
768 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
769 1.0,
770 0.3f,
771 0.7f,
772 false);
774 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
776 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
777 test_layer->PushPropertiesTo(impl_layer.get());
778 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
780 impl_layer->ResetAllChangeTrackingForSubtree();
781 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
782 1.0,
783 0.3f,
784 0.7f,
785 false);
786 impl_layer->layer_animation_controller()
787 ->GetAnimation(Animation::OPACITY)
788 ->set_is_impl_only(true);
789 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
791 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
792 test_layer->PushPropertiesTo(impl_layer.get());
793 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
796 TEST_F(LayerTest,
797 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
798 scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
799 scoped_ptr<LayerImpl> impl_layer =
800 LayerImpl::Create(host_impl_.active_tree(), 1);
802 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
803 layer_tree_host_->SetRootLayer(test_layer));
805 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
806 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
807 registrar.get());
809 AddAnimatedFilterToController(
810 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
812 FilterOperations filters;
813 filters.Append(FilterOperation::CreateBlurFilter(2.f));
814 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
816 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
817 test_layer->PushPropertiesTo(impl_layer.get());
818 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
820 impl_layer->ResetAllChangeTrackingForSubtree();
821 AddAnimatedFilterToController(
822 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
823 impl_layer->layer_animation_controller()
824 ->GetAnimation(Animation::FILTER)
825 ->set_is_impl_only(true);
826 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
827 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
829 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
830 test_layer->PushPropertiesTo(impl_layer.get());
831 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
834 TEST_F(LayerTest, MaskAndReplicaHasParent) {
835 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
836 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
837 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
838 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
839 scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
840 scoped_refptr<Layer> mask_replacement = Layer::Create(layer_settings_);
841 scoped_refptr<Layer> replica_replacement = Layer::Create(layer_settings_);
842 scoped_refptr<Layer> replica_mask_replacement =
843 Layer::Create(layer_settings_);
845 parent->AddChild(child);
846 child->SetMaskLayer(mask.get());
847 child->SetReplicaLayer(replica.get());
848 replica->SetMaskLayer(replica_mask.get());
850 EXPECT_EQ(parent.get(), child->parent());
851 EXPECT_EQ(child.get(), mask->parent());
852 EXPECT_EQ(child.get(), replica->parent());
853 EXPECT_EQ(replica.get(), replica_mask->parent());
855 replica->SetMaskLayer(replica_mask_replacement.get());
856 EXPECT_EQ(nullptr, replica_mask->parent());
857 EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
859 child->SetMaskLayer(mask_replacement.get());
860 EXPECT_EQ(nullptr, mask->parent());
861 EXPECT_EQ(child.get(), mask_replacement->parent());
863 child->SetReplicaLayer(replica_replacement.get());
864 EXPECT_EQ(nullptr, replica->parent());
865 EXPECT_EQ(child.get(), replica_replacement->parent());
867 EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
870 TEST_F(LayerTest, CheckTranformIsInvertible) {
871 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
872 scoped_ptr<LayerImpl> impl_layer =
873 LayerImpl::Create(host_impl_.active_tree(), 1);
874 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
875 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
876 layer_tree_host_->SetRootLayer(layer);
878 EXPECT_TRUE(layer->transform_is_invertible());
880 gfx::Transform singular_transform;
881 singular_transform.Scale3d(
882 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
884 layer->SetTransform(singular_transform);
885 layer->PushPropertiesTo(impl_layer.get());
887 EXPECT_FALSE(layer->transform_is_invertible());
888 EXPECT_FALSE(impl_layer->transform_is_invertible());
890 gfx::Transform rotation_transform;
891 rotation_transform.RotateAboutZAxis(-45.0);
893 layer->SetTransform(rotation_transform);
894 layer->PushPropertiesTo(impl_layer.get());
895 EXPECT_TRUE(layer->transform_is_invertible());
896 EXPECT_TRUE(impl_layer->transform_is_invertible());
898 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
901 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
902 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
903 scoped_ptr<LayerImpl> impl_layer =
904 LayerImpl::Create(host_impl_.active_tree(), 1);
905 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
906 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
907 layer_tree_host_->SetRootLayer(layer);
909 EXPECT_TRUE(layer->transform_is_invertible());
911 gfx::Transform singular_transform;
912 singular_transform.Scale3d(
913 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
915 layer->SetTransform(singular_transform);
916 layer->PushPropertiesTo(impl_layer.get());
918 EXPECT_FALSE(layer->transform_is_invertible());
919 EXPECT_FALSE(impl_layer->transform_is_invertible());
921 gfx::Transform identity_transform;
923 layer->SetTransform(identity_transform);
924 static_cast<LayerAnimationValueObserver*>(layer.get())
925 ->OnTransformAnimated(singular_transform);
926 layer->PushPropertiesTo(impl_layer.get());
927 EXPECT_FALSE(layer->transform_is_invertible());
928 EXPECT_FALSE(impl_layer->transform_is_invertible());
930 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
933 class LayerTreeHostFactory {
934 public:
935 LayerTreeHostFactory() : client_(FakeLayerTreeHostClient::DIRECT_3D) {}
937 scoped_ptr<LayerTreeHost> Create() { return Create(LayerTreeSettings()); }
939 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
940 LayerTreeHost::InitParams params;
941 params.client = &client_;
942 params.shared_bitmap_manager = &shared_bitmap_manager_;
943 params.task_graph_runner = &task_graph_runner_;
944 params.gpu_memory_buffer_manager = &gpu_memory_buffer_manager_;
945 params.settings = &settings;
946 params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
947 return LayerTreeHost::CreateSingleThreaded(&client_, &params);
950 private:
951 FakeLayerTreeHostClient client_;
952 TestSharedBitmapManager shared_bitmap_manager_;
953 TestTaskGraphRunner task_graph_runner_;
954 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 class LayerLayerTreeHostTest : public testing::Test {
971 public:
972 protected:
973 LayerSettings layer_settings_;
976 TEST_F(LayerLayerTreeHostTest, EnteringTree) {
977 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
978 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
979 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
980 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
981 scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
983 // Set up a detached tree of layers. The host pointer should be nil for these
984 // layers.
985 parent->AddChild(child);
986 child->SetMaskLayer(mask.get());
987 child->SetReplicaLayer(replica.get());
988 replica->SetMaskLayer(replica_mask.get());
990 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
992 LayerTreeHostFactory factory;
993 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
994 // Setting the root layer should set the host pointer for all layers in the
995 // tree.
996 layer_tree_host->SetRootLayer(parent.get());
998 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1000 // Clearing the root layer should also clear out the host pointers for all
1001 // layers in the tree.
1002 layer_tree_host->SetRootLayer(nullptr);
1004 AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
1007 TEST_F(LayerLayerTreeHostTest, AddingLayerSubtree) {
1008 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
1009 LayerTreeHostFactory factory;
1010 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1012 layer_tree_host->SetRootLayer(parent.get());
1014 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
1016 // Adding a subtree to a layer already associated with a host should set the
1017 // host pointer on all layers in that subtree.
1018 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
1019 scoped_refptr<Layer> grand_child = Layer::Create(layer_settings_);
1020 child->AddChild(grand_child);
1022 // Masks, replicas, and replica masks should pick up the new host too.
1023 scoped_refptr<Layer> child_mask = Layer::Create(layer_settings_);
1024 child->SetMaskLayer(child_mask.get());
1025 scoped_refptr<Layer> child_replica = Layer::Create(layer_settings_);
1026 child->SetReplicaLayer(child_replica.get());
1027 scoped_refptr<Layer> child_replica_mask = Layer::Create(layer_settings_);
1028 child_replica->SetMaskLayer(child_replica_mask.get());
1030 parent->AddChild(child);
1031 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1033 layer_tree_host->SetRootLayer(nullptr);
1036 TEST_F(LayerLayerTreeHostTest, ChangeHost) {
1037 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
1038 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
1039 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
1040 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
1041 scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
1043 // Same setup as the previous test.
1044 parent->AddChild(child);
1045 child->SetMaskLayer(mask.get());
1046 child->SetReplicaLayer(replica.get());
1047 replica->SetMaskLayer(replica_mask.get());
1049 LayerTreeHostFactory factory;
1050 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1051 first_layer_tree_host->SetRootLayer(parent.get());
1053 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1054 first_layer_tree_host.get());
1056 // Now re-root the tree to a new host (simulating what we do on a context lost
1057 // event). This should update the host pointers for all layers in the tree.
1058 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1059 second_layer_tree_host->SetRootLayer(parent.get());
1061 AssertLayerTreeHostMatchesForSubtree(parent.get(),
1062 second_layer_tree_host.get());
1064 second_layer_tree_host->SetRootLayer(nullptr);
1067 TEST_F(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1068 scoped_refptr<Layer> first_parent = Layer::Create(layer_settings_);
1069 scoped_refptr<Layer> first_child = Layer::Create(layer_settings_);
1070 scoped_refptr<Layer> second_parent = Layer::Create(layer_settings_);
1071 scoped_refptr<Layer> second_child = Layer::Create(layer_settings_);
1072 scoped_refptr<Layer> second_grand_child = Layer::Create(layer_settings_);
1074 // First put all children under the first parent and set the first host.
1075 first_parent->AddChild(first_child);
1076 second_child->AddChild(second_grand_child);
1077 first_parent->AddChild(second_child);
1079 LayerTreeHostFactory factory;
1080 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1081 first_layer_tree_host->SetRootLayer(first_parent.get());
1083 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1084 first_layer_tree_host.get());
1086 // Now reparent the subtree starting at second_child to a layer in a different
1087 // tree.
1088 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1089 second_layer_tree_host->SetRootLayer(second_parent.get());
1091 second_parent->AddChild(second_child);
1093 // The moved layer and its children should point to the new host.
1094 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1095 EXPECT_EQ(second_layer_tree_host.get(),
1096 second_grand_child->layer_tree_host());
1098 // Test over, cleanup time.
1099 first_layer_tree_host->SetRootLayer(nullptr);
1100 second_layer_tree_host->SetRootLayer(nullptr);
1103 TEST_F(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1104 scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
1105 scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
1106 scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
1107 scoped_refptr<Layer> mask_child = Layer::Create(layer_settings_);
1108 scoped_refptr<Layer> replica_child = Layer::Create(layer_settings_);
1109 scoped_refptr<Layer> mask_replacement = Layer::Create(layer_settings_);
1110 scoped_refptr<Layer> replica_replacement = Layer::Create(layer_settings_);
1112 parent->SetMaskLayer(mask.get());
1113 parent->SetReplicaLayer(replica.get());
1114 mask->AddChild(mask_child);
1115 replica->AddChild(replica_child);
1117 LayerTreeHostFactory factory;
1118 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1119 layer_tree_host->SetRootLayer(parent.get());
1121 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1123 // Replacing the mask should clear out the old mask's subtree's host pointers.
1124 parent->SetMaskLayer(mask_replacement.get());
1125 EXPECT_EQ(nullptr, mask->layer_tree_host());
1126 EXPECT_EQ(nullptr, mask_child->layer_tree_host());
1128 // Same for replacing a replica layer.
1129 parent->SetReplicaLayer(replica_replacement.get());
1130 EXPECT_EQ(nullptr, replica->layer_tree_host());
1131 EXPECT_EQ(nullptr, replica_child->layer_tree_host());
1133 // Test over, cleanup time.
1134 layer_tree_host->SetRootLayer(nullptr);
1137 TEST_F(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1138 scoped_refptr<Layer> root = Layer::Create(layer_settings_);
1139 scoped_refptr<Layer> child = Layer::Create(layer_settings_);
1140 root->AddChild(child);
1141 LayerTreeHostFactory factory;
1142 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1143 layer_tree_host->SetRootLayer(root);
1146 static bool AddTestAnimation(Layer* layer) {
1147 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1148 KeyframedFloatAnimationCurve::Create();
1149 curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f, nullptr));
1150 curve->AddKeyframe(
1151 FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f, nullptr));
1152 scoped_ptr<Animation> animation =
1153 Animation::Create(curve.Pass(), 0, 0, Animation::OPACITY);
1155 return layer->AddAnimation(animation.Pass());
1158 TEST_F(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1159 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
1161 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1162 // animation should not be accepted.
1163 EXPECT_FALSE(AddTestAnimation(layer.get()));
1165 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1166 layer->RegisterForAnimations(registrar.get());
1168 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1169 EXPECT_TRUE(AddTestAnimation(layer.get()));
1171 LayerTreeSettings settings;
1172 settings.accelerated_animation_enabled = false;
1173 LayerTreeHostFactory factory;
1174 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1175 layer_tree_host->SetRootLayer(layer);
1176 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1178 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1179 // animation should be rejected.
1180 EXPECT_FALSE(AddTestAnimation(layer.get()));
1183 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1184 LayerTreeHostFactory factory;
1185 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1187 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
1188 layer_tree_host->SetRootLayer(layer);
1190 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1191 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1192 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1193 layer->SetContentsOpaque(!!contents_opaque);
1194 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1195 : SK_ColorTRANSPARENT);
1196 layer_tree_host->set_background_color(
1197 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1199 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1200 if (contents_opaque) {
1201 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1202 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1203 << host_opaque << "\n";
1204 } else {
1205 EXPECT_NE(SkColorGetA(safe_color), 255u)
1206 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1207 << host_opaque << "\n";
1214 class DrawsContentChangeLayer : public Layer {
1215 public:
1216 static scoped_refptr<DrawsContentChangeLayer> Create(
1217 const LayerSettings& settings) {
1218 return make_scoped_refptr(new DrawsContentChangeLayer(settings));
1221 void SetLayerTreeHost(LayerTreeHost* host) override {
1222 Layer::SetLayerTreeHost(host);
1223 SetFakeDrawsContent(!fake_draws_content_);
1226 bool HasDrawableContent() const override {
1227 return fake_draws_content_ && Layer::HasDrawableContent();
1230 void SetFakeDrawsContent(bool fake_draws_content) {
1231 fake_draws_content_ = fake_draws_content;
1232 UpdateDrawsContent(HasDrawableContent());
1235 private:
1236 explicit DrawsContentChangeLayer(const LayerSettings& settings)
1237 : Layer(settings), fake_draws_content_(false) {}
1238 ~DrawsContentChangeLayer() override {}
1240 bool fake_draws_content_;
1243 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1244 scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
1245 scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1246 DrawsContentChangeLayer::Create(layer_settings_);
1247 scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1248 DrawsContentChangeLayer::Create(layer_settings_);
1249 root_layer->SetIsDrawable(true);
1250 becomes_not_draws_content->SetIsDrawable(true);
1251 becomes_not_draws_content->SetFakeDrawsContent(true);
1252 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1253 root_layer->AddChild(becomes_not_draws_content);
1254 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1256 becomes_draws_content->SetIsDrawable(true);
1257 root_layer->AddChild(becomes_draws_content);
1258 EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
1261 void ReceiveCopyOutputResult(int* result_count,
1262 scoped_ptr<CopyOutputResult> result) {
1263 ++(*result_count);
1266 TEST_F(LayerTest, DedupesCopyOutputRequestsBySource) {
1267 scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
1268 int result_count = 0;
1270 // Create identical requests without the source being set, and expect the
1271 // layer does not abort either one.
1272 scoped_ptr<CopyOutputRequest> request = CopyOutputRequest::CreateRequest(
1273 base::Bind(&ReceiveCopyOutputResult, &result_count));
1274 layer->RequestCopyOfOutput(request.Pass());
1275 EXPECT_EQ(0, result_count);
1276 request = CopyOutputRequest::CreateRequest(
1277 base::Bind(&ReceiveCopyOutputResult, &result_count));
1278 layer->RequestCopyOfOutput(request.Pass());
1279 EXPECT_EQ(0, result_count);
1281 // When the layer is destroyed, expect both requests to be aborted.
1282 layer = nullptr;
1283 EXPECT_EQ(2, result_count);
1285 layer = Layer::Create(layer_settings_);
1286 result_count = 0;
1288 // Create identical requests, but this time the source is being set. Expect
1289 // the first request from |this| source aborts immediately when the second
1290 // request from |this| source is made.
1291 int did_receive_first_result_from_this_source = 0;
1292 request = CopyOutputRequest::CreateRequest(base::Bind(
1293 &ReceiveCopyOutputResult, &did_receive_first_result_from_this_source));
1294 request->set_source(this);
1295 layer->RequestCopyOfOutput(request.Pass());
1296 EXPECT_EQ(0, did_receive_first_result_from_this_source);
1297 // Make a request from a different source.
1298 int did_receive_result_from_different_source = 0;
1299 request = CopyOutputRequest::CreateRequest(base::Bind(
1300 &ReceiveCopyOutputResult, &did_receive_result_from_different_source));
1301 request->set_source(reinterpret_cast<void*>(0xdeadbee0));
1302 layer->RequestCopyOfOutput(request.Pass());
1303 EXPECT_EQ(0, did_receive_result_from_different_source);
1304 // Make a request without specifying the source.
1305 int did_receive_result_from_anonymous_source = 0;
1306 request = CopyOutputRequest::CreateRequest(base::Bind(
1307 &ReceiveCopyOutputResult, &did_receive_result_from_anonymous_source));
1308 layer->RequestCopyOfOutput(request.Pass());
1309 EXPECT_EQ(0, did_receive_result_from_anonymous_source);
1310 // Make the second request from |this| source.
1311 int did_receive_second_result_from_this_source = 0;
1312 request = CopyOutputRequest::CreateRequest(base::Bind(
1313 &ReceiveCopyOutputResult, &did_receive_second_result_from_this_source));
1314 request->set_source(this);
1315 layer->RequestCopyOfOutput(request.Pass()); // First request to be aborted.
1316 EXPECT_EQ(1, did_receive_first_result_from_this_source);
1317 EXPECT_EQ(0, did_receive_result_from_different_source);
1318 EXPECT_EQ(0, did_receive_result_from_anonymous_source);
1319 EXPECT_EQ(0, did_receive_second_result_from_this_source);
1321 // When the layer is destroyed, the other three requests should be aborted.
1322 layer = nullptr;
1323 EXPECT_EQ(1, did_receive_first_result_from_this_source);
1324 EXPECT_EQ(1, did_receive_result_from_different_source);
1325 EXPECT_EQ(1, did_receive_result_from_anonymous_source);
1326 EXPECT_EQ(1, did_receive_second_result_from_this_source);
1329 } // namespace
1330 } // namespace cc