1 // Copyright (c) 2012 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/drive_resource_metadata.h"
12 #include "base/sequenced_task_runner.h"
13 #include "base/string_number_conversions.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "base/message_loop.h"
16 #include "chrome/browser/chromeos/drive/drive.pb.h"
17 #include "chrome/browser/chromeos/drive/drive_cache.h"
18 #include "chrome/browser/chromeos/drive/drive_files.h"
19 #include "chrome/browser/chromeos/drive/drive_test_util.h"
20 #include "chrome/browser/google_apis/gdata_util.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "testing/gtest/include/gtest/gtest.h"
28 // The root directory resource ID for WAPI.
29 // TODO(haruki): Make Drive API equivalent work. http://crbug.com/157114
30 const char kTestRootDirectoryResourceId
[] = "folder:testroot";
32 // See drive.proto for the difference between the two URLs.
33 const char kResumableEditMediaUrl
[] = "http://resumable-edit-media/";
34 const char kResumableCreateMediaUrl
[] = "http://resumable-create-media/";
36 // Callback for DriveResourceMetadata::InitFromDB.
37 void InitFromDBCallback(DriveFileError expected_error
,
38 DriveFileError actual_error
) {
39 EXPECT_EQ(expected_error
, actual_error
);
42 // Callback for DriveResourceMetadata::ReadDirectoryByPath.
43 void ReadDirectoryByPathCallback(
44 scoped_ptr
<DriveEntryProtoVector
>* result
,
46 scoped_ptr
<DriveEntryProtoVector
> entries
) {
47 EXPECT_EQ(DRIVE_FILE_OK
, error
);
48 *result
= entries
.Pass();
53 class DriveResourceMetadataTest
: public testing::Test
{
55 DriveResourceMetadataTest();
58 DriveResourceMetadata resource_metadata_
;
61 // Creates the following files/directories
70 // drive/dir1/dir3/file9
71 // drive/dir1/dir3/file10
74 // Add a directory to |parent| and return that directory. The name and
75 // resource_id are determined by the incrementing counter |sequence_id|.
76 DriveDirectory
* AddDirectory(DriveDirectory
* parent
, int sequence_id
);
78 // Add a file to |parent| and return that file. The name and
79 // resource_id are determined by the incrementing counter |sequence_id|.
80 DriveFile
* AddFile(DriveDirectory
* parent
, int sequence_id
);
82 MessageLoopForUI message_loop_
;
83 content::TestBrowserThread ui_thread_
;
86 DriveResourceMetadataTest::DriveResourceMetadataTest()
87 : ui_thread_(content::BrowserThread::UI
, &message_loop_
) {
91 void DriveResourceMetadataTest::Init() {
92 resource_metadata_
.InitializeRootEntry(kTestRootDirectoryResourceId
);
95 DriveDirectory
* dir1
= AddDirectory(resource_metadata_
.root(), sequence_id
++);
96 DriveDirectory
* dir2
= AddDirectory(resource_metadata_
.root(), sequence_id
++);
97 DriveDirectory
* dir3
= AddDirectory(dir1
, sequence_id
++);
99 AddFile(dir1
, sequence_id
++);
100 AddFile(dir1
, sequence_id
++);
102 AddFile(dir2
, sequence_id
++);
103 AddFile(dir2
, sequence_id
++);
104 AddFile(dir2
, sequence_id
++);
106 AddFile(dir3
, sequence_id
++);
107 AddFile(dir3
, sequence_id
++);
110 DriveDirectory
* DriveResourceMetadataTest::AddDirectory(DriveDirectory
* parent
,
112 scoped_ptr
<DriveDirectory
> dir
= resource_metadata_
.CreateDriveDirectory();
113 const std::string dir_name
= "dir" + base::IntToString(sequence_id
);
114 const std::string resource_id
= std::string("dir_resource_id:") + dir_name
;
115 dir
->set_title(dir_name
);
116 dir
->set_resource_id(resource_id
);
118 parent
->AddEntry(dir
.get());
120 return dir
.release();
123 DriveFile
* DriveResourceMetadataTest::AddFile(DriveDirectory
* parent
,
125 scoped_ptr
<DriveFile
> file
= resource_metadata_
.CreateDriveFile();
126 const std::string title
= "file" + base::IntToString(sequence_id
);
127 const std::string resource_id
= std::string("file_resource_id:") + title
;
128 file
->set_title(title
);
129 file
->set_resource_id(resource_id
);
130 file
->set_file_md5(std::string("file_md5:") + title
);
132 parent
->AddEntry(file
.get());
134 return file
.release();
137 TEST_F(DriveResourceMetadataTest
, VersionCheck
) {
138 // Set up the root directory.
139 DriveRootDirectoryProto proto
;
140 DriveEntryProto
* mutable_entry
=
141 proto
.mutable_drive_directory()->mutable_drive_entry();
142 mutable_entry
->mutable_file_info()->set_is_directory(true);
143 mutable_entry
->set_resource_id(kTestRootDirectoryResourceId
);
144 mutable_entry
->set_upload_url(kResumableCreateMediaUrl
);
145 mutable_entry
->set_title("drive");
147 DriveResourceMetadata resource_metadata
;
149 std::string serialized_proto
;
150 ASSERT_TRUE(proto
.SerializeToString(&serialized_proto
));
151 // This should fail as the version is emtpy.
152 ASSERT_FALSE(resource_metadata
.ParseFromString(serialized_proto
));
154 // Set an older version, and serialize.
155 proto
.set_version(kProtoVersion
- 1);
156 ASSERT_TRUE(proto
.SerializeToString(&serialized_proto
));
157 // This should fail as the version is older.
158 ASSERT_FALSE(resource_metadata
.ParseFromString(serialized_proto
));
160 // Set the current version, and serialize.
161 proto
.set_version(kProtoVersion
);
162 ASSERT_TRUE(proto
.SerializeToString(&serialized_proto
));
163 // This should succeed as the version matches the current number.
164 ASSERT_TRUE(resource_metadata
.ParseFromString(serialized_proto
));
166 // Set a newer version, and serialize.
167 proto
.set_version(kProtoVersion
+ 1);
168 ASSERT_TRUE(proto
.SerializeToString(&serialized_proto
));
169 // This should fail as the version is newer.
170 ASSERT_FALSE(resource_metadata
.ParseFromString(serialized_proto
));
173 TEST_F(DriveResourceMetadataTest
, GetEntryByResourceId_RootDirectory
) {
174 DriveResourceMetadata resource_metadata
;
175 resource_metadata
.InitializeRootEntry(kTestRootDirectoryResourceId
);
176 EXPECT_EQ(kTestRootDirectoryResourceId
,
177 resource_metadata
.root()->resource_id());
178 // Look up the root directory by its resource ID.
179 DriveEntry
* entry
= resource_metadata
.GetEntryByResourceId(
180 resource_metadata
.root()->resource_id());
182 EXPECT_EQ(kTestRootDirectoryResourceId
, entry
->resource_id());
185 TEST_F(DriveResourceMetadataTest
, GetEntryInfoByResourceId
) {
186 // Confirm that an existing file is found.
187 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
188 FilePath drive_file_path
;
189 scoped_ptr
<DriveEntryProto
> entry_proto
;
190 resource_metadata_
.GetEntryInfoByResourceId(
191 "file_resource_id:file4",
192 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
193 &error
, &drive_file_path
, &entry_proto
));
194 google_apis::test_util::RunBlockingPoolTask();
195 EXPECT_EQ(DRIVE_FILE_OK
, error
);
196 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"), drive_file_path
);
197 ASSERT_TRUE(entry_proto
.get());
198 EXPECT_EQ("file4", entry_proto
->base_name());
200 // Confirm that a non existing file is not found.
201 error
= DRIVE_FILE_ERROR_FAILED
;
203 resource_metadata_
.GetEntryInfoByResourceId(
205 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
206 &error
, &drive_file_path
, &entry_proto
));
207 google_apis::test_util::RunBlockingPoolTask();
208 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
209 EXPECT_FALSE(entry_proto
.get());
212 TEST_F(DriveResourceMetadataTest
, GetEntryInfoByPath
) {
213 // Confirm that an existing file is found.
214 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
215 scoped_ptr
<DriveEntryProto
> entry_proto
;
216 resource_metadata_
.GetEntryInfoByPath(
217 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
218 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback
,
219 &error
, &entry_proto
));
220 google_apis::test_util::RunBlockingPoolTask();
221 EXPECT_EQ(DRIVE_FILE_OK
, error
);
222 ASSERT_TRUE(entry_proto
.get());
223 EXPECT_EQ("file4", entry_proto
->base_name());
225 // Confirm that a non existing file is not found.
226 error
= DRIVE_FILE_ERROR_FAILED
;
228 resource_metadata_
.GetEntryInfoByPath(
229 FilePath::FromUTF8Unsafe("drive/dir1/non_existing"),
230 base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback
,
231 &error
, &entry_proto
));
232 google_apis::test_util::RunBlockingPoolTask();
233 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
234 EXPECT_FALSE(entry_proto
.get());
237 TEST_F(DriveResourceMetadataTest
, ReadDirectoryByPath
) {
238 // Confirm that an existing directory is found.
239 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
240 scoped_ptr
<DriveEntryProtoVector
> entries
;
241 resource_metadata_
.ReadDirectoryByPath(
242 FilePath::FromUTF8Unsafe("drive/dir1"),
243 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback
,
245 google_apis::test_util::RunBlockingPoolTask();
246 EXPECT_EQ(DRIVE_FILE_OK
, error
);
247 ASSERT_TRUE(entries
.get());
248 ASSERT_EQ(3U, entries
->size());
250 // The order is not guaranteed so we should sort the base names.
251 std::vector
<std::string
> base_names
;
252 for (size_t i
= 0; i
< 3; ++i
)
253 base_names
.push_back(entries
->at(i
).base_name());
254 std::sort(base_names
.begin(), base_names
.end());
256 EXPECT_EQ("dir3", base_names
[0]);
257 EXPECT_EQ("file4", base_names
[1]);
258 EXPECT_EQ("file5", base_names
[2]);
260 // Confirm that a non existing directory is not found.
261 error
= DRIVE_FILE_ERROR_FAILED
;
263 resource_metadata_
.ReadDirectoryByPath(
264 FilePath::FromUTF8Unsafe("drive/non_existing"),
265 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback
,
267 google_apis::test_util::RunBlockingPoolTask();
268 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
269 EXPECT_FALSE(entries
.get());
271 // Confirm that reading a file results in DRIVE_FILE_ERROR_NOT_A_DIRECTORY.
272 error
= DRIVE_FILE_ERROR_FAILED
;
274 resource_metadata_
.ReadDirectoryByPath(
275 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
276 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback
,
278 google_apis::test_util::RunBlockingPoolTask();
279 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_A_DIRECTORY
, error
);
280 EXPECT_FALSE(entries
.get());
283 TEST_F(DriveResourceMetadataTest
, GetEntryInfoPairByPaths
) {
284 // Confirm that existing two files are found.
285 scoped_ptr
<EntryInfoPairResult
> pair_result
;
286 resource_metadata_
.GetEntryInfoPairByPaths(
287 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
288 FilePath::FromUTF8Unsafe("drive/dir1/file5"),
289 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback
,
291 google_apis::test_util::RunBlockingPoolTask();
292 // The first entry should be found.
293 EXPECT_EQ(DRIVE_FILE_OK
, pair_result
->first
.error
);
294 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"),
295 pair_result
->first
.path
);
296 ASSERT_TRUE(pair_result
->first
.proto
.get());
297 EXPECT_EQ("file4", pair_result
->first
.proto
->base_name());
298 // The second entry should be found.
299 EXPECT_EQ(DRIVE_FILE_OK
, pair_result
->second
.error
);
300 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file5"),
301 pair_result
->second
.path
);
302 ASSERT_TRUE(pair_result
->second
.proto
.get());
303 EXPECT_EQ("file5", pair_result
->second
.proto
->base_name());
305 // Confirm that the first non existent file is not found.
307 resource_metadata_
.GetEntryInfoPairByPaths(
308 FilePath::FromUTF8Unsafe("drive/dir1/non_existent"),
309 FilePath::FromUTF8Unsafe("drive/dir1/file5"),
310 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback
,
312 google_apis::test_util::RunBlockingPoolTask();
313 // The first entry should not be found.
314 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, pair_result
->first
.error
);
315 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/non_existent"),
316 pair_result
->first
.path
);
317 ASSERT_FALSE(pair_result
->first
.proto
.get());
318 // The second entry should not be found, because the first one failed.
319 EXPECT_EQ(DRIVE_FILE_ERROR_FAILED
, pair_result
->second
.error
);
320 EXPECT_EQ(FilePath(), pair_result
->second
.path
);
321 ASSERT_FALSE(pair_result
->second
.proto
.get());
323 // Confirm that the second non existent file is not found.
325 resource_metadata_
.GetEntryInfoPairByPaths(
326 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
327 FilePath::FromUTF8Unsafe("drive/dir1/non_existent"),
328 base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback
,
330 google_apis::test_util::RunBlockingPoolTask();
331 // The first entry should be found.
332 EXPECT_EQ(DRIVE_FILE_OK
, pair_result
->first
.error
);
333 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file4"),
334 pair_result
->first
.path
);
335 ASSERT_TRUE(pair_result
->first
.proto
.get());
336 EXPECT_EQ("file4", pair_result
->first
.proto
->base_name());
337 // The second entry should not be found.
338 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, pair_result
->second
.error
);
339 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/non_existent"),
340 pair_result
->second
.path
);
341 ASSERT_FALSE(pair_result
->second
.proto
.get());
344 TEST_F(DriveResourceMetadataTest
, DBTest
) {
345 TestingProfile profile
;
346 scoped_refptr
<base::SequencedWorkerPool
> pool
=
347 content::BrowserThread::GetBlockingPool();
348 scoped_refptr
<base::SequencedTaskRunner
> blocking_task_runner
=
349 pool
->GetSequencedTaskRunner(pool
->GetSequenceToken());
351 FilePath
db_path(DriveCache::GetCacheRootPath(&profile
).
352 AppendASCII("meta").AppendASCII("resource_metadata.db"));
353 // InitFromDB should fail with DRIVE_FILE_ERROR_NOT_FOUND since the db
355 resource_metadata_
.InitFromDB(db_path
, blocking_task_runner
,
356 base::Bind(&InitFromDBCallback
, DRIVE_FILE_ERROR_NOT_FOUND
));
357 google_apis::test_util::RunBlockingPoolTask();
359 // Create a file system and write it to disk.
360 // We cannot call SaveToDB without first having called InitFromDB because
361 // InitFrom initializes the db_path and blocking_task_runner needed by
363 resource_metadata_
.SaveToDB();
364 google_apis::test_util::RunBlockingPoolTask();
366 // InitFromDB should fail with DRIVE_FILE_ERROR_IN_USE.
367 resource_metadata_
.InitFromDB(db_path
, blocking_task_runner
,
368 base::Bind(&InitFromDBCallback
, DRIVE_FILE_ERROR_IN_USE
));
369 google_apis::test_util::RunBlockingPoolTask();
371 // InitFromDB should succeed.
372 DriveResourceMetadata test_resource_metadata
;
373 test_resource_metadata
.InitializeRootEntry(kTestRootDirectoryResourceId
);
374 test_resource_metadata
.InitFromDB(db_path
, blocking_task_runner
,
375 base::Bind(&InitFromDBCallback
, DRIVE_FILE_OK
));
376 google_apis::test_util::RunBlockingPoolTask();
378 // Verify by checking for drive/dir2, which should have 3 children.
379 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
380 scoped_ptr
<DriveEntryProtoVector
> entries
;
381 test_resource_metadata
.ReadDirectoryByPath(
382 FilePath::FromUTF8Unsafe("drive/dir2"),
383 base::Bind(&test_util::CopyResultsFromReadDirectoryCallback
,
385 google_apis::test_util::RunBlockingPoolTask();
386 EXPECT_EQ(DRIVE_FILE_OK
, error
);
387 ASSERT_TRUE(entries
.get());
388 ASSERT_EQ(3U, entries
->size());
391 TEST_F(DriveResourceMetadataTest
, RemoveEntryFromParent
) {
392 // Make sure file9 is found.
393 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
394 FilePath drive_file_path
;
395 const std::string file9_resource_id
= "file_resource_id:file9";
396 scoped_ptr
<DriveEntryProto
> entry_proto
;
397 resource_metadata_
.GetEntryInfoByResourceId(
399 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
400 &error
, &drive_file_path
, &entry_proto
));
401 google_apis::test_util::RunBlockingPoolTask();
402 EXPECT_EQ(DRIVE_FILE_OK
, error
);
403 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/dir3/file9"), drive_file_path
);
404 ASSERT_TRUE(entry_proto
.get());
405 EXPECT_EQ("file9", entry_proto
->base_name());
407 // Remove file9 using RemoveEntryFromParent.
408 resource_metadata_
.RemoveEntryFromParent(
410 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
411 &error
, &drive_file_path
));
412 google_apis::test_util::RunBlockingPoolTask();
413 EXPECT_EQ(DRIVE_FILE_OK
, error
);
414 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/dir3"), drive_file_path
);
416 // file9 should no longer exist.
417 resource_metadata_
.GetEntryInfoByResourceId(
419 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
420 &error
, &drive_file_path
, &entry_proto
));
421 google_apis::test_util::RunBlockingPoolTask();
422 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
423 EXPECT_FALSE(entry_proto
.get());
426 const std::string dir3_resource_id
= "dir_resource_id:dir3";
427 resource_metadata_
.GetEntryInfoByResourceId(
429 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
430 &error
, &drive_file_path
, &entry_proto
));
431 google_apis::test_util::RunBlockingPoolTask();
432 EXPECT_EQ(DRIVE_FILE_OK
, error
);
433 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/dir3"), drive_file_path
);
434 ASSERT_TRUE(entry_proto
.get());
435 EXPECT_EQ("dir3", entry_proto
->base_name());
437 // Remove dir3 using RemoveEntryFromParent.
438 resource_metadata_
.RemoveEntryFromParent(
440 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
441 &error
, &drive_file_path
));
442 google_apis::test_util::RunBlockingPoolTask();
443 EXPECT_EQ(DRIVE_FILE_OK
, error
);
444 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1"), drive_file_path
);
446 // dir3 should no longer exist.
447 resource_metadata_
.GetEntryInfoByResourceId(
449 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
450 &error
, &drive_file_path
, &entry_proto
));
451 google_apis::test_util::RunBlockingPoolTask();
452 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
453 EXPECT_FALSE(entry_proto
.get());
455 // Remove unknown resource_id using RemoveEntryFromParent.
456 resource_metadata_
.RemoveEntryFromParent(
458 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
459 &error
, &drive_file_path
));
460 google_apis::test_util::RunBlockingPoolTask();
461 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
463 // Try removing root. This should fail.
464 resource_metadata_
.RemoveEntryFromParent(
465 resource_metadata_
.root()->resource_id(),
466 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
467 &error
, &drive_file_path
));
468 google_apis::test_util::RunBlockingPoolTask();
469 EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED
, error
);
472 TEST_F(DriveResourceMetadataTest
, MoveEntryToDirectory
) {
473 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
474 FilePath drive_file_path
;
475 scoped_ptr
<DriveEntryProto
> entry_proto
;
477 // Move file8 to drive/dir1.
478 resource_metadata_
.MoveEntryToDirectory(
479 FilePath::FromUTF8Unsafe("drive/dir2/file8"),
480 FilePath::FromUTF8Unsafe("drive/dir1"),
481 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
482 &error
, &drive_file_path
));
483 google_apis::test_util::RunBlockingPoolTask();
484 EXPECT_EQ(DRIVE_FILE_OK
, error
);
485 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file8"), drive_file_path
);
487 // Look up the entry by its resource id and make sure it really moved.
488 resource_metadata_
.GetEntryInfoByResourceId(
489 "file_resource_id:file8",
490 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
491 &error
, &drive_file_path
, &entry_proto
));
492 google_apis::test_util::RunBlockingPoolTask();
493 EXPECT_EQ(DRIVE_FILE_OK
, error
);
494 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file8"), drive_file_path
);
496 // Move non-existent file to drive/dir1. This should fail.
497 resource_metadata_
.MoveEntryToDirectory(
498 FilePath::FromUTF8Unsafe("drive/dir2/file8"),
499 FilePath::FromUTF8Unsafe("drive/dir1"),
500 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
501 &error
, &drive_file_path
));
502 google_apis::test_util::RunBlockingPoolTask();
503 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
504 EXPECT_EQ(FilePath(), drive_file_path
);
506 // Move existing file to non-existent directory. This should fail.
507 resource_metadata_
.MoveEntryToDirectory(
508 FilePath::FromUTF8Unsafe("drive/dir1/file8"),
509 FilePath::FromUTF8Unsafe("drive/dir4"),
510 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
511 &error
, &drive_file_path
));
512 google_apis::test_util::RunBlockingPoolTask();
513 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
514 EXPECT_EQ(FilePath(), drive_file_path
);
516 // Move existing file to existing file (non-directory). This should fail.
517 resource_metadata_
.MoveEntryToDirectory(
518 FilePath::FromUTF8Unsafe("drive/dir1/file8"),
519 FilePath::FromUTF8Unsafe("drive/dir1/file4"),
520 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
521 &error
, &drive_file_path
));
522 google_apis::test_util::RunBlockingPoolTask();
523 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_A_DIRECTORY
, error
);
524 EXPECT_EQ(FilePath(), drive_file_path
);
526 // Move the file to root.
527 resource_metadata_
.MoveEntryToDirectory(
528 FilePath::FromUTF8Unsafe("drive/dir1/file8"),
529 FilePath::FromUTF8Unsafe("drive"),
530 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
531 &error
, &drive_file_path
));
532 google_apis::test_util::RunBlockingPoolTask();
533 EXPECT_EQ(DRIVE_FILE_OK
, error
);
534 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/file8"), drive_file_path
);
536 // Move the file from root.
537 resource_metadata_
.MoveEntryToDirectory(
538 FilePath::FromUTF8Unsafe("drive/file8"),
539 FilePath::FromUTF8Unsafe("drive/dir2"),
540 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
541 &error
, &drive_file_path
));
542 google_apis::test_util::RunBlockingPoolTask();
543 EXPECT_EQ(DRIVE_FILE_OK
, error
);
544 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file8"), drive_file_path
);
546 // Make sure file is still ok.
547 resource_metadata_
.GetEntryInfoByResourceId(
548 "file_resource_id:file8",
549 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
550 &error
, &drive_file_path
, &entry_proto
));
551 google_apis::test_util::RunBlockingPoolTask();
552 EXPECT_EQ(DRIVE_FILE_OK
, error
);
553 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file8"), drive_file_path
);
556 TEST_F(DriveResourceMetadataTest
, RenameEntry
) {
557 DriveFileError error
= DRIVE_FILE_ERROR_FAILED
;
558 FilePath drive_file_path
;
559 scoped_ptr
<DriveEntryProto
> entry_proto
;
561 // Rename file8 to file11.
562 resource_metadata_
.RenameEntry(
563 FilePath::FromUTF8Unsafe("drive/dir2/file8"),
565 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
566 &error
, &drive_file_path
));
567 google_apis::test_util::RunBlockingPoolTask();
568 EXPECT_EQ(DRIVE_FILE_OK
, error
);
569 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file11"), drive_file_path
);
571 // Lookup the file by resource id to make sure the file actually got renamed.
572 resource_metadata_
.GetEntryInfoByResourceId(
573 "file_resource_id:file8",
574 base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback
,
575 &error
, &drive_file_path
, &entry_proto
));
576 google_apis::test_util::RunBlockingPoolTask();
577 EXPECT_EQ(DRIVE_FILE_OK
, error
);
578 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file11"), drive_file_path
);
580 // Rename to file7 to force a duplicate name.
581 resource_metadata_
.RenameEntry(
582 FilePath::FromUTF8Unsafe("drive/dir2/file11"),
584 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
585 &error
, &drive_file_path
));
586 google_apis::test_util::RunBlockingPoolTask();
587 EXPECT_EQ(DRIVE_FILE_OK
, error
);
588 EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file7 (2)"), drive_file_path
);
590 // Rename to same name. This should fail.
591 resource_metadata_
.RenameEntry(
592 FilePath::FromUTF8Unsafe("drive/dir2/file7 (2)"),
594 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
595 &error
, &drive_file_path
));
596 google_apis::test_util::RunBlockingPoolTask();
597 EXPECT_EQ(DRIVE_FILE_ERROR_EXISTS
, error
);
598 EXPECT_EQ(FilePath(), drive_file_path
);
600 // Rename non-existent.
601 resource_metadata_
.RenameEntry(
602 FilePath::FromUTF8Unsafe("drive/dir2/file11"),
604 base::Bind(&test_util::CopyResultsFromFileMoveCallback
,
605 &error
, &drive_file_path
));
606 google_apis::test_util::RunBlockingPoolTask();
607 EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND
, error
);
608 EXPECT_EQ(FilePath(), drive_file_path
);