VectorDrawableTests.cpp revision 0a1a5167be26d363d4e27bdc7b816f425b7b4e66
1/*
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"
20#include "VectorDrawable.h"
21#include "utils/MathUtils.h"
22#include "utils/VectorDrawableUtils.h"
23
24#include <functional>
25
26namespace android {
27namespace uirenderer {
28
29struct TestData {
30    const char* pathString;
31    const PathData pathData;
32    const std::function<void(SkPath*)> skPathLamda;
33};
34
35const static TestData sTestDataSet[] = {
36    // TestData with scientific notation -2e3 etc.
37    {
38        // Path
39        "M2.000000,22.000000l20.000000,0.000000 1e0-2e3z",
40        {
41            // Verbs
42            {'M', 'l', 'z'},
43            // Verb sizes
44            {2, 4, 0},
45            // Points
46            {2, 22, 20, 0,  1, -2000},
47        },
48        [](SkPath* outPath) {
49            outPath->moveTo(2, 22);
50            outPath->rLineTo(20, 0);
51            outPath->rLineTo(1, -2000);
52            outPath->close();
53            outPath->moveTo(2, 22);
54        }
55    },
56
57    // Comprehensive data, containing all the verbs possible.
58    {
59        // Path
60        "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",
61        {
62            // Verbs
63            {'M', 'm', 'l', 'L', 'H', 'h', 'V', 'v', 'Q', 'q', 't', 'T', 'C', 'c', 'S', 's', 'A', 'a'},
64            // VerbSizes
65            {2, 2, 2, 2, 1, 1, 1, 1, 4, 4, 2, 2, 6, 6, 4, 4, 7, 7},
66            // Points
67            {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, }
68        },
69        [](SkPath* outPath) {
70            outPath->moveTo(1.0, 1.0);
71            outPath->rMoveTo(2.0, 2.0);
72            outPath->rLineTo(3.0, 3.0);
73            outPath->lineTo(3.0, 3.0);
74            outPath->lineTo(4.0, 3.0);
75            outPath->rLineTo(4.0, 0);
76            outPath->lineTo(8.0, 5.0);
77            outPath->rLineTo(0, 5.0);
78            outPath->quadTo(6.0, 6.0, 6.0, 6.0);
79            outPath->rQuadTo(6.0, 6.0, 6.0, 6.0);
80            outPath->rQuadTo(0.0, 0.0, 7.0, 7.0);
81            outPath->quadTo(26.0, 26.0, 7.0, 7.0);
82            outPath->cubicTo(8.0, 8.0, 8.0, 8.0, 8.0, 8.0);
83            outPath->rCubicTo(8.0, 8.0, 8.0, 8.0, 8.0, 8.0);
84            outPath->cubicTo(16.0, 16.0, 9.0, 9.0, 9.0, 9.0);
85            outPath->rCubicTo(0.0, 0.0, 9.0, 9.0, 9.0, 9.0);
86            outPath->cubicTo(18.447775037328352, 20.404243860300607, 17.998389141249767, 22.8911717921705, 16.737515350332117, 24.986664170401575);
87            outPath->cubicTo(15.476641559414468, 27.08215654863265, 13.489843598291483, 28.644011882390082, 11.155893964798905, 29.37447073281729);
88            outPath->cubicTo(8.821944331306327, 30.1049295832445, 6.299226382436471, 29.954422532383525, 4.0686829203897235, 28.951642951534332);
89            outPath->cubicTo(1.838139458342976, 27.94886337068514, 0.05113662931485696, 26.161860541657013, -0.9516429515343354, 23.931317079610267);
90            outPath->cubicTo(-1.9544225323835278, 21.70077361756352, -2.1049295832444987, 19.178055668693663, -1.37447073281729, 16.844106035201087);
91            outPath->cubicTo(-0.6440118823900814, 14.51015640170851, 0.9178434513673546, 12.523358440585524, 3.0133358295984305, 11.262484649667876);
92            outPath->cubicTo(5.108828207829506, 10.001610858750228, 7.5957561396993984, 9.552224962671648, 10.000000000000005, 10.0);
93            outPath->cubicTo(10.0, 7.348852265086975, 11.054287646850167, 4.803576729418881, 12.928932188134523, 2.9289321881345254);
94            outPath->cubicTo(14.803576729418879, 1.0542876468501696, 17.348852265086972, 4.870079381441987E-16, 19.999999999999996, 0.0);
95            outPath->cubicTo(22.65114773491302, -4.870079381441987E-16, 25.19642327058112, 1.0542876468501678, 27.071067811865476, 2.9289321881345227);
96            outPath->cubicTo(28.94571235314983, 4.803576729418878, 30.0, 7.348852265086974, 30.0, 9.999999999999998);
97            outPath->cubicTo(30.0, 12.651147734913023, 28.94571235314983, 15.19642327058112, 27.071067811865476, 17.071067811865476);
98            outPath->cubicTo(25.19642327058112, 18.94571235314983, 22.651147734913028, 20.0, 20.000000000000004, 20.0);
99        }
100    },
101
102    // Check box VectorDrawable path data
103    {
104        // Path
105        "M 0.0,-1.0 l 0.0,0.0 c 0.5522847498,0.0 1.0,0.4477152502 1.0,1.0 l 0.0,0.0 c 0.0,0.5522847498 -0.4477152502,1.0 -1.0,1.0 l 0.0,0.0 c -0.5522847498,0.0 -1.0,-0.4477152502 -1.0,-1.0 l 0.0,0.0 c 0.0,-0.5522847498 0.4477152502,-1.0 1.0,-1.0 Z M 7.0,-9.0 c 0.0,0.0 -14.0,0.0 -14.0,0.0 c -1.1044921875,0.0 -2.0,0.8955078125 -2.0,2.0 c 0.0,0.0 0.0,14.0 0.0,14.0 c 0.0,1.1044921875 0.8955078125,2.0 2.0,2.0 c 0.0,0.0 14.0,0.0 14.0,0.0 c 1.1044921875,0.0 2.0,-0.8955078125 2.0,-2.0 c 0.0,0.0 0.0,-14.0 0.0,-14.0 c 0.0,-1.1044921875 -0.8955078125,-2.0 -2.0,-2.0 c 0.0,0.0 0.0,0.0 0.0,0.0 Z",
106        {
107            {'M', 'l', 'c', 'l', 'c', 'l', 'c', 'l', 'c', 'Z', 'M', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'Z'},
108            {2, 2, 6, 2, 6, 2, 6, 2, 6, 0, 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0},
109            {0.0, -1.0, 0.0, 0.0, 0.5522848, 0.0, 1.0, 0.44771525, 1.0, 1.0, 0.0, 0.0, 0.0, 0.5522848, -0.44771525, 1.0, -1.0, 1.0, 0.0, 0.0, -0.5522848, 0.0, -1.0, -0.44771525, -1.0, -1.0, 0.0, 0.0, 0.0, -0.5522848, 0.44771525, -1.0, 1.0, -1.0, 7.0, -9.0, 0.0, 0.0, -14.0, 0.0, -14.0, 0.0, -1.1044922, 0.0, -2.0, 0.8955078, -2.0, 2.0, 0.0, 0.0, 0.0, 14.0, 0.0, 14.0, 0.0, 1.1044922, 0.8955078, 2.0, 2.0, 2.0, 0.0, 0.0, 14.0, 0.0, 14.0, 0.0, 1.1044922, 0.0, 2.0, -0.8955078, 2.0, -2.0, 0.0, 0.0, 0.0, -14.0, 0.0, -14.0, 0.0, -1.1044922, -0.8955078, -2.0, -2.0, -2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
110        },
111        [](SkPath* outPath) {
112            outPath->moveTo(0.0, -1.0);
113            outPath->rLineTo(0.0, 0.0);
114            outPath->rCubicTo(0.5522848, 0.0, 1.0, 0.44771525, 1.0, 1.0);
115            outPath->rLineTo(0.0, 0.0);
116            outPath->rCubicTo(0.0, 0.5522848, -0.44771525, 1.0, -1.0, 1.0);
117            outPath->rLineTo(0.0, 0.0);
118            outPath->rCubicTo(-0.5522848, 0.0, -1.0, -0.44771525, -1.0, -1.0);
119            outPath->rLineTo(0.0, 0.0);
120            outPath->rCubicTo(0.0, -0.5522848, 0.44771525, -1.0, 1.0, -1.0);
121            outPath->close();
122            outPath->moveTo(0.0, -1.0);
123            outPath->moveTo(7.0, -9.0);
124            outPath->rCubicTo(0.0, 0.0, -14.0, 0.0, -14.0, 0.0);
125            outPath->rCubicTo(-1.1044922, 0.0, -2.0, 0.8955078, -2.0, 2.0);
126            outPath->rCubicTo(0.0, 0.0, 0.0, 14.0, 0.0, 14.0);
127            outPath->rCubicTo(0.0, 1.1044922, 0.8955078, 2.0, 2.0, 2.0);
128            outPath->rCubicTo(0.0, 0.0, 14.0, 0.0, 14.0, 0.0);
129            outPath->rCubicTo(1.1044922, 0.0, 2.0, -0.8955078, 2.0, -2.0);
130            outPath->rCubicTo(0.0, 0.0, 0.0, -14.0, 0.0, -14.0);
131            outPath->rCubicTo(0.0, -1.1044922, -0.8955078, -2.0, -2.0, -2.0);
132            outPath->rCubicTo(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
133            outPath->close();
134            outPath->moveTo(7.0, -9.0);
135        }
136    },
137
138    // pie1 in progress bar
139    {
140        "M300,70 a230,230 0 1,0 1,0 z",
141        {
142             {'M', 'a', 'z', },
143             {2, 7, 0, },
144             {300.0, 70.0, 230.0, 230.0, 0.0, 1.0, 0.0, 1.0, 0.0, },
145        },
146        [](SkPath* outPath) {
147            outPath->moveTo(300.0, 70.0);
148            outPath->cubicTo(239.06697794203706, 70.13246340443499, 180.6164396449267, 94.47383115953485, 137.6004913602211, 137.6302781499585);
149            outPath->cubicTo(94.58454307551551, 180.78672514038215, 70.43390412842275, 239.3163266242308, 70.50013586976587, 300.2494566687817);
150            outPath->cubicTo(70.56636761110899, 361.1825867133326, 94.84418775550249, 419.65954850554147, 137.9538527586204, 462.72238058830936);
151            outPath->cubicTo(181.06351776173827, 505.7852126710772, 239.5668339599056, 529.999456521097, 300.49999999999994, 529.999456521097);
152            outPath->cubicTo(361.43316604009436, 529.999456521097, 419.93648223826176, 505.78521267107726, 463.0461472413797, 462.7223805883093);
153            outPath->cubicTo(506.1558122444976, 419.65954850554135, 530.433632388891, 361.1825867133324, 530.4998641302341, 300.2494566687815);
154            outPath->cubicTo(530.5660958715771, 239.31632662423056, 506.4154569244844, 180.7867251403819, 463.3995086397787, 137.6302781499583);
155            outPath->cubicTo(420.383560355073, 94.47383115953468, 361.93302205796255, 70.13246340443492, 300.9999999999996, 70.00000000000003);
156            outPath->close();
157            outPath->moveTo(300.0, 70.0);
158        }
159    },
160
161    // Random long data
162    {
163        // Path
164        "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",
165        {
166            // Verbs
167            {'M', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'c', 'C', 'z'},
168            // Verb sizes
169            {2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0},
170            // Points
171            {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},
172        },
173        [](SkPath* outPath) {
174            outPath->moveTo(5.3, 13.2);
175            outPath->rCubicTo(-0.1, 0.0, -0.3, 0.0, -0.4, -0.1);
176            outPath->rCubicTo(-0.3, -0.2, -0.4, -0.7, -0.2, -1.0);
177            outPath->rCubicTo(1.3, -1.9, 2.9, -3.4, 4.9, -4.5);
178            outPath->rCubicTo(4.1, -2.2, 9.3, -2.2, 13.4, 0.0);
179            outPath->rCubicTo(1.9, 1.1, 3.6, 2.5, 4.9, 4.4);
180            outPath->rCubicTo(0.2, 0.3, 0.1, 0.8, -0.2, 1.0);
181            outPath->rCubicTo(-0.3, 0.2, -0.8, 0.1, -1.0, -0.2);
182            outPath->rCubicTo(-1.2, -1.7, -2.6, -3.0, -4.3, -4.0);
183            outPath->rCubicTo(-3.7, -2.0, -8.3, -2.0, -12.0, 0.0);
184            outPath->rCubicTo(-1.7, 0.9, -3.2, 2.3, -4.3, 4.0);
185            outPath->cubicTo(5.7, 13.1, 5.5, 13.2, 5.3, 13.2);
186            outPath->close();
187            outPath->moveTo(5.3, 13.2);
188        }
189    },
190
191    // Extreme case with numbers and decimal points crunched together
192    {
193        // Path
194        "l0.0.0.5.0.0.5-0.5.0.0-.5z",
195        {
196            // Verbs
197            {'l', 'z'},
198            // Verb sizes
199            {10, 0},
200            // Points
201            {0, 0, 0.5, 0, 0, 0.5, -0.5, 0, 0, -0.5},
202        },
203        [](SkPath* outPath) {
204            outPath->rLineTo(0.0, 0.0);
205            outPath->rLineTo(0.5, 0.0);
206            outPath->rLineTo(0.0, 0.5);
207            outPath->rLineTo(-0.5, 0.0);
208            outPath->rLineTo(0.0, -0.5);
209            outPath->close();
210            outPath->moveTo(0.0, 0.0);
211        }
212    },
213
214    // Empty test data
215    {
216        "",
217        {
218                // Verbs
219                {},
220                {},
221                {},
222        },
223        [](SkPath* outPath) {}
224    }
225
226};
227
228struct StringPath {
229    const char* stringPath;
230    bool isValid;
231};
232
233const StringPath sStringPaths[] = {
234    {"3e...3", false}, // Not starting with a verb and ill-formatted float
235    {"L.M.F.A.O", false}, // No floats following verbs
236    {"m 1 1", true}, // Valid path data
237    {"z", true}, // Valid path data
238    {"1-2e34567", false}, // Not starting with a verb and ill-formatted float
239    {"f 4 5", false} // Invalid verb
240};
241
242
243static bool hasSameVerbs(const PathData& from, const PathData& to) {
244    return from.verbs == to.verbs && from.verbSizes == to.verbSizes;
245}
246
247TEST(PathParser, parseStringForData) {
248    for (TestData testData: sTestDataSet) {
249        PathParser::ParseResult result;
250        // Test generated path data against the given data.
251        PathData pathData;
252        size_t length = strlen(testData.pathString);
253        PathParser::getPathDataFromString(&pathData, &result, testData.pathString, length);
254        EXPECT_EQ(testData.pathData, pathData);
255    }
256
257    for (StringPath stringPath : sStringPaths) {
258        PathParser::ParseResult result;
259        PathData pathData;
260        SkPath skPath;
261        PathParser::getPathDataFromString(&pathData, &result,
262                stringPath.stringPath, strlen(stringPath.stringPath));
263        EXPECT_EQ(stringPath.isValid, !result.failureOccurred);
264    }
265}
266
267TEST(VectorDrawableUtils, createSkPathFromPathData) {
268    for (TestData testData: sTestDataSet) {
269        SkPath expectedPath;
270        testData.skPathLamda(&expectedPath);
271        SkPath actualPath;
272        VectorDrawableUtils::verbsToPath(&actualPath, testData.pathData);
273        EXPECT_EQ(expectedPath, actualPath);
274    }
275}
276
277TEST(PathParser, parseStringForSkPath) {
278    for (TestData testData: sTestDataSet) {
279        PathParser::ParseResult result;
280        size_t length = strlen(testData.pathString);
281        // Check the return value as well as the SkPath generated.
282        SkPath actualPath;
283        PathParser::parseStringForSkPath(&actualPath, &result, testData.pathString, length);
284        bool hasValidData = !result.failureOccurred;
285        EXPECT_EQ(hasValidData, testData.pathData.verbs.size() > 0);
286        SkPath expectedPath;
287        testData.skPathLamda(&expectedPath);
288        EXPECT_EQ(expectedPath, actualPath);
289    }
290
291    for (StringPath stringPath : sStringPaths) {
292        PathParser::ParseResult result;
293        SkPath skPath;
294        PathParser::parseStringForSkPath(&skPath, &result, stringPath.stringPath,
295                strlen(stringPath.stringPath));
296        EXPECT_EQ(stringPath.isValid, !result.failureOccurred);
297    }
298}
299
300TEST(VectorDrawableUtils, morphPathData) {
301    for (TestData fromData: sTestDataSet) {
302        for (TestData toData: sTestDataSet) {
303            bool canMorph = VectorDrawableUtils::canMorph(fromData.pathData, toData.pathData);
304            if (fromData.pathData == toData.pathData) {
305                EXPECT_TRUE(canMorph);
306            } else {
307                bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData);
308                EXPECT_EQ(expectedToMorph, canMorph);
309            }
310        }
311    }
312}
313
314TEST(VectorDrawableUtils, interpolatePathData) {
315    // Interpolate path data with itself and every other path data
316    for (TestData fromData: sTestDataSet) {
317        for (TestData toData: sTestDataSet) {
318            PathData outData;
319            bool success = VectorDrawableUtils::interpolatePathData(&outData, fromData.pathData,
320                    toData.pathData, 0.5);
321            bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData);
322            EXPECT_EQ(expectedToMorph, success);
323        }
324    }
325
326    float fractions[] = {0, 0.00001, 0.28, 0.5, 0.7777, 0.9999999, 1};
327    // Now try to interpolate with a slightly modified version of self and expect success
328    for (TestData fromData : sTestDataSet) {
329        PathData toPathData = fromData.pathData;
330        for (size_t i = 0; i < toPathData.points.size(); i++) {
331            toPathData.points[i]++;
332        }
333        const PathData& fromPathData = fromData.pathData;
334        PathData outData;
335        // Interpolate the two path data with different fractions
336        for (float fraction : fractions) {
337            bool success = VectorDrawableUtils::interpolatePathData(
338                    &outData, fromPathData, toPathData, fraction);
339            EXPECT_TRUE(success);
340            for (size_t i = 0; i < outData.points.size(); i++) {
341                float expectedResult = fromPathData.points[i] * (1.0 - fraction) +
342                        toPathData.points[i] * fraction;
343                EXPECT_TRUE(MathUtils::areEqual(expectedResult, outData.points[i]));
344            }
345        }
346    }
347}
348
349TEST(VectorDrawable, matrixScale) {
350    struct MatrixAndScale {
351        float buffer[9];
352        float matrixScale;
353    };
354
355    const MatrixAndScale sMatrixAndScales[] {
356        {
357            {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f},
358            1.0
359        },
360        {
361            {1.0f, 0.0f, 240.0f, 0.0f, 1.0f, 240.0f, 0.0f, 0.0f, 1.0f},
362            1.0f,
363        },
364        {
365            {1.5f, 0.0f, 24.0f, 0.0f, 1.5f, 24.0f, 0.0f, 0.0f, 1.0f},
366            1.5f,
367        },
368        {
369            {0.99999994f, 0.0f, 300.0f, 0.0f, 0.99999994f, 158.57864f, 0.0f, 0.0f, 1.0f},
370            0.99999994f,
371        },
372        {
373            {0.7071067f, 0.7071067f, 402.5305f, -0.7071067f, 0.7071067f, 169.18524f, 0.0f, 0.0f, 1.0f},
374            0.99999994f,
375        },
376        {
377            {0.0f, 0.9999999f, 482.5305f, -0.9999999f, 0.0f, 104.18525f, 0.0f, 0.0f, 1.0f},
378            0.9999999f,
379        },
380        {
381            {-0.35810637f, -0.93368083f, 76.55821f, 0.93368083f, -0.35810637f, 89.538506f, 0.0f, 0.0f, 1.0f},
382            1.0000001f,
383        },
384    };
385
386    for (MatrixAndScale matrixAndScale : sMatrixAndScales) {
387        SkMatrix matrix;
388        matrix.set9(matrixAndScale.buffer);
389        float actualMatrixScale = VectorDrawable::Path::getMatrixScale(matrix);
390        EXPECT_EQ(matrixAndScale.matrixScale, actualMatrixScale);
391    }
392}
393}; // namespace uirenderer
394}; // namespace android
395