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 "google_apis/gaia/gaia_constants.h"
25 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_test.h"
26 #include "sync/internal_api/public/test/test_internal_components_factory.h"
27 #include "sync/internal_api/public/user_share.h"
28 #include "sync/protocol/encryption.pb.h"
29 #include "testing/gmock/include/gmock/gmock.h"
31 using syncer::InternalComponentsFactory
;
32 using syncer::TestInternalComponentsFactory
;
33 using syncer::UserShare
;
35 namespace browser_sync
{
37 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest(
39 invalidation::InvalidationService
* invalidator
,
40 const base::WeakPtr
<sync_driver::SyncPrefs
>& sync_prefs
,
41 base::Closure callback
)
42 : browser_sync::SyncBackendHostImpl(
43 profile
->GetDebugName(), profile
, invalidator
,
44 sync_prefs
, base::FilePath(FILE_PATH_LITERAL("test"))),
45 callback_(callback
) {}
47 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {}
49 void SyncBackendHostForProfileSyncTest::InitCore(
50 scoped_ptr
<DoInitializeOptions
> options
) {
51 options
->http_bridge_factory
=
52 scoped_ptr
<syncer::HttpPostProviderFactory
>(
53 new browser_sync::TestHttpBridgeFactory());
54 options
->sync_manager_factory
.reset(
55 new syncer::SyncManagerFactoryForProfileSyncTest(callback_
));
56 options
->credentials
.email
= "testuser@gmail.com";
57 options
->credentials
.sync_token
= "token";
58 options
->credentials
.scope_set
.insert(GaiaConstants::kChromeSyncOAuth2Scope
);
59 options
->restored_key_for_bootstrapping
= "";
61 // It'd be nice if we avoided creating the InternalComponentsFactory in the
62 // first place, but SyncBackendHost will have created one by now so we must
63 // free it. Grab the switches to pass on first.
64 InternalComponentsFactory::Switches factory_switches
=
65 options
->internal_components_factory
->GetSwitches();
66 options
->internal_components_factory
.reset(
67 new TestInternalComponentsFactory(
68 factory_switches
, InternalComponentsFactory::STORAGE_IN_MEMORY
,
71 SyncBackendHostImpl::InitCore(options
.Pass());
74 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer(
75 syncer::ConfigureReason reason
,
76 syncer::ModelTypeSet to_download
,
77 syncer::ModelTypeSet to_purge
,
78 syncer::ModelTypeSet to_journal
,
79 syncer::ModelTypeSet to_unapply
,
80 syncer::ModelTypeSet to_ignore
,
81 const syncer::ModelSafeRoutingInfo
& routing_info
,
82 const base::Callback
<void(syncer::ModelTypeSet
,
83 syncer::ModelTypeSet
)>& ready_task
,
84 const base::Closure
& retry_callback
) {
85 syncer::ModelTypeSet failed_configuration_types
;
87 // The first parameter there should be the set of enabled types. That's not
88 // something we have access to from this strange test harness. We'll just
89 // send back the list of newly configured types instead and hope it doesn't
91 // Posted to avoid re-entrancy issues.
92 base::ThreadTaskRunnerHandle::Get()->PostTask(
94 base::Bind(&SyncBackendHostForProfileSyncTest::
95 FinishConfigureDataTypesOnFrontendLoop
,
96 base::Unretained(this),
97 syncer::Difference(to_download
, failed_configuration_types
),
98 syncer::Difference(to_download
, failed_configuration_types
),
99 failed_configuration_types
, ready_task
));
102 } // namespace browser_sync
104 syncer::TestIdFactory
* TestProfileSyncService::id_factory() {
108 syncer::WeakHandle
<syncer::JsEventHandler
>
109 TestProfileSyncService::GetJsEventHandler() {
110 return syncer::WeakHandle
<syncer::JsEventHandler
>();
113 TestProfileSyncService::TestProfileSyncService(
114 scoped_ptr
<sync_driver::SyncApiComponentFactory
> factory
,
116 SigninManagerBase
* signin
,
117 ProfileOAuth2TokenService
* oauth2_token_service
,
118 browser_sync::ProfileSyncServiceStartBehavior behavior
)
119 : ProfileSyncService(
122 make_scoped_ptr(new SigninManagerWrapper(signin
)),
123 oauth2_token_service
,
125 SetSyncSetupCompleted();
128 TestProfileSyncService::~TestProfileSyncService() {
132 scoped_ptr
<KeyedService
> TestProfileSyncService::TestFactoryFunction(
133 content::BrowserContext
* context
) {
134 Profile
* profile
= static_cast<Profile
*>(context
);
135 SigninManagerBase
* signin
=
136 SigninManagerFactory::GetForProfile(profile
);
137 ProfileOAuth2TokenService
* oauth2_token_service
=
138 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
);
139 return make_scoped_ptr(new TestProfileSyncService(
140 scoped_ptr
<sync_driver::SyncApiComponentFactory
>(
141 new ProfileSyncComponentsFactoryMock()),
142 profile
, signin
, oauth2_token_service
, browser_sync::AUTO_START
));
146 TestProfileSyncService
* TestProfileSyncService::BuildAutoStartAsyncInit(
147 Profile
* profile
, base::Closure callback
) {
148 TestProfileSyncService
* sync_service
= static_cast<TestProfileSyncService
*>(
149 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
150 profile
, &TestProfileSyncService::TestFactoryFunction
));
151 ProfileSyncComponentsFactoryMock
* components
=
152 sync_service
->components_factory_mock();
153 // TODO(tim): Convert to a fake instead of mock.
154 EXPECT_CALL(*components
,
155 CreateSyncBackendHost(testing::_
, testing::_
,
156 testing::_
, testing::_
)).
157 WillOnce(testing::Return(
158 new browser_sync::SyncBackendHostForProfileSyncTest(
160 invalidation::ProfileInvalidationProviderFactory::GetForProfile(
161 profile
)->GetInvalidationService(),
162 sync_service
->sync_prefs_
.AsWeakPtr(),
167 ProfileSyncComponentsFactoryMock
*
168 TestProfileSyncService::components_factory_mock() {
169 // We always create a mock factory, see Build* routines.
170 return static_cast<ProfileSyncComponentsFactoryMock
*>(factory());
173 void TestProfileSyncService::OnConfigureDone(
174 const sync_driver::DataTypeManager::ConfigureResult
& result
) {
175 ProfileSyncService::OnConfigureDone(result
);
176 base::MessageLoop::current()->Quit();
179 UserShare
* TestProfileSyncService::GetUserShare() const {
180 return backend_
->GetUserShare();
183 bool TestProfileSyncService::NeedBackup() const {