Flutter Engine
The Flutter Engine
dl_accumulation_rect_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/display_list/utils/dl_accumulation_rect.h"
6#include "flutter/testing/assertions_skia.h"
7#include "gtest/gtest.h"
8
9namespace flutter {
10namespace testing {
11
12TEST(DisplayListAccumulationRect, Constructor) {
13 AccumulationRect accumulator;
14
15 EXPECT_TRUE(accumulator.is_empty());
16 EXPECT_TRUE(accumulator.bounds().isEmpty());
17 EXPECT_FALSE(accumulator.overlap_detected());
18}
19
20TEST(DisplayListAccumulationRect, OnePoint) {
21 AccumulationRect accumulator;
22 accumulator.accumulate(10.0f, 10.0f);
23
24 EXPECT_TRUE(accumulator.is_empty());
25 EXPECT_TRUE(accumulator.bounds().isEmpty());
26 EXPECT_FALSE(accumulator.overlap_detected());
27}
28
29TEST(DisplayListAccumulationRect, TwoPoints) {
30 auto test = [](DlScalar x1, DlScalar y1, //
31 DlScalar x2, DlScalar y2, //
32 SkRect bounds, //
33 bool should_be_empty, bool should_overlap,
34 const std::string& label) {
35 {
36 AccumulationRect accumulator;
37 accumulator.accumulate(x1, y1);
38 accumulator.accumulate(x2, y2);
39
40 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
41 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
42 EXPECT_EQ(accumulator.bounds(), bounds) << label;
43 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
44 }
45
46 {
47 AccumulationRect accumulator;
48 accumulator.accumulate(SkPoint::Make(x1, y1));
49 accumulator.accumulate(SkPoint::Make(x2, y2));
50
51 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
52 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
53 EXPECT_EQ(accumulator.bounds(), bounds) << label;
54 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
55 }
56
57 {
58 AccumulationRect accumulator;
59 accumulator.accumulate(DlPoint(x1, y1));
60 accumulator.accumulate(DlPoint(x2, y2));
61
62 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
63 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
64 EXPECT_EQ(accumulator.bounds(), bounds) << label;
65 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
66 }
67 };
68
69 test(10.0f, 10.0f, 10.0f, 10.0f, SkRect::MakeLTRB(10.0f, 10.0f, 10.0f, 10.0f),
70 true, false, "Same");
71 test(10.0f, 10.0f, 20.0f, 10.0f, SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 10.0f),
72 true, false, "Horizontal");
73 test(10.0f, 10.0f, 10.0f, 20.0f, SkRect::MakeLTRB(10.0f, 10.0f, 10.0f, 20.0f),
74 true, false, "Vertical");
75 test(10.0f, 10.0f, 20.0f, 20.0f, SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 20.0f),
76 false, false, "Diagonal");
77}
78
79TEST(DisplayListAccumulationRect, ThreePoints) {
80 auto test = [](DlScalar x1, DlScalar y1, //
81 DlScalar x2, DlScalar y2, //
82 DlScalar x3, DlScalar y3, //
83 SkRect bounds, //
84 bool should_be_empty, bool should_overlap,
85 const std::string& label) {
86 {
87 AccumulationRect accumulator;
88 accumulator.accumulate(x1, y1);
89 accumulator.accumulate(x2, y2);
90 accumulator.accumulate(x3, y3);
91
92 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
93 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
94 EXPECT_EQ(accumulator.bounds(), bounds) << label;
95 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
96 }
97
98 {
99 AccumulationRect accumulator;
100 accumulator.accumulate(SkPoint::Make(x1, y1));
101 accumulator.accumulate(SkPoint::Make(x2, y2));
102 accumulator.accumulate(SkPoint::Make(x3, y3));
103
104 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
105 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
106 EXPECT_EQ(accumulator.bounds(), bounds) << label;
107 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
108 }
109
110 {
111 AccumulationRect accumulator;
112 accumulator.accumulate(DlPoint(x1, y1));
113 accumulator.accumulate(DlPoint(x2, y2));
114 accumulator.accumulate(DlPoint(x3, y3));
115
116 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
117 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
118 EXPECT_EQ(accumulator.bounds(), bounds) << label;
119 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
120 }
121 };
122
123 test(10.0f, 10.0f, 10.0f, 10.0f, 10.0f, 10.0f,
124 SkRect::MakeLTRB(10.0f, 10.0f, 10.0f, 10.0f), true, false, "Same");
125 test(10.0f, 10.0f, 20.0f, 10.0f, 15.0f, 10.0f,
126 SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 10.0f), true, false, "Horizontal");
127 test(10.0f, 10.0f, 10.0f, 20.0f, 10.0f, 15.0f,
128 SkRect::MakeLTRB(10.0f, 10.0f, 10.0f, 20.0f), true, false, "Vertical");
129 test(10.0f, 10.0f, 20.0f, 20.0f, 25.0f, 15.0f,
130 SkRect::MakeLTRB(10.0f, 10.0f, 25.0f, 20.0f), false, false, "Disjoint");
131 test(10.0f, 10.0f, 20.0f, 20.0f, 15.0f, 15.0f,
132 SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 20.0f), false, true, "Inside");
133}
134
135TEST(DisplayListAccumulationRect, EmptyRect) {
136 auto test = [](DlScalar l, DlScalar t, DlScalar r, DlScalar b, //
137 SkRect bounds, //
138 bool should_be_empty, bool should_overlap,
139 const std::string& label) {
140 {
141 AccumulationRect accumulator;
142 accumulator.accumulate(SkRect::MakeLTRB(l, t, r, b));
143
144 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
145 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
146 EXPECT_EQ(accumulator.bounds(), bounds) << label;
147 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
148 }
149
150 {
151 AccumulationRect accumulator;
152 accumulator.accumulate(DlRect::MakeLTRB(l, t, r, b));
153
154 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
155 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
156 EXPECT_EQ(accumulator.bounds(), bounds) << label;
157 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
158 }
159
160 {
162 content.accumulate(l, t);
163 content.accumulate(r, b);
164 EXPECT_EQ(content.is_empty(), should_be_empty) << label;
165 EXPECT_EQ(content.bounds().isEmpty(), should_be_empty) << label;
166 // bounds for an accumulation by points may be different than the
167 // bounds for an accumulation by the rect they produce because
168 // construction by points has no "empty rejection" case.
169 if (!should_be_empty) {
170 EXPECT_EQ(content.bounds(), bounds) << label;
171 }
172 EXPECT_EQ(content.overlap_detected(), should_overlap) << label;
173
174 AccumulationRect accumulator;
175 accumulator.accumulate(content);
176
177 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
178 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
179 EXPECT_EQ(accumulator.bounds(), bounds) << label;
180 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
181 }
182 };
183
184 test(10.0f, 10.0f, 10.0f, 10.0f, SkRect::MakeLTRB(0.0f, 0.0f, 0.0f, 0.0f),
185 true, false, "Singular");
186 test(10.0f, 10.0f, 20.0f, 10.0f, SkRect::MakeLTRB(0.0f, 0.0f, 0.0f, 0.0f),
187 true, false, "Horizontal Empty");
188 test(10.0f, 10.0f, 10.0f, 20.0f, SkRect::MakeLTRB(0.0f, 0.0f, 0.0f, 0.0f),
189 true, false, "Vertical Empty");
190 test(10.0f, 10.0f, 20.0f, 20.0f, SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 20.0f),
191 false, false, "Non-Empty");
192}
193
194TEST(DisplayListAccumulationRect, TwoRects) {
195 auto test = [](DlScalar l1, DlScalar t1, DlScalar r1, DlScalar b1, //
196 DlScalar l2, DlScalar t2, DlScalar r2, DlScalar b2, //
197 SkRect bounds, //
198 bool should_be_empty, bool should_overlap,
199 const std::string& label) {
200 {
201 AccumulationRect accumulator;
202 accumulator.accumulate(SkRect::MakeLTRB(l1, t1, r1, b1));
203 accumulator.accumulate(SkRect::MakeLTRB(l2, t2, r2, b2));
204
205 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
206 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
207 EXPECT_EQ(accumulator.bounds(), bounds) << label;
208 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
209 }
210
211 {
212 AccumulationRect accumulator;
213 accumulator.accumulate(DlRect::MakeLTRB(l1, t1, r1, b1));
214 accumulator.accumulate(DlRect::MakeLTRB(l2, t2, r2, b2));
215
216 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
217 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
218 EXPECT_EQ(accumulator.bounds(), bounds) << label;
219 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
220 }
221
222 {
223 AccumulationRect content1;
224 content1.accumulate(l1, t1);
225 content1.accumulate(r1, b1);
226
227 AccumulationRect content2;
228 content2.accumulate(l2, t2);
229 content2.accumulate(r2, b2);
230
231 AccumulationRect accumulator;
232 accumulator.accumulate(content1);
233 accumulator.accumulate(content2);
234
235 EXPECT_EQ(accumulator.is_empty(), should_be_empty) << label;
236 EXPECT_EQ(accumulator.bounds().isEmpty(), should_be_empty) << label;
237 EXPECT_EQ(accumulator.bounds(), bounds) << label;
238 EXPECT_EQ(accumulator.overlap_detected(), should_overlap) << label;
239 }
240 };
241
242 test(10.0f, 10.0f, 10.0f, 10.0f, //
243 20.0f, 20.0f, 20.0f, 20.0f, //
244 SkRect::MakeLTRB(0.0f, 0.0f, 0.0f, 0.0f), //
245 true, false, "Empty + Empty");
246 test(10.0f, 10.0f, 20.0f, 10.0f, //
247 10.0f, 10.0f, 10.0f, 20.0f, //
248 SkRect::MakeLTRB(0.0f, 0.0f, 0.0f, 0.0f), //
249 true, false, "Horizontal + Vertical");
250 test(10.0f, 10.0f, 10.0f, 10.0f, //
251 15.0f, 15.0f, 20.0f, 20.0f, //
252 SkRect::MakeLTRB(15.0f, 15.0f, 20.0f, 20.0f), //
253 false, false, "Empty + Non-Empty");
254 test(10.0f, 10.0f, 15.0f, 15.0f, //
255 20.0f, 20.0f, 20.0f, 20.0f, //
256 SkRect::MakeLTRB(10.0f, 10.0f, 15.0f, 15.0f), //
257 false, false, "Non-Empty + Empty");
258 test(10.0f, 10.0f, 15.0f, 15.0f, //
259 15.0f, 15.0f, 20.0f, 20.0f, //
260 SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 20.0f), //
261 false, false, "Abutting");
262 test(10.0f, 10.0f, 15.0f, 15.0f, //
263 16.0f, 16.0f, 20.0f, 20.0f, //
264 SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 20.0f), //
265 false, false, "Disjoint");
266 test(10.0f, 10.0f, 16.0f, 16.0f, //
267 15.0f, 15.0f, 20.0f, 20.0f, //
268 SkRect::MakeLTRB(10.0f, 10.0f, 20.0f, 20.0f), //
269 false, true, "Overlapping");
270}
271
272} // namespace testing
273} // namespace flutter
#define test(name)
void accumulate(SkScalar x, SkScalar y)
static bool b
union flutter::testing::@2836::KeyboardChange::@76 content
Optional< SkRect > bounds
Definition: SkRecords.h:189
TEST(DisplayListComplexity, EmptyDisplayList)
impeller::Scalar DlScalar
impeller::Point DlPoint
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
bool isEmpty() const
Definition: SkRect.h:693
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678