blob: 228aee9031881bc2c33ee05d59972ec902518d0e [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"
12
13// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
14// fInitialDashLength < 0 (a signal the effect is nonsense). Here we test that it flattens.
15
16DEF_TEST(DashPathEffectTest_crbug_348821, r) {
17 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug.
18 const int count = 2;
19 SkScalar phase = SK_ScalarInfinity; // Used to force the bad fInitialDashLength = -1 path.
20 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, count, phase));
21
22 // NULL -> refuses to work with flattening framework.
23 REPORTER_ASSERT(r, dash->getFactory() != NULL);
24
25 SkWriteBuffer buffer;
26 buffer.writeFlattenable(dash);
27 REPORTER_ASSERT(r, buffer.bytesWritten() > 12); // We'd write 12 if broken, >=40 if not.
28}
robertphillips2b9ee632014-11-05 08:06:40 -080029
30// Test out the asPoint culling behavior.
31DEF_TEST(DashPathEffectTest_asPoints, r) {
32
33 const SkScalar intervals[] = { 1.0f, 1.0f };
34 const int count = 2;
35 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, count, 0.0f));
36
37 SkRect cull = SkRect::MakeWH(1.0f, 1.0f);
38
39 const struct {
40 SkPoint fPts[2];
41 bool fExpectedResult;
42 } testCases[] = {
43 { { { -5.0f, 0.5f }, { -4.0f, 0.5f } }, false }, // off to the left
44 { { { 4.0f, 0.5f }, { 5.0f, 0.5f } }, false }, // off to the right
45 { { { 0.5f, 4.0f }, { 0.5f, 5.0f } }, false }, // off the bottom
46 { { { 0.5f, -5.0f }, { 0.5f, -4.0f } }, false }, // off the top
47 { { { 0.5f, 0.2f }, { 0.5f, 0.8f } }, true }, // entirely inside vertical
48 { { { 0.2f, 0.5f }, { 0.8f, 0.5f } }, true }, // entirely inside horizontal
49 { { { 0.5f, -5.0f }, { 0.5f, 5.0f } }, true }, // straddles both sides vertically
50 { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both sides horizontally
51 { { { 0.5f, -5.0f }, { 0.5f, 0.5f } }, true }, // straddles top
52 { { { 0.5f, 5.0f }, { 0.5f, 0.5f } }, true }, // straddles bottom
53 { { { -5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles left
54 { { { 5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles right
55 { { { 0.5f, 0.5f }, { 0.5f, 0.5f } }, false }, // zero length
56 };
57
58 SkPaint paint;
59 paint.setStyle(SkPaint::kStroke_Style);
60 paint.setStrokeWidth(1.0f);
61 SkStrokeRec rec(paint);
62
63 static const int kNumMats = 3;
64 SkMatrix mats[kNumMats];
65 mats[0].reset();
66 mats[1].setRotate(90, 0.5f, 0.5f);
67 mats[2].setTranslate(10.0f, 10.0f);
68
69 for (int i = 0; i < kNumMats; ++i) {
70 for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) {
71 for (int k = 0; k < 2; ++k) { // exercise alternating endpoints
72 SkPathEffect::PointData results;
73 SkPath src;
74
75 src.moveTo(testCases[j].fPts[k]);
76 src.lineTo(testCases[j].fPts[(k+1)%2]);
77
78 bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull);
79 if (i < 2) {
80 REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult);
81 } else {
82 // On the third pass all the lines should be outside the translated cull rect
83 REPORTER_ASSERT(r, !actualResult);
84 }
85 }
86 }
87 }
88}