Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system / copy_operation_unittest.cc
blobd30c333853b9f46bebeb0efb4ae2360998a8090e
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/copy_operation.h"
7 #include "base/task_runner_util.h"
8 #include "chrome/browser/chromeos/drive/file_cache.h"
9 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
10 #include "chrome/browser/chromeos/drive/file_system_util.h"
11 #include "chrome/browser/drive/drive_api_util.h"
12 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "google_apis/drive/test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace drive {
17 namespace file_system {
19 class CopyOperationTest : public OperationTestBase {
20 protected:
21 virtual void SetUp() OVERRIDE {
22 OperationTestBase::SetUp();
23 operation_.reset(new CopyOperation(
24 blocking_task_runner(),
25 observer(),
26 scheduler(),
27 metadata(),
28 cache(),
29 util::GetIdentityResourceIdCanonicalizer()));
32 scoped_ptr<CopyOperation> operation_;
35 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_RegularFile) {
36 const base::FilePath local_src_path = temp_dir().AppendASCII("local.txt");
37 const base::FilePath remote_dest_path(
38 FILE_PATH_LITERAL("drive/root/remote.txt"));
40 // Prepare a local file.
41 ASSERT_TRUE(
42 google_apis::test_util::WriteStringToFile(local_src_path, "hello"));
43 // Confirm that the remote file does not exist.
44 ResourceEntry entry;
45 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
46 GetLocalResourceEntry(remote_dest_path, &entry));
48 // Transfer the local file to Drive.
49 FileError error = FILE_ERROR_FAILED;
50 operation_->TransferFileFromLocalToRemote(
51 local_src_path,
52 remote_dest_path,
53 google_apis::test_util::CreateCopyResultCallback(&error));
54 test_util::RunBlockingPoolTask();
55 EXPECT_EQ(FILE_ERROR_OK, error);
57 // TransferFileFromLocalToRemote stores a copy of the local file in the cache,
58 // marks it dirty and requests the observer to upload the file.
59 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
60 EXPECT_EQ(1U, observer()->upload_needed_local_ids().count(
61 GetLocalId(remote_dest_path)));
62 FileCacheEntry cache_entry;
63 bool found = false;
64 base::PostTaskAndReplyWithResult(
65 blocking_task_runner(),
66 FROM_HERE,
67 base::Bind(&internal::FileCache::GetCacheEntry,
68 base::Unretained(cache()),
69 GetLocalId(remote_dest_path),
70 &cache_entry),
71 google_apis::test_util::CreateCopyResultCallback(&found));
72 test_util::RunBlockingPoolTask();
73 EXPECT_TRUE(found);
74 EXPECT_TRUE(cache_entry.is_present());
75 EXPECT_TRUE(cache_entry.is_dirty());
77 EXPECT_EQ(1U, observer()->get_changed_paths().size());
78 EXPECT_TRUE(observer()->get_changed_paths().count(
79 remote_dest_path.DirName()));
82 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_QuotaCheck) {
83 const base::FilePath local_src_path = temp_dir().AppendASCII("local.txt");
84 const base::FilePath remote_dest_path(
85 FILE_PATH_LITERAL("drive/root/remote.txt"));
87 const size_t kFileSize = 10;
89 // Prepare a local file.
90 ASSERT_TRUE(
91 google_apis::test_util::WriteStringToFile(local_src_path,
92 std::string(kFileSize, 'a')));
94 // Set insufficient quota.
95 fake_service()->SetQuotaValue(100, 100 + kFileSize - 1);
97 // Transfer the local file to Drive.
98 FileError error = FILE_ERROR_FAILED;
99 operation_->TransferFileFromLocalToRemote(
100 local_src_path,
101 remote_dest_path,
102 google_apis::test_util::CreateCopyResultCallback(&error));
103 test_util::RunBlockingPoolTask();
104 EXPECT_EQ(FILE_ERROR_NO_SERVER_SPACE, error);
106 // Set sufficient quota.
107 fake_service()->SetQuotaValue(100, 100 + kFileSize);
109 // Transfer the local file to Drive.
110 error = FILE_ERROR_FAILED;
111 operation_->TransferFileFromLocalToRemote(
112 local_src_path,
113 remote_dest_path,
114 google_apis::test_util::CreateCopyResultCallback(&error));
115 test_util::RunBlockingPoolTask();
116 EXPECT_EQ(FILE_ERROR_OK, error);
119 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_HostedDocument) {
120 const base::FilePath local_src_path = temp_dir().AppendASCII("local.gdoc");
121 const base::FilePath remote_dest_path(FILE_PATH_LITERAL(
122 "drive/root/Directory 1/Document 1 excludeDir-test.gdoc"));
124 // Prepare a local file, which is a json file of a hosted document, which
125 // matches "Document 1" in root_feed.json.
126 ASSERT_TRUE(util::CreateGDocFile(
127 local_src_path,
128 GURL("https://3_document_self_link/document:5_document_resource_id"),
129 "document:5_document_resource_id"));
131 ResourceEntry entry;
132 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
133 GetLocalResourceEntry(remote_dest_path, &entry));
135 // Transfer the local file to Drive.
136 FileError error = FILE_ERROR_FAILED;
137 operation_->TransferFileFromLocalToRemote(
138 local_src_path,
139 remote_dest_path,
140 google_apis::test_util::CreateCopyResultCallback(&error));
141 test_util::RunBlockingPoolTask();
142 EXPECT_EQ(FILE_ERROR_OK, error);
144 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
146 EXPECT_EQ(1U, observer()->get_changed_paths().size());
147 EXPECT_TRUE(
148 observer()->get_changed_paths().count(remote_dest_path.DirName()));
152 TEST_F(CopyOperationTest, CopyNotExistingFile) {
153 base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
154 base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Test.log"));
156 ResourceEntry entry;
157 ASSERT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
159 FileError error = FILE_ERROR_OK;
160 operation_->Copy(src_path,
161 dest_path,
162 false,
163 google_apis::test_util::CreateCopyResultCallback(&error));
164 test_util::RunBlockingPoolTask();
165 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
167 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
168 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
169 EXPECT_TRUE(observer()->get_changed_paths().empty());
172 TEST_F(CopyOperationTest, CopyFileToNonExistingDirectory) {
173 base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
174 base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Dummy/Test.log"));
176 ResourceEntry entry;
177 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
178 ASSERT_EQ(FILE_ERROR_NOT_FOUND,
179 GetLocalResourceEntry(dest_path.DirName(), &entry));
181 FileError error = FILE_ERROR_OK;
182 operation_->Copy(src_path,
183 dest_path,
184 false,
185 google_apis::test_util::CreateCopyResultCallback(&error));
186 test_util::RunBlockingPoolTask();
187 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
189 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
190 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
191 EXPECT_TRUE(observer()->get_changed_paths().empty());
194 // Test the case where the parent of the destination path is an existing file,
195 // not a directory.
196 TEST_F(CopyOperationTest, CopyFileToInvalidPath) {
197 base::FilePath src_path(FILE_PATH_LITERAL(
198 "drive/root/Document 1 excludeDir-test.gdoc"));
199 base::FilePath dest_path(FILE_PATH_LITERAL(
200 "drive/root/Duplicate Name.txt/Document 1 excludeDir-test.gdoc"));
202 ResourceEntry entry;
203 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
204 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path.DirName(), &entry));
205 ASSERT_FALSE(entry.file_info().is_directory());
207 FileError error = FILE_ERROR_OK;
208 operation_->Copy(src_path,
209 dest_path,
210 false,
211 google_apis::test_util::CreateCopyResultCallback(&error));
212 test_util::RunBlockingPoolTask();
213 EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
215 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
216 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
217 EXPECT_TRUE(observer()->get_changed_paths().empty());
220 TEST_F(CopyOperationTest, CopyDirectory) {
221 base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
222 base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/New Directory"));
224 ResourceEntry entry;
225 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
226 ASSERT_TRUE(entry.file_info().is_directory());
227 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path.DirName(), &entry));
228 ASSERT_TRUE(entry.file_info().is_directory());
230 FileError error = FILE_ERROR_OK;
231 operation_->Copy(src_path,
232 dest_path,
233 false,
234 google_apis::test_util::CreateCopyResultCallback(&error));
235 test_util::RunBlockingPoolTask();
236 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, error);
239 TEST_F(CopyOperationTest, PreserveLastModified) {
240 base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
241 base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/File 2.txt"));
243 ResourceEntry entry;
244 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
245 ASSERT_EQ(FILE_ERROR_OK,
246 GetLocalResourceEntry(dest_path.DirName(), &entry));
248 FileError error = FILE_ERROR_OK;
249 operation_->Copy(src_path,
250 dest_path,
251 true, // Preserve last modified.
252 google_apis::test_util::CreateCopyResultCallback(&error));
253 test_util::RunBlockingPoolTask();
254 EXPECT_EQ(FILE_ERROR_OK, error);
256 ResourceEntry entry2;
257 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
258 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &entry2));
259 EXPECT_EQ(entry.file_info().last_modified(),
260 entry2.file_info().last_modified());
263 } // namespace file_system
264 } // namespace drive