2013-09-26 21:35:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2020-04-24 17:00:48 +00:00
|
|
|
// Make sure SkUserConfig.h is included so #defines are available on
|
|
|
|
// Android.
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#ifdef SK_ENABLE_ANDROID_UTILS
|
|
|
|
#include "client_utils/android/FrontBufferedStream.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/codec/SkCodec.h"
|
|
|
|
#include "include/core/SkBitmap.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkStream.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "tests/Test.h"
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
static void test_read(skiatest::Reporter* reporter, SkStream* bufferedStream,
|
|
|
|
const void* expectations, size_t bytesToRead) {
|
|
|
|
// output for reading bufferedStream.
|
|
|
|
SkAutoMalloc storage(bytesToRead);
|
|
|
|
|
2014-02-10 22:03:21 +00:00
|
|
|
const size_t bytesRead = bufferedStream->read(storage.get(), bytesToRead);
|
2013-09-26 21:35:39 +00:00
|
|
|
REPORTER_ASSERT(reporter, bytesRead == bytesToRead || bufferedStream->isAtEnd());
|
|
|
|
REPORTER_ASSERT(reporter, memcmp(storage.get(), expectations, bytesRead) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_rewind(skiatest::Reporter* reporter,
|
|
|
|
SkStream* bufferedStream, bool shouldSucceed) {
|
|
|
|
const bool success = bufferedStream->rewind();
|
|
|
|
REPORTER_ASSERT(reporter, success == shouldSucceed);
|
|
|
|
}
|
|
|
|
|
2014-02-10 22:03:21 +00:00
|
|
|
// Test that hasLength() returns the correct value, based on the stream
|
|
|
|
// being wrapped. A length can only be known if the wrapped stream has a
|
|
|
|
// length and it has a position (so its initial position can be taken into
|
|
|
|
// account when computing the length).
|
|
|
|
static void test_hasLength(skiatest::Reporter* reporter,
|
|
|
|
const SkStream& bufferedStream,
|
|
|
|
const SkStream& streamBeingBuffered) {
|
|
|
|
if (streamBeingBuffered.hasLength() && streamBeingBuffered.hasPosition()) {
|
|
|
|
REPORTER_ASSERT(reporter, bufferedStream.hasLength());
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, !bufferedStream.hasLength());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:35:39 +00:00
|
|
|
// All tests will buffer this string, and compare output to the original.
|
|
|
|
// The string is long to ensure that all of our lengths being tested are
|
|
|
|
// smaller than the string length.
|
|
|
|
const char gAbcs[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx";
|
|
|
|
|
|
|
|
// Tests reading the stream across boundaries of what has been buffered so far and what
|
|
|
|
// the total buffer size is.
|
|
|
|
static void test_incremental_buffering(skiatest::Reporter* reporter, size_t bufferSize) {
|
2015-01-21 20:09:53 +00:00
|
|
|
// NOTE: For this and other tests in this file, we cheat and continue to refer to the
|
|
|
|
// wrapped stream, but that's okay because we know the wrapping stream has not been
|
|
|
|
// deleted yet (and we only call const methods in it).
|
2017-09-16 01:39:47 +00:00
|
|
|
SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
|
2013-09-26 21:35:39 +00:00
|
|
|
|
2020-04-24 17:00:48 +00:00
|
|
|
auto bufferedStream = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(memStream), bufferSize);
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
test_hasLength(reporter, *bufferedStream, *memStream);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// First, test reading less than the max buffer size.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 2);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Now test rewinding back to the beginning and reading less than what was
|
|
|
|
// already buffered.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_rewind(reporter, bufferedStream.get(), true);
|
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Now test reading part of what was buffered, and buffering new data.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs + bufferSize / 4, bufferSize / 2);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Now test reading what was buffered, buffering new data, and
|
|
|
|
// reading directly from the stream.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_rewind(reporter, bufferedStream.get(), true);
|
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize << 1);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// We have reached the end of the buffer, so rewinding will fail.
|
|
|
|
// This test assumes that the stream is larger than the buffer; otherwise the
|
|
|
|
// result of rewind should be true.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_rewind(reporter, bufferedStream.get(), false);
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_perfectly_sized_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
|
2017-09-16 01:39:47 +00:00
|
|
|
SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
|
2020-04-24 17:00:48 +00:00
|
|
|
auto bufferedStream = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(memStream), bufferSize);
|
2016-11-03 18:40:50 +00:00
|
|
|
test_hasLength(reporter, *bufferedStream, *memStream);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Read exactly the amount that fits in the buffer.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Rewinding should succeed.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_rewind(reporter, bufferedStream.get(), true);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Once again reading buffered info should succeed
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Read past the size of the buffer. At this point, we cannot return.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), 1);
|
|
|
|
test_rewind(reporter, bufferedStream.get(), false);
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_skipping(skiatest::Reporter* reporter, size_t bufferSize) {
|
2017-09-16 01:39:47 +00:00
|
|
|
SkMemoryStream* memStream = SkMemoryStream::MakeDirect(gAbcs, strlen(gAbcs)).release();
|
2020-04-24 17:00:48 +00:00
|
|
|
auto bufferedStream = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(memStream), bufferSize);
|
2016-11-03 18:40:50 +00:00
|
|
|
test_hasLength(reporter, *bufferedStream, *memStream);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Skip half the buffer.
|
|
|
|
bufferedStream->skip(bufferSize / 2);
|
|
|
|
|
|
|
|
// Rewind, then read part of the buffer, which should have been read.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_rewind(reporter, bufferedStream.get(), true);
|
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize / 4);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Now skip beyond the buffered piece, but still within the total buffer.
|
|
|
|
bufferedStream->skip(bufferSize / 2);
|
|
|
|
|
|
|
|
// Test that reading will still work.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs + memStream->getPosition(), bufferSize / 4);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
test_rewind(reporter, bufferedStream.get(), true);
|
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A custom class whose isAtEnd behaves the way Android's stream does - since it is an adaptor to a
|
|
|
|
// Java InputStream, it does not know that it is at the end until it has attempted to read beyond
|
|
|
|
// the end and failed. Used by test_read_beyond_buffer.
|
|
|
|
class AndroidLikeMemoryStream : public SkMemoryStream {
|
|
|
|
public:
|
|
|
|
AndroidLikeMemoryStream(void* data, size_t size, bool ownMemory)
|
|
|
|
: INHERITED(data, size, ownMemory)
|
|
|
|
, fIsAtEnd(false) {}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t read(void* dst, size_t requested) override {
|
2013-09-26 21:35:39 +00:00
|
|
|
size_t bytesRead = this->INHERITED::read(dst, requested);
|
|
|
|
if (bytesRead < requested) {
|
|
|
|
fIsAtEnd = true;
|
|
|
|
}
|
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isAtEnd() const override {
|
2013-09-26 21:35:39 +00:00
|
|
|
return fIsAtEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fIsAtEnd;
|
|
|
|
typedef SkMemoryStream INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This test ensures that buffering the exact length of the stream and attempting to read beyond it
|
|
|
|
// does not invalidate the buffer.
|
|
|
|
static void test_read_beyond_buffer(skiatest::Reporter* reporter, size_t bufferSize) {
|
|
|
|
// Use a stream that behaves like Android's stream.
|
2015-08-26 20:07:48 +00:00
|
|
|
AndroidLikeMemoryStream* memStream =
|
|
|
|
new AndroidLikeMemoryStream((void*)gAbcs, bufferSize, false);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Create a buffer that matches the length of the stream.
|
2020-04-24 17:00:48 +00:00
|
|
|
auto bufferedStream = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(memStream), bufferSize);
|
2015-01-21 20:09:53 +00:00
|
|
|
test_hasLength(reporter, *bufferedStream.get(), *memStream);
|
2013-09-26 21:35:39 +00:00
|
|
|
|
|
|
|
// Attempt to read one more than the bufferSize
|
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize + 1);
|
|
|
|
test_rewind(reporter, bufferedStream.get(), true);
|
|
|
|
|
|
|
|
// Ensure that the initial read did not invalidate the buffer.
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs, bufferSize);
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 22:03:21 +00:00
|
|
|
// Dummy stream that optionally has a length and/or position. Tests that FrontBufferedStream's
|
|
|
|
// length depends on the stream it's buffering having a length and position.
|
|
|
|
class LengthOptionalStream : public SkStream {
|
|
|
|
public:
|
|
|
|
LengthOptionalStream(bool hasLength, bool hasPosition)
|
|
|
|
: fHasLength(hasLength)
|
|
|
|
, fHasPosition(hasPosition)
|
|
|
|
{}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool hasLength() const override {
|
2014-02-10 22:03:21 +00:00
|
|
|
return fHasLength;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool hasPosition() const override {
|
2014-02-10 22:03:21 +00:00
|
|
|
return fHasPosition;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t read(void*, size_t) override {
|
2014-02-10 22:03:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isAtEnd() const override {
|
2014-02-10 22:03:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const bool fHasLength;
|
|
|
|
const bool fHasPosition;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test all possible combinations of the wrapped stream having a length and a position.
|
|
|
|
static void test_length_combos(skiatest::Reporter* reporter, size_t bufferSize) {
|
|
|
|
for (int hasLen = 0; hasLen <= 1; hasLen++) {
|
|
|
|
for (int hasPos = 0; hasPos <= 1; hasPos++) {
|
2015-08-26 20:07:48 +00:00
|
|
|
LengthOptionalStream* stream =
|
|
|
|
new LengthOptionalStream(SkToBool(hasLen), SkToBool(hasPos));
|
2020-04-24 17:00:48 +00:00
|
|
|
auto buffered = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(stream), bufferSize);
|
2015-01-21 20:09:53 +00:00
|
|
|
test_hasLength(reporter, *buffered.get(), *stream);
|
2014-02-10 22:03:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test using a stream with an initial offset.
|
|
|
|
static void test_initial_offset(skiatest::Reporter* reporter, size_t bufferSize) {
|
2015-08-26 20:07:48 +00:00
|
|
|
SkMemoryStream* memStream = new SkMemoryStream(gAbcs, strlen(gAbcs), false);
|
2014-02-10 22:03:21 +00:00
|
|
|
|
|
|
|
// Skip a few characters into the memStream, so that bufferedStream represents an offset into
|
|
|
|
// the stream it wraps.
|
|
|
|
const size_t arbitraryOffset = 17;
|
2015-01-21 20:09:53 +00:00
|
|
|
memStream->skip(arbitraryOffset);
|
2020-04-24 17:00:48 +00:00
|
|
|
auto bufferedStream = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(memStream), bufferSize);
|
2014-02-10 22:03:21 +00:00
|
|
|
|
2016-02-18 13:59:25 +00:00
|
|
|
// Since SkMemoryStream has a length, bufferedStream must also.
|
2014-02-10 22:03:21 +00:00
|
|
|
REPORTER_ASSERT(reporter, bufferedStream->hasLength());
|
|
|
|
|
|
|
|
const size_t amountToRead = 10;
|
|
|
|
const size_t bufferedLength = bufferedStream->getLength();
|
2016-02-18 13:59:25 +00:00
|
|
|
size_t currentPosition = 0;
|
2014-02-10 22:03:21 +00:00
|
|
|
|
|
|
|
// Read the stream in chunks. After each read, the position must match currentPosition,
|
|
|
|
// which sums the amount attempted to read, unless the end of the stream has been reached.
|
|
|
|
// Importantly, the end should not have been reached until currentPosition == bufferedLength.
|
|
|
|
while (currentPosition < bufferedLength) {
|
|
|
|
REPORTER_ASSERT(reporter, !bufferedStream->isAtEnd());
|
2016-11-03 18:40:50 +00:00
|
|
|
test_read(reporter, bufferedStream.get(), gAbcs + arbitraryOffset + currentPosition,
|
2014-02-10 22:03:21 +00:00
|
|
|
amountToRead);
|
2020-02-07 15:36:46 +00:00
|
|
|
currentPosition = std::min(currentPosition + amountToRead, bufferedLength);
|
2016-02-18 13:59:25 +00:00
|
|
|
REPORTER_ASSERT(reporter, memStream->getPosition() - arbitraryOffset == currentPosition);
|
2014-02-10 22:03:21 +00:00
|
|
|
}
|
|
|
|
REPORTER_ASSERT(reporter, bufferedStream->isAtEnd());
|
|
|
|
REPORTER_ASSERT(reporter, bufferedLength == currentPosition);
|
|
|
|
}
|
|
|
|
|
2013-09-26 21:35:39 +00:00
|
|
|
static void test_buffers(skiatest::Reporter* reporter, size_t bufferSize) {
|
|
|
|
test_incremental_buffering(reporter, bufferSize);
|
|
|
|
test_perfectly_sized_buffer(reporter, bufferSize);
|
|
|
|
test_skipping(reporter, bufferSize);
|
|
|
|
test_read_beyond_buffer(reporter, bufferSize);
|
2014-02-10 22:03:21 +00:00
|
|
|
test_length_combos(reporter, bufferSize);
|
|
|
|
test_initial_offset(reporter, bufferSize);
|
2013-09-26 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(FrontBufferedStream, reporter) {
|
2013-09-26 21:35:39 +00:00
|
|
|
// Test 6 and 64, which are used by Android, as well as another arbitrary length.
|
|
|
|
test_buffers(reporter, 6);
|
|
|
|
test_buffers(reporter, 15);
|
|
|
|
test_buffers(reporter, 64);
|
|
|
|
}
|
2014-10-24 13:55:07 +00:00
|
|
|
|
|
|
|
// Test that a FrontBufferedStream does not allow reading after the end of a stream.
|
|
|
|
// This class is a dummy SkStream which reports that it is at the end on the first
|
|
|
|
// read (simulating a failure). Then it tracks whether someone calls read() again.
|
|
|
|
class FailingStream : public SkStream {
|
|
|
|
public:
|
|
|
|
FailingStream()
|
|
|
|
: fAtEnd(false)
|
|
|
|
{}
|
2016-03-01 21:29:15 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
size_t read(void* buffer, size_t size) override {
|
2016-03-01 21:29:15 +00:00
|
|
|
SkASSERT(!fAtEnd);
|
|
|
|
fAtEnd = true;
|
2014-10-24 13:55:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
bool isAtEnd() const override {
|
2014-10-24 13:55:07 +00:00
|
|
|
return fAtEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool fAtEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
DEF_TEST(ShortFrontBufferedStream, reporter) {
|
2015-08-26 20:07:48 +00:00
|
|
|
FailingStream* failingStream = new FailingStream;
|
2020-04-24 17:00:48 +00:00
|
|
|
auto stream = android::skia::FrontBufferedStream::Make(
|
|
|
|
std::unique_ptr<SkStream>(failingStream), 64);
|
2016-03-01 20:12:27 +00:00
|
|
|
|
|
|
|
// This will fail to create a codec. However, what we really want to test is that we
|
|
|
|
// won't read past the end of the stream.
|
2017-07-23 19:30:02 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(std::move(stream)));
|
2014-10-24 13:55:07 +00:00
|
|
|
}
|
2020-04-24 17:00:48 +00:00
|
|
|
#endif // SK_ENABLE_ANDROID_UTILS
|