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