- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / copy_operation_unittest.cc
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/copy_operation.h"
6
7 #include "base/file_util.h"
8 #include "base/task_runner_util.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 "chrome/browser/google_apis/test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace drive {
17 namespace file_system {
18
19 class CopyOperationTest : public OperationTestBase {
20  protected:
21   virtual void SetUp() OVERRIDE {
22    OperationTestBase::SetUp();
23    operation_.reset(new CopyOperation(blocking_task_runner(),
24                                       observer(),
25                                       scheduler(),
26                                       metadata(),
27                                       cache(),
28                                       fake_service(),
29                                       temp_dir()));
30   }
31
32   scoped_ptr<CopyOperation> operation_;
33 };
34
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"));
39
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));
47
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);
56
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());
76
77   EXPECT_EQ(1U, observer()->get_changed_paths().size());
78   EXPECT_TRUE(observer()->get_changed_paths().count(
79       remote_dest_path.DirName()));
80 }
81
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"));
86
87   const size_t kFileSize = 10;
88
89   // Prepare a local file.
90   ASSERT_TRUE(
91       google_apis::test_util::WriteStringToFile(local_src_path,
92                                                 std::string(kFileSize, 'a')));
93
94   // Set insufficient quota.
95   fake_service()->SetQuotaValue(100, 100 + kFileSize - 1);
96
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);
105
106   // Set sufficient quota.
107   fake_service()->SetQuotaValue(100, 100 + kFileSize);
108
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);
117 }
118
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"));
123
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"));
130
131   ResourceEntry entry;
132   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
133             GetLocalResourceEntry(remote_dest_path, &entry));
134
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);
143
144   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
145
146   // We added a file to the Drive root and then moved to "Directory 1".
147   if (util::IsDriveV2ApiEnabled()) {
148     EXPECT_EQ(1U, observer()->get_changed_paths().size());
149     EXPECT_TRUE(
150         observer()->get_changed_paths().count(remote_dest_path.DirName()));
151   } else {
152     EXPECT_EQ(2U, observer()->get_changed_paths().size());
153     EXPECT_TRUE(observer()->get_changed_paths().count(
154         base::FilePath(FILE_PATH_LITERAL("drive/root"))));
155     EXPECT_TRUE(observer()->get_changed_paths().count(
156         remote_dest_path.DirName()));
157   }
158 }
159
160
161 TEST_F(CopyOperationTest, CopyNotExistingFile) {
162   base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Dummy file.txt"));
163   base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Test.log"));
164
165   ResourceEntry entry;
166   ASSERT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
167
168   FileError error = FILE_ERROR_OK;
169   operation_->Copy(src_path,
170                    dest_path,
171                    false,
172                    google_apis::test_util::CreateCopyResultCallback(&error));
173   test_util::RunBlockingPoolTask();
174   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
175
176   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
177   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
178   EXPECT_TRUE(observer()->get_changed_paths().empty());
179 }
180
181 TEST_F(CopyOperationTest, CopyFileToNonExistingDirectory) {
182   base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
183   base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/Dummy/Test.log"));
184
185   ResourceEntry entry;
186   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
187   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
188             GetLocalResourceEntry(dest_path.DirName(), &entry));
189
190   FileError error = FILE_ERROR_OK;
191   operation_->Copy(src_path,
192                    dest_path,
193                    false,
194                    google_apis::test_util::CreateCopyResultCallback(&error));
195   test_util::RunBlockingPoolTask();
196   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
197
198   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
199   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
200   EXPECT_TRUE(observer()->get_changed_paths().empty());
201 }
202
203 // Test the case where the parent of the destination path is an existing file,
204 // not a directory.
205 TEST_F(CopyOperationTest, CopyFileToInvalidPath) {
206   base::FilePath src_path(FILE_PATH_LITERAL(
207       "drive/root/Document 1 excludeDir-test.gdoc"));
208   base::FilePath dest_path(FILE_PATH_LITERAL(
209       "drive/root/Duplicate Name.txt/Document 1 excludeDir-test.gdoc"));
210
211   ResourceEntry entry;
212   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
213   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path.DirName(), &entry));
214   ASSERT_FALSE(entry.file_info().is_directory());
215
216   FileError error = FILE_ERROR_OK;
217   operation_->Copy(src_path,
218                    dest_path,
219                    false,
220                    google_apis::test_util::CreateCopyResultCallback(&error));
221   test_util::RunBlockingPoolTask();
222   EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
223
224   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
225   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
226   EXPECT_TRUE(observer()->get_changed_paths().empty());
227 }
228
229 TEST_F(CopyOperationTest, CopyDirectory) {
230   base::FilePath src_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
231   base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/New Directory"));
232
233   ResourceEntry entry;
234   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
235   ASSERT_TRUE(entry.file_info().is_directory());
236   ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path.DirName(), &entry));
237   ASSERT_TRUE(entry.file_info().is_directory());
238
239   FileError error = FILE_ERROR_OK;
240   operation_->Copy(src_path,
241                    dest_path,
242                    false,
243                    google_apis::test_util::CreateCopyResultCallback(&error));
244   test_util::RunBlockingPoolTask();
245   EXPECT_EQ(FILE_ERROR_NOT_A_FILE, error);
246 }
247
248 TEST_F(CopyOperationTest, PreserveLastModified) {
249   // Preserve last modified feature is only available on Drive API v2.
250   if (util::IsDriveV2ApiEnabled()) {
251     base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
252     base::FilePath dest_path(FILE_PATH_LITERAL("drive/root/File 2.txt"));
253
254     ResourceEntry entry;
255     ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
256     ASSERT_EQ(FILE_ERROR_OK,
257               GetLocalResourceEntry(dest_path.DirName(), &entry));
258
259     FileError error = FILE_ERROR_OK;
260     operation_->Copy(src_path,
261                      dest_path,
262                      true,  // Preserve last modified.
263                      google_apis::test_util::CreateCopyResultCallback(&error));
264     test_util::RunBlockingPoolTask();
265     EXPECT_EQ(FILE_ERROR_OK, error);
266
267     ResourceEntry entry2;
268     EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
269     EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &entry2));
270     EXPECT_EQ(entry.file_info().last_modified(),
271               entry2.file_info().last_modified());
272   }
273 }
274
275 }  // namespace file_system
276 }  // namespace drive