| 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/signin/signin_tracker.h" | 5 #include "chrome/browser/signin/signin_tracker.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 content::NotificationService::current()->Notify( | 86 content::NotificationService::current()->Notify( |
| 87 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 87 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 88 content::Source<Profile>(profile_.get()), | 88 content::Source<Profile>(profile_.get()), |
| 89 content::Details<const GoogleServiceAuthError>(&error)); | 89 content::Details<const GoogleServiceAuthError>(&error)); |
| 90 } | 90 } |
| 91 | 91 |
| 92 TEST_F(SigninTrackerTest, GaiaSignInSucceeded) { | 92 TEST_F(SigninTrackerTest, GaiaSignInSucceeded) { |
| 93 // SIGNIN_SUCCEEDED notification should lead us to get a GaiCredentialsValid() | 93 // SIGNIN_SUCCEEDED notification should lead us to get a GaiCredentialsValid() |
| 94 // callback. | 94 // callback. |
| 95 EXPECT_CALL(observer_, GaiaCredentialsValid()); | 95 EXPECT_CALL(observer_, GaiaCredentialsValid()); |
| 96 EXPECT_CALL(*mock_pss_, AreCredentialsAvailable()) | 96 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()) |
| 97 .WillRepeatedly(Return(false)); | 97 .WillRepeatedly(Return(false)); |
| 98 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 98 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| 99 .WillRepeatedly(Return(false)); | 99 .WillRepeatedly(Return(false)); |
| 100 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); | 100 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); |
| 101 content::NotificationService::current()->Notify( | 101 content::NotificationService::current()->Notify( |
| 102 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 102 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 103 content::Source<Profile>(profile_.get()), | 103 content::Source<Profile>(profile_.get()), |
| 104 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 104 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 static void ExpectSignedInSyncService(ProfileSyncServiceMock* sync_service, | 107 static void ExpectSignedInSyncService(ProfileSyncServiceMock* sync_service, |
| 108 MockTokenService* token_service, | 108 MockTokenService* token_service, |
| 109 const GoogleServiceAuthError& error) { | 109 const GoogleServiceAuthError& error) { |
| 110 if (token_service) { | 110 if (token_service) { |
| 111 EXPECT_CALL(*token_service, HasTokenForService(_)) | 111 EXPECT_CALL(*token_service, HasTokenForService(_)) |
| 112 .WillRepeatedly(Return(true)); | 112 .WillRepeatedly(Return(true)); |
| 113 } | 113 } |
| 114 EXPECT_CALL(*sync_service, AreCredentialsAvailable()).WillRepeatedly( | 114 EXPECT_CALL(*sync_service, IsSyncEnabledAndLoggedIn()).WillRepeatedly( |
| 115 Return(true)); |
| 116 EXPECT_CALL(*sync_service, IsSyncTokenAvailable()).WillRepeatedly( |
| 115 Return(true)); | 117 Return(true)); |
| 116 EXPECT_CALL(*sync_service, waiting_for_auth()).WillRepeatedly(Return(false)); | 118 EXPECT_CALL(*sync_service, waiting_for_auth()).WillRepeatedly(Return(false)); |
| 117 EXPECT_CALL(*sync_service, GetAuthError()).WillRepeatedly(ReturnRef(error)); | 119 EXPECT_CALL(*sync_service, GetAuthError()).WillRepeatedly(ReturnRef(error)); |
| 118 EXPECT_CALL(*sync_service, unrecoverable_error_detected()).WillRepeatedly( | 120 EXPECT_CALL(*sync_service, unrecoverable_error_detected()).WillRepeatedly( |
| 119 Return(false)); | 121 Return(false)); |
| 120 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true)); | 122 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true)); |
| 121 | 123 |
| 122 } | 124 } |
| 123 | 125 |
| 124 TEST_F(SigninTrackerTest, GaiaSigninWhenServicesAlreadyRunning) { | 126 TEST_F(SigninTrackerTest, GaiaSigninWhenServicesAlreadyRunning) { |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 content::Source<TokenService>(mock_token_service_), | 212 content::Source<TokenService>(mock_token_service_), |
| 211 content::Details<const TokenService::TokenRequestFailedDetails>(&failed)); | 213 content::Details<const TokenService::TokenRequestFailedDetails>(&failed)); |
| 212 } | 214 } |
| 213 | 215 |
| 214 TEST_F(SigninTrackerTest, NoGaiaSigninWhenServicesNotRunning) { | 216 TEST_F(SigninTrackerTest, NoGaiaSigninWhenServicesNotRunning) { |
| 215 // SIGNIN_SUCCEEDED notification should not result in a SigninSuccess() | 217 // SIGNIN_SUCCEEDED notification should not result in a SigninSuccess() |
| 216 // callback if we're not already signed in. | 218 // callback if we're not already signed in. |
| 217 EXPECT_CALL(observer_, GaiaCredentialsValid()); | 219 EXPECT_CALL(observer_, GaiaCredentialsValid()); |
| 218 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 220 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| 219 .WillRepeatedly(Return(true)); | 221 .WillRepeatedly(Return(true)); |
| 220 EXPECT_CALL(*mock_pss_, AreCredentialsAvailable()).WillRepeatedly( | 222 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( |
| 223 Return(false)); |
| 224 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( |
| 221 Return(false)); | 225 Return(false)); |
| 222 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); | 226 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); |
| 223 content::NotificationService::current()->Notify( | 227 content::NotificationService::current()->Notify( |
| 224 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 228 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 225 content::Source<Profile>(profile_.get()), | 229 content::Source<Profile>(profile_.get()), |
| 226 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 230 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 227 } | 231 } |
| 228 | 232 |
| 229 TEST_F(SigninTrackerTest, GaiaSigninAfterSyncStarts) { | 233 TEST_F(SigninTrackerTest, GaiaSigninAfterSyncStarts) { |
| 230 // Make sure that we don't get a SigninSuccess() callback until after the | 234 // Make sure that we don't get a SigninSuccess() callback until after the |
| 231 // sync service reports that it's signed in. | 235 // sync service reports that it's signed in. |
| 232 EXPECT_CALL(observer_, GaiaCredentialsValid()); | 236 EXPECT_CALL(observer_, GaiaCredentialsValid()); |
| 233 EXPECT_CALL(*mock_pss_, AreCredentialsAvailable()).WillOnce( | 237 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillOnce( |
| 234 Return(false)); | 238 Return(false)); |
| 235 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 239 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| 236 .WillRepeatedly(Return(true)); | 240 .WillRepeatedly(Return(true)); |
| 237 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); | 241 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); |
| 238 content::NotificationService::current()->Notify( | 242 content::NotificationService::current()->Notify( |
| 239 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 243 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 240 content::Source<Profile>(profile_.get()), | 244 content::Source<Profile>(profile_.get()), |
| 241 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 245 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 242 Mock::VerifyAndClearExpectations(mock_pss_); | 246 Mock::VerifyAndClearExpectations(mock_pss_); |
| 243 // Mimic the sync engine getting credentials. | 247 // Mimic the sync engine getting credentials. |
| 244 EXPECT_CALL(observer_, SigninSuccess()); | 248 EXPECT_CALL(observer_, SigninSuccess()); |
| 245 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); | 249 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); |
| 246 ExpectSignedInSyncService(mock_pss_, mock_token_service_, error); | 250 ExpectSignedInSyncService(mock_pss_, mock_token_service_, error); |
| 247 tracker_->OnStateChanged(); | 251 tracker_->OnStateChanged(); |
| 248 } | 252 } |
| 249 | 253 |
| 250 TEST_F(SigninTrackerTest, SyncSigninError) { | 254 TEST_F(SigninTrackerTest, SyncSigninError) { |
| 251 // Make sure that we get a SigninFailed() callback if sync gets an error after | 255 // Make sure that we get a SigninFailed() callback if sync gets an error after |
| 252 // initializaiton. | 256 // initializaiton. |
| 253 EXPECT_CALL(observer_, GaiaCredentialsValid()); | 257 EXPECT_CALL(observer_, GaiaCredentialsValid()); |
| 254 EXPECT_CALL(*mock_pss_, AreCredentialsAvailable()).WillRepeatedly( | 258 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( |
| 259 Return(false)); |
| 260 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( |
| 255 Return(false)); | 261 Return(false)); |
| 256 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 262 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| 257 .WillRepeatedly(Return(true)); | 263 .WillRepeatedly(Return(true)); |
| 258 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); | 264 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); |
| 259 content::NotificationService::current()->Notify( | 265 content::NotificationService::current()->Notify( |
| 260 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 266 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 261 content::Source<Profile>(profile_.get()), | 267 content::Source<Profile>(profile_.get()), |
| 262 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 268 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 263 | 269 |
| 264 // Still waiting for auth, so sync state changes should be ignored. | 270 // Still waiting for auth, so sync state changes should be ignored. |
| 265 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(true)); | 271 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(true)); |
| 266 tracker_->OnStateChanged(); | 272 tracker_->OnStateChanged(); |
| 267 // Now mimic an auth error - this should cause us to fail (not waiting for | 273 // Now mimic an auth error - this should cause us to fail (not waiting for |
| 268 // auth, but still have no credentials). | 274 // auth, but still have no credentials). |
| 269 GoogleServiceAuthError error( | 275 GoogleServiceAuthError error( |
| 270 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 276 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 271 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); | 277 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); |
| 272 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(false)); | 278 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(false)); |
| 273 EXPECT_CALL(observer_, SigninFailed(error)); | 279 EXPECT_CALL(observer_, SigninFailed(error)); |
| 274 tracker_->OnStateChanged(); | 280 tracker_->OnStateChanged(); |
| 275 } | 281 } |
| 276 | 282 |
| 277 // TODO(kochi) Add tests with initial state != WATIING_FOR_GAIA_VALIDATION. | 283 // TODO(kochi) Add tests with initial state != WATIING_FOR_GAIA_VALIDATION. |
| OLD | NEW |