blob: 4d00f70db21ab75892fcdab3fef39fd5022c753e [file] [log] [blame]
reed@android.com3abec1d2009-03-02 05:36:20 +00001#include "Test.h"
2#include "SkPath.h"
reed@google.com04863fa2011-05-15 04:08:24 +00003#include "SkParse.h"
reed@android.com60bc6d52010-02-11 11:09:39 +00004#include "SkSize.h"
reed@android.com3abec1d2009-03-02 05:36:20 +00005
reed@android.com6b82d1a2009-06-03 02:35:01 +00006static void check_convex_bounds(skiatest::Reporter* reporter, const SkPath& p,
7 const SkRect& bounds) {
8 REPORTER_ASSERT(reporter, p.isConvex());
9 REPORTER_ASSERT(reporter, p.getBounds() == bounds);
reed@google.com62047cf2011-02-07 19:39:09 +000010
reed@android.com6b82d1a2009-06-03 02:35:01 +000011 SkPath p2(p);
12 REPORTER_ASSERT(reporter, p2.isConvex());
13 REPORTER_ASSERT(reporter, p2.getBounds() == bounds);
14
15 SkPath other;
16 other.swap(p2);
17 REPORTER_ASSERT(reporter, other.isConvex());
18 REPORTER_ASSERT(reporter, other.getBounds() == bounds);
19}
20
reed@google.com04863fa2011-05-15 04:08:24 +000021static void setFromString(SkPath* path, const char str[]) {
22 bool first = true;
23 while (str) {
24 SkScalar x, y;
25 str = SkParse::FindScalar(str, &x);
26 if (NULL == str) {
27 break;
28 }
29 str = SkParse::FindScalar(str, &y);
30 SkASSERT(str);
31 if (first) {
32 path->moveTo(x, y);
33 first = false;
34 } else {
35 path->lineTo(x, y);
36 }
37 }
38}
39
40static void test_convexity(skiatest::Reporter* reporter) {
41 static const SkPath::Convexity U = SkPath::kUnknown_Convexity;
42 static const SkPath::Convexity C = SkPath::kConcave_Convexity;
43 static const SkPath::Convexity V = SkPath::kConvex_Convexity;
44
45 SkPath path;
46
47 REPORTER_ASSERT(reporter, U == SkPath::ComputeConvexity(path));
48 path.addCircle(0, 0, 10);
49 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
50 path.addCircle(0, 0, 10); // 2nd circle
51 REPORTER_ASSERT(reporter, C == SkPath::ComputeConvexity(path));
52 path.reset();
53 path.addRect(0, 0, 10, 10, SkPath::kCCW_Direction);
54 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
55 path.reset();
56 path.addRect(0, 0, 10, 10, SkPath::kCW_Direction);
57 REPORTER_ASSERT(reporter, V == SkPath::ComputeConvexity(path));
58
59 static const struct {
60 const char* fPathStr;
61 SkPath::Convexity fExpectedConvexity;
62 } gRec[] = {
63 { "0 0", SkPath::kUnknown_Convexity },
64 { "0 0 10 10", SkPath::kUnknown_Convexity },
65 { "0 0 10 10 20 20 0 0 10 10", SkPath::kUnknown_Convexity },
66 { "0 0 10 10 10 20", SkPath::kConvex_Convexity },
67 { "0 0 10 10 10 0", SkPath::kConvex_Convexity },
68 { "0 0 10 10 10 0 0 10", SkPath::kConcave_Convexity },
69 { "0 0 10 0 0 10 -10 -10", SkPath::kConcave_Convexity },
70 };
71
72 for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) {
73 SkPath path;
74 setFromString(&path, gRec[i].fPathStr);
75 SkPath::Convexity c = SkPath::ComputeConvexity(path);
76 REPORTER_ASSERT(reporter, c == gRec[i].fExpectedConvexity);
77 }
78}
79
80void TestPath(skiatest::Reporter* reporter);
81void TestPath(skiatest::Reporter* reporter) {
reed@android.com60bc6d52010-02-11 11:09:39 +000082 {
83 SkSize size;
84 size.fWidth = 3.4f;
85 size.width();
86 size = SkSize::Make(3,4);
87 SkISize isize = SkISize::Make(3,4);
88 }
89
90 SkTSize<SkScalar>::Make(3,4);
91
reed@android.com3abec1d2009-03-02 05:36:20 +000092 SkPath p, p2;
93 SkRect bounds, bounds2;
reed@android.com80e39a72009-04-02 16:59:40 +000094
reed@android.com3abec1d2009-03-02 05:36:20 +000095 REPORTER_ASSERT(reporter, p.isEmpty());
reed@android.com6b82d1a2009-06-03 02:35:01 +000096 REPORTER_ASSERT(reporter, !p.isConvex());
reed@android.com3abec1d2009-03-02 05:36:20 +000097 REPORTER_ASSERT(reporter, p.getFillType() == SkPath::kWinding_FillType);
98 REPORTER_ASSERT(reporter, !p.isInverseFillType());
99 REPORTER_ASSERT(reporter, p == p2);
100 REPORTER_ASSERT(reporter, !(p != p2));
101
reed@android.comd252db02009-04-01 18:31:44 +0000102 REPORTER_ASSERT(reporter, p.getBounds().isEmpty());
reed@android.com80e39a72009-04-02 16:59:40 +0000103
reed@android.com3abec1d2009-03-02 05:36:20 +0000104 bounds.set(0, 0, SK_Scalar1, SK_Scalar1);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000105
106 p.setIsConvex(false);
107 p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1);
108 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000109
reed@android.com6b82d1a2009-06-03 02:35:01 +0000110 p.reset();
111 p.setIsConvex(false);
112 p.addOval(bounds);
113 check_convex_bounds(reporter, p, bounds);
reed@google.com62047cf2011-02-07 19:39:09 +0000114
reed@android.com6b82d1a2009-06-03 02:35:01 +0000115 p.reset();
116 p.setIsConvex(false);
reed@android.com3abec1d2009-03-02 05:36:20 +0000117 p.addRect(bounds);
reed@android.com6b82d1a2009-06-03 02:35:01 +0000118 check_convex_bounds(reporter, p, bounds);
reed@android.com3abec1d2009-03-02 05:36:20 +0000119
120 REPORTER_ASSERT(reporter, p != p2);
121 REPORTER_ASSERT(reporter, !(p == p2));
122
123 // does getPoints return the right result
124 REPORTER_ASSERT(reporter, p.getPoints(NULL, 5) == 4);
125 SkPoint pts[4];
126 int count = p.getPoints(pts, 4);
127 REPORTER_ASSERT(reporter, count == 4);
128 bounds2.set(pts, 4);
129 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000130
reed@android.com3abec1d2009-03-02 05:36:20 +0000131 bounds.offset(SK_Scalar1*3, SK_Scalar1*4);
132 p.offset(SK_Scalar1*3, SK_Scalar1*4);
reed@android.comd252db02009-04-01 18:31:44 +0000133 REPORTER_ASSERT(reporter, bounds == p.getBounds());
reed@android.com3abec1d2009-03-02 05:36:20 +0000134
135#if 0 // isRect needs to be implemented
136 REPORTER_ASSERT(reporter, p.isRect(NULL));
137 bounds.setEmpty();
138 REPORTER_ASSERT(reporter, p.isRect(&bounds2));
139 REPORTER_ASSERT(reporter, bounds == bounds2);
reed@android.com80e39a72009-04-02 16:59:40 +0000140
reed@android.com3abec1d2009-03-02 05:36:20 +0000141 // now force p to not be a rect
142 bounds.set(0, 0, SK_Scalar1/2, SK_Scalar1/2);
143 p.addRect(bounds);
144 REPORTER_ASSERT(reporter, !p.isRect(NULL));
145#endif
146
147 SkPoint pt;
148
149 p.moveTo(SK_Scalar1, 0);
150 p.getLastPt(&pt);
151 REPORTER_ASSERT(reporter, pt.fX == SK_Scalar1);
reed@google.com62047cf2011-02-07 19:39:09 +0000152
153 // check that reset and rewind clear the convex hint back to false
154 p.setIsConvex(false);
155 REPORTER_ASSERT(reporter, !p.isConvex());
156 p.setIsConvex(true);
157 REPORTER_ASSERT(reporter, p.isConvex());
158 p.reset();
159 REPORTER_ASSERT(reporter, !p.isConvex());
160 p.setIsConvex(true);
161 REPORTER_ASSERT(reporter, p.isConvex());
162 p.rewind();
163 REPORTER_ASSERT(reporter, !p.isConvex());
reed@google.com04863fa2011-05-15 04:08:24 +0000164
165 test_convexity(reporter);
reed@android.com3abec1d2009-03-02 05:36:20 +0000166}
167
168#include "TestClassDef.h"
169DEFINE_TESTCLASS("Path", PathTestClass, TestPath)