1 /*
2 * Copyright (c) 2022 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, software
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 <cstddef>
17 #include "core/SkPaint.h"
18 #include "gtest/gtest.h"
19 #define private public
20 #include "skia_adapter/skia_paint.h"
21 #undef private
22 #include "draw/brush.h"
23 #include "draw/color.h"
24 #include "draw/pen.h"
25 #include "effect/color_space.h"
26 #include "effect/filter.h"
27 #include "effect/mask_filter.h"
28 #include "effect/path_effect.h"
29 #include "effect/shader_effect.h"
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace Rosen {
36 namespace Drawing {
37 class SkiaPaintTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp() override;
42 void TearDown() override;
43 };
44
SetUpTestCase()45 void SkiaPaintTest::SetUpTestCase() {}
TearDownTestCase()46 void SkiaPaintTest::TearDownTestCase() {}
SetUp()47 void SkiaPaintTest::SetUp() {}
TearDown()48 void SkiaPaintTest::TearDown() {}
49
50 /**
51 * @tc.name: BrushToSkPaint001
52 * @tc.desc:
53 * @tc.type: FUNC
54 * @tc.author:
55 */
56 HWTEST_F(SkiaPaintTest, BrushToSkPaint001, TestSize.Level1)
57 {
58 Brush brush;
59 SkPaint skPaint;
60 SkiaPaint skiaPaint;
61 skiaPaint.BrushToSkPaint(brush, skPaint);
62 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
63 }
64
65 /**
66 * @tc.name: BrushToSkPaint002
67 * @tc.desc:
68 * @tc.type: FUNC
69 * @tc.author:
70 */
71 HWTEST_F(SkiaPaintTest, BrushToSkPaint002, TestSize.Level1)
72 {
73 Brush brush;
74 SkPaint skPaint;
75 SkiaPaint skiaPaint;
76 Color4f color4f;
77 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
78 brush.SetColor(color4f, colorSpace);
79 skiaPaint.BrushToSkPaint(brush, skPaint);
80 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
81 }
82
83 /**
84 * @tc.name: BrushToSkPaint003
85 * @tc.desc:
86 * @tc.type: FUNC
87 * @tc.author:
88 */
89 HWTEST_F(SkiaPaintTest, BrushToSkPaint003, TestSize.Level1)
90 {
91 Brush brush;
92 SkPaint skPaint;
93 SkiaPaint skiaPaint;
94 std::shared_ptr<ShaderEffect> shaderEffect = ShaderEffect::CreateColorShader(20);
95 brush.SetShaderEffect(shaderEffect);
96 skiaPaint.BrushToSkPaint(brush, skPaint);
97 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
98 }
99
100 /**
101 * @tc.name: PenToSkPaint001
102 * @tc.desc:
103 * @tc.type: FUNC
104 * @tc.author:
105 */
106 HWTEST_F(SkiaPaintTest, PenToSkPaint001, TestSize.Level1)
107 {
108 Pen pen;
109 SkPaint paint;
110 SkiaPaint skiaPaint;
111 skiaPaint.PenToSkPaint(pen, paint);
112 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
113 }
114
115 /**
116 * @tc.name: PenToSkPaint002
117 * @tc.desc:
118 * @tc.type: FUNC
119 * @tc.author:
120 */
121 HWTEST_F(SkiaPaintTest, PenToSkPaint002, TestSize.Level1)
122 {
123 Pen pen;
124 SkPaint paint;
125 SkiaPaint skiaPaint;
126 Color4f color4f;
127 std::shared_ptr<ColorSpace> colorSpace = ColorSpace::CreateSRGB();
128 pen.SetColor(color4f, colorSpace);
129 skiaPaint.PenToSkPaint(pen, paint);
130 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
131 }
132
133 /**
134 * @tc.name: PenToSkPaint003
135 * @tc.desc:
136 * @tc.type: FUNC
137 * @tc.author:
138 */
139 HWTEST_F(SkiaPaintTest, PenToSkPaint003, TestSize.Level1)
140 {
141 Pen pen;
142 SkPaint paint;
143 SkiaPaint skiaPaint;
144 pen.SetCapStyle(Pen::CapStyle::FLAT_CAP);
145 skiaPaint.PenToSkPaint(pen, paint);
146 pen.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
147 skiaPaint.PenToSkPaint(pen, paint);
148 pen.SetCapStyle(Pen::CapStyle::ROUND_CAP);
149 skiaPaint.PenToSkPaint(pen, paint);
150 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
151 }
152
153 /**
154 * @tc.name: PenToSkPaint004
155 * @tc.desc:
156 * @tc.type: FUNC
157 * @tc.author:
158 */
159 HWTEST_F(SkiaPaintTest, PenToSkPaint004, TestSize.Level1)
160 {
161 Pen pen;
162 SkPaint paint;
163 SkiaPaint skiaPaint;
164 pen.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
165 skiaPaint.PenToSkPaint(pen, paint);
166 pen.SetJoinStyle(Pen::JoinStyle::MITER_JOIN);
167 skiaPaint.PenToSkPaint(pen, paint);
168 pen.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
169 skiaPaint.PenToSkPaint(pen, paint);
170 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
171 }
172
173 /**
174 * @tc.name: PenToSkPaint005
175 * @tc.desc:
176 * @tc.type: FUNC
177 * @tc.author:
178 */
179 HWTEST_F(SkiaPaintTest, PenToSkPaint005, TestSize.Level1)
180 {
181 Pen pen;
182 SkPaint paint;
183 SkiaPaint skiaPaint;
184 skiaPaint.PenToSkPaint(pen, paint);
185 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
186 }
187
188 /**
189 * @tc.name: PenToSkPaint006
190 * @tc.desc:
191 * @tc.type: FUNC
192 * @tc.author:
193 */
194 HWTEST_F(SkiaPaintTest, PenToSkPaint006, TestSize.Level1)
195 {
196 Pen pen;
197 SkPaint paint;
198 SkiaPaint skiaPaint;
199 std::shared_ptr<ShaderEffect> shaderEffect = ShaderEffect::CreateColorShader(25);
200 pen.SetShaderEffect(shaderEffect);
201 skiaPaint.PenToSkPaint(pen, paint);
202 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
203 }
204
205 /**
206 * @tc.name: PenToSkPaint007
207 * @tc.desc:
208 * @tc.type: FUNC
209 * @tc.author:
210 */
211 HWTEST_F(SkiaPaintTest, PenToSkPaint007, TestSize.Level1)
212 {
213 Pen pen;
214 SkPaint paint;
215 SkiaPaint skiaPaint;
216 std::shared_ptr<PathEffect> pathEffect = PathEffect::CreateCornerPathEffect(25.0f);
217 pen.SetPathEffect(pathEffect);
218 skiaPaint.PenToSkPaint(pen, paint);
219 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
220 }
221
222 /**
223 * @tc.name: GetSortedPaints001
224 * @tc.desc:
225 * @tc.type: FUNC
226 * @tc.author:
227 */
228 HWTEST_F(SkiaPaintTest, GetSortedPaints001, TestSize.Level1)
229 {
230 SkiaPaint skiaPaint;
231 skiaPaint.GetSortedPaints();
232 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
233 }
234
235 /**
236 * @tc.name: GetSortedPaints002
237 * @tc.desc:
238 * @tc.type: FUNC
239 * @tc.author:
240 */
241 HWTEST_F(SkiaPaintTest, GetSortedPaints002, TestSize.Level1)
242 {
243 Pen pen;
244 Brush brush;
245 SkiaPaint skiaPaint;
246 skiaPaint.SetStrokeFirst(true);
247 skiaPaint.ApplyPenToStroke(pen);
248 skiaPaint.ApplyBrushToFill(brush);
249 skiaPaint.GetSortedPaints();
250 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
251 }
252
253 /**
254 * @tc.name: GetSortedPaints003
255 * @tc.desc:
256 * @tc.type: FUNC
257 * @tc.author:
258 */
259 HWTEST_F(SkiaPaintTest, GetSortedPaints003, TestSize.Level1)
260 {
261 Pen pen;
262 SkiaPaint skiaPaint;
263 skiaPaint.SetStrokeFirst(true);
264 skiaPaint.ApplyPenToStroke(pen);
265 skiaPaint.GetSortedPaints();
266 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
267 }
268
269 /**
270 * @tc.name: GetSortedPaints004
271 * @tc.desc:
272 * @tc.type: FUNC
273 * @tc.author:
274 */
275 HWTEST_F(SkiaPaintTest, GetSortedPaints004, TestSize.Level1)
276 {
277 Brush brush;
278 SkiaPaint skiaPaint;
279 skiaPaint.SetStrokeFirst(true);
280 skiaPaint.ApplyBrushToFill(brush);
281 skiaPaint.GetSortedPaints();
282 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
283 }
284
285 /**
286 * @tc.name: ApplyFilter001
287 * @tc.desc:
288 * @tc.type: FUNC
289 * @tc.author:
290 */
291 HWTEST_F(SkiaPaintTest, ApplyFilter001, TestSize.Level1)
292 {
293 Brush brush;
294 SkPaint paint;
295 SkiaPaint skiaPaint;
296 skiaPaint.BrushToSkPaint(brush, paint);
297 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
298 }
299
300 /**
301 * @tc.name: ApplyFilter002
302 * @tc.desc:
303 * @tc.type: FUNC
304 * @tc.author:
305 */
306 HWTEST_F(SkiaPaintTest, ApplyFilter002, TestSize.Level1)
307 {
308 Brush brush;
309 SkPaint paint;
310 Filter filter;
311 SkiaPaint skiaPaint;
312 filter.SetFilterQuality(Filter::FilterQuality::LOW);
313 brush.SetFilter(filter);
314 skiaPaint.BrushToSkPaint(brush, paint);
315 filter.SetFilterQuality(Filter::FilterQuality::MEDIUM);
316 brush.SetFilter(filter);
317 skiaPaint.BrushToSkPaint(brush, paint);
318 filter.SetFilterQuality(Filter::FilterQuality::HIGH);
319 brush.SetFilter(filter);
320 skiaPaint.BrushToSkPaint(brush, paint);
321 filter.SetFilterQuality(Filter::FilterQuality::NONE);
322 brush.SetFilter(filter);
323 skiaPaint.BrushToSkPaint(brush, paint);
324 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
325 }
326
327 /**
328 * @tc.name: ApplyFilter003
329 * @tc.desc:
330 * @tc.type: FUNC
331 * @tc.author:
332 */
333 HWTEST_F(SkiaPaintTest, ApplyFilter003, TestSize.Level1)
334 {
335 Brush brush;
336 SkPaint paint;
337 Filter filter;
338 SkiaPaint skiaPaint;
339 std::shared_ptr<MaskFilter> maskFilter = MaskFilter::CreateBlurMaskFilter(BlurType::INNER, 20.0f);
340 filter.SetMaskFilter(maskFilter);
341 skiaPaint.BrushToSkPaint(brush, paint);
342 EXPECT_TRUE(skiaPaint.stroke_ != nullptr);
343 }
344
345 } // namespace Drawing
346 } // namespace Rosen
347 } // namespace OHOS