epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkParsePath.h" |
| 9 | #include "tests/Test.h" |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 10 | |
| 11 | static void test_to_from(skiatest::Reporter* reporter, const SkPath& path) { |
| 12 | SkString str, str2; |
| 13 | SkParsePath::ToSVGString(path, &str); |
| 14 | |
| 15 | SkPath path2; |
| 16 | bool success = SkParsePath::FromSVGString(str.c_str(), &path2); |
| 17 | REPORTER_ASSERT(reporter, success); |
| 18 | |
| 19 | SkParsePath::ToSVGString(path2, &str2); |
| 20 | REPORTER_ASSERT(reporter, str == str2); |
| 21 | #if 0 // closed paths are not equal, the iter explicitly gives the closing |
| 22 | // edge, even if it is not in the path. |
| 23 | REPORTER_ASSERT(reporter, path == path2); |
| 24 | if (path != path2) { |
| 25 | SkDebugf("str1=%s\nstr2=%s\n", str.c_str(), str2.c_str()); |
| 26 | } |
| 27 | #endif |
| 28 | } |
| 29 | |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 30 | static struct { |
| 31 | const char* fStr; |
| 32 | const SkRect fBounds; |
| 33 | } gRec[] = { |
caryclark | 52ee813 | 2015-01-29 09:45:44 -0800 | [diff] [blame] | 34 | { "M1,1 l-2.58-2.828-3.82-0.113, 1.9-3.3223-1.08-3.6702, 3.75,0.7744,3.16-2.1551," |
| 35 | "0.42,3.8008,3.02,2.3384-3.48,1.574-1.29,3.601z", |
| 36 | { -5.39999962f, -10.3142f, 5.77000046f, 1.f } }, |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 37 | { "", { 0, 0, 0, 0 } }, |
| 38 | { "M0,0L10,10", { 0, 0, SkIntToScalar(10), SkIntToScalar(10) } }, |
| 39 | { "M-5.5,-0.5 Q 0 0 6,6.50", |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 40 | { -5.5f, -0.5f, |
| 41 | 6, 6.5f } } |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 44 | DEF_TEST(ParsePath, reporter) { |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 45 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); i++) { |
| 46 | SkPath path; |
| 47 | bool success = SkParsePath::FromSVGString(gRec[i].fStr, &path); |
| 48 | REPORTER_ASSERT(reporter, success); |
| 49 | const SkRect& expectedBounds = gRec[i].fBounds; |
| 50 | const SkRect& pathBounds = path.getBounds(); |
| 51 | REPORTER_ASSERT(reporter, expectedBounds == pathBounds); |
| 52 | |
| 53 | test_to_from(reporter, path); |
| 54 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 55 | |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 56 | SkRect r; |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 57 | r.set(0, 0, 10, 10.5f); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 58 | SkPath p; |
| 59 | p.addRect(r); |
| 60 | test_to_from(reporter, p); |
| 61 | p.addOval(r); |
| 62 | test_to_from(reporter, p); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 63 | p.addRoundRect(r, 4, 4.5f); |
reed@android.com | bbff1d5 | 2009-06-05 16:21:03 +0000 | [diff] [blame] | 64 | test_to_from(reporter, p); |
| 65 | } |
scroggo | 28e80f4 | 2015-12-11 09:49:57 -0800 | [diff] [blame] | 66 | |
| 67 | DEF_TEST(ParsePath_invalid, r) { |
| 68 | SkPath path; |
| 69 | // This is an invalid SVG string, but the test verifies that we do not |
| 70 | // crash. |
| 71 | bool success = SkParsePath::FromSVGString("M 5", &path); |
| 72 | REPORTER_ASSERT(r, !success); |
| 73 | } |
caryclark | f1d4151 | 2016-02-09 10:30:22 -0800 | [diff] [blame] | 74 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 75 | #include "include/utils/SkRandom.h" |
| 76 | #include "tools/random_parse_path.h" |
caryclark | f1d4151 | 2016-02-09 10:30:22 -0800 | [diff] [blame] | 77 | |
| 78 | DEF_TEST(ParsePathRandom, r) { |
| 79 | SkRandom rand; |
| 80 | for (int index = 0; index < 1000; ++index) { |
| 81 | SkPath path, path2; |
| 82 | SkString spec; |
| 83 | uint32_t count = rand.nextRangeU(0, 10); |
| 84 | for (uint32_t i = 0; i < count; ++i) { |
| 85 | spec.append(MakeRandomParsePathPiece(&rand)); |
| 86 | } |
| 87 | bool success = SkParsePath::FromSVGString(spec.c_str(), &path); |
| 88 | REPORTER_ASSERT(r, success); |
| 89 | } |
| 90 | } |
fmalita | a17411f | 2016-09-14 10:14:08 -0700 | [diff] [blame] | 91 | |
| 92 | DEF_TEST(ParsePathOptionalCommand, r) { |
| 93 | struct { |
| 94 | const char* fStr; |
| 95 | int fVerbs; |
| 96 | int fPoints; |
| 97 | } gTests[] = { |
| 98 | { "", 0, 0 }, |
| 99 | |
| 100 | { "H100 200 ", 3, 3 }, |
| 101 | { "H-100-200", 3, 3 }, |
| 102 | { "H+100+200", 3, 3 }, |
| 103 | { "H.10.20" , 3, 3 }, |
| 104 | { "H-.10-.20", 3, 3 }, |
| 105 | { "H+.10+.20", 3, 3 }, |
| 106 | |
| 107 | { "L100 100 200 200" , 3, 3 }, |
| 108 | { "L-100-100-200-200", 3, 3 }, |
| 109 | { "L+100+100+200+200", 3, 3 }, |
| 110 | { "L.10.10.20.20" , 3, 3 }, |
| 111 | { "L-.10-.10-.20-.20", 3, 3 }, |
| 112 | { "L+.10+.10+.20+.20", 3, 3 }, |
| 113 | |
| 114 | { "C100 100 200 200 300 300 400 400 500 500 600 600" , 3, 7 }, |
| 115 | { "C100-100-200-200-300-300-400-400-500-500-600-600" , 3, 7 }, |
| 116 | { "C100+100+200+200+300+300+400+400+500+500+600+600" , 3, 7 }, |
| 117 | { "C.10.10.20.20.30.30.40.40.50.50.60.60" , 3, 7 }, |
| 118 | { "C-.10-.10-.20-.20-.30-.30-.40-.40-.50-.50-.60-.60", 3, 7 }, |
| 119 | { "C+.10+.10+.20+.20+.30+.30+.40+.40+.50+.50+.60+.60", 3, 7 }, |
| 120 | |
| 121 | { "c-1.49.71-2.12 2.5-1.4 4 .71 1.49 2.5 2.12 4 1.4z", 4, 7 }, |
| 122 | }; |
| 123 | |
| 124 | SkPath path; |
| 125 | for (size_t i = 0; i < SK_ARRAY_COUNT(gTests); ++i) { |
| 126 | REPORTER_ASSERT(r, SkParsePath::FromSVGString(gTests[i].fStr, &path)); |
| 127 | REPORTER_ASSERT(r, path.countVerbs() == gTests[i].fVerbs); |
| 128 | REPORTER_ASSERT(r, path.countPoints() == gTests[i].fPoints); |
| 129 | } |
| 130 | } |