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 #define TDD_PAINT_RADIUS 10
35
36 namespace OHOS {
37 namespace Rosen {
38 namespace Drawing {
39 class SkiaPaintTest : public testing::Test {
40 public:
41 static void SetUpTestCase();
42 static void TearDownTestCase();
43 void SetUp() override;
44 void TearDown() override;
45 };
46
SetUpTestCase()47 void SkiaPaintTest::SetUpTestCase() {}
TearDownTestCase()48 void SkiaPaintTest::TearDownTestCase() {}
SetUp()49 void SkiaPaintTest::SetUp() {}
TearDown()50 void SkiaPaintTest::TearDown() {}
51
52 /**
53 * @tc.name: ApplyPaint001
54 * @tc.desc:
55 * @tc.type: FUNC
56 * @tc.author:
57 */
58 HWTEST_F(SkiaPaintTest, ApplyPaint001, TestSize.Level1)
59 {
60 Paint paint;
61 SkiaPaint skiaPaint;
62 skiaPaint.ApplyPaint(paint);
63 EXPECT_TRUE(skiaPaint.paintInUse_ == 0);
64 }
65
66 /**
67 * @tc.name: ApplyPaint002
68 * @tc.desc:
69 * @tc.type: FUNC
70 * @tc.author:
71 */
72 HWTEST_F(SkiaPaintTest, ApplyPaint002, TestSize.Level1)
73 {
74 Paint paint;
75 paint.SetStyle(Paint::PaintStyle::PAINT_FILL);
76 SkiaPaint skiaPaint;
77 skiaPaint.ApplyPaint(paint);
78 EXPECT_TRUE(skiaPaint.paintInUse_ != 0);
79 }
80
81 /**
82 * @tc.name: AsBlendMode001
83 * @tc.desc: Test AsBlendMode
84 * @tc.type: FUNC
85 * @tc.require: I8VQSW
86 */
87 HWTEST_F(SkiaPaintTest, AsBlendMode001, TestSize.Level1)
88 {
89 Brush brush;
90 SkiaPaint skiaPaint;
91 skiaPaint.AsBlendMode(brush);
92 }
93
94 /**
95 * @tc.name: BrushToSkPaint001
96 * @tc.desc: Test BrushToSkPaint
97 * @tc.type: FUNC
98 * @tc.require: I8VQSW
99 */
100 HWTEST_F(SkiaPaintTest, BrushToSkPaint001, TestSize.Level1)
101 {
102 Brush brush;
103 brush.SetAntiAlias(true);
104 SkPaint skPaint;
105 SkiaPaint::BrushToSkPaint(brush, skPaint);
106 EXPECT_TRUE(skPaint.isAntiAlias());
107 }
108
109 /**
110 * @tc.name: BrushToSkPaint002
111 * @tc.desc: Test BrushToSkPaint
112 * @tc.type: FUNC
113 * @tc.require: I8VQSW
114 */
115 HWTEST_F(SkiaPaintTest, BrushToSkPaint002, TestSize.Level1)
116 {
117 Brush brush;
118 brush.SetAntiAlias(true);
119 Color4f color{0, 0, 0, 1};
120 auto space = std::make_shared<ColorSpace>();
121 brush.SetColor(color, space);
122 brush.SetAlpha(100);
123 brush.SetShaderEffect(ShaderEffect::CreateColorShader(0xFF000000));
124 auto blender = std::make_shared<Blender>();
125 brush.SetBlender(blender);
126 SkPaint skPaint;
127 SkiaPaint::BrushToSkPaint(brush, skPaint);
128 EXPECT_TRUE(skPaint.isAntiAlias());
129 }
130
131 /**
132 * @tc.name: PenToSkPaint001
133 * @tc.desc: Test PenToSkPaint
134 * @tc.type: FUNC
135 * @tc.require: I8VQSW
136 */
137 HWTEST_F(SkiaPaintTest, PenToSkPaint001, TestSize.Level1)
138 {
139 Pen pen;
140 pen.SetAntiAlias(true);
141 SkPaint skPaint;
142 SkiaPaint::PenToSkPaint(pen, skPaint);
143 EXPECT_TRUE(skPaint.isAntiAlias());
144 }
145
146 /**
147 * @tc.name: PenToSkPaint002
148 * @tc.desc: Test PenToSkPaint
149 * @tc.type: FUNC
150 * @tc.require: I8VQSW
151 */
152 HWTEST_F(SkiaPaintTest, PenToSkPaint002, TestSize.Level1)
153 {
154 Pen pen;
155 pen.SetAntiAlias(true);
156 pen.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
157 pen.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
158 SkPaint skPaint;
159 SkiaPaint::PenToSkPaint(pen, skPaint);
160 EXPECT_TRUE(skPaint.isAntiAlias());
161 }
162
163 /**
164 * @tc.name: PenToSkPaint003
165 * @tc.desc: Test PenToSkPaint
166 * @tc.type: FUNC
167 * @tc.require: I8VQSW
168 */
169 HWTEST_F(SkiaPaintTest, PenToSkPaint003, TestSize.Level1)
170 {
171 Pen pen;
172 pen.SetAntiAlias(true);
173 Color4f color{0, 0, 0, 1};
174 auto space = std::make_shared<ColorSpace>();
175 pen.SetColor(color, space);
176 pen.SetBlendMode(BlendMode::CLEAR);
177 pen.SetCapStyle(Pen::CapStyle::ROUND_CAP);
178 pen.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
179 pen.SetShaderEffect(ShaderEffect::CreateColorShader(0xFF000000));
180 pen.SetPathEffect(PathEffect::CreateCornerPathEffect(TDD_PAINT_RADIUS));
181 SkPaint skPaint;
182 SkiaPaint::PenToSkPaint(pen, skPaint);
183 EXPECT_TRUE(skPaint.isAntiAlias());
184 }
185
186 /**
187 * @tc.name: PaintToSkPaint001
188 * @tc.desc: Test PaintToSkPaint
189 * @tc.type: FUNC
190 * @tc.require: I8VQSW
191 */
192 HWTEST_F(SkiaPaintTest, PaintToSkPaint001, TestSize.Level1)
193 {
194 Paint paint;
195 paint.SetAntiAlias(true);
196 auto space = std::make_shared<ColorSpace>();
197 Color4f color{0, 0, 0, 1};
198 paint.SetColor(color, space);
199 paint.SetBlendMode(BlendMode::CLEAR);
200 SkPaint skPaint;
201 SkiaPaint::PaintToSkPaint(paint, skPaint);
202 EXPECT_TRUE(skPaint.isAntiAlias());
203 }
204
205 /**
206 * @tc.name: GetSortedPaints001
207 * @tc.desc: Test GetSortedPaints
208 * @tc.type: FUNC
209 * @tc.require: I8VQSW
210 */
211 HWTEST_F(SkiaPaintTest, GetSortedPaints001, TestSize.Level1)
212 {
213 Paint paint;
214 paint.SetStyle(Paint::PaintStyle::PAINT_FILL);
215 SkiaPaint skiaPaint;
216 skiaPaint.ApplyPaint(paint);
217 skiaPaint.GetSortedPaints();
218 EXPECT_TRUE(skiaPaint.paintInUse_ == 0);
219 }
220
221 /**
222 * @tc.name: ApplyStrokeParam001
223 * @tc.desc: Test ApplyStrokeParam
224 * @tc.type: FUNC
225 * @tc.require: I8VQSW
226 */
227 HWTEST_F(SkiaPaintTest, ApplyStrokeParam001, TestSize.Level1)
228 {
229 Paint paint;
230 paint.SetStyle(Paint::PaintStyle::PAINT_FILL);
231 paint.SetCapStyle(Pen::CapStyle::ROUND_CAP);
232 paint.SetJoinStyle(Pen::JoinStyle::BEVEL_JOIN);
233 paint.SetPathEffect(PathEffect::CreateCornerPathEffect(TDD_PAINT_RADIUS));
234 SkiaPaint skiaPaint;
235 SkPaint skPaint;
236 skiaPaint.ApplyStrokeParam(paint, skPaint);
237 Paint paint2;
238 paint2.SetStyle(Paint::PaintStyle::PAINT_FILL);
239 paint2.SetCapStyle(Pen::CapStyle::SQUARE_CAP);
240 paint2.SetJoinStyle(Pen::JoinStyle::ROUND_JOIN);
241 paint2.SetPathEffect(PathEffect::CreateCornerPathEffect(TDD_PAINT_RADIUS));
242 skiaPaint.ApplyStrokeParam(paint2, skPaint);
243 }
244
245 /**
246 * @tc.name: ComputeFastBounds001
247 * @tc.desc: Test ComputeFastBounds
248 * @tc.type: FUNC
249 * @tc.require: I8VQSW
250 */
251 HWTEST_F(SkiaPaintTest, ComputeFastBounds001, TestSize.Level1)
252 {
253 Brush brush;
254 Rect rect;
255 SkiaPaint skiaPaint;
256 skiaPaint.ComputeFastBounds(brush, rect, nullptr);
257 Rect storage;
258 skiaPaint.ComputeFastBounds(brush, rect, &storage);
259 EXPECT_TRUE(skiaPaint.paintInUse_ == 0);
260 }
261 } // namespace Drawing
262 } // namespace Rosen
263 } // namespace OHOS