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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 // NOTE: For this and other tests in this file, we cheat and continue to ref
er to the | 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 | 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). | 56 // deleted yet (and we only call const methods in it). |
57 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); | 57 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
58 | 58 |
59 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); | 59 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
60 test_hasLength(reporter, *bufferedStream.get(), *memStream); | 60 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
61 | 61 |
62 // First, test reading less than the max buffer size. | 62 // First, test reading less than the max buffer size. |
63 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); | 63 test_read(reporter, bufferedStream, gAbcs, bufferSize / 2); |
64 | 64 |
65 // 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 |
66 // already buffered. | 66 // already buffered. |
67 test_rewind(reporter, bufferedStream, true); | 67 test_rewind(reporter, bufferedStream, true); |
68 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); | 68 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
69 | 69 |
70 // 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. |
71 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), b
ufferSize / 2); | 71 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), b
ufferSize / 2); |
72 | 72 |
73 // Now test reading what was buffered, buffering new data, and | 73 // Now test reading what was buffered, buffering new data, and |
74 // reading directly from the stream. | 74 // reading directly from the stream. |
75 test_rewind(reporter, bufferedStream, true); | 75 test_rewind(reporter, bufferedStream, true); |
76 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); | 76 test_read(reporter, bufferedStream, gAbcs, bufferSize << 1); |
77 | 77 |
78 // 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. |
79 // 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 |
80 // result of rewind should be true. | 80 // result of rewind should be true. |
81 test_rewind(reporter, bufferedStream, false); | 81 test_rewind(reporter, bufferedStream, false); |
82 } | 82 } |
83 | 83 |
84 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) { |
85 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); | 85 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
86 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); | 86 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
87 test_hasLength(reporter, *bufferedStream.get(), *memStream); | 87 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
88 | 88 |
89 // Read exactly the amount that fits in the buffer. | 89 // Read exactly the amount that fits in the buffer. |
90 test_read(reporter, bufferedStream, gAbcs, bufferSize); | 90 test_read(reporter, bufferedStream, gAbcs, bufferSize); |
91 | 91 |
92 // Rewinding should succeed. | 92 // Rewinding should succeed. |
93 test_rewind(reporter, bufferedStream, true); | 93 test_rewind(reporter, bufferedStream, true); |
94 | 94 |
95 // Once again reading buffered info should succeed | 95 // Once again reading buffered info should succeed |
96 test_read(reporter, bufferedStream, gAbcs, bufferSize); | 96 test_read(reporter, bufferedStream, gAbcs, bufferSize); |
97 | 97 |
98 // 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. |
99 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1
); | 99 test_read(reporter, bufferedStream, gAbcs + bufferedStream->getPosition(), 1
); |
100 test_rewind(reporter, bufferedStream, false); | 100 test_rewind(reporter, bufferedStream, false); |
101 } | 101 } |
102 | 102 |
103 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { | 103 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) { |
104 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); | 104 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
105 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); | 105 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
106 test_hasLength(reporter, *bufferedStream.get(), *memStream); | 106 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
107 | 107 |
108 // Skip half the buffer. | 108 // Skip half the buffer. |
109 bufferedStream->skip(bufferSize / 2); | 109 bufferedStream->skip(bufferSize / 2); |
110 | 110 |
111 // 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. |
112 test_rewind(reporter, bufferedStream, true); | 112 test_rewind(reporter, bufferedStream, true); |
113 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); | 113 test_read(reporter, bufferedStream, gAbcs, bufferSize / 4); |
114 | 114 |
(...skipping 30 matching lines...) Expand all Loading... |
145 | 145 |
146 private: | 146 private: |
147 bool fIsAtEnd; | 147 bool fIsAtEnd; |
148 typedef SkMemoryStream INHERITED; | 148 typedef SkMemoryStream INHERITED; |
149 }; | 149 }; |
150 | 150 |
151 // 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 |
152 // does not invalidate the buffer. | 152 // does not invalidate the buffer. |
153 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) { |
154 // Use a stream that behaves like Android's stream. | 154 // Use a stream that behaves like Android's stream. |
155 AndroidLikeMemoryStream* memStream = SkNEW_ARGS(AndroidLikeMemoryStream, ((v
oid*)gAbcs, bufferSize, false)); | 155 AndroidLikeMemoryStream* memStream = |
| 156 new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false); |
156 | 157 |
157 // Create a buffer that matches the length of the stream. | 158 // Create a buffer that matches the length of the stream. |
158 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); | 159 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
159 test_hasLength(reporter, *bufferedStream.get(), *memStream); | 160 test_hasLength(reporter, *bufferedStream.get(), *memStream); |
160 | 161 |
161 // Attempt to read one more than the bufferSize | 162 // Attempt to read one more than the bufferSize |
162 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); | 163 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1); |
163 test_rewind(reporter, bufferedStream.get(), true); | 164 test_rewind(reporter, bufferedStream.get(), true); |
164 | 165 |
165 // Ensure that the initial read did not invalidate the buffer. | 166 // Ensure that the initial read did not invalidate the buffer. |
(...skipping 27 matching lines...) Expand all Loading... |
193 | 194 |
194 private: | 195 private: |
195 const bool fHasLength; | 196 const bool fHasLength; |
196 const bool fHasPosition; | 197 const bool fHasPosition; |
197 }; | 198 }; |
198 | 199 |
199 // Test all possible combinations of the wrapped stream having a length and a po
sition. | 200 // Test all possible combinations of the wrapped stream having a length and a po
sition. |
200 static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize)
{ | 201 static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize)
{ |
201 for (int hasLen = 0; hasLen <= 1; hasLen++) { | 202 for (int hasLen = 0; hasLen <= 1; hasLen++) { |
202 for (int hasPos = 0; hasPos <= 1; hasPos++) { | 203 for (int hasPos = 0; hasPos <= 1; hasPos++) { |
203 LengthOptionalStream* stream = SkNEW_ARGS(LengthOptionalStream, (SkT
oBool(hasLen), SkToBool(hasPos))); | 204 LengthOptionalStream* stream = |
| 205 new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos))
; |
204 SkAutoTDelete<SkStream> buffered(SkFrontBufferedStream::Create(strea
m, bufferSize)); | 206 SkAutoTDelete<SkStream> buffered(SkFrontBufferedStream::Create(strea
m, bufferSize)); |
205 test_hasLength(reporter, *buffered.get(), *stream); | 207 test_hasLength(reporter, *buffered.get(), *stream); |
206 } | 208 } |
207 } | 209 } |
208 } | 210 } |
209 | 211 |
210 // Test using a stream with an initial offset. | 212 // Test using a stream with an initial offset. |
211 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize)
{ | 213 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize)
{ |
212 SkMemoryStream* memStream = SkNEW_ARGS(SkMemoryStream, (gAbcs, strlen(gAbcs)
, false)); | 214 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false); |
213 | 215 |
214 // Skip a few characters into the memStream, so that bufferedStream represen
ts an offset into | 216 // Skip a few characters into the memStream, so that bufferedStream represen
ts an offset into |
215 // the stream it wraps. | 217 // the stream it wraps. |
216 const size_t arbitraryOffset = 17; | 218 const size_t arbitraryOffset = 17; |
217 memStream->skip(arbitraryOffset); | 219 memStream->skip(arbitraryOffset); |
218 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); | 220 SkAutoTDelete<SkStream> bufferedStream(SkFrontBufferedStream::Create(memStre
am, bufferSize)); |
219 | 221 |
220 // Since SkMemoryStream has a length and a position, bufferedStream must als
o. | 222 // Since SkMemoryStream has a length and a position, bufferedStream must als
o. |
221 REPORTER_ASSERT(reporter, bufferedStream->hasLength()); | 223 REPORTER_ASSERT(reporter, bufferedStream->hasLength()); |
222 | 224 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 | 281 |
280 bool readAfterEnd() const { | 282 bool readAfterEnd() const { |
281 return fReadAfterEnd; | 283 return fReadAfterEnd; |
282 } | 284 } |
283 private: | 285 private: |
284 bool fAtEnd; | 286 bool fAtEnd; |
285 bool fReadAfterEnd; | 287 bool fReadAfterEnd; |
286 }; | 288 }; |
287 | 289 |
288 DEF_TEST(ShortFrontBufferedStream, reporter) { | 290 DEF_TEST(ShortFrontBufferedStream, reporter) { |
289 FailingStream* failingStream = SkNEW(FailingStream); | 291 FailingStream* failingStream = new FailingStream; |
290 SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(faili
ngStream, 64)); | 292 SkAutoTDelete<SkStreamRewindable> stream(SkFrontBufferedStream::Create(faili
ngStream, 64)); |
291 SkBitmap bm; | 293 SkBitmap bm; |
292 // The return value of DecodeStream is not important. We are just using Deco
deStream because | 294 // The return value of DecodeStream is not important. We are just using Deco
deStream because |
293 // it simulates a bug. DecodeStream will read the stream, then rewind, then
attempt to read | 295 // it simulates a bug. DecodeStream will read the stream, then rewind, then
attempt to read |
294 // again. FrontBufferedStream::read should not continue to read its underlyi
ng stream beyond | 296 // again. FrontBufferedStream::read should not continue to read its underlyi
ng stream beyond |
295 // its end. | 297 // its end. |
296 SkImageDecoder::DecodeStream(stream, &bm); | 298 SkImageDecoder::DecodeStream(stream, &bm); |
297 REPORTER_ASSERT(reporter, !failingStream->readAfterEnd()); | 299 REPORTER_ASSERT(reporter, !failingStream->readAfterEnd()); |
298 } | 300 } |
OLD | NEW |