2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 // Make sure SkUserConfig.h is included so #defines are available on
10 #include "include/core/SkTypes.h"
11 #ifdef SK_ENABLE_ANDROID_UTILS
12 #include "client_utils/android/FrontBufferedStream.h"
13 #include "include/codec/SkCodec.h"
14 #include "include/core/SkBitmap.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkStream.h"
17 #include "src/core/SkAutoMalloc.h"
18 #include "tests/Test.h"
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);
25 const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead);
26 REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd());
27 REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0);
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);
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());
46 REPORTER_ASSERT(reporter, !bufferedStream.hasLength());
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";
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) {
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).
61 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
63 auto bufferedStream = android::skia::FrontBufferedStream::Make(
64 std::unique_ptr<SkStream>(memStream), bufferSize);
66 test_hasLength(reporter, *bufferedStream, *memStream);
68 // First, test reading less than the max buffer size.
69 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2);
71 // Now test rewinding back to the beginning and reading less than what was
73 test_rewind(reporter, bufferedStream.get(), true);
74 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
76 // Now test reading part of what was buffered, and buffering new data.
77 test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2);
79 // Now test reading what was buffered, buffering new data, and
80 // reading directly from the stream.
81 test_rewind(reporter, bufferedStream.get(), true);
82 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1);
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.
87 test_rewind(reporter, bufferedStream.get(), false);
90 static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
91 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
92 auto bufferedStream = android::skia::FrontBufferedStream::Make(
93 std::unique_ptr<SkStream>(memStream), bufferSize);
94 test_hasLength(reporter, *bufferedStream, *memStream);
96 // Read exactly the amount that fits in the buffer.
97 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
99 // Rewinding should succeed.
100 test_rewind(reporter, bufferedStream.get(), true);
102 // Once again reading buffered info should succeed
103 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
105 // Read past the size of the buffer. At this point, we cannot return.
106 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1);
107 test_rewind(reporter, bufferedStream.get(), false);
110 static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
111 SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
112 auto bufferedStream = android::skia::FrontBufferedStream::Make(
113 std::unique_ptr<SkStream>(memStream), bufferSize);
114 test_hasLength(reporter, *bufferedStream, *memStream);
116 // Skip half the buffer.
117 bufferedStream->skip(bufferSize / 2);
119 // Rewind, then read part of the buffer, which should have been read.
120 test_rewind(reporter, bufferedStream.get(), true);
121 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
123 // Now skip beyond the buffered piece, but still within the total buffer.
124 bufferedStream->skip(bufferSize / 2);
126 // Test that reading will still work.
127 test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4);
129 test_rewind(reporter, bufferedStream.get(), true);
130 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
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 {
138 AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
139 : INHERITED(data, size, ownMemory)
142 size_t read(void* dst, size_t requested) override {
143 size_t bytesRead = this->INHERITED::read(dst, requested);
144 if (bytesRead < requested) {
150 bool isAtEnd() const override {
156 using INHERITED = SkMemoryStream;
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.
163 AndroidLikeMemoryStream* memStream =
164 new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false);
166 // Create a buffer that matches the length of the stream.
167 auto bufferedStream = android::skia::FrontBufferedStream::Make(
168 std::unique_ptr<SkStream>(memStream), bufferSize);
169 test_hasLength(reporter, *bufferedStream, *memStream);
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);
175 // Ensure that the initial read did not invalidate the buffer.
176 test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
179 // Mock stream that optionally has a length and/or position. Tests that FrontBufferedStream's
180 // length depends on the stream it's buffering having a length and position.
181 class LengthOptionalStream : public SkStream {
183 LengthOptionalStream(bool hasLength, bool hasPosition)
184 : fHasLength(hasLength)
185 , fHasPosition(hasPosition)
188 bool hasLength() const override {
192 bool hasPosition() const override {
196 size_t read(void*, size_t) override {
200 bool isAtEnd() const override {
205 const bool fHasLength;
206 const bool fHasPosition;
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++) {
213 LengthOptionalStream* stream =
214 new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos));
215 auto buffered = android::skia::FrontBufferedStream::Make(
216 std::unique_ptr<SkStream>(stream), bufferSize);
217 test_hasLength(reporter, *buffered, *stream);
222 // Test using a stream with an initial offset.
223 static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
224 SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
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;
229 memStream->skip(arbitraryOffset);
230 auto bufferedStream = android::skia::FrontBufferedStream::Make(
231 std::unique_ptr<SkStream>(memStream), bufferSize);
233 // Since SkMemoryStream has a length, bufferedStream must also.
234 REPORTER_ASSERT(reporter, bufferedStream->hasLength());
236 const size_t amountToRead = 10;
237 const size_t bufferedLength = bufferedStream->getLength();
238 size_t currentPosition = 0;
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());
245 test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition,
247 currentPosition = std::min(currentPosition + amountToRead, bufferedLength);
248 REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
250 REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
251 REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
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);
259 test_length_combos(reporter, bufferSize);
260 test_initial_offset(reporter, bufferSize);
263 DEF_TEST(FrontBufferedStream, reporter) {
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);
270 // Test that a FrontBufferedStream does not allow reading after the end of a stream.
271 // This class is a mock SkStream which reports that it is at the end on the first
272 // read (simulating a failure). Then it tracks whether someone calls read() again.
273 class FailingStream : public SkStream {
279 size_t read(void* buffer, size_t size) override {
285 bool isAtEnd() const override {
293 DEF_TEST(ShortFrontBufferedStream, reporter) {
294 FailingStream* failingStream = new FailingStream;
295 auto stream = android::skia::FrontBufferedStream::Make(
296 std::unique_ptr<SkStream>(failingStream), 64);
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.
300 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
302 #endif // SK_ENABLE_ANDROID_UTILS