Chromium Code Reviews| 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/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/signin/fake_auth_status_provider.h" | 12 #include "chrome/browser/signin/fake_auth_status_provider.h" |
| 13 #include "chrome/browser/signin/fake_signin_manager.h" | 13 #include "chrome/browser/signin/fake_signin_manager.h" |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service.h" | |
| 15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 14 #include "chrome/browser/signin/signin_manager.h" | 16 #include "chrome/browser/signin/signin_manager.h" |
| 15 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 16 #include "chrome/browser/signin/token_service.h" | |
| 17 #include "chrome/browser/signin/token_service_factory.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_service_mock.h" | 19 #include "chrome/browser/sync/profile_sync_service_mock.h" |
| 20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 22 #include "google_apis/gaia/gaia_constants.h" | 22 #include "google_apis/gaia/gaia_constants.h" |
| 23 #include "google_apis/gaia/google_service_auth_error.h" | 23 #include "google_apis/gaia/google_service_auth_error.h" |
| 24 | 24 |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 27 |
| 28 using ::testing::_; | 28 using ::testing::_; |
| 29 using ::testing::AnyNumber; | 29 using ::testing::AnyNumber; |
| 30 using ::testing::Mock; | 30 using ::testing::Mock; |
| 31 using ::testing::Return; | 31 using ::testing::Return; |
| 32 using ::testing::ReturnRef; | 32 using ::testing::ReturnRef; |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 class MockTokenService : public TokenService { | 36 class MockOAuth2TokenService : public ProfileOAuth2TokenService { |
| 37 public: | 37 public: |
| 38 MockTokenService() { } | 38 MockOAuth2TokenService() : ProfileOAuth2TokenService() {} |
| 39 virtual ~MockTokenService() { } | 39 virtual ~MockOAuth2TokenService() {} |
| 40 | 40 |
| 41 MOCK_CONST_METHOD1(HasTokenForService, bool(const char*)); | 41 MOCK_METHOD0(GetRefreshToken, std::string()); |
|
Andrew T Wilson (Slow)
2013/08/19 15:23:06
BTW, this is a misuse of GMock (using a mock inste
Roger Tawa OOO till Jul 10th
2013/08/29 19:28:41
Changed to use new fake o2ts.
| |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 BrowserContextKeyedService* BuildMockTokenService( | 44 BrowserContextKeyedService* BuildMockOAuth2TokenService( |
| 45 content::BrowserContext* profile) { | 45 content::BrowserContext* profile) { |
| 46 return new MockTokenService; | 46 MockOAuth2TokenService* token_service = new MockOAuth2TokenService; |
| 47 token_service->Initialize(static_cast<Profile*>(profile)); | |
| 48 return token_service; | |
| 47 } | 49 } |
| 48 | 50 |
| 49 class MockObserver : public SigninTracker::Observer { | 51 class MockObserver : public SigninTracker::Observer { |
| 50 public: | 52 public: |
| 51 MockObserver() {} | 53 MockObserver() {} |
| 52 ~MockObserver() {} | 54 ~MockObserver() {} |
| 53 | 55 |
| 54 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&)); | 56 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&)); |
| 55 MOCK_METHOD0(SigninSuccess, void(void)); | 57 MOCK_METHOD0(SigninSuccess, void(void)); |
| 56 }; | 58 }; |
| 57 | 59 |
| 58 } // namespace | 60 } // namespace |
| 59 | 61 |
| 60 class SigninTrackerTest : public testing::Test { | 62 class SigninTrackerTest : public testing::Test { |
| 61 public: | 63 public: |
| 62 SigninTrackerTest() {} | 64 SigninTrackerTest() {} |
| 63 virtual void SetUp() OVERRIDE { | 65 virtual void SetUp() OVERRIDE { |
| 64 profile_.reset(new TestingProfile()); | 66 profile_.reset(new TestingProfile()); |
| 65 mock_token_service_ = static_cast<MockTokenService*>( | 67 mock_oauth2_token_service_ = static_cast<MockOAuth2TokenService*>( |
| 66 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 68 ProfileOAuth2TokenServiceFactory::GetInstance()-> |
|
Andrew T Wilson (Slow)
2013/08/19 15:23:06
After I land my CL, this is going to crash - you'l
Roger Tawa OOO till Jul 10th
2013/08/29 19:28:41
Done.
| |
| 67 profile_.get(), BuildMockTokenService)); | 69 SetTestingFactoryAndUse(profile_.get(), |
| 70 BuildMockOAuth2TokenService)); | |
| 68 | 71 |
| 69 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>( | 72 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>( |
| 70 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( | 73 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| 71 profile_.get(), FakeSigninManagerBase::Build)); | 74 profile_.get(), FakeSigninManagerBase::Build)); |
| 72 mock_signin_manager_->Initialize(profile_.get(), NULL); | 75 mock_signin_manager_->Initialize(profile_.get(), NULL); |
| 73 tracker_.reset(new SigninTracker(profile_.get(), &observer_)); | 76 tracker_.reset(new SigninTracker(profile_.get(), &observer_)); |
| 74 } | 77 } |
| 75 virtual void TearDown() OVERRIDE { | 78 virtual void TearDown() OVERRIDE { |
| 76 tracker_.reset(); | 79 tracker_.reset(); |
| 77 profile_.reset(); | 80 profile_.reset(); |
| 78 } | 81 } |
| 79 | 82 |
| 80 void SendSigninSuccessful() { | 83 void SendSigninSuccessful() { |
| 81 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); | 84 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); |
| 82 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); | 85 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); |
| 83 content::NotificationService::current()->Notify( | 86 content::NotificationService::current()->Notify( |
| 84 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, | 87 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| 85 content::Source<Profile>(profile_.get()), | 88 content::Source<Profile>(profile_.get()), |
| 86 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); | 89 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| 87 } | 90 } |
| 88 | 91 |
| 89 content::TestBrowserThreadBundle thread_bundle_; | 92 content::TestBrowserThreadBundle thread_bundle_; |
| 90 scoped_ptr<SigninTracker> tracker_; | 93 scoped_ptr<SigninTracker> tracker_; |
| 91 scoped_ptr<TestingProfile> profile_; | 94 scoped_ptr<TestingProfile> profile_; |
| 92 FakeSigninManagerBase* mock_signin_manager_; | 95 FakeSigninManagerBase* mock_signin_manager_; |
| 93 MockTokenService* mock_token_service_; | 96 MockOAuth2TokenService* mock_oauth2_token_service_; |
| 94 MockObserver observer_; | 97 MockObserver observer_; |
| 95 }; | 98 }; |
| 96 | 99 |
| 97 TEST_F(SigninTrackerTest, GaiaSignInFailed) { | 100 TEST_F(SigninTrackerTest, GaiaSignInFailed) { |
| 98 // SIGNIN_FAILED notification should result in a SigninFailed callback. | 101 // SIGNIN_FAILED notification should result in a SigninFailed callback. |
| 99 GoogleServiceAuthError error( | 102 GoogleServiceAuthError error( |
| 100 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 103 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 101 EXPECT_CALL(observer_, SigninFailed(error)); | 104 EXPECT_CALL(observer_, SigninFailed(error)); |
| 102 content::NotificationService::current()->Notify( | 105 content::NotificationService::current()->Notify( |
| 103 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, | 106 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| 104 content::Source<Profile>(profile_.get()), | 107 content::Source<Profile>(profile_.get()), |
| 105 content::Details<const GoogleServiceAuthError>(&error)); | 108 content::Details<const GoogleServiceAuthError>(&error)); |
| 106 } | 109 } |
| 107 | 110 |
| 108 TEST_F(SigninTrackerTest, GaiaSignInSucceeded) { | 111 TEST_F(SigninTrackerTest, GaiaSignInSucceeded) { |
| 109 // SIGNIN_SUCCEEDED notification should lead us to get a SigninSuccess() | 112 // SIGNIN_SUCCEEDED notification should lead us to get a SigninSuccess() |
| 110 // callback. | 113 // callback. |
| 111 EXPECT_CALL(observer_, SigninSuccess()); | 114 EXPECT_CALL(observer_, SigninSuccess()); |
| 112 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 115 EXPECT_CALL(*mock_oauth2_token_service_, GetRefreshToken()) |
| 113 .WillRepeatedly(Return(true)); | 116 .WillRepeatedly(Return("refresh_token")); |
| 114 SendSigninSuccessful(); | 117 SendSigninSuccessful(); |
| 115 } | 118 } |
| 116 | 119 |
| 117 TEST_F(SigninTrackerTest, NoGaiaSigninWhenOAuthTokensNotAvailable) { | 120 TEST_F(SigninTrackerTest, NoGaiaSigninWhenOAuthTokensNotAvailable) { |
| 118 // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess() | 121 // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess() |
| 119 // callback if our oauth token hasn't been fetched. | 122 // callback if our oauth token hasn't been fetched. |
| 120 EXPECT_CALL(observer_, SigninSuccess()).Times(0); | 123 EXPECT_CALL(observer_, SigninSuccess()).Times(0); |
| 121 EXPECT_CALL(observer_, SigninFailed(_)).Times(0); | 124 EXPECT_CALL(observer_, SigninFailed(_)).Times(0); |
| 122 EXPECT_CALL(*mock_token_service_, | 125 EXPECT_CALL(*mock_oauth2_token_service_, GetRefreshToken()) |
| 123 HasTokenForService(GaiaConstants::kSyncService)) | 126 .WillRepeatedly(Return("")); |
| 124 .WillRepeatedly(Return(true)); | |
| 125 EXPECT_CALL(*mock_token_service_, | |
| 126 HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken)) | |
| 127 .WillRepeatedly(Return(false)); | |
| 128 SendSigninSuccessful(); | 127 SendSigninSuccessful(); |
| 129 } | 128 } |
| 130 | 129 |
| 131 TEST_F(SigninTrackerTest, GaiaSigninAfterOAuthTokenBecomesAvailable) { | 130 TEST_F(SigninTrackerTest, GaiaSigninAfterOAuthTokenBecomesAvailable) { |
| 132 // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess() | 131 // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess() |
| 133 // callback until after our oauth token has been fetched. | 132 // callback until after our oauth token has been fetched. |
| 134 EXPECT_CALL(observer_, SigninSuccess()).Times(0); | 133 EXPECT_CALL(observer_, SigninSuccess()).Times(0); |
| 135 EXPECT_CALL(*mock_token_service_, | 134 EXPECT_CALL(*mock_oauth2_token_service_, GetRefreshToken()) |
| 136 HasTokenForService(GaiaConstants::kSyncService)) | 135 .WillRepeatedly(Return("")); |
| 137 .WillRepeatedly(Return(true)); | |
| 138 EXPECT_CALL(*mock_token_service_, | |
| 139 HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken)) | |
| 140 .WillRepeatedly(Return(false)); | |
| 141 SendSigninSuccessful(); | 136 SendSigninSuccessful(); |
| 142 Mock::VerifyAndClearExpectations(mock_token_service_); | 137 |
| 143 EXPECT_CALL(observer_, SigninSuccess()); | 138 EXPECT_CALL(observer_, SigninSuccess()); |
| 144 TokenService::TokenAvailableDetails available( | 139 EXPECT_CALL(*mock_oauth2_token_service_, GetRefreshToken()) |
| 145 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "foo_token"); | 140 .WillRepeatedly(Return("refresh_token")); |
| 146 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) | 141 tracker_->OnRefreshTokenAvailable("username@gmail.com"); |
|
Andrew T Wilson (Slow)
2013/08/19 15:23:06
If you change to use FakeProfileOAuth2TokenService
Roger Tawa OOO till Jul 10th
2013/08/29 19:28:41
Done.
| |
| 147 .WillRepeatedly(Return(true)); | |
| 148 content::NotificationService::current()->Notify( | |
| 149 chrome::NOTIFICATION_TOKEN_AVAILABLE, | |
| 150 content::Source<TokenService>(mock_token_service_), | |
| 151 content::Details<const TokenService::TokenAvailableDetails>(&available)); | |
| 152 } | 142 } |
| 153 | 143 |
| 154 TEST_F(SigninTrackerTest, SigninFailedWhenTokenFetchFails) { | 144 TEST_F(SigninTrackerTest, SigninFailedWhenTokenFetchFails) { |
| 155 // TOKEN_REQUEST_FAILED notification should result in SigninFailed() callback. | 145 // TOKEN_REQUEST_FAILED notification should result in SigninFailed() callback. |
| 156 // We should not get any SigninFailed() callbacks until we issue the | 146 // We should not get any SigninFailed() callbacks until we issue the |
| 157 // TOKEN_REQUEST_FAILED notification. | 147 // TOKEN_REQUEST_FAILED notification. |
| 158 EXPECT_CALL(observer_, SigninFailed(_)).Times(0); | 148 EXPECT_CALL(observer_, SigninFailed(_)).Times(0); |
| 159 EXPECT_CALL(*mock_token_service_, | 149 EXPECT_CALL(*mock_oauth2_token_service_, GetRefreshToken()) |
| 160 HasTokenForService(GaiaConstants::kSyncService)) | 150 .WillRepeatedly(Return("")); |
| 161 .WillRepeatedly(Return(true)); | |
| 162 EXPECT_CALL(*mock_token_service_, | |
| 163 HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken)) | |
| 164 .WillRepeatedly(Return(false)); | |
| 165 SendSigninSuccessful(); | 151 SendSigninSuccessful(); |
| 166 | 152 |
| 167 Mock::VerifyAndClearExpectations(&observer_); | 153 Mock::VerifyAndClearExpectations(&observer_); |
| 168 GoogleServiceAuthError error( | 154 GoogleServiceAuthError error( |
| 169 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 155 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 170 EXPECT_CALL(observer_, SigninFailed(error)); | 156 EXPECT_CALL(observer_, SigninFailed(error)); |
| 171 TokenService::TokenRequestFailedDetails failed( | 157 tracker_->OnRefreshTokenRevoked("username@gmail.com", error); |
|
Andrew T Wilson (Slow)
2013/08/19 15:23:06
Ditto for previous comment - you shouldn't have to
Roger Tawa OOO till Jul 10th
2013/08/29 19:28:41
Done.
| |
| 172 GaiaConstants::kGaiaOAuth2LoginRefreshToken, error); | |
| 173 content::NotificationService::current()->Notify( | |
| 174 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | |
| 175 content::Source<TokenService>(mock_token_service_), | |
| 176 content::Details<const TokenService::TokenRequestFailedDetails>(&failed)); | |
| 177 } | 158 } |
| OLD | NEW |