| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/browser_sync/browser/abstract_profile_sync_service_test.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/location.h" | |
| 13 #include "base/memory/ptr_util.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "components/browser_sync/browser/test_http_bridge_factory.h" | |
| 16 #include "components/browser_sync/browser/test_profile_sync_service.h" | |
| 17 #include "components/sync/core/test/sync_manager_factory_for_profile_sync_test.h
" | |
| 18 #include "components/sync/core/test/test_internal_components_factory.h" | |
| 19 #include "components/sync/core/test/test_user_share.h" | |
| 20 #include "components/sync/driver/glue/sync_backend_host_core.h" | |
| 21 #include "components/sync/driver/sync_api_component_factory_mock.h" | |
| 22 #include "components/sync/protocol/sync.pb.h" | |
| 23 #include "google_apis/gaia/gaia_constants.h" | |
| 24 | |
| 25 using syncer::ModelType; | |
| 26 using testing::_; | |
| 27 using testing::Return; | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 class SyncBackendHostForProfileSyncTest | |
| 32 : public browser_sync::SyncBackendHostImpl { | |
| 33 public: | |
| 34 SyncBackendHostForProfileSyncTest( | |
| 35 const base::FilePath& temp_dir, | |
| 36 sync_driver::SyncClient* sync_client, | |
| 37 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | |
| 38 invalidation::InvalidationService* invalidator, | |
| 39 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs, | |
| 40 const base::Closure& callback); | |
| 41 ~SyncBackendHostForProfileSyncTest() override; | |
| 42 | |
| 43 void RequestConfigureSyncer( | |
| 44 syncer::ConfigureReason reason, | |
| 45 syncer::ModelTypeSet to_download, | |
| 46 syncer::ModelTypeSet to_purge, | |
| 47 syncer::ModelTypeSet to_journal, | |
| 48 syncer::ModelTypeSet to_unapply, | |
| 49 syncer::ModelTypeSet to_ignore, | |
| 50 const syncer::ModelSafeRoutingInfo& routing_info, | |
| 51 const base::Callback<void(syncer::ModelTypeSet, syncer::ModelTypeSet)>& | |
| 52 ready_task, | |
| 53 const base::Closure& retry_callback) override; | |
| 54 | |
| 55 protected: | |
| 56 void InitCore( | |
| 57 std::unique_ptr<browser_sync::DoInitializeOptions> options) override; | |
| 58 | |
| 59 private: | |
| 60 // Invoked at the start of HandleSyncManagerInitializationOnFrontendLoop. | |
| 61 // Allows extra initialization work to be performed before the backend comes | |
| 62 // up. | |
| 63 base::Closure callback_; | |
| 64 | |
| 65 DISALLOW_COPY_AND_ASSIGN(SyncBackendHostForProfileSyncTest); | |
| 66 }; | |
| 67 | |
| 68 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( | |
| 69 const base::FilePath& temp_dir, | |
| 70 sync_driver::SyncClient* sync_client, | |
| 71 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | |
| 72 invalidation::InvalidationService* invalidator, | |
| 73 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs, | |
| 74 const base::Closure& callback) | |
| 75 : browser_sync::SyncBackendHostImpl( | |
| 76 "dummy_debug_name", | |
| 77 sync_client, | |
| 78 ui_thread, | |
| 79 invalidator, | |
| 80 sync_prefs, | |
| 81 temp_dir.Append(base::FilePath(FILE_PATH_LITERAL("test")))), | |
| 82 callback_(callback) {} | |
| 83 | |
| 84 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} | |
| 85 | |
| 86 void SyncBackendHostForProfileSyncTest::InitCore( | |
| 87 std::unique_ptr<browser_sync::DoInitializeOptions> options) { | |
| 88 options->http_bridge_factory = | |
| 89 std::unique_ptr<syncer::HttpPostProviderFactory>( | |
| 90 new browser_sync::TestHttpBridgeFactory()); | |
| 91 options->sync_manager_factory.reset( | |
| 92 new syncer::SyncManagerFactoryForProfileSyncTest(callback_)); | |
| 93 options->credentials.email = "testuser@gmail.com"; | |
| 94 options->credentials.sync_token = "token"; | |
| 95 options->credentials.scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope); | |
| 96 options->restored_key_for_bootstrapping.clear(); | |
| 97 | |
| 98 // It'd be nice if we avoided creating the InternalComponentsFactory in the | |
| 99 // first place, but SyncBackendHost will have created one by now so we must | |
| 100 // free it. Grab the switches to pass on first. | |
| 101 syncer::InternalComponentsFactory::Switches factory_switches = | |
| 102 options->internal_components_factory->GetSwitches(); | |
| 103 options->internal_components_factory.reset( | |
| 104 new syncer::TestInternalComponentsFactory( | |
| 105 factory_switches, | |
| 106 syncer::InternalComponentsFactory::STORAGE_IN_MEMORY, nullptr)); | |
| 107 | |
| 108 browser_sync::SyncBackendHostImpl::InitCore(std::move(options)); | |
| 109 } | |
| 110 | |
| 111 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( | |
| 112 syncer::ConfigureReason reason, | |
| 113 syncer::ModelTypeSet to_download, | |
| 114 syncer::ModelTypeSet to_purge, | |
| 115 syncer::ModelTypeSet to_journal, | |
| 116 syncer::ModelTypeSet to_unapply, | |
| 117 syncer::ModelTypeSet to_ignore, | |
| 118 const syncer::ModelSafeRoutingInfo& routing_info, | |
| 119 const base::Callback<void(syncer::ModelTypeSet, syncer::ModelTypeSet)>& | |
| 120 ready_task, | |
| 121 const base::Closure& retry_callback) { | |
| 122 syncer::ModelTypeSet failed_configuration_types; | |
| 123 | |
| 124 // The first parameter there should be the set of enabled types. That's not | |
| 125 // something we have access to from this strange test harness. We'll just | |
| 126 // send back the list of newly configured types instead and hope it doesn't | |
| 127 // break anything. | |
| 128 // Posted to avoid re-entrancy issues. | |
| 129 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 130 FROM_HERE, | |
| 131 base::Bind(&SyncBackendHostForProfileSyncTest:: | |
| 132 FinishConfigureDataTypesOnFrontendLoop, | |
| 133 base::Unretained(this), | |
| 134 syncer::Difference(to_download, failed_configuration_types), | |
| 135 syncer::Difference(to_download, failed_configuration_types), | |
| 136 failed_configuration_types, ready_task)); | |
| 137 } | |
| 138 | |
| 139 // Helper function for return-type-upcasting of the callback. | |
| 140 sync_driver::SyncService* GetSyncService( | |
| 141 base::Callback<TestProfileSyncService*(void)> get_sync_service_callback) { | |
| 142 return get_sync_service_callback.Run(); | |
| 143 } | |
| 144 | |
| 145 } // namespace | |
| 146 | |
| 147 /* static */ | |
| 148 syncer::ImmutableChangeRecordList | |
| 149 ProfileSyncServiceTestHelper::MakeSingletonChangeRecordList( | |
| 150 int64_t node_id, | |
| 151 syncer::ChangeRecord::Action action) { | |
| 152 syncer::ChangeRecord record; | |
| 153 record.action = action; | |
| 154 record.id = node_id; | |
| 155 syncer::ChangeRecordList records(1, record); | |
| 156 return syncer::ImmutableChangeRecordList(&records); | |
| 157 } | |
| 158 | |
| 159 /* static */ | |
| 160 syncer::ImmutableChangeRecordList | |
| 161 ProfileSyncServiceTestHelper::MakeSingletonDeletionChangeRecordList( | |
| 162 int64_t node_id, | |
| 163 const sync_pb::EntitySpecifics& specifics) { | |
| 164 syncer::ChangeRecord record; | |
| 165 record.action = syncer::ChangeRecord::ACTION_DELETE; | |
| 166 record.id = node_id; | |
| 167 record.specifics = specifics; | |
| 168 syncer::ChangeRecordList records(1, record); | |
| 169 return syncer::ImmutableChangeRecordList(&records); | |
| 170 } | |
| 171 | |
| 172 AbstractProfileSyncServiceTest::AbstractProfileSyncServiceTest() | |
| 173 : data_type_thread_("Extra thread") { | |
| 174 CHECK(temp_dir_.CreateUniqueTempDir()); | |
| 175 } | |
| 176 | |
| 177 AbstractProfileSyncServiceTest::~AbstractProfileSyncServiceTest() { | |
| 178 sync_service_->Shutdown(); | |
| 179 } | |
| 180 | |
| 181 bool AbstractProfileSyncServiceTest::CreateRoot(ModelType model_type) { | |
| 182 return syncer::TestUserShare::CreateRoot(model_type, | |
| 183 sync_service_->GetUserShare()); | |
| 184 } | |
| 185 | |
| 186 void AbstractProfileSyncServiceTest::CreateSyncService( | |
| 187 std::unique_ptr<sync_driver::SyncClient> sync_client, | |
| 188 const base::Closure& initialization_success_callback) { | |
| 189 DCHECK(sync_client); | |
| 190 ProfileSyncService::InitParams init_params = | |
| 191 profile_sync_service_bundle_.CreateBasicInitParams( | |
| 192 ProfileSyncService::AUTO_START, std::move(sync_client)); | |
| 193 sync_service_ = | |
| 194 base::MakeUnique<TestProfileSyncService>(std::move(init_params)); | |
| 195 | |
| 196 SyncApiComponentFactoryMock* components = | |
| 197 profile_sync_service_bundle_.component_factory(); | |
| 198 EXPECT_CALL(*components, CreateSyncBackendHost(_, _, _, _)) | |
| 199 .WillOnce(Return(new SyncBackendHostForProfileSyncTest( | |
| 200 temp_dir_.GetPath(), sync_service_->GetSyncClient(), | |
| 201 base::ThreadTaskRunnerHandle::Get(), | |
| 202 profile_sync_service_bundle_.fake_invalidation_service(), | |
| 203 sync_service_->sync_prefs()->AsWeakPtr(), | |
| 204 initialization_success_callback))); | |
| 205 | |
| 206 sync_service_->SetFirstSetupComplete(); | |
| 207 } | |
| 208 | |
| 209 base::Callback<sync_driver::SyncService*(void)> | |
| 210 AbstractProfileSyncServiceTest::GetSyncServiceCallback() { | |
| 211 return base::Bind(GetSyncService, | |
| 212 base::Bind(&AbstractProfileSyncServiceTest::sync_service, | |
| 213 base::Unretained(this))); | |
| 214 } | |
| 215 | |
| 216 CreateRootHelper::CreateRootHelper(AbstractProfileSyncServiceTest* test, | |
| 217 ModelType model_type) | |
| 218 : callback_(base::Bind(&CreateRootHelper::CreateRootCallback, | |
| 219 base::Unretained(this))), | |
| 220 test_(test), | |
| 221 model_type_(model_type), | |
| 222 success_(false) { | |
| 223 } | |
| 224 | |
| 225 CreateRootHelper::~CreateRootHelper() { | |
| 226 } | |
| 227 | |
| 228 const base::Closure& CreateRootHelper::callback() const { | |
| 229 return callback_; | |
| 230 } | |
| 231 | |
| 232 bool CreateRootHelper::success() { | |
| 233 return success_; | |
| 234 } | |
| 235 | |
| 236 void CreateRootHelper::CreateRootCallback() { | |
| 237 success_ = test_->CreateRoot(model_type_); | |
| 238 } | |
| OLD | NEW |