| 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" |
| 11 #include "chrome/browser/signin/fake_auth_status_provider.h" |
| 11 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "chrome/browser/signin/signin_manager_fake.h" | 14 #include "chrome/browser/signin/signin_manager_fake.h" |
| 14 #include "chrome/browser/signin/token_service.h" | 15 #include "chrome/browser/signin/token_service.h" |
| 15 #include "chrome/browser/signin/token_service_factory.h" | 16 #include "chrome/browser/signin/token_service_factory.h" |
| 16 #include "chrome/browser/sync/profile_sync_service_factory.h" | 17 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 17 #include "chrome/browser/sync/profile_sync_service_mock.h" | 18 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 20 #include "google_apis/gaia/gaia_constants.h" | 21 #include "google_apis/gaia/gaia_constants.h" |
| 21 #include "google_apis/gaia/google_service_auth_error.h" | 22 #include "google_apis/gaia/google_service_auth_error.h" |
| 22 | 23 |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using ::testing::_; | 27 using ::testing::_; |
| 27 using ::testing::AnyNumber; | 28 using ::testing::AnyNumber; |
| 28 using ::testing::Mock; | 29 using ::testing::Mock; |
| 29 using ::testing::Return; | 30 using ::testing::Return; |
| 30 using ::testing::ReturnRef; | 31 using ::testing::ReturnRef; |
| 31 | 32 |
| 33 namespace { |
| 34 |
| 32 class MockTokenService : public TokenService { | 35 class MockTokenService : public TokenService { |
| 33 public: | 36 public: |
| 34 MockTokenService() { } | 37 MockTokenService() { } |
| 35 virtual ~MockTokenService() { } | 38 virtual ~MockTokenService() { } |
| 36 | 39 |
| 37 MOCK_CONST_METHOD1(HasTokenForService, bool(const char*)); | 40 MOCK_CONST_METHOD1(HasTokenForService, bool(const char*)); |
| 38 }; | 41 }; |
| 39 | 42 |
| 40 ProfileKeyedService* BuildMockTokenService(Profile* profile) { | 43 ProfileKeyedService* BuildMockTokenService(Profile* profile) { |
| 41 return new MockTokenService; | 44 return new MockTokenService; |
| 42 } | 45 } |
| 43 | 46 |
| 44 class MockObserver : public SigninTracker::Observer { | 47 class MockObserver : public SigninTracker::Observer { |
| 45 public: | 48 public: |
| 46 MockObserver() {} | 49 MockObserver() {} |
| 47 ~MockObserver() {} | 50 ~MockObserver() {} |
| 48 | 51 |
| 49 MOCK_METHOD0(GaiaCredentialsValid, void(void)); | 52 MOCK_METHOD0(GaiaCredentialsValid, void(void)); |
| 50 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&)); | 53 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&)); |
| 51 MOCK_METHOD0(SigninSuccess, void(void)); | 54 MOCK_METHOD0(SigninSuccess, void(void)); |
| 52 }; | 55 }; |
| 53 | 56 |
| 57 } // namespace |
| 58 |
| 54 class SigninTrackerTest : public testing::Test { | 59 class SigninTrackerTest : public testing::Test { |
| 55 public: | 60 public: |
| 56 SigninTrackerTest() {} | 61 SigninTrackerTest() {} |
| 57 virtual void SetUp() OVERRIDE { | 62 virtual void SetUp() OVERRIDE { |
| 58 profile_.reset(ProfileSyncServiceMock::MakeSignedInTestingProfile()); | 63 profile_.reset(ProfileSyncServiceMock::MakeSignedInTestingProfile()); |
| 59 mock_token_service_ = static_cast<MockTokenService*>( | 64 mock_token_service_ = static_cast<MockTokenService*>( |
| 60 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 65 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 61 profile_.get(), BuildMockTokenService)); | 66 profile_.get(), BuildMockTokenService)); |
| 62 mock_pss_ = static_cast<ProfileSyncServiceMock*>( | 67 mock_pss_ = static_cast<ProfileSyncServiceMock*>( |
| 63 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 68 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 content::Source<Profile>(profile_.get()), | 289 content::Source<Profile>(profile_.get()), |
| 285 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 290 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 286 | 291 |
| 287 // Still waiting for auth, so sync state changes should be ignored. | 292 // Still waiting for auth, so sync state changes should be ignored. |
| 288 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(true)); | 293 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(true)); |
| 289 tracker_->OnStateChanged(); | 294 tracker_->OnStateChanged(); |
| 290 // Now mimic an auth error - this should cause us to fail (not waiting for | 295 // Now mimic an auth error - this should cause us to fail (not waiting for |
| 291 // auth, but still have no credentials). | 296 // auth, but still have no credentials). |
| 292 GoogleServiceAuthError error( | 297 GoogleServiceAuthError error( |
| 293 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 298 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 294 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); | 299 FakeAuthStatusProvider provider(mock_signin_manager_->signin_global_error()); |
| 295 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillOnce(Return(false)); | 300 provider.set_auth_error(error); |
| 301 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false)); |
| 296 EXPECT_CALL(observer_, SigninFailed(error)); | 302 EXPECT_CALL(observer_, SigninFailed(error)); |
| 297 tracker_->OnStateChanged(); | 303 tracker_->OnStateChanged(); |
| 298 } | 304 } |
| 299 | 305 |
| 300 // Test cases for initial state = SERVICES_INITIALIZING. | 306 // Test cases for initial state = SERVICES_INITIALIZING. |
| 301 TEST_F(SigninTrackerTest, SigninSuccess) { | 307 TEST_F(SigninTrackerTest, SigninSuccess) { |
| 302 // Reset the |tracker_| and restart with initial state parameter for its | 308 // Reset the |tracker_| and restart with initial state parameter for its |
| 303 // constructor later. | 309 // constructor later. |
| 304 tracker_.reset(); | 310 tracker_.reset(); |
| 305 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); | 311 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 336 } | 342 } |
| 337 | 343 |
| 338 TEST_F(SigninTrackerTest, SigninFailedGoogleServiceAuthError) { | 344 TEST_F(SigninTrackerTest, SigninFailedGoogleServiceAuthError) { |
| 339 tracker_.reset(); | 345 tracker_.reset(); |
| 340 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 346 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| 341 .WillRepeatedly(Return(true)); | 347 .WillRepeatedly(Return(true)); |
| 342 mock_signin_manager_->StartSignInWithCredentials("0", "username@gmail.com", | 348 mock_signin_manager_->StartSignInWithCredentials("0", "username@gmail.com", |
| 343 "password"); | 349 "password"); |
| 344 // Inject authentication error. | 350 // Inject authentication error. |
| 345 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE); | 351 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE); |
| 352 FakeAuthStatusProvider provider(mock_signin_manager_->signin_global_error()); |
| 353 provider.set_auth_error(error); |
| 346 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( | 354 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( |
| 347 Return(true)); | 355 Return(true)); |
| 348 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( | 356 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( |
| 349 Return(true)); | 357 Return(true)); |
| 350 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false)); | 358 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false)); |
| 351 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); | 359 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); |
| 352 EXPECT_CALL(*mock_pss_, HasUnrecoverableError()).WillRepeatedly( | 360 EXPECT_CALL(*mock_pss_, HasUnrecoverableError()).WillRepeatedly( |
| 353 Return(false)); | 361 Return(false)); |
| 354 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true)); | 362 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true)); |
| 355 // Any error should result in SigninFailed() being called. | 363 // Any error should result in SigninFailed() being called. |
| 356 EXPECT_CALL(observer_, SigninFailed(error)); | 364 EXPECT_CALL(observer_, SigninFailed(error)); |
| 357 tracker_.reset(new SigninTracker(profile_.get(), &observer_, | 365 tracker_.reset(new SigninTracker(profile_.get(), &observer_, |
| 358 SigninTracker::SERVICES_INITIALIZING)); | 366 SigninTracker::SERVICES_INITIALIZING)); |
| 359 } | 367 } |
| 360 | 368 |
| 361 | 369 |
| 362 TEST_F(SigninTrackerTest, SigninFailedWhenInitializing) { | 370 TEST_F(SigninTrackerTest, SigninFailedWhenInitializing) { |
| 363 tracker_.reset(); | 371 tracker_.reset(); |
| 364 // SigninFailed() should be called. | 372 // SigninFailed() should be called. |
| 365 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); | 373 GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| 366 EXPECT_CALL(observer_, SigninFailed(error)); | 374 EXPECT_CALL(observer_, SigninFailed(error)); |
| 367 tracker_.reset(new SigninTracker(profile_.get(), &observer_, | 375 tracker_.reset(new SigninTracker(profile_.get(), &observer_, |
| 368 SigninTracker::SERVICES_INITIALIZING)); | 376 SigninTracker::SERVICES_INITIALIZING)); |
| 369 tracker_->OnStateChanged(); | 377 tracker_->OnStateChanged(); |
| 370 } | 378 } |
| OLD | NEW |