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