1 /*
2  * Copyright (c) 2024 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 "drawing_color.h"
19 #include "drawing_filter.h"
20 #include "drawing_mask_filter.h"
21 #include "drawing_rect.h"
22 #include "utils/scalar.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 namespace Drawing {
30 class NativeDrawingRectTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp() override;
35     void TearDown() override;
36 };
37 
SetUpTestCase()38 void NativeDrawingRectTest::SetUpTestCase() {}
TearDownTestCase()39 void NativeDrawingRectTest::TearDownTestCase() {}
SetUp()40 void NativeDrawingRectTest::SetUp() {}
TearDown()41 void NativeDrawingRectTest::TearDown() {}
42 
43 /*
44  * @tc.name: NativeDrawingRectTest_CreateAndDestroy001
45  * @tc.desc: test for create Rect and destroy Rect.
46  * @tc.size  : MediumTest
47  * @tc.type  : Function
48  * @tc.level : Level 1
49  */
HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_CreateAndDestroy001, TestSize.Level1)50 HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_CreateAndDestroy001, TestSize.Level1)
51 {
52     OH_Drawing_Rect *rect = OH_Drawing_RectCreate(100, 200, 500, 300);
53     EXPECT_NE(nullptr, rect);
54     OH_Drawing_RectDestroy(rect);
55 }
56 
57 /*
58  * @tc.name: NativeDrawingRectTest_Intersect002
59  * @tc.desc: test for the Intersect methods of Rect.
60  * @tc.size  : MediumTest
61  * @tc.type  : Function
62  * @tc.level : Level 1
63  */
HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_Intersect002, TestSize.Level1)64 HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_Intersect002, TestSize.Level1)
65 {
66     OH_Drawing_Rect *rect = OH_Drawing_RectCreate(100, 200, 500, 300);
67     EXPECT_NE(nullptr, rect);
68 
69     OH_Drawing_Rect *otherOne = OH_Drawing_RectCreate(300, 250, 600, 400);
70     EXPECT_NE(nullptr, otherOne);
71 
72     OH_Drawing_Rect *otherTwo = OH_Drawing_RectCreate(600, 400, 700, 500);
73     EXPECT_NE(nullptr, otherTwo);
74 
75     bool ret = OH_Drawing_RectIntersect(rect, otherOne);
76     EXPECT_EQ(ret, true);
77 
78     ret = OH_Drawing_RectIntersect(rect, otherTwo);
79     EXPECT_EQ(ret, false);
80 
81     OH_Drawing_RectDestroy(rect);
82     OH_Drawing_RectDestroy(otherOne);
83     OH_Drawing_RectDestroy(otherTwo);
84 }
85 /*
86  * @tc.name: NativeDrawingRectTest_GetHeight003
87  * @tc.desc: test for get height of rect.
88  * @tc.size  : MediumTest
89  * @tc.type  : Function
90  * @tc.level : Level 1
91  */
HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_GetHeight003, TestSize.Level1)92 HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_GetHeight003, TestSize.Level1)
93 {
94     OH_Drawing_Rect* rect = OH_Drawing_RectCreate(0, 0, 400, 800);
95     float height = OH_Drawing_RectGetHeight(rect);
96     EXPECT_TRUE(IsScalarAlmostEqual(height, 800)); // 800 means height
97     OH_Drawing_RectDestroy(rect);
98 }
99 
100 /*
101  * @tc.name: NativeDrawingRectTest_GetWidth004
102  * @tc.desc: test for get width of rect.
103  * @tc.size  : MediumTest
104  * @tc.type  : Function
105  * @tc.level : Level 1
106  */
HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_GetWidth004, TestSize.Level1)107 HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_GetWidth004, TestSize.Level1)
108 {
109     OH_Drawing_Rect* rect = OH_Drawing_RectCreate(0, 0, 400, 800);
110     float width = OH_Drawing_RectGetWidth(rect);
111     EXPECT_TRUE(IsScalarAlmostEqual(width, 400)); // 400 means height
112     OH_Drawing_RectDestroy(rect);
113 }
114 
115 /*
116  * @tc.name: NativeDrawingRectTest_SetAndGet005
117  * @tc.desc: test for set and get of rect.
118  * @tc.size  : MediumTest
119  * @tc.type  : Function
120  * @tc.level : Level 1
121  */
HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_SetAndGet005, TestSize.Level1)122 HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_SetAndGet005, TestSize.Level1)
123 {
124     OH_Drawing_Rect* rect = OH_Drawing_RectCreate(0, 0, 400, 800);
125     OH_Drawing_RectSetLeft(rect, 10);
126     OH_Drawing_RectSetTop(rect, 10);
127     OH_Drawing_RectSetRight(rect, 300);
128     OH_Drawing_RectSetBottom(rect, 400);
129     float left = OH_Drawing_RectGetLeft(rect);
130     float top = OH_Drawing_RectGetTop(rect);
131     float right = OH_Drawing_RectGetRight(rect);
132     float bottom = OH_Drawing_RectGetBottom(rect);
133     EXPECT_TRUE(IsScalarAlmostEqual(left, 10)); // 10 means left
134     EXPECT_TRUE(IsScalarAlmostEqual(top, 10)); // 10 means top
135     EXPECT_TRUE(IsScalarAlmostEqual(right, 300)); // 300 means right
136     EXPECT_TRUE(IsScalarAlmostEqual(bottom, 400)); // 400 means bottom
137     OH_Drawing_RectDestroy(rect);
138 }
139 
140 /*
141  * @tc.name: NativeDrawingRectTest_Copy006
142  * @tc.desc: test for Copy of rect.
143  * @tc.size  : MediumTest
144  * @tc.type  : Function
145  * @tc.level : Level 1
146  */
HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_Copy006, TestSize.Level1)147 HWTEST_F(NativeDrawingRectTest, NativeDrawingRectTest_Copy006, TestSize.Level1)
148 {
149     OH_Drawing_Rect* rectSrc = OH_Drawing_RectCreate(0, 0, 400, 800);
150     OH_Drawing_Rect* rectDst = OH_Drawing_RectCreate(11, 22, 333, 444);
151     OH_Drawing_RectCopy(rectDst, rectSrc);
152     float left = OH_Drawing_RectGetLeft(rectSrc);
153     float top = OH_Drawing_RectGetTop(rectSrc);
154     float right = OH_Drawing_RectGetRight(rectSrc);
155     float bottom = OH_Drawing_RectGetBottom(rectSrc);
156     EXPECT_TRUE(IsScalarAlmostEqual(left, 11)); // 11 means left
157     EXPECT_TRUE(IsScalarAlmostEqual(top, 22)); // 22 means top
158     EXPECT_TRUE(IsScalarAlmostEqual(right, 333)); // 333 means right
159     EXPECT_TRUE(IsScalarAlmostEqual(bottom, 444)); // 444 means bottom
160     OH_Drawing_RectDestroy(rectSrc);
161     OH_Drawing_RectDestroy(rectDst);
162 }
163 } // namespace Drawing
164 } // namespace Rosen
165 } // namespace OHOS