Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: android_webview/native/input_stream_reader_unittest.cc

Issue 11363123: [android_webview] Don't block the IO thread when reading from an InputStream. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix clang error Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/native/input_stream.h"
6 #include "android_webview/native/input_stream_reader.h"
7 #include "base/android/scoped_java_ref.h"
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "net/base/io_buffer.h"
11 #include "net/http/http_byte_range.h"
12
13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using android_webview::InputStream;
17 using android_webview::InputStreamReader;
18 using testing::DoAll;
19 using testing::Ge;
20 using testing::InSequence;
21 using testing::Lt;
22 using testing::Ne;
23 using testing::NotNull;
24 using testing::Return;
25 using testing::SetArgPointee;
26 using testing::Test;
27 using testing::_;
28
29 class MockInputStream : public InputStream {
30 public:
31 MockInputStream() {}
32 virtual ~MockInputStream() {}
33
34 MOCK_CONST_METHOD2(BytesAvailable, bool(JNIEnv* env, int* bytes_available));
35
36 MOCK_METHOD3(Skip,
37 bool(JNIEnv* env, int64_t n, int64_t* bytes_skipped));
38
39 MOCK_METHOD4(Read,
40 bool(JNIEnv* env,
41 net::IOBuffer* dest,
42 int length,
43 int* bytes_read));
44 };
45
46 class CallbackReceiver {
47 public:
48 MOCK_METHOD1(OnSeekCompletion, void(int status));
49 MOCK_METHOD1(OnReadCompletion, void(int status));
50 };
51
52 class InputStreamReaderForTest : public InputStreamReader {
53 public:
54 InputStreamReaderForTest(InputStream* stream)
55 : InputStreamReader(stream) {
56 }
57
58 protected:
59 virtual void PostCompletionCallback(const net::CompletionCallback& callback,
60 int result) {
61 callback.Run(result);
62 }
63 };
64
65 class InputStreamReaderTest : public Test {
66 public:
67 InputStreamReaderTest()
68 : input_stream_reader_(&input_stream_) {
69 }
70 protected:
71 void SeekRange(int first_byte, int last_byte) {
72 net::HttpByteRange byte_range;
73 byte_range.set_first_byte_position(first_byte);
74 byte_range.set_last_byte_position(last_byte);
75 input_stream_reader_.Seek(
76 byte_range,
77 base::Bind(&CallbackReceiver::OnSeekCompletion,
78 base::Unretained(&callback_receiver_)));
79 }
80
81 void ReadRawData(scoped_refptr<net::IOBuffer> buffer, int bytesToRead) {
82 input_stream_reader_.ReadRawData(
83 buffer.get(),
84 bytesToRead,
85 base::Bind(&CallbackReceiver::OnReadCompletion,
86 base::Unretained(&callback_receiver_)));
87 }
88
89 MockInputStream input_stream_;
90 InputStreamReaderForTest input_stream_reader_;
91 CallbackReceiver callback_receiver_;
92 };
93
94 TEST_F(InputStreamReaderTest, BytesAvailableFailurePropagationOnSeek) {
95 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
96 .WillOnce(Return(false));
97
98 EXPECT_CALL(callback_receiver_, OnSeekCompletion(Lt(0)))
99 .Times(1);
100
101 SeekRange(0, 0);
102 }
103
104 TEST_F(InputStreamReaderTest, SkipFailurePropagationOnSeek) {
105 const int streamSize = 10;
106 const int bytesToSkip = 5;
107
108 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
109 .WillOnce(DoAll(SetArgPointee<1>(streamSize),
110 Return(true)));
111
112 EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull()))
113 .WillOnce(Return(false));
114
115 EXPECT_CALL(callback_receiver_, OnSeekCompletion(Lt(0)))
116 .Times(1);
117
118 SeekRange(bytesToSkip, streamSize - 1);
119 }
120
121 TEST_F(InputStreamReaderTest, SeekToMiddle) {
122 const int streamSize = 10;
123 const int bytesToSkip = 5;
124
125 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
126 .WillOnce(DoAll(SetArgPointee<1>(streamSize),
127 Return(true)));
128
129 EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull()))
130 .WillOnce(DoAll(SetArgPointee<2>(bytesToSkip),
131 Return(true)));
132
133 EXPECT_CALL(callback_receiver_, OnSeekCompletion(bytesToSkip))
134 .Times(1);
135
136 SeekRange(bytesToSkip, streamSize - 1);
137 }
138
139 TEST_F(InputStreamReaderTest, SeekToMiddleInSteps) {
140 const int streamSize = 10;
141 const int bytesToSkip = 5;
142
143 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
144 .Times(1)
145 .WillOnce(DoAll(SetArgPointee<1>(streamSize),
146 Return(true)));
147
148 EXPECT_CALL(input_stream_, Skip(_, _, _))
149 .Times(0);
150 {
151 InSequence s;
152 EXPECT_CALL(input_stream_, Skip(_, bytesToSkip, NotNull()))
153 .WillOnce(DoAll(SetArgPointee<2>(bytesToSkip - 3),
154 Return(true)))
155 .RetiresOnSaturation();
156 EXPECT_CALL(input_stream_, Skip(_, 3, NotNull()))
157 .WillOnce(DoAll(SetArgPointee<2>(1),
158 Return(true)))
159 .RetiresOnSaturation();
160 EXPECT_CALL(input_stream_, Skip(_, 2, NotNull()))
161 .WillOnce(DoAll(SetArgPointee<2>(2),
162 Return(true)))
163 .RetiresOnSaturation();
164 }
165
166 EXPECT_CALL(callback_receiver_, OnSeekCompletion(bytesToSkip))
167 .Times(1);
168
169 SeekRange(bytesToSkip, streamSize - 1);
170 }
171
172 TEST_F(InputStreamReaderTest, SeekEmpty) {
173 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
174 .WillOnce(DoAll(SetArgPointee<1>(0),
175 Return(true)));
176
177 EXPECT_CALL(callback_receiver_, OnSeekCompletion(0))
178 .Times(1);
179
180 SeekRange(0, 0);
181 }
182
183 TEST_F(InputStreamReaderTest, SeekMoreThanAvailable) {
184 const int bytesAvailable = 256;
185 EXPECT_CALL(input_stream_, BytesAvailable(_, NotNull()))
186 .WillOnce(DoAll(SetArgPointee<1>(bytesAvailable),
187 Return(true)));
188
189 EXPECT_CALL(callback_receiver_, OnSeekCompletion(Lt(0)))
190 .Times(1);
191
192 SeekRange(bytesAvailable, 2 * bytesAvailable);
193 }
194
195 TEST_F(InputStreamReaderTest, ReadFailure) {
196 const int bytesToRead = 128;
197 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead);
198 EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull()))
199 .WillOnce(Return(false));
200
201 EXPECT_CALL(callback_receiver_, OnReadCompletion(Lt(0)))
202 .Times(1);
203
204 ReadRawData(buffer, bytesToRead);
205 }
206
207 TEST_F(InputStreamReaderTest, ReadNothing) {
208 const int bytesToRead = 0;
209 // Size of net::IOBuffer can't be 0.
210 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(1);
211 EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull()))
212 .Times(0);
213
214 EXPECT_CALL(callback_receiver_, OnReadCompletion(0))
215 .Times(1);
216
217 ReadRawData(buffer, bytesToRead);
218 }
219
220 TEST_F(InputStreamReaderTest, ReadSuccess) {
221 const int bytesToRead = 128;
222 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(bytesToRead);
223
224 EXPECT_CALL(input_stream_, Read(_, buffer.get(), bytesToRead, NotNull()))
225 .WillOnce(DoAll(SetArgPointee<3>(bytesToRead),
226 Return(true)));
227
228 EXPECT_CALL(callback_receiver_, OnReadCompletion(bytesToRead))
229 .Times(1);
230
231 ReadRawData(buffer, bytesToRead);
232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698