File: cull_rect_test.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1152 lines) | stat: -rw-r--r-- 49,494 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/graphics/paint/cull_rect.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/testing/paint_property_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/paint_test_configurations.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"
#include "ui/gfx/geometry/rect_f.h"

namespace blink {

class CullRectTest : public testing::Test, private CullRectTestConfig {
 protected:
  bool ApplyPaintProperties(
      CullRect& cull_rect,
      const PropertyTreeState& root,
      const PropertyTreeState& source,
      const PropertyTreeState& destination,
      const std::optional<CullRect>& old_cull_rect = std::nullopt) {
    return cull_rect.ApplyPaintProperties(root, source, destination,
                                          old_cull_rect, expansion_ratio_);
  }

  std::pair<bool, bool> ApplyScrollTranslation(
      CullRect& cull_rect,
      const TransformPaintPropertyNode& t) {
    return cull_rect.ApplyScrollTranslation(t, t, expansion_ratio_);
  }

  bool ChangedEnough(const gfx::Rect& old_rect,
                     const gfx::Rect& new_rect,
                     const std::optional<gfx::Rect>& bounds = std::nullopt,
                     const std::pair<bool, bool>& expanded = {true, true}) {
    return CullRect(new_rect).ChangedEnough(expanded, CullRect(old_rect),
                                            bounds, t0(), 1.f);
  }

  float expansion_ratio_ = 1.f;
};

TEST_F(CullRectTest, IntersectsRect) {
  CullRect cull_rect(gfx::Rect(0, 0, 50, 50));

  EXPECT_TRUE(cull_rect.Intersects(gfx::Rect(0, 0, 1, 1)));
  EXPECT_FALSE(cull_rect.Intersects(gfx::Rect(51, 51, 1, 1)));
  EXPECT_FALSE(cull_rect.Intersects(gfx::Rect(1, 1, 1, 0)));

  EXPECT_TRUE(CullRect::Infinite().Intersects(gfx::Rect(0, 0, 1, 1)));
  EXPECT_FALSE(CullRect::Infinite().Intersects(gfx::Rect(1, 1, 1, 0)));
  EXPECT_FALSE(CullRect(gfx::Rect()).Intersects(gfx::Rect()));
}

TEST_F(CullRectTest, IntersectsTransformed) {
  CullRect cull_rect(gfx::Rect(0, 0, 50, 50));
  AffineTransform transform;
  transform.Translate(-2, -2);

  EXPECT_TRUE(
      cull_rect.IntersectsTransformed(transform, gfx::RectF(51, 51, 1, 1)));
  EXPECT_FALSE(cull_rect.Intersects(gfx::Rect(52, 52, 1, 1)));

  EXPECT_TRUE(CullRect::Infinite().IntersectsTransformed(
      transform, gfx::RectF(51, 51, 1, 1)));
  EXPECT_FALSE(CullRect::Infinite().IntersectsTransformed(
      transform, gfx::RectF(1, 1, 1, 0)));
}

TEST_F(CullRectTest, Infinite) {
  EXPECT_TRUE(CullRect::Infinite().IsInfinite());
  EXPECT_TRUE(CullRect(InfiniteIntRect()).IsInfinite());
  EXPECT_FALSE(CullRect(gfx::Rect(0, 0, 100, 100)).IsInfinite());
}

TEST_F(CullRectTest, Move) {
  CullRect cull_rect(gfx::Rect(0, 0, 50, 50));
  cull_rect.Move(gfx::Vector2d());
  EXPECT_EQ(gfx::Rect(0, 0, 50, 50), cull_rect.Rect());
  cull_rect.Move(gfx::Vector2d(10, 20));
  EXPECT_EQ(gfx::Rect(10, 20, 50, 50), cull_rect.Rect());
}

TEST_F(CullRectTest, MoveInfinite) {
  CullRect cull_rect = CullRect::Infinite();
  cull_rect.Move(gfx::Vector2d());
  EXPECT_TRUE(cull_rect.IsInfinite());
  cull_rect.Move(gfx::Vector2d(10, 20));
  EXPECT_TRUE(cull_rect.IsInfinite());
}

TEST_F(CullRectTest, ApplyTransform) {
  CullRect cull_rect(gfx::Rect(1, 1, 50, 50));
  auto* transform = CreateTransform(t0(), MakeTranslationMatrix(1, 1));
  cull_rect.ApplyTransform(*transform);

  EXPECT_EQ(gfx::Rect(0, 0, 50, 50), cull_rect.Rect());
}

TEST_F(CullRectTest, ApplyTransformInfinite) {
  CullRect cull_rect = CullRect::Infinite();
  auto* transform = CreateTransform(t0(), MakeTranslationMatrix(1, 1));
  cull_rect.ApplyTransform(*transform);
  EXPECT_TRUE(cull_rect.IsInfinite());
}

TEST_F(CullRectTest,
       ApplyScrollTranslationSmallContainerPartialScrollingContents1) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), 0, -5000, gfx::Rect(20, 10, 40, 50),
      gfx::Size(40, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // Use container rect: 20,10 40x50
    // Scrolled: 20,5010 40x50
    // Expanded(0,1024): 20,3986 40x2098
    // Clipped by contents_rect: 20,3986 40x2098
    EXPECT_EQ(gfx::Rect(20, 3986, 40, 2098), cull_rect.Rect());
  } else {
    // Clipped by container rect: 20,10 30x50
    // Scrolled: 20,5010 30x50
    // Expanded(0,1024): 20,3986 30x2098
    // Clipped by contents_rect: 20,3986 30x2098
    EXPECT_EQ(gfx::Rect(20, 3986, 30, 2098), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  if (!RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // This result differs from the above result in width (30 vs 40)
    // because it's not clipped by the infinite input cull rect.
  }
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 3986, 40, 2098), cull_rect.Rect());

  // This cull rect is fully contained by the container rect.
  cull_rect = CullRect(gfx::Rect(30, 10, 20, 30));
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // No expansion in the non-scrollable direction.
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(20, 3986, 40, 2098), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(30, 3986, 20, 2078), cull_rect.Rect());
  }
}

TEST_F(CullRectTest,
       ApplyScrollTranslationSmallContainerPartialScrollingContents2) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -3000, -5000, gfx::Rect(20, 10, 40, 50),
      gfx::Size(8000, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  // Similar to ApplyScrollTranslationSmallContainerPartialScrollingContents1,
  // but expands cull rect along both axes.
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(1996, 3986, 2088, 2098), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(1996, 3986, 2078, 2098), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(1996, 3986, 2088, 2098), cull_rect.Rect());
}

TEST_F(CullRectTest, ApplyScrollTranslationPartialScrollingContents1) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), 0, -5000, gfx::Rect(20, 10, 300, 400),
      gfx::Size(300, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // Use container rect: 20,10 300x400
    // Scrolled: 20,5010 300x400
    // Expanded(0,4000): 20,2010 300x8400
    // Clipped by contents_rect: 20,2010 300x7000
    EXPECT_EQ(gfx::Rect(20, 1010, 300, 7000), cull_rect.Rect());
  } else {
    // Clipped by container rect: 20,10 280x390
    // Scrolled: 20,5010 280x390
    // Expanded(0,4000): 20,2010 280x8390
    // Clipped by contents_rect: 20,2010 280x7000
    EXPECT_EQ(gfx::Rect(20, 1010, 280, 7000), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();

  if (!RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // This result differs from the above result in width (280 vs 300)
    // because it's not clipped by the infinite input cull rect.
  }
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 1010, 300, 7000), cull_rect.Rect());

  // This cull rect is fully contained by the container rect.
  cull_rect = CullRect(gfx::Rect(30, 10, 100, 200));
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // No expansion in the non-scrollable direction.
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(20, 1010, 300, 7000), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(30, 1010, 100, 7000), cull_rect.Rect());
  }
}

TEST_F(CullRectTest, ApplyScrollTranslationPartialScrollingContents2) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -3000, -5000, gfx::Rect(20, 10, 300, 400),
      gfx::Size(8000, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  // Similar to ApplyScrollTranslationPartialScrollingContents1, but expands
  // cull rect along both axes.
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(1020, 3010, 4300, 4400), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(1020, 3010, 4280, 4390), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(1020, 3010, 4300, 4400), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyScrollTranslationPartialScrollingContentsExpansionRatio) {
  expansion_ratio_ = 3;
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -9000, -15000, gfx::Rect(20, 10, 300, 400),
      gfx::Size(24000, 24000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  // Similar to ApplyScrollTranslationPartialScrollingContents1, but expands
  // cull rect along both axes, and the scroller is treated as small scroller.
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(5948, 11938, 6444, 6544), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(5948, 11938, 6424, 6534), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(5948, 11938, 6444, 6544), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyNonCompositedScrollTranslationPartialScrollingContents1) {
  auto state = CreateScrollTranslationState(PropertyTreeState::Root(), 0, -5000,
                                            gfx::Rect(20, 10, 300, 400),
                                            gfx::Size(300, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  // Same as ApplyScrollTranslationPartialScrollingContents1.
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(20, 1010, 300, 7000), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(20, 1010, 280, 7000), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(false, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 1010, 300, 7000), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyNonCompositedScrollTranslationPartialScrollingContents2) {
  auto state = CreateScrollTranslationState(PropertyTreeState::Root(), -3000,
                                            -5000, gfx::Rect(20, 10, 300, 400),
                                            gfx::Size(8000, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  // Same as ApplyScrollTranslationPartialScrollingContents2.
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(1020, 3010, 4300, 4400), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(1020, 3010, 4280, 4390), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(1020, 3010, 4300, 4400), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyScrollTranslationPartialScrollingContentsWithoutExpansion) {
  expansion_ratio_ = 0;
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -3000, -5000, gfx::Rect(20, 10, 40, 50),
      gfx::Size(8000, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // Clipped by container rect: 20,10 30x50
  // Scrolled: 3020,5010 30x50
  EXPECT_EQ(gfx::Rect(3020, 5010, 30, 50), cull_rect.Rect());

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // This result differs from the above result in width (40 vs 30)
  // because it's not clipped by the infinite input cull rect.
  EXPECT_EQ(gfx::Rect(3020, 5010, 40, 50), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyScrollTranslationPartialScrollingContentsWithTinyExpansionRatio) {
  // Results should be the same as no expansion.
  expansion_ratio_ = 0.00001;
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -3000, -5000, gfx::Rect(20, 10, 40, 50),
      gfx::Size(8000, 8000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(3020, 5010, 40, 50), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(3020, 5010, 30, 50), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(3020, 5010, 40, 50), cull_rect.Rect());
}

TEST_F(CullRectTest, ApplyScrollTranslationNoIntersectionWithContainerRect) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(200, 100, 300, 400),
      gfx::Size(2000, 2000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_TRUE(cull_rect.Rect().IsEmpty());
}

TEST_F(CullRectTest,
       ApplyNonCompositedScrollTranslationNoIntersectionWithContainerRect) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(200, 100, 300, 400),
      gfx::Size(2000, 2000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_TRUE(cull_rect.Rect().IsEmpty());
}

TEST_F(CullRectTest, ApplyScrollTranslationWholeScrollingContents) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(20, 10, 300, 400),
      gfx::Size(2000, 2000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));

  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(20, 10, 2000, 2000), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(20, 10, 1990, 2000), cull_rect.Rect());
  }

  // The cull rect used full expansion, and the expanded rect covers
  // the whole scrolling contents.
  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 10, 2000, 2000), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyNonCompositedScrollTranslationWholeScrollingContents) {
  auto state = CreateScrollTranslationState(PropertyTreeState::Root(), -10, -15,
                                            gfx::Rect(20, 10, 300, 400),
                                            gfx::Size(2000, 2000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 300, 400));
  // Same as ApplyScrollTranslationWholeScrollingContents.
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(20, 10, 2000, 2000), cull_rect.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(20, 10, 1990, 2000), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 10, 2000, 2000), cull_rect.Rect());
}

TEST_F(CullRectTest,
       ApplyScrollTranslationWholeScrollingContentsWithoutExpansion) {
  expansion_ratio_ = 0;
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(20, 10, 40, 50),
      gfx::Size(2000, 2000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));

  // Clipped: (20, 10, 30, 50)
  // Inverse transformed: (30, 25, 30, 50)
  EXPECT_EQ(gfx::Rect(30, 25, 30, 50), cull_rect.Rect());

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(false, false),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // This result differs from the above result in height (40 vs 30)
  // because it's not clipped by the infinite input cull rect.
  EXPECT_EQ(gfx::Rect(30, 25, 40, 50), cull_rect.Rect());
}

TEST_F(CullRectTest, ChangedEnoughEmpty) {
  EXPECT_FALSE(ChangedEnough(gfx::Rect(), gfx::Rect()));
  EXPECT_FALSE(ChangedEnough(gfx::Rect(1, 1, 0, 0), gfx::Rect(2, 2, 0, 0)));
  EXPECT_TRUE(ChangedEnough(gfx::Rect(), gfx::Rect(0, 0, 1, 1)));
  EXPECT_FALSE(ChangedEnough(gfx::Rect(0, 0, 1, 1), gfx::Rect()));
}

TEST_F(CullRectTest, ChangedNotEnough) {
  gfx::Rect old_rect(100, 100, 100, 100);
  EXPECT_FALSE(ChangedEnough(old_rect, old_rect));
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(100, 100, 90, 90)));
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(100, 100, 100, 100)));
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(1, 1, 200, 200)));
}

TEST_F(CullRectTest, ChangedEnoughOnMovement) {
  gfx::Rect old_rect(100, 100, 100, 100);
  gfx::Rect new_rect(old_rect);
  new_rect.Offset(500, 0);
  EXPECT_FALSE(ChangedEnough(old_rect, new_rect));
  new_rect.Offset(0, 500);
  EXPECT_FALSE(ChangedEnough(old_rect, new_rect));
  new_rect.Offset(50, 0);
  EXPECT_TRUE(ChangedEnough(old_rect, new_rect));
  new_rect.Offset(-50, 50);
  EXPECT_TRUE(ChangedEnough(old_rect, new_rect));
}

TEST_F(CullRectTest, ChangedEnoughNewRectTouchingEdge) {
  gfx::Rect bounds(0, 0, 500, 500);
  gfx::Rect old_rect(100, 100, 100, 100);
  // Top edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(100, 50, 100, 200), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(100, 0, 100, 200), bounds));
  // Left edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(50, 100, 200, 100), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(0, 100, 200, 100), bounds));
  // Bottom edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(100, 100, 100, 350), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(100, 100, 100, 400), bounds));
  // Right edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(100, 100, 350, 100), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(100, 100, 400, 100), bounds));

  // With offset.
  bounds.Offset(-100, 100);
  old_rect.Offset(-100, 100);
  // Top edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(0, 150, 100, 200), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(0, 100, 100, 200), bounds));
  // Left edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(-50, 200, 200, 100), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(-100, 200, 200, 100), bounds));
  // Bottom edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(0, 200, 100, 350), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(0, 200, 100, 400), bounds));
  // Right edge.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(0, 200, 350, 100), bounds));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(0, 200, 400, 100), bounds));
}

TEST_F(CullRectTest, ChangedEnoughOldRectTouchingEdge) {
  gfx::Rect bounds(0, 0, 500, 500);
  gfx::Rect new_rect(100, 100, 300, 300);
  // Top edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(100, 0, 100, 100), new_rect, bounds));
  // Left edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(0, 100, 100, 100), new_rect, bounds));
  // Bottom edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(300, 400, 100, 100), new_rect, bounds));
  // Right edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(400, 300, 100, 100), new_rect, bounds));

  // With offset.
  bounds.Offset(-100, 100);
  new_rect.Offset(-100, 100);
  // Top edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(0, 100, 100, 100), new_rect, bounds));
  // Left edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(-100, 0, 100, 100), new_rect, bounds));
  // Bottom edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(200, 500, 100, 100), new_rect, bounds));
  // Right edge.
  EXPECT_FALSE(ChangedEnough(gfx::Rect(300, 400, 100, 100), new_rect, bounds));
}

TEST_F(CullRectTest, ChangedEnoughNotExpanded) {
  gfx::Rect old_rect(100, 100, 300, 300);
  // X is not expanded and unchanged, y isn't changed enough.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(100, 0, 300, 300),
                             std::nullopt, {false, true}));
  // X is not expanded and changed, y unchanged.
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(0, 100, 300, 300), std::nullopt,
                            {false, true}));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(100, 100, 200, 300),
                            std::nullopt, {false, true}));

  // X isn't changed enough, y is not expanded and unchanged.
  EXPECT_FALSE(ChangedEnough(old_rect, gfx::Rect(0, 100, 300, 300),
                             std::nullopt, {true, false}));
  // X unchanged, Y is not expanded and changed.
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(100, 0, 300, 300), std::nullopt,
                            {true, false}));
  EXPECT_TRUE(ChangedEnough(old_rect, gfx::Rect(100, 100, 300, 200),
                            std::nullopt, {true, false}));
}

TEST_F(CullRectTest, ApplyPaintPropertiesWithoutClipScroll) {
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(1, 2));
  auto* t2 = CreateTransform(*t1, MakeTranslationMatrix(10, 20));
  PropertyTreeState root = PropertyTreeState::Root();
  PropertyTreeState state1(*t1, c0(), e0());
  PropertyTreeState state2(*t2, c0(), e0());

  CullRect cull_rect1(gfx::Rect(1, 1, 50, 50));
  EXPECT_FALSE(ApplyPaintProperties(cull_rect1, root, state1, state2));
  EXPECT_EQ(gfx::Rect(-9, -19, 50, 50), cull_rect1.Rect());

  CullRect cull_rect2(gfx::Rect(1, 1, 50, 50));
  EXPECT_FALSE(ApplyPaintProperties(cull_rect2, root, root, state2));
  EXPECT_EQ(gfx::Rect(-10, -21, 50, 50), cull_rect2.Rect());

  CullRect old_cull_rect = cull_rect2;
  old_cull_rect.Move(gfx::Vector2d(1, 1));
  CullRect cull_rect3(gfx::Rect(1, 1, 50, 50));
  // Should ignore old_cull_rect.
  EXPECT_FALSE(ApplyPaintProperties(cull_rect3, root, root, state2));
  EXPECT_EQ(cull_rect2, cull_rect3);

  CullRect infinite = CullRect::Infinite();
  EXPECT_FALSE(ApplyPaintProperties(infinite, root, root, state2));
  EXPECT_TRUE(infinite.IsInfinite());
}

TEST_F(CullRectTest, SingleScrollWholeCompsitedScrollingContents) {
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(1, 2));
  PropertyTreeState state1(*t1, c0(), e0());
  auto scroll_translation_state = CreateCompositedScrollTranslationState(
      state1, -10, -15, gfx::Rect(20, 10, 300, 400), gfx::Size(2000, 2000));

  // Same as ApplyScrollTranslationWholeScrollingContents.
  CullRect cull_rect1(gfx::Rect(0, 0, 300, 400));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect1, state1, state1,
                                   scroll_translation_state));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    EXPECT_EQ(gfx::Rect(20, 10, 2000, 2000), cull_rect1.Rect());
  } else {
    EXPECT_EQ(gfx::Rect(20, 10, 1990, 2000), cull_rect1.Rect());
  }

  CullRect old_cull_rect = cull_rect1;
  old_cull_rect.Move(gfx::Vector2d(1, 1));
  CullRect cull_rect2(gfx::Rect(0, 0, 300, 400));
  // Should ignore old_cull_rect.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect2, state1, state1,
                                   scroll_translation_state, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect2);

  CullRect cull_rect3 = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(cull_rect3, state1, state1,
                                   scroll_translation_state));
  EXPECT_EQ(gfx::Rect(20, 10, 2000, 2000), cull_rect3.Rect());
}

TEST_F(CullRectTest, ApplyTransformsWithOrigin) {
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(1, 2));
  auto* t2 =
      CreateTransform(*t1, MakeScaleMatrix(0.5), gfx::Point3F(50, 100, 0));
  PropertyTreeState root = PropertyTreeState::Root();
  PropertyTreeState state1(*t1, c0(), e0());
  PropertyTreeState state2(*t2, c0(), e0());
  CullRect cull_rect1(gfx::Rect(0, 0, 50, 200));
  EXPECT_FALSE(ApplyPaintProperties(cull_rect1, root, state1, state2));
  EXPECT_EQ(gfx::Rect(-50, -100, 100, 400), cull_rect1.Rect());
}

TEST_F(CullRectTest, SingleScrollPartialScrollingContents) {
  auto* t1 = Create2DTranslation(t0(), 1, 2);
  PropertyTreeState state1(*t1, c0(), e0());

  auto scroll_translation_state = CreateCompositedScrollTranslationState(
      state1, -3000, -5000, gfx::Rect(20, 10, 300, 400), gfx::Size(8000, 8000));

  // Same as ApplyScrollTranslationPartialScrollingContents.
  CullRect cull_rect1(gfx::Rect(0, 0, 400, 500));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect1, state1, state1,
                                   scroll_translation_state));
  EXPECT_EQ(gfx::Rect(1020, 3010, 4300, 4400), cull_rect1.Rect());

  CullRect old_cull_rect(gfx::Rect(1000, 3100, 4000, 4000));
  CullRect cull_rect2(gfx::Rect(0, 0, 400, 500));
  // Use old_cull_rect if the new cull rect didn't change enough.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect2, state1, state1,
                                   scroll_translation_state, old_cull_rect));
  EXPECT_EQ(old_cull_rect, cull_rect2);

  CullRect cull_rect3(gfx::Rect(0, 0, 300, 500));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect3, state1, state1,
                                   scroll_translation_state, old_cull_rect));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // Use old_cull_rect if the new cull rect didn't change enough.
    EXPECT_EQ(old_cull_rect, cull_rect3);
  } else {
    // Use the new cull rect if it is mapped from a rect that is smaller than
    // the scroll container rect.
    EXPECT_EQ(gfx::Rect(1020, 3010, 4280, 4400), cull_rect3.Rect());
  }

  old_cull_rect.Move(gfx::Vector2d(1000, 1000));
  CullRect cull_rect4(gfx::Rect(0, 0, 400, 500));
  // Use the new cull rect if it changed enough.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect4, state1, state1,
                                   scroll_translation_state, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect4);

  CullRect cull_rect5 = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(cull_rect5, state1, state1,
                                   scroll_translation_state));
  // This is the same as the first result.
  EXPECT_EQ(gfx::Rect(1020, 3010, 4300, 4400), cull_rect5.Rect());
}

TEST_F(CullRectTest, TransformUnderScrollTranslation) {
  auto* t1 = Create2DTranslation(t0(), 1, 2);
  PropertyTreeState state1(*t1, c0(), e0());
  auto scroll_translation_state = CreateCompositedScrollTranslationState(
      state1, -3000, -5000, gfx::Rect(20, 10, 300, 400), gfx::Size(8000, 8000));
  auto* t2 =
      Create2DTranslation(scroll_translation_state.Transform(), 2000, 3000);
  PropertyTreeState state2 = scroll_translation_state;
  state2.SetTransform(*t2);

  // Cases below are the same as those in SingleScrollPartialScrollingContents,
  // except that the offset is adjusted with |t2|.
  CullRect cull_rect1(gfx::Rect(0, 0, 400, 500));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect1, state1, state1, state2));
  EXPECT_EQ(gfx::Rect(-980, 10, 4300, 4400), cull_rect1.Rect());

  CullRect old_cull_rect(gfx::Rect(-980, 10, 4000, 4000));
  CullRect cull_rect2(gfx::Rect(0, 0, 400, 500));
  // Use old_cull_rect if the new cull rect didn't change enough.
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect2, state1, state1, state2, old_cull_rect));
  EXPECT_EQ(old_cull_rect, cull_rect2);

  CullRect cull_rect3(gfx::Rect(0, 0, 300, 500));
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect3, state1, state1, state2, old_cull_rect));
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // Use old_cull_rect if the new cull rect didn't change enough.
    EXPECT_EQ(old_cull_rect, cull_rect3);
  } else {
    // Use new cull_rect if it is mapped from a rect that is smaller than the
    // scroll container rect.
    EXPECT_EQ(gfx::Rect(-980, 10, 4280, 4400), cull_rect3.Rect());
  }

  old_cull_rect.Move(gfx::Vector2d(1000, 2000));
  CullRect cull_rect4(gfx::Rect(0, 0, 400, 500));
  // Use the new cull rect if it changed enough.
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect4, state1, state1, state2, old_cull_rect));
  EXPECT_EQ(gfx::Rect(-980, 10, 4300, 4400), cull_rect4.Rect());

  CullRect cull_rect5 = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(cull_rect5, state1, state1, state2));
  // This is the same as the first result.
  EXPECT_EQ(gfx::Rect(-980, 10, 4300, 4400), cull_rect5.Rect());
}

TEST_F(CullRectTest, TransformEscapingScroll) {
  PropertyTreeState root = PropertyTreeState::Root();
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(1, 2));
  auto* c1 = CreateClip(c0(), t0(), FloatRoundedRect(111, 222, 333, 444));
  PropertyTreeState state1(*t1, *c1, e0());

  auto scroll_translation_state = CreateCompositedScrollTranslationState(
      state1, -3000, -5000, gfx::Rect(20, 10, 40, 50), gfx::Size(8000, 8000));

  auto* t2 = CreateTransform(scroll_translation_state.Transform(),
                             MakeTranslationMatrix(100, 200));
  PropertyTreeState state2(*t2, scroll_translation_state.Clip(), e0());

  CullRect cull_rect1(gfx::Rect(0, 0, 50, 100));
  // Ignore the current cull rect, and apply paint properties from root to
  // state1 on infinite cull rect instead.
  EXPECT_FALSE(ApplyPaintProperties(cull_rect1, root, state2, state1));
  EXPECT_EQ(gfx::Rect(110, 220, 333, 444), cull_rect1.Rect());

  CullRect old_cull_rect = cull_rect1;
  old_cull_rect.Move(gfx::Vector2d(1, 1));
  CullRect cull_rect2(gfx::Rect(0, 0, 50, 100));
  // Should ignore old_cull_rect.
  EXPECT_FALSE(
      ApplyPaintProperties(cull_rect2, root, state2, state1, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect2);

  CullRect cull_rect3 = CullRect::Infinite();
  EXPECT_FALSE(ApplyPaintProperties(cull_rect3, root, state2, state1));
  EXPECT_EQ(cull_rect1, cull_rect3);
}

TEST_F(CullRectTest, SmallScrollContentsAfterBigScrollContents) {
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(1, 2));
  PropertyTreeState state1(*t1, c0(), e0());

  auto scroll_translation_state1 = CreateCompositedScrollTranslationState(
      state1, -10, -15, gfx::Rect(20, 10, 40, 50), gfx::Size(8000, 8000));

  auto* t2 = CreateTransform(scroll_translation_state1.Transform(),
                             MakeTranslationMatrix(500, 600));
  PropertyTreeState state2(*t2, scroll_translation_state1.Clip(), e0());

  auto scroll_translation_state2 = CreateCompositedScrollTranslationState(
      state2, -10, -15, gfx::Rect(30, 20, 100, 200), gfx::Size(200, 400));

  CullRect cull_rect1(gfx::Rect(0, 0, 50, 100));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect1, state1, state1,
                                   scroll_translation_state2));
  EXPECT_EQ(gfx::Rect(30, 20, 200, 400), cull_rect1.Rect());

  CullRect old_cull_rect = cull_rect1;
  old_cull_rect.Move(gfx::Vector2d(1, 1));
  CullRect cull_rect2(gfx::Rect(0, 0, 50, 100));
  // Should ignore old_cull_rect.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect2, state1, state1,
                                   scroll_translation_state2, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect2);
}

TEST_F(CullRectTest, BigScrollContentsAfterSmallScrollContents) {
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(1, 2));
  PropertyTreeState state1(*t1, c0(), e0());

  auto scroll_translation_state1 = CreateCompositedScrollTranslationState(
      state1, -10, -15, gfx::Rect(30, 20, 100, 200), gfx::Size(300, 400));

  auto* t2 = CreateTransform(scroll_translation_state1.Transform(),
                             MakeTranslationMatrix(10, 20));
  PropertyTreeState state2(*t2, scroll_translation_state1.Clip(), e0());

  auto scroll_translation_state2 = CreateCompositedScrollTranslationState(
      state2, -3000, -5000, gfx::Rect(20, 10, 50, 100),
      gfx::Size(10000, 20000));

  CullRect cull_rect1(gfx::Rect(0, 0, 100, 200));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect1, state1, state1,
                                   scroll_translation_state2));
  // The first scroller doesn't matter as long as the second scroller is in
  // its contents cull rect.
  // The cull rect starts from the container rect of the second scroll:
  //     (20, 10, 50, 100)
  // After the second scroll offset: (3020, 5010, 50, 100)
  // Expanded (using minimum expansion for small scrollers).
  // Then clipped by the contents rect.
  EXPECT_EQ(gfx::Rect(1996, 3986, 2098, 2148), cull_rect1.Rect());

  CullRect old_cull_rect = cull_rect1;
  old_cull_rect.Move(gfx::Vector2d(0, 100));
  CullRect cull_rect2(gfx::Rect(0, 0, 100, 200));
  // Use old_cull_rect if the new cull rect didn't change enough.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect2, state1, state1,
                                   scroll_translation_state2, old_cull_rect));
  EXPECT_EQ(old_cull_rect, cull_rect2);

  old_cull_rect.Move(gfx::Vector2d(1000, 1000));
  CullRect cull_rect3(gfx::Rect(0, 0, 100, 200));
  // Use the new cull rect if it changed enough.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect3, state1, state1,
                                   scroll_translation_state2, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect3);
}

TEST_F(CullRectTest, NonCompositedTransformUnderClip) {
  PropertyTreeState root = PropertyTreeState::Root();
  auto* c1 = CreateClip(c0(), t0(), FloatRoundedRect(100, 200, 300, 400));
  auto* t1 = CreateTransform(t0(), MakeTranslationMatrix(10, 20));
  PropertyTreeState state1(*t1, *c1, e0());

  CullRect cull_rect1(gfx::Rect(0, 0, 300, 500));
  EXPECT_FALSE(ApplyPaintProperties(cull_rect1, root, root, state1));
  // Clip by c1, then transformed by t1.
  EXPECT_EQ(gfx::Rect(90, 180, 200, 300), cull_rect1.Rect());

  CullRect cull_rect2(gfx::Rect(0, 0, 300, 500));
  CullRect old_cull_rect(gfx::Rect(133, 244, 333, 444));
  // Should ignore old_cull_rect.
  EXPECT_FALSE(
      ApplyPaintProperties(cull_rect2, root, root, state1, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect2);

  CullRect cull_rect3 = CullRect::Infinite();
  EXPECT_FALSE(ApplyPaintProperties(cull_rect3, root, root, state1));
  EXPECT_EQ(gfx::Rect(90, 180, 300, 400), cull_rect3.Rect());

  CullRect cull_rect4;
  EXPECT_FALSE(ApplyPaintProperties(cull_rect4, root, root, state1));
  EXPECT_EQ(gfx::Rect(), cull_rect4.Rect());
}

TEST_F(CullRectTest, CompositedTranslationUnderClip) {
  PropertyTreeState root = PropertyTreeState::Root();
  auto* c1 = CreateClip(c0(), t0(), FloatRoundedRect(100, 200, 300, 400));
  auto transform = MakeTranslationMatrix(10, 20);
  transform.Scale3d(2, 4, 1);
  auto* t1 = CreateTransform(t0(), transform, gfx::Point3F(),
                             CompositingReason::kWillChangeTransform);
  PropertyTreeState state1(*t1, *c1, e0());

  CullRect cull_rect1(gfx::Rect(0, 0, 300, 500));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect1, root, root, state1));
  // The result in NonCompositedTransformUnderClip expanded by 2000 (scaled by
  // minimum of 1/2 and 1/4), and clamped by minimum 2 * 512.
  EXPECT_EQ(gfx::Rect(-979, -979, 2148, 2123), cull_rect1.Rect());

  CullRect cull_rect2(gfx::Rect(0, 0, 300, 500));
  CullRect old_cull_rect = cull_rect1;
  old_cull_rect.Move(gfx::Vector2d(200, 200));
  // Use old_cull_rect if the new cull rect didn't change enough.
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect2, root, root, state1, old_cull_rect));
  EXPECT_EQ(old_cull_rect, cull_rect2);

  CullRect cull_rect3(gfx::Rect(0, 0, 300, 500));
  old_cull_rect.Move(gfx::Vector2d(1000, 1000));
  // Use the new cull rect if it changed enough.
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect3, root, root, state1, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect3);

  CullRect cull_rect4 = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(cull_rect4, root, root, state1));
  EXPECT_EQ(gfx::Rect(-979, -979, 2198, 2148), cull_rect4.Rect());

  CullRect cull_rect5;
  EXPECT_TRUE(ApplyPaintProperties(cull_rect4, root, root, state1));
  EXPECT_EQ(gfx::Rect(), cull_rect5.Rect());
}

TEST_F(CullRectTest, CompositedTransformUnderClipWithoutExpansion) {
  expansion_ratio_ = 0;
  PropertyTreeState root = PropertyTreeState::Root();
  auto* c1 = CreateClip(c0(), t0(), FloatRoundedRect(100, 200, 300, 400));
  auto* t1 =
      CreateTransform(t0(), MakeTranslationMatrix(10, 20), gfx::Point3F(),
                      CompositingReason::kWillChangeTransform);
  PropertyTreeState state1(*t1, *c1, e0());

  CullRect cull_rect1(gfx::Rect(0, 0, 300, 500));
  EXPECT_FALSE(ApplyPaintProperties(cull_rect1, root, root, state1));
  // Clip by c1, then transformed by t1.
  EXPECT_EQ(gfx::Rect(90, 180, 200, 300), cull_rect1.Rect());

  CullRect cull_rect2(gfx::Rect(0, 0, 300, 500));
  CullRect old_cull_rect(gfx::Rect(133, 244, 333, 444));
  // Should ignore old_cull_rect.
  EXPECT_FALSE(
      ApplyPaintProperties(cull_rect2, root, root, state1, old_cull_rect));
  EXPECT_EQ(cull_rect1, cull_rect2);

  CullRect cull_rect3 = CullRect::Infinite();
  EXPECT_FALSE(ApplyPaintProperties(cull_rect3, root, root, state1));
  EXPECT_EQ(gfx::Rect(90, 180, 300, 400), cull_rect3.Rect());

  CullRect cull_rect4;
  EXPECT_FALSE(ApplyPaintProperties(cull_rect4, root, root, state1));
  EXPECT_EQ(gfx::Rect(), cull_rect4.Rect());
}

TEST_F(CullRectTest, ClipAndCompositedScrollAndClip) {
  auto root = PropertyTreeState::Root();
  auto* c1 = CreateClip(c0(), t0(), FloatRoundedRect(0, 10000, 100, 100));
  auto* t1 = Create2DTranslation(t0(), 0, 10000);
  auto scroll_state = CreateCompositedScrollTranslationState(
      PropertyTreeState(*t1, *c1, e0()), 0, 0, gfx::Rect(0, 0, 400, 400),
      gfx::Size(10000, 5000));
  auto& scroll_clip = scroll_state.Clip();
  auto& scroll_translation = scroll_state.Transform();
  auto* c2a = CreateClip(scroll_clip, scroll_translation,
                         FloatRoundedRect(0, 300, 100, 100));
  auto* c2b = CreateClip(scroll_clip, scroll_translation,
                         FloatRoundedRect(0, 8000, 100, 100));
  auto* t2 =
      CreateTransform(scroll_translation, gfx::Transform(), gfx::Point3F(),
                      CompositingReason::kWillChangeTransform);

  // c2a is out of view, but in the expansion area of the composited scroll.
  CullRect cull_rect = CullRect::Infinite();
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect, root, root,
                           PropertyTreeState(scroll_translation, *c2a, e0())));
  EXPECT_EQ(gfx::Rect(0, 300, 100, 100), cull_rect.Rect());

  // Composited case. The cull rect should be expanded.
  cull_rect = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(cull_rect, root, root,
                                   PropertyTreeState(*t2, *c2a, e0())));
  EXPECT_EQ(gfx::Rect(-4000, -3700, 8100, 8100), cull_rect.Rect());

  // Using c2a with old cull rect.
  cull_rect = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(
      cull_rect, root, root, PropertyTreeState(scroll_translation, *c2a, e0()),
      CullRect(gfx::Rect(0, 310, 100, 100))));
  // The new cull rect touches the left edge of the clipped expanded scrolling
  // contents bounds, so the old cull rect is not used.
  EXPECT_EQ(gfx::Rect(0, 300, 100, 100), cull_rect.Rect());

  // Composited case. The cull rect should be expanded.
  cull_rect = CullRect::Infinite();
  EXPECT_TRUE(ApplyPaintProperties(
      cull_rect, root, root, PropertyTreeState(*t2, *c2a, e0()),
      CullRect(gfx::Rect(-3900, -3700, 8100, 8100))));
  // The new cull rect touches the left edge of the clipped expanded scrolling
  // contents bounds, so the old cull rect is not used.
  EXPECT_EQ(gfx::Rect(-4000, -3700, 8100, 8100), cull_rect.Rect());

  // c2b is out of the expansion area of the composited scroll.
  cull_rect = CullRect::Infinite();
  EXPECT_FALSE(
      ApplyPaintProperties(cull_rect, root, root,
                           PropertyTreeState(scroll_translation, *c2b, e0())));
  EXPECT_EQ(gfx::Rect(), cull_rect.Rect());

  // Composited case. The cull rect should be still empty.
  cull_rect = CullRect::Infinite();
  EXPECT_FALSE(ApplyPaintProperties(cull_rect, root, root,
                                    PropertyTreeState(*t2, *c2b, e0())));
  EXPECT_EQ(gfx::Rect(), cull_rect.Rect());
}

// Test for multiple clips (e.g., overflow clip and inner border radius)
// associated with the same scroll translation.
TEST_F(CullRectTest, MultipleClips) {
  auto* t1 = Create2DTranslation(t0(), 0, 0);
  auto scroll_state = CreateCompositedScrollTranslationState(
      PropertyTreeState(*t1, c0(), e0()), 0, 0, gfx::Rect(0, 0, 400, 400),
      gfx::Size(400, 2000));
  auto* border_radius_clip =
      CreateClip(c0(), *t1, FloatRoundedRect(0, 0, 400, 400));
  auto* scroll_clip =
      CreateClip(*border_radius_clip, *t1, FloatRoundedRect(0, 0, 400, 400));

  PropertyTreeState root = PropertyTreeState::Root();
  PropertyTreeState source(*t1, c0(), e0());
  PropertyTreeState destination = scroll_state;
  destination.SetClip(*scroll_clip);
  CullRect cull_rect(gfx::Rect(0, 0, 800, 600));
  EXPECT_TRUE(ApplyPaintProperties(cull_rect, root, source, destination));
  EXPECT_EQ(gfx::Rect(0, 0, 400, 2000), cull_rect.Rect());
}

TEST_F(CullRectTest, ClipWithNonIntegralOffsetAndZeroSize) {
  auto* clip = CreateClip(c0(), t0(), FloatRoundedRect(0.4, 0.6, 0, 0));
  PropertyTreeState source = PropertyTreeState::Root();
  PropertyTreeState destination(t0(), *clip, e0());
  CullRect cull_rect(gfx::Rect(0, 0, 800, 600));
  EXPECT_FALSE(ApplyPaintProperties(cull_rect, source, source, destination));
  EXPECT_TRUE(cull_rect.Rect().IsEmpty());
}

TEST_F(CullRectTest, ScrollableAlongOneAxisWithClippedInput) {
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // This test doesn't provide additional test coverage.
    GTEST_SKIP();
  }

  auto root = PropertyTreeState::Root();
  // Scrollable along y only.
  auto scroll_state = CreateCompositedScrollTranslationState(
      root, 0, 0, gfx::Rect(0, 0, 300, 300), gfx::Size(300, 5000));

  // The input rect is smaller than the container rect.
  CullRect cull_rect(gfx::Rect(10, 10, 150, 250));
  // Apply scroll translation. Should expand in the scrollable direction.
  EXPECT_TRUE(ApplyPaintProperties(cull_rect, root, root, scroll_state));
  EXPECT_EQ(gfx::Rect(10, 0, 150, 1284), cull_rect.Rect());

  CullRect old_cull_rect = cull_rect;
  // The input rect becomes wider but still smaller than the container rect.
  // ChangedEnough should return true as the changed direction is not expanded.
  cull_rect = CullRect(gfx::Rect(10, 10, 200, 250));
  EXPECT_TRUE(
      ApplyPaintProperties(cull_rect, root, root, scroll_state, old_cull_rect));
  EXPECT_EQ(gfx::Rect(10, 0, 200, 1284), cull_rect.Rect());
}

TEST_F(CullRectTest, IntersectsVerticalRange) {
  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));

  EXPECT_TRUE(cull_rect.IntersectsVerticalRange(LayoutUnit(), LayoutUnit(1)));
  EXPECT_FALSE(
      cull_rect.IntersectsVerticalRange(LayoutUnit(100), LayoutUnit(101)));
}

TEST_F(CullRectTest, IntersectsHorizontalRange) {
  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));

  EXPECT_TRUE(cull_rect.IntersectsHorizontalRange(LayoutUnit(), LayoutUnit(1)));
  EXPECT_FALSE(
      cull_rect.IntersectsHorizontalRange(LayoutUnit(50), LayoutUnit(51)));
}

TEST_F(CullRectTest, TransferExpansionOutsetY) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(20, 10, 400, 500),
      gfx::Size(560, 12000));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 350, 450));
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));

  // Outsets in the dynamic case are initially 4000, but in this case, the
  // scrollable is scrollable in both dimensions, so we initially drop this to
  // 2000 in all directions to prevent the rect from being too large. However,
  // in this case, our scroll extent in the x direction is small (160). This
  // reduces the total extent in the x dimension to 160 and the remaining
  // outset (1840) is added to the y outset (giving a total outset of 3840).
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // Use container rect: 20,10 400x500
    // Scrolled: 30,25 400x500
    // Expanded(160,3840): -130,-3815 720x8180
    // Clipped by contents_rect: 20,10 560x4355
    EXPECT_EQ(gfx::Rect(20, 10, 560, 4355), cull_rect.Rect());
  } else {
    // Clipped by container rect: 20,10 330x440
    // Scrolled: 30,25 330x440
    // Expanded(160,3840): -130,-3815 650x8120
    // Clipped by contents_rect: 20,10 500x4295
    EXPECT_EQ(gfx::Rect(20, 10, 500, 4295), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 10, 560, 4355), cull_rect.Rect());
}

TEST_F(CullRectTest, TransferExpansionOutsetX) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(20, 10, 400, 500),
      gfx::Size(12000, 650));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 350, 450));
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));

  // We have a scroll range of 150 in y. We're starting with 2000 in the case
  // of being scrollable in two dimensions, so this leaves 1850 to be
  // transferred to the x outset leading to an outset of 3850.
  if (RuntimeEnabledFeatures::ScrollCullRectFromContainerRectEnabled()) {
    // Use container rect: 20,10 400x500
    // Scrolled: 30,25 400x500
    // Expanded(3850,150): -3820,-125 8100x800
    // Clipped by contents_rect: 20,10 4260x650
    EXPECT_EQ(gfx::Rect(20, 10, 4260, 650), cull_rect.Rect());
  } else {
    // Clipped by container rect: 20,10 330x440
    // Scrolled: 30,25 330x440
    // Expanded(1024,150): -3820,-125 8030x740
    // Clipped by contents_rect: 20,10 4190x605
    EXPECT_EQ(gfx::Rect(20, 10, 4190, 605), cull_rect.Rect());
  }

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // In the following cases, the infinite rect is clipped to (20, 10, 400, 500).
  // The increase in width is reflected in the values below.
  EXPECT_EQ(gfx::Rect(20, 10, 4260, 650), cull_rect.Rect());
}

TEST_F(CullRectTest, TransferExpansionOutsetBlocked) {
  auto state = CreateCompositedScrollTranslationState(
      PropertyTreeState::Root(), -10, -15, gfx::Rect(20, 10, 40, 50),
      gfx::Size(200, 200));
  auto& scroll_translation = state.Transform();

  CullRect cull_rect(gfx::Rect(0, 0, 50, 100));
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  // Clipping to the contents rect should give us 200 both directions in all
  // cases.
  EXPECT_EQ(gfx::Rect(20, 10, 200, 200), cull_rect.Rect());

  cull_rect = CullRect::Infinite();
  EXPECT_EQ(std::make_pair(true, true),
            ApplyScrollTranslation(cull_rect, scroll_translation));
  EXPECT_EQ(gfx::Rect(20, 10, 200, 200), cull_rect.Rect());
}

}  // namespace blink