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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/browser/signin/signin_manager.h" | 10 #include "chrome/browser/signin/signin_manager.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 using browser_sync::DataTypeManager; | 28 using browser_sync::DataTypeManager; |
29 using browser_sync::DataTypeManagerMock; | 29 using browser_sync::DataTypeManagerMock; |
30 using content::BrowserThread; | 30 using content::BrowserThread; |
31 using testing::_; | 31 using testing::_; |
32 using testing::AnyNumber; | 32 using testing::AnyNumber; |
33 using testing::DoAll; | 33 using testing::DoAll; |
34 using testing::InvokeArgument; | 34 using testing::InvokeArgument; |
35 using testing::Mock; | 35 using testing::Mock; |
36 using testing::Return; | 36 using testing::Return; |
37 | 37 |
| 38 ACTION_P(InvokeOnConfigureStart, pss) { |
| 39 TestProfileSyncService* service = static_cast<TestProfileSyncService*>(pss); |
| 40 service->OnConfigureStart(); |
| 41 } |
| 42 |
| 43 ACTION_P2(InvokeOnConfigureDone, pss, result) { |
| 44 TestProfileSyncService* service = static_cast<TestProfileSyncService*>(pss); |
| 45 DataTypeManager::ConfigureResult configure_result = |
| 46 static_cast<DataTypeManager::ConfigureResult>(result); |
| 47 service->OnConfigureDone(configure_result); |
| 48 } |
| 49 |
38 // TODO(chron): Test not using cros_user flag and use signin_ | 50 // TODO(chron): Test not using cros_user flag and use signin_ |
39 class ProfileSyncServiceStartupTest : public testing::Test { | 51 class ProfileSyncServiceStartupTest : public testing::Test { |
40 public: | 52 public: |
41 ProfileSyncServiceStartupTest() | 53 ProfileSyncServiceStartupTest() |
42 : ui_thread_(BrowserThread::UI, &ui_loop_), | 54 : ui_thread_(BrowserThread::UI, &ui_loop_), |
43 db_thread_(BrowserThread::DB), | 55 db_thread_(BrowserThread::DB), |
44 file_thread_(BrowserThread::FILE), | 56 file_thread_(BrowserThread::FILE), |
45 io_thread_(BrowserThread::IO), | 57 io_thread_(BrowserThread::IO), |
46 profile_(new TestingProfile) {} | 58 profile_(new TestingProfile) {} |
47 | 59 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 new ProfileSyncComponentsFactoryMock(), | 93 new ProfileSyncComponentsFactoryMock(), |
82 profile_.get(), | 94 profile_.get(), |
83 signin, | 95 signin, |
84 ProfileSyncService::MANUAL_START, | 96 ProfileSyncService::MANUAL_START, |
85 true, | 97 true, |
86 base::Closure())); | 98 base::Closure())); |
87 } | 99 } |
88 | 100 |
89 DataTypeManagerMock* SetUpDataTypeManager() { | 101 DataTypeManagerMock* SetUpDataTypeManager() { |
90 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); | 102 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); |
91 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _)). | 103 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _, _)). |
92 WillOnce(Return(data_type_manager)); | 104 WillOnce(Return(data_type_manager)); |
93 return data_type_manager; | 105 return data_type_manager; |
94 } | 106 } |
95 | 107 |
96 ProfileSyncComponentsFactoryMock* factory_mock() { | 108 ProfileSyncComponentsFactoryMock* factory_mock() { |
97 return static_cast<ProfileSyncComponentsFactoryMock*>(service_->factory()); | 109 return static_cast<ProfileSyncComponentsFactoryMock*>(service_->factory()); |
98 } | 110 } |
99 | 111 |
100 MessageLoop ui_loop_; | 112 MessageLoop ui_loop_; |
101 content::TestBrowserThread ui_thread_; | 113 content::TestBrowserThread ui_thread_; |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, | 210 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, |
199 content::Source<TokenService>( | 211 content::Source<TokenService>( |
200 TokenServiceFactory::GetForProfile(profile_.get())), | 212 TokenServiceFactory::GetForProfile(profile_.get())), |
201 content::NotificationService::NoDetails()); | 213 content::NotificationService::NoDetails()); |
202 service_->SetSetupInProgress(false); | 214 service_->SetSetupInProgress(false); |
203 // Backend should initialize using a bogus GAIA token for credentials. | 215 // Backend should initialize using a bogus GAIA token for credentials. |
204 EXPECT_TRUE(service_->ShouldPushChanges()); | 216 EXPECT_TRUE(service_->ShouldPushChanges()); |
205 } | 217 } |
206 | 218 |
207 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { | 219 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { |
208 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _)).Times(0); | 220 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _, _)).Times(0); |
209 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | 221 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); |
210 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 222 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
211 | 223 |
212 service_->Initialize(); | 224 service_->Initialize(); |
213 // Sync should not start because there are no tokens yet. | 225 // Sync should not start because there are no tokens yet. |
214 EXPECT_FALSE(service_->ShouldPushChanges()); | 226 EXPECT_FALSE(service_->ShouldPushChanges()); |
215 EXPECT_FALSE(service_->GetBackendForTest()); | 227 EXPECT_FALSE(service_->GetBackendForTest()); |
216 content::NotificationService::current()->Notify( | 228 content::NotificationService::current()->Notify( |
217 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, | 229 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, |
218 content::Source<TokenService>( | 230 content::Source<TokenService>( |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 264 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
253 GaiaConstants::kSyncService, "sync_token"); | 265 GaiaConstants::kSyncService, "sync_token"); |
254 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); | 266 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); |
255 service_->Initialize(); | 267 service_->Initialize(); |
256 } | 268 } |
257 | 269 |
258 TEST_F(ProfileSyncServiceStartupTest, ManagedStartup) { | 270 TEST_F(ProfileSyncServiceStartupTest, ManagedStartup) { |
259 // Disable sync through policy. | 271 // Disable sync through policy. |
260 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); | 272 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); |
261 | 273 |
262 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _)).Times(0); | 274 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _, _)).Times(0); |
263 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 275 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
264 | 276 |
265 // Service should not be started by Initialize() since it's managed. | 277 // Service should not be started by Initialize() since it's managed. |
266 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 278 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
267 GaiaConstants::kSyncService, "sync_token"); | 279 GaiaConstants::kSyncService, "sync_token"); |
268 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); | 280 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); |
269 service_->Initialize(); | 281 service_->Initialize(); |
270 } | 282 } |
271 | 283 |
272 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) { | 284 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) { |
(...skipping 10 matching lines...) Expand all Loading... |
283 Mock::VerifyAndClearExpectations(data_type_manager); | 295 Mock::VerifyAndClearExpectations(data_type_manager); |
284 EXPECT_CALL(*data_type_manager, state()). | 296 EXPECT_CALL(*data_type_manager, state()). |
285 WillOnce(Return(DataTypeManager::CONFIGURED)); | 297 WillOnce(Return(DataTypeManager::CONFIGURED)); |
286 EXPECT_CALL(*data_type_manager, Stop()).Times(1); | 298 EXPECT_CALL(*data_type_manager, Stop()).Times(1); |
287 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 299 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
288 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); | 300 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); |
289 | 301 |
290 // When switching back to unmanaged, the state should change, but the service | 302 // When switching back to unmanaged, the state should change, but the service |
291 // should not start up automatically (kSyncSetupCompleted will be false). | 303 // should not start up automatically (kSyncSetupCompleted will be false). |
292 Mock::VerifyAndClearExpectations(data_type_manager); | 304 Mock::VerifyAndClearExpectations(data_type_manager); |
293 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _)).Times(0); | 305 EXPECT_CALL(*factory_mock(), CreateDataTypeManager(_, _, _)).Times(0); |
294 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 306 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
295 profile_->GetPrefs()->ClearPref(prefs::kSyncManaged); | 307 profile_->GetPrefs()->ClearPref(prefs::kSyncManaged); |
296 } | 308 } |
297 | 309 |
298 TEST_F(ProfileSyncServiceStartupTest, StartFailure) { | 310 TEST_F(ProfileSyncServiceStartupTest, StartFailure) { |
299 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); | 311 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); |
300 DataTypeManager::ConfigureStatus status = DataTypeManager::ABORTED; | 312 DataTypeManager::ConfigureStatus status = DataTypeManager::ABORTED; |
301 syncer::SyncError error( | 313 syncer::SyncError error( |
302 FROM_HERE, "Association failed.", syncer::BOOKMARKS); | 314 FROM_HERE, "Association failed.", syncer::BOOKMARKS); |
303 std::list<syncer::SyncError> errors; | 315 std::list<syncer::SyncError> errors; |
304 errors.push_back(error); | 316 errors.push_back(error); |
305 browser_sync::DataTypeManager::ConfigureResult result( | 317 DataTypeManager::ConfigureResult result( |
306 status, | 318 status, |
307 syncer::ModelTypeSet(), | 319 syncer::ModelTypeSet(), |
308 errors, | 320 errors, |
309 syncer::ModelTypeSet()); | 321 syncer::ModelTypeSet()); |
310 EXPECT_CALL(*data_type_manager, Configure(_, _)). | 322 EXPECT_CALL(*data_type_manager, Configure(_, _)). |
311 WillRepeatedly( | 323 WillRepeatedly( |
312 DoAll( | 324 DoAll(InvokeOnConfigureStart(service_.get()), |
313 NotifyFromDataTypeManager(data_type_manager, | 325 InvokeOnConfigureDone(service_.get(), result))); |
314 static_cast<int>(chrome::NOTIFICATION_SYNC_CONFIGURE_START)), | |
315 NotifyFromDataTypeManagerWithResult(data_type_manager, | |
316 static_cast<int>(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE), | |
317 &result))); | |
318 EXPECT_CALL(*data_type_manager, state()). | 326 EXPECT_CALL(*data_type_manager, state()). |
319 WillOnce(Return(DataTypeManager::STOPPED)); | 327 WillOnce(Return(DataTypeManager::STOPPED)); |
320 | 328 |
321 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 329 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
322 | 330 |
323 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 331 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
324 GaiaConstants::kSyncService, "sync_token"); | 332 GaiaConstants::kSyncService, "sync_token"); |
325 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); | 333 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); |
326 service_->Initialize(); | 334 service_->Initialize(); |
327 EXPECT_TRUE(service_->HasUnrecoverableError()); | 335 EXPECT_TRUE(service_->HasUnrecoverableError()); |
328 } | 336 } |
329 | 337 |
330 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) { | 338 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) { |
331 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | 339 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); |
332 | 340 |
333 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 341 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
334 | 342 |
335 // Preload the tokens. | 343 // Preload the tokens. |
336 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 344 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
337 GaiaConstants::kSyncService, "sync_token"); | 345 GaiaConstants::kSyncService, "sync_token"); |
338 service_->fail_initial_download(); | 346 service_->fail_initial_download(); |
339 | 347 |
340 service_->Initialize(); | 348 service_->Initialize(); |
341 EXPECT_FALSE(service_->sync_initialized()); | 349 EXPECT_FALSE(service_->sync_initialized()); |
342 EXPECT_FALSE(service_->GetBackendForTest()); | 350 EXPECT_FALSE(service_->GetBackendForTest()); |
343 } | 351 } |
OLD | NEW |