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

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

Issue 15421011: Use OAuth2 token for sync (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add commandline switch to toggle between OAuth2 and ClientLogin token Created 7 years, 6 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
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/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/fake_auth_status_provider.h"
12 #include "chrome/browser/signin/fake_signin_manager.h" 12 #include "chrome/browser/signin/fake_signin_manager.h"
13 #include "chrome/browser/signin/signin_manager.h" 13 #include "chrome/browser/signin/signin_manager.h"
14 #include "chrome/browser/signin/signin_manager_factory.h" 14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/signin/token_service.h" 15 #include "chrome/browser/signin/token_service.h"
16 #include "chrome/browser/signin/token_service_factory.h" 16 #include "chrome/browser/signin/token_service_factory.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h" 17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/sync/profile_sync_service_mock.h" 18 #include "chrome/browser/sync/profile_sync_service_mock.h"
19 #include "chrome/common/chrome_notification_types.h" 19 #include "chrome/common/chrome_notification_types.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.h"
21 #include "google_apis/gaia/gaia_constants.h" 22 #include "google_apis/gaia/gaia_constants.h"
22 #include "google_apis/gaia/google_service_auth_error.h" 23 #include "google_apis/gaia/google_service_auth_error.h"
23 24
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 27
27 using ::testing::_; 28 using ::testing::_;
28 using ::testing::AnyNumber; 29 using ::testing::AnyNumber;
29 using ::testing::Mock; 30 using ::testing::Mock;
30 using ::testing::Return; 31 using ::testing::Return;
(...skipping 21 matching lines...) Expand all
52 53
53 MOCK_METHOD0(GaiaCredentialsValid, void(void)); 54 MOCK_METHOD0(GaiaCredentialsValid, void(void));
54 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&)); 55 MOCK_METHOD1(SigninFailed, void(const GoogleServiceAuthError&));
55 MOCK_METHOD0(SigninSuccess, void(void)); 56 MOCK_METHOD0(SigninSuccess, void(void));
56 }; 57 };
57 58
58 } // namespace 59 } // namespace
59 60
60 class SigninTrackerTest : public testing::Test { 61 class SigninTrackerTest : public testing::Test {
61 public: 62 public:
62 SigninTrackerTest() {} 63 SigninTrackerTest()
64 : ui_thread_(content::BrowserThread::UI, &ui_loop_) {}
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_token_service_ = static_cast<MockTokenService*>(
66 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( 68 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse(
67 profile_.get(), BuildMockTokenService)); 69 profile_.get(), BuildMockTokenService));
70 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>(
71 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
72 profile_.get(), FakeSigninManagerBase::Build));
68 mock_pss_ = static_cast<ProfileSyncServiceMock*>( 73 mock_pss_ = static_cast<ProfileSyncServiceMock*>(
69 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 74 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
70 profile_.get(), 75 profile_.get(),
71 ProfileSyncServiceMock::BuildMockProfileSyncService)); 76 ProfileSyncServiceMock::BuildMockProfileSyncService));
72 mock_pss_->Initialize(); 77 mock_pss_->Initialize();
73 78
74 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>( 79 mock_signin_manager_ = static_cast<FakeSigninManagerBase*>(
75 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse( 80 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
76 profile_.get(), FakeSigninManagerBase::Build)); 81 profile_.get(), FakeSigninManagerBase::Build));
77 mock_signin_manager_->Initialize(profile_.get(), NULL); 82 mock_signin_manager_->Initialize(profile_.get(), NULL);
78 // Make gmock not spam the output with information about these uninteresting 83 // Make gmock not spam the output with information about these uninteresting
79 // calls. 84 // calls.
80 EXPECT_CALL(*mock_pss_, AddObserver(_)).Times(AnyNumber()); 85 EXPECT_CALL(*mock_pss_, AddObserver(_)).Times(AnyNumber());
81 EXPECT_CALL(*mock_pss_, RemoveObserver(_)).Times(AnyNumber()); 86 EXPECT_CALL(*mock_pss_, RemoveObserver(_)).Times(AnyNumber());
82 tracker_.reset(new SigninTracker(profile_.get(), &observer_)); 87 tracker_.reset(new SigninTracker(profile_.get(), &observer_));
83 } 88 }
84 virtual void TearDown() OVERRIDE { 89 virtual void TearDown() OVERRIDE {
85 tracker_.reset(); 90 tracker_.reset();
86 profile_.reset(); 91 profile_.reset();
87 } 92 }
88 scoped_ptr<SigninTracker> tracker_; 93 scoped_ptr<SigninTracker> tracker_;
89 scoped_ptr<TestingProfile> profile_; 94 scoped_ptr<TestingProfile> profile_;
90 ProfileSyncServiceMock* mock_pss_; 95 ProfileSyncServiceMock* mock_pss_;
91 FakeSigninManagerBase* mock_signin_manager_; 96 FakeSigninManagerBase* mock_signin_manager_;
92 MockTokenService* mock_token_service_; 97 MockTokenService* mock_token_service_;
93 MockObserver observer_; 98 MockObserver observer_;
99 base::MessageLoop ui_loop_;
100 content::TestBrowserThread ui_thread_;
94 }; 101 };
95 102
96 TEST_F(SigninTrackerTest, GaiaSignInFailed) { 103 TEST_F(SigninTrackerTest, GaiaSignInFailed) {
97 // SIGNIN_FAILED notification should result in a SigninFailed callback. 104 // SIGNIN_FAILED notification should result in a SigninFailed callback.
98 GoogleServiceAuthError error( 105 GoogleServiceAuthError error(
99 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); 106 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
100 EXPECT_CALL(observer_, SigninFailed(error)); 107 EXPECT_CALL(observer_, SigninFailed(error));
101 content::NotificationService::current()->Notify( 108 content::NotificationService::current()->Notify(
102 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED, 109 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED,
103 content::Source<Profile>(profile_.get()), 110 content::Source<Profile>(profile_.get()),
(...skipping 17 matching lines...) Expand all
121 128
122 static void ExpectSignedInSyncService(ProfileSyncServiceMock* sync_service, 129 static void ExpectSignedInSyncService(ProfileSyncServiceMock* sync_service,
123 MockTokenService* token_service, 130 MockTokenService* token_service,
124 const GoogleServiceAuthError& error) { 131 const GoogleServiceAuthError& error) {
125 if (token_service) { 132 if (token_service) {
126 EXPECT_CALL(*token_service, HasTokenForService(_)) 133 EXPECT_CALL(*token_service, HasTokenForService(_))
127 .WillRepeatedly(Return(true)); 134 .WillRepeatedly(Return(true));
128 } 135 }
129 EXPECT_CALL(*sync_service, IsSyncEnabledAndLoggedIn()).WillRepeatedly( 136 EXPECT_CALL(*sync_service, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
130 Return(true)); 137 Return(true));
131 EXPECT_CALL(*sync_service, IsSyncTokenAvailable()).WillRepeatedly( 138 EXPECT_CALL(*sync_service, IsOAuthRefreshTokenAvailable()).WillRepeatedly(
132 Return(true)); 139 Return(true));
133 EXPECT_CALL(*sync_service, waiting_for_auth()).WillRepeatedly(Return(false)); 140 EXPECT_CALL(*sync_service, waiting_for_auth()).WillRepeatedly(Return(false));
134 EXPECT_CALL(*sync_service, GetAuthError()).WillRepeatedly(ReturnRef(error)); 141 EXPECT_CALL(*sync_service, GetAuthError()).WillRepeatedly(ReturnRef(error));
135 EXPECT_CALL(*sync_service, HasUnrecoverableError()).WillRepeatedly( 142 EXPECT_CALL(*sync_service, HasUnrecoverableError()).WillRepeatedly(
136 Return(false)); 143 Return(false));
137 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true)); 144 EXPECT_CALL(*sync_service, sync_initialized()).WillRepeatedly(Return(true));
138 145
139 } 146 }
140 147
141 TEST_F(SigninTrackerTest, GaiaSigninWhenServicesAlreadyRunning) { 148 TEST_F(SigninTrackerTest, GaiaSigninWhenServicesAlreadyRunning) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 } 238 }
232 239
233 TEST_F(SigninTrackerTest, NoGaiaSigninWhenServicesNotRunning) { 240 TEST_F(SigninTrackerTest, NoGaiaSigninWhenServicesNotRunning) {
234 // SIGNIN_SUCCEEDED notification should not result in a SigninSuccess() 241 // SIGNIN_SUCCEEDED notification should not result in a SigninSuccess()
235 // callback if we're not already signed in. 242 // callback if we're not already signed in.
236 EXPECT_CALL(observer_, GaiaCredentialsValid()); 243 EXPECT_CALL(observer_, GaiaCredentialsValid());
237 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) 244 EXPECT_CALL(*mock_token_service_, HasTokenForService(_))
238 .WillRepeatedly(Return(true)); 245 .WillRepeatedly(Return(true));
239 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( 246 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
240 Return(false)); 247 Return(false));
241 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( 248 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()).WillRepeatedly(
242 Return(false)); 249 Return(false));
243 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); 250 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password");
244 content::NotificationService::current()->Notify( 251 content::NotificationService::current()->Notify(
245 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 252 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
246 content::Source<Profile>(profile_.get()), 253 content::Source<Profile>(profile_.get()),
247 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 254 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
248 } 255 }
249 256
250 TEST_F(SigninTrackerTest, GaiaSigninAfterSyncStarts) { 257 TEST_F(SigninTrackerTest, GaiaSigninAfterSyncStarts) {
251 // Make sure that we don't get a SigninSuccess() callback until after the 258 // Make sure that we don't get a SigninSuccess() callback until after the
(...skipping 16 matching lines...) Expand all
268 ExpectSignedInSyncService(mock_pss_, mock_token_service_, error); 275 ExpectSignedInSyncService(mock_pss_, mock_token_service_, error);
269 tracker_->OnStateChanged(); 276 tracker_->OnStateChanged();
270 } 277 }
271 278
272 TEST_F(SigninTrackerTest, SyncSigninError) { 279 TEST_F(SigninTrackerTest, SyncSigninError) {
273 // Make sure that we get a SigninFailed() callback if sync gets an error after 280 // Make sure that we get a SigninFailed() callback if sync gets an error after
274 // initializaiton. 281 // initializaiton.
275 EXPECT_CALL(observer_, GaiaCredentialsValid()); 282 EXPECT_CALL(observer_, GaiaCredentialsValid());
276 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( 283 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
277 Return(false)); 284 Return(false));
278 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( 285 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()).WillRepeatedly(
279 Return(false)); 286 Return(false));
280 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) 287 EXPECT_CALL(*mock_token_service_, HasTokenForService(_))
281 .WillRepeatedly(Return(true)); 288 .WillRepeatedly(Return(true));
282 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); 289 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com");
283 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password"); 290 GoogleServiceSigninSuccessDetails details("username@gmail.com", "password");
284 content::NotificationService::current()->Notify( 291 content::NotificationService::current()->Notify(
285 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 292 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
286 content::Source<Profile>(profile_.get()), 293 content::Source<Profile>(profile_.get()),
287 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 294 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
288 295
(...skipping 28 matching lines...) Expand all
317 324
318 TEST_F(SigninTrackerTest, SigninFailedSyncTokenUnavailable) { 325 TEST_F(SigninTrackerTest, SigninFailedSyncTokenUnavailable) {
319 tracker_.reset(); 326 tracker_.reset();
320 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) 327 EXPECT_CALL(*mock_token_service_, HasTokenForService(_))
321 .WillRepeatedly(Return(true)); 328 .WillRepeatedly(Return(true));
322 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); 329 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com");
323 GoogleServiceAuthError error(GoogleServiceAuthError::NONE); 330 GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
324 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( 331 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
325 Return(true)); 332 Return(true));
326 // Inject Token unavailable error. 333 // Inject Token unavailable error.
327 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( 334 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()).WillRepeatedly(
328 Return(false)); 335 Return(false));
329 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false)); 336 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false));
330 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); 337 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
331 EXPECT_CALL(*mock_pss_, HasUnrecoverableError()).WillRepeatedly( 338 EXPECT_CALL(*mock_pss_, HasUnrecoverableError()).WillRepeatedly(
332 Return(false)); 339 Return(false));
333 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true)); 340 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
334 // Any error should result in SigninFailed() being called. 341 // Any error should result in SigninFailed() being called.
335 EXPECT_CALL(observer_, SigninFailed(error)); 342 EXPECT_CALL(observer_, SigninFailed(error));
336 tracker_.reset(new SigninTracker(profile_.get(), &observer_, 343 tracker_.reset(new SigninTracker(profile_.get(), &observer_,
337 SigninTracker::SERVICES_INITIALIZING)); 344 SigninTracker::SERVICES_INITIALIZING));
338 } 345 }
339 346
340 TEST_F(SigninTrackerTest, SigninFailedGoogleServiceAuthError) { 347 TEST_F(SigninTrackerTest, SigninFailedGoogleServiceAuthError) {
341 tracker_.reset(); 348 tracker_.reset();
342 EXPECT_CALL(*mock_token_service_, HasTokenForService(_)) 349 EXPECT_CALL(*mock_token_service_, HasTokenForService(_))
343 .WillRepeatedly(Return(true)); 350 .WillRepeatedly(Return(true));
344 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); 351 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com");
345 // Inject authentication error. 352 // Inject authentication error.
346 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE); 353 GoogleServiceAuthError error(GoogleServiceAuthError::SERVICE_UNAVAILABLE);
347 FakeAuthStatusProvider provider(mock_signin_manager_->signin_global_error()); 354 FakeAuthStatusProvider provider(mock_signin_manager_->signin_global_error());
348 provider.SetAuthError(error); 355 provider.SetAuthError(error);
349 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly( 356 EXPECT_CALL(*mock_pss_, IsSyncEnabledAndLoggedIn()).WillRepeatedly(
350 Return(true)); 357 Return(true));
351 EXPECT_CALL(*mock_pss_, IsSyncTokenAvailable()).WillRepeatedly( 358 EXPECT_CALL(*mock_pss_, IsOAuthRefreshTokenAvailable()).WillRepeatedly(
352 Return(true)); 359 Return(true));
353 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false)); 360 EXPECT_CALL(*mock_pss_, waiting_for_auth()).WillRepeatedly(Return(false));
354 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error)); 361 EXPECT_CALL(*mock_pss_, GetAuthError()).WillRepeatedly(ReturnRef(error));
355 EXPECT_CALL(*mock_pss_, HasUnrecoverableError()).WillRepeatedly( 362 EXPECT_CALL(*mock_pss_, HasUnrecoverableError()).WillRepeatedly(
356 Return(false)); 363 Return(false));
357 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true)); 364 EXPECT_CALL(*mock_pss_, sync_initialized()).WillRepeatedly(Return(true));
358 // Any error should result in SigninFailed() being called. 365 // Any error should result in SigninFailed() being called.
359 EXPECT_CALL(observer_, SigninFailed(error)); 366 EXPECT_CALL(observer_, SigninFailed(error));
360 tracker_.reset(new SigninTracker(profile_.get(), &observer_, 367 tracker_.reset(new SigninTracker(profile_.get(), &observer_,
361 SigninTracker::SERVICES_INITIALIZING)); 368 SigninTracker::SERVICES_INITIALIZING));
(...skipping 12 matching lines...) Expand all
374 TEST_F(SigninTrackerTest, ConnectionErrorWhenInitializing) { 381 TEST_F(SigninTrackerTest, ConnectionErrorWhenInitializing) {
375 // SigninFailed() should not be called for a CONNECTION_FAILED error. 382 // SigninFailed() should not be called for a CONNECTION_FAILED error.
376 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED); 383 GoogleServiceAuthError error(GoogleServiceAuthError::CONNECTION_FAILED);
377 ExpectSignedInSyncService(mock_pss_, mock_token_service_, error); 384 ExpectSignedInSyncService(mock_pss_, mock_token_service_, error);
378 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com"); 385 mock_signin_manager_->SetAuthenticatedUsername("username@gmail.com");
379 EXPECT_CALL(observer_, SigninFailed(_)).Times(0); 386 EXPECT_CALL(observer_, SigninFailed(_)).Times(0);
380 tracker_.reset(new SigninTracker(profile_.get(), &observer_, 387 tracker_.reset(new SigninTracker(profile_.get(), &observer_,
381 SigninTracker::SERVICES_INITIALIZING)); 388 SigninTracker::SERVICES_INITIALIZING));
382 tracker_->OnStateChanged(); 389 tracker_->OnStateChanged();
383 } 390 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698