blob: 7b4c948ad266198b231fdf06efbd8581d8e91e28 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@google.combdee9fc2011-02-22 20:17:43 +00008#include "Test.h"
9#include "SkClipStack.h"
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +000010#include "SkPath.h"
11#include "SkRect.h"
12
13static void test_assign_and_comparison(skiatest::Reporter* reporter) {
14 SkClipStack s;
15
16 // Build up a clip stack with a path, an empty clip, and a rect.
17 s.save();
18 SkPath p;
19 p.moveTo(5, 6);
20 p.lineTo(7, 8);
21 p.lineTo(5, 9);
22 p.close();
23 s.clipDevPath(p);
24
25 s.save();
26 SkRect r = SkRect::MakeLTRB(1, 2, 3, 4);
27 s.clipDevRect(r);
28 r = SkRect::MakeLTRB(10, 11, 12, 13);
29 s.clipDevRect(r);
30
31 s.save();
32 r = SkRect::MakeLTRB(14, 15, 16, 17);
33 s.clipDevRect(r, SkRegion::kUnion_Op);
34
35 // Test that assignment works.
36 SkClipStack copy = s;
37 REPORTER_ASSERT(reporter, s == copy);
38
39 // Test that different save levels triggers not equal.
40 s.restore();
41 REPORTER_ASSERT(reporter, s != copy);
42
43 // Test that an equal, but not copied version is equal.
44 s.save();
45 r = SkRect::MakeLTRB(14, 15, 16, 17);
46 s.clipDevRect(r, SkRegion::kUnion_Op);
47 REPORTER_ASSERT(reporter, s == copy);
48
49 // Test that a different op on one level triggers not equal.
50 s.restore();
51 s.save();
52 r = SkRect::MakeLTRB(14, 15, 16, 17);
53 s.clipDevRect(r);
54 REPORTER_ASSERT(reporter, s != copy);
55
56 // Test that different state (clip type) triggers not equal.
57 s.restore();
58 s.save();
59 SkPath rp;
60 rp.addRect(r);
61 s.clipDevPath(rp, SkRegion::kUnion_Op);
62 REPORTER_ASSERT(reporter, s != copy);
63
64 // Test that different rects triggers not equal.
65 s.restore();
66 s.save();
67 r = SkRect::MakeLTRB(24, 25, 26, 27);
68 s.clipDevRect(r, SkRegion::kUnion_Op);
69 REPORTER_ASSERT(reporter, s != copy);
70
71 // Sanity check
72 s.restore();
73 copy.restore();
74 REPORTER_ASSERT(reporter, s == copy);
75 s.restore();
76 copy.restore();
77 REPORTER_ASSERT(reporter, s == copy);
78
79 // Test that different paths triggers not equal.
80 s.restore();
81 s.save();
82 p.addRect(r);
83 s.clipDevPath(p);
84 REPORTER_ASSERT(reporter, s != copy);
85}
reed@google.combdee9fc2011-02-22 20:17:43 +000086
87static void assert_count(skiatest::Reporter* reporter, const SkClipStack& stack,
88 int count) {
89 REPORTER_ASSERT(reporter, count == stack.getSaveCount());
90
91 SkClipStack::B2FIter iter(stack);
92 int counter = 0;
93 while (iter.next()) {
94 counter += 1;
95 }
96 REPORTER_ASSERT(reporter, count == counter);
97}
98
99static void TestClipStack(skiatest::Reporter* reporter) {
100 SkClipStack stack;
101
102 assert_count(reporter, stack, 0);
103
104 static const SkIRect gRects[] = {
105 { 0, 0, 100, 100 },
106 { 25, 25, 125, 125 },
107 { 0, 0, 1000, 1000 },
108 { 0, 0, 75, 75 }
109 };
110 for (size_t i = 0; i < SK_ARRAY_COUNT(gRects); i++) {
111 stack.clipDevRect(gRects[i]);
112 }
113
114 // all of the above rects should have been intersected, leaving only 1 rect
115 SkClipStack::B2FIter iter(stack);
116 const SkClipStack::B2FIter::Clip* clip = iter.next();
epoger@google.com2047f002011-05-17 17:36:59 +0000117 SkRect answer;
118 answer.iset(25, 25, 75, 75);
reed@google.combdee9fc2011-02-22 20:17:43 +0000119
120 REPORTER_ASSERT(reporter, clip);
121 REPORTER_ASSERT(reporter, clip->fRect);
122 REPORTER_ASSERT(reporter, !clip->fPath);
123 REPORTER_ASSERT(reporter, SkRegion::kIntersect_Op == clip->fOp);
124 REPORTER_ASSERT(reporter, *clip->fRect == answer);
125 // now check that we only had one in our iterator
126 REPORTER_ASSERT(reporter, !iter.next());
127
128 stack.reset();
129 assert_count(reporter, stack, 0);
vandebo@chromium.org1e1c36f2011-05-03 16:26:09 +0000130
131 test_assign_and_comparison(reporter);
reed@google.combdee9fc2011-02-22 20:17:43 +0000132}
133
134#include "TestClassDef.h"
135DEFINE_TESTCLASS("ClipStack", TestClipStackClass, TestClipStack)