OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "android_webview/browser/input_stream.h" |
| 6 #include "android_webview/browser/net/input_stream_reader.h" |
| 7 #include "base/android/scoped_java_ref.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "net/base/io_buffer.h" |
| 12 #include "net/http/http_byte_range.h" |
| 13 |
| 14 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using android_webview::InputStream; |
| 18 using android_webview::InputStreamReader; |
| 19 using testing::DoAll; |
| 20 using testing::Ge; |
| 21 using testing::InSequence; |
| 22 using testing::Lt; |
| 23 using testing::Ne; |
| 24 using testing::NotNull; |
| 25 using testing::Return; |
| 26 using testing::SetArgPointee; |
| 27 using testing::Test; |
| 28 using testing::_; |
| 29 |
| 30 class MockInputStream : public InputStream { |
| 31 public: |
| 32 MockInputStream() {} |
| 33 virtual ~MockInputStream() {} |
| 34 |
| 35 MOCK_CONST_METHOD2(BytesAvailable, bool(JNIEnv* env, int* bytes_available)); |
| 36 |
| 37 MOCK_METHOD3(Skip, |
| 38 bool(JNIEnv* env, int64_t n, int64_t* bytes_skipped)); |
| 39 |
| 40 MOCK_METHOD4(Read, |
| 41 bool(JNIEnv* env, |
| 42 net::IOBuffer* dest, |
| 43 int length, |
| 44 int* bytes_read)); |
| 45 }; |
| 46 |
| 47 class InputStreamReaderTest : public Test { |
| 48 public: |
| 49 InputStreamReaderTest() |
| 50 : input_stream_reader_(new InputStreamReader(&input_stream_)) { |
| 51 } |
| 52 protected: |
| 53 int SeekRange(int first_byte, int last_byte) { |
| 54 net::HttpByteRange byte_range; |
| 55 byte_range.set_first_byte_position(first_byte); |
| 56 byte_range.set_last_byte_position(last_byte); |
| 57 return input_stream_reader_->Seek(byte_range); |
| 58 } |
| 59 |
| 60 int ReadRawData(scoped_refptr<net::IOBuffer> buffer, int bytesToRead) { |
| 61 return input_stream_reader_->ReadRawData(buffer.get(), bytesToRead); |
| 62 } |
| 63 |
| 64 MockInputStream input_stream_; |
| 65 scoped_refptr<InputStreamReader> input_stream_reader_; |
| 66 }; |
| 67 |
| 68 TEST_F(InputStreamReaderTest, BytesAvailableFailurePropagationOnSeek) { |
| 69 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull())) |
| 70 .WillOnce(Return(false)); |
| 71 |
| 72 ASSERT_GT(0, SeekRange(0, 0)); |
| 73 } |
| 74 |
| 75 TEST_F(InputStreamReaderTest, SkipFailurePropagationOnSeek) { |
| 76 const int streamSize = 10; |
| 77 const int bytesToSkip = 5; |
| 78 |
| 79 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull())) |
| 80 .WillOnce(DoAll(SetArgPointee<1>(streamSize), |
| 81 Return(true))); |
| 82 |
| 83 EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull())) |
| 84 .WillOnce(Return(false)); |
| 85 |
| 86 ASSERT_GT(0, SeekRange(bytesToSkip, streamSize - 1)); |
| 87 } |
| 88 |
| 89 TEST_F(InputStreamReaderTest, SeekToMiddle) { |
| 90 const int streamSize = 10; |
| 91 const int bytesToSkip = 5; |
| 92 |
| 93 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull())) |
| 94 .WillOnce(DoAll(SetArgPointee<1>(streamSize), |
| 95 Return(true))); |
| 96 |
| 97 EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull())) |
| 98 .WillOnce(DoAll(SetArgPointee<2>(bytesToSkip), |
| 99 Return(true))); |
| 100 |
| 101 ASSERT_EQ(bytesToSkip, SeekRange(bytesToSkip, streamSize - 1)); |
| 102 } |
| 103 |
| 104 TEST_F(InputStreamReaderTest, SeekToMiddleInSteps) { |
| 105 const int streamSize = 10; |
| 106 const int bytesToSkip = 5; |
| 107 |
| 108 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull())) |
| 109 .Times(1) |
| 110 .WillOnce(DoAll(SetArgPointee<1>(streamSize), |
| 111 Return(true))); |
| 112 |
| 113 EXPECT_CALL(input_stream_, Skip(_, _, _)) |
| 114 .Times(0); |
| 115 { |
| 116 InSequence s; |
| 117 EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull())) |
| 118 .WillOnce(DoAll(SetArgPointee<2>(bytesToSkip - 3), |
| 119 Return(true))) |
| 120 .RetiresOnSaturation(); |
| 121 EXPECT_CALL(input_stream_, Skip(_, 3, NotNull())) |
| 122 .WillOnce(DoAll(SetArgPointee<2>(1), |
| 123 Return(true))) |
| 124 .RetiresOnSaturation(); |
| 125 EXPECT_CALL(input_stream_, Skip(_, 2, NotNull())) |
| 126 .WillOnce(DoAll(SetArgPointee<2>(2), |
| 127 Return(true))) |
| 128 .RetiresOnSaturation(); |
| 129 } |
| 130 |
| 131 ASSERT_EQ(bytesToSkip, SeekRange(bytesToSkip, streamSize - 1)); |
| 132 } |
| 133 |
| 134 TEST_F(InputStreamReaderTest, SeekEmpty) { |
| 135 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull())) |
| 136 .WillOnce(DoAll(SetArgPointee<1>(0), |
| 137 Return(true))); |
| 138 |
| 139 ASSERT_EQ(0, SeekRange(0, 0)); |
| 140 } |
| 141 |
| 142 TEST_F(InputStreamReaderTest, SeekMoreThanAvailable) { |
| 143 const int bytesAvailable = 256; |
| 144 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull())) |
| 145 .WillOnce(DoAll(SetArgPointee<1>(bytesAvailable), |
| 146 Return(true))); |
| 147 |
| 148 ASSERT_GT(0, SeekRange(bytesAvailable, 2 * bytesAvailable)); |
| 149 } |
| 150 |
| 151 TEST_F(InputStreamReaderTest, ReadFailure) { |
| 152 const int bytesToRead = 128; |
| 153 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead); |
| 154 EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull())) |
| 155 .WillOnce(Return(false)); |
| 156 |
| 157 ASSERT_GT(0, ReadRawData(buffer, bytesToRead)); |
| 158 } |
| 159 |
| 160 TEST_F(InputStreamReaderTest, ReadNothing) { |
| 161 const int bytesToRead = 0; |
| 162 // Size of net::IOBuffer can't be 0. |
| 163 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(1); |
| 164 EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull())) |
| 165 .Times(0); |
| 166 |
| 167 ASSERT_EQ(0, ReadRawData(buffer, bytesToRead)); |
| 168 } |
| 169 |
| 170 TEST_F(InputStreamReaderTest, ReadSuccess) { |
| 171 const int bytesToRead = 128; |
| 172 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead); |
| 173 |
| 174 EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull())) |
| 175 .WillOnce(DoAll(SetArgPointee<3>(bytesToRead), |
| 176 Return(true))); |
| 177 |
| 178 ASSERT_EQ(bytesToRead, ReadRawData(buffer, bytesToRead)); |
| 179 } |
OLD | NEW |