Chromium Code Reviews| 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 25 matching lines...) Expand all Loading... | |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 fileapi::FileSystemURL URL(const GURL& origin, | 38 fileapi::FileSystemURL URL(const GURL& origin, |
| 39 const std::string& path) { | 39 const std::string& path) { |
| 40 return CreateSyncableFileSystemURL( | 40 return CreateSyncableFileSystemURL( |
| 41 origin, base::FilePath::FromUTF8Unsafe(path)); | 41 origin, base::FilePath::FromUTF8Unsafe(path)); |
| 42 } | 42 } |
| 43 | 43 |
| 44 } // namespace | 44 } // namespace |
| 45 | 45 |
| 46 class ConflictResolverTest : public testing::Test, | 46 class ConflictResolverTest : public testing::Test { |
| 47 public SyncEngineContext { | |
| 48 public: | 47 public: |
| 49 typedef FakeRemoteChangeProcessor::URLToFileChangesMap URLToFileChangesMap; | 48 typedef FakeRemoteChangeProcessor::URLToFileChangesMap URLToFileChangesMap; |
| 50 | 49 |
| 51 ConflictResolverTest() | 50 ConflictResolverTest() |
| 52 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 51 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 53 virtual ~ConflictResolverTest() {} | 52 virtual ~ConflictResolverTest() {} |
| 54 | 53 |
| 55 virtual void SetUp() OVERRIDE { | 54 virtual void SetUp() OVERRIDE { |
| 56 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); | 55 ASSERT_TRUE(database_dir_.CreateUniqueTempDir()); |
| 57 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); | 56 in_memory_env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); |
| 58 | 57 |
| 59 fake_drive_service_.reset(new FakeDriveServiceWrapper); | 58 scoped_ptr<FakeDriveServiceWrapper> fake_drive_service; |
| 60 ASSERT_TRUE(fake_drive_service_->LoadAccountMetadataForWapi( | 59 fake_drive_service.reset(new FakeDriveServiceWrapper); |
|
tzik
2014/03/27 11:24:17
Just like below should work:
scoped_ptr<FakeDriveS
peria
2014/03/28 03:20:23
Done.
| |
| 60 ASSERT_TRUE(fake_drive_service->LoadAccountMetadataForWapi( | |
| 61 "sync_file_system/account_metadata.json")); | 61 "sync_file_system/account_metadata.json")); |
| 62 ASSERT_TRUE(fake_drive_service_->LoadResourceListForWapi( | 62 ASSERT_TRUE(fake_drive_service->LoadResourceListForWapi( |
| 63 "gdata/empty_feed.json")); | 63 "gdata/empty_feed.json")); |
| 64 | 64 |
| 65 drive_uploader_.reset(new FakeDriveUploader(fake_drive_service_.get())); | 65 scoped_ptr<FakeDriveUploader> drive_uploader; |
| 66 drive_uploader.reset(new FakeDriveUploader(fake_drive_service.get())); | |
|
tzik
2014/03/27 11:24:17
How about rewrite like this? So that we don't need
peria
2014/03/28 03:20:23
Done.
| |
| 66 fake_drive_helper_.reset(new FakeDriveServiceHelper( | 67 fake_drive_helper_.reset(new FakeDriveServiceHelper( |
| 67 fake_drive_service_.get(), drive_uploader_.get(), | 68 fake_drive_service.get(), drive_uploader.get(), |
| 68 kSyncRootFolderTitle)); | 69 kSyncRootFolderTitle)); |
| 69 fake_remote_change_processor_.reset(new FakeRemoteChangeProcessor); | 70 fake_remote_change_processor_.reset(new FakeRemoteChangeProcessor); |
| 70 | 71 |
| 72 context_.reset(new SyncEngineContext( | |
| 73 fake_drive_service.PassAs<drive::DriveServiceInterface>(), | |
| 74 drive_uploader.PassAs<drive::DriveUploaderInterface>(), | |
| 75 base::MessageLoopProxy::current())); | |
| 76 context_->SetRemoteChangeProcessor(fake_remote_change_processor_.get()); | |
| 77 | |
| 71 RegisterSyncableFileSystem(); | 78 RegisterSyncableFileSystem(); |
| 72 } | 79 } |
| 73 | 80 |
| 74 virtual void TearDown() OVERRIDE { | 81 virtual void TearDown() OVERRIDE { |
| 75 RevokeSyncableFileSystem(); | 82 RevokeSyncableFileSystem(); |
| 76 | 83 |
| 77 fake_remote_change_processor_.reset(); | 84 fake_remote_change_processor_.reset(); |
| 78 metadata_database_.reset(); | |
| 79 fake_drive_helper_.reset(); | 85 fake_drive_helper_.reset(); |
| 80 drive_uploader_.reset(); | 86 context_.reset(); |
| 81 fake_drive_service_.reset(); | |
| 82 base::RunLoop().RunUntilIdle(); | 87 base::RunLoop().RunUntilIdle(); |
| 83 } | 88 } |
| 84 | 89 |
| 85 void InitializeMetadataDatabase() { | 90 void InitializeMetadataDatabase() { |
| 86 SyncEngineInitializer initializer(this, | 91 SyncEngineInitializer initializer(context_.get(), |
| 87 base::MessageLoopProxy::current(), | 92 base::MessageLoopProxy::current(), |
| 88 fake_drive_service_.get(), | 93 context_->GetDriveService(), |
| 89 database_dir_.path(), | 94 database_dir_.path(), |
| 90 in_memory_env_.get()); | 95 in_memory_env_.get()); |
| 91 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 96 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 92 initializer.RunSequential(CreateResultReceiver(&status)); | 97 initializer.RunSequential(CreateResultReceiver(&status)); |
| 93 base::RunLoop().RunUntilIdle(); | 98 base::RunLoop().RunUntilIdle(); |
| 94 EXPECT_EQ(SYNC_STATUS_OK, status); | 99 EXPECT_EQ(SYNC_STATUS_OK, status); |
| 95 metadata_database_ = initializer.PassMetadataDatabase(); | 100 context_->SetMetadataDatabase(initializer.PassMetadataDatabase()); |
| 96 } | 101 } |
| 97 | 102 |
| 98 void RegisterApp(const std::string& app_id, | 103 void RegisterApp(const std::string& app_id, |
| 99 const std::string& app_root_folder_id) { | 104 const std::string& app_root_folder_id) { |
| 100 SyncStatusCode status = SYNC_STATUS_FAILED; | 105 SyncStatusCode status = SYNC_STATUS_FAILED; |
| 101 metadata_database_->RegisterApp(app_id, app_root_folder_id, | 106 context_->GetMetadataDatabase()->RegisterApp(app_id, app_root_folder_id, |
| 102 CreateResultReceiver(&status)); | 107 CreateResultReceiver(&status)); |
| 103 base::RunLoop().RunUntilIdle(); | 108 base::RunLoop().RunUntilIdle(); |
| 104 EXPECT_EQ(SYNC_STATUS_OK, status); | 109 EXPECT_EQ(SYNC_STATUS_OK, status); |
| 105 } | 110 } |
| 106 | 111 |
| 107 virtual drive::DriveServiceInterface* GetDriveService() OVERRIDE { | |
| 108 return fake_drive_service_.get(); | |
| 109 } | |
| 110 | |
| 111 virtual drive::DriveUploaderInterface* GetDriveUploader() OVERRIDE { | |
| 112 return drive_uploader_.get(); | |
| 113 } | |
| 114 | |
| 115 virtual MetadataDatabase* GetMetadataDatabase() OVERRIDE { | |
| 116 return metadata_database_.get(); | |
| 117 } | |
| 118 | |
| 119 virtual RemoteChangeProcessor* GetRemoteChangeProcessor() OVERRIDE { | |
| 120 return fake_remote_change_processor_.get(); | |
| 121 } | |
| 122 | |
| 123 virtual base::SequencedTaskRunner* GetBlockingTaskRunner() OVERRIDE { | |
| 124 return base::MessageLoopProxy::current().get(); | |
| 125 } | |
| 126 | |
| 127 protected: | 112 protected: |
| 128 std::string CreateSyncRoot() { | 113 std::string CreateSyncRoot() { |
| 129 std::string sync_root_folder_id; | 114 std::string sync_root_folder_id; |
| 130 EXPECT_EQ(google_apis::HTTP_CREATED, | 115 EXPECT_EQ(google_apis::HTTP_CREATED, |
| 131 fake_drive_helper_->AddOrphanedFolder( | 116 fake_drive_helper_->AddOrphanedFolder( |
| 132 kSyncRootFolderTitle, &sync_root_folder_id)); | 117 kSyncRootFolderTitle, &sync_root_folder_id)); |
| 133 return sync_root_folder_id; | 118 return sync_root_folder_id; |
| 134 } | 119 } |
| 135 | 120 |
| 136 std::string CreateRemoteFolder(const std::string& parent_folder_id, | 121 std::string CreateRemoteFolder(const std::string& parent_folder_id, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 155 void CreateLocalFile(const fileapi::FileSystemURL& url) { | 140 void CreateLocalFile(const fileapi::FileSystemURL& url) { |
| 156 fake_remote_change_processor_->UpdateLocalFileMetadata( | 141 fake_remote_change_processor_->UpdateLocalFileMetadata( |
| 157 url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, | 142 url, FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE, |
| 158 SYNC_FILE_TYPE_FILE)); | 143 SYNC_FILE_TYPE_FILE)); |
| 159 } | 144 } |
| 160 | 145 |
| 161 google_apis::GDataErrorCode AddFileToFolder( | 146 google_apis::GDataErrorCode AddFileToFolder( |
| 162 const std::string& parent_folder_id, | 147 const std::string& parent_folder_id, |
| 163 const std::string& file_id) { | 148 const std::string& file_id) { |
| 164 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; | 149 google_apis::GDataErrorCode error = google_apis::GDATA_OTHER_ERROR; |
| 165 fake_drive_service_->AddResourceToDirectory( | 150 context_->GetDriveService()->AddResourceToDirectory( |
| 166 parent_folder_id, file_id, | 151 parent_folder_id, file_id, |
| 167 CreateResultReceiver(&error)); | 152 CreateResultReceiver(&error)); |
| 168 base::RunLoop().RunUntilIdle(); | 153 base::RunLoop().RunUntilIdle(); |
| 169 return error; | 154 return error; |
| 170 } | 155 } |
| 171 | 156 |
| 172 int CountParents(const std::string& file_id) { | 157 int CountParents(const std::string& file_id) { |
| 173 scoped_ptr<google_apis::ResourceEntry> entry; | 158 scoped_ptr<google_apis::ResourceEntry> entry; |
| 174 EXPECT_EQ(google_apis::HTTP_SUCCESS, | 159 EXPECT_EQ(google_apis::HTTP_SUCCESS, |
| 175 fake_drive_helper_->GetResourceEntry(file_id, &entry)); | 160 fake_drive_helper_->GetResourceEntry(file_id, &entry)); |
| 176 int count = 0; | 161 int count = 0; |
| 177 const ScopedVector<google_apis::Link>& links = entry->links(); | 162 const ScopedVector<google_apis::Link>& links = entry->links(); |
| 178 for (ScopedVector<google_apis::Link>::const_iterator itr = links.begin(); | 163 for (ScopedVector<google_apis::Link>::const_iterator itr = links.begin(); |
| 179 itr != links.end(); ++itr) { | 164 itr != links.end(); ++itr) { |
| 180 if ((*itr)->type() == google_apis::Link::LINK_PARENT) | 165 if ((*itr)->type() == google_apis::Link::LINK_PARENT) |
| 181 ++count; | 166 ++count; |
| 182 } | 167 } |
| 183 return count; | 168 return count; |
| 184 } | 169 } |
| 185 | 170 |
| 186 SyncStatusCode RunRemoteToLocalSyncer() { | 171 SyncStatusCode RunRemoteToLocalSyncer() { |
| 187 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 172 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 188 scoped_ptr<RemoteToLocalSyncer> syncer(new RemoteToLocalSyncer(this)); | 173 scoped_ptr<RemoteToLocalSyncer> syncer( |
| 174 new RemoteToLocalSyncer(context_.get())); | |
| 189 syncer->RunSequential(CreateResultReceiver(&status)); | 175 syncer->RunSequential(CreateResultReceiver(&status)); |
| 190 base::RunLoop().RunUntilIdle(); | 176 base::RunLoop().RunUntilIdle(); |
| 191 return status; | 177 return status; |
| 192 } | 178 } |
| 193 | 179 |
| 194 SyncStatusCode RunLocalToRemoteSyncer( | 180 SyncStatusCode RunLocalToRemoteSyncer( |
| 195 const fileapi::FileSystemURL& url, | 181 const fileapi::FileSystemURL& url, |
| 196 const FileChange& file_change) { | 182 const FileChange& file_change) { |
| 197 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 183 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 198 base::FilePath local_path = base::FilePath(FILE_PATH_LITERAL("dummy")); | 184 base::FilePath local_path = base::FilePath(FILE_PATH_LITERAL("dummy")); |
| 199 if (file_change.IsAddOrUpdate()) | 185 if (file_change.IsAddOrUpdate()) |
| 200 CreateTemporaryFileInDir(database_dir_.path(), &local_path); | 186 CreateTemporaryFileInDir(database_dir_.path(), &local_path); |
| 201 scoped_ptr<LocalToRemoteSyncer> syncer(new LocalToRemoteSyncer( | 187 scoped_ptr<LocalToRemoteSyncer> syncer(new LocalToRemoteSyncer( |
| 202 this, SyncFileMetadata(file_change.file_type(), 0, base::Time()), | 188 context_.get(), |
| 189 SyncFileMetadata(file_change.file_type(), 0, base::Time()), | |
| 203 file_change, local_path, url)); | 190 file_change, local_path, url)); |
| 204 syncer->RunSequential(CreateResultReceiver(&status)); | 191 syncer->RunSequential(CreateResultReceiver(&status)); |
| 205 base::RunLoop().RunUntilIdle(); | 192 base::RunLoop().RunUntilIdle(); |
| 206 if (status == SYNC_STATUS_OK) | 193 if (status == SYNC_STATUS_OK) |
| 207 fake_remote_change_processor_->ClearLocalChanges(url); | 194 fake_remote_change_processor_->ClearLocalChanges(url); |
| 208 return status; | 195 return status; |
| 209 } | 196 } |
| 210 | 197 |
| 211 void RunRemoteToLocalSyncerUntilIdle() { | 198 void RunRemoteToLocalSyncerUntilIdle() { |
| 212 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 199 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 213 while (status != SYNC_STATUS_NO_CHANGE_TO_SYNC) | 200 while (status != SYNC_STATUS_NO_CHANGE_TO_SYNC) |
| 214 status = RunRemoteToLocalSyncer(); | 201 status = RunRemoteToLocalSyncer(); |
| 215 } | 202 } |
| 216 | 203 |
| 217 SyncStatusCode RunConflictResolver() { | 204 SyncStatusCode RunConflictResolver() { |
| 218 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 205 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 219 ConflictResolver resolver(this); | 206 ConflictResolver resolver(context_.get()); |
| 220 resolver.RunSequential(CreateResultReceiver(&status)); | 207 resolver.RunSequential(CreateResultReceiver(&status)); |
| 221 base::RunLoop().RunUntilIdle(); | 208 base::RunLoop().RunUntilIdle(); |
| 222 return status; | 209 return status; |
| 223 } | 210 } |
| 224 | 211 |
| 225 SyncStatusCode ListChanges() { | 212 SyncStatusCode ListChanges() { |
| 226 ListChangesTask list_changes(this); | 213 ListChangesTask list_changes(context_.get()); |
| 227 SyncStatusCode status = SYNC_STATUS_UNKNOWN; | 214 SyncStatusCode status = SYNC_STATUS_UNKNOWN; |
| 228 list_changes.RunSequential(CreateResultReceiver(&status)); | 215 list_changes.RunSequential(CreateResultReceiver(&status)); |
| 229 base::RunLoop().RunUntilIdle(); | 216 base::RunLoop().RunUntilIdle(); |
| 230 return status; | 217 return status; |
| 231 } | 218 } |
| 232 | 219 |
| 233 ScopedVector<google_apis::ResourceEntry> | 220 ScopedVector<google_apis::ResourceEntry> |
| 234 GetResourceEntriesForParentAndTitle(const std::string& parent_folder_id, | 221 GetResourceEntriesForParentAndTitle(const std::string& parent_folder_id, |
| 235 const std::string& title) { | 222 const std::string& title) { |
| 236 ScopedVector<google_apis::ResourceEntry> entries; | 223 ScopedVector<google_apis::ResourceEntry> entries; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 256 void VerifyLocalChangeConsistency( | 243 void VerifyLocalChangeConsistency( |
| 257 const URLToFileChangesMap& expected_changes) { | 244 const URLToFileChangesMap& expected_changes) { |
| 258 fake_remote_change_processor_->VerifyConsistency(expected_changes); | 245 fake_remote_change_processor_->VerifyConsistency(expected_changes); |
| 259 } | 246 } |
| 260 | 247 |
| 261 private: | 248 private: |
| 262 content::TestBrowserThreadBundle thread_bundle_; | 249 content::TestBrowserThreadBundle thread_bundle_; |
| 263 base::ScopedTempDir database_dir_; | 250 base::ScopedTempDir database_dir_; |
| 264 scoped_ptr<leveldb::Env> in_memory_env_; | 251 scoped_ptr<leveldb::Env> in_memory_env_; |
| 265 | 252 |
| 266 scoped_ptr<FakeDriveServiceWrapper> fake_drive_service_; | 253 scoped_ptr<SyncEngineContext> context_; |
| 267 scoped_ptr<FakeDriveUploader> drive_uploader_; | |
| 268 scoped_ptr<FakeDriveServiceHelper> fake_drive_helper_; | 254 scoped_ptr<FakeDriveServiceHelper> fake_drive_helper_; |
| 269 scoped_ptr<MetadataDatabase> metadata_database_; | |
| 270 scoped_ptr<FakeRemoteChangeProcessor> fake_remote_change_processor_; | 255 scoped_ptr<FakeRemoteChangeProcessor> fake_remote_change_processor_; |
| 271 | 256 |
| 272 DISALLOW_COPY_AND_ASSIGN(ConflictResolverTest); | 257 DISALLOW_COPY_AND_ASSIGN(ConflictResolverTest); |
| 273 }; | 258 }; |
| 274 | 259 |
| 275 TEST_F(ConflictResolverTest, NoFileToBeResolved) { | 260 TEST_F(ConflictResolverTest, NoFileToBeResolved) { |
| 276 const GURL kOrigin("chrome-extension://example"); | 261 const GURL kOrigin("chrome-extension://example"); |
| 277 const std::string sync_root = CreateSyncRoot(); | 262 const std::string sync_root = CreateSyncRoot(); |
| 278 const std::string app_root = CreateRemoteFolder(sync_root, kOrigin.host()); | 263 const std::string app_root = CreateRemoteFolder(sync_root, kOrigin.host()); |
| 279 InitializeMetadataDatabase(); | 264 InitializeMetadataDatabase(); |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 508 | 493 |
| 509 EXPECT_EQ(4, CountParents(file)); | 494 EXPECT_EQ(4, CountParents(file)); |
| 510 | 495 |
| 511 EXPECT_EQ(SYNC_STATUS_OK, RunConflictResolver()); | 496 EXPECT_EQ(SYNC_STATUS_OK, RunConflictResolver()); |
| 512 | 497 |
| 513 EXPECT_EQ(1, CountParents(file)); | 498 EXPECT_EQ(1, CountParents(file)); |
| 514 } | 499 } |
| 515 | 500 |
| 516 } // namespace drive_backend | 501 } // namespace drive_backend |
| 517 } // namespace sync_file_system | 502 } // namespace sync_file_system |
| OLD | NEW |