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