10f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson// Copyright 2013 The Chromium Authors. All rights reserved. 20f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson// Use of this source code is governed by a BSD-style license that can be 30f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson// found in the LICENSE file. 40f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 55692b3b0303c55524ff206dc7840ffdb1fa47628Jesse Wilson#include "chrome/browser/chromeos/drive/file_system/get_file_for_saving_operation.h" 60f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 70f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "base/callback.h" 80f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "base/files/file_path.h" 90f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "base/files/file_util.h" 105692b3b0303c55524ff206dc7840ffdb1fa47628Jesse Wilson#include "base/run_loop.h" 110f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "base/task_runner_util.h" 120f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "chrome/browser/chromeos/drive/drive.pb.h" 130f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "chrome/browser/chromeos/drive/file_change.h" 140f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "chrome/browser/chromeos/drive/file_errors.h" 150f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" 160f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "chrome/browser/chromeos/drive/file_write_watcher.h" 170f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "content/public/test/test_utils.h" 180f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "google_apis/drive/test_util.h" 190f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson#include "testing/gtest/include/gtest/gtest.h" 200f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 210f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilsonnamespace drive { 220f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilsonnamespace file_system { 230f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 240f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilsonnamespace { 250f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 260f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson// If OnCacheFileUploadNeededByOperation is called, records the local ID and 270f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson// calls |quit_closure|. 280f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilsonclass TestDelegate : public OperationDelegate { 290f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson public: 300f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson void set_quit_closure(const base::Closure& quit_closure) { 310f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson quit_closure_ = quit_closure; 320f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson } 330f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 340f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson const std::string& updated_local_id() const { 350f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson return updated_local_id_; 360f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson } 370f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 380f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson // OperationDelegate overrides. 390f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE { 400f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson updated_local_id_ = local_id; 410f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson if (!quit_closure_.is_null()) 420f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson quit_closure_.Run(); 430f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson } 440f1cd359d28cd5c6899e0a6bd588735c3f9a7192Jesse Wilson 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