| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
| 9 #include "SkFrontBufferedStream.h" | 9 #include "SkFrontBufferedStream.h" |
| 10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 // All tests will buffer this string, and compare output to the original. | 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 | 47 // The string is long to ensure that all of our lengths being tested are |
| 48 // smaller than the string length. | 48 // smaller than the string length. |
| 49 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef
ghijklmnopqrstuvwx"; | 49 const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdef
ghijklmnopqrstuvwx"; |
| 50 | 50 |
| 51 // Tests reading the stream across boundaries of what has been buffered so far a
nd what | 51 // Tests reading the stream across boundaries of what has been buffered so far a
nd what |
| 52 // the total buffer size is. | 52 // the total buffer size is. |
| 53 static void test_incremental_buffering(skiatest::Reporter* reporter, size_t buff
erSize) { | 53 static void test_incremental_buffering(skiatest::Reporter* reporter, size_t buff
erSize) { |
| 54 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); | 54 // NOTE: For this and other tests in this file, we cheat and continue to ref
er to the |
| 55 // wrapped stream, but that's okay because we know the wrapping stream has n
ot been |
| 56 // deleted yet (and we only call const methods in it). |
| 57 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); |
| 55 | 58 |
| 56 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStre
am, bufferSize)); | 59 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
| 57 test_hasLength(reporter, *bufferedStream.get(), memStream); | 60 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
| 58 | 61 |
| 59 // First, test reading less than the max buffer size. | 62 // First, test reading less than the max buffer size. |
| 60 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); | 63 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); |
| 61 | 64 |
| 62 // Now test rewinding back to the beginning and reading less than what was | 65 // Now test rewinding back to the beginning and reading less than what was |
| 63 // already buffered. | 66 // already buffered. |
| 64 test_rewind(reporter, bufferedStream, true); | 67 test_rewind(reporter, bufferedStream, true); |
| 65 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); | 68 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
| 66 | 69 |
| 67 // Now test reading part of what was buffered, and buffering new data. | 70 // Now test reading part of what was buffered, and buffering new data. |
| 68 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), b
ufferSize / 2); | 71 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), b
ufferSize / 2); |
| 69 | 72 |
| 70 // Now test reading what was buffered, buffering new data, and | 73 // Now test reading what was buffered, buffering new data, and |
| 71 // reading directly from the stream. | 74 // reading directly from the stream. |
| 72 test_rewind(reporter, bufferedStream, true); | 75 test_rewind(reporter, bufferedStream, true); |
| 73 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); | 76 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); |
| 74 | 77 |
| 75 // We have reached the end of the buffer, so rewinding will fail. | 78 // We have reached the end of the buffer, so rewinding will fail. |
| 76 // This test assumes that the stream is larger than the buffer; otherwise th
e | 79 // This test assumes that the stream is larger than the buffer; otherwise th
e |
| 77 // result of rewind should be true. | 80 // result of rewind should be true. |
| 78 test_rewind(reporter, bufferedStream, false); | 81 test_rewind(reporter, bufferedStream, false); |
| 79 } | 82 } |
| 80 | 83 |
| 81 static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t buf
ferSize) { | 84 static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t buf
ferSize) { |
| 82 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); | 85 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); |
| 83 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStre
am, bufferSize)); | 86 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
| 84 test_hasLength(reporter, *bufferedStream.get(), memStream); | 87 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
| 85 | 88 |
| 86 // Read exactly the amount that fits in the buffer. | 89 // Read exactly the amount that fits in the buffer. |
| 87 test_read(reporter, bufferedStream, gAbcs, bufferSize); | 90 test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 88 | 91 |
| 89 // Rewinding should succeed. | 92 // Rewinding should succeed. |
| 90 test_rewind(reporter, bufferedStream, true); | 93 test_rewind(reporter, bufferedStream, true); |
| 91 | 94 |
| 92 // Once again reading buffered info should succeed | 95 // Once again reading buffered info should succeed |
| 93 test_read(reporter, bufferedStream, gAbcs, bufferSize); | 96 test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 94 | 97 |
| 95 // Read past the size of the buffer. At this point, we cannot return. | 98 // Read past the size of the buffer. At this point, we cannot return. |
| 96 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1
); | 99 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1
); |
| 97 test_rewind(reporter, bufferedStream, false); | 100 test_rewind(reporter, bufferedStream, false); |
| 98 } | 101 } |
| 99 | 102 |
| 100 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { | 103 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { |
| 101 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); | 104 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); |
| 102 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStre
am, bufferSize)); | 105 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
| 103 test_hasLength(reporter, *bufferedStream.get(), memStream); | 106 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
| 104 | 107 |
| 105 // Skip half the buffer. | 108 // Skip half the buffer. |
| 106 bufferedStream->skip(bufferSize / 2); | 109 bufferedStream->skip(bufferSize / 2); |
| 107 | 110 |
| 108 // Rewind, then read part of the buffer, which should have been read. | 111 // Rewind, then read part of the buffer, which should have been read. |
| 109 test_rewind(reporter, bufferedStream, true); | 112 test_rewind(reporter, bufferedStream, true); |
| 110 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); | 113 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
| 111 | 114 |
| 112 // Now skip beyond the buffered piece, but still within the total buffer. | 115 // Now skip beyond the buffered piece, but still within the total buffer. |
| 113 bufferedStream->skip(bufferSize / 2); | 116 bufferedStream->skip(bufferSize / 2); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 142 | 145 |
| 143 private: | 146 private: |
| 144 bool fIsAtEnd; | 147 bool fIsAtEnd; |
| 145 typedef SkMemoryStream INHERITED; | 148 typedef SkMemoryStream INHERITED; |
| 146 }; | 149 }; |
| 147 | 150 |
| 148 // This test ensures that buffering the exact length of the stream and attemptin
g to read beyond it | 151 // This test ensures that buffering the exact length of the stream and attemptin
g to read beyond it |
| 149 // does not invalidate the buffer. | 152 // does not invalidate the buffer. |
| 150 static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferS
ize) { | 153 static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferS
ize) { |
| 151 // Use a stream that behaves like Android's stream. | 154 // Use a stream that behaves like Android's stream. |
| 152 AndroidLikeMemoryStream memStream((void*)gAbcs, bufferSize, false); | 155 AndroidLikeMemoryStream* memStream = SkNEW_ARGS(AndroidLikeMemoryStream, ((v
oid*)gAbcs, bufferSize, false)); |
| 153 | 156 |
| 154 // Create a buffer that matches the length of the stream. | 157 // Create a buffer that matches the length of the stream. |
| 155 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStre
am, bufferSize)); | 158 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
| 156 test_hasLength(reporter, *bufferedStream.get(), memStream); | 159 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
| 157 | 160 |
| 158 // Attempt to read one more than the bufferSize | 161 // Attempt to read one more than the bufferSize |
| 159 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); | 162 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); |
| 160 test_rewind(reporter, bufferedStream.get(), true); | 163 test_rewind(reporter, bufferedStream.get(), true); |
| 161 | 164 |
| 162 // Ensure that the initial read did not invalidate the buffer. | 165 // Ensure that the initial read did not invalidate the buffer. |
| 163 test_read(reporter, bufferedStream, gAbcs, bufferSize); | 166 test_read(reporter, bufferedStream, gAbcs, bufferSize); |
| 164 } | 167 } |
| 165 | 168 |
| 166 // Dummy stream that optionally has a length and/or position. Tests that FrontBu
fferedStream's | 169 // Dummy stream that optionally has a length and/or position. Tests that FrontBu
fferedStream's |
| (...skipping 23 matching lines...) Expand all Loading... |
| 190 | 193 |
| 191 private: | 194 private: |
| 192 const bool fHasLength; | 195 const bool fHasLength; |
| 193 const bool fHasPosition; | 196 const bool fHasPosition; |
| 194 }; | 197 }; |
| 195 | 198 |
| 196 // Test all possible combinations of the wrapped stream having a length and a po
sition. | 199 // Test all possible combinations of the wrapped stream having a length and a po
sition. |
| 197 static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize)
{ | 200 static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize)
{ |
| 198 for (int hasLen = 0; hasLen <= 1; hasLen++) { | 201 for (int hasLen = 0; hasLen <= 1; hasLen++) { |
| 199 for (int hasPos = 0; hasPos <= 1; hasPos++) { | 202 for (int hasPos = 0; hasPos <= 1; hasPos++) { |
| 200 LengthOptionalStream stream(SkToBool(hasLen), SkToBool(hasPos)); | 203 LengthOptionalStream* stream = SkNEW_ARGS(LengthOptionalStream, (SkT
oBool(hasLen), SkToBool(hasPos))); |
| 201 SkAutoTUnref<SkStream> buffered(SkFrontBufferedStream::Create(&strea
m, bufferSize)); | 204 SkAutoTDelete<SkStream> buffered(SkFrontBufferedStream::Create(strea
m, bufferSize)); |
| 202 test_hasLength(reporter, *buffered.get(), stream); | 205 test_hasLength(reporter, *buffered.get(), *stream); |
| 203 } | 206 } |
| 204 } | 207 } |
| 205 } | 208 } |
| 206 | 209 |
| 207 // Test using a stream with an initial offset. | 210 // Test using a stream with an initial offset. |
| 208 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize)
{ | 211 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize)
{ |
| 209 SkMemoryStream memStream(gAbcs, strlen(gAbcs), false); | 212 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); |
| 210 | 213 |
| 211 // Skip a few characters into the memStream, so that bufferedStream represen
ts an offset into | 214 // Skip a few characters into the memStream, so that bufferedStream represen
ts an offset into |
| 212 // the stream it wraps. | 215 // the stream it wraps. |
| 213 const size_t arbitraryOffset = 17; | 216 const size_t arbitraryOffset = 17; |
| 214 memStream.skip(arbitraryOffset); | 217 memStream->skip(arbitraryOffset); |
| 215 SkAutoTUnref<SkStream> bufferedStream(SkFrontBufferedStream::Create(&memStre
am, bufferSize)); | 218 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
| 216 | 219 |
| 217 // Since SkMemoryStream has a length and a position, bufferedStream must als
o. | 220 // Since SkMemoryStream has a length and a position, bufferedStream must als
o. |
| 218 REPORTER_ASSERT(reporter, bufferedStream->hasLength()); | 221 REPORTER_ASSERT(reporter, bufferedStream->hasLength()); |
| 219 | 222 |
| 220 const size_t amountToRead = 10; | 223 const size_t amountToRead = 10; |
| 221 const size_t bufferedLength = bufferedStream->getLength(); | 224 const size_t bufferedLength = bufferedStream->getLength(); |
| 222 size_t currentPosition = bufferedStream->getPosition(); | 225 size_t currentPosition = bufferedStream->getPosition(); |
| 223 REPORTER_ASSERT(reporter, 0 == currentPosition); | 226 REPORTER_ASSERT(reporter, 0 == currentPosition); |
| 224 | 227 |
| 225 // Read the stream in chunks. After each read, the position must match curre
ntPosition, | 228 // Read the stream in chunks. After each read, the position must match curre
ntPosition, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 279 |
| 277 bool readAfterEnd() const { | 280 bool readAfterEnd() const { |
| 278 return fReadAfterEnd; | 281 return fReadAfterEnd; |
| 279 } | 282 } |
| 280 private: | 283 private: |
| 281 bool fAtEnd; | 284 bool fAtEnd; |
| 282 bool fReadAfterEnd; | 285 bool fReadAfterEnd; |
| 283 }; | 286 }; |
| 284 | 287 |
| 285 DEF_TEST(ShortFrontBufferedStream, reporter) { | 288 DEF_TEST(ShortFrontBufferedStream, reporter) { |
| 286 FailingStream failingStream; | 289 FailingStream* failingStream = SkNEW(FailingStream); |
| 287 SkAutoTUnref<SkStreamRewindable> stream(SkFrontBufferedStream::Create(&faili
ngStream, 64)); | 290 SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(faili
ngStream, 64)); |
| 288 SkBitmap bm; | 291 SkBitmap bm; |
| 289 // The return value of DecodeStream is not important. We are just using Deco
deStream because | 292 // The return value of DecodeStream is not important. We are just using Deco
deStream because |
| 290 // it simulates a bug. DecodeStream will read the stream, then rewind, then
attempt to read | 293 // it simulates a bug. DecodeStream will read the stream, then rewind, then
attempt to read |
| 291 // again. FrontBufferedStream::read should not continue to read its underlyi
ng stream beyond | 294 // again. FrontBufferedStream::read should not continue to read its underlyi
ng stream beyond |
| 292 // its end. | 295 // its end. |
| 293 SkImageDecoder::DecodeStream(stream, &bm); | 296 SkImageDecoder::DecodeStream(stream, &bm); |
| 294 REPORTER_ASSERT(reporter, !failingStream.readAfterEnd()); | 297 REPORTER_ASSERT(reporter, !failingStream->readAfterEnd()); |
| 295 } | 298 } |
| OLD | NEW |