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_cache.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 "google_apis/drive/test_util.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 namespace file_system {
25 // If OnCacheFileUploadNeededByOperation is called, records the local ID and
26 // calls |quit_closure|.
27 class TestObserver : public OperationObserver {
29 void set_quit_closure(const base::Closure& quit_closure) {
30 quit_closure_ = quit_closure;
33 const std::string& observerd_local_id() const {
34 return observed_local_id_;
37 // OperationObserver overrides.
38 virtual void OnDirectoryChangedByOperation(
39 const base::FilePath& path) OVERRIDE {}
41 virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE {
42 observed_local_id_ = local_id;
43 if (!quit_closure_.is_null())
48 std::string observed_local_id_;
49 base::Closure quit_closure_;
54 class GetFileForSavingOperationTest : public OperationTestBase {
56 // FileWriteWatcher requires TYPE_IO message loop to run.
57 GetFileForSavingOperationTest()
58 : OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) {
61 virtual void SetUp() OVERRIDE {
62 OperationTestBase::SetUp();
64 operation_.reset(new GetFileForSavingOperation(
65 logger(), blocking_task_runner(), &observer_, scheduler(), metadata(),
66 cache(), temp_dir()));
67 operation_->file_write_watcher_for_testing()->DisableDelayForTesting();
70 TestObserver observer_;
71 scoped_ptr<GetFileForSavingOperation> operation_;
74 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) {
75 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
76 ResourceEntry src_entry;
77 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
80 FileError error = FILE_ERROR_FAILED;
81 scoped_ptr<ResourceEntry> entry;
82 base::FilePath local_path;
83 operation_->GetFileForSaving(
85 google_apis::test_util::CreateCopyResultCallback(
86 &error, &local_path, &entry));
87 test_util::RunBlockingPoolTask();
89 // Checks that the file is retrieved.
90 EXPECT_EQ(FILE_ERROR_OK, error);
92 EXPECT_EQ(src_entry.resource_id(), entry->resource_id());
94 // Checks that it presents in cache and marked dirty.
96 FileCacheEntry cache_entry;
97 base::PostTaskAndReplyWithResult(
98 blocking_task_runner(),
100 base::Bind(&internal::FileCache::GetCacheEntry,
101 base::Unretained(cache()),
102 GetLocalId(drive_path),
104 google_apis::test_util::CreateCopyResultCallback(&success));
105 test_util::RunBlockingPoolTask();
106 EXPECT_TRUE(success);
107 EXPECT_TRUE(cache_entry.is_present());
108 EXPECT_TRUE(cache_entry.is_dirty());
110 // Write something to the cache and checks that the event is reported.
112 base::RunLoop run_loop;
113 observer_.set_quit_closure(run_loop.QuitClosure());
114 google_apis::test_util::WriteStringToFile(local_path, "hello");
116 EXPECT_EQ(GetLocalId(drive_path), observer_.observerd_local_id());
120 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
121 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
122 ResourceEntry src_entry;
123 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
124 GetLocalResourceEntry(drive_path, &src_entry));
126 // Run the operation.
127 FileError error = FILE_ERROR_FAILED;
128 scoped_ptr<ResourceEntry> entry;
129 base::FilePath local_path;
130 operation_->GetFileForSaving(
132 google_apis::test_util::CreateCopyResultCallback(
133 &error, &local_path, &entry));
134 test_util::RunBlockingPoolTask();
136 // Checks that the file is created and retrieved.
137 EXPECT_EQ(FILE_ERROR_OK, error);
138 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
140 EXPECT_TRUE(base::GetFileSize(local_path, &size));
144 TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
145 base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
146 ResourceEntry src_entry;
147 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
148 ASSERT_TRUE(src_entry.file_info().is_directory());
150 // Run the operation.
151 FileError error = FILE_ERROR_FAILED;
152 scoped_ptr<ResourceEntry> entry;
153 base::FilePath local_path;
154 operation_->GetFileForSaving(
156 google_apis::test_util::CreateCopyResultCallback(
157 &error, &local_path, &entry));
158 test_util::RunBlockingPoolTask();
160 // Checks that an error is returned.
161 EXPECT_EQ(FILE_ERROR_EXISTS, error);
164 } // namespace file_system