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