blob: 77dd73acff1017d36d18434010b500b605810e3f [file] [log] [blame]
Doris Liu30bcf692015-11-04 14:56:24 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include "PathParser.h"
Doris Liu804618d2015-11-16 22:48:34 -080020#include "utils/MathUtils.h"
21#include "utils/VectorDrawableUtils.h"
Doris Liu30bcf692015-11-04 14:56:24 -080022
23#include <functional>
24
25namespace android {
26namespace uirenderer {
27
28struct TestData {
29 const char* pathString;
30 const PathData pathData;
31 const std::function<void(SkPath*)> skPathLamda;
32};
33
Doris Liub89610c2015-11-12 10:36:28 -080034const static TestData sTestDataSet[] = {
Doris Liucdd23f92015-11-11 14:31:13 -080035 // TestData with scientific notation -2e3 etc.
Doris Liu30bcf692015-11-04 14:56:24 -080036 {
Doris Liucdd23f92015-11-11 14:31:13 -080037 // Path
38 "M2.000000,22.000000l20.000000,0.000000 1e0-2e3z",
39 {
40 // Verbs
41 {'M', 'l', 'z'},
42 // Verb sizes
43 {2, 4, 0},
44 // Points
45 {2, 22, 20, 0, 1, -2000},
46 },
47 [](SkPath* outPath) {
48 outPath->moveTo(2, 22);
49 outPath->rLineTo(20, 0);
50 outPath->rLineTo(1, -2000);
51 outPath->close();
52 outPath->moveTo(2, 22);
53 }
Doris Liu30bcf692015-11-04 14:56:24 -080054 },
Doris Liucdd23f92015-11-11 14:31:13 -080055
56 // Comprehensive data, containing all the verbs possible.
57 {
58 // Path
59 "M 1 1 m 2 2, l 3 3 L 3 3 H 4 h4 V5 v5, Q6 6 6 6 q 6 6 6 6t 7 7 T 7 7 C 8 8 8 8 8 8 c 8 8 8 8 8 8 S 9 9 9 9 s 9 9 9 9 A 10 10 0 1 1 10 10 a 10 10 0 1 1 10 10",
60 {
61 // Verbs
62 {'M', 'm', 'l', 'L', 'H', 'h', 'V', 'v', 'Q', 'q', 't', 'T', 'C', 'c', 'S', 's', 'A', 'a'},
63 // VerbSizes
64 {2, 2, 2, 2, 1, 1, 1, 1, 4, 4, 2, 2, 6, 6, 4, 4, 7, 7},
65 // Points
66 {1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 5.0, 5.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 6.0, 7.0, 7.0, 7.0, 7.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 10.0, 10.0, 0.0, 1.0, 1.0, 10.0, 10.0, 10.0, 10.0, 0.0, 1.0, 1.0, 10.0, 10.0, }
67 },
68 [](SkPath* outPath) {
69 outPath->moveTo(1.0, 1.0);
70 outPath->rMoveTo(2.0, 2.0);
71 outPath->rLineTo(3.0, 3.0);
72 outPath->lineTo(3.0, 3.0);
73 outPath->lineTo(4.0, 3.0);
74 outPath->rLineTo(4.0, 0);
75 outPath->lineTo(8.0, 5.0);
76 outPath->rLineTo(0, 5.0);
77 outPath->quadTo(6.0, 6.0, 6.0, 6.0);
78 outPath->rQuadTo(6.0, 6.0, 6.0, 6.0);
79 outPath->rQuadTo(0.0, 0.0, 7.0, 7.0);
80 outPath->quadTo(26.0, 26.0, 7.0, 7.0);
81 outPath->cubicTo(8.0, 8.0, 8.0, 8.0, 8.0, 8.0);
82 outPath->rCubicTo(8.0, 8.0, 8.0, 8.0, 8.0, 8.0);
83 outPath->cubicTo(16.0, 16.0, 9.0, 9.0, 9.0, 9.0);
84 outPath->rCubicTo(0.0, 0.0, 9.0, 9.0, 9.0, 9.0);
85 outPath->cubicTo(18.447775037328352, 20.404243860300607, 17.998389141249767, 22.8911717921705, 16.737515350332117, 24.986664170401575);
86 outPath->cubicTo(15.476641559414468, 27.08215654863265, 13.489843598291483, 28.644011882390082, 11.155893964798905, 29.37447073281729);
87 outPath->cubicTo(8.821944331306327, 30.1049295832445, 6.299226382436471, 29.954422532383525, 4.0686829203897235, 28.951642951534332);
88 outPath->cubicTo(1.838139458342976, 27.94886337068514, 0.05113662931485696, 26.161860541657013, -0.9516429515343354, 23.931317079610267);
89 outPath->cubicTo(-1.9544225323835278, 21.70077361756352, -2.1049295832444987, 19.178055668693663, -1.37447073281729, 16.844106035201087);
90 outPath->cubicTo(-0.6440118823900814, 14.51015640170851, 0.9178434513673546, 12.523358440585524, 3.0133358295984305, 11.262484649667876);
91 outPath->cubicTo(5.108828207829506, 10.001610858750228, 7.5957561396993984, 9.552224962671648, 10.000000000000005, 10.0);
92 outPath->cubicTo(10.0, 7.348852265086975, 11.054287646850167, 4.803576729418881, 12.928932188134523, 2.9289321881345254);
93 outPath->cubicTo(14.803576729418879, 1.0542876468501696, 17.348852265086972, 4.870079381441987E-16, 19.999999999999996, 0.0);
94 outPath->cubicTo(22.65114773491302, -4.870079381441987E-16, 25.19642327058112, 1.0542876468501678, 27.071067811865476, 2.9289321881345227);
95 outPath->cubicTo(28.94571235314983, 4.803576729418878, 30.0, 7.348852265086974, 30.0, 9.999999999999998);
96 outPath->cubicTo(30.0, 12.651147734913023, 28.94571235314983, 15.19642327058112, 27.071067811865476, 17.071067811865476);
97 outPath->cubicTo(25.19642327058112, 18.94571235314983, 22.651147734913028, 20.0, 20.000000000000004, 20.0);
98 }
99 },
100
101 // Random long data
102 {
103 // Path
104 "M5.3,13.2c-0.1,0.0 -0.3,0.0 -0.4,-0.1c-0.3,-0.2 -0.4,-0.7 -0.2,-1.0c1.3,-1.9 2.9,-3.4 4.9,-4.5c4.1,-2.2 9.3,-2.2 13.4,0.0c1.9,1.1 3.6,2.5 4.9,4.4c0.2,0.3 0.1,0.8 -0.2,1.0c-0.3,0.2 -0.8,0.1 -1.0,-0.2c-1.2,-1.7 -2.6,-3.0 -4.3,-4.0c-3.7,-2.0 -8.3,-2.0 -12.0,0.0c-1.7,0.9 -3.2,2.3 -4.3,4.0C5.7,13.1 5.5,13.2 5.3,13.2z",
105 {
106 // Verbs
107 {'M', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'C', 'z'},
108 // Verb sizes
109 {2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0},
110 // Points
111 {5.3, 13.2, -0.1, 0, -0.3, 0, -0.4, -0.1, -0.3, -0.2, -0.4, -0.7, -0.2, -1, 1.3, -1.9, 2.9, -3.4, 4.9, -4.5, 4.1, -2.2, 9.3, -2.2, 13.4, 0, 1.9, 1.1, 3.6, 2.5, 4.9, 4.4, 0.2, 0.3, 0.1, 0.8, -0.2, 1, -0.3, 0.2, -0.8, 0.1, -1, -0.2, -1.2, -1.7, -2.6, -3, -4.3, -4, -3.7, -2, -8.3, -2, -12, 0, -1.7, 0.9, -3.2, 2.3, -4.3, 4, 5.7, 13.1, 5.5, 13.2, 5.3, 13.2},
112 },
113 [](SkPath* outPath) {
114 outPath->moveTo(5.3, 13.2);
115 outPath->rCubicTo(-0.1, 0.0, -0.3, 0.0, -0.4, -0.1);
116 outPath->rCubicTo(-0.3, -0.2, -0.4, -0.7, -0.2, -1.0);
117 outPath->rCubicTo(1.3, -1.9, 2.9, -3.4, 4.9, -4.5);
118 outPath->rCubicTo(4.1, -2.2, 9.3, -2.2, 13.4, 0.0);
119 outPath->rCubicTo(1.9, 1.1, 3.6, 2.5, 4.9, 4.4);
120 outPath->rCubicTo(0.2, 0.3, 0.1, 0.8, -0.2, 1.0);
121 outPath->rCubicTo(-0.3, 0.2, -0.8, 0.1, -1.0, -0.2);
122 outPath->rCubicTo(-1.2, -1.7, -2.6, -3.0, -4.3, -4.0);
123 outPath->rCubicTo(-3.7, -2.0, -8.3, -2.0, -12.0, 0.0);
124 outPath->rCubicTo(-1.7, 0.9, -3.2, 2.3, -4.3, 4.0);
125 outPath->cubicTo(5.7, 13.1, 5.5, 13.2, 5.3, 13.2);
126 outPath->close();
127 outPath->moveTo(5.3, 13.2);
128 }
129 },
130
131 // Extreme case with numbers and decimal points crunched together
132 {
133 // Path
134 "l0.0.0.5.0.0.5-0.5.0.0-.5z",
135 {
136 // Verbs
137 {'l', 'z'},
138 // Verb sizes
139 {10, 0},
140 // Points
141 {0, 0, 0.5, 0, 0, 0.5, -0.5, 0, 0, -0.5},
142 },
143 [](SkPath* outPath) {
144 outPath->rLineTo(0.0, 0.0);
145 outPath->rLineTo(0.5, 0.0);
146 outPath->rLineTo(0.0, 0.5);
147 outPath->rLineTo(-0.5, 0.0);
148 outPath->rLineTo(0.0, -0.5);
149 outPath->close();
150 outPath->moveTo(0.0, 0.0);
151 }
152 },
153
154 // Empty test data
155 {
156 "",
157 {
158 // Verbs
159 {},
160 {},
161 {},
162 },
163 [](SkPath* outPath) {}
Doris Liu30bcf692015-11-04 14:56:24 -0800164 }
Doris Liucdd23f92015-11-11 14:31:13 -0800165
Doris Liu30bcf692015-11-04 14:56:24 -0800166};
167
Doris Liu1e67f082015-11-12 15:57:45 -0800168struct StringPath {
169 const char* stringPath;
170 bool isValid;
171};
172
173const StringPath sStringPaths[] = {
174 {"3e...3", false},
175 {"L.M.F.A.O", false},
176 {"m 1 1", true},
177 {"z", true},
178 {"1-2e34567", false}
179};
180
Doris Liu804618d2015-11-16 22:48:34 -0800181static bool hasSameVerbs(const PathData& from, const PathData& to) {
182 return from.verbs == to.verbs && from.verbSizes == to.verbSizes;
183}
184
Doris Liucdd23f92015-11-11 14:31:13 -0800185TEST(PathParser, parseStringForData) {
Doris Liub89610c2015-11-12 10:36:28 -0800186 for (TestData testData: sTestDataSet) {
Doris Liu1e67f082015-11-12 15:57:45 -0800187 PathParser::ParseResult result;
Doris Liu30bcf692015-11-04 14:56:24 -0800188 // Test generated path data against the given data.
189 PathData pathData;
Doris Liu30bcf692015-11-04 14:56:24 -0800190 size_t length = strlen(testData.pathString);
Doris Liu1e67f082015-11-12 15:57:45 -0800191 PathParser::getPathDataFromString(&pathData, &result, testData.pathString, length);
Doris Liu30bcf692015-11-04 14:56:24 -0800192 EXPECT_EQ(testData.pathData, pathData);
Doris Liucdd23f92015-11-11 14:31:13 -0800193 }
Doris Liu30bcf692015-11-04 14:56:24 -0800194
Doris Liu1e67f082015-11-12 15:57:45 -0800195 for (StringPath stringPath : sStringPaths) {
196 PathParser::ParseResult result;
197 PathData pathData;
198 SkPath skPath;
199 PathParser::getPathDataFromString(&pathData, &result,
200 stringPath.stringPath, strlen(stringPath.stringPath));
201 EXPECT_EQ(stringPath.isValid, !result.failureOccurred);
202 }
Doris Liucdd23f92015-11-11 14:31:13 -0800203}
204
Doris Liu804618d2015-11-16 22:48:34 -0800205TEST(VectorDrawableUtils, createSkPathFromPathData) {
Doris Liub89610c2015-11-12 10:36:28 -0800206 for (TestData testData: sTestDataSet) {
Doris Liu30bcf692015-11-04 14:56:24 -0800207 SkPath expectedPath;
208 testData.skPathLamda(&expectedPath);
209 SkPath actualPath;
Doris Liu804618d2015-11-16 22:48:34 -0800210 VectorDrawableUtils::verbsToPath(&actualPath, testData.pathData);
Doris Liu30bcf692015-11-04 14:56:24 -0800211 EXPECT_EQ(expectedPath, actualPath);
212 }
Doris Liucdd23f92015-11-11 14:31:13 -0800213}
Doris Liu30bcf692015-11-04 14:56:24 -0800214
Doris Liucdd23f92015-11-11 14:31:13 -0800215TEST(PathParser, parseStringForSkPath) {
Doris Liub89610c2015-11-12 10:36:28 -0800216 for (TestData testData: sTestDataSet) {
Doris Liu1e67f082015-11-12 15:57:45 -0800217 PathParser::ParseResult result;
Doris Liucdd23f92015-11-11 14:31:13 -0800218 size_t length = strlen(testData.pathString);
219 // Check the return value as well as the SkPath generated.
220 SkPath actualPath;
Doris Liu1e67f082015-11-12 15:57:45 -0800221 PathParser::parseStringForSkPath(&actualPath, &result, testData.pathString, length);
222 bool hasValidData = !result.failureOccurred;
Doris Liucdd23f92015-11-11 14:31:13 -0800223 EXPECT_EQ(hasValidData, testData.pathData.verbs.size() > 0);
224 SkPath expectedPath;
225 testData.skPathLamda(&expectedPath);
226 EXPECT_EQ(expectedPath, actualPath);
227 }
Doris Liu1e67f082015-11-12 15:57:45 -0800228
229 for (StringPath stringPath : sStringPaths) {
230 PathParser::ParseResult result;
231 SkPath skPath;
232 PathParser::parseStringForSkPath(&skPath, &result, stringPath.stringPath,
233 strlen(stringPath.stringPath));
234 EXPECT_EQ(stringPath.isValid, !result.failureOccurred);
235 }
Doris Liu30bcf692015-11-04 14:56:24 -0800236}
237
Doris Liu804618d2015-11-16 22:48:34 -0800238TEST(VectorDrawableUtils, morphPathData) {
239 for (TestData fromData: sTestDataSet) {
240 for (TestData toData: sTestDataSet) {
241 bool canMorph = VectorDrawableUtils::canMorph(fromData.pathData, toData.pathData);
242 if (fromData.pathData == toData.pathData) {
243 EXPECT_TRUE(canMorph);
244 } else {
245 bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData);
246 EXPECT_EQ(expectedToMorph, canMorph);
247 }
248 }
249 }
250}
251
252TEST(VectorDrawableUtils, interpolatePathData) {
253 // Interpolate path data with itself and every other path data
254 for (TestData fromData: sTestDataSet) {
255 for (TestData toData: sTestDataSet) {
256 PathData outData;
257 bool success = VectorDrawableUtils::interpolatePathData(&outData, fromData.pathData,
258 toData.pathData, 0.5);
259 bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData);
260 EXPECT_EQ(expectedToMorph, success);
261 }
262 }
263
264 float fractions[] = {0, 0.00001, 0.28, 0.5, 0.7777, 0.9999999, 1};
265 // Now try to interpolate with a slightly modified version of self and expect success
266 for (TestData fromData : sTestDataSet) {
267 PathData toPathData = fromData.pathData;
268 for (size_t i = 0; i < toPathData.points.size(); i++) {
269 toPathData.points[i]++;
270 }
271 const PathData& fromPathData = fromData.pathData;
272 PathData outData;
273 // Interpolate the two path data with different fractions
274 for (float fraction : fractions) {
275 bool success = VectorDrawableUtils::interpolatePathData(
276 &outData, fromPathData, toPathData, fraction);
277 EXPECT_TRUE(success);
278 for (size_t i = 0; i < outData.points.size(); i++) {
279 float expectedResult = fromPathData.points[i] * (1.0 - fraction) +
280 toPathData.points[i] * fraction;
281 EXPECT_TRUE(MathUtils::areEqual(expectedResult, outData.points[i]));
282 }
283 }
284 }
285}
286
287
Doris Liu30bcf692015-11-04 14:56:24 -0800288}; // namespace uirenderer
289}; // namespace android