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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
|
// 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 "cc/layers/layer.h"
#include "base/thread_task_runner_handle.h"
#include "cc/animation/keyframed_animation_curve.h"
#include "cc/base/math_util.h"
#include "cc/layers/layer_impl.h"
#include "cc/output/copy_output_request.h"
#include "cc/output/copy_output_result.h"
#include "cc/resources/layer_painter.h"
#include "cc/test/animation_test_common.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_client.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/test/layer_test_common.h"
#include "cc/test/test_gpu_memory_buffer_manager.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/test/test_task_graph_runner.h"
#include "cc/trees/layer_tree_host.h"
#include "cc/trees/single_thread_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/transform.h"
using ::testing::AnyNumber;
using ::testing::AtLeast;
using ::testing::Mock;
using ::testing::StrictMock;
using ::testing::_;
#define EXPECT_SET_NEEDS_FULL_TREE_SYNC(expect, code_to_test) \
do { \
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((expect)); \
code_to_test; \
Mock::VerifyAndClearExpectations(layer_tree_host_.get()); \
} while (false)
namespace cc {
namespace {
class MockLayerTreeHost : public LayerTreeHost {
public:
MockLayerTreeHost(LayerTreeHostSingleThreadClient* single_thread_client,
LayerTreeHost::InitParams* params)
: LayerTreeHost(params) {
InitializeSingleThreaded(single_thread_client,
base::ThreadTaskRunnerHandle::Get(), nullptr);
}
MOCK_METHOD0(SetNeedsCommit, void());
MOCK_METHOD0(SetNeedsUpdateLayers, void());
MOCK_METHOD0(SetNeedsFullTreeSync, void());
};
class MockLayerPainter : public LayerPainter {
public:
void Paint(SkCanvas* canvas, const gfx::Rect& content_rect) override {}
};
class LayerTest : public testing::Test {
public:
LayerTest()
: host_impl_(&proxy_, &shared_bitmap_manager_, &task_graph_runner_),
fake_client_(FakeLayerTreeHostClient::DIRECT_3D) {}
protected:
void SetUp() override {
LayerTreeHost::InitParams params;
LayerTreeSettings settings;
params.client = &fake_client_;
params.settings = &settings;
layer_tree_host_.reset(
new StrictMock<MockLayerTreeHost>(&fake_client_, ¶ms));
}
void TearDown() override {
Mock::VerifyAndClearExpectations(layer_tree_host_.get());
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
parent_ = nullptr;
child1_ = nullptr;
child2_ = nullptr;
child3_ = nullptr;
grand_child1_ = nullptr;
grand_child2_ = nullptr;
grand_child3_ = nullptr;
layer_tree_host_->SetRootLayer(nullptr);
layer_tree_host_ = nullptr;
}
void VerifyTestTreeInitialState() const {
ASSERT_EQ(3U, 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(), child1_->parent());
EXPECT_EQ(parent_.get(), child2_->parent());
EXPECT_EQ(parent_.get(), child3_->parent());
ASSERT_EQ(2U, child1_->children().size());
EXPECT_EQ(grand_child1_, child1_->children()[0]);
EXPECT_EQ(grand_child2_, child1_->children()[1]);
EXPECT_EQ(child1_.get(), grand_child1_->parent());
EXPECT_EQ(child1_.get(), grand_child2_->parent());
ASSERT_EQ(1U, child2_->children().size());
EXPECT_EQ(grand_child3_, child2_->children()[0]);
EXPECT_EQ(child2_.get(), grand_child3_->parent());
ASSERT_EQ(0U, child3_->children().size());
}
void CreateSimpleTestTree() {
parent_ = Layer::Create(layer_settings_);
child1_ = Layer::Create(layer_settings_);
child2_ = Layer::Create(layer_settings_);
child3_ = Layer::Create(layer_settings_);
grand_child1_ = Layer::Create(layer_settings_);
grand_child2_ = Layer::Create(layer_settings_);
grand_child3_ = Layer::Create(layer_settings_);
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
layer_tree_host_->SetRootLayer(parent_);
parent_->AddChild(child1_);
parent_->AddChild(child2_);
parent_->AddChild(child3_);
child1_->AddChild(grand_child1_);
child1_->AddChild(grand_child2_);
child2_->AddChild(grand_child3_);
Mock::VerifyAndClearExpectations(layer_tree_host_.get());
VerifyTestTreeInitialState();
}
FakeImplProxy proxy_;
TestSharedBitmapManager shared_bitmap_manager_;
TestTaskGraphRunner task_graph_runner_;
FakeLayerTreeHostImpl host_impl_;
FakeLayerTreeHostClient fake_client_;
scoped_ptr<StrictMock<MockLayerTreeHost>> layer_tree_host_;
scoped_refptr<Layer> parent_;
scoped_refptr<Layer> child1_;
scoped_refptr<Layer> child2_;
scoped_refptr<Layer> child3_;
scoped_refptr<Layer> grand_child1_;
scoped_refptr<Layer> grand_child2_;
scoped_refptr<Layer> grand_child3_;
LayerSettings layer_settings_;
};
TEST_F(LayerTest, BasicCreateAndDestroy) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
ASSERT_TRUE(test_layer.get());
EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
test_layer->SetLayerTreeHost(layer_tree_host_.get());
Mock::VerifyAndClearExpectations(layer_tree_host_.get());
EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
test_layer->SetLayerTreeHost(nullptr);
}
TEST_F(LayerTest, AddAndRemoveChild) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
// Upon creation, layers should not have children or parent.
ASSERT_EQ(0U, parent->children().size());
EXPECT_FALSE(child->parent());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->AddChild(child));
ASSERT_EQ(1U, parent->children().size());
EXPECT_EQ(child.get(), parent->children()[0].get());
EXPECT_EQ(parent.get(), child->parent());
EXPECT_EQ(parent.get(), child->RootLayer());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), child->RemoveFromParent());
}
TEST_F(LayerTest, AddSameChildTwice) {
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AtLeast(1));
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
layer_tree_host_->SetRootLayer(parent);
ASSERT_EQ(0u, parent->children().size());
parent->AddChild(child);
ASSERT_EQ(1u, parent->children().size());
EXPECT_EQ(parent.get(), child->parent());
parent->AddChild(child);
ASSERT_EQ(1u, parent->children().size());
EXPECT_EQ(parent.get(), child->parent());
}
TEST_F(LayerTest, InsertChild) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child3 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
ASSERT_EQ(0U, parent->children().size());
// Case 1: inserting to empty list.
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child3, 0));
ASSERT_EQ(1U, parent->children().size());
EXPECT_EQ(child3, parent->children()[0]);
EXPECT_EQ(parent.get(), child3->parent());
// Case 2: inserting to beginning of list
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
ASSERT_EQ(2U, 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
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
ASSERT_EQ(3U, 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
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child4, 3));
ASSERT_EQ(4U, 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_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
}
TEST_F(LayerTest, InsertChildPastEndOfList) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
ASSERT_EQ(0U, parent->children().size());
// insert to an out-of-bounds index
parent->InsertChild(child1, 53);
ASSERT_EQ(1U, 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(2U, 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(layer_settings_);
scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
ASSERT_EQ(0U, parent->children().size());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
ASSERT_EQ(2U, 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.
EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(1), parent->InsertChild(child1, 1));
// child1 should now be at the end of the list.
ASSERT_EQ(2U, parent->children().size());
EXPECT_EQ(child2, parent->children()[0]);
EXPECT_EQ(child1, parent->children()[1]);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
}
TEST_F(LayerTest, ReplaceChildWithNewChild) {
CreateSimpleTestTree();
scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
EXPECT_FALSE(child4->parent());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(
AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
EXPECT_FALSE(parent_->NeedsDisplayForTesting());
EXPECT_FALSE(child1_->NeedsDisplayForTesting());
EXPECT_FALSE(child2_->NeedsDisplayForTesting());
EXPECT_FALSE(child3_->NeedsDisplayForTesting());
EXPECT_FALSE(child4->NeedsDisplayForTesting());
ASSERT_EQ(static_cast<size_t>(3), parent_->children().size());
EXPECT_EQ(child1_, parent_->children()[0]);
EXPECT_EQ(child4, parent_->children()[1]);
EXPECT_EQ(child3_, parent_->children()[2]);
EXPECT_EQ(parent_.get(), child4->parent());
EXPECT_FALSE(child2_->parent());
}
TEST_F(LayerTest, ReplaceChildWithNewChildThatHasOtherParent) {
CreateSimpleTestTree();
// create another simple tree with test_layer and child4.
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
test_layer->AddChild(child4);
ASSERT_EQ(1U, test_layer->children().size());
EXPECT_EQ(child4, test_layer->children()[0]);
EXPECT_EQ(test_layer.get(), child4->parent());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(
AtLeast(1), parent_->ReplaceChild(child2_.get(), child4));
ASSERT_EQ(3U, parent_->children().size());
EXPECT_EQ(child1_, parent_->children()[0]);
EXPECT_EQ(child4, parent_->children()[1]);
EXPECT_EQ(child3_, parent_->children()[2]);
EXPECT_EQ(parent_.get(), child4->parent());
// test_layer should no longer have child4,
// and child2 should no longer have a parent.
ASSERT_EQ(0U, test_layer->children().size());
EXPECT_FALSE(child2_->parent());
}
TEST_F(LayerTest, DeleteRemovedScrollParent) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
ASSERT_EQ(0U, parent->children().size());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
ASSERT_EQ(2U, parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child2, parent->children()[1]);
EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child2->RemoveFromParent());
child1->reset_needs_push_properties_for_testing();
EXPECT_SET_NEEDS_COMMIT(1, child2 = nullptr);
EXPECT_TRUE(child1->needs_push_properties());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
}
TEST_F(LayerTest, DeleteRemovedScrollChild) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(parent));
ASSERT_EQ(0U, parent->children().size());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child1, 0));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, parent->InsertChild(child2, 1));
ASSERT_EQ(2U, parent->children().size());
EXPECT_EQ(child1, parent->children()[0]);
EXPECT_EQ(child2, parent->children()[1]);
EXPECT_SET_NEEDS_COMMIT(2, child1->SetScrollParent(child2.get()));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, child1->RemoveFromParent());
child2->reset_needs_push_properties_for_testing();
EXPECT_SET_NEEDS_COMMIT(1, child1 = nullptr);
EXPECT_TRUE(child2->needs_push_properties());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
}
TEST_F(LayerTest, ReplaceChildWithSameChild) {
CreateSimpleTestTree();
// SetNeedsFullTreeSync / SetNeedsCommit should not be called because its the
// same child.
EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(0);
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(0);
parent_->ReplaceChild(child2_.get(), child2_);
VerifyTestTreeInitialState();
}
TEST_F(LayerTest, RemoveAllChildren) {
CreateSimpleTestTree();
EXPECT_SET_NEEDS_FULL_TREE_SYNC(AtLeast(3), parent_->RemoveAllChildren());
ASSERT_EQ(0U, parent_->children().size());
EXPECT_FALSE(child1_->parent());
EXPECT_FALSE(child2_->parent());
EXPECT_FALSE(child3_->parent());
}
TEST_F(LayerTest, SetChildren) {
scoped_refptr<Layer> old_parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> new_parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> child2 = Layer::Create(layer_settings_);
LayerList new_children;
new_children.push_back(child1);
new_children.push_back(child2);
// Set up and verify initial test conditions: child1 has a parent, child2 has
// no parent.
old_parent->AddChild(child1);
ASSERT_EQ(0U, new_parent->children().size());
EXPECT_EQ(old_parent.get(), child1->parent());
EXPECT_FALSE(child2->parent());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(
1, layer_tree_host_->SetRootLayer(new_parent));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(
AtLeast(1), new_parent->SetChildren(new_children));
ASSERT_EQ(2U, new_parent->children().size());
EXPECT_EQ(new_parent.get(), child1->parent());
EXPECT_EQ(new_parent.get(), child2->parent());
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, layer_tree_host_->SetRootLayer(nullptr));
}
TEST_F(LayerTest, HasAncestor) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
EXPECT_FALSE(parent->HasAncestor(parent.get()));
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
parent->AddChild(child);
EXPECT_FALSE(child->HasAncestor(child.get()));
EXPECT_TRUE(child->HasAncestor(parent.get()));
EXPECT_FALSE(parent->HasAncestor(child.get()));
scoped_refptr<Layer> child_child = Layer::Create(layer_settings_);
child->AddChild(child_child);
EXPECT_FALSE(child_child->HasAncestor(child_child.get()));
EXPECT_TRUE(child_child->HasAncestor(parent.get()));
EXPECT_TRUE(child_child->HasAncestor(child.get()));
EXPECT_FALSE(parent->HasAncestor(child.get()));
EXPECT_FALSE(parent->HasAncestor(child_child.get()));
}
TEST_F(LayerTest, GetRootLayerAfterTreeManipulations) {
CreateSimpleTestTree();
// For this test we don't care about SetNeedsFullTreeSync calls.
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(AnyNumber());
scoped_refptr<Layer> child4 = Layer::Create(layer_settings_);
EXPECT_EQ(parent_.get(), parent_->RootLayer());
EXPECT_EQ(parent_.get(), child1_->RootLayer());
EXPECT_EQ(parent_.get(), child2_->RootLayer());
EXPECT_EQ(parent_.get(), child3_->RootLayer());
EXPECT_EQ(child4.get(), child4->RootLayer());
EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
child1_->RemoveFromParent();
// |child1| and its children, grand_child1 and grand_child2 are now on a
// separate subtree.
EXPECT_EQ(parent_.get(), parent_->RootLayer());
EXPECT_EQ(child1_.get(), child1_->RootLayer());
EXPECT_EQ(parent_.get(), child2_->RootLayer());
EXPECT_EQ(parent_.get(), child3_->RootLayer());
EXPECT_EQ(child4.get(), child4->RootLayer());
EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
grand_child3_->AddChild(child4);
EXPECT_EQ(parent_.get(), parent_->RootLayer());
EXPECT_EQ(child1_.get(), child1_->RootLayer());
EXPECT_EQ(parent_.get(), child2_->RootLayer());
EXPECT_EQ(parent_.get(), child3_->RootLayer());
EXPECT_EQ(parent_.get(), child4->RootLayer());
EXPECT_EQ(child1_.get(), grand_child1_->RootLayer());
EXPECT_EQ(child1_.get(), grand_child2_->RootLayer());
EXPECT_EQ(parent_.get(), grand_child3_->RootLayer());
child2_->ReplaceChild(grand_child3_.get(), child1_);
// |grand_child3| gets orphaned and the child1 subtree gets planted back into
// the tree under child2.
EXPECT_EQ(parent_.get(), parent_->RootLayer());
EXPECT_EQ(parent_.get(), child1_->RootLayer());
EXPECT_EQ(parent_.get(), child2_->RootLayer());
EXPECT_EQ(parent_.get(), child3_->RootLayer());
EXPECT_EQ(grand_child3_.get(), child4->RootLayer());
EXPECT_EQ(parent_.get(), grand_child1_->RootLayer());
EXPECT_EQ(parent_.get(), grand_child2_->RootLayer());
EXPECT_EQ(grand_child3_.get(), grand_child3_->RootLayer());
}
TEST_F(LayerTest, CheckSetNeedsDisplayCausesCorrectBehavior) {
// The semantics for SetNeedsDisplay which are tested here:
// 1. sets NeedsDisplay flag appropriately.
// 2. indirectly calls SetNeedsUpdate, exactly once for each call to
// SetNeedsDisplay.
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(
1, layer_tree_host_->SetRootLayer(test_layer));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
gfx::Size test_bounds = gfx::Size(501, 508);
gfx::Rect dirty1 = gfx::Rect(10, 15, 1, 2);
gfx::Rect dirty2 = gfx::Rect(20, 25, 3, 4);
gfx::Rect out_of_bounds_dirty_rect = gfx::Rect(400, 405, 500, 502);
// Before anything, test_layer should not be dirty.
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
// This is just initialization, but SetNeedsCommit behavior is verified anyway
// to avoid warnings.
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBounds(test_bounds));
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
// The real test begins here.
test_layer->ResetNeedsDisplayForTesting();
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
// Case 1: Layer should accept dirty rects that go beyond its bounds.
test_layer->ResetNeedsDisplayForTesting();
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
EXPECT_SET_NEEDS_UPDATE(
1, test_layer->SetNeedsDisplayRect(out_of_bounds_dirty_rect));
EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
test_layer->ResetNeedsDisplayForTesting();
// Case 2: SetNeedsDisplay() without the dirty rect arg.
test_layer->ResetNeedsDisplayForTesting();
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
EXPECT_SET_NEEDS_UPDATE(1, test_layer->SetNeedsDisplay());
EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
test_layer->ResetNeedsDisplayForTesting();
// Case 3: SetNeedsDisplay() with an empty rect.
test_layer->ResetNeedsDisplayForTesting();
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
EXPECT_SET_NEEDS_COMMIT(0, test_layer->SetNeedsDisplayRect(gfx::Rect()));
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
// Case 4: SetNeedsDisplay() with a non-drawable layer
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(false));
test_layer->ResetNeedsDisplayForTesting();
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
EXPECT_SET_NEEDS_UPDATE(0, test_layer->SetNeedsDisplayRect(dirty1));
EXPECT_TRUE(test_layer->NeedsDisplayForTesting());
}
TEST_F(LayerTest, CheckPropertyChangeCausesCorrectBehavior) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(
1, layer_tree_host_->SetRootLayer(test_layer));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsDrawable(true));
scoped_refptr<Layer> dummy_layer1 = Layer::Create(layer_settings_);
scoped_refptr<Layer> dummy_layer2 = Layer::Create(layer_settings_);
// sanity check of initial test condition
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
// 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.
EXPECT_SET_NEEDS_COMMIT(
1, test_layer->SetTransformOrigin(gfx::Point3F(1.23f, 4.56f, 0.f)));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBackgroundColor(SK_ColorLTGRAY));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetMasksToBounds(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetBlendMode(SkXfermode::kHue_Mode));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetIsRootForIsolatedGroup(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetContentsOpaque(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetPosition(gfx::PointF(4.f, 9.f)));
// We can use any layer pointer here since we aren't syncing for real.
EXPECT_SET_NEEDS_COMMIT(1,
test_layer->SetScrollClipLayerId(test_layer->id()));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetUserScrollable(true, false));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetScrollOffset(
gfx::ScrollOffset(10, 10)));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetShouldScrollOnMainThread(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetNonFastScrollableRegion(
Region(gfx::Rect(1, 1, 2, 2))));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveWheelEventHandlers(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHaveScrollEventHandlers(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(
gfx::Transform(0.0, 0.0, 0.0, 0.0, 0.0, 0.0)));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetDoubleSided(false));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTouchEventHandlerRegion(
gfx::Rect(10, 10)));
EXPECT_SET_NEEDS_COMMIT(
1,
test_layer->SetDrawCheckerboardForMissingTiles(
!test_layer->draw_checkerboard_for_missing_tiles()));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetForceRenderSurface(true));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetHideLayerAndSubtree(true));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetMaskLayer(
dummy_layer1.get()));
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1, test_layer->SetReplicaLayer(
dummy_layer2.get()));
// The above tests should not have caused a change to the needs_display flag.
EXPECT_FALSE(test_layer->NeedsDisplayForTesting());
// As layers are removed from the tree, they will cause a tree sync.
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times((AnyNumber()));
}
TEST_F(LayerTest, PushPropertiesAccumulatesUpdateRect) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
layer_tree_host_->SetRootLayer(test_layer));
test_layer->SetNeedsDisplayRect(gfx::Rect(5, 5));
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 5.f, 5.f),
impl_layer->update_rect());
// The LayerImpl's update_rect() should be accumulated here, since we did not
// do anything to clear it.
test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_FLOAT_RECT_EQ(gfx::RectF(0.f, 0.f, 15.f, 15.f),
impl_layer->update_rect());
// If we do clear the LayerImpl side, then the next update_rect() should be
// fresh without accumulation.
impl_layer->ResetAllChangeTrackingForSubtree();
test_layer->SetNeedsDisplayRect(gfx::Rect(10, 10, 5, 5));
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_FLOAT_RECT_EQ(gfx::RectF(10.f, 10.f, 5.f, 5.f),
impl_layer->update_rect());
}
TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForTransform) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
layer_tree_host_->SetRootLayer(test_layer));
gfx::Transform transform;
transform.Rotate(45.0);
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_TRUE(impl_layer->LayerPropertyChanged());
}
TEST_F(LayerTest, PushPropertiesCausesLayerPropertyChangedForOpacity) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
layer_tree_host_->SetRootLayer(test_layer));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_TRUE(impl_layer->LayerPropertyChanged());
}
TEST_F(LayerTest,
PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyTransformAnim) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
layer_tree_host_->SetRootLayer(test_layer));
scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
impl_layer->layer_animation_controller()->SetAnimationRegistrar(
registrar.get());
AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
1.0,
0,
100);
gfx::Transform transform;
transform.Rotate(45.0);
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_TRUE(impl_layer->LayerPropertyChanged());
impl_layer->ResetAllChangeTrackingForSubtree();
AddAnimatedTransformToController(impl_layer->layer_animation_controller(),
1.0,
0,
100);
impl_layer->layer_animation_controller()
->GetAnimation(Animation::TRANSFORM)
->set_is_impl_only(true);
transform.Rotate(45.0);
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetTransform(transform));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
}
TEST_F(LayerTest,
PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyOpacityAnim) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
layer_tree_host_->SetRootLayer(test_layer));
scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
impl_layer->layer_animation_controller()->SetAnimationRegistrar(
registrar.get());
AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
1.0,
0.3f,
0.7f,
false);
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.5f));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_TRUE(impl_layer->LayerPropertyChanged());
impl_layer->ResetAllChangeTrackingForSubtree();
AddOpacityTransitionToController(impl_layer->layer_animation_controller(),
1.0,
0.3f,
0.7f,
false);
impl_layer->layer_animation_controller()
->GetAnimation(Animation::OPACITY)
->set_is_impl_only(true);
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetOpacity(0.75f));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
}
TEST_F(LayerTest,
PushPropsDoesntCauseLayerPropertyChangedDuringImplOnlyFilterAnim) {
scoped_refptr<Layer> test_layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_SET_NEEDS_FULL_TREE_SYNC(1,
layer_tree_host_->SetRootLayer(test_layer));
scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
impl_layer->layer_animation_controller()->SetAnimationRegistrar(
registrar.get());
AddAnimatedFilterToController(
impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
FilterOperations filters;
filters.Append(FilterOperation::CreateBlurFilter(2.f));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_TRUE(impl_layer->LayerPropertyChanged());
impl_layer->ResetAllChangeTrackingForSubtree();
AddAnimatedFilterToController(
impl_layer->layer_animation_controller(), 1.0, 1.f, 2.f);
impl_layer->layer_animation_controller()
->GetAnimation(Animation::FILTER)
->set_is_impl_only(true);
filters.Append(FilterOperation::CreateSepiaFilter(0.5f));
EXPECT_SET_NEEDS_COMMIT(1, test_layer->SetFilters(filters));
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
test_layer->PushPropertiesTo(impl_layer.get());
EXPECT_FALSE(impl_layer->LayerPropertyChanged());
}
TEST_F(LayerTest, MaskAndReplicaHasParent) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask_replacement = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_replacement = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_mask_replacement =
Layer::Create(layer_settings_);
parent->AddChild(child);
child->SetMaskLayer(mask.get());
child->SetReplicaLayer(replica.get());
replica->SetMaskLayer(replica_mask.get());
EXPECT_EQ(parent.get(), child->parent());
EXPECT_EQ(child.get(), mask->parent());
EXPECT_EQ(child.get(), replica->parent());
EXPECT_EQ(replica.get(), replica_mask->parent());
replica->SetMaskLayer(replica_mask_replacement.get());
EXPECT_EQ(nullptr, replica_mask->parent());
EXPECT_EQ(replica.get(), replica_mask_replacement->parent());
child->SetMaskLayer(mask_replacement.get());
EXPECT_EQ(nullptr, mask->parent());
EXPECT_EQ(child.get(), mask_replacement->parent());
child->SetReplicaLayer(replica_replacement.get());
EXPECT_EQ(nullptr, replica->parent());
EXPECT_EQ(child.get(), replica_replacement->parent());
EXPECT_EQ(replica.get(), replica->mask_layer()->parent());
}
TEST_F(LayerTest, CheckTranformIsInvertible) {
scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
layer_tree_host_->SetRootLayer(layer);
EXPECT_TRUE(layer->transform_is_invertible());
gfx::Transform singular_transform;
singular_transform.Scale3d(
SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
layer->SetTransform(singular_transform);
layer->PushPropertiesTo(impl_layer.get());
EXPECT_FALSE(layer->transform_is_invertible());
EXPECT_FALSE(impl_layer->transform_is_invertible());
gfx::Transform rotation_transform;
rotation_transform.RotateAboutZAxis(-45.0);
layer->SetTransform(rotation_transform);
layer->PushPropertiesTo(impl_layer.get());
EXPECT_TRUE(layer->transform_is_invertible());
EXPECT_TRUE(impl_layer->transform_is_invertible());
Mock::VerifyAndClearExpectations(layer_tree_host_.get());
}
TEST_F(LayerTest, TranformIsInvertibleAnimation) {
scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
scoped_ptr<LayerImpl> impl_layer =
LayerImpl::Create(host_impl_.active_tree(), 1);
EXPECT_CALL(*layer_tree_host_, SetNeedsFullTreeSync()).Times(1);
EXPECT_CALL(*layer_tree_host_, SetNeedsCommit()).Times(AnyNumber());
layer_tree_host_->SetRootLayer(layer);
EXPECT_TRUE(layer->transform_is_invertible());
gfx::Transform singular_transform;
singular_transform.Scale3d(
SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
layer->SetTransform(singular_transform);
layer->PushPropertiesTo(impl_layer.get());
EXPECT_FALSE(layer->transform_is_invertible());
EXPECT_FALSE(impl_layer->transform_is_invertible());
gfx::Transform identity_transform;
layer->SetTransform(identity_transform);
static_cast<LayerAnimationValueObserver*>(layer.get())
->OnTransformAnimated(singular_transform);
layer->PushPropertiesTo(impl_layer.get());
EXPECT_FALSE(layer->transform_is_invertible());
EXPECT_FALSE(impl_layer->transform_is_invertible());
Mock::VerifyAndClearExpectations(layer_tree_host_.get());
}
class LayerTreeHostFactory {
public:
LayerTreeHostFactory()
: client_(FakeLayerTreeHostClient::DIRECT_3D),
shared_bitmap_manager_(new TestSharedBitmapManager),
gpu_memory_buffer_manager_(new TestGpuMemoryBufferManager) {}
scoped_ptr<LayerTreeHost> Create() { return Create(LayerTreeSettings()); }
scoped_ptr<LayerTreeHost> Create(LayerTreeSettings settings) {
LayerTreeHost::InitParams params;
params.client = &client_;
params.shared_bitmap_manager = shared_bitmap_manager_.get();
params.gpu_memory_buffer_manager = gpu_memory_buffer_manager_.get();
params.settings = &settings;
params.main_task_runner = base::ThreadTaskRunnerHandle::Get();
return LayerTreeHost::CreateSingleThreaded(&client_, ¶ms);
}
private:
FakeLayerTreeHostClient client_;
scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_;
scoped_ptr<TestGpuMemoryBufferManager> gpu_memory_buffer_manager_;
};
void AssertLayerTreeHostMatchesForSubtree(Layer* layer, LayerTreeHost* host) {
EXPECT_EQ(host, layer->layer_tree_host());
for (size_t i = 0; i < layer->children().size(); ++i)
AssertLayerTreeHostMatchesForSubtree(layer->children()[i].get(), host);
if (layer->mask_layer())
AssertLayerTreeHostMatchesForSubtree(layer->mask_layer(), host);
if (layer->replica_layer())
AssertLayerTreeHostMatchesForSubtree(layer->replica_layer(), host);
}
class LayerLayerTreeHostTest : public testing::Test {
public:
protected:
LayerSettings layer_settings_;
};
TEST_F(LayerLayerTreeHostTest, EnteringTree) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
// 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(replica_mask.get());
AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
// Setting the root layer should set the host pointer for all layers in the
// tree.
layer_tree_host->SetRootLayer(parent.get());
AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
// Clearing the root layer should also clear out the host pointers for all
// layers in the tree.
layer_tree_host->SetRootLayer(nullptr);
AssertLayerTreeHostMatchesForSubtree(parent.get(), nullptr);
}
TEST_F(LayerLayerTreeHostTest, AddingLayerSubtree) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
layer_tree_host->SetRootLayer(parent.get());
EXPECT_EQ(parent->layer_tree_host(), layer_tree_host.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(layer_settings_);
scoped_refptr<Layer> grand_child = Layer::Create(layer_settings_);
child->AddChild(grand_child);
// Masks, replicas, and replica masks should pick up the new host too.
scoped_refptr<Layer> child_mask = Layer::Create(layer_settings_);
child->SetMaskLayer(child_mask.get());
scoped_refptr<Layer> child_replica = Layer::Create(layer_settings_);
child->SetReplicaLayer(child_replica.get());
scoped_refptr<Layer> child_replica_mask = Layer::Create(layer_settings_);
child_replica->SetMaskLayer(child_replica_mask.get());
parent->AddChild(child);
AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
layer_tree_host->SetRootLayer(nullptr);
}
TEST_F(LayerLayerTreeHostTest, ChangeHost) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_mask = Layer::Create(layer_settings_);
// Same setup as the previous test.
parent->AddChild(child);
child->SetMaskLayer(mask.get());
child->SetReplicaLayer(replica.get());
replica->SetMaskLayer(replica_mask.get());
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
first_layer_tree_host->SetRootLayer(parent.get());
AssertLayerTreeHostMatchesForSubtree(parent.get(),
first_layer_tree_host.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<LayerTreeHost> second_layer_tree_host = factory.Create();
second_layer_tree_host->SetRootLayer(parent.get());
AssertLayerTreeHostMatchesForSubtree(parent.get(),
second_layer_tree_host.get());
second_layer_tree_host->SetRootLayer(nullptr);
}
TEST_F(LayerLayerTreeHostTest, ChangeHostInSubtree) {
scoped_refptr<Layer> first_parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> first_child = Layer::Create(layer_settings_);
scoped_refptr<Layer> second_parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> second_child = Layer::Create(layer_settings_);
scoped_refptr<Layer> second_grand_child = Layer::Create(layer_settings_);
// First put all children under the first parent and set the first host.
first_parent->AddChild(first_child);
second_child->AddChild(second_grand_child);
first_parent->AddChild(second_child);
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> first_layer_tree_host = factory.Create();
first_layer_tree_host->SetRootLayer(first_parent.get());
AssertLayerTreeHostMatchesForSubtree(first_parent.get(),
first_layer_tree_host.get());
// Now reparent the subtree starting at second_child to a layer in a different
// tree.
scoped_ptr<LayerTreeHost> second_layer_tree_host = factory.Create();
second_layer_tree_host->SetRootLayer(second_parent.get());
second_parent->AddChild(second_child);
// The moved layer and its children should point to the new host.
EXPECT_EQ(second_layer_tree_host.get(), second_child->layer_tree_host());
EXPECT_EQ(second_layer_tree_host.get(),
second_grand_child->layer_tree_host());
// Test over, cleanup time.
first_layer_tree_host->SetRootLayer(nullptr);
second_layer_tree_host->SetRootLayer(nullptr);
}
TEST_F(LayerLayerTreeHostTest, ReplaceMaskAndReplicaLayer) {
scoped_refptr<Layer> parent = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask_child = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_child = Layer::Create(layer_settings_);
scoped_refptr<Layer> mask_replacement = Layer::Create(layer_settings_);
scoped_refptr<Layer> replica_replacement = Layer::Create(layer_settings_);
parent->SetMaskLayer(mask.get());
parent->SetReplicaLayer(replica.get());
mask->AddChild(mask_child);
replica->AddChild(replica_child);
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
layer_tree_host->SetRootLayer(parent.get());
AssertLayerTreeHostMatchesForSubtree(parent.get(), layer_tree_host.get());
// Replacing the mask should clear out the old mask's subtree's host pointers.
parent->SetMaskLayer(mask_replacement.get());
EXPECT_EQ(nullptr, mask->layer_tree_host());
EXPECT_EQ(nullptr, mask_child->layer_tree_host());
// Same for replacing a replica layer.
parent->SetReplicaLayer(replica_replacement.get());
EXPECT_EQ(nullptr, replica->layer_tree_host());
EXPECT_EQ(nullptr, replica_child->layer_tree_host());
// Test over, cleanup time.
layer_tree_host->SetRootLayer(nullptr);
}
TEST_F(LayerLayerTreeHostTest, DestroyHostWithNonNullRootLayer) {
scoped_refptr<Layer> root = Layer::Create(layer_settings_);
scoped_refptr<Layer> child = Layer::Create(layer_settings_);
root->AddChild(child);
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
layer_tree_host->SetRootLayer(root);
}
static bool AddTestAnimation(Layer* layer) {
scoped_ptr<KeyframedFloatAnimationCurve> curve =
KeyframedFloatAnimationCurve::Create();
curve->AddKeyframe(FloatKeyframe::Create(base::TimeDelta(), 0.3f, nullptr));
curve->AddKeyframe(
FloatKeyframe::Create(base::TimeDelta::FromSecondsD(1.0), 0.7f, nullptr));
scoped_ptr<Animation> animation =
Animation::Create(curve.Pass(), 0, 0, Animation::OPACITY);
return layer->AddAnimation(animation.Pass());
}
TEST_F(LayerLayerTreeHostTest, ShouldNotAddAnimationWithoutAnimationRegistrar) {
scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
// Case 1: without a LayerTreeHost and without an AnimationRegistrar, the
// animation should not be accepted.
EXPECT_FALSE(AddTestAnimation(layer.get()));
scoped_ptr<AnimationRegistrar> registrar = AnimationRegistrar::Create();
layer->RegisterForAnimations(registrar.get());
// Case 2: with an AnimationRegistrar, the animation should be accepted.
EXPECT_TRUE(AddTestAnimation(layer.get()));
LayerTreeSettings settings;
settings.accelerated_animation_enabled = false;
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create(settings);
layer_tree_host->SetRootLayer(layer);
AssertLayerTreeHostMatchesForSubtree(layer.get(), layer_tree_host.get());
// Case 3: with a LayerTreeHost where accelerated animation is disabled, the
// animation should be rejected.
EXPECT_FALSE(AddTestAnimation(layer.get()));
}
TEST_F(LayerTest, SafeOpaqueBackgroundColor) {
LayerTreeHostFactory factory;
scoped_ptr<LayerTreeHost> layer_tree_host = factory.Create();
scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
layer_tree_host->SetRootLayer(layer);
for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
layer->SetContentsOpaque(!!contents_opaque);
layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
: SK_ColorTRANSPARENT);
layer_tree_host->set_background_color(
host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
SkColor safe_color = layer->SafeOpaqueBackgroundColor();
if (contents_opaque) {
EXPECT_EQ(SkColorGetA(safe_color), 255u)
<< "Flags: " << contents_opaque << ", " << layer_opaque << ", "
<< host_opaque << "\n";
} else {
EXPECT_NE(SkColorGetA(safe_color), 255u)
<< "Flags: " << contents_opaque << ", " << layer_opaque << ", "
<< host_opaque << "\n";
}
}
}
}
}
class DrawsContentChangeLayer : public Layer {
public:
static scoped_refptr<DrawsContentChangeLayer> Create(
const LayerSettings& settings) {
return make_scoped_refptr(new DrawsContentChangeLayer(settings));
}
void SetLayerTreeHost(LayerTreeHost* host) override {
Layer::SetLayerTreeHost(host);
SetFakeDrawsContent(!fake_draws_content_);
}
bool HasDrawableContent() const override {
return fake_draws_content_ && Layer::HasDrawableContent();
}
void SetFakeDrawsContent(bool fake_draws_content) {
fake_draws_content_ = fake_draws_content;
UpdateDrawsContent(HasDrawableContent());
}
private:
explicit DrawsContentChangeLayer(const LayerSettings& settings)
: Layer(settings), fake_draws_content_(false) {}
~DrawsContentChangeLayer() override {}
bool fake_draws_content_;
};
TEST_F(LayerTest, DrawsContentChangedInSetLayerTreeHost) {
scoped_refptr<Layer> root_layer = Layer::Create(layer_settings_);
scoped_refptr<DrawsContentChangeLayer> becomes_not_draws_content =
DrawsContentChangeLayer::Create(layer_settings_);
scoped_refptr<DrawsContentChangeLayer> becomes_draws_content =
DrawsContentChangeLayer::Create(layer_settings_);
root_layer->SetIsDrawable(true);
becomes_not_draws_content->SetIsDrawable(true);
becomes_not_draws_content->SetFakeDrawsContent(true);
EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
root_layer->AddChild(becomes_not_draws_content);
EXPECT_EQ(0, root_layer->NumDescendantsThatDrawContent());
becomes_draws_content->SetIsDrawable(true);
root_layer->AddChild(becomes_draws_content);
EXPECT_EQ(1, root_layer->NumDescendantsThatDrawContent());
}
void ReceiveCopyOutputResult(int* result_count,
scoped_ptr<CopyOutputResult> result) {
++(*result_count);
}
TEST_F(LayerTest, DedupesCopyOutputRequestsBySource) {
scoped_refptr<Layer> layer = Layer::Create(layer_settings_);
int result_count = 0;
// Create identical requests without the source being set, and expect the
// layer does not abort either one.
scoped_ptr<CopyOutputRequest> request = CopyOutputRequest::CreateRequest(
base::Bind(&ReceiveCopyOutputResult, &result_count));
layer->RequestCopyOfOutput(request.Pass());
EXPECT_EQ(0, result_count);
request = CopyOutputRequest::CreateRequest(
base::Bind(&ReceiveCopyOutputResult, &result_count));
layer->RequestCopyOfOutput(request.Pass());
EXPECT_EQ(0, result_count);
// When the layer is destroyed, expect both requests to be aborted.
layer = nullptr;
EXPECT_EQ(2, result_count);
layer = Layer::Create(layer_settings_);
result_count = 0;
// Create identical requests, but this time the source is being set. Expect
// the first request from |this| source aborts immediately when the second
// request from |this| source is made.
int did_receive_first_result_from_this_source = 0;
request = CopyOutputRequest::CreateRequest(base::Bind(
&ReceiveCopyOutputResult, &did_receive_first_result_from_this_source));
request->set_source(this);
layer->RequestCopyOfOutput(request.Pass());
EXPECT_EQ(0, did_receive_first_result_from_this_source);
// Make a request from a different source.
int did_receive_result_from_different_source = 0;
request = CopyOutputRequest::CreateRequest(base::Bind(
&ReceiveCopyOutputResult, &did_receive_result_from_different_source));
request->set_source(reinterpret_cast<void*>(0xdeadbee0));
layer->RequestCopyOfOutput(request.Pass());
EXPECT_EQ(0, did_receive_result_from_different_source);
// Make a request without specifying the source.
int did_receive_result_from_anonymous_source = 0;
request = CopyOutputRequest::CreateRequest(base::Bind(
&ReceiveCopyOutputResult, &did_receive_result_from_anonymous_source));
layer->RequestCopyOfOutput(request.Pass());
EXPECT_EQ(0, did_receive_result_from_anonymous_source);
// Make the second request from |this| source.
int did_receive_second_result_from_this_source = 0;
request = CopyOutputRequest::CreateRequest(base::Bind(
&ReceiveCopyOutputResult, &did_receive_second_result_from_this_source));
request->set_source(this);
layer->RequestCopyOfOutput(request.Pass()); // First request to be aborted.
EXPECT_EQ(1, did_receive_first_result_from_this_source);
EXPECT_EQ(0, did_receive_result_from_different_source);
EXPECT_EQ(0, did_receive_result_from_anonymous_source);
EXPECT_EQ(0, did_receive_second_result_from_this_source);
// When the layer is destroyed, the other three requests should be aborted.
layer = nullptr;
EXPECT_EQ(1, did_receive_first_result_from_this_source);
EXPECT_EQ(1, did_receive_result_from_different_source);
EXPECT_EQ(1, did_receive_result_from_anonymous_source);
EXPECT_EQ(1, did_receive_second_result_from_this_source);
}
} // namespace
} // namespace cc
|