mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 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; |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 16 | typedef Pattern<Is<Save>, |
| 17 | Is<ClipRect>, |
| 18 | Is<Restore>> |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 19 | SaveClipRectRestore; |
| 20 | |
| 21 | DEF_TEST(RecordPattern_Simple, r) { |
| 22 | SaveClipRectRestore pattern; |
| 23 | |
| 24 | SkRecord record; |
| 25 | REPORTER_ASSERT(r, !pattern.match(&record, 0)); |
| 26 | |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 27 | SkRecorder recorder(&record, 1920, 1200); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 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)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | 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.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | DEF_TEST(RecordPattern_StartingIndex, r) { |
| 44 | SaveClipRectRestore pattern; |
| 45 | |
| 46 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 47 | SkRecorder recorder(&record, 1920, 1200); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 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. |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 57 | for (int i = 0; i < 8; i++) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 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 | |
| 66 | DEF_TEST(RecordPattern_DontMatchSubsequences, r) { |
| 67 | SaveClipRectRestore pattern; |
| 68 | |
| 69 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 70 | SkRecorder recorder(&record, 1920, 1200); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 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 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 80 | DEF_TEST(RecordPattern_Greedy, r) { |
| 81 | Pattern<Is<Save>, Greedy<Is<ClipRect>>, Is<Restore>> pattern; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 82 | |
| 83 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 84 | SkRecorder recorder(&record, 1920, 1200); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 85 | int index = 0; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 86 | |
| 87 | recorder.save(); |
| 88 | recorder.clipRect(SkRect::MakeWH(300, 200)); |
| 89 | recorder.restore(); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 90 | REPORTER_ASSERT(r, pattern.match(&record, index)); |
| 91 | index += 3; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 92 | |
| 93 | recorder.save(); |
| 94 | recorder.clipRect(SkRect::MakeWH(300, 200)); |
| 95 | recorder.clipRect(SkRect::MakeWH(100, 100)); |
| 96 | recorder.restore(); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 97 | REPORTER_ASSERT(r, pattern.match(&record, index)); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | DEF_TEST(RecordPattern_Complex, r) { |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 101 | Pattern<Is<Save>, |
| 102 | Greedy<Not<Or<Is<Save>, |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 103 | Is<Restore>, |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 104 | IsDraw>>>, |
| 105 | Is<Restore>> pattern; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 106 | |
| 107 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 108 | SkRecorder recorder(&record, 1920, 1200); |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 109 | int start, begin, end; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 110 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 111 | start = record.count(); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 112 | recorder.save(); |
| 113 | recorder.clipRect(SkRect::MakeWH(300, 200)); |
| 114 | recorder.restore(); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 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()); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 120 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 121 | start = record.count(); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 122 | recorder.save(); |
| 123 | recorder.clipRect(SkRect::MakeWH(300, 200)); |
| 124 | recorder.drawRect(SkRect::MakeWH(100, 3000), SkPaint()); |
| 125 | recorder.restore(); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 126 | REPORTER_ASSERT(r, !pattern.match(&record, start)); |
| 127 | end = start; |
| 128 | REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end)); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 129 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 130 | start = record.count(); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 131 | recorder.save(); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 132 | recorder.clipRect(SkRect::MakeWH(300, 200)); |
| 133 | recorder.clipRect(SkRect::MakeWH(100, 400)); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 134 | recorder.restore(); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 135 | REPORTER_ASSERT(r, pattern.match(&record, start) == record.count()); |
| 136 | end = start; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 137 | REPORTER_ASSERT(r, pattern.search(&record, &begin, &end)); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 138 | REPORTER_ASSERT(r, begin == start); |
| 139 | REPORTER_ASSERT(r, end == record.count()); |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 140 | |
| 141 | REPORTER_ASSERT(r, !pattern.search(&record, &begin, &end)); |
| 142 | } |
commit-bot@chromium.org | 1b54646 | 2014-05-06 21:32:19 +0000 | [diff] [blame] | 143 | |
| 144 | DEF_TEST(RecordPattern_SaveLayerIsNotADraw, r) { |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 145 | Pattern<IsDraw> pattern; |
commit-bot@chromium.org | 1b54646 | 2014-05-06 21:32:19 +0000 | [diff] [blame] | 146 | |
| 147 | SkRecord record; |
commit-bot@chromium.org | a095041 | 2014-05-29 16:52:40 +0000 | [diff] [blame] | 148 | SkRecorder recorder(&record, 1920, 1200); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | recorder.saveLayer(nullptr, nullptr); |
commit-bot@chromium.org | 1b54646 | 2014-05-06 21:32:19 +0000 | [diff] [blame] | 150 | |
| 151 | REPORTER_ASSERT(r, !pattern.match(&record, 0)); |
| 152 | } |