OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/file_system_provider/fileapi/buffering_file_st
ream_writer.h" |
| 6 |
| 7 #include <memory> |
5 #include <string> | 8 #include <string> |
6 #include <vector> | 9 #include <vector> |
7 | 10 |
8 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" |
9 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
12 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
13 #include "chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_st
ream_writer.h" | |
14 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 | 20 |
19 namespace chromeos { | 21 namespace chromeos { |
20 namespace file_system_provider { | 22 namespace file_system_provider { |
21 namespace { | 23 namespace { |
22 | 24 |
23 // Size of the intermediate buffer in bytes. | 25 // Size of the intermediate buffer in bytes. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 111 |
110 content::TestBrowserThreadBundle thread_bundle_; | 112 content::TestBrowserThreadBundle thread_bundle_; |
111 scoped_refptr<net::StringIOBuffer> short_text_buffer_; | 113 scoped_refptr<net::StringIOBuffer> short_text_buffer_; |
112 scoped_refptr<net::StringIOBuffer> long_text_buffer_; | 114 scoped_refptr<net::StringIOBuffer> long_text_buffer_; |
113 }; | 115 }; |
114 | 116 |
115 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Write) { | 117 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Write) { |
116 std::vector<std::string> inner_write_log; | 118 std::vector<std::string> inner_write_log; |
117 std::vector<int> inner_flush_log; | 119 std::vector<int> inner_flush_log; |
118 BufferingFileStreamWriter writer( | 120 BufferingFileStreamWriter writer( |
119 make_scoped_ptr(new FakeFileStreamWriter( | 121 base::WrapUnique(new FakeFileStreamWriter( |
120 &inner_write_log, &inner_flush_log, NULL, net::OK)), | 122 &inner_write_log, &inner_flush_log, NULL, net::OK)), |
121 kIntermediateBufferLength); | 123 kIntermediateBufferLength); |
122 | 124 |
123 ASSERT_LT(kIntermediateBufferLength, 2 * short_text_buffer_->size()); | 125 ASSERT_LT(kIntermediateBufferLength, 2 * short_text_buffer_->size()); |
124 | 126 |
125 // Writing for the first time should succeed, but buffer the write without | 127 // Writing for the first time should succeed, but buffer the write without |
126 // calling the inner file stream writer. | 128 // calling the inner file stream writer. |
127 { | 129 { |
128 std::vector<int> write_log; | 130 std::vector<int> write_log; |
129 const int result = writer.Write(short_text_buffer_.get(), | 131 const int result = writer.Write(short_text_buffer_.get(), |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 const int expected_inner_flush = 2 * short_text_buffer_->size(); | 185 const int expected_inner_flush = 2 * short_text_buffer_->size(); |
184 ASSERT_EQ(1u, inner_flush_log.size()); | 186 ASSERT_EQ(1u, inner_flush_log.size()); |
185 EXPECT_EQ(expected_inner_flush, inner_flush_log[0]); | 187 EXPECT_EQ(expected_inner_flush, inner_flush_log[0]); |
186 } | 188 } |
187 } | 189 } |
188 | 190 |
189 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Write_WithError) { | 191 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Write_WithError) { |
190 std::vector<std::string> inner_write_log; | 192 std::vector<std::string> inner_write_log; |
191 std::vector<int> inner_flush_log; | 193 std::vector<int> inner_flush_log; |
192 BufferingFileStreamWriter writer( | 194 BufferingFileStreamWriter writer( |
193 scoped_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( | 195 std::unique_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( |
194 &inner_write_log, &inner_flush_log, NULL, net::ERR_FAILED)), | 196 &inner_write_log, &inner_flush_log, NULL, net::ERR_FAILED)), |
195 kIntermediateBufferLength); | 197 kIntermediateBufferLength); |
196 | 198 |
197 ASSERT_LT(kIntermediateBufferLength, 2 * short_text_buffer_->size()); | 199 ASSERT_LT(kIntermediateBufferLength, 2 * short_text_buffer_->size()); |
198 | 200 |
199 // Writing for the first time should succeed, but buffer the write without | 201 // Writing for the first time should succeed, but buffer the write without |
200 // calling the inner file stream writer. Because of that, the error will | 202 // calling the inner file stream writer. Because of that, the error will |
201 // not be generated unless the intermediate buffer is flushed. | 203 // not be generated unless the intermediate buffer is flushed. |
202 { | 204 { |
203 std::vector<int> write_log; | 205 std::vector<int> write_log; |
(...skipping 29 matching lines...) Expand all Loading... |
233 ASSERT_EQ(1u, write_log.size()); | 235 ASSERT_EQ(1u, write_log.size()); |
234 EXPECT_EQ(net::ERR_FAILED, write_log[0]); | 236 EXPECT_EQ(net::ERR_FAILED, write_log[0]); |
235 EXPECT_EQ(0u, inner_flush_log.size()); | 237 EXPECT_EQ(0u, inner_flush_log.size()); |
236 } | 238 } |
237 } | 239 } |
238 | 240 |
239 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Write_Directly) { | 241 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Write_Directly) { |
240 std::vector<std::string> inner_write_log; | 242 std::vector<std::string> inner_write_log; |
241 std::vector<int> inner_flush_log; | 243 std::vector<int> inner_flush_log; |
242 BufferingFileStreamWriter writer( | 244 BufferingFileStreamWriter writer( |
243 scoped_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( | 245 std::unique_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( |
244 &inner_write_log, &inner_flush_log, NULL, net::OK)), | 246 &inner_write_log, &inner_flush_log, NULL, net::OK)), |
245 kIntermediateBufferLength); | 247 kIntermediateBufferLength); |
246 | 248 |
247 ASSERT_GT(kIntermediateBufferLength, short_text_buffer_->size()); | 249 ASSERT_GT(kIntermediateBufferLength, short_text_buffer_->size()); |
248 ASSERT_LT(kIntermediateBufferLength, long_text_buffer_->size()); | 250 ASSERT_LT(kIntermediateBufferLength, long_text_buffer_->size()); |
249 | 251 |
250 // Write few bytes first, so the intermediate buffer is not empty. | 252 // Write few bytes first, so the intermediate buffer is not empty. |
251 { | 253 { |
252 std::vector<int> write_log; | 254 std::vector<int> write_log; |
253 const int result = writer.Write(short_text_buffer_.get(), | 255 const int result = writer.Write(short_text_buffer_.get(), |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 EXPECT_EQ(0u, inner_flush_log.size()); | 303 EXPECT_EQ(0u, inner_flush_log.size()); |
302 } | 304 } |
303 } | 305 } |
304 | 306 |
305 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Cancel) { | 307 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Cancel) { |
306 std::vector<std::string> inner_write_log; | 308 std::vector<std::string> inner_write_log; |
307 std::vector<int> inner_flush_log; | 309 std::vector<int> inner_flush_log; |
308 int inner_cancel_counter = 0; | 310 int inner_cancel_counter = 0; |
309 | 311 |
310 BufferingFileStreamWriter writer( | 312 BufferingFileStreamWriter writer( |
311 scoped_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( | 313 std::unique_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( |
312 &inner_write_log, &inner_flush_log, &inner_cancel_counter, net::OK)), | 314 &inner_write_log, &inner_flush_log, &inner_cancel_counter, net::OK)), |
313 kIntermediateBufferLength); | 315 kIntermediateBufferLength); |
314 | 316 |
315 // Write directly, so there is something to actually cancel. Note, that | 317 // Write directly, so there is something to actually cancel. Note, that |
316 // buffered writes which do not invoke flushing the intermediate buffer finish | 318 // buffered writes which do not invoke flushing the intermediate buffer finish |
317 // immediately, so they are not cancellable. | 319 // immediately, so they are not cancellable. |
318 std::vector<int> write_log; | 320 std::vector<int> write_log; |
319 const int write_result = writer.Write(long_text_buffer_.get(), | 321 const int write_result = writer.Write(long_text_buffer_.get(), |
320 long_text_buffer_->size(), | 322 long_text_buffer_->size(), |
321 base::Bind(&LogValue<int>, &write_log)); | 323 base::Bind(&LogValue<int>, &write_log)); |
322 EXPECT_EQ(net::ERR_IO_PENDING, write_result); | 324 EXPECT_EQ(net::ERR_IO_PENDING, write_result); |
323 | 325 |
324 std::vector<int> cancel_log; | 326 std::vector<int> cancel_log; |
325 const int cancel_result = | 327 const int cancel_result = |
326 writer.Cancel(base::Bind(&LogValue<int>, &cancel_log)); | 328 writer.Cancel(base::Bind(&LogValue<int>, &cancel_log)); |
327 base::RunLoop().RunUntilIdle(); | 329 base::RunLoop().RunUntilIdle(); |
328 | 330 |
329 EXPECT_EQ(net::ERR_IO_PENDING, cancel_result); | 331 EXPECT_EQ(net::ERR_IO_PENDING, cancel_result); |
330 ASSERT_EQ(1u, cancel_log.size()); | 332 ASSERT_EQ(1u, cancel_log.size()); |
331 EXPECT_EQ(net::OK, cancel_log[0]); | 333 EXPECT_EQ(net::OK, cancel_log[0]); |
332 EXPECT_EQ(1, inner_cancel_counter); | 334 EXPECT_EQ(1, inner_cancel_counter); |
333 } | 335 } |
334 | 336 |
335 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Flush) { | 337 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Flush) { |
336 std::vector<std::string> inner_write_log; | 338 std::vector<std::string> inner_write_log; |
337 std::vector<int> inner_flush_log; | 339 std::vector<int> inner_flush_log; |
338 BufferingFileStreamWriter writer( | 340 BufferingFileStreamWriter writer( |
339 scoped_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( | 341 std::unique_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( |
340 &inner_write_log, &inner_flush_log, NULL, net::OK)), | 342 &inner_write_log, &inner_flush_log, NULL, net::OK)), |
341 kIntermediateBufferLength); | 343 kIntermediateBufferLength); |
342 | 344 |
343 // Write less bytes than size of the intermediate buffer. | 345 // Write less bytes than size of the intermediate buffer. |
344 std::vector<int> write_log; | 346 std::vector<int> write_log; |
345 const int write_result = writer.Write(short_text_buffer_.get(), | 347 const int write_result = writer.Write(short_text_buffer_.get(), |
346 short_text_buffer_->size(), | 348 short_text_buffer_->size(), |
347 base::Bind(&LogValue<int>, &write_log)); | 349 base::Bind(&LogValue<int>, &write_log)); |
348 base::RunLoop().RunUntilIdle(); | 350 base::RunLoop().RunUntilIdle(); |
349 | 351 |
(...skipping 16 matching lines...) Expand all Loading... |
366 EXPECT_EQ(kShortTextToWrite, inner_write_log[0]); | 368 EXPECT_EQ(kShortTextToWrite, inner_write_log[0]); |
367 | 369 |
368 ASSERT_EQ(1u, inner_flush_log.size()); | 370 ASSERT_EQ(1u, inner_flush_log.size()); |
369 EXPECT_EQ(short_text_buffer_->size(), inner_flush_log[0]); | 371 EXPECT_EQ(short_text_buffer_->size(), inner_flush_log[0]); |
370 } | 372 } |
371 | 373 |
372 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Flush_AfterWriteError) { | 374 TEST_F(FileSystemProviderBufferingFileStreamWriterTest, Flush_AfterWriteError) { |
373 std::vector<std::string> inner_write_log; | 375 std::vector<std::string> inner_write_log; |
374 std::vector<int> inner_flush_log; | 376 std::vector<int> inner_flush_log; |
375 BufferingFileStreamWriter writer( | 377 BufferingFileStreamWriter writer( |
376 scoped_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( | 378 std::unique_ptr<storage::FileStreamWriter>(new FakeFileStreamWriter( |
377 &inner_write_log, &inner_flush_log, NULL, net::ERR_FAILED)), | 379 &inner_write_log, &inner_flush_log, NULL, net::ERR_FAILED)), |
378 kIntermediateBufferLength); | 380 kIntermediateBufferLength); |
379 | 381 |
380 // Write less bytes than size of the intermediate buffer. This should succeed | 382 // Write less bytes than size of the intermediate buffer. This should succeed |
381 // since the inner file stream writer is not invoked. | 383 // since the inner file stream writer is not invoked. |
382 std::vector<int> write_log; | 384 std::vector<int> write_log; |
383 const int write_result = writer.Write(short_text_buffer_.get(), | 385 const int write_result = writer.Write(short_text_buffer_.get(), |
384 short_text_buffer_->size(), | 386 short_text_buffer_->size(), |
385 base::Bind(&LogValue<int>, &write_log)); | 387 base::Bind(&LogValue<int>, &write_log)); |
386 base::RunLoop().RunUntilIdle(); | 388 base::RunLoop().RunUntilIdle(); |
(...skipping 16 matching lines...) Expand all Loading... |
403 ASSERT_EQ(1u, inner_write_log.size()); | 405 ASSERT_EQ(1u, inner_write_log.size()); |
404 EXPECT_EQ(kShortTextToWrite, inner_write_log[0]); | 406 EXPECT_EQ(kShortTextToWrite, inner_write_log[0]); |
405 | 407 |
406 // Flush of the inner file stream writer is not invoked, since a Write method | 408 // Flush of the inner file stream writer is not invoked, since a Write method |
407 // invocation fails before it. | 409 // invocation fails before it. |
408 EXPECT_EQ(0u, inner_flush_log.size()); | 410 EXPECT_EQ(0u, inner_flush_log.size()); |
409 } | 411 } |
410 | 412 |
411 } // namespace file_system_provider | 413 } // namespace file_system_provider |
412 } // namespace chromeos | 414 } // namespace chromeos |
OLD | NEW |