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 EXPECT_FALSE(OH_Drawing_RegionContains(nullptr, 10, 20)); // 10: point's x, 20 point's y
89 OH_Drawing_RegionDestroy(region);
90 OH_Drawing_RectDestroy(rect);
91 }
92
93 /*
94 * @tc.name: NativeDrawingRegionOpTest_region004
95 * @tc.desc: test for combines two regions.
96 * @tc.type: FUNC
97 * @tc.require: AR000GTO5R
98 */
99 HWTEST_F(NativeDrawingRegionTest, NativeDrawingRegionOpTest_region004, TestSize.Level1)
100 {
101 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
102 // rect left[100.0f], top[100.0f],right[256.0f], bottom[256.0f]
103 OH_Drawing_Rect* rect = OH_Drawing_RectCreate(100.0f, 100.0f, 256.0f, 256.0f);
104 OH_Drawing_RegionSetRect(region, rect);
105 OH_Drawing_Region* region2 = OH_Drawing_RegionCreate();
106 // rect left[150.0f], top[180.0f],right[200.0f], bottom[250.0f]
107 OH_Drawing_Rect* rect2 = OH_Drawing_RectCreate(150.0f, 180.0f, 200.0f, 250.0f);
108 OH_Drawing_RegionSetRect(region2, rect2);
109 EXPECT_TRUE(OH_Drawing_RegionOp(region, region2, OH_Drawing_RegionOpMode::REGION_OP_MODE_INTERSECT));
110
111 // rect left[10.0f], top[10.0f],right[26.0f], bottom[26.0f]
112 OH_Drawing_Rect* rect3 = OH_Drawing_RectCreate(10.0f, 10.0f, 26.0f, 26.0f);
113 OH_Drawing_RegionSetRect(region2, rect3);
114 EXPECT_FALSE(OH_Drawing_RegionOp(region, region2, OH_Drawing_RegionOpMode::REGION_OP_MODE_INTERSECT));
115 EXPECT_FALSE(OH_Drawing_RegionOp(nullptr, region2, OH_Drawing_RegionOpMode::REGION_OP_MODE_INTERSECT));
116 EXPECT_FALSE(OH_Drawing_RegionOp(region, nullptr, OH_Drawing_RegionOpMode::REGION_OP_MODE_INTERSECT));
117 EXPECT_FALSE(OH_Drawing_RegionOp(region, region2, static_cast<OH_Drawing_RegionOpMode>(-1))); // illegal input
118 EXPECT_FALSE(OH_Drawing_RegionOp(region, region2, static_cast<OH_Drawing_RegionOpMode>(99))); // illegal input
119
120 OH_Drawing_RegionDestroy(region);
121 OH_Drawing_RegionDestroy(region2);
122 OH_Drawing_RegionDestroy(nullptr);
123 OH_Drawing_RectDestroy(rect);
124 OH_Drawing_RectDestroy(rect2);
125 OH_Drawing_RectDestroy(rect3);
126 }
127
128 /*
129 * @tc.name: NativeDrawingSetPathTest_region005
130 * @tc.desc: test for combines two regions.
131 * @tc.type: FUNC
132 * @tc.require: AR000GTO5R
133 */
134 HWTEST_F(NativeDrawingRegionTest, NativeDrawingSetPathTest_region005, TestSize.Level1)
135 {
136 OH_Drawing_Path* path = OH_Drawing_PathCreate();
137 OH_Drawing_Region* region = OH_Drawing_RegionCreate();
138 OH_Drawing_Region* clip = OH_Drawing_RegionCreate();
139 // rect left[150.0f], top[180.0f],right[200.0f], bottom[200.0f]
140 OH_Drawing_Rect* rect = OH_Drawing_RectCreate(150.0f, 180.0f, 200.0f, 200.0f);
141 OH_Drawing_RegionSetRect(clip, rect);
142 // rect left[100.0f], top[100.0f],right[256.0f], bottom[256.0f]
143 OH_Drawing_PathAddRect(path, 100.0f, 100.0f, 256.0f, 256.0f, OH_Drawing_PathDirection::PATH_DIRECTION_CW);
144 EXPECT_FALSE(OH_Drawing_RegionSetPath(nullptr, path, clip));
145 EXPECT_FALSE(OH_Drawing_RegionSetPath(region, nullptr, clip));
146 EXPECT_FALSE(OH_Drawing_RegionSetPath(region, path, nullptr));
147 EXPECT_TRUE(OH_Drawing_RegionSetPath(region, path, clip));
148
149 OH_Drawing_RegionDestroy(region);
150 OH_Drawing_RegionDestroy(clip);
151 OH_Drawing_PathDestroy(path);
152 OH_Drawing_RectDestroy(rect);
153 }
154 } // namespace Drawing
155 } // namespace Rosen
156 } // namespace OHOS