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
3 // found in the LICENSE file.
5 #include "components/signin/core/browser/signin_tracker.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/signin/account_tracker_service_factory.h"
13 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
14 #include "chrome/browser/signin/fake_signin_manager_builder.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "chrome/browser/signin/signin_tracker_factory.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/sync/profile_sync_service_mock.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "components/signin/core/browser/account_tracker_service.h"
22 #include "components/signin/core/browser/fake_auth_status_provider.h"
23 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
24 #include "components/signin/core/browser/profile_oauth2_token_service.h"
25 #include "components/signin/core/browser/signin_manager.h"
26 #include "content/public/test/test_browser_thread_bundle.h"
27 #include "google_apis/gaia/gaia_constants.h"
28 #include "google_apis/gaia/google_service_auth_error.h"
30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h"
34 using ::testing::AnyNumber
;
35 using ::testing::Mock
;
36 using ::testing::Return
;
37 using ::testing::ReturnRef
;
41 class MockObserver
: public SigninTracker::Observer
{
46 MOCK_METHOD1(SigninFailed
, void(const GoogleServiceAuthError
&));
47 MOCK_METHOD0(SigninSuccess
, void(void));
48 MOCK_METHOD1(AccountAddedToCookie
, void(const GoogleServiceAuthError
&));
53 class SigninTrackerTest
: public testing::Test
{
55 SigninTrackerTest() {}
56 void SetUp() override
{
57 TestingProfile::Builder builder
;
58 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
59 BuildFakeProfileOAuth2TokenService
);
60 builder
.AddTestingFactory(SigninManagerFactory::GetInstance(),
61 BuildFakeSigninManagerBase
);
62 profile_
= builder
.Build();
64 fake_oauth2_token_service_
=
65 static_cast<FakeProfileOAuth2TokenService
*>(
66 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get()));
68 mock_signin_manager_
= static_cast<FakeSigninManagerForTesting
*>(
69 SigninManagerFactory::GetForProfile(profile_
.get()));
72 SigninTrackerFactory::CreateForProfile(profile_
.get(), &observer_
);
75 void TearDown() override
{
80 // Seed the account tracker with information from logged in user. Normally
81 // this is done by UI code before calling SigninManager. Returns the string
82 // to use as the account_id.
83 std::string
AddToAccountTracker(const std::string
& gaia_id
,
84 const std::string
& email
) {
85 AccountTrackerService
* service
=
86 AccountTrackerServiceFactory::GetForProfile(profile_
.get());
87 return service
->SeedAccountInfo(gaia_id
, email
);
90 content::TestBrowserThreadBundle thread_bundle_
;
91 scoped_ptr
<SigninTracker
> tracker_
;
92 scoped_ptr
<TestingProfile
> profile_
;
93 FakeSigninManagerForTesting
* mock_signin_manager_
;
94 FakeProfileOAuth2TokenService
* fake_oauth2_token_service_
;
95 MockObserver observer_
;
98 #if !defined(OS_CHROMEOS)
99 TEST_F(SigninTrackerTest
, SignInFails
) {
100 const GoogleServiceAuthError
error(
101 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
103 // Signin failure should result in a SigninFailed callback.
104 EXPECT_CALL(observer_
, SigninSuccess()).Times(0);
105 EXPECT_CALL(observer_
, SigninFailed(error
));
107 mock_signin_manager_
->FailSignin(error
);
109 #endif // !defined(OS_CHROMEOS)
111 TEST_F(SigninTrackerTest
, SignInSucceeds
) {
112 EXPECT_CALL(observer_
, SigninSuccess());
113 EXPECT_CALL(observer_
, SigninFailed(_
)).Times(0);
115 AccountTrackerService
* service
=
116 AccountTrackerServiceFactory::GetForProfile(profile_
.get());
117 std::string gaia_id
= "gaia_id";
118 std::string email
= "user@gmail.com";
119 std::string account_id
= service
->SeedAccountInfo(gaia_id
, email
);
121 mock_signin_manager_
->SetAuthenticatedAccountInfo(gaia_id
, email
);
122 fake_oauth2_token_service_
->UpdateCredentials(account_id
, "refresh_token");