1#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
20    SkRecorder recorder(&record, 1920, 1200);
21
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;
40    SkRecorder recorder(&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 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;
63    SkRecorder recorder(&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
73DEF_TEST(RecordPattern_Star, r) {
74    Pattern3<Is<Save>, Star<Is<ClipRect> >, Is<Restore> > pattern;
75
76    SkRecord record;
77    SkRecorder recorder(&record, 1920, 1200);
78    int index = 0;
79
80    recorder.save();
81        recorder.clipRect(SkRect::MakeWH(300, 200));
82    recorder.restore();
83    REPORTER_ASSERT(r, pattern.match(&record, index));
84    index += 3;
85
86    recorder.save();
87        recorder.clipRect(SkRect::MakeWH(300, 200));
88        recorder.clipRect(SkRect::MakeWH(100, 100));
89    recorder.restore();
90    REPORTER_ASSERT(r, pattern.match(&record, index));
91}
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;
101    SkRecorder recorder(&record, 1920, 1200);
102    unsigned start, begin, end;
103
104    start = record.count();
105    recorder.save();
106        recorder.clipRect(SkRect::MakeWH(300, 200));
107    recorder.restore();
108    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());
113
114    start = record.count();
115    recorder.save();
116        recorder.clipRect(SkRect::MakeWH(300, 200));
117        recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
118    recorder.restore();
119    REPORTER_ASSERT(r, !pattern.match(&record, start));
120    end = start;
121    REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
122
123    start = record.count();
124    recorder.save();
125        recorder.clipRect(SkRect::MakeWH(300, 200));
126        recorder.clipRect(SkRect::MakeWH(100, 400));
127    recorder.restore();
128    REPORTER_ASSERT(r, pattern.match(&record, start) == record.count());
129    end = start;
130    REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
131    REPORTER_ASSERT(r, begin == start);
132    REPORTER_ASSERT(r, end == record.count());
133
134    REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
135}
136
137DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) {
138    Pattern1<IsDraw> pattern;
139
140    SkRecord record;
141    SkRecorder recorder(&record, 1920, 1200);
142    recorder.saveLayer(NULL, NULL);
143
144    REPORTER_ASSERT(r, !pattern.match(&record, 0));
145}
146