Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / drive / file_system / copy_operation_unittest.cc
index 2348382..dea22cf 100644 (file)
@@ -7,28 +7,40 @@
 #include "base/file_util.h"
 #include "base/task_runner_util.h"
 #include "chrome/browser/chromeos/drive/file_cache.h"
+#include "chrome/browser/chromeos/drive/file_change.h"
 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
 #include "chrome/browser/chromeos/drive/file_system_util.h"
+#include "chrome/browser/chromeos/drive/resource_metadata.h"
 #include "chrome/browser/drive/drive_api_util.h"
 #include "chrome/browser/drive/fake_drive_service.h"
-#include "google_apis/drive/gdata_wapi_parser.h"
+#include "content/public/test/test_utils.h"
+#include "google_apis/drive/drive_api_parser.h"
 #include "google_apis/drive/test_util.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
 namespace drive {
 namespace file_system {
 
+namespace {
+
+// Used to handle WaitForSyncComplete() calls.
+bool CopyWaitForSyncCompleteArguments(std::string* out_local_id,
+                                      FileOperationCallback* out_callback,
+                                      const std::string& local_id,
+                                      const FileOperationCallback& callback) {
+  *out_local_id = local_id;
+  *out_callback = callback;
+  return true;
+}
+
+}  // namespace
+
 class CopyOperationTest : public OperationTestBase {
  protected:
   virtual void SetUp() OVERRIDE {
    OperationTestBase::SetUp();
    operation_.reset(new CopyOperation(
-       blocking_task_runner(),
-       observer(),
-       scheduler(),
-       metadata(),
-       cache(),
-       util::GetIdentityResourceIdCanonicalizer()));
+       blocking_task_runner(), delegate(), scheduler(), metadata(), cache()));
   }
 
   scoped_ptr<CopyOperation> operation_;
@@ -53,32 +65,18 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_RegularFile) {
       local_src_path,
       remote_dest_path,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // TransferFileFromLocalToRemote stores a copy of the local file in the cache,
   // marks it dirty and requests the observer to upload the file.
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
-  EXPECT_EQ(1U, observer()->updated_local_ids().count(
-      GetLocalId(remote_dest_path)));
-  FileCacheEntry cache_entry;
-  bool found = false;
-  base::PostTaskAndReplyWithResult(
-      blocking_task_runner(),
-      FROM_HERE,
-      base::Bind(&internal::FileCache::GetCacheEntry,
-                 base::Unretained(cache()),
-                 GetLocalId(remote_dest_path),
-                 &cache_entry),
-      google_apis::test_util::CreateCopyResultCallback(&found));
-  test_util::RunBlockingPoolTask();
-  EXPECT_TRUE(found);
-  EXPECT_TRUE(cache_entry.is_present());
-  EXPECT_TRUE(cache_entry.is_dirty());
-
-  EXPECT_EQ(1U, observer()->get_changed_paths().size());
-  EXPECT_TRUE(observer()->get_changed_paths().count(
-      remote_dest_path.DirName()));
+  EXPECT_EQ(1U, delegate()->updated_local_ids().count(entry.local_id()));
+  EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
+  EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
+
+  EXPECT_EQ(1U, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(remote_dest_path));
 }
 
 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_Overwrite) {
@@ -99,29 +97,18 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_Overwrite) {
       local_src_path,
       remote_dest_path,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // TransferFileFromLocalToRemote stores a copy of the local file in the cache,
   // marks it dirty and requests the observer to upload the file.
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
-  EXPECT_EQ(1U, observer()->updated_local_ids().count(entry.local_id()));
-  FileCacheEntry cache_entry;
-  bool found = false;
-  base::PostTaskAndReplyWithResult(
-      blocking_task_runner(),
-      FROM_HERE,
-      base::Bind(&internal::FileCache::GetCacheEntry,
-                 base::Unretained(cache()), entry.local_id(), &cache_entry),
-      google_apis::test_util::CreateCopyResultCallback(&found));
-  test_util::RunBlockingPoolTask();
-  EXPECT_TRUE(found);
-  EXPECT_TRUE(cache_entry.is_present());
-  EXPECT_TRUE(cache_entry.is_dirty());
-
-  EXPECT_EQ(1U, observer()->get_changed_paths().size());
-  EXPECT_TRUE(observer()->get_changed_paths().count(
-      remote_dest_path.DirName()));
+  EXPECT_EQ(1U, delegate()->updated_local_ids().count(entry.local_id()));
+  EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
+  EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
+
+  EXPECT_EQ(1U, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(remote_dest_path));
 }
 
 TEST_F(CopyOperationTest,
@@ -131,11 +118,11 @@ TEST_F(CopyOperationTest,
       "drive/root/Directory 1/copied.gdoc"));
 
   // Prepare a local file, which is a json file of a hosted document, which
-  // matches "drive/root/Document 1 excludeDir-test" in root_feed.json.
+  // matches "drive/root/Document 1 excludeDir-test".
   ASSERT_TRUE(util::CreateGDocFile(
       local_src_path,
-      GURL("https://3_document_self_link/document:5_document_resource_id"),
-      "document:5_document_resource_id"));
+      GURL("https://3_document_self_link/5_document_resource_id"),
+      "5_document_resource_id"));
 
   ResourceEntry entry;
   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
@@ -147,16 +134,15 @@ TEST_F(CopyOperationTest,
       local_src_path,
       remote_dest_path,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
 
-  EXPECT_EQ(1U, observer()->get_changed_paths().size());
-  EXPECT_TRUE(
-      observer()->get_changed_paths().count(remote_dest_path.DirName()));
+  EXPECT_EQ(1U, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(remote_dest_path));
   // New copy is created.
-  EXPECT_NE("document:5_document_resource_id", entry.resource_id());
+  EXPECT_NE("5_document_resource_id", entry.resource_id());
 }
 
 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_OrphanHostedDocument) {
@@ -165,11 +151,11 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_OrphanHostedDocument) {
       "drive/root/Directory 1/moved.gdoc"));
 
   // Prepare a local file, which is a json file of a hosted document, which
-  // matches "drive/other/Orphan Document" in root_feed.json.
+  // matches "drive/other/Orphan Document".
   ASSERT_TRUE(util::CreateGDocFile(
       local_src_path,
-      GURL("https://3_document_self_link/document:orphan_doc_1"),
-      "document:orphan_doc_1"));
+      GURL("https://3_document_self_link/orphan_doc_1"),
+      "orphan_doc_1"));
 
   ResourceEntry entry;
   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
@@ -181,18 +167,17 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_OrphanHostedDocument) {
       local_src_path,
       remote_dest_path,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
   EXPECT_EQ(ResourceEntry::DIRTY, entry.metadata_edit_state());
-  EXPECT_TRUE(observer()->updated_local_ids().count(entry.local_id()));
+  EXPECT_TRUE(delegate()->updated_local_ids().count(entry.local_id()));
 
-  EXPECT_EQ(1U, observer()->get_changed_paths().size());
-  EXPECT_TRUE(
-      observer()->get_changed_paths().count(remote_dest_path.DirName()));
+  EXPECT_EQ(1U, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(remote_dest_path));
   // The original document got new parent.
-  EXPECT_EQ("document:orphan_doc_1", entry.resource_id());
+  EXPECT_EQ("orphan_doc_1", entry.resource_id());
 }
 
 TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_NewHostedDocument) {
@@ -202,19 +187,19 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_NewHostedDocument) {
 
   // Create a hosted document on the server that is not synced to local yet.
   google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR;
-  scoped_ptr<google_apis::ResourceEntry> new_gdoc_entry;
+  scoped_ptr<google_apis::FileResource> new_gdoc_entry;
   fake_service()->AddNewFile(
       "application/vnd.google-apps.document", "", "", "title", true,
       google_apis::test_util::CreateCopyResultCallback(&gdata_error,
                                                        &new_gdoc_entry));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error);
 
   // Prepare a local file, which is a json file of the added hosted document.
   ASSERT_TRUE(util::CreateGDocFile(
       local_src_path,
-      GURL("https://3_document_self_link/" + new_gdoc_entry->resource_id()),
-      new_gdoc_entry->resource_id()));
+      GURL("https://3_document_self_link/" + new_gdoc_entry->file_id()),
+      new_gdoc_entry->file_id()));
 
   ResourceEntry entry;
   ASSERT_EQ(FILE_ERROR_NOT_FOUND,
@@ -226,16 +211,15 @@ TEST_F(CopyOperationTest, TransferFileFromLocalToRemote_NewHostedDocument) {
       local_src_path,
       remote_dest_path,
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(remote_dest_path, &entry));
 
-  EXPECT_EQ(1U, observer()->get_changed_paths().size());
-  EXPECT_TRUE(
-      observer()->get_changed_paths().count(remote_dest_path.DirName()));
+  EXPECT_EQ(1U, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(remote_dest_path));
   // The original document got new parent.
-  EXPECT_EQ(new_gdoc_entry->resource_id(), entry.resource_id());
+  EXPECT_EQ(new_gdoc_entry->file_id(), entry.resource_id());
 }
 
 TEST_F(CopyOperationTest, CopyNotExistingFile) {
@@ -250,12 +234,12 @@ TEST_F(CopyOperationTest, CopyNotExistingFile) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
 
   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(src_path, &entry));
   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
-  EXPECT_TRUE(observer()->get_changed_paths().empty());
+  EXPECT_TRUE(delegate()->get_changed_files().empty());
 }
 
 TEST_F(CopyOperationTest, CopyFileToNonExistingDirectory) {
@@ -272,12 +256,12 @@ TEST_F(CopyOperationTest, CopyFileToNonExistingDirectory) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_NOT_FOUND, error);
 
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
-  EXPECT_TRUE(observer()->get_changed_paths().empty());
+  EXPECT_TRUE(delegate()->get_changed_files().empty());
 }
 
 // Test the case where the parent of the destination path is an existing file,
@@ -298,12 +282,12 @@ TEST_F(CopyOperationTest, CopyFileToInvalidPath) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_NOT_A_DIRECTORY, error);
 
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(src_path, &entry));
   EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntry(dest_path, &entry));
-  EXPECT_TRUE(observer()->get_changed_paths().empty());
+  EXPECT_TRUE(delegate()->get_changed_files().empty());
 }
 
 TEST_F(CopyOperationTest, CopyDirtyFile) {
@@ -330,7 +314,7 @@ TEST_F(CopyOperationTest, CopyDirtyFile) {
                  temp_file,
                  internal::FileCache::FILE_OPERATION_MOVE),
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   // Copy.
@@ -338,32 +322,20 @@ TEST_F(CopyOperationTest, CopyDirtyFile) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   ResourceEntry dest_entry;
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &dest_entry));
   EXPECT_EQ(ResourceEntry::DIRTY, dest_entry.metadata_edit_state());
 
-  EXPECT_EQ(1u, observer()->updated_local_ids().size());
-  EXPECT_TRUE(observer()->updated_local_ids().count(dest_entry.local_id()));
-  EXPECT_EQ(1u, observer()->get_changed_paths().size());
-  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));
+  EXPECT_EQ(1u, delegate()->updated_local_ids().size());
+  EXPECT_TRUE(delegate()->updated_local_ids().count(dest_entry.local_id()));
+  EXPECT_EQ(1u, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(dest_path));
 
   // Copied cache file should be dirty.
-  bool success = false;
-  FileCacheEntry cache_entry;
-  base::PostTaskAndReplyWithResult(
-      blocking_task_runner(),
-      FROM_HERE,
-      base::Bind(&internal::FileCache::GetCacheEntry,
-                 base::Unretained(cache()),
-                 dest_entry.local_id(),
-                 &cache_entry),
-      google_apis::test_util::CreateCopyResultCallback(&success));
-  test_util::RunBlockingPoolTask();
-  EXPECT_TRUE(success);
-  EXPECT_TRUE(cache_entry.is_dirty());
+  EXPECT_TRUE(dest_entry.file_specific_info().cache_state().is_dirty());
 
   // File contents should match.
   base::FilePath cache_file_path;
@@ -375,7 +347,7 @@ TEST_F(CopyOperationTest, CopyDirtyFile) {
                  dest_entry.local_id(),
                  &cache_file_path),
       google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   std::string copied_contents;
@@ -396,16 +368,16 @@ TEST_F(CopyOperationTest, CopyFileOverwriteFile) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   ResourceEntry new_dest_entry;
   EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &new_dest_entry));
 
-  EXPECT_EQ(1u, observer()->updated_local_ids().size());
-  EXPECT_TRUE(observer()->updated_local_ids().count(old_dest_entry.local_id()));
-  EXPECT_EQ(1u, observer()->get_changed_paths().size());
-  EXPECT_TRUE(observer()->get_changed_paths().count(dest_path.DirName()));
+  EXPECT_EQ(1u, delegate()->updated_local_ids().size());
+  EXPECT_TRUE(delegate()->updated_local_ids().count(old_dest_entry.local_id()));
+  EXPECT_EQ(1u, delegate()->get_changed_files().size());
+  EXPECT_TRUE(delegate()->get_changed_files().count(dest_path));
 }
 
 TEST_F(CopyOperationTest, CopyFileOverwriteDirectory) {
@@ -417,7 +389,7 @@ TEST_F(CopyOperationTest, CopyFileOverwriteDirectory) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_INVALID_OPERATION, error);
 }
 
@@ -436,7 +408,7 @@ TEST_F(CopyOperationTest, CopyDirectory) {
                    dest_path,
                    false,
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_NOT_A_FILE, error);
 }
 
@@ -454,7 +426,7 @@ TEST_F(CopyOperationTest, PreserveLastModified) {
                    dest_path,
                    true,  // Preserve last modified.
                    google_apis::test_util::CreateCopyResultCallback(&error));
-  test_util::RunBlockingPoolTask();
+  content::RunAllBlockingPoolTasksUntilIdle();
   EXPECT_EQ(FILE_ERROR_OK, error);
 
   ResourceEntry entry2;
@@ -464,5 +436,83 @@ TEST_F(CopyOperationTest, PreserveLastModified) {
             entry2.file_info().last_modified());
 }
 
+TEST_F(CopyOperationTest, WaitForSyncComplete) {
+  // Create a directory locally.
+  base::FilePath src_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
+  base::FilePath directory_path(FILE_PATH_LITERAL("drive/root/New Directory"));
+  base::FilePath dest_path = directory_path.AppendASCII("File 1.txt");
+
+  ResourceEntry directory_parent;
+  EXPECT_EQ(FILE_ERROR_OK,
+            GetLocalResourceEntry(directory_path.DirName(), &directory_parent));
+
+  ResourceEntry directory;
+  directory.set_parent_local_id(directory_parent.local_id());
+  directory.set_title(directory_path.BaseName().AsUTF8Unsafe());
+  directory.mutable_file_info()->set_is_directory(true);
+  directory.set_metadata_edit_state(ResourceEntry::DIRTY);
+
+  std::string directory_local_id;
+  FileError error = FILE_ERROR_FAILED;
+  base::PostTaskAndReplyWithResult(
+      blocking_task_runner(),
+      FROM_HERE,
+      base::Bind(&internal::ResourceMetadata::AddEntry,
+                 base::Unretained(metadata()), directory, &directory_local_id),
+      google_apis::test_util::CreateCopyResultCallback(&error));
+  content::RunAllBlockingPoolTasksUntilIdle();
+  EXPECT_EQ(FILE_ERROR_OK, error);
+
+  // Try to copy a file to the new directory which lacks resource ID.
+  // This should result in waiting for the directory to sync.
+  std::string waited_local_id;
+  FileOperationCallback pending_callback;
+  delegate()->set_wait_for_sync_complete_handler(
+      base::Bind(&CopyWaitForSyncCompleteArguments,
+                 &waited_local_id, &pending_callback));
+
+  FileError copy_error = FILE_ERROR_FAILED;
+  operation_->Copy(src_path,
+                   dest_path,
+                   true,  // Preserve last modified.
+                   google_apis::test_util::CreateCopyResultCallback(
+                       &copy_error));
+  content::RunAllBlockingPoolTasksUntilIdle();
+  EXPECT_EQ(directory_local_id, waited_local_id);
+  ASSERT_FALSE(pending_callback.is_null());
+
+  // Add a new directory to the server and store the resource ID locally.
+  google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR;
+  scoped_ptr<google_apis::FileResource> file_resource;
+  fake_service()->AddNewDirectory(
+      directory_parent.resource_id(),
+      directory.title(),
+      DriveServiceInterface::AddNewDirectoryOptions(),
+      google_apis::test_util::CreateCopyResultCallback(
+          &status, &file_resource));
+  content::RunAllBlockingPoolTasksUntilIdle();
+  EXPECT_EQ(google_apis::HTTP_CREATED, status);
+  ASSERT_TRUE(file_resource);
+
+  directory.set_local_id(directory_local_id);
+  directory.set_resource_id(file_resource->file_id());
+  base::PostTaskAndReplyWithResult(
+      blocking_task_runner(),
+      FROM_HERE,
+      base::Bind(&internal::ResourceMetadata::RefreshEntry,
+                 base::Unretained(metadata()), directory),
+      google_apis::test_util::CreateCopyResultCallback(&error));
+  content::RunAllBlockingPoolTasksUntilIdle();
+  EXPECT_EQ(FILE_ERROR_OK, error);
+
+  // Resume the copy operation.
+  pending_callback.Run(FILE_ERROR_OK);
+  content::RunAllBlockingPoolTasksUntilIdle();
+
+  EXPECT_EQ(FILE_ERROR_OK, copy_error);
+  ResourceEntry entry;
+  EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(dest_path, &entry));
+}
+
 }  // namespace file_system
 }  // namespace drive