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