Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
RecordPatternTest.cpp
Go to the documentation of this file.
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
10#include "include/core/SkRect.h"
11#include "src/core/SkRecord.h"
13#include "src/core/SkRecorder.h"
14#include "src/core/SkRecords.h"
15#include "tests/Test.h"
16
17using namespace SkRecords;
18typedef Pattern<Is<Save>,
22
23DEF_TEST(RecordPattern_Simple, r) {
24 SaveClipRectRestore pattern;
25
26 SkRecord record;
27 REPORTER_ASSERT(r, !pattern.match(&record, 0));
28
29 SkRecorder recorder(&record, 1920, 1200);
30
31 // Build up a save-clip-restore block. The pattern will match only it's complete.
32 recorder.save();
33 REPORTER_ASSERT(r, !pattern.match(&record, 0));
34
35 recorder.clipRect(SkRect::MakeWH(300, 200));
36 REPORTER_ASSERT(r, !pattern.match(&record, 0));
37
38 recorder.restore();
39 REPORTER_ASSERT(r, pattern.match(&record, 0));
40 REPORTER_ASSERT(r, pattern.first<Save>() != nullptr);
41 REPORTER_ASSERT(r, pattern.second<ClipRect>() != nullptr);
42 REPORTER_ASSERT(r, pattern.third<Restore>() != nullptr);
43}
44
45DEF_TEST(RecordPattern_StartingIndex, r) {
46 SaveClipRectRestore pattern;
47
48 SkRecord record;
49 SkRecorder recorder(&record, 1920, 1200);
50
51 // There will be two save-clipRect-restore blocks [0,3) and [3,6).
52 for (int i = 0; i < 2; i++) {
53 recorder.save();
54 recorder.clipRect(SkRect::MakeWH(300, 200));
55 recorder.restore();
56 }
57
58 // We should match only at 0 and 3. Going over the length should fail gracefully.
59 for (int i = 0; i < 8; i++) {
60 if (i == 0 || i == 3) {
61 REPORTER_ASSERT(r, pattern.match(&record, i) == i + 3);
62 } else {
63 REPORTER_ASSERT(r, !pattern.match(&record, i));
64 }
65 }
66}
67
68DEF_TEST(RecordPattern_DontMatchSubsequences, r) {
69 SaveClipRectRestore pattern;
70
71 SkRecord record;
72 SkRecorder recorder(&record, 1920, 1200);
73
74 recorder.save();
75 recorder.clipRect(SkRect::MakeWH(300, 200));
76 recorder.drawRect(SkRect::MakeWH(600, 300), SkPaint());
77 recorder.restore();
78
79 REPORTER_ASSERT(r, !pattern.match(&record, 0));
80}
81
82DEF_TEST(RecordPattern_Greedy, r) {
84
85 SkRecord record;
86 SkRecorder recorder(&record, 1920, 1200);
87 int index = 0;
88
89 recorder.save();
90 recorder.clipRect(SkRect::MakeWH(300, 200));
91 recorder.restore();
92 REPORTER_ASSERT(r, pattern.match(&record, index));
93 index += 3;
94
95 recorder.save();
96 recorder.clipRect(SkRect::MakeWH(300, 200));
97 recorder.clipRect(SkRect::MakeWH(100, 100));
98 recorder.restore();
99 REPORTER_ASSERT(r, pattern.match(&record, index));
100}
101
102DEF_TEST(RecordPattern_Complex, r) {
106 IsDraw>>>,
107 Is<Restore>> pattern;
108
109 SkRecord record;
110 SkRecorder recorder(&record, 1920, 1200);
111 int start, begin, end;
112
113 start = record.count();
114 recorder.save();
115 recorder.clipRect(SkRect::MakeWH(300, 200));
116 recorder.restore();
117 REPORTER_ASSERT(r, pattern.match(&record, 0) == record.count());
118 end = start;
119 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
121 REPORTER_ASSERT(r, end == record.count());
122
123 start = record.count();
124 recorder.save();
125 recorder.clipRect(SkRect::MakeWH(300, 200));
126 recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
127 recorder.restore();
128 REPORTER_ASSERT(r, !pattern.match(&record, start));
129 end = start;
130 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
131
132 start = record.count();
133 recorder.save();
134 recorder.clipRect(SkRect::MakeWH(300, 200));
135 recorder.clipRect(SkRect::MakeWH(100, 400));
136 recorder.restore();
137 REPORTER_ASSERT(r, pattern.match(&record, start) == record.count());
138 end = start;
139 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
141 REPORTER_ASSERT(r, end == record.count());
142
143 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
144}
145
146DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) {
147 Pattern<IsDraw> pattern;
148
149 SkRecord record;
150 SkRecorder recorder(&record, 1920, 1200);
151 recorder.saveLayer(nullptr, nullptr);
152
153 REPORTER_ASSERT(r, !pattern.match(&record, 0));
154}
Pattern< Is< Save >, Is< ClipRect >, Is< Restore > > SaveClipRectRestore
#define DEF_TEST(name, reporter)
Definition Test.h:312
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
int save()
Definition SkCanvas.cpp:451
int count() const
Definition SkRecord.h:38
static const char * begin(const StringSlice &s)
Definition editor.cpp:252
glong glong end
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609