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.
7 #include "cc/keyframed_animation_curve.h"
8 #include "cc/layer_impl.h"
9 #include "cc/layer_painter.h"
10 #include "cc/layer_tree_host.h"
11 #include "cc/math_util.h"
12 #include "cc/single_thread_proxy.h"
13 #include "cc/test/fake_impl_proxy.h"
14 #include "cc/test/fake_layer_tree_host_client.h"
15 #include "cc/test/fake_layer_tree_host_impl.h"
16 #include "cc/test/geometry_test_utils.h"
17 #include "cc/thread.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gfx/transform.h"
22 using ::testing::AnyNumber
;
23 using ::testing::AtLeast
;
24 using ::testing::Mock
;
25 using ::testing::StrictMock
;
28 #define EXPECT_SET_NEEDS_COMMIT(expect, codeToTest) do { \
29 EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times((expect)); \
31 Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); \
34 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, codeToTest) do { \
35 EXPECT_CALL(*m_layerTreeHost, setNeedsFullTreeSync()).Times((expect)); \
37 Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); \
44 class MockLayerImplTreeHost
: public LayerTreeHost
{
46 MockLayerImplTreeHost()
47 : LayerTreeHost(&m_fakeClient
, LayerTreeSettings())
49 initialize(scoped_ptr
<Thread
>(NULL
));
52 MOCK_METHOD0(setNeedsCommit
, void());
53 MOCK_METHOD0(setNeedsFullTreeSync
, void());
56 FakeLayerImplTreeHostClient m_fakeClient
;
59 class MockLayerPainter
: public LayerPainter
{
61 virtual void paint(SkCanvas
*, gfx::Rect
, gfx::RectF
&) OVERRIDE
{ }
65 class LayerTest
: public testing::Test
{
68 : m_hostImpl(&m_proxy
)
75 m_layerTreeHost
.reset(new StrictMock
<MockLayerImplTreeHost
>);
78 virtual void TearDown()
80 Mock::VerifyAndClearExpectations(m_layerTreeHost
.get());
81 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AnyNumber());
90 m_layerTreeHost
->setRootLayer(0);
91 m_layerTreeHost
.reset();
94 void verifyTestTreeInitialState() const
96 ASSERT_EQ(static_cast<size_t>(3), m_parent
->children().size());
97 EXPECT_EQ(m_child1
, m_parent
->children()[0]);
98 EXPECT_EQ(m_child2
, m_parent
->children()[1]);
99 EXPECT_EQ(m_child3
, m_parent
->children()[2]);
100 EXPECT_EQ(m_parent
.get(), m_child1
->parent());
101 EXPECT_EQ(m_parent
.get(), m_child2
->parent());
102 EXPECT_EQ(m_parent
.get(), m_child3
->parent());
104 ASSERT_EQ(static_cast<size_t>(2), m_child1
->children().size());
105 EXPECT_EQ(m_grandChild1
, m_child1
->children()[0]);
106 EXPECT_EQ(m_grandChild2
, m_child1
->children()[1]);
107 EXPECT_EQ(m_child1
.get(), m_grandChild1
->parent());
108 EXPECT_EQ(m_child1
.get(), m_grandChild2
->parent());
110 ASSERT_EQ(static_cast<size_t>(1), m_child2
->children().size());
111 EXPECT_EQ(m_grandChild3
, m_child2
->children()[0]);
112 EXPECT_EQ(m_child2
.get(), m_grandChild3
->parent());
114 ASSERT_EQ(static_cast<size_t>(0), m_child3
->children().size());
117 void createSimpleTestTree()
119 m_parent
= Layer::create();
120 m_child1
= Layer::create();
121 m_child2
= Layer::create();
122 m_child3
= Layer::create();
123 m_grandChild1
= Layer::create();
124 m_grandChild2
= Layer::create();
125 m_grandChild3
= Layer::create();
127 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AnyNumber());
128 m_layerTreeHost
->setRootLayer(m_parent
);
130 m_parent
->addChild(m_child1
);
131 m_parent
->addChild(m_child2
);
132 m_parent
->addChild(m_child3
);
133 m_child1
->addChild(m_grandChild1
);
134 m_child1
->addChild(m_grandChild2
);
135 m_child2
->addChild(m_grandChild3
);
137 Mock::VerifyAndClearExpectations(m_layerTreeHost
.get());
139 verifyTestTreeInitialState();
142 FakeImplProxy m_proxy
;
143 FakeLayerTreeHostImpl m_hostImpl
;
145 scoped_ptr
<StrictMock
<MockLayerImplTreeHost
> > m_layerTreeHost
;
146 scoped_refptr
<Layer
> m_parent
;
147 scoped_refptr
<Layer
> m_child1
;
148 scoped_refptr
<Layer
> m_child2
;
149 scoped_refptr
<Layer
> m_child3
;
150 scoped_refptr
<Layer
> m_grandChild1
;
151 scoped_refptr
<Layer
> m_grandChild2
;
152 scoped_refptr
<Layer
> m_grandChild3
;
155 TEST_F(LayerTest
, basicCreateAndDestroy
)
157 scoped_refptr
<Layer
> testLayer
= Layer::create();
158 ASSERT_TRUE(testLayer
);
160 EXPECT_CALL(*m_layerTreeHost
, setNeedsCommit()).Times(0);
161 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
164 TEST_F(LayerTest
, addAndRemoveChild
)
166 scoped_refptr
<Layer
> parent
= Layer::create();
167 scoped_refptr
<Layer
> child
= Layer::create();
169 // Upon creation, layers should not have children or parent.
170 ASSERT_EQ(static_cast<size_t>(0), parent
->children().size());
171 EXPECT_FALSE(child
->parent());
173 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, m_layerTreeHost
->setRootLayer(parent
));
174 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->addChild(child
));
176 ASSERT_EQ(static_cast<size_t>(1), parent
->children().size());
177 EXPECT_EQ(child
.get(), parent
->children()[0]);
178 EXPECT_EQ(parent
.get(), child
->parent());
179 EXPECT_EQ(parent
.get(), child
->rootLayer());
181 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child
->removeFromParent());
184 TEST_F(LayerTest
, addSameChildTwice
)
186 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AtLeast(1));
188 scoped_refptr
<Layer
> parent
= Layer::create();
189 scoped_refptr
<Layer
> child
= Layer::create();
191 m_layerTreeHost
->setRootLayer(parent
);
193 ASSERT_EQ(0u, parent
->children().size());
195 parent
->addChild(child
);
196 ASSERT_EQ(1u, parent
->children().size());
197 EXPECT_EQ(parent
.get(), child
->parent());
199 parent
->addChild(child
);
200 ASSERT_EQ(1u, parent
->children().size());
201 EXPECT_EQ(parent
.get(), child
->parent());
204 TEST_F(LayerTest
, insertChild
)
206 scoped_refptr
<Layer
> parent
= Layer::create();
207 scoped_refptr
<Layer
> child1
= Layer::create();
208 scoped_refptr
<Layer
> child2
= Layer::create();
209 scoped_refptr
<Layer
> child3
= Layer::create();
210 scoped_refptr
<Layer
> child4
= Layer::create();
212 parent
->setLayerTreeHost(m_layerTreeHost
.get());
214 ASSERT_EQ(static_cast<size_t>(0), parent
->children().size());
216 // Case 1: inserting to empty list.
217 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->insertChild(child3
, 0));
218 ASSERT_EQ(static_cast<size_t>(1), parent
->children().size());
219 EXPECT_EQ(child3
, parent
->children()[0]);
220 EXPECT_EQ(parent
.get(), child3
->parent());
222 // Case 2: inserting to beginning of list
223 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->insertChild(child1
, 0));
224 ASSERT_EQ(static_cast<size_t>(2), parent
->children().size());
225 EXPECT_EQ(child1
, parent
->children()[0]);
226 EXPECT_EQ(child3
, parent
->children()[1]);
227 EXPECT_EQ(parent
.get(), child1
->parent());
229 // Case 3: inserting to middle of list
230 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->insertChild(child2
, 1));
231 ASSERT_EQ(static_cast<size_t>(3), parent
->children().size());
232 EXPECT_EQ(child1
, parent
->children()[0]);
233 EXPECT_EQ(child2
, parent
->children()[1]);
234 EXPECT_EQ(child3
, parent
->children()[2]);
235 EXPECT_EQ(parent
.get(), child2
->parent());
237 // Case 4: inserting to end of list
238 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->insertChild(child4
, 3));
240 ASSERT_EQ(static_cast<size_t>(4), parent
->children().size());
241 EXPECT_EQ(child1
, parent
->children()[0]);
242 EXPECT_EQ(child2
, parent
->children()[1]);
243 EXPECT_EQ(child3
, parent
->children()[2]);
244 EXPECT_EQ(child4
, parent
->children()[3]);
245 EXPECT_EQ(parent
.get(), child4
->parent());
247 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AtLeast(1));
250 TEST_F(LayerTest
, insertChildPastEndOfList
)
252 scoped_refptr
<Layer
> parent
= Layer::create();
253 scoped_refptr
<Layer
> child1
= Layer::create();
254 scoped_refptr
<Layer
> child2
= Layer::create();
256 ASSERT_EQ(static_cast<size_t>(0), parent
->children().size());
258 // insert to an out-of-bounds index
259 parent
->insertChild(child1
, 53);
261 ASSERT_EQ(static_cast<size_t>(1), parent
->children().size());
262 EXPECT_EQ(child1
, parent
->children()[0]);
264 // insert another child to out-of-bounds, when list is not already empty.
265 parent
->insertChild(child2
, 2459);
267 ASSERT_EQ(static_cast<size_t>(2), parent
->children().size());
268 EXPECT_EQ(child1
, parent
->children()[0]);
269 EXPECT_EQ(child2
, parent
->children()[1]);
272 TEST_F(LayerTest
, insertSameChildTwice
)
274 scoped_refptr
<Layer
> parent
= Layer::create();
275 scoped_refptr
<Layer
> child1
= Layer::create();
276 scoped_refptr
<Layer
> child2
= Layer::create();
278 parent
->setLayerTreeHost(m_layerTreeHost
.get());
280 ASSERT_EQ(static_cast<size_t>(0), parent
->children().size());
282 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->insertChild(child1
, 0));
283 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->insertChild(child2
, 1));
285 ASSERT_EQ(static_cast<size_t>(2), parent
->children().size());
286 EXPECT_EQ(child1
, parent
->children()[0]);
287 EXPECT_EQ(child2
, parent
->children()[1]);
289 // Inserting the same child again should cause the child to be removed and re-inserted at the new location.
290 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent
->insertChild(child1
, 1));
292 // child1 should now be at the end of the list.
293 ASSERT_EQ(static_cast<size_t>(2), parent
->children().size());
294 EXPECT_EQ(child2
, parent
->children()[0]);
295 EXPECT_EQ(child1
, parent
->children()[1]);
297 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AtLeast(1));
300 TEST_F(LayerTest
, replaceChildWithNewChild
)
302 createSimpleTestTree();
303 scoped_refptr
<Layer
> child4
= Layer::create();
305 EXPECT_FALSE(child4
->parent());
307 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), m_parent
->replaceChild(m_child2
.get(), child4
));
309 ASSERT_EQ(static_cast<size_t>(3), m_parent
->children().size());
310 EXPECT_EQ(m_child1
, m_parent
->children()[0]);
311 EXPECT_EQ(child4
, m_parent
->children()[1]);
312 EXPECT_EQ(m_child3
, m_parent
->children()[2]);
313 EXPECT_EQ(m_parent
.get(), child4
->parent());
315 EXPECT_FALSE(m_child2
->parent());
318 TEST_F(LayerTest
, replaceChildWithNewChildThatHasOtherParent
)
320 createSimpleTestTree();
322 // create another simple tree with testLayer and child4.
323 scoped_refptr
<Layer
> testLayer
= Layer::create();
324 scoped_refptr
<Layer
> child4
= Layer::create();
325 testLayer
->addChild(child4
);
326 ASSERT_EQ(static_cast<size_t>(1), testLayer
->children().size());
327 EXPECT_EQ(child4
, testLayer
->children()[0]);
328 EXPECT_EQ(testLayer
.get(), child4
->parent());
330 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), m_parent
->replaceChild(m_child2
.get(), child4
));
332 ASSERT_EQ(static_cast<size_t>(3), m_parent
->children().size());
333 EXPECT_EQ(m_child1
, m_parent
->children()[0]);
334 EXPECT_EQ(child4
, m_parent
->children()[1]);
335 EXPECT_EQ(m_child3
, m_parent
->children()[2]);
336 EXPECT_EQ(m_parent
.get(), child4
->parent());
338 // testLayer should no longer have child4,
339 // and child2 should no longer have a parent.
340 ASSERT_EQ(static_cast<size_t>(0), testLayer
->children().size());
341 EXPECT_FALSE(m_child2
->parent());
344 TEST_F(LayerTest
, replaceChildWithSameChild
)
346 createSimpleTestTree();
348 // setNeedsFullTreeSync / setNeedsCommit should not be called because its the same child
349 EXPECT_CALL(*m_layerTreeHost
, setNeedsCommit()).Times(0);
350 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(0);
351 m_parent
->replaceChild(m_child2
.get(), m_child2
);
353 verifyTestTreeInitialState();
356 TEST_F(LayerTest
, removeAllChildren
)
358 createSimpleTestTree();
360 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), m_parent
->removeAllChildren());
362 ASSERT_EQ(static_cast<size_t>(0), m_parent
->children().size());
363 EXPECT_FALSE(m_child1
->parent());
364 EXPECT_FALSE(m_child2
->parent());
365 EXPECT_FALSE(m_child3
->parent());
368 TEST_F(LayerTest
, setChildren
)
370 scoped_refptr
<Layer
> oldParent
= Layer::create();
371 scoped_refptr
<Layer
> newParent
= Layer::create();
373 scoped_refptr
<Layer
> child1
= Layer::create();
374 scoped_refptr
<Layer
> child2
= Layer::create();
376 std::vector
<scoped_refptr
<Layer
> > newChildren
;
377 newChildren
.push_back(child1
);
378 newChildren
.push_back(child2
);
380 // Set up and verify initial test conditions: child1 has a parent, child2 has no parent.
381 oldParent
->addChild(child1
);
382 ASSERT_EQ(static_cast<size_t>(0), newParent
->children().size());
383 EXPECT_EQ(oldParent
.get(), child1
->parent());
384 EXPECT_FALSE(child2
->parent());
386 newParent
->setLayerTreeHost(m_layerTreeHost
.get());
388 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), newParent
->setChildren(newChildren
));
390 ASSERT_EQ(static_cast<size_t>(2), newParent
->children().size());
391 EXPECT_EQ(newParent
.get(), child1
->parent());
392 EXPECT_EQ(newParent
.get(), child2
->parent());
394 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AtLeast(1));
397 TEST_F(LayerTest
, getRootLayerAfterTreeManipulations
)
399 createSimpleTestTree();
401 // For this test we don't care about setNeedsFullTreeSync calls.
402 EXPECT_CALL(*m_layerTreeHost
, setNeedsFullTreeSync()).Times(AnyNumber());
404 scoped_refptr
<Layer
> child4
= Layer::create();
406 EXPECT_EQ(m_parent
.get(), m_parent
->rootLayer());
407 EXPECT_EQ(m_parent
.get(), m_child1
->rootLayer());
408 EXPECT_EQ(m_parent
.get(), m_child2
->rootLayer());
409 EXPECT_EQ(m_parent
.get(), m_child3
->rootLayer());
410 EXPECT_EQ(child4
.get(), child4
->rootLayer());
411 EXPECT_EQ(m_parent
.get(), m_grandChild1
->rootLayer());
412 EXPECT_EQ(m_parent
.get(), m_grandChild2
->rootLayer());
413 EXPECT_EQ(m_parent
.get(), m_grandChild3
->rootLayer());
415 m_child1
->removeFromParent();
417 // child1 and its children, grandChild1 and grandChild2 are now on a separate subtree.
418 EXPECT_EQ(m_parent
.get(), m_parent
->rootLayer());
419 EXPECT_EQ(m_child1
.get(), m_child1
->rootLayer());
420 EXPECT_EQ(m_parent
.get(), m_child2
->rootLayer());
421 EXPECT_EQ(m_parent
.get(), m_child3
->rootLayer());
422 EXPECT_EQ(child4
.get(), child4
->rootLayer());
423 EXPECT_EQ(m_child1
.get(), m_grandChild1
->rootLayer());
424 EXPECT_EQ(m_child1
.get(), m_grandChild2
->rootLayer());
425 EXPECT_EQ(m_parent
.get(), m_grandChild3
->rootLayer());
427 m_grandChild3
->addChild(child4
);
429 EXPECT_EQ(m_parent
.get(), m_parent
->rootLayer());
430 EXPECT_EQ(m_child1
.get(), m_child1
->rootLayer());
431 EXPECT_EQ(m_parent
.get(), m_child2
->rootLayer());
432 EXPECT_EQ(m_parent
.get(), m_child3
->rootLayer());
433 EXPECT_EQ(m_parent
.get(), child4
->rootLayer());
434 EXPECT_EQ(m_child1
.get(), m_grandChild1
->rootLayer());
435 EXPECT_EQ(m_child1
.get(), m_grandChild2
->rootLayer());
436 EXPECT_EQ(m_parent
.get(), m_grandChild3
->rootLayer());
438 m_child2
->replaceChild(m_grandChild3
.get(), m_child1
);
440 // grandChild3 gets orphaned and the child1 subtree gets planted back into the tree under child2.
441 EXPECT_EQ(m_parent
.get(), m_parent
->rootLayer());
442 EXPECT_EQ(m_parent
.get(), m_child1
->rootLayer());
443 EXPECT_EQ(m_parent
.get(), m_child2
->rootLayer());
444 EXPECT_EQ(m_parent
.get(), m_child3
->rootLayer());
445 EXPECT_EQ(m_grandChild3
.get(), child4
->rootLayer());
446 EXPECT_EQ(m_parent
.get(), m_grandChild1
->rootLayer());
447 EXPECT_EQ(m_parent
.get(), m_grandChild2
->rootLayer());
448 EXPECT_EQ(m_grandChild3
.get(), m_grandChild3
->rootLayer());
451 TEST_F(LayerTest
, checkSetNeedsDisplayCausesCorrectBehavior
)
453 // The semantics for setNeedsDisplay which are tested here:
454 // 1. sets needsDisplay flag appropriately.
455 // 2. indirectly calls setNeedsCommit, exactly once for each call to setNeedsDisplay.
457 scoped_refptr
<Layer
> testLayer
= Layer::create();
458 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
459 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setIsDrawable(true));
461 gfx::Size testBounds
= gfx::Size(501, 508);
463 gfx::RectF dirty1
= gfx::RectF(10, 15, 1, 2);
464 gfx::RectF dirty2
= gfx::RectF(20, 25, 3, 4);
465 gfx::RectF emptyDirtyRect
= gfx::RectF(40, 45, 0, 0);
466 gfx::RectF outOfBoundsDirtyRect
= gfx::RectF(400, 405, 500, 502);
468 // Before anything, testLayer should not be dirty.
469 EXPECT_FALSE(testLayer
->needsDisplay());
471 // This is just initialization, but setNeedsCommit behavior is verified anyway to avoid warnings.
472 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setBounds(testBounds
));
473 testLayer
= Layer::create();
474 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
475 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setIsDrawable(true));
476 EXPECT_FALSE(testLayer
->needsDisplay());
478 // The real test begins here.
480 // Case 1: needsDisplay flag should not change because of an empty dirty rect.
481 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setNeedsDisplayRect(emptyDirtyRect
));
482 EXPECT_FALSE(testLayer
->needsDisplay());
485 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setNeedsDisplayRect(dirty1
));
486 EXPECT_TRUE(testLayer
->needsDisplay());
488 // Case 3: a second dirty rect.
489 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setNeedsDisplayRect(dirty2
));
490 EXPECT_TRUE(testLayer
->needsDisplay());
492 // Case 4: Layer should accept dirty rects that go beyond its bounds.
493 testLayer
= Layer::create();
494 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
495 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setIsDrawable(true));
496 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setBounds(testBounds
));
497 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setNeedsDisplayRect(outOfBoundsDirtyRect
));
498 EXPECT_TRUE(testLayer
->needsDisplay());
500 // Case 5: setNeedsDisplay() without the dirty rect arg.
501 testLayer
= Layer::create();
502 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
503 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setIsDrawable(true));
504 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setBounds(testBounds
));
505 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setNeedsDisplay());
506 EXPECT_TRUE(testLayer
->needsDisplay());
508 // Case 6: setNeedsDisplay() with a non-drawable layer
509 testLayer
= Layer::create();
510 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
511 EXPECT_SET_NEEDS_COMMIT(0, testLayer
->setBounds(testBounds
));
512 EXPECT_SET_NEEDS_COMMIT(0, testLayer
->setNeedsDisplayRect(dirty1
));
513 EXPECT_TRUE(testLayer
->needsDisplay());
516 TEST_F(LayerTest
, checkPropertyChangeCausesCorrectBehavior
)
518 scoped_refptr
<Layer
> testLayer
= Layer::create();
519 testLayer
->setLayerTreeHost(m_layerTreeHost
.get());
520 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setIsDrawable(true));
522 scoped_refptr
<Layer
> dummyLayer
= Layer::create(); // just a dummy layer for this test case.
524 // sanity check of initial test condition
525 EXPECT_FALSE(testLayer
->needsDisplay());
527 // Next, test properties that should call setNeedsCommit (but not setNeedsDisplay)
528 // All properties need to be set to new values in order for setNeedsCommit to be called.
529 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setAnchorPoint(gfx::PointF(1.23f
, 4.56f
)));
530 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setAnchorPointZ(0.7f
));
531 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setBackgroundColor(SK_ColorLTGRAY
));
532 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setMasksToBounds(true));
533 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setOpacity(0.5));
534 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setContentsOpaque(true));
535 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setPosition(gfx::PointF(4, 9)));
536 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setSublayerTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
537 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setScrollable(true));
538 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setScrollOffset(gfx::Vector2d(10, 10)));
539 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setShouldScrollOnMainThread(true));
540 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setNonFastScrollableRegion(gfx::Rect(1, 1, 2, 2)));
541 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setHaveWheelEventHandlers(true));
542 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
543 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setDoubleSided(false));
544 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setDebugName("Test Layer"));
545 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setDrawCheckerboardForMissingTiles(!testLayer
->drawCheckerboardForMissingTiles()));
546 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setForceRenderSurface(true));
548 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, testLayer
->setMaskLayer(dummyLayer
.get()));
549 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, testLayer
->setReplicaLayer(dummyLayer
.get()));
551 // The above tests should not have caused a change to the needsDisplay flag.
552 EXPECT_FALSE(testLayer
->needsDisplay());
554 // Test properties that should call setNeedsDisplay and setNeedsCommit
555 EXPECT_SET_NEEDS_COMMIT(1, testLayer
->setBounds(gfx::Size(5, 10)));
556 EXPECT_TRUE(testLayer
->needsDisplay());
559 TEST_F(LayerTest
, verifyPushPropertiesAccumulatesUpdateRect
)
561 scoped_refptr
<Layer
> testLayer
= Layer::create();
562 scoped_ptr
<LayerImpl
> implLayer
= LayerImpl::create(m_hostImpl
.activeTree(), 1);
564 testLayer
->setNeedsDisplayRect(gfx::RectF(gfx::PointF(), gfx::SizeF(5, 5)));
565 testLayer
->pushPropertiesTo(implLayer
.get());
566 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(), gfx::SizeF(5, 5)), implLayer
->updateRect());
568 // The LayerImpl's updateRect should be accumulated here, since we did not do anything to clear it.
569 testLayer
->setNeedsDisplayRect(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)));
570 testLayer
->pushPropertiesTo(implLayer
.get());
571 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(), gfx::SizeF(15, 15)), implLayer
->updateRect());
573 // If we do clear the LayerImpl side, then the next updateRect should be fresh without accumulation.
574 implLayer
->resetAllChangeTrackingForSubtree();
575 testLayer
->setNeedsDisplayRect(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)));
576 testLayer
->pushPropertiesTo(implLayer
.get());
577 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)), implLayer
->updateRect());
580 TEST_F(LayerTest
, verifyPushPropertiesCausesSurfacePropertyChangedForTransform
)
582 scoped_refptr
<Layer
> testLayer
= Layer::create();
583 scoped_ptr
<LayerImpl
> implLayer
= LayerImpl::create(m_hostImpl
.activeTree(), 1);
585 gfx::Transform transform
;
586 transform
.Rotate(45.0);
587 testLayer
->setTransform(transform
);
589 EXPECT_FALSE(implLayer
->layerSurfacePropertyChanged());
591 testLayer
->pushPropertiesTo(implLayer
.get());
593 EXPECT_TRUE(implLayer
->layerSurfacePropertyChanged());
596 TEST_F(LayerTest
, verifyPushPropertiesCausesSurfacePropertyChangedForOpacity
)
598 scoped_refptr
<Layer
> testLayer
= Layer::create();
599 scoped_ptr
<LayerImpl
> implLayer
= LayerImpl::create(m_hostImpl
.activeTree(), 1);
601 testLayer
->setOpacity(0.5);
603 EXPECT_FALSE(implLayer
->layerSurfacePropertyChanged());
605 testLayer
->pushPropertiesTo(implLayer
.get());
607 EXPECT_TRUE(implLayer
->layerSurfacePropertyChanged());
610 class FakeLayerImplTreeHost
: public LayerTreeHost
{
612 static scoped_ptr
<FakeLayerImplTreeHost
> create()
614 scoped_ptr
<FakeLayerImplTreeHost
> host(new FakeLayerImplTreeHost(LayerTreeSettings()));
615 // 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.
616 host
->initialize(scoped_ptr
<Thread
>(NULL
));
621 FakeLayerImplTreeHost(const LayerTreeSettings
& settings
)
622 : LayerTreeHost(&m_client
, settings
)
626 FakeLayerImplTreeHostClient m_client
;
629 void assertLayerTreeHostMatchesForSubtree(Layer
* layer
, LayerTreeHost
* host
)
631 EXPECT_EQ(host
, layer
->layerTreeHost());
633 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
634 assertLayerTreeHostMatchesForSubtree(layer
->children()[i
].get(), host
);
636 if (layer
->maskLayer())
637 assertLayerTreeHostMatchesForSubtree(layer
->maskLayer(), host
);
639 if (layer
->replicaLayer())
640 assertLayerTreeHostMatchesForSubtree(layer
->replicaLayer(), host
);
644 TEST(LayerLayerTreeHostTest
, enteringTree
)
646 scoped_refptr
<Layer
> parent
= Layer::create();
647 scoped_refptr
<Layer
> child
= Layer::create();
648 scoped_refptr
<Layer
> mask
= Layer::create();
649 scoped_refptr
<Layer
> replica
= Layer::create();
650 scoped_refptr
<Layer
> replicaMask
= Layer::create();
652 // Set up a detached tree of layers. The host pointer should be nil for these layers.
653 parent
->addChild(child
);
654 child
->setMaskLayer(mask
.get());
655 child
->setReplicaLayer(replica
.get());
656 replica
->setMaskLayer(mask
.get());
658 assertLayerTreeHostMatchesForSubtree(parent
.get(), 0);
660 scoped_ptr
<FakeLayerImplTreeHost
> layerTreeHost(FakeLayerImplTreeHost::create());
661 // Setting the root layer should set the host pointer for all layers in the tree.
662 layerTreeHost
->setRootLayer(parent
.get());
664 assertLayerTreeHostMatchesForSubtree(parent
.get(), layerTreeHost
.get());
666 // Clearing the root layer should also clear out the host pointers for all layers in the tree.
667 layerTreeHost
->setRootLayer(0);
669 assertLayerTreeHostMatchesForSubtree(parent
.get(), 0);
672 TEST(LayerLayerTreeHostTest
, addingLayerSubtree
)
674 scoped_refptr
<Layer
> parent
= Layer::create();
675 scoped_ptr
<FakeLayerImplTreeHost
> layerTreeHost(FakeLayerImplTreeHost::create());
677 layerTreeHost
->setRootLayer(parent
.get());
679 EXPECT_EQ(parent
->layerTreeHost(), layerTreeHost
.get());
681 // Adding a subtree to a layer already associated with a host should set the host pointer on all layers in that subtree.
682 scoped_refptr
<Layer
> child
= Layer::create();
683 scoped_refptr
<Layer
> grandChild
= Layer::create();
684 child
->addChild(grandChild
);
686 // Masks, replicas, and replica masks should pick up the new host too.
687 scoped_refptr
<Layer
> childMask
= Layer::create();
688 child
->setMaskLayer(childMask
.get());
689 scoped_refptr
<Layer
> childReplica
= Layer::create();
690 child
->setReplicaLayer(childReplica
.get());
691 scoped_refptr
<Layer
> childReplicaMask
= Layer::create();
692 childReplica
->setMaskLayer(childReplicaMask
.get());
694 parent
->addChild(child
);
695 assertLayerTreeHostMatchesForSubtree(parent
.get(), layerTreeHost
.get());
697 layerTreeHost
->setRootLayer(0);
700 TEST(LayerLayerTreeHostTest
, changeHost
)
702 scoped_refptr
<Layer
> parent
= Layer::create();
703 scoped_refptr
<Layer
> child
= Layer::create();
704 scoped_refptr
<Layer
> mask
= Layer::create();
705 scoped_refptr
<Layer
> replica
= Layer::create();
706 scoped_refptr
<Layer
> replicaMask
= Layer::create();
708 // Same setup as the previous test.
709 parent
->addChild(child
);
710 child
->setMaskLayer(mask
.get());
711 child
->setReplicaLayer(replica
.get());
712 replica
->setMaskLayer(mask
.get());
714 scoped_ptr
<FakeLayerImplTreeHost
> firstLayerTreeHost(FakeLayerImplTreeHost::create());
715 firstLayerTreeHost
->setRootLayer(parent
.get());
717 assertLayerTreeHostMatchesForSubtree(parent
.get(), firstLayerTreeHost
.get());
719 // Now re-root the tree to a new host (simulating what we do on a context lost event).
720 // This should update the host pointers for all layers in the tree.
721 scoped_ptr
<FakeLayerImplTreeHost
> secondLayerTreeHost(FakeLayerImplTreeHost::create());
722 secondLayerTreeHost
->setRootLayer(parent
.get());
724 assertLayerTreeHostMatchesForSubtree(parent
.get(), secondLayerTreeHost
.get());
726 secondLayerTreeHost
->setRootLayer(0);
729 TEST(LayerLayerTreeHostTest
, changeHostInSubtree
)
731 scoped_refptr
<Layer
> firstParent
= Layer::create();
732 scoped_refptr
<Layer
> firstChild
= Layer::create();
733 scoped_refptr
<Layer
> secondParent
= Layer::create();
734 scoped_refptr
<Layer
> secondChild
= Layer::create();
735 scoped_refptr
<Layer
> secondGrandChild
= Layer::create();
737 // First put all children under the first parent and set the first host.
738 firstParent
->addChild(firstChild
);
739 secondChild
->addChild(secondGrandChild
);
740 firstParent
->addChild(secondChild
);
742 scoped_ptr
<FakeLayerImplTreeHost
> firstLayerTreeHost(FakeLayerImplTreeHost::create());
743 firstLayerTreeHost
->setRootLayer(firstParent
.get());
745 assertLayerTreeHostMatchesForSubtree(firstParent
.get(), firstLayerTreeHost
.get());
747 // Now reparent the subtree starting at secondChild to a layer in a different tree.
748 scoped_ptr
<FakeLayerImplTreeHost
> secondLayerTreeHost(FakeLayerImplTreeHost::create());
749 secondLayerTreeHost
->setRootLayer(secondParent
.get());
751 secondParent
->addChild(secondChild
);
753 // The moved layer and its children should point to the new host.
754 EXPECT_EQ(secondLayerTreeHost
.get(), secondChild
->layerTreeHost());
755 EXPECT_EQ(secondLayerTreeHost
.get(), secondGrandChild
->layerTreeHost());
757 // Test over, cleanup time.
758 firstLayerTreeHost
->setRootLayer(0);
759 secondLayerTreeHost
->setRootLayer(0);
762 TEST(LayerLayerTreeHostTest
, replaceMaskAndReplicaLayer
)
764 scoped_refptr
<Layer
> parent
= Layer::create();
765 scoped_refptr
<Layer
> mask
= Layer::create();
766 scoped_refptr
<Layer
> replica
= Layer::create();
767 scoped_refptr
<Layer
> maskChild
= Layer::create();
768 scoped_refptr
<Layer
> replicaChild
= Layer::create();
769 scoped_refptr
<Layer
> maskReplacement
= Layer::create();
770 scoped_refptr
<Layer
> replicaReplacement
= Layer::create();
772 parent
->setMaskLayer(mask
.get());
773 parent
->setReplicaLayer(replica
.get());
774 mask
->addChild(maskChild
);
775 replica
->addChild(replicaChild
);
777 scoped_ptr
<FakeLayerImplTreeHost
> layerTreeHost(FakeLayerImplTreeHost::create());
778 layerTreeHost
->setRootLayer(parent
.get());
780 assertLayerTreeHostMatchesForSubtree(parent
.get(), layerTreeHost
.get());
782 // Replacing the mask should clear out the old mask's subtree's host pointers.
783 parent
->setMaskLayer(maskReplacement
.get());
784 EXPECT_EQ(0, mask
->layerTreeHost());
785 EXPECT_EQ(0, maskChild
->layerTreeHost());
787 // Same for replacing a replica layer.
788 parent
->setReplicaLayer(replicaReplacement
.get());
789 EXPECT_EQ(0, replica
->layerTreeHost());
790 EXPECT_EQ(0, replicaChild
->layerTreeHost());
792 // Test over, cleanup time.
793 layerTreeHost
->setRootLayer(0);
796 TEST(LayerLayerTreeHostTest
, destroyHostWithNonNullRootLayer
)
798 scoped_refptr
<Layer
> root
= Layer::create();
799 scoped_refptr
<Layer
> child
= Layer::create();
800 root
->addChild(child
);
801 scoped_ptr
<FakeLayerImplTreeHost
> layerTreeHost(FakeLayerImplTreeHost::create());
802 layerTreeHost
->setRootLayer(root
);
805 static bool addTestAnimation(Layer
* layer
)
807 scoped_ptr
<KeyframedFloatAnimationCurve
> curve(KeyframedFloatAnimationCurve::create());
808 curve
->addKeyframe(FloatKeyframe::create(0, 0.3f
, scoped_ptr
<TimingFunction
>()));
809 curve
->addKeyframe(FloatKeyframe::create(1, 0.7f
, scoped_ptr
<TimingFunction
>()));
810 scoped_ptr
<Animation
> animation(Animation::create(curve
.PassAs
<AnimationCurve
>(), 0, 0, Animation::Opacity
));
812 return layer
->addAnimation(animation
.Pass());
815 TEST(LayerLayerTreeHostTest
, shouldNotAddAnimationWithoutLayerTreeHost
)
817 // Currently, WebCore assumes that animations will be started immediately / very soon
818 // if a composited layer's addAnimation() returns true. However, without a layerTreeHost,
819 // layers cannot actually animate yet. So, to prevent violating this WebCore assumption,
820 // the animation should not be accepted if the layer doesn't already have a layerTreeHost.
822 scoped_refptr
<Layer
> layer
= Layer::create();
824 // Case 1: without a layerTreeHost, the animation should not be accepted.
825 #if defined(OS_ANDROID)
826 // All animations are enabled on Android to avoid performance regressions.
827 // Other platforms will be enabled with http://crbug.com/129683
828 EXPECT_TRUE(addTestAnimation(layer
.get()));
830 EXPECT_FALSE(addTestAnimation(layer
.get()));
833 scoped_ptr
<FakeLayerImplTreeHost
> layerTreeHost(FakeLayerImplTreeHost::create());
834 layerTreeHost
->setRootLayer(layer
.get());
835 layer
->setLayerTreeHost(layerTreeHost
.get());
836 assertLayerTreeHostMatchesForSubtree(layer
.get(), layerTreeHost
.get());
838 // Case 2: with a layerTreeHost, the animation should be accepted.
839 EXPECT_TRUE(addTestAnimation(layer
.get()));
842 class MockLayer
: public Layer
{
844 bool needsDisplay() const { return m_needsDisplay
; }
852 TEST(LayerTestWithoutFixture
, setBoundsTriggersSetNeedsRedrawAfterGettingNonEmptyBounds
)
854 scoped_refptr
<MockLayer
> layer(new MockLayer
);
855 EXPECT_FALSE(layer
->needsDisplay());
856 layer
->setBounds(gfx::Size(0, 10));
857 EXPECT_FALSE(layer
->needsDisplay());
858 layer
->setBounds(gfx::Size(10, 10));
859 EXPECT_TRUE(layer
->needsDisplay());