OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" | 5 #include "chrome/browser/chromeos/drive/drive_file_stream_reader.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 class NetworkReaderProxyTest : public ::testing::Test { | 103 class NetworkReaderProxyTest : public ::testing::Test { |
104 protected: | 104 protected: |
105 NetworkReaderProxyTest() | 105 NetworkReaderProxyTest() |
106 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { | 106 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
107 } | 107 } |
108 | 108 |
109 content::TestBrowserThreadBundle thread_bundle_; | 109 content::TestBrowserThreadBundle thread_bundle_; |
110 }; | 110 }; |
111 | 111 |
112 TEST_F(NetworkReaderProxyTest, EmptyFile) { | 112 TEST_F(NetworkReaderProxyTest, EmptyFile) { |
113 NetworkReaderProxy proxy(0, 0, base::Bind(&base::DoNothing)); | 113 NetworkReaderProxy proxy(0, 0, 0, base::Bind(&base::DoNothing)); |
114 | 114 |
115 net::TestCompletionCallback callback; | 115 net::TestCompletionCallback callback; |
116 const int kBufferSize = 10; | 116 const int kBufferSize = 10; |
117 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 117 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
118 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 118 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
119 | 119 |
120 // For empty file, Read() should return 0 immediately. | 120 // For empty file, Read() should return 0 immediately. |
121 EXPECT_EQ(0, result); | 121 EXPECT_EQ(0, result); |
122 } | 122 } |
123 | 123 |
124 TEST_F(NetworkReaderProxyTest, Read) { | 124 TEST_F(NetworkReaderProxyTest, Read) { |
125 NetworkReaderProxy proxy(0, 10, base::Bind(&base::DoNothing)); | 125 int cancel_called = 0; |
| 126 { |
| 127 NetworkReaderProxy proxy(0, 10, 10, |
| 128 base::Bind(&IncrementCallback, &cancel_called)); |
| 129 |
| 130 net::TestCompletionCallback callback; |
| 131 const int kBufferSize = 3; |
| 132 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
| 133 |
| 134 // If no data is available yet, ERR_IO_PENDING should be returned. |
| 135 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 136 EXPECT_EQ(net::ERR_IO_PENDING, result); |
| 137 |
| 138 // And when the data is supplied, the callback will be called. |
| 139 scoped_ptr<std::string> data(new std::string("abcde")); |
| 140 proxy.OnGetContent(data.Pass()); |
| 141 |
| 142 // The returned data should be fit to the buffer size. |
| 143 result = callback.GetResult(result); |
| 144 EXPECT_EQ(3, result); |
| 145 EXPECT_EQ("abc", std::string(buffer->data(), result)); |
| 146 |
| 147 // The next Read should return immediately because there is pending data |
| 148 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 149 EXPECT_EQ(2, result); |
| 150 EXPECT_EQ("de", std::string(buffer->data(), result)); |
| 151 |
| 152 // Supply the data before calling Read operation. |
| 153 data.reset(new std::string("fg")); |
| 154 proxy.OnGetContent(data.Pass()); |
| 155 data.reset(new std::string("hij")); |
| 156 proxy.OnGetContent(data.Pass()); // Now 10 bytes are supplied. |
| 157 |
| 158 // The data should be concatenated if possible. |
| 159 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 160 EXPECT_EQ(3, result); |
| 161 EXPECT_EQ("fgh", std::string(buffer->data(), result)); |
| 162 |
| 163 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 164 EXPECT_EQ(2, result); |
| 165 EXPECT_EQ("ij", std::string(buffer->data(), result)); |
| 166 |
| 167 // The whole data is read, so Read() should return 0 immediately by then. |
| 168 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
| 169 EXPECT_EQ(0, result); |
| 170 } |
| 171 |
| 172 // Proxy is deleted without any called to OnCompleted(). Even in the case, |
| 173 // cancel callback should not be invoked. |
| 174 EXPECT_EQ(0, cancel_called); |
| 175 } |
| 176 |
| 177 TEST_F(NetworkReaderProxyTest, ReadWithLimit) { |
| 178 NetworkReaderProxy proxy(10, 10, 10, base::Bind(&base::DoNothing)); |
126 | 179 |
127 net::TestCompletionCallback callback; | 180 net::TestCompletionCallback callback; |
128 const int kBufferSize = 3; | 181 const int kBufferSize = 3; |
129 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | |
130 | |
131 // If no data is available yet, ERR_IO_PENDING should be returned. | |
132 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | |
133 EXPECT_EQ(net::ERR_IO_PENDING, result); | |
134 | |
135 // And when the data is supplied, the callback will be called. | |
136 scoped_ptr<std::string> data(new std::string("abcde")); | |
137 proxy.OnGetContent(data.Pass()); | |
138 | |
139 // The returned data should be fit to the buffer size. | |
140 result = callback.GetResult(result); | |
141 EXPECT_EQ(3, result); | |
142 EXPECT_EQ("abc", std::string(buffer->data(), result)); | |
143 | |
144 // The next Read should return immediately because there is pending data | |
145 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | |
146 EXPECT_EQ(2, result); | |
147 EXPECT_EQ("de", std::string(buffer->data(), result)); | |
148 | |
149 // Supply the data before calling Read operation. | |
150 data.reset(new std::string("fg")); | |
151 proxy.OnGetContent(data.Pass()); | |
152 data.reset(new std::string("hij")); | |
153 proxy.OnGetContent(data.Pass()); // Now 10 bytes are supplied. | |
154 | |
155 // The data should be concatenated if possible. | |
156 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | |
157 EXPECT_EQ(3, result); | |
158 EXPECT_EQ("fgh", std::string(buffer->data(), result)); | |
159 | |
160 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | |
161 EXPECT_EQ(2, result); | |
162 EXPECT_EQ("ij", std::string(buffer->data(), result)); | |
163 | |
164 // The whole data is read, so Read() should return 0 immediately by then. | |
165 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | |
166 EXPECT_EQ(0, result); | |
167 } | |
168 | |
169 TEST_F(NetworkReaderProxyTest, ReadWithLimit) { | |
170 NetworkReaderProxy proxy(10, 10, base::Bind(&base::DoNothing)); | |
171 | |
172 net::TestCompletionCallback callback; | |
173 const int kBufferSize = 3; | |
174 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 182 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
175 | 183 |
176 // If no data is available yet, ERR_IO_PENDING should be returned. | 184 // If no data is available yet, ERR_IO_PENDING should be returned. |
177 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 185 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
178 EXPECT_EQ(net::ERR_IO_PENDING, result); | 186 EXPECT_EQ(net::ERR_IO_PENDING, result); |
179 | 187 |
180 // And when the data is supplied, the callback will be called. | 188 // And when the data is supplied, the callback will be called. |
181 scoped_ptr<std::string> data(new std::string("abcde")); | 189 scoped_ptr<std::string> data(new std::string("abcde")); |
182 proxy.OnGetContent(data.Pass()); | 190 proxy.OnGetContent(data.Pass()); |
183 data.reset(new std::string("fgh")); | 191 data.reset(new std::string("fgh")); |
(...skipping 25 matching lines...) Expand all Loading... |
209 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 217 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
210 EXPECT_EQ(2, result); | 218 EXPECT_EQ(2, result); |
211 EXPECT_EQ("st", std::string(buffer->data(), result)); | 219 EXPECT_EQ("st", std::string(buffer->data(), result)); |
212 | 220 |
213 // The whole data is read, so Read() should return 0 immediately by then. | 221 // The whole data is read, so Read() should return 0 immediately by then. |
214 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 222 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
215 EXPECT_EQ(0, result); | 223 EXPECT_EQ(0, result); |
216 } | 224 } |
217 | 225 |
218 TEST_F(NetworkReaderProxyTest, ErrorWithPendingCallback) { | 226 TEST_F(NetworkReaderProxyTest, ErrorWithPendingCallback) { |
219 NetworkReaderProxy proxy(0, 10, base::Bind(&base::DoNothing)); | 227 NetworkReaderProxy proxy(0, 10, 10, base::Bind(&base::DoNothing)); |
220 | 228 |
221 net::TestCompletionCallback callback; | 229 net::TestCompletionCallback callback; |
222 const int kBufferSize = 3; | 230 const int kBufferSize = 3; |
223 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 231 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
224 | 232 |
225 // Set pending callback. | 233 // Set pending callback. |
226 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 234 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
227 EXPECT_EQ(net::ERR_IO_PENDING, result); | 235 EXPECT_EQ(net::ERR_IO_PENDING, result); |
228 | 236 |
229 // Emulate that an error is found. The callback should be called internally. | 237 // Emulate that an error is found. The callback should be called internally. |
230 proxy.OnCompleted(FILE_ERROR_FAILED); | 238 proxy.OnCompleted(FILE_ERROR_FAILED); |
231 result = callback.GetResult(result); | 239 result = callback.GetResult(result); |
232 EXPECT_EQ(net::ERR_FAILED, result); | 240 EXPECT_EQ(net::ERR_FAILED, result); |
233 | 241 |
234 // The next Read call should also return the same error code. | 242 // The next Read call should also return the same error code. |
235 EXPECT_EQ(net::ERR_FAILED, | 243 EXPECT_EQ(net::ERR_FAILED, |
236 proxy.Read(buffer.get(), kBufferSize, callback.callback())); | 244 proxy.Read(buffer.get(), kBufferSize, callback.callback())); |
237 } | 245 } |
238 | 246 |
239 TEST_F(NetworkReaderProxyTest, ErrorWithPendingData) { | 247 TEST_F(NetworkReaderProxyTest, ErrorWithPendingData) { |
240 NetworkReaderProxy proxy(0, 10, base::Bind(&base::DoNothing)); | 248 NetworkReaderProxy proxy(0, 10, 10, base::Bind(&base::DoNothing)); |
241 | 249 |
242 net::TestCompletionCallback callback; | 250 net::TestCompletionCallback callback; |
243 const int kBufferSize = 3; | 251 const int kBufferSize = 3; |
244 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 252 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
245 | 253 |
246 // Supply the data before an error. | 254 // Supply the data before an error. |
247 scoped_ptr<std::string> data(new std::string("abcde")); | 255 scoped_ptr<std::string> data(new std::string("abcde")); |
248 proxy.OnGetContent(data.Pass()); | 256 proxy.OnGetContent(data.Pass()); |
249 | 257 |
250 // Emulate that an error is found. | 258 // Emulate that an error is found. |
251 proxy.OnCompleted(FILE_ERROR_FAILED); | 259 proxy.OnCompleted(FILE_ERROR_FAILED); |
252 | 260 |
253 // The next Read call should return the error code, even if there is | 261 // The next Read call should return the error code, even if there is |
254 // pending data (the pending data should be released in OnCompleted. | 262 // pending data (the pending data should be released in OnCompleted. |
255 EXPECT_EQ(net::ERR_FAILED, | 263 EXPECT_EQ(net::ERR_FAILED, |
256 proxy.Read(buffer.get(), kBufferSize, callback.callback())); | 264 proxy.Read(buffer.get(), kBufferSize, callback.callback())); |
257 } | 265 } |
258 | 266 |
259 TEST_F(NetworkReaderProxyTest, CancelJob) { | 267 TEST_F(NetworkReaderProxyTest, CancelJob) { |
260 int num_called = 0; | 268 int num_called = 0; |
261 { | 269 { |
262 NetworkReaderProxy proxy( | 270 NetworkReaderProxy proxy( |
263 0, 0, base::Bind(&IncrementCallback, &num_called)); | 271 0, 0, 0, base::Bind(&IncrementCallback, &num_called)); |
264 proxy.OnCompleted(FILE_ERROR_OK); | 272 proxy.OnCompleted(FILE_ERROR_OK); |
265 // Destroy the instance after the network operation is completed. | 273 // Destroy the instance after the network operation is completed. |
266 // The cancelling callback shouldn't be called. | 274 // The cancelling callback shouldn't be called. |
267 } | 275 } |
268 EXPECT_EQ(0, num_called); | 276 EXPECT_EQ(0, num_called); |
269 | 277 |
270 num_called = 0; | 278 num_called = 0; |
271 { | 279 { |
272 NetworkReaderProxy proxy( | 280 NetworkReaderProxy proxy( |
273 0, 0, base::Bind(&IncrementCallback, &num_called)); | 281 0, 0, 0, base::Bind(&IncrementCallback, &num_called)); |
274 // Destroy the instance before the network operation is completed. | 282 // Destroy the instance before the network operation is completed. |
275 // The cancelling callback should be called. | 283 // The cancelling callback should be called. |
276 } | 284 } |
277 EXPECT_EQ(1, num_called); | 285 EXPECT_EQ(1, num_called); |
278 } | 286 } |
279 | 287 |
280 } // namespace internal | 288 } // namespace internal |
281 | 289 |
282 class DriveFileStreamReaderTest : public ::testing::Test { | 290 class DriveFileStreamReaderTest : public ::testing::Test { |
283 protected: | 291 protected: |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
551 ASSERT_TRUE(entry); | 559 ASSERT_TRUE(entry); |
552 EXPECT_TRUE(reader->IsInitialized()); | 560 EXPECT_TRUE(reader->IsInitialized()); |
553 | 561 |
554 // Read data from the reader, again. | 562 // Read data from the reader, again. |
555 std::string second_content; | 563 std::string second_content; |
556 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); | 564 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); |
557 EXPECT_EQ(0u, second_content.size()); | 565 EXPECT_EQ(0u, second_content.size()); |
558 } | 566 } |
559 | 567 |
560 } // namespace drive | 568 } // namespace drive |
OLD | NEW |