OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/drive/file_system/get_file_for_saving_operatio
n.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 12 #include "chrome/browser/chromeos/drive/file_errors.h" |
| 13 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
| 14 #include "chrome/browser/chromeos/drive/file_write_watcher.h" |
| 15 #include "chrome/browser/google_apis/test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace drive { |
| 19 namespace file_system { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // If OnCacheFileUploadNeededByOperation is called, records the resource ID and |
| 24 // calls |quit_closure|. |
| 25 class TestObserver : public OperationObserver { |
| 26 public: |
| 27 void set_quit_closure(const base::Closure& quit_closure) { |
| 28 quit_closure_ = quit_closure; |
| 29 } |
| 30 |
| 31 const std::string& observerd_resource_id() const { |
| 32 return observed_resource_id_; |
| 33 } |
| 34 |
| 35 // OperationObserver overrides. |
| 36 virtual void OnDirectoryChangedByOperation( |
| 37 const base::FilePath& path) OVERRIDE {} |
| 38 |
| 39 virtual void OnCacheFileUploadNeededByOperation( |
| 40 const std::string& resource_id) OVERRIDE { |
| 41 observed_resource_id_ = resource_id; |
| 42 quit_closure_.Run(); |
| 43 } |
| 44 |
| 45 private: |
| 46 std::string observed_resource_id_; |
| 47 base::Closure quit_closure_; |
| 48 }; |
| 49 |
| 50 } // namespace |
| 51 |
| 52 class GetFileForSavingOperationTest : public OperationTestBase { |
| 53 protected: |
| 54 // FileWriteWatcher requires TYPE_IO message loop to run. |
| 55 GetFileForSavingOperationTest() |
| 56 : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
| 57 } |
| 58 |
| 59 virtual void SetUp() OVERRIDE { |
| 60 OperationTestBase::SetUp(); |
| 61 |
| 62 operation_.reset(new GetFileForSavingOperation( |
| 63 blocking_task_runner(), &observer_, scheduler(), metadata(), cache(), |
| 64 temp_dir())); |
| 65 operation_->file_write_watcher_for_testing()->DisableDelayForTesting(); |
| 66 } |
| 67 |
| 68 TestObserver observer_; |
| 69 scoped_ptr<GetFileForSavingOperation> operation_; |
| 70 }; |
| 71 |
| 72 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) { |
| 73 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 74 ResourceEntry src_entry; |
| 75 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry)); |
| 76 |
| 77 // Run the operation. |
| 78 FileError error = FILE_ERROR_FAILED; |
| 79 scoped_ptr<ResourceEntry> entry; |
| 80 base::FilePath local_path; |
| 81 operation_->GetFileForSaving( |
| 82 drive_path, |
| 83 google_apis::test_util::CreateCopyResultCallback( |
| 84 &error, &local_path, &entry)); |
| 85 test_util::RunBlockingPoolTask(); |
| 86 |
| 87 // Checks that the file is retrieved. |
| 88 EXPECT_EQ(FILE_ERROR_OK, error); |
| 89 ASSERT_TRUE(entry); |
| 90 EXPECT_EQ(src_entry.resource_id(), entry->resource_id()); |
| 91 |
| 92 // Checks that it presents in cache and marked dirty. |
| 93 bool success = false; |
| 94 FileCacheEntry cache_entry; |
| 95 cache()->GetCacheEntryOnUIThread( |
| 96 src_entry.resource_id(), |
| 97 google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry)); |
| 98 test_util::RunBlockingPoolTask(); |
| 99 EXPECT_TRUE(success); |
| 100 EXPECT_TRUE(cache_entry.is_present()); |
| 101 EXPECT_TRUE(cache_entry.is_dirty()); |
| 102 |
| 103 // Write something to the cache and checks that the event is reported. |
| 104 { |
| 105 base::RunLoop run_loop; |
| 106 observer_.set_quit_closure(run_loop.QuitClosure()); |
| 107 google_apis::test_util::WriteStringToFile(local_path, "hello"); |
| 108 run_loop.Run(); |
| 109 EXPECT_EQ(entry->resource_id(), observer_.observerd_resource_id()); |
| 110 } |
| 111 } |
| 112 |
| 113 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) { |
| 114 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt")); |
| 115 ResourceEntry src_entry; |
| 116 ASSERT_EQ(FILE_ERROR_NOT_FOUND, |
| 117 GetLocalResourceEntry(drive_path, &src_entry)); |
| 118 |
| 119 // Run the operation. |
| 120 FileError error = FILE_ERROR_FAILED; |
| 121 scoped_ptr<ResourceEntry> entry; |
| 122 base::FilePath local_path; |
| 123 operation_->GetFileForSaving( |
| 124 drive_path, |
| 125 google_apis::test_util::CreateCopyResultCallback( |
| 126 &error, &local_path, &entry)); |
| 127 test_util::RunBlockingPoolTask(); |
| 128 |
| 129 // Checks that the file is created and retrieved. |
| 130 EXPECT_EQ(FILE_ERROR_OK, error); |
| 131 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry)); |
| 132 int64 size = -1; |
| 133 EXPECT_TRUE(file_util::GetFileSize(local_path, &size)); |
| 134 EXPECT_EQ(0, size); |
| 135 } |
| 136 |
| 137 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) { |
| 138 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1")); |
| 139 ResourceEntry src_entry; |
| 140 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry)); |
| 141 ASSERT_TRUE(src_entry.file_info().is_directory()); |
| 142 |
| 143 // Run the operation. |
| 144 FileError error = FILE_ERROR_FAILED; |
| 145 scoped_ptr<ResourceEntry> entry; |
| 146 base::FilePath local_path; |
| 147 operation_->GetFileForSaving( |
| 148 drive_path, |
| 149 google_apis::test_util::CreateCopyResultCallback( |
| 150 &error, &local_path, &entry)); |
| 151 test_util::RunBlockingPoolTask(); |
| 152 |
| 153 // Checks that an error is returned. |
| 154 EXPECT_EQ(FILE_ERROR_EXISTS, error); |
| 155 } |
| 156 |
| 157 } // namespace file_system |
| 158 } // namespace drive |
OLD | NEW |