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 "chrome/browser/signin/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_auth_status_provider.h"
13 #include "chrome/browser/signin/fake_profile_oauth2_token_service.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"
17 #include "chrome/browser/signin/signin_manager.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
19 #include "chrome/browser/sync/profile_sync_service_factory.h"
20 #include "chrome/browser/sync/profile_sync_service_mock.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "google_apis/gaia/gaia_constants.h"
25 #include "google_apis/gaia/google_service_auth_error.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
31 using ::testing::AnyNumber
;
32 using ::testing::Mock
;
33 using ::testing::Return
;
34 using ::testing::ReturnRef
;
38 class MockObserver
: public SigninTracker::Observer
{
43 MOCK_METHOD1(SigninFailed
, void(const GoogleServiceAuthError
&));
44 MOCK_METHOD0(SigninSuccess
, void(void));
45 MOCK_METHOD1(MergeSessionComplete
, void(const GoogleServiceAuthError
&));
50 class SigninTrackerTest
: public testing::Test
{
52 #if defined(OS_CHROMEOS)
53 typedef FakeSigninManagerBase FakeSigninManagerForTesting
;
55 typedef FakeSigninManager FakeSigninManagerForTesting
;
58 SigninTrackerTest() {}
59 virtual void SetUp() OVERRIDE
{
60 TestingProfile::Builder builder
;
61 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
62 FakeProfileOAuth2TokenService::Build
);
64 profile_
= builder
.Build();
66 fake_oauth2_token_service_
=
67 static_cast<FakeProfileOAuth2TokenService
*>(
68 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get()));
70 mock_signin_manager_
= static_cast<FakeSigninManagerForTesting
*>(
71 SigninManagerFactory::GetInstance()->SetTestingFactoryAndUse(
72 profile_
.get(), FakeSigninManagerForTesting::Build
));
73 mock_signin_manager_
->Initialize(profile_
.get(), NULL
);
75 tracker_
.reset(new SigninTracker(profile_
.get(), &observer_
));
77 virtual void TearDown() OVERRIDE
{
82 content::TestBrowserThreadBundle thread_bundle_
;
83 scoped_ptr
<SigninTracker
> tracker_
;
84 scoped_ptr
<TestingProfile
> profile_
;
85 FakeSigninManagerForTesting
* mock_signin_manager_
;
86 FakeProfileOAuth2TokenService
* fake_oauth2_token_service_
;
87 MockObserver observer_
;
90 TEST_F(SigninTrackerTest
, SignInFails
) {
91 // SIGNIN_FAILED notification should result in a SigninFailed callback.
92 const GoogleServiceAuthError
error(
93 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS
);
94 EXPECT_CALL(observer_
, SigninSuccess()).Times(0);
95 EXPECT_CALL(observer_
, SigninFailed(error
));
97 content::NotificationService::current()->Notify(
98 chrome::NOTIFICATION_GOOGLE_SIGNIN_FAILED
,
99 content::Source
<Profile
>(profile_
.get()),
100 content::Details
<const GoogleServiceAuthError
>(&error
));
103 TEST_F(SigninTrackerTest
, SignInSucceeds
) {
104 EXPECT_CALL(observer_
, SigninSuccess());
105 EXPECT_CALL(observer_
, SigninFailed(_
)).Times(0);
107 mock_signin_manager_
->SetAuthenticatedUsername("user@gmail.com");
108 fake_oauth2_token_service_
->IssueRefreshToken("refresh_token");