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/test_profile_sync_service.h" 6 7 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h" 9 #include "chrome/browser/profiles/profile.h" 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 11 #include "chrome/browser/signin/signin_manager_factory.h" 12 #include "chrome/browser/sync/glue/sync_backend_host.h" 13 #include "chrome/browser/sync/glue/sync_backend_host_core.h" 14 #include "chrome/browser/sync/profile_sync_components_factory.h" 15 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" 16 #include "chrome/browser/sync/profile_sync_service_factory.h" 17 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h" 18 #include "chrome/browser/sync/test/test_http_bridge_factory.h" 19 #include "components/invalidation/profile_invalidation_provider.h" 20 #include "components/signin/core/browser/signin_manager.h" 21 #include "google_apis/gaia/gaia_constants.h" 22 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_test.h" 23 #include "sync/internal_api/public/test/test_internal_components_factory.h" 24 #include "sync/internal_api/public/user_share.h" 25 #include "sync/protocol/encryption.pb.h" 26 #include "testing/gmock/include/gmock/gmock.h" 27 28 using syncer::InternalComponentsFactory; 29 using syncer::TestInternalComponentsFactory; 30 using syncer::UserShare; 31 32 namespace browser_sync { 33 34 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( 35 Profile* profile, 36 invalidation::InvalidationService* invalidator, 37 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs, 38 base::Closure callback) 39 : browser_sync::SyncBackendHostImpl( 40 profile->GetDebugName(), profile, invalidator, 41 sync_prefs, base::FilePath(FILE_PATH_LITERAL("test"))), 42 callback_(callback) {} 43 44 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} 45 46 void SyncBackendHostForProfileSyncTest::InitCore( 47 scoped_ptr<DoInitializeOptions> options) { 48 options->http_bridge_factory = 49 scoped_ptr<syncer::HttpPostProviderFactory>( 50 new browser_sync::TestHttpBridgeFactory()); 51 options->sync_manager_factory.reset( 52 new syncer::SyncManagerFactoryForProfileSyncTest(callback_)); 53 options->credentials.email = "testuser (at) gmail.com"; 54 options->credentials.sync_token = "token"; 55 options->credentials.scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope); 56 options->restored_key_for_bootstrapping = ""; 57 58 // It'd be nice if we avoided creating the InternalComponentsFactory in the 59 // first place, but SyncBackendHost will have created one by now so we must 60 // free it. Grab the switches to pass on first. 61 InternalComponentsFactory::Switches factory_switches = 62 options->internal_components_factory->GetSwitches(); 63 options->internal_components_factory.reset( 64 new TestInternalComponentsFactory( 65 factory_switches, InternalComponentsFactory::STORAGE_IN_MEMORY, 66 NULL)); 67 68 SyncBackendHostImpl::InitCore(options.Pass()); 69 } 70 71 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( 72 syncer::ConfigureReason reason, 73 syncer::ModelTypeSet to_download, 74 syncer::ModelTypeSet to_purge, 75 syncer::ModelTypeSet to_journal, 76 syncer::ModelTypeSet to_unapply, 77 syncer::ModelTypeSet to_ignore, 78 const syncer::ModelSafeRoutingInfo& routing_info, 79 const base::Callback<void(syncer::ModelTypeSet, 80 syncer::ModelTypeSet)>& ready_task, 81 const base::Closure& retry_callback) { 82 syncer::ModelTypeSet failed_configuration_types; 83 84 // The first parameter there should be the set of enabled types. That's not 85 // something we have access to from this strange test harness. We'll just 86 // send back the list of newly configured types instead and hope it doesn't 87 // break anything. 88 FinishConfigureDataTypesOnFrontendLoop( 89 syncer::Difference(to_download, failed_configuration_types), 90 syncer::Difference(to_download, failed_configuration_types), 91 failed_configuration_types, 92 ready_task); 93 } 94 95 } // namespace browser_sync 96 97 syncer::TestIdFactory* TestProfileSyncService::id_factory() { 98 return &id_factory_; 99 } 100 101 syncer::WeakHandle<syncer::JsEventHandler> 102 TestProfileSyncService::GetJsEventHandler() { 103 return syncer::WeakHandle<syncer::JsEventHandler>(); 104 } 105 106 TestProfileSyncService::TestProfileSyncService( 107 scoped_ptr<ProfileSyncComponentsFactory> factory, 108 Profile* profile, 109 SigninManagerBase* signin, 110 ProfileOAuth2TokenService* oauth2_token_service, 111 browser_sync::ProfileSyncServiceStartBehavior behavior) 112 : ProfileSyncService( 113 factory.Pass(), 114 profile, 115 make_scoped_ptr(new SupervisedUserSigninManagerWrapper(profile, 116 signin)), 117 oauth2_token_service, 118 behavior) { 119 SetSyncSetupCompleted(); 120 } 121 122 TestProfileSyncService::~TestProfileSyncService() { 123 } 124 125 // static 126 KeyedService* TestProfileSyncService::TestFactoryFunction( 127 content::BrowserContext* context) { 128 Profile* profile = static_cast<Profile*>(context); 129 SigninManagerBase* signin = 130 SigninManagerFactory::GetForProfile(profile); 131 ProfileOAuth2TokenService* oauth2_token_service = 132 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 133 return new TestProfileSyncService( 134 scoped_ptr<ProfileSyncComponentsFactory>( 135 new ProfileSyncComponentsFactoryMock()), 136 profile, 137 signin, 138 oauth2_token_service, 139 browser_sync::AUTO_START); 140 } 141 142 // static 143 TestProfileSyncService* TestProfileSyncService::BuildAutoStartAsyncInit( 144 Profile* profile, base::Closure callback) { 145 TestProfileSyncService* sync_service = static_cast<TestProfileSyncService*>( 146 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 147 profile, &TestProfileSyncService::TestFactoryFunction)); 148 ProfileSyncComponentsFactoryMock* components = 149 sync_service->components_factory_mock(); 150 // TODO(tim): Convert to a fake instead of mock. 151 EXPECT_CALL(*components, 152 CreateSyncBackendHost(testing::_,testing::_, testing::_, 153 testing::_, testing::_)). 154 WillOnce(testing::Return( 155 new browser_sync::SyncBackendHostForProfileSyncTest( 156 profile, 157 invalidation::ProfileInvalidationProviderFactory::GetForProfile( 158 profile)->GetInvalidationService(), 159 sync_service->sync_prefs_.AsWeakPtr(), 160 callback))); 161 return sync_service; 162 } 163 164 ProfileSyncComponentsFactoryMock* 165 TestProfileSyncService::components_factory_mock() { 166 // We always create a mock factory, see Build* routines. 167 return static_cast<ProfileSyncComponentsFactoryMock*>(factory()); 168 } 169 170 void TestProfileSyncService::OnConfigureDone( 171 const sync_driver::DataTypeManager::ConfigureResult& result) { 172 ProfileSyncService::OnConfigureDone(result); 173 base::MessageLoop::current()->Quit(); 174 } 175 176 UserShare* TestProfileSyncService::GetUserShare() const { 177 return backend_->GetUserShare(); 178 } 179 180 bool TestProfileSyncService::NeedBackup() const { 181 return false; 182 } 183