Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / android_webview / browser / net / input_stream_reader_unittest.cc
blob7d6a52ee58878b8186f7056d72397092f51ea341
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.
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"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
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::_;
30 class MockInputStream : public InputStream {
31 public:
32 MockInputStream() {}
33 virtual ~MockInputStream() {}
35 MOCK_CONST_METHOD1(BytesAvailable, bool(int* bytes_available));
36 MOCK_METHOD2(Skip, bool(int64_t n, int64_t* bytes_skipped));
37 MOCK_METHOD3(Read, bool(net::IOBuffer* dest, int length, int* bytes_read));
40 class InputStreamReaderTest : public Test {
41 public:
42 InputStreamReaderTest()
43 : input_stream_reader_(&input_stream_) {
45 protected:
46 int SeekRange(int first_byte, int last_byte) {
47 net::HttpByteRange byte_range;
48 byte_range.set_first_byte_position(first_byte);
49 byte_range.set_last_byte_position(last_byte);
50 return input_stream_reader_.Seek(byte_range);
53 int ReadRawData(scoped_refptr<net::IOBuffer> buffer, int bytesToRead) {
54 return input_stream_reader_.ReadRawData(buffer.get(), bytesToRead);
57 MockInputStream input_stream_;
58 InputStreamReader input_stream_reader_;
61 TEST_F(InputStreamReaderTest, BytesAvailableFailurePropagationOnSeek) {
62 EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
63 .WillOnce(Return(false));
65 ASSERT_GT(0, SeekRange(0, 0));
68 TEST_F(InputStreamReaderTest, SkipFailurePropagationOnSeek) {
69 const int streamSize = 10;
70 const int bytesToSkip = 5;
72 EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
73 .WillOnce(DoAll(SetArgPointee<0>(streamSize),
74 Return(true)));
76 EXPECT_CALL(input_stream_, Skip(bytesToSkip, NotNull()))
77 .WillOnce(Return(false));
79 ASSERT_GT(0, SeekRange(bytesToSkip, streamSize - 1));
82 TEST_F(InputStreamReaderTest, SeekToMiddle) {
83 const int streamSize = 10;
84 const int bytesToSkip = 5;
86 EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
87 .WillOnce(DoAll(SetArgPointee<0>(streamSize),
88 Return(true)));
90 EXPECT_CALL(input_stream_, Skip(bytesToSkip, NotNull()))
91 .WillOnce(DoAll(SetArgPointee<1>(bytesToSkip),
92 Return(true)));
94 ASSERT_EQ(bytesToSkip, SeekRange(bytesToSkip, streamSize - 1));
97 TEST_F(InputStreamReaderTest, SeekToMiddleInSteps) {
98 const int streamSize = 10;
99 const int bytesToSkip = 5;
101 EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
102 .Times(1)
103 .WillOnce(DoAll(SetArgPointee<0>(streamSize),
104 Return(true)));
106 EXPECT_CALL(input_stream_, Skip(_, _))
107 .Times(0);
109 InSequence s;
110 EXPECT_CALL(input_stream_, Skip(bytesToSkip, NotNull()))
111 .WillOnce(DoAll(SetArgPointee<1>(bytesToSkip - 3),
112 Return(true)))
113 .RetiresOnSaturation();
114 EXPECT_CALL(input_stream_, Skip(3, NotNull()))
115 .WillOnce(DoAll(SetArgPointee<1>(1),
116 Return(true)))
117 .RetiresOnSaturation();
118 EXPECT_CALL(input_stream_, Skip(2, NotNull()))
119 .WillOnce(DoAll(SetArgPointee<1>(2),
120 Return(true)))
121 .RetiresOnSaturation();
124 ASSERT_EQ(bytesToSkip, SeekRange(bytesToSkip, streamSize - 1));
127 TEST_F(InputStreamReaderTest, SeekEmpty) {
128 EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
129 .WillOnce(DoAll(SetArgPointee<0>(0),
130 Return(true)));
132 ASSERT_EQ(0, SeekRange(0, 0));
135 TEST_F(InputStreamReaderTest, SeekMoreThanAvailable) {
136 const int bytesAvailable = 256;
137 EXPECT_CALL(input_stream_, BytesAvailable(NotNull()))
138 .WillOnce(DoAll(SetArgPointee<0>(bytesAvailable),
139 Return(true)));
141 ASSERT_GT(0, SeekRange(bytesAvailable, 2 * bytesAvailable));
144 TEST_F(InputStreamReaderTest, ReadFailure) {
145 const int bytesToRead = 128;
146 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead);
147 EXPECT_CALL(input_stream_, Read(buffer.get(), bytesToRead, NotNull()))
148 .WillOnce(Return(false));
150 ASSERT_GT(0, ReadRawData(buffer, bytesToRead));
153 TEST_F(InputStreamReaderTest, ReadNothing) {
154 const int bytesToRead = 0;
155 // Size of net::IOBuffer can't be 0.
156 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(1);
157 EXPECT_CALL(input_stream_, Read(buffer.get(), bytesToRead, NotNull()))
158 .Times(0);
160 ASSERT_EQ(0, ReadRawData(buffer, bytesToRead));
163 TEST_F(InputStreamReaderTest, ReadSuccess) {
164 const int bytesToRead = 128;
165 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead);
167 EXPECT_CALL(input_stream_, Read(buffer.get(), bytesToRead, NotNull()))
168 .WillOnce(DoAll(SetArgPointee<2>(bytesToRead),
169 Return(true)));
171 ASSERT_EQ(bytesToRead, ReadRawData(buffer, bytesToRead));