| Index: android_webview/native/input_stream_reader_unittest.cc
|
| diff --git a/android_webview/native/input_stream_reader_unittest.cc b/android_webview/native/input_stream_reader_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..46e4623043d9cf7c86173aa279c991986e602ea8
|
| --- /dev/null
|
| +++ b/android_webview/native/input_stream_reader_unittest.cc
|
| @@ -0,0 +1,232 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "android_webview/native/input_stream.h"
|
| +#include "android_webview/native/input_stream_reader.h"
|
| +#include "base/android/scoped_java_ref.h"
|
| +#include "base/callback.h"
|
| +#include "base/memory/scoped_ptr.h"
|
| +#include "net/base/io_buffer.h"
|
| +#include "net/http/http_byte_range.h"
|
| +
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using android_webview::InputStream;
|
| +using android_webview::InputStreamReader;
|
| +using testing::DoAll;
|
| +using testing::Ge;
|
| +using testing::InSequence;
|
| +using testing::Lt;
|
| +using testing::Ne;
|
| +using testing::NotNull;
|
| +using testing::Return;
|
| +using testing::SetArgPointee;
|
| +using testing::Test;
|
| +using testing::_;
|
| +
|
| +class MockInputStream : public InputStream {
|
| + public:
|
| + MockInputStream() {}
|
| + virtual ~MockInputStream() {}
|
| +
|
| + MOCK_CONST_METHOD2(BytesAvailable, bool(JNIEnv* env, int* bytes_available));
|
| +
|
| + MOCK_METHOD3(Skip,
|
| + bool(JNIEnv* env, int64_t n, int64_t* bytes_skipped));
|
| +
|
| + MOCK_METHOD4(Read,
|
| + bool(JNIEnv* env,
|
| + net::IOBuffer* dest,
|
| + int length,
|
| + int* bytes_read));
|
| +};
|
| +
|
| +class CallbackReceiver {
|
| + public:
|
| + MOCK_METHOD1(OnSeekCompletion, void(int status));
|
| + MOCK_METHOD1(OnReadCompletion, void(int status));
|
| +};
|
| +
|
| +class InputStreamReaderForTest : public InputStreamReader {
|
| + public:
|
| + InputStreamReaderForTest(InputStream* stream)
|
| + : InputStreamReader(stream) {
|
| + }
|
| +
|
| + protected:
|
| + virtual void PostCompletionCallback(const net::CompletionCallback& callback,
|
| + int result) {
|
| + callback.Run(result);
|
| + }
|
| +};
|
| +
|
| +class InputStreamReaderTest : public Test {
|
| + public:
|
| + InputStreamReaderTest()
|
| + : input_stream_reader_(&input_stream_) {
|
| + }
|
| + protected:
|
| + void SeekRange(int first_byte, int last_byte) {
|
| + net::HttpByteRange byte_range;
|
| + byte_range.set_first_byte_position(first_byte);
|
| + byte_range.set_last_byte_position(last_byte);
|
| + input_stream_reader_.Seek(
|
| + byte_range,
|
| + base::Bind(&CallbackReceiver::OnSeekCompletion,
|
| + base::Unretained(&callback_receiver_)));
|
| + }
|
| +
|
| + void ReadRawData(scoped_refptr<net::IOBuffer> buffer, int bytesToRead) {
|
| + input_stream_reader_.ReadRawData(
|
| + buffer.get(),
|
| + bytesToRead,
|
| + base::Bind(&CallbackReceiver::OnReadCompletion,
|
| + base::Unretained(&callback_receiver_)));
|
| + }
|
| +
|
| + MockInputStream input_stream_;
|
| + InputStreamReaderForTest input_stream_reader_;
|
| + CallbackReceiver callback_receiver_;
|
| +};
|
| +
|
| +TEST_F(InputStreamReaderTest, BytesAvailableFailurePropagationOnSeek) {
|
| + EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
|
| + .WillOnce(Return(false));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnSeekCompletion(Lt(0)))
|
| + .Times(1);
|
| +
|
| + SeekRange(0, 0);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, SkipFailurePropagationOnSeek) {
|
| + const int streamSize = 10;
|
| + const int bytesToSkip = 5;
|
| +
|
| + EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<1>(streamSize),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull()))
|
| + .WillOnce(Return(false));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnSeekCompletion(Lt(0)))
|
| + .Times(1);
|
| +
|
| + SeekRange(bytesToSkip, streamSize - 1);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, SeekToMiddle) {
|
| + const int streamSize = 10;
|
| + const int bytesToSkip = 5;
|
| +
|
| + EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<1>(streamSize),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<2>(bytesToSkip),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnSeekCompletion(bytesToSkip))
|
| + .Times(1);
|
| +
|
| + SeekRange(bytesToSkip, streamSize - 1);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, SeekToMiddleInSteps) {
|
| + const int streamSize = 10;
|
| + const int bytesToSkip = 5;
|
| +
|
| + EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
|
| + .Times(1)
|
| + .WillOnce(DoAll(SetArgPointee<1>(streamSize),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(input_stream_, Skip(_, _, _))
|
| + .Times(0);
|
| + {
|
| + InSequence s;
|
| + EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<2>(bytesToSkip - 3),
|
| + Return(true)))
|
| + .RetiresOnSaturation();
|
| + EXPECT_CALL(input_stream_, Skip(_, 3, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<2>(1),
|
| + Return(true)))
|
| + .RetiresOnSaturation();
|
| + EXPECT_CALL(input_stream_, Skip(_, 2, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<2>(2),
|
| + Return(true)))
|
| + .RetiresOnSaturation();
|
| + }
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnSeekCompletion(bytesToSkip))
|
| + .Times(1);
|
| +
|
| + SeekRange(bytesToSkip, streamSize - 1);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, SeekEmpty) {
|
| + EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<1>(0),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnSeekCompletion(0))
|
| + .Times(1);
|
| +
|
| + SeekRange(0, 0);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, SeekMoreThanAvailable) {
|
| + const int bytesAvailable = 256;
|
| + EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<1>(bytesAvailable),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnSeekCompletion(Lt(0)))
|
| + .Times(1);
|
| +
|
| + SeekRange(bytesAvailable, 2 * bytesAvailable);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, ReadFailure) {
|
| + const int bytesToRead = 128;
|
| + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead);
|
| + EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull()))
|
| + .WillOnce(Return(false));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnReadCompletion(Lt(0)))
|
| + .Times(1);
|
| +
|
| + ReadRawData(buffer, bytesToRead);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, ReadNothing) {
|
| + const int bytesToRead = 0;
|
| + // Size of net::IOBuffer can't be 0.
|
| + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(1);
|
| + EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull()))
|
| + .Times(0);
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnReadCompletion(0))
|
| + .Times(1);
|
| +
|
| + ReadRawData(buffer, bytesToRead);
|
| +}
|
| +
|
| +TEST_F(InputStreamReaderTest, ReadSuccess) {
|
| + const int bytesToRead = 128;
|
| + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead);
|
| +
|
| + EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull()))
|
| + .WillOnce(DoAll(SetArgPointee<3>(bytesToRead),
|
| + Return(true)));
|
| +
|
| + EXPECT_CALL(callback_receiver_, OnReadCompletion(bytesToRead))
|
| + .Times(1);
|
| +
|
| + ReadRawData(buffer, bytesToRead);
|
| +}
|
|
|