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 "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/paint.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 #include "effect/shader_effect_lazy.h"
31 #include "effect/image_filter.h"
32 #include "effect/image_filter_lazy.h"
33
34 using namespace testing;
35 using namespace testing::ext;
36
37 namespace OHOS {
38 namespace Rosen {
39 namespace Drawing {
40 class SkiaPaintTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp() override;
45 void TearDown() override;
46 };
47
SetUpTestCase()48 void SkiaPaintTest::SetUpTestCase() {}
TearDownTestCase()49 void SkiaPaintTest::TearDownTestCase() {}
SetUp()50 void SkiaPaintTest::SetUp() {}
TearDown()51 void SkiaPaintTest::TearDown() {}
52
53 /**
54 * @tc.name: AsBlendMode001
55 * @tc.desc: Test AsBlendMode
56 * @tc.type: FUNC
57 * @tc.require: I8VQSW
58 */
59 HWTEST_F(SkiaPaintTest, AsBlendMode001, TestSize.Level1)
60 {
61 Brush brush;
62 SkiaPaint skiaPaint;
63 EXPECT_TRUE(skiaPaint.AsBlendMode(brush));
64 }
65
66 /**
67 * @tc.name: BrushToSkPaint001
68 * @tc.desc: Test BrushToSkPaint
69 * @tc.type: FUNC
70 * @tc.require: I8VQSW
71 */
72 HWTEST_F(SkiaPaintTest, BrushToSkPaint001, TestSize.Level1)
73 {
74 Brush brush;
75 brush.SetAntiAlias(true);
76 SkPaint skPaint;
77 SkiaPaint::BrushToSkPaint(brush, skPaint);
78 EXPECT_TRUE(skPaint.isAntiAlias());
79 }
80
81 /**
82 * @tc.name: BrushToSkPaint002
83 * @tc.desc: Test BrushToSkPaint
84 * @tc.type: FUNC
85 * @tc.require: I8VQSW
86 */
87 HWTEST_F(SkiaPaintTest, BrushToSkPaint002, TestSize.Level1)
88 {
89 Brush brush;
90 brush.SetAntiAlias(true);
91 Color4f color;
92 auto space = std::make_shared<ColorSpace>();
93 brush.SetColor(color, space);
94 brush.SetAlpha(100);
95 brush.SetShaderEffect(ShaderEffect::CreateColorShader(0xFF000000));
96 auto blender = std::make_shared<Blender>();
97 brush.SetBlender(blender);
98 SkPaint skPaint;
99 SkiaPaint::BrushToSkPaint(brush, skPaint);
100 EXPECT_TRUE(skPaint.isAntiAlias());
101 }
102
103 /**
104 * @tc.name: PenToSkPaint001
105 * @tc.desc: Test PenToSkPaint
106 * @tc.type: FUNC
107 * @tc.require: I8VQSW
108 */
109 HWTEST_F(SkiaPaintTest, PenToSkPaint001, TestSize.Level1)
110 {
111 Pen pen;
112 pen.SetAntiAlias(true);
113 SkPaint skPaint;
114 SkiaPaint::PenToSkPaint(pen, skPaint);
115 EXPECT_TRUE(skPaint.isAntiAlias());
116 }
117
118 /**
119 * @tc.name: PenToSkPaint002
120 * @tc.desc: Test PenToSkPaint
121 * @tc.type: FUNC
122 * @tc.require: I8VQSW
123 */
124 HWTEST_F(SkiaPaintTest, PenToSkPaint002, TestSize.Level1)
125 {
126 Pen pen;
127 pen.SetAntiAlias(true);
128 pen.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
129 pen.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
130 SkPaint skPaint;
131 SkiaPaint::PenToSkPaint(pen, skPaint);
132 EXPECT_TRUE(skPaint.isAntiAlias());
133 }
134
135 /**
136 * @tc.name: PenToSkPaint003
137 * @tc.desc: Test PenToSkPaint
138 * @tc.type: FUNC
139 * @tc.require: I8VQSW
140 */
141 HWTEST_F(SkiaPaintTest, PenToSkPaint003, TestSize.Level1)
142 {
143 Pen pen;
144 pen.SetAntiAlias(true);
145 Color4f color;
146 auto space = std::make_shared<ColorSpace>();
147 pen.SetColor(color, space);
148 pen.SetBlendMode(BlendMode::CLEAR);
149 pen.SetCapStyle(Pen::CapStyle::ROUND_CAP);
150 pen.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
151 pen.SetShaderEffect(ShaderEffect::CreateColorShader(0xFF000000));
152 pen.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
153 SkPaint skPaint;
154 SkiaPaint::PenToSkPaint(pen, skPaint);
155 EXPECT_TRUE(skPaint.isAntiAlias());
156 }
157
158 /**
159 * @tc.name: PaintToSkPaint001
160 * @tc.desc: Test PaintToSkPaint
161 * @tc.type: FUNC
162 * @tc.require: I8VQSW
163 */
164 HWTEST_F(SkiaPaintTest, PaintToSkPaint001, TestSize.Level1)
165 {
166 Paint paint;
167 paint.SetAntiAlias(true);
168 auto space = std::make_shared<ColorSpace>();
169 Color4f color;
170 paint.SetColor(color, space);
171 paint.SetBlendMode(BlendMode::CLEAR);
172 SkPaint skPaint;
173 SkiaPaint::PaintToSkPaint(paint, skPaint);
174 EXPECT_TRUE(skPaint.isAntiAlias());
175 }
176
177 /**
178 * @tc.name: ApplyStrokeParam001
179 * @tc.desc: Test ApplyStrokeParam
180 * @tc.type: FUNC
181 * @tc.require: I8VQSW
182 */
183 HWTEST_F(SkiaPaintTest, ApplyStrokeParam001, TestSize.Level1)
184 {
185 Paint paint;
186 paint.SetStyle(Paint::PaintStyle::PAINT_FILL);
187 paint.SetCapStyle(Pen::CapStyle::ROUND_CAP);
188 paint.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
189 paint.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
190 SkiaPaint skiaPaint;
191 SkPaint skPaint;
192 skiaPaint.ApplyStrokeParam(paint, skPaint);
193 EXPECT_TRUE(skPaint.getStrokeCap() == SkPaint::Cap::kRound_Cap);
194 Paint paint2;
195 paint2.SetStyle(Paint::PaintStyle::PAINT_FILL);
196 paint2.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
197 paint2.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
198 paint2.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
199 skiaPaint.ApplyStrokeParam(paint2, skPaint);
200 EXPECT_TRUE(skPaint.getStrokeCap() == SkPaint::Cap::kSquare_Cap);
201 }
202
203 /**
204 * @tc.name: ComputeFastBounds001
205 * @tc.desc: Test ComputeFastBounds
206 * @tc.type: FUNC
207 * @tc.require: I8VQSW
208 */
209 HWTEST_F(SkiaPaintTest, ComputeFastBounds001, TestSize.Level1)
210 {
211 Brush brush;
212 Rect rect;
213 SkiaPaint skiaPaint;
214 Rect ret1 = skiaPaint.ComputeFastBounds(brush, rect, nullptr);
215 EXPECT_TRUE(!ret1.IsValid());
216 Rect storage;
217 Rect ret2 = skiaPaint.ComputeFastBounds(brush, rect, &storage);
218 EXPECT_TRUE(!ret2.IsValid());
219 }
220
221 /**
222 * @tc.name: GetFillPath001
223 * @tc.desc: Test GetFillPath
224 * @tc.type: FUNC
225 * @tc.require: I8VQSW
226 */
227 HWTEST_F(SkiaPaintTest, GetFillPath001, TestSize.Level1)
228 {
229 SkiaPaint skiaPaint;
230 Pen pen;
231 pen.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
232 pen.SetWidth(10);
233 Path srcPath;
234 Path dstPath;
235 srcPath.LineTo(100, 100); // 100: x, y
236 Rect rect(0, 0, 100, 100); // 100: right, bottom
237 Matrix matrix;
238 bool ret = false;
239 ret = skiaPaint.GetFillPath(pen, srcPath, dstPath, &rect, matrix);
240 EXPECT_TRUE(ret);
241 ret = skiaPaint.GetFillPath(pen, srcPath, dstPath, nullptr, matrix);
242 EXPECT_TRUE(ret);
243 }
244
245 /**
246 * @tc.name: BrushWithRegularShader001
247 * @tc.desc: Test BrushToSkPaint with regular shader
248 * @tc.type: FUNC
249 * @tc.require: I8VQSW
250 */
251 HWTEST_F(SkiaPaintTest, BrushWithRegularShader001, TestSize.Level1)
252 {
253 Brush brush;
254 auto colorShader = ShaderEffect::CreateColorShader(0xFF00FF00);
255 brush.SetShaderEffect(colorShader);
256
257 SkPaint skPaint;
258 SkiaPaint::BrushToSkPaint(brush, skPaint);
259 EXPECT_TRUE(skPaint.getShader() != nullptr);
260 }
261
262 /**
263 * @tc.name: BrushWithLazyShader001
264 * @tc.desc: Test BrushToSkPaint with lazy shader
265 * @tc.type: FUNC
266 * @tc.require: I8VQSW
267 */
268 HWTEST_F(SkiaPaintTest, BrushWithLazyShader001, TestSize.Level1)
269 {
270 Brush brush;
271 // Create a lazy blend shader to test lazy handling
272 auto srcShader = ShaderEffect::CreateColorShader(0xFF00FF00);
273 auto dstShader = ShaderEffect::CreateColorShader(0xFF0000FF);
274 auto lazyShader = ShaderEffectLazy::CreateBlendShader(dstShader, srcShader, BlendMode::SRC_OVER);
275 brush.SetShaderEffect(lazyShader);
276
277 SkPaint skPaint;
278 SkiaPaint::BrushToSkPaint(brush, skPaint);
279 EXPECT_TRUE(skPaint.getShader() != nullptr);
280 }
281
282 /**
283 * @tc.name: FilterWithRegularImageFilter001
284 * @tc.desc: Test ApplyFilter with regular image filter
285 * @tc.type: FUNC
286 * @tc.require: I8VQSW
287 */
288 HWTEST_F(SkiaPaintTest, FilterWithRegularImageFilter001, TestSize.Level1)
289 {
290 Filter filter;
291 auto blurFilter = ImageFilter::CreateBlurImageFilter(5.0f, 5.0f, TileMode::CLAMP, nullptr);
292 filter.SetImageFilter(blurFilter);
293
294 SkPaint skPaint;
295 SkiaPaint skiaPaint;
296 skiaPaint.ApplyFilter(skPaint, filter);
297 EXPECT_TRUE(skPaint.getImageFilter() != nullptr);
298 }
299
300 /**
301 * @tc.name: FilterWithLazyImageFilter001
302 * @tc.desc: Test ApplyFilter with lazy image filter
303 * @tc.type: FUNC
304 * @tc.require: I8VQSW
305 */
306 HWTEST_F(SkiaPaintTest, FilterWithLazyImageFilter001, TestSize.Level1)
307 {
308 Filter filter;
309 // Create a lazy blur filter to test lazy handling
310 auto lazyFilter = ImageFilterLazy::CreateBlur(5.0f, 5.0f, TileMode::CLAMP, nullptr);
311 filter.SetImageFilter(lazyFilter);
312
313 SkPaint skPaint;
314 SkiaPaint skiaPaint;
315 skiaPaint.ApplyFilter(skPaint, filter);
316 EXPECT_TRUE(skPaint.getImageFilter() != nullptr);
317 }
318
319 /**
320 * @tc.name: PenWithLazyShader001
321 * @tc.desc: Test PenToSkPaint with lazy shader
322 * @tc.type: FUNC
323 * @tc.require: I8VQSW
324 */
325 HWTEST_F(SkiaPaintTest, PenWithLazyShader001, TestSize.Level1)
326 {
327 Pen pen;
328 // Create a lazy blend shader to test lazy handling
329 auto srcShader = ShaderEffect::CreateColorShader(0xFF0000FF);
330 auto dstShader = ShaderEffect::CreateColorShader(0xFF00FF00);
331 auto lazyShader = ShaderEffectLazy::CreateBlendShader(dstShader, srcShader, BlendMode::SRC_OVER);
332 pen.SetShaderEffect(lazyShader);
333
334 SkPaint skPaint;
335 SkiaPaint::PenToSkPaint(pen, skPaint);
336 EXPECT_TRUE(skPaint.getShader() != nullptr);
337 }
338
339 /**
340 * @tc.name: PaintWithLazyShader001
341 * @tc.desc: Test PaintToSkPaint with lazy shader
342 * @tc.type: FUNC
343 * @tc.require: I8VQSW
344 */
345 HWTEST_F(SkiaPaintTest, PaintWithLazyShader001, TestSize.Level1)
346 {
347 Paint paint;
348 // Create a lazy blend shader to test lazy handling
349 auto srcShader = ShaderEffect::CreateColorShader(0xFF0000FF);
350 auto dstShader = ShaderEffect::CreateColorShader(0xFF00FF00);
351 auto lazyShader = ShaderEffectLazy::CreateBlendShader(dstShader, srcShader, BlendMode::SRC_OVER);
352 paint.SetShaderEffect(lazyShader);
353
354 SkPaint skPaint;
355 SkiaPaint::PaintToSkPaint(paint, skPaint);
356 EXPECT_TRUE(skPaint.getShader() != nullptr);
357 }
358 } // namespace Drawing
359 } // namespace Rosen
360 } // namespace OHOS