Chromium Code Reviews| Index: chrome/browser/signin/signin_tracker_unittest.cc |
| diff --git a/chrome/browser/signin/signin_tracker_unittest.cc b/chrome/browser/signin/signin_tracker_unittest.cc |
| index ac2d366ebb4d258f28c09441b6afc3c23bef7f3b..84735bba52ac068c84ca27e9585831758c95a586 100644 |
| --- a/chrome/browser/signin/signin_tracker_unittest.cc |
| +++ b/chrome/browser/signin/signin_tracker_unittest.cc |
| @@ -10,13 +10,15 @@ |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/signin/fake_auth_status_provider.h" |
| +#include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| #include "chrome/browser/signin/fake_signin_manager.h" |
| +#include "chrome/browser/signin/profile_oauth2_token_service.h" |
| +#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| #include "chrome/browser/signin/signin_manager.h" |
| #include "chrome/browser/signin/signin_manager_factory.h" |
| -#include "chrome/browser/signin/token_service.h" |
| -#include "chrome/browser/signin/token_service_factory.h" |
| #include "chrome/browser/sync/profile_sync_service_factory.h" |
| #include "chrome/browser/sync/profile_sync_service_mock.h" |
| +#include "chrome/test/base/testing_profile.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| #include "google_apis/gaia/gaia_constants.h" |
| @@ -33,19 +35,6 @@ using ::testing::ReturnRef; |
| namespace { |
| -class MockTokenService : public TokenService { |
| - public: |
| - MockTokenService() { } |
| - virtual ~MockTokenService() { } |
| - |
| - MOCK_CONST_METHOD1(HasTokenForService, bool(const char*)); |
| -}; |
| - |
| -BrowserContextKeyedService* BuildMockTokenService( |
| - content::BrowserContext* profile) { |
| - return new MockTokenService; |
| -} |
| - |
| class MockObserver : public SigninTracker::Observer { |
| public: |
| MockObserver() {} |
| @@ -61,15 +50,21 @@ class SigninTrackerTest : public testing::Test { |
| public: |
| SigninTrackerTest() {} |
| virtual void SetUp() OVERRIDE { |
| - profile_.reset(new TestingProfile()); |
| - mock_token_service_ = static_cast<MockTokenService*>( |
| - TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| - profile_.get(), BuildMockTokenService)); |
| + TestingProfile::Builder builder; |
| + builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| + FakeProfileOAuth2TokenService::Build); |
| + |
| + profile_ = builder.Build(); |
| + |
| + fake_oauth2_token_service_ = |
| + static_cast<FakeProfileOAuth2TokenService*>( |
| + ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get())); |
| mock_signin_manager_ = static_cast<FakeSigninManagerBase*>( |
| SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( |
| profile_.get(), FakeSigninManagerBase::Build)); |
| mock_signin_manager_->Initialize(profile_.get(), NULL); |
| + |
| tracker_.reset(new SigninTracker(profile_.get(), &observer_)); |
| } |
| virtual void TearDown() OVERRIDE { |
| @@ -77,101 +72,40 @@ class SigninTrackerTest : public testing::Test { |
| profile_.reset(); |
| } |
| - void SendSigninSuccessful() { |
| - mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); |
| - GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); |
| - content::NotificationService::current()->Notify( |
| - chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, |
| - content::Source<Profile>(profile_.get()), |
| - content::Details<const GoogleServiceSigninSuccessDetails>(&details)); |
| - } |
| - |
| content::TestBrowserThreadBundle thread_bundle_; |
| scoped_ptr<SigninTracker> tracker_; |
| scoped_ptr<TestingProfile> profile_; |
| FakeSigninManagerBase* mock_signin_manager_; |
| - MockTokenService* mock_token_service_; |
| + FakeProfileOAuth2TokenService* fake_oauth2_token_service_; |
| MockObserver observer_; |
| }; |
| -TEST_F(SigninTrackerTest, GaiaSignInFailed) { |
| +TEST_F(SigninTrackerTest, SignInFails) { |
| // SIGNIN_FAILED notification should result in a SigninFailed callback. |
| - GoogleServiceAuthError error( |
| + const GoogleServiceAuthError error( |
| GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| + EXPECT_CALL(observer_, SigninSuccess()).Times(0); |
|
Andrew T Wilson (Slow)
2013/09/09 12:08:27
Heh, good addition - we assumed that you wouldn't
Roger Tawa OOO till Jul 10th
2013/09/10 14:29:23
Done.
|
| EXPECT_CALL(observer_, SigninFailed(error)); |
| + |
| content::NotificationService::current()->Notify( |
| chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, |
| content::Source<Profile>(profile_.get()), |
| content::Details<const GoogleServiceAuthError>(&error)); |
| } |
| -TEST_F(SigninTrackerTest, GaiaSignInSucceeded) { |
| - // SIGNIN_SUCCEEDED notification should lead us to get a SigninSuccess() |
| - // callback. |
| - EXPECT_CALL(observer_, SigninSuccess()); |
| - EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| - .WillRepeatedly(Return(true)); |
| - SendSigninSuccessful(); |
| -} |
| - |
| -TEST_F(SigninTrackerTest, NoGaiaSigninWhenOAuthTokensNotAvailable) { |
| - // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess() |
| - // callback if our oauth token hasn't been fetched. |
| +TEST_F(SigninTrackerTest, SigninFailsWhenTokenFetchFails) { |
| + // TOKEN_REQUEST_FAILED notification should result in SigninFailed() callback. |
| + const GoogleServiceAuthError error(GoogleServiceAuthError::REQUEST_CANCELED); |
| EXPECT_CALL(observer_, SigninSuccess()).Times(0); |
| - EXPECT_CALL(observer_, SigninFailed(_)).Times(0); |
| - EXPECT_CALL(*mock_token_service_, |
| - HasTokenForService(GaiaConstants::kSyncService)) |
| - .WillRepeatedly(Return(true)); |
| - EXPECT_CALL(*mock_token_service_, |
| - HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken)) |
| - .WillRepeatedly(Return(false)); |
| - SendSigninSuccessful(); |
| -} |
| + EXPECT_CALL(observer_, SigninFailed(error)); |
| -TEST_F(SigninTrackerTest, GaiaSigninAfterOAuthTokenBecomesAvailable) { |
| - // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess() |
| - // callback until after our oauth token has been fetched. |
| - EXPECT_CALL(observer_, SigninSuccess()).Times(0); |
| - EXPECT_CALL(*mock_token_service_, |
| - HasTokenForService(GaiaConstants::kSyncService)) |
| - .WillRepeatedly(Return(true)); |
| - EXPECT_CALL(*mock_token_service_, |
| - HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken)) |
| - .WillRepeatedly(Return(false)); |
| - SendSigninSuccessful(); |
| - Mock::VerifyAndClearExpectations(mock_token_service_); |
| - EXPECT_CALL(observer_, SigninSuccess()); |
| - TokenService::TokenAvailableDetails available( |
| - GaiaConstants::kGaiaOAuth2LoginRefreshToken, "foo_token"); |
| - EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) |
| - .WillRepeatedly(Return(true)); |
| - content::NotificationService::current()->Notify( |
| - chrome::NOTIFICATION_TOKEN_AVAILABLE, |
| - content::Source<TokenService>(mock_token_service_), |
| - content::Details<const TokenService::TokenAvailableDetails>(&available)); |
| + fake_oauth2_token_service_->IssueRefreshToken(""); |
| } |
| -TEST_F(SigninTrackerTest, SigninFailedWhenTokenFetchFails) { |
| - // TOKEN_REQUEST_FAILED notification should result in SigninFailed() callback. |
| - // We should not get any SigninFailed() callbacks until we issue the |
| - // TOKEN_REQUEST_FAILED notification. |
| +TEST_F(SigninTrackerTest, SignInSucceeds) { |
| + EXPECT_CALL(observer_, SigninSuccess()); |
| EXPECT_CALL(observer_, SigninFailed(_)).Times(0); |
| - EXPECT_CALL(*mock_token_service_, |
| - HasTokenForService(GaiaConstants::kSyncService)) |
| - .WillRepeatedly(Return(true)); |
| - EXPECT_CALL(*mock_token_service_, |
| - HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken)) |
| - .WillRepeatedly(Return(false)); |
| - SendSigninSuccessful(); |
| - |
| - Mock::VerifyAndClearExpectations(&observer_); |
| - GoogleServiceAuthError error( |
| - GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| - EXPECT_CALL(observer_, SigninFailed(error)); |
| - TokenService::TokenRequestFailedDetails failed( |
| - GaiaConstants::kGaiaOAuth2LoginRefreshToken, error); |
| - content::NotificationService::current()->Notify( |
| - chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
| - content::Source<TokenService>(mock_token_service_), |
| - content::Details<const TokenService::TokenRequestFailedDetails>(&failed)); |
| + |
| + mock_signin_manager_->SetAuthenticatedUsername("user@gmail.com"); |
| + fake_oauth2_token_service_->IssueRefreshToken("refresh_token"); |
| } |