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" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "chrome/browser/chromeos/drive/fake_file_system.h" | 14 #include "chrome/browser/chromeos/drive/fake_file_system.h" |
15 #include "chrome/browser/chromeos/drive/file_system_util.h" | 15 #include "chrome/browser/chromeos/drive/file_system_util.h" |
16 #include "chrome/browser/chromeos/drive/local_file_reader.h" | 16 #include "chrome/browser/chromeos/drive/local_file_reader.h" |
17 #include "chrome/browser/chromeos/drive/test_util.h" | 17 #include "chrome/browser/chromeos/drive/test_util.h" |
18 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 20 #include "google_apis/drive/gdata_wapi_parser.h" |
20 #include "google_apis/drive/test_util.h" | 21 #include "google_apis/drive/test_util.h" |
21 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
22 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
23 #include "net/base/test_completion_callback.h" | 24 #include "net/base/test_completion_callback.h" |
24 #include "net/http/http_byte_range.h" | 25 #include "net/http/http_byte_range.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 namespace drive { | 28 namespace drive { |
28 namespace internal { | 29 namespace internal { |
29 namespace { | 30 namespace { |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 byte_range, | 476 byte_range, |
476 google_apis::test_util::CreateQuitCallback( | 477 google_apis::test_util::CreateQuitCallback( |
477 &run_loop, | 478 &run_loop, |
478 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); | 479 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
479 run_loop.Run(); | 480 run_loop.Run(); |
480 } | 481 } |
481 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); | 482 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, error); |
482 EXPECT_FALSE(entry); | 483 EXPECT_FALSE(entry); |
483 } | 484 } |
484 | 485 |
| 486 TEST_F(DriveFileStreamReaderTest, ZeroByteFileRead) { |
| 487 // Prepare an empty file |
| 488 { |
| 489 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 490 scoped_ptr<google_apis::ResourceEntry> entry; |
| 491 fake_drive_service_->AddNewFile( |
| 492 "text/plain", |
| 493 "", // empty |
| 494 fake_drive_service_->GetRootResourceId(), |
| 495 "EmptyFile.txt", |
| 496 false, // shared_with_me |
| 497 google_apis::test_util::CreateCopyResultCallback(&error, &entry)); |
| 498 drive::test_util::RunBlockingPoolTask(); |
| 499 ASSERT_EQ(google_apis::HTTP_CREATED, error); |
| 500 ASSERT_TRUE(entry); |
| 501 ASSERT_EQ(0, entry->file_size()); |
| 502 } |
| 503 |
| 504 const base::FilePath kDriveFile = |
| 505 util::GetDriveMyDriveRootPath().AppendASCII("EmptyFile.txt"); |
| 506 // Create the reader, and initialize it. |
| 507 // In this case, the file is not yet locally cached. |
| 508 scoped_ptr<DriveFileStreamReader> reader(new DriveFileStreamReader( |
| 509 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
| 510 EXPECT_FALSE(reader->IsInitialized()); |
| 511 |
| 512 int error = net::ERR_FAILED; |
| 513 scoped_ptr<ResourceEntry> entry; |
| 514 { |
| 515 base::RunLoop run_loop; |
| 516 reader->Initialize( |
| 517 kDriveFile, |
| 518 net::HttpByteRange(), |
| 519 google_apis::test_util::CreateQuitCallback( |
| 520 &run_loop, |
| 521 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 522 run_loop.Run(); |
| 523 } |
| 524 EXPECT_EQ(net::OK, error); |
| 525 ASSERT_TRUE(entry); |
| 526 ASSERT_EQ(0u, entry->file_info().size()); // It's a zero-byte file. |
| 527 EXPECT_TRUE(reader->IsInitialized()); |
| 528 |
| 529 // Read data from the reader. Check that it successfuly reads empty data. |
| 530 std::string first_content; |
| 531 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &first_content)); |
| 532 EXPECT_EQ(0u, first_content.size()); |
| 533 |
| 534 // Create second instance and initialize it. |
| 535 // In this case, the file should be cached one. |
| 536 reader.reset(new DriveFileStreamReader( |
| 537 GetFileSystemGetter(), worker_thread_->message_loop_proxy().get())); |
| 538 EXPECT_FALSE(reader->IsInitialized()); |
| 539 |
| 540 error = net::ERR_FAILED; |
| 541 entry.reset(); |
| 542 { |
| 543 base::RunLoop run_loop; |
| 544 reader->Initialize( |
| 545 kDriveFile, |
| 546 net::HttpByteRange(), |
| 547 google_apis::test_util::CreateQuitCallback( |
| 548 &run_loop, |
| 549 google_apis::test_util::CreateCopyResultCallback(&error, &entry))); |
| 550 run_loop.Run(); |
| 551 } |
| 552 EXPECT_EQ(net::OK, error); |
| 553 ASSERT_TRUE(entry); |
| 554 EXPECT_TRUE(reader->IsInitialized()); |
| 555 |
| 556 // Read data from the reader, again. |
| 557 std::string second_content; |
| 558 ASSERT_EQ(net::OK, test_util::ReadAllData(reader.get(), &second_content)); |
| 559 EXPECT_EQ(0u, second_content.size()); |
| 560 } |
| 561 |
485 } // namespace drive | 562 } // namespace drive |
OLD | NEW |