1 /*
2 * Copyright (c) 2024 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 skiaPath.Interpolate(path, 2, path2); // 2: weight
77 skiaPath.Transform(matrix);
78 std::string svgString = skiaPath.ConvertToSVGString();
79 SkiaPath skiaPath2;
80 skiaPath2.InitWithSVGString(svgString);
81 EXPECT_TRUE(skiaPath2.GetLength(true) >= 0);
82 EXPECT_TRUE(skiaPath2.Contains(0, 0));
83 EXPECT_TRUE(!skiaPath2.Deserialize(nullptr));
84 }
85
86 /**
87 * @tc.name: SkiaPathTestGetFillStyle001
88 * @tc.desc: Test SkiaPath's GetFillStyle
89 * @tc.type: FUNC
90 * @tc.require: IB742Z
91 */
92 HWTEST_F(SkiaPathTest, SkiaPathTestGetFillStyle001, TestSize.Level1)
93 {
94 SkiaPath skiaPath;
95 skiaPath.MoveTo(0, 0);
96 skiaPath.LineTo(100, 100); // 100: x, y
97 skiaPath.LineTo(100, 0); // 100: x
98 skiaPath.SetFillStyle(PathFillType::WINDING);
99 EXPECT_TRUE(skiaPath.GetFillStyle() == PathFillType::WINDING);
100 skiaPath.SetFillStyle(PathFillType::INVERSE_WINDING);
101 EXPECT_TRUE(skiaPath.GetFillStyle() == PathFillType::INVERSE_WINDING);
102 }
103
104 /**
105 * @tc.name: RArcTo001
106 * @tc.desc: Test RArcTo
107 * @tc.type: FUNC
108 * @tc.require: I8VQSW
109 */
110 HWTEST_F(SkiaPathTest, RArcTo001, TestSize.Level1)
111 {
112 SkiaPath skiaPath;
113 skiaPath.RArcTo(0, 0, 90, PathDirection::CW_DIRECTION, 90, 90); // 90: angle, 90: endX and endY
114 ASSERT_TRUE(skiaPath.IsValid());
115 }
116
117 /**
118 * @tc.name: InitWithInterpolate001
119 * @tc.desc: Test InitWithInterpolate
120 * @tc.type: FUNC
121 * @tc.require: I8VQSW
122 */
123 HWTEST_F(SkiaPathTest, InitWithInterpolate001, TestSize.Level1)
124 {
125 SkiaPath skiaPath;
126 Path path;
127 Path path2;
128 skiaPath.InitWithInterpolate(path, path2, 2); // 2: weight
129 ASSERT_TRUE(!skiaPath.IsValid());
130 }
131
132 /**
133 * @tc.name: TransformWithPerspectiveClip001
134 * @tc.desc: Test TransformWithPerspectiveClip
135 * @tc.type: FUNC
136 * @tc.require: I8VQSW
137 */
138 HWTEST_F(SkiaPathTest, TransformWithPerspectiveClip001, TestSize.Level1)
139 {
140 SkiaPath skiaPath;
141 Path path;
142 Matrix matrix;
143 skiaPath.TransformWithPerspectiveClip(matrix, &path, true);
144 ASSERT_TRUE(!skiaPath.IsValid());
145 }
146
147 /**
148 * @tc.name: OpWith001
149 * @tc.desc: Test OpWith
150 * @tc.type: FUNC
151 * @tc.require: I8VQSW
152 */
153 HWTEST_F(SkiaPathTest, OpWith001, TestSize.Level1)
154 {
155 SkiaPath skiaPath;
156 Path path;
157 Path path2;
158 skiaPath.OpWith(path, path2, PathOp::DIFFERENCE);
159 ASSERT_TRUE(!skiaPath.IsValid());
160 }
161
162 /**
163 * @tc.name: Offset001
164 * @tc.desc: Test Offset
165 * @tc.type: FUNC
166 * @tc.require: I8VQSW
167 */
168 HWTEST_F(SkiaPathTest, Offset001, TestSize.Level1)
169 {
170 SkiaPath skiaPath;
171 skiaPath.Offset(100, 100);
172 ASSERT_TRUE(!skiaPath.IsValid());
173 }
174
175 /**
176 * @tc.name: Offset002
177 * @tc.desc: Test Offset
178 * @tc.type: FUNC
179 * @tc.require: I8VQSW
180 */
181 HWTEST_F(SkiaPathTest, Offset002, TestSize.Level1)
182 {
183 SkiaPath skiaPath;
184 Path path;
185 skiaPath.Offset(&path, 100, 100);
186 ASSERT_TRUE(!skiaPath.IsValid());
187 }
188
189 /**
190 * @tc.name: SkiaPathGetPositionAndTangent002
191 * @tc.desc: Test GetPositionAndTangent
192 * @tc.type: FUNC
193 * @tc.require: I8VQSW
194 */
195 HWTEST_F(SkiaPathTest, SkiaPathGetPositionAndTangent002, TestSize.Level1)
196 {
197 Point position;
198 Point tangent;
199 bool ret = false;
200 SkiaPath skiaPathTmp; // test no path
201 ret = skiaPathTmp.GetPositionAndTangent(10, position, tangent, true); // 10: distance
202 EXPECT_FALSE(ret);
203
204 SkiaPath skiaPath; // test path add oval
205 skiaPath.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
206 ret = skiaPath.GetPositionAndTangent(0, position, tangent, true);
207 EXPECT_TRUE(ret);
208 ret = skiaPath.GetPositionAndTangent(std::nanf(""), position, tangent, true);
209 EXPECT_FALSE(ret);
210 ret = skiaPath.GetPositionAndTangent(10, position, tangent, true); // 10: distance
211 EXPECT_TRUE(ret);
212 }
213
214 /**
215 * @tc.name: SkiaPathIsClosed003
216 * @tc.desc: Test IsClosed
217 * @tc.type: FUNC
218 * @tc.require: I8VQSW
219 */
220 HWTEST_F(SkiaPathTest, SkiaPathIsClosed003, TestSize.Level1)
221 {
222 SkiaPath skiaPath;
223 bool ret = false;
224 skiaPath.MoveTo(0, 0);
225 skiaPath.LineTo(100, 100); // 100: x, y
226 ret = skiaPath.IsClosed(false);
227 EXPECT_FALSE(ret);
228 ret = skiaPath.IsClosed(true);
229 EXPECT_TRUE(ret);
230
231 SkiaPath skiaPathOval;
232 skiaPathOval.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
233 ret = skiaPathOval.IsClosed(false);
234 EXPECT_TRUE(ret);
235 ret = skiaPathOval.IsClosed(true);
236 EXPECT_TRUE(ret);
237 }
238
239 /**
240 * @tc.name: SkiaPathGetMatrix004
241 * @tc.desc: Test GetMatrix
242 * @tc.type: FUNC
243 * @tc.require: I8VQSW
244 */
245 HWTEST_F(SkiaPathTest, SkiaPathGetMatrix004, TestSize.Level1)
246 {
247 SkiaPath skiaPath;
248 Matrix matrix;
249 bool ret = false;
250 ret = skiaPath.GetMatrix(false, 0, &matrix, PathMeasureMatrixFlags::GET_POSITION_MATRIX);
251 EXPECT_FALSE(ret);
252 ret = skiaPath.GetMatrix(false, -10, &matrix, PathMeasureMatrixFlags::GET_POSITION_MATRIX); // -10: distance
253 EXPECT_FALSE(ret);
254 ret = skiaPath.GetMatrix(false, 10, nullptr, PathMeasureMatrixFlags::GET_POSITION_MATRIX); // 10: distance
255 EXPECT_FALSE(ret);
256 skiaPath.AddOval(0, 0, 100, 100, PathDirection::CW_DIRECTION); // 100: right, bottom
257 ret = skiaPath.GetMatrix(true, 10, &matrix, PathMeasureMatrixFlags::GET_POSITION_MATRIX); // 10: distance
258 EXPECT_TRUE(ret);
259 }
260 } // namespace Drawing
261 } // namespace Rosen
262 } // namespace OHOS