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 "PathOpsExtendedTest.h"
9#include "PathOpsTestCommon.h"
10#include "SkBitmap.h"
11#include "Test.h"
12
13DEF_TEST(PathOpsBuilder, reporter) {
14    SkOpBuilder builder;
15    SkPath result;
16    REPORTER_ASSERT(reporter, builder.resolve(&result));
17    REPORTER_ASSERT(reporter, result.isEmpty());
18
19    builder.add(result, kDifference_SkPathOp);
20    REPORTER_ASSERT(reporter, builder.resolve(&result));
21    REPORTER_ASSERT(reporter, result.isEmpty());
22
23    builder.add(result, kUnion_SkPathOp);
24    REPORTER_ASSERT(reporter, builder.resolve(&result));
25    REPORTER_ASSERT(reporter, result.isEmpty());
26
27    SkPath rectPath;
28    rectPath.setFillType(SkPath::kEvenOdd_FillType);
29    rectPath.addRect(0, 1, 2, 3, SkPath::kCW_Direction);
30    builder.add(rectPath, kUnion_SkPathOp);
31    REPORTER_ASSERT(reporter, builder.resolve(&result));
32    bool closed;
33    SkPath::Direction dir;
34    REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir));
35    REPORTER_ASSERT(reporter, closed);
36    REPORTER_ASSERT(reporter, dir == SkPath::kCW_Direction);
37    REPORTER_ASSERT(reporter, rectPath == result);
38
39    rectPath.reset();
40    rectPath.setFillType(SkPath::kEvenOdd_FillType);
41    rectPath.addRect(0, 1, 2, 3, SkPath::kCCW_Direction);
42    builder.add(rectPath, kUnion_SkPathOp);
43    REPORTER_ASSERT(reporter, builder.resolve(&result));
44    REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir));
45    REPORTER_ASSERT(reporter, closed);
46    REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction);
47     REPORTER_ASSERT(reporter, rectPath == result);
48
49    builder.add(rectPath, kDifference_SkPathOp);
50    REPORTER_ASSERT(reporter, builder.resolve(&result));
51    REPORTER_ASSERT(reporter, result.isEmpty());
52
53    SkPath rect2, rect3;
54    rect2.addRect(2, 1, 4, 3, SkPath::kCW_Direction);
55    rect3.addRect(4, 1, 5, 3, SkPath::kCCW_Direction);
56    builder.add(rectPath, kUnion_SkPathOp);
57    builder.add(rect2, kUnion_SkPathOp);
58    builder.add(rect3, kUnion_SkPathOp);
59    REPORTER_ASSERT(reporter, builder.resolve(&result));
60    REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir));
61    REPORTER_ASSERT(reporter, closed);
62    SkRect expected;
63    expected.set(0, 1, 5, 3);
64    REPORTER_ASSERT(reporter, result.getBounds() == expected);
65
66    SkPath circle1, circle2, circle3;
67    circle1.addCircle(5, 6, 4, SkPath::kCW_Direction);
68    circle2.addCircle(7, 4, 8, SkPath::kCCW_Direction);
69    circle3.addCircle(6, 5, 6, SkPath::kCW_Direction);
70    SkPath opCompare;
71    Op(circle1, circle2, kUnion_SkPathOp, &opCompare);
72    Op(opCompare, circle3, kDifference_SkPathOp, &opCompare);
73    builder.add(circle1, kUnion_SkPathOp);
74    builder.add(circle2, kUnion_SkPathOp);
75    builder.add(circle3, kDifference_SkPathOp);
76    REPORTER_ASSERT(reporter, builder.resolve(&result));
77    SkBitmap bitmap;
78    int pixelDiff = comparePaths(reporter, __FUNCTION__, opCompare, result, bitmap);
79    REPORTER_ASSERT(reporter, pixelDiff == 0);
80}
81
82DEF_TEST(Issue3838, reporter) {
83    SkPath path;
84    path.moveTo(200, 170);
85    path.lineTo(220, 170);
86    path.lineTo(220, 230);
87    path.lineTo(240, 230);
88    path.lineTo(240, 210);
89    path.lineTo(180, 210);
90    path.lineTo(180, 190);
91    path.lineTo(260, 190);
92    path.lineTo(260, 250);
93    path.lineTo(200, 250);
94    path.lineTo(200, 170);
95    path.close();
96    testSimplify(reporter, path, __FUNCTION__);
97    SkPath path3;
98    Simplify(path, &path3);
99    SkPath path2;
100    SkOpBuilder builder;
101    builder.add(path, kUnion_SkPathOp);
102    builder.resolve(&path2);
103    SkBitmap bitmap;
104    int pixelDiff = comparePaths(reporter, __FUNCTION__, path, path2, bitmap);
105    REPORTER_ASSERT(reporter, pixelDiff == 0);
106}
107