Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/browser/signin/signin_tracker_unittest.cc

Issue 19567004: Convert SigninTracker to use OAuth2TokenService notifications (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_profile_oauth2_token_service.h"
13 #include "chrome/browser/signin/fake_signin_manager.h" 14 #include "chrome/browser/signin/fake_signin_manager.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager.h" 17 #include "chrome/browser/signin/signin_manager.h"
15 #include "chrome/browser/signin/signin_manager_factory.h" 18 #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" 19 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/sync/profile_sync_service_mock.h" 20 #include "chrome/browser/sync/profile_sync_service_mock.h"
21 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
21 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "google_apis/gaia/gaia_constants.h" 24 #include "google_apis/gaia/gaia_constants.h"
23 #include "google_apis/gaia/google_service_auth_error.h" 25 #include "google_apis/gaia/google_service_auth_error.h"
24 26
25 #include "testing/gmock/include/gmock/gmock.h" 27 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
27 29
28 using ::testing::_; 30 using ::testing::_;
29 using ::testing::AnyNumber; 31 using ::testing::AnyNumber;
30 using ::testing::Mock; 32 using ::testing::Mock;
31 using ::testing::Return; 33 using ::testing::Return;
32 using ::testing::ReturnRef; 34 using ::testing::ReturnRef;
33 35
34 namespace { 36 namespace {
35 37
36 class MockTokenService : public TokenService {
37 public:
38 MockTokenService() { }
39 virtual ~MockTokenService() { }
40
41 MOCK_CONST_METHOD1(HasTokenForService, bool(const char*));
42 };
43
44 BrowserContextKeyedService* BuildMockTokenService(
45 content::BrowserContext* profile) {
46 return new MockTokenService;
47 }
48
49 class MockObserver : public SigninTracker::Observer { 38 class MockObserver : public SigninTracker::Observer {
50 public: 39 public:
51 MockObserver() {} 40 MockObserver() {}
52 ~MockObserver() {} 41 ~MockObserver() {}
53 42
54 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&)); 43 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&));
55 MOCK_METHOD0(SigninSuccess, void(void)); 44 MOCK_METHOD0(SigninSuccess, void(void));
56 }; 45 };
57 46
58 } // namespace 47 } // namespace
59 48
60 class SigninTrackerTest : public testing::Test { 49 class SigninTrackerTest : public testing::Test {
61 public: 50 public:
62 SigninTrackerTest() {} 51 SigninTrackerTest() {}
63 virtual void SetUp() OVERRIDE { 52 virtual void SetUp() OVERRIDE {
64 profile_.reset(new TestingProfile()); 53 TestingProfile::Builder builder;
65 mock_token_service_ = static_cast<MockTokenService*>( 54 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
66 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( 55 FakeProfileOAuth2TokenService::Build);
67 profile_.get(), BuildMockTokenService)); 56
57 profile_ = builder.Build();
58
59 fake_oauth2_token_service_ =
60 static_cast<FakeProfileOAuth2TokenService*>(
61 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()));
68 62
69 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>( 63 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>(
70 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( 64 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
71 profile_.get(), FakeSigninManagerBase::Build)); 65 profile_.get(), FakeSigninManagerBase::Build));
72 mock_signin_manager_->Initialize(profile_.get(), NULL); 66 mock_signin_manager_->Initialize(profile_.get(), NULL);
67
73 tracker_.reset(new SigninTracker(profile_.get(), &observer_)); 68 tracker_.reset(new SigninTracker(profile_.get(), &observer_));
74 } 69 }
75 virtual void TearDown() OVERRIDE { 70 virtual void TearDown() OVERRIDE {
76 tracker_.reset(); 71 tracker_.reset();
77 profile_.reset(); 72 profile_.reset();
78 } 73 }
79 74
80 void SendSigninSuccessful() {
81 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com");
82 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password");
83 content::NotificationService::current()->Notify(
84 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
85 content::Source<Profile>(profile_.get()),
86 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
87 }
88
89 content::TestBrowserThreadBundle thread_bundle_; 75 content::TestBrowserThreadBundle thread_bundle_;
90 scoped_ptr<SigninTracker> tracker_; 76 scoped_ptr<SigninTracker> tracker_;
91 scoped_ptr<TestingProfile> profile_; 77 scoped_ptr<TestingProfile> profile_;
92 FakeSigninManagerBase* mock_signin_manager_; 78 FakeSigninManagerBase* mock_signin_manager_;
93 MockTokenService* mock_token_service_; 79 FakeProfileOAuth2TokenService* fake_oauth2_token_service_;
94 MockObserver observer_; 80 MockObserver observer_;
95 }; 81 };
96 82
97 TEST_F(SigninTrackerTest, GaiaSignInFailed) { 83 TEST_F(SigninTrackerTest, SignInFails) {
98 // SIGNIN_FAILED notification should result in a SigninFailed callback. 84 // SIGNIN_FAILED notification should result in a SigninFailed callback.
99 GoogleServiceAuthError error( 85 const GoogleServiceAuthError error(
100 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 86 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
87 EXPECT_CALL(observer_, SigninSuccess()).Times(0);
101 EXPECT_CALL(observer_, SigninFailed(error)); 88 EXPECT_CALL(observer_, SigninFailed(error));
89
102 content::NotificationService::current()->Notify( 90 content::NotificationService::current()->Notify(
103 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, 91 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED,
104 content::Source<Profile>(profile_.get()), 92 content::Source<Profile>(profile_.get()),
105 content::Details<const GoogleServiceAuthError>(&error)); 93 content::Details<const GoogleServiceAuthError>(&error));
106 } 94 }
107 95
108 TEST_F(SigninTrackerTest, GaiaSignInSucceeded) { 96 TEST_F(SigninTrackerTest, SignInSucceeds) {
109 // SIGNIN_SUCCEEDED notification should lead us to get a SigninSuccess()
110 // callback.
111 EXPECT_CALL(observer_, SigninSuccess()); 97 EXPECT_CALL(observer_, SigninSuccess());
112 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) 98 EXPECT_CALL(observer_, SigninFailed(_)).Times(0);
113 .WillRepeatedly(Return(true)); 99
114 SendSigninSuccessful(); 100 mock_signin_manager_->SetAuthenticatedUsername("user@gmail.com");
101 fake_oauth2_token_service_->IssueRefreshToken("refresh_token");
115 } 102 }
116
117 TEST_F(SigninTrackerTest, NoGaiaSigninWhenOAuthTokensNotAvailable) {
118 // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess()
119 // callback if our oauth token hasn't been fetched.
120 EXPECT_CALL(observer_, SigninSuccess()).Times(0);
121 EXPECT_CALL(observer_, SigninFailed(_)).Times(0);
122 EXPECT_CALL(*mock_token_service_,
123 HasTokenForService(GaiaConstants::kSyncService))
124 .WillRepeatedly(Return(true));
125 EXPECT_CALL(*mock_token_service_,
126 HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken))
127 .WillRepeatedly(Return(false));
128 SendSigninSuccessful();
129 }
130
131 TEST_F(SigninTrackerTest, GaiaSigninAfterOAuthTokenBecomesAvailable) {
132 // SIGNIN_SUCCESSFUL notification should not result in a SigninSuccess()
133 // callback until after our oauth token has been fetched.
134 EXPECT_CALL(observer_, SigninSuccess()).Times(0);
135 EXPECT_CALL(*mock_token_service_,
136 HasTokenForService(GaiaConstants::kSyncService))
137 .WillRepeatedly(Return(true));
138 EXPECT_CALL(*mock_token_service_,
139 HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken))
140 .WillRepeatedly(Return(false));
141 SendSigninSuccessful();
142 Mock::VerifyAndClearExpectations(mock_token_service_);
143 EXPECT_CALL(observer_, SigninSuccess());
144 TokenService::TokenAvailableDetails available(
145 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "foo_token");
146 EXPECT_CALL(*mock_token_service_, HasTokenForService(_))
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 }
153
154 TEST_F(SigninTrackerTest, SigninFailedWhenTokenFetchFails) {
155 // TOKEN_REQUEST_FAILED notification should result in SigninFailed() callback.
156 // We should not get any SigninFailed() callbacks until we issue the
157 // TOKEN_REQUEST_FAILED notification.
158 EXPECT_CALL(observer_, SigninFailed(_)).Times(0);
159 EXPECT_CALL(*mock_token_service_,
160 HasTokenForService(GaiaConstants::kSyncService))
161 .WillRepeatedly(Return(true));
162 EXPECT_CALL(*mock_token_service_,
163 HasTokenForService(GaiaConstants::kGaiaOAuth2LoginRefreshToken))
164 .WillRepeatedly(Return(false));
165 SendSigninSuccessful();
166
167 Mock::VerifyAndClearExpectations(&observer_);
168 GoogleServiceAuthError error(
169 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
170 EXPECT_CALL(observer_, SigninFailed(error));
171 TokenService::TokenRequestFailedDetails failed(
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 }
OLDNEW
« no previous file with comments | « chrome/browser/signin/signin_tracker.cc ('k') | chrome/browser/ui/sync/one_click_signin_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698