OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/test_profile_sync_service.h" | 5 #include "chrome/browser/sync/test_profile_sync_service.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
| 8 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
8 #include "chrome/browser/signin/signin_manager.h" | 9 #include "chrome/browser/signin/signin_manager.h" |
9 #include "chrome/browser/signin/signin_manager_factory.h" | 10 #include "chrome/browser/signin/signin_manager_factory.h" |
10 #include "chrome/browser/sync/glue/sync_backend_host.h" | 11 #include "chrome/browser/sync/glue/sync_backend_host.h" |
11 #include "chrome/browser/sync/glue/sync_backend_host_core.h" | 12 #include "chrome/browser/sync/glue/sync_backend_host_core.h" |
12 #include "chrome/browser/sync/profile_sync_components_factory.h" | 13 #include "chrome/browser/sync/profile_sync_components_factory.h" |
| 14 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
13 #include "chrome/browser/sync/test/test_http_bridge_factory.h" | 15 #include "chrome/browser/sync/test/test_http_bridge_factory.h" |
14 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_te
st.h" | 16 #include "sync/internal_api/public/test/sync_manager_factory_for_profile_sync_te
st.h" |
| 17 #include "sync/internal_api/public/test/test_internal_components_factory.h" |
15 #include "sync/internal_api/public/user_share.h" | 18 #include "sync/internal_api/public/user_share.h" |
16 #include "sync/js/js_reply_handler.h" | 19 #include "sync/js/js_reply_handler.h" |
17 #include "sync/protocol/encryption.pb.h" | 20 #include "sync/protocol/encryption.pb.h" |
18 | 21 |
19 using syncer::InternalComponentsFactory; | 22 using syncer::InternalComponentsFactory; |
20 using syncer::ModelSafeRoutingInfo; | |
21 using syncer::TestInternalComponentsFactory; | 23 using syncer::TestInternalComponentsFactory; |
22 using syncer::UserShare; | 24 using syncer::UserShare; |
23 | 25 |
24 namespace browser_sync { | 26 namespace browser_sync { |
25 | 27 |
26 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( | 28 SyncBackendHostForProfileSyncTest::SyncBackendHostForProfileSyncTest( |
27 Profile* profile, | 29 Profile* profile, |
28 const base::WeakPtr<SyncPrefs>& sync_prefs, | 30 const base::WeakPtr<SyncPrefs>& sync_prefs, |
29 base::Closure& callback, | 31 base::Closure& callback) |
30 bool set_initial_sync_ended_on_init, | |
31 bool synchronous_init, | |
32 bool fail_initial_download, | |
33 syncer::StorageOption storage_option) | |
34 : browser_sync::SyncBackendHostImpl( | 32 : browser_sync::SyncBackendHostImpl( |
35 profile->GetDebugName(), profile, sync_prefs), | 33 profile->GetDebugName(), profile, sync_prefs), |
36 callback_(callback), | 34 callback_(callback) {} |
37 fail_initial_download_(fail_initial_download), | |
38 set_initial_sync_ended_on_init_(set_initial_sync_ended_on_init), | |
39 synchronous_init_(synchronous_init), | |
40 storage_option_(storage_option), | |
41 weak_ptr_factory_(this) {} | |
42 | 35 |
43 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} | 36 SyncBackendHostForProfileSyncTest::~SyncBackendHostForProfileSyncTest() {} |
44 | 37 |
45 namespace { | |
46 | |
47 scoped_ptr<syncer::HttpPostProviderFactory> MakeTestHttpBridgeFactory() { | |
48 return scoped_ptr<syncer::HttpPostProviderFactory>( | |
49 new browser_sync::TestHttpBridgeFactory()); | |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 void SyncBackendHostForProfileSyncTest::InitCore( | 38 void SyncBackendHostForProfileSyncTest::InitCore( |
55 scoped_ptr<DoInitializeOptions> options) { | 39 scoped_ptr<DoInitializeOptions> options) { |
56 options->http_bridge_factory = MakeTestHttpBridgeFactory(); | 40 options->http_bridge_factory = |
| 41 scoped_ptr<syncer::HttpPostProviderFactory>( |
| 42 new browser_sync::TestHttpBridgeFactory()); |
57 options->sync_manager_factory.reset( | 43 options->sync_manager_factory.reset( |
58 new syncer::SyncManagerFactoryForProfileSyncTest( | 44 new syncer::SyncManagerFactoryForProfileSyncTest(callback_)); |
59 callback_, | |
60 set_initial_sync_ended_on_init_)); | |
61 options->credentials.email = "testuser@gmail.com"; | 45 options->credentials.email = "testuser@gmail.com"; |
62 options->credentials.sync_token = "token"; | 46 options->credentials.sync_token = "token"; |
63 options->restored_key_for_bootstrapping = ""; | 47 options->restored_key_for_bootstrapping = ""; |
64 syncer::StorageOption storage = storage_option_; | |
65 | 48 |
66 // It'd be nice if we avoided creating the InternalComponentsFactory in the | 49 // It'd be nice if we avoided creating the InternalComponentsFactory in the |
67 // first place, but SyncBackendHost will have created one by now so we must | 50 // first place, but SyncBackendHost will have created one by now so we must |
68 // free it. Grab the switches to pass on first. | 51 // free it. Grab the switches to pass on first. |
69 InternalComponentsFactory::Switches factory_switches = | 52 InternalComponentsFactory::Switches factory_switches = |
70 options->internal_components_factory->GetSwitches(); | 53 options->internal_components_factory->GetSwitches(); |
71 options->internal_components_factory.reset( | 54 options->internal_components_factory.reset( |
72 new TestInternalComponentsFactory(factory_switches, storage)); | 55 new TestInternalComponentsFactory(factory_switches, |
| 56 syncer::STORAGE_IN_MEMORY)); |
73 | 57 |
74 SyncBackendHostImpl::InitCore(options.Pass()); | 58 SyncBackendHostImpl::InitCore(options.Pass()); |
75 if (synchronous_init_ && !base::MessageLoop::current()->is_running()) { | |
76 // The SyncBackend posts a task to the current loop when | |
77 // initialization completes. | |
78 base::MessageLoop::current()->Run(); | |
79 } | |
80 } | |
81 | |
82 void SyncBackendHostForProfileSyncTest::UpdateCredentials( | |
83 const syncer::SyncCredentials& credentials) { | |
84 // If we had failed the initial download, complete initialization now. | |
85 if (!initial_download_closure_.is_null()) { | |
86 initial_download_closure_.Run(); | |
87 initial_download_closure_.Reset(); | |
88 } | |
89 } | 59 } |
90 | 60 |
91 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( | 61 void SyncBackendHostForProfileSyncTest::RequestConfigureSyncer( |
92 syncer::ConfigureReason reason, | 62 syncer::ConfigureReason reason, |
93 syncer::ModelTypeSet to_download, | 63 syncer::ModelTypeSet to_download, |
94 syncer::ModelTypeSet to_purge, | 64 syncer::ModelTypeSet to_purge, |
95 syncer::ModelTypeSet to_journal, | 65 syncer::ModelTypeSet to_journal, |
96 syncer::ModelTypeSet to_unapply, | 66 syncer::ModelTypeSet to_unapply, |
97 syncer::ModelTypeSet to_ignore, | 67 syncer::ModelTypeSet to_ignore, |
98 const syncer::ModelSafeRoutingInfo& routing_info, | 68 const syncer::ModelSafeRoutingInfo& routing_info, |
99 const base::Callback<void(syncer::ModelTypeSet, | 69 const base::Callback<void(syncer::ModelTypeSet, |
100 syncer::ModelTypeSet)>& ready_task, | 70 syncer::ModelTypeSet)>& ready_task, |
101 const base::Closure& retry_callback) { | 71 const base::Closure& retry_callback) { |
102 syncer::ModelTypeSet failed_configuration_types; | 72 syncer::ModelTypeSet failed_configuration_types; |
103 if (fail_initial_download_) | |
104 failed_configuration_types = to_download; | |
105 | 73 |
106 // The first parameter there should be the set of enabled types. That's not | 74 // The first parameter there should be the set of enabled types. That's not |
107 // something we have access to from this strange test harness. We'll just | 75 // something we have access to from this strange test harness. We'll just |
108 // send back the list of newly configured types instead and hope it doesn't | 76 // send back the list of newly configured types instead and hope it doesn't |
109 // break anything. | 77 // break anything. |
110 FinishConfigureDataTypesOnFrontendLoop( | 78 FinishConfigureDataTypesOnFrontendLoop( |
111 syncer::Difference(to_download, failed_configuration_types), | 79 syncer::Difference(to_download, failed_configuration_types), |
112 syncer::Difference(to_download, failed_configuration_types), | 80 syncer::Difference(to_download, failed_configuration_types), |
113 failed_configuration_types, | 81 failed_configuration_types, |
114 ready_task); | 82 ready_task); |
115 } | 83 } |
116 | 84 |
117 void | |
118 SyncBackendHostForProfileSyncTest::HandleInitializationSuccessOnFrontendLoop( | |
119 const syncer::WeakHandle<syncer::JsBackend> js_backend, | |
120 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener> | |
121 debug_info_listener) { | |
122 if (fail_initial_download_) { | |
123 // We interrupt this successful init to bring you a simulated failure. | |
124 initial_download_closure_ = base::Bind( | |
125 &SyncBackendHostForProfileSyncTest:: | |
126 HandleInitializationSuccessOnFrontendLoop, | |
127 weak_ptr_factory_.GetWeakPtr(), | |
128 js_backend, | |
129 debug_info_listener); | |
130 HandleControlTypesDownloadRetry(); | |
131 if (synchronous_init_) | |
132 base::MessageLoop::current()->Quit(); | |
133 } else { | |
134 SyncBackendHostImpl::HandleInitializationSuccessOnFrontendLoop( | |
135 js_backend, | |
136 debug_info_listener); | |
137 } | |
138 } | |
139 | |
140 } // namespace browser_sync | 85 } // namespace browser_sync |
141 | 86 |
142 syncer::TestIdFactory* TestProfileSyncService::id_factory() { | 87 syncer::TestIdFactory* TestProfileSyncService::id_factory() { |
143 return &id_factory_; | 88 return &id_factory_; |
144 } | 89 } |
145 | 90 |
146 browser_sync::SyncBackendHostForProfileSyncTest* | |
147 TestProfileSyncService::GetBackendForTest() { | |
148 return static_cast<browser_sync::SyncBackendHostForProfileSyncTest*>( | |
149 ProfileSyncService::GetBackendForTest()); | |
150 } | |
151 | |
152 syncer::WeakHandle<syncer::JsEventHandler> | 91 syncer::WeakHandle<syncer::JsEventHandler> |
153 TestProfileSyncService::GetJsEventHandler() { | 92 TestProfileSyncService::GetJsEventHandler() { |
154 return syncer::WeakHandle<syncer::JsEventHandler>(); | 93 return syncer::WeakHandle<syncer::JsEventHandler>(); |
155 } | 94 } |
156 | 95 |
157 TestProfileSyncService::TestProfileSyncService( | 96 TestProfileSyncService::TestProfileSyncService( |
158 ProfileSyncComponentsFactory* factory, | 97 ProfileSyncComponentsFactory* factory, |
159 Profile* profile, | 98 Profile* profile, |
160 SigninManagerBase* signin, | 99 SigninManagerBase* signin, |
161 ProfileOAuth2TokenService* oauth2_token_service, | 100 ProfileOAuth2TokenService* oauth2_token_service, |
162 ProfileSyncService::StartBehavior behavior, | 101 ProfileSyncService::StartBehavior behavior) |
163 bool synchronous_backend_initialization) | |
164 : ProfileSyncService(factory, | 102 : ProfileSyncService(factory, |
165 profile, | 103 profile, |
166 signin, | 104 signin, |
167 oauth2_token_service, | 105 oauth2_token_service, |
168 behavior), | 106 behavior) { |
169 synchronous_backend_initialization_( | |
170 synchronous_backend_initialization), | |
171 synchronous_sync_configuration_(false), | |
172 set_initial_sync_ended_on_init_(true), | |
173 fail_initial_download_(false), | |
174 storage_option_(syncer::STORAGE_IN_MEMORY) { | |
175 SetSyncSetupCompleted(); | 107 SetSyncSetupCompleted(); |
176 } | 108 } |
177 | 109 |
178 TestProfileSyncService::~TestProfileSyncService() { | 110 TestProfileSyncService::~TestProfileSyncService() { |
179 } | 111 } |
180 | 112 |
181 // static | 113 // static |
182 BrowserContextKeyedService* TestProfileSyncService::BuildAutoStartAsyncInit( | 114 BrowserContextKeyedService* TestProfileSyncService::BuildAutoStartAsyncInit( |
183 content::BrowserContext* context) { | 115 content::BrowserContext* context) { |
184 Profile* profile = static_cast<Profile*>(context); | 116 Profile* profile = static_cast<Profile*>(context); |
185 SigninManagerBase* signin = | 117 SigninManagerBase* signin = |
186 SigninManagerFactory::GetForProfile(profile); | 118 SigninManagerFactory::GetForProfile(profile); |
187 ProfileOAuth2TokenService* oauth2_token_service = | 119 ProfileOAuth2TokenService* oauth2_token_service = |
188 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 120 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
189 ProfileSyncComponentsFactoryMock* factory = | 121 ProfileSyncComponentsFactoryMock* factory = |
190 new ProfileSyncComponentsFactoryMock(); | 122 new ProfileSyncComponentsFactoryMock(); |
191 return new TestProfileSyncService(factory, | 123 return new TestProfileSyncService(factory, |
192 profile, | 124 profile, |
193 signin, | 125 signin, |
194 oauth2_token_service, | 126 oauth2_token_service, |
195 ProfileSyncService::AUTO_START, | 127 ProfileSyncService::AUTO_START); |
196 false); | |
197 } | 128 } |
198 | 129 |
199 ProfileSyncComponentsFactoryMock* | 130 ProfileSyncComponentsFactoryMock* |
200 TestProfileSyncService::components_factory_mock() { | 131 TestProfileSyncService::components_factory_mock() { |
201 // We always create a mock factory, see Build* routines. | 132 // We always create a mock factory, see Build* routines. |
202 return static_cast<ProfileSyncComponentsFactoryMock*>(factory()); | 133 return static_cast<ProfileSyncComponentsFactoryMock*>(factory()); |
203 } | 134 } |
204 | 135 |
205 void TestProfileSyncService::RequestAccessToken() { | |
206 ProfileSyncService::RequestAccessToken(); | |
207 if (synchronous_backend_initialization_) { | |
208 base::MessageLoop::current()->Run(); | |
209 } | |
210 } | |
211 | |
212 void TestProfileSyncService::OnGetTokenSuccess( | |
213 const OAuth2TokenService::Request* request, | |
214 const std::string& access_token, | |
215 const base::Time& expiration_time) { | |
216 ProfileSyncService::OnGetTokenSuccess(request, access_token, | |
217 expiration_time); | |
218 if (synchronous_backend_initialization_) { | |
219 base::MessageLoop::current()->Quit(); | |
220 } | |
221 } | |
222 | |
223 void TestProfileSyncService::OnGetTokenFailure( | |
224 const OAuth2TokenService::Request* request, | |
225 const GoogleServiceAuthError& error) { | |
226 ProfileSyncService::OnGetTokenFailure(request, error); | |
227 if (synchronous_backend_initialization_) { | |
228 base::MessageLoop::current()->Quit(); | |
229 } | |
230 } | |
231 | |
232 | |
233 void TestProfileSyncService::OnBackendInitialized( | |
234 const syncer::WeakHandle<syncer::JsBackend>& backend, | |
235 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& | |
236 debug_info_listener, | |
237 bool success) { | |
238 ProfileSyncService::OnBackendInitialized(backend, | |
239 debug_info_listener, | |
240 success); | |
241 | |
242 // TODO(akalin): Figure out a better way to do this. | |
243 if (synchronous_backend_initialization_) { | |
244 base::MessageLoop::current()->Quit(); | |
245 } | |
246 } | |
247 | |
248 void TestProfileSyncService::OnConfigureDone( | 136 void TestProfileSyncService::OnConfigureDone( |
249 const browser_sync::DataTypeManager::ConfigureResult& result) { | 137 const browser_sync::DataTypeManager::ConfigureResult& result) { |
250 ProfileSyncService::OnConfigureDone(result); | 138 ProfileSyncService::OnConfigureDone(result); |
251 if (!synchronous_sync_configuration_) | 139 base::MessageLoop::current()->Quit(); |
252 base::MessageLoop::current()->Quit(); | |
253 } | 140 } |
254 | 141 |
255 UserShare* TestProfileSyncService::GetUserShare() const { | 142 UserShare* TestProfileSyncService::GetUserShare() const { |
256 return backend_->GetUserShare(); | 143 return backend_->GetUserShare(); |
257 } | 144 } |
258 | 145 |
259 void TestProfileSyncService::dont_set_initial_sync_ended_on_init() { | |
260 set_initial_sync_ended_on_init_ = false; | |
261 } | |
262 void TestProfileSyncService::set_synchronous_sync_configuration() { | |
263 synchronous_sync_configuration_ = true; | |
264 } | |
265 void TestProfileSyncService::fail_initial_download() { | |
266 fail_initial_download_ = true; | |
267 } | |
268 void TestProfileSyncService::set_storage_option( | |
269 syncer::StorageOption storage_option) { | |
270 storage_option_ = storage_option; | |
271 } | |
272 | |
273 void TestProfileSyncService::CreateBackend() { | 146 void TestProfileSyncService::CreateBackend() { |
274 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( | 147 backend_.reset(new browser_sync::SyncBackendHostForProfileSyncTest( |
275 profile(), | 148 profile(), |
276 sync_prefs_.AsWeakPtr(), | 149 sync_prefs_.AsWeakPtr(), |
277 callback_, | 150 callback_)); |
278 set_initial_sync_ended_on_init_, | |
279 synchronous_backend_initialization_, | |
280 fail_initial_download_, | |
281 storage_option_)); | |
282 } | 151 } |
283 | 152 |
OLD | NEW |