[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 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 "config.h" |
| 6 | |
| 7 | #include "LayerChromium.h" |
| 8 | |
[email protected] | 022cbf16 | 2012-09-01 01:15:17 | [diff] [blame] | 9 | #include "CCGeometryTestUtils.h" |
[email protected] | c5d37405 | 2012-09-14 19:57:44 | [diff] [blame] | 10 | #include "CCKeyframedAnimationCurve.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 11 | #include "CCLayerImpl.h" |
| 12 | #include "CCLayerTreeHost.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 13 | #include "CCSingleThreadProxy.h" |
| 14 | #include "FakeCCLayerTreeHostClient.h" |
| 15 | #include "LayerPainterChromium.h" |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 16 | #include "WebCompositorInitializer.h" |
[email protected] | 7f0c53db | 2012-10-02 00:23:18 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 19 | #include <public/WebTransformationMatrix.h> |
| 20 | |
[email protected] | 9c88e56 | 2012-09-14 22:21:30 | [diff] [blame] | 21 | using namespace cc; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 22 | using namespace WebKitTests; |
| 23 | using WebKit::WebTransformationMatrix; |
| 24 | using ::testing::Mock; |
| 25 | using ::testing::_; |
| 26 | using ::testing::AtLeast; |
| 27 | using ::testing::AnyNumber; |
| 28 | |
| 29 | #define EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(numTimesExpectedSetNeedsCommit, codeToTest) do { \ |
| 30 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times((numTimesExpectedSetNeedsCommit)); \ |
| 31 | codeToTest; \ |
| 32 | Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); \ |
| 33 | } while (0) |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | class MockCCLayerTreeHost : public CCLayerTreeHost { |
| 38 | public: |
| 39 | MockCCLayerTreeHost() |
| 40 | : CCLayerTreeHost(&m_fakeClient, CCLayerTreeSettings()) |
| 41 | { |
| 42 | initialize(); |
| 43 | } |
| 44 | |
| 45 | MOCK_METHOD0(setNeedsCommit, void()); |
| 46 | |
| 47 | private: |
| 48 | FakeCCLayerTreeHostClient m_fakeClient; |
| 49 | }; |
| 50 | |
| 51 | class MockLayerPainterChromium : public LayerPainterChromium { |
| 52 | public: |
| 53 | virtual void paint(SkCanvas*, const IntRect&, FloatRect&) OVERRIDE { } |
| 54 | }; |
| 55 | |
| 56 | |
| 57 | class LayerChromiumTest : public testing::Test { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 58 | public: |
| 59 | LayerChromiumTest() |
| 60 | : m_compositorInitializer(0) |
| 61 | { |
| 62 | } |
| 63 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 64 | protected: |
| 65 | virtual void SetUp() |
| 66 | { |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 67 | m_layerTreeHost = scoped_ptr<MockCCLayerTreeHost>(new MockCCLayerTreeHost); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | virtual void TearDown() |
| 71 | { |
| 72 | Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); |
| 73 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AnyNumber()); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 74 | m_parent = NULL; |
| 75 | m_child1 = NULL; |
| 76 | m_child2 = NULL; |
| 77 | m_child3 = NULL; |
| 78 | m_grandChild1 = NULL; |
| 79 | m_grandChild2 = NULL; |
| 80 | m_grandChild3 = NULL; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 81 | |
| 82 | m_layerTreeHost->setRootLayer(0); |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 83 | m_layerTreeHost.reset(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void verifyTestTreeInitialState() const |
| 87 | { |
| 88 | ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size()); |
| 89 | EXPECT_EQ(m_child1, m_parent->children()[0]); |
| 90 | EXPECT_EQ(m_child2, m_parent->children()[1]); |
| 91 | EXPECT_EQ(m_child3, m_parent->children()[2]); |
| 92 | EXPECT_EQ(m_parent.get(), m_child1->parent()); |
| 93 | EXPECT_EQ(m_parent.get(), m_child2->parent()); |
| 94 | EXPECT_EQ(m_parent.get(), m_child3->parent()); |
| 95 | |
| 96 | ASSERT_EQ(static_cast<size_t>(2), m_child1->children().size()); |
| 97 | EXPECT_EQ(m_grandChild1, m_child1->children()[0]); |
| 98 | EXPECT_EQ(m_grandChild2, m_child1->children()[1]); |
| 99 | EXPECT_EQ(m_child1.get(), m_grandChild1->parent()); |
| 100 | EXPECT_EQ(m_child1.get(), m_grandChild2->parent()); |
| 101 | |
| 102 | ASSERT_EQ(static_cast<size_t>(1), m_child2->children().size()); |
| 103 | EXPECT_EQ(m_grandChild3, m_child2->children()[0]); |
| 104 | EXPECT_EQ(m_child2.get(), m_grandChild3->parent()); |
| 105 | |
| 106 | ASSERT_EQ(static_cast<size_t>(0), m_child3->children().size()); |
| 107 | } |
| 108 | |
| 109 | void createSimpleTestTree() |
| 110 | { |
| 111 | m_parent = LayerChromium::create(); |
| 112 | m_child1 = LayerChromium::create(); |
| 113 | m_child2 = LayerChromium::create(); |
| 114 | m_child3 = LayerChromium::create(); |
| 115 | m_grandChild1 = LayerChromium::create(); |
| 116 | m_grandChild2 = LayerChromium::create(); |
| 117 | m_grandChild3 = LayerChromium::create(); |
| 118 | |
| 119 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AnyNumber()); |
| 120 | m_layerTreeHost->setRootLayer(m_parent); |
| 121 | |
| 122 | m_parent->addChild(m_child1); |
| 123 | m_parent->addChild(m_child2); |
| 124 | m_parent->addChild(m_child3); |
| 125 | m_child1->addChild(m_grandChild1); |
| 126 | m_child1->addChild(m_grandChild2); |
| 127 | m_child2->addChild(m_grandChild3); |
| 128 | |
| 129 | Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); |
| 130 | |
| 131 | verifyTestTreeInitialState(); |
| 132 | } |
| 133 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 134 | scoped_ptr<MockCCLayerTreeHost> m_layerTreeHost; |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 135 | scoped_refptr<LayerChromium> m_parent, m_child1, m_child2, m_child3, m_grandChild1, m_grandChild2, m_grandChild3; |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 136 | WebCompositorInitializer m_compositorInitializer; |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | TEST_F(LayerChromiumTest, basicCreateAndDestroy) |
| 140 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 141 | scoped_refptr<LayerChromium> testLayer = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 142 | ASSERT_TRUE(testLayer); |
| 143 | |
| 144 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(0); |
| 145 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 146 | } |
| 147 | |
| 148 | TEST_F(LayerChromiumTest, addAndRemoveChild) |
| 149 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 150 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 151 | scoped_refptr<LayerChromium> child = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 152 | |
| 153 | // Upon creation, layers should not have children or parent. |
| 154 | ASSERT_EQ(static_cast<size_t>(0), parent->children().size()); |
| 155 | EXPECT_FALSE(child->parent()); |
| 156 | |
| 157 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, m_layerTreeHost->setRootLayer(parent)); |
| 158 | |
| 159 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->addChild(child)); |
| 160 | |
| 161 | ASSERT_EQ(static_cast<size_t>(1), parent->children().size()); |
| 162 | EXPECT_EQ(child.get(), parent->children()[0]); |
| 163 | EXPECT_EQ(parent.get(), child->parent()); |
| 164 | EXPECT_EQ(parent.get(), child->rootLayer()); |
| 165 | |
| 166 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), child->removeFromParent()); |
| 167 | } |
| 168 | |
| 169 | TEST_F(LayerChromiumTest, insertChild) |
| 170 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 171 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 172 | scoped_refptr<LayerChromium> child1 = LayerChromium::create(); |
| 173 | scoped_refptr<LayerChromium> child2 = LayerChromium::create(); |
| 174 | scoped_refptr<LayerChromium> child3 = LayerChromium::create(); |
| 175 | scoped_refptr<LayerChromium> child4 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 176 | |
| 177 | parent->setLayerTreeHost(m_layerTreeHost.get()); |
| 178 | |
| 179 | ASSERT_EQ(static_cast<size_t>(0), parent->children().size()); |
| 180 | |
| 181 | // Case 1: inserting to empty list. |
| 182 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child3, 0)); |
| 183 | ASSERT_EQ(static_cast<size_t>(1), parent->children().size()); |
| 184 | EXPECT_EQ(child3, parent->children()[0]); |
| 185 | EXPECT_EQ(parent.get(), child3->parent()); |
| 186 | |
| 187 | // Case 2: inserting to beginning of list |
| 188 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child1, 0)); |
| 189 | ASSERT_EQ(static_cast<size_t>(2), parent->children().size()); |
| 190 | EXPECT_EQ(child1, parent->children()[0]); |
| 191 | EXPECT_EQ(child3, parent->children()[1]); |
| 192 | EXPECT_EQ(parent.get(), child1->parent()); |
| 193 | |
| 194 | // Case 3: inserting to middle of list |
| 195 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child2, 1)); |
| 196 | ASSERT_EQ(static_cast<size_t>(3), parent->children().size()); |
| 197 | EXPECT_EQ(child1, parent->children()[0]); |
| 198 | EXPECT_EQ(child2, parent->children()[1]); |
| 199 | EXPECT_EQ(child3, parent->children()[2]); |
| 200 | EXPECT_EQ(parent.get(), child2->parent()); |
| 201 | |
| 202 | // Case 4: inserting to end of list |
| 203 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child4, 3)); |
| 204 | |
| 205 | ASSERT_EQ(static_cast<size_t>(4), parent->children().size()); |
| 206 | EXPECT_EQ(child1, parent->children()[0]); |
| 207 | EXPECT_EQ(child2, parent->children()[1]); |
| 208 | EXPECT_EQ(child3, parent->children()[2]); |
| 209 | EXPECT_EQ(child4, parent->children()[3]); |
| 210 | EXPECT_EQ(parent.get(), child4->parent()); |
| 211 | |
| 212 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1)); |
| 213 | } |
| 214 | |
| 215 | TEST_F(LayerChromiumTest, insertChildPastEndOfList) |
| 216 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 217 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 218 | scoped_refptr<LayerChromium> child1 = LayerChromium::create(); |
| 219 | scoped_refptr<LayerChromium> child2 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 220 | |
| 221 | ASSERT_EQ(static_cast<size_t>(0), parent->children().size()); |
| 222 | |
| 223 | // insert to an out-of-bounds index |
| 224 | parent->insertChild(child1, 53); |
| 225 | |
| 226 | ASSERT_EQ(static_cast<size_t>(1), parent->children().size()); |
| 227 | EXPECT_EQ(child1, parent->children()[0]); |
| 228 | |
| 229 | // insert another child to out-of-bounds, when list is not already empty. |
| 230 | parent->insertChild(child2, 2459); |
| 231 | |
| 232 | ASSERT_EQ(static_cast<size_t>(2), parent->children().size()); |
| 233 | EXPECT_EQ(child1, parent->children()[0]); |
| 234 | EXPECT_EQ(child2, parent->children()[1]); |
| 235 | } |
| 236 | |
| 237 | TEST_F(LayerChromiumTest, insertSameChildTwice) |
| 238 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 239 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 240 | scoped_refptr<LayerChromium> child1 = LayerChromium::create(); |
| 241 | scoped_refptr<LayerChromium> child2 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 242 | |
| 243 | parent->setLayerTreeHost(m_layerTreeHost.get()); |
| 244 | |
| 245 | ASSERT_EQ(static_cast<size_t>(0), parent->children().size()); |
| 246 | |
| 247 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child1, 0)); |
| 248 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child2, 1)); |
| 249 | |
| 250 | ASSERT_EQ(static_cast<size_t>(2), parent->children().size()); |
| 251 | EXPECT_EQ(child1, parent->children()[0]); |
| 252 | EXPECT_EQ(child2, parent->children()[1]); |
| 253 | |
| 254 | // Inserting the same child again should cause the child to be removed and re-inserted at the new location. |
| 255 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), parent->insertChild(child1, 1)); |
| 256 | |
| 257 | // child1 should now be at the end of the list. |
| 258 | ASSERT_EQ(static_cast<size_t>(2), parent->children().size()); |
| 259 | EXPECT_EQ(child2, parent->children()[0]); |
| 260 | EXPECT_EQ(child1, parent->children()[1]); |
| 261 | |
| 262 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1)); |
| 263 | } |
| 264 | |
| 265 | TEST_F(LayerChromiumTest, replaceChildWithNewChild) |
| 266 | { |
| 267 | createSimpleTestTree(); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 268 | scoped_refptr<LayerChromium> child4 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 269 | |
| 270 | EXPECT_FALSE(child4->parent()); |
| 271 | |
| 272 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), m_parent->replaceChild(m_child2.get(), child4)); |
| 273 | |
| 274 | ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size()); |
| 275 | EXPECT_EQ(m_child1, m_parent->children()[0]); |
| 276 | EXPECT_EQ(child4, m_parent->children()[1]); |
| 277 | EXPECT_EQ(m_child3, m_parent->children()[2]); |
| 278 | EXPECT_EQ(m_parent.get(), child4->parent()); |
| 279 | |
| 280 | EXPECT_FALSE(m_child2->parent()); |
| 281 | } |
| 282 | |
| 283 | TEST_F(LayerChromiumTest, replaceChildWithNewChildThatHasOtherParent) |
| 284 | { |
| 285 | createSimpleTestTree(); |
| 286 | |
| 287 | // create another simple tree with testLayer and child4. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 288 | scoped_refptr<LayerChromium> testLayer = LayerChromium::create(); |
| 289 | scoped_refptr<LayerChromium> child4 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 290 | testLayer->addChild(child4); |
| 291 | ASSERT_EQ(static_cast<size_t>(1), testLayer->children().size()); |
| 292 | EXPECT_EQ(child4, testLayer->children()[0]); |
| 293 | EXPECT_EQ(testLayer.get(), child4->parent()); |
| 294 | |
| 295 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), m_parent->replaceChild(m_child2.get(), child4)); |
| 296 | |
| 297 | ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size()); |
| 298 | EXPECT_EQ(m_child1, m_parent->children()[0]); |
| 299 | EXPECT_EQ(child4, m_parent->children()[1]); |
| 300 | EXPECT_EQ(m_child3, m_parent->children()[2]); |
| 301 | EXPECT_EQ(m_parent.get(), child4->parent()); |
| 302 | |
| 303 | // testLayer should no longer have child4, |
| 304 | // and child2 should no longer have a parent. |
| 305 | ASSERT_EQ(static_cast<size_t>(0), testLayer->children().size()); |
| 306 | EXPECT_FALSE(m_child2->parent()); |
| 307 | } |
| 308 | |
| 309 | TEST_F(LayerChromiumTest, replaceChildWithSameChild) |
| 310 | { |
| 311 | createSimpleTestTree(); |
| 312 | |
| 313 | // setNeedsCommit should not be called because its the same child |
| 314 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, m_parent->replaceChild(m_child2.get(), m_child2)); |
| 315 | |
| 316 | verifyTestTreeInitialState(); |
| 317 | } |
| 318 | |
| 319 | TEST_F(LayerChromiumTest, removeAllChildren) |
| 320 | { |
| 321 | createSimpleTestTree(); |
| 322 | |
| 323 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(3), m_parent->removeAllChildren()); |
| 324 | |
| 325 | ASSERT_EQ(static_cast<size_t>(0), m_parent->children().size()); |
| 326 | EXPECT_FALSE(m_child1->parent()); |
| 327 | EXPECT_FALSE(m_child2->parent()); |
| 328 | EXPECT_FALSE(m_child3->parent()); |
| 329 | } |
| 330 | |
| 331 | TEST_F(LayerChromiumTest, setChildren) |
| 332 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 333 | scoped_refptr<LayerChromium> oldParent = LayerChromium::create(); |
| 334 | scoped_refptr<LayerChromium> newParent = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 335 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 336 | scoped_refptr<LayerChromium> child1 = LayerChromium::create(); |
| 337 | scoped_refptr<LayerChromium> child2 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 338 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 339 | std::vector<scoped_refptr<LayerChromium> > newChildren; |
| 340 | newChildren.push_back(child1); |
| 341 | newChildren.push_back(child2); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 342 | |
| 343 | // Set up and verify initial test conditions: child1 has a parent, child2 has no parent. |
| 344 | oldParent->addChild(child1); |
| 345 | ASSERT_EQ(static_cast<size_t>(0), newParent->children().size()); |
| 346 | EXPECT_EQ(oldParent.get(), child1->parent()); |
| 347 | EXPECT_FALSE(child2->parent()); |
| 348 | |
| 349 | newParent->setLayerTreeHost(m_layerTreeHost.get()); |
| 350 | |
| 351 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), newParent->setChildren(newChildren)); |
| 352 | |
| 353 | ASSERT_EQ(static_cast<size_t>(2), newParent->children().size()); |
| 354 | EXPECT_EQ(newParent.get(), child1->parent()); |
| 355 | EXPECT_EQ(newParent.get(), child2->parent()); |
| 356 | |
| 357 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1)); |
| 358 | } |
| 359 | |
| 360 | TEST_F(LayerChromiumTest, getRootLayerAfterTreeManipulations) |
| 361 | { |
| 362 | createSimpleTestTree(); |
| 363 | |
| 364 | // For this test we don't care about setNeedsCommit calls. |
| 365 | EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1)); |
| 366 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 367 | scoped_refptr<LayerChromium> child4 = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 368 | |
| 369 | EXPECT_EQ(m_parent.get(), m_parent->rootLayer()); |
| 370 | EXPECT_EQ(m_parent.get(), m_child1->rootLayer()); |
| 371 | EXPECT_EQ(m_parent.get(), m_child2->rootLayer()); |
| 372 | EXPECT_EQ(m_parent.get(), m_child3->rootLayer()); |
| 373 | EXPECT_EQ(child4.get(), child4->rootLayer()); |
| 374 | EXPECT_EQ(m_parent.get(), m_grandChild1->rootLayer()); |
| 375 | EXPECT_EQ(m_parent.get(), m_grandChild2->rootLayer()); |
| 376 | EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer()); |
| 377 | |
| 378 | m_child1->removeFromParent(); |
| 379 | |
| 380 | // child1 and its children, grandChild1 and grandChild2 are now on a separate subtree. |
| 381 | EXPECT_EQ(m_parent.get(), m_parent->rootLayer()); |
| 382 | EXPECT_EQ(m_child1.get(), m_child1->rootLayer()); |
| 383 | EXPECT_EQ(m_parent.get(), m_child2->rootLayer()); |
| 384 | EXPECT_EQ(m_parent.get(), m_child3->rootLayer()); |
| 385 | EXPECT_EQ(child4.get(), child4->rootLayer()); |
| 386 | EXPECT_EQ(m_child1.get(), m_grandChild1->rootLayer()); |
| 387 | EXPECT_EQ(m_child1.get(), m_grandChild2->rootLayer()); |
| 388 | EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer()); |
| 389 | |
| 390 | m_grandChild3->addChild(child4); |
| 391 | |
| 392 | EXPECT_EQ(m_parent.get(), m_parent->rootLayer()); |
| 393 | EXPECT_EQ(m_child1.get(), m_child1->rootLayer()); |
| 394 | EXPECT_EQ(m_parent.get(), m_child2->rootLayer()); |
| 395 | EXPECT_EQ(m_parent.get(), m_child3->rootLayer()); |
| 396 | EXPECT_EQ(m_parent.get(), child4->rootLayer()); |
| 397 | EXPECT_EQ(m_child1.get(), m_grandChild1->rootLayer()); |
| 398 | EXPECT_EQ(m_child1.get(), m_grandChild2->rootLayer()); |
| 399 | EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer()); |
| 400 | |
| 401 | m_child2->replaceChild(m_grandChild3.get(), m_child1); |
| 402 | |
| 403 | // grandChild3 gets orphaned and the child1 subtree gets planted back into the tree under child2. |
| 404 | EXPECT_EQ(m_parent.get(), m_parent->rootLayer()); |
| 405 | EXPECT_EQ(m_parent.get(), m_child1->rootLayer()); |
| 406 | EXPECT_EQ(m_parent.get(), m_child2->rootLayer()); |
| 407 | EXPECT_EQ(m_parent.get(), m_child3->rootLayer()); |
| 408 | EXPECT_EQ(m_grandChild3.get(), child4->rootLayer()); |
| 409 | EXPECT_EQ(m_parent.get(), m_grandChild1->rootLayer()); |
| 410 | EXPECT_EQ(m_parent.get(), m_grandChild2->rootLayer()); |
| 411 | EXPECT_EQ(m_grandChild3.get(), m_grandChild3->rootLayer()); |
| 412 | } |
| 413 | |
| 414 | TEST_F(LayerChromiumTest, checkSetNeedsDisplayCausesCorrectBehavior) |
| 415 | { |
| 416 | // The semantics for setNeedsDisplay which are tested here: |
| 417 | // 1. sets needsDisplay flag appropriately. |
| 418 | // 2. indirectly calls setNeedsCommit, exactly once for each call to setNeedsDisplay. |
| 419 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 420 | scoped_refptr<LayerChromium> testLayer = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 421 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 422 | |
| 423 | IntSize testBounds = IntSize(501, 508); |
| 424 | |
| 425 | FloatRect dirty1 = FloatRect(10, 15, 1, 2); |
| 426 | FloatRect dirty2 = FloatRect(20, 25, 3, 4); |
| 427 | FloatRect emptyDirtyRect = FloatRect(40, 45, 0, 0); |
| 428 | FloatRect outOfBoundsDirtyRect = FloatRect(400, 405, 500, 502); |
| 429 | |
| 430 | // Before anything, testLayer should not be dirty. |
| 431 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 432 | |
| 433 | // This is just initialization, but setNeedsCommit behavior is verified anyway to avoid warnings. |
| 434 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds)); |
| 435 | testLayer = LayerChromium::create(); |
| 436 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 437 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 438 | |
| 439 | // The real test begins here. |
| 440 | |
| 441 | // Case 1: needsDisplay flag should not change because of an empty dirty rect. |
| 442 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(emptyDirtyRect)); |
| 443 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 444 | |
| 445 | // Case 2: basic. |
| 446 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(dirty1)); |
| 447 | EXPECT_TRUE(testLayer->needsDisplay()); |
| 448 | |
| 449 | // Case 3: a second dirty rect. |
| 450 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(dirty2)); |
| 451 | EXPECT_TRUE(testLayer->needsDisplay()); |
| 452 | |
| 453 | // Case 4: LayerChromium should accept dirty rects that go beyond its bounds. |
| 454 | testLayer = LayerChromium::create(); |
| 455 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 456 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds)); |
| 457 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(outOfBoundsDirtyRect)); |
| 458 | EXPECT_TRUE(testLayer->needsDisplay()); |
| 459 | |
| 460 | // Case 5: setNeedsDisplay() without the dirty rect arg. |
| 461 | testLayer = LayerChromium::create(); |
| 462 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 463 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds)); |
| 464 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplay()); |
| 465 | EXPECT_TRUE(testLayer->needsDisplay()); |
| 466 | } |
| 467 | |
| 468 | TEST_F(LayerChromiumTest, checkPropertyChangeCausesCorrectBehavior) |
| 469 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 470 | scoped_refptr<LayerChromium> testLayer = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 471 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 472 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 473 | scoped_refptr<LayerChromium> dummyLayer = LayerChromium::create(); // just a dummy layer for this test case. |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 474 | |
| 475 | // sanity check of initial test condition |
| 476 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 477 | |
| 478 | // Test properties that should not call needsDisplay and needsCommit when changed. |
| 479 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setVisibleContentRect(IntRect(0, 0, 40, 50))); |
| 480 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setUseLCDText(true)); |
| 481 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawOpacity(0.5)); |
| 482 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setRenderTarget(0)); |
| 483 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawTransform(WebTransformationMatrix())); |
| 484 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setScreenSpaceTransform(WebTransformationMatrix())); |
| 485 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawableContentRect(IntRect(4, 5, 6, 7))); |
| 486 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 487 | |
| 488 | // Next, test properties that should call setNeedsCommit (but not setNeedsDisplay) |
| 489 | // All properties need to be set to new values in order for setNeedsCommit to be called. |
| 490 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setAnchorPoint(FloatPoint(1.23f, 4.56f))); |
| 491 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setAnchorPointZ(0.7f)); |
| 492 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBackgroundColor(SK_ColorLTGRAY)); |
| 493 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setMasksToBounds(true)); |
| 494 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setMaskLayer(dummyLayer.get())); |
| 495 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setOpacity(0.5)); |
[email protected] | 048634c | 2012-10-02 22:33:14 | [diff] [blame] | 496 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setContentsOpaque(true)); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 497 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setPosition(FloatPoint(4, 9))); |
| 498 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setReplicaLayer(dummyLayer.get())); |
| 499 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setSublayerTransform(WebTransformationMatrix(0, 0, 0, 0, 0, 0))); |
| 500 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setScrollable(true)); |
| 501 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setShouldScrollOnMainThread(true)); |
| 502 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNonFastScrollableRegion(Region(IntRect(1, 1, 2, 2)))); |
| 503 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setHaveWheelEventHandlers(true)); |
| 504 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setScrollPosition(IntPoint(10, 10))); |
| 505 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setTransform(WebTransformationMatrix(0, 0, 0, 0, 0, 0))); |
| 506 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setDoubleSided(false)); |
| 507 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setDebugName("Test Layer")); |
| 508 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setDrawCheckerboardForMissingTiles(!testLayer->drawCheckerboardForMissingTiles())); |
| 509 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setForceRenderSurface(true)); |
| 510 | |
| 511 | // The above tests should not have caused a change to the needsDisplay flag. |
| 512 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 513 | |
| 514 | // Test properties that should call setNeedsDisplay and setNeedsCommit |
| 515 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(IntSize(5, 10))); |
| 516 | EXPECT_TRUE(testLayer->needsDisplay()); |
| 517 | } |
| 518 | |
| 519 | TEST_F(LayerChromiumTest, verifyPushPropertiesAccumulatesUpdateRect) |
| 520 | { |
| 521 | DebugScopedSetImplThread setImplThread; |
| 522 | |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 523 | scoped_refptr<LayerChromium> testLayer = LayerChromium::create(); |
[email protected] | a36c6649 | 2012-10-12 03:33:40 | [diff] [blame] | 524 | OwnPtr<CCLayerImpl> implLayer = CCLayerImpl::create(1); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 525 | |
| 526 | testLayer->setNeedsDisplayRect(FloatRect(FloatPoint::zero(), FloatSize(5, 5))); |
| 527 | testLayer->pushPropertiesTo(implLayer.get()); |
| 528 | EXPECT_FLOAT_RECT_EQ(FloatRect(FloatPoint::zero(), FloatSize(5, 5)), implLayer->updateRect()); |
| 529 | |
| 530 | // The CCLayerImpl's updateRect should be accumulated here, since we did not do anything to clear it. |
| 531 | testLayer->setNeedsDisplayRect(FloatRect(FloatPoint(10, 10), FloatSize(5, 5))); |
| 532 | testLayer->pushPropertiesTo(implLayer.get()); |
| 533 | EXPECT_FLOAT_RECT_EQ(FloatRect(FloatPoint::zero(), FloatSize(15, 15)), implLayer->updateRect()); |
| 534 | |
| 535 | // If we do clear the CCLayerImpl side, then the next updateRect should be fresh without accumulation. |
| 536 | implLayer->resetAllChangeTrackingForSubtree(); |
| 537 | testLayer->setNeedsDisplayRect(FloatRect(FloatPoint(10, 10), FloatSize(5, 5))); |
| 538 | testLayer->pushPropertiesTo(implLayer.get()); |
| 539 | EXPECT_FLOAT_RECT_EQ(FloatRect(FloatPoint(10, 10), FloatSize(5, 5)), implLayer->updateRect()); |
| 540 | } |
| 541 | |
| 542 | class LayerChromiumWithContentScaling : public LayerChromium { |
| 543 | public: |
| 544 | explicit LayerChromiumWithContentScaling() |
| 545 | : LayerChromium() |
| 546 | { |
| 547 | } |
| 548 | |
| 549 | virtual bool needsContentsScale() const OVERRIDE |
| 550 | { |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | virtual void setNeedsDisplayRect(const FloatRect& dirtyRect) OVERRIDE |
| 555 | { |
| 556 | m_lastNeedsDisplayRect = dirtyRect; |
| 557 | LayerChromium::setNeedsDisplayRect(dirtyRect); |
| 558 | } |
| 559 | |
| 560 | void resetNeedsDisplay() |
| 561 | { |
| 562 | m_needsDisplay = false; |
| 563 | } |
| 564 | |
| 565 | const FloatRect& lastNeedsDisplayRect() const { return m_lastNeedsDisplayRect; } |
| 566 | |
| 567 | private: |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 568 | virtual ~LayerChromiumWithContentScaling() |
| 569 | { |
| 570 | } |
| 571 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 572 | FloatRect m_lastNeedsDisplayRect; |
| 573 | }; |
| 574 | |
| 575 | TEST_F(LayerChromiumTest, checkContentsScaleChangeTriggersNeedsDisplay) |
| 576 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 577 | scoped_refptr<LayerChromiumWithContentScaling> testLayer = make_scoped_refptr(new LayerChromiumWithContentScaling()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 578 | testLayer->setLayerTreeHost(m_layerTreeHost.get()); |
| 579 | |
| 580 | IntSize testBounds = IntSize(320, 240); |
| 581 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds)); |
| 582 | |
| 583 | testLayer->resetNeedsDisplay(); |
| 584 | EXPECT_FALSE(testLayer->needsDisplay()); |
| 585 | |
| 586 | EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setContentsScale(testLayer->contentsScale() + 1.f)); |
| 587 | EXPECT_TRUE(testLayer->needsDisplay()); |
| 588 | EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 320, 240), testLayer->lastNeedsDisplayRect()); |
| 589 | } |
| 590 | |
| 591 | class FakeCCLayerTreeHost : public CCLayerTreeHost { |
| 592 | public: |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 593 | static scoped_ptr<FakeCCLayerTreeHost> create() |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 594 | { |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 595 | scoped_ptr<FakeCCLayerTreeHost> host(new FakeCCLayerTreeHost); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 596 | // The initialize call will fail, since our client doesn't provide a valid GraphicsContext3D, but it doesn't matter in the tests that use this fake so ignore the return value. |
| 597 | host->initialize(); |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 598 | return host.Pass(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | private: |
| 602 | FakeCCLayerTreeHost() |
| 603 | : CCLayerTreeHost(&m_client, CCLayerTreeSettings()) |
| 604 | { |
| 605 | } |
| 606 | |
| 607 | FakeCCLayerTreeHostClient m_client; |
| 608 | }; |
| 609 | |
| 610 | void assertLayerTreeHostMatchesForSubtree(LayerChromium* layer, CCLayerTreeHost* host) |
| 611 | { |
| 612 | EXPECT_EQ(host, layer->layerTreeHost()); |
| 613 | |
| 614 | for (size_t i = 0; i < layer->children().size(); ++i) |
| 615 | assertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host); |
| 616 | |
| 617 | if (layer->maskLayer()) |
| 618 | assertLayerTreeHostMatchesForSubtree(layer->maskLayer(), host); |
| 619 | |
| 620 | if (layer->replicaLayer()) |
| 621 | assertLayerTreeHostMatchesForSubtree(layer->replicaLayer(), host); |
| 622 | } |
| 623 | |
| 624 | |
| 625 | TEST(LayerChromiumLayerTreeHostTest, enteringTree) |
| 626 | { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 627 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 628 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 629 | scoped_refptr<LayerChromium> child = LayerChromium::create(); |
| 630 | scoped_refptr<LayerChromium> mask = LayerChromium::create(); |
| 631 | scoped_refptr<LayerChromium> replica = LayerChromium::create(); |
| 632 | scoped_refptr<LayerChromium> replicaMask = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 633 | |
| 634 | // Set up a detached tree of layers. The host pointer should be nil for these layers. |
| 635 | parent->addChild(child); |
| 636 | child->setMaskLayer(mask.get()); |
| 637 | child->setReplicaLayer(replica.get()); |
| 638 | replica->setMaskLayer(mask.get()); |
| 639 | |
| 640 | assertLayerTreeHostMatchesForSubtree(parent.get(), 0); |
| 641 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 642 | scoped_ptr<FakeCCLayerTreeHost> layerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 643 | // Setting the root layer should set the host pointer for all layers in the tree. |
| 644 | layerTreeHost->setRootLayer(parent.get()); |
| 645 | |
| 646 | assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get()); |
| 647 | |
| 648 | // Clearing the root layer should also clear out the host pointers for all layers in the tree. |
| 649 | layerTreeHost->setRootLayer(0); |
| 650 | |
| 651 | assertLayerTreeHostMatchesForSubtree(parent.get(), 0); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | TEST(LayerChromiumLayerTreeHostTest, addingLayerSubtree) |
| 655 | { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 656 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 657 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 658 | scoped_ptr<FakeCCLayerTreeHost> layerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 659 | |
| 660 | layerTreeHost->setRootLayer(parent.get()); |
| 661 | |
| 662 | EXPECT_EQ(parent->layerTreeHost(), layerTreeHost.get()); |
| 663 | |
| 664 | // Adding a subtree to a layer already associated with a host should set the host pointer on all layers in that subtree. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 665 | scoped_refptr<LayerChromium> child = LayerChromium::create(); |
| 666 | scoped_refptr<LayerChromium> grandChild = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 667 | child->addChild(grandChild); |
| 668 | |
| 669 | // Masks, replicas, and replica masks should pick up the new host too. |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 670 | scoped_refptr<LayerChromium> childMask = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 671 | child->setMaskLayer(childMask.get()); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 672 | scoped_refptr<LayerChromium> childReplica = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 673 | child->setReplicaLayer(childReplica.get()); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 674 | scoped_refptr<LayerChromium> childReplicaMask = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 675 | childReplica->setMaskLayer(childReplicaMask.get()); |
| 676 | |
| 677 | parent->addChild(child); |
| 678 | assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get()); |
| 679 | |
| 680 | layerTreeHost->setRootLayer(0); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | TEST(LayerChromiumLayerTreeHostTest, changeHost) |
| 684 | { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 685 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 686 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 687 | scoped_refptr<LayerChromium> child = LayerChromium::create(); |
| 688 | scoped_refptr<LayerChromium> mask = LayerChromium::create(); |
| 689 | scoped_refptr<LayerChromium> replica = LayerChromium::create(); |
| 690 | scoped_refptr<LayerChromium> replicaMask = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 691 | |
| 692 | // Same setup as the previous test. |
| 693 | parent->addChild(child); |
| 694 | child->setMaskLayer(mask.get()); |
| 695 | child->setReplicaLayer(replica.get()); |
| 696 | replica->setMaskLayer(mask.get()); |
| 697 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 698 | scoped_ptr<FakeCCLayerTreeHost> firstLayerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 699 | firstLayerTreeHost->setRootLayer(parent.get()); |
| 700 | |
| 701 | assertLayerTreeHostMatchesForSubtree(parent.get(), firstLayerTreeHost.get()); |
| 702 | |
| 703 | // Now re-root the tree to a new host (simulating what we do on a context lost event). |
| 704 | // This should update the host pointers for all layers in the tree. |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 705 | scoped_ptr<FakeCCLayerTreeHost> secondLayerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 706 | secondLayerTreeHost->setRootLayer(parent.get()); |
| 707 | |
| 708 | assertLayerTreeHostMatchesForSubtree(parent.get(), secondLayerTreeHost.get()); |
| 709 | |
| 710 | secondLayerTreeHost->setRootLayer(0); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | TEST(LayerChromiumLayerTreeHostTest, changeHostInSubtree) |
| 714 | { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 715 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 716 | scoped_refptr<LayerChromium> firstParent = LayerChromium::create(); |
| 717 | scoped_refptr<LayerChromium> firstChild = LayerChromium::create(); |
| 718 | scoped_refptr<LayerChromium> secondParent = LayerChromium::create(); |
| 719 | scoped_refptr<LayerChromium> secondChild = LayerChromium::create(); |
| 720 | scoped_refptr<LayerChromium> secondGrandChild = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 721 | |
| 722 | // First put all children under the first parent and set the first host. |
| 723 | firstParent->addChild(firstChild); |
| 724 | secondChild->addChild(secondGrandChild); |
| 725 | firstParent->addChild(secondChild); |
| 726 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 727 | scoped_ptr<FakeCCLayerTreeHost> firstLayerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 728 | firstLayerTreeHost->setRootLayer(firstParent.get()); |
| 729 | |
| 730 | assertLayerTreeHostMatchesForSubtree(firstParent.get(), firstLayerTreeHost.get()); |
| 731 | |
| 732 | // Now reparent the subtree starting at secondChild to a layer in a different tree. |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 733 | scoped_ptr<FakeCCLayerTreeHost> secondLayerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 734 | secondLayerTreeHost->setRootLayer(secondParent.get()); |
| 735 | |
| 736 | secondParent->addChild(secondChild); |
| 737 | |
| 738 | // The moved layer and its children should point to the new host. |
| 739 | EXPECT_EQ(secondLayerTreeHost.get(), secondChild->layerTreeHost()); |
| 740 | EXPECT_EQ(secondLayerTreeHost.get(), secondGrandChild->layerTreeHost()); |
| 741 | |
| 742 | // Test over, cleanup time. |
| 743 | firstLayerTreeHost->setRootLayer(0); |
| 744 | secondLayerTreeHost->setRootLayer(0); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | TEST(LayerChromiumLayerTreeHostTest, replaceMaskAndReplicaLayer) |
| 748 | { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 749 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 750 | scoped_refptr<LayerChromium> parent = LayerChromium::create(); |
| 751 | scoped_refptr<LayerChromium> mask = LayerChromium::create(); |
| 752 | scoped_refptr<LayerChromium> replica = LayerChromium::create(); |
| 753 | scoped_refptr<LayerChromium> maskChild = LayerChromium::create(); |
| 754 | scoped_refptr<LayerChromium> replicaChild = LayerChromium::create(); |
| 755 | scoped_refptr<LayerChromium> maskReplacement = LayerChromium::create(); |
| 756 | scoped_refptr<LayerChromium> replicaReplacement = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 757 | |
| 758 | parent->setMaskLayer(mask.get()); |
| 759 | parent->setReplicaLayer(replica.get()); |
| 760 | mask->addChild(maskChild); |
| 761 | replica->addChild(replicaChild); |
| 762 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 763 | scoped_ptr<FakeCCLayerTreeHost> layerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 764 | layerTreeHost->setRootLayer(parent.get()); |
| 765 | |
| 766 | assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get()); |
| 767 | |
| 768 | // Replacing the mask should clear out the old mask's subtree's host pointers. |
| 769 | parent->setMaskLayer(maskReplacement.get()); |
| 770 | EXPECT_EQ(0, mask->layerTreeHost()); |
| 771 | EXPECT_EQ(0, maskChild->layerTreeHost()); |
| 772 | |
| 773 | // Same for replacing a replica layer. |
| 774 | parent->setReplicaLayer(replicaReplacement.get()); |
| 775 | EXPECT_EQ(0, replica->layerTreeHost()); |
| 776 | EXPECT_EQ(0, replicaChild->layerTreeHost()); |
| 777 | |
| 778 | // Test over, cleanup time. |
| 779 | layerTreeHost->setRootLayer(0); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | TEST(LayerChromiumLayerTreeHostTest, destroyHostWithNonNullRootLayer) |
| 783 | { |
[email protected] | 0f077a5 | 2012-09-08 01:45:24 | [diff] [blame] | 784 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 785 | scoped_refptr<LayerChromium> root = LayerChromium::create(); |
| 786 | scoped_refptr<LayerChromium> child = LayerChromium::create(); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 787 | root->addChild(child); |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 788 | scoped_ptr<FakeCCLayerTreeHost> layerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 789 | layerTreeHost->setRootLayer(root); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 790 | } |
| 791 | |
[email protected] | c5d37405 | 2012-09-14 19:57:44 | [diff] [blame] | 792 | static bool addTestAnimation(LayerChromium* layer) |
| 793 | { |
[email protected] | 9aca359 | 2012-10-12 15:57:09 | [diff] [blame^] | 794 | scoped_ptr<CCKeyframedFloatAnimationCurve> curve(CCKeyframedFloatAnimationCurve::create()); |
| 795 | curve->addKeyframe(CCFloatKeyframe::create(0, 0.3f, scoped_ptr<CCTimingFunction>())); |
| 796 | curve->addKeyframe(CCFloatKeyframe::create(1, 0.7f, scoped_ptr<CCTimingFunction>())); |
| 797 | scoped_ptr<CCActiveAnimation> animation(CCActiveAnimation::create(curve.PassAs<CCAnimationCurve>(), 0, 0, CCActiveAnimation::Opacity)); |
[email protected] | c5d37405 | 2012-09-14 19:57:44 | [diff] [blame] | 798 | |
[email protected] | 9aca359 | 2012-10-12 15:57:09 | [diff] [blame^] | 799 | return layer->addAnimation(animation.Pass()); |
[email protected] | c5d37405 | 2012-09-14 19:57:44 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | TEST(LayerChromiumLayerTreeHostTest, shouldNotAddAnimationWithoutLayerTreeHost) |
| 803 | { |
| 804 | // Currently, WebCore assumes that animations will be started immediately / very soon |
| 805 | // if a composited layer's addAnimation() returns true. However, without a layerTreeHost, |
| 806 | // layers cannot actually animate yet. So, to prevent violating this WebCore assumption, |
| 807 | // the animation should not be accepted if the layer doesn't already have a layerTreeHost. |
| 808 | |
| 809 | WebKit::Platform::current()->compositorSupport()->setAcceleratedAnimationEnabled(true); |
| 810 | |
| 811 | WebCompositorInitializer compositorInitializer(0); |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 812 | scoped_refptr<LayerChromium> layer = LayerChromium::create(); |
[email protected] | c5d37405 | 2012-09-14 19:57:44 | [diff] [blame] | 813 | |
| 814 | // Case 1: without a layerTreeHost, the animation should not be accepted. |
| 815 | EXPECT_FALSE(addTestAnimation(layer.get())); |
| 816 | |
[email protected] | 51928176 | 2012-10-06 20:06:39 | [diff] [blame] | 817 | scoped_ptr<FakeCCLayerTreeHost> layerTreeHost(FakeCCLayerTreeHost::create()); |
[email protected] | c5d37405 | 2012-09-14 19:57:44 | [diff] [blame] | 818 | layerTreeHost->setRootLayer(layer.get()); |
| 819 | layer->setLayerTreeHost(layerTreeHost.get()); |
| 820 | assertLayerTreeHostMatchesForSubtree(layer.get(), layerTreeHost.get()); |
| 821 | |
| 822 | // Case 2: with a layerTreeHost, the animation should be accepted. |
| 823 | EXPECT_TRUE(addTestAnimation(layer.get())); |
| 824 | } |
| 825 | |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 826 | class MockLayerChromium : public LayerChromium { |
| 827 | public: |
| 828 | bool needsDisplay() const { return m_needsDisplay; } |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 829 | |
| 830 | private: |
| 831 | virtual ~MockLayerChromium() |
| 832 | { |
| 833 | } |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 834 | }; |
| 835 | |
| 836 | TEST(LayerChromiumTestWithoutFixture, setBoundsTriggersSetNeedsRedrawAfterGettingNonEmptyBounds) |
| 837 | { |
[email protected] | d58499a | 2012-10-09 22:27:47 | [diff] [blame] | 838 | scoped_refptr<MockLayerChromium> layer(new MockLayerChromium); |
[email protected] | c0dd24c | 2012-08-30 23:25:27 | [diff] [blame] | 839 | EXPECT_FALSE(layer->needsDisplay()); |
| 840 | layer->setBounds(IntSize(0, 10)); |
| 841 | EXPECT_FALSE(layer->needsDisplay()); |
| 842 | layer->setBounds(IntSize(10, 10)); |
| 843 | EXPECT_TRUE(layer->needsDisplay()); |
| 844 | } |
| 845 | |
| 846 | |
| 847 | } // namespace |