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.
5 #include "chrome/browser/chromeos/drive/file_system/get_file_for_saving_operation.h"
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 "base/task_runner_util.h"
12 #include "chrome/browser/chromeos/drive/drive.pb.h"
13 #include "chrome/browser/chromeos/drive/file_errors.h"
14 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
15 #include "chrome/browser/chromeos/drive/file_write_watcher.h"
16 #include "chrome/browser/google_apis/test_util.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 namespace file_system {
24 // If OnCacheFileUploadNeededByOperation is called, records the local ID and
25 // calls |quit_closure|.
26 class TestObserver : public OperationObserver {
28 void set_quit_closure(const base::Closure& quit_closure) {
29 quit_closure_ = quit_closure;
32 const std::string& observerd_local_id() const {
33 return observed_local_id_;
36 // OperationObserver overrides.
37 virtual void OnDirectoryChangedByOperation(
38 const base::FilePath& path) OVERRIDE {}
40 virtual void OnCacheFileUploadNeededByOperation(
41 const std::string& local_id) OVERRIDE {
42 observed_local_id_ = local_id;
47 std::string observed_local_id_;
48 base::Closure quit_closure_;
53 class GetFileForSavingOperationTest : public OperationTestBase {
55 // FileWriteWatcher requires TYPE_IO message loop to run.
56 GetFileForSavingOperationTest()
57 : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) {
60 virtual void SetUp() OVERRIDE {
61 OperationTestBase::SetUp();
63 operation_.reset(new GetFileForSavingOperation(
64 blocking_task_runner(), &observer_, scheduler(), metadata(), cache(),
66 operation_->file_write_watcher_for_testing()->DisableDelayForTesting();
69 TestObserver observer_;
70 scoped_ptr<GetFileForSavingOperation> operation_;
73 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) {
74 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
75 ResourceEntry src_entry;
76 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
79 FileError error = FILE_ERROR_FAILED;
80 scoped_ptr<ResourceEntry> entry;
81 base::FilePath local_path;
82 operation_->GetFileForSaving(
84 google_apis::test_util::CreateCopyResultCallback(
85 &error, &local_path, &entry));
86 test_util::RunBlockingPoolTask();
88 // Checks that the file is retrieved.
89 EXPECT_EQ(FILE_ERROR_OK, error);
91 EXPECT_EQ(src_entry.resource_id(), entry->resource_id());
93 // Checks that it presents in cache and marked dirty.
95 FileCacheEntry cache_entry;
96 base::PostTaskAndReplyWithResult(
97 blocking_task_runner(),
99 base::Bind(&internal::FileCache::GetCacheEntry,
100 base::Unretained(cache()),
101 GetLocalId(drive_path),
103 google_apis::test_util::CreateCopyResultCallback(&success));
104 test_util::RunBlockingPoolTask();
105 EXPECT_TRUE(success);
106 EXPECT_TRUE(cache_entry.is_present());
107 EXPECT_TRUE(cache_entry.is_dirty());
109 // Write something to the cache and checks that the event is reported.
111 base::RunLoop run_loop;
112 observer_.set_quit_closure(run_loop.QuitClosure());
113 google_apis::test_util::WriteStringToFile(local_path, "hello");
115 EXPECT_EQ(GetLocalId(drive_path), observer_.observerd_local_id());
119 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
120 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
121 ResourceEntry src_entry;
122 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
123 GetLocalResourceEntry(drive_path, &src_entry));
125 // Run the operation.
126 FileError error = FILE_ERROR_FAILED;
127 scoped_ptr<ResourceEntry> entry;
128 base::FilePath local_path;
129 operation_->GetFileForSaving(
131 google_apis::test_util::CreateCopyResultCallback(
132 &error, &local_path, &entry));
133 test_util::RunBlockingPoolTask();
135 // Checks that the file is created and retrieved.
136 EXPECT_EQ(FILE_ERROR_OK, error);
137 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
139 EXPECT_TRUE(file_util::GetFileSize(local_path, &size));
143 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
144 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
145 ResourceEntry src_entry;
146 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
147 ASSERT_TRUE(src_entry.file_info().is_directory());
149 // Run the operation.
150 FileError error = FILE_ERROR_FAILED;
151 scoped_ptr<ResourceEntry> entry;
152 base::FilePath local_path;
153 operation_->GetFileForSaving(
155 google_apis::test_util::CreateCopyResultCallback(
156 &error, &local_path, &entry));
157 test_util::RunBlockingPoolTask();
159 // Checks that an error is returned.
160 EXPECT_EQ(FILE_ERROR_EXISTS, error);
163 } // namespace file_system