get_file_for_saving_operation_unittest.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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_operation.h" 6 7#include "base/callback.h" 8#include "base/files/file_path.h" 9#include "base/files/file_util.h" 10#include "base/run_loop.h" 11#include "base/task_runner_util.h" 12#include "chrome/browser/chromeos/drive/drive.pb.h" 13#include "chrome/browser/chromeos/drive/file_change.h" 14#include "chrome/browser/chromeos/drive/file_errors.h" 15#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" 16#include "chrome/browser/chromeos/drive/file_write_watcher.h" 17#include "content/public/test/test_utils.h" 18#include "google_apis/drive/test_util.h" 19#include "testing/gtest/include/gtest/gtest.h" 20 21namespace drive { 22namespace file_system { 23 24namespace { 25 26// If OnCacheFileUploadNeededByOperation is called, records the local ID and 27// calls |quit_closure|. 28class TestDelegate : public OperationDelegate { 29 public: 30 void set_quit_closure(const base::Closure& quit_closure) { 31 quit_closure_ = quit_closure; 32 } 33 34 const std::string& updated_local_id() const { 35 return updated_local_id_; 36 } 37 38 // OperationDelegate overrides. 39 virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE { 40 updated_local_id_ = local_id; 41 if (!quit_closure_.is_null()) 42 quit_closure_.Run(); 43 } 44 45 private: 46 std::string updated_local_id_; 47 base::Closure quit_closure_; 48}; 49 50} // namespace 51 52class 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 logger(), blocking_task_runner(), &delegate_, scheduler(), metadata(), 64 cache(), temp_dir())); 65 operation_->file_write_watcher_for_testing()->DisableDelayForTesting(); 66 } 67 68 TestDelegate delegate_; 69 scoped_ptr<GetFileForSavingOperation> operation_; 70}; 71 72TEST_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 content::RunAllBlockingPoolTasksUntilIdle(); 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 EXPECT_TRUE(entry->file_specific_info().cache_state().is_present()); 94 EXPECT_TRUE(entry->file_specific_info().cache_state().is_dirty()); 95 96 // Write something to the cache and checks that the event is reported. 97 { 98 base::RunLoop run_loop; 99 delegate_.set_quit_closure(run_loop.QuitClosure()); 100 google_apis::test_util::WriteStringToFile(local_path, "hello"); 101 run_loop.Run(); 102 EXPECT_EQ(GetLocalId(drive_path), delegate_.updated_local_id()); 103 } 104} 105 106TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) { 107 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt")); 108 ResourceEntry src_entry; 109 ASSERT_EQ(FILE_ERROR_NOT_FOUND, 110 GetLocalResourceEntry(drive_path, &src_entry)); 111 112 // Run the operation. 113 FileError error = FILE_ERROR_FAILED; 114 scoped_ptr<ResourceEntry> entry; 115 base::FilePath local_path; 116 operation_->GetFileForSaving( 117 drive_path, 118 google_apis::test_util::CreateCopyResultCallback( 119 &error, &local_path, &entry)); 120 content::RunAllBlockingPoolTasksUntilIdle(); 121 122 // Checks that the file is created and retrieved. 123 EXPECT_EQ(FILE_ERROR_OK, error); 124 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry)); 125 int64 size = -1; 126 EXPECT_TRUE(base::GetFileSize(local_path, &size)); 127 EXPECT_EQ(0, size); 128} 129 130TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) { 131 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1")); 132 ResourceEntry src_entry; 133 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry)); 134 ASSERT_TRUE(src_entry.file_info().is_directory()); 135 136 // Run the operation. 137 FileError error = FILE_ERROR_FAILED; 138 scoped_ptr<ResourceEntry> entry; 139 base::FilePath local_path; 140 operation_->GetFileForSaving( 141 drive_path, 142 google_apis::test_util::CreateCopyResultCallback( 143 &error, &local_path, &entry)); 144 content::RunAllBlockingPoolTasksUntilIdle(); 145 146 // Checks that an error is returned. 147 EXPECT_EQ(FILE_ERROR_EXISTS, error); 148} 149 150} // namespace file_system 151} // namespace drive 152