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;
30 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
32 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
34 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
40 class MockLayerTreeHost : public LayerTreeHost {
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 {
54 virtual void Paint(SkCanvas* canvas,
55 const gfx::Rect& content_rect,
56 gfx::RectF* opaque) OVERRIDE {}
60 class LayerTest : public testing::Test {
63 : host_impl_(&proxy_, &shared_bitmap_manager_),
64 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
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());
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
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
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
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
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(1, test_layer->SetAnchorPoint(
545 gfx::PointF(1.23f, 4.56f)));
546 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetAnchorPointZ(0.7f));
547 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
548 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
549 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
550 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
551 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
552 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
553 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
554 // We can use any layer pointer here since we aren't syncing for real.
555 EXPECT_SET_NEEDS_COMMIT(1,
556 test_layer->SetScrollClipLayerId(test_layer->id()));
557 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
558 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
559 gfx::Vector2d(10, 10)));
560 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
561 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
562 Region(gfx::Rect(1, 1, 2, 2))));
563 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
564 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
565 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
566 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
567 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
568 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
570 EXPECT_SET_NEEDS_COMMIT(
572 test_layer->SetDrawCheckerboardForMissingTiles(
573 !test_layer->draw_checkerboard_for_missing_tiles()));
574 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
575 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
577 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
578 dummy_layer1.get()));
579 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
580 dummy_layer2.get()));
582 // The above tests should not have caused a change to the needs_display flag.
583 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
585 // As layers are removed from the tree, they will cause a tree sync.
586 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
589 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
590 scoped_refptr<Layer> test_layer = Layer::Create();
591 scoped_ptr<LayerImpl> impl_layer =
592 LayerImpl::Create(host_impl_.active_tree(), 1);
594 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
595 layer_tree_host_->SetRootLayer(test_layer));
597 test_layer->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f));
598 test_layer->PushPropertiesTo(impl_layer.get());
599 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
600 impl_layer->update_rect());
602 // The LayerImpl's update_rect() should be accumulated here, since we did not
603 // do anything to clear it.
604 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f));
605 test_layer->PushPropertiesTo(impl_layer.get());
606 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
607 impl_layer->update_rect());
609 // If we do clear the LayerImpl side, then the next update_rect() should be
610 // fresh without accumulation.
611 impl_layer->ResetAllChangeTrackingForSubtree();
612 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f));
613 test_layer->PushPropertiesTo(impl_layer.get());
614 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
615 impl_layer->update_rect());
618 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
619 scoped_refptr<Layer> test_layer = Layer::Create();
620 scoped_ptr<LayerImpl> impl_layer =
621 LayerImpl::Create(host_impl_.active_tree(), 1);
623 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
624 layer_tree_host_->SetRootLayer(test_layer));
626 gfx::Transform transform;
627 transform.Rotate(45.0);
628 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
630 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
632 test_layer->PushPropertiesTo(impl_layer.get());
634 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
637 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
638 scoped_refptr<Layer> test_layer = Layer::Create();
639 scoped_ptr<LayerImpl> impl_layer =
640 LayerImpl::Create(host_impl_.active_tree(), 1);
642 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
643 layer_tree_host_->SetRootLayer(test_layer));
645 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
647 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
649 test_layer->PushPropertiesTo(impl_layer.get());
651 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
655 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
656 scoped_refptr<Layer> test_layer = Layer::Create();
657 scoped_ptr<LayerImpl> impl_layer =
658 LayerImpl::Create(host_impl_.active_tree(), 1);
660 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
661 layer_tree_host_->SetRootLayer(test_layer));
663 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
664 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
667 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
672 gfx::Transform transform;
673 transform.Rotate(45.0);
674 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
676 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
677 test_layer->PushPropertiesTo(impl_layer.get());
678 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
680 impl_layer->ResetAllChangeTrackingForSubtree();
681 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
685 impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)->
686 set_is_impl_only(true);
687 transform.Rotate(45.0);
688 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
690 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
691 test_layer->PushPropertiesTo(impl_layer.get());
692 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
696 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
697 scoped_refptr<Layer> test_layer = Layer::Create();
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 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
705 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
708 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
714 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
716 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
717 test_layer->PushPropertiesTo(impl_layer.get());
718 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
720 impl_layer->ResetAllChangeTrackingForSubtree();
721 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
726 impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)->
727 set_is_impl_only(true);
728 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
730 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
731 test_layer->PushPropertiesTo(impl_layer.get());
732 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
736 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
737 scoped_refptr<Layer> test_layer = Layer::Create();
738 scoped_ptr<LayerImpl> impl_layer =
739 LayerImpl::Create(host_impl_.active_tree(), 1);
741 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
742 layer_tree_host_->SetRootLayer(test_layer));
744 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
745 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
748 AddAnimatedFilterToController(
749 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
751 FilterOperations filters;
752 filters.Append(FilterOperation::CreateBlurFilter(2.f));
753 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
755 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
756 test_layer->PushPropertiesTo(impl_layer.get());
757 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
759 impl_layer->ResetAllChangeTrackingForSubtree();
760 AddAnimatedFilterToController(
761 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
762 impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)->
763 set_is_impl_only(true);
764 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
765 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
767 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
768 test_layer->PushPropertiesTo(impl_layer.get());
769 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
772 TEST_F(LayerTest, MaskAndReplicaHasParent) {
773 scoped_refptr<Layer> parent = Layer::Create();
774 scoped_refptr<Layer> child = Layer::Create();
775 scoped_refptr<Layer> mask = Layer::Create();
776 scoped_refptr<Layer> replica = Layer::Create();
777 scoped_refptr<Layer> replica_mask = Layer::Create();
778 scoped_refptr<Layer> mask_replacement = Layer::Create();
779 scoped_refptr<Layer> replica_replacement = Layer::Create();
780 scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
782 parent->AddChild(child);
783 child->SetMaskLayer(mask.get());
784 child->SetReplicaLayer(replica.get());
785 replica->SetMaskLayer(replica_mask.get());
787 EXPECT_EQ(parent, child->parent());
788 EXPECT_EQ(child, mask->parent());
789 EXPECT_EQ(child, replica->parent());
790 EXPECT_EQ(replica, replica_mask->parent());
792 replica->SetMaskLayer(replica_mask_replacement.get());
793 EXPECT_EQ(NULL, replica_mask->parent());
794 EXPECT_EQ(replica, replica_mask_replacement->parent());
796 child->SetMaskLayer(mask_replacement.get());
797 EXPECT_EQ(NULL, mask->parent());
798 EXPECT_EQ(child, mask_replacement->parent());
800 child->SetReplicaLayer(replica_replacement.get());
801 EXPECT_EQ(NULL, replica->parent());
802 EXPECT_EQ(child, replica_replacement->parent());
804 EXPECT_EQ(replica, replica->mask_layer()->parent());
807 TEST_F(LayerTest, CheckTranformIsInvertible) {
808 scoped_refptr<Layer> layer = Layer::Create();
809 scoped_ptr<LayerImpl> impl_layer =
810 LayerImpl::Create(host_impl_.active_tree(), 1);
811 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
812 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
813 layer_tree_host_->SetRootLayer(layer);
815 EXPECT_TRUE(layer->transform_is_invertible());
817 gfx::Transform singular_transform;
818 singular_transform.Scale3d(
819 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
821 layer->SetTransform(singular_transform);
822 layer->PushPropertiesTo(impl_layer.get());
824 EXPECT_FALSE(layer->transform_is_invertible());
825 EXPECT_FALSE(impl_layer->transform_is_invertible());
827 gfx::Transform rotation_transform;
828 rotation_transform.RotateAboutZAxis(-45.0);
830 layer->SetTransform(rotation_transform);
831 layer->PushPropertiesTo(impl_layer.get());
832 EXPECT_TRUE(layer->transform_is_invertible());
833 EXPECT_TRUE(impl_layer->transform_is_invertible());
835 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
838 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
839 scoped_refptr<Layer> layer = Layer::Create();
840 scoped_ptr<LayerImpl> impl_layer =
841 LayerImpl::Create(host_impl_.active_tree(), 1);
842 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
843 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
844 layer_tree_host_->SetRootLayer(layer);
846 EXPECT_TRUE(layer->transform_is_invertible());
848 gfx::Transform singular_transform;
849 singular_transform.Scale3d(
850 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
852 layer->SetTransform(singular_transform);
853 layer->PushPropertiesTo(impl_layer.get());
855 EXPECT_FALSE(layer->transform_is_invertible());
856 EXPECT_FALSE(impl_layer->transform_is_invertible());
858 gfx::Transform identity_transform;
860 layer->SetTransform(identity_transform);
861 static_cast<LayerAnimationValueObserver*>(layer)
862 ->OnTransformAnimated(singular_transform);
863 layer->PushPropertiesTo(impl_layer.get());
864 EXPECT_FALSE(layer->transform_is_invertible());
865 EXPECT_FALSE(impl_layer->transform_is_invertible());
867 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
870 class LayerTreeHostFactory {
872 LayerTreeHostFactory()
873 : client_(FakeLayerTreeHostClient::DIRECT_3D),
874 shared_bitmap_manager_(new TestSharedBitmapManager()) {}
876 scoped_ptr<LayerTreeHost> Create() {
877 return LayerTreeHost::CreateSingleThreaded(&client_,
879 shared_bitmap_manager_.get(),
880 LayerTreeSettings()).Pass();
883 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
884 return LayerTreeHost::CreateSingleThreaded(
885 &client_, &client_, shared_bitmap_manager_.get(), settings)
890 FakeLayerTreeHostClient client_;
891 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
894 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
895 EXPECT_EQ(host, layer->layer_tree_host());
897 for (size_t i = 0; i < layer->children().size(); ++i)
898 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
900 if (layer->mask_layer())
901 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
903 if (layer->replica_layer())
904 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
907 TEST(LayerLayerTreeHostTest, EnteringTree) {
908 scoped_refptr<Layer> parent = Layer::Create();
909 scoped_refptr<Layer> child = Layer::Create();
910 scoped_refptr<Layer> mask = Layer::Create();
911 scoped_refptr<Layer> replica = Layer::Create();
912 scoped_refptr<Layer> replica_mask = Layer::Create();
914 // Set up a detached tree of layers. The host pointer should be nil for these
916 parent->AddChild(child);
917 child->SetMaskLayer(mask.get());
918 child->SetReplicaLayer(replica.get());
919 replica->SetMaskLayer(replica_mask.get());
921 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL);
923 LayerTreeHostFactory factory;
924 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
925 // Setting the root layer should set the host pointer for all layers in the
927 layer_tree_host->SetRootLayer(parent.get());
929 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
931 // Clearing the root layer should also clear out the host pointers for all
932 // layers in the tree.
933 layer_tree_host->SetRootLayer(NULL);
935 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL);
938 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
939 scoped_refptr<Layer> parent = Layer::Create();
940 LayerTreeHostFactory factory;
941 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
943 layer_tree_host->SetRootLayer(parent.get());
945 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
947 // Adding a subtree to a layer already associated with a host should set the
948 // host pointer on all layers in that subtree.
949 scoped_refptr<Layer> child = Layer::Create();
950 scoped_refptr<Layer> grand_child = Layer::Create();
951 child->AddChild(grand_child);
953 // Masks, replicas, and replica masks should pick up the new host too.
954 scoped_refptr<Layer> child_mask = Layer::Create();
955 child->SetMaskLayer(child_mask.get());
956 scoped_refptr<Layer> child_replica = Layer::Create();
957 child->SetReplicaLayer(child_replica.get());
958 scoped_refptr<Layer> child_replica_mask = Layer::Create();
959 child_replica->SetMaskLayer(child_replica_mask.get());
961 parent->AddChild(child);
962 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
964 layer_tree_host->SetRootLayer(NULL);
967 TEST(LayerLayerTreeHostTest, ChangeHost) {
968 scoped_refptr<Layer> parent = Layer::Create();
969 scoped_refptr<Layer> child = Layer::Create();
970 scoped_refptr<Layer> mask = Layer::Create();
971 scoped_refptr<Layer> replica = Layer::Create();
972 scoped_refptr<Layer> replica_mask = Layer::Create();
974 // Same setup as the previous test.
975 parent->AddChild(child);
976 child->SetMaskLayer(mask.get());
977 child->SetReplicaLayer(replica.get());
978 replica->SetMaskLayer(replica_mask.get());
980 LayerTreeHostFactory factory;
981 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
982 first_layer_tree_host->SetRootLayer(parent.get());
984 AssertLayerTreeHostMatchesForSubtree(parent.get(),
985 first_layer_tree_host.get());
987 // Now re-root the tree to a new host (simulating what we do on a context lost
988 // event). This should update the host pointers for all layers in the tree.
989 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
990 second_layer_tree_host->SetRootLayer(parent.get());
992 AssertLayerTreeHostMatchesForSubtree(parent.get(),
993 second_layer_tree_host.get());
995 second_layer_tree_host->SetRootLayer(NULL);
998 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
999 scoped_refptr<Layer> first_parent = Layer::Create();
1000 scoped_refptr<Layer> first_child = Layer::Create();
1001 scoped_refptr<Layer> second_parent = Layer::Create();
1002 scoped_refptr<Layer> second_child = Layer::Create();
1003 scoped_refptr<Layer> second_grand_child = Layer::Create();
1005 // First put all children under the first parent and set the first host.
1006 first_parent->AddChild(first_child);
1007 second_child->AddChild(second_grand_child);
1008 first_parent->AddChild(second_child);
1010 LayerTreeHostFactory factory;
1011 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1012 first_layer_tree_host->SetRootLayer(first_parent.get());
1014 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1015 first_layer_tree_host.get());
1017 // Now reparent the subtree starting at second_child to a layer in a different
1019 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1020 second_layer_tree_host->SetRootLayer(second_parent.get());
1022 second_parent->AddChild(second_child);
1024 // The moved layer and its children should point to the new host.
1025 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1026 EXPECT_EQ(second_layer_tree_host.get(),
1027 second_grand_child->layer_tree_host());
1029 // Test over, cleanup time.
1030 first_layer_tree_host->SetRootLayer(NULL);
1031 second_layer_tree_host->SetRootLayer(NULL);
1034 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1035 scoped_refptr<Layer> parent = Layer::Create();
1036 scoped_refptr<Layer> mask = Layer::Create();
1037 scoped_refptr<Layer> replica = Layer::Create();
1038 scoped_refptr<Layer> mask_child = Layer::Create();
1039 scoped_refptr<Layer> replica_child = Layer::Create();
1040 scoped_refptr<Layer> mask_replacement = Layer::Create();
1041 scoped_refptr<Layer> replica_replacement = Layer::Create();
1043 parent->SetMaskLayer(mask.get());
1044 parent->SetReplicaLayer(replica.get());
1045 mask->AddChild(mask_child);
1046 replica->AddChild(replica_child);
1048 LayerTreeHostFactory factory;
1049 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1050 layer_tree_host->SetRootLayer(parent.get());
1052 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1054 // Replacing the mask should clear out the old mask's subtree's host pointers.
1055 parent->SetMaskLayer(mask_replacement.get());
1056 EXPECT_EQ(NULL, mask->layer_tree_host());
1057 EXPECT_EQ(NULL, mask_child->layer_tree_host());
1059 // Same for replacing a replica layer.
1060 parent->SetReplicaLayer(replica_replacement.get());
1061 EXPECT_EQ(NULL, replica->layer_tree_host());
1062 EXPECT_EQ(NULL, replica_child->layer_tree_host());
1064 // Test over, cleanup time.
1065 layer_tree_host->SetRootLayer(NULL);
1068 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1069 scoped_refptr<Layer> root = Layer::Create();
1070 scoped_refptr<Layer> child = Layer::Create();
1071 root->AddChild(child);
1072 LayerTreeHostFactory factory;
1073 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1074 layer_tree_host->SetRootLayer(root);
1077 static bool AddTestAnimation(Layer* layer) {
1078 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1079 KeyframedFloatAnimationCurve::Create();
1080 curve->AddKeyframe(FloatKeyframe::Create(0.0,
1082 scoped_ptr<TimingFunction>()));
1083 curve->AddKeyframe(FloatKeyframe::Create(1.0,
1085 scoped_ptr<TimingFunction>()));
1086 scoped_ptr<Animation> animation =
1087 Animation::Create(curve.PassAs<AnimationCurve>(),
1090 Animation::Opacity);
1092 return layer->AddAnimation(animation.Pass());
1095 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1096 scoped_refptr<Layer> layer = Layer::Create();
1098 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1099 // animation should not be accepted.
1100 EXPECT_FALSE(AddTestAnimation(layer.get()));
1102 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1103 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1105 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1106 EXPECT_TRUE(AddTestAnimation(layer.get()));
1108 LayerTreeSettings settings;
1109 settings.accelerated_animation_enabled = false;
1110 LayerTreeHostFactory factory;
1111 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1112 layer_tree_host->SetRootLayer(layer);
1113 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1115 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1116 // animation should be rejected.
1117 EXPECT_FALSE(AddTestAnimation(layer.get()));
1120 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1121 LayerTreeHostFactory factory;
1122 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1124 scoped_refptr<Layer> layer = Layer::Create();
1125 layer_tree_host->SetRootLayer(layer);
1127 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1128 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1129 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1130 layer->SetContentsOpaque(!!contents_opaque);
1131 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1132 : SK_ColorTRANSPARENT);
1133 layer_tree_host->set_background_color(
1134 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1136 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1137 if (contents_opaque) {
1138 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1139 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1140 << host_opaque << "\n";
1142 EXPECT_NE(SkColorGetA(safe_color), 255u)
1143 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1144 << host_opaque << "\n";