| 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[] = { |
| 234 | {"3e...3", false}, |
| 235 | {"L.M.F.A.O", false}, |
| 236 | {"m 1 1", true}, |
| 237 | {"z", true}, |
| 238 | {"1-2e34567", false} |
| 239 | }; |
| 240 | |
| Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 241 | |
| Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 242 | static bool hasSameVerbs(const PathData& from, const PathData& to) { |
| 243 | return from.verbs == to.verbs && from.verbSizes == to.verbSizes; |
| 244 | } |
| 245 | |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 246 | TEST(PathParser, parseStringForData) { |
| Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 247 | for (TestData testData: sTestDataSet) { |
| Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 248 | PathParser::ParseResult result; |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 249 | // Test generated path data against the given data. |
| 250 | PathData pathData; |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 251 | size_t length = strlen(testData.pathString); |
| Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 252 | PathParser::getPathDataFromString(&pathData, &result, testData.pathString, length); |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 253 | EXPECT_EQ(testData.pathData, pathData); |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 254 | } |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 255 | |
| Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 256 | for (StringPath stringPath : sStringPaths) { |
| 257 | PathParser::ParseResult result; |
| 258 | PathData pathData; |
| 259 | SkPath skPath; |
| 260 | PathParser::getPathDataFromString(&pathData, &result, |
| 261 | stringPath.stringPath, strlen(stringPath.stringPath)); |
| 262 | EXPECT_EQ(stringPath.isValid, !result.failureOccurred); |
| 263 | } |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 266 | TEST(VectorDrawableUtils, createSkPathFromPathData) { |
| Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 267 | for (TestData testData: sTestDataSet) { |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 268 | SkPath expectedPath; |
| 269 | testData.skPathLamda(&expectedPath); |
| 270 | SkPath actualPath; |
| Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 271 | VectorDrawableUtils::verbsToPath(&actualPath, testData.pathData); |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 272 | EXPECT_EQ(expectedPath, actualPath); |
| 273 | } |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 274 | } |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 275 | |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 276 | TEST(PathParser, parseStringForSkPath) { |
| Doris Liu | b89610c | 2015-11-12 10:36:28 -0800 | [diff] [blame] | 277 | for (TestData testData: sTestDataSet) { |
| Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 278 | PathParser::ParseResult result; |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 279 | size_t length = strlen(testData.pathString); |
| 280 | // Check the return value as well as the SkPath generated. |
| 281 | SkPath actualPath; |
| Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 282 | PathParser::parseStringForSkPath(&actualPath, &result, testData.pathString, length); |
| 283 | bool hasValidData = !result.failureOccurred; |
| Doris Liu | cdd23f9 | 2015-11-11 14:31:13 -0800 | [diff] [blame] | 284 | EXPECT_EQ(hasValidData, testData.pathData.verbs.size() > 0); |
| 285 | SkPath expectedPath; |
| 286 | testData.skPathLamda(&expectedPath); |
| 287 | EXPECT_EQ(expectedPath, actualPath); |
| 288 | } |
| Doris Liu | 1e67f08 | 2015-11-12 15:57:45 -0800 | [diff] [blame] | 289 | |
| 290 | for (StringPath stringPath : sStringPaths) { |
| 291 | PathParser::ParseResult result; |
| 292 | SkPath skPath; |
| 293 | PathParser::parseStringForSkPath(&skPath, &result, stringPath.stringPath, |
| 294 | strlen(stringPath.stringPath)); |
| 295 | EXPECT_EQ(stringPath.isValid, !result.failureOccurred); |
| 296 | } |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 299 | TEST(VectorDrawableUtils, morphPathData) { |
| 300 | for (TestData fromData: sTestDataSet) { |
| 301 | for (TestData toData: sTestDataSet) { |
| 302 | bool canMorph = VectorDrawableUtils::canMorph(fromData.pathData, toData.pathData); |
| 303 | if (fromData.pathData == toData.pathData) { |
| 304 | EXPECT_TRUE(canMorph); |
| 305 | } else { |
| 306 | bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData); |
| 307 | EXPECT_EQ(expectedToMorph, canMorph); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | TEST(VectorDrawableUtils, interpolatePathData) { |
| 314 | // Interpolate path data with itself and every other path data |
| 315 | for (TestData fromData: sTestDataSet) { |
| 316 | for (TestData toData: sTestDataSet) { |
| 317 | PathData outData; |
| 318 | bool success = VectorDrawableUtils::interpolatePathData(&outData, fromData.pathData, |
| 319 | toData.pathData, 0.5); |
| 320 | bool expectedToMorph = hasSameVerbs(fromData.pathData, toData.pathData); |
| 321 | EXPECT_EQ(expectedToMorph, success); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | float fractions[] = {0, 0.00001, 0.28, 0.5, 0.7777, 0.9999999, 1}; |
| 326 | // Now try to interpolate with a slightly modified version of self and expect success |
| 327 | for (TestData fromData : sTestDataSet) { |
| 328 | PathData toPathData = fromData.pathData; |
| 329 | for (size_t i = 0; i < toPathData.points.size(); i++) { |
| 330 | toPathData.points[i]++; |
| 331 | } |
| 332 | const PathData& fromPathData = fromData.pathData; |
| 333 | PathData outData; |
| 334 | // Interpolate the two path data with different fractions |
| 335 | for (float fraction : fractions) { |
| 336 | bool success = VectorDrawableUtils::interpolatePathData( |
| 337 | &outData, fromPathData, toPathData, fraction); |
| 338 | EXPECT_TRUE(success); |
| 339 | for (size_t i = 0; i < outData.points.size(); i++) { |
| 340 | float expectedResult = fromPathData.points[i] * (1.0 - fraction) + |
| 341 | toPathData.points[i] * fraction; |
| 342 | EXPECT_TRUE(MathUtils::areEqual(expectedResult, outData.points[i])); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 348 | TEST(VectorDrawable, matrixScale) { |
| 349 | struct MatrixAndScale { |
| 350 | float buffer[9]; |
| 351 | float matrixScale; |
| 352 | }; |
| Doris Liu | 804618d | 2015-11-16 22:48:34 -0800 | [diff] [blame] | 353 | |
| Doris Liu | 4bbc293 | 2015-12-01 17:59:40 -0800 | [diff] [blame] | 354 | const MatrixAndScale sMatrixAndScales[] { |
| 355 | { |
| 356 | {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}, |
| 357 | 1.0 |
| 358 | }, |
| 359 | { |
| 360 | {1.0f, 0.0f, 240.0f, 0.0f, 1.0f, 240.0f, 0.0f, 0.0f, 1.0f}, |
| 361 | 1.0f, |
| 362 | }, |
| 363 | { |
| 364 | {1.5f, 0.0f, 24.0f, 0.0f, 1.5f, 24.0f, 0.0f, 0.0f, 1.0f}, |
| 365 | 1.5f, |
| 366 | }, |
| 367 | { |
| 368 | {0.99999994f, 0.0f, 300.0f, 0.0f, 0.99999994f, 158.57864f, 0.0f, 0.0f, 1.0f}, |
| 369 | 0.99999994f, |
| 370 | }, |
| 371 | { |
| 372 | {0.7071067f, 0.7071067f, 402.5305f, -0.7071067f, 0.7071067f, 169.18524f, 0.0f, 0.0f, 1.0f}, |
| 373 | 0.99999994f, |
| 374 | }, |
| 375 | { |
| 376 | {0.0f, 0.9999999f, 482.5305f, -0.9999999f, 0.0f, 104.18525f, 0.0f, 0.0f, 1.0f}, |
| 377 | 0.9999999f, |
| 378 | }, |
| 379 | { |
| 380 | {-0.35810637f, -0.93368083f, 76.55821f, 0.93368083f, -0.35810637f, 89.538506f, 0.0f, 0.0f, 1.0f}, |
| 381 | 1.0000001f, |
| 382 | }, |
| 383 | }; |
| 384 | |
| 385 | for (MatrixAndScale matrixAndScale : sMatrixAndScales) { |
| 386 | SkMatrix matrix; |
| 387 | matrix.set9(matrixAndScale.buffer); |
| 388 | float actualMatrixScale = VectorDrawable::Path::getMatrixScale(matrix); |
| 389 | EXPECT_EQ(matrixAndScale.matrixScale, actualMatrixScale); |
| 390 | } |
| 391 | } |
| Doris Liu | 32d7cda | 2016-04-08 13:48:47 -0700 | [diff] [blame^] | 392 | |
| 393 | TEST(VectorDrawable, groupProperties) { |
| 394 | //TODO: Also need to test property sync and dirty flag when properties change. |
| 395 | VectorDrawable::Group group; |
| 396 | VectorDrawable::Group::GroupProperties* properties = group.mutateProperties(); |
| 397 | // Test default values, change values through setters and verify the change through getters. |
| 398 | EXPECT_EQ(0.0f, properties->getTranslateX()); |
| 399 | properties->setTranslateX(1.0f); |
| 400 | EXPECT_EQ(1.0f, properties->getTranslateX()); |
| 401 | |
| 402 | EXPECT_EQ(0.0f, properties->getTranslateY()); |
| 403 | properties->setTranslateY(1.0f); |
| 404 | EXPECT_EQ(1.0f, properties->getTranslateY()); |
| 405 | |
| 406 | EXPECT_EQ(0.0f, properties->getRotation()); |
| 407 | properties->setRotation(1.0f); |
| 408 | EXPECT_EQ(1.0f, properties->getRotation()); |
| 409 | |
| 410 | EXPECT_EQ(1.0f, properties->getScaleX()); |
| 411 | properties->setScaleX(0.0f); |
| 412 | EXPECT_EQ(0.0f, properties->getScaleX()); |
| 413 | |
| 414 | EXPECT_EQ(1.0f, properties->getScaleY()); |
| 415 | properties->setScaleY(0.0f); |
| 416 | EXPECT_EQ(0.0f, properties->getScaleY()); |
| 417 | |
| 418 | EXPECT_EQ(0.0f, properties->getPivotX()); |
| 419 | properties->setPivotX(1.0f); |
| 420 | EXPECT_EQ(1.0f, properties->getPivotX()); |
| 421 | |
| 422 | EXPECT_EQ(0.0f, properties->getPivotY()); |
| 423 | properties->setPivotY(1.0f); |
| 424 | EXPECT_EQ(1.0f, properties->getPivotY()); |
| 425 | |
| 426 | } |
| Doris Liu | 30bcf69 | 2015-11-04 14:56:24 -0800 | [diff] [blame] | 427 | }; // namespace uirenderer |
| 428 | }; // namespace android |