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