1 /*
2  * Copyright (c) 2020-2021 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, software
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 "gfx_utils/rect.h"
17 
18 #include <climits>
19 #include <gtest/gtest.h>
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace {
25     const int16_t TEST_VALUE = 100;
26     const int16_t MIN_VALUE = 50;
27     const int16_t MAX_VALUE = 200;
28 }
29 class RectTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
TearDown()34     void TearDown() {}
35     static Rect* rect_;
36 };
37 
38 Rect* RectTest::rect_ = nullptr;
39 
SetUpTestCase(void)40 void RectTest::SetUpTestCase(void)
41 {
42     if (rect_ == nullptr) {
43         rect_ = new Rect();
44     }
45 }
46 
TearDownTestCase(void)47 void RectTest::TearDownTestCase(void)
48 {
49     if (rect_ != nullptr) {
50         delete rect_;
51         rect_ = nullptr;
52     }
53 }
54 
SetUp()55 void RectTest::SetUp()
56 {
57     if (rect_ == nullptr) {
58         EXPECT_EQ(1, 0);
59         return;
60     }
61     rect_->SetRect(0, 0, 0, 0);
62 }
63 /**
64  * @tc.name: RectOperator_001
65  * @tc.desc: Verify copy operator function, equal.
66  * @tc.type: FUNC
67  * @tc.require: AR000EEMQ9
68  */
69 HWTEST_F(RectTest, RectOperator_001, TestSize.Level0)
70 {
71     if (rect_ == nullptr) {
72         EXPECT_EQ(1, 0);
73         return;
74     }
75     Rect* rect1 = new Rect();
76     if (rect1 == nullptr) {
77         EXPECT_EQ(1, 0);
78         return;
79     }
80     rect1->SetRect(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
81     rect_->operator=(*rect1);
82 
83     EXPECT_EQ(rect_->GetLeft(), 0);
84     EXPECT_EQ(rect_->GetTop(), 0);
85     EXPECT_EQ(rect_->GetRight(), HORIZONTAL_RESOLUTION);
86     EXPECT_EQ(rect_->GetBottom(), VERTICAL_RESOLUTION);
87 
88     delete rect1;
89 }
90 
91 /**
92  * @tc.name: RectSetRect_001
93  * @tc.desc: Verify SetRect function, equal.
94  * @tc.type: FUNC
95  * @tc.require: AR000EEMQ9
96  */
97 HWTEST_F(RectTest, RectSetRect_001, TestSize.Level0)
98 {
99     if (rect_ == nullptr) {
100         EXPECT_EQ(1, 0);
101         return;
102     }
103     rect_->SetRect(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
104 
105     EXPECT_EQ(rect_->GetLeft(), 0);
106     EXPECT_EQ(rect_->GetTop(), 0);
107     EXPECT_EQ(rect_->GetRight(), HORIZONTAL_RESOLUTION);
108     EXPECT_EQ(rect_->GetBottom(), VERTICAL_RESOLUTION);
109 }
110 
111 /**
112  * @tc.name: RectGetWidth_001
113  * @tc.desc: Verify GetWidth function, equal.
114  * @tc.type: FUNC
115  * @tc.require: AR000EEMQ9
116  */
117 HWTEST_F(RectTest, RectGetWidth_001, TestSize.Level0)
118 {
119     if (rect_ == nullptr) {
120         EXPECT_EQ(1, 0);
121         return;
122     }
123     EXPECT_EQ(rect_->GetWidth(), 1);
124 }
125 
126 /**
127  * @tc.name: RectGetHeight_001
128  * @tc.desc: Verify GetHeight function, equal.
129  * @tc.type: FUNC
130  * @tc.require: AR000EEMQ9
131  */
132 HWTEST_F(RectTest, RectGetHeight_001, TestSize.Level0)
133 {
134     if (rect_ == nullptr) {
135         EXPECT_EQ(1, 0);
136         return;
137     }
138     EXPECT_EQ(rect_->GetHeight(), 1);
139 }
140 
141 /**
142  * @tc.name: RectGetX_001
143  * @tc.desc: Verify GetX function, equal.
144  * @tc.type: FUNC
145  * @tc.require: AR000EEMQ9
146  */
147 HWTEST_F(RectTest, RectGetX_001, TestSize.Level0)
148 {
149     if (rect_ == nullptr) {
150         EXPECT_EQ(1, 0);
151         return;
152     }
153     rect_->SetRect(TEST_VALUE, 0, 0, 0);
154     EXPECT_EQ(rect_->GetX(), TEST_VALUE);
155 }
156 
157 /**
158  * @tc.name: RectGetY_001
159  * @tc.desc: Verify GetY function, equal.
160  * @tc.type: FUNC
161  * @tc.require: AR000EEMQ9
162  */
163 HWTEST_F(RectTest, RectGetY_001, TestSize.Level0)
164 {
165     if (rect_ == nullptr) {
166         EXPECT_EQ(1, 0);
167         return;
168     }
169     rect_->SetRect(0, TEST_VALUE, 0, 0);
170     EXPECT_EQ(rect_->GetY(), TEST_VALUE);
171 }
172 
173 /**
174  * @tc.name: RectGetLeft_001
175  * @tc.desc: Verify GetLeft function, equal.
176  * @tc.type: FUNC
177  * @tc.require: AR000EEMQ9
178  */
179 HWTEST_F(RectTest, RectGetLeft_001, TestSize.Level0)
180 {
181     if (rect_ == nullptr) {
182         EXPECT_EQ(1, 0);
183         return;
184     }
185     rect_->SetRect(TEST_VALUE, 0, 0, 0);
186     EXPECT_EQ(rect_->GetLeft(), TEST_VALUE);
187 }
188 
189 /**
190  * @tc.name: RectGetTop_001
191  * @tc.desc: Verify GetTop function, equal.
192  * @tc.type: FUNC
193  * @tc.require: AR000EEMQ9
194  */
195 HWTEST_F(RectTest, RectGetTop_001, TestSize.Level0)
196 {
197     if (rect_ == nullptr) {
198         EXPECT_EQ(1, 0);
199         return;
200     }
201     rect_->SetRect(0, TEST_VALUE, 0, 0);
202     EXPECT_EQ(rect_->GetTop(), TEST_VALUE);
203 }
204 
205 /**
206  * @tc.name: RectGetRight_001
207  * @tc.desc: Verify GetRight function, equal.
208  * @tc.type: FUNC
209  * @tc.require: AR000EEMQ9
210  */
211 HWTEST_F(RectTest, RectGetRight_001, TestSize.Level0)
212 {
213     if (rect_ == nullptr) {
214         EXPECT_EQ(1, 0);
215         return;
216     }
217     rect_->SetRect(0, 0, TEST_VALUE, 0);
218     EXPECT_EQ(rect_->GetRight(), TEST_VALUE);
219 }
220 
221 /**
222  * @tc.name: RectGetBottom_001
223  * @tc.desc: Verify GetBottom function, equal.
224  * @tc.type: FUNC
225  * @tc.require: AR000EEMQ9
226  */
227 HWTEST_F(RectTest, RectGetBottom_001, TestSize.Level0)
228 {
229     if (rect_ == nullptr) {
230         EXPECT_EQ(1, 0);
231         return;
232     }
233     rect_->SetRect(0, 0, 0, TEST_VALUE);
234     EXPECT_EQ(rect_->GetBottom(), TEST_VALUE);
235 }
236 
237 /**
238  * @tc.name: RectSetX_001
239  * @tc.desc: Verify SetX function, equal.
240  * @tc.type: FUNC
241  * @tc.require: AR000EEMQ9
242  */
243 HWTEST_F(RectTest, RectSetX_001, TestSize.Level0)
244 {
245     if (rect_ == nullptr) {
246         EXPECT_EQ(1, 0);
247         return;
248     }
249     rect_->SetX(TEST_VALUE);
250     EXPECT_EQ(rect_->GetLeft(), TEST_VALUE);
251     EXPECT_EQ(rect_->GetRight(), TEST_VALUE);
252 }
253 
254 /**
255  * @tc.name: RectSetY_001
256  * @tc.desc: Verify SetY function, equal.
257  * @tc.type: FUNC
258  * @tc.require: AR000EEMQ9
259  */
260 HWTEST_F(RectTest, RectSetY_001, TestSize.Level0)
261 {
262     if (rect_ == nullptr) {
263         EXPECT_EQ(1, 0);
264         return;
265     }
266     rect_->SetY(TEST_VALUE);
267     EXPECT_EQ(rect_->GetTop(), TEST_VALUE);
268     EXPECT_EQ(rect_->GetBottom(), TEST_VALUE);
269 }
270 
271 /**
272  * @tc.name: RectSetPosition_001
273  * @tc.desc: Verify SetPosition function, equal.
274  * @tc.type: FUNC
275  * @tc.require: AR000EEMQ9
276  */
277 HWTEST_F(RectTest, RectSetPosition_001, TestSize.Level0)
278 {
279     if (rect_ == nullptr) {
280         EXPECT_EQ(1, 0);
281         return;
282     }
283     rect_->SetPosition(TEST_VALUE, TEST_VALUE);
284     EXPECT_EQ(rect_->GetLeft(), TEST_VALUE);
285     EXPECT_EQ(rect_->GetRight(), TEST_VALUE);
286     EXPECT_EQ(rect_->GetTop(), TEST_VALUE);
287     EXPECT_EQ(rect_->GetBottom(), TEST_VALUE);
288 }
289 
290 /**
291  * @tc.name: RectSetWidth_001
292  * @tc.desc: Verify SetWidth function, equal.
293  * @tc.type: FUNC
294  * @tc.require: AR000EEMQ9
295  */
296 HWTEST_F(RectTest, RectSetWidth_001, TestSize.Level0)
297 {
298     if (rect_ == nullptr) {
299         EXPECT_EQ(1, 0);
300         return;
301     }
302     rect_->SetWidth(TEST_VALUE);
303     EXPECT_EQ(rect_->GetRight(), TEST_VALUE - 1);
304 }
305 
306 /**
307  * @tc.name: RectSetHeight_001
308  * @tc.desc: Verify SetHeight function, equal.
309  * @tc.type: FUNC
310  * @tc.require: AR000EEMQ9
311  */
312 HWTEST_F(RectTest, RectSetHeight_001, TestSize.Level0)
313 {
314     if (rect_ == nullptr) {
315         EXPECT_EQ(1, 0);
316         return;
317     }
318     rect_->SetHeight(TEST_VALUE);
319     EXPECT_EQ(rect_->GetBottom(), TEST_VALUE - 1);
320 }
321 
322 /**
323  * @tc.name: RectSetLeft_001
324  * @tc.desc: Verify SetLeft function, equal.
325  * @tc.type: FUNC
326  * @tc.require: AR000EEMQ9
327  */
328 HWTEST_F(RectTest, RectSetLeft_001, TestSize.Level0)
329 {
330     if (rect_ == nullptr) {
331         EXPECT_EQ(1, 0);
332         return;
333     }
334     rect_->SetLeft(TEST_VALUE);
335     EXPECT_EQ(rect_->GetLeft(), TEST_VALUE);
336 }
337 
338 /**
339  * @tc.name: RectSetTop_001
340  * @tc.desc: Verify SetTop function, equal.
341  * @tc.type: FUNC
342  * @tc.require: AR000EEMQ9
343  */
344 HWTEST_F(RectTest, RectSetTop_001, TestSize.Level0)
345 {
346     if (rect_ == nullptr) {
347         EXPECT_EQ(1, 0);
348         return;
349     }
350     rect_->SetTop(TEST_VALUE);
351     EXPECT_EQ(rect_->GetTop(), TEST_VALUE);
352 }
353 
354 /**
355  * @tc.name: RectSetRight_001
356  * @tc.desc: Verify SetRight function, equal.
357  * @tc.type: FUNC
358  * @tc.require: AR000EEMQ9
359  */
360 HWTEST_F(RectTest, RectSetRight_001, TestSize.Level0)
361 {
362     if (rect_ == nullptr) {
363         EXPECT_EQ(1, 0);
364         return;
365     }
366     rect_->SetRight(TEST_VALUE);
367     EXPECT_EQ(rect_->GetRight(), TEST_VALUE);
368 }
369 
370 /**
371  * @tc.name: RectSetBottom_001
372  * @tc.desc: Verify SetBottom function, equal.
373  * @tc.type: FUNC
374  * @tc.require: AR000EEMQ9
375  */
376 HWTEST_F(RectTest, RectSetBottom_001, TestSize.Level0)
377 {
378     if (rect_ == nullptr) {
379         EXPECT_EQ(1, 0);
380         return;
381     }
382     rect_->SetBottom(TEST_VALUE);
383     EXPECT_EQ(rect_->GetBottom(), TEST_VALUE);
384 }
385 
386 /**
387  * @tc.name: RectResize_001
388  * @tc.desc: Verify Resize function, equal.
389  * @tc.type: FUNC
390  * @tc.require: AR000EEMQ9
391  */
392 HWTEST_F(RectTest, RectResize_001, TestSize.Level0)
393 {
394     if (rect_ == nullptr) {
395         EXPECT_EQ(1, 0);
396         return;
397     }
398     rect_->Resize(TEST_VALUE, TEST_VALUE);
399 
400     EXPECT_EQ(rect_->GetLeft(), 0);
401     EXPECT_EQ(rect_->GetTop(), 0);
402     EXPECT_EQ(rect_->GetRight(), TEST_VALUE - 1);
403     EXPECT_EQ(rect_->GetBottom(), TEST_VALUE - 1);
404 }
405 
406 /**
407  * @tc.name: RectGetSize_001
408  * @tc.desc: Verify GetSize function, equal.
409  * @tc.type: FUNC
410  * @tc.require: AR000EEMQ9
411  */
412 HWTEST_F(RectTest, RectGetSize_001, TestSize.Level0)
413 {
414     if (rect_ == nullptr) {
415         EXPECT_EQ(1, 0);
416         return;
417     }
418     EXPECT_EQ(rect_->GetSize(), 1);
419 }
420 
421 /**
422  * @tc.name: RectIntersect_001
423  * @tc.desc: Verify Intersect function, equal.
424  * @tc.type: FUNC
425  * @tc.require: AR000EEMQ9
426  */
427 HWTEST_F(RectTest, RectIntersect_001, TestSize.Level0)
428 {
429     Rect* rect1 = new Rect(TEST_VALUE, TEST_VALUE, MAX_VALUE, MAX_VALUE);
430     if (rect1 == nullptr) {
431         EXPECT_EQ(1, 0);
432         return;
433     }
434     Rect* rect2 = new Rect(0, 0, MAX_VALUE, MAX_VALUE);
435     if (rect2 == nullptr) {
436         delete rect1;
437         EXPECT_EQ(1, 0);
438         return;
439     }
440     Rect* rect3 = new Rect(0, 0, MIN_VALUE, 0);
441     if (rect3 == nullptr) {
442         delete rect1;
443         delete rect2;
444         EXPECT_EQ(1, 0);
445         return;
446     }
447 
448     EXPECT_EQ(rect1->Intersect(*rect1, *rect2), true);
449     EXPECT_EQ(rect1->Intersect(*rect1, *rect3), false);
450 
451     delete rect1;
452     delete rect2;
453     delete rect3;
454 }
455 
456 /**
457  * @tc.name: RectJoin_001
458  * @tc.desc: Verify Join function, equal.
459  * @tc.type: FUNC
460  * @tc.require: AR000EEMQ9
461  */
462 HWTEST_F(RectTest, RectJoin_001, TestSize.Level0)
463 {
464     Rect* rect1 = new Rect(TEST_VALUE, TEST_VALUE, TEST_VALUE, TEST_VALUE);
465     if (rect1 == nullptr) {
466         EXPECT_EQ(1, 0);
467         return;
468     }
469     Rect* rect2 = new Rect(MIN_VALUE, MIN_VALUE, MAX_VALUE, MAX_VALUE);
470     if (rect2 == nullptr) {
471         delete rect1;
472         EXPECT_EQ(1, 0);
473         return;
474     }
475 
476     rect1->Join(*rect1, *rect2);
477     EXPECT_EQ(rect1->GetLeft(), MIN_VALUE);
478     EXPECT_EQ(rect1->GetTop(), MIN_VALUE);
479     EXPECT_EQ(rect1->GetRight(), MAX_VALUE);
480     EXPECT_EQ(rect1->GetBottom(), MAX_VALUE);
481 
482     delete rect1;
483     delete rect2;
484 }
485 
486 /**
487  * @tc.name: RectIsContains_001
488  * @tc.desc: Verify IsContains function, equal.
489  * @tc.type: FUNC
490  * @tc.require: AR000EEMQ9
491  */
492 HWTEST_F(RectTest, RectIsContains_001, TestSize.Level0)
493 {
494     Rect* rect1 = new Rect(MIN_VALUE, MIN_VALUE, MAX_VALUE, MAX_VALUE);
495     if (rect1 == nullptr) {
496         EXPECT_EQ(1, 0);
497         return;
498     }
499     Vector2<int16_t> pt1 = {TEST_VALUE, TEST_VALUE};
500     Vector2<int16_t> pt2 = {0, 0};
501 
502     EXPECT_EQ(rect1->IsContains(pt1), true);
503     EXPECT_EQ(rect1->IsContains(pt2), false);
504 
505     delete rect1;
506 }
507 
508 /**
509  * @tc.name: RectIsContains_002
510  * @tc.desc: Verify IsContains function, equal.
511  * @tc.type: FUNC
512  * @tc.require: AR000EEMQ9
513  */
514 HWTEST_F(RectTest, RectIsContains_002, TestSize.Level0)
515 {
516     Rect* rect1 = new Rect(MIN_VALUE, MIN_VALUE, MAX_VALUE, MAX_VALUE);
517     if (rect1 == nullptr) {
518         EXPECT_EQ(1, 0);
519         return;
520     }
521     Point pt1;
522     pt1.x = TEST_VALUE;
523     pt1.y = TEST_VALUE;
524     Point pt2;
525     pt2.x = 0;
526     pt2.y = 0;
527 
528     EXPECT_EQ(rect1->IsContains(pt1), true);
529     EXPECT_EQ(rect1->IsContains(pt2), false);
530 
531     delete rect1;
532 }
533 
534 /**
535  * @tc.name: RectIsIntersect_001
536  * @tc.desc: Verify IsIntersect function, equal.
537  * @tc.type: FUNC
538  * @tc.require: AR000EEMQ9
539  */
540 HWTEST_F(RectTest, RectIsIntersect_001, TestSize.Level0)
541 {
542     Rect* rect1 = new Rect(MIN_VALUE, MIN_VALUE, MAX_VALUE, MAX_VALUE);
543     if (rect1 == nullptr) {
544         EXPECT_EQ(1, 0);
545         return;
546     }
547     Rect* rect2 = new Rect(MAX_VALUE, MAX_VALUE, MIN_VALUE, MIN_VALUE);
548     if (rect2 == nullptr) {
549         delete rect1;
550         EXPECT_EQ(1, 0);
551         return;
552     }
553     Rect* rect3 = new Rect();
554     if (rect3 == nullptr) {
555         delete rect1;
556         delete rect2;
557         EXPECT_EQ(1, 0);
558         return;
559     }
560 
561     EXPECT_EQ(rect1->IsIntersect(*rect2), true);
562     EXPECT_EQ(rect1->IsIntersect(*rect3), false);
563 
564     delete rect1;
565     delete rect2;
566     delete rect3;
567 }
568 
569 /**
570  * @tc.name: RectIsContains_003
571  * @tc.desc: Verify IsContains function, equal.
572  * @tc.type: FUNC
573  * @tc.require: AR000EEMQ9
574  */
575 HWTEST_F(RectTest, RectIsContains_003, TestSize.Level0)
576 {
577     Rect* rect1 = new Rect(MIN_VALUE, MIN_VALUE, MAX_VALUE, MAX_VALUE);
578     if (rect1 == nullptr) {
579         EXPECT_EQ(1, 0);
580         return;
581     }
582     Rect* rect2 = new Rect(TEST_VALUE, TEST_VALUE, TEST_VALUE, TEST_VALUE);
583     if (rect2 == nullptr) {
584         delete rect1;
585         EXPECT_EQ(1, 0);
586         return;
587     }
588     EXPECT_EQ(rect2->IsContains(*rect1), false);
589 
590     delete rect1;
591     delete rect2;
592 }
593 } // namespace OHOS
594