FileSystem mods: Changes to snapshot file creation to remove dependencies on blobs.
[chromium-blink-merge.git] / cc / layer_unittest.cc
blob6784858c7fd0f35434d3ff3e13455d88382c31a7
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.
5 #include "cc/layer.h"
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;
26 using ::testing::_;
28 #define EXPECT_SET_NEEDS_COMMIT(expect, codeToTest) do { \
29 EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times((expect)); \
30 codeToTest; \
31 Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); \
32 } while (0)
34 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, codeToTest) do { \
35 EXPECT_CALL(*m_layerTreeHost, setNeedsFullTreeSync()).Times((expect)); \
36 codeToTest; \
37 Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); \
38 } while (0)
41 namespace cc {
42 namespace {
44 class MockLayerImplTreeHost : public LayerTreeHost {
45 public:
46 MockLayerImplTreeHost()
47 : LayerTreeHost(&m_fakeClient, LayerTreeSettings())
49 initialize(scoped_ptr<Thread>(NULL));
52 MOCK_METHOD0(setNeedsCommit, void());
53 MOCK_METHOD0(setNeedsFullTreeSync, void());
55 private:
56 FakeLayerImplTreeHostClient m_fakeClient;
59 class MockLayerPainter : public LayerPainter {
60 public:
61 virtual void paint(SkCanvas*, gfx::Rect, gfx::RectF&) OVERRIDE { }
65 class LayerTest : public testing::Test {
66 public:
67 LayerTest()
68 : m_hostImpl(&m_proxy)
72 protected:
73 virtual void SetUp()
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());
82 m_parent = NULL;
83 m_child1 = NULL;
84 m_child2 = NULL;
85 m_child3 = NULL;
86 m_grandChild1 = NULL;
87 m_grandChild2 = NULL;
88 m_grandChild3 = NULL;
90 m_layerTreeHost->setRootLayer(0);
91 m_layerTreeHost.reset();
94 void verifyTestTreeInitialState() const
96 ASSERT_EQ(3U, 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(2U, 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(1U, 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(0U, 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(0U, 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(1U, 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(0U, 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(1U, 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(2U, 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(3U, 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(4U, 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(0U, parent->children().size());
258 // insert to an out-of-bounds index
259 parent->insertChild(child1, 53);
261 ASSERT_EQ(1U, 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(2U, 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(0U, 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(2U, 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(2U, 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));
308 EXPECT_FALSE(m_parent->needsDisplayForTesting());
309 EXPECT_FALSE(m_child1->needsDisplayForTesting());
310 EXPECT_FALSE(m_child2->needsDisplayForTesting());
311 EXPECT_FALSE(m_child3->needsDisplayForTesting());
312 EXPECT_FALSE(child4->needsDisplayForTesting());
314 ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size());
315 EXPECT_EQ(m_child1, m_parent->children()[0]);
316 EXPECT_EQ(child4, m_parent->children()[1]);
317 EXPECT_EQ(m_child3, m_parent->children()[2]);
318 EXPECT_EQ(m_parent.get(), child4->parent());
320 EXPECT_FALSE(m_child2->parent());
323 TEST_F(LayerTest, replaceChildWithNewChildAutomaticRasterScale)
325 createSimpleTestTree();
326 scoped_refptr<Layer> child4 = Layer::create();
327 EXPECT_SET_NEEDS_COMMIT(1, m_child1->setAutomaticallyComputeRasterScale(true));
328 EXPECT_SET_NEEDS_COMMIT(1, m_child2->setAutomaticallyComputeRasterScale(true));
329 EXPECT_SET_NEEDS_COMMIT(1, m_child3->setAutomaticallyComputeRasterScale(true));
331 EXPECT_FALSE(child4->parent());
333 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), m_parent->replaceChild(m_child2.get(), child4));
334 EXPECT_FALSE(m_parent->needsDisplayForTesting());
335 EXPECT_FALSE(m_child1->needsDisplayForTesting());
336 EXPECT_FALSE(m_child2->needsDisplayForTesting());
337 EXPECT_FALSE(m_child3->needsDisplayForTesting());
338 EXPECT_FALSE(child4->needsDisplayForTesting());
340 ASSERT_EQ(3U, m_parent->children().size());
341 EXPECT_EQ(m_child1, m_parent->children()[0]);
342 EXPECT_EQ(child4, m_parent->children()[1]);
343 EXPECT_EQ(m_child3, m_parent->children()[2]);
344 EXPECT_EQ(m_parent.get(), child4->parent());
346 EXPECT_FALSE(m_child2->parent());
349 TEST_F(LayerTest, replaceChildWithNewChildThatHasOtherParent)
351 createSimpleTestTree();
353 // create another simple tree with testLayer and child4.
354 scoped_refptr<Layer> testLayer = Layer::create();
355 scoped_refptr<Layer> child4 = Layer::create();
356 testLayer->addChild(child4);
357 ASSERT_EQ(1U, testLayer->children().size());
358 EXPECT_EQ(child4, testLayer->children()[0]);
359 EXPECT_EQ(testLayer.get(), child4->parent());
361 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), m_parent->replaceChild(m_child2.get(), child4));
363 ASSERT_EQ(3U, m_parent->children().size());
364 EXPECT_EQ(m_child1, m_parent->children()[0]);
365 EXPECT_EQ(child4, m_parent->children()[1]);
366 EXPECT_EQ(m_child3, m_parent->children()[2]);
367 EXPECT_EQ(m_parent.get(), child4->parent());
369 // testLayer should no longer have child4,
370 // and child2 should no longer have a parent.
371 ASSERT_EQ(0U, testLayer->children().size());
372 EXPECT_FALSE(m_child2->parent());
375 TEST_F(LayerTest, replaceChildWithSameChild)
377 createSimpleTestTree();
379 // setNeedsFullTreeSync / setNeedsCommit should not be called because its the same child
380 EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(0);
381 EXPECT_CALL(*m_layerTreeHost, setNeedsFullTreeSync()).Times(0);
382 m_parent->replaceChild(m_child2.get(), m_child2);
384 verifyTestTreeInitialState();
387 TEST_F(LayerTest, removeAllChildren)
389 createSimpleTestTree();
391 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), m_parent->removeAllChildren());
393 ASSERT_EQ(0U, m_parent->children().size());
394 EXPECT_FALSE(m_child1->parent());
395 EXPECT_FALSE(m_child2->parent());
396 EXPECT_FALSE(m_child3->parent());
399 TEST_F(LayerTest, setChildren)
401 scoped_refptr<Layer> oldParent = Layer::create();
402 scoped_refptr<Layer> newParent = Layer::create();
404 scoped_refptr<Layer> child1 = Layer::create();
405 scoped_refptr<Layer> child2 = Layer::create();
407 std::vector<scoped_refptr<Layer> > newChildren;
408 newChildren.push_back(child1);
409 newChildren.push_back(child2);
411 // Set up and verify initial test conditions: child1 has a parent, child2 has no parent.
412 oldParent->addChild(child1);
413 ASSERT_EQ(0U, newParent->children().size());
414 EXPECT_EQ(oldParent.get(), child1->parent());
415 EXPECT_FALSE(child2->parent());
417 newParent->setLayerTreeHost(m_layerTreeHost.get());
419 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), newParent->setChildren(newChildren));
421 ASSERT_EQ(2U, newParent->children().size());
422 EXPECT_EQ(newParent.get(), child1->parent());
423 EXPECT_EQ(newParent.get(), child2->parent());
425 EXPECT_CALL(*m_layerTreeHost, setNeedsFullTreeSync()).Times(AtLeast(1));
428 TEST_F(LayerTest, getRootLayerAfterTreeManipulations)
430 createSimpleTestTree();
432 // For this test we don't care about setNeedsFullTreeSync calls.
433 EXPECT_CALL(*m_layerTreeHost, setNeedsFullTreeSync()).Times(AnyNumber());
435 scoped_refptr<Layer> child4 = Layer::create();
437 EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
438 EXPECT_EQ(m_parent.get(), m_child1->rootLayer());
439 EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
440 EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
441 EXPECT_EQ(child4.get(), child4->rootLayer());
442 EXPECT_EQ(m_parent.get(), m_grandChild1->rootLayer());
443 EXPECT_EQ(m_parent.get(), m_grandChild2->rootLayer());
444 EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer());
446 m_child1->removeFromParent();
448 // child1 and its children, grandChild1 and grandChild2 are now on a separate subtree.
449 EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
450 EXPECT_EQ(m_child1.get(), m_child1->rootLayer());
451 EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
452 EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
453 EXPECT_EQ(child4.get(), child4->rootLayer());
454 EXPECT_EQ(m_child1.get(), m_grandChild1->rootLayer());
455 EXPECT_EQ(m_child1.get(), m_grandChild2->rootLayer());
456 EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer());
458 m_grandChild3->addChild(child4);
460 EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
461 EXPECT_EQ(m_child1.get(), m_child1->rootLayer());
462 EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
463 EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
464 EXPECT_EQ(m_parent.get(), child4->rootLayer());
465 EXPECT_EQ(m_child1.get(), m_grandChild1->rootLayer());
466 EXPECT_EQ(m_child1.get(), m_grandChild2->rootLayer());
467 EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer());
469 m_child2->replaceChild(m_grandChild3.get(), m_child1);
471 // grandChild3 gets orphaned and the child1 subtree gets planted back into the tree under child2.
472 EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
473 EXPECT_EQ(m_parent.get(), m_child1->rootLayer());
474 EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
475 EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
476 EXPECT_EQ(m_grandChild3.get(), child4->rootLayer());
477 EXPECT_EQ(m_parent.get(), m_grandChild1->rootLayer());
478 EXPECT_EQ(m_parent.get(), m_grandChild2->rootLayer());
479 EXPECT_EQ(m_grandChild3.get(), m_grandChild3->rootLayer());
482 TEST_F(LayerTest, checkSetNeedsDisplayCausesCorrectBehavior)
484 // The semantics for setNeedsDisplay which are tested here:
485 // 1. sets needsDisplay flag appropriately.
486 // 2. indirectly calls setNeedsCommit, exactly once for each call to setNeedsDisplay.
488 scoped_refptr<Layer> testLayer = Layer::create();
489 testLayer->setLayerTreeHost(m_layerTreeHost.get());
490 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setIsDrawable(true));
492 gfx::Size testBounds = gfx::Size(501, 508);
494 gfx::RectF dirty1 = gfx::RectF(10, 15, 1, 2);
495 gfx::RectF dirty2 = gfx::RectF(20, 25, 3, 4);
496 gfx::RectF emptyDirtyRect = gfx::RectF(40, 45, 0, 0);
497 gfx::RectF outOfBoundsDirtyRect = gfx::RectF(400, 405, 500, 502);
499 // Before anything, testLayer should not be dirty.
500 EXPECT_FALSE(testLayer->needsDisplayForTesting());
502 // This is just initialization, but setNeedsCommit behavior is verified anyway to avoid warnings.
503 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setBounds(testBounds));
504 EXPECT_TRUE(testLayer->needsDisplayForTesting());
506 // The real test begins here.
507 testLayer->resetNeedsDisplayForTesting();
508 EXPECT_FALSE(testLayer->needsDisplayForTesting());
510 // Case 1: Layer should accept dirty rects that go beyond its bounds.
511 testLayer->resetNeedsDisplayForTesting();
512 EXPECT_FALSE(testLayer->needsDisplayForTesting());
513 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setNeedsDisplayRect(outOfBoundsDirtyRect));
514 EXPECT_TRUE(testLayer->needsDisplayForTesting());
515 testLayer->resetNeedsDisplayForTesting();
517 // Case 2: setNeedsDisplay() without the dirty rect arg.
518 testLayer->resetNeedsDisplayForTesting();
519 EXPECT_FALSE(testLayer->needsDisplayForTesting());
520 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setNeedsDisplay());
521 EXPECT_TRUE(testLayer->needsDisplayForTesting());
522 testLayer->resetNeedsDisplayForTesting();
524 // Case 3: setNeedsDisplay() with a non-drawable layer
525 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setIsDrawable(false));
526 testLayer->resetNeedsDisplayForTesting();
527 EXPECT_FALSE(testLayer->needsDisplayForTesting());
528 EXPECT_SET_NEEDS_COMMIT(0, testLayer->setNeedsDisplayRect(dirty1));
529 EXPECT_TRUE(testLayer->needsDisplayForTesting());
532 TEST_F(LayerTest, checkPropertyChangeCausesCorrectBehavior)
534 scoped_refptr<Layer> testLayer = Layer::create();
535 testLayer->setLayerTreeHost(m_layerTreeHost.get());
536 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setIsDrawable(true));
538 scoped_refptr<Layer> dummyLayer = Layer::create(); // just a dummy layer for this test case.
540 // sanity check of initial test condition
541 EXPECT_FALSE(testLayer->needsDisplayForTesting());
543 // Next, test properties that should call setNeedsCommit (but not setNeedsDisplay)
544 // All properties need to be set to new values in order for setNeedsCommit to be called.
545 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setAnchorPoint(gfx::PointF(1.23f, 4.56f)));
546 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setAnchorPointZ(0.7f));
547 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setBackgroundColor(SK_ColorLTGRAY));
548 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setMasksToBounds(true));
549 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setOpacity(0.5));
550 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setContentsOpaque(true));
551 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setPosition(gfx::PointF(4, 9)));
552 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setSublayerTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
553 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setScrollable(true));
554 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setScrollOffset(gfx::Vector2d(10, 10)));
555 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setShouldScrollOnMainThread(true));
556 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setNonFastScrollableRegion(gfx::Rect(1, 1, 2, 2)));
557 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setHaveWheelEventHandlers(true));
558 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setTransform(gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
559 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setDoubleSided(false));
560 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setDebugName("Test Layer"));
561 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setDrawCheckerboardForMissingTiles(!testLayer->drawCheckerboardForMissingTiles()));
562 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setForceRenderSurface(true));
564 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, testLayer->setMaskLayer(dummyLayer.get()));
565 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, testLayer->setReplicaLayer(dummyLayer.get()));
567 // The above tests should not have caused a change to the needsDisplay flag.
568 EXPECT_FALSE(testLayer->needsDisplayForTesting());
571 TEST_F(LayerTest, setBoundsTriggersSetNeedsRedrawAfterGettingNonEmptyBounds)
573 scoped_refptr<Layer> testLayer = Layer::create();
574 testLayer->setLayerTreeHost(m_layerTreeHost.get());
575 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setIsDrawable(true));
577 EXPECT_FALSE(testLayer->needsDisplayForTesting());
578 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setBounds(gfx::Size(0, 10)));
579 EXPECT_FALSE(testLayer->needsDisplayForTesting());
580 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setBounds(gfx::Size(10, 10)));
581 EXPECT_TRUE(testLayer->needsDisplayForTesting());
583 testLayer->resetNeedsDisplayForTesting();
584 EXPECT_FALSE(testLayer->needsDisplayForTesting());
586 // Calling setBounds only invalidates on the first time.
587 EXPECT_SET_NEEDS_COMMIT(1, testLayer->setBounds(gfx::Size(7, 10)));
588 EXPECT_FALSE(testLayer->needsDisplayForTesting());
591 TEST_F(LayerTest, verifyPushPropertiesAccumulatesUpdateRect)
593 scoped_refptr<Layer> testLayer = Layer::create();
594 scoped_ptr<LayerImpl> implLayer = LayerImpl::create(m_hostImpl.activeTree(), 1);
596 testLayer->setNeedsDisplayRect(gfx::RectF(gfx::PointF(), gfx::SizeF(5, 5)));
597 testLayer->pushPropertiesTo(implLayer.get());
598 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(), gfx::SizeF(5, 5)), implLayer->updateRect());
600 // The LayerImpl's updateRect should be accumulated here, since we did not do anything to clear it.
601 testLayer->setNeedsDisplayRect(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)));
602 testLayer->pushPropertiesTo(implLayer.get());
603 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(), gfx::SizeF(15, 15)), implLayer->updateRect());
605 // If we do clear the LayerImpl side, then the next updateRect should be fresh without accumulation.
606 implLayer->resetAllChangeTrackingForSubtree();
607 testLayer->setNeedsDisplayRect(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)));
608 testLayer->pushPropertiesTo(implLayer.get());
609 EXPECT_FLOAT_RECT_EQ(gfx::RectF(gfx::PointF(10, 10), gfx::SizeF(5, 5)), implLayer->updateRect());
612 TEST_F(LayerTest, verifyPushPropertiesCausesSurfacePropertyChangedForTransform)
614 scoped_refptr<Layer> testLayer = Layer::create();
615 scoped_ptr<LayerImpl> implLayer = LayerImpl::create(m_hostImpl.activeTree(), 1);
617 gfx::Transform transform;
618 transform.Rotate(45.0);
619 testLayer->setTransform(transform);
621 EXPECT_FALSE(implLayer->layerSurfacePropertyChanged());
623 testLayer->pushPropertiesTo(implLayer.get());
625 EXPECT_TRUE(implLayer->layerSurfacePropertyChanged());
628 TEST_F(LayerTest, verifyPushPropertiesCausesSurfacePropertyChangedForOpacity)
630 scoped_refptr<Layer> testLayer = Layer::create();
631 scoped_ptr<LayerImpl> implLayer = LayerImpl::create(m_hostImpl.activeTree(), 1);
633 testLayer->setOpacity(0.5);
635 EXPECT_FALSE(implLayer->layerSurfacePropertyChanged());
637 testLayer->pushPropertiesTo(implLayer.get());
639 EXPECT_TRUE(implLayer->layerSurfacePropertyChanged());
642 class FakeLayerImplTreeHost : public LayerTreeHost {
643 public:
644 static scoped_ptr<FakeLayerImplTreeHost> create()
646 scoped_ptr<FakeLayerImplTreeHost> host(new FakeLayerImplTreeHost(LayerTreeSettings()));
647 // 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.
648 host->initialize(scoped_ptr<Thread>(NULL));
649 return host.Pass();
652 private:
653 FakeLayerImplTreeHost(const LayerTreeSettings& settings)
654 : LayerTreeHost(&m_client, settings)
658 FakeLayerImplTreeHostClient m_client;
661 void assertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host)
663 EXPECT_EQ(host, layer->layerTreeHost());
665 for (size_t i = 0; i < layer->children().size(); ++i)
666 assertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
668 if (layer->maskLayer())
669 assertLayerTreeHostMatchesForSubtree(layer->maskLayer(), host);
671 if (layer->replicaLayer())
672 assertLayerTreeHostMatchesForSubtree(layer->replicaLayer(), host);
676 TEST(LayerLayerTreeHostTest, enteringTree)
678 scoped_refptr<Layer> parent = Layer::create();
679 scoped_refptr<Layer> child = Layer::create();
680 scoped_refptr<Layer> mask = Layer::create();
681 scoped_refptr<Layer> replica = Layer::create();
682 scoped_refptr<Layer> replicaMask = Layer::create();
684 // Set up a detached tree of layers. The host pointer should be nil for these layers.
685 parent->addChild(child);
686 child->setMaskLayer(mask.get());
687 child->setReplicaLayer(replica.get());
688 replica->setMaskLayer(mask.get());
690 assertLayerTreeHostMatchesForSubtree(parent.get(), 0);
692 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
693 // Setting the root layer should set the host pointer for all layers in the tree.
694 layerTreeHost->setRootLayer(parent.get());
696 assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
698 // Clearing the root layer should also clear out the host pointers for all layers in the tree.
699 layerTreeHost->setRootLayer(0);
701 assertLayerTreeHostMatchesForSubtree(parent.get(), 0);
704 TEST(LayerLayerTreeHostTest, addingLayerSubtree)
706 scoped_refptr<Layer> parent = Layer::create();
707 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
709 layerTreeHost->setRootLayer(parent.get());
711 EXPECT_EQ(parent->layerTreeHost(), layerTreeHost.get());
713 // Adding a subtree to a layer already associated with a host should set the host pointer on all layers in that subtree.
714 scoped_refptr<Layer> child = Layer::create();
715 scoped_refptr<Layer> grandChild = Layer::create();
716 child->addChild(grandChild);
718 // Masks, replicas, and replica masks should pick up the new host too.
719 scoped_refptr<Layer> childMask = Layer::create();
720 child->setMaskLayer(childMask.get());
721 scoped_refptr<Layer> childReplica = Layer::create();
722 child->setReplicaLayer(childReplica.get());
723 scoped_refptr<Layer> childReplicaMask = Layer::create();
724 childReplica->setMaskLayer(childReplicaMask.get());
726 parent->addChild(child);
727 assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
729 layerTreeHost->setRootLayer(0);
732 TEST(LayerLayerTreeHostTest, changeHost)
734 scoped_refptr<Layer> parent = Layer::create();
735 scoped_refptr<Layer> child = Layer::create();
736 scoped_refptr<Layer> mask = Layer::create();
737 scoped_refptr<Layer> replica = Layer::create();
738 scoped_refptr<Layer> replicaMask = Layer::create();
740 // Same setup as the previous test.
741 parent->addChild(child);
742 child->setMaskLayer(mask.get());
743 child->setReplicaLayer(replica.get());
744 replica->setMaskLayer(mask.get());
746 scoped_ptr<FakeLayerImplTreeHost> firstLayerTreeHost(FakeLayerImplTreeHost::create());
747 firstLayerTreeHost->setRootLayer(parent.get());
749 assertLayerTreeHostMatchesForSubtree(parent.get(), firstLayerTreeHost.get());
751 // Now re-root the tree to a new host (simulating what we do on a context lost event).
752 // This should update the host pointers for all layers in the tree.
753 scoped_ptr<FakeLayerImplTreeHost> secondLayerTreeHost(FakeLayerImplTreeHost::create());
754 secondLayerTreeHost->setRootLayer(parent.get());
756 assertLayerTreeHostMatchesForSubtree(parent.get(), secondLayerTreeHost.get());
758 secondLayerTreeHost->setRootLayer(0);
761 TEST(LayerLayerTreeHostTest, changeHostInSubtree)
763 scoped_refptr<Layer> firstParent = Layer::create();
764 scoped_refptr<Layer> firstChild = Layer::create();
765 scoped_refptr<Layer> secondParent = Layer::create();
766 scoped_refptr<Layer> secondChild = Layer::create();
767 scoped_refptr<Layer> secondGrandChild = Layer::create();
769 // First put all children under the first parent and set the first host.
770 firstParent->addChild(firstChild);
771 secondChild->addChild(secondGrandChild);
772 firstParent->addChild(secondChild);
774 scoped_ptr<FakeLayerImplTreeHost> firstLayerTreeHost(FakeLayerImplTreeHost::create());
775 firstLayerTreeHost->setRootLayer(firstParent.get());
777 assertLayerTreeHostMatchesForSubtree(firstParent.get(), firstLayerTreeHost.get());
779 // Now reparent the subtree starting at secondChild to a layer in a different tree.
780 scoped_ptr<FakeLayerImplTreeHost> secondLayerTreeHost(FakeLayerImplTreeHost::create());
781 secondLayerTreeHost->setRootLayer(secondParent.get());
783 secondParent->addChild(secondChild);
785 // The moved layer and its children should point to the new host.
786 EXPECT_EQ(secondLayerTreeHost.get(), secondChild->layerTreeHost());
787 EXPECT_EQ(secondLayerTreeHost.get(), secondGrandChild->layerTreeHost());
789 // Test over, cleanup time.
790 firstLayerTreeHost->setRootLayer(0);
791 secondLayerTreeHost->setRootLayer(0);
794 TEST(LayerLayerTreeHostTest, replaceMaskAndReplicaLayer)
796 scoped_refptr<Layer> parent = Layer::create();
797 scoped_refptr<Layer> mask = Layer::create();
798 scoped_refptr<Layer> replica = Layer::create();
799 scoped_refptr<Layer> maskChild = Layer::create();
800 scoped_refptr<Layer> replicaChild = Layer::create();
801 scoped_refptr<Layer> maskReplacement = Layer::create();
802 scoped_refptr<Layer> replicaReplacement = Layer::create();
804 parent->setMaskLayer(mask.get());
805 parent->setReplicaLayer(replica.get());
806 mask->addChild(maskChild);
807 replica->addChild(replicaChild);
809 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
810 layerTreeHost->setRootLayer(parent.get());
812 assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
814 // Replacing the mask should clear out the old mask's subtree's host pointers.
815 parent->setMaskLayer(maskReplacement.get());
816 EXPECT_EQ(0, mask->layerTreeHost());
817 EXPECT_EQ(0, maskChild->layerTreeHost());
819 // Same for replacing a replica layer.
820 parent->setReplicaLayer(replicaReplacement.get());
821 EXPECT_EQ(0, replica->layerTreeHost());
822 EXPECT_EQ(0, replicaChild->layerTreeHost());
824 // Test over, cleanup time.
825 layerTreeHost->setRootLayer(0);
828 TEST(LayerLayerTreeHostTest, destroyHostWithNonNullRootLayer)
830 scoped_refptr<Layer> root = Layer::create();
831 scoped_refptr<Layer> child = Layer::create();
832 root->addChild(child);
833 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
834 layerTreeHost->setRootLayer(root);
837 static bool addTestAnimation(Layer* layer)
839 scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
840 curve->addKeyframe(FloatKeyframe::create(0, 0.3f, scoped_ptr<TimingFunction>()));
841 curve->addKeyframe(FloatKeyframe::create(1, 0.7f, scoped_ptr<TimingFunction>()));
842 scoped_ptr<Animation> animation(Animation::create(curve.PassAs<AnimationCurve>(), 0, 0, Animation::Opacity));
844 return layer->addAnimation(animation.Pass());
847 TEST(LayerLayerTreeHostTest, shouldNotAddAnimationWithoutLayerTreeHost)
849 // Currently, WebCore assumes that animations will be started immediately / very soon
850 // if a composited layer's addAnimation() returns true. However, without a layerTreeHost,
851 // layers cannot actually animate yet. So, to prevent violating this WebCore assumption,
852 // the animation should not be accepted if the layer doesn't already have a layerTreeHost.
854 scoped_refptr<Layer> layer = Layer::create();
856 // Case 1: without a layerTreeHost, the animation should not be accepted.
857 #if defined(OS_ANDROID)
858 // All animations are enabled on Android to avoid performance regressions.
859 // Other platforms will be enabled with http://crbug.com/129683
860 EXPECT_TRUE(addTestAnimation(layer.get()));
861 #else
862 EXPECT_FALSE(addTestAnimation(layer.get()));
863 #endif
865 scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
866 layerTreeHost->setRootLayer(layer.get());
867 layer->setLayerTreeHost(layerTreeHost.get());
868 assertLayerTreeHostMatchesForSubtree(layer.get(), layerTreeHost.get());
870 // Case 2: with a layerTreeHost, the animation should be accepted.
871 EXPECT_TRUE(addTestAnimation(layer.get()));
874 } // namespace
875 } // namespace cc