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/fake_profile_oauth2_token_service.h"
13 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
14 #include "chrome/browser/signin/fake_signin_manager.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/fake_auth_status_provider.h"
22 #include "components/signin/core/browser/profile_oauth2_token_service.h"
23 #include "components/signin/core/browser/signin_manager.h"
24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "google_apis/gaia/gaia_constants.h"
26 #include "google_apis/gaia/google_service_auth_error.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
32 using ::testing::AnyNumber
;
33 using ::testing::Mock
;
34 using ::testing::Return
;
35 using ::testing::ReturnRef
;
39 class MockObserver
: public SigninTracker::Observer
{
44 MOCK_METHOD1(SigninFailed
, void(const GoogleServiceAuthError
&));
45 MOCK_METHOD0(SigninSuccess
, void(void));
46 MOCK_METHOD1(MergeSessionComplete
, void(const GoogleServiceAuthError
&));
51 class SigninTrackerTest
: public testing::Test
{
53 SigninTrackerTest() {}
54 void SetUp() override
{
55 TestingProfile::Builder builder
;
56 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
57 BuildFakeProfileOAuth2TokenService
);
58 builder
.AddTestingFactory(SigninManagerFactory::GetInstance(),
59 FakeSigninManagerBase::Build
);
60 profile_
= builder
.Build();
62 fake_oauth2_token_service_
=
63 static_cast<FakeProfileOAuth2TokenService
*>(
64 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get()));
66 mock_signin_manager_
= static_cast<FakeSigninManagerForTesting
*>(
67 SigninManagerFactory::GetForProfile(profile_
.get()));
70 SigninTrackerFactory::CreateForProfile(profile_
.get(), &observer_
);
72 void TearDown() override
{
77 content::TestBrowserThreadBundle thread_bundle_
;
78 scoped_ptr
<SigninTracker
> tracker_
;
79 scoped_ptr
<TestingProfile
> profile_
;
80 FakeSigninManagerForTesting
* mock_signin_manager_
;
81 FakeProfileOAuth2TokenService
* fake_oauth2_token_service_
;
82 MockObserver observer_
;
85 #if !defined(OS_CHROMEOS)
86 TEST_F(SigninTrackerTest
, SignInFails
) {
87 const GoogleServiceAuthError
error(
88 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
90 // Signin failure should result in a SigninFailed callback.
91 EXPECT_CALL(observer_
, SigninSuccess()).Times(0);
92 EXPECT_CALL(observer_
, SigninFailed(error
));
94 mock_signin_manager_
->FailSignin(error
);
96 #endif // !defined(OS_CHROMEOS)
98 TEST_F(SigninTrackerTest
, SignInSucceeds
) {
99 EXPECT_CALL(observer_
, SigninSuccess());
100 EXPECT_CALL(observer_
, SigninFailed(_
)).Times(0);
102 mock_signin_manager_
->SetAuthenticatedUsername("user@gmail.com");
103 fake_oauth2_token_service_
->IssueRefreshTokenForUser(
104 "user@gmail.com", "refresh_token");