| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/drive/sync/remove_performer.h" | 5 #include "chrome/browser/chromeos/drive/sync/remove_performer.h" |
| 6 | 6 |
| 7 #include "base/task_runner_util.h" | 7 #include "base/task_runner_util.h" |
| 8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" | 8 #include "chrome/browser/chromeos/drive/file_system/operation_test_base.h" |
| 9 #include "chrome/browser/chromeos/drive/file_system_util.h" | 9 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 10 #include "chrome/browser/chromeos/drive/job_scheduler.h" | 10 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 11 #include "chrome/browser/chromeos/drive/resource_metadata.h" | 11 #include "chrome/browser/chromeos/drive/resource_metadata.h" |
| 12 #include "chrome/browser/drive/fake_drive_service.h" | 12 #include "chrome/browser/drive/fake_drive_service.h" |
| 13 #include "content/public/test/test_utils.h" |
| 13 #include "google_apis/drive/drive_api_parser.h" | 14 #include "google_apis/drive/drive_api_parser.h" |
| 14 #include "google_apis/drive/test_util.h" | 15 #include "google_apis/drive/test_util.h" |
| 15 | 16 |
| 16 namespace drive { | 17 namespace drive { |
| 17 namespace internal { | 18 namespace internal { |
| 18 | 19 |
| 19 typedef file_system::OperationTestBase RemovePerformerTest; | 20 typedef file_system::OperationTestBase RemovePerformerTest; |
| 20 | 21 |
| 21 TEST_F(RemovePerformerTest, RemoveFile) { | 22 TEST_F(RemovePerformerTest, RemoveFile) { |
| 22 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(), | 23 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(), |
| 23 metadata()); | 24 metadata()); |
| 24 | 25 |
| 25 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); | 26 base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt")); |
| 26 base::FilePath file_in_subdir( | 27 base::FilePath file_in_subdir( |
| 27 FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt")); | 28 FILE_PATH_LITERAL("drive/root/Directory 1/SubDirectory File 1.txt")); |
| 28 | 29 |
| 29 // Remove a file in root. | 30 // Remove a file in root. |
| 30 ResourceEntry entry; | 31 ResourceEntry entry; |
| 31 FileError error = FILE_ERROR_FAILED; | 32 FileError error = FILE_ERROR_FAILED; |
| 32 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry)); | 33 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &entry)); |
| 33 performer.Remove(entry.local_id(), | 34 performer.Remove(entry.local_id(), |
| 34 ClientContext(USER_INITIATED), | 35 ClientContext(USER_INITIATED), |
| 35 google_apis::test_util::CreateCopyResultCallback(&error)); | 36 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 36 test_util::RunBlockingPoolTask(); | 37 content::RunAllBlockingPoolTasksUntilIdle(); |
| 37 EXPECT_EQ(FILE_ERROR_OK, error); | 38 EXPECT_EQ(FILE_ERROR_OK, error); |
| 38 | 39 |
| 39 // Remove a file in subdirectory. | 40 // Remove a file in subdirectory. |
| 40 error = FILE_ERROR_FAILED; | 41 error = FILE_ERROR_FAILED; |
| 41 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry)); | 42 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_subdir, &entry)); |
| 42 const std::string resource_id = entry.resource_id(); | 43 const std::string resource_id = entry.resource_id(); |
| 43 | 44 |
| 44 performer.Remove(entry.local_id(), | 45 performer.Remove(entry.local_id(), |
| 45 ClientContext(USER_INITIATED), | 46 ClientContext(USER_INITIATED), |
| 46 google_apis::test_util::CreateCopyResultCallback(&error)); | 47 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 47 test_util::RunBlockingPoolTask(); | 48 content::RunAllBlockingPoolTasksUntilIdle(); |
| 48 EXPECT_EQ(FILE_ERROR_OK, error); | 49 EXPECT_EQ(FILE_ERROR_OK, error); |
| 49 | 50 |
| 50 // Verify the file is indeed removed in the server. | 51 // Verify the file is indeed removed in the server. |
| 51 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR; | 52 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR; |
| 52 scoped_ptr<google_apis::FileResource> gdata_entry; | 53 scoped_ptr<google_apis::FileResource> gdata_entry; |
| 53 fake_service()->GetFileResource( | 54 fake_service()->GetFileResource( |
| 54 resource_id, | 55 resource_id, |
| 55 google_apis::test_util::CreateCopyResultCallback(&gdata_error, | 56 google_apis::test_util::CreateCopyResultCallback(&gdata_error, |
| 56 &gdata_entry)); | 57 &gdata_entry)); |
| 57 test_util::RunBlockingPoolTask(); | 58 content::RunAllBlockingPoolTasksUntilIdle(); |
| 58 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error); | 59 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error); |
| 59 EXPECT_TRUE(gdata_entry->labels().is_trashed()); | 60 EXPECT_TRUE(gdata_entry->labels().is_trashed()); |
| 60 | 61 |
| 61 // Try removing non-existing file. | 62 // Try removing non-existing file. |
| 62 error = FILE_ERROR_FAILED; | 63 error = FILE_ERROR_FAILED; |
| 63 performer.Remove("non-existing-id", | 64 performer.Remove("non-existing-id", |
| 64 ClientContext(USER_INITIATED), | 65 ClientContext(USER_INITIATED), |
| 65 google_apis::test_util::CreateCopyResultCallback(&error)); | 66 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 66 test_util::RunBlockingPoolTask(); | 67 content::RunAllBlockingPoolTasksUntilIdle(); |
| 67 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); | 68 EXPECT_EQ(FILE_ERROR_NOT_FOUND, error); |
| 68 } | 69 } |
| 69 | 70 |
| 70 TEST_F(RemovePerformerTest, RemoveShared) { | 71 TEST_F(RemovePerformerTest, RemoveShared) { |
| 71 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(), | 72 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(), |
| 72 metadata()); | 73 metadata()); |
| 73 | 74 |
| 74 const base::FilePath kPathInMyDrive(FILE_PATH_LITERAL( | 75 const base::FilePath kPathInMyDrive(FILE_PATH_LITERAL( |
| 75 "drive/root/shared.txt")); | 76 "drive/root/shared.txt")); |
| 76 const base::FilePath kPathInOther(FILE_PATH_LITERAL( | 77 const base::FilePath kPathInOther(FILE_PATH_LITERAL( |
| 77 "drive/other/shared.txt")); | 78 "drive/other/shared.txt")); |
| 78 | 79 |
| 79 // Prepare a shared file to the root folder. | 80 // Prepare a shared file to the root folder. |
| 80 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR; | 81 google_apis::GDataErrorCode gdata_error = google_apis::GDATA_OTHER_ERROR; |
| 81 scoped_ptr<google_apis::FileResource> gdata_entry; | 82 scoped_ptr<google_apis::FileResource> gdata_entry; |
| 82 fake_service()->AddNewFile( | 83 fake_service()->AddNewFile( |
| 83 "text/plain", | 84 "text/plain", |
| 84 "dummy content", | 85 "dummy content", |
| 85 fake_service()->GetRootResourceId(), | 86 fake_service()->GetRootResourceId(), |
| 86 kPathInMyDrive.BaseName().AsUTF8Unsafe(), | 87 kPathInMyDrive.BaseName().AsUTF8Unsafe(), |
| 87 true, // shared_with_me, | 88 true, // shared_with_me, |
| 88 google_apis::test_util::CreateCopyResultCallback(&gdata_error, | 89 google_apis::test_util::CreateCopyResultCallback(&gdata_error, |
| 89 &gdata_entry)); | 90 &gdata_entry)); |
| 90 test_util::RunBlockingPoolTask(); | 91 content::RunAllBlockingPoolTasksUntilIdle(); |
| 91 ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error); | 92 ASSERT_EQ(google_apis::HTTP_CREATED, gdata_error); |
| 92 CheckForUpdates(); | 93 CheckForUpdates(); |
| 93 | 94 |
| 94 // Remove it. Locally, the file should be moved to drive/other. | 95 // Remove it. Locally, the file should be moved to drive/other. |
| 95 ResourceEntry entry; | 96 ResourceEntry entry; |
| 96 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInMyDrive, &entry)); | 97 ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInMyDrive, &entry)); |
| 97 FileError error = FILE_ERROR_FAILED; | 98 FileError error = FILE_ERROR_FAILED; |
| 98 performer.Remove(entry.local_id(), | 99 performer.Remove(entry.local_id(), |
| 99 ClientContext(USER_INITIATED), | 100 ClientContext(USER_INITIATED), |
| 100 google_apis::test_util::CreateCopyResultCallback(&error)); | 101 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 101 test_util::RunBlockingPoolTask(); | 102 content::RunAllBlockingPoolTasksUntilIdle(); |
| 102 EXPECT_EQ(FILE_ERROR_OK, error); | 103 EXPECT_EQ(FILE_ERROR_OK, error); |
| 103 EXPECT_EQ(FILE_ERROR_NOT_FOUND, | 104 EXPECT_EQ(FILE_ERROR_NOT_FOUND, |
| 104 GetLocalResourceEntry(kPathInMyDrive, &entry)); | 105 GetLocalResourceEntry(kPathInMyDrive, &entry)); |
| 105 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInOther, &entry)); | 106 EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(kPathInOther, &entry)); |
| 106 | 107 |
| 107 // Remotely, the entry should have lost its parent. | 108 // Remotely, the entry should have lost its parent. |
| 108 gdata_error = google_apis::GDATA_OTHER_ERROR; | 109 gdata_error = google_apis::GDATA_OTHER_ERROR; |
| 109 const std::string resource_id = gdata_entry->file_id(); | 110 const std::string resource_id = gdata_entry->file_id(); |
| 110 fake_service()->GetFileResource( | 111 fake_service()->GetFileResource( |
| 111 resource_id, | 112 resource_id, |
| 112 google_apis::test_util::CreateCopyResultCallback(&gdata_error, | 113 google_apis::test_util::CreateCopyResultCallback(&gdata_error, |
| 113 &gdata_entry)); | 114 &gdata_entry)); |
| 114 test_util::RunBlockingPoolTask(); | 115 content::RunAllBlockingPoolTasksUntilIdle(); |
| 115 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error); | 116 ASSERT_EQ(google_apis::HTTP_SUCCESS, gdata_error); |
| 116 EXPECT_FALSE(gdata_entry->labels().is_trashed()); // It's not deleted. | 117 EXPECT_FALSE(gdata_entry->labels().is_trashed()); // It's not deleted. |
| 117 EXPECT_TRUE(gdata_entry->parents().empty()); | 118 EXPECT_TRUE(gdata_entry->parents().empty()); |
| 118 } | 119 } |
| 119 | 120 |
| 120 TEST_F(RemovePerformerTest, RemoveLocallyCreatedFile) { | 121 TEST_F(RemovePerformerTest, RemoveLocallyCreatedFile) { |
| 121 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(), | 122 RemovePerformer performer(blocking_task_runner(), observer(), scheduler(), |
| 122 metadata()); | 123 metadata()); |
| 123 | 124 |
| 124 // Add an entry without resource ID. | 125 // Add an entry without resource ID. |
| 125 ResourceEntry entry; | 126 ResourceEntry entry; |
| 126 entry.set_title("New File.txt"); | 127 entry.set_title("New File.txt"); |
| 127 entry.set_parent_local_id(util::kDriveTrashDirLocalId); | 128 entry.set_parent_local_id(util::kDriveTrashDirLocalId); |
| 128 | 129 |
| 129 FileError error = FILE_ERROR_FAILED; | 130 FileError error = FILE_ERROR_FAILED; |
| 130 std::string local_id; | 131 std::string local_id; |
| 131 base::PostTaskAndReplyWithResult( | 132 base::PostTaskAndReplyWithResult( |
| 132 blocking_task_runner(), | 133 blocking_task_runner(), |
| 133 FROM_HERE, | 134 FROM_HERE, |
| 134 base::Bind(&ResourceMetadata::AddEntry, | 135 base::Bind(&ResourceMetadata::AddEntry, |
| 135 base::Unretained(metadata()), | 136 base::Unretained(metadata()), |
| 136 entry, | 137 entry, |
| 137 &local_id), | 138 &local_id), |
| 138 google_apis::test_util::CreateCopyResultCallback(&error)); | 139 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 139 test_util::RunBlockingPoolTask(); | 140 content::RunAllBlockingPoolTasksUntilIdle(); |
| 140 EXPECT_EQ(FILE_ERROR_OK, error); | 141 EXPECT_EQ(FILE_ERROR_OK, error); |
| 141 | 142 |
| 142 // Remove the entry. | 143 // Remove the entry. |
| 143 performer.Remove(local_id, ClientContext(USER_INITIATED), | 144 performer.Remove(local_id, ClientContext(USER_INITIATED), |
| 144 google_apis::test_util::CreateCopyResultCallback(&error)); | 145 google_apis::test_util::CreateCopyResultCallback(&error)); |
| 145 test_util::RunBlockingPoolTask(); | 146 content::RunAllBlockingPoolTasksUntilIdle(); |
| 146 EXPECT_EQ(FILE_ERROR_OK, error); | 147 EXPECT_EQ(FILE_ERROR_OK, error); |
| 147 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry)); | 148 EXPECT_EQ(FILE_ERROR_NOT_FOUND, GetLocalResourceEntryById(local_id, &entry)); |
| 148 } | 149 } |
| 149 | 150 |
| 150 } // namespace internal | 151 } // namespace internal |
| 151 } // namespace drive | 152 } // namespace drive |
| OLD | NEW |