• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "gtest/gtest.h"
18 #include "draw/path.h"
19 #include "skia_adapter/skia_path.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Rosen {
26 namespace Drawing {
27 class SkiaPathTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     void SetUp() override;
32     void TearDown() override;
33 };
34 
SetUpTestCase()35 void SkiaPathTest::SetUpTestCase() {}
TearDownTestCase()36 void SkiaPathTest::TearDownTestCase() {}
SetUp()37 void SkiaPathTest::SetUp() {}
TearDown()38 void SkiaPathTest::TearDown() {}
39 
40 /**
41  * @tc.name: SkiaPath001
42  * @tc.desc: Test SkiaPath's funstions
43  * @tc.type: FUNC
44  * @tc.require: I8VQSW
45  */
46 HWTEST_F(SkiaPathTest, SkiaPath001, TestSize.Level1)
47 {
48     SkiaPath skiaPath;
49     skiaPath.MoveTo(0, 0);
50     skiaPath.LineTo(100, 100); // 100: x, y
51     skiaPath.LineTo(100, 0); // 100: x
52     skiaPath.Close();
53     skiaPath.Reset();
54     skiaPath.ArcTo(0, 0, 100, 100, 90, 90); // 100: pt2X and pt2Y, 90: startAngle and sweepAngle
55     skiaPath.ArcTo(0, 0, 90, PathDirection::CW_DIRECTION, 90, 90); // 90: angle, 90: endX and endY
56     skiaPath.ArcTo(0, 0, 90, 90, 10); // 90: x2 and y2, 10: radius
57     skiaPath.CubicTo(0, 0, 100, 100, 100, 200); // 100: angle, direction and endX, 200: endY
58     skiaPath.QuadTo(0, 0, 100, 100); // 100: endX and endY
59     skiaPath.RMoveTo(100, 100); // 100: dx and dy
60     skiaPath.RLineTo(200, 200); // 200: dx and dy
61     skiaPath.RCubicTo(0, 0, 100, 100, 100, 200); // 200: dx and dy
62     skiaPath.RQuadTo(0, 0, 100, 100); // 100: dx2 and dy2
63     skiaPath.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
64     skiaPath.AddArc(0, 0, 100, 100, 90, 90); // 100: right, bottom, 90: startAngle, sweepAngle
65     skiaPath.AddCircle(0, 0, 100, PathDirection::CW_DIRECTION); // 100: radius
66     skiaPath.AddRoundRect(0, 0,
67         100, 100, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom, xRadius, yRadius
68     Path path;
69     skiaPath.AddPath(path, 200, 200, PathAddMode::APPEND_PATH_ADD_MODE); // 200: dx and dy
70     skiaPath.AddPath(path, PathAddMode::APPEND_PATH_ADD_MODE);
71     skiaPath.ReverseAddPath(path);
72     Matrix matrix;
73     skiaPath.AddPath(path, matrix, PathAddMode::APPEND_PATH_ADD_MODE);
74     skiaPath.SetFillStyle(PathFillType::WINDING);
75     Path path2;
76     path2.MoveTo(0, 0);
77     path2.LineTo(100, 100); // 100: x, y
78     skiaPath.Interpolate(path, 2, path2); // 2: weight
79     EXPECT_FALSE(path2.IsEmpty());
80     skiaPath.Transform(matrix);
81     std::string svgString = skiaPath.ConvertToSVGString();
82     SkiaPath skiaPath2;
83     skiaPath2.InitWithSVGString(svgString);
84     EXPECT_TRUE(skiaPath2.GetLength(true) >= 0);
85     EXPECT_TRUE(skiaPath2.Contains(0, 0));
86     EXPECT_TRUE(!skiaPath2.Deserialize(nullptr));
87 }
88 
89 /**
90  * @tc.name: SkiaPathTestGetFillStyle001
91  * @tc.desc: Test SkiaPath's GetFillStyle
92  * @tc.type: FUNC
93  * @tc.require: IB742Z
94  */
95 HWTEST_F(SkiaPathTest, SkiaPathTestGetFillStyle001, TestSize.Level1)
96 {
97     SkiaPath skiaPath;
98     skiaPath.MoveTo(0, 0);
99     skiaPath.LineTo(100, 100); // 100: x, y
100     skiaPath.LineTo(100, 0); // 100: x
101     skiaPath.SetFillStyle(PathFillType::WINDING);
102     EXPECT_TRUE(skiaPath.GetFillStyle() == PathFillType::WINDING);
103     skiaPath.SetFillStyle(PathFillType::INVERSE_WINDING);
104     EXPECT_TRUE(skiaPath.GetFillStyle() == PathFillType::INVERSE_WINDING);
105 }
106 
107 /**
108  * @tc.name: RArcTo001
109  * @tc.desc: Test RArcTo
110  * @tc.type: FUNC
111  * @tc.require: I8VQSW
112  */
113 HWTEST_F(SkiaPathTest, RArcTo001, TestSize.Level1)
114 {
115     SkiaPath skiaPath;
116     skiaPath.RArcTo(0, 0, 90, PathDirection::CW_DIRECTION, 90, 90); // 90: angle, 90: endX and endY
117     ASSERT_TRUE(skiaPath.IsValid());
118 }
119 
120 /**
121  * @tc.name: InitWithInterpolate001
122  * @tc.desc: Test InitWithInterpolate
123  * @tc.type: FUNC
124  * @tc.require: I8VQSW
125  */
126 HWTEST_F(SkiaPathTest, InitWithInterpolate001, TestSize.Level1)
127 {
128     SkiaPath skiaPath;
129     Path path;
130     Path path2;
131     skiaPath.InitWithInterpolate(path, path2, 2); // 2: weight
132     ASSERT_TRUE(!skiaPath.IsValid());
133 }
134 
135 /**
136  * @tc.name: TransformWithPerspectiveClip001
137  * @tc.desc: Test TransformWithPerspectiveClip
138  * @tc.type: FUNC
139  * @tc.require: I8VQSW
140  */
141 HWTEST_F(SkiaPathTest, TransformWithPerspectiveClip001, TestSize.Level1)
142 {
143     SkiaPath skiaPath;
144     Path path;
145     Matrix matrix;
146     skiaPath.TransformWithPerspectiveClip(matrix, &path, true);
147     ASSERT_TRUE(!skiaPath.IsValid());
148 }
149 
150 /**
151  * @tc.name: OpWith001
152  * @tc.desc: Test OpWith
153  * @tc.type: FUNC
154  * @tc.require: I8VQSW
155  */
156 HWTEST_F(SkiaPathTest, OpWith001, TestSize.Level1)
157 {
158     SkiaPath skiaPath;
159     Path path;
160     Path path2;
161     skiaPath.OpWith(path, path2, PathOp::DIFFERENCE);
162     ASSERT_TRUE(!skiaPath.IsValid());
163 }
164 
165 /**
166  * @tc.name: Offset001
167  * @tc.desc: Test Offset
168  * @tc.type: FUNC
169  * @tc.require: I8VQSW
170  */
171 HWTEST_F(SkiaPathTest, Offset001, TestSize.Level1)
172 {
173     SkiaPath skiaPath;
174     skiaPath.Offset(100, 100);
175     ASSERT_TRUE(!skiaPath.IsValid());
176 }
177 
178 /**
179  * @tc.name: Offset002
180  * @tc.desc: Test Offset
181  * @tc.type: FUNC
182  * @tc.require: I8VQSW
183  */
184 HWTEST_F(SkiaPathTest, Offset002, TestSize.Level1)
185 {
186     SkiaPath skiaPath;
187     Path path;
188     skiaPath.Offset(&path, 100, 100);
189     ASSERT_TRUE(!skiaPath.IsValid());
190 }
191 
192 /**
193  * @tc.name: SkiaPathGetPositionAndTangent002
194  * @tc.desc: Test GetPositionAndTangent
195  * @tc.type: FUNC
196  * @tc.require: I8VQSW
197  */
198 HWTEST_F(SkiaPathTest, SkiaPathGetPositionAndTangent002, TestSize.Level1)
199 {
200     Point position;
201     Point tangent;
202     bool ret = false;
203     SkiaPath skiaPathTmp; // test no path
204     ret = skiaPathTmp.GetPositionAndTangent(10, position, tangent, true); // 10: distance
205     EXPECT_FALSE(ret);
206 
207     SkiaPath skiaPath; // test path add oval
208     skiaPath.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
209     ret = skiaPath.GetPositionAndTangent(0, position, tangent, true);
210     EXPECT_TRUE(ret);
211     ret = skiaPath.GetPositionAndTangent(std::nanf(""), position, tangent, true);
212     EXPECT_FALSE(ret);
213     ret = skiaPath.GetPositionAndTangent(10, position, tangent, true); // 10: distance
214     EXPECT_TRUE(ret);
215 }
216 
217 /**
218  * @tc.name: SkiaPathIsClosed001
219  * @tc.desc: Test IsClosed
220  * @tc.type: FUNC
221  * @tc.require: I8VQSW
222  */
223 HWTEST_F(SkiaPathTest, SkiaPathIsClosed001, TestSize.Level1)
224 {
225     SkiaPath skiaPath;
226     bool ret = false;
227     skiaPath.MoveTo(0, 0);
228     skiaPath.LineTo(100, 100); // 100: x, y
229     ret = skiaPath.IsClosed(false);
230     EXPECT_FALSE(ret);
231     ret = skiaPath.IsClosed(true);
232     EXPECT_TRUE(ret);
233 
234     SkiaPath skiaPathOval;
235     skiaPathOval.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
236     ret = skiaPathOval.IsClosed(false);
237     EXPECT_TRUE(ret);
238     ret = skiaPathOval.IsClosed(true);
239     EXPECT_TRUE(ret);
240 }
241 
242 /**
243  * @tc.name: SkiaPathGetMatrix004
244  * @tc.desc: Test GetMatrix
245  * @tc.type: FUNC
246  * @tc.require: I8VQSW
247  */
248 HWTEST_F(SkiaPathTest, SkiaPathGetMatrix004, TestSize.Level1)
249 {
250     SkiaPath skiaPath;
251     Matrix matrix;
252     bool ret = false;
253     ret = skiaPath.GetMatrix(false, 0, &matrix, PathMeasureMatrixFlags::GET_POSITION_MATRIX);
254     EXPECT_FALSE(ret);
255     ret = skiaPath.GetMatrix(false, -10, &matrix, PathMeasureMatrixFlags::GET_POSITION_MATRIX); // -10: distance
256     EXPECT_FALSE(ret);
257     ret = skiaPath.GetMatrix(false, 10, nullptr, PathMeasureMatrixFlags::GET_POSITION_MATRIX); // 10: distance
258     EXPECT_FALSE(ret);
259     skiaPath.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
260     ret = skiaPath.GetMatrix(true, 10, &matrix, PathMeasureMatrixFlags::GET_POSITION_MATRIX); // 10: distance
261     EXPECT_TRUE(ret);
262 }
263 
264 /**
265  * @tc.name: SkiaPathIsEmpty001
266  * @tc.desc: Test IsEmpty
267  * @tc.type: FUNC
268  * @tc.require: I8VQSW
269  */
270 HWTEST_F(SkiaPathTest, SkiaPathIsEmpty001, TestSize.Level1)
271 {
272     SkiaPath skiaPath;
273     ASSERT_TRUE(skiaPath.IsEmpty());
274     SkiaPath skiaPath1;
275     skiaPath1.MoveTo(1.0f, 2.0f);
276     skiaPath1.LineTo(3.0f, 4.0f);
277     ASSERT_FALSE(skiaPath1.IsEmpty());
278 }
279 
280 /**
281  * @tc.name: SkiaPathIsRect001
282  * @tc.desc: Test GetMatrix
283  * @tc.type: FUNC
284  * @tc.require: I8VQSW
285  */
286 HWTEST_F(SkiaPathTest, SkiaPathIsRect001, TestSize.Level1)
287 {
288     SkiaPath skiaPath1;
289     Rect rect;
290     bool isClosed = false;
291     PathDirection dir;
292     skiaPath1.MoveTo(1.0f, 2.0f);
293     skiaPath1.LineTo(2.0f, 2.0f);
294     skiaPath1.LineTo(2.0f, 3.0f);
295     skiaPath1.LineTo(1.0f, 3.0f);
296     skiaPath1.LineTo(1.0f, 2.0f);
297     ASSERT_TRUE(skiaPath1.IsRect(&rect, &isClosed, &dir));
298     SkiaPath skiaPath2;
299     skiaPath2.MoveTo(1.0f, 2.0f);
300     skiaPath2.LineTo(2.0f, 2.0f);
301     ASSERT_FALSE(skiaPath2.IsRect(&rect, nullptr, nullptr));
302     ASSERT_FALSE(skiaPath2.IsRect(&rect, &isClosed, nullptr));
303     ASSERT_FALSE(skiaPath2.IsRect(&rect, &isClosed, &dir));
304     ASSERT_FALSE(skiaPath2.IsRect(&rect, &isClosed, nullptr));
305     ASSERT_FALSE(skiaPath2.IsRect(&rect, nullptr, &dir));
306 }
307 
308 /**
309  * @tc.name: SkiaPathCountVerbs001
310  * @tc.desc: Test CountVerbs
311  * @tc.type: FUNC
312  * @tc.require: ICAWXU
313  */
314 HWTEST_F(SkiaPathTest, SkiaPathCountVerbs001, TestSize.Level1)
315 {
316     SkiaPath skiaPath;
317     skiaPath.MoveTo(1.0f, 2.0f); // 1.0f: x, 2.0f: y
318     skiaPath.LineTo(3.0f, 4.0f); // 3.0f: x, 4.0f: y
319     skiaPath.LineTo(5.0f, 6.0f); // 5.0f: x, 6.0f: y
320     skiaPath.Close();
321     int count = skiaPath.CountVerbs();
322     ASSERT_EQ(count, 4); // There are 4 verbs in the path.
323 }
324 
325 /**
326  * @tc.name: SkiaPathGetPoint001
327  * @tc.desc: Test GetPoint
328  * @tc.type: FUNC
329  * @tc.require: ICAWXU
330  */
331 HWTEST_F(SkiaPathTest, SkiaPathGetPoint001, TestSize.Level1)
332 {
333     SkiaPath skiaPath;
334     skiaPath.MoveTo(1.0f, 2.0f); // 1.0f: x, 2.0f: y
335     skiaPath.LineTo(3.0f, 4.0f); // 3.0f: x, 4.0f: y
336     Point point1 = skiaPath.GetPoint(0);
337     Point point2 = skiaPath.GetPoint(1);
338     Point point3 = skiaPath.GetPoint(-1);
339     ASSERT_EQ(point1, Point(1.0f, 2.0f)); // 1.0f: x, 2.0f: y
340     ASSERT_EQ(point2, Point(3.0f, 4.0f)); // 3.0f: x, 4.0f: y
341     ASSERT_EQ(point3, Point(0.0f, 0.0f));
342 }
343 
344 /**
345  * @tc.name: SkiaPathIsInterpolate001
346  * @tc.desc: Test IsInterpolate
347  * @tc.type: FUNC
348  * @tc.require: ICAWXU
349  */
350 HWTEST_F(SkiaPathTest, SkiaPathIsInterpolate001, TestSize.Level1)
351 {
352     SkiaPath skiaPath1;
353     Path path;
354     skiaPath1.MoveTo(1.0f, 2.0f); // Start point of the path.
355     skiaPath1.LineTo(3.0f, 4.0f); // End point of the path.
356     path.MoveTo(3.0f, 4.0f); // Start point of the path.
357     path.LineTo(1.0f, 2.0f); // End point of the path.
358     bool ret = skiaPath1.IsInterpolate(path);
359     ASSERT_TRUE(ret);
360 }
361 
362 /**
363  * @tc.name: SkiaPathSetLastPoint001
364  * @tc.desc: Test SetLastPoint
365  * @tc.type: FUNC
366  * @tc.require: I8VQSW
367  */
368 HWTEST_F(SkiaPathTest, SkiaPathSetLastPoint001, TestSize.Level1)
369 {
370     SkiaPath skiaPath;
371     ASSERT_TRUE(skiaPath.IsEmpty());
372     skiaPath.SetLastPoint(1.0f, 2.0f);
373     ASSERT_TRUE(!skiaPath.IsEmpty());
374 }
375 
376 /**
377  * @tc.name: SkiaPathReWind001
378  * @tc.desc: Test SetLastPoint
379  * @tc.type: FUNC
380  * @tc.require: I8VQSW
381  */
382 HWTEST_F(SkiaPathTest, SkiaPathReWind001, TestSize.Level1)
383 {
384     SkiaPath skiaPath;
385     skiaPath.SetLastPoint(1.0f, 2.0f);
386     ASSERT_TRUE(!skiaPath.IsEmpty());
387     skiaPath.ReWind();
388     ASSERT_TRUE(skiaPath.IsEmpty());
389 }
390 
391 /**
392  * @tc.name: SkiaPathSetPath001
393  * @tc.desc: Test SkiaPathSetPath
394  * @tc.type: FUNC
395  * @tc.require: I8VQSW
396  */
397 HWTEST_F(SkiaPathTest, SkiaPathSetPath001, TestSize.Level1)
398 {
399     SkiaPath skiaPath;
400     Path path;
401     path.MoveTo(10.0f, 20.0f);
402     skiaPath.SetPath(path);
403     SkPath skPath = skiaPath.GetPath();
404     ASSERT_TRUE(!skPath.isEmpty());
405 }
406 } // namespace Drawing
407 } // namespace Rosen
408 } // namespace OHOS