1 /*
2 * Copyright (c) 2022-2025 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_brush.h"
19 #include "drawing_color.h"
20 #include "drawing_error_code.h"
21 #include "drawing_filter.h"
22 #include "drawing_helper.h"
23 #include "drawing_mask_filter.h"
24 #include "drawing_rect.h"
25 #include "drawing_shadow_layer.h"
26 #include "drawing_color_filter.h"
27 #include "effect/color_filter.h"
28 #include "effect/filter.h"
29 #include "native_color_space_manager.h"
30
31 #ifdef RS_ENABLE_VK
32 #include "platform/ohos/backend/rs_vulkan_context.h"
33 #endif
34
35 using namespace testing;
36 using namespace testing::ext;
37
38 namespace OHOS {
39 namespace Rosen {
40 namespace Drawing {
41 class NativeDrawingBrushTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp() override;
46 void TearDown() override;
47 };
48
SetUpTestCase()49 void NativeDrawingBrushTest::SetUpTestCase()
50 {
51 #ifdef RS_ENABLE_VK
52 RsVulkanContext::SetRecyclable(false);
53 #endif
54 }
TearDownTestCase()55 void NativeDrawingBrushTest::TearDownTestCase() {}
SetUp()56 void NativeDrawingBrushTest::SetUp() {}
TearDown()57 void NativeDrawingBrushTest::TearDown() {}
58
CastToFilter(OH_Drawing_Filter * cFilter)59 static Filter* CastToFilter(OH_Drawing_Filter* cFilter)
60 {
61 return reinterpret_cast<Filter*>(cFilter);
62 }
63
64 /*
65 * @tc.name: NativeDrawingBrushTest_brushCreate001
66 * @tc.desc: test for create brush and destroy brush.
67 * @tc.type: FUNC
68 * @tc.require: AR000GTO5R
69 */
70 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushCreate001, TestSize.Level1)
71 {
72 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
73 EXPECT_EQ(brush == nullptr, false);
74 OH_Drawing_BrushDestroy(brush);
75 }
76
77 /*
78 * @tc.name: NativeDrawingBrushTest_brushSetColor002
79 * @tc.desc: test for the set methods of brush.
80 * @tc.type: FUNC
81 * @tc.require: AR000GTO5R
82 */
83 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetColor002, TestSize.Level1)
84 {
85 OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
86 OH_Drawing_BrushSetAntiAlias(brush1, false);
87 EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(brush1), false);
88 OH_Drawing_BrushSetAntiAlias(nullptr, false);
89 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
90 EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(nullptr), false);
91 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
92 OH_Drawing_BrushSetColor(brush1, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
93 EXPECT_EQ(OH_Drawing_BrushGetColor(brush1), 0xFFFF0000);
94 OH_Drawing_BrushSetColor(nullptr, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
95 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
96 EXPECT_EQ(OH_Drawing_BrushGetColor(nullptr), 0);
97 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
98 constexpr uint8_t alpha = 128;
99 OH_Drawing_BrushSetAlpha(brush1, alpha);
100 EXPECT_EQ(OH_Drawing_BrushGetAlpha(brush1), alpha);
101 OH_Drawing_BrushSetAlpha(nullptr, alpha);
102 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
103 EXPECT_EQ(OH_Drawing_BrushGetAlpha(nullptr), 0);
104 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
105 }
106
107 /*
108 * @tc.name: NativeDrawingBrushTest_brushSetBlendMode003
109 * @tc.desc: test for SetBlendMode.
110 * @tc.type: FUNC
111 * @tc.require: AR000GTO5R
112 */
113 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetBlendMode003, TestSize.Level1)
114 {
115 OH_Drawing_BrushSetBlendMode(nullptr, OH_Drawing_BlendMode::BLEND_MODE_CLEAR);
116 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
117 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
118 EXPECT_NE(brush, nullptr);
119 OH_Drawing_BrushSetBlendMode(brush, OH_Drawing_BlendMode::BLEND_MODE_CLEAR);
120 }
121
122 /*
123 * @tc.name: NativeDrawingBrushTest_brushReset004
124 * @tc.desc: test for the reset methods of brush.
125 * @tc.type: FUNC
126 * @tc.require: AR000GTO5R
127 */
128 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushReset004, TestSize.Level1)
129 {
130 OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
131 OH_Drawing_BrushSetAntiAlias(brush1, true);
132 OH_Drawing_BrushSetColor(brush1, OH_Drawing_ColorSetArgb(0x00, 0xFF, 0x00, 0xFF));
133 constexpr uint8_t alpha = 128;
134 OH_Drawing_BrushSetAlpha(brush1, alpha);
135
136 OH_Drawing_BrushReset(brush1);
137 EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(brush1), false);
138 EXPECT_EQ(OH_Drawing_BrushGetColor(brush1), 0xFF000000);
139 EXPECT_EQ(OH_Drawing_BrushGetAlpha(brush1), 0xFF);
140 OH_Drawing_BrushReset(nullptr);
141 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
142
143 OH_Drawing_BrushDestroy(brush1);
144 }
145
146 /*
147 * @tc.name: NativeDrawingBrushTest_brushGetFilter005
148 * @tc.desc: gets the filter from a brush.
149 * @tc.type: FUNC
150 * @tc.require: AR000GTO5R
151 */
152 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushGetFilter005, TestSize.Level1)
153 {
154 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
155 EXPECT_NE(brush, nullptr);
156 OH_Drawing_Filter* cFilter_ = OH_Drawing_FilterCreate();
157 EXPECT_NE(cFilter_, nullptr);
158 OH_Drawing_Filter* tmpFilter_ = OH_Drawing_FilterCreate();
159 EXPECT_NE(tmpFilter_, nullptr);
160
161 OH_Drawing_ColorFilter* colorFilterTmp = OH_Drawing_ColorFilterCreateLinearToSrgbGamma();
162
163 OH_Drawing_FilterSetColorFilter(cFilter_, nullptr);
164 OH_Drawing_FilterGetColorFilter(cFilter_, colorFilterTmp);
165 NativeHandle<ColorFilter>* colorFilterHandle = Helper::CastTo<OH_Drawing_ColorFilter*,
166 NativeHandle<ColorFilter>*>(colorFilterTmp);
167 EXPECT_NE(colorFilterHandle, nullptr);
168 EXPECT_NE(colorFilterHandle->value, nullptr);
169 EXPECT_EQ(colorFilterHandle->value->GetType(), ColorFilter::FilterType::NO_TYPE);
170
171 OH_Drawing_ColorFilter* cColorFilter_ = OH_Drawing_ColorFilterCreateBlendMode(0xFF0000FF, BLEND_MODE_COLOR);
172 OH_Drawing_FilterSetColorFilter(cFilter_, cColorFilter_);
173 OH_Drawing_BrushSetFilter(brush, cFilter_);
174 OH_Drawing_BrushGetFilter(brush, tmpFilter_);
175
176 EXPECT_NE(CastToFilter(tmpFilter_)->GetColorFilter(), nullptr);
177 EXPECT_EQ(CastToFilter(tmpFilter_)->GetColorFilter()->GetType(), ColorFilter::FilterType::BLEND_MODE);
178
179 OH_Drawing_BrushSetFilter(nullptr, cFilter_);
180 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
181 OH_Drawing_BrushGetFilter(nullptr, tmpFilter_);
182 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
183 OH_Drawing_BrushGetFilter(brush, nullptr);
184 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
185
186 OH_Drawing_FilterDestroy(cFilter_);
187 OH_Drawing_FilterDestroy(tmpFilter_);
188 OH_Drawing_ColorFilterDestroy(cColorFilter_);
189 OH_Drawing_ColorFilterDestroy(colorFilterTmp);
190 OH_Drawing_BrushDestroy(brush);
191 }
192
193 /*
194 * @tc.name: NativeDrawingBrushTest_brushSetShadowLayer006
195 * @tc.desc: test for the set methods of brush.
196 * @tc.type: FUNC
197 * @tc.require: AR000GTO5R
198 */
199 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetShadowLayer006, TestSize.Level1)
200 {
201 // blurRadius:-3.f, offset:(-3.f, 3.f), shadowColor:green
202 OH_Drawing_ShadowLayer* shadow = OH_Drawing_ShadowLayerCreate(-3.f, -3.f, 3.f, 0xFF00FF00);
203 EXPECT_EQ(shadow, nullptr);
204 OH_Drawing_ShadowLayerDestroy(nullptr);
205 // blurRadius:3.f, offset:(-3.f, 3.f), shadowColor:green
206 OH_Drawing_ShadowLayer* shadowLayer = OH_Drawing_ShadowLayerCreate(3.f, -3.f, 3.f, 0xFF00FF00);
207 EXPECT_NE(shadowLayer, nullptr);
208 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
209 EXPECT_NE(brush, nullptr);
210 OH_Drawing_BrushSetShadowLayer(nullptr, shadowLayer);
211 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
212 OH_Drawing_BrushSetShadowLayer(brush, nullptr);
213 OH_Drawing_BrushSetShadowLayer(brush, shadowLayer);
214 OH_Drawing_ShadowLayerDestroy(shadowLayer);
215 OH_Drawing_BrushDestroy(brush);
216 }
217
218 /*
219 @tc.name: NativeDrawingBrushTest_brushCopy007
220 * @tc.desc: test for creates an copy object.
221 * @tc.type: FUNC
222 * @tc.require: AR000GTO5R
223 */
224 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushCopy007, TestSize.Level1)
225 {
226 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
227 EXPECT_EQ(brush == nullptr, false);
228 OH_Drawing_Brush* brush_copy = OH_Drawing_BrushCopy(brush);
229 EXPECT_EQ(brush_copy == nullptr, false);
230 OH_Drawing_BrushDestroy(brush);
231 OH_Drawing_BrushDestroy(brush_copy);
232 }
233
234 /*
235 * @tc.name: NativeDrawingBrushTest_BrushSetColor4f
236 * @tc.desc: test for OH_Drawing_BrushSetColor4f.
237 * @tc.type: FUNC
238 * @tc.require: IC9835
239 */
240 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_BrushSetColor4f, TestSize.Level1)
241 {
242 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
243 EXPECT_NE(brush, nullptr);
244 OH_NativeColorSpaceManager* colorSpaceManager =
245 OH_NativeColorSpaceManager_CreateFromName(ColorSpaceName::ADOBE_RGB);
246 EXPECT_EQ(OH_Drawing_BrushSetColor4f(nullptr, 0.2f, 0.3f, 0.4f, 0.1f, colorSpaceManager),
247 OH_DRAWING_ERROR_INVALID_PARAMETER);
248 EXPECT_EQ(OH_Drawing_BrushSetColor4f(brush, 0.2f, 0.3f, 0.4f, 0.1f, colorSpaceManager), OH_DRAWING_SUCCESS);
249 EXPECT_EQ(OH_Drawing_BrushSetColor4f(brush, 0.2f, 0.3f, 0.4f, 0.1f, nullptr), OH_DRAWING_SUCCESS);
250 OH_Drawing_BrushDestroy(brush);
251 OH_NativeColorSpaceManager_Destroy(colorSpaceManager);
252 }
253
254 /*
255 * @tc.name: NativeDrawingBrushTest_BrushSetColor4f001
256 * @tc.desc: test for OH_Drawing_BrushSetColor4f.
257 * @tc.type: FUNC
258 * @tc.require: IC9835
259 */
260 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_BrushSetColor4f001, TestSize.Level1)
261 {
262 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
263 EXPECT_NE(brush, nullptr);
264 OH_NativeColorSpaceManager* colorSpaceManager =
265 OH_NativeColorSpaceManager_CreateFromName(ColorSpaceName::ADOBE_RGB);
266 EXPECT_EQ(OH_Drawing_BrushSetColor4f(brush, 1.01f, 1.1f, 1.5f, 1.4f, colorSpaceManager),
267 OH_DRAWING_SUCCESS);
268 float a = 0;
269 float r = 0;
270 float g = 0;
271 float b = 0;
272 EXPECT_EQ(OH_Drawing_BrushGetAlphaFloat(brush, &a), OH_DRAWING_SUCCESS);
273 EXPECT_EQ(OH_Drawing_BrushGetRedFloat(brush, &r), OH_DRAWING_SUCCESS);
274 EXPECT_EQ(OH_Drawing_BrushGetGreenFloat(brush, &g), OH_DRAWING_SUCCESS);
275 EXPECT_EQ(OH_Drawing_BrushGetBlueFloat(brush, &b), OH_DRAWING_SUCCESS);
276 EXPECT_TRUE(std::abs(a - 1.0f) < 0.01);
277 EXPECT_TRUE(std::abs(r - 1.0f) < 0.01);
278 EXPECT_TRUE(std::abs(g - 1.0f) < 0.01);
279 EXPECT_TRUE(std::abs(b - 1.0f) < 0.01);
280 EXPECT_EQ(OH_Drawing_BrushSetColor4f(brush, -0.1f, -1.1f, -1.5f, -1.4f, colorSpaceManager),
281 OH_DRAWING_SUCCESS);
282 EXPECT_EQ(OH_Drawing_BrushGetAlphaFloat(brush, &a), OH_DRAWING_SUCCESS);
283 EXPECT_EQ(OH_Drawing_BrushGetRedFloat(brush, &r), OH_DRAWING_SUCCESS);
284 EXPECT_EQ(OH_Drawing_BrushGetGreenFloat(brush, &g), OH_DRAWING_SUCCESS);
285 EXPECT_EQ(OH_Drawing_BrushGetBlueFloat(brush, &b), OH_DRAWING_SUCCESS);
286 EXPECT_TRUE(std::abs(a) < 0.01);
287 EXPECT_TRUE(std::abs(r) < 0.01);
288 EXPECT_TRUE(std::abs(g) < 0.01);
289 EXPECT_TRUE(std::abs(b) < 0.01);
290 OH_Drawing_BrushDestroy(brush);
291 OH_NativeColorSpaceManager_Destroy(colorSpaceManager);
292 }
293
294 /*
295 @tc.name: NativeDrawingBrushTest_BrushGetAlphaFloat
296 * @tc.desc: test for OH_Drawing_BrushGetAlphaFloat.
297 * @tc.type: FUNC
298 * @tc.require: IC9835
299 */
300 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_BrushGetAlphaFloat, TestSize.Level1)
301 {
302 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
303 EXPECT_NE(brush, nullptr);
304 float a = 0.0f;
305 OH_NativeColorSpaceManager* colorSpaceManager = nullptr;
306 OH_Drawing_BrushSetColor4f(brush, 0.2f, 0.3f, 0.4f, 0.1f, colorSpaceManager);
307 EXPECT_EQ(OH_Drawing_BrushGetAlphaFloat(nullptr, &a), OH_DRAWING_ERROR_INVALID_PARAMETER);
308 EXPECT_EQ(OH_Drawing_BrushGetAlphaFloat(brush, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
309 EXPECT_EQ(OH_Drawing_BrushGetAlphaFloat(nullptr, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
310 EXPECT_EQ(OH_Drawing_BrushGetAlphaFloat(brush, &a), OH_DRAWING_SUCCESS);
311 EXPECT_TRUE(std::abs(a - 0.2f) < 0.01);
312 OH_Drawing_BrushDestroy(brush);
313 }
314
315 /*
316 @tc.name: NativeDrawingBrushTest_BrushGetRedFloat
317 * @tc.desc: test for OH_Drawing_BrushGetRedFloat.
318 * @tc.type: FUNC
319 * @tc.require: IC9835
320 */
321 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_BrushGetRedFloat, TestSize.Level1)
322 {
323 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
324 EXPECT_NE(brush, nullptr);
325 float r = 0.0f;
326 OH_NativeColorSpaceManager* colorSpaceManager = nullptr;
327 OH_Drawing_BrushSetColor4f(brush, 0.2f, 0.3f, 0.4f, 0.1f, colorSpaceManager);
328 EXPECT_EQ(OH_Drawing_BrushGetRedFloat(nullptr, &r), OH_DRAWING_ERROR_INVALID_PARAMETER);
329 EXPECT_EQ(OH_Drawing_BrushGetRedFloat(brush, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
330 EXPECT_EQ(OH_Drawing_BrushGetRedFloat(nullptr, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
331 EXPECT_EQ(OH_Drawing_BrushGetRedFloat(brush, &r), OH_DRAWING_SUCCESS);
332 EXPECT_TRUE(std::abs(r - 0.3f) < 0.01);
333 OH_Drawing_BrushDestroy(brush);
334 }
335
336 /*
337 @tc.name: NativeDrawingBrushTest_BrushGetGreenFloat
338 * @tc.desc: test for OH_Drawing_BrushGetGreenFloat.
339 * @tc.type: FUNC
340 * @tc.require: IC9835
341 */
342 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_BrushGetGreenFloat, TestSize.Level1)
343 {
344 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
345 EXPECT_NE(brush, nullptr);
346 float g = 0.0f;
347 OH_NativeColorSpaceManager* colorSpaceManager = nullptr;
348 OH_Drawing_BrushSetColor4f(brush, 0.2f, 0.3f, 0.4f, 0.1f, colorSpaceManager);
349 EXPECT_EQ(OH_Drawing_BrushGetGreenFloat(nullptr, &g), OH_DRAWING_ERROR_INVALID_PARAMETER);
350 EXPECT_EQ(OH_Drawing_BrushGetGreenFloat(brush, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
351 EXPECT_EQ(OH_Drawing_BrushGetGreenFloat(nullptr, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
352 EXPECT_EQ(OH_Drawing_BrushGetGreenFloat(brush, &g), OH_DRAWING_SUCCESS);
353 EXPECT_TRUE(std::abs(g - 0.4f) < 0.01);
354 OH_Drawing_BrushDestroy(brush);
355 }
356
357 /*
358 @tc.name: NativeDrawingBrushTest_BrushGetBlueFloat
359 * @tc.desc: test for OH_Drawing_BrushGetBlueFloat.
360 * @tc.type: FUNC
361 * @tc.require: IC9835
362 */
363 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_BrushGetBlueFloat, TestSize.Level1)
364 {
365 OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
366 EXPECT_NE(brush, nullptr);
367 float b = 0.2f;
368 OH_NativeColorSpaceManager* colorSpaceManager = nullptr;
369 OH_Drawing_BrushSetColor4f(brush, 0.2f, 0.3f, 0.4f, 0.1f, colorSpaceManager);
370 EXPECT_EQ(OH_Drawing_BrushGetBlueFloat(nullptr, &b), OH_DRAWING_ERROR_INVALID_PARAMETER);
371 EXPECT_EQ(OH_Drawing_BrushGetBlueFloat(brush, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
372 EXPECT_EQ(OH_Drawing_BrushGetBlueFloat(nullptr, nullptr), OH_DRAWING_ERROR_INVALID_PARAMETER);
373 EXPECT_EQ(OH_Drawing_BrushGetBlueFloat(brush, &b), OH_DRAWING_SUCCESS);
374 EXPECT_TRUE(std::abs(b - 0.1f) < 0.01);
375 OH_Drawing_BrushDestroy(brush);
376 }
377
378 } // namespace Drawing
379 } // namespace Rosen
380 } // namespace OHOS