VectorDrawableTests.cpp revision fc9cf72339c7ce61adb11ceb3b247f112577fb6b
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    {"\n \t   z", true}, // Valid path data with leading spaces
238    {"1-2e34567", false}, // Not starting with a verb and ill-formatted float
239    {"f 4 5", false}, // Invalid verb
240    {"\r      ", false} // Empty string
241};
242
243
244static bool hasSameVerbs(const PathData& from, const PathData& to) {
245    return from.verbs == to.verbs && from.verbSizes == to.verbSizes;
246}
247
248TEST(PathParser, parseStringForData) {
249    for (TestData testData: sTestDataSet) {
250        PathParser::ParseResult result;
251        // Test generated path data against the given data.
252        PathData pathData;
253        size_t length = strlen(testData.pathString);
254        PathParser::getPathDataFromAsciiString(&pathData, &result, testData.pathString, length);
255        EXPECT_EQ(testData.pathData, pathData);
256    }
257
258    for (StringPath stringPath : sStringPaths) {
259        PathParser::ParseResult result;
260        PathData pathData;
261        SkPath skPath;
262        PathParser::getPathDataFromAsciiString(&pathData, &result,
263                stringPath.stringPath, strlen(stringPath.stringPath));
264        EXPECT_EQ(stringPath.isValid, !result.failureOccurred);
265    }
266}
267
268TEST(VectorDrawableUtils, createSkPathFromPathData) {
269    for (TestData testData: sTestDataSet) {
270        SkPath expectedPath;
271        testData.skPathLamda(&expectedPath);
272        SkPath actualPath;
273        VectorDrawableUtils::verbsToPath(&actualPath, testData.pathData);
274        EXPECT_EQ(expectedPath, actualPath);
275    }
276}
277
278TEST(PathParser, parseAsciiStringForSkPath) {
279    for (TestData testData: sTestDataSet) {
280        PathParser::ParseResult result;
281        size_t length = strlen(testData.pathString);
282        // Check the return value as well as the SkPath generated.
283        SkPath actualPath;
284        PathParser::parseAsciiStringForSkPath(&actualPath, &result, testData.pathString, length);
285        bool hasValidData = !result.failureOccurred;
286        EXPECT_EQ(hasValidData, testData.pathData.verbs.size() > 0);
287        SkPath expectedPath;
288        testData.skPathLamda(&expectedPath);
289        EXPECT_EQ(expectedPath, actualPath);
290    }
291
292    for (StringPath stringPath : sStringPaths) {
293        PathParser::ParseResult result;
294        SkPath skPath;
295        PathParser::parseAsciiStringForSkPath(&skPath, &result, stringPath.stringPath,
296                strlen(stringPath.stringPath));
297        EXPECT_EQ(stringPath.isValid, !result.failureOccurred);
298    }
299}
300
301TEST(VectorDrawableUtils, morphPathData) {
302    for (TestData fromData: sTestDataSet) {
303        for (TestData toData: sTestDataSet) {
304            bool canMorph = VectorDrawableUtils::canMorph(fromData.pathData, toData.pathData);
305            if (fromData.pathData == toData.pathData) {
306                EXPECT_TRUE(canMorph);
307            } else {
308                bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData);
309                EXPECT_EQ(expectedToMorph, canMorph);
310            }
311        }
312    }
313}
314
315TEST(VectorDrawableUtils, interpolatePathData) {
316    // Interpolate path data with itself and every other path data
317    for (TestData fromData: sTestDataSet) {
318        for (TestData toData: sTestDataSet) {
319            PathData outData;
320            bool success = VectorDrawableUtils::interpolatePathData(&outData, fromData.pathData,
321                    toData.pathData, 0.5);
322            bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData);
323            EXPECT_EQ(expectedToMorph, success);
324        }
325    }
326
327    float fractions[] = {0, 0.00001, 0.28, 0.5, 0.7777, 0.9999999, 1};
328    // Now try to interpolate with a slightly modified version of self and expect success
329    for (TestData fromData : sTestDataSet) {
330        PathData toPathData = fromData.pathData;
331        for (size_t i = 0; i < toPathData.points.size(); i++) {
332            toPathData.points[i]++;
333        }
334        const PathData& fromPathData = fromData.pathData;
335        PathData outData;
336        // Interpolate the two path data with different fractions
337        for (float fraction : fractions) {
338            bool success = VectorDrawableUtils::interpolatePathData(
339                    &outData, fromPathData, toPathData, fraction);
340            EXPECT_TRUE(success);
341            for (size_t i = 0; i < outData.points.size(); i++) {
342                float expectedResult = fromPathData.points[i] * (1.0 - fraction) +
343                        toPathData.points[i] * fraction;
344                EXPECT_TRUE(MathUtils::areEqual(expectedResult, outData.points[i]));
345            }
346        }
347    }
348}
349
350TEST(VectorDrawable, matrixScale) {
351    struct MatrixAndScale {
352        float buffer[9];
353        float matrixScale;
354    };
355
356    const MatrixAndScale sMatrixAndScales[] {
357        {
358            {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f},
359            1.0
360        },
361        {
362            {1.0f, 0.0f, 240.0f, 0.0f, 1.0f, 240.0f, 0.0f, 0.0f, 1.0f},
363            1.0f,
364        },
365        {
366            {1.5f, 0.0f, 24.0f, 0.0f, 1.5f, 24.0f, 0.0f, 0.0f, 1.0f},
367            1.5f,
368        },
369        {
370            {0.99999994f, 0.0f, 300.0f, 0.0f, 0.99999994f, 158.57864f, 0.0f, 0.0f, 1.0f},
371            0.99999994f,
372        },
373        {
374            {0.7071067f, 0.7071067f, 402.5305f, -0.7071067f, 0.7071067f, 169.18524f, 0.0f, 0.0f, 1.0f},
375            0.99999994f,
376        },
377        {
378            {0.0f, 0.9999999f, 482.5305f, -0.9999999f, 0.0f, 104.18525f, 0.0f, 0.0f, 1.0f},
379            0.9999999f,
380        },
381        {
382            {-0.35810637f, -0.93368083f, 76.55821f, 0.93368083f, -0.35810637f, 89.538506f, 0.0f, 0.0f, 1.0f},
383            1.0000001f,
384        },
385    };
386
387    for (MatrixAndScale matrixAndScale : sMatrixAndScales) {
388        SkMatrix matrix;
389        matrix.set9(matrixAndScale.buffer);
390        float actualMatrixScale = VectorDrawable::Path::getMatrixScale(matrix);
391        EXPECT_EQ(matrixAndScale.matrixScale, actualMatrixScale);
392    }
393}
394
395TEST(VectorDrawable, groupProperties) {
396    //TODO: Also need to test property sync and dirty flag when properties change.
397    VectorDrawable::Group group;
398    VectorDrawable::Group::GroupProperties* properties = group.mutateProperties();
399    // Test default values, change values through setters and verify the change through getters.
400    EXPECT_EQ(0.0f, properties->getTranslateX());
401    properties->setTranslateX(1.0f);
402    EXPECT_EQ(1.0f, properties->getTranslateX());
403
404    EXPECT_EQ(0.0f, properties->getTranslateY());
405    properties->setTranslateY(1.0f);
406    EXPECT_EQ(1.0f, properties->getTranslateY());
407
408    EXPECT_EQ(0.0f, properties->getRotation());
409    properties->setRotation(1.0f);
410    EXPECT_EQ(1.0f, properties->getRotation());
411
412    EXPECT_EQ(1.0f, properties->getScaleX());
413    properties->setScaleX(0.0f);
414    EXPECT_EQ(0.0f, properties->getScaleX());
415
416    EXPECT_EQ(1.0f, properties->getScaleY());
417    properties->setScaleY(0.0f);
418    EXPECT_EQ(0.0f, properties->getScaleY());
419
420    EXPECT_EQ(0.0f, properties->getPivotX());
421    properties->setPivotX(1.0f);
422    EXPECT_EQ(1.0f, properties->getPivotX());
423
424    EXPECT_EQ(0.0f, properties->getPivotY());
425    properties->setPivotY(1.0f);
426    EXPECT_EQ(1.0f, properties->getPivotY());
427
428}
429
430static SkShader* createShader(bool* isDestroyed) {
431    class TestShader : public SkShader {
432    public:
433        TestShader(bool* isDestroyed) : SkShader(), mDestroyed(isDestroyed) {
434        }
435        ~TestShader() {
436            *mDestroyed = true;
437        }
438
439        Factory getFactory() const override { return nullptr; }
440    private:
441        bool* mDestroyed;
442    };
443    return new TestShader(isDestroyed);
444}
445
446TEST(VectorDrawable, drawPathWithoutIncrementingShaderRefCount) {
447    VectorDrawable::FullPath path("m1 1", 4);
448    SkBitmap bitmap;
449    SkImageInfo info = SkImageInfo::Make(5, 5, kN32_SkColorType, kPremul_SkAlphaType);
450    bitmap.setInfo(info);
451    bitmap.allocPixels(info);
452    SkCanvas canvas(bitmap);
453
454    bool shaderIsDestroyed = false;
455
456    // Initial ref count is 1
457    SkShader* shader = createShader(&shaderIsDestroyed);
458
459    // Setting the fill gradient increments the ref count of the shader by 1
460    path.mutateStagingProperties()->setFillGradient(shader);
461    path.draw(&canvas, SkMatrix::I(), 1.0f, 1.0f, true);
462    // Resetting the fill gradient decrements the ref count of the shader by 1
463    path.mutateStagingProperties()->setFillGradient(nullptr);
464
465    // Expect ref count to be 1 again, i.e. nothing else to have a ref to the shader now. Unref()
466    // again should bring the ref count to zero and consequently trigger detor.
467    shader->unref();
468
469    // Verify that detor is called.
470    EXPECT_TRUE(shaderIsDestroyed);
471}
472
473}; // namespace uirenderer
474}; // namespace android
475