blob: 7e832cdd6698ab5197f01270910733ad14e069a8 [file] [log] [blame]
robertphillips2b9ee632014-11-05 08:06:40 -08001/*
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.orgaf5346a2014-03-18 17:38:34 +00008#include "Test.h"
9
10#include "SkDashPathEffect.h"
11#include "SkWriteBuffer.h"
halcanary435657f2015-09-15 12:53:07 -070012#include "SkStrokeRec.h"
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000013
14// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
caryclarkeb75c7d2016-03-18 06:04:26 -070015// the effect is nonsense. Here we test that it fails when passed nonsense parameters.
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000016
17DEF_TEST(DashPathEffectTest_crbug_348821, r) {
18 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug.
19 const int count = 2;
caryclarkeb75c7d2016-03-18 06:04:26 -070020 SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect.
reeda4393342016-03-18 11:22:57 -070021 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000022
caryclarkeb75c7d2016-03-18 06:04:26 -070023 REPORTER_ASSERT(r, dash == nullptr);
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000024}
robertphillips2b9ee632014-11-05 08:06:40 -080025
26// Test out the asPoint culling behavior.
27DEF_TEST(DashPathEffectTest_asPoints, r) {
28
29 const SkScalar intervals[] = { 1.0f, 1.0f };
30 const int count = 2;
reeda4393342016-03-18 11:22:57 -070031 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
robertphillips2b9ee632014-11-05 08:06:40 -080032
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}
caryclark703348f2016-01-29 09:54:20 -080085
86DEF_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 };
reeda4393342016-03-18 11:22:57 -070093 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
caryclark703348f2016-01-29 09:54:20 -080094
95 SkPaint paint;
96 paint.setStyle(SkPaint::kStroke_Style);
97 paint.setPathEffect(dash);
98
99 SkPath fill;
100 paint.getFillPath(path, &fill);
101}