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