1 /*
2  * Copyright (c) 2022-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_brush.h"
19 #include "drawing_color.h"
20 #include "drawing_error_code.h"
21 #include "drawing_filter.h"
22 #include "drawing_mask_filter.h"
23 #include "drawing_rect.h"
24 #include "drawing_shadow_layer.h"
25 #include "drawing_color_filter.h"
26 #include "effect/color_filter.h"
27 #include "effect/filter.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace Rosen {
34 namespace Drawing {
35 class NativeDrawingBrushTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41 };
42 
SetUpTestCase()43 void NativeDrawingBrushTest::SetUpTestCase() {}
TearDownTestCase()44 void NativeDrawingBrushTest::TearDownTestCase() {}
SetUp()45 void NativeDrawingBrushTest::SetUp() {}
TearDown()46 void NativeDrawingBrushTest::TearDown() {}
47 
CastToFilter(OH_Drawing_Filter * cFilter)48 static Filter* CastToFilter(OH_Drawing_Filter* cFilter)
49 {
50     return reinterpret_cast<Filter*>(cFilter);
51 }
52 
53 /*
54  * @tc.name: NativeDrawingBrushTest_brushCreate001
55  * @tc.desc: test for create brush and destroy brush.
56  * @tc.type: FUNC
57  * @tc.require: AR000GTO5R
58  */
59 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushCreate001, TestSize.Level1)
60 {
61     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
62     EXPECT_EQ(brush == nullptr, false);
63     OH_Drawing_BrushDestroy(brush);
64 }
65 
66 /*
67  * @tc.name: NativeDrawingBrushTest_brushSetColor002
68  * @tc.desc: test for the set methods of brush.
69  * @tc.type: FUNC
70  * @tc.require: AR000GTO5R
71  */
72 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetColor002, TestSize.Level1)
73 {
74     OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
75     OH_Drawing_BrushSetAntiAlias(brush1, false);
76     EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(brush1), false);
77     OH_Drawing_BrushSetAntiAlias(nullptr, false);
78     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
79     EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(nullptr), false);
80     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
81     OH_Drawing_BrushSetColor(brush1, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
82     EXPECT_EQ(OH_Drawing_BrushGetColor(brush1), 0xFFFF0000);
83     OH_Drawing_BrushSetColor(nullptr, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
84     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
85     EXPECT_EQ(OH_Drawing_BrushGetColor(nullptr), 0);
86     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
87     constexpr uint8_t alpha = 128;
88     OH_Drawing_BrushSetAlpha(brush1, alpha);
89     EXPECT_EQ(OH_Drawing_BrushGetAlpha(brush1), alpha);
90     OH_Drawing_BrushSetAlpha(nullptr, alpha);
91     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
92     EXPECT_EQ(OH_Drawing_BrushGetAlpha(nullptr), 0);
93     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
94 }
95 
96 /*
97  * @tc.name: NativeDrawingBrushTest_brushSetBlendMode003
98  * @tc.desc: test for SetBlendMode.
99  * @tc.type: FUNC
100  * @tc.require: AR000GTO5R
101  */
102 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetBlendMode003, TestSize.Level1)
103 {
104     OH_Drawing_BrushSetBlendMode(nullptr, OH_Drawing_BlendMode::BLEND_MODE_CLEAR);
105     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
106     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
107     EXPECT_NE(brush, nullptr);
108     OH_Drawing_BrushSetBlendMode(brush, OH_Drawing_BlendMode::BLEND_MODE_CLEAR);
109 }
110 
111 /*
112  * @tc.name: NativeDrawingBrushTest_brushReset004
113  * @tc.desc: test for the reset methods of brush.
114  * @tc.type: FUNC
115  * @tc.require: AR000GTO5R
116  */
117 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushReset004, TestSize.Level1)
118 {
119     OH_Drawing_Brush* brush1 = OH_Drawing_BrushCreate();
120     OH_Drawing_BrushSetAntiAlias(brush1, true);
121     OH_Drawing_BrushSetColor(brush1, OH_Drawing_ColorSetArgb(0x00, 0xFF, 0x00, 0xFF));
122     constexpr uint8_t alpha = 128;
123     OH_Drawing_BrushSetAlpha(brush1, alpha);
124 
125     OH_Drawing_BrushReset(brush1);
126     EXPECT_EQ(OH_Drawing_BrushIsAntiAlias(brush1), false);
127     EXPECT_EQ(OH_Drawing_BrushGetColor(brush1), 0xFF000000);
128     EXPECT_EQ(OH_Drawing_BrushGetAlpha(brush1), 0xFF);
129     OH_Drawing_BrushReset(nullptr);
130     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
131 
132     OH_Drawing_BrushDestroy(brush1);
133 }
134 
135 /*
136  * @tc.name: NativeDrawingBrushTest_brushGetFilter005
137  * @tc.desc: gets the filter from a brush.
138  * @tc.type: FUNC
139  * @tc.require: AR000GTO5R
140  */
141 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushGetFilter005, TestSize.Level1)
142 {
143     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
144     EXPECT_NE(brush, nullptr);
145     OH_Drawing_Filter* cFilter_ = OH_Drawing_FilterCreate();
146     EXPECT_NE(cFilter_, nullptr);
147     OH_Drawing_Filter* tmpFilter_ = OH_Drawing_FilterCreate();
148     EXPECT_NE(tmpFilter_, nullptr);
149 
150     OH_Drawing_ColorFilter* colorFilterTmp = OH_Drawing_ColorFilterCreateLinearToSrgbGamma();
151 
152     OH_Drawing_FilterSetColorFilter(cFilter_, nullptr);
153     OH_Drawing_FilterGetColorFilter(cFilter_, colorFilterTmp);
154     EXPECT_EQ((reinterpret_cast<ColorFilter*>(colorFilterTmp))->GetType(),
155         ColorFilter::FilterType::NO_TYPE);
156 
157     OH_Drawing_ColorFilter* cColorFilter_ = OH_Drawing_ColorFilterCreateBlendMode(0xFF0000FF, BLEND_MODE_COLOR);
158     OH_Drawing_FilterSetColorFilter(cFilter_, cColorFilter_);
159     OH_Drawing_BrushSetFilter(brush, cFilter_);
160     OH_Drawing_BrushGetFilter(brush, tmpFilter_);
161 
162     EXPECT_NE(CastToFilter(tmpFilter_)->GetColorFilter(), nullptr);
163     EXPECT_EQ(CastToFilter(tmpFilter_)->GetColorFilter()->GetType(), ColorFilter::FilterType::BLEND_MODE);
164 
165     OH_Drawing_BrushSetFilter(nullptr, cFilter_);
166     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
167     OH_Drawing_BrushGetFilter(nullptr, tmpFilter_);
168     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
169     OH_Drawing_BrushGetFilter(brush, nullptr);
170     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
171 
172     OH_Drawing_FilterDestroy(cFilter_);
173     OH_Drawing_FilterDestroy(tmpFilter_);
174     OH_Drawing_ColorFilterDestroy(cColorFilter_);
175     OH_Drawing_ColorFilterDestroy(colorFilterTmp);
176     OH_Drawing_BrushDestroy(brush);
177 }
178 
179 /*
180  * @tc.name: NativeDrawingBrushTest_brushSetShadowLayer006
181  * @tc.desc: test for the set methods of brush.
182  * @tc.type: FUNC
183  * @tc.require: AR000GTO5R
184  */
185 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushSetShadowLayer006, TestSize.Level1)
186 {
187     // blurRadius:-3.f, offset:(-3.f, 3.f), shadowColor:green
188     OH_Drawing_ShadowLayer* shadow = OH_Drawing_ShadowLayerCreate(-3.f, -3.f, 3.f, 0xFF00FF00);
189     EXPECT_EQ(shadow, nullptr);
190     OH_Drawing_ShadowLayerDestroy(nullptr);
191     // blurRadius:3.f, offset:(-3.f, 3.f), shadowColor:green
192     OH_Drawing_ShadowLayer* shadowLayer = OH_Drawing_ShadowLayerCreate(3.f, -3.f, 3.f, 0xFF00FF00);
193     EXPECT_NE(shadowLayer, nullptr);
194     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
195     EXPECT_NE(brush, nullptr);
196     OH_Drawing_BrushSetShadowLayer(nullptr, shadowLayer);
197     EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_INVALID_PARAMETER);
198     OH_Drawing_BrushSetShadowLayer(brush, nullptr);
199     OH_Drawing_BrushSetShadowLayer(brush, shadowLayer);
200     OH_Drawing_ShadowLayerDestroy(shadowLayer);
201     OH_Drawing_BrushDestroy(brush);
202 }
203 
204 /*
205  @tc.name: NativeDrawingBrushTest_brushCopy007
206  * @tc.desc: test for creates an copy object.
207  * @tc.type: FUNC
208  * @tc.require: AR000GTO5R
209  */
210 HWTEST_F(NativeDrawingBrushTest, NativeDrawingBrushTest_brushCopy007, TestSize.Level1)
211 {
212     OH_Drawing_Brush* brush = OH_Drawing_BrushCreate();
213     EXPECT_EQ(brush == nullptr, false);
214     OH_Drawing_Brush* brush_copy = OH_Drawing_BrushCopy(brush);
215     EXPECT_EQ(brush_copy == nullptr, false);
216     OH_Drawing_BrushDestroy(brush);
217     OH_Drawing_BrushDestroy(brush_copy);
218 }
219 } // namespace Drawing
220 } // namespace Rosen
221 } // namespace OHOS