diff options
author | achuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-06 09:44:01 +0000 |
---|---|---|
committer | achuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-09-06 09:44:01 +0000 |
commit | 9eb52cf9acd967a437d11a8c5067c0e470538ba9 (patch) | |
tree | e8ec1121cb2967d04b56c78fcdb8381533a660d5 /chrome/browser/chromeos/gdata | |
parent | b3a791ffa733bd6fa5a2d5e3bb5d40e7d9995ad1 (diff) | |
download | chromium_src-9eb52cf9acd967a437d11a8c5067c0e470538ba9.zip chromium_src-9eb52cf9acd967a437d11a8c5067c0e470538ba9.tar.gz chromium_src-9eb52cf9acd967a437d11a8c5067c0e470538ba9.tar.bz2 |
DriveResourceMetadata::MoveEntryToDirectory cleanup.
* DriveResourceMetadata::MoveEntryToDirectory now takes a FilePath instead of a DriveEntry*.
* DriveResourceMetadata has a new public method RenameEntry, which does what DriveFileSystem::RenameEntryLocally used to do. RenameEntryLocally now simply calls DriveResourceMetadata::RenameEntry.
* DriveFileSystem's MoveEntryToRootDirectoryLocally and OnMoveEntryFromRootDirectoryCompletely are eliminated in favor of one method MoveEntryToDirectory. Both these methods did very similar things.
* DriveResourceMetadataTest introduced because MoveEntryToDirectory can no longer be used. This class is a friend of DriveDirectory, and we directly use AddEntry instead. This also eliminates a bunch of repetitive initialization code. All TEST are now TEST_F.
* DBTest has been rewritten a bit. Get rid of VerifyDirectoryService. We only check for children of drive/dir3 to ensure that the filesystem has been properly initialized from the database.
* DriveResourceMetadata::InitFromDB's callback may not be null.
BUG=142048
TEST=unit tests.
Review URL: https://chromiumcodereview.appspot.com/10914059
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@155159 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/gdata')
6 files changed, 370 insertions, 339 deletions
diff --git a/chrome/browser/chromeos/gdata/drive_file_system.cc b/chrome/browser/chromeos/gdata/drive_file_system.cc index baf1445..0933b27 100644 --- a/chrome/browser/chromeos/gdata/drive_file_system.cc +++ b/chrome/browser/chromeos/gdata/drive_file_system.cc @@ -1049,6 +1049,7 @@ void DriveFileSystem::MoveOnUIThreadAfterGetEntryInfoPair( // If the file/directory is moved to the same directory, just rename it. const FilePath& src_file_path = result->first.path; const FilePath& dest_parent_path = result->second.path; + DCHECK_EQ(dest_parent_path.value(), dest_file_path.DirName().value()); if (src_file_path.DirName() == dest_parent_path) { FileMoveCallback final_file_path_update_callback = base::Bind(&DriveFileSystem::OnFilePathUpdated, @@ -1072,7 +1073,7 @@ void DriveFileSystem::MoveOnUIThreadAfterGetEntryInfoPair( const FileMoveCallback add_file_to_directory_callback = base::Bind(&DriveFileSystem::MoveEntryFromRootDirectory, ui_weak_ptr_, - dest_file_path.DirName(), + dest_parent_path, callback); const FileMoveCallback remove_file_from_directory_callback = @@ -1136,11 +1137,13 @@ void DriveFileSystem::MoveEntryFromRootDirectoryAfterGetEntryInfoPair( drive_service_->AddResourceToDirectory( GURL(dir_proto->content_url()), GURL(src_proto->edit_url()), - base::Bind(&DriveFileSystem::OnMoveEntryFromRootDirectoryCompleted, + base::Bind(&DriveFileSystem::MoveEntryToDirectory, ui_weak_ptr_, - callback, file_path, - dir_path)); + dir_path, + base::Bind(&DriveFileSystem::NotifyAndRunFileOperationCallback, + ui_weak_ptr_, + callback))); } void DriveFileSystem::RemoveEntryFromNonRootDirectory( @@ -1174,7 +1177,6 @@ void DriveFileSystem::RemoveEntryFromNonRootDirectoryAfterEntryInfoPair( DCHECK(result.get()); const FilePath& file_path = result->first.path; - const FilePath& dir_path = result->second.path; if (result->first.error != DRIVE_FILE_OK) { callback.Run(result->first.error, file_path); return; @@ -1191,15 +1193,18 @@ void DriveFileSystem::RemoveEntryFromNonRootDirectoryAfterEntryInfoPair( return; } + // The entry is moved to the root directory. drive_service_->RemoveResourceFromDirectory( GURL(dir_proto->content_url()), GURL(entry_proto->edit_url()), entry_proto->resource_id(), - base::Bind(&DriveFileSystem::MoveEntryToRootDirectoryLocally, + base::Bind(&DriveFileSystem::MoveEntryToDirectory, ui_weak_ptr_, - callback, file_path, - dir_path)); + resource_metadata_->root()->GetFilePath(), + base::Bind(&DriveFileSystem::NotifyAndRunFileMoveCallback, + ui_weak_ptr_, + callback))); } void DriveFileSystem::Remove(const FilePath& file_path, @@ -2364,36 +2369,6 @@ void DriveFileSystem::OnCopyDocumentCompleted( callback)); } -void DriveFileSystem::OnMoveEntryFromRootDirectoryCompleted( - const FileOperationCallback& callback, - const FilePath& file_path, - const FilePath& dir_path, - GDataErrorCode status, - const GURL& document_url) { - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(!callback.is_null()); - - DriveFileError error = util::GDataToDriveFileError(status); - if (error == DRIVE_FILE_OK) { - DriveEntry* entry = resource_metadata_->FindEntryByPathSync(file_path); - if (entry) { - DCHECK_EQ(resource_metadata_->root(), entry->parent()); - resource_metadata_->MoveEntryToDirectory( - dir_path, - entry, - base::Bind( - &DriveFileSystem::NotifyAndRunFileOperationCallback, - ui_weak_ptr_, - callback)); - return; - } else { - error = DRIVE_FILE_ERROR_NOT_FOUND; - } - } - - callback.Run(error); -} - void DriveFileSystem::OnFileDownloaded( const GetFileFromCacheParams& params, GDataErrorCode status, @@ -2505,7 +2480,7 @@ void DriveFileSystem::RenameEntryLocally( const FilePath::StringType& new_name, const FileMoveCallback& callback, GDataErrorCode status, - const GURL& document_url) { + const GURL& /* document_url */) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(!callback.is_null()); @@ -2515,35 +2490,20 @@ void DriveFileSystem::RenameEntryLocally( return; } - DriveEntry* entry = resource_metadata_->FindEntryByPathSync(file_path); - if (!entry) { - callback.Run(DRIVE_FILE_ERROR_NOT_FOUND, FilePath()); - return; - } - - DCHECK(entry->parent()); - entry->set_title(new_name); - // After changing the title of the entry, call MoveEntryToDirectory() to - // remove the entry from its parent directory and then add it back in order to - // go through the file name de-duplication. - // TODO(achuith/satorux/zel): This code is fragile. The title has been - // changed, but not the file_name. MoveEntryToDirectory calls RemoveChild to - // remove the child based on the old file_name, and then re-adds the child by - // first assigning the new title to file_name. http://crbug.com/30157 - resource_metadata_->MoveEntryToDirectory( - entry->parent()->GetFilePath(), - entry, + resource_metadata_->RenameEntry( + file_path, + new_name, base::Bind(&DriveFileSystem::NotifyAndRunFileMoveCallback, ui_weak_ptr_, callback)); } -void DriveFileSystem::MoveEntryToRootDirectoryLocally( - const FileMoveCallback& callback, +void DriveFileSystem::MoveEntryToDirectory( const FilePath& file_path, const FilePath& dir_path, + const FileMoveCallback& callback, GDataErrorCode status, - const GURL& document_url) { + const GURL& /* document_url */) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(!callback.is_null()); @@ -2553,18 +2513,7 @@ void DriveFileSystem::MoveEntryToRootDirectoryLocally( return; } - DriveEntry* entry = resource_metadata_->FindEntryByPathSync(file_path); - if (!entry) { - callback.Run(DRIVE_FILE_ERROR_NOT_FOUND, FilePath()); - return; - } - - resource_metadata_->MoveEntryToDirectory( - resource_metadata_->root()->GetFilePath(), - entry, - base::Bind(&DriveFileSystem::NotifyAndRunFileMoveCallback, - ui_weak_ptr_, - callback)); + resource_metadata_->MoveEntryToDirectory(file_path, dir_path, callback); } void DriveFileSystem::NotifyAndRunFileMoveCallback( diff --git a/chrome/browser/chromeos/gdata/drive_file_system.h b/chrome/browser/chromeos/gdata/drive_file_system.h index 65293c1..e19ce97 100644 --- a/chrome/browser/chromeos/gdata/drive_file_system.h +++ b/chrome/browser/chromeos/gdata/drive_file_system.h @@ -478,19 +478,6 @@ class DriveFileSystem : public DriveFileSystemInterface, GDataErrorCode status, scoped_ptr<base::Value> data); - // Callback for handling an attempt to move a file or directory from the - // root directory to another directory on the server side. This function - // moves |entry| to the root directory on the client side with - // DriveResourceMetadata::MoveEntryToDirectory(). - // - // |callback| must not be null. - void OnMoveEntryFromRootDirectoryCompleted( - const FileOperationCallback& callback, - const FilePath& file_path, - const FilePath& dir_path, - GDataErrorCode status, - const GURL& document_url); - // Callback for handling account metadata fetch. void OnGetAvailableSpace(const GetAvailableSpaceCallback& callback, GDataErrorCode status, @@ -549,15 +536,14 @@ class DriveFileSystem : public DriveFileSystemInterface, GDataErrorCode status, const GURL& document_url); - // Callback for handling an attempt to remove a file or directory from - // another directory. Moves a file or directory at |file_path| to root on - // the client side. + // Moves entry specified by |file_path| to the directory specified by + // |dir_path| and calls |callback| asynchronously. // |callback| must not be null. - void MoveEntryToRootDirectoryLocally(const FileMoveCallback& callback, - const FilePath& file_path, - const FilePath& dir_path, - GDataErrorCode status, - const GURL& document_url); + void MoveEntryToDirectory(const FilePath& file_path, + const FilePath& dir_path, + const FileMoveCallback& callback, + GDataErrorCode status, + const GURL& document_url); // Callback when an entry is moved to another directory on the client side. // Notifies the directory change and runs |callback|. diff --git a/chrome/browser/chromeos/gdata/drive_files.h b/chrome/browser/chromeos/gdata/drive_files.h index 1c43a07..3f22106 100644 --- a/chrome/browser/chromeos/gdata/drive_files.h +++ b/chrome/browser/chromeos/gdata/drive_files.h @@ -228,6 +228,7 @@ class DriveDirectory : public DriveEntry { private: // TODO(satorux): Remove the friend statements. crbug.com/139649 friend class DriveResourceMetadata; + friend class DriveResourceMetadataTest; friend class GDataWapiFeedProcessor; explicit DriveDirectory(DriveResourceMetadata* resource_metadata); diff --git a/chrome/browser/chromeos/gdata/drive_resource_metadata.cc b/chrome/browser/chromeos/gdata/drive_resource_metadata.cc index 9a97c8d..92a5367 100644 --- a/chrome/browser/chromeos/gdata/drive_resource_metadata.cc +++ b/chrome/browser/chromeos/gdata/drive_resource_metadata.cc @@ -276,15 +276,19 @@ void DriveResourceMetadata::AddEntryToDirectory( } void DriveResourceMetadata::MoveEntryToDirectory( + const FilePath& file_path, const FilePath& directory_path, - DriveEntry* entry, const FileMoveCallback& callback) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - DCHECK(entry); + DCHECK(!directory_path.empty()); + DCHECK(!file_path.empty()); DCHECK(!callback.is_null()); - if (entry->parent()) - entry->parent()->RemoveChild(entry); + DriveEntry* entry = FindEntryByPathSync(file_path); + if (!entry) { + PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); + return; + } DriveEntry* destination = FindEntryByPathSync(directory_path); FilePath moved_file_path; @@ -294,6 +298,9 @@ void DriveResourceMetadata::MoveEntryToDirectory( } else if (!destination->AsDriveDirectory()) { error = DRIVE_FILE_ERROR_NOT_A_DIRECTORY; } else { + if (entry->parent()) + entry->parent()->RemoveChild(entry); + destination->AsDriveDirectory()->AddEntry(entry); moved_file_path = entry->GetFilePath(); error = DRIVE_FILE_OK; @@ -303,6 +310,39 @@ void DriveResourceMetadata::MoveEntryToDirectory( FROM_HERE, base::Bind(callback, error, moved_file_path)); } +void DriveResourceMetadata::RenameEntry( + const FilePath& file_path, + const FilePath::StringType& new_name, + const FileMoveCallback& callback) { + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); + DCHECK(!file_path.empty()); + DCHECK(!new_name.empty()); + DCHECK(!callback.is_null()); + + DVLOG(1) << "RenameEntry " << file_path.value() << " to " << new_name; + DriveEntry* entry = FindEntryByPathSync(file_path); + if (!entry) { + PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_NOT_FOUND); + return; + } + + if (new_name == file_path.BaseName().value()) { + PostFileMoveCallbackError(callback, DRIVE_FILE_ERROR_EXISTS); + return; + } + + entry->set_title(new_name); + DCHECK(entry->parent()); + // After changing the title of the entry, call MoveEntryToDirectory to + // remove the entry from its parent directory and then add it back in order to + // go through the file name de-duplication. + // TODO(achuith/satorux/zel): This code is fragile. The title has been + // changed, but not the file_name. MoveEntryToDirectory calls RemoveChild to + // remove the child based on the old file_name, and then re-adds the child by + // first assigning the new title to file_name. http://crbug.com/30157 + MoveEntryToDirectory(file_path, entry->parent()->GetFilePath(), callback); +} + void DriveResourceMetadata::RemoveEntryFromParent( const std::string& resource_id, const FileMoveCallback& callback) { @@ -566,10 +606,10 @@ void DriveResourceMetadata::InitFromDB( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(!db_path.empty()); DCHECK(blocking_task_runner); + DCHECK(!callback.is_null()); if (resource_metadata_db_.get()) { - if (!callback.is_null()) - callback.Run(DRIVE_FILE_ERROR_FAILED); + callback.Run(DRIVE_FILE_ERROR_IN_USE); return; } @@ -595,13 +635,14 @@ void DriveResourceMetadata::InitResourceMap( DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(create_params); DCHECK(!resource_metadata_db_.get()); + DCHECK(!callback.is_null()); + SerializedMap* serialized_resources = &create_params->serialized_resources; resource_metadata_db_ = create_params->db.Pass(); if (serialized_resources->empty()) { origin_ = INITIALIZING; - if (!callback.is_null()) - callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); + callback.Run(DRIVE_FILE_ERROR_NOT_FOUND); return; } @@ -613,8 +654,7 @@ void DriveResourceMetadata::InitResourceMap( if (iter == serialized_resources->end() || !base::StringToInt(iter->second, &version) || version != kProtoVersion) { - if (!callback.is_null()) - callback.Run(DRIVE_FILE_ERROR_FAILED); + callback.Run(DRIVE_FILE_ERROR_FAILED); return; } serialized_resources->erase(iter); @@ -624,8 +664,7 @@ void DriveResourceMetadata::InitResourceMap( if (iter == serialized_resources->end() || !base::StringToInt64(iter->second, &largest_changestamp_)) { NOTREACHED() << "Could not find/parse largest_changestamp"; - if (!callback.is_null()) - callback.Run(DRIVE_FILE_ERROR_FAILED); + callback.Run(DRIVE_FILE_ERROR_FAILED); return; } else { DVLOG(1) << "InitResourceMap largest_changestamp_" << largest_changestamp_; @@ -683,8 +722,7 @@ void DriveResourceMetadata::InitResourceMap( origin_ = FROM_CACHE; - if (!callback.is_null()) - callback.Run(DRIVE_FILE_OK); + callback.Run(DRIVE_FILE_OK); } void DriveResourceMetadata::SaveToDB() { diff --git a/chrome/browser/chromeos/gdata/drive_resource_metadata.h b/chrome/browser/chromeos/gdata/drive_resource_metadata.h index 523a33d..0429ef3 100644 --- a/chrome/browser/chromeos/gdata/drive_resource_metadata.h +++ b/chrome/browser/chromeos/gdata/drive_resource_metadata.h @@ -167,13 +167,21 @@ class DriveResourceMetadata { scoped_ptr<DocumentEntry> doc_entry, const FileMoveCallback& callback); - // Moves |entry| to |directory_path| asynchronously. Removes entry from - // previous parent. Must be called on UI thread. |callback| is called on the - // UI thread. |callback| must not be null. - void MoveEntryToDirectory(const FilePath& directory_path, - DriveEntry* entry, + // Moves entry specified by |file_path| to the directory specified by + // |directory_path| and calls the callback asynchronously. Removes the entry + // from the previous parent. + // |callback| must not be null. + void MoveEntryToDirectory(const FilePath& file_path, + const FilePath& directory_path, const FileMoveCallback& callback); + // Renames entry specified by |file_path| with the new name |new_name| and + // calls |callback| asynchronously. + // |callback| must not be null. + void RenameEntry(const FilePath& file_path, + const FilePath::StringType& new_name, + const FileMoveCallback& callback); + // Removes entry with |resource_id| from its parent. Calls |callback| with the // path of the parent directory. |callback| must not be null. void RemoveEntryFromParent(const std::string& resource_id, @@ -246,7 +254,8 @@ class DriveResourceMetadata { void SerializeToString(std::string* serialized_proto) const; bool ParseFromString(const std::string& serialized_proto); - // Restores from and saves to database. + // Restores from and saves to database, calling |callback| asynchronously. + // |callback| must not be null. void InitFromDB(const FilePath& db_path, base::SequencedTaskRunner* blocking_task_runner, const FileOperationCallback& callback); @@ -255,6 +264,7 @@ class DriveResourceMetadata { private: // Initializes the resource map using serialized_resources fetched from the // database. + // |callback| must not be null. void InitResourceMap(CreateDBParams* create_params, const FileOperationCallback& callback); diff --git a/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc b/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc index a6eafba..717fb70 100644 --- a/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc +++ b/chrome/browser/chromeos/gdata/drive_resource_metadata_unittest.cc @@ -28,180 +28,106 @@ namespace { const char kResumableEditMediaUrl[] = "http://resumable-edit-media/"; const char kResumableCreateMediaUrl[] = "http://resumable-create-media/"; -// Add a directory to |parent| and return that directory. The name and -// resource_id are determined by the incrementing counter |sequence_id|. -DriveDirectory* AddDirectory(DriveDirectory* parent, - DriveResourceMetadata* resource_metadata, - int sequence_id) { - scoped_ptr<DriveDirectory> dir = resource_metadata->CreateDriveDirectory(); - const std::string dir_name = "dir" + base::IntToString(sequence_id); - const std::string resource_id = std::string("dir_resource_id:") + - dir_name; - dir->set_title(dir_name); - dir->set_resource_id(resource_id); - DriveFileError error = DRIVE_FILE_ERROR_FAILED; - FilePath moved_file_path; - resource_metadata->MoveEntryToDirectory( - parent->GetFilePath(), - dir.get(), - base::Bind(&test_util::CopyResultsFromFileMoveCallback, - &error, - &moved_file_path)); - test_util::RunBlockingPoolTask(); - EXPECT_EQ(DRIVE_FILE_OK, error); - EXPECT_EQ(parent->GetFilePath().AppendASCII(dir_name), moved_file_path); - return dir.release(); +// Callback for DriveResourceMetadata::InitFromDB. +void InitFromDBCallback(DriveFileError expected_error, + DriveFileError actual_error) { + EXPECT_EQ(expected_error, actual_error); } -// Add a file to |parent| and return that file. The name and -// resource_id are determined by the incrementing counter |sequence_id|. -DriveFile* AddFile(DriveDirectory* parent, - DriveResourceMetadata* resource_metadata, - int sequence_id) { - scoped_ptr<DriveFile> file = resource_metadata->CreateDriveFile(); - const std::string title = "file" + base::IntToString(sequence_id); - const std::string resource_id = std::string("file_resource_id:") + - title; - file->set_title(title); - file->set_resource_id(resource_id); - file->set_file_md5(std::string("file_md5:") + title); - DriveFileError error = DRIVE_FILE_ERROR_FAILED; - FilePath moved_file_path; - resource_metadata->MoveEntryToDirectory( - parent->GetFilePath(), - file.get(), - base::Bind(&test_util::CopyResultsFromFileMoveCallback, - &error, - &moved_file_path)); - test_util::RunBlockingPoolTask(); +// Callback for DriveResourceMetadata::ReadDirectoryByPath. +void ReadDirectoryByPathCallback( + scoped_ptr<DriveEntryProtoVector>* result, + DriveFileError error, + scoped_ptr<DriveEntryProtoVector> entries) { EXPECT_EQ(DRIVE_FILE_OK, error); - EXPECT_EQ(parent->GetFilePath().AppendASCII(title), moved_file_path); - return file.release(); + *result = entries.Pass(); +} + +} // namespace + +class DriveResourceMetadataTest : public testing::Test { + public: + DriveResourceMetadataTest(); + + protected: + DriveResourceMetadata resource_metadata_; + + private: + // Creates the following files/directories + // drive/dir1/ + // drive/dir2/ + // drive/dir1/dir3/ + // drive/dir1/file4 + // drive/dir1/file5 + // drive/dir2/file6 + // drive/dir2/file7 + // drive/dir2/file8 + // drive/dir1/dir3/file9 + // drive/dir1/dir3/file10 + void Init(); + + // Add a directory to |parent| and return that directory. The name and + // resource_id are determined by the incrementing counter |sequence_id|. + DriveDirectory* AddDirectory(DriveDirectory* parent, int sequence_id); + + // Add a file to |parent| and return that file. The name and + // resource_id are determined by the incrementing counter |sequence_id|. + DriveFile* AddFile(DriveDirectory* parent, int sequence_id); + + MessageLoopForUI message_loop_; + content::TestBrowserThread ui_thread_; +}; + +DriveResourceMetadataTest::DriveResourceMetadataTest() + : ui_thread_(content::BrowserThread::UI, &message_loop_) { + Init(); } -// Creates the following files/directories -// drive/dir1/ -// drive/dir2/ -// drive/dir1/dir3/ -// drive/dir1/file4 -// drive/dir1/file5 -// drive/dir2/file6 -// drive/dir2/file7 -// drive/dir2/file8 -// drive/dir1/dir3/file9 -// drive/dir1/dir3/file10 -void InitDirectoryService(DriveResourceMetadata* resource_metadata) { +void DriveResourceMetadataTest::Init() { int sequence_id = 1; - DriveDirectory* dir1 = AddDirectory(resource_metadata->root(), - resource_metadata, sequence_id++); - DriveDirectory* dir2 = AddDirectory(resource_metadata->root(), - resource_metadata, sequence_id++); - DriveDirectory* dir3 = AddDirectory(dir1, resource_metadata, sequence_id++); + DriveDirectory* dir1 = AddDirectory(resource_metadata_.root(), sequence_id++); + DriveDirectory* dir2 = AddDirectory(resource_metadata_.root(), sequence_id++); + DriveDirectory* dir3 = AddDirectory(dir1, sequence_id++); - AddFile(dir1, resource_metadata, sequence_id++); - AddFile(dir1, resource_metadata, sequence_id++); + AddFile(dir1, sequence_id++); + AddFile(dir1, sequence_id++); - AddFile(dir2, resource_metadata, sequence_id++); - AddFile(dir2, resource_metadata, sequence_id++); - AddFile(dir2, resource_metadata, sequence_id++); + AddFile(dir2, sequence_id++); + AddFile(dir2, sequence_id++); + AddFile(dir2, sequence_id++); - AddFile(dir3, resource_metadata, sequence_id++); - AddFile(dir3, resource_metadata, sequence_id++); + AddFile(dir3, sequence_id++); + AddFile(dir3, sequence_id++); } -// Find directory by path. -DriveDirectory* FindDirectory(DriveResourceMetadata* resource_metadata, - const char* path) { - return resource_metadata->FindEntryByPathSync( - FilePath(path))->AsDriveDirectory(); -} +DriveDirectory* DriveResourceMetadataTest::AddDirectory(DriveDirectory* parent, + int sequence_id) { + scoped_ptr<DriveDirectory> dir = resource_metadata_.CreateDriveDirectory(); + const std::string dir_name = "dir" + base::IntToString(sequence_id); + const std::string resource_id = std::string("dir_resource_id:") + dir_name; + dir->set_title(dir_name); + dir->set_resource_id(resource_id); -// Find file by path. -DriveFile* FindFile(DriveResourceMetadata* resource_metadata, - const char* path) { - return resource_metadata->FindEntryByPathSync(FilePath(path))->AsDriveFile(); -} + parent->AddEntry(dir.get()); -// Verify that the recreated directory service matches what we created in -// InitDirectoryService. -void VerifyDirectoryService(DriveResourceMetadata* resource_metadata) { - ASSERT_TRUE(resource_metadata->root()); - - DriveDirectory* dir1 = FindDirectory(resource_metadata, "drive/dir1"); - ASSERT_TRUE(dir1); - DriveDirectory* dir2 = FindDirectory(resource_metadata, "drive/dir2"); - ASSERT_TRUE(dir2); - DriveDirectory* dir3 = FindDirectory(resource_metadata, "drive/dir1/dir3"); - ASSERT_TRUE(dir3); - - DriveFile* file4 = FindFile(resource_metadata, "drive/dir1/file4"); - ASSERT_TRUE(file4); - EXPECT_EQ(file4->parent(), dir1); - - DriveFile* file5 = FindFile(resource_metadata, "drive/dir1/file5"); - ASSERT_TRUE(file5); - EXPECT_EQ(file5->parent(), dir1); - - DriveFile* file6 = FindFile(resource_metadata, "drive/dir2/file6"); - ASSERT_TRUE(file6); - EXPECT_EQ(file6->parent(), dir2); - - DriveFile* file7 = FindFile(resource_metadata, "drive/dir2/file7"); - ASSERT_TRUE(file7); - EXPECT_EQ(file7->parent(), dir2); - - DriveFile* file8 = FindFile(resource_metadata, "drive/dir2/file8"); - ASSERT_TRUE(file8); - EXPECT_EQ(file8->parent(), dir2); - - DriveFile* file9 = FindFile(resource_metadata, "drive/dir1/dir3/file9"); - ASSERT_TRUE(file9); - EXPECT_EQ(file9->parent(), dir3); - - DriveFile* file10 = FindFile(resource_metadata, "drive/dir1/dir3/file10"); - ASSERT_TRUE(file10); - EXPECT_EQ(file10->parent(), dir3); - - EXPECT_EQ(dir1, resource_metadata->GetEntryByResourceId( - "dir_resource_id:dir1")); - EXPECT_EQ(dir2, resource_metadata->GetEntryByResourceId( - "dir_resource_id:dir2")); - EXPECT_EQ(dir3, resource_metadata->GetEntryByResourceId( - "dir_resource_id:dir3")); - EXPECT_EQ(file4, resource_metadata->GetEntryByResourceId( - "file_resource_id:file4")); - EXPECT_EQ(file5, resource_metadata->GetEntryByResourceId( - "file_resource_id:file5")); - EXPECT_EQ(file6, resource_metadata->GetEntryByResourceId( - "file_resource_id:file6")); - EXPECT_EQ(file7, resource_metadata->GetEntryByResourceId( - "file_resource_id:file7")); - EXPECT_EQ(file8, resource_metadata->GetEntryByResourceId( - "file_resource_id:file8")); - EXPECT_EQ(file9, resource_metadata->GetEntryByResourceId( - "file_resource_id:file9")); - EXPECT_EQ(file10, resource_metadata->GetEntryByResourceId( - "file_resource_id:file10")); + return dir.release(); } -// Callback for DriveResourceMetadata::InitFromDB. -void InitFromDBCallback(DriveFileError expected_error, - DriveFileError actual_error) { - EXPECT_EQ(expected_error, actual_error); -} +DriveFile* DriveResourceMetadataTest::AddFile(DriveDirectory* parent, + int sequence_id) { + scoped_ptr<DriveFile> file = resource_metadata_.CreateDriveFile(); + const std::string title = "file" + base::IntToString(sequence_id); + const std::string resource_id = std::string("file_resource_id:") + title; + file->set_title(title); + file->set_resource_id(resource_id); + file->set_file_md5(std::string("file_md5:") + title); -// Callback for DriveResourceMetadata::ReadDirectoryByPath. -void ReadDirectoryByPathCallback( - scoped_ptr<DriveEntryProtoVector>* result, - DriveFileError error, - scoped_ptr<DriveEntryProtoVector> entries) { - EXPECT_EQ(DRIVE_FILE_OK, error); - *result = entries.Pass(); -} + parent->AddEntry(file.get()); -} // namespace + return file.release(); +} -TEST(DriveResourceMetadataTest, VersionCheck) { +TEST_F(DriveResourceMetadataTest, VersionCheck) { // Set up the root directory. DriveRootDirectoryProto proto; DriveEntryProto* mutable_entry = @@ -237,7 +163,7 @@ TEST(DriveResourceMetadataTest, VersionCheck) { ASSERT_FALSE(resource_metadata.ParseFromString(serialized_proto)); } -TEST(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { +TEST_F(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { DriveResourceMetadata resource_metadata; // Look up the root directory by its resource ID. DriveEntry* entry = resource_metadata.GetEntryByResourceId( @@ -246,18 +172,12 @@ TEST(DriveResourceMetadataTest, GetEntryByResourceId_RootDirectory) { EXPECT_EQ(kDriveRootDirectoryResourceId, entry->resource_id()); } -TEST(DriveResourceMetadataTest, GetEntryInfoByResourceId) { - MessageLoopForUI message_loop; - content::TestBrowserThread ui_thread(content::BrowserThread::UI, - &message_loop); - DriveResourceMetadata resource_metadata; - InitDirectoryService(&resource_metadata); - +TEST_F(DriveResourceMetadataTest, GetEntryInfoByResourceId) { // Confirm that an existing file is found. DriveFileError error = DRIVE_FILE_ERROR_FAILED; FilePath drive_file_path; scoped_ptr<DriveEntryProto> entry_proto; - resource_metadata.GetEntryInfoByResourceId( + resource_metadata_.GetEntryInfoByResourceId( "file_resource_id:file4", base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, &error, &drive_file_path, &entry_proto)); @@ -270,7 +190,7 @@ TEST(DriveResourceMetadataTest, GetEntryInfoByResourceId) { // Confirm that a non existing file is not found. error = DRIVE_FILE_ERROR_FAILED; entry_proto.reset(); - resource_metadata.GetEntryInfoByResourceId( + resource_metadata_.GetEntryInfoByResourceId( "file:non_existing", base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, &error, &drive_file_path, &entry_proto)); @@ -279,17 +199,11 @@ TEST(DriveResourceMetadataTest, GetEntryInfoByResourceId) { EXPECT_FALSE(entry_proto.get()); } -TEST(DriveResourceMetadataTest, GetEntryInfoByPath) { - MessageLoopForUI message_loop; - content::TestBrowserThread ui_thread(content::BrowserThread::UI, - &message_loop); - DriveResourceMetadata resource_metadata; - InitDirectoryService(&resource_metadata); - +TEST_F(DriveResourceMetadataTest, GetEntryInfoByPath) { // Confirm that an existing file is found. DriveFileError error = DRIVE_FILE_ERROR_FAILED; scoped_ptr<DriveEntryProto> entry_proto; - resource_metadata.GetEntryInfoByPath( + resource_metadata_.GetEntryInfoByPath( FilePath::FromUTF8Unsafe("drive/dir1/file4"), base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, &error, &entry_proto)); @@ -301,7 +215,7 @@ TEST(DriveResourceMetadataTest, GetEntryInfoByPath) { // Confirm that a non existing file is not found. error = DRIVE_FILE_ERROR_FAILED; entry_proto.reset(); - resource_metadata.GetEntryInfoByPath( + resource_metadata_.GetEntryInfoByPath( FilePath::FromUTF8Unsafe("drive/dir1/non_existing"), base::Bind(&test_util::CopyResultsFromGetEntryInfoCallback, &error, &entry_proto)); @@ -310,17 +224,11 @@ TEST(DriveResourceMetadataTest, GetEntryInfoByPath) { EXPECT_FALSE(entry_proto.get()); } -TEST(DriveResourceMetadataTest, ReadDirectoryByPath) { - MessageLoopForUI message_loop; - content::TestBrowserThread ui_thread(content::BrowserThread::UI, - &message_loop); - DriveResourceMetadata resource_metadata; - InitDirectoryService(&resource_metadata); - +TEST_F(DriveResourceMetadataTest, ReadDirectoryByPath) { // Confirm that an existing directory is found. DriveFileError error = DRIVE_FILE_ERROR_FAILED; scoped_ptr<DriveEntryProtoVector> entries; - resource_metadata.ReadDirectoryByPath( + resource_metadata_.ReadDirectoryByPath( FilePath::FromUTF8Unsafe("drive/dir1"), base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, &error, &entries)); @@ -342,7 +250,7 @@ TEST(DriveResourceMetadataTest, ReadDirectoryByPath) { // Confirm that a non existing directory is not found. error = DRIVE_FILE_ERROR_FAILED; entries.reset(); - resource_metadata.ReadDirectoryByPath( + resource_metadata_.ReadDirectoryByPath( FilePath::FromUTF8Unsafe("drive/non_existing"), base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, &error, &entries)); @@ -353,7 +261,7 @@ TEST(DriveResourceMetadataTest, ReadDirectoryByPath) { // Confirm that reading a file results in DRIVE_FILE_ERROR_NOT_A_DIRECTORY. error = DRIVE_FILE_ERROR_FAILED; entries.reset(); - resource_metadata.ReadDirectoryByPath( + resource_metadata_.ReadDirectoryByPath( FilePath::FromUTF8Unsafe("drive/dir1/file4"), base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, &error, &entries)); @@ -362,16 +270,10 @@ TEST(DriveResourceMetadataTest, ReadDirectoryByPath) { EXPECT_FALSE(entries.get()); } -TEST(DriveResourceMetadataTest, GetEntryInfoPairByPaths) { - MessageLoopForUI message_loop; - content::TestBrowserThread ui_thread(content::BrowserThread::UI, - &message_loop); - DriveResourceMetadata resource_metadata; - InitDirectoryService(&resource_metadata); - +TEST_F(DriveResourceMetadataTest, GetEntryInfoPairByPaths) { // Confirm that existing two files are found. scoped_ptr<EntryInfoPairResult> pair_result; - resource_metadata.GetEntryInfoPairByPaths( + resource_metadata_.GetEntryInfoPairByPaths( FilePath::FromUTF8Unsafe("drive/dir1/file4"), FilePath::FromUTF8Unsafe("drive/dir1/file5"), base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, @@ -392,7 +294,7 @@ TEST(DriveResourceMetadataTest, GetEntryInfoPairByPaths) { // Confirm that the first non existent file is not found. pair_result.reset(); - resource_metadata.GetEntryInfoPairByPaths( + resource_metadata_.GetEntryInfoPairByPaths( FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), FilePath::FromUTF8Unsafe("drive/dir1/file5"), base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, @@ -410,7 +312,7 @@ TEST(DriveResourceMetadataTest, GetEntryInfoPairByPaths) { // Confirm that the second non existent file is not found. pair_result.reset(); - resource_metadata.GetEntryInfoPairByPaths( + resource_metadata_.GetEntryInfoPairByPaths( FilePath::FromUTF8Unsafe("drive/dir1/file4"), FilePath::FromUTF8Unsafe("drive/dir1/non_existent"), base::Bind(&test_util::CopyResultsFromGetEntryInfoPairCallback, @@ -429,53 +331,59 @@ TEST(DriveResourceMetadataTest, GetEntryInfoPairByPaths) { ASSERT_FALSE(pair_result->second.proto.get()); } -TEST(DriveResourceMetadataTest, DBTest) { - MessageLoopForUI message_loop; - content::TestBrowserThread ui_thread(content::BrowserThread::UI, - &message_loop); - - scoped_ptr<TestingProfile> profile(new TestingProfile); +TEST_F(DriveResourceMetadataTest, DBTest) { + TestingProfile profile; scoped_refptr<base::SequencedWorkerPool> pool = content::BrowserThread::GetBlockingPool(); scoped_refptr<base::SequencedTaskRunner> blocking_task_runner = pool->GetSequencedTaskRunner(pool->GetSequenceToken()); - DriveResourceMetadata resource_metadata; - FilePath db_path(DriveCache::GetCacheRootPath(profile.get()). + FilePath db_path(DriveCache::GetCacheRootPath(&profile). AppendASCII("meta").AppendASCII("resource_metadata.db")); // InitFromDB should fail with DRIVE_FILE_ERROR_NOT_FOUND since the db // doesn't exist. - resource_metadata.InitFromDB(db_path, blocking_task_runner, + resource_metadata_.InitFromDB(db_path, blocking_task_runner, base::Bind(&InitFromDBCallback, DRIVE_FILE_ERROR_NOT_FOUND)); test_util::RunBlockingPoolTask(); - InitDirectoryService(&resource_metadata); - // Write the filesystem to db. - resource_metadata.SaveToDB(); + // Create a file system and write it to disk. + // We cannot call SaveToDB without first having called InitFromDB because + // InitFrom initializes the db_path and blocking_task_runner needed by + // SaveToDB. + resource_metadata_.SaveToDB(); test_util::RunBlockingPoolTask(); - DriveResourceMetadata resource_metadata2; - // InitFromDB should succeed with DRIVE_FILE_OK as the db now exists. - resource_metadata2.InitFromDB(db_path, blocking_task_runner, + // InitFromDB should fail with DRIVE_FILE_ERROR_IN_USE. + resource_metadata_.InitFromDB(db_path, blocking_task_runner, + base::Bind(&InitFromDBCallback, DRIVE_FILE_ERROR_IN_USE)); + test_util::RunBlockingPoolTask(); + + // InitFromDB should succeed. + DriveResourceMetadata test_resource_metadata; + test_resource_metadata.InitFromDB(db_path, blocking_task_runner, base::Bind(&InitFromDBCallback, DRIVE_FILE_OK)); test_util::RunBlockingPoolTask(); - VerifyDirectoryService(&resource_metadata2); + // Verify by checking for drive/dir2, which should have 3 children. + DriveFileError error = DRIVE_FILE_ERROR_FAILED; + scoped_ptr<DriveEntryProtoVector> entries; + test_resource_metadata.ReadDirectoryByPath( + FilePath::FromUTF8Unsafe("drive/dir2"), + base::Bind(&test_util::CopyResultsFromReadDirectoryCallback, + &error, &entries)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + ASSERT_TRUE(entries.get()); + ASSERT_EQ(3U, entries->size()); } -TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { - MessageLoopForUI message_loop; - content::TestBrowserThread ui_thread(content::BrowserThread::UI, - &message_loop); - DriveResourceMetadata resource_metadata; - InitDirectoryService(&resource_metadata); - +TEST_F(DriveResourceMetadataTest, RemoveEntryFromParent) { // Make sure file9 is found. DriveFileError error = DRIVE_FILE_ERROR_FAILED; FilePath drive_file_path; const std::string file9_resource_id = "file_resource_id:file9"; scoped_ptr<DriveEntryProto> entry_proto; - resource_metadata.GetEntryInfoByResourceId( + resource_metadata_.GetEntryInfoByResourceId( file9_resource_id, base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, &error, &drive_file_path, &entry_proto)); @@ -486,7 +394,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_EQ("file9", entry_proto->base_name()); // Remove file9 using RemoveEntryFromParent. - resource_metadata.RemoveEntryFromParent( + resource_metadata_.RemoveEntryFromParent( file9_resource_id, base::Bind(&test_util::CopyResultsFromFileMoveCallback, &error, &drive_file_path)); @@ -495,7 +403,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/dir3"), drive_file_path); // file9 should no longer exist. - resource_metadata.GetEntryInfoByResourceId( + resource_metadata_.GetEntryInfoByResourceId( file9_resource_id, base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, &error, &drive_file_path, &entry_proto)); @@ -505,7 +413,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { // Look for dir3. const std::string dir3_resource_id = "dir_resource_id:dir3"; - resource_metadata.GetEntryInfoByResourceId( + resource_metadata_.GetEntryInfoByResourceId( dir3_resource_id, base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, &error, &drive_file_path, &entry_proto)); @@ -516,7 +424,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_EQ("dir3", entry_proto->base_name()); // Remove dir3 using RemoveEntryFromParent. - resource_metadata.RemoveEntryFromParent( + resource_metadata_.RemoveEntryFromParent( dir3_resource_id, base::Bind(&test_util::CopyResultsFromFileMoveCallback, &error, &drive_file_path)); @@ -525,7 +433,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1"), drive_file_path); // dir3 should no longer exist. - resource_metadata.GetEntryInfoByResourceId( + resource_metadata_.GetEntryInfoByResourceId( dir3_resource_id, base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, &error, &drive_file_path, &entry_proto)); @@ -534,7 +442,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_FALSE(entry_proto.get()); // Remove unknown resource_id using RemoveEntryFromParent. - resource_metadata.RemoveEntryFromParent( + resource_metadata_.RemoveEntryFromParent( "foo", base::Bind(&test_util::CopyResultsFromFileMoveCallback, &error, &drive_file_path)); @@ -542,7 +450,7 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); // Try removing root. This should fail. - resource_metadata.RemoveEntryFromParent( + resource_metadata_.RemoveEntryFromParent( kDriveRootDirectoryResourceId, base::Bind(&test_util::CopyResultsFromFileMoveCallback, &error, &drive_file_path)); @@ -550,4 +458,143 @@ TEST(DriveResourceMetadataTest, RemoveEntryFromParent) { EXPECT_EQ(DRIVE_FILE_ERROR_ACCESS_DENIED, error); } +TEST_F(DriveResourceMetadataTest, MoveEntryToDirectory) { + DriveFileError error = DRIVE_FILE_ERROR_FAILED; + FilePath drive_file_path; + scoped_ptr<DriveEntryProto> entry_proto; + + // Move file8 to drive/dir1. + resource_metadata_.MoveEntryToDirectory( + FilePath::FromUTF8Unsafe("drive/dir2/file8"), + FilePath::FromUTF8Unsafe("drive/dir1"), + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file8"), drive_file_path); + + // Look up the entry by its resource id and make sure it really moved. + resource_metadata_.GetEntryInfoByResourceId( + "file_resource_id:file8", + base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, + &error, &drive_file_path, &entry_proto)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir1/file8"), drive_file_path); + + // Move non-existent file to drive/dir1. This should fail. + resource_metadata_.MoveEntryToDirectory( + FilePath::FromUTF8Unsafe("drive/dir2/file8"), + FilePath::FromUTF8Unsafe("drive/dir1"), + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); + EXPECT_EQ(FilePath(), drive_file_path); + + // Move existing file to non-existent directory. This should fail. + resource_metadata_.MoveEntryToDirectory( + FilePath::FromUTF8Unsafe("drive/dir1/file8"), + FilePath::FromUTF8Unsafe("drive/dir4"), + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); + EXPECT_EQ(FilePath(), drive_file_path); + + // Move existing file to existing file (non-directory). This should fail. + resource_metadata_.MoveEntryToDirectory( + FilePath::FromUTF8Unsafe("drive/dir1/file8"), + FilePath::FromUTF8Unsafe("drive/dir1/file4"), + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_ERROR_NOT_A_DIRECTORY, error); + EXPECT_EQ(FilePath(), drive_file_path); + + // Move the file to root. + resource_metadata_.MoveEntryToDirectory( + FilePath::FromUTF8Unsafe("drive/dir1/file8"), + FilePath::FromUTF8Unsafe("drive"), + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/file8"), drive_file_path); + + // Move the file from root. + resource_metadata_.MoveEntryToDirectory( + FilePath::FromUTF8Unsafe("drive/file8"), + FilePath::FromUTF8Unsafe("drive/dir2"), + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file8"), drive_file_path); + + // Make sure file is still ok. + resource_metadata_.GetEntryInfoByResourceId( + "file_resource_id:file8", + base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, + &error, &drive_file_path, &entry_proto)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file8"), drive_file_path); +} + +TEST_F(DriveResourceMetadataTest, RenameEntry) { + DriveFileError error = DRIVE_FILE_ERROR_FAILED; + FilePath drive_file_path; + scoped_ptr<DriveEntryProto> entry_proto; + + // Rename file8 to file11. + resource_metadata_.RenameEntry( + FilePath::FromUTF8Unsafe("drive/dir2/file8"), + "file11", + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file11"), drive_file_path); + + // Lookup the file by resource id to make sure the file actually got renamed. + resource_metadata_.GetEntryInfoByResourceId( + "file_resource_id:file8", + base::Bind(&test_util::CopyResultsFromGetEntryInfoWithFilePathCallback, + &error, &drive_file_path, &entry_proto)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file11"), drive_file_path); + + // Rename to file7 to force a duplicate name. + resource_metadata_.RenameEntry( + FilePath::FromUTF8Unsafe("drive/dir2/file11"), + "file7", + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_OK, error); + EXPECT_EQ(FilePath::FromUTF8Unsafe("drive/dir2/file7 (2)"), drive_file_path); + + // Rename to same name. This should fail. + resource_metadata_.RenameEntry( + FilePath::FromUTF8Unsafe("drive/dir2/file7 (2)"), + "file7 (2)", + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_ERROR_EXISTS, error); + EXPECT_EQ(FilePath(), drive_file_path); + + // Rename non-existent. + resource_metadata_.RenameEntry( + FilePath::FromUTF8Unsafe("drive/dir2/file11"), + "file11", + base::Bind(&test_util::CopyResultsFromFileMoveCallback, + &error, &drive_file_path)); + test_util::RunBlockingPoolTask(); + EXPECT_EQ(DRIVE_FILE_ERROR_NOT_FOUND, error); + EXPECT_EQ(FilePath(), drive_file_path); +} + } // namespace gdata |