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/layers/layer.h"
7 #include "cc/animation/keyframed_animation_curve.h"
8 #include "cc/base/math_util.h"
9 #include "cc/layers/layer_impl.h"
10 #include "cc/resources/layer_painter.h"
11 #include "cc/test/animation_test_common.h"
12 #include "cc/test/fake_impl_proxy.h"
13 #include "cc/test/fake_layer_tree_host_client.h"
14 #include "cc/test/fake_layer_tree_host_impl.h"
15 #include "cc/test/geometry_test_utils.h"
16 #include "cc/test/layer_test_common.h"
17 #include "cc/trees/layer_tree_host.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "ui/gfx/transform.h"
23 using ::testing::AnyNumber
;
24 using ::testing::AtLeast
;
25 using ::testing::Mock
;
26 using ::testing::StrictMock
;
29 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
31 EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
33 Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
39 class MockLayerTreeHost
: public LayerTreeHost
{
41 explicit MockLayerTreeHost(FakeLayerTreeHostClient
* client
)
42 : LayerTreeHost(client
, NULL
, LayerTreeSettings()) {
43 InitializeSingleThreaded(client
);
46 MOCK_METHOD0(SetNeedsCommit
, void());
47 MOCK_METHOD0(SetNeedsUpdateLayers
, void());
48 MOCK_METHOD0(SetNeedsFullTreeSync
, void());
51 class MockLayerPainter
: public LayerPainter
{
53 virtual void Paint(SkCanvas
* canvas
,
54 const gfx::Rect
& content_rect
,
55 gfx::RectF
* opaque
) OVERRIDE
{}
59 class LayerTest
: public testing::Test
{
62 : host_impl_(&proxy_
),
63 fake_client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
66 virtual void SetUp() OVERRIDE
{
67 layer_tree_host_
.reset(new StrictMock
<MockLayerTreeHost
>(&fake_client_
));
70 virtual void TearDown() OVERRIDE
{
71 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
72 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
81 layer_tree_host_
->SetRootLayer(NULL
);
82 layer_tree_host_
.reset();
85 void VerifyTestTreeInitialState() const {
86 ASSERT_EQ(3U, parent_
->children().size());
87 EXPECT_EQ(child1_
, parent_
->children()[0]);
88 EXPECT_EQ(child2_
, parent_
->children()[1]);
89 EXPECT_EQ(child3_
, parent_
->children()[2]);
90 EXPECT_EQ(parent_
.get(), child1_
->parent());
91 EXPECT_EQ(parent_
.get(), child2_
->parent());
92 EXPECT_EQ(parent_
.get(), child3_
->parent());
94 ASSERT_EQ(2U, child1_
->children().size());
95 EXPECT_EQ(grand_child1_
, child1_
->children()[0]);
96 EXPECT_EQ(grand_child2_
, child1_
->children()[1]);
97 EXPECT_EQ(child1_
.get(), grand_child1_
->parent());
98 EXPECT_EQ(child1_
.get(), grand_child2_
->parent());
100 ASSERT_EQ(1U, child2_
->children().size());
101 EXPECT_EQ(grand_child3_
, child2_
->children()[0]);
102 EXPECT_EQ(child2_
.get(), grand_child3_
->parent());
104 ASSERT_EQ(0U, child3_
->children().size());
107 void CreateSimpleTestTree() {
108 parent_
= Layer::Create();
109 child1_
= Layer::Create();
110 child2_
= Layer::Create();
111 child3_
= Layer::Create();
112 grand_child1_
= Layer::Create();
113 grand_child2_
= Layer::Create();
114 grand_child3_
= Layer::Create();
116 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
117 layer_tree_host_
->SetRootLayer(parent_
);
119 parent_
->AddChild(child1_
);
120 parent_
->AddChild(child2_
);
121 parent_
->AddChild(child3_
);
122 child1_
->AddChild(grand_child1_
);
123 child1_
->AddChild(grand_child2_
);
124 child2_
->AddChild(grand_child3_
);
126 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
128 VerifyTestTreeInitialState();
131 FakeImplProxy proxy_
;
132 FakeLayerTreeHostImpl host_impl_
;
134 FakeLayerTreeHostClient fake_client_
;
135 scoped_ptr
<StrictMock
<MockLayerTreeHost
> > layer_tree_host_
;
136 scoped_refptr
<Layer
> parent_
;
137 scoped_refptr
<Layer
> child1_
;
138 scoped_refptr
<Layer
> child2_
;
139 scoped_refptr
<Layer
> child3_
;
140 scoped_refptr
<Layer
> grand_child1_
;
141 scoped_refptr
<Layer
> grand_child2_
;
142 scoped_refptr
<Layer
> grand_child3_
;
145 TEST_F(LayerTest
, BasicCreateAndDestroy
) {
146 scoped_refptr
<Layer
> test_layer
= Layer::Create();
147 ASSERT_TRUE(test_layer
.get());
149 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
150 test_layer
->SetLayerTreeHost(layer_tree_host_
.get());
151 Mock::VerifyAndClearExpectations(layer_tree_host_
.get());
153 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
154 test_layer
->SetLayerTreeHost(NULL
);
157 TEST_F(LayerTest
, AddAndRemoveChild
) {
158 scoped_refptr
<Layer
> parent
= Layer::Create();
159 scoped_refptr
<Layer
> child
= Layer::Create();
161 // Upon creation, layers should not have children or parent.
162 ASSERT_EQ(0U, parent
->children().size());
163 EXPECT_FALSE(child
->parent());
165 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
166 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->AddChild(child
));
168 ASSERT_EQ(1U, parent
->children().size());
169 EXPECT_EQ(child
.get(), parent
->children()[0]);
170 EXPECT_EQ(parent
.get(), child
->parent());
171 EXPECT_EQ(parent
.get(), child
->RootLayer());
173 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child
->RemoveFromParent());
176 TEST_F(LayerTest
, AddSameChildTwice
) {
177 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AtLeast(1));
179 scoped_refptr
<Layer
> parent
= Layer::Create();
180 scoped_refptr
<Layer
> child
= Layer::Create();
182 layer_tree_host_
->SetRootLayer(parent
);
184 ASSERT_EQ(0u, parent
->children().size());
186 parent
->AddChild(child
);
187 ASSERT_EQ(1u, parent
->children().size());
188 EXPECT_EQ(parent
.get(), child
->parent());
190 parent
->AddChild(child
);
191 ASSERT_EQ(1u, parent
->children().size());
192 EXPECT_EQ(parent
.get(), child
->parent());
195 TEST_F(LayerTest
, InsertChild
) {
196 scoped_refptr
<Layer
> parent
= Layer::Create();
197 scoped_refptr
<Layer
> child1
= Layer::Create();
198 scoped_refptr
<Layer
> child2
= Layer::Create();
199 scoped_refptr
<Layer
> child3
= Layer::Create();
200 scoped_refptr
<Layer
> child4
= Layer::Create();
202 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
204 ASSERT_EQ(0U, parent
->children().size());
206 // Case 1: inserting to empty list.
207 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child3
, 0));
208 ASSERT_EQ(1U, parent
->children().size());
209 EXPECT_EQ(child3
, parent
->children()[0]);
210 EXPECT_EQ(parent
.get(), child3
->parent());
212 // Case 2: inserting to beginning of list
213 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
214 ASSERT_EQ(2U, parent
->children().size());
215 EXPECT_EQ(child1
, parent
->children()[0]);
216 EXPECT_EQ(child3
, parent
->children()[1]);
217 EXPECT_EQ(parent
.get(), child1
->parent());
219 // Case 3: inserting to middle of list
220 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
221 ASSERT_EQ(3U, parent
->children().size());
222 EXPECT_EQ(child1
, parent
->children()[0]);
223 EXPECT_EQ(child2
, parent
->children()[1]);
224 EXPECT_EQ(child3
, parent
->children()[2]);
225 EXPECT_EQ(parent
.get(), child2
->parent());
227 // Case 4: inserting to end of list
228 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child4
, 3));
230 ASSERT_EQ(4U, parent
->children().size());
231 EXPECT_EQ(child1
, parent
->children()[0]);
232 EXPECT_EQ(child2
, parent
->children()[1]);
233 EXPECT_EQ(child3
, parent
->children()[2]);
234 EXPECT_EQ(child4
, parent
->children()[3]);
235 EXPECT_EQ(parent
.get(), child4
->parent());
237 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(NULL
));
240 TEST_F(LayerTest
, InsertChildPastEndOfList
) {
241 scoped_refptr
<Layer
> parent
= Layer::Create();
242 scoped_refptr
<Layer
> child1
= Layer::Create();
243 scoped_refptr
<Layer
> child2
= Layer::Create();
245 ASSERT_EQ(0U, parent
->children().size());
247 // insert to an out-of-bounds index
248 parent
->InsertChild(child1
, 53);
250 ASSERT_EQ(1U, parent
->children().size());
251 EXPECT_EQ(child1
, parent
->children()[0]);
253 // insert another child to out-of-bounds, when list is not already empty.
254 parent
->InsertChild(child2
, 2459);
256 ASSERT_EQ(2U, parent
->children().size());
257 EXPECT_EQ(child1
, parent
->children()[0]);
258 EXPECT_EQ(child2
, parent
->children()[1]);
261 TEST_F(LayerTest
, InsertSameChildTwice
) {
262 scoped_refptr
<Layer
> parent
= Layer::Create();
263 scoped_refptr
<Layer
> child1
= Layer::Create();
264 scoped_refptr
<Layer
> child2
= Layer::Create();
266 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(parent
));
268 ASSERT_EQ(0U, parent
->children().size());
270 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child1
, 0));
271 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent
->InsertChild(child2
, 1));
273 ASSERT_EQ(2U, parent
->children().size());
274 EXPECT_EQ(child1
, parent
->children()[0]);
275 EXPECT_EQ(child2
, parent
->children()[1]);
277 // Inserting the same child again should cause the child to be removed and
278 // re-inserted at the new location.
279 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent
->InsertChild(child1
, 1));
281 // child1 should now be at the end of the list.
282 ASSERT_EQ(2U, parent
->children().size());
283 EXPECT_EQ(child2
, parent
->children()[0]);
284 EXPECT_EQ(child1
, parent
->children()[1]);
286 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(NULL
));
289 TEST_F(LayerTest
, ReplaceChildWithNewChild
) {
290 CreateSimpleTestTree();
291 scoped_refptr
<Layer
> child4
= Layer::Create();
293 EXPECT_FALSE(child4
->parent());
295 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
296 AtLeast(1), parent_
->ReplaceChild(child2_
.get(), child4
));
297 EXPECT_FALSE(parent_
->NeedsDisplayForTesting());
298 EXPECT_FALSE(child1_
->NeedsDisplayForTesting());
299 EXPECT_FALSE(child2_
->NeedsDisplayForTesting());
300 EXPECT_FALSE(child3_
->NeedsDisplayForTesting());
301 EXPECT_FALSE(child4
->NeedsDisplayForTesting());
303 ASSERT_EQ(static_cast<size_t>(3), parent_
->children().size());
304 EXPECT_EQ(child1_
, parent_
->children()[0]);
305 EXPECT_EQ(child4
, parent_
->children()[1]);
306 EXPECT_EQ(child3_
, parent_
->children()[2]);
307 EXPECT_EQ(parent_
.get(), child4
->parent());
309 EXPECT_FALSE(child2_
->parent());
312 TEST_F(LayerTest
, ReplaceChildWithNewChildThatHasOtherParent
) {
313 CreateSimpleTestTree();
315 // create another simple tree with test_layer and child4.
316 scoped_refptr
<Layer
> test_layer
= Layer::Create();
317 scoped_refptr
<Layer
> child4
= Layer::Create();
318 test_layer
->AddChild(child4
);
319 ASSERT_EQ(1U, test_layer
->children().size());
320 EXPECT_EQ(child4
, test_layer
->children()[0]);
321 EXPECT_EQ(test_layer
.get(), child4
->parent());
323 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
324 AtLeast(1), parent_
->ReplaceChild(child2_
.get(), child4
));
326 ASSERT_EQ(3U, parent_
->children().size());
327 EXPECT_EQ(child1_
, parent_
->children()[0]);
328 EXPECT_EQ(child4
, parent_
->children()[1]);
329 EXPECT_EQ(child3_
, parent_
->children()[2]);
330 EXPECT_EQ(parent_
.get(), child4
->parent());
332 // test_layer should no longer have child4,
333 // and child2 should no longer have a parent.
334 ASSERT_EQ(0U, test_layer
->children().size());
335 EXPECT_FALSE(child2_
->parent());
338 TEST_F(LayerTest
, ReplaceChildWithSameChild
) {
339 CreateSimpleTestTree();
341 // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
343 EXPECT_CALL(*layer_tree_host_
, SetNeedsCommit()).Times(0);
344 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(0);
345 parent_
->ReplaceChild(child2_
.get(), child2_
);
347 VerifyTestTreeInitialState();
350 TEST_F(LayerTest
, RemoveAllChildren
) {
351 CreateSimpleTestTree();
353 EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_
->RemoveAllChildren());
355 ASSERT_EQ(0U, parent_
->children().size());
356 EXPECT_FALSE(child1_
->parent());
357 EXPECT_FALSE(child2_
->parent());
358 EXPECT_FALSE(child3_
->parent());
361 TEST_F(LayerTest
, SetChildren
) {
362 scoped_refptr
<Layer
> old_parent
= Layer::Create();
363 scoped_refptr
<Layer
> new_parent
= Layer::Create();
365 scoped_refptr
<Layer
> child1
= Layer::Create();
366 scoped_refptr
<Layer
> child2
= Layer::Create();
368 LayerList new_children
;
369 new_children
.push_back(child1
);
370 new_children
.push_back(child2
);
372 // Set up and verify initial test conditions: child1 has a parent, child2 has
374 old_parent
->AddChild(child1
);
375 ASSERT_EQ(0U, new_parent
->children().size());
376 EXPECT_EQ(old_parent
.get(), child1
->parent());
377 EXPECT_FALSE(child2
->parent());
379 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
380 1, layer_tree_host_
->SetRootLayer(new_parent
));
382 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
383 AtLeast(1), new_parent
->SetChildren(new_children
));
385 ASSERT_EQ(2U, new_parent
->children().size());
386 EXPECT_EQ(new_parent
.get(), child1
->parent());
387 EXPECT_EQ(new_parent
.get(), child2
->parent());
389 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_
->SetRootLayer(NULL
));
392 TEST_F(LayerTest
, HasAncestor
) {
393 scoped_refptr
<Layer
> parent
= Layer::Create();
394 EXPECT_FALSE(parent
->HasAncestor(parent
));
396 scoped_refptr
<Layer
> child
= Layer::Create();
397 parent
->AddChild(child
);
399 EXPECT_FALSE(child
->HasAncestor(child
));
400 EXPECT_TRUE(child
->HasAncestor(parent
));
401 EXPECT_FALSE(parent
->HasAncestor(child
));
403 scoped_refptr
<Layer
> child_child
= Layer::Create();
404 child
->AddChild(child_child
);
406 EXPECT_FALSE(child_child
->HasAncestor(child_child
));
407 EXPECT_TRUE(child_child
->HasAncestor(parent
));
408 EXPECT_TRUE(child_child
->HasAncestor(child
));
409 EXPECT_FALSE(parent
->HasAncestor(child
));
410 EXPECT_FALSE(parent
->HasAncestor(child_child
));
413 TEST_F(LayerTest
, GetRootLayerAfterTreeManipulations
) {
414 CreateSimpleTestTree();
416 // For this test we don't care about SetNeedsFullTreeSync calls.
417 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times(AnyNumber());
419 scoped_refptr
<Layer
> child4
= Layer::Create();
421 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
422 EXPECT_EQ(parent_
.get(), child1_
->RootLayer());
423 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
424 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
425 EXPECT_EQ(child4
.get(), child4
->RootLayer());
426 EXPECT_EQ(parent_
.get(), grand_child1_
->RootLayer());
427 EXPECT_EQ(parent_
.get(), grand_child2_
->RootLayer());
428 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
430 child1_
->RemoveFromParent();
432 // |child1| and its children, grand_child1 and grand_child2 are now on a
434 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
435 EXPECT_EQ(child1_
.get(), child1_
->RootLayer());
436 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
437 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
438 EXPECT_EQ(child4
.get(), child4
->RootLayer());
439 EXPECT_EQ(child1_
.get(), grand_child1_
->RootLayer());
440 EXPECT_EQ(child1_
.get(), grand_child2_
->RootLayer());
441 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
443 grand_child3_
->AddChild(child4
);
445 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
446 EXPECT_EQ(child1_
.get(), child1_
->RootLayer());
447 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
448 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
449 EXPECT_EQ(parent_
.get(), child4
->RootLayer());
450 EXPECT_EQ(child1_
.get(), grand_child1_
->RootLayer());
451 EXPECT_EQ(child1_
.get(), grand_child2_
->RootLayer());
452 EXPECT_EQ(parent_
.get(), grand_child3_
->RootLayer());
454 child2_
->ReplaceChild(grand_child3_
.get(), child1_
);
456 // |grand_child3| gets orphaned and the child1 subtree gets planted back into
457 // the tree under child2.
458 EXPECT_EQ(parent_
.get(), parent_
->RootLayer());
459 EXPECT_EQ(parent_
.get(), child1_
->RootLayer());
460 EXPECT_EQ(parent_
.get(), child2_
->RootLayer());
461 EXPECT_EQ(parent_
.get(), child3_
->RootLayer());
462 EXPECT_EQ(grand_child3_
.get(), child4
->RootLayer());
463 EXPECT_EQ(parent_
.get(), grand_child1_
->RootLayer());
464 EXPECT_EQ(parent_
.get(), grand_child2_
->RootLayer());
465 EXPECT_EQ(grand_child3_
.get(), grand_child3_
->RootLayer());
468 TEST_F(LayerTest
, CheckSetNeedsDisplayCausesCorrectBehavior
) {
469 // The semantics for SetNeedsDisplay which are tested here:
470 // 1. sets NeedsDisplay flag appropriately.
471 // 2. indirectly calls SetNeedsUpdate, exactly once for each call to
474 scoped_refptr
<Layer
> test_layer
= Layer::Create();
475 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
476 1, layer_tree_host_
->SetRootLayer(test_layer
));
477 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(true));
479 gfx::Size test_bounds
= gfx::Size(501, 508);
481 gfx::RectF dirty1
= gfx::RectF(10.f
, 15.f
, 1.f
, 2.f
);
482 gfx::RectF dirty2
= gfx::RectF(20.f
, 25.f
, 3.f
, 4.f
);
483 gfx::RectF empty_dirty_rect
= gfx::RectF(40.f
, 45.f
, 0.f
, 0.f
);
484 gfx::RectF out_of_bounds_dirty_rect
= gfx::RectF(400.f
, 405.f
, 500.f
, 502.f
);
486 // Before anything, test_layer should not be dirty.
487 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
489 // This is just initialization, but SetNeedsCommit behavior is verified anyway
490 // to avoid warnings.
491 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBounds(test_bounds
));
492 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
494 // The real test begins here.
495 test_layer
->ResetNeedsDisplayForTesting();
496 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
498 // Case 1: Layer should accept dirty rects that go beyond its bounds.
499 test_layer
->ResetNeedsDisplayForTesting();
500 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
501 EXPECT_SET_NEEDS_UPDATE(
502 1, test_layer
->SetNeedsDisplayRect(out_of_bounds_dirty_rect
));
503 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
504 test_layer
->ResetNeedsDisplayForTesting();
506 // Case 2: SetNeedsDisplay() without the dirty rect arg.
507 test_layer
->ResetNeedsDisplayForTesting();
508 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
509 EXPECT_SET_NEEDS_UPDATE(1, test_layer
->SetNeedsDisplay());
510 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
511 test_layer
->ResetNeedsDisplayForTesting();
513 // Case 3: SetNeedsDisplay() with an empty rect.
514 test_layer
->ResetNeedsDisplayForTesting();
515 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
516 EXPECT_SET_NEEDS_COMMIT(0, test_layer
->SetNeedsDisplayRect(gfx::Rect()));
517 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
519 // Case 4: SetNeedsDisplay() with a non-drawable layer
520 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(false));
521 test_layer
->ResetNeedsDisplayForTesting();
522 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
523 EXPECT_SET_NEEDS_UPDATE(0, test_layer
->SetNeedsDisplayRect(dirty1
));
524 EXPECT_TRUE(test_layer
->NeedsDisplayForTesting());
527 TEST_F(LayerTest
, CheckPropertyChangeCausesCorrectBehavior
) {
528 scoped_refptr
<Layer
> test_layer
= Layer::Create();
529 EXPECT_SET_NEEDS_FULL_TREE_SYNC(
530 1, layer_tree_host_
->SetRootLayer(test_layer
));
531 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsDrawable(true));
533 scoped_refptr
<Layer
> dummy_layer1
= Layer::Create();
534 scoped_refptr
<Layer
> dummy_layer2
= Layer::Create();
536 // sanity check of initial test condition
537 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
539 // Next, test properties that should call SetNeedsCommit (but not
540 // SetNeedsDisplay). All properties need to be set to new values in order for
541 // SetNeedsCommit to be called.
542 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetAnchorPoint(
543 gfx::PointF(1.23f
, 4.56f
)));
544 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetAnchorPointZ(0.7f
));
545 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBackgroundColor(SK_ColorLTGRAY
));
546 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetMasksToBounds(true));
547 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
548 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetBlendMode(SkXfermode::kHue_Mode
));
549 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetIsRootForIsolatedGroup(true));
550 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetContentsOpaque(true));
551 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetPosition(gfx::PointF(4.f
, 9.f
)));
552 // We can use any layer pointer here since we aren't syncing for real.
553 EXPECT_SET_NEEDS_COMMIT(1,
554 test_layer
->SetScrollClipLayerId(test_layer
->id()));
555 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetUserScrollable(true, false));
556 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetScrollOffset(
557 gfx::Vector2d(10, 10)));
558 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetShouldScrollOnMainThread(true));
559 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetNonFastScrollableRegion(
560 Region(gfx::Rect(1, 1, 2, 2))));
561 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHaveWheelEventHandlers(true));
562 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(
563 gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
564 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetDoubleSided(false));
565 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTouchEventHandlerRegion(
567 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetDrawCheckerboardForMissingTiles(
568 !test_layer
->DrawCheckerboardForMissingTiles()));
569 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetForceRenderSurface(true));
570 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetHideLayerAndSubtree(true));
572 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer
->SetMaskLayer(
573 dummy_layer1
.get()));
574 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer
->SetReplicaLayer(
575 dummy_layer2
.get()));
577 // The above tests should not have caused a change to the needs_display flag.
578 EXPECT_FALSE(test_layer
->NeedsDisplayForTesting());
580 // As layers are removed from the tree, they will cause a tree sync.
581 EXPECT_CALL(*layer_tree_host_
, SetNeedsFullTreeSync()).Times((AnyNumber()));
584 TEST_F(LayerTest
, PushPropertiesAccumulatesUpdateRect
) {
585 scoped_refptr
<Layer
> test_layer
= Layer::Create();
586 scoped_ptr
<LayerImpl
> impl_layer
=
587 LayerImpl::Create(host_impl_
.active_tree(), 1);
589 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
590 layer_tree_host_
->SetRootLayer(test_layer
));
592 test_layer
->SetNeedsDisplayRect(gfx::RectF(0.f
, 0.f
, 5.f
, 5.f
));
593 test_layer
->PushPropertiesTo(impl_layer
.get());
594 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f
, 0.f
, 5.f
, 5.f
),
595 impl_layer
->update_rect());
597 // The LayerImpl's update_rect() should be accumulated here, since we did not
598 // do anything to clear it.
599 test_layer
->SetNeedsDisplayRect(gfx::RectF(10.f
, 10.f
, 5.f
, 5.f
));
600 test_layer
->PushPropertiesTo(impl_layer
.get());
601 EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f
, 0.f
, 15.f
, 15.f
),
602 impl_layer
->update_rect());
604 // If we do clear the LayerImpl side, then the next update_rect() should be
605 // fresh without accumulation.
606 impl_layer
->ResetAllChangeTrackingForSubtree();
607 test_layer
->SetNeedsDisplayRect(gfx::RectF(10.f
, 10.f
, 5.f
, 5.f
));
608 test_layer
->PushPropertiesTo(impl_layer
.get());
609 EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f
, 10.f
, 5.f
, 5.f
),
610 impl_layer
->update_rect());
613 TEST_F(LayerTest
, PushPropertiesCausesLayerPropertyChangedForTransform
) {
614 scoped_refptr
<Layer
> test_layer
= Layer::Create();
615 scoped_ptr
<LayerImpl
> impl_layer
=
616 LayerImpl::Create(host_impl_
.active_tree(), 1);
618 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
619 layer_tree_host_
->SetRootLayer(test_layer
));
621 gfx::Transform transform
;
622 transform
.Rotate(45.0);
623 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
625 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
627 test_layer
->PushPropertiesTo(impl_layer
.get());
629 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
632 TEST_F(LayerTest
, PushPropertiesCausesLayerPropertyChangedForOpacity
) {
633 scoped_refptr
<Layer
> test_layer
= Layer::Create();
634 scoped_ptr
<LayerImpl
> impl_layer
=
635 LayerImpl::Create(host_impl_
.active_tree(), 1);
637 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
638 layer_tree_host_
->SetRootLayer(test_layer
));
640 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
642 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
644 test_layer
->PushPropertiesTo(impl_layer
.get());
646 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
650 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim
) {
651 scoped_refptr
<Layer
> test_layer
= Layer::Create();
652 scoped_ptr
<LayerImpl
> impl_layer
=
653 LayerImpl::Create(host_impl_
.active_tree(), 1);
655 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
656 layer_tree_host_
->SetRootLayer(test_layer
));
658 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
659 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
662 AddAnimatedTransformToController(impl_layer
->layer_animation_controller(),
667 gfx::Transform transform
;
668 transform
.Rotate(45.0);
669 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
671 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
672 test_layer
->PushPropertiesTo(impl_layer
.get());
673 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
675 impl_layer
->ResetAllChangeTrackingForSubtree();
676 AddAnimatedTransformToController(impl_layer
->layer_animation_controller(),
680 impl_layer
->layer_animation_controller()->GetAnimation(Animation::Transform
)->
681 set_is_impl_only(true);
682 transform
.Rotate(45.0);
683 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetTransform(transform
));
685 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
686 test_layer
->PushPropertiesTo(impl_layer
.get());
687 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
691 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim
) {
692 scoped_refptr
<Layer
> test_layer
= Layer::Create();
693 scoped_ptr
<LayerImpl
> impl_layer
=
694 LayerImpl::Create(host_impl_
.active_tree(), 1);
696 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
697 layer_tree_host_
->SetRootLayer(test_layer
));
699 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
700 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
703 AddOpacityTransitionToController(impl_layer
->layer_animation_controller(),
709 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.5f
));
711 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
712 test_layer
->PushPropertiesTo(impl_layer
.get());
713 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
715 impl_layer
->ResetAllChangeTrackingForSubtree();
716 AddOpacityTransitionToController(impl_layer
->layer_animation_controller(),
721 impl_layer
->layer_animation_controller()->GetAnimation(Animation::Opacity
)->
722 set_is_impl_only(true);
723 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetOpacity(0.75f
));
725 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
726 test_layer
->PushPropertiesTo(impl_layer
.get());
727 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
731 PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim
) {
732 scoped_refptr
<Layer
> test_layer
= Layer::Create();
733 scoped_ptr
<LayerImpl
> impl_layer
=
734 LayerImpl::Create(host_impl_
.active_tree(), 1);
736 EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
737 layer_tree_host_
->SetRootLayer(test_layer
));
739 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
740 impl_layer
->layer_animation_controller()->SetAnimationRegistrar(
743 AddAnimatedFilterToController(
744 impl_layer
->layer_animation_controller(), 1.0, 1.f
, 2.f
);
746 FilterOperations filters
;
747 filters
.Append(FilterOperation::CreateBlurFilter(2.f
));
748 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetFilters(filters
));
750 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
751 test_layer
->PushPropertiesTo(impl_layer
.get());
752 EXPECT_TRUE(impl_layer
->LayerPropertyChanged());
754 impl_layer
->ResetAllChangeTrackingForSubtree();
755 AddAnimatedFilterToController(
756 impl_layer
->layer_animation_controller(), 1.0, 1.f
, 2.f
);
757 impl_layer
->layer_animation_controller()->GetAnimation(Animation::Filter
)->
758 set_is_impl_only(true);
759 filters
.Append(FilterOperation::CreateSepiaFilter(0.5f
));
760 EXPECT_SET_NEEDS_COMMIT(1, test_layer
->SetFilters(filters
));
762 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
763 test_layer
->PushPropertiesTo(impl_layer
.get());
764 EXPECT_FALSE(impl_layer
->LayerPropertyChanged());
767 TEST_F(LayerTest
, MaskAndReplicaHasParent
) {
768 scoped_refptr
<Layer
> parent
= Layer::Create();
769 scoped_refptr
<Layer
> child
= Layer::Create();
770 scoped_refptr
<Layer
> mask
= Layer::Create();
771 scoped_refptr
<Layer
> replica
= Layer::Create();
772 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
773 scoped_refptr
<Layer
> mask_replacement
= Layer::Create();
774 scoped_refptr
<Layer
> replica_replacement
= Layer::Create();
775 scoped_refptr
<Layer
> replica_mask_replacement
= Layer::Create();
777 parent
->AddChild(child
);
778 child
->SetMaskLayer(mask
.get());
779 child
->SetReplicaLayer(replica
.get());
780 replica
->SetMaskLayer(replica_mask
.get());
782 EXPECT_EQ(parent
, child
->parent());
783 EXPECT_EQ(child
, mask
->parent());
784 EXPECT_EQ(child
, replica
->parent());
785 EXPECT_EQ(replica
, replica_mask
->parent());
787 replica
->SetMaskLayer(replica_mask_replacement
.get());
788 EXPECT_EQ(NULL
, replica_mask
->parent());
789 EXPECT_EQ(replica
, replica_mask_replacement
->parent());
791 child
->SetMaskLayer(mask_replacement
.get());
792 EXPECT_EQ(NULL
, mask
->parent());
793 EXPECT_EQ(child
, mask_replacement
->parent());
795 child
->SetReplicaLayer(replica_replacement
.get());
796 EXPECT_EQ(NULL
, replica
->parent());
797 EXPECT_EQ(child
, replica_replacement
->parent());
799 EXPECT_EQ(replica
, replica
->mask_layer()->parent());
802 class LayerTreeHostFactory
{
804 LayerTreeHostFactory()
805 : client_(FakeLayerTreeHostClient::DIRECT_3D
) {}
807 scoped_ptr
<LayerTreeHost
> Create() {
808 return LayerTreeHost::CreateSingleThreaded(&client_
,
811 LayerTreeSettings()).Pass();
814 scoped_ptr
<LayerTreeHost
> Create(LayerTreeSettings settings
) {
815 return LayerTreeHost::CreateSingleThreaded(&client_
,
822 FakeLayerTreeHostClient client_
;
825 void AssertLayerTreeHostMatchesForSubtree(Layer
* layer
, LayerTreeHost
* host
) {
826 EXPECT_EQ(host
, layer
->layer_tree_host());
828 for (size_t i
= 0; i
< layer
->children().size(); ++i
)
829 AssertLayerTreeHostMatchesForSubtree(layer
->children()[i
].get(), host
);
831 if (layer
->mask_layer())
832 AssertLayerTreeHostMatchesForSubtree(layer
->mask_layer(), host
);
834 if (layer
->replica_layer())
835 AssertLayerTreeHostMatchesForSubtree(layer
->replica_layer(), host
);
838 TEST(LayerLayerTreeHostTest
, EnteringTree
) {
839 scoped_refptr
<Layer
> parent
= Layer::Create();
840 scoped_refptr
<Layer
> child
= Layer::Create();
841 scoped_refptr
<Layer
> mask
= Layer::Create();
842 scoped_refptr
<Layer
> replica
= Layer::Create();
843 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
845 // Set up a detached tree of layers. The host pointer should be nil for these
847 parent
->AddChild(child
);
848 child
->SetMaskLayer(mask
.get());
849 child
->SetReplicaLayer(replica
.get());
850 replica
->SetMaskLayer(replica_mask
.get());
852 AssertLayerTreeHostMatchesForSubtree(parent
.get(), NULL
);
854 LayerTreeHostFactory factory
;
855 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
856 // Setting the root layer should set the host pointer for all layers in the
858 layer_tree_host
->SetRootLayer(parent
.get());
860 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
862 // Clearing the root layer should also clear out the host pointers for all
863 // layers in the tree.
864 layer_tree_host
->SetRootLayer(NULL
);
866 AssertLayerTreeHostMatchesForSubtree(parent
.get(), NULL
);
869 TEST(LayerLayerTreeHostTest
, AddingLayerSubtree
) {
870 scoped_refptr
<Layer
> parent
= Layer::Create();
871 LayerTreeHostFactory factory
;
872 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
874 layer_tree_host
->SetRootLayer(parent
.get());
876 EXPECT_EQ(parent
->layer_tree_host(), layer_tree_host
.get());
878 // Adding a subtree to a layer already associated with a host should set the
879 // host pointer on all layers in that subtree.
880 scoped_refptr
<Layer
> child
= Layer::Create();
881 scoped_refptr
<Layer
> grand_child
= Layer::Create();
882 child
->AddChild(grand_child
);
884 // Masks, replicas, and replica masks should pick up the new host too.
885 scoped_refptr
<Layer
> child_mask
= Layer::Create();
886 child
->SetMaskLayer(child_mask
.get());
887 scoped_refptr
<Layer
> child_replica
= Layer::Create();
888 child
->SetReplicaLayer(child_replica
.get());
889 scoped_refptr
<Layer
> child_replica_mask
= Layer::Create();
890 child_replica
->SetMaskLayer(child_replica_mask
.get());
892 parent
->AddChild(child
);
893 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
895 layer_tree_host
->SetRootLayer(NULL
);
898 TEST(LayerLayerTreeHostTest
, ChangeHost
) {
899 scoped_refptr
<Layer
> parent
= Layer::Create();
900 scoped_refptr
<Layer
> child
= Layer::Create();
901 scoped_refptr
<Layer
> mask
= Layer::Create();
902 scoped_refptr
<Layer
> replica
= Layer::Create();
903 scoped_refptr
<Layer
> replica_mask
= Layer::Create();
905 // Same setup as the previous test.
906 parent
->AddChild(child
);
907 child
->SetMaskLayer(mask
.get());
908 child
->SetReplicaLayer(replica
.get());
909 replica
->SetMaskLayer(replica_mask
.get());
911 LayerTreeHostFactory factory
;
912 scoped_ptr
<LayerTreeHost
> first_layer_tree_host
= factory
.Create();
913 first_layer_tree_host
->SetRootLayer(parent
.get());
915 AssertLayerTreeHostMatchesForSubtree(parent
.get(),
916 first_layer_tree_host
.get());
918 // Now re-root the tree to a new host (simulating what we do on a context lost
919 // event). This should update the host pointers for all layers in the tree.
920 scoped_ptr
<LayerTreeHost
> second_layer_tree_host
= factory
.Create();
921 second_layer_tree_host
->SetRootLayer(parent
.get());
923 AssertLayerTreeHostMatchesForSubtree(parent
.get(),
924 second_layer_tree_host
.get());
926 second_layer_tree_host
->SetRootLayer(NULL
);
929 TEST(LayerLayerTreeHostTest
, ChangeHostInSubtree
) {
930 scoped_refptr
<Layer
> first_parent
= Layer::Create();
931 scoped_refptr
<Layer
> first_child
= Layer::Create();
932 scoped_refptr
<Layer
> second_parent
= Layer::Create();
933 scoped_refptr
<Layer
> second_child
= Layer::Create();
934 scoped_refptr
<Layer
> second_grand_child
= Layer::Create();
936 // First put all children under the first parent and set the first host.
937 first_parent
->AddChild(first_child
);
938 second_child
->AddChild(second_grand_child
);
939 first_parent
->AddChild(second_child
);
941 LayerTreeHostFactory factory
;
942 scoped_ptr
<LayerTreeHost
> first_layer_tree_host
= factory
.Create();
943 first_layer_tree_host
->SetRootLayer(first_parent
.get());
945 AssertLayerTreeHostMatchesForSubtree(first_parent
.get(),
946 first_layer_tree_host
.get());
948 // Now reparent the subtree starting at second_child to a layer in a different
950 scoped_ptr
<LayerTreeHost
> second_layer_tree_host
= factory
.Create();
951 second_layer_tree_host
->SetRootLayer(second_parent
.get());
953 second_parent
->AddChild(second_child
);
955 // The moved layer and its children should point to the new host.
956 EXPECT_EQ(second_layer_tree_host
.get(), second_child
->layer_tree_host());
957 EXPECT_EQ(second_layer_tree_host
.get(),
958 second_grand_child
->layer_tree_host());
960 // Test over, cleanup time.
961 first_layer_tree_host
->SetRootLayer(NULL
);
962 second_layer_tree_host
->SetRootLayer(NULL
);
965 TEST(LayerLayerTreeHostTest
, ReplaceMaskAndReplicaLayer
) {
966 scoped_refptr
<Layer
> parent
= Layer::Create();
967 scoped_refptr
<Layer
> mask
= Layer::Create();
968 scoped_refptr
<Layer
> replica
= Layer::Create();
969 scoped_refptr
<Layer
> mask_child
= Layer::Create();
970 scoped_refptr
<Layer
> replica_child
= Layer::Create();
971 scoped_refptr
<Layer
> mask_replacement
= Layer::Create();
972 scoped_refptr
<Layer
> replica_replacement
= Layer::Create();
974 parent
->SetMaskLayer(mask
.get());
975 parent
->SetReplicaLayer(replica
.get());
976 mask
->AddChild(mask_child
);
977 replica
->AddChild(replica_child
);
979 LayerTreeHostFactory factory
;
980 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
981 layer_tree_host
->SetRootLayer(parent
.get());
983 AssertLayerTreeHostMatchesForSubtree(parent
.get(), layer_tree_host
.get());
985 // Replacing the mask should clear out the old mask's subtree's host pointers.
986 parent
->SetMaskLayer(mask_replacement
.get());
987 EXPECT_EQ(NULL
, mask
->layer_tree_host());
988 EXPECT_EQ(NULL
, mask_child
->layer_tree_host());
990 // Same for replacing a replica layer.
991 parent
->SetReplicaLayer(replica_replacement
.get());
992 EXPECT_EQ(NULL
, replica
->layer_tree_host());
993 EXPECT_EQ(NULL
, replica_child
->layer_tree_host());
995 // Test over, cleanup time.
996 layer_tree_host
->SetRootLayer(NULL
);
999 TEST(LayerLayerTreeHostTest
, DestroyHostWithNonNullRootLayer
) {
1000 scoped_refptr
<Layer
> root
= Layer::Create();
1001 scoped_refptr
<Layer
> child
= Layer::Create();
1002 root
->AddChild(child
);
1003 LayerTreeHostFactory factory
;
1004 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1005 layer_tree_host
->SetRootLayer(root
);
1008 static bool AddTestAnimation(Layer
* layer
) {
1009 scoped_ptr
<KeyframedFloatAnimationCurve
> curve
=
1010 KeyframedFloatAnimationCurve::Create();
1011 curve
->AddKeyframe(FloatKeyframe::Create(0.0,
1013 scoped_ptr
<TimingFunction
>()));
1014 curve
->AddKeyframe(FloatKeyframe::Create(1.0,
1016 scoped_ptr
<TimingFunction
>()));
1017 scoped_ptr
<Animation
> animation
=
1018 Animation::Create(curve
.PassAs
<AnimationCurve
>(),
1021 Animation::Opacity
);
1023 return layer
->AddAnimation(animation
.Pass());
1026 TEST(LayerLayerTreeHostTest
, ShouldNotAddAnimationWithoutAnimationRegistrar
) {
1027 scoped_refptr
<Layer
> layer
= Layer::Create();
1029 // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1030 // animation should not be accepted.
1031 EXPECT_FALSE(AddTestAnimation(layer
.get()));
1033 scoped_ptr
<AnimationRegistrar
> registrar
= AnimationRegistrar::Create();
1034 layer
->layer_animation_controller()->SetAnimationRegistrar(registrar
.get());
1036 // Case 2: with an AnimationRegistrar, the animation should be accepted.
1037 EXPECT_TRUE(AddTestAnimation(layer
.get()));
1039 LayerTreeSettings settings
;
1040 settings
.accelerated_animation_enabled
= false;
1041 LayerTreeHostFactory factory
;
1042 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create(settings
);
1043 layer_tree_host
->SetRootLayer(layer
);
1044 AssertLayerTreeHostMatchesForSubtree(layer
.get(), layer_tree_host
.get());
1046 // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1047 // animation should be rejected.
1048 EXPECT_FALSE(AddTestAnimation(layer
.get()));
1051 TEST_F(LayerTest
, SafeOpaqueBackgroundColor
) {
1052 LayerTreeHostFactory factory
;
1053 scoped_ptr
<LayerTreeHost
> layer_tree_host
= factory
.Create();
1055 scoped_refptr
<Layer
> layer
= Layer::Create();
1056 layer_tree_host
->SetRootLayer(layer
);
1058 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
1059 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
1060 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
1061 layer
->SetContentsOpaque(!!contents_opaque
);
1062 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
1063 : SK_ColorTRANSPARENT
);
1064 layer_tree_host
->set_background_color(
1065 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
1067 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
1068 if (contents_opaque
) {
1069 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
1070 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
1071 << host_opaque
<< "\n";
1073 EXPECT_NE(SkColorGetA(safe_color
), 255u)
1074 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
1075 << host_opaque
<< "\n";