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/signin/signin_manager.h"
8 #include "chrome/browser/sync/glue/data_type_controller.h"
9 #include "chrome/browser/sync/glue/sync_backend_host.h"
10 #include "chrome/browser/sync/profile_sync_components_factory.h"
11 #include "chrome/browser/sync/test/test_http_bridge_factory.h"
12 #include "chrome/common/chrome_notification_types.h"
13 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
14 #include "sync/internal_api/public/test/test_user_share.h"
15 #include "sync/internal_api/public/user_share.h"
16 #include "sync/js/js_reply_handler.h"
17 #include "sync/protocol/encryption.pb.h"
18 #include "sync/syncable/directory.h"
20 using syncer::InternalComponentsFactory
;
21 using syncer::ModelSafeRoutingInfo
;
22 using syncer::TestInternalComponentsFactory
;
23 using syncer::sessions::ModelNeutralState
;
24 using syncer::sessions::SyncSessionSnapshot
;
25 using syncer::sessions::SyncSourceInfo
;
26 using syncer::UserShare
;
27 using syncer::syncable::Directory
;
29 using syncer::DEVICE_INFO
;
30 using syncer::EXPERIMENTS
;
32 namespace browser_sync
{
34 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest(
36 const base::WeakPtr
<SyncPrefs
>& sync_prefs
,
37 const base::WeakPtr
<InvalidatorStorage
>& invalidator_storage
,
38 syncer::TestIdFactory
& id_factory
,
39 base::Closure
& callback
,
40 bool set_initial_sync_ended_on_init
,
41 bool synchronous_init
,
42 bool fail_initial_download
,
43 syncer::StorageOption storage_option
)
44 : browser_sync::SyncBackendHost(
45 profile
->GetDebugName(), profile
, sync_prefs
, invalidator_storage
),
46 id_factory_(id_factory
),
48 set_initial_sync_ended_on_init_(set_initial_sync_ended_on_init
),
49 synchronous_init_(synchronous_init
),
50 fail_initial_download_(fail_initial_download
),
51 storage_option_(storage_option
) {}
53 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {}
57 scoped_ptr
<syncer::HttpPostProviderFactory
> MakeTestHttpBridgeFactory() {
58 return scoped_ptr
<syncer::HttpPostProviderFactory
>(
59 new browser_sync::TestHttpBridgeFactory());
64 void SyncBackendHostForProfileSyncTest::InitCore(
65 const DoInitializeOptions
& options
) {
66 DoInitializeOptions test_options
= options
;
67 test_options
.make_http_bridge_factory_fn
=
68 base::Bind(&MakeTestHttpBridgeFactory
);
69 test_options
.credentials
.email
= "testuser@gmail.com";
70 test_options
.credentials
.sync_token
= "token";
71 test_options
.restored_key_for_bootstrapping
= "";
72 syncer::StorageOption storage
= storage_option_
;
74 // It'd be nice if we avoided creating the InternalComponentsFactory in the
75 // first place, but SyncBackendHost will have created one by now so we must
76 // free it. Grab the switches to pass on first.
77 InternalComponentsFactory::Switches factory_switches
=
78 test_options
.internal_components_factory
->GetSwitches();
79 delete test_options
.internal_components_factory
;
81 test_options
.internal_components_factory
=
82 new TestInternalComponentsFactory(factory_switches
, storage
);
84 SyncBackendHost::InitCore(test_options
);
85 if (synchronous_init_
) {
86 // The SyncBackend posts a task to the current loop when
87 // initialization completes.
88 MessageLoop::current()->Run();
92 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer(
93 syncer::ConfigureReason reason
,
94 syncer::ModelTypeSet types_to_config
,
95 const syncer::ModelSafeRoutingInfo
& routing_info
,
96 const base::Callback
<void(syncer::ModelTypeSet
)>& ready_task
,
97 const base::Closure
& retry_callback
) {
98 syncer::ModelTypeSet failed_configuration_types
;
99 if (fail_initial_download_
)
100 failed_configuration_types
= types_to_config
;
102 FinishConfigureDataTypesOnFrontendLoop(failed_configuration_types
,
106 void SyncBackendHostForProfileSyncTest
107 ::HandleSyncManagerInitializationOnFrontendLoop(
108 const syncer::WeakHandle
<syncer::JsBackend
>& js_backend
,
109 const syncer::WeakHandle
<syncer::DataTypeDebugInfoListener
>&
112 syncer::ModelTypeSet restored_types
) {
113 // Here's our opportunity to pretend to do things that the SyncManager would
114 // normally do during initialization, but can't because this is a test.
116 // Set up any nodes the test wants around before model association.
117 if (!callback_
.is_null()) {
121 // Pretend we downloaded initial updates and set initial sync ended bits
122 // if we were asked to.
123 if (set_initial_sync_ended_on_init_
) {
124 UserShare
* user_share
= GetUserShare();
125 Directory
* directory
= user_share
->directory
.get();
127 if (!directory
->initial_sync_ended_for_type(NIGORI
)) {
128 syncer::TestUserShare::CreateRoot(NIGORI
, user_share
);
130 // A side effect of adding the NIGORI mode (normally done by the
131 // syncer) is a decryption attempt, which will fail the first time.
134 if (!directory
->initial_sync_ended_for_type(EXPERIMENTS
)) {
135 syncer::TestUserShare::CreateRoot(EXPERIMENTS
, user_share
);
138 SetInitialSyncEndedForAllTypes();
139 restored_types
= syncer::ModelTypeSet::All();
143 SyncBackendHost::HandleSyncManagerInitializationOnFrontendLoop(
144 js_backend
, debug_info_listener
, success
, restored_types
);
147 void SyncBackendHostForProfileSyncTest::SetInitialSyncEndedForAllTypes() {
148 UserShare
* user_share
= GetUserShare();
149 Directory
* directory
= user_share
->directory
.get();
151 for (int i
= syncer::FIRST_REAL_MODEL_TYPE
;
152 i
< syncer::MODEL_TYPE_COUNT
; ++i
) {
153 directory
->set_initial_sync_ended_for_type(
154 syncer::ModelTypeFromInt(i
), true);
158 void SyncBackendHostForProfileSyncTest::EmitOnInvalidatorStateChange(
159 syncer::InvalidatorState state
) {
160 frontend()->OnInvalidatorStateChange(state
);
163 void SyncBackendHostForProfileSyncTest::EmitOnIncomingInvalidation(
164 const syncer::ObjectIdInvalidationMap
& invalidation_map
,
165 const syncer::IncomingInvalidationSource source
) {
166 frontend()->OnIncomingInvalidation(invalidation_map
, source
);
169 } // namespace browser_sync
171 syncer::TestIdFactory
* TestProfileSyncService::id_factory() {
175 browser_sync::SyncBackendHostForProfileSyncTest
*
176 TestProfileSyncService::GetBackendForTest() {
177 return static_cast<browser_sync::SyncBackendHostForProfileSyncTest
*>(
178 ProfileSyncService::GetBackendForTest());
181 TestProfileSyncService::TestProfileSyncService(
182 ProfileSyncComponentsFactory
* factory
,
184 SigninManager
* signin
,
185 ProfileSyncService::StartBehavior behavior
,
186 bool synchronous_backend_initialization
,
187 const base::Closure
& callback
)
188 : ProfileSyncService(factory
,
192 synchronous_backend_initialization_(
193 synchronous_backend_initialization
),
194 synchronous_sync_configuration_(false),
196 set_initial_sync_ended_on_init_(true),
197 fail_initial_download_(false),
198 storage_option_(syncer::STORAGE_IN_MEMORY
) {
199 SetSyncSetupCompleted();
202 TestProfileSyncService::~TestProfileSyncService() {
205 void TestProfileSyncService::OnBackendInitialized(
206 const syncer::WeakHandle
<syncer::JsBackend
>& backend
,
207 const syncer::WeakHandle
<syncer::DataTypeDebugInfoListener
>&
210 ProfileSyncService::OnBackendInitialized(backend
,
214 // TODO(akalin): Figure out a better way to do this.
215 if (synchronous_backend_initialization_
) {
216 MessageLoop::current()->Quit();
220 void TestProfileSyncService::OnConfigureDone(
221 const browser_sync::DataTypeManager::ConfigureResult
& result
) {
222 ProfileSyncService::OnConfigureDone(result
);
223 if (!synchronous_sync_configuration_
)
224 MessageLoop::current()->Quit();
227 UserShare
* TestProfileSyncService::GetUserShare() const {
228 return backend_
->GetUserShare();
231 void TestProfileSyncService::dont_set_initial_sync_ended_on_init() {
232 set_initial_sync_ended_on_init_
= false;
234 void TestProfileSyncService::set_synchronous_sync_configuration() {
235 synchronous_sync_configuration_
= true;
237 void TestProfileSyncService::fail_initial_download() {
238 fail_initial_download_
= true;
240 void TestProfileSyncService::set_storage_option(
241 syncer::StorageOption storage_option
) {
242 storage_option_
= storage_option
;
245 void TestProfileSyncService::CreateBackend() {
246 backend_
.reset(new browser_sync::SyncBackendHostForProfileSyncTest(
248 sync_prefs_
.AsWeakPtr(),
249 invalidator_storage_
.AsWeakPtr(),
252 set_initial_sync_ended_on_init_
,
253 synchronous_backend_initialization_
,
254 fail_initial_download_
,