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.
5 #include "chrome/browser/sync/test_profile_sync_service.h"
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/protocol/encryption.pb.h"
23 #include "testing/gmock/include/gmock/gmock.h"
25 using syncer::InternalComponentsFactory
;
26 using syncer::TestInternalComponentsFactory
;
27 using syncer::UserShare
;
29 namespace browser_sync
{
31 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest(
33 const base::WeakPtr
<sync_driver::SyncPrefs
>& sync_prefs
,
34 base::Closure callback
)
35 : browser_sync::SyncBackendHostImpl(profile
->GetDebugName(),
38 callback_(callback
) {}
40 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {}
42 void SyncBackendHostForProfileSyncTest::InitCore(
43 scoped_ptr
<DoInitializeOptions
> options
) {
44 options
->http_bridge_factory
=
45 scoped_ptr
<syncer::HttpPostProviderFactory
>(
46 new browser_sync::TestHttpBridgeFactory());
47 options
->sync_manager_factory
.reset(
48 new syncer::SyncManagerFactoryForProfileSyncTest(callback_
));
49 options
->credentials
.email
= "testuser@gmail.com";
50 options
->credentials
.sync_token
= "token";
51 options
->restored_key_for_bootstrapping
= "";
53 // It'd be nice if we avoided creating the InternalComponentsFactory in the
54 // first place, but SyncBackendHost will have created one by now so we must
55 // free it. Grab the switches to pass on first.
56 InternalComponentsFactory::Switches factory_switches
=
57 options
->internal_components_factory
->GetSwitches();
58 options
->internal_components_factory
.reset(
59 new TestInternalComponentsFactory(factory_switches
,
60 syncer::STORAGE_IN_MEMORY
));
62 SyncBackendHostImpl::InitCore(options
.Pass());
65 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer(
66 syncer::ConfigureReason reason
,
67 syncer::ModelTypeSet to_download
,
68 syncer::ModelTypeSet to_purge
,
69 syncer::ModelTypeSet to_journal
,
70 syncer::ModelTypeSet to_unapply
,
71 syncer::ModelTypeSet to_ignore
,
72 const syncer::ModelSafeRoutingInfo
& routing_info
,
73 const base::Callback
<void(syncer::ModelTypeSet
,
74 syncer::ModelTypeSet
)>& ready_task
,
75 const base::Closure
& retry_callback
) {
76 syncer::ModelTypeSet failed_configuration_types
;
78 // The first parameter there should be the set of enabled types. That's not
79 // something we have access to from this strange test harness. We'll just
80 // send back the list of newly configured types instead and hope it doesn't
82 FinishConfigureDataTypesOnFrontendLoop(
83 syncer::Difference(to_download
, failed_configuration_types
),
84 syncer::Difference(to_download
, failed_configuration_types
),
85 failed_configuration_types
,
89 } // namespace browser_sync
91 syncer::TestIdFactory
* TestProfileSyncService::id_factory() {
95 syncer::WeakHandle
<syncer::JsEventHandler
>
96 TestProfileSyncService::GetJsEventHandler() {
97 return syncer::WeakHandle
<syncer::JsEventHandler
>();
100 TestProfileSyncService::TestProfileSyncService(
101 ProfileSyncComponentsFactory
* factory
,
103 SigninManagerBase
* signin
,
104 ProfileOAuth2TokenService
* oauth2_token_service
,
105 browser_sync::ProfileSyncServiceStartBehavior behavior
)
106 : ProfileSyncService(factory
,
108 new ManagedUserSigninManagerWrapper(profile
, signin
),
109 oauth2_token_service
,
111 SetSyncSetupCompleted();
114 TestProfileSyncService::~TestProfileSyncService() {
118 KeyedService
* TestProfileSyncService::TestFactoryFunction(
119 content::BrowserContext
* context
) {
120 Profile
* profile
= static_cast<Profile
*>(context
);
121 SigninManagerBase
* signin
=
122 SigninManagerFactory::GetForProfile(profile
);
123 ProfileOAuth2TokenService
* oauth2_token_service
=
124 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
);
125 ProfileSyncComponentsFactoryMock
* factory
=
126 new ProfileSyncComponentsFactoryMock();
127 return new TestProfileSyncService(factory
,
130 oauth2_token_service
,
131 browser_sync::AUTO_START
);
135 TestProfileSyncService
* TestProfileSyncService::BuildAutoStartAsyncInit(
136 Profile
* profile
, base::Closure callback
) {
137 TestProfileSyncService
* sync_service
= static_cast<TestProfileSyncService
*>(
138 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
139 profile
, &TestProfileSyncService::TestFactoryFunction
));
140 ProfileSyncComponentsFactoryMock
* components
=
141 sync_service
->components_factory_mock();
142 // TODO(tim): Convert to a fake instead of mock.
143 EXPECT_CALL(*components
,
144 CreateSyncBackendHost(testing::_
,testing::_
, testing::_
)).
145 WillOnce(testing::Return(
146 new browser_sync::SyncBackendHostForProfileSyncTest(
148 sync_service
->sync_prefs_
.AsWeakPtr(),
153 ProfileSyncComponentsFactoryMock
*
154 TestProfileSyncService::components_factory_mock() {
155 // We always create a mock factory, see Build* routines.
156 return static_cast<ProfileSyncComponentsFactoryMock
*>(factory());
159 void TestProfileSyncService::OnConfigureDone(
160 const browser_sync::DataTypeManager::ConfigureResult
& result
) {
161 ProfileSyncService::OnConfigureDone(result
);
162 base::MessageLoop::current()->Quit();
165 UserShare
* TestProfileSyncService::GetUserShare() const {
166 return backend_
->GetUserShare();