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