| 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/sync_file_system/drive_backend/conflict_resolver.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/conflict_resolver.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 fileapi::FileSystemURL URL(const GURL& origin, | 39 fileapi::FileSystemURL URL(const GURL& origin, |
| 40 const std::string& path) { | 40 const std::string& path) { |
| 41 return CreateSyncableFileSystemURL( | 41 return CreateSyncableFileSystemURL( |
| 42 origin, base::FilePath::FromUTF8Unsafe(path)); | 42 origin, base::FilePath::FromUTF8Unsafe(path)); |
| 43 } | 43 } |
| 44 | 44 |
| 45 } // namespace | 45 } // namespace |
| 46 | 46 |
| 47 class ConflictResolverTest : public testing::Test, | 47 class ConflictResolverTest : public testing::Test { |
| 48 public SyncEngineContext { | |
| 49 public: | 48 public: |
| 50 typedef FakeRemoteChangeProcessor::URLToFileChangesMap URLToFileChangesMap; | 49 typedef FakeRemoteChangeProcessor::URLToFileChangesMap URLToFileChangesMap; |
| 51 | 50 |
| 52 ConflictResolverTest() | 51 ConflictResolverTest() |
| 53 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 52 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 54 virtual ~ConflictResolverTest() {} | 53 virtual ~ConflictResolverTest() {} |
| 55 | 54 |
| 56 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
| 57 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); | 56 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); |
| 58 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); | 57 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); |
| 59 | 58 |
| 60 fake_drive_service_.reset(new FakeDriveServiceWrapper); | 59 scoped_ptr<FakeDriveServiceWrapper> |
| 61 ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi( | 60 fake_drive_service(new FakeDriveServiceWrapper); |
| 61 ASSERT_TRUE(fake_drive_service->LoadAccountMetadataForWapi( |
| 62 "sync_file_system/account_metadata.json")); | 62 "sync_file_system/account_metadata.json")); |
| 63 ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi( | 63 ASSERT_TRUE(fake_drive_service->LoadResourceListForWapi( |
| 64 "gdata/empty_feed.json")); | 64 "gdata/empty_feed.json")); |
| 65 | 65 |
| 66 drive_uploader_.reset(new FakeDriveUploader(fake_drive_service_.get())); | 66 scoped_ptr<drive::DriveUploaderInterface> |
| 67 drive_uploader(new FakeDriveUploader(fake_drive_service.get())); |
| 67 fake_drive_helper_.reset(new FakeDriveServiceHelper( | 68 fake_drive_helper_.reset(new FakeDriveServiceHelper( |
| 68 fake_drive_service_.get(), drive_uploader_.get(), | 69 fake_drive_service.get(), drive_uploader.get(), kSyncRootFolderTitle)); |
| 69 kSyncRootFolderTitle)); | |
| 70 fake_remote_change_processor_.reset(new FakeRemoteChangeProcessor); | 70 fake_remote_change_processor_.reset(new FakeRemoteChangeProcessor); |
| 71 | 71 |
| 72 context_.reset(new SyncEngineContext( |
| 73 fake_drive_service.PassAs<drive::DriveServiceInterface>(), |
| 74 drive_uploader.Pass(), base::MessageLoopProxy::current())); |
| 75 context_->SetRemoteChangeProcessor(fake_remote_change_processor_.get()); |
| 76 |
| 72 RegisterSyncableFileSystem(); | 77 RegisterSyncableFileSystem(); |
| 73 | 78 |
| 74 sync_task_manager_.reset(new SyncTaskManager( | 79 sync_task_manager_.reset(new SyncTaskManager( |
| 75 base::WeakPtr<SyncTaskManager::Client>(), | 80 base::WeakPtr<SyncTaskManager::Client>(), |
| 76 10 /* maximum_background_task */)); | 81 10 /* maximum_background_task */)); |
| 77 sync_task_manager_->Initialize(SYNC_STATUS_OK); | 82 sync_task_manager_->Initialize(SYNC_STATUS_OK); |
| 78 } | 83 } |
| 79 | 84 |
| 80 virtual void TearDown() OVERRIDE { | 85 virtual void TearDown() OVERRIDE { |
| 81 sync_task_manager_.reset(); | 86 sync_task_manager_.reset(); |
| 82 | 87 |
| 83 RevokeSyncableFileSystem(); | 88 RevokeSyncableFileSystem(); |
| 84 | 89 |
| 85 fake_remote_change_processor_.reset(); | 90 fake_remote_change_processor_.reset(); |
| 86 metadata_database_.reset(); | |
| 87 fake_drive_helper_.reset(); | 91 fake_drive_helper_.reset(); |
| 88 drive_uploader_.reset(); | 92 context_.reset(); |
| 89 fake_drive_service_.reset(); | |
| 90 base::RunLoop().RunUntilIdle(); | 93 base::RunLoop().RunUntilIdle(); |
| 91 } | 94 } |
| 92 | 95 |
| 93 void InitializeMetadataDatabase() { | 96 void InitializeMetadataDatabase() { |
| 94 SyncEngineInitializer initializer(this, | 97 SyncEngineInitializer initializer(context_.get(), |
| 95 base::MessageLoopProxy::current(), | 98 base::MessageLoopProxy::current(), |
| 96 fake_drive_service_.get(), | 99 context_->GetDriveService(), |
| 97 database_dir_.path(), | 100 database_dir_.path(), |
| 98 in_memory_env_.get()); | 101 in_memory_env_.get()); |
| 99 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 102 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 100 initializer.RunSequential(CreateResultReceiver(&status)); | 103 initializer.RunSequential(CreateResultReceiver(&status)); |
| 101 base::RunLoop().RunUntilIdle(); | 104 base::RunLoop().RunUntilIdle(); |
| 102 EXPECT_EQ(SYNC_STATUS_OK, status); | 105 EXPECT_EQ(SYNC_STATUS_OK, status); |
| 103 metadata_database_ = initializer.PassMetadataDatabase(); | 106 context_->SetMetadataDatabase(initializer.PassMetadataDatabase()); |
| 104 } | 107 } |
| 105 | 108 |
| 106 void RegisterApp(const std::string& app_id, | 109 void RegisterApp(const std::string& app_id, |
| 107 const std::string& app_root_folder_id) { | 110 const std::string& app_root_folder_id) { |
| 108 SyncStatusCode status = SYNC_STATUS_FAILED; | 111 SyncStatusCode status = SYNC_STATUS_FAILED; |
| 109 metadata_database_->RegisterApp(app_id, app_root_folder_id, | 112 context_->GetMetadataDatabase()->RegisterApp(app_id, app_root_folder_id, |
| 110 CreateResultReceiver(&status)); | 113 CreateResultReceiver(&status)); |
| 111 base::RunLoop().RunUntilIdle(); | 114 base::RunLoop().RunUntilIdle(); |
| 112 EXPECT_EQ(SYNC_STATUS_OK, status); | 115 EXPECT_EQ(SYNC_STATUS_OK, status); |
| 113 } | 116 } |
| 114 | 117 |
| 115 virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE { | |
| 116 return fake_drive_service_.get(); | |
| 117 } | |
| 118 | |
| 119 virtual drive::DriveUploaderInterface* GetDriveUploader() OVERRIDE { | |
| 120 return drive_uploader_.get(); | |
| 121 } | |
| 122 | |
| 123 virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE { | |
| 124 return metadata_database_.get(); | |
| 125 } | |
| 126 | |
| 127 virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE { | |
| 128 return fake_remote_change_processor_.get(); | |
| 129 } | |
| 130 | |
| 131 virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE { | |
| 132 return base::MessageLoopProxy::current().get(); | |
| 133 } | |
| 134 | |
| 135 protected: | 118 protected: |
| 136 std::string CreateSyncRoot() { | 119 std::string CreateSyncRoot() { |
| 137 std::string sync_root_folder_id; | 120 std::string sync_root_folder_id; |
| 138 EXPECT_EQ(google_apis::HTTP_CREATED, | 121 EXPECT_EQ(google_apis::HTTP_CREATED, |
| 139 fake_drive_helper_->AddOrphanedFolder( | 122 fake_drive_helper_->AddOrphanedFolder( |
| 140 kSyncRootFolderTitle, &sync_root_folder_id)); | 123 kSyncRootFolderTitle, &sync_root_folder_id)); |
| 141 return sync_root_folder_id; | 124 return sync_root_folder_id; |
| 142 } | 125 } |
| 143 | 126 |
| 144 std::string CreateRemoteFolder(const std::string& parent_folder_id, | 127 std::string CreateRemoteFolder(const std::string& parent_folder_id, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 163 void CreateLocalFile(const fileapi::FileSystemURL& url) { | 146 void CreateLocalFile(const fileapi::FileSystemURL& url) { |
| 164 fake_remote_change_processor_->UpdateLocalFileMetadata( | 147 fake_remote_change_processor_->UpdateLocalFileMetadata( |
| 165 url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 148 url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 166 SYNC_FILE_TYPE_FILE)); | 149 SYNC_FILE_TYPE_FILE)); |
| 167 } | 150 } |
| 168 | 151 |
| 169 google_apis::GDataErrorCode AddFileToFolder( | 152 google_apis::GDataErrorCode AddFileToFolder( |
| 170 const std::string& parent_folder_id, | 153 const std::string& parent_folder_id, |
| 171 const std::string& file_id) { | 154 const std::string& file_id) { |
| 172 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; | 155 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 173 fake_drive_service_->AddResourceToDirectory( | 156 context_->GetDriveService()->AddResourceToDirectory( |
| 174 parent_folder_id, file_id, | 157 parent_folder_id, file_id, |
| 175 CreateResultReceiver(&error)); | 158 CreateResultReceiver(&error)); |
| 176 base::RunLoop().RunUntilIdle(); | 159 base::RunLoop().RunUntilIdle(); |
| 177 return error; | 160 return error; |
| 178 } | 161 } |
| 179 | 162 |
| 180 int CountParents(const std::string& file_id) { | 163 int CountParents(const std::string& file_id) { |
| 181 scoped_ptr<google_apis::ResourceEntry> entry; | 164 scoped_ptr<google_apis::ResourceEntry> entry; |
| 182 EXPECT_EQ(google_apis::HTTP_SUCCESS, | 165 EXPECT_EQ(google_apis::HTTP_SUCCESS, |
| 183 fake_drive_helper_->GetResourceEntry(file_id, &entry)); | 166 fake_drive_helper_->GetResourceEntry(file_id, &entry)); |
| 184 int count = 0; | 167 int count = 0; |
| 185 const ScopedVector<google_apis::Link>& links = entry->links(); | 168 const ScopedVector<google_apis::Link>& links = entry->links(); |
| 186 for (ScopedVector<google_apis::Link>::const_iterator itr = links.begin(); | 169 for (ScopedVector<google_apis::Link>::const_iterator itr = links.begin(); |
| 187 itr != links.end(); ++itr) { | 170 itr != links.end(); ++itr) { |
| 188 if ((*itr)->type() == google_apis::Link::LINK_PARENT) | 171 if ((*itr)->type() == google_apis::Link::LINK_PARENT) |
| 189 ++count; | 172 ++count; |
| 190 } | 173 } |
| 191 return count; | 174 return count; |
| 192 } | 175 } |
| 193 | 176 |
| 194 SyncStatusCode RunRemoteToLocalSyncer() { | 177 SyncStatusCode RunRemoteToLocalSyncer() { |
| 195 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 178 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 196 scoped_ptr<RemoteToLocalSyncer> syncer(new RemoteToLocalSyncer(this)); | 179 scoped_ptr<RemoteToLocalSyncer> syncer( |
| 180 new RemoteToLocalSyncer(context_.get())); |
| 197 syncer->RunSequential(CreateResultReceiver(&status)); | 181 syncer->RunSequential(CreateResultReceiver(&status)); |
| 198 base::RunLoop().RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
| 199 return status; | 183 return status; |
| 200 } | 184 } |
| 201 | 185 |
| 202 SyncStatusCode RunLocalToRemoteSyncer( | 186 SyncStatusCode RunLocalToRemoteSyncer( |
| 203 const fileapi::FileSystemURL& url, | 187 const fileapi::FileSystemURL& url, |
| 204 const FileChange& file_change) { | 188 const FileChange& file_change) { |
| 205 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 189 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 206 base::FilePath local_path = base::FilePath(FILE_PATH_LITERAL("dummy")); | 190 base::FilePath local_path = base::FilePath(FILE_PATH_LITERAL("dummy")); |
| 207 if (file_change.IsAddOrUpdate()) | 191 if (file_change.IsAddOrUpdate()) |
| 208 CreateTemporaryFileInDir(database_dir_.path(), &local_path); | 192 CreateTemporaryFileInDir(database_dir_.path(), &local_path); |
| 209 scoped_ptr<LocalToRemoteSyncer> syncer(new LocalToRemoteSyncer( | 193 scoped_ptr<LocalToRemoteSyncer> syncer(new LocalToRemoteSyncer( |
| 210 this, SyncFileMetadata(file_change.file_type(), 0, base::Time()), | 194 context_.get(), |
| 195 SyncFileMetadata(file_change.file_type(), 0, base::Time()), |
| 211 file_change, local_path, url)); | 196 file_change, local_path, url)); |
| 212 syncer->RunSequential(CreateResultReceiver(&status)); | 197 syncer->RunSequential(CreateResultReceiver(&status)); |
| 213 base::RunLoop().RunUntilIdle(); | 198 base::RunLoop().RunUntilIdle(); |
| 214 if (status == SYNC_STATUS_OK) | 199 if (status == SYNC_STATUS_OK) |
| 215 fake_remote_change_processor_->ClearLocalChanges(url); | 200 fake_remote_change_processor_->ClearLocalChanges(url); |
| 216 return status; | 201 return status; |
| 217 } | 202 } |
| 218 | 203 |
| 219 void RunRemoteToLocalSyncerUntilIdle() { | 204 void RunRemoteToLocalSyncerUntilIdle() { |
| 220 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 205 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 221 while (status != SYNC_STATUS_NO_CHANGE_TO_SYNC) | 206 while (status != SYNC_STATUS_NO_CHANGE_TO_SYNC) |
| 222 status = RunRemoteToLocalSyncer(); | 207 status = RunRemoteToLocalSyncer(); |
| 223 } | 208 } |
| 224 | 209 |
| 225 SyncStatusCode RunConflictResolver() { | 210 SyncStatusCode RunConflictResolver() { |
| 226 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 211 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 227 ConflictResolver resolver(this); | 212 ConflictResolver resolver(context_.get()); |
| 228 resolver.RunSequential(CreateResultReceiver(&status)); | 213 resolver.RunSequential(CreateResultReceiver(&status)); |
| 229 base::RunLoop().RunUntilIdle(); | 214 base::RunLoop().RunUntilIdle(); |
| 230 return status; | 215 return status; |
| 231 } | 216 } |
| 232 | 217 |
| 233 SyncStatusCode ListChanges() { | 218 SyncStatusCode ListChanges() { |
| 234 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 219 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 235 sync_task_manager_->ScheduleSyncTask( | 220 sync_task_manager_->ScheduleSyncTask( |
| 236 FROM_HERE, | 221 FROM_HERE, |
| 237 scoped_ptr<SyncTask>(new ListChangesTask(this)), | 222 scoped_ptr<SyncTask>(new ListChangesTask(context_.get())), |
| 238 SyncTaskManager::PRIORITY_MED, | 223 SyncTaskManager::PRIORITY_MED, |
| 239 CreateResultReceiver(&status)); | 224 CreateResultReceiver(&status)); |
| 240 base::RunLoop().RunUntilIdle(); | 225 base::RunLoop().RunUntilIdle(); |
| 241 return status; | 226 return status; |
| 242 } | 227 } |
| 243 | 228 |
| 244 ScopedVector<google_apis::ResourceEntry> | 229 ScopedVector<google_apis::ResourceEntry> |
| 245 GetResourceEntriesForParentAndTitle(const std::string& parent_folder_id, | 230 GetResourceEntriesForParentAndTitle(const std::string& parent_folder_id, |
| 246 const std::string& title) { | 231 const std::string& title) { |
| 247 ScopedVector<google_apis::ResourceEntry> entries; | 232 ScopedVector<google_apis::ResourceEntry> entries; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 267 void VerifyLocalChangeConsistency( | 252 void VerifyLocalChangeConsistency( |
| 268 const URLToFileChangesMap& expected_changes) { | 253 const URLToFileChangesMap& expected_changes) { |
| 269 fake_remote_change_processor_->VerifyConsistency(expected_changes); | 254 fake_remote_change_processor_->VerifyConsistency(expected_changes); |
| 270 } | 255 } |
| 271 | 256 |
| 272 private: | 257 private: |
| 273 content::TestBrowserThreadBundle thread_bundle_; | 258 content::TestBrowserThreadBundle thread_bundle_; |
| 274 base::ScopedTempDir database_dir_; | 259 base::ScopedTempDir database_dir_; |
| 275 scoped_ptr<leveldb::Env> in_memory_env_; | 260 scoped_ptr<leveldb::Env> in_memory_env_; |
| 276 | 261 |
| 277 scoped_ptr<FakeDriveServiceWrapper> fake_drive_service_; | 262 scoped_ptr<SyncEngineContext> context_; |
| 278 scoped_ptr<FakeDriveUploader> drive_uploader_; | |
| 279 scoped_ptr<FakeDriveServiceHelper> fake_drive_helper_; | 263 scoped_ptr<FakeDriveServiceHelper> fake_drive_helper_; |
| 280 scoped_ptr<MetadataDatabase> metadata_database_; | |
| 281 scoped_ptr<FakeRemoteChangeProcessor> fake_remote_change_processor_; | 264 scoped_ptr<FakeRemoteChangeProcessor> fake_remote_change_processor_; |
| 282 | 265 |
| 283 scoped_ptr<SyncTaskManager> sync_task_manager_; | 266 scoped_ptr<SyncTaskManager> sync_task_manager_; |
| 284 | 267 |
| 285 DISALLOW_COPY_AND_ASSIGN(ConflictResolverTest); | 268 DISALLOW_COPY_AND_ASSIGN(ConflictResolverTest); |
| 286 }; | 269 }; |
| 287 | 270 |
| 288 TEST_F(ConflictResolverTest, NoFileToBeResolved) { | 271 TEST_F(ConflictResolverTest, NoFileToBeResolved) { |
| 289 const GURL kOrigin("chrome-extension://example"); | 272 const GURL kOrigin("chrome-extension://example"); |
| 290 const std::string sync_root = CreateSyncRoot(); | 273 const std::string sync_root = CreateSyncRoot(); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 | 504 |
| 522 EXPECT_EQ(4, CountParents(file)); | 505 EXPECT_EQ(4, CountParents(file)); |
| 523 | 506 |
| 524 EXPECT_EQ(SYNC_STATUS_OK, RunConflictResolver()); | 507 EXPECT_EQ(SYNC_STATUS_OK, RunConflictResolver()); |
| 525 | 508 |
| 526 EXPECT_EQ(1, CountParents(file)); | 509 EXPECT_EQ(1, CountParents(file)); |
| 527 } | 510 } |
| 528 | 511 |
| 529 } // namespace drive_backend | 512 } // namespace drive_backend |
| 530 } // namespace sync_file_system | 513 } // namespace sync_file_system |
| OLD | NEW |