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 <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 worker_thread_.reset(new base::Thread("ReaderProxyTest")); | 56 worker_thread_.reset(new base::Thread("ReaderProxyTest")); |
57 ASSERT_TRUE(worker_thread_->Start()); | 57 ASSERT_TRUE(worker_thread_->Start()); |
58 } | 58 } |
59 | 59 |
60 content::TestBrowserThreadBundle thread_bundle_; | 60 content::TestBrowserThreadBundle thread_bundle_; |
61 | 61 |
62 base::ScopedTempDir temp_dir_; | 62 base::ScopedTempDir temp_dir_; |
63 base::FilePath file_path_; | 63 base::FilePath file_path_; |
64 std::string file_content_; | 64 std::string file_content_; |
65 | 65 |
66 scoped_ptr<base::Thread> worker_thread_; | 66 std::unique_ptr<base::Thread> worker_thread_; |
67 }; | 67 }; |
68 | 68 |
69 TEST_F(LocalReaderProxyTest, Read) { | 69 TEST_F(LocalReaderProxyTest, Read) { |
70 // Open the file first. | 70 // Open the file first. |
71 scoped_ptr<util::LocalFileReader> file_reader( | 71 std::unique_ptr<util::LocalFileReader> file_reader( |
72 new util::LocalFileReader(worker_thread_->task_runner().get())); | 72 new util::LocalFileReader(worker_thread_->task_runner().get())); |
73 net::TestCompletionCallback callback; | 73 net::TestCompletionCallback callback; |
74 file_reader->Open(file_path_, 0, callback.callback()); | 74 file_reader->Open(file_path_, 0, callback.callback()); |
75 ASSERT_EQ(net::OK, callback.WaitForResult()); | 75 ASSERT_EQ(net::OK, callback.WaitForResult()); |
76 | 76 |
77 // Test instance. | 77 // Test instance. |
78 LocalReaderProxy proxy(std::move(file_reader), file_content_.size()); | 78 LocalReaderProxy proxy(std::move(file_reader), file_content_.size()); |
79 | 79 |
80 // Make sure the read content is as same as the file. | 80 // Make sure the read content is as same as the file. |
81 std::string content; | 81 std::string content; |
82 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); | 82 ASSERT_EQ(net::OK, test_util::ReadAllData(&proxy, &content)); |
83 EXPECT_EQ(file_content_, content); | 83 EXPECT_EQ(file_content_, content); |
84 } | 84 } |
85 | 85 |
86 TEST_F(LocalReaderProxyTest, ReadWithLimit) { | 86 TEST_F(LocalReaderProxyTest, ReadWithLimit) { |
87 // This test case, we only read first half of the file. | 87 // This test case, we only read first half of the file. |
88 const std::string expected_content = | 88 const std::string expected_content = |
89 file_content_.substr(0, file_content_.size() / 2); | 89 file_content_.substr(0, file_content_.size() / 2); |
90 | 90 |
91 // Open the file first. | 91 // Open the file first. |
92 scoped_ptr<util::LocalFileReader> file_reader( | 92 std::unique_ptr<util::LocalFileReader> file_reader( |
93 new util::LocalFileReader(worker_thread_->task_runner().get())); | 93 new util::LocalFileReader(worker_thread_->task_runner().get())); |
94 net::TestCompletionCallback callback; | 94 net::TestCompletionCallback callback; |
95 file_reader->Open(file_path_, 0, callback.callback()); | 95 file_reader->Open(file_path_, 0, callback.callback()); |
96 ASSERT_EQ(net::OK, callback.WaitForResult()); | 96 ASSERT_EQ(net::OK, callback.WaitForResult()); |
97 | 97 |
98 // Test instance. | 98 // Test instance. |
99 LocalReaderProxy proxy(std::move(file_reader), expected_content.size()); | 99 LocalReaderProxy proxy(std::move(file_reader), expected_content.size()); |
100 | 100 |
101 // Make sure the read content is as same as the file. | 101 // Make sure the read content is as same as the file. |
102 std::string content; | 102 std::string content; |
(...skipping 30 matching lines...) Expand all Loading... |
133 | 133 |
134 net::TestCompletionCallback callback; | 134 net::TestCompletionCallback callback; |
135 const int kBufferSize = 3; | 135 const int kBufferSize = 3; |
136 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 136 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
137 | 137 |
138 // If no data is available yet, ERR_IO_PENDING should be returned. | 138 // If no data is available yet, ERR_IO_PENDING should be returned. |
139 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 139 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
140 EXPECT_EQ(net::ERR_IO_PENDING, result); | 140 EXPECT_EQ(net::ERR_IO_PENDING, result); |
141 | 141 |
142 // And when the data is supplied, the callback will be called. | 142 // And when the data is supplied, the callback will be called. |
143 scoped_ptr<std::string> data(new std::string("abcde")); | 143 std::unique_ptr<std::string> data(new std::string("abcde")); |
144 proxy.OnGetContent(std::move(data)); | 144 proxy.OnGetContent(std::move(data)); |
145 | 145 |
146 // The returned data should be fit to the buffer size. | 146 // The returned data should be fit to the buffer size. |
147 result = callback.GetResult(result); | 147 result = callback.GetResult(result); |
148 EXPECT_EQ(3, result); | 148 EXPECT_EQ(3, result); |
149 EXPECT_EQ("abc", std::string(buffer->data(), result)); | 149 EXPECT_EQ("abc", std::string(buffer->data(), result)); |
150 | 150 |
151 // The next Read should return immediately because there is pending data | 151 // The next Read should return immediately because there is pending data |
152 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 152 result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
153 EXPECT_EQ(2, result); | 153 EXPECT_EQ(2, result); |
(...skipping 29 matching lines...) Expand all Loading... |
183 | 183 |
184 net::TestCompletionCallback callback; | 184 net::TestCompletionCallback callback; |
185 const int kBufferSize = 3; | 185 const int kBufferSize = 3; |
186 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 186 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
187 | 187 |
188 // If no data is available yet, ERR_IO_PENDING should be returned. | 188 // If no data is available yet, ERR_IO_PENDING should be returned. |
189 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); | 189 int result = proxy.Read(buffer.get(), kBufferSize, callback.callback()); |
190 EXPECT_EQ(net::ERR_IO_PENDING, result); | 190 EXPECT_EQ(net::ERR_IO_PENDING, result); |
191 | 191 |
192 // And when the data is supplied, the callback will be called. | 192 // And when the data is supplied, the callback will be called. |
193 scoped_ptr<std::string> data(new std::string("abcde")); | 193 std::unique_ptr<std::string> data(new std::string("abcde")); |
194 proxy.OnGetContent(std::move(data)); | 194 proxy.OnGetContent(std::move(data)); |
195 data.reset(new std::string("fgh")); | 195 data.reset(new std::string("fgh")); |
196 proxy.OnGetContent(std::move(data)); | 196 proxy.OnGetContent(std::move(data)); |
197 data.reset(new std::string("ijklmno")); | 197 data.reset(new std::string("ijklmno")); |
198 proxy.OnGetContent(std::move(data)); | 198 proxy.OnGetContent(std::move(data)); |
199 | 199 |
200 // The returned data should be fit to the buffer size. | 200 // The returned data should be fit to the buffer size. |
201 result = callback.GetResult(result); | 201 result = callback.GetResult(result); |
202 EXPECT_EQ(3, result); | 202 EXPECT_EQ(3, result); |
203 EXPECT_EQ("klm", std::string(buffer->data(), result)); | 203 EXPECT_EQ("klm", std::string(buffer->data(), result)); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 } | 249 } |
250 | 250 |
251 TEST_F(NetworkReaderProxyTest, ErrorWithPendingData) { | 251 TEST_F(NetworkReaderProxyTest, ErrorWithPendingData) { |
252 NetworkReaderProxy proxy(0, 10, 10, base::Bind(&base::DoNothing)); | 252 NetworkReaderProxy proxy(0, 10, 10, base::Bind(&base::DoNothing)); |
253 | 253 |
254 net::TestCompletionCallback callback; | 254 net::TestCompletionCallback callback; |
255 const int kBufferSize = 3; | 255 const int kBufferSize = 3; |
256 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | 256 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); |
257 | 257 |
258 // Supply the data before an error. | 258 // Supply the data before an error. |
259 scoped_ptr<std::string> data(new std::string("abcde")); | 259 std::unique_ptr<std::string> data(new std::string("abcde")); |
260 proxy.OnGetContent(std::move(data)); | 260 proxy.OnGetContent(std::move(data)); |
261 | 261 |
262 // Emulate that an error is found. | 262 // Emulate that an error is found. |
263 proxy.OnCompleted(FILE_ERROR_FAILED); | 263 proxy.OnCompleted(FILE_ERROR_FAILED); |
264 | 264 |
265 // The next Read call should return the error code, even if there is | 265 // The next Read call should return the error code, even if there is |
266 // pending data (the pending data should be released in OnCompleted. | 266 // pending data (the pending data should be released in OnCompleted. |
267 EXPECT_EQ(net::ERR_FAILED, | 267 EXPECT_EQ(net::ERR_FAILED, |
268 proxy.Read(buffer.get(), kBufferSize, callback.callback())); | 268 proxy.Read(buffer.get(), kBufferSize, callback.callback())); |
269 } | 269 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 return fake_file_system_.get(); | 314 return fake_file_system_.get(); |
315 } | 315 } |
316 | 316 |
317 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { | 317 DriveFileStreamReader::FileSystemGetter GetFileSystemGetter() { |
318 return base::Bind(&DriveFileStreamReaderTest::GetFileSystem, | 318 return base::Bind(&DriveFileStreamReaderTest::GetFileSystem, |
319 base::Unretained(this)); | 319 base::Unretained(this)); |
320 } | 320 } |
321 | 321 |
322 content::TestBrowserThreadBundle thread_bundle_; | 322 content::TestBrowserThreadBundle thread_bundle_; |
323 | 323 |
324 scoped_ptr<base::Thread> worker_thread_; | 324 std::unique_ptr<base::Thread> worker_thread_; |
325 | 325 |
326 scoped_ptr<FakeDriveService> fake_drive_service_; | 326 std::unique_ptr<FakeDriveService> fake_drive_service_; |
327 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 327 std::unique_ptr<test_util::FakeFileSystem> fake_file_system_; |
328 }; | 328 }; |
329 | 329 |
330 TEST_F(DriveFileStreamReaderTest, Read) { | 330 TEST_F(DriveFileStreamReaderTest, Read) { |
331 const base::FilePath kDriveFile = | 331 const base::FilePath kDriveFile = |
332 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 332 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
333 // Create the reader, and initialize it. | 333 // Create the reader, and initialize it. |
334 // In this case, the file is not yet locally cached. | 334 // In this case, the file is not yet locally cached. |
335 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 335 std::unique_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
336 GetFileSystemGetter(), worker_thread_->task_runner().get())); | 336 GetFileSystemGetter(), worker_thread_->task_runner().get())); |
337 EXPECT_FALSE(reader->IsInitialized()); | 337 EXPECT_FALSE(reader->IsInitialized()); |
338 | 338 |
339 int error = net::ERR_FAILED; | 339 int error = net::ERR_FAILED; |
340 scoped_ptr<ResourceEntry> entry; | 340 std::unique_ptr<ResourceEntry> entry; |
341 { | 341 { |
342 base::RunLoop run_loop; | 342 base::RunLoop run_loop; |
343 reader->Initialize( | 343 reader->Initialize( |
344 kDriveFile, | 344 kDriveFile, |
345 net::HttpByteRange(), | 345 net::HttpByteRange(), |
346 google_apis::test_util::CreateQuitCallback( | 346 google_apis::test_util::CreateQuitCallback( |
347 &run_loop, | 347 &run_loop, |
348 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); | 348 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
349 run_loop.Run(); | 349 run_loop.Run(); |
350 } | 350 } |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 393 |
394 TEST_F(DriveFileStreamReaderTest, ReadRange) { | 394 TEST_F(DriveFileStreamReaderTest, ReadRange) { |
395 // In this test case, we just confirm that the part of file is read. | 395 // In this test case, we just confirm that the part of file is read. |
396 const int64_t kRangeOffset = 3; | 396 const int64_t kRangeOffset = 3; |
397 const int64_t kRangeLength = 4; | 397 const int64_t kRangeLength = 4; |
398 | 398 |
399 const base::FilePath kDriveFile = | 399 const base::FilePath kDriveFile = |
400 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 400 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
401 // Create the reader, and initialize it. | 401 // Create the reader, and initialize it. |
402 // In this case, the file is not yet locally cached. | 402 // In this case, the file is not yet locally cached. |
403 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 403 std::unique_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
404 GetFileSystemGetter(), worker_thread_->task_runner().get())); | 404 GetFileSystemGetter(), worker_thread_->task_runner().get())); |
405 EXPECT_FALSE(reader->IsInitialized()); | 405 EXPECT_FALSE(reader->IsInitialized()); |
406 | 406 |
407 int error = net::ERR_FAILED; | 407 int error = net::ERR_FAILED; |
408 scoped_ptr<ResourceEntry> entry; | 408 std::unique_ptr<ResourceEntry> entry; |
409 net::HttpByteRange byte_range; | 409 net::HttpByteRange byte_range; |
410 byte_range.set_first_byte_position(kRangeOffset); | 410 byte_range.set_first_byte_position(kRangeOffset); |
411 // Last byte position is inclusive. | 411 // Last byte position is inclusive. |
412 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); | 412 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); |
413 { | 413 { |
414 base::RunLoop run_loop; | 414 base::RunLoop run_loop; |
415 reader->Initialize( | 415 reader->Initialize( |
416 kDriveFile, | 416 kDriveFile, |
417 byte_range, | 417 byte_range, |
418 google_apis::test_util::CreateQuitCallback( | 418 google_apis::test_util::CreateQuitCallback( |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 } | 462 } |
463 | 463 |
464 TEST_F(DriveFileStreamReaderTest, OutOfRangeError) { | 464 TEST_F(DriveFileStreamReaderTest, OutOfRangeError) { |
465 const int64_t kRangeOffset = 1000000; // Out of range. | 465 const int64_t kRangeOffset = 1000000; // Out of range. |
466 const int64_t kRangeLength = 4; | 466 const int64_t kRangeLength = 4; |
467 | 467 |
468 const base::FilePath kDriveFile = | 468 const base::FilePath kDriveFile = |
469 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); | 469 util::GetDriveMyDriveRootPath().AppendASCII("File 1.txt"); |
470 // Create the reader, and initialize it. | 470 // Create the reader, and initialize it. |
471 // In this case, the file is not yet locally cached. | 471 // In this case, the file is not yet locally cached. |
472 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 472 std::unique_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
473 GetFileSystemGetter(), worker_thread_->task_runner().get())); | 473 GetFileSystemGetter(), worker_thread_->task_runner().get())); |
474 EXPECT_FALSE(reader->IsInitialized()); | 474 EXPECT_FALSE(reader->IsInitialized()); |
475 | 475 |
476 int error = net::ERR_FAILED; | 476 int error = net::ERR_FAILED; |
477 scoped_ptr<ResourceEntry> entry; | 477 std::unique_ptr<ResourceEntry> entry; |
478 net::HttpByteRange byte_range; | 478 net::HttpByteRange byte_range; |
479 byte_range.set_first_byte_position(kRangeOffset); | 479 byte_range.set_first_byte_position(kRangeOffset); |
480 // Last byte position is inclusive. | 480 // Last byte position is inclusive. |
481 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); | 481 byte_range.set_last_byte_position(kRangeOffset + kRangeLength - 1); |
482 { | 482 { |
483 base::RunLoop run_loop; | 483 base::RunLoop run_loop; |
484 reader->Initialize( | 484 reader->Initialize( |
485 kDriveFile, | 485 kDriveFile, |
486 byte_range, | 486 byte_range, |
487 google_apis::test_util::CreateQuitCallback( | 487 google_apis::test_util::CreateQuitCallback( |
488 &run_loop, | 488 &run_loop, |
489 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); | 489 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
490 run_loop.Run(); | 490 run_loop.Run(); |
491 } | 491 } |
492 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); | 492 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); |
493 EXPECT_FALSE(entry); | 493 EXPECT_FALSE(entry); |
494 } | 494 } |
495 | 495 |
496 TEST_F(DriveFileStreamReaderTest, ZeroByteFileRead) { | 496 TEST_F(DriveFileStreamReaderTest, ZeroByteFileRead) { |
497 // Prepare an empty file | 497 // Prepare an empty file |
498 { | 498 { |
499 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR; | 499 google_apis::DriveApiErrorCode error = google_apis::DRIVE_OTHER_ERROR; |
500 scoped_ptr<google_apis::FileResource> entry; | 500 std::unique_ptr<google_apis::FileResource> entry; |
501 fake_drive_service_->AddNewFile( | 501 fake_drive_service_->AddNewFile( |
502 "text/plain", | 502 "text/plain", |
503 "", // empty | 503 "", // empty |
504 fake_drive_service_->GetRootResourceId(), | 504 fake_drive_service_->GetRootResourceId(), |
505 "EmptyFile.txt", | 505 "EmptyFile.txt", |
506 false, // shared_with_me | 506 false, // shared_with_me |
507 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); | 507 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
508 content::RunAllBlockingPoolTasksUntilIdle(); | 508 content::RunAllBlockingPoolTasksUntilIdle(); |
509 ASSERT_EQ(google_apis::HTTP_CREATED, error); | 509 ASSERT_EQ(google_apis::HTTP_CREATED, error); |
510 ASSERT_TRUE(entry); | 510 ASSERT_TRUE(entry); |
511 ASSERT_EQ(0, entry->file_size()); | 511 ASSERT_EQ(0, entry->file_size()); |
512 } | 512 } |
513 | 513 |
514 const base::FilePath kDriveFile = | 514 const base::FilePath kDriveFile = |
515 util::GetDriveMyDriveRootPath().AppendASCII("EmptyFile.txt"); | 515 util::GetDriveMyDriveRootPath().AppendASCII("EmptyFile.txt"); |
516 // Create the reader, and initialize it. | 516 // Create the reader, and initialize it. |
517 // In this case, the file is not yet locally cached. | 517 // In this case, the file is not yet locally cached. |
518 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( | 518 std::unique_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
519 GetFileSystemGetter(), worker_thread_->task_runner().get())); | 519 GetFileSystemGetter(), worker_thread_->task_runner().get())); |
520 EXPECT_FALSE(reader->IsInitialized()); | 520 EXPECT_FALSE(reader->IsInitialized()); |
521 | 521 |
522 int error = net::ERR_FAILED; | 522 int error = net::ERR_FAILED; |
523 scoped_ptr<ResourceEntry> entry; | 523 std::unique_ptr<ResourceEntry> entry; |
524 { | 524 { |
525 base::RunLoop run_loop; | 525 base::RunLoop run_loop; |
526 reader->Initialize( | 526 reader->Initialize( |
527 kDriveFile, | 527 kDriveFile, |
528 net::HttpByteRange(), | 528 net::HttpByteRange(), |
529 google_apis::test_util::CreateQuitCallback( | 529 google_apis::test_util::CreateQuitCallback( |
530 &run_loop, | 530 &run_loop, |
531 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); | 531 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
532 run_loop.Run(); | 532 run_loop.Run(); |
533 } | 533 } |
(...skipping 29 matching lines...) Expand all Loading... |
563 ASSERT_TRUE(entry); | 563 ASSERT_TRUE(entry); |
564 EXPECT_TRUE(reader->IsInitialized()); | 564 EXPECT_TRUE(reader->IsInitialized()); |
565 | 565 |
566 // Read data from the reader, again. | 566 // Read data from the reader, again. |
567 std::string second_content; | 567 std::string second_content; |
568 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); | 568 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); |
569 EXPECT_EQ(0u, second_content.size()); | 569 EXPECT_EQ(0u, second_content.size()); |
570 } | 570 } |
571 | 571 |
572 } // namespace drive | 572 } // namespace drive |
OLD | NEW |