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