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_error_code.h"
19 #include "drawing_rect.h"
20 #include "drawing_region.h"
21 #include "drawing_path.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29 class NativeDrawingRegionTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
34 void TearDown() override;
35 };
36
SetUpTestCase()37 void NativeDrawingRegionTest::SetUpTestCase() {}
TearDownTestCase()38 void NativeDrawingRegionTest::TearDownTestCase() {}
SetUp()39 void NativeDrawingRegionTest::SetUp() {}
TearDown()40 void NativeDrawingRegionTest::TearDown() {}
41
42 /*
43 * @tc.name: NativeDrawingRegionTest_region001
44 * @tc.desc: test for create drawing_region.
45 * @tc.type: FUNC
46 * @tc.require: AR000GTO5R
47 */
48 HWTEST_F(NativeDrawingRegionTest, NativeDrawingRegionTest_region001, TestSize.Level1)
49 {
50 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
51 EXPECT_NE(region, nullptr);
52 OH_Drawing_RegionDestroy(region);
53 }
54
55 /*
56 * @tc.name: NativeDrawingRegionSetRectTest_region002
57 * @tc.desc: test for constructs a rectangular Region matching the bounds of rect.
58 * @tc.type: FUNC
59 * @tc.require: AR000GTO5R
60 */
61 HWTEST_F(NativeDrawingRegionTest, NativeDrawingRegionSetRectTest_region002, TestSize.Level1)
62 {
63 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
64 OH_Drawing_Rect* rect=OH_Drawing_RectCreate(0.0f, 0.0f, 256.0f, 256.0f);
65 EXPECT_TRUE(OH_Drawing_RegionSetRect(region, rect));
66 OH_Drawing_RegionSetRect(nullptr, rect);
67 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
68 OH_Drawing_RegionSetRect(region, nullptr);
69 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
70 OH_Drawing_RegionDestroy(region);
71 OH_Drawing_RectDestroy(rect);
72 }
73
74 /*
75 * @tc.name: NativeDrawingRegionContainsTest_region003
76 * @tc.desc: test for determines whether the region contains the specified coordinates.
77 * @tc.type: FUNC
78 * @tc.require: AR000GTO5R
79 */
80 HWTEST_F(NativeDrawingRegionTest, NativeDrawingRegionContainsTest_region003, TestSize.Level1)
81 {
82 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
83 // rect left[100.0f], top[100.0f],right[256.0f], bottom[256.0f]
84 OH_Drawing_Rect* rect = OH_Drawing_RectCreate(100.0f, 100.0f, 256.0f, 256.0f);
85 OH_Drawing_RegionSetRect(region, rect);
86 EXPECT_TRUE(OH_Drawing_RegionContains(region, 150, 180)); // 150: point's x, 180 point's y
87 EXPECT_FALSE(OH_Drawing_RegionContains(region, 10, 20)); // 10: point's x, 20 point's y
88 OH_Drawing_RegionDestroy(region);
89 OH_Drawing_RectDestroy(rect);
90 }
91
92 /*
93 * @tc.name: NativeDrawingRegionOpTest_region004
94 * @tc.desc: test for combines two regions.
95 * @tc.type: FUNC
96 * @tc.require: AR000GTO5R
97 */
98 HWTEST_F(NativeDrawingRegionTest, NativeDrawingRegionOpTest_region004, TestSize.Level1)
99 {
100 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
101 // rect left[100.0f], top[100.0f],right[256.0f], bottom[256.0f]
102 OH_Drawing_Rect* rect = OH_Drawing_RectCreate(100.0f, 100.0f, 256.0f, 256.0f);
103 OH_Drawing_RegionSetRect(region, rect);
104 OH_Drawing_Region* region2 = OH_Drawing_RegionCreate();
105 // rect left[150.0f], top[180.0f],right[200.0f], bottom[250.0f]
106 OH_Drawing_Rect* rect2 = OH_Drawing_RectCreate(150.0f, 180.0f, 200.0f, 250.0f);
107 OH_Drawing_RegionSetRect(region2, rect2);
108 EXPECT_TRUE(OH_Drawing_RegionOp(region, region2, OH_Drawing_RegionOpMode::REGION_OP_MODE_INTERSECT));
109
110 // rect left[10.0f], top[10.0f],right[26.0f], bottom[26.0f]
111 OH_Drawing_Rect* rect3 = OH_Drawing_RectCreate(10.0f, 10.0f, 26.0f, 26.0f);
112 OH_Drawing_RegionSetRect(region2, rect3);
113 EXPECT_FALSE(OH_Drawing_RegionOp(region, region2, OH_Drawing_RegionOpMode::REGION_OP_MODE_INTERSECT));
114
115 OH_Drawing_RegionDestroy(region);
116 OH_Drawing_RegionDestroy(region2);
117 OH_Drawing_RectDestroy(rect);
118 OH_Drawing_RectDestroy(rect2);
119 OH_Drawing_RectDestroy(rect3);
120 }
121
122 /*
123 * @tc.name: NativeDrawingSetPathTest_region005
124 * @tc.desc: test for combines two regions.
125 * @tc.type: FUNC
126 * @tc.require: AR000GTO5R
127 */
128 HWTEST_F(NativeDrawingRegionTest, NativeDrawingSetPathTest_region005, TestSize.Level1)
129 {
130 OH_Drawing_Path* path = OH_Drawing_PathCreate();
131 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
132 OH_Drawing_Region* clip = OH_Drawing_RegionCreate();
133 // rect left[150.0f], top[180.0f],right[200.0f], bottom[200.0f]
134 OH_Drawing_Rect* rect = OH_Drawing_RectCreate(150.0f, 180.0f, 200.0f, 200.0f);
135 OH_Drawing_RegionSetRect(clip, rect);
136 // rect left[100.0f], top[100.0f],right[256.0f], bottom[256.0f]
137 OH_Drawing_PathAddRect(path, 100.0f, 100.0f, 256.0f, 256.0f, OH_Drawing_PathDirection::PATH_DIRECTION_CW);
138 EXPECT_FALSE(OH_Drawing_RegionSetPath(region, path, nullptr));
139 EXPECT_TRUE(OH_Drawing_RegionSetPath(region, path, clip));
140
141 OH_Drawing_RegionDestroy(region);
142 OH_Drawing_RegionDestroy(clip);
143 OH_Drawing_PathDestroy(path);
144 OH_Drawing_RectDestroy(rect);
145 }
146 } // namespace Drawing
147 } // namespace Rosen
148 } // namespace OHOS