• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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 "Test.h"
9 
10 #include "SkRecord.h"
11 #include "SkRecordPattern.h"
12 #include "SkRecorder.h"
13 #include "SkRecords.h"
14 
15 using namespace SkRecords;
16 typedef Pattern<Is<Save>,
17                 Is<ClipRect>,
18                 Is<Restore>>
19     SaveClipRectRestore;
20 
DEF_TEST(RecordPattern_Simple,r)21 DEF_TEST(RecordPattern_Simple, r) {
22     SaveClipRectRestore pattern;
23 
24     SkRecord record;
25     REPORTER_ASSERT(r, !pattern.match(&record, 0));
26 
27     SkRecorder recorder(&record, 1920, 1200);
28 
29     // Build up a save-clip-restore block.  The pattern will match only it's complete.
30     recorder.save();
31     REPORTER_ASSERT(r, !pattern.match(&record, 0));
32 
33     recorder.clipRect(SkRect::MakeWH(300, 200));
34     REPORTER_ASSERT(r, !pattern.match(&record, 0));
35 
36     recorder.restore();
37     REPORTER_ASSERT(r, pattern.match(&record, 0));
38     REPORTER_ASSERT(r, pattern.first<Save>()      != nullptr);
39     REPORTER_ASSERT(r, pattern.second<ClipRect>() != nullptr);
40     REPORTER_ASSERT(r, pattern.third<Restore>()   != nullptr);
41 }
42 
DEF_TEST(RecordPattern_StartingIndex,r)43 DEF_TEST(RecordPattern_StartingIndex, r) {
44     SaveClipRectRestore pattern;
45 
46     SkRecord record;
47     SkRecorder recorder(&record, 1920, 1200);
48 
49     // There will be two save-clipRect-restore blocks [0,3) and [3,6).
50     for (int i = 0; i < 2; i++) {
51         recorder.save();
52             recorder.clipRect(SkRect::MakeWH(300, 200));
53         recorder.restore();
54     }
55 
56     // We should match only at 0 and 3.  Going over the length should fail gracefully.
57     for (int i = 0; i < 8; i++) {
58         if (i == 0 || i == 3) {
59             REPORTER_ASSERT(r, pattern.match(&record, i) == i + 3);
60         } else {
61             REPORTER_ASSERT(r, !pattern.match(&record, i));
62         }
63     }
64 }
65 
DEF_TEST(RecordPattern_DontMatchSubsequences,r)66 DEF_TEST(RecordPattern_DontMatchSubsequences, r) {
67     SaveClipRectRestore pattern;
68 
69     SkRecord record;
70     SkRecorder recorder(&record, 1920, 1200);
71 
72     recorder.save();
73         recorder.clipRect(SkRect::MakeWH(300, 200));
74         recorder.drawRect(SkRect::MakeWH(600, 300), SkPaint());
75     recorder.restore();
76 
77     REPORTER_ASSERT(r, !pattern.match(&record, 0));
78 }
79 
DEF_TEST(RecordPattern_Greedy,r)80 DEF_TEST(RecordPattern_Greedy, r) {
81     Pattern<Is<Save>, Greedy<Is<ClipRect>>, Is<Restore>> pattern;
82 
83     SkRecord record;
84     SkRecorder recorder(&record, 1920, 1200);
85     int index = 0;
86 
87     recorder.save();
88         recorder.clipRect(SkRect::MakeWH(300, 200));
89     recorder.restore();
90     REPORTER_ASSERT(r, pattern.match(&record, index));
91     index += 3;
92 
93     recorder.save();
94         recorder.clipRect(SkRect::MakeWH(300, 200));
95         recorder.clipRect(SkRect::MakeWH(100, 100));
96     recorder.restore();
97     REPORTER_ASSERT(r, pattern.match(&record, index));
98 }
99 
DEF_TEST(RecordPattern_Complex,r)100 DEF_TEST(RecordPattern_Complex, r) {
101     Pattern<Is<Save>,
102             Greedy<Not<Or<Is<Save>,
103                           Is<Restore>,
104                           IsDraw>>>,
105             Is<Restore>> pattern;
106 
107     SkRecord record;
108     SkRecorder recorder(&record, 1920, 1200);
109     int start, begin, end;
110 
111     start = record.count();
112     recorder.save();
113         recorder.clipRect(SkRect::MakeWH(300, 200));
114     recorder.restore();
115     REPORTER_ASSERT(r, pattern.match(&record, 0) == record.count());
116     end = start;
117     REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
118     REPORTER_ASSERT(r, begin == start);
119     REPORTER_ASSERT(r, end == record.count());
120 
121     start = record.count();
122     recorder.save();
123         recorder.clipRect(SkRect::MakeWH(300, 200));
124         recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
125     recorder.restore();
126     REPORTER_ASSERT(r, !pattern.match(&record, start));
127     end = start;
128     REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
129 
130     start = record.count();
131     recorder.save();
132         recorder.clipRect(SkRect::MakeWH(300, 200));
133         recorder.clipRect(SkRect::MakeWH(100, 400));
134     recorder.restore();
135     REPORTER_ASSERT(r, pattern.match(&record, start) == record.count());
136     end = start;
137     REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
138     REPORTER_ASSERT(r, begin == start);
139     REPORTER_ASSERT(r, end == record.count());
140 
141     REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
142 }
143 
DEF_TEST(RecordPattern_SaveLayerIsNotADraw,r)144 DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) {
145     Pattern<IsDraw> pattern;
146 
147     SkRecord record;
148     SkRecorder recorder(&record, 1920, 1200);
149     recorder.saveLayer(nullptr, nullptr);
150 
151     REPORTER_ASSERT(r, !pattern.match(&record, 0));
152 }
153