blob: aadf11ea3989bc19cef1a28ba745d180925c4ba9 [file] [log] [blame]
[email protected]c0dd24c2012-08-30 23:25:271// 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]a8461d82012-10-16 21:11:147#include "cc/layer.h"
[email protected]c0dd24c2012-08-30 23:25:278
[email protected]d50c6862012-10-23 02:08:319#include "cc/keyframed_animation_curve.h"
10#include "cc/layer_impl.h"
[email protected]a8461d82012-10-16 21:11:1411#include "cc/layer_painter.h"
[email protected]d50c6862012-10-23 02:08:3112#include "cc/layer_tree_host.h"
13#include "cc/settings.h"
[email protected]4456eee22012-10-19 18:16:3814#include "cc/single_thread_proxy.h"
[email protected]101441ce2012-10-16 01:45:0315#include "cc/test/fake_layer_tree_host_client.h"
16#include "cc/test/geometry_test_utils.h"
[email protected]65bfd9972012-10-19 03:39:3717#include "cc/test/test_common.h"
[email protected]7f0c53db2012-10-02 00:23:1818#include "testing/gmock/include/gmock/gmock.h"
19#include "testing/gtest/include/gtest/gtest.h"
[email protected]c0dd24c2012-08-30 23:25:2720#include <public/WebTransformationMatrix.h>
21
[email protected]9c88e562012-09-14 22:21:3022using namespace cc;
[email protected]c0dd24c2012-08-30 23:25:2723using namespace WebKitTests;
24using WebKit::WebTransformationMatrix;
25using ::testing::Mock;
26using ::testing::_;
27using ::testing::AtLeast;
28using ::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
36namespace {
37
[email protected]96baf3e2012-10-22 23:09:5538class MockLayerImplTreeHost : public LayerTreeHost {
[email protected]c0dd24c2012-08-30 23:25:2739public:
[email protected]96baf3e2012-10-22 23:09:5540 MockLayerImplTreeHost()
41 : LayerTreeHost(&m_fakeClient, LayerTreeSettings())
[email protected]c0dd24c2012-08-30 23:25:2742 {
[email protected]5bc29a22012-11-01 21:21:5943 initialize();
[email protected]c0dd24c2012-08-30 23:25:2744 }
45
46 MOCK_METHOD0(setNeedsCommit, void());
47
48private:
[email protected]96baf3e2012-10-22 23:09:5549 FakeLayerImplTreeHostClient m_fakeClient;
[email protected]c0dd24c2012-08-30 23:25:2750};
51
[email protected]96baf3e2012-10-22 23:09:5552class MockLayerPainter : public LayerPainter {
[email protected]c0dd24c2012-08-30 23:25:2753public:
[email protected]f809d3bb2012-10-31 20:52:2554 virtual void paint(SkCanvas*, const gfx::Rect&, gfx::RectF&) OVERRIDE { }
[email protected]c0dd24c2012-08-30 23:25:2755};
56
57
[email protected]96baf3e2012-10-22 23:09:5558class LayerTest : public testing::Test {
[email protected]0f077a52012-09-08 01:45:2459public:
[email protected]96baf3e2012-10-22 23:09:5560 LayerTest()
[email protected]0f077a52012-09-08 01:45:2461 {
62 }
63
[email protected]c0dd24c2012-08-30 23:25:2764protected:
65 virtual void SetUp()
66 {
[email protected]96baf3e2012-10-22 23:09:5567 m_layerTreeHost = scoped_ptr<MockLayerImplTreeHost>(new MockLayerImplTreeHost);
[email protected]c0dd24c2012-08-30 23:25:2768 }
69
70 virtual void TearDown()
71 {
72 Mock::VerifyAndClearExpectations(m_layerTreeHost.get());
73 EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AnyNumber());
[email protected]d58499a2012-10-09 22:27:4774 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]c0dd24c2012-08-30 23:25:2781
82 m_layerTreeHost->setRootLayer(0);
[email protected]519281762012-10-06 20:06:3983 m_layerTreeHost.reset();
[email protected]c0dd24c2012-08-30 23:25:2784 }
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]96baf3e2012-10-22 23:09:55111 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]c0dd24c2012-08-30 23:25:27118
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]96baf3e2012-10-22 23:09:55134 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]c0dd24c2012-08-30 23:25:27136};
137
[email protected]96baf3e2012-10-22 23:09:55138TEST_F(LayerTest, basicCreateAndDestroy)
[email protected]c0dd24c2012-08-30 23:25:27139{
[email protected]96baf3e2012-10-22 23:09:55140 scoped_refptr<Layer> testLayer = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27141 ASSERT_TRUE(testLayer);
142
143 EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(0);
144 testLayer->setLayerTreeHost(m_layerTreeHost.get());
145}
146
[email protected]96baf3e2012-10-22 23:09:55147TEST_F(LayerTest, addAndRemoveChild)
[email protected]c0dd24c2012-08-30 23:25:27148{
[email protected]96baf3e2012-10-22 23:09:55149 scoped_refptr<Layer> parent = Layer::create();
150 scoped_refptr<Layer> child = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27151
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]c0dd24c2012-08-30 23:25:27157 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]96baf3e2012-10-22 23:09:55167TEST_F(LayerTest, insertChild)
[email protected]c0dd24c2012-08-30 23:25:27168{
[email protected]96baf3e2012-10-22 23:09:55169 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]c0dd24c2012-08-30 23:25:27174
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]96baf3e2012-10-22 23:09:55213TEST_F(LayerTest, insertChildPastEndOfList)
[email protected]c0dd24c2012-08-30 23:25:27214{
[email protected]96baf3e2012-10-22 23:09:55215 scoped_refptr<Layer> parent = Layer::create();
216 scoped_refptr<Layer> child1 = Layer::create();
217 scoped_refptr<Layer> child2 = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27218
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]96baf3e2012-10-22 23:09:55235TEST_F(LayerTest, insertSameChildTwice)
[email protected]c0dd24c2012-08-30 23:25:27236{
[email protected]96baf3e2012-10-22 23:09:55237 scoped_refptr<Layer> parent = Layer::create();
238 scoped_refptr<Layer> child1 = Layer::create();
239 scoped_refptr<Layer> child2 = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27240
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]96baf3e2012-10-22 23:09:55263TEST_F(LayerTest, replaceChildWithNewChild)
[email protected]c0dd24c2012-08-30 23:25:27264{
265 createSimpleTestTree();
[email protected]96baf3e2012-10-22 23:09:55266 scoped_refptr<Layer> child4 = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27267
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]96baf3e2012-10-22 23:09:55281TEST_F(LayerTest, replaceChildWithNewChildThatHasOtherParent)
[email protected]c0dd24c2012-08-30 23:25:27282{
283 createSimpleTestTree();
284
285 // create another simple tree with testLayer and child4.
[email protected]96baf3e2012-10-22 23:09:55286 scoped_refptr<Layer> testLayer = Layer::create();
287 scoped_refptr<Layer> child4 = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27288 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]96baf3e2012-10-22 23:09:55307TEST_F(LayerTest, replaceChildWithSameChild)
[email protected]c0dd24c2012-08-30 23:25:27308{
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]96baf3e2012-10-22 23:09:55317TEST_F(LayerTest, removeAllChildren)
[email protected]c0dd24c2012-08-30 23:25:27318{
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]96baf3e2012-10-22 23:09:55329TEST_F(LayerTest, setChildren)
[email protected]c0dd24c2012-08-30 23:25:27330{
[email protected]96baf3e2012-10-22 23:09:55331 scoped_refptr<Layer> oldParent = Layer::create();
332 scoped_refptr<Layer> newParent = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27333
[email protected]96baf3e2012-10-22 23:09:55334 scoped_refptr<Layer> child1 = Layer::create();
335 scoped_refptr<Layer> child2 = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27336
[email protected]96baf3e2012-10-22 23:09:55337 std::vector<scoped_refptr<Layer> > newChildren;
[email protected]d58499a2012-10-09 22:27:47338 newChildren.push_back(child1);
339 newChildren.push_back(child2);
[email protected]c0dd24c2012-08-30 23:25:27340
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]96baf3e2012-10-22 23:09:55358TEST_F(LayerTest, getRootLayerAfterTreeManipulations)
[email protected]c0dd24c2012-08-30 23:25:27359{
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]96baf3e2012-10-22 23:09:55365 scoped_refptr<Layer> child4 = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27366
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]96baf3e2012-10-22 23:09:55412TEST_F(LayerTest, checkSetNeedsDisplayCausesCorrectBehavior)
[email protected]c0dd24c2012-08-30 23:25:27413{
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]96baf3e2012-10-22 23:09:55418 scoped_refptr<Layer> testLayer = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27419 testLayer->setLayerTreeHost(m_layerTreeHost.get());
[email protected]031ccfff2012-10-26 00:57:38420 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
[email protected]c0dd24c2012-08-30 23:25:27421
[email protected]aad0a0072012-11-01 18:15:58422 gfx::Size testBounds = gfx::Size(501, 508);
[email protected]c0dd24c2012-08-30 23:25:27423
[email protected]aad0a0072012-11-01 18:15:58424 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]c0dd24c2012-08-30 23:25:27428
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]96baf3e2012-10-22 23:09:55434 testLayer = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27435 testLayer->setLayerTreeHost(m_layerTreeHost.get());
[email protected]031ccfff2012-10-26 00:57:38436 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
[email protected]c0dd24c2012-08-30 23:25:27437 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]96baf3e2012-10-22 23:09:55453 // Case 4: Layer should accept dirty rects that go beyond its bounds.
454 testLayer = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27455 testLayer->setLayerTreeHost(m_layerTreeHost.get());
[email protected]031ccfff2012-10-26 00:57:38456 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
[email protected]c0dd24c2012-08-30 23:25:27457 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]96baf3e2012-10-22 23:09:55462 testLayer = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27463 testLayer->setLayerTreeHost(m_layerTreeHost.get());
[email protected]031ccfff2012-10-26 00:57:38464 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
[email protected]c0dd24c2012-08-30 23:25:27465 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]031ccfff2012-10-26 00:57:38468
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]c0dd24c2012-08-30 23:25:27475}
476
[email protected]96baf3e2012-10-22 23:09:55477TEST_F(LayerTest, checkPropertyChangeCausesCorrectBehavior)
[email protected]c0dd24c2012-08-30 23:25:27478{
[email protected]96baf3e2012-10-22 23:09:55479 scoped_refptr<Layer> testLayer = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27480 testLayer->setLayerTreeHost(m_layerTreeHost.get());
[email protected]031ccfff2012-10-26 00:57:38481 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
[email protected]c0dd24c2012-08-30 23:25:27482
[email protected]96baf3e2012-10-22 23:09:55483 scoped_refptr<Layer> dummyLayer = Layer::create(); // just a dummy layer for this test case.
[email protected]c0dd24c2012-08-30 23:25:27484
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]aad0a0072012-11-01 18:15:58489 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setVisibleContentRect(gfx::Rect(0, 0, 40, 50)));
[email protected]c0dd24c2012-08-30 23:25:27490 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]aad0a0072012-11-01 18:15:58495 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawableContentRect(gfx::Rect(4, 5, 6, 7)));
[email protected]c0dd24c2012-08-30 23:25:27496 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]aad0a0072012-11-01 18:15:58500 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setAnchorPoint(gfx::PointF(1.23f, 4.56f)));
[email protected]c0dd24c2012-08-30 23:25:27501 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]048634c2012-10-02 22:33:14506 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setContentsOpaque(true));
[email protected]aad0a0072012-11-01 18:15:58507 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setPosition(gfx::PointF(4, 9)));
[email protected]c0dd24c2012-08-30 23:25:27508 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]d0f98362012-11-01 23:02:38512 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNonFastScrollableRegion(gfx::Rect(1, 1, 2, 2)));
[email protected]c0dd24c2012-08-30 23:25:27513 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]aad0a0072012-11-01 18:15:58525 EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(gfx::Size(5, 10)));
[email protected]c0dd24c2012-08-30 23:25:27526 EXPECT_TRUE(testLayer->needsDisplay());
527}
528
[email protected]96baf3e2012-10-22 23:09:55529TEST_F(LayerTest, verifyPushPropertiesAccumulatesUpdateRect)
[email protected]c0dd24c2012-08-30 23:25:27530{
[email protected]5bc29a22012-11-01 21:21:59531 DebugScopedSetImplThread setImplThread;
532
[email protected]96baf3e2012-10-22 23:09:55533 scoped_refptr<Layer> testLayer = Layer::create();
534 scoped_ptr<LayerImpl> implLayer = LayerImpl::create(1);
[email protected]c0dd24c2012-08-30 23:25:27535
[email protected]aad0a0072012-11-01 18:15:58536 testLayer->setNeedsDisplayRect(gfx::RectF(gfx::PointF(), gfx::SizeF(5, 5)));
[email protected]c0dd24c2012-08-30 23:25:27537 testLayer->pushPropertiesTo(implLayer.get());
[email protected]aad0a0072012-11-01 18:15:58538 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(), gfx::SizeF(5, 5)), implLayer->updateRect());
[email protected]c0dd24c2012-08-30 23:25:27539
[email protected]96baf3e2012-10-22 23:09:55540 // The LayerImpl's updateRect should be accumulated here, since we did not do anything to clear it.
[email protected]aad0a0072012-11-01 18:15:58541 testLayer->setNeedsDisplayRect(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)));
[email protected]c0dd24c2012-08-30 23:25:27542 testLayer->pushPropertiesTo(implLayer.get());
[email protected]aad0a0072012-11-01 18:15:58543 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(), gfx::SizeF(15, 15)), implLayer->updateRect());
[email protected]c0dd24c2012-08-30 23:25:27544
[email protected]96baf3e2012-10-22 23:09:55545 // If we do clear the LayerImpl side, then the next updateRect should be fresh without accumulation.
[email protected]c0dd24c2012-08-30 23:25:27546 implLayer->resetAllChangeTrackingForSubtree();
[email protected]aad0a0072012-11-01 18:15:58547 testLayer->setNeedsDisplayRect(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)));
[email protected]c0dd24c2012-08-30 23:25:27548 testLayer->pushPropertiesTo(implLayer.get());
[email protected]aad0a0072012-11-01 18:15:58549 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)), implLayer->updateRect());
[email protected]c0dd24c2012-08-30 23:25:27550}
551
[email protected]96baf3e2012-10-22 23:09:55552class FakeLayerImplTreeHost : public LayerTreeHost {
[email protected]c0dd24c2012-08-30 23:25:27553public:
[email protected]96baf3e2012-10-22 23:09:55554 static scoped_ptr<FakeLayerImplTreeHost> create()
[email protected]c0dd24c2012-08-30 23:25:27555 {
[email protected]96baf3e2012-10-22 23:09:55556 scoped_ptr<FakeLayerImplTreeHost> host(new FakeLayerImplTreeHost);
[email protected]c0dd24c2012-08-30 23:25:27557 // 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]5bc29a22012-11-01 21:21:59558 host->initialize();
[email protected]519281762012-10-06 20:06:39559 return host.Pass();
[email protected]c0dd24c2012-08-30 23:25:27560 }
561
562private:
[email protected]96baf3e2012-10-22 23:09:55563 FakeLayerImplTreeHost()
564 : LayerTreeHost(&m_client, LayerTreeSettings())
[email protected]c0dd24c2012-08-30 23:25:27565 {
566 }
567
[email protected]96baf3e2012-10-22 23:09:55568 FakeLayerImplTreeHostClient m_client;
[email protected]c0dd24c2012-08-30 23:25:27569};
570
[email protected]96baf3e2012-10-22 23:09:55571void assertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host)
[email protected]c0dd24c2012-08-30 23:25:27572{
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]96baf3e2012-10-22 23:09:55586TEST(LayerLayerTreeHostTest, enteringTree)
[email protected]c0dd24c2012-08-30 23:25:27587{
[email protected]96baf3e2012-10-22 23:09:55588 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]c0dd24c2012-08-30 23:25:27593
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]96baf3e2012-10-22 23:09:55602 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27603 // 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]c0dd24c2012-08-30 23:25:27612}
613
[email protected]96baf3e2012-10-22 23:09:55614TEST(LayerLayerTreeHostTest, addingLayerSubtree)
[email protected]c0dd24c2012-08-30 23:25:27615{
[email protected]96baf3e2012-10-22 23:09:55616 scoped_refptr<Layer> parent = Layer::create();
617 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27618
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]96baf3e2012-10-22 23:09:55624 scoped_refptr<Layer> child = Layer::create();
625 scoped_refptr<Layer> grandChild = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27626 child->addChild(grandChild);
627
628 // Masks, replicas, and replica masks should pick up the new host too.
[email protected]96baf3e2012-10-22 23:09:55629 scoped_refptr<Layer> childMask = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27630 child->setMaskLayer(childMask.get());
[email protected]96baf3e2012-10-22 23:09:55631 scoped_refptr<Layer> childReplica = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27632 child->setReplicaLayer(childReplica.get());
[email protected]96baf3e2012-10-22 23:09:55633 scoped_refptr<Layer> childReplicaMask = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27634 childReplica->setMaskLayer(childReplicaMask.get());
635
636 parent->addChild(child);
637 assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
638
639 layerTreeHost->setRootLayer(0);
[email protected]c0dd24c2012-08-30 23:25:27640}
641
[email protected]96baf3e2012-10-22 23:09:55642TEST(LayerLayerTreeHostTest, changeHost)
[email protected]c0dd24c2012-08-30 23:25:27643{
[email protected]96baf3e2012-10-22 23:09:55644 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]c0dd24c2012-08-30 23:25:27649
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]96baf3e2012-10-22 23:09:55656 scoped_ptr<FakeLayerImplTreeHost> firstLayerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27657 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]96baf3e2012-10-22 23:09:55663 scoped_ptr<FakeLayerImplTreeHost> secondLayerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27664 secondLayerTreeHost->setRootLayer(parent.get());
665
666 assertLayerTreeHostMatchesForSubtree(parent.get(), secondLayerTreeHost.get());
667
668 secondLayerTreeHost->setRootLayer(0);
[email protected]c0dd24c2012-08-30 23:25:27669}
670
[email protected]96baf3e2012-10-22 23:09:55671TEST(LayerLayerTreeHostTest, changeHostInSubtree)
[email protected]c0dd24c2012-08-30 23:25:27672{
[email protected]96baf3e2012-10-22 23:09:55673 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]c0dd24c2012-08-30 23:25:27678
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]96baf3e2012-10-22 23:09:55684 scoped_ptr<FakeLayerImplTreeHost> firstLayerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27685 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]96baf3e2012-10-22 23:09:55690 scoped_ptr<FakeLayerImplTreeHost> secondLayerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27691 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]c0dd24c2012-08-30 23:25:27702}
703
[email protected]96baf3e2012-10-22 23:09:55704TEST(LayerLayerTreeHostTest, replaceMaskAndReplicaLayer)
[email protected]c0dd24c2012-08-30 23:25:27705{
[email protected]96baf3e2012-10-22 23:09:55706 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]c0dd24c2012-08-30 23:25:27713
714 parent->setMaskLayer(mask.get());
715 parent->setReplicaLayer(replica.get());
716 mask->addChild(maskChild);
717 replica->addChild(replicaChild);
718
[email protected]96baf3e2012-10-22 23:09:55719 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27720 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]c0dd24c2012-08-30 23:25:27736}
737
[email protected]96baf3e2012-10-22 23:09:55738TEST(LayerLayerTreeHostTest, destroyHostWithNonNullRootLayer)
[email protected]c0dd24c2012-08-30 23:25:27739{
[email protected]96baf3e2012-10-22 23:09:55740 scoped_refptr<Layer> root = Layer::create();
741 scoped_refptr<Layer> child = Layer::create();
[email protected]c0dd24c2012-08-30 23:25:27742 root->addChild(child);
[email protected]96baf3e2012-10-22 23:09:55743 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c0dd24c2012-08-30 23:25:27744 layerTreeHost->setRootLayer(root);
[email protected]c0dd24c2012-08-30 23:25:27745}
746
[email protected]96baf3e2012-10-22 23:09:55747static bool addTestAnimation(Layer* layer)
[email protected]c5d374052012-09-14 19:57:44748{
[email protected]96baf3e2012-10-22 23:09:55749 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]c5d374052012-09-14 19:57:44753
[email protected]9aca3592012-10-12 15:57:09754 return layer->addAnimation(animation.Pass());
[email protected]c5d374052012-09-14 19:57:44755}
756
[email protected]96baf3e2012-10-22 23:09:55757TEST(LayerLayerTreeHostTest, shouldNotAddAnimationWithoutLayerTreeHost)
[email protected]c5d374052012-09-14 19:57:44758{
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]96baf3e2012-10-22 23:09:55764 ScopedSettings scopedSettings;
[email protected]65bfd9972012-10-19 03:39:37765 Settings::setAcceleratedAnimationEnabled(true);
[email protected]c5d374052012-09-14 19:57:44766
[email protected]96baf3e2012-10-22 23:09:55767 scoped_refptr<Layer> layer = Layer::create();
[email protected]c5d374052012-09-14 19:57:44768
769 // Case 1: without a layerTreeHost, the animation should not be accepted.
770 EXPECT_FALSE(addTestAnimation(layer.get()));
771
[email protected]96baf3e2012-10-22 23:09:55772 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
[email protected]c5d374052012-09-14 19:57:44773 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]96baf3e2012-10-22 23:09:55781class MockLayer : public Layer {
[email protected]c0dd24c2012-08-30 23:25:27782public:
783 bool needsDisplay() const { return m_needsDisplay; }
[email protected]d58499a2012-10-09 22:27:47784
785private:
[email protected]96baf3e2012-10-22 23:09:55786 virtual ~MockLayer()
[email protected]d58499a2012-10-09 22:27:47787 {
788 }
[email protected]c0dd24c2012-08-30 23:25:27789};
790
[email protected]96baf3e2012-10-22 23:09:55791TEST(LayerTestWithoutFixture, setBoundsTriggersSetNeedsRedrawAfterGettingNonEmptyBounds)
[email protected]c0dd24c2012-08-30 23:25:27792{
[email protected]96baf3e2012-10-22 23:09:55793 scoped_refptr<MockLayer> layer(new MockLayer);
[email protected]c0dd24c2012-08-30 23:25:27794 EXPECT_FALSE(layer->needsDisplay());
[email protected]aad0a0072012-11-01 18:15:58795 layer->setBounds(gfx::Size(0, 10));
[email protected]c0dd24c2012-08-30 23:25:27796 EXPECT_FALSE(layer->needsDisplay());
[email protected]aad0a0072012-11-01 18:15:58797 layer->setBounds(gfx::Size(10, 10));
[email protected]c0dd24c2012-08-30 23:25:27798 EXPECT_TRUE(layer->needsDisplay());
799}
800
801
802} // namespace