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/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
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: AsBlendMode001
52 * @tc.desc: Test AsBlendMode
53 * @tc.type: FUNC
54 * @tc.require: I8VQSW
55 */
56 HWTEST_F(SkiaPaintTest, AsBlendMode001, TestSize.Level1)
57 {
58 Brush brush;
59 SkiaPaint skiaPaint;
60 EXPECT_TRUE(skiaPaint.AsBlendMode(brush));
61 }
62
63 /**
64 * @tc.name: BrushToSkPaint001
65 * @tc.desc: Test BrushToSkPaint
66 * @tc.type: FUNC
67 * @tc.require: I8VQSW
68 */
69 HWTEST_F(SkiaPaintTest, BrushToSkPaint001, TestSize.Level1)
70 {
71 Brush brush;
72 brush.SetAntiAlias(true);
73 SkPaint skPaint;
74 SkiaPaint::BrushToSkPaint(brush, skPaint);
75 EXPECT_TRUE(skPaint.isAntiAlias());
76 }
77
78 /**
79 * @tc.name: BrushToSkPaint002
80 * @tc.desc: Test BrushToSkPaint
81 * @tc.type: FUNC
82 * @tc.require: I8VQSW
83 */
84 HWTEST_F(SkiaPaintTest, BrushToSkPaint002, TestSize.Level1)
85 {
86 Brush brush;
87 brush.SetAntiAlias(true);
88 Color4f color;
89 auto space = std::make_shared<ColorSpace>();
90 brush.SetColor(color, space);
91 brush.SetAlpha(100);
92 brush.SetShaderEffect(ShaderEffect::CreateColorShader(0xFF000000));
93 auto blender = std::make_shared<Blender>();
94 brush.SetBlender(blender);
95 SkPaint skPaint;
96 SkiaPaint::BrushToSkPaint(brush, skPaint);
97 EXPECT_TRUE(skPaint.isAntiAlias());
98 }
99
100 /**
101 * @tc.name: PenToSkPaint001
102 * @tc.desc: Test PenToSkPaint
103 * @tc.type: FUNC
104 * @tc.require: I8VQSW
105 */
106 HWTEST_F(SkiaPaintTest, PenToSkPaint001, TestSize.Level1)
107 {
108 Pen pen;
109 pen.SetAntiAlias(true);
110 SkPaint skPaint;
111 SkiaPaint::PenToSkPaint(pen, skPaint);
112 EXPECT_TRUE(skPaint.isAntiAlias());
113 }
114
115 /**
116 * @tc.name: PenToSkPaint002
117 * @tc.desc: Test PenToSkPaint
118 * @tc.type: FUNC
119 * @tc.require: I8VQSW
120 */
121 HWTEST_F(SkiaPaintTest, PenToSkPaint002, TestSize.Level1)
122 {
123 Pen pen;
124 pen.SetAntiAlias(true);
125 pen.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
126 pen.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
127 SkPaint skPaint;
128 SkiaPaint::PenToSkPaint(pen, skPaint);
129 EXPECT_TRUE(skPaint.isAntiAlias());
130 }
131
132 /**
133 * @tc.name: PenToSkPaint003
134 * @tc.desc: Test PenToSkPaint
135 * @tc.type: FUNC
136 * @tc.require: I8VQSW
137 */
138 HWTEST_F(SkiaPaintTest, PenToSkPaint003, TestSize.Level1)
139 {
140 Pen pen;
141 pen.SetAntiAlias(true);
142 Color4f color;
143 auto space = std::make_shared<ColorSpace>();
144 pen.SetColor(color, space);
145 pen.SetBlendMode(BlendMode::CLEAR);
146 pen.SetCapStyle(Pen::CapStyle::ROUND_CAP);
147 pen.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
148 pen.SetShaderEffect(ShaderEffect::CreateColorShader(0xFF000000));
149 pen.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
150 SkPaint skPaint;
151 SkiaPaint::PenToSkPaint(pen, skPaint);
152 EXPECT_TRUE(skPaint.isAntiAlias());
153 }
154
155 /**
156 * @tc.name: PaintToSkPaint001
157 * @tc.desc: Test PaintToSkPaint
158 * @tc.type: FUNC
159 * @tc.require: I8VQSW
160 */
161 HWTEST_F(SkiaPaintTest, PaintToSkPaint001, TestSize.Level1)
162 {
163 Paint paint;
164 paint.SetAntiAlias(true);
165 auto space = std::make_shared<ColorSpace>();
166 Color4f color;
167 paint.SetColor(color, space);
168 paint.SetBlendMode(BlendMode::CLEAR);
169 SkPaint skPaint;
170 SkiaPaint::PaintToSkPaint(paint, skPaint);
171 EXPECT_TRUE(skPaint.isAntiAlias());
172 }
173
174 /**
175 * @tc.name: ApplyStrokeParam001
176 * @tc.desc: Test ApplyStrokeParam
177 * @tc.type: FUNC
178 * @tc.require: I8VQSW
179 */
180 HWTEST_F(SkiaPaintTest, ApplyStrokeParam001, TestSize.Level1)
181 {
182 Paint paint;
183 paint.SetStyle(Paint::PaintStyle::PAINT_FILL);
184 paint.SetCapStyle(Pen::CapStyle::ROUND_CAP);
185 paint.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
186 paint.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
187 SkiaPaint skiaPaint;
188 SkPaint skPaint;
189 skiaPaint.ApplyStrokeParam(paint, skPaint);
190 Paint paint2;
191 paint2.SetStyle(Paint::PaintStyle::PAINT_FILL);
192 paint2.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
193 paint2.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
194 paint2.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
195 skiaPaint.ApplyStrokeParam(paint2, skPaint);
196 }
197
198 /**
199 * @tc.name: ComputeFastBounds001
200 * @tc.desc: Test ComputeFastBounds
201 * @tc.type: FUNC
202 * @tc.require: I8VQSW
203 */
204 HWTEST_F(SkiaPaintTest, ComputeFastBounds001, TestSize.Level1)
205 {
206 Brush brush;
207 Rect rect;
208 SkiaPaint skiaPaint;
209 Rect ret1 = skiaPaint.ComputeFastBounds(brush, rect, nullptr);
210 EXPECT_TRUE(!ret1.IsValid());
211 Rect storage;
212 Rect ret2 = skiaPaint.ComputeFastBounds(brush, rect, &storage);
213 EXPECT_TRUE(!ret2.IsValid());
214 }
215
216 /**
217 * @tc.name: GetFillPath001
218 * @tc.desc: Test GetFillPath
219 * @tc.type: FUNC
220 * @tc.require: I8VQSW
221 */
222 HWTEST_F(SkiaPaintTest, GetFillPath001, TestSize.Level1)
223 {
224 SkiaPaint skiaPaint;
225 Pen pen;
226 pen.SetPathEffect(PathEffect::CreateCornerPathEffect(10));
227 pen.SetWidth(10);
228 Path srcPath;
229 Path dstPath;
230 srcPath.LineTo(100, 100); // 100: x, y
231 Rect rect(0, 0, 100, 100); // 100: right, bottom
232 Matrix matrix;
233 bool ret = false;
234 ret = skiaPaint.GetFillPath(pen, srcPath, dstPath, &rect, matrix);
235 EXPECT_TRUE(ret);
236 ret = skiaPaint.GetFillPath(pen, srcPath, dstPath, nullptr, matrix);
237 EXPECT_TRUE(ret);
238 }
239 } // namespace Drawing
240 } // namespace Rosen
241 } // namespace OHOS