Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 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" |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 20 | #include "VectorDrawable.h" |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 21 | #include "utils/MathUtils.h" |
| 22 | #include "utils/VectorDrawableUtils.h" |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 23 | |
| 24 | #include <functional> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | struct TestData { |
| 30 | const char* pathString; |
| 31 | const PathData pathData; |
| 32 | const std::function<void(SkPath*)> skPathLamda; |
| 33 | }; |
| 34 | |
Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 35 | const static TestData sTestDataSet[] = { |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 36 | // TestData with scientific notation -2e3 etc. |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 37 | { |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 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 | } |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 55 | }, |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 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 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 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 | |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 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) {} |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 224 | } |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 225 | |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 228 | struct StringPath { |
| 229 | const char* stringPath; |
| 230 | bool isValid; |
| 231 | }; |
| 232 | |
| 233 | const StringPath sStringPaths[] = { |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 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 |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 237 | {"\n \t z", true}, // Valid path data with leading spaces |
Doris Liu | 0a1a516 | 2016-04-07 15:03:11 -0700 | [diff] [blame] | 238 | {"1-2e34567", false}, // Not starting with a verb and ill-formatted float |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 239 | {"f 4 5", false}, // Invalid verb |
| 240 | {"\r ", false} // Empty string |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 241 | }; |
| 242 | |
Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 243 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 244 | static bool hasSameVerbs(const PathData& from, const PathData& to) { |
| 245 | return from.verbs == to.verbs && from.verbSizes == to.verbSizes; |
| 246 | } |
| 247 | |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 248 | TEST(PathParser, parseStringForData) { |
Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 249 | for (TestData testData: sTestDataSet) { |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 250 | PathParser::ParseResult result; |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 251 | // Test generated path data against the given data. |
| 252 | PathData pathData; |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 253 | size_t length = strlen(testData.pathString); |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 254 | PathParser::getPathDataFromAsciiString(&pathData, &result, testData.pathString, length); |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 255 | EXPECT_EQ(testData.pathData, pathData); |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 256 | } |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 257 | |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 258 | for (StringPath stringPath : sStringPaths) { |
| 259 | PathParser::ParseResult result; |
| 260 | PathData pathData; |
| 261 | SkPath skPath; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 262 | PathParser::getPathDataFromAsciiString(&pathData, &result, |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 263 | stringPath.stringPath, strlen(stringPath.stringPath)); |
| 264 | EXPECT_EQ(stringPath.isValid, !result.failureOccurred); |
| 265 | } |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 268 | TEST(VectorDrawableUtils, createSkPathFromPathData) { |
Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 269 | for (TestData testData: sTestDataSet) { |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 270 | SkPath expectedPath; |
| 271 | testData.skPathLamda(&expectedPath); |
| 272 | SkPath actualPath; |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 273 | VectorDrawableUtils::verbsToPath(&actualPath, testData.pathData); |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 274 | EXPECT_EQ(expectedPath, actualPath); |
| 275 | } |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 276 | } |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 277 | |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 278 | TEST(PathParser, parseAsciiStringForSkPath) { |
Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 279 | for (TestData testData: sTestDataSet) { |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 280 | PathParser::ParseResult result; |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 281 | size_t length = strlen(testData.pathString); |
| 282 | // Check the return value as well as the SkPath generated. |
| 283 | SkPath actualPath; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 284 | PathParser::parseAsciiStringForSkPath(&actualPath, &result, testData.pathString, length); |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 285 | bool hasValidData = !result.failureOccurred; |
Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 286 | EXPECT_EQ(hasValidData, testData.pathData.verbs.size() > 0); |
| 287 | SkPath expectedPath; |
| 288 | testData.skPathLamda(&expectedPath); |
| 289 | EXPECT_EQ(expectedPath, actualPath); |
| 290 | } |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 291 | |
| 292 | for (StringPath stringPath : sStringPaths) { |
| 293 | PathParser::ParseResult result; |
| 294 | SkPath skPath; |
Doris Liu | b35da39 | 2016-04-12 11:06:23 -0700 | [diff] [blame] | 295 | PathParser::parseAsciiStringForSkPath(&skPath, &result, stringPath.stringPath, |
Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 296 | strlen(stringPath.stringPath)); |
| 297 | EXPECT_EQ(stringPath.isValid, !result.failureOccurred); |
| 298 | } |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 301 | TEST(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 | |
| 315 | TEST(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 | |
Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame] | 350 | TEST(VectorDrawable, groupProperties) { |
| 351 | //TODO: Also need to test property sync and dirty flag when properties change. |
| 352 | VectorDrawable::Group group; |
| 353 | VectorDrawable::Group::GroupProperties* properties = group.mutateProperties(); |
| 354 | // Test default values, change values through setters and verify the change through getters. |
| 355 | EXPECT_EQ(0.0f, properties->getTranslateX()); |
| 356 | properties->setTranslateX(1.0f); |
| 357 | EXPECT_EQ(1.0f, properties->getTranslateX()); |
| 358 | |
| 359 | EXPECT_EQ(0.0f, properties->getTranslateY()); |
| 360 | properties->setTranslateY(1.0f); |
| 361 | EXPECT_EQ(1.0f, properties->getTranslateY()); |
| 362 | |
| 363 | EXPECT_EQ(0.0f, properties->getRotation()); |
| 364 | properties->setRotation(1.0f); |
| 365 | EXPECT_EQ(1.0f, properties->getRotation()); |
| 366 | |
| 367 | EXPECT_EQ(1.0f, properties->getScaleX()); |
| 368 | properties->setScaleX(0.0f); |
| 369 | EXPECT_EQ(0.0f, properties->getScaleX()); |
| 370 | |
| 371 | EXPECT_EQ(1.0f, properties->getScaleY()); |
| 372 | properties->setScaleY(0.0f); |
| 373 | EXPECT_EQ(0.0f, properties->getScaleY()); |
| 374 | |
| 375 | EXPECT_EQ(0.0f, properties->getPivotX()); |
| 376 | properties->setPivotX(1.0f); |
| 377 | EXPECT_EQ(1.0f, properties->getPivotX()); |
| 378 | |
| 379 | EXPECT_EQ(0.0f, properties->getPivotY()); |
| 380 | properties->setPivotY(1.0f); |
| 381 | EXPECT_EQ(1.0f, properties->getPivotY()); |
| 382 | |
| 383 | } |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 384 | |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 385 | TEST(VectorDrawable, drawPathWithoutIncrementingShaderRefCount) { |
| 386 | VectorDrawable::FullPath path("m1 1", 4); |
| 387 | SkBitmap bitmap; |
Mike Reed | 2653f83 | 2017-05-26 09:12:46 -0400 | [diff] [blame] | 388 | bitmap.allocN32Pixels(5, 5, false); |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 389 | SkCanvas canvas(bitmap); |
| 390 | |
Mike Reed | 2653f83 | 2017-05-26 09:12:46 -0400 | [diff] [blame] | 391 | sk_sp<SkShader> shader = SkShader::MakeColorShader(SK_ColorBLACK); |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 392 | // Initial ref count is 1 |
Mike Reed | 2653f83 | 2017-05-26 09:12:46 -0400 | [diff] [blame] | 393 | EXPECT_TRUE(shader->unique()); |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 394 | |
| 395 | // Setting the fill gradient increments the ref count of the shader by 1 |
Mike Reed | 2653f83 | 2017-05-26 09:12:46 -0400 | [diff] [blame] | 396 | path.mutateStagingProperties()->setFillGradient(shader.get()); |
| 397 | EXPECT_FALSE(shader->unique()); |
Stan Iliev | cc29a5d | 2017-03-15 16:37:10 -0400 | [diff] [blame] | 398 | path.draw(&canvas, true); |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 399 | // Resetting the fill gradient decrements the ref count of the shader by 1 |
| 400 | path.mutateStagingProperties()->setFillGradient(nullptr); |
| 401 | |
Mike Reed | 2653f83 | 2017-05-26 09:12:46 -0400 | [diff] [blame] | 402 | EXPECT_TRUE(shader->unique()); |
Doris Liu | fc9cf72 | 2016-10-10 15:50:01 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 405 | }; // namespace uirenderer |
| 406 | }; // namespace android |