robertphillips | 2b9ee63 | 2014-11-05 08:06:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
| 10 | #include "SkDashPathEffect.h" |
| 11 | #include "SkWriteBuffer.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 12 | #include "SkStrokeRec.h" |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 13 | |
| 14 | // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 15 | // the effect is nonsense. Here we test that it fails when passed nonsense parameters. |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 16 | |
| 17 | DEF_TEST(DashPathEffectTest_crbug_348821, r) { |
| 18 | SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug. |
| 19 | const int count = 2; |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 20 | SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect. |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 21 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase)); |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 22 | |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 23 | REPORTER_ASSERT(r, dash == nullptr); |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 24 | } |
robertphillips | 2b9ee63 | 2014-11-05 08:06:40 -0800 | [diff] [blame] | 25 | |
| 26 | // Test out the asPoint culling behavior. |
| 27 | DEF_TEST(DashPathEffectTest_asPoints, r) { |
| 28 | |
| 29 | const SkScalar intervals[] = { 1.0f, 1.0f }; |
| 30 | const int count = 2; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 31 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f)); |
robertphillips | 2b9ee63 | 2014-11-05 08:06:40 -0800 | [diff] [blame] | 32 | |
| 33 | SkRect cull = SkRect::MakeWH(1.0f, 1.0f); |
| 34 | |
| 35 | const struct { |
| 36 | SkPoint fPts[2]; |
| 37 | bool fExpectedResult; |
| 38 | } testCases[] = { |
| 39 | { { { -5.0f, 0.5f }, { -4.0f, 0.5f } }, false }, // off to the left |
| 40 | { { { 4.0f, 0.5f }, { 5.0f, 0.5f } }, false }, // off to the right |
| 41 | { { { 0.5f, 4.0f }, { 0.5f, 5.0f } }, false }, // off the bottom |
| 42 | { { { 0.5f, -5.0f }, { 0.5f, -4.0f } }, false }, // off the top |
| 43 | { { { 0.5f, 0.2f }, { 0.5f, 0.8f } }, true }, // entirely inside vertical |
| 44 | { { { 0.2f, 0.5f }, { 0.8f, 0.5f } }, true }, // entirely inside horizontal |
| 45 | { { { 0.5f, -5.0f }, { 0.5f, 5.0f } }, true }, // straddles both sides vertically |
| 46 | { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both sides horizontally |
| 47 | { { { 0.5f, -5.0f }, { 0.5f, 0.5f } }, true }, // straddles top |
| 48 | { { { 0.5f, 5.0f }, { 0.5f, 0.5f } }, true }, // straddles bottom |
| 49 | { { { -5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles left |
| 50 | { { { 5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles right |
| 51 | { { { 0.5f, 0.5f }, { 0.5f, 0.5f } }, false }, // zero length |
| 52 | }; |
| 53 | |
| 54 | SkPaint paint; |
| 55 | paint.setStyle(SkPaint::kStroke_Style); |
| 56 | paint.setStrokeWidth(1.0f); |
| 57 | SkStrokeRec rec(paint); |
| 58 | |
| 59 | static const int kNumMats = 3; |
| 60 | SkMatrix mats[kNumMats]; |
| 61 | mats[0].reset(); |
| 62 | mats[1].setRotate(90, 0.5f, 0.5f); |
| 63 | mats[2].setTranslate(10.0f, 10.0f); |
| 64 | |
| 65 | for (int i = 0; i < kNumMats; ++i) { |
| 66 | for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) { |
| 67 | for (int k = 0; k < 2; ++k) { // exercise alternating endpoints |
| 68 | SkPathEffect::PointData results; |
| 69 | SkPath src; |
| 70 | |
| 71 | src.moveTo(testCases[j].fPts[k]); |
| 72 | src.lineTo(testCases[j].fPts[(k+1)%2]); |
| 73 | |
| 74 | bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull); |
| 75 | if (i < 2) { |
| 76 | REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult); |
| 77 | } else { |
| 78 | // On the third pass all the lines should be outside the translated cull rect |
| 79 | REPORTER_ASSERT(r, !actualResult); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
caryclark | 703348f | 2016-01-29 09:54:20 -0800 | [diff] [blame] | 85 | |
| 86 | DEF_TEST(DashPath_bug4871, r) { |
| 87 | SkPath path; |
| 88 | path.moveTo(30, 24); |
| 89 | path.cubicTo(30.002f, 24, 30, 24, 30, 24); |
| 90 | path.close(); |
| 91 | |
| 92 | SkScalar intervals[2] = { 1, 1 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 93 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0)); |
caryclark | 703348f | 2016-01-29 09:54:20 -0800 | [diff] [blame] | 94 | |
| 95 | SkPaint paint; |
| 96 | paint.setStyle(SkPaint::kStroke_Style); |
| 97 | paint.setPathEffect(dash); |
| 98 | |
| 99 | SkPath fill; |
| 100 | paint.getFillPath(path, &fill); |
| 101 | } |