Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / cc / layers / layer_unittest.cc
blobba77513370e4d5350e970a60864b690abb324d3f
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_shared_bitmap_manager.h"
18 #include "cc/trees/layer_tree_host.h"
19 #include "cc/trees/single_thread_proxy.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "ui/gfx/transform.h"
24 using ::testing::AnyNumber;
25 using ::testing::AtLeast;
26 using ::testing::Mock;
27 using ::testing::StrictMock;
28 using ::testing::_;
30 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
31 do { \
32 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
33 code_to_test; \
34 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
35 } while (false)
37 namespace cc {
38 namespace {
40 class MockLayerTreeHost : public LayerTreeHost {
41 public:
42 explicit MockLayerTreeHost(FakeLayerTreeHostClient* client)
43 : LayerTreeHost(client, NULL, LayerTreeSettings()) {
44 InitializeSingleThreaded(client);
47 MOCK_METHOD0(SetNeedsCommit, void());
48 MOCK_METHOD0(SetNeedsUpdateLayers, void());
49 MOCK_METHOD0(SetNeedsFullTreeSync, void());
52 class MockLayerPainter : public LayerPainter {
53 public:
54 virtual void Paint(SkCanvas* canvas,
55 const gfx::Rect& content_rect,
56 gfx::RectF* opaque) 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 virtual void SetUp() OVERRIDE {
68 layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_));
71 virtual void TearDown() OVERRIDE {
72 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
73 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
74 parent_ = NULL;
75 child1_ = NULL;
76 child2_ = NULL;
77 child3_ = NULL;
78 grand_child1_ = NULL;
79 grand_child2_ = NULL;
80 grand_child3_ = NULL;
82 layer_tree_host_->SetRootLayer(NULL);
83 layer_tree_host_.reset();
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(NULL);
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]);
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(NULL));
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(NULL));
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, ReplaceChildWithSameChild) {
341 CreateSimpleTestTree();
343 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
344 // same child.
345 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
346 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
347 parent_->ReplaceChild(child2_.get(), child2_);
349 VerifyTestTreeInitialState();
352 TEST_F(LayerTest, RemoveAllChildren) {
353 CreateSimpleTestTree();
355 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
357 ASSERT_EQ(0U, parent_->children().size());
358 EXPECT_FALSE(child1_->parent());
359 EXPECT_FALSE(child2_->parent());
360 EXPECT_FALSE(child3_->parent());
363 TEST_F(LayerTest, SetChildren) {
364 scoped_refptr<Layer> old_parent = Layer::Create();
365 scoped_refptr<Layer> new_parent = Layer::Create();
367 scoped_refptr<Layer> child1 = Layer::Create();
368 scoped_refptr<Layer> child2 = Layer::Create();
370 LayerList new_children;
371 new_children.push_back(child1);
372 new_children.push_back(child2);
374 // Set up and verify initial test conditions: child1 has a parent, child2 has
375 // no parent.
376 old_parent->AddChild(child1);
377 ASSERT_EQ(0U, new_parent->children().size());
378 EXPECT_EQ(old_parent.get(), child1->parent());
379 EXPECT_FALSE(child2->parent());
381 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
382 1, layer_tree_host_->SetRootLayer(new_parent));
384 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
385 AtLeast(1), new_parent->SetChildren(new_children));
387 ASSERT_EQ(2U, new_parent->children().size());
388 EXPECT_EQ(new_parent.get(), child1->parent());
389 EXPECT_EQ(new_parent.get(), child2->parent());
391 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL));
394 TEST_F(LayerTest, HasAncestor) {
395 scoped_refptr<Layer> parent = Layer::Create();
396 EXPECT_FALSE(parent->HasAncestor(parent));
398 scoped_refptr<Layer> child = Layer::Create();
399 parent->AddChild(child);
401 EXPECT_FALSE(child->HasAncestor(child));
402 EXPECT_TRUE(child->HasAncestor(parent));
403 EXPECT_FALSE(parent->HasAncestor(child));
405 scoped_refptr<Layer> child_child = Layer::Create();
406 child->AddChild(child_child);
408 EXPECT_FALSE(child_child->HasAncestor(child_child));
409 EXPECT_TRUE(child_child->HasAncestor(parent));
410 EXPECT_TRUE(child_child->HasAncestor(child));
411 EXPECT_FALSE(parent->HasAncestor(child));
412 EXPECT_FALSE(parent->HasAncestor(child_child));
415 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
416 CreateSimpleTestTree();
418 // For this test we don't care about SetNeedsFullTreeSync calls.
419 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
421 scoped_refptr<Layer> child4 = Layer::Create();
423 EXPECT_EQ(parent_.get(), parent_->RootLayer());
424 EXPECT_EQ(parent_.get(), child1_->RootLayer());
425 EXPECT_EQ(parent_.get(), child2_->RootLayer());
426 EXPECT_EQ(parent_.get(), child3_->RootLayer());
427 EXPECT_EQ(child4.get(), child4->RootLayer());
428 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
429 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
430 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
432 child1_->RemoveFromParent();
434 // |child1| and its children, grand_child1 and grand_child2 are now on a
435 // separate subtree.
436 EXPECT_EQ(parent_.get(), parent_->RootLayer());
437 EXPECT_EQ(child1_.get(), child1_->RootLayer());
438 EXPECT_EQ(parent_.get(), child2_->RootLayer());
439 EXPECT_EQ(parent_.get(), child3_->RootLayer());
440 EXPECT_EQ(child4.get(), child4->RootLayer());
441 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
442 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
443 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
445 grand_child3_->AddChild(child4);
447 EXPECT_EQ(parent_.get(), parent_->RootLayer());
448 EXPECT_EQ(child1_.get(), child1_->RootLayer());
449 EXPECT_EQ(parent_.get(), child2_->RootLayer());
450 EXPECT_EQ(parent_.get(), child3_->RootLayer());
451 EXPECT_EQ(parent_.get(), child4->RootLayer());
452 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
453 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
454 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
456 child2_->ReplaceChild(grand_child3_.get(), child1_);
458 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
459 // the tree under child2.
460 EXPECT_EQ(parent_.get(), parent_->RootLayer());
461 EXPECT_EQ(parent_.get(), child1_->RootLayer());
462 EXPECT_EQ(parent_.get(), child2_->RootLayer());
463 EXPECT_EQ(parent_.get(), child3_->RootLayer());
464 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
465 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
466 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
467 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
470 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
471 // The semantics for SetNeedsDisplay which are tested here:
472 // 1. sets NeedsDisplay flag appropriately.
473 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
474 // SetNeedsDisplay.
476 scoped_refptr<Layer> test_layer = Layer::Create();
477 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
478 1, layer_tree_host_->SetRootLayer(test_layer));
479 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
481 gfx::Size test_bounds = gfx::Size(501, 508);
483 gfx::RectF dirty1 = gfx::RectF(10.f, 15.f, 1.f, 2.f);
484 gfx::RectF dirty2 = gfx::RectF(20.f, 25.f, 3.f, 4.f);
485 gfx::RectF empty_dirty_rect = gfx::RectF(40.f, 45.f, 0.f, 0.f);
486 gfx::RectF out_of_bounds_dirty_rect = gfx::RectF(400.f, 405.f, 500.f, 502.f);
488 // Before anything, test_layer should not be dirty.
489 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
491 // This is just initialization, but SetNeedsCommit behavior is verified anyway
492 // to avoid warnings.
493 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
494 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
496 // The real test begins here.
497 test_layer->ResetNeedsDisplayForTesting();
498 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
500 // Case 1: Layer should accept dirty rects that go beyond its bounds.
501 test_layer->ResetNeedsDisplayForTesting();
502 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
503 EXPECT_SET_NEEDS_UPDATE(
504 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
505 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
506 test_layer->ResetNeedsDisplayForTesting();
508 // Case 2: SetNeedsDisplay() without the dirty rect arg.
509 test_layer->ResetNeedsDisplayForTesting();
510 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
511 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
512 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
513 test_layer->ResetNeedsDisplayForTesting();
515 // Case 3: SetNeedsDisplay() with an empty rect.
516 test_layer->ResetNeedsDisplayForTesting();
517 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
518 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
519 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
521 // Case 4: SetNeedsDisplay() with a non-drawable layer
522 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
523 test_layer->ResetNeedsDisplayForTesting();
524 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
525 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
526 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
529 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
530 scoped_refptr<Layer> test_layer = Layer::Create();
531 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
532 1, layer_tree_host_->SetRootLayer(test_layer));
533 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
535 scoped_refptr<Layer> dummy_layer1 = Layer::Create();
536 scoped_refptr<Layer> dummy_layer2 = Layer::Create();
538 // sanity check of initial test condition
539 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
541 // Next, test properties that should call SetNeedsCommit (but not
542 // SetNeedsDisplay). All properties need to be set to new values in order for
543 // SetNeedsCommit to be called.
544 EXPECT_SET_NEEDS_COMMIT(
545 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
546 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
547 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
548 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
549 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
550 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
551 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
552 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
553 // We can use any layer pointer here since we aren't syncing for real.
554 EXPECT_SET_NEEDS_COMMIT(1,
555 test_layer->SetScrollClipLayerId(test_layer->id()));
556 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
557 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
558 gfx::Vector2d(10, 10)));
559 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
560 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
561 Region(gfx::Rect(1, 1, 2, 2))));
562 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
563 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
564 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
565 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
566 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
567 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
568 gfx::Rect(10, 10)));
569 EXPECT_SET_NEEDS_COMMIT(
571 test_layer->SetDrawCheckerboardForMissingTiles(
572 !test_layer->draw_checkerboard_for_missing_tiles()));
573 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
574 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
576 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
577 dummy_layer1.get()));
578 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
579 dummy_layer2.get()));
581 // The above tests should not have caused a change to the needs_display flag.
582 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
584 // As layers are removed from the tree, they will cause a tree sync.
585 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
588 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
589 scoped_refptr<Layer> test_layer = Layer::Create();
590 scoped_ptr<LayerImpl> impl_layer =
591 LayerImpl::Create(host_impl_.active_tree(), 1);
593 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
594 layer_tree_host_->SetRootLayer(test_layer));
596 test_layer->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f));
597 test_layer->PushPropertiesTo(impl_layer.get());
598 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
599 impl_layer->update_rect());
601 // The LayerImpl's update_rect() should be accumulated here, since we did not
602 // do anything to clear it.
603 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f));
604 test_layer->PushPropertiesTo(impl_layer.get());
605 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
606 impl_layer->update_rect());
608 // If we do clear the LayerImpl side, then the next update_rect() should be
609 // fresh without accumulation.
610 impl_layer->ResetAllChangeTrackingForSubtree();
611 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f));
612 test_layer->PushPropertiesTo(impl_layer.get());
613 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
614 impl_layer->update_rect());
617 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
618 scoped_refptr<Layer> test_layer = Layer::Create();
619 scoped_ptr<LayerImpl> impl_layer =
620 LayerImpl::Create(host_impl_.active_tree(), 1);
622 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
623 layer_tree_host_->SetRootLayer(test_layer));
625 gfx::Transform transform;
626 transform.Rotate(45.0);
627 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
629 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
631 test_layer->PushPropertiesTo(impl_layer.get());
633 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
636 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
637 scoped_refptr<Layer> test_layer = Layer::Create();
638 scoped_ptr<LayerImpl> impl_layer =
639 LayerImpl::Create(host_impl_.active_tree(), 1);
641 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
642 layer_tree_host_->SetRootLayer(test_layer));
644 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
646 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
648 test_layer->PushPropertiesTo(impl_layer.get());
650 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
653 TEST_F(LayerTest,
654 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
655 scoped_refptr<Layer> test_layer = Layer::Create();
656 scoped_ptr<LayerImpl> impl_layer =
657 LayerImpl::Create(host_impl_.active_tree(), 1);
659 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
660 layer_tree_host_->SetRootLayer(test_layer));
662 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
663 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
664 registrar.get());
666 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
667 1.0,
669 100);
671 gfx::Transform transform;
672 transform.Rotate(45.0);
673 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
675 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
676 test_layer->PushPropertiesTo(impl_layer.get());
677 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
679 impl_layer->ResetAllChangeTrackingForSubtree();
680 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
681 1.0,
683 100);
684 impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)->
685 set_is_impl_only(true);
686 transform.Rotate(45.0);
687 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
689 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
690 test_layer->PushPropertiesTo(impl_layer.get());
691 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
694 TEST_F(LayerTest,
695 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
696 scoped_refptr<Layer> test_layer = Layer::Create();
697 scoped_ptr<LayerImpl> impl_layer =
698 LayerImpl::Create(host_impl_.active_tree(), 1);
700 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
701 layer_tree_host_->SetRootLayer(test_layer));
703 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
704 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
705 registrar.get());
707 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
708 1.0,
709 0.3f,
710 0.7f,
711 false);
713 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
715 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
716 test_layer->PushPropertiesTo(impl_layer.get());
717 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
719 impl_layer->ResetAllChangeTrackingForSubtree();
720 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
721 1.0,
722 0.3f,
723 0.7f,
724 false);
725 impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)->
726 set_is_impl_only(true);
727 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
729 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
730 test_layer->PushPropertiesTo(impl_layer.get());
731 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
734 TEST_F(LayerTest,
735 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
736 scoped_refptr<Layer> test_layer = Layer::Create();
737 scoped_ptr<LayerImpl> impl_layer =
738 LayerImpl::Create(host_impl_.active_tree(), 1);
740 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
741 layer_tree_host_->SetRootLayer(test_layer));
743 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
744 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
745 registrar.get());
747 AddAnimatedFilterToController(
748 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
750 FilterOperations filters;
751 filters.Append(FilterOperation::CreateBlurFilter(2.f));
752 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
754 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
755 test_layer->PushPropertiesTo(impl_layer.get());
756 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
758 impl_layer->ResetAllChangeTrackingForSubtree();
759 AddAnimatedFilterToController(
760 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
761 impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)->
762 set_is_impl_only(true);
763 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
764 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
766 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
767 test_layer->PushPropertiesTo(impl_layer.get());
768 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
771 TEST_F(LayerTest, MaskAndReplicaHasParent) {
772 scoped_refptr<Layer> parent = Layer::Create();
773 scoped_refptr<Layer> child = Layer::Create();
774 scoped_refptr<Layer> mask = Layer::Create();
775 scoped_refptr<Layer> replica = Layer::Create();
776 scoped_refptr<Layer> replica_mask = Layer::Create();
777 scoped_refptr<Layer> mask_replacement = Layer::Create();
778 scoped_refptr<Layer> replica_replacement = Layer::Create();
779 scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
781 parent->AddChild(child);
782 child->SetMaskLayer(mask.get());
783 child->SetReplicaLayer(replica.get());
784 replica->SetMaskLayer(replica_mask.get());
786 EXPECT_EQ(parent, child->parent());
787 EXPECT_EQ(child, mask->parent());
788 EXPECT_EQ(child, replica->parent());
789 EXPECT_EQ(replica, replica_mask->parent());
791 replica->SetMaskLayer(replica_mask_replacement.get());
792 EXPECT_EQ(NULL, replica_mask->parent());
793 EXPECT_EQ(replica, replica_mask_replacement->parent());
795 child->SetMaskLayer(mask_replacement.get());
796 EXPECT_EQ(NULL, mask->parent());
797 EXPECT_EQ(child, mask_replacement->parent());
799 child->SetReplicaLayer(replica_replacement.get());
800 EXPECT_EQ(NULL, replica->parent());
801 EXPECT_EQ(child, replica_replacement->parent());
803 EXPECT_EQ(replica, replica->mask_layer()->parent());
806 TEST_F(LayerTest, CheckTranformIsInvertible) {
807 scoped_refptr<Layer> layer = Layer::Create();
808 scoped_ptr<LayerImpl> impl_layer =
809 LayerImpl::Create(host_impl_.active_tree(), 1);
810 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
811 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
812 layer_tree_host_->SetRootLayer(layer);
814 EXPECT_TRUE(layer->transform_is_invertible());
816 gfx::Transform singular_transform;
817 singular_transform.Scale3d(
818 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
820 layer->SetTransform(singular_transform);
821 layer->PushPropertiesTo(impl_layer.get());
823 EXPECT_FALSE(layer->transform_is_invertible());
824 EXPECT_FALSE(impl_layer->transform_is_invertible());
826 gfx::Transform rotation_transform;
827 rotation_transform.RotateAboutZAxis(-45.0);
829 layer->SetTransform(rotation_transform);
830 layer->PushPropertiesTo(impl_layer.get());
831 EXPECT_TRUE(layer->transform_is_invertible());
832 EXPECT_TRUE(impl_layer->transform_is_invertible());
834 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
837 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
838 scoped_refptr<Layer> layer = Layer::Create();
839 scoped_ptr<LayerImpl> impl_layer =
840 LayerImpl::Create(host_impl_.active_tree(), 1);
841 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
842 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
843 layer_tree_host_->SetRootLayer(layer);
845 EXPECT_TRUE(layer->transform_is_invertible());
847 gfx::Transform singular_transform;
848 singular_transform.Scale3d(
849 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
851 layer->SetTransform(singular_transform);
852 layer->PushPropertiesTo(impl_layer.get());
854 EXPECT_FALSE(layer->transform_is_invertible());
855 EXPECT_FALSE(impl_layer->transform_is_invertible());
857 gfx::Transform identity_transform;
859 layer->SetTransform(identity_transform);
860 static_cast<LayerAnimationValueObserver*>(layer)
861 ->OnTransformAnimated(singular_transform);
862 layer->PushPropertiesTo(impl_layer.get());
863 EXPECT_FALSE(layer->transform_is_invertible());
864 EXPECT_FALSE(impl_layer->transform_is_invertible());
866 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
869 class LayerTreeHostFactory {
870 public:
871 LayerTreeHostFactory()
872 : client_(FakeLayerTreeHostClient::DIRECT_3D),
873 shared_bitmap_manager_(new TestSharedBitmapManager()) {}
875 scoped_ptr<LayerTreeHost> Create() {
876 return LayerTreeHost::CreateSingleThreaded(&client_,
877 &client_,
878 shared_bitmap_manager_.get(),
879 LayerTreeSettings()).Pass();
882 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
883 return LayerTreeHost::CreateSingleThreaded(
884 &client_, &client_, shared_bitmap_manager_.get(), settings)
885 .Pass();
888 private:
889 FakeLayerTreeHostClient client_;
890 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
893 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
894 EXPECT_EQ(host, layer->layer_tree_host());
896 for (size_t i = 0; i < layer->children().size(); ++i)
897 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
899 if (layer->mask_layer())
900 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
902 if (layer->replica_layer())
903 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
906 TEST(LayerLayerTreeHostTest, EnteringTree) {
907 scoped_refptr<Layer> parent = Layer::Create();
908 scoped_refptr<Layer> child = Layer::Create();
909 scoped_refptr<Layer> mask = Layer::Create();
910 scoped_refptr<Layer> replica = Layer::Create();
911 scoped_refptr<Layer> replica_mask = Layer::Create();
913 // Set up a detached tree of layers. The host pointer should be nil for these
914 // layers.
915 parent->AddChild(child);
916 child->SetMaskLayer(mask.get());
917 child->SetReplicaLayer(replica.get());
918 replica->SetMaskLayer(replica_mask.get());
920 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL);
922 LayerTreeHostFactory factory;
923 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
924 // Setting the root layer should set the host pointer for all layers in the
925 // tree.
926 layer_tree_host->SetRootLayer(parent.get());
928 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
930 // Clearing the root layer should also clear out the host pointers for all
931 // layers in the tree.
932 layer_tree_host->SetRootLayer(NULL);
934 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL);
937 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
938 scoped_refptr<Layer> parent = Layer::Create();
939 LayerTreeHostFactory factory;
940 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
942 layer_tree_host->SetRootLayer(parent.get());
944 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
946 // Adding a subtree to a layer already associated with a host should set the
947 // host pointer on all layers in that subtree.
948 scoped_refptr<Layer> child = Layer::Create();
949 scoped_refptr<Layer> grand_child = Layer::Create();
950 child->AddChild(grand_child);
952 // Masks, replicas, and replica masks should pick up the new host too.
953 scoped_refptr<Layer> child_mask = Layer::Create();
954 child->SetMaskLayer(child_mask.get());
955 scoped_refptr<Layer> child_replica = Layer::Create();
956 child->SetReplicaLayer(child_replica.get());
957 scoped_refptr<Layer> child_replica_mask = Layer::Create();
958 child_replica->SetMaskLayer(child_replica_mask.get());
960 parent->AddChild(child);
961 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
963 layer_tree_host->SetRootLayer(NULL);
966 TEST(LayerLayerTreeHostTest, ChangeHost) {
967 scoped_refptr<Layer> parent = Layer::Create();
968 scoped_refptr<Layer> child = Layer::Create();
969 scoped_refptr<Layer> mask = Layer::Create();
970 scoped_refptr<Layer> replica = Layer::Create();
971 scoped_refptr<Layer> replica_mask = Layer::Create();
973 // Same setup as the previous test.
974 parent->AddChild(child);
975 child->SetMaskLayer(mask.get());
976 child->SetReplicaLayer(replica.get());
977 replica->SetMaskLayer(replica_mask.get());
979 LayerTreeHostFactory factory;
980 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
981 first_layer_tree_host->SetRootLayer(parent.get());
983 AssertLayerTreeHostMatchesForSubtree(parent.get(),
984 first_layer_tree_host.get());
986 // Now re-root the tree to a new host (simulating what we do on a context lost
987 // event). This should update the host pointers for all layers in the tree.
988 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
989 second_layer_tree_host->SetRootLayer(parent.get());
991 AssertLayerTreeHostMatchesForSubtree(parent.get(),
992 second_layer_tree_host.get());
994 second_layer_tree_host->SetRootLayer(NULL);
997 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
998 scoped_refptr<Layer> first_parent = Layer::Create();
999 scoped_refptr<Layer> first_child = Layer::Create();
1000 scoped_refptr<Layer> second_parent = Layer::Create();
1001 scoped_refptr<Layer> second_child = Layer::Create();
1002 scoped_refptr<Layer> second_grand_child = Layer::Create();
1004 // First put all children under the first parent and set the first host.
1005 first_parent->AddChild(first_child);
1006 second_child->AddChild(second_grand_child);
1007 first_parent->AddChild(second_child);
1009 LayerTreeHostFactory factory;
1010 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1011 first_layer_tree_host->SetRootLayer(first_parent.get());
1013 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1014 first_layer_tree_host.get());
1016 // Now reparent the subtree starting at second_child to a layer in a different
1017 // tree.
1018 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1019 second_layer_tree_host->SetRootLayer(second_parent.get());
1021 second_parent->AddChild(second_child);
1023 // The moved layer and its children should point to the new host.
1024 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1025 EXPECT_EQ(second_layer_tree_host.get(),
1026 second_grand_child->layer_tree_host());
1028 // Test over, cleanup time.
1029 first_layer_tree_host->SetRootLayer(NULL);
1030 second_layer_tree_host->SetRootLayer(NULL);
1033 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1034 scoped_refptr<Layer> parent = Layer::Create();
1035 scoped_refptr<Layer> mask = Layer::Create();
1036 scoped_refptr<Layer> replica = Layer::Create();
1037 scoped_refptr<Layer> mask_child = Layer::Create();
1038 scoped_refptr<Layer> replica_child = Layer::Create();
1039 scoped_refptr<Layer> mask_replacement = Layer::Create();
1040 scoped_refptr<Layer> replica_replacement = Layer::Create();
1042 parent->SetMaskLayer(mask.get());
1043 parent->SetReplicaLayer(replica.get());
1044 mask->AddChild(mask_child);
1045 replica->AddChild(replica_child);
1047 LayerTreeHostFactory factory;
1048 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1049 layer_tree_host->SetRootLayer(parent.get());
1051 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1053 // Replacing the mask should clear out the old mask's subtree's host pointers.
1054 parent->SetMaskLayer(mask_replacement.get());
1055 EXPECT_EQ(NULL, mask->layer_tree_host());
1056 EXPECT_EQ(NULL, mask_child->layer_tree_host());
1058 // Same for replacing a replica layer.
1059 parent->SetReplicaLayer(replica_replacement.get());
1060 EXPECT_EQ(NULL, replica->layer_tree_host());
1061 EXPECT_EQ(NULL, replica_child->layer_tree_host());
1063 // Test over, cleanup time.
1064 layer_tree_host->SetRootLayer(NULL);
1067 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1068 scoped_refptr<Layer> root = Layer::Create();
1069 scoped_refptr<Layer> child = Layer::Create();
1070 root->AddChild(child);
1071 LayerTreeHostFactory factory;
1072 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1073 layer_tree_host->SetRootLayer(root);
1076 static bool AddTestAnimation(Layer* layer) {
1077 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1078 KeyframedFloatAnimationCurve::Create();
1079 curve->AddKeyframe(FloatKeyframe::Create(0.0,
1080 0.3f,
1081 scoped_ptr<TimingFunction>()));
1082 curve->AddKeyframe(FloatKeyframe::Create(1.0,
1083 0.7f,
1084 scoped_ptr<TimingFunction>()));
1085 scoped_ptr<Animation> animation =
1086 Animation::Create(curve.PassAs<AnimationCurve>(),
1089 Animation::Opacity);
1091 return layer->AddAnimation(animation.Pass());
1094 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1095 scoped_refptr<Layer> layer = Layer::Create();
1097 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1098 // animation should not be accepted.
1099 EXPECT_FALSE(AddTestAnimation(layer.get()));
1101 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1102 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1104 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1105 EXPECT_TRUE(AddTestAnimation(layer.get()));
1107 LayerTreeSettings settings;
1108 settings.accelerated_animation_enabled = false;
1109 LayerTreeHostFactory factory;
1110 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1111 layer_tree_host->SetRootLayer(layer);
1112 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1114 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1115 // animation should be rejected.
1116 EXPECT_FALSE(AddTestAnimation(layer.get()));
1119 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1120 LayerTreeHostFactory factory;
1121 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1123 scoped_refptr<Layer> layer = Layer::Create();
1124 layer_tree_host->SetRootLayer(layer);
1126 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1127 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1128 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1129 layer->SetContentsOpaque(!!contents_opaque);
1130 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1131 : SK_ColorTRANSPARENT);
1132 layer_tree_host->set_background_color(
1133 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1135 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1136 if (contents_opaque) {
1137 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1138 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1139 << host_opaque << "\n";
1140 } else {
1141 EXPECT_NE(SkColorGetA(safe_color), 255u)
1142 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1143 << host_opaque << "\n";
1150 } // namespace
1151 } // namespace cc