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