Remove NavigationRequestInfo::is_showing.
[chromium-blink-merge.git] / cc / layers / layer_unittest.cc
blob7a1cb796363805e0870cbf0a7befec93625cfa0f
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, base::MessageLoopProxy::current());
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 {}
59 class LayerTest : public testing::Test {
60 public:
61 LayerTest()
62 : host_impl_(&proxy_, &shared_bitmap_manager_),
63 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
65 protected:
66 virtual void SetUp() OVERRIDE {
67 layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_));
70 virtual void TearDown() OVERRIDE {
71 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
72 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
73 parent_ = NULL;
74 child1_ = NULL;
75 child2_ = NULL;
76 child3_ = NULL;
77 grand_child1_ = NULL;
78 grand_child2_ = NULL;
79 grand_child3_ = NULL;
81 layer_tree_host_->SetRootLayer(NULL);
82 layer_tree_host_.reset();
85 void VerifyTestTreeInitialState() const {
86 ASSERT_EQ(3U, parent_->children().size());
87 EXPECT_EQ(child1_, parent_->children()[0]);
88 EXPECT_EQ(child2_, parent_->children()[1]);
89 EXPECT_EQ(child3_, parent_->children()[2]);
90 EXPECT_EQ(parent_.get(), child1_->parent());
91 EXPECT_EQ(parent_.get(), child2_->parent());
92 EXPECT_EQ(parent_.get(), child3_->parent());
94 ASSERT_EQ(2U, child1_->children().size());
95 EXPECT_EQ(grand_child1_, child1_->children()[0]);
96 EXPECT_EQ(grand_child2_, child1_->children()[1]);
97 EXPECT_EQ(child1_.get(), grand_child1_->parent());
98 EXPECT_EQ(child1_.get(), grand_child2_->parent());
100 ASSERT_EQ(1U, child2_->children().size());
101 EXPECT_EQ(grand_child3_, child2_->children()[0]);
102 EXPECT_EQ(child2_.get(), grand_child3_->parent());
104 ASSERT_EQ(0U, child3_->children().size());
107 void CreateSimpleTestTree() {
108 parent_ = Layer::Create();
109 child1_ = Layer::Create();
110 child2_ = Layer::Create();
111 child3_ = Layer::Create();
112 grand_child1_ = Layer::Create();
113 grand_child2_ = Layer::Create();
114 grand_child3_ = Layer::Create();
116 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
117 layer_tree_host_->SetRootLayer(parent_);
119 parent_->AddChild(child1_);
120 parent_->AddChild(child2_);
121 parent_->AddChild(child3_);
122 child1_->AddChild(grand_child1_);
123 child1_->AddChild(grand_child2_);
124 child2_->AddChild(grand_child3_);
126 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
128 VerifyTestTreeInitialState();
131 FakeImplProxy proxy_;
132 TestSharedBitmapManager shared_bitmap_manager_;
133 FakeLayerTreeHostImpl host_impl_;
135 FakeLayerTreeHostClient fake_client_;
136 scoped_ptr<StrictMock<MockLayerTreeHost> > layer_tree_host_;
137 scoped_refptr<Layer> parent_;
138 scoped_refptr<Layer> child1_;
139 scoped_refptr<Layer> child2_;
140 scoped_refptr<Layer> child3_;
141 scoped_refptr<Layer> grand_child1_;
142 scoped_refptr<Layer> grand_child2_;
143 scoped_refptr<Layer> grand_child3_;
146 TEST_F(LayerTest, BasicCreateAndDestroy) {
147 scoped_refptr<Layer> test_layer = Layer::Create();
148 ASSERT_TRUE(test_layer.get());
150 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
151 test_layer->SetLayerTreeHost(layer_tree_host_.get());
152 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
154 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
155 test_layer->SetLayerTreeHost(NULL);
158 TEST_F(LayerTest, AddAndRemoveChild) {
159 scoped_refptr<Layer> parent = Layer::Create();
160 scoped_refptr<Layer> child = Layer::Create();
162 // Upon creation, layers should not have children or parent.
163 ASSERT_EQ(0U, parent->children().size());
164 EXPECT_FALSE(child->parent());
166 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
167 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
169 ASSERT_EQ(1U, parent->children().size());
170 EXPECT_EQ(child.get(), parent->children()[0].get());
171 EXPECT_EQ(parent.get(), child->parent());
172 EXPECT_EQ(parent.get(), child->RootLayer());
174 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
177 TEST_F(LayerTest, AddSameChildTwice) {
178 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
180 scoped_refptr<Layer> parent = Layer::Create();
181 scoped_refptr<Layer> child = Layer::Create();
183 layer_tree_host_->SetRootLayer(parent);
185 ASSERT_EQ(0u, parent->children().size());
187 parent->AddChild(child);
188 ASSERT_EQ(1u, parent->children().size());
189 EXPECT_EQ(parent.get(), child->parent());
191 parent->AddChild(child);
192 ASSERT_EQ(1u, parent->children().size());
193 EXPECT_EQ(parent.get(), child->parent());
196 TEST_F(LayerTest, InsertChild) {
197 scoped_refptr<Layer> parent = Layer::Create();
198 scoped_refptr<Layer> child1 = Layer::Create();
199 scoped_refptr<Layer> child2 = Layer::Create();
200 scoped_refptr<Layer> child3 = Layer::Create();
201 scoped_refptr<Layer> child4 = Layer::Create();
203 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
205 ASSERT_EQ(0U, parent->children().size());
207 // Case 1: inserting to empty list.
208 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
209 ASSERT_EQ(1U, parent->children().size());
210 EXPECT_EQ(child3, parent->children()[0]);
211 EXPECT_EQ(parent.get(), child3->parent());
213 // Case 2: inserting to beginning of list
214 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
215 ASSERT_EQ(2U, parent->children().size());
216 EXPECT_EQ(child1, parent->children()[0]);
217 EXPECT_EQ(child3, parent->children()[1]);
218 EXPECT_EQ(parent.get(), child1->parent());
220 // Case 3: inserting to middle of list
221 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
222 ASSERT_EQ(3U, parent->children().size());
223 EXPECT_EQ(child1, parent->children()[0]);
224 EXPECT_EQ(child2, parent->children()[1]);
225 EXPECT_EQ(child3, parent->children()[2]);
226 EXPECT_EQ(parent.get(), child2->parent());
228 // Case 4: inserting to end of list
229 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
231 ASSERT_EQ(4U, parent->children().size());
232 EXPECT_EQ(child1, parent->children()[0]);
233 EXPECT_EQ(child2, parent->children()[1]);
234 EXPECT_EQ(child3, parent->children()[2]);
235 EXPECT_EQ(child4, parent->children()[3]);
236 EXPECT_EQ(parent.get(), child4->parent());
238 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL));
241 TEST_F(LayerTest, InsertChildPastEndOfList) {
242 scoped_refptr<Layer> parent = Layer::Create();
243 scoped_refptr<Layer> child1 = Layer::Create();
244 scoped_refptr<Layer> child2 = Layer::Create();
246 ASSERT_EQ(0U, parent->children().size());
248 // insert to an out-of-bounds index
249 parent->InsertChild(child1, 53);
251 ASSERT_EQ(1U, parent->children().size());
252 EXPECT_EQ(child1, parent->children()[0]);
254 // insert another child to out-of-bounds, when list is not already empty.
255 parent->InsertChild(child2, 2459);
257 ASSERT_EQ(2U, parent->children().size());
258 EXPECT_EQ(child1, parent->children()[0]);
259 EXPECT_EQ(child2, parent->children()[1]);
262 TEST_F(LayerTest, InsertSameChildTwice) {
263 scoped_refptr<Layer> parent = Layer::Create();
264 scoped_refptr<Layer> child1 = Layer::Create();
265 scoped_refptr<Layer> child2 = Layer::Create();
267 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
269 ASSERT_EQ(0U, parent->children().size());
271 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
272 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
274 ASSERT_EQ(2U, parent->children().size());
275 EXPECT_EQ(child1, parent->children()[0]);
276 EXPECT_EQ(child2, parent->children()[1]);
278 // Inserting the same child again should cause the child to be removed and
279 // re-inserted at the new location.
280 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
282 // child1 should now be at the end of the list.
283 ASSERT_EQ(2U, parent->children().size());
284 EXPECT_EQ(child2, parent->children()[0]);
285 EXPECT_EQ(child1, parent->children()[1]);
287 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL));
290 TEST_F(LayerTest, ReplaceChildWithNewChild) {
291 CreateSimpleTestTree();
292 scoped_refptr<Layer> child4 = Layer::Create();
294 EXPECT_FALSE(child4->parent());
296 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
297 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
298 EXPECT_FALSE(parent_->NeedsDisplayForTesting());
299 EXPECT_FALSE(child1_->NeedsDisplayForTesting());
300 EXPECT_FALSE(child2_->NeedsDisplayForTesting());
301 EXPECT_FALSE(child3_->NeedsDisplayForTesting());
302 EXPECT_FALSE(child4->NeedsDisplayForTesting());
304 ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
305 EXPECT_EQ(child1_, parent_->children()[0]);
306 EXPECT_EQ(child4, parent_->children()[1]);
307 EXPECT_EQ(child3_, parent_->children()[2]);
308 EXPECT_EQ(parent_.get(), child4->parent());
310 EXPECT_FALSE(child2_->parent());
313 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
314 CreateSimpleTestTree();
316 // create another simple tree with test_layer and child4.
317 scoped_refptr<Layer> test_layer = Layer::Create();
318 scoped_refptr<Layer> child4 = Layer::Create();
319 test_layer->AddChild(child4);
320 ASSERT_EQ(1U, test_layer->children().size());
321 EXPECT_EQ(child4, test_layer->children()[0]);
322 EXPECT_EQ(test_layer.get(), child4->parent());
324 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
325 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
327 ASSERT_EQ(3U, parent_->children().size());
328 EXPECT_EQ(child1_, parent_->children()[0]);
329 EXPECT_EQ(child4, parent_->children()[1]);
330 EXPECT_EQ(child3_, parent_->children()[2]);
331 EXPECT_EQ(parent_.get(), child4->parent());
333 // test_layer should no longer have child4,
334 // and child2 should no longer have a parent.
335 ASSERT_EQ(0U, test_layer->children().size());
336 EXPECT_FALSE(child2_->parent());
339 TEST_F(LayerTest, ReplaceChildWithSameChild) {
340 CreateSimpleTestTree();
342 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
343 // same child.
344 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
345 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
346 parent_->ReplaceChild(child2_.get(), child2_);
348 VerifyTestTreeInitialState();
351 TEST_F(LayerTest, RemoveAllChildren) {
352 CreateSimpleTestTree();
354 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
356 ASSERT_EQ(0U, parent_->children().size());
357 EXPECT_FALSE(child1_->parent());
358 EXPECT_FALSE(child2_->parent());
359 EXPECT_FALSE(child3_->parent());
362 TEST_F(LayerTest, SetChildren) {
363 scoped_refptr<Layer> old_parent = Layer::Create();
364 scoped_refptr<Layer> new_parent = Layer::Create();
366 scoped_refptr<Layer> child1 = Layer::Create();
367 scoped_refptr<Layer> child2 = Layer::Create();
369 LayerList new_children;
370 new_children.push_back(child1);
371 new_children.push_back(child2);
373 // Set up and verify initial test conditions: child1 has a parent, child2 has
374 // no parent.
375 old_parent->AddChild(child1);
376 ASSERT_EQ(0U, new_parent->children().size());
377 EXPECT_EQ(old_parent.get(), child1->parent());
378 EXPECT_FALSE(child2->parent());
380 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
381 1, layer_tree_host_->SetRootLayer(new_parent));
383 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
384 AtLeast(1), new_parent->SetChildren(new_children));
386 ASSERT_EQ(2U, new_parent->children().size());
387 EXPECT_EQ(new_parent.get(), child1->parent());
388 EXPECT_EQ(new_parent.get(), child2->parent());
390 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL));
393 TEST_F(LayerTest, HasAncestor) {
394 scoped_refptr<Layer> parent = Layer::Create();
395 EXPECT_FALSE(parent->HasAncestor(parent.get()));
397 scoped_refptr<Layer> child = Layer::Create();
398 parent->AddChild(child);
400 EXPECT_FALSE(child->HasAncestor(child.get()));
401 EXPECT_TRUE(child->HasAncestor(parent.get()));
402 EXPECT_FALSE(parent->HasAncestor(child.get()));
404 scoped_refptr<Layer> child_child = Layer::Create();
405 child->AddChild(child_child);
407 EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
408 EXPECT_TRUE(child_child->HasAncestor(parent.get()));
409 EXPECT_TRUE(child_child->HasAncestor(child.get()));
410 EXPECT_FALSE(parent->HasAncestor(child.get()));
411 EXPECT_FALSE(parent->HasAncestor(child_child.get()));
414 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
415 CreateSimpleTestTree();
417 // For this test we don't care about SetNeedsFullTreeSync calls.
418 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
420 scoped_refptr<Layer> child4 = Layer::Create();
422 EXPECT_EQ(parent_.get(), parent_->RootLayer());
423 EXPECT_EQ(parent_.get(), child1_->RootLayer());
424 EXPECT_EQ(parent_.get(), child2_->RootLayer());
425 EXPECT_EQ(parent_.get(), child3_->RootLayer());
426 EXPECT_EQ(child4.get(), child4->RootLayer());
427 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
428 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
429 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
431 child1_->RemoveFromParent();
433 // |child1| and its children, grand_child1 and grand_child2 are now on a
434 // separate subtree.
435 EXPECT_EQ(parent_.get(), parent_->RootLayer());
436 EXPECT_EQ(child1_.get(), child1_->RootLayer());
437 EXPECT_EQ(parent_.get(), child2_->RootLayer());
438 EXPECT_EQ(parent_.get(), child3_->RootLayer());
439 EXPECT_EQ(child4.get(), child4->RootLayer());
440 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
441 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
442 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
444 grand_child3_->AddChild(child4);
446 EXPECT_EQ(parent_.get(), parent_->RootLayer());
447 EXPECT_EQ(child1_.get(), child1_->RootLayer());
448 EXPECT_EQ(parent_.get(), child2_->RootLayer());
449 EXPECT_EQ(parent_.get(), child3_->RootLayer());
450 EXPECT_EQ(parent_.get(), child4->RootLayer());
451 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
452 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
453 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
455 child2_->ReplaceChild(grand_child3_.get(), child1_);
457 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
458 // the tree under child2.
459 EXPECT_EQ(parent_.get(), parent_->RootLayer());
460 EXPECT_EQ(parent_.get(), child1_->RootLayer());
461 EXPECT_EQ(parent_.get(), child2_->RootLayer());
462 EXPECT_EQ(parent_.get(), child3_->RootLayer());
463 EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
464 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
465 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
466 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
469 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
470 // The semantics for SetNeedsDisplay which are tested here:
471 // 1. sets NeedsDisplay flag appropriately.
472 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
473 // SetNeedsDisplay.
475 scoped_refptr<Layer> test_layer = Layer::Create();
476 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
477 1, layer_tree_host_->SetRootLayer(test_layer));
478 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
480 gfx::Size test_bounds = gfx::Size(501, 508);
482 gfx::RectF dirty1 = gfx::RectF(10.f, 15.f, 1.f, 2.f);
483 gfx::RectF dirty2 = gfx::RectF(20.f, 25.f, 3.f, 4.f);
484 gfx::RectF empty_dirty_rect = gfx::RectF(40.f, 45.f, 0.f, 0.f);
485 gfx::RectF out_of_bounds_dirty_rect = gfx::RectF(400.f, 405.f, 500.f, 502.f);
487 // Before anything, test_layer should not be dirty.
488 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
490 // This is just initialization, but SetNeedsCommit behavior is verified anyway
491 // to avoid warnings.
492 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
493 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
495 // The real test begins here.
496 test_layer->ResetNeedsDisplayForTesting();
497 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
499 // Case 1: Layer should accept dirty rects that go beyond its bounds.
500 test_layer->ResetNeedsDisplayForTesting();
501 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
502 EXPECT_SET_NEEDS_UPDATE(
503 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
504 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
505 test_layer->ResetNeedsDisplayForTesting();
507 // Case 2: SetNeedsDisplay() without the dirty rect arg.
508 test_layer->ResetNeedsDisplayForTesting();
509 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
510 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
511 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
512 test_layer->ResetNeedsDisplayForTesting();
514 // Case 3: SetNeedsDisplay() with an empty rect.
515 test_layer->ResetNeedsDisplayForTesting();
516 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
517 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
518 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
520 // Case 4: SetNeedsDisplay() with a non-drawable layer
521 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
522 test_layer->ResetNeedsDisplayForTesting();
523 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
524 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
525 EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
528 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
529 scoped_refptr<Layer> test_layer = Layer::Create();
530 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
531 1, layer_tree_host_->SetRootLayer(test_layer));
532 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
534 scoped_refptr<Layer> dummy_layer1 = Layer::Create();
535 scoped_refptr<Layer> dummy_layer2 = Layer::Create();
537 // sanity check of initial test condition
538 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
540 // Next, test properties that should call SetNeedsCommit (but not
541 // SetNeedsDisplay). All properties need to be set to new values in order for
542 // SetNeedsCommit to be called.
543 EXPECT_SET_NEEDS_COMMIT(
544 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
545 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
546 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
547 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
548 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
549 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
550 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
551 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
552 // We can use any layer pointer here since we aren't syncing for real.
553 EXPECT_SET_NEEDS_COMMIT(1,
554 test_layer->SetScrollClipLayerId(test_layer->id()));
555 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
556 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
557 gfx::Vector2d(10, 10)));
558 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
559 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
560 Region(gfx::Rect(1, 1, 2, 2))));
561 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
562 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
563 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
564 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
565 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
566 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
567 gfx::Rect(10, 10)));
568 EXPECT_SET_NEEDS_COMMIT(
570 test_layer->SetDrawCheckerboardForMissingTiles(
571 !test_layer->draw_checkerboard_for_missing_tiles()));
572 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
573 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
575 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
576 dummy_layer1.get()));
577 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
578 dummy_layer2.get()));
580 // The above tests should not have caused a change to the needs_display flag.
581 EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
583 // As layers are removed from the tree, they will cause a tree sync.
584 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
587 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
588 scoped_refptr<Layer> test_layer = Layer::Create();
589 scoped_ptr<LayerImpl> impl_layer =
590 LayerImpl::Create(host_impl_.active_tree(), 1);
592 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
593 layer_tree_host_->SetRootLayer(test_layer));
595 test_layer->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f));
596 test_layer->PushPropertiesTo(impl_layer.get());
597 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
598 impl_layer->update_rect());
600 // The LayerImpl's update_rect() should be accumulated here, since we did not
601 // do anything to clear it.
602 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f));
603 test_layer->PushPropertiesTo(impl_layer.get());
604 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
605 impl_layer->update_rect());
607 // If we do clear the LayerImpl side, then the next update_rect() should be
608 // fresh without accumulation.
609 impl_layer->ResetAllChangeTrackingForSubtree();
610 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f));
611 test_layer->PushPropertiesTo(impl_layer.get());
612 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
613 impl_layer->update_rect());
616 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
617 scoped_refptr<Layer> test_layer = Layer::Create();
618 scoped_ptr<LayerImpl> impl_layer =
619 LayerImpl::Create(host_impl_.active_tree(), 1);
621 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
622 layer_tree_host_->SetRootLayer(test_layer));
624 gfx::Transform transform;
625 transform.Rotate(45.0);
626 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
628 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
630 test_layer->PushPropertiesTo(impl_layer.get());
632 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
635 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
636 scoped_refptr<Layer> test_layer = Layer::Create();
637 scoped_ptr<LayerImpl> impl_layer =
638 LayerImpl::Create(host_impl_.active_tree(), 1);
640 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
641 layer_tree_host_->SetRootLayer(test_layer));
643 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
645 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
647 test_layer->PushPropertiesTo(impl_layer.get());
649 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
652 TEST_F(LayerTest,
653 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
654 scoped_refptr<Layer> test_layer = Layer::Create();
655 scoped_ptr<LayerImpl> impl_layer =
656 LayerImpl::Create(host_impl_.active_tree(), 1);
658 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
659 layer_tree_host_->SetRootLayer(test_layer));
661 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
662 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
663 registrar.get());
665 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
666 1.0,
668 100);
670 gfx::Transform transform;
671 transform.Rotate(45.0);
672 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
674 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
675 test_layer->PushPropertiesTo(impl_layer.get());
676 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
678 impl_layer->ResetAllChangeTrackingForSubtree();
679 AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
680 1.0,
682 100);
683 impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)->
684 set_is_impl_only(true);
685 transform.Rotate(45.0);
686 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
688 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
689 test_layer->PushPropertiesTo(impl_layer.get());
690 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
693 TEST_F(LayerTest,
694 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
695 scoped_refptr<Layer> test_layer = Layer::Create();
696 scoped_ptr<LayerImpl> impl_layer =
697 LayerImpl::Create(host_impl_.active_tree(), 1);
699 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
700 layer_tree_host_->SetRootLayer(test_layer));
702 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
703 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
704 registrar.get());
706 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
707 1.0,
708 0.3f,
709 0.7f,
710 false);
712 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
714 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
715 test_layer->PushPropertiesTo(impl_layer.get());
716 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
718 impl_layer->ResetAllChangeTrackingForSubtree();
719 AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
720 1.0,
721 0.3f,
722 0.7f,
723 false);
724 impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)->
725 set_is_impl_only(true);
726 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
728 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
729 test_layer->PushPropertiesTo(impl_layer.get());
730 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
733 TEST_F(LayerTest,
734 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
735 scoped_refptr<Layer> test_layer = Layer::Create();
736 scoped_ptr<LayerImpl> impl_layer =
737 LayerImpl::Create(host_impl_.active_tree(), 1);
739 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
740 layer_tree_host_->SetRootLayer(test_layer));
742 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
743 impl_layer->layer_animation_controller()->SetAnimationRegistrar(
744 registrar.get());
746 AddAnimatedFilterToController(
747 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
749 FilterOperations filters;
750 filters.Append(FilterOperation::CreateBlurFilter(2.f));
751 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
753 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
754 test_layer->PushPropertiesTo(impl_layer.get());
755 EXPECT_TRUE(impl_layer->LayerPropertyChanged());
757 impl_layer->ResetAllChangeTrackingForSubtree();
758 AddAnimatedFilterToController(
759 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
760 impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)->
761 set_is_impl_only(true);
762 filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
763 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
765 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
766 test_layer->PushPropertiesTo(impl_layer.get());
767 EXPECT_FALSE(impl_layer->LayerPropertyChanged());
770 TEST_F(LayerTest, MaskAndReplicaHasParent) {
771 scoped_refptr<Layer> parent = Layer::Create();
772 scoped_refptr<Layer> child = Layer::Create();
773 scoped_refptr<Layer> mask = Layer::Create();
774 scoped_refptr<Layer> replica = Layer::Create();
775 scoped_refptr<Layer> replica_mask = Layer::Create();
776 scoped_refptr<Layer> mask_replacement = Layer::Create();
777 scoped_refptr<Layer> replica_replacement = Layer::Create();
778 scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
780 parent->AddChild(child);
781 child->SetMaskLayer(mask.get());
782 child->SetReplicaLayer(replica.get());
783 replica->SetMaskLayer(replica_mask.get());
785 EXPECT_EQ(parent.get(), child->parent());
786 EXPECT_EQ(child.get(), mask->parent());
787 EXPECT_EQ(child.get(), replica->parent());
788 EXPECT_EQ(replica.get(), replica_mask->parent());
790 replica->SetMaskLayer(replica_mask_replacement.get());
791 EXPECT_EQ(NULL, replica_mask->parent());
792 EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
794 child->SetMaskLayer(mask_replacement.get());
795 EXPECT_EQ(NULL, mask->parent());
796 EXPECT_EQ(child.get(), mask_replacement->parent());
798 child->SetReplicaLayer(replica_replacement.get());
799 EXPECT_EQ(NULL, replica->parent());
800 EXPECT_EQ(child.get(), replica_replacement->parent());
802 EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
805 TEST_F(LayerTest, CheckTranformIsInvertible) {
806 scoped_refptr<Layer> layer = Layer::Create();
807 scoped_ptr<LayerImpl> impl_layer =
808 LayerImpl::Create(host_impl_.active_tree(), 1);
809 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
810 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
811 layer_tree_host_->SetRootLayer(layer);
813 EXPECT_TRUE(layer->transform_is_invertible());
815 gfx::Transform singular_transform;
816 singular_transform.Scale3d(
817 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
819 layer->SetTransform(singular_transform);
820 layer->PushPropertiesTo(impl_layer.get());
822 EXPECT_FALSE(layer->transform_is_invertible());
823 EXPECT_FALSE(impl_layer->transform_is_invertible());
825 gfx::Transform rotation_transform;
826 rotation_transform.RotateAboutZAxis(-45.0);
828 layer->SetTransform(rotation_transform);
829 layer->PushPropertiesTo(impl_layer.get());
830 EXPECT_TRUE(layer->transform_is_invertible());
831 EXPECT_TRUE(impl_layer->transform_is_invertible());
833 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
836 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
837 scoped_refptr<Layer> layer = Layer::Create();
838 scoped_ptr<LayerImpl> impl_layer =
839 LayerImpl::Create(host_impl_.active_tree(), 1);
840 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
841 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
842 layer_tree_host_->SetRootLayer(layer);
844 EXPECT_TRUE(layer->transform_is_invertible());
846 gfx::Transform singular_transform;
847 singular_transform.Scale3d(
848 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
850 layer->SetTransform(singular_transform);
851 layer->PushPropertiesTo(impl_layer.get());
853 EXPECT_FALSE(layer->transform_is_invertible());
854 EXPECT_FALSE(impl_layer->transform_is_invertible());
856 gfx::Transform identity_transform;
858 layer->SetTransform(identity_transform);
859 static_cast<LayerAnimationValueObserver*>(layer.get())
860 ->OnTransformAnimated(singular_transform);
861 layer->PushPropertiesTo(impl_layer.get());
862 EXPECT_FALSE(layer->transform_is_invertible());
863 EXPECT_FALSE(impl_layer->transform_is_invertible());
865 Mock::VerifyAndClearExpectations(layer_tree_host_.get());
868 class LayerTreeHostFactory {
869 public:
870 LayerTreeHostFactory()
871 : client_(FakeLayerTreeHostClient::DIRECT_3D),
872 shared_bitmap_manager_(new TestSharedBitmapManager()) {}
874 scoped_ptr<LayerTreeHost> Create() {
875 return LayerTreeHost::CreateSingleThreaded(
876 &client_,
877 &client_,
878 shared_bitmap_manager_.get(),
879 LayerTreeSettings(),
880 base::MessageLoopProxy::current()).Pass();
883 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
884 return LayerTreeHost::CreateSingleThreaded(
885 &client_,
886 &client_,
887 shared_bitmap_manager_.get(),
888 settings,
889 base::MessageLoopProxy::current()).Pass();
892 private:
893 FakeLayerTreeHostClient client_;
894 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
897 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
898 EXPECT_EQ(host, layer->layer_tree_host());
900 for (size_t i = 0; i < layer->children().size(); ++i)
901 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
903 if (layer->mask_layer())
904 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
906 if (layer->replica_layer())
907 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
910 TEST(LayerLayerTreeHostTest, EnteringTree) {
911 scoped_refptr<Layer> parent = Layer::Create();
912 scoped_refptr<Layer> child = Layer::Create();
913 scoped_refptr<Layer> mask = Layer::Create();
914 scoped_refptr<Layer> replica = Layer::Create();
915 scoped_refptr<Layer> replica_mask = Layer::Create();
917 // Set up a detached tree of layers. The host pointer should be nil for these
918 // layers.
919 parent->AddChild(child);
920 child->SetMaskLayer(mask.get());
921 child->SetReplicaLayer(replica.get());
922 replica->SetMaskLayer(replica_mask.get());
924 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL);
926 LayerTreeHostFactory factory;
927 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
928 // Setting the root layer should set the host pointer for all layers in the
929 // tree.
930 layer_tree_host->SetRootLayer(parent.get());
932 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
934 // Clearing the root layer should also clear out the host pointers for all
935 // layers in the tree.
936 layer_tree_host->SetRootLayer(NULL);
938 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL);
941 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
942 scoped_refptr<Layer> parent = Layer::Create();
943 LayerTreeHostFactory factory;
944 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
946 layer_tree_host->SetRootLayer(parent.get());
948 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
950 // Adding a subtree to a layer already associated with a host should set the
951 // host pointer on all layers in that subtree.
952 scoped_refptr<Layer> child = Layer::Create();
953 scoped_refptr<Layer> grand_child = Layer::Create();
954 child->AddChild(grand_child);
956 // Masks, replicas, and replica masks should pick up the new host too.
957 scoped_refptr<Layer> child_mask = Layer::Create();
958 child->SetMaskLayer(child_mask.get());
959 scoped_refptr<Layer> child_replica = Layer::Create();
960 child->SetReplicaLayer(child_replica.get());
961 scoped_refptr<Layer> child_replica_mask = Layer::Create();
962 child_replica->SetMaskLayer(child_replica_mask.get());
964 parent->AddChild(child);
965 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
967 layer_tree_host->SetRootLayer(NULL);
970 TEST(LayerLayerTreeHostTest, ChangeHost) {
971 scoped_refptr<Layer> parent = Layer::Create();
972 scoped_refptr<Layer> child = Layer::Create();
973 scoped_refptr<Layer> mask = Layer::Create();
974 scoped_refptr<Layer> replica = Layer::Create();
975 scoped_refptr<Layer> replica_mask = Layer::Create();
977 // Same setup as the previous test.
978 parent->AddChild(child);
979 child->SetMaskLayer(mask.get());
980 child->SetReplicaLayer(replica.get());
981 replica->SetMaskLayer(replica_mask.get());
983 LayerTreeHostFactory factory;
984 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
985 first_layer_tree_host->SetRootLayer(parent.get());
987 AssertLayerTreeHostMatchesForSubtree(parent.get(),
988 first_layer_tree_host.get());
990 // Now re-root the tree to a new host (simulating what we do on a context lost
991 // event). This should update the host pointers for all layers in the tree.
992 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
993 second_layer_tree_host->SetRootLayer(parent.get());
995 AssertLayerTreeHostMatchesForSubtree(parent.get(),
996 second_layer_tree_host.get());
998 second_layer_tree_host->SetRootLayer(NULL);
1001 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1002 scoped_refptr<Layer> first_parent = Layer::Create();
1003 scoped_refptr<Layer> first_child = Layer::Create();
1004 scoped_refptr<Layer> second_parent = Layer::Create();
1005 scoped_refptr<Layer> second_child = Layer::Create();
1006 scoped_refptr<Layer> second_grand_child = Layer::Create();
1008 // First put all children under the first parent and set the first host.
1009 first_parent->AddChild(first_child);
1010 second_child->AddChild(second_grand_child);
1011 first_parent->AddChild(second_child);
1013 LayerTreeHostFactory factory;
1014 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1015 first_layer_tree_host->SetRootLayer(first_parent.get());
1017 AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1018 first_layer_tree_host.get());
1020 // Now reparent the subtree starting at second_child to a layer in a different
1021 // tree.
1022 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1023 second_layer_tree_host->SetRootLayer(second_parent.get());
1025 second_parent->AddChild(second_child);
1027 // The moved layer and its children should point to the new host.
1028 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1029 EXPECT_EQ(second_layer_tree_host.get(),
1030 second_grand_child->layer_tree_host());
1032 // Test over, cleanup time.
1033 first_layer_tree_host->SetRootLayer(NULL);
1034 second_layer_tree_host->SetRootLayer(NULL);
1037 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1038 scoped_refptr<Layer> parent = Layer::Create();
1039 scoped_refptr<Layer> mask = Layer::Create();
1040 scoped_refptr<Layer> replica = Layer::Create();
1041 scoped_refptr<Layer> mask_child = Layer::Create();
1042 scoped_refptr<Layer> replica_child = Layer::Create();
1043 scoped_refptr<Layer> mask_replacement = Layer::Create();
1044 scoped_refptr<Layer> replica_replacement = Layer::Create();
1046 parent->SetMaskLayer(mask.get());
1047 parent->SetReplicaLayer(replica.get());
1048 mask->AddChild(mask_child);
1049 replica->AddChild(replica_child);
1051 LayerTreeHostFactory factory;
1052 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1053 layer_tree_host->SetRootLayer(parent.get());
1055 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1057 // Replacing the mask should clear out the old mask's subtree's host pointers.
1058 parent->SetMaskLayer(mask_replacement.get());
1059 EXPECT_EQ(NULL, mask->layer_tree_host());
1060 EXPECT_EQ(NULL, mask_child->layer_tree_host());
1062 // Same for replacing a replica layer.
1063 parent->SetReplicaLayer(replica_replacement.get());
1064 EXPECT_EQ(NULL, replica->layer_tree_host());
1065 EXPECT_EQ(NULL, replica_child->layer_tree_host());
1067 // Test over, cleanup time.
1068 layer_tree_host->SetRootLayer(NULL);
1071 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1072 scoped_refptr<Layer> root = Layer::Create();
1073 scoped_refptr<Layer> child = Layer::Create();
1074 root->AddChild(child);
1075 LayerTreeHostFactory factory;
1076 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1077 layer_tree_host->SetRootLayer(root);
1080 static bool AddTestAnimation(Layer* layer) {
1081 scoped_ptr<KeyframedFloatAnimationCurve> curve =
1082 KeyframedFloatAnimationCurve::Create();
1083 curve->AddKeyframe(FloatKeyframe::Create(0.0,
1084 0.3f,
1085 scoped_ptr<TimingFunction>()));
1086 curve->AddKeyframe(FloatKeyframe::Create(1.0,
1087 0.7f,
1088 scoped_ptr<TimingFunction>()));
1089 scoped_ptr<Animation> animation =
1090 Animation::Create(curve.PassAs<AnimationCurve>(),
1093 Animation::Opacity);
1095 return layer->AddAnimation(animation.Pass());
1098 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1099 scoped_refptr<Layer> layer = Layer::Create();
1101 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1102 // animation should not be accepted.
1103 EXPECT_FALSE(AddTestAnimation(layer.get()));
1105 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1106 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1108 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1109 EXPECT_TRUE(AddTestAnimation(layer.get()));
1111 LayerTreeSettings settings;
1112 settings.accelerated_animation_enabled = false;
1113 LayerTreeHostFactory factory;
1114 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1115 layer_tree_host->SetRootLayer(layer);
1116 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1118 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1119 // animation should be rejected.
1120 EXPECT_FALSE(AddTestAnimation(layer.get()));
1123 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1124 LayerTreeHostFactory factory;
1125 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1127 scoped_refptr<Layer> layer = Layer::Create();
1128 layer_tree_host->SetRootLayer(layer);
1130 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1131 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1132 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1133 layer->SetContentsOpaque(!!contents_opaque);
1134 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1135 : SK_ColorTRANSPARENT);
1136 layer_tree_host->set_background_color(
1137 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1139 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1140 if (contents_opaque) {
1141 EXPECT_EQ(SkColorGetA(safe_color), 255u)
1142 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1143 << host_opaque << "\n";
1144 } else {
1145 EXPECT_NE(SkColorGetA(safe_color), 255u)
1146 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1147 << host_opaque << "\n";
1154 class DrawsContentChangeLayer : public Layer {
1155 public:
1156 static scoped_refptr<DrawsContentChangeLayer> Create() {
1157 return make_scoped_refptr(new DrawsContentChangeLayer());
1160 virtual void SetLayerTreeHost(LayerTreeHost* host) OVERRIDE {
1161 Layer::SetLayerTreeHost(host);
1162 SetFakeDrawsContent(!fake_draws_content_);
1165 virtual bool HasDrawableContent() const OVERRIDE {
1166 return fake_draws_content_ && Layer::HasDrawableContent();
1169 void SetFakeDrawsContent(bool fake_draws_content) {
1170 fake_draws_content_ = fake_draws_content;
1171 UpdateDrawsContent(HasDrawableContent());
1174 private:
1175 DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1176 virtual ~DrawsContentChangeLayer() OVERRIDE {}
1178 bool fake_draws_content_;
1181 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1182 scoped_refptr<Layer> root_layer = Layer::Create();
1183 scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1184 DrawsContentChangeLayer::Create();
1185 scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1186 DrawsContentChangeLayer::Create();
1187 root_layer->SetIsDrawable(true);
1188 becomes_not_draws_content->SetIsDrawable(true);
1189 becomes_not_draws_content->SetFakeDrawsContent(true);
1190 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1191 root_layer->AddChild(becomes_not_draws_content);
1192 EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1194 becomes_draws_content->SetIsDrawable(true);
1195 root_layer->AddChild(becomes_draws_content);
1196 EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
1199 } // namespace
1200 } // namespace cc