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 "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "chrome/browser/signin/fake_signin_manager.h" | 10 #include "chrome/browser/signin/fake_signin_manager.h" |
11 #include "chrome/browser/signin/signin_manager.h" | 11 #include "chrome/browser/signin/signin_manager.h" |
12 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
13 #include "chrome/browser/signin/token_service.h" | 13 #include "chrome/browser/signin/token_service.h" |
14 #include "chrome/browser/signin/token_service_factory.h" | 14 #include "chrome/browser/signin/token_service_factory.h" |
15 #include "chrome/browser/sync/glue/data_type_manager.h" | 15 #include "chrome/browser/sync/glue/data_type_manager.h" |
16 #include "chrome/browser/sync/glue/data_type_manager_mock.h" | 16 #include "chrome/browser/sync/glue/data_type_manager_mock.h" |
17 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 17 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
18 #include "chrome/browser/sync/profile_sync_service_factory.h" | 18 #include "chrome/browser/sync/profile_sync_service_factory.h" |
19 #include "chrome/browser/sync/profile_sync_test_util.h" | 19 #include "chrome/browser/sync/profile_sync_test_util.h" |
20 #include "chrome/browser/sync/sync_prefs.h" | 20 #include "chrome/browser/sync/sync_prefs.h" |
21 #include "chrome/browser/sync/test_profile_sync_service.h" | 21 #include "chrome/browser/sync/test_profile_sync_service.h" |
22 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/public/browser/notification_service.h" |
| 26 #include "content/public/browser/notification_source.h" |
25 #include "content/public/test/test_browser_thread.h" | 27 #include "content/public/test/test_browser_thread.h" |
26 #include "google_apis/gaia/gaia_auth_consumer.h" | 28 #include "google_apis/gaia/gaia_auth_consumer.h" |
27 #include "google_apis/gaia/gaia_constants.h" | 29 #include "google_apis/gaia/gaia_constants.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
29 | 31 |
30 using browser_sync::DataTypeManager; | 32 using browser_sync::DataTypeManager; |
31 using browser_sync::DataTypeManagerMock; | 33 using browser_sync::DataTypeManagerMock; |
32 using content::BrowserThread; | 34 using content::BrowserThread; |
33 using testing::_; | 35 using testing::_; |
34 using testing::AnyNumber; | 36 using testing::AnyNumber; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 profile_.reset(); | 94 profile_.reset(); |
93 | 95 |
94 // Pump messages posted by the sync core thread (which may end up | 96 // Pump messages posted by the sync core thread (which may end up |
95 // posting on the IO thread). | 97 // posting on the IO thread). |
96 ui_loop_.RunUntilIdle(); | 98 ui_loop_.RunUntilIdle(); |
97 io_thread_.Stop(); | 99 io_thread_.Stop(); |
98 file_thread_.Stop(); | 100 file_thread_.Stop(); |
99 ui_loop_.RunUntilIdle(); | 101 ui_loop_.RunUntilIdle(); |
100 } | 102 } |
101 | 103 |
| 104 void Signin() { |
| 105 sync_->signin()->SetAuthenticatedUsername("test_user"); |
| 106 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
| 107 "test_user"); |
| 108 GoogleServiceSigninSuccessDetails details("test_user", ""); |
| 109 content::NotificationService::current()->Notify( |
| 110 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 111 content::Source<Profile>(profile_.get()), |
| 112 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 113 } |
| 114 |
102 static ProfileKeyedService* BuildService(Profile* profile) { | 115 static ProfileKeyedService* BuildService(Profile* profile) { |
103 SigninManager* signin = static_cast<SigninManager*>( | 116 SigninManagerBase* signin = static_cast<SigninManagerBase*>( |
104 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 117 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
105 profile, FakeSigninManager::Build)); | 118 profile, FakeSigninManagerBase::Build)); |
106 signin->SetAuthenticatedUsername("test_user"); | 119 signin->SetAuthenticatedUsername("test_user"); |
107 return new TestProfileSyncService( | 120 return new TestProfileSyncService( |
108 new ProfileSyncComponentsFactoryMock(), | 121 new ProfileSyncComponentsFactoryMock(), |
109 profile, | 122 profile, |
110 signin, | 123 signin, |
111 ProfileSyncService::MANUAL_START, | 124 ProfileSyncService::MANUAL_START, |
112 true); | 125 true); |
113 } | 126 } |
114 | 127 |
115 protected: | 128 protected: |
(...skipping 18 matching lines...) Expand all Loading... |
134 content::TestBrowserThread file_thread_; | 147 content::TestBrowserThread file_thread_; |
135 content::TestBrowserThread io_thread_; | 148 content::TestBrowserThread io_thread_; |
136 scoped_ptr<TestingProfile> profile_; | 149 scoped_ptr<TestingProfile> profile_; |
137 TestProfileSyncService* sync_; | 150 TestProfileSyncService* sync_; |
138 ProfileSyncServiceObserverMock observer_; | 151 ProfileSyncServiceObserverMock observer_; |
139 }; | 152 }; |
140 | 153 |
141 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { | 154 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { |
142 public: | 155 public: |
143 static ProfileKeyedService* BuildCrosService(Profile* profile) { | 156 static ProfileKeyedService* BuildCrosService(Profile* profile) { |
144 SigninManager* signin = SigninManagerFactory::GetForProfile(profile); | 157 SigninManagerBase* signin = |
| 158 SigninManagerFactory::GetForProfile(profile); |
145 signin->SetAuthenticatedUsername("test_user"); | 159 signin->SetAuthenticatedUsername("test_user"); |
146 return new TestProfileSyncService( | 160 return new TestProfileSyncService( |
147 new ProfileSyncComponentsFactoryMock(), | 161 new ProfileSyncComponentsFactoryMock(), |
148 profile, | 162 profile, |
149 signin, | 163 signin, |
150 ProfileSyncService::AUTO_START, | 164 ProfileSyncService::AUTO_START, |
151 true); | 165 true); |
152 } | 166 } |
| 167 |
153 protected: | 168 protected: |
154 virtual void CreateSyncService() OVERRIDE { | 169 virtual void CreateSyncService() OVERRIDE { |
155 sync_ = static_cast<TestProfileSyncService*>( | 170 sync_ = static_cast<TestProfileSyncService*>( |
156 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 171 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
157 profile_.get(), BuildCrosService)); | 172 profile_.get(), BuildCrosService)); |
158 } | 173 } |
159 }; | 174 }; |
160 | 175 |
161 TEST_F(ProfileSyncServiceStartupTest, StartFirstTime) { | 176 TEST_F(ProfileSyncServiceStartupTest, StartFirstTime) { |
162 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); | 177 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); |
(...skipping 19 matching lines...) Expand all Loading... |
182 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1); | 197 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1); |
183 EXPECT_CALL(*data_type_manager, state()). | 198 EXPECT_CALL(*data_type_manager, state()). |
184 WillOnce(Return(DataTypeManager::CONFIGURED)). | 199 WillOnce(Return(DataTypeManager::CONFIGURED)). |
185 WillOnce(Return(DataTypeManager::CONFIGURED)); | 200 WillOnce(Return(DataTypeManager::CONFIGURED)); |
186 EXPECT_CALL(*data_type_manager, Stop()).Times(1); | 201 EXPECT_CALL(*data_type_manager, Stop()).Times(1); |
187 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 202 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
188 | 203 |
189 // Create some tokens in the token service; the service will startup when | 204 // Create some tokens in the token service; the service will startup when |
190 // it is notified that tokens are available. | 205 // it is notified that tokens are available. |
191 sync_->SetSetupInProgress(true); | 206 sync_->SetSetupInProgress(true); |
192 sync_->signin() | 207 Signin(); |
193 ->StartSignIn("test_user", std::string(), std::string(), std::string()); | 208 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
194 TokenServiceFactory::GetForProfile(profile_.get()) | 209 GaiaConstants::kSyncService, "sync_token"); |
195 ->IssueAuthTokenForTest(GaiaConstants::kSyncService, "sync_token"); | |
196 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 210 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
197 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token"); | 211 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token"); |
198 sync_->SetSetupInProgress(false); | 212 sync_->SetSetupInProgress(false); |
199 EXPECT_TRUE(sync_->ShouldPushChanges()); | 213 EXPECT_TRUE(sync_->ShouldPushChanges()); |
200 } | 214 } |
201 | 215 |
202 ProfileKeyedService* BuildFakeTokenService(Profile* profile) { | 216 ProfileKeyedService* BuildFakeTokenService(Profile* profile) { |
203 return new FakeTokenService(); | 217 return new FakeTokenService(); |
204 } | 218 } |
205 | 219 |
(...skipping 23 matching lines...) Expand all Loading... |
229 | 243 |
230 // Then start things up. | 244 // Then start things up. |
231 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1); | 245 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1); |
232 EXPECT_CALL(*data_type_manager, state()). | 246 EXPECT_CALL(*data_type_manager, state()). |
233 WillOnce(Return(DataTypeManager::CONFIGURED)). | 247 WillOnce(Return(DataTypeManager::CONFIGURED)). |
234 WillOnce(Return(DataTypeManager::CONFIGURED)); | 248 WillOnce(Return(DataTypeManager::CONFIGURED)); |
235 EXPECT_CALL(*data_type_manager, Stop()).Times(1); | 249 EXPECT_CALL(*data_type_manager, Stop()).Times(1); |
236 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 250 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
237 | 251 |
238 sync_->SetSetupInProgress(true); | 252 sync_->SetSetupInProgress(true); |
239 sync_->signin() | 253 Signin(); |
240 ->StartSignIn("test_user", std::string(), std::string(), std::string()); | |
241 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens. | 254 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens. |
242 token_service->LoadTokensFromDB(); | 255 token_service->LoadTokensFromDB(); |
243 sync_->SetSetupInProgress(false); | 256 sync_->SetSetupInProgress(false); |
244 // Backend should initialize using a bogus GAIA token for credentials. | 257 // Backend should initialize using a bogus GAIA token for credentials. |
245 EXPECT_TRUE(sync_->ShouldPushChanges()); | 258 EXPECT_TRUE(sync_->ShouldPushChanges()); |
246 } | 259 } |
247 | 260 |
248 TEST_F(ProfileSyncServiceStartupTest, StartInvalidCredentials) { | 261 TEST_F(ProfileSyncServiceStartupTest, StartInvalidCredentials) { |
249 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); | 262 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); |
250 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); | 263 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); |
(...skipping 13 matching lines...) Expand all Loading... |
264 EXPECT_FALSE(sync_->ShouldPushChanges()); | 277 EXPECT_FALSE(sync_->ShouldPushChanges()); |
265 Mock::VerifyAndClearExpectations(data_type_manager); | 278 Mock::VerifyAndClearExpectations(data_type_manager); |
266 | 279 |
267 // Update the credentials, unstalling the backend. | 280 // Update the credentials, unstalling the backend. |
268 EXPECT_CALL(*data_type_manager, Configure(_, _)); | 281 EXPECT_CALL(*data_type_manager, Configure(_, _)); |
269 EXPECT_CALL(*data_type_manager, state()). | 282 EXPECT_CALL(*data_type_manager, state()). |
270 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); | 283 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); |
271 EXPECT_CALL(*data_type_manager, Stop()).Times(1); | 284 EXPECT_CALL(*data_type_manager, Stop()).Times(1); |
272 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 285 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
273 sync_->SetSetupInProgress(true); | 286 sync_->SetSetupInProgress(true); |
274 sync_->signin() | 287 Signin(); |
275 ->StartSignIn("test_user", std::string(), std::string(), std::string()); | 288 token_service->IssueAuthTokenForTest( |
276 token_service->IssueAuthTokenForTest(GaiaConstants::kSyncService, | 289 GaiaConstants::kSyncService, "sync_token"); |
277 "sync_token"); | |
278 sync_->SetSetupInProgress(false); | 290 sync_->SetSetupInProgress(false); |
279 MessageLoop::current()->Run(); | 291 MessageLoop::current()->Run(); |
280 | 292 |
281 // Verify we successfully finish startup and configuration. | 293 // Verify we successfully finish startup and configuration. |
282 EXPECT_TRUE(sync_->ShouldPushChanges()); | 294 EXPECT_TRUE(sync_->ShouldPushChanges()); |
283 } | 295 } |
284 | 296 |
285 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { | 297 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { |
286 EXPECT_CALL(*sync_->components_factory_mock(), | 298 EXPECT_CALL(*sync_->components_factory_mock(), |
287 CreateDataTypeManager(_, _, _, _, _)).Times(0); | 299 CreateDataTypeManager(_, _, _, _, _)).Times(0); |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 | 479 |
468 // Preload the tokens. | 480 // Preload the tokens. |
469 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 481 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
470 GaiaConstants::kSyncService, "sync_token"); | 482 GaiaConstants::kSyncService, "sync_token"); |
471 sync_->fail_initial_download(); | 483 sync_->fail_initial_download(); |
472 | 484 |
473 sync_->Initialize(); | 485 sync_->Initialize(); |
474 EXPECT_FALSE(sync_->sync_initialized()); | 486 EXPECT_FALSE(sync_->sync_initialized()); |
475 EXPECT_FALSE(sync_->GetBackendForTest()); | 487 EXPECT_FALSE(sync_->GetBackendForTest()); |
476 } | 488 } |
OLD | NEW |