scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 8 | // Make sure SkUserConfig.h is included so #defines are available on |
| 9 | // Android. |
| 10 | #include "include/core/SkTypes.h" |
| 11 | #ifdef SK_ENABLE_ANDROID_UTILS |
| 12 | #include "client_utils/android/FrontBufferedStream.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/codec/SkCodec.h" |
| 14 | #include "include/core/SkBitmap.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkStream.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/core/SkAutoMalloc.h" |
| 18 | #include "tests/Test.h" |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 19 | |
| 20 | static void test_read(skiatest::Reporter* reporter, SkStream* bufferedStream, |
| 21 | const void* expectations, size_t bytesToRead) { |
| 22 | // output for reading bufferedStream. |
| 23 | SkAutoMalloc storage(bytesToRead); |
| 24 | |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 25 | const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 26 | REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd()); |
| 27 | REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0); |
| 28 | } |
| 29 | |
| 30 | static void test_rewind(skiatest::Reporter* reporter, |
| 31 | SkStream* bufferedStream, bool shouldSucceed) { |
| 32 | const bool success = bufferedStream->rewind(); |
| 33 | REPORTER_ASSERT(reporter, success == shouldSucceed); |
| 34 | } |
| 35 | |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 36 | // Test that hasLength() returns the correct value, based on the stream |
| 37 | // being wrapped. A length can only be known if the wrapped stream has a |
| 38 | // length and it has a position (so its initial position can be taken into |
| 39 | // account when computing the length). |
| 40 | static void test_hasLength(skiatest::Reporter* reporter, |
| 41 | const SkStream& bufferedStream, |
| 42 | const SkStream& streamBeingBuffered) { |
| 43 | if (streamBeingBuffered.hasLength() && streamBeingBuffered.hasPosition()) { |
| 44 | REPORTER_ASSERT(reporter, bufferedStream.hasLength()); |
| 45 | } else { |
| 46 | REPORTER_ASSERT(reporter, !bufferedStream.hasLength()); |
| 47 | } |
| 48 | } |
| 49 | |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 50 | // All tests will buffer this string, and compare output to the original. |
| 51 | // The string is long to ensure that all of our lengths being tested are |
| 52 | // smaller than the string length. |
| 53 | const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"; |
| 54 | |
| 55 | // Tests reading the stream across boundaries of what has been buffered so far and what |
| 56 | // the total buffer size is. |
| 57 | static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) { |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 58 | // NOTE: For this and other tests in this file, we cheat and continue to refer to the |
| 59 | // wrapped stream, but that's okay because we know the wrapping stream has not been |
| 60 | // deleted yet (and we only call const methods in it). |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 61 | SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release(); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 62 | |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 63 | auto bufferedStream = android::skia::FrontBufferedStream::Make( |
| 64 | std::unique_ptr<SkStream>(memStream), bufferSize); |
| 65 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 66 | test_hasLength(reporter, *bufferedStream, *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 67 | |
| 68 | // First, test reading less than the max buffer size. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 69 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 70 | |
| 71 | // Now test rewinding back to the beginning and reading less than what was |
| 72 | // already buffered. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 73 | test_rewind(reporter, bufferedStream.get(), true); |
| 74 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 75 | |
| 76 | // Now test reading part of what was buffered, and buffering new data. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 77 | test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 78 | |
| 79 | // Now test reading what was buffered, buffering new data, and |
| 80 | // reading directly from the stream. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 81 | test_rewind(reporter, bufferedStream.get(), true); |
| 82 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 83 | |
| 84 | // We have reached the end of the buffer, so rewinding will fail. |
| 85 | // This test assumes that the stream is larger than the buffer; otherwise the |
| 86 | // result of rewind should be true. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 87 | test_rewind(reporter, bufferedStream.get(), false); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) { |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 91 | SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release(); |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 92 | auto bufferedStream = android::skia::FrontBufferedStream::Make( |
| 93 | std::unique_ptr<SkStream>(memStream), bufferSize); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 94 | test_hasLength(reporter, *bufferedStream, *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 95 | |
| 96 | // Read exactly the amount that fits in the buffer. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 97 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 98 | |
| 99 | // Rewinding should succeed. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 100 | test_rewind(reporter, bufferedStream.get(), true); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 101 | |
| 102 | // Once again reading buffered info should succeed |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 103 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 104 | |
| 105 | // Read past the size of the buffer. At this point, we cannot return. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 106 | test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1); |
| 107 | test_rewind(reporter, bufferedStream.get(), false); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { |
Mike Reed | 98c5d92 | 2017-09-15 21:39:47 -0400 | [diff] [blame] | 111 | SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release(); |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 112 | auto bufferedStream = android::skia::FrontBufferedStream::Make( |
| 113 | std::unique_ptr<SkStream>(memStream), bufferSize); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 114 | test_hasLength(reporter, *bufferedStream, *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 115 | |
| 116 | // Skip half the buffer. |
| 117 | bufferedStream->skip(bufferSize / 2); |
| 118 | |
| 119 | // Rewind, then read part of the buffer, which should have been read. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 120 | test_rewind(reporter, bufferedStream.get(), true); |
| 121 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 122 | |
| 123 | // Now skip beyond the buffered piece, but still within the total buffer. |
| 124 | bufferedStream->skip(bufferSize / 2); |
| 125 | |
| 126 | // Test that reading will still work. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 127 | test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 128 | |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 129 | test_rewind(reporter, bufferedStream.get(), true); |
| 130 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a |
| 134 | // Java InputStream, it does not know that it is at the end until it has attempted to read beyond |
| 135 | // the end and failed. Used by test_read_beyond_buffer. |
| 136 | class AndroidLikeMemoryStream : public SkMemoryStream { |
| 137 | public: |
| 138 | AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory) |
| 139 | : INHERITED(data, size, ownMemory) |
| 140 | , fIsAtEnd(false) {} |
| 141 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 142 | size_t read(void* dst, size_t requested) override { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 143 | size_t bytesRead = this->INHERITED::read(dst, requested); |
| 144 | if (bytesRead < requested) { |
| 145 | fIsAtEnd = true; |
| 146 | } |
| 147 | return bytesRead; |
| 148 | } |
| 149 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 150 | bool isAtEnd() const override { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 151 | return fIsAtEnd; |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | bool fIsAtEnd; |
| 156 | typedef SkMemoryStream INHERITED; |
| 157 | }; |
| 158 | |
| 159 | // This test ensures that buffering the exact length of the stream and attempting to read beyond it |
| 160 | // does not invalidate the buffer. |
| 161 | static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) { |
| 162 | // Use a stream that behaves like Android's stream. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 163 | AndroidLikeMemoryStream* memStream = |
| 164 | new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 165 | |
| 166 | // Create a buffer that matches the length of the stream. |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 167 | auto bufferedStream = android::skia::FrontBufferedStream::Make( |
| 168 | std::unique_ptr<SkStream>(memStream), bufferSize); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame^] | 169 | test_hasLength(reporter, *bufferedStream, *memStream); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 170 | |
| 171 | // Attempt to read one more than the bufferSize |
| 172 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); |
| 173 | test_rewind(reporter, bufferedStream.get(), true); |
| 174 | |
| 175 | // Ensure that the initial read did not invalidate the buffer. |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 176 | test_read(reporter, bufferedStream.get(), gAbcs, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Leon Scroggins III | 577536a | 2020-07-31 13:47:50 -0400 | [diff] [blame] | 179 | // Mock stream that optionally has a length and/or position. Tests that FrontBufferedStream's |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 180 | // length depends on the stream it's buffering having a length and position. |
| 181 | class LengthOptionalStream : public SkStream { |
| 182 | public: |
| 183 | LengthOptionalStream(bool hasLength, bool hasPosition) |
| 184 | : fHasLength(hasLength) |
| 185 | , fHasPosition(hasPosition) |
| 186 | {} |
| 187 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 188 | bool hasLength() const override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 189 | return fHasLength; |
| 190 | } |
| 191 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 192 | bool hasPosition() const override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 193 | return fHasPosition; |
| 194 | } |
| 195 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 196 | size_t read(void*, size_t) override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 200 | bool isAtEnd() const override { |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 201 | return true; |
| 202 | } |
| 203 | |
| 204 | private: |
| 205 | const bool fHasLength; |
| 206 | const bool fHasPosition; |
| 207 | }; |
| 208 | |
| 209 | // Test all possible combinations of the wrapped stream having a length and a position. |
| 210 | static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) { |
| 211 | for (int hasLen = 0; hasLen <= 1; hasLen++) { |
| 212 | for (int hasPos = 0; hasPos <= 1; hasPos++) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 213 | LengthOptionalStream* stream = |
| 214 | new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos)); |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 215 | auto buffered = android::skia::FrontBufferedStream::Make( |
| 216 | std::unique_ptr<SkStream>(stream), bufferSize); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame^] | 217 | test_hasLength(reporter, *buffered, *stream); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Test using a stream with an initial offset. |
| 223 | static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 224 | SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 225 | |
| 226 | // Skip a few characters into the memStream, so that bufferedStream represents an offset into |
| 227 | // the stream it wraps. |
| 228 | const size_t arbitraryOffset = 17; |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 229 | memStream->skip(arbitraryOffset); |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 230 | auto bufferedStream = android::skia::FrontBufferedStream::Make( |
| 231 | std::unique_ptr<SkStream>(memStream), bufferSize); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 232 | |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 233 | // Since SkMemoryStream has a length, bufferedStream must also. |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 234 | REPORTER_ASSERT(reporter, bufferedStream->hasLength()); |
| 235 | |
| 236 | const size_t amountToRead = 10; |
| 237 | const size_t bufferedLength = bufferedStream->getLength(); |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 238 | size_t currentPosition = 0; |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 239 | |
| 240 | // Read the stream in chunks. After each read, the position must match currentPosition, |
| 241 | // which sums the amount attempted to read, unless the end of the stream has been reached. |
| 242 | // Importantly, the end should not have been reached until currentPosition == bufferedLength. |
| 243 | while (currentPosition < bufferedLength) { |
| 244 | REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd()); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 245 | test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition, |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 246 | amountToRead); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 247 | currentPosition = std::min(currentPosition + amountToRead, bufferedLength); |
scroggo | ef0fed3 | 2016-02-18 05:59:25 -0800 | [diff] [blame] | 248 | REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 249 | } |
| 250 | REPORTER_ASSERT(reporter, bufferedStream->isAtEnd()); |
| 251 | REPORTER_ASSERT(reporter, bufferedLength == currentPosition); |
| 252 | } |
| 253 | |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 254 | static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) { |
| 255 | test_incremental_buffering(reporter, bufferSize); |
| 256 | test_perfectly_sized_buffer(reporter, bufferSize); |
| 257 | test_skipping(reporter, bufferSize); |
| 258 | test_read_beyond_buffer(reporter, bufferSize); |
commit-bot@chromium.org | 74b88b7 | 2014-02-10 22:03:21 +0000 | [diff] [blame] | 259 | test_length_combos(reporter, bufferSize); |
| 260 | test_initial_offset(reporter, bufferSize); |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 261 | } |
| 262 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 263 | DEF_TEST(FrontBufferedStream, reporter) { |
scroggo@google.com | 83fd2c7 | 2013-09-26 21:35:39 +0000 | [diff] [blame] | 264 | // Test 6 and 64, which are used by Android, as well as another arbitrary length. |
| 265 | test_buffers(reporter, 6); |
| 266 | test_buffers(reporter, 15); |
| 267 | test_buffers(reporter, 64); |
| 268 | } |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 269 | |
| 270 | // Test that a FrontBufferedStream does not allow reading after the end of a stream. |
Leon Scroggins III | 577536a | 2020-07-31 13:47:50 -0400 | [diff] [blame] | 271 | // This class is a mock SkStream which reports that it is at the end on the first |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 272 | // read (simulating a failure). Then it tracks whether someone calls read() again. |
| 273 | class FailingStream : public SkStream { |
| 274 | public: |
| 275 | FailingStream() |
| 276 | : fAtEnd(false) |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 277 | {} |
msarett | b9e56c1 | 2016-03-01 13:29:15 -0800 | [diff] [blame] | 278 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 279 | size_t read(void* buffer, size_t size) override { |
msarett | b9e56c1 | 2016-03-01 13:29:15 -0800 | [diff] [blame] | 280 | SkASSERT(!fAtEnd); |
| 281 | fAtEnd = true; |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 285 | bool isAtEnd() const override { |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 286 | return fAtEnd; |
| 287 | } |
| 288 | |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 289 | private: |
| 290 | bool fAtEnd; |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | DEF_TEST(ShortFrontBufferedStream, reporter) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 294 | FailingStream* failingStream = new FailingStream; |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 295 | auto stream = android::skia::FrontBufferedStream::Make( |
| 296 | std::unique_ptr<SkStream>(failingStream), 64); |
msarett | 7f7ec20 | 2016-03-01 12:12:27 -0800 | [diff] [blame] | 297 | |
| 298 | // This will fail to create a codec. However, what we really want to test is that we |
| 299 | // won't read past the end of the stream. |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 300 | std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream))); |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 301 | } |
Leon Scroggins III | 63cfb36 | 2020-04-24 13:00:48 -0400 | [diff] [blame] | 302 | #endif // SK_ENABLE_ANDROID_UTILS |