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