Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(975)

Side by Side Diff: tests/RecordPatternTest.cpp

Issue 263063002: Add pattern matchers for SkRecord (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: yet more notes Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/utils/SkTLogic.h ('K') | « src/utils/SkTLogic.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "Test.h"
2
3 #include "SkRecord.h"
4 #include "SkRecordPattern.h"
5 #include "SkRecorder.h"
6 #include "SkRecords.h"
7
8 using namespace SkRecords;
9 typedef Pattern3<Is<Save>,
10 Is<ClipRect>,
11 Is<Restore> >
12 SaveClipRectRestore;
13
14 DEF_TEST(RecordPattern_Simple, r) {
15 SaveClipRectRestore pattern;
16
17 SkRecord record;
18 REPORTER_ASSERT(r, !pattern.match(&record, 0));
19
20 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1200);
21
22 // Build up a save-clip-restore block. The pattern will match only it's com plete.
23 recorder.save();
24 REPORTER_ASSERT(r, !pattern.match(&record, 0));
25
26 recorder.clipRect(SkRect::MakeWH(300, 200));
27 REPORTER_ASSERT(r, !pattern.match(&record, 0));
28
29 recorder.restore();
30 REPORTER_ASSERT(r, pattern.match(&record, 0));
31 REPORTER_ASSERT(r, pattern.first<Save>() != NULL);
32 REPORTER_ASSERT(r, pattern.second<ClipRect>() != NULL);
33 REPORTER_ASSERT(r, pattern.third<Restore>() != NULL);
34 }
35
36 DEF_TEST(RecordPattern_StartingIndex, r) {
37 SaveClipRectRestore pattern;
38
39 SkRecord record;
40 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1200);
41
42 // There will be two save-clipRect-restore blocks [0,3) and [3,6).
43 for (int i = 0; i < 2; i++) {
44 recorder.save();
45 recorder.clipRect(SkRect::MakeWH(300, 200));
46 recorder.restore();
47 }
48
49 // We should match only at 0 and 3. Going over the length should fail grace fully.
50 for (unsigned i = 0; i < 8; i++) {
51 if (i == 0 || i == 3) {
52 REPORTER_ASSERT(r, pattern.match(&record, i) == i + 3);
53 } else {
54 REPORTER_ASSERT(r, !pattern.match(&record, i));
55 }
56 }
57 }
58
59 DEF_TEST(RecordPattern_DontMatchSubsequences, r) {
60 SaveClipRectRestore pattern;
61
62 SkRecord record;
63 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1200);
64
65 recorder.save();
66 recorder.clipRect(SkRect::MakeWH(300, 200));
67 recorder.drawRect(SkRect::MakeWH(600, 300), SkPaint());
68 recorder.restore();
69
70 REPORTER_ASSERT(r, !pattern.match(&record, 0));
71 }
72
73 DEF_TEST(RecordPattern_Star, r) {
74 Pattern3<Is<Save>, Star<Is<ClipRect> >, Is<Restore> > pattern;
75
76 SkRecord record;
77 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1200);
78
79 recorder.save();
80 recorder.restore();
81 REPORTER_ASSERT(r, pattern.match(&record, 0));
82 REPORTER_ASSERT(r, pattern.second<SkTDArray<ClipRect*> >()->count() == 0);
83
84 recorder.save();
85 recorder.clipRect(SkRect::MakeWH(300, 200));
86 recorder.restore();
87 REPORTER_ASSERT(r, pattern.match(&record, 2));
88 REPORTER_ASSERT(r, pattern.second<SkTDArray<ClipRect*> >()->count() == 1);
89
90 recorder.save();
91 recorder.clipRect(SkRect::MakeWH(300, 200));
92 recorder.clipRect(SkRect::MakeWH(100, 100));
93 recorder.restore();
94 REPORTER_ASSERT(r, pattern.match(&record, 5));
95 REPORTER_ASSERT(r, pattern.second<SkTDArray<ClipRect*> >()->count() == 2);
96 }
97
98 DEF_TEST(RecordPattern_IsDraw, r) {
99 Pattern3<Is<Save>, IsDraw, Is<Restore> > pattern;
100
101 SkRecord record;
102 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1200);
103
104 recorder.save();
105 recorder.clipRect(SkRect::MakeWH(300, 200));
106 recorder.restore();
107
108 REPORTER_ASSERT(r, !pattern.match(&record, 0));
109
110 SkPaint paint;
111
112 recorder.save();
113 paint.setColor(0xEEAA8822);
114 recorder.drawRect(SkRect::MakeWH(300, 200), paint);
115 recorder.restore();
116
117 recorder.save();
118 paint.setColor(0xFACEFACE);
119 recorder.drawPaint(paint);
120 recorder.restore();
121
122 REPORTER_ASSERT(r, pattern.match(&record, 3));
123 REPORTER_ASSERT(r, pattern.first<Save>() != NULL);
124 REPORTER_ASSERT(r, pattern.second<SkPaint>()->getColor() == 0xEEAA8822);
125 REPORTER_ASSERT(r, pattern.third<Restore>() != NULL);
126
127 REPORTER_ASSERT(r, pattern.match(&record, 6));
128 REPORTER_ASSERT(r, pattern.first<Save>() != NULL);
129 REPORTER_ASSERT(r, pattern.second<SkPaint>()->getColor() == 0xFACEFACE);
130 REPORTER_ASSERT(r, pattern.third<Restore>() != NULL);
131 }
132
133 DEF_TEST(RecordPattern_Complex, r) {
134 Pattern3<Is<Save>,
135 Star<Not<Or3<Is<Save>,
136 Is<Restore>,
137 IsDraw> > >,
138 Is<Restore> > pattern;
139
140 SkRecord record;
141 SkRecorder recorder(SkRecorder::kWriteOnly_Mode, &record, 1920, 1200);
142
143 recorder.save();
144 recorder.restore();
145 REPORTER_ASSERT(r, pattern.match(&record, 0) == 2);
146
147 recorder.save();
148 recorder.save();
149 recorder.restore();
150 recorder.restore();
151 REPORTER_ASSERT(r, !pattern.match(&record, 2));
152 REPORTER_ASSERT(r, pattern.match(&record, 3) == 5);
153
154 recorder.save();
155 recorder.clipRect(SkRect::MakeWH(300, 200));
156 recorder.restore();
157 REPORTER_ASSERT(r, pattern.match(&record, 6) == 9);
158
159 recorder.save();
160 recorder.clipRect(SkRect::MakeWH(300, 200));
161 recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
162 recorder.restore();
163 REPORTER_ASSERT(r, !pattern.match(&record, 9));
164
165 recorder.save();
166 recorder.pushCull(SkRect::MakeWH(300, 200));
167 recorder.clipRect(SkRect::MakeWH(300, 200));
168 recorder.clipRect(SkRect::MakeWH(100, 400));
169 recorder.popCull();
170 recorder.restore();
171 REPORTER_ASSERT(r, pattern.match(&record, 13) == 19);
172
173 // Same as above, but using pattern.search to step through matches.
174 unsigned begin, end = 0;
175 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
176 REPORTER_ASSERT(r, begin == 0);
177 REPORTER_ASSERT(r, end == 2);
178
179 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
180 REPORTER_ASSERT(r, begin == 3);
181 REPORTER_ASSERT(r, end == 5);
182
183 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
184 REPORTER_ASSERT(r, begin == 6);
185 REPORTER_ASSERT(r, end == 9);
186
187 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
188 REPORTER_ASSERT(r, begin == 13);
189 REPORTER_ASSERT(r, end == 19);
190
191 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
192 }
OLDNEW
« src/utils/SkTLogic.h ('K') | « src/utils/SkTLogic.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698