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  */
7 
8 #include "SkParsePath.h"
9 #include "Test.h"
10 
test_to_from(skiatest::Reporter * reporter,const SkPath & path)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 
30 static struct {
31     const char* fStr;
32     const SkRect fBounds;
33 } gRec[] = {
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 } },
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",
40         { -5.5f, -0.5f,
41           6, 6.5f } }
42 };
43 
DEF_TEST(ParsePath,reporter)44 DEF_TEST(ParsePath, reporter) {
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     }
55 
56     SkRect r;
57     r.set(0, 0, 10, 10.5f);
58     SkPath p;
59     p.addRect(r);
60     test_to_from(reporter, p);
61     p.addOval(r);
62     test_to_from(reporter, p);
63     p.addRoundRect(r, 4, 4.5f);
64     test_to_from(reporter, p);
65 }
66 
DEF_TEST(ParsePath_invalid,r)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 }
74 
75 #include "random_parse_path.h"
76 #include "SkRandom.h"
77 
DEF_TEST(ParsePathRandom,r)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 }
91