Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 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 #include "TestClassDef.h"
     10 #include "SkPaint.h"
     11 #include "SkPath.h"
     12 #include "SkRect.h"
     13 #include "SkStroke.h"
     14 
     15 static bool equal(const SkRect& a, const SkRect& b) {
     16     return  SkScalarNearlyEqual(a.left(), b.left()) &&
     17             SkScalarNearlyEqual(a.top(), b.top()) &&
     18             SkScalarNearlyEqual(a.right(), b.right()) &&
     19             SkScalarNearlyEqual(a.bottom(), b.bottom());
     20 }
     21 
     22 static void test_strokerect(skiatest::Reporter* reporter) {
     23     const SkScalar width = SkIntToScalar(10);
     24     SkPaint paint;
     25 
     26     paint.setStyle(SkPaint::kStroke_Style);
     27     paint.setStrokeWidth(width);
     28 
     29     SkRect r = { 0, 0, SkIntToScalar(200), SkIntToScalar(100) };
     30 
     31     SkRect outer(r);
     32     outer.outset(width/2, width/2);
     33 
     34     static const SkPaint::Join joins[] = {
     35         SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
     36     };
     37 
     38     for (size_t i = 0; i < SK_ARRAY_COUNT(joins); ++i) {
     39         paint.setStrokeJoin(joins[i]);
     40 
     41         SkPath path, fillPath;
     42         path.addRect(r);
     43         paint.getFillPath(path, &fillPath);
     44 
     45         REPORTER_ASSERT(reporter, equal(outer, fillPath.getBounds()));
     46 
     47         bool isMiter = SkPaint::kMiter_Join == joins[i];
     48         SkRect nested[2];
     49         REPORTER_ASSERT(reporter, fillPath.isNestedRects(nested) == isMiter);
     50         if (isMiter) {
     51             SkRect inner(r);
     52             inner.inset(width/2, width/2);
     53             REPORTER_ASSERT(reporter, equal(nested[0], outer));
     54             REPORTER_ASSERT(reporter, equal(nested[1], inner));
     55         }
     56     }
     57 }
     58 
     59 DEF_TEST(Stroke, reporter) {
     60     test_strokerect(reporter);
     61 }
     62