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. 4 5 #include "cc/layers/layer.h" 6 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" 23 24 using ::testing::AnyNumber; 25 using ::testing::AtLeast; 26 using ::testing::Mock; 27 using ::testing::StrictMock; 28 using ::testing::_; 29 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) 36 37 namespace cc { 38 namespace { 39 40 class MockLayerTreeHost : public LayerTreeHost { 41 public: 42 explicit MockLayerTreeHost(FakeLayerTreeHostClient* client) 43 : LayerTreeHost(client, NULL, LayerTreeSettings()) { 44 InitializeSingleThreaded(client); 45 } 46 47 MOCK_METHOD0(SetNeedsCommit, void()); 48 MOCK_METHOD0(SetNeedsUpdateLayers, void()); 49 MOCK_METHOD0(SetNeedsFullTreeSync, void()); 50 }; 51 52 class MockLayerPainter : public LayerPainter { 53 public: 54 virtual void Paint(SkCanvas* canvas, 55 const gfx::Rect& content_rect, 56 gfx::RectF* opaque) OVERRIDE {} 57 }; 58 59 60 class LayerTest : public testing::Test { 61 public: 62 LayerTest() 63 : host_impl_(&proxy_, &shared_bitmap_manager_), 64 fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {} 65 66 protected: 67 virtual void SetUp() OVERRIDE { 68 layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_)); 69 } 70 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; 81 82 layer_tree_host_->SetRootLayer(NULL); 83 layer_tree_host_.reset(); 84 } 85 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()); 94 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()); 100 101 ASSERT_EQ(1U, child2_->children().size()); 102 EXPECT_EQ(grand_child3_, child2_->children()[0]); 103 EXPECT_EQ(child2_.get(), grand_child3_->parent()); 104 105 ASSERT_EQ(0U, child3_->children().size()); 106 } 107 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(); 116 117 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber()); 118 layer_tree_host_->SetRootLayer(parent_); 119 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_); 126 127 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); 128 129 VerifyTestTreeInitialState(); 130 } 131 132 FakeImplProxy proxy_; 133 TestSharedBitmapManager shared_bitmap_manager_; 134 FakeLayerTreeHostImpl host_impl_; 135 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_; 145 }; 146 147 TEST_F(LayerTest, BasicCreateAndDestroy) { 148 scoped_refptr<Layer> test_layer = Layer::Create(); 149 ASSERT_TRUE(test_layer.get()); 150 151 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); 152 test_layer->SetLayerTreeHost(layer_tree_host_.get()); 153 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); 154 155 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); 156 test_layer->SetLayerTreeHost(NULL); 157 } 158 159 TEST_F(LayerTest, AddAndRemoveChild) { 160 scoped_refptr<Layer> parent = Layer::Create(); 161 scoped_refptr<Layer> child = Layer::Create(); 162 163 // Upon creation, layers should not have children or parent. 164 ASSERT_EQ(0U, parent->children().size()); 165 EXPECT_FALSE(child->parent()); 166 167 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent)); 168 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child)); 169 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()); 174 175 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent()); 176 } 177 178 TEST_F(LayerTest, AddSameChildTwice) { 179 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1)); 180 181 scoped_refptr<Layer> parent = Layer::Create(); 182 scoped_refptr<Layer> child = Layer::Create(); 183 184 layer_tree_host_->SetRootLayer(parent); 185 186 ASSERT_EQ(0u, parent->children().size()); 187 188 parent->AddChild(child); 189 ASSERT_EQ(1u, parent->children().size()); 190 EXPECT_EQ(parent.get(), child->parent()); 191 192 parent->AddChild(child); 193 ASSERT_EQ(1u, parent->children().size()); 194 EXPECT_EQ(parent.get(), child->parent()); 195 } 196 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(); 203 204 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent)); 205 206 ASSERT_EQ(0U, parent->children().size()); 207 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()); 213 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()); 220 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()); 228 229 // Case 4: inserting to end of list 230 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3)); 231 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()); 238 239 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL)); 240 } 241 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(); 246 247 ASSERT_EQ(0U, parent->children().size()); 248 249 // insert to an out-of-bounds index 250 parent->InsertChild(child1, 53); 251 252 ASSERT_EQ(1U, parent->children().size()); 253 EXPECT_EQ(child1, parent->children()[0]); 254 255 // insert another child to out-of-bounds, when list is not already empty. 256 parent->InsertChild(child2, 2459); 257 258 ASSERT_EQ(2U, parent->children().size()); 259 EXPECT_EQ(child1, parent->children()[0]); 260 EXPECT_EQ(child2, parent->children()[1]); 261 } 262 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(); 267 268 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent)); 269 270 ASSERT_EQ(0U, parent->children().size()); 271 272 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0)); 273 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1)); 274 275 ASSERT_EQ(2U, parent->children().size()); 276 EXPECT_EQ(child1, parent->children()[0]); 277 EXPECT_EQ(child2, parent->children()[1]); 278 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)); 282 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]); 287 288 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL)); 289 } 290 291 TEST_F(LayerTest, ReplaceChildWithNewChild) { 292 CreateSimpleTestTree(); 293 scoped_refptr<Layer> child4 = Layer::Create(); 294 295 EXPECT_FALSE(child4->parent()); 296 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()); 304 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()); 310 311 EXPECT_FALSE(child2_->parent()); 312 } 313 314 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) { 315 CreateSimpleTestTree(); 316 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()); 324 325 EXPECT_SET_NEEDS_FULL_TREE_SYNC( 326 AtLeast(1), parent_->ReplaceChild(child2_.get(), child4)); 327 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()); 333 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()); 338 } 339 340 TEST_F(LayerTest, DeleteRemovedScrollParent) { 341 scoped_refptr<Layer> parent = Layer::Create(); 342 scoped_refptr<Layer> child1 = Layer::Create(); 343 scoped_refptr<Layer> child2 = Layer::Create(); 344 345 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent)); 346 347 ASSERT_EQ(0U, parent->children().size()); 348 349 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0)); 350 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1)); 351 352 ASSERT_EQ(2U, parent->children().size()); 353 EXPECT_EQ(child1, parent->children()[0]); 354 EXPECT_EQ(child2, parent->children()[1]); 355 356 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get())); 357 358 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent()); 359 360 child1->reset_needs_push_properties_for_testing(); 361 362 EXPECT_SET_NEEDS_COMMIT(1, child2 = NULL); 363 364 EXPECT_TRUE(child1->needs_push_properties()); 365 366 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL)); 367 } 368 369 TEST_F(LayerTest, DeleteRemovedScrollChild) { 370 scoped_refptr<Layer> parent = Layer::Create(); 371 scoped_refptr<Layer> child1 = Layer::Create(); 372 scoped_refptr<Layer> child2 = Layer::Create(); 373 374 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent)); 375 376 ASSERT_EQ(0U, parent->children().size()); 377 378 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0)); 379 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1)); 380 381 ASSERT_EQ(2U, parent->children().size()); 382 EXPECT_EQ(child1, parent->children()[0]); 383 EXPECT_EQ(child2, parent->children()[1]); 384 385 EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get())); 386 387 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent()); 388 389 child2->reset_needs_push_properties_for_testing(); 390 391 EXPECT_SET_NEEDS_COMMIT(1, child1 = NULL); 392 393 EXPECT_TRUE(child2->needs_push_properties()); 394 395 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL)); 396 } 397 398 TEST_F(LayerTest, ReplaceChildWithSameChild) { 399 CreateSimpleTestTree(); 400 401 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the 402 // same child. 403 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0); 404 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0); 405 parent_->ReplaceChild(child2_.get(), child2_); 406 407 VerifyTestTreeInitialState(); 408 } 409 410 TEST_F(LayerTest, RemoveAllChildren) { 411 CreateSimpleTestTree(); 412 413 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren()); 414 415 ASSERT_EQ(0U, parent_->children().size()); 416 EXPECT_FALSE(child1_->parent()); 417 EXPECT_FALSE(child2_->parent()); 418 EXPECT_FALSE(child3_->parent()); 419 } 420 421 TEST_F(LayerTest, SetChildren) { 422 scoped_refptr<Layer> old_parent = Layer::Create(); 423 scoped_refptr<Layer> new_parent = Layer::Create(); 424 425 scoped_refptr<Layer> child1 = Layer::Create(); 426 scoped_refptr<Layer> child2 = Layer::Create(); 427 428 LayerList new_children; 429 new_children.push_back(child1); 430 new_children.push_back(child2); 431 432 // Set up and verify initial test conditions: child1 has a parent, child2 has 433 // no parent. 434 old_parent->AddChild(child1); 435 ASSERT_EQ(0U, new_parent->children().size()); 436 EXPECT_EQ(old_parent.get(), child1->parent()); 437 EXPECT_FALSE(child2->parent()); 438 439 EXPECT_SET_NEEDS_FULL_TREE_SYNC( 440 1, layer_tree_host_->SetRootLayer(new_parent)); 441 442 EXPECT_SET_NEEDS_FULL_TREE_SYNC( 443 AtLeast(1), new_parent->SetChildren(new_children)); 444 445 ASSERT_EQ(2U, new_parent->children().size()); 446 EXPECT_EQ(new_parent.get(), child1->parent()); 447 EXPECT_EQ(new_parent.get(), child2->parent()); 448 449 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(NULL)); 450 } 451 452 TEST_F(LayerTest, HasAncestor) { 453 scoped_refptr<Layer> parent = Layer::Create(); 454 EXPECT_FALSE(parent->HasAncestor(parent)); 455 456 scoped_refptr<Layer> child = Layer::Create(); 457 parent->AddChild(child); 458 459 EXPECT_FALSE(child->HasAncestor(child)); 460 EXPECT_TRUE(child->HasAncestor(parent)); 461 EXPECT_FALSE(parent->HasAncestor(child)); 462 463 scoped_refptr<Layer> child_child = Layer::Create(); 464 child->AddChild(child_child); 465 466 EXPECT_FALSE(child_child->HasAncestor(child_child)); 467 EXPECT_TRUE(child_child->HasAncestor(parent)); 468 EXPECT_TRUE(child_child->HasAncestor(child)); 469 EXPECT_FALSE(parent->HasAncestor(child)); 470 EXPECT_FALSE(parent->HasAncestor(child_child)); 471 } 472 473 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) { 474 CreateSimpleTestTree(); 475 476 // For this test we don't care about SetNeedsFullTreeSync calls. 477 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber()); 478 479 scoped_refptr<Layer> child4 = Layer::Create(); 480 481 EXPECT_EQ(parent_.get(), parent_->RootLayer()); 482 EXPECT_EQ(parent_.get(), child1_->RootLayer()); 483 EXPECT_EQ(parent_.get(), child2_->RootLayer()); 484 EXPECT_EQ(parent_.get(), child3_->RootLayer()); 485 EXPECT_EQ(child4.get(), child4->RootLayer()); 486 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer()); 487 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer()); 488 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer()); 489 490 child1_->RemoveFromParent(); 491 492 // |child1| and its children, grand_child1 and grand_child2 are now on a 493 // separate subtree. 494 EXPECT_EQ(parent_.get(), parent_->RootLayer()); 495 EXPECT_EQ(child1_.get(), child1_->RootLayer()); 496 EXPECT_EQ(parent_.get(), child2_->RootLayer()); 497 EXPECT_EQ(parent_.get(), child3_->RootLayer()); 498 EXPECT_EQ(child4.get(), child4->RootLayer()); 499 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer()); 500 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer()); 501 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer()); 502 503 grand_child3_->AddChild(child4); 504 505 EXPECT_EQ(parent_.get(), parent_->RootLayer()); 506 EXPECT_EQ(child1_.get(), child1_->RootLayer()); 507 EXPECT_EQ(parent_.get(), child2_->RootLayer()); 508 EXPECT_EQ(parent_.get(), child3_->RootLayer()); 509 EXPECT_EQ(parent_.get(), child4->RootLayer()); 510 EXPECT_EQ(child1_.get(), grand_child1_->RootLayer()); 511 EXPECT_EQ(child1_.get(), grand_child2_->RootLayer()); 512 EXPECT_EQ(parent_.get(), grand_child3_->RootLayer()); 513 514 child2_->ReplaceChild(grand_child3_.get(), child1_); 515 516 // |grand_child3| gets orphaned and the child1 subtree gets planted back into 517 // the tree under child2. 518 EXPECT_EQ(parent_.get(), parent_->RootLayer()); 519 EXPECT_EQ(parent_.get(), child1_->RootLayer()); 520 EXPECT_EQ(parent_.get(), child2_->RootLayer()); 521 EXPECT_EQ(parent_.get(), child3_->RootLayer()); 522 EXPECT_EQ(grand_child3_.get(), child4->RootLayer()); 523 EXPECT_EQ(parent_.get(), grand_child1_->RootLayer()); 524 EXPECT_EQ(parent_.get(), grand_child2_->RootLayer()); 525 EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer()); 526 } 527 528 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) { 529 // The semantics for SetNeedsDisplay which are tested here: 530 // 1. sets NeedsDisplay flag appropriately. 531 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to 532 // SetNeedsDisplay. 533 534 scoped_refptr<Layer> test_layer = Layer::Create(); 535 EXPECT_SET_NEEDS_FULL_TREE_SYNC( 536 1, layer_tree_host_->SetRootLayer(test_layer)); 537 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true)); 538 539 gfx::Size test_bounds = gfx::Size(501, 508); 540 541 gfx::RectF dirty1 = gfx::RectF(10.f, 15.f, 1.f, 2.f); 542 gfx::RectF dirty2 = gfx::RectF(20.f, 25.f, 3.f, 4.f); 543 gfx::RectF empty_dirty_rect = gfx::RectF(40.f, 45.f, 0.f, 0.f); 544 gfx::RectF out_of_bounds_dirty_rect = gfx::RectF(400.f, 405.f, 500.f, 502.f); 545 546 // Before anything, test_layer should not be dirty. 547 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 548 549 // This is just initialization, but SetNeedsCommit behavior is verified anyway 550 // to avoid warnings. 551 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds)); 552 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 553 554 // The real test begins here. 555 test_layer->ResetNeedsDisplayForTesting(); 556 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 557 558 // Case 1: Layer should accept dirty rects that go beyond its bounds. 559 test_layer->ResetNeedsDisplayForTesting(); 560 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 561 EXPECT_SET_NEEDS_UPDATE( 562 1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect)); 563 EXPECT_TRUE(test_layer->NeedsDisplayForTesting()); 564 test_layer->ResetNeedsDisplayForTesting(); 565 566 // Case 2: SetNeedsDisplay() without the dirty rect arg. 567 test_layer->ResetNeedsDisplayForTesting(); 568 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 569 EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay()); 570 EXPECT_TRUE(test_layer->NeedsDisplayForTesting()); 571 test_layer->ResetNeedsDisplayForTesting(); 572 573 // Case 3: SetNeedsDisplay() with an empty rect. 574 test_layer->ResetNeedsDisplayForTesting(); 575 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 576 EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect())); 577 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 578 579 // Case 4: SetNeedsDisplay() with a non-drawable layer 580 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false)); 581 test_layer->ResetNeedsDisplayForTesting(); 582 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 583 EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1)); 584 EXPECT_TRUE(test_layer->NeedsDisplayForTesting()); 585 } 586 587 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) { 588 scoped_refptr<Layer> test_layer = Layer::Create(); 589 EXPECT_SET_NEEDS_FULL_TREE_SYNC( 590 1, layer_tree_host_->SetRootLayer(test_layer)); 591 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true)); 592 593 scoped_refptr<Layer> dummy_layer1 = Layer::Create(); 594 scoped_refptr<Layer> dummy_layer2 = Layer::Create(); 595 596 // sanity check of initial test condition 597 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 598 599 // Next, test properties that should call SetNeedsCommit (but not 600 // SetNeedsDisplay). All properties need to be set to new values in order for 601 // SetNeedsCommit to be called. 602 EXPECT_SET_NEEDS_COMMIT( 603 1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f))); 604 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY)); 605 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true)); 606 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f)); 607 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode)); 608 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true)); 609 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true)); 610 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f))); 611 // We can use any layer pointer here since we aren't syncing for real. 612 EXPECT_SET_NEEDS_COMMIT(1, 613 test_layer->SetScrollClipLayerId(test_layer->id())); 614 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false)); 615 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset( 616 gfx::Vector2d(10, 10))); 617 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true)); 618 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion( 619 Region(gfx::Rect(1, 1, 2, 2)))); 620 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true)); 621 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true)); 622 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform( 623 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0))); 624 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false)); 625 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion( 626 gfx::Rect(10, 10))); 627 EXPECT_SET_NEEDS_COMMIT( 628 1, 629 test_layer->SetDrawCheckerboardForMissingTiles( 630 !test_layer->draw_checkerboard_for_missing_tiles())); 631 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true)); 632 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true)); 633 634 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer( 635 dummy_layer1.get())); 636 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer( 637 dummy_layer2.get())); 638 639 // The above tests should not have caused a change to the needs_display flag. 640 EXPECT_FALSE(test_layer->NeedsDisplayForTesting()); 641 642 // As layers are removed from the tree, they will cause a tree sync. 643 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber())); 644 } 645 646 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) { 647 scoped_refptr<Layer> test_layer = Layer::Create(); 648 scoped_ptr<LayerImpl> impl_layer = 649 LayerImpl::Create(host_impl_.active_tree(), 1); 650 651 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, 652 layer_tree_host_->SetRootLayer(test_layer)); 653 654 test_layer->SetNeedsDisplayRect(gfx::RectF(0.f, 0.f, 5.f, 5.f)); 655 test_layer->PushPropertiesTo(impl_layer.get()); 656 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f), 657 impl_layer->update_rect()); 658 659 // The LayerImpl's update_rect() should be accumulated here, since we did not 660 // do anything to clear it. 661 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f)); 662 test_layer->PushPropertiesTo(impl_layer.get()); 663 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f), 664 impl_layer->update_rect()); 665 666 // If we do clear the LayerImpl side, then the next update_rect() should be 667 // fresh without accumulation. 668 impl_layer->ResetAllChangeTrackingForSubtree(); 669 test_layer->SetNeedsDisplayRect(gfx::RectF(10.f, 10.f, 5.f, 5.f)); 670 test_layer->PushPropertiesTo(impl_layer.get()); 671 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f), 672 impl_layer->update_rect()); 673 } 674 675 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) { 676 scoped_refptr<Layer> test_layer = Layer::Create(); 677 scoped_ptr<LayerImpl> impl_layer = 678 LayerImpl::Create(host_impl_.active_tree(), 1); 679 680 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, 681 layer_tree_host_->SetRootLayer(test_layer)); 682 683 gfx::Transform transform; 684 transform.Rotate(45.0); 685 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform)); 686 687 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 688 689 test_layer->PushPropertiesTo(impl_layer.get()); 690 691 EXPECT_TRUE(impl_layer->LayerPropertyChanged()); 692 } 693 694 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) { 695 scoped_refptr<Layer> test_layer = Layer::Create(); 696 scoped_ptr<LayerImpl> impl_layer = 697 LayerImpl::Create(host_impl_.active_tree(), 1); 698 699 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, 700 layer_tree_host_->SetRootLayer(test_layer)); 701 702 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f)); 703 704 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 705 706 test_layer->PushPropertiesTo(impl_layer.get()); 707 708 EXPECT_TRUE(impl_layer->LayerPropertyChanged()); 709 } 710 711 TEST_F(LayerTest, 712 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) { 713 scoped_refptr<Layer> test_layer = Layer::Create(); 714 scoped_ptr<LayerImpl> impl_layer = 715 LayerImpl::Create(host_impl_.active_tree(), 1); 716 717 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, 718 layer_tree_host_->SetRootLayer(test_layer)); 719 720 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create(); 721 impl_layer->layer_animation_controller()->SetAnimationRegistrar( 722 registrar.get()); 723 724 AddAnimatedTransformToController(impl_layer->layer_animation_controller(), 725 1.0, 726 0, 727 100); 728 729 gfx::Transform transform; 730 transform.Rotate(45.0); 731 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform)); 732 733 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 734 test_layer->PushPropertiesTo(impl_layer.get()); 735 EXPECT_TRUE(impl_layer->LayerPropertyChanged()); 736 737 impl_layer->ResetAllChangeTrackingForSubtree(); 738 AddAnimatedTransformToController(impl_layer->layer_animation_controller(), 739 1.0, 740 0, 741 100); 742 impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)-> 743 set_is_impl_only(true); 744 transform.Rotate(45.0); 745 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform)); 746 747 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 748 test_layer->PushPropertiesTo(impl_layer.get()); 749 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 750 } 751 752 TEST_F(LayerTest, 753 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) { 754 scoped_refptr<Layer> test_layer = Layer::Create(); 755 scoped_ptr<LayerImpl> impl_layer = 756 LayerImpl::Create(host_impl_.active_tree(), 1); 757 758 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, 759 layer_tree_host_->SetRootLayer(test_layer)); 760 761 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create(); 762 impl_layer->layer_animation_controller()->SetAnimationRegistrar( 763 registrar.get()); 764 765 AddOpacityTransitionToController(impl_layer->layer_animation_controller(), 766 1.0, 767 0.3f, 768 0.7f, 769 false); 770 771 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f)); 772 773 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 774 test_layer->PushPropertiesTo(impl_layer.get()); 775 EXPECT_TRUE(impl_layer->LayerPropertyChanged()); 776 777 impl_layer->ResetAllChangeTrackingForSubtree(); 778 AddOpacityTransitionToController(impl_layer->layer_animation_controller(), 779 1.0, 780 0.3f, 781 0.7f, 782 false); 783 impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)-> 784 set_is_impl_only(true); 785 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f)); 786 787 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 788 test_layer->PushPropertiesTo(impl_layer.get()); 789 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 790 } 791 792 TEST_F(LayerTest, 793 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) { 794 scoped_refptr<Layer> test_layer = Layer::Create(); 795 scoped_ptr<LayerImpl> impl_layer = 796 LayerImpl::Create(host_impl_.active_tree(), 1); 797 798 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, 799 layer_tree_host_->SetRootLayer(test_layer)); 800 801 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create(); 802 impl_layer->layer_animation_controller()->SetAnimationRegistrar( 803 registrar.get()); 804 805 AddAnimatedFilterToController( 806 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f); 807 808 FilterOperations filters; 809 filters.Append(FilterOperation::CreateBlurFilter(2.f)); 810 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters)); 811 812 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 813 test_layer->PushPropertiesTo(impl_layer.get()); 814 EXPECT_TRUE(impl_layer->LayerPropertyChanged()); 815 816 impl_layer->ResetAllChangeTrackingForSubtree(); 817 AddAnimatedFilterToController( 818 impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f); 819 impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)-> 820 set_is_impl_only(true); 821 filters.Append(FilterOperation::CreateSepiaFilter(0.5f)); 822 EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters)); 823 824 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 825 test_layer->PushPropertiesTo(impl_layer.get()); 826 EXPECT_FALSE(impl_layer->LayerPropertyChanged()); 827 } 828 829 TEST_F(LayerTest, MaskAndReplicaHasParent) { 830 scoped_refptr<Layer> parent = Layer::Create(); 831 scoped_refptr<Layer> child = Layer::Create(); 832 scoped_refptr<Layer> mask = Layer::Create(); 833 scoped_refptr<Layer> replica = Layer::Create(); 834 scoped_refptr<Layer> replica_mask = Layer::Create(); 835 scoped_refptr<Layer> mask_replacement = Layer::Create(); 836 scoped_refptr<Layer> replica_replacement = Layer::Create(); 837 scoped_refptr<Layer> replica_mask_replacement = Layer::Create(); 838 839 parent->AddChild(child); 840 child->SetMaskLayer(mask.get()); 841 child->SetReplicaLayer(replica.get()); 842 replica->SetMaskLayer(replica_mask.get()); 843 844 EXPECT_EQ(parent, child->parent()); 845 EXPECT_EQ(child, mask->parent()); 846 EXPECT_EQ(child, replica->parent()); 847 EXPECT_EQ(replica, replica_mask->parent()); 848 849 replica->SetMaskLayer(replica_mask_replacement.get()); 850 EXPECT_EQ(NULL, replica_mask->parent()); 851 EXPECT_EQ(replica, replica_mask_replacement->parent()); 852 853 child->SetMaskLayer(mask_replacement.get()); 854 EXPECT_EQ(NULL, mask->parent()); 855 EXPECT_EQ(child, mask_replacement->parent()); 856 857 child->SetReplicaLayer(replica_replacement.get()); 858 EXPECT_EQ(NULL, replica->parent()); 859 EXPECT_EQ(child, replica_replacement->parent()); 860 861 EXPECT_EQ(replica, replica->mask_layer()->parent()); 862 } 863 864 TEST_F(LayerTest, CheckTranformIsInvertible) { 865 scoped_refptr<Layer> layer = Layer::Create(); 866 scoped_ptr<LayerImpl> impl_layer = 867 LayerImpl::Create(host_impl_.active_tree(), 1); 868 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1); 869 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); 870 layer_tree_host_->SetRootLayer(layer); 871 872 EXPECT_TRUE(layer->transform_is_invertible()); 873 874 gfx::Transform singular_transform; 875 singular_transform.Scale3d( 876 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0)); 877 878 layer->SetTransform(singular_transform); 879 layer->PushPropertiesTo(impl_layer.get()); 880 881 EXPECT_FALSE(layer->transform_is_invertible()); 882 EXPECT_FALSE(impl_layer->transform_is_invertible()); 883 884 gfx::Transform rotation_transform; 885 rotation_transform.RotateAboutZAxis(-45.0); 886 887 layer->SetTransform(rotation_transform); 888 layer->PushPropertiesTo(impl_layer.get()); 889 EXPECT_TRUE(layer->transform_is_invertible()); 890 EXPECT_TRUE(impl_layer->transform_is_invertible()); 891 892 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); 893 } 894 895 TEST_F(LayerTest, TranformIsInvertibleAnimation) { 896 scoped_refptr<Layer> layer = Layer::Create(); 897 scoped_ptr<LayerImpl> impl_layer = 898 LayerImpl::Create(host_impl_.active_tree(), 1); 899 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1); 900 EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber()); 901 layer_tree_host_->SetRootLayer(layer); 902 903 EXPECT_TRUE(layer->transform_is_invertible()); 904 905 gfx::Transform singular_transform; 906 singular_transform.Scale3d( 907 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0)); 908 909 layer->SetTransform(singular_transform); 910 layer->PushPropertiesTo(impl_layer.get()); 911 912 EXPECT_FALSE(layer->transform_is_invertible()); 913 EXPECT_FALSE(impl_layer->transform_is_invertible()); 914 915 gfx::Transform identity_transform; 916 917 layer->SetTransform(identity_transform); 918 static_cast<LayerAnimationValueObserver*>(layer) 919 ->OnTransformAnimated(singular_transform); 920 layer->PushPropertiesTo(impl_layer.get()); 921 EXPECT_FALSE(layer->transform_is_invertible()); 922 EXPECT_FALSE(impl_layer->transform_is_invertible()); 923 924 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); 925 } 926 927 class LayerTreeHostFactory { 928 public: 929 LayerTreeHostFactory() 930 : client_(FakeLayerTreeHostClient::DIRECT_3D), 931 shared_bitmap_manager_(new TestSharedBitmapManager()) {} 932 933 scoped_ptr<LayerTreeHost> Create() { 934 return LayerTreeHost::CreateSingleThreaded(&client_, 935 &client_, 936 shared_bitmap_manager_.get(), 937 LayerTreeSettings()).Pass(); 938 } 939 940 scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) { 941 return LayerTreeHost::CreateSingleThreaded( 942 &client_, &client_, shared_bitmap_manager_.get(), settings) 943 .Pass(); 944 } 945 946 private: 947 FakeLayerTreeHostClient client_; 948 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_; 949 }; 950 951 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) { 952 EXPECT_EQ(host, layer->layer_tree_host()); 953 954 for (size_t i = 0; i < layer->children().size(); ++i) 955 AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host); 956 957 if (layer->mask_layer()) 958 AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host); 959 960 if (layer->replica_layer()) 961 AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host); 962 } 963 964 TEST(LayerLayerTreeHostTest, EnteringTree) { 965 scoped_refptr<Layer> parent = Layer::Create(); 966 scoped_refptr<Layer> child = Layer::Create(); 967 scoped_refptr<Layer> mask = Layer::Create(); 968 scoped_refptr<Layer> replica = Layer::Create(); 969 scoped_refptr<Layer> replica_mask = Layer::Create(); 970 971 // Set up a detached tree of layers. The host pointer should be nil for these 972 // layers. 973 parent->AddChild(child); 974 child->SetMaskLayer(mask.get()); 975 child->SetReplicaLayer(replica.get()); 976 replica->SetMaskLayer(replica_mask.get()); 977 978 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL); 979 980 LayerTreeHostFactory factory; 981 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(); 982 // Setting the root layer should set the host pointer for all layers in the 983 // tree. 984 layer_tree_host->SetRootLayer(parent.get()); 985 986 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get()); 987 988 // Clearing the root layer should also clear out the host pointers for all 989 // layers in the tree. 990 layer_tree_host->SetRootLayer(NULL); 991 992 AssertLayerTreeHostMatchesForSubtree(parent.get(), NULL); 993 } 994 995 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) { 996 scoped_refptr<Layer> parent = Layer::Create(); 997 LayerTreeHostFactory factory; 998 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(); 999 1000 layer_tree_host->SetRootLayer(parent.get()); 1001 1002 EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get()); 1003 1004 // Adding a subtree to a layer already associated with a host should set the 1005 // host pointer on all layers in that subtree. 1006 scoped_refptr<Layer> child = Layer::Create(); 1007 scoped_refptr<Layer> grand_child = Layer::Create(); 1008 child->AddChild(grand_child); 1009 1010 // Masks, replicas, and replica masks should pick up the new host too. 1011 scoped_refptr<Layer> child_mask = Layer::Create(); 1012 child->SetMaskLayer(child_mask.get()); 1013 scoped_refptr<Layer> child_replica = Layer::Create(); 1014 child->SetReplicaLayer(child_replica.get()); 1015 scoped_refptr<Layer> child_replica_mask = Layer::Create(); 1016 child_replica->SetMaskLayer(child_replica_mask.get()); 1017 1018 parent->AddChild(child); 1019 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get()); 1020 1021 layer_tree_host->SetRootLayer(NULL); 1022 } 1023 1024 TEST(LayerLayerTreeHostTest, ChangeHost) { 1025 scoped_refptr<Layer> parent = Layer::Create(); 1026 scoped_refptr<Layer> child = Layer::Create(); 1027 scoped_refptr<Layer> mask = Layer::Create(); 1028 scoped_refptr<Layer> replica = Layer::Create(); 1029 scoped_refptr<Layer> replica_mask = Layer::Create(); 1030 1031 // Same setup as the previous test. 1032 parent->AddChild(child); 1033 child->SetMaskLayer(mask.get()); 1034 child->SetReplicaLayer(replica.get()); 1035 replica->SetMaskLayer(replica_mask.get()); 1036 1037 LayerTreeHostFactory factory; 1038 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create(); 1039 first_layer_tree_host->SetRootLayer(parent.get()); 1040 1041 AssertLayerTreeHostMatchesForSubtree(parent.get(), 1042 first_layer_tree_host.get()); 1043 1044 // Now re-root the tree to a new host (simulating what we do on a context lost 1045 // event). This should update the host pointers for all layers in the tree. 1046 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create(); 1047 second_layer_tree_host->SetRootLayer(parent.get()); 1048 1049 AssertLayerTreeHostMatchesForSubtree(parent.get(), 1050 second_layer_tree_host.get()); 1051 1052 second_layer_tree_host->SetRootLayer(NULL); 1053 } 1054 1055 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) { 1056 scoped_refptr<Layer> first_parent = Layer::Create(); 1057 scoped_refptr<Layer> first_child = Layer::Create(); 1058 scoped_refptr<Layer> second_parent = Layer::Create(); 1059 scoped_refptr<Layer> second_child = Layer::Create(); 1060 scoped_refptr<Layer> second_grand_child = Layer::Create(); 1061 1062 // First put all children under the first parent and set the first host. 1063 first_parent->AddChild(first_child); 1064 second_child->AddChild(second_grand_child); 1065 first_parent->AddChild(second_child); 1066 1067 LayerTreeHostFactory factory; 1068 scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create(); 1069 first_layer_tree_host->SetRootLayer(first_parent.get()); 1070 1071 AssertLayerTreeHostMatchesForSubtree(first_parent.get(), 1072 first_layer_tree_host.get()); 1073 1074 // Now reparent the subtree starting at second_child to a layer in a different 1075 // tree. 1076 scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create(); 1077 second_layer_tree_host->SetRootLayer(second_parent.get()); 1078 1079 second_parent->AddChild(second_child); 1080 1081 // The moved layer and its children should point to the new host. 1082 EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host()); 1083 EXPECT_EQ(second_layer_tree_host.get(), 1084 second_grand_child->layer_tree_host()); 1085 1086 // Test over, cleanup time. 1087 first_layer_tree_host->SetRootLayer(NULL); 1088 second_layer_tree_host->SetRootLayer(NULL); 1089 } 1090 1091 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) { 1092 scoped_refptr<Layer> parent = Layer::Create(); 1093 scoped_refptr<Layer> mask = Layer::Create(); 1094 scoped_refptr<Layer> replica = Layer::Create(); 1095 scoped_refptr<Layer> mask_child = Layer::Create(); 1096 scoped_refptr<Layer> replica_child = Layer::Create(); 1097 scoped_refptr<Layer> mask_replacement = Layer::Create(); 1098 scoped_refptr<Layer> replica_replacement = Layer::Create(); 1099 1100 parent->SetMaskLayer(mask.get()); 1101 parent->SetReplicaLayer(replica.get()); 1102 mask->AddChild(mask_child); 1103 replica->AddChild(replica_child); 1104 1105 LayerTreeHostFactory factory; 1106 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(); 1107 layer_tree_host->SetRootLayer(parent.get()); 1108 1109 AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get()); 1110 1111 // Replacing the mask should clear out the old mask's subtree's host pointers. 1112 parent->SetMaskLayer(mask_replacement.get()); 1113 EXPECT_EQ(NULL, mask->layer_tree_host()); 1114 EXPECT_EQ(NULL, mask_child->layer_tree_host()); 1115 1116 // Same for replacing a replica layer. 1117 parent->SetReplicaLayer(replica_replacement.get()); 1118 EXPECT_EQ(NULL, replica->layer_tree_host()); 1119 EXPECT_EQ(NULL, replica_child->layer_tree_host()); 1120 1121 // Test over, cleanup time. 1122 layer_tree_host->SetRootLayer(NULL); 1123 } 1124 1125 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) { 1126 scoped_refptr<Layer> root = Layer::Create(); 1127 scoped_refptr<Layer> child = Layer::Create(); 1128 root->AddChild(child); 1129 LayerTreeHostFactory factory; 1130 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(); 1131 layer_tree_host->SetRootLayer(root); 1132 } 1133 1134 static bool AddTestAnimation(Layer* layer) { 1135 scoped_ptr<KeyframedFloatAnimationCurve> curve = 1136 KeyframedFloatAnimationCurve::Create(); 1137 curve->AddKeyframe(FloatKeyframe::Create(0.0, 1138 0.3f, 1139 scoped_ptr<TimingFunction>())); 1140 curve->AddKeyframe(FloatKeyframe::Create(1.0, 1141 0.7f, 1142 scoped_ptr<TimingFunction>())); 1143 scoped_ptr<Animation> animation = 1144 Animation::Create(curve.PassAs<AnimationCurve>(), 1145 0, 1146 0, 1147 Animation::Opacity); 1148 1149 return layer->AddAnimation(animation.Pass()); 1150 } 1151 1152 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) { 1153 scoped_refptr<Layer> layer = Layer::Create(); 1154 1155 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the 1156 // animation should not be accepted. 1157 EXPECT_FALSE(AddTestAnimation(layer.get())); 1158 1159 scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create(); 1160 layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get()); 1161 1162 // Case 2: with an AnimationRegistrar, the animation should be accepted. 1163 EXPECT_TRUE(AddTestAnimation(layer.get())); 1164 1165 LayerTreeSettings settings; 1166 settings.accelerated_animation_enabled = false; 1167 LayerTreeHostFactory factory; 1168 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings); 1169 layer_tree_host->SetRootLayer(layer); 1170 AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get()); 1171 1172 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the 1173 // animation should be rejected. 1174 EXPECT_FALSE(AddTestAnimation(layer.get())); 1175 } 1176 1177 TEST_F(LayerTest, SafeOpaqueBackgroundColor) { 1178 LayerTreeHostFactory factory; 1179 scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(); 1180 1181 scoped_refptr<Layer> layer = Layer::Create(); 1182 layer_tree_host->SetRootLayer(layer); 1183 1184 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) { 1185 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) { 1186 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) { 1187 layer->SetContentsOpaque(!!contents_opaque); 1188 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED 1189 : SK_ColorTRANSPARENT); 1190 layer_tree_host->set_background_color( 1191 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT); 1192 1193 SkColor safe_color = layer->SafeOpaqueBackgroundColor(); 1194 if (contents_opaque) { 1195 EXPECT_EQ(SkColorGetA(safe_color), 255u) 1196 << "Flags: " << contents_opaque << ", " << layer_opaque << ", " 1197 << host_opaque << "\n"; 1198 } else { 1199 EXPECT_NE(SkColorGetA(safe_color), 255u) 1200 << "Flags: " << contents_opaque << ", " << layer_opaque << ", " 1201 << host_opaque << "\n"; 1202 } 1203 } 1204 } 1205 } 1206 } 1207 1208 } // namespace 1209 } // namespace cc 1210