OLD | NEW |
| 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 |
1 #include "Test.h" | 8 #include "Test.h" |
2 | 9 |
3 #include "SkDashPathEffect.h" | 10 #include "SkDashPathEffect.h" |
4 #include "SkWriteBuffer.h" | 11 #include "SkWriteBuffer.h" |
5 | 12 |
6 // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unfla
tten itself when | 13 // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unfla
tten itself when |
7 // fInitialDashLength < 0 (a signal the effect is nonsense). Here we test that
it flattens. | 14 // fInitialDashLength < 0 (a signal the effect is nonsense). Here we test that
it flattens. |
8 | 15 |
9 DEF_TEST(DashPathEffectTest_crbug_348821, r) { | 16 DEF_TEST(DashPathEffectTest_crbug_348821, r) { |
10 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from
bug. | 17 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from
bug. |
11 const int count = 2; | 18 const int count = 2; |
12 SkScalar phase = SK_ScalarInfinity; // Used to force the bad fInitialDashLe
ngth = -1 path. | 19 SkScalar phase = SK_ScalarInfinity; // Used to force the bad fInitialDashLe
ngth = -1 path. |
13 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, coun
t, phase)); | 20 SkAutoTUnref<SkDashPathEffect> dash(SkDashPathEffect::Create(intervals, coun
t, phase)); |
14 | 21 |
15 // NULL -> refuses to work with flattening framework. | 22 // NULL -> refuses to work with flattening framework. |
16 REPORTER_ASSERT(r, dash->getFactory() != NULL); | 23 REPORTER_ASSERT(r, dash->getFactory() != NULL); |
17 | 24 |
18 SkWriteBuffer buffer; | 25 SkWriteBuffer buffer; |
19 buffer.writeFlattenable(dash); | 26 buffer.writeFlattenable(dash); |
20 REPORTER_ASSERT(r, buffer.bytesWritten() > 12); // We'd write 12 if broken,
>=40 if not. | 27 REPORTER_ASSERT(r, buffer.bytesWritten() > 12); // We'd write 12 if broken,
>=40 if not. |
21 } | 28 } |
| 29 |
| 30 // Test out the asPoint culling behavior. |
| 31 DEF_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, coun
t, 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 s
ides vertically |
| 50 { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both s
ides 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].fExpectedRes
ult); |
| 81 } else { |
| 82 // On the third pass all the lines should be outside the tra
nslated cull rect |
| 83 REPORTER_ASSERT(r, !actualResult); |
| 84 } |
| 85 } |
| 86 } |
| 87 } |
| 88 } |
OLD | NEW |