OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/file_system.h" | 5 #include "chrome/browser/chromeos/drive/file_system.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
17 #include "chrome/browser/chromeos/drive/change_list_loader.h" | 17 #include "chrome/browser/chromeos/drive/change_list_loader.h" |
18 #include "chrome/browser/chromeos/drive/drive.pb.h" | 18 #include "chrome/browser/chromeos/drive/drive.pb.h" |
19 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" | 19 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
20 #include "chrome/browser/chromeos/drive/file_system_util.h" | 20 #include "chrome/browser/chromeos/drive/file_system_util.h" |
21 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 21 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
22 #include "chrome/browser/chromeos/drive/mock_directory_change_observer.h" | 22 #include "chrome/browser/chromeos/drive/mock_directory_change_observer.h" |
23 #include "chrome/browser/chromeos/drive/mock_file_cache_observer.h" | 23 #include "chrome/browser/chromeos/drive/mock_file_cache_observer.h" |
24 #include "chrome/browser/chromeos/drive/sync_client.h" | |
24 #include "chrome/browser/chromeos/drive/test_util.h" | 25 #include "chrome/browser/chromeos/drive/test_util.h" |
25 #include "chrome/browser/google_apis/drive_api_parser.h" | 26 #include "chrome/browser/google_apis/drive_api_parser.h" |
26 #include "chrome/browser/google_apis/fake_drive_service.h" | 27 #include "chrome/browser/google_apis/fake_drive_service.h" |
27 #include "chrome/browser/google_apis/test_util.h" | 28 #include "chrome/browser/google_apis/test_util.h" |
28 #include "chrome/test/base/testing_profile.h" | 29 #include "chrome/test/base/testing_profile.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/test/test_browser_thread.h" | 31 #include "content/public/test/test_browser_thread.h" |
31 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
33 | 34 |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 | 113 |
113 file_system_.reset(new FileSystem(profile_.get(), | 114 file_system_.reset(new FileSystem(profile_.get(), |
114 cache_.get(), | 115 cache_.get(), |
115 fake_drive_service_.get(), | 116 fake_drive_service_.get(), |
116 scheduler_.get(), | 117 scheduler_.get(), |
117 resource_metadata_.get(), | 118 resource_metadata_.get(), |
118 blocking_task_runner_)); | 119 blocking_task_runner_)); |
119 file_system_->AddObserver(mock_directory_observer_.get()); | 120 file_system_->AddObserver(mock_directory_observer_.get()); |
120 file_system_->Initialize(); | 121 file_system_->Initialize(); |
121 | 122 |
123 // Disable delaying so that the sync starts immediately. | |
124 file_system_->sync_client_->set_delay_for_testing( | |
125 base::TimeDelta::FromSeconds(0)); | |
126 | |
122 FileError error = FILE_ERROR_FAILED; | 127 FileError error = FILE_ERROR_FAILED; |
123 resource_metadata_->Initialize( | 128 resource_metadata_->Initialize( |
124 google_apis::test_util::CreateCopyResultCallback(&error)); | 129 google_apis::test_util::CreateCopyResultCallback(&error)); |
125 google_apis::test_util::RunBlockingPoolTask(); | 130 google_apis::test_util::RunBlockingPoolTask(); |
126 ASSERT_EQ(FILE_ERROR_OK, error); | 131 ASSERT_EQ(FILE_ERROR_OK, error); |
127 } | 132 } |
128 | 133 |
129 virtual void TearDown() OVERRIDE { | 134 virtual void TearDown() OVERRIDE { |
130 ASSERT_TRUE(file_system_); | 135 ASSERT_TRUE(file_system_); |
131 file_system_.reset(); | 136 file_system_.reset(); |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
718 google_apis::test_util::RunBlockingPoolTask(); | 723 google_apis::test_util::RunBlockingPoolTask(); |
719 EXPECT_EQ(FILE_ERROR_OK, error); | 724 EXPECT_EQ(FILE_ERROR_OK, error); |
720 } | 725 } |
721 | 726 |
722 TEST_F(FileSystemTest, OpenAndCloseFile) { | 727 TEST_F(FileSystemTest, OpenAndCloseFile) { |
723 ASSERT_TRUE(LoadFullResourceList()); | 728 ASSERT_TRUE(LoadFullResourceList()); |
724 | 729 |
725 // The transfered file is cached and the change of "offline available" | 730 // The transfered file is cached and the change of "offline available" |
726 // attribute is notified. | 731 // attribute is notified. |
727 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( | 732 EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged( |
728 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1); | 733 Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(AtLeast(1)); |
729 | 734 |
730 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 735 const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
731 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot)); | 736 scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot)); |
732 const std::string& file_resource_id = entry->resource_id(); | 737 const std::string& file_resource_id = entry->resource_id(); |
733 const std::string& md5 = entry->file_specific_info().md5(); | 738 const std::string& md5 = entry->file_specific_info().md5(); |
734 | 739 |
735 // A dirty file is created on close. | |
736 EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(file_resource_id)) | |
737 .Times(1); | |
738 | |
739 // Open kFileInRoot ("drive/root/File 1.txt"). | 740 // Open kFileInRoot ("drive/root/File 1.txt"). |
740 FileError error = FILE_ERROR_FAILED; | 741 FileError error = FILE_ERROR_FAILED; |
741 base::FilePath file_path; | 742 base::FilePath file_path; |
742 file_system_->OpenFile( | 743 file_system_->OpenFile( |
743 kFileInRoot, | 744 kFileInRoot, |
744 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); | 745 google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); |
745 google_apis::test_util::RunBlockingPoolTask(); | 746 google_apis::test_util::RunBlockingPoolTask(); |
746 const base::FilePath opened_file_path = file_path; | 747 const base::FilePath opened_file_path = file_path; |
747 | 748 |
748 // Verify that the file was properly opened. | 749 // Verify that the file was properly opened. |
(...skipping 22 matching lines...) Expand all Loading... | |
771 EXPECT_TRUE(cache_entry.is_persistent()); | 772 EXPECT_TRUE(cache_entry.is_persistent()); |
772 | 773 |
773 base::FilePath cache_file_path; | 774 base::FilePath cache_file_path; |
774 cache_->GetFileOnUIThread(file_resource_id, md5, | 775 cache_->GetFileOnUIThread(file_resource_id, md5, |
775 google_apis::test_util::CreateCopyResultCallback( | 776 google_apis::test_util::CreateCopyResultCallback( |
776 &error, &cache_file_path)); | 777 &error, &cache_file_path)); |
777 google_apis::test_util::RunBlockingPoolTask(); | 778 google_apis::test_util::RunBlockingPoolTask(); |
778 EXPECT_EQ(FILE_ERROR_OK, error); | 779 EXPECT_EQ(FILE_ERROR_OK, error); |
779 EXPECT_EQ(cache_file_path, opened_file_path); | 780 EXPECT_EQ(cache_file_path, opened_file_path); |
780 | 781 |
782 // Write a new content. | |
783 const std::string kNewContent = kExpectedContent + kExpectedContent; | |
784 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path, | |
785 kNewContent)); | |
786 | |
781 // Close kFileInRoot ("drive/root/File 1.txt"). | 787 // Close kFileInRoot ("drive/root/File 1.txt"). |
782 file_system_->CloseFile( | 788 file_system_->CloseFile( |
783 kFileInRoot, | 789 kFileInRoot, |
784 google_apis::test_util::CreateCopyResultCallback(&error)); | 790 google_apis::test_util::CreateCopyResultCallback(&error)); |
785 google_apis::test_util::RunBlockingPoolTask(); | 791 google_apis::test_util::RunBlockingPoolTask(); |
786 | 792 |
787 // Verify that the file was properly closed. | 793 // Verify that the file was properly closed. |
788 EXPECT_EQ(FILE_ERROR_OK, error); | 794 EXPECT_EQ(FILE_ERROR_OK, error); |
789 | 795 |
790 // Verify that the cache state was changed as expected. | 796 // Verify that the file was synced as expected. |
791 EXPECT_TRUE(GetCacheEntryFromOriginThread(file_resource_id, md5, | 797 entry = GetResourceEntryByPathSync(kFileInRoot); |
kinaba
2013/06/11 07:08:37
GetResourceEntryByPathSync ends up with local reso
hashimoto
2013/06/11 07:37:57
Good catch, I should have checked the change stamp
| |
792 &cache_entry)); | 798 ASSERT_TRUE(entry); |
793 EXPECT_TRUE(cache_entry.is_present()); | 799 EXPECT_EQ(static_cast<int>(kNewContent.size()), entry->file_info().size()); |
794 EXPECT_TRUE(cache_entry.is_dirty()); | |
795 EXPECT_TRUE(cache_entry.is_persistent()); | |
796 | 800 |
797 // Try to close the same file twice. | 801 // Try to close the same file twice. |
798 file_system_->CloseFile( | 802 file_system_->CloseFile( |
799 kFileInRoot, | 803 kFileInRoot, |
800 google_apis::test_util::CreateCopyResultCallback(&error)); | 804 google_apis::test_util::CreateCopyResultCallback(&error)); |
801 google_apis::test_util::RunBlockingPoolTask(); | 805 google_apis::test_util::RunBlockingPoolTask(); |
802 | 806 |
803 // It must fail. | 807 // It must fail. |
804 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 808 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
805 } | 809 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
855 entry->resource_id(), | 859 entry->resource_id(), |
856 entry->file_specific_info().md5(), | 860 entry->file_specific_info().md5(), |
857 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); | 861 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); |
858 google_apis::test_util::RunBlockingPoolTask(); | 862 google_apis::test_util::RunBlockingPoolTask(); |
859 | 863 |
860 EXPECT_TRUE(success); | 864 EXPECT_TRUE(success); |
861 EXPECT_FALSE(cache_entry.is_mounted()); | 865 EXPECT_FALSE(cache_entry.is_mounted()); |
862 } | 866 } |
863 | 867 |
864 } // namespace drive | 868 } // namespace drive |
OLD | NEW |