reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkPath.h" |
reed@google.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame^] | 3 | #include "SkParse.h" |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 4 | #include "SkSize.h" |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 5 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 6 | static 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.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 10 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 11 | 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.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame^] | 21 | static 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 | |
| 40 | static 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 | |
| 80 | void TestPath(skiatest::Reporter* reporter); |
| 81 | void TestPath(skiatest::Reporter* reporter) { |
reed@android.com | 60bc6d5 | 2010-02-11 11:09:39 +0000 | [diff] [blame] | 82 | { |
| 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.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 92 | SkPath p, p2; |
| 93 | SkRect bounds, bounds2; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 94 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, p.isEmpty()); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 96 | REPORTER_ASSERT(reporter, !p.isConvex()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 97 | 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.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 102 | REPORTER_ASSERT(reporter, p.getBounds().isEmpty()); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 103 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 104 | bounds.set(0, 0, SK_Scalar1, SK_Scalar1); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 105 | |
| 106 | p.setIsConvex(false); |
| 107 | p.addRoundRect(bounds, SK_Scalar1, SK_Scalar1); |
| 108 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 109 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 110 | p.reset(); |
| 111 | p.setIsConvex(false); |
| 112 | p.addOval(bounds); |
| 113 | check_convex_bounds(reporter, p, bounds); |
reed@google.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 114 | |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 115 | p.reset(); |
| 116 | p.setIsConvex(false); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 117 | p.addRect(bounds); |
reed@android.com | 6b82d1a | 2009-06-03 02:35:01 +0000 | [diff] [blame] | 118 | check_convex_bounds(reporter, p, bounds); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 119 | |
| 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.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 130 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 131 | bounds.offset(SK_Scalar1*3, SK_Scalar1*4); |
| 132 | p.offset(SK_Scalar1*3, SK_Scalar1*4); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, bounds == p.getBounds()); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 134 | |
| 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.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 140 | |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 141 | // 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.com | 62047cf | 2011-02-07 19:39:09 +0000 | [diff] [blame] | 152 | |
| 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.com | 04863fa | 2011-05-15 04:08:24 +0000 | [diff] [blame^] | 164 | |
| 165 | test_convexity(reporter); |
reed@android.com | 3abec1d | 2009-03-02 05:36:20 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | #include "TestClassDef.h" |
| 169 | DEFINE_TESTCLASS("Path", PathTestClass, TestPath) |