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