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_color.h"
19#include "drawing_color_filter.h"
20#include "drawing_filter.h"
21#include "drawing_pen.h"
22#include "drawing_shadow_layer.h"
23#include "effect/color_filter.h"
24#include "effect/filter.h"
25
26using namespace testing;
27using namespace testing::ext;
28
29namespace OHOS {
30namespace Rosen {
31namespace Drawing {
32class NativeDrawingPenTest : public testing::Test {
33public:
34    static void SetUpTestCase();
35    static void TearDownTestCase();
36    void SetUp() override;
37    void TearDown() override;
38};
39
40void NativeDrawingPenTest::SetUpTestCase() {}
41void NativeDrawingPenTest::TearDownTestCase() {}
42void NativeDrawingPenTest::SetUp() {}
43void NativeDrawingPenTest::TearDown() {}
44static Filter* CastToFilter(OH_Drawing_Filter* cFilter)
45{
46    return reinterpret_cast<Filter*>(cFilter);
47}
48
49/*
50 * @tc.name: NativeDrawingPenTest_penCreate001
51 * @tc.desc: test for create drawing_pen.
52 * @tc.size  : MediumTest
53 * @tc.type  : Function
54 * @tc.level : Level 1
55 */
56HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penCreate001, TestSize.Level1)
57{
58    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
59    EXPECT_EQ(pen == nullptr, false);
60    OH_Drawing_PenDestroy(pen);
61}
62
63/*
64 * @tc.name: NativeDrawingPenTest_penSetAntiAlias002
65 * @tc.desc: test for the get and set methods about AntiAlias for a pen.
66 * @tc.size  : MediumTest
67 * @tc.type  : Function
68 * @tc.level : Level 1
69 */
70HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetAntiAlias002, TestSize.Level1)
71{
72    OH_Drawing_Pen* pen1 = OH_Drawing_PenCreate();
73    OH_Drawing_PenSetAntiAlias(pen1, true);
74    EXPECT_EQ(OH_Drawing_PenIsAntiAlias(pen1), true);
75    OH_Drawing_PenSetAntiAlias(pen1, false);
76    EXPECT_EQ(OH_Drawing_PenIsAntiAlias(pen1), false);
77    OH_Drawing_PenDestroy(pen1);
78}
79
80/*
81 * @tc.name: NativeDrawingPenTest_penSetColor003
82 * @tc.desc: test for the get and set methods about the color for a pen.
83 * @tc.size  : MediumTest
84 * @tc.type  : Function
85 * @tc.level : Level 1
86 */
87HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetColor003, TestSize.Level1)
88{
89    OH_Drawing_Pen* pen2 = OH_Drawing_PenCreate();
90    OH_Drawing_PenSetColor(pen2, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
91    EXPECT_EQ(OH_Drawing_PenGetColor(pen2), 0xFFFF0000);
92    OH_Drawing_PenDestroy(pen2);
93}
94
95/*
96 * @tc.name: NativeDrawingPenTest_penSetWidth004
97 * @tc.desc: test for the get and set methods about the width for a pen.
98 * @tc.size  : MediumTest
99 * @tc.type  : Function
100 * @tc.level : Level 1
101 */
102HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetWidth004, TestSize.Level1)
103{
104    OH_Drawing_Pen* pen3 = OH_Drawing_PenCreate();
105    OH_Drawing_PenSetWidth(pen3, 10);
106    EXPECT_EQ(OH_Drawing_PenGetWidth(pen3), 10);
107    OH_Drawing_PenDestroy(pen3);
108}
109
110/*
111 * @tc.name: NativeDrawingPenTest_penSetMiterLimit005
112 * @tc.desc: test for the get and set methods about the miterLimit for a pen.
113 * @tc.size  : MediumTest
114 * @tc.type  : Function
115 * @tc.level : Level 1
116 */
117HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetMiterLimit005, TestSize.Level1)
118{
119    OH_Drawing_Pen* pen4 = OH_Drawing_PenCreate();
120    OH_Drawing_PenSetMiterLimit(pen4, 5);
121    EXPECT_EQ(OH_Drawing_PenGetMiterLimit(pen4), 5);
122    OH_Drawing_PenDestroy(pen4);
123}
124
125/*
126 * @tc.name: NativeDrawingPenTest_penSetCap006
127 * @tc.desc: test for the get and set methods about the line cap style for a pen.
128 * @tc.size  : MediumTest
129 * @tc.type  : Function
130 * @tc.level : Level 1
131 */
132HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetCap006, TestSize.Level1)
133{
134    OH_Drawing_Pen* pen5 = OH_Drawing_PenCreate();
135    OH_Drawing_PenSetCap(pen5, OH_Drawing_PenLineCapStyle::LINE_SQUARE_CAP);
136    EXPECT_EQ(OH_Drawing_PenGetCap(pen5), OH_Drawing_PenLineCapStyle::LINE_SQUARE_CAP);
137    OH_Drawing_PenSetCap(pen5, OH_Drawing_PenLineCapStyle::LINE_FLAT_CAP);
138    EXPECT_EQ(OH_Drawing_PenGetCap(pen5), OH_Drawing_PenLineCapStyle::LINE_FLAT_CAP);
139    OH_Drawing_PenSetCap(pen5, OH_Drawing_PenLineCapStyle::LINE_ROUND_CAP);
140    EXPECT_EQ(OH_Drawing_PenGetCap(pen5), OH_Drawing_PenLineCapStyle::LINE_ROUND_CAP);
141    OH_Drawing_PenDestroy(pen5);
142}
143
144/*
145 * @tc.name: NativeDrawingPenTest_penSetJoin007
146 * @tc.desc: test for the get and set methods about the line join style for a pen.
147 * @tc.size  : MediumTest
148 * @tc.type  : Function
149 * @tc.level : Level 1
150 */
151HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetJoin007, TestSize.Level1)
152{
153    OH_Drawing_Pen* pen6 = OH_Drawing_PenCreate();
154    OH_Drawing_PenSetJoin(pen6, OH_Drawing_PenLineJoinStyle::LINE_ROUND_JOIN);
155    EXPECT_EQ(OH_Drawing_PenGetJoin(pen6), OH_Drawing_PenLineJoinStyle::LINE_ROUND_JOIN);
156    OH_Drawing_PenSetJoin(pen6, OH_Drawing_PenLineJoinStyle::LINE_MITER_JOIN);
157    EXPECT_EQ(OH_Drawing_PenGetJoin(pen6), OH_Drawing_PenLineJoinStyle::LINE_MITER_JOIN);
158    OH_Drawing_PenSetJoin(pen6, OH_Drawing_PenLineJoinStyle::LINE_BEVEL_JOIN);
159    EXPECT_EQ(OH_Drawing_PenGetJoin(pen6), OH_Drawing_PenLineJoinStyle::LINE_BEVEL_JOIN);
160    OH_Drawing_PenDestroy(pen6);
161}
162
163/*
164 * @tc.name: NativeDrawingPenTest_pen008
165 * @tc.desc: test for the get and set alpha for a pen.
166 * @tc.size  : MediumTest
167 * @tc.type  : Function
168 * @tc.level : Level 1
169 */
170HWTEST_F(NativeDrawingPenTest, NativeDrawingPenAlphaTest001, TestSize.Level1)
171{
172    OH_Drawing_Pen* pen7 = OH_Drawing_PenCreate();
173    OH_Drawing_PenSetColor(pen7, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
174    OH_Drawing_PenSetAlpha(pen7, 128);
175    EXPECT_EQ(OH_Drawing_PenGetAlpha(pen7), 128);
176    OH_Drawing_PenDestroy(pen7);
177}
178
179/*
180 * @tc.name: NativeDrawingPenTest_penSetBlendMode008
181 * @tc.desc: test for the get and set methods about the line join style for a pen.
182 * @tc.size  : MediumTest
183 * @tc.type  : Function
184 * @tc.level : Level 1
185 */
186HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penSetBlendMode008, TestSize.Level1)
187{
188    OH_Drawing_Pen* pen8 = OH_Drawing_PenCreate();
189    EXPECT_NE(pen8, nullptr);
190    OH_Drawing_PenSetBlendMode(pen8, OH_Drawing_BlendMode::BLEND_MODE_SRC);
191    OH_Drawing_PenSetBlendMode(nullptr, OH_Drawing_BlendMode::BLEND_MODE_SRC);
192    OH_Drawing_PenDestroy(pen8);
193}
194
195/*
196 * @tc.name: NativeDrawingPenTest_penReset009
197 * @tc.desc: test for the reset method for a pen.
198 * @tc.size  : MediumTest
199 * @tc.type  : Function
200 * @tc.level : Level 1
201 */
202HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penReset009, TestSize.Level1)
203{
204    OH_Drawing_Pen* pen9 = OH_Drawing_PenCreate();
205    OH_Drawing_PenSetAntiAlias(pen9, true);
206    OH_Drawing_PenSetColor(pen9, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
207    OH_Drawing_PenSetWidth(pen9, 10);
208    OH_Drawing_PenSetMiterLimit(pen9, 5);
209    OH_Drawing_PenSetCap(pen9, OH_Drawing_PenLineCapStyle::LINE_ROUND_CAP);
210    OH_Drawing_PenSetJoin(pen9, OH_Drawing_PenLineJoinStyle::LINE_BEVEL_JOIN);
211
212    OH_Drawing_PenReset(pen9);
213    EXPECT_EQ(OH_Drawing_PenIsAntiAlias(pen9), false);
214    EXPECT_EQ(OH_Drawing_PenGetColor(pen9), 0xFF000000);
215    EXPECT_EQ(OH_Drawing_PenGetWidth(pen9), 0);
216    EXPECT_EQ(OH_Drawing_PenGetMiterLimit(pen9), -1);
217    EXPECT_EQ(OH_Drawing_PenGetCap(pen9), OH_Drawing_PenLineCapStyle::LINE_FLAT_CAP);
218    EXPECT_EQ(OH_Drawing_PenGetJoin(pen9), OH_Drawing_PenLineJoinStyle::LINE_MITER_JOIN);
219
220    OH_Drawing_PenDestroy(pen9);
221}
222
223/*
224 * @tc.name: NativeDrawingPenTest_penGetFilter010
225 * @tc.desc: gets the filter from a pen.
226 * @tc.size  : MediumTest
227 * @tc.type  : Function
228 * @tc.level : Level 1
229 */
230HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_penGetFilter010, TestSize.Level1)
231{
232    OH_Drawing_Pen* pen9 = OH_Drawing_PenCreate();
233    EXPECT_NE(pen9, nullptr);
234    OH_Drawing_Filter* cFilter_ = OH_Drawing_FilterCreate();
235    EXPECT_NE(cFilter_, nullptr);
236
237    OH_Drawing_ColorFilter* colorFilterTmp = OH_Drawing_ColorFilterCreateLinearToSrgbGamma();
238    OH_Drawing_FilterSetColorFilter(cFilter_, nullptr);
239    OH_Drawing_FilterGetColorFilter(cFilter_, colorFilterTmp);
240    EXPECT_EQ((reinterpret_cast<ColorFilter*>(colorFilterTmp))->GetType(),
241        ColorFilter::FilterType::NO_TYPE);
242
243    OH_Drawing_Filter* tmpFilter_ = OH_Drawing_FilterCreate();
244    EXPECT_NE(cFilter_, nullptr);
245    EXPECT_NE(tmpFilter_, nullptr);
246    OH_Drawing_ColorFilter* cColorFilter_ = OH_Drawing_ColorFilterCreateBlendMode(0xFF0000FF, BLEND_MODE_COLOR);
247    OH_Drawing_FilterSetColorFilter(cFilter_, cColorFilter_);
248    OH_Drawing_PenSetFilter(pen9, cFilter_);
249    OH_Drawing_PenGetFilter(pen9, tmpFilter_);
250
251    EXPECT_NE(CastToFilter(tmpFilter_)->GetColorFilter(), nullptr);
252    EXPECT_EQ(CastToFilter(tmpFilter_)->GetColorFilter()->GetType(), ColorFilter::FilterType::BLEND_MODE);
253    OH_Drawing_FilterDestroy(cFilter_);
254    OH_Drawing_FilterDestroy(tmpFilter_);
255    OH_Drawing_ColorFilterDestroy(cColorFilter_);
256    OH_Drawing_ColorFilterDestroy(colorFilterTmp);
257    OH_Drawing_PenDestroy(pen9);
258}
259/*
260 * @tc.name: NativeDrawingPenTest_PenSetShadowLayer011
261 * @tc.desc: gets the filter from a pen.
262 * @tc.size  : MediumTest
263 * @tc.type  : Function
264 * @tc.level : Level 1
265 */
266HWTEST_F(NativeDrawingPenTest, NativeDrawingPenTest_PenSetShadowLayer011, TestSize.Level1)
267{
268    // blurRadius:3.f, offset:(-3.f, 3.f), shadowColor:green
269    OH_Drawing_ShadowLayer* shadowLayer = OH_Drawing_ShadowLayerCreate(3.f, -3.f, 3.f, 0xFF00FF00);
270    EXPECT_NE(shadowLayer, nullptr);
271    OH_Drawing_Pen* pen = OH_Drawing_PenCreate();
272    OH_Drawing_PenSetShadowLayer(nullptr, shadowLayer);
273    OH_Drawing_PenSetShadowLayer(pen, nullptr);
274    OH_Drawing_PenSetShadowLayer(pen, shadowLayer);
275    OH_Drawing_ShadowLayerDestroy(shadowLayer);
276    OH_Drawing_PenDestroy(pen);
277}
278} // namespace Drawing
279} // namespace Rosen
280} // namespace OHOS