Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / layers / layer_unittest.cc
1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "cc/layers/layer.h"
6
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/test/test_gpu_memory_buffer_manager.h"
18 #include "cc/test/test_shared_bitmap_manager.h"
19 #include "cc/trees/layer_tree_host.h"
20 #include "cc/trees/single_thread_proxy.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "ui/gfx/transform.h"
24
25 using ::testing::AnyNumber;
26 using ::testing::AtLeast;
27 using ::testing::Mock;
28 using ::testing::StrictMock;
29 using ::testing::_;
30
31 #define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test)               \
32   do {                                                                      \
33     EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
34     code_to_test;                                                           \
35     Mock::VerifyAndClearExpectations(layer_tree_host_.get());               \
36   } while (false)
37
38 namespace cc {
39 namespace {
40
41 class MockLayerTreeHost : public LayerTreeHost {
42  public:
43   explicit MockLayerTreeHost(FakeLayerTreeHostClient* client)
44       : LayerTreeHost(client, nullptr, nullptr, LayerTreeSettings()) {
45     InitializeSingleThreaded(client, base::MessageLoopProxy::current());
46   }
47
48   MOCK_METHOD0(SetNeedsCommit, void());
49   MOCK_METHOD0(SetNeedsUpdateLayers, void());
50   MOCK_METHOD0(SetNeedsFullTreeSync, void());
51 };
52
53 class MockLayerPainter : public LayerPainter {
54  public:
55   void Paint(SkCanvas* canvas, const gfx::Rect& content_rect) override {}
56 };
57
58 class LayerTest : public testing::Test {
59  public:
60   LayerTest()
61       : host_impl_(&proxy_, &shared_bitmap_manager_),
62         fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
63
64  protected:
65   virtual void SetUp() override {
66     layer_tree_host_.reset(new StrictMock<MockLayerTreeHost>(&fake_client_));
67   }
68
69   virtual void TearDown() override {
70     Mock::VerifyAndClearExpectations(layer_tree_host_.get());
71     EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
72     parent_ = nullptr;
73     child1_ = nullptr;
74     child2_ = nullptr;
75     child3_ = nullptr;
76     grand_child1_ = nullptr;
77     grand_child2_ = nullptr;
78     grand_child3_ = nullptr;
79
80     layer_tree_host_->SetRootLayer(nullptr);
81     layer_tree_host_ = nullptr;
82   }
83
84   void VerifyTestTreeInitialState() const {
85     ASSERT_EQ(3U, parent_->children().size());
86     EXPECT_EQ(child1_, parent_->children()[0]);
87     EXPECT_EQ(child2_, parent_->children()[1]);
88     EXPECT_EQ(child3_, parent_->children()[2]);
89     EXPECT_EQ(parent_.get(), child1_->parent());
90     EXPECT_EQ(parent_.get(), child2_->parent());
91     EXPECT_EQ(parent_.get(), child3_->parent());
92
93     ASSERT_EQ(2U, child1_->children().size());
94     EXPECT_EQ(grand_child1_, child1_->children()[0]);
95     EXPECT_EQ(grand_child2_, child1_->children()[1]);
96     EXPECT_EQ(child1_.get(), grand_child1_->parent());
97     EXPECT_EQ(child1_.get(), grand_child2_->parent());
98
99     ASSERT_EQ(1U, child2_->children().size());
100     EXPECT_EQ(grand_child3_, child2_->children()[0]);
101     EXPECT_EQ(child2_.get(), grand_child3_->parent());
102
103     ASSERT_EQ(0U, child3_->children().size());
104   }
105
106   void CreateSimpleTestTree() {
107     parent_ = Layer::Create();
108     child1_ = Layer::Create();
109     child2_ = Layer::Create();
110     child3_ = Layer::Create();
111     grand_child1_ = Layer::Create();
112     grand_child2_ = Layer::Create();
113     grand_child3_ = Layer::Create();
114
115     EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
116     layer_tree_host_->SetRootLayer(parent_);
117
118     parent_->AddChild(child1_);
119     parent_->AddChild(child2_);
120     parent_->AddChild(child3_);
121     child1_->AddChild(grand_child1_);
122     child1_->AddChild(grand_child2_);
123     child2_->AddChild(grand_child3_);
124
125     Mock::VerifyAndClearExpectations(layer_tree_host_.get());
126
127     VerifyTestTreeInitialState();
128   }
129
130   FakeImplProxy proxy_;
131   TestSharedBitmapManager shared_bitmap_manager_;
132   FakeLayerTreeHostImpl host_impl_;
133
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_;
143 };
144
145 TEST_F(LayerTest, BasicCreateAndDestroy) {
146   scoped_refptr<Layer> test_layer = Layer::Create();
147   ASSERT_TRUE(test_layer.get());
148
149   EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
150   test_layer->SetLayerTreeHost(layer_tree_host_.get());
151   Mock::VerifyAndClearExpectations(layer_tree_host_.get());
152
153   EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
154   test_layer->SetLayerTreeHost(nullptr);
155 }
156
157 TEST_F(LayerTest, AddAndRemoveChild) {
158   scoped_refptr<Layer> parent = Layer::Create();
159   scoped_refptr<Layer> child = Layer::Create();
160
161   // Upon creation, layers should not have children or parent.
162   ASSERT_EQ(0U, parent->children().size());
163   EXPECT_FALSE(child->parent());
164
165   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
166   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
167
168   ASSERT_EQ(1U, parent->children().size());
169   EXPECT_EQ(child.get(), parent->children()[0].get());
170   EXPECT_EQ(parent.get(), child->parent());
171   EXPECT_EQ(parent.get(), child->RootLayer());
172
173   EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
174 }
175
176 TEST_F(LayerTest, AddSameChildTwice) {
177   EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
178
179   scoped_refptr<Layer> parent = Layer::Create();
180   scoped_refptr<Layer> child = Layer::Create();
181
182   layer_tree_host_->SetRootLayer(parent);
183
184   ASSERT_EQ(0u, parent->children().size());
185
186   parent->AddChild(child);
187   ASSERT_EQ(1u, parent->children().size());
188   EXPECT_EQ(parent.get(), child->parent());
189
190   parent->AddChild(child);
191   ASSERT_EQ(1u, parent->children().size());
192   EXPECT_EQ(parent.get(), child->parent());
193 }
194
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();
201
202   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
203
204   ASSERT_EQ(0U, parent->children().size());
205
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());
211
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());
218
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());
226
227   // Case 4: inserting to end of list
228   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
229
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());
236
237   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
238 }
239
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();
244
245   ASSERT_EQ(0U, parent->children().size());
246
247   // insert to an out-of-bounds index
248   parent->InsertChild(child1, 53);
249
250   ASSERT_EQ(1U, parent->children().size());
251   EXPECT_EQ(child1, parent->children()[0]);
252
253   // insert another child to out-of-bounds, when list is not already empty.
254   parent->InsertChild(child2, 2459);
255
256   ASSERT_EQ(2U, parent->children().size());
257   EXPECT_EQ(child1, parent->children()[0]);
258   EXPECT_EQ(child2, parent->children()[1]);
259 }
260
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();
265
266   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
267
268   ASSERT_EQ(0U, parent->children().size());
269
270   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
271   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
272
273   ASSERT_EQ(2U, parent->children().size());
274   EXPECT_EQ(child1, parent->children()[0]);
275   EXPECT_EQ(child2, parent->children()[1]);
276
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));
280
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]);
285
286   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
287 }
288
289 TEST_F(LayerTest, ReplaceChildWithNewChild) {
290   CreateSimpleTestTree();
291   scoped_refptr<Layer> child4 = Layer::Create();
292
293   EXPECT_FALSE(child4->parent());
294
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());
302
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());
308
309   EXPECT_FALSE(child2_->parent());
310 }
311
312 TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
313   CreateSimpleTestTree();
314
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());
322
323   EXPECT_SET_NEEDS_FULL_TREE_SYNC(
324       AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
325
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());
331
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());
336 }
337
338 TEST_F(LayerTest, DeleteRemovedScrollParent) {
339   scoped_refptr<Layer> parent = Layer::Create();
340   scoped_refptr<Layer> child1 = Layer::Create();
341   scoped_refptr<Layer> child2 = Layer::Create();
342
343   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
344
345   ASSERT_EQ(0U, parent->children().size());
346
347   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
348   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
349
350   ASSERT_EQ(2U, parent->children().size());
351   EXPECT_EQ(child1, parent->children()[0]);
352   EXPECT_EQ(child2, parent->children()[1]);
353
354   EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
355
356   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
357
358   child1->reset_needs_push_properties_for_testing();
359
360   EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
361
362   EXPECT_TRUE(child1->needs_push_properties());
363
364   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
365 }
366
367 TEST_F(LayerTest, DeleteRemovedScrollChild) {
368   scoped_refptr<Layer> parent = Layer::Create();
369   scoped_refptr<Layer> child1 = Layer::Create();
370   scoped_refptr<Layer> child2 = Layer::Create();
371
372   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
373
374   ASSERT_EQ(0U, parent->children().size());
375
376   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
377   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
378
379   ASSERT_EQ(2U, parent->children().size());
380   EXPECT_EQ(child1, parent->children()[0]);
381   EXPECT_EQ(child2, parent->children()[1]);
382
383   EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
384
385   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
386
387   child2->reset_needs_push_properties_for_testing();
388
389   EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
390
391   EXPECT_TRUE(child2->needs_push_properties());
392
393   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
394 }
395
396 TEST_F(LayerTest, ReplaceChildWithSameChild) {
397   CreateSimpleTestTree();
398
399   // SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
400   // same child.
401   EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
402   EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
403   parent_->ReplaceChild(child2_.get(), child2_);
404
405   VerifyTestTreeInitialState();
406 }
407
408 TEST_F(LayerTest, RemoveAllChildren) {
409   CreateSimpleTestTree();
410
411   EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
412
413   ASSERT_EQ(0U, parent_->children().size());
414   EXPECT_FALSE(child1_->parent());
415   EXPECT_FALSE(child2_->parent());
416   EXPECT_FALSE(child3_->parent());
417 }
418
419 TEST_F(LayerTest, SetChildren) {
420   scoped_refptr<Layer> old_parent = Layer::Create();
421   scoped_refptr<Layer> new_parent = Layer::Create();
422
423   scoped_refptr<Layer> child1 = Layer::Create();
424   scoped_refptr<Layer> child2 = Layer::Create();
425
426   LayerList new_children;
427   new_children.push_back(child1);
428   new_children.push_back(child2);
429
430   // Set up and verify initial test conditions: child1 has a parent, child2 has
431   // no parent.
432   old_parent->AddChild(child1);
433   ASSERT_EQ(0U, new_parent->children().size());
434   EXPECT_EQ(old_parent.get(), child1->parent());
435   EXPECT_FALSE(child2->parent());
436
437   EXPECT_SET_NEEDS_FULL_TREE_SYNC(
438       1, layer_tree_host_->SetRootLayer(new_parent));
439
440   EXPECT_SET_NEEDS_FULL_TREE_SYNC(
441       AtLeast(1), new_parent->SetChildren(new_children));
442
443   ASSERT_EQ(2U, new_parent->children().size());
444   EXPECT_EQ(new_parent.get(), child1->parent());
445   EXPECT_EQ(new_parent.get(), child2->parent());
446
447   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
448 }
449
450 TEST_F(LayerTest, HasAncestor) {
451   scoped_refptr<Layer> parent = Layer::Create();
452   EXPECT_FALSE(parent->HasAncestor(parent.get()));
453
454   scoped_refptr<Layer> child = Layer::Create();
455   parent->AddChild(child);
456
457   EXPECT_FALSE(child->HasAncestor(child.get()));
458   EXPECT_TRUE(child->HasAncestor(parent.get()));
459   EXPECT_FALSE(parent->HasAncestor(child.get()));
460
461   scoped_refptr<Layer> child_child = Layer::Create();
462   child->AddChild(child_child);
463
464   EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
465   EXPECT_TRUE(child_child->HasAncestor(parent.get()));
466   EXPECT_TRUE(child_child->HasAncestor(child.get()));
467   EXPECT_FALSE(parent->HasAncestor(child.get()));
468   EXPECT_FALSE(parent->HasAncestor(child_child.get()));
469 }
470
471 TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
472   CreateSimpleTestTree();
473
474   // For this test we don't care about SetNeedsFullTreeSync calls.
475   EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
476
477   scoped_refptr<Layer> child4 = Layer::Create();
478
479   EXPECT_EQ(parent_.get(), parent_->RootLayer());
480   EXPECT_EQ(parent_.get(), child1_->RootLayer());
481   EXPECT_EQ(parent_.get(), child2_->RootLayer());
482   EXPECT_EQ(parent_.get(), child3_->RootLayer());
483   EXPECT_EQ(child4.get(),   child4->RootLayer());
484   EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
485   EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
486   EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
487
488   child1_->RemoveFromParent();
489
490   // |child1| and its children, grand_child1 and grand_child2 are now on a
491   // separate subtree.
492   EXPECT_EQ(parent_.get(), parent_->RootLayer());
493   EXPECT_EQ(child1_.get(), child1_->RootLayer());
494   EXPECT_EQ(parent_.get(), child2_->RootLayer());
495   EXPECT_EQ(parent_.get(), child3_->RootLayer());
496   EXPECT_EQ(child4.get(), child4->RootLayer());
497   EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
498   EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
499   EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
500
501   grand_child3_->AddChild(child4);
502
503   EXPECT_EQ(parent_.get(), parent_->RootLayer());
504   EXPECT_EQ(child1_.get(), child1_->RootLayer());
505   EXPECT_EQ(parent_.get(), child2_->RootLayer());
506   EXPECT_EQ(parent_.get(), child3_->RootLayer());
507   EXPECT_EQ(parent_.get(), child4->RootLayer());
508   EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
509   EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
510   EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
511
512   child2_->ReplaceChild(grand_child3_.get(), child1_);
513
514   // |grand_child3| gets orphaned and the child1 subtree gets planted back into
515   // the tree under child2.
516   EXPECT_EQ(parent_.get(), parent_->RootLayer());
517   EXPECT_EQ(parent_.get(), child1_->RootLayer());
518   EXPECT_EQ(parent_.get(), child2_->RootLayer());
519   EXPECT_EQ(parent_.get(), child3_->RootLayer());
520   EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
521   EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
522   EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
523   EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
524 }
525
526 TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
527   // The semantics for SetNeedsDisplay which are tested here:
528   //   1. sets NeedsDisplay flag appropriately.
529   //   2. indirectly calls SetNeedsUpdate, exactly once for each call to
530   //      SetNeedsDisplay.
531
532   scoped_refptr<Layer> test_layer = Layer::Create();
533   EXPECT_SET_NEEDS_FULL_TREE_SYNC(
534       1, layer_tree_host_->SetRootLayer(test_layer));
535   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
536
537   gfx::Size test_bounds = gfx::Size(501, 508);
538
539   gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
540   gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
541   gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
542
543   // Before anything, test_layer should not be dirty.
544   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
545
546   // This is just initialization, but SetNeedsCommit behavior is verified anyway
547   // to avoid warnings.
548   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
549   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
550
551   // The real test begins here.
552   test_layer->ResetNeedsDisplayForTesting();
553   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
554
555   // Case 1: Layer should accept dirty rects that go beyond its bounds.
556   test_layer->ResetNeedsDisplayForTesting();
557   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
558   EXPECT_SET_NEEDS_UPDATE(
559       1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
560   EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
561   test_layer->ResetNeedsDisplayForTesting();
562
563   // Case 2: SetNeedsDisplay() without the dirty rect arg.
564   test_layer->ResetNeedsDisplayForTesting();
565   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
566   EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
567   EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
568   test_layer->ResetNeedsDisplayForTesting();
569
570   // Case 3: SetNeedsDisplay() with an empty rect.
571   test_layer->ResetNeedsDisplayForTesting();
572   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
573   EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
574   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
575
576   // Case 4: SetNeedsDisplay() with a non-drawable layer
577   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
578   test_layer->ResetNeedsDisplayForTesting();
579   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
580   EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
581   EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
582 }
583
584 TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
585   scoped_refptr<Layer> test_layer = Layer::Create();
586   EXPECT_SET_NEEDS_FULL_TREE_SYNC(
587       1, layer_tree_host_->SetRootLayer(test_layer));
588   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
589
590   scoped_refptr<Layer> dummy_layer1 = Layer::Create();
591   scoped_refptr<Layer> dummy_layer2 = Layer::Create();
592
593   // sanity check of initial test condition
594   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
595
596   // Next, test properties that should call SetNeedsCommit (but not
597   // SetNeedsDisplay). All properties need to be set to new values in order for
598   // SetNeedsCommit to be called.
599   EXPECT_SET_NEEDS_COMMIT(
600       1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
601   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
602   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
603   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
604   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
605   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
606   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
607   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
608   // We can use any layer pointer here since we aren't syncing for real.
609   EXPECT_SET_NEEDS_COMMIT(1,
610                           test_layer->SetScrollClipLayerId(test_layer->id()));
611   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
612   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
613       gfx::ScrollOffset(10, 10)));
614   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
615   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
616       Region(gfx::Rect(1, 1, 2, 2))));
617   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
618   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
619   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
620       gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
621   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
622   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
623       gfx::Rect(10, 10)));
624   EXPECT_SET_NEEDS_COMMIT(
625       1,
626       test_layer->SetDrawCheckerboardForMissingTiles(
627           !test_layer->draw_checkerboard_for_missing_tiles()));
628   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
629   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
630
631   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
632       dummy_layer1.get()));
633   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
634       dummy_layer2.get()));
635
636   // The above tests should not have caused a change to the needs_display flag.
637   EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
638
639   // As layers are removed from the tree, they will cause a tree sync.
640   EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
641 }
642
643 TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
644   scoped_refptr<Layer> test_layer = Layer::Create();
645   scoped_ptr<LayerImpl> impl_layer =
646       LayerImpl::Create(host_impl_.active_tree(), 1);
647
648   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
649                                   layer_tree_host_->SetRootLayer(test_layer));
650
651   test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
652   test_layer->PushPropertiesTo(impl_layer.get());
653   EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
654                        impl_layer->update_rect());
655
656   // The LayerImpl's update_rect() should be accumulated here, since we did not
657   // do anything to clear it.
658   test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
659   test_layer->PushPropertiesTo(impl_layer.get());
660   EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
661                        impl_layer->update_rect());
662
663   // If we do clear the LayerImpl side, then the next update_rect() should be
664   // fresh without accumulation.
665   impl_layer->ResetAllChangeTrackingForSubtree();
666   test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
667   test_layer->PushPropertiesTo(impl_layer.get());
668   EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
669                        impl_layer->update_rect());
670 }
671
672 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
673   scoped_refptr<Layer> test_layer = Layer::Create();
674   scoped_ptr<LayerImpl> impl_layer =
675       LayerImpl::Create(host_impl_.active_tree(), 1);
676
677   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
678                                   layer_tree_host_->SetRootLayer(test_layer));
679
680   gfx::Transform transform;
681   transform.Rotate(45.0);
682   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
683
684   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
685
686   test_layer->PushPropertiesTo(impl_layer.get());
687
688   EXPECT_TRUE(impl_layer->LayerPropertyChanged());
689 }
690
691 TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
692   scoped_refptr<Layer> test_layer = Layer::Create();
693   scoped_ptr<LayerImpl> impl_layer =
694       LayerImpl::Create(host_impl_.active_tree(), 1);
695
696   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
697                                   layer_tree_host_->SetRootLayer(test_layer));
698
699   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
700
701   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
702
703   test_layer->PushPropertiesTo(impl_layer.get());
704
705   EXPECT_TRUE(impl_layer->LayerPropertyChanged());
706 }
707
708 TEST_F(LayerTest,
709        PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
710   scoped_refptr<Layer> test_layer = Layer::Create();
711   scoped_ptr<LayerImpl> impl_layer =
712       LayerImpl::Create(host_impl_.active_tree(), 1);
713
714   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
715                                   layer_tree_host_->SetRootLayer(test_layer));
716
717   scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
718   impl_layer->layer_animation_controller()->SetAnimationRegistrar(
719       registrar.get());
720
721   AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
722                                    1.0,
723                                    0,
724                                    100);
725
726   gfx::Transform transform;
727   transform.Rotate(45.0);
728   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
729
730   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
731   test_layer->PushPropertiesTo(impl_layer.get());
732   EXPECT_TRUE(impl_layer->LayerPropertyChanged());
733
734   impl_layer->ResetAllChangeTrackingForSubtree();
735   AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
736                                    1.0,
737                                    0,
738                                    100);
739   impl_layer->layer_animation_controller()->GetAnimation(Animation::Transform)->
740       set_is_impl_only(true);
741   transform.Rotate(45.0);
742   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
743
744   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
745   test_layer->PushPropertiesTo(impl_layer.get());
746   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
747 }
748
749 TEST_F(LayerTest,
750        PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
751   scoped_refptr<Layer> test_layer = Layer::Create();
752   scoped_ptr<LayerImpl> impl_layer =
753       LayerImpl::Create(host_impl_.active_tree(), 1);
754
755   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
756                                   layer_tree_host_->SetRootLayer(test_layer));
757
758   scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
759   impl_layer->layer_animation_controller()->SetAnimationRegistrar(
760       registrar.get());
761
762   AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
763                                    1.0,
764                                    0.3f,
765                                    0.7f,
766                                    false);
767
768   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
769
770   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
771   test_layer->PushPropertiesTo(impl_layer.get());
772   EXPECT_TRUE(impl_layer->LayerPropertyChanged());
773
774   impl_layer->ResetAllChangeTrackingForSubtree();
775   AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
776                                    1.0,
777                                    0.3f,
778                                    0.7f,
779                                    false);
780   impl_layer->layer_animation_controller()->GetAnimation(Animation::Opacity)->
781       set_is_impl_only(true);
782   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
783
784   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
785   test_layer->PushPropertiesTo(impl_layer.get());
786   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
787 }
788
789 TEST_F(LayerTest,
790        PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
791   scoped_refptr<Layer> test_layer = Layer::Create();
792   scoped_ptr<LayerImpl> impl_layer =
793       LayerImpl::Create(host_impl_.active_tree(), 1);
794
795   EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
796                                   layer_tree_host_->SetRootLayer(test_layer));
797
798   scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
799   impl_layer->layer_animation_controller()->SetAnimationRegistrar(
800       registrar.get());
801
802   AddAnimatedFilterToController(
803       impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
804
805   FilterOperations filters;
806   filters.Append(FilterOperation::CreateBlurFilter(2.f));
807   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
808
809   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
810   test_layer->PushPropertiesTo(impl_layer.get());
811   EXPECT_TRUE(impl_layer->LayerPropertyChanged());
812
813   impl_layer->ResetAllChangeTrackingForSubtree();
814   AddAnimatedFilterToController(
815       impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
816   impl_layer->layer_animation_controller()->GetAnimation(Animation::Filter)->
817       set_is_impl_only(true);
818   filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
819   EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
820
821   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
822   test_layer->PushPropertiesTo(impl_layer.get());
823   EXPECT_FALSE(impl_layer->LayerPropertyChanged());
824 }
825
826 TEST_F(LayerTest, MaskAndReplicaHasParent) {
827   scoped_refptr<Layer> parent = Layer::Create();
828   scoped_refptr<Layer> child = Layer::Create();
829   scoped_refptr<Layer> mask = Layer::Create();
830   scoped_refptr<Layer> replica = Layer::Create();
831   scoped_refptr<Layer> replica_mask = Layer::Create();
832   scoped_refptr<Layer> mask_replacement = Layer::Create();
833   scoped_refptr<Layer> replica_replacement = Layer::Create();
834   scoped_refptr<Layer> replica_mask_replacement = Layer::Create();
835
836   parent->AddChild(child);
837   child->SetMaskLayer(mask.get());
838   child->SetReplicaLayer(replica.get());
839   replica->SetMaskLayer(replica_mask.get());
840
841   EXPECT_EQ(parent.get(), child->parent());
842   EXPECT_EQ(child.get(), mask->parent());
843   EXPECT_EQ(child.get(), replica->parent());
844   EXPECT_EQ(replica.get(), replica_mask->parent());
845
846   replica->SetMaskLayer(replica_mask_replacement.get());
847   EXPECT_EQ(nullptr, replica_mask->parent());
848   EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
849
850   child->SetMaskLayer(mask_replacement.get());
851   EXPECT_EQ(nullptr, mask->parent());
852   EXPECT_EQ(child.get(), mask_replacement->parent());
853
854   child->SetReplicaLayer(replica_replacement.get());
855   EXPECT_EQ(nullptr, replica->parent());
856   EXPECT_EQ(child.get(), replica_replacement->parent());
857
858   EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
859 }
860
861 TEST_F(LayerTest, CheckTranformIsInvertible) {
862   scoped_refptr<Layer> layer = Layer::Create();
863   scoped_ptr<LayerImpl> impl_layer =
864       LayerImpl::Create(host_impl_.active_tree(), 1);
865   EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
866   EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
867   layer_tree_host_->SetRootLayer(layer);
868
869   EXPECT_TRUE(layer->transform_is_invertible());
870
871   gfx::Transform singular_transform;
872   singular_transform.Scale3d(
873       SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
874
875   layer->SetTransform(singular_transform);
876   layer->PushPropertiesTo(impl_layer.get());
877
878   EXPECT_FALSE(layer->transform_is_invertible());
879   EXPECT_FALSE(impl_layer->transform_is_invertible());
880
881   gfx::Transform rotation_transform;
882   rotation_transform.RotateAboutZAxis(-45.0);
883
884   layer->SetTransform(rotation_transform);
885   layer->PushPropertiesTo(impl_layer.get());
886   EXPECT_TRUE(layer->transform_is_invertible());
887   EXPECT_TRUE(impl_layer->transform_is_invertible());
888
889   Mock::VerifyAndClearExpectations(layer_tree_host_.get());
890 }
891
892 TEST_F(LayerTest, TranformIsInvertibleAnimation) {
893   scoped_refptr<Layer> layer = Layer::Create();
894   scoped_ptr<LayerImpl> impl_layer =
895       LayerImpl::Create(host_impl_.active_tree(), 1);
896   EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
897   EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
898   layer_tree_host_->SetRootLayer(layer);
899
900   EXPECT_TRUE(layer->transform_is_invertible());
901
902   gfx::Transform singular_transform;
903   singular_transform.Scale3d(
904       SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
905
906   layer->SetTransform(singular_transform);
907   layer->PushPropertiesTo(impl_layer.get());
908
909   EXPECT_FALSE(layer->transform_is_invertible());
910   EXPECT_FALSE(impl_layer->transform_is_invertible());
911
912   gfx::Transform identity_transform;
913
914   layer->SetTransform(identity_transform);
915   static_cast<LayerAnimationValueObserver*>(layer.get())
916       ->OnTransformAnimated(singular_transform);
917   layer->PushPropertiesTo(impl_layer.get());
918   EXPECT_FALSE(layer->transform_is_invertible());
919   EXPECT_FALSE(impl_layer->transform_is_invertible());
920
921   Mock::VerifyAndClearExpectations(layer_tree_host_.get());
922 }
923
924 class LayerTreeHostFactory {
925  public:
926   LayerTreeHostFactory()
927       : client_(FakeLayerTreeHostClient::DIRECT_3D),
928         shared_bitmap_manager_(new TestSharedBitmapManager),
929         gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager) {}
930
931   scoped_ptr<LayerTreeHost> Create() {
932     return LayerTreeHost::CreateSingleThreaded(
933                &client_,
934                &client_,
935                shared_bitmap_manager_.get(),
936                gpu_memory_buffer_manager_.get(),
937                LayerTreeSettings(),
938                base::MessageLoopProxy::current()).Pass();
939   }
940
941   scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
942     return LayerTreeHost::CreateSingleThreaded(
943                &client_,
944                &client_,
945                shared_bitmap_manager_.get(),
946                gpu_memory_buffer_manager_.get(),
947                settings,
948                base::MessageLoopProxy::current()).Pass();
949   }
950
951  private:
952   FakeLayerTreeHostClient client_;
953   scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
954   scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
955 };
956
957 void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
958   EXPECT_EQ(host, layer->layer_tree_host());
959
960   for (size_t i = 0; i < layer->children().size(); ++i)
961     AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
962
963   if (layer->mask_layer())
964     AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
965
966   if (layer->replica_layer())
967     AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
968 }
969
970 TEST(LayerLayerTreeHostTest, EnteringTree) {
971   scoped_refptr<Layer> parent = Layer::Create();
972   scoped_refptr<Layer> child = Layer::Create();
973   scoped_refptr<Layer> mask = Layer::Create();
974   scoped_refptr<Layer> replica = Layer::Create();
975   scoped_refptr<Layer> replica_mask = Layer::Create();
976
977   // Set up a detached tree of layers. The host pointer should be nil for these
978   // layers.
979   parent->AddChild(child);
980   child->SetMaskLayer(mask.get());
981   child->SetReplicaLayer(replica.get());
982   replica->SetMaskLayer(replica_mask.get());
983
984   AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
985
986   LayerTreeHostFactory factory;
987   scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
988   // Setting the root layer should set the host pointer for all layers in the
989   // tree.
990   layer_tree_host->SetRootLayer(parent.get());
991
992   AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
993
994   // Clearing the root layer should also clear out the host pointers for all
995   // layers in the tree.
996   layer_tree_host->SetRootLayer(nullptr);
997
998   AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
999 }
1000
1001 TEST(LayerLayerTreeHostTest, AddingLayerSubtree) {
1002   scoped_refptr<Layer> parent = Layer::Create();
1003   LayerTreeHostFactory factory;
1004   scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1005
1006   layer_tree_host->SetRootLayer(parent.get());
1007
1008   EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.get());
1009
1010   // Adding a subtree to a layer already associated with a host should set the
1011   // host pointer on all layers in that subtree.
1012   scoped_refptr<Layer> child = Layer::Create();
1013   scoped_refptr<Layer> grand_child = Layer::Create();
1014   child->AddChild(grand_child);
1015
1016   // Masks, replicas, and replica masks should pick up the new host too.
1017   scoped_refptr<Layer> child_mask = Layer::Create();
1018   child->SetMaskLayer(child_mask.get());
1019   scoped_refptr<Layer> child_replica = Layer::Create();
1020   child->SetReplicaLayer(child_replica.get());
1021   scoped_refptr<Layer> child_replica_mask = Layer::Create();
1022   child_replica->SetMaskLayer(child_replica_mask.get());
1023
1024   parent->AddChild(child);
1025   AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1026
1027   layer_tree_host->SetRootLayer(nullptr);
1028 }
1029
1030 TEST(LayerLayerTreeHostTest, ChangeHost) {
1031   scoped_refptr<Layer> parent = Layer::Create();
1032   scoped_refptr<Layer> child = Layer::Create();
1033   scoped_refptr<Layer> mask = Layer::Create();
1034   scoped_refptr<Layer> replica = Layer::Create();
1035   scoped_refptr<Layer> replica_mask = Layer::Create();
1036
1037   // Same setup as the previous test.
1038   parent->AddChild(child);
1039   child->SetMaskLayer(mask.get());
1040   child->SetReplicaLayer(replica.get());
1041   replica->SetMaskLayer(replica_mask.get());
1042
1043   LayerTreeHostFactory factory;
1044   scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1045   first_layer_tree_host->SetRootLayer(parent.get());
1046
1047   AssertLayerTreeHostMatchesForSubtree(parent.get(),
1048                                        first_layer_tree_host.get());
1049
1050   // Now re-root the tree to a new host (simulating what we do on a context lost
1051   // event). This should update the host pointers for all layers in the tree.
1052   scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1053   second_layer_tree_host->SetRootLayer(parent.get());
1054
1055   AssertLayerTreeHostMatchesForSubtree(parent.get(),
1056                                        second_layer_tree_host.get());
1057
1058   second_layer_tree_host->SetRootLayer(nullptr);
1059 }
1060
1061 TEST(LayerLayerTreeHostTest, ChangeHostInSubtree) {
1062   scoped_refptr<Layer> first_parent = Layer::Create();
1063   scoped_refptr<Layer> first_child = Layer::Create();
1064   scoped_refptr<Layer> second_parent = Layer::Create();
1065   scoped_refptr<Layer> second_child = Layer::Create();
1066   scoped_refptr<Layer> second_grand_child = Layer::Create();
1067
1068   // First put all children under the first parent and set the first host.
1069   first_parent->AddChild(first_child);
1070   second_child->AddChild(second_grand_child);
1071   first_parent->AddChild(second_child);
1072
1073   LayerTreeHostFactory factory;
1074   scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
1075   first_layer_tree_host->SetRootLayer(first_parent.get());
1076
1077   AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
1078                                        first_layer_tree_host.get());
1079
1080   // Now reparent the subtree starting at second_child to a layer in a different
1081   // tree.
1082   scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
1083   second_layer_tree_host->SetRootLayer(second_parent.get());
1084
1085   second_parent->AddChild(second_child);
1086
1087   // The moved layer and its children should point to the new host.
1088   EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
1089   EXPECT_EQ(second_layer_tree_host.get(),
1090             second_grand_child->layer_tree_host());
1091
1092   // Test over, cleanup time.
1093   first_layer_tree_host->SetRootLayer(nullptr);
1094   second_layer_tree_host->SetRootLayer(nullptr);
1095 }
1096
1097 TEST(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
1098   scoped_refptr<Layer> parent = Layer::Create();
1099   scoped_refptr<Layer> mask = Layer::Create();
1100   scoped_refptr<Layer> replica = Layer::Create();
1101   scoped_refptr<Layer> mask_child = Layer::Create();
1102   scoped_refptr<Layer> replica_child = Layer::Create();
1103   scoped_refptr<Layer> mask_replacement = Layer::Create();
1104   scoped_refptr<Layer> replica_replacement = Layer::Create();
1105
1106   parent->SetMaskLayer(mask.get());
1107   parent->SetReplicaLayer(replica.get());
1108   mask->AddChild(mask_child);
1109   replica->AddChild(replica_child);
1110
1111   LayerTreeHostFactory factory;
1112   scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1113   layer_tree_host->SetRootLayer(parent.get());
1114
1115   AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
1116
1117   // Replacing the mask should clear out the old mask's subtree's host pointers.
1118   parent->SetMaskLayer(mask_replacement.get());
1119   EXPECT_EQ(nullptr, mask->layer_tree_host());
1120   EXPECT_EQ(nullptr, mask_child->layer_tree_host());
1121
1122   // Same for replacing a replica layer.
1123   parent->SetReplicaLayer(replica_replacement.get());
1124   EXPECT_EQ(nullptr, replica->layer_tree_host());
1125   EXPECT_EQ(nullptr, replica_child->layer_tree_host());
1126
1127   // Test over, cleanup time.
1128   layer_tree_host->SetRootLayer(nullptr);
1129 }
1130
1131 TEST(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
1132   scoped_refptr<Layer> root = Layer::Create();
1133   scoped_refptr<Layer> child = Layer::Create();
1134   root->AddChild(child);
1135   LayerTreeHostFactory factory;
1136   scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1137   layer_tree_host->SetRootLayer(root);
1138 }
1139
1140 static bool AddTestAnimation(Layer* layer) {
1141   scoped_ptr<KeyframedFloatAnimationCurve> curve =
1142       KeyframedFloatAnimationCurve::Create();
1143   curve->AddKeyframe(FloatKeyframe::Create(0.0, 0.3f, nullptr));
1144   curve->AddKeyframe(FloatKeyframe::Create(1.0, 0.7f, nullptr));
1145   scoped_ptr<Animation> animation =
1146       Animation::Create(curve.Pass(), 0, 0, Animation::Opacity);
1147
1148   return layer->AddAnimation(animation.Pass());
1149 }
1150
1151 TEST(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
1152   scoped_refptr<Layer> layer = Layer::Create();
1153
1154   // Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
1155   // animation should not be accepted.
1156   EXPECT_FALSE(AddTestAnimation(layer.get()));
1157
1158   scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
1159   layer->layer_animation_controller()->SetAnimationRegistrar(registrar.get());
1160
1161   // Case 2: with an AnimationRegistrar, the animation should be accepted.
1162   EXPECT_TRUE(AddTestAnimation(layer.get()));
1163
1164   LayerTreeSettings settings;
1165   settings.accelerated_animation_enabled = false;
1166   LayerTreeHostFactory factory;
1167   scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
1168   layer_tree_host->SetRootLayer(layer);
1169   AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
1170
1171   // Case 3: with a LayerTreeHost where accelerated animation is disabled, the
1172   // animation should be rejected.
1173   EXPECT_FALSE(AddTestAnimation(layer.get()));
1174 }
1175
1176 TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
1177   LayerTreeHostFactory factory;
1178   scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
1179
1180   scoped_refptr<Layer> layer = Layer::Create();
1181   layer_tree_host->SetRootLayer(layer);
1182
1183   for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
1184     for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
1185       for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
1186         layer->SetContentsOpaque(!!contents_opaque);
1187         layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
1188                                                : SK_ColorTRANSPARENT);
1189         layer_tree_host->set_background_color(
1190             host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
1191
1192         SkColor safe_color = layer->SafeOpaqueBackgroundColor();
1193         if (contents_opaque) {
1194           EXPECT_EQ(SkColorGetA(safe_color), 255u)
1195               << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1196               << host_opaque << "\n";
1197         } else {
1198           EXPECT_NE(SkColorGetA(safe_color), 255u)
1199               << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
1200               << host_opaque << "\n";
1201         }
1202       }
1203     }
1204   }
1205 }
1206
1207 class DrawsContentChangeLayer : public Layer {
1208  public:
1209   static scoped_refptr<DrawsContentChangeLayer> Create() {
1210     return make_scoped_refptr(new DrawsContentChangeLayer());
1211   }
1212
1213   void SetLayerTreeHost(LayerTreeHost* host) override {
1214     Layer::SetLayerTreeHost(host);
1215     SetFakeDrawsContent(!fake_draws_content_);
1216   }
1217
1218   bool HasDrawableContent() const override {
1219     return fake_draws_content_ && Layer::HasDrawableContent();
1220   }
1221
1222   void SetFakeDrawsContent(bool fake_draws_content) {
1223     fake_draws_content_ = fake_draws_content;
1224     UpdateDrawsContent(HasDrawableContent());
1225   }
1226
1227  private:
1228   DrawsContentChangeLayer() : Layer(), fake_draws_content_(false) {}
1229   ~DrawsContentChangeLayer() override {}
1230
1231   bool fake_draws_content_;
1232 };
1233
1234 TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
1235   scoped_refptr<Layer> root_layer = Layer::Create();
1236   scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
1237       DrawsContentChangeLayer::Create();
1238   scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
1239       DrawsContentChangeLayer::Create();
1240   root_layer->SetIsDrawable(true);
1241   becomes_not_draws_content->SetIsDrawable(true);
1242   becomes_not_draws_content->SetFakeDrawsContent(true);
1243   EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1244   root_layer->AddChild(becomes_not_draws_content);
1245   EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
1246
1247   becomes_draws_content->SetIsDrawable(true);
1248   root_layer->AddChild(becomes_draws_content);
1249   EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
1250 }
1251
1252 }  // namespace
1253 }  // namespace cc