Home | History | Annotate | Download | only in tests
      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 
      8 #include "Test.h"
      9 
     10 #include "SkDashPathEffect.h"
     11 #include "SkWriteBuffer.h"
     12 #include "SkStrokeRec.h"
     13 #include "SkCanvas.h"
     14 #include "SkSurface.h"
     15 
     16 // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
     17 // the effect is nonsense.  Here we test that it fails when passed nonsense parameters.
     18 
     19 DEF_TEST(DashPathEffectTest_crbug_348821, r) {
     20     SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f };  // Values from bug.
     21     const int count = 2;
     22     SkScalar phase = SK_ScalarInfinity;  // Used to force a nonsense effect.
     23     sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
     24 
     25     REPORTER_ASSERT(r, dash == nullptr);
     26 }
     27 
     28 // Test out the asPoint culling behavior.
     29 DEF_TEST(DashPathEffectTest_asPoints, r) {
     30 
     31     const SkScalar intervals[] = { 1.0f, 1.0f };
     32     const int count = 2;
     33     sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
     34 
     35     SkRect cull = SkRect::MakeWH(1.0f, 1.0f);
     36 
     37     const struct {
     38         SkPoint fPts[2];
     39         bool    fExpectedResult;
     40     } testCases[] = {
     41         { { { -5.0f,  0.5f }, { -4.0f,  0.5f } }, false },   // off to the left
     42         { { {  4.0f,  0.5f }, {  5.0f,  0.5f } }, false },   // off to the right
     43         { { {  0.5f,  4.0f }, {  0.5f,  5.0f } }, false },   // off the bottom
     44         { { {  0.5f, -5.0f }, {  0.5f, -4.0f } }, false },   // off the top
     45         { { {  0.5f,  0.2f }, {  0.5f,  0.8f } }, true  },   // entirely inside vertical
     46         { { {  0.2f,  0.5f }, {  0.8f,  0.5f } }, true  },   // entirely inside horizontal
     47         { { {  0.5f, -5.0f }, {  0.5f,  5.0f } }, true  },   // straddles both sides vertically
     48         { { { -5.0f,  0.5f }, {  5.0f,  0.5f } }, true  },   // straddles both sides horizontally
     49         { { {  0.5f, -5.0f }, {  0.5f,  0.5f } }, true  },   // straddles top
     50         { { {  0.5f,  5.0f }, {  0.5f,  0.5f } }, true  },   // straddles bottom
     51         { { { -5.0f,  0.5f }, {  0.5f,  0.5f } }, true  },   // straddles left
     52         { { {  5.0f,  0.5f }, {  0.5f,  0.5f } }, true  },   // straddles right
     53         { { {  0.5f,  0.5f }, {  0.5f,  0.5f } }, false },   // zero length
     54     };
     55 
     56     SkPaint paint;
     57     paint.setStyle(SkPaint::kStroke_Style);
     58     paint.setStrokeWidth(1.0f);
     59     SkStrokeRec rec(paint);
     60 
     61     static const int kNumMats = 3;
     62     SkMatrix mats[kNumMats];
     63     mats[0].reset();
     64     mats[1].setRotate(90, 0.5f, 0.5f);
     65     mats[2].setTranslate(10.0f, 10.0f);
     66 
     67     for (int i = 0; i < kNumMats; ++i) {
     68         for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) {
     69             for (int k = 0; k < 2; ++k) {  // exercise alternating endpoints
     70                 SkPathEffect::PointData results;
     71                 SkPath src;
     72 
     73                 src.moveTo(testCases[j].fPts[k]);
     74                 src.lineTo(testCases[j].fPts[(k+1)%2]);
     75 
     76                 bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull);
     77                 if (i < 2) {
     78                     REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult);
     79                 } else {
     80                     // On the third pass all the lines should be outside the translated cull rect
     81                     REPORTER_ASSERT(r, !actualResult);
     82                 }
     83             }
     84         }
     85     }
     86 }
     87 
     88 DEF_TEST(DashPath_bug4871, r) {
     89     SkPath path;
     90     path.moveTo(30, 24);
     91     path.cubicTo(30.002f, 24, 30, 24, 30, 24);
     92     path.close();
     93 
     94     SkScalar intervals[2] = { 1, 1 };
     95     sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
     96 
     97     SkPaint paint;
     98     paint.setStyle(SkPaint::kStroke_Style);
     99     paint.setPathEffect(dash);
    100 
    101     SkPath fill;
    102     paint.getFillPath(path, &fill);
    103 }
    104 
    105 // Verify that long lines with many dashes don't cause overflows/OOMs.
    106 DEF_TEST(DashPathEffectTest_asPoints_limit, r) {
    107     sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(256, 256)));
    108     SkCanvas* canvas = surface->getCanvas();
    109 
    110     SkPaint p;
    111     p.setStyle(SkPaint::kStroke_Style);
    112     // force the bounds to outset by a large amount
    113     p.setStrokeWidth(5.0e10f);
    114     const SkScalar intervals[] = { 1, 1 };
    115     p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
    116     canvas->drawLine(1, 1, 1, 5.0e10f, p);
    117 }
    118