1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
|
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "cc/layer.h"
#include "cc/keyframed_animation_curve.h"
#include "cc/layer_impl.h"
#include "cc/layer_painter.h"
#include "cc/layer_tree_host.h"
#include "cc/settings.h"
#include "cc/single_thread_proxy.h"
#include "cc/test/fake_layer_tree_host_client.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/test/test_common.h"
#include "cc/test/web_compositor_initializer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include <public/WebTransformationMatrix.h>
using namespace cc;
using namespace WebKitTests;
using WebKit::WebTransformationMatrix;
using ::testing::Mock;
using ::testing::_;
using ::testing::AtLeast;
using ::testing::AnyNumber;
#define EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(numTimesExpectedSetNeedsCommit, codeToTest) do { \
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times((numTimesExpectedSetNeedsCommit)); \
codeToTest; \
Mock::VerifyAndClearExpectations(m_layerTreeHost.get()); \
} while (0)
namespace {
class MockLayerImplTreeHost : public LayerTreeHost {
public:
MockLayerImplTreeHost()
: LayerTreeHost(&m_fakeClient, LayerTreeSettings())
{
initialize();
}
MOCK_METHOD0(setNeedsCommit, void());
private:
FakeLayerImplTreeHostClient m_fakeClient;
};
class MockLayerPainter : public LayerPainter {
public:
virtual void paint(SkCanvas*, const IntRect&, FloatRect&) OVERRIDE { }
};
class LayerTest : public testing::Test {
public:
LayerTest()
: m_compositorInitializer(0)
{
}
protected:
virtual void SetUp()
{
m_layerTreeHost = scoped_ptr<MockLayerImplTreeHost>(new MockLayerImplTreeHost);
}
virtual void TearDown()
{
Mock::VerifyAndClearExpectations(m_layerTreeHost.get());
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AnyNumber());
m_parent = NULL;
m_child1 = NULL;
m_child2 = NULL;
m_child3 = NULL;
m_grandChild1 = NULL;
m_grandChild2 = NULL;
m_grandChild3 = NULL;
m_layerTreeHost->setRootLayer(0);
m_layerTreeHost.reset();
}
void verifyTestTreeInitialState() const
{
ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size());
EXPECT_EQ(m_child1, m_parent->children()[0]);
EXPECT_EQ(m_child2, m_parent->children()[1]);
EXPECT_EQ(m_child3, m_parent->children()[2]);
EXPECT_EQ(m_parent.get(), m_child1->parent());
EXPECT_EQ(m_parent.get(), m_child2->parent());
EXPECT_EQ(m_parent.get(), m_child3->parent());
ASSERT_EQ(static_cast<size_t>(2), m_child1->children().size());
EXPECT_EQ(m_grandChild1, m_child1->children()[0]);
EXPECT_EQ(m_grandChild2, m_child1->children()[1]);
EXPECT_EQ(m_child1.get(), m_grandChild1->parent());
EXPECT_EQ(m_child1.get(), m_grandChild2->parent());
ASSERT_EQ(static_cast<size_t>(1), m_child2->children().size());
EXPECT_EQ(m_grandChild3, m_child2->children()[0]);
EXPECT_EQ(m_child2.get(), m_grandChild3->parent());
ASSERT_EQ(static_cast<size_t>(0), m_child3->children().size());
}
void createSimpleTestTree()
{
m_parent = Layer::create();
m_child1 = Layer::create();
m_child2 = Layer::create();
m_child3 = Layer::create();
m_grandChild1 = Layer::create();
m_grandChild2 = Layer::create();
m_grandChild3 = Layer::create();
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AnyNumber());
m_layerTreeHost->setRootLayer(m_parent);
m_parent->addChild(m_child1);
m_parent->addChild(m_child2);
m_parent->addChild(m_child3);
m_child1->addChild(m_grandChild1);
m_child1->addChild(m_grandChild2);
m_child2->addChild(m_grandChild3);
Mock::VerifyAndClearExpectations(m_layerTreeHost.get());
verifyTestTreeInitialState();
}
scoped_ptr<MockLayerImplTreeHost> m_layerTreeHost;
scoped_refptr<Layer> m_parent, m_child1, m_child2, m_child3, m_grandChild1, m_grandChild2, m_grandChild3;
WebCompositorInitializer m_compositorInitializer;
};
TEST_F(LayerTest, basicCreateAndDestroy)
{
scoped_refptr<Layer> testLayer = Layer::create();
ASSERT_TRUE(testLayer);
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(0);
testLayer->setLayerTreeHost(m_layerTreeHost.get());
}
TEST_F(LayerTest, addAndRemoveChild)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
// Upon creation, layers should not have children or parent.
ASSERT_EQ(static_cast<size_t>(0), parent->children().size());
EXPECT_FALSE(child->parent());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, m_layerTreeHost->setRootLayer(parent));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->addChild(child));
ASSERT_EQ(static_cast<size_t>(1), parent->children().size());
EXPECT_EQ(child.get(), parent->children()[0]);
EXPECT_EQ(parent.get(), child->parent());
EXPECT_EQ(parent.get(), child->rootLayer());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), child->removeFromParent());
}
TEST_F(LayerTest, insertChild)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child1 = Layer::create();
scoped_refptr<Layer> child2 = Layer::create();
scoped_refptr<Layer> child3 = Layer::create();
scoped_refptr<Layer> child4 = Layer::create();
parent->setLayerTreeHost(m_layerTreeHost.get());
ASSERT_EQ(static_cast<size_t>(0), parent->children().size());
// Case 1: inserting to empty list.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child3, 0));
ASSERT_EQ(static_cast<size_t>(1), parent->children().size());
EXPECT_EQ(child3, parent->children()[0]);
EXPECT_EQ(parent.get(), child3->parent());
// Case 2: inserting to beginning of list
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child1, 0));
ASSERT_EQ(static_cast<size_t>(2), parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child3, parent->children()[1]);
EXPECT_EQ(parent.get(), child1->parent());
// Case 3: inserting to middle of list
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child2, 1));
ASSERT_EQ(static_cast<size_t>(3), parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child2, parent->children()[1]);
EXPECT_EQ(child3, parent->children()[2]);
EXPECT_EQ(parent.get(), child2->parent());
// Case 4: inserting to end of list
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child4, 3));
ASSERT_EQ(static_cast<size_t>(4), parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child2, parent->children()[1]);
EXPECT_EQ(child3, parent->children()[2]);
EXPECT_EQ(child4, parent->children()[3]);
EXPECT_EQ(parent.get(), child4->parent());
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1));
}
TEST_F(LayerTest, insertChildPastEndOfList)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child1 = Layer::create();
scoped_refptr<Layer> child2 = Layer::create();
ASSERT_EQ(static_cast<size_t>(0), parent->children().size());
// insert to an out-of-bounds index
parent->insertChild(child1, 53);
ASSERT_EQ(static_cast<size_t>(1), parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
// insert another child to out-of-bounds, when list is not already empty.
parent->insertChild(child2, 2459);
ASSERT_EQ(static_cast<size_t>(2), parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child2, parent->children()[1]);
}
TEST_F(LayerTest, insertSameChildTwice)
{
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child1 = Layer::create();
scoped_refptr<Layer> child2 = Layer::create();
parent->setLayerTreeHost(m_layerTreeHost.get());
ASSERT_EQ(static_cast<size_t>(0), parent->children().size());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child1, 0));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, parent->insertChild(child2, 1));
ASSERT_EQ(static_cast<size_t>(2), parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child2, parent->children()[1]);
// Inserting the same child again should cause the child to be removed and re-inserted at the new location.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), parent->insertChild(child1, 1));
// child1 should now be at the end of the list.
ASSERT_EQ(static_cast<size_t>(2), parent->children().size());
EXPECT_EQ(child2, parent->children()[0]);
EXPECT_EQ(child1, parent->children()[1]);
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1));
}
TEST_F(LayerTest, replaceChildWithNewChild)
{
createSimpleTestTree();
scoped_refptr<Layer> child4 = Layer::create();
EXPECT_FALSE(child4->parent());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), m_parent->replaceChild(m_child2.get(), child4));
ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size());
EXPECT_EQ(m_child1, m_parent->children()[0]);
EXPECT_EQ(child4, m_parent->children()[1]);
EXPECT_EQ(m_child3, m_parent->children()[2]);
EXPECT_EQ(m_parent.get(), child4->parent());
EXPECT_FALSE(m_child2->parent());
}
TEST_F(LayerTest, replaceChildWithNewChildThatHasOtherParent)
{
createSimpleTestTree();
// create another simple tree with testLayer and child4.
scoped_refptr<Layer> testLayer = Layer::create();
scoped_refptr<Layer> child4 = Layer::create();
testLayer->addChild(child4);
ASSERT_EQ(static_cast<size_t>(1), testLayer->children().size());
EXPECT_EQ(child4, testLayer->children()[0]);
EXPECT_EQ(testLayer.get(), child4->parent());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), m_parent->replaceChild(m_child2.get(), child4));
ASSERT_EQ(static_cast<size_t>(3), m_parent->children().size());
EXPECT_EQ(m_child1, m_parent->children()[0]);
EXPECT_EQ(child4, m_parent->children()[1]);
EXPECT_EQ(m_child3, m_parent->children()[2]);
EXPECT_EQ(m_parent.get(), child4->parent());
// testLayer should no longer have child4,
// and child2 should no longer have a parent.
ASSERT_EQ(static_cast<size_t>(0), testLayer->children().size());
EXPECT_FALSE(m_child2->parent());
}
TEST_F(LayerTest, replaceChildWithSameChild)
{
createSimpleTestTree();
// setNeedsCommit should not be called because its the same child
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, m_parent->replaceChild(m_child2.get(), m_child2));
verifyTestTreeInitialState();
}
TEST_F(LayerTest, removeAllChildren)
{
createSimpleTestTree();
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(3), m_parent->removeAllChildren());
ASSERT_EQ(static_cast<size_t>(0), m_parent->children().size());
EXPECT_FALSE(m_child1->parent());
EXPECT_FALSE(m_child2->parent());
EXPECT_FALSE(m_child3->parent());
}
TEST_F(LayerTest, setChildren)
{
scoped_refptr<Layer> oldParent = Layer::create();
scoped_refptr<Layer> newParent = Layer::create();
scoped_refptr<Layer> child1 = Layer::create();
scoped_refptr<Layer> child2 = Layer::create();
std::vector<scoped_refptr<Layer> > newChildren;
newChildren.push_back(child1);
newChildren.push_back(child2);
// Set up and verify initial test conditions: child1 has a parent, child2 has no parent.
oldParent->addChild(child1);
ASSERT_EQ(static_cast<size_t>(0), newParent->children().size());
EXPECT_EQ(oldParent.get(), child1->parent());
EXPECT_FALSE(child2->parent());
newParent->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(AtLeast(1), newParent->setChildren(newChildren));
ASSERT_EQ(static_cast<size_t>(2), newParent->children().size());
EXPECT_EQ(newParent.get(), child1->parent());
EXPECT_EQ(newParent.get(), child2->parent());
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1));
}
TEST_F(LayerTest, getRootLayerAfterTreeManipulations)
{
createSimpleTestTree();
// For this test we don't care about setNeedsCommit calls.
EXPECT_CALL(*m_layerTreeHost, setNeedsCommit()).Times(AtLeast(1));
scoped_refptr<Layer> child4 = Layer::create();
EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
EXPECT_EQ(m_parent.get(), m_child1->rootLayer());
EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
EXPECT_EQ(child4.get(), child4->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild1->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild2->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer());
m_child1->removeFromParent();
// child1 and its children, grandChild1 and grandChild2 are now on a separate subtree.
EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
EXPECT_EQ(m_child1.get(), m_child1->rootLayer());
EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
EXPECT_EQ(child4.get(), child4->rootLayer());
EXPECT_EQ(m_child1.get(), m_grandChild1->rootLayer());
EXPECT_EQ(m_child1.get(), m_grandChild2->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer());
m_grandChild3->addChild(child4);
EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
EXPECT_EQ(m_child1.get(), m_child1->rootLayer());
EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
EXPECT_EQ(m_parent.get(), child4->rootLayer());
EXPECT_EQ(m_child1.get(), m_grandChild1->rootLayer());
EXPECT_EQ(m_child1.get(), m_grandChild2->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild3->rootLayer());
m_child2->replaceChild(m_grandChild3.get(), m_child1);
// grandChild3 gets orphaned and the child1 subtree gets planted back into the tree under child2.
EXPECT_EQ(m_parent.get(), m_parent->rootLayer());
EXPECT_EQ(m_parent.get(), m_child1->rootLayer());
EXPECT_EQ(m_parent.get(), m_child2->rootLayer());
EXPECT_EQ(m_parent.get(), m_child3->rootLayer());
EXPECT_EQ(m_grandChild3.get(), child4->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild1->rootLayer());
EXPECT_EQ(m_parent.get(), m_grandChild2->rootLayer());
EXPECT_EQ(m_grandChild3.get(), m_grandChild3->rootLayer());
}
TEST_F(LayerTest, checkSetNeedsDisplayCausesCorrectBehavior)
{
// The semantics for setNeedsDisplay which are tested here:
// 1. sets needsDisplay flag appropriately.
// 2. indirectly calls setNeedsCommit, exactly once for each call to setNeedsDisplay.
scoped_refptr<Layer> testLayer = Layer::create();
testLayer->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
IntSize testBounds = IntSize(501, 508);
FloatRect dirty1 = FloatRect(10, 15, 1, 2);
FloatRect dirty2 = FloatRect(20, 25, 3, 4);
FloatRect emptyDirtyRect = FloatRect(40, 45, 0, 0);
FloatRect outOfBoundsDirtyRect = FloatRect(400, 405, 500, 502);
// Before anything, testLayer should not be dirty.
EXPECT_FALSE(testLayer->needsDisplay());
// This is just initialization, but setNeedsCommit behavior is verified anyway to avoid warnings.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds));
testLayer = Layer::create();
testLayer->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
EXPECT_FALSE(testLayer->needsDisplay());
// The real test begins here.
// Case 1: needsDisplay flag should not change because of an empty dirty rect.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(emptyDirtyRect));
EXPECT_FALSE(testLayer->needsDisplay());
// Case 2: basic.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(dirty1));
EXPECT_TRUE(testLayer->needsDisplay());
// Case 3: a second dirty rect.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(dirty2));
EXPECT_TRUE(testLayer->needsDisplay());
// Case 4: Layer should accept dirty rects that go beyond its bounds.
testLayer = Layer::create();
testLayer->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplayRect(outOfBoundsDirtyRect));
EXPECT_TRUE(testLayer->needsDisplay());
// Case 5: setNeedsDisplay() without the dirty rect arg.
testLayer = Layer::create();
testLayer->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNeedsDisplay());
EXPECT_TRUE(testLayer->needsDisplay());
// Case 6: setNeedsDisplay() with a non-drawable layer
testLayer = Layer::create();
testLayer->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setBounds(testBounds));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setNeedsDisplayRect(dirty1));
EXPECT_TRUE(testLayer->needsDisplay());
}
TEST_F(LayerTest, checkPropertyChangeCausesCorrectBehavior)
{
scoped_refptr<Layer> testLayer = Layer::create();
testLayer->setLayerTreeHost(m_layerTreeHost.get());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setIsDrawable(true));
scoped_refptr<Layer> dummyLayer = Layer::create(); // just a dummy layer for this test case.
// sanity check of initial test condition
EXPECT_FALSE(testLayer->needsDisplay());
// Test properties that should not call needsDisplay and needsCommit when changed.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setVisibleContentRect(IntRect(0, 0, 40, 50)));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setUseLCDText(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawOpacity(0.5));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setRenderTarget(0));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawTransform(WebTransformationMatrix()));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setScreenSpaceTransform(WebTransformationMatrix()));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(0, testLayer->setDrawableContentRect(IntRect(4, 5, 6, 7)));
EXPECT_FALSE(testLayer->needsDisplay());
// Next, test properties that should call setNeedsCommit (but not setNeedsDisplay)
// All properties need to be set to new values in order for setNeedsCommit to be called.
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setAnchorPoint(FloatPoint(1.23f, 4.56f)));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setAnchorPointZ(0.7f));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBackgroundColor(SK_ColorLTGRAY));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setMasksToBounds(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setMaskLayer(dummyLayer.get()));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setOpacity(0.5));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setContentsOpaque(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setPosition(FloatPoint(4, 9)));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setReplicaLayer(dummyLayer.get()));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setSublayerTransform(WebTransformationMatrix(0, 0, 0, 0, 0, 0)));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setScrollable(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setShouldScrollOnMainThread(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setNonFastScrollableRegion(Region(IntRect(1, 1, 2, 2))));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setHaveWheelEventHandlers(true));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setScrollPosition(IntPoint(10, 10)));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setTransform(WebTransformationMatrix(0, 0, 0, 0, 0, 0)));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setDoubleSided(false));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setDebugName("Test Layer"));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setDrawCheckerboardForMissingTiles(!testLayer->drawCheckerboardForMissingTiles()));
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setForceRenderSurface(true));
// The above tests should not have caused a change to the needsDisplay flag.
EXPECT_FALSE(testLayer->needsDisplay());
// Test properties that should call setNeedsDisplay and setNeedsCommit
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(IntSize(5, 10)));
EXPECT_TRUE(testLayer->needsDisplay());
}
TEST_F(LayerTest, verifyPushPropertiesAccumulatesUpdateRect)
{
DebugScopedSetImplThread setImplThread;
scoped_refptr<Layer> testLayer = Layer::create();
scoped_ptr<LayerImpl> implLayer = LayerImpl::create(1);
testLayer->setNeedsDisplayRect(FloatRect(FloatPoint::zero(), FloatSize(5, 5)));
testLayer->pushPropertiesTo(implLayer.get());
EXPECT_FLOAT_RECT_EQ(FloatRect(FloatPoint::zero(), FloatSize(5, 5)), implLayer->updateRect());
// The LayerImpl's updateRect should be accumulated here, since we did not do anything to clear it.
testLayer->setNeedsDisplayRect(FloatRect(FloatPoint(10, 10), FloatSize(5, 5)));
testLayer->pushPropertiesTo(implLayer.get());
EXPECT_FLOAT_RECT_EQ(FloatRect(FloatPoint::zero(), FloatSize(15, 15)), implLayer->updateRect());
// If we do clear the LayerImpl side, then the next updateRect should be fresh without accumulation.
implLayer->resetAllChangeTrackingForSubtree();
testLayer->setNeedsDisplayRect(FloatRect(FloatPoint(10, 10), FloatSize(5, 5)));
testLayer->pushPropertiesTo(implLayer.get());
EXPECT_FLOAT_RECT_EQ(FloatRect(FloatPoint(10, 10), FloatSize(5, 5)), implLayer->updateRect());
}
class LayerWithContentScaling : public Layer {
public:
explicit LayerWithContentScaling()
: Layer()
{
}
virtual bool needsContentsScale() const OVERRIDE
{
return true;
}
virtual void setNeedsDisplayRect(const FloatRect& dirtyRect) OVERRIDE
{
m_lastNeedsDisplayRect = dirtyRect;
Layer::setNeedsDisplayRect(dirtyRect);
}
void resetNeedsDisplay()
{
m_needsDisplay = false;
}
const FloatRect& lastNeedsDisplayRect() const { return m_lastNeedsDisplayRect; }
private:
virtual ~LayerWithContentScaling()
{
}
FloatRect m_lastNeedsDisplayRect;
};
TEST_F(LayerTest, checkContentsScaleChangeTriggersNeedsDisplay)
{
scoped_refptr<LayerWithContentScaling> testLayer = make_scoped_refptr(new LayerWithContentScaling());
testLayer->setIsDrawable(true);
testLayer->setLayerTreeHost(m_layerTreeHost.get());
IntSize testBounds = IntSize(320, 240);
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setBounds(testBounds));
testLayer->resetNeedsDisplay();
EXPECT_FALSE(testLayer->needsDisplay());
EXECUTE_AND_VERIFY_SET_NEEDS_COMMIT_BEHAVIOR(1, testLayer->setContentsScale(testLayer->contentsScale() + 1.f));
EXPECT_TRUE(testLayer->needsDisplay());
EXPECT_FLOAT_RECT_EQ(FloatRect(0, 0, 320, 240), testLayer->lastNeedsDisplayRect());
}
class FakeLayerImplTreeHost : public LayerTreeHost {
public:
static scoped_ptr<FakeLayerImplTreeHost> create()
{
scoped_ptr<FakeLayerImplTreeHost> host(new FakeLayerImplTreeHost);
// The initialize call will fail, since our client doesn't provide a valid GraphicsContext3D, but it doesn't matter in the tests that use this fake so ignore the return value.
host->initialize();
return host.Pass();
}
private:
FakeLayerImplTreeHost()
: LayerTreeHost(&m_client, LayerTreeSettings())
{
}
FakeLayerImplTreeHostClient m_client;
};
void assertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host)
{
EXPECT_EQ(host, layer->layerTreeHost());
for (size_t i = 0; i < layer->children().size(); ++i)
assertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
if (layer->maskLayer())
assertLayerTreeHostMatchesForSubtree(layer->maskLayer(), host);
if (layer->replicaLayer())
assertLayerTreeHostMatchesForSubtree(layer->replicaLayer(), host);
}
TEST(LayerLayerTreeHostTest, enteringTree)
{
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> mask = Layer::create();
scoped_refptr<Layer> replica = Layer::create();
scoped_refptr<Layer> replicaMask = Layer::create();
// Set up a detached tree of layers. The host pointer should be nil for these layers.
parent->addChild(child);
child->setMaskLayer(mask.get());
child->setReplicaLayer(replica.get());
replica->setMaskLayer(mask.get());
assertLayerTreeHostMatchesForSubtree(parent.get(), 0);
scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
// Setting the root layer should set the host pointer for all layers in the tree.
layerTreeHost->setRootLayer(parent.get());
assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
// Clearing the root layer should also clear out the host pointers for all layers in the tree.
layerTreeHost->setRootLayer(0);
assertLayerTreeHostMatchesForSubtree(parent.get(), 0);
}
TEST(LayerLayerTreeHostTest, addingLayerSubtree)
{
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> parent = Layer::create();
scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
layerTreeHost->setRootLayer(parent.get());
EXPECT_EQ(parent->layerTreeHost(), layerTreeHost.get());
// Adding a subtree to a layer already associated with a host should set the host pointer on all layers in that subtree.
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> grandChild = Layer::create();
child->addChild(grandChild);
// Masks, replicas, and replica masks should pick up the new host too.
scoped_refptr<Layer> childMask = Layer::create();
child->setMaskLayer(childMask.get());
scoped_refptr<Layer> childReplica = Layer::create();
child->setReplicaLayer(childReplica.get());
scoped_refptr<Layer> childReplicaMask = Layer::create();
childReplica->setMaskLayer(childReplicaMask.get());
parent->addChild(child);
assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
layerTreeHost->setRootLayer(0);
}
TEST(LayerLayerTreeHostTest, changeHost)
{
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> child = Layer::create();
scoped_refptr<Layer> mask = Layer::create();
scoped_refptr<Layer> replica = Layer::create();
scoped_refptr<Layer> replicaMask = Layer::create();
// Same setup as the previous test.
parent->addChild(child);
child->setMaskLayer(mask.get());
child->setReplicaLayer(replica.get());
replica->setMaskLayer(mask.get());
scoped_ptr<FakeLayerImplTreeHost> firstLayerTreeHost(FakeLayerImplTreeHost::create());
firstLayerTreeHost->setRootLayer(parent.get());
assertLayerTreeHostMatchesForSubtree(parent.get(), firstLayerTreeHost.get());
// Now re-root the tree to a new host (simulating what we do on a context lost event).
// This should update the host pointers for all layers in the tree.
scoped_ptr<FakeLayerImplTreeHost> secondLayerTreeHost(FakeLayerImplTreeHost::create());
secondLayerTreeHost->setRootLayer(parent.get());
assertLayerTreeHostMatchesForSubtree(parent.get(), secondLayerTreeHost.get());
secondLayerTreeHost->setRootLayer(0);
}
TEST(LayerLayerTreeHostTest, changeHostInSubtree)
{
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> firstParent = Layer::create();
scoped_refptr<Layer> firstChild = Layer::create();
scoped_refptr<Layer> secondParent = Layer::create();
scoped_refptr<Layer> secondChild = Layer::create();
scoped_refptr<Layer> secondGrandChild = Layer::create();
// First put all children under the first parent and set the first host.
firstParent->addChild(firstChild);
secondChild->addChild(secondGrandChild);
firstParent->addChild(secondChild);
scoped_ptr<FakeLayerImplTreeHost> firstLayerTreeHost(FakeLayerImplTreeHost::create());
firstLayerTreeHost->setRootLayer(firstParent.get());
assertLayerTreeHostMatchesForSubtree(firstParent.get(), firstLayerTreeHost.get());
// Now reparent the subtree starting at secondChild to a layer in a different tree.
scoped_ptr<FakeLayerImplTreeHost> secondLayerTreeHost(FakeLayerImplTreeHost::create());
secondLayerTreeHost->setRootLayer(secondParent.get());
secondParent->addChild(secondChild);
// The moved layer and its children should point to the new host.
EXPECT_EQ(secondLayerTreeHost.get(), secondChild->layerTreeHost());
EXPECT_EQ(secondLayerTreeHost.get(), secondGrandChild->layerTreeHost());
// Test over, cleanup time.
firstLayerTreeHost->setRootLayer(0);
secondLayerTreeHost->setRootLayer(0);
}
TEST(LayerLayerTreeHostTest, replaceMaskAndReplicaLayer)
{
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> parent = Layer::create();
scoped_refptr<Layer> mask = Layer::create();
scoped_refptr<Layer> replica = Layer::create();
scoped_refptr<Layer> maskChild = Layer::create();
scoped_refptr<Layer> replicaChild = Layer::create();
scoped_refptr<Layer> maskReplacement = Layer::create();
scoped_refptr<Layer> replicaReplacement = Layer::create();
parent->setMaskLayer(mask.get());
parent->setReplicaLayer(replica.get());
mask->addChild(maskChild);
replica->addChild(replicaChild);
scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
layerTreeHost->setRootLayer(parent.get());
assertLayerTreeHostMatchesForSubtree(parent.get(), layerTreeHost.get());
// Replacing the mask should clear out the old mask's subtree's host pointers.
parent->setMaskLayer(maskReplacement.get());
EXPECT_EQ(0, mask->layerTreeHost());
EXPECT_EQ(0, maskChild->layerTreeHost());
// Same for replacing a replica layer.
parent->setReplicaLayer(replicaReplacement.get());
EXPECT_EQ(0, replica->layerTreeHost());
EXPECT_EQ(0, replicaChild->layerTreeHost());
// Test over, cleanup time.
layerTreeHost->setRootLayer(0);
}
TEST(LayerLayerTreeHostTest, destroyHostWithNonNullRootLayer)
{
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> root = Layer::create();
scoped_refptr<Layer> child = Layer::create();
root->addChild(child);
scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
layerTreeHost->setRootLayer(root);
}
static bool addTestAnimation(Layer* layer)
{
scoped_ptr<KeyframedFloatAnimationCurve> curve(KeyframedFloatAnimationCurve::create());
curve->addKeyframe(FloatKeyframe::create(0, 0.3f, scoped_ptr<TimingFunction>()));
curve->addKeyframe(FloatKeyframe::create(1, 0.7f, scoped_ptr<TimingFunction>()));
scoped_ptr<ActiveAnimation> animation(ActiveAnimation::create(curve.PassAs<AnimationCurve>(), 0, 0, ActiveAnimation::Opacity));
return layer->addAnimation(animation.Pass());
}
TEST(LayerLayerTreeHostTest, shouldNotAddAnimationWithoutLayerTreeHost)
{
// Currently, WebCore assumes that animations will be started immediately / very soon
// if a composited layer's addAnimation() returns true. However, without a layerTreeHost,
// layers cannot actually animate yet. So, to prevent violating this WebCore assumption,
// the animation should not be accepted if the layer doesn't already have a layerTreeHost.
ScopedSettings scopedSettings;
Settings::setAcceleratedAnimationEnabled(true);
WebCompositorInitializer compositorInitializer(0);
scoped_refptr<Layer> layer = Layer::create();
// Case 1: without a layerTreeHost, the animation should not be accepted.
EXPECT_FALSE(addTestAnimation(layer.get()));
scoped_ptr<FakeLayerImplTreeHost> layerTreeHost(FakeLayerImplTreeHost::create());
layerTreeHost->setRootLayer(layer.get());
layer->setLayerTreeHost(layerTreeHost.get());
assertLayerTreeHostMatchesForSubtree(layer.get(), layerTreeHost.get());
// Case 2: with a layerTreeHost, the animation should be accepted.
EXPECT_TRUE(addTestAnimation(layer.get()));
}
class MockLayer : public Layer {
public:
bool needsDisplay() const { return m_needsDisplay; }
private:
virtual ~MockLayer()
{
}
};
TEST(LayerTestWithoutFixture, setBoundsTriggersSetNeedsRedrawAfterGettingNonEmptyBounds)
{
scoped_refptr<MockLayer> layer(new MockLayer);
EXPECT_FALSE(layer->needsDisplay());
layer->setBounds(IntSize(0, 10));
EXPECT_FALSE(layer->needsDisplay());
layer->setBounds(IntSize(10, 10));
EXPECT_TRUE(layer->needsDisplay());
}
} // namespace
|