1 /*
2 * Copyright (c) 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, Hardware
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 <vector>
17 #include "gtest/gtest.h"
18 #include "draw/path.h"
19 #include "draw/path_iterator.h"
20 #include "utils/point.h"
21 #include "utils/utils_path.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 namespace Drawing {
29
30 class UtilsPathTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void UtilsPathTest::SetUpTestCase() {}
TearDownTestCase()39 void UtilsPathTest::TearDownTestCase() {}
SetUp()40 void UtilsPathTest::SetUp() {}
TearDown()41 void UtilsPathTest::TearDown() {}
42
43 /**
44 * @tc.name: UtilsPathDistance001
45 * @tc.desc: Test Distance
46 * @tc.type: FUNC
47 * @tc.require: ICAWXU
48 */
49 HWTEST_F(UtilsPathTest, UtilsPathDistance001, TestSize.Level1)
50 {
51 Point p1(0, 0);
52 Point p2(3, 4); // 3, 4 are x, y coordinates
53 scalar dist = UtilsPath::Distance(p1, p2);
54 EXPECT_FLOAT_EQ(dist, 5.0f); // 5.0f is the expected Euclidean distance
55 }
56
57 /**
58 * @tc.name: UtilsPathAddMove001
59 * @tc.desc: Test AddMove
60 * @tc.type: FUNC
61 * @tc.require: ICAWXU
62 */
63 HWTEST_F(UtilsPathTest, UtilsPathAddMove001, TestSize.Level1)
64 {
65 std::vector<Point> points;
66 std::vector<float> lengths;
67 Point p(1, 2); // 1, 2 are x, y coordinates
68 UtilsPath::AddMove(p, points, lengths);
69 EXPECT_EQ(points.size(), 1);
70 EXPECT_EQ(lengths.size(), 1);
71 EXPECT_EQ(points[0], p);
72 EXPECT_FLOAT_EQ(lengths[0], 0.0f);
73 }
74
75 /**
76 * @tc.name: UtilsPathAddLine001
77 * @tc.desc: Test AddLine
78 * @tc.type: FUNC
79 * @tc.require: ICAWXU
80 */
81 HWTEST_F(UtilsPathTest, UtilsPathAddLine001, TestSize.Level1)
82 {
83 std::vector<Point> points;
84 std::vector<float> lengths;
85 Point p1(0, 0);
86 Point p2(2, 0); // 2, 0 are x, y coordinates
87 UtilsPath::AddMove(p1, points, lengths);
88 UtilsPath::AddLine(p2, points, lengths);
89 EXPECT_EQ(points.size(), 2); // Should have two points now
90 EXPECT_EQ(lengths.size(), 2); // Should have two lengths now
91 EXPECT_EQ(points[1], p2);
92 UtilsPath::AddLine(p2, points, lengths);
93 EXPECT_EQ(points.size(), 2); // Should still have two points
94 }
95
96 /**
97 * @tc.name: UtilsPathCalculateQuadraticBezier001
98 * @tc.desc: Test CalculateQuadraticBezier
99 * @tc.type: FUNC
100 * @tc.require: ICAWXU
101 */
102 HWTEST_F(UtilsPathTest, UtilsPathCalculateQuadraticBezier001, TestSize.Level1)
103 {
104 Point pts[3] = { Point(0, 0), Point(1, 2), Point(2, 0) }; // 1, 2 are x, y coordinates
105 Point p0 = UtilsPath::CalculateQuadraticBezier(0.0f, pts);
106 EXPECT_EQ(p0, pts[0]);
107 Point p1 = UtilsPath::CalculateQuadraticBezier(1.0f, pts);
108 EXPECT_EQ(p1, pts[2]);
109 Point pmid = UtilsPath::CalculateQuadraticBezier(0.5f, pts); // 0.5f should be the midpoint
110 EXPECT_EQ(pmid, Point(1, 1));
111 }
112
113 /**
114 * @tc.name: UtilsPathCalculateCubicBezier001
115 * @tc.desc: Test CalculateCubicBezier
116 * @tc.type: FUNC
117 * @tc.require: ICAWXU
118 */
119 HWTEST_F(UtilsPathTest, UtilsPathCalculateCubicBezier001, TestSize.Level1)
120 {
121 Point pts[4] = { Point(0, 0), Point(1, 3), Point(3, 3), Point(4, 0) }; // 1, 3, 4 are x, y coordinates
122 Point p0 = UtilsPath::CalculateCubicBezier(0.0f, pts);
123 EXPECT_EQ(p0, pts[0]);
124 Point p1 = UtilsPath::CalculateCubicBezier(1.0f, pts);
125 EXPECT_EQ(p1, pts[3]);
126 Point pmid = UtilsPath::CalculateCubicBezier(0.5f, pts); // 0.5f should be the midpoint
127 EXPECT_EQ(pmid, Point(2.0f, 2.25f)); // 2.0f, 2.25f are x, y coordinates
128 }
129
130 /**
131 * @tc.name: UtilsPathAddBezier001
132 * @tc.desc: Test AddBezier
133 * @tc.type: FUNC
134 * @tc.require: ICAWXU
135 */
136 HWTEST_F(UtilsPathTest, UtilsPathAddBezier001, TestSize.Level1)
137 {
138 Point pts1[3] = { Point(0, 0), Point(1, 2), Point(2, 0) }; // 1, 2 are x, y coordinates
139 std::vector<Point> approxPoints1;
140 std::vector<float> approxLengths1;
141 // 100.0f for the test
142 UtilsPath::AddBezier(pts1, UtilsPath::CalculateQuadraticBezier, approxPoints1, approxLengths1, 100.0f, false);
143 EXPECT_EQ(approxPoints1.size(), 3); // 3 for the test
144 Point pts2[3] = { Point(0, 0), Point(100, 200), Point(200, 0) }; // 100, 200 are x, y coordinates
145 std::vector<Point> approxPoints2;
146 std::vector<float> approxLengths2;
147 UtilsPath::AddBezier(pts2, UtilsPath::CalculateQuadraticBezier, approxPoints2, approxLengths2, 1.0f, false);
148 EXPECT_EQ(approxPoints2.size(), 18); // 18 for the test
149 Point pts3[3] = { Point(0, 0), Point(1, 2), Point(2, 0) }; // 1, 2 are x, y coordinates
150 std::vector<Point> approxPoints3;
151 std::vector<float> approxLengths3;
152 // 100.0f for the test
153 UtilsPath::AddBezier(pts3, UtilsPath::CalculateQuadraticBezier, approxPoints3, approxLengths3, 100.0f, true);
154 EXPECT_EQ(approxPoints3.size(), 3); // 3 for the test
155 Point pts4[3] = { Point(0, 0), Point(100, 200), Point(200, 0) }; // 100, 200 are x, y coordinates
156 std::vector<Point> approxPoints4;
157 std::vector<float> approxLengths4;
158 // 0.5f for the test
159 UtilsPath::AddBezier(pts4, UtilsPath::CalculateQuadraticBezier, approxPoints4, approxLengths4, 0.5f, true);
160 EXPECT_EQ(approxPoints4.size(), 18); // 18 for the test
161 }
162
163 /**
164 * @tc.name: UtilsPathAddConic001
165 * @tc.desc: Test AddConic
166 * @tc.type: FUNC
167 * @tc.require: ICAWXU
168 */
169 HWTEST_F(UtilsPathTest, UtilsPathAddConic001, TestSize.Level1)
170 {
171 Path path;
172 path.MoveTo(0, 0);
173 path.ConicTo(1, 2, 3, 0, 1.0f); // 1, 2, 3 are x, y coordinates, 1.0 is weight
174 PathIter iter(path, false);
175 PathVerb verb;
176 Point pts[4] = {};
177 do {
178 verb = iter.Next(pts);
179 } while (verb != PathVerb::CONIC && verb != PathVerb::DONE);
180 if (verb == PathVerb::CONIC) {
181 std::vector<Point> approxPoints;
182 std::vector<float> approxLengths;
183 UtilsPath::AddConic(iter, pts, approxPoints, approxLengths, 0.5f); // 0.5 is errorConic
184 EXPECT_EQ(approxPoints.size(), 2); // 2 for the test
185 }
186 }
187
188 } // namespace Drawing
189 } // namespace Rosen
190 } // namespace OHOS
191