blob: 33a0114c3591f671eeb3a2ff6af5001ec82e89de [file] [log] [blame]
mtkleinc6ad06a2015-08-19 09:51:00 -07001/*
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
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +00008#include "Test.h"
9
10#include "SkRecord.h"
11#include "SkRecordPattern.h"
12#include "SkRecorder.h"
13#include "SkRecords.h"
14
15using namespace SkRecords;
mtklein24e7db82015-11-19 08:53:27 -080016typedef Pattern<Is<Save>,
17 Is<ClipRect>,
18 Is<Restore>>
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000019 SaveClipRectRestore;
20
21DEF_TEST(RecordPattern_Simple, r) {
22 SaveClipRectRestore pattern;
23
24 SkRecord record;
25 REPORTER_ASSERT(r, !pattern.match(&record, 0));
26
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000027 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000028
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));
halcanary96fcdcc2015-08-27 07:41:13 -070038 REPORTER_ASSERT(r, pattern.first<Save>() != nullptr);
39 REPORTER_ASSERT(r, pattern.second<ClipRect>() != nullptr);
40 REPORTER_ASSERT(r, pattern.third<Restore>() != nullptr);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000041}
42
43DEF_TEST(RecordPattern_StartingIndex, r) {
44 SaveClipRectRestore pattern;
45
46 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000047 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000048
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.
mtkleinc6ad06a2015-08-19 09:51:00 -070057 for (int i = 0; i < 8; i++) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000058 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
66DEF_TEST(RecordPattern_DontMatchSubsequences, r) {
67 SaveClipRectRestore pattern;
68
69 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000070 SkRecorder recorder(&record, 1920, 1200);
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000071
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
mtklein24e7db82015-11-19 08:53:27 -080080DEF_TEST(RecordPattern_Greedy, r) {
81 Pattern<Is<Save>, Greedy<Is<ClipRect>>, Is<Restore>> pattern;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000082
83 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +000084 SkRecorder recorder(&record, 1920, 1200);
reed2ff1fce2014-12-11 07:07:37 -080085 int index = 0;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000086
87 recorder.save();
88 recorder.clipRect(SkRect::MakeWH(300, 200));
89 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -080090 REPORTER_ASSERT(r, pattern.match(&record, index));
91 index += 3;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000092
93 recorder.save();
94 recorder.clipRect(SkRect::MakeWH(300, 200));
95 recorder.clipRect(SkRect::MakeWH(100, 100));
96 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -080097 REPORTER_ASSERT(r, pattern.match(&record, index));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000098}
99
100DEF_TEST(RecordPattern_Complex, r) {
mtklein24e7db82015-11-19 08:53:27 -0800101 Pattern<Is<Save>,
102 Greedy<Not<Or<Is<Save>,
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000103 Is<Restore>,
mtklein24e7db82015-11-19 08:53:27 -0800104 IsDraw>>>,
105 Is<Restore>> pattern;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000106
107 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000108 SkRecorder recorder(&record, 1920, 1200);
mtkleinc6ad06a2015-08-19 09:51:00 -0700109 int start, begin, end;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000110
reed2ff1fce2014-12-11 07:07:37 -0800111 start = record.count();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000112 recorder.save();
113 recorder.clipRect(SkRect::MakeWH(300, 200));
114 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -0800115 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());
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000120
reed2ff1fce2014-12-11 07:07:37 -0800121 start = record.count();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000122 recorder.save();
123 recorder.clipRect(SkRect::MakeWH(300, 200));
124 recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint());
125 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -0800126 REPORTER_ASSERT(r, !pattern.match(&record, start));
127 end = start;
128 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000129
reed2ff1fce2014-12-11 07:07:37 -0800130 start = record.count();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000131 recorder.save();
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000132 recorder.clipRect(SkRect::MakeWH(300, 200));
133 recorder.clipRect(SkRect::MakeWH(100, 400));
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000134 recorder.restore();
reed2ff1fce2014-12-11 07:07:37 -0800135 REPORTER_ASSERT(r, pattern.match(&record, start) == record.count());
136 end = start;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000137 REPORTER_ASSERT(r, pattern.search(&record, &begin, &end));
reed2ff1fce2014-12-11 07:07:37 -0800138 REPORTER_ASSERT(r, begin == start);
139 REPORTER_ASSERT(r, end == record.count());
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000140
141 REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end));
142}
commit-bot@chromium.org1b546462014-05-06 21:32:19 +0000143
144DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) {
mtklein24e7db82015-11-19 08:53:27 -0800145 Pattern<IsDraw> pattern;
commit-bot@chromium.org1b546462014-05-06 21:32:19 +0000146
147 SkRecord record;
commit-bot@chromium.orga0950412014-05-29 16:52:40 +0000148 SkRecorder recorder(&record, 1920, 1200);
halcanary96fcdcc2015-08-27 07:41:13 -0700149 recorder.saveLayer(nullptr, nullptr);
commit-bot@chromium.org1b546462014-05-06 21:32:19 +0000150
151 REPORTER_ASSERT(r, !pattern.match(&record, 0));
152}