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