1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, Hardware
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gtest/gtest.h"
17
18 #include "utils/region.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Drawing {
26 class RegionTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp() override;
31 void TearDown() override;
32 };
33
SetUpTestCase()34 void RegionTest::SetUpTestCase() {}
TearDownTestCase()35 void RegionTest::TearDownTestCase() {}
SetUp()36 void RegionTest::SetUp() {}
TearDown()37 void RegionTest::TearDown() {}
38
39 /**
40 * @tc.name: CreateRegion001
41 * @tc.desc: test for creating Regin.
42 * @tc.type: FUNC
43 * @tc.require: I766AZ
44 */
45 HWTEST_F(RegionTest, CreateRegion001, TestSize.Level1)
46 {
47 std::unique_ptr<Region> region = std::make_unique<Region>();
48 ASSERT_TRUE(region != nullptr);
49 }
50
51 /**
52 * @tc.name: SetRectTest001
53 * @tc.desc: test for constructs a rectangular Region matching the bounds of rect.
54 * @tc.type: FUNC
55 * @tc.require: I766AZ
56 */
57 HWTEST_F(RegionTest, SetRectTest001, TestSize.Level1)
58 {
59 std::unique_ptr<Region> region = std::make_unique<Region>();
60 ASSERT_TRUE(region != nullptr);
61 RectI rectI;
62 EXPECT_FALSE(region->SetRect(rectI));
63 }
64
65 /**
66 * @tc.name: SetRectTest002
67 * @tc.desc: test for constructs a rectangular Region matching the bounds of rect.
68 * @tc.type: FUNC
69 * @tc.require: I766AZ
70 */
71 HWTEST_F(RegionTest, SetRectTest002, TestSize.Level1)
72 {
73 std::unique_ptr<Region> region = std::make_unique<Region>();
74 ASSERT_TRUE(region != nullptr);
75 RectI rectI(0, 0, 256, 256);
76 EXPECT_TRUE(region->SetRect(rectI));
77 }
78
79 /**
80 * @tc.name: SetPathTest001
81 * @tc.desc: test for constructs Region to match outline of path within clip.
82 * @tc.type: FUNC
83 * @tc.require: I766AZ
84 */
85 HWTEST_F(RegionTest, SetPathTest001, TestSize.Level1)
86 {
87 std::unique_ptr<Region> region = std::make_unique<Region>();
88 ASSERT_TRUE(region != nullptr);
89 Path path;
90 Region clip;
91 EXPECT_FALSE(region->SetPath(path, clip));
92 }
93
94 /**
95 * @tc.name: SetPathTest002
96 * @tc.desc: test for constructs Region to match outline of path within clip.
97 * @tc.type: FUNC
98 * @tc.require: I766AZ
99 */
100 HWTEST_F(RegionTest, SetPathTest002, TestSize.Level1)
101 {
102 std::unique_ptr<Region> region = std::make_unique<Region>();
103 ASSERT_TRUE(region != nullptr);
104 Path path;
105 path.AddRect(1.0f, 4.0f, 3.0f, 2.0f, PathDirection::CCW_DIRECTION);
106 Region clip;
107 EXPECT_FALSE(region->SetPath(path, clip));
108 }
109
110 /**
111 * @tc.name: IsIntersectsTest001
112 * @tc.desc: test for IsIntersects function
113 * @tc.type: FUNC
114 * @tc.require: I766AZ
115 */
116 HWTEST_F(RegionTest, IsIntersectsTest001, TestSize.Level1)
117 {
118 std::unique_ptr<Region> region = std::make_unique<Region>();
119 ASSERT_TRUE(region != nullptr);
120 Region other;
121 EXPECT_FALSE(region->IsIntersects(other));
122 }
123
124 /**
125 * @tc.name: IsIntersectsTest002
126 * @tc.desc: test for IsIntersects function
127 * @tc.type: FUNC
128 * @tc.require: I766AZ
129 */
130 HWTEST_F(RegionTest, IsIntersectsTest002, TestSize.Level1)
131 {
132 std::unique_ptr<Region> region = std::make_unique<Region>();
133 ASSERT_TRUE(region != nullptr);
134 RectI rectI(0, 0, 256, 256);
135 region->SetRect(rectI);
136 Region other;
137 other.SetRect(rectI);
138 EXPECT_TRUE(region->IsIntersects(other));
139 }
140
141 /**
142 * @tc.name: OpTest001
143 * @tc.desc: test for OP function with DIFFERENCE option
144 * @tc.type: FUNC
145 * @tc.require: I766AZ
146 */
147 HWTEST_F(RegionTest, OpTest001, TestSize.Level1)
148 {
149 std::unique_ptr<Region> region = std::make_unique<Region>();
150 ASSERT_TRUE(region != nullptr);
151 Region other;
152 EXPECT_FALSE(region->Op(other, RegionOp::DIFFERENCE));
153 }
154
155 /**
156 * @tc.name: OpTest002
157 * @tc.desc: test for OP function with DIFFERENCE option
158 * @tc.type: FUNC
159 * @tc.require: I766AZ
160 */
161 HWTEST_F(RegionTest, OpTest002, TestSize.Level1)
162 {
163 std::unique_ptr<Region> region = std::make_unique<Region>();
164 ASSERT_TRUE(region != nullptr);
165 RectI rectI(0, 0, 256, 256);
166 RectI otherRectI(0, 400, 500, 600);
167 region->SetRect(rectI);
168 Region other;
169 other.SetRect(otherRectI);
170 EXPECT_TRUE(region->Op(other, RegionOp::DIFFERENCE));
171 }
172
173 /**
174 * @tc.name: OpTest003
175 * @tc.desc: test for OP function with INTERSECT option
176 * @tc.type: FUNC
177 * @tc.require: I766AZ
178 */
179 HWTEST_F(RegionTest, OpTest003, TestSize.Level1)
180 {
181 std::unique_ptr<Region> region = std::make_unique<Region>();
182 ASSERT_TRUE(region != nullptr);
183 RectI rectI(0, 0, 256, 256);
184 RectI otherRectI(0, 0, 256, 256);
185 region->SetRect(rectI);
186 Region other;
187 other.SetRect(otherRectI);
188 EXPECT_TRUE(region->Op(other, RegionOp::INTERSECT));
189 }
190
191 /**
192 * @tc.name: OpTest004
193 * @tc.desc: test for OP function with UNION option
194 * @tc.type: FUNC
195 * @tc.require: I766AZ
196 */
197 HWTEST_F(RegionTest, OpTest004, TestSize.Level1)
198 {
199 std::unique_ptr<Region> region = std::make_unique<Region>();
200 ASSERT_TRUE(region != nullptr);
201 RectI rectI(0, 0, 256, 256);
202 RectI otherRectI(0, 400, 500, 600);
203 region->SetRect(rectI);
204 Region other;
205 other.SetRect(otherRectI);
206 EXPECT_TRUE(region->Op(other, RegionOp::UNION));
207 }
208
209 /**
210 * @tc.name: OpTest005
211 * @tc.desc: test for OP function with XOR option
212 * @tc.type: FUNC
213 * @tc.require: I766AZ
214 */
215 HWTEST_F(RegionTest, OpTest005, TestSize.Level1)
216 {
217 std::unique_ptr<Region> region = std::make_unique<Region>();
218 ASSERT_TRUE(region != nullptr);
219 RectI rectI(0, 0, 256, 256);
220 RectI otherRectI(0, 400, 500, 600);
221 region->SetRect(rectI);
222 Region other;
223 other.SetRect(otherRectI);
224 EXPECT_TRUE(region->Op(other, RegionOp::XOR));
225 }
226
227 /**
228 * @tc.name: OpTest006
229 * @tc.desc: test for OP function with REVERSE_DIFFERENCE option
230 * @tc.type: FUNC
231 * @tc.require: I766AZ
232 */
233 HWTEST_F(RegionTest, OpTest006, TestSize.Level1)
234 {
235 std::unique_ptr<Region> region = std::make_unique<Region>();
236 ASSERT_TRUE(region != nullptr);
237 RectI rectI(0, 0, 256, 256);
238 RectI otherRectI(0, 400, 500, 600);
239 region->SetRect(rectI);
240 Region other;
241 other.SetRect(otherRectI);
242 EXPECT_TRUE(region->Op(other, RegionOp::REVERSE_DIFFERENCE));
243 }
244
245 /**
246 * @tc.name: OpTest007
247 * @tc.desc: test for OP function with REPLACE option
248 * @tc.type: FUNC
249 * @tc.require: I766AZ
250 */
251 HWTEST_F(RegionTest, OpTest007, TestSize.Level1)
252 {
253 std::unique_ptr<Region> region = std::make_unique<Region>();
254 ASSERT_TRUE(region != nullptr);
255 RectI rectI(0, 0, 256, 256);
256 RectI otherRectI(0, 400, 500, 600);
257 region->SetRect(rectI);
258 Region other;
259 other.SetRect(otherRectI);
260 EXPECT_TRUE(region->Op(other, RegionOp::REPLACE));
261 }
262
263 /**
264 * @tc.name: CloneTest001
265 * @tc.desc: test for clone func
266 * @tc.type: FUNC
267 * @tc.require: I766AZ
268 */
269 HWTEST_F(RegionTest, CloneTest001, TestSize.Level1)
270 {
271 Region region;
272 // rect, left: 0, top: 0, right: 2, bottom: 2
273 RectI rectI(0, 0, 2, 2);
274 region.SetRect(rectI);
275 // point(1, 1) is inside rect
276 EXPECT_TRUE(region.Contains(1, 1));
277 Region other(region);
278 EXPECT_TRUE(other.Contains(1, 1));
279
280 Region region2;
281 // rect, left: 10, top: 10, right: 20, bottom: 20
282 RectI rectI2(10, 10, 20, 20);
283 region2.SetRect(rectI2);
284 // point(15, 15) is inside rect
285 EXPECT_TRUE(region2.Contains(15, 15));
286 Region other2;
287 other2 = region2;
288 EXPECT_TRUE(region2.Contains(15, 15));
289 }
290
291 /**
292 * @tc.name: EqualsTest001
293 * @tc.desc: test for two regions are equal.
294 * @tc.type: FUNC
295 * @tc.require: I766AZ
296 */
297 HWTEST_F(RegionTest, EqualsTest001, TestSize.Level1)
298 {
299 Region region;
300 Region region2;
301 ASSERT_TRUE(region == region2);
302 RectI rectI(0, 0, 256, 256);
303 region.SetRect(rectI);
304 ASSERT_FALSE(region == region2);
305 }
306
307 /**
308 * @tc.name: SetEmptyTest001
309 * @tc.desc: test for set region empty.
310 * @tc.type: FUNC
311 * @tc.require: I766AZ
312 */
313 HWTEST_F(RegionTest, SetEmptyTest001, TestSize.Level1)
314 {
315 Region region;
316 RectI rectI(0, 0, 256, 256);
317 region.SetRect(rectI);
318 region.SetEmpty();
319 ASSERT_TRUE(region.IsEmpty());
320 }
321
322 /**
323 * @tc.name: SetRegionTest001
324 * @tc.desc: test for set region.
325 * @tc.type: FUNC
326 * @tc.require: I766AZ
327 */
328 HWTEST_F(RegionTest, SetRegionTest001, TestSize.Level1)
329 {
330 Region region;
331 Region region2;
332 RectI rectI(0, 0, 256, 256);
333 region.SetRect(rectI);
334 ASSERT_FALSE(region == region2);
335 region2.SetRegion(region);
336 ASSERT_TRUE(region == region2);
337 }
338
339 /**
340 * @tc.name: GetBoundsTest001
341 * @tc.desc: test for set region.
342 * @tc.type: FUNC
343 * @tc.require: I766AZ
344 */
345 HWTEST_F(RegionTest, GetBoundsTest001, TestSize.Level1)
346 {
347 Region region;
348 Region region2;
349 RectI rectI(0, 0, 256, 256);
350 region.SetRect(rectI);
351 ASSERT_TRUE(rectI == region.GetBounds());
352 }
353
354 /**
355 * @tc.name: isComplexTest001
356 * @tc.desc: test for region has multi rects.
357 * @tc.type: FUNC
358 * @tc.require: I766AZ
359 */
360 HWTEST_F(RegionTest, isComplexTest001, TestSize.Level1)
361 {
362 Region region;
363 Region region2;
364 RectI rectI(0, 0, 256, 256);
365 RectI otherRectI(50, 50, 400, 400);
366 region.SetRect(rectI);
367 ASSERT_FALSE(region.IsComplex());
368 region2.SetRect(otherRectI);
369 region.Op(region2, RegionOp::UNION);
370 ASSERT_TRUE(region.IsComplex());
371 }
372
373 /**
374 * @tc.name: QuickRejectTest001
375 * @tc.desc: test for region QuickReject with other.
376 * @tc.type: FUNC
377 * @tc.require: I766AZ
378 */
379 HWTEST_F(RegionTest, QuickRejectTest001, TestSize.Level1)
380 {
381 Region region;
382 Region region2;
383 RectI rectI(0, 0, 256, 256);
384 RectI otherRectI(50, 50, 400, 400);
385 region.SetRect(rectI);
386 region2.SetRect(otherRectI);
387 ASSERT_FALSE(region2.QuickReject(region));
388 }
389
390 /**
391 * @tc.name: TranslateTest001
392 * @tc.desc: test for region QuickReject with other.
393 * @tc.type: FUNC
394 * @tc.require: I766AZ
395 */
396 HWTEST_F(RegionTest, TranslateTest001, TestSize.Level1)
397 {
398 Region region;
399 Region region2;
400 RectI rectI(0, 0, 100, 100);
401 RectI otherRectI(100, 100, 200, 200);
402 region.SetRect(rectI);
403 region2.SetRect(otherRectI);
404 region.Translate(100, 100);
405 ASSERT_TRUE(region == region2);
406 }
407 } // namespace Drawing
408 } // namespace Rosen
409 } // namespace OHOS
410