blob: ae13b28fc4165a81ddb64b6d425fae05da6c5afc [file] [log] [blame]
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +00001#include "Test.h"
2
3#include "SkRecord.h"
4#include "SkRecordPattern.h"
5#include "SkRecorder.h"
6#include "SkRecords.h"
7
8using namespace SkRecords;
9typedef Pattern3<Is<Save>,
10 Is<ClipRect>,
11 Is<Restore> >
12 SaveClipRectRestore;
13
14DEF_TEST(RecordPattern_Simple, r) {
15 SaveClipRectRestore pattern;
16
17 SkRecord record;
18 REPORTER_ASSERT(r, !pattern.match(&record, 0));
19
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000020 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000021
22 // Build up a save-clip-restore block. The pattern will match only it's complete.
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
36DEF_TEST(RecordPattern_StartingIndex, r) {
37 SaveClipRectRestore pattern;
38
39 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000040 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000041
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 gracefully.
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
59DEF_TEST(RecordPattern_DontMatchSubsequences, r) {
60 SaveClipRectRestore pattern;
61
62 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000063 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000064
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
73DEF_TEST(RecordPattern_Star, r) {
74 Pattern3<Is<Save>, Star<Is<ClipRect> >, Is<Restore> > pattern;
75
76 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000077 SkRecorder recorder(&record, 1920, 1200);
reed2ff1fce2014-12-11 07:07:37 -080078 int index = 0;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000079
80 recorder.save();
81 recorder.clipRect(SkRect::MakeWH(300, 200));
82 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -080083 REPORTER_ASSERT(r, pattern.match(&record, index));
84 index += 3;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000085
86 recorder.save();
87 recorder.clipRect(SkRect::MakeWH(300, 200));
88 recorder.clipRect(SkRect::MakeWH(100, 100));
89 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -080090 REPORTER_ASSERT(r, pattern.match(&record, index));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000091}
92
93DEF_TEST(RecordPattern_Complex, r) {
94 Pattern3<Is<Save>,
95 Star<Not<Or3<Is<Save>,
96 Is<Restore>,
97 IsDraw> > >,
98 Is<Restore> > pattern;
99
100 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000101 SkRecorder recorder(&record, 1920, 1200);
reed2ff1fce2014-12-11 07:07:37 -0800102 unsigned start, begin, end;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000103
reed2ff1fce2014-12-11 07:07:37 -0800104 start = record.count();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000105 recorder.save();
106 recorder.clipRect(SkRect::MakeWH(300, 200));
107 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -0800108 REPORTER_ASSERT(r, pattern.match(&record, 0) == record.count());
109 end = start;
110 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
111 REPORTER_ASSERT(r, begin == start);
112 REPORTER_ASSERT(r, end == record.count());
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000113
reed2ff1fce2014-12-11 07:07:37 -0800114 start = record.count();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000115 recorder.save();
116 recorder.clipRect(SkRect::MakeWH(300, 200));
117 recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
118 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -0800119 REPORTER_ASSERT(r, !pattern.match(&record, start));
120 end = start;
121 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000122
reed2ff1fce2014-12-11 07:07:37 -0800123 start = record.count();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000124 recorder.save();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000125 recorder.clipRect(SkRect::MakeWH(300, 200));
126 recorder.clipRect(SkRect::MakeWH(100, 400));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000127 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -0800128 REPORTER_ASSERT(r, pattern.match(&record, start) == record.count());
129 end = start;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000130 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
reed2ff1fce2014-12-11 07:07:37 -0800131 REPORTER_ASSERT(r, begin == start);
132 REPORTER_ASSERT(r, end == record.count());
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000133
134 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
135}
commit-bot@chromium.org1b546462014-05-06 21:32:19 +0000136
137DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) {
138 Pattern1<IsDraw> pattern;
139
140 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000141 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org1b546462014-05-06 21:32:19 +0000142 recorder.saveLayer(NULL, NULL);
143
144 REPORTER_ASSERT(r, !pattern.match(&record, 0));
145}