1 // Copyright 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.
4 #include "chrome/browser/signin/profile_oauth2_token_service_request.h"
9 #include "base/threading/thread.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 const char kAccessToken
[] = "access_token";
21 const char kAccountId
[] = "test_user@gmail.com";
22 const char kRefreshToken
[] = "refresh_token";
24 class TestingOAuth2TokenServiceConsumer
: public OAuth2TokenService::Consumer
{
26 TestingOAuth2TokenServiceConsumer();
27 virtual ~TestingOAuth2TokenServiceConsumer();
29 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request
* request
,
30 const std::string
& access_token
,
31 const base::Time
& expiration_time
) OVERRIDE
;
32 virtual void OnGetTokenFailure(const OAuth2TokenService::Request
* request
,
33 const GoogleServiceAuthError
& error
) OVERRIDE
;
35 std::string last_token_
;
36 int number_of_successful_tokens_
;
37 GoogleServiceAuthError last_error_
;
38 int number_of_errors_
;
41 TestingOAuth2TokenServiceConsumer::TestingOAuth2TokenServiceConsumer()
42 : OAuth2TokenService::Consumer("test"),
43 number_of_successful_tokens_(0),
44 last_error_(GoogleServiceAuthError::AuthErrorNone()),
45 number_of_errors_(0) {
48 TestingOAuth2TokenServiceConsumer::~TestingOAuth2TokenServiceConsumer() {
51 void TestingOAuth2TokenServiceConsumer::OnGetTokenSuccess(
52 const OAuth2TokenService::Request
* request
,
53 const std::string
& token
,
54 const base::Time
& expiration_date
) {
56 ++number_of_successful_tokens_
;
59 void TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(
60 const OAuth2TokenService::Request
* request
,
61 const GoogleServiceAuthError
& error
) {
66 class ProfileOAuth2TokenServiceRequestTest
: public testing::Test
{
68 virtual void SetUp() OVERRIDE
;
71 base::MessageLoop ui_loop_
;
72 scoped_ptr
<content::TestBrowserThread
> ui_thread_
;
74 scoped_ptr
<Profile
> profile_
;
75 TestingOAuth2TokenServiceConsumer consumer_
;
76 FakeProfileOAuth2TokenService
* oauth2_service_
;
78 scoped_ptr
<ProfileOAuth2TokenServiceRequest
> request_
;
81 void ProfileOAuth2TokenServiceRequestTest::SetUp() {
82 ui_thread_
.reset(new content::TestBrowserThread(content::BrowserThread::UI
,
84 TestingProfile::Builder builder
;
85 builder
.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
86 &FakeProfileOAuth2TokenService::Build
);
87 profile_
= builder
.Build();
89 oauth2_service_
= (FakeProfileOAuth2TokenService
*)
90 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
.get());
93 TEST_F(ProfileOAuth2TokenServiceRequestTest
,
95 scoped_ptr
<ProfileOAuth2TokenServiceRequest
> request(
96 ProfileOAuth2TokenServiceRequest::CreateAndStart(
99 OAuth2TokenService::ScopeSet(),
101 oauth2_service_
->IssueErrorForAllPendingRequests(
102 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE
));
103 ui_loop_
.RunUntilIdle();
104 EXPECT_EQ(0, consumer_
.number_of_successful_tokens_
);
105 EXPECT_EQ(1, consumer_
.number_of_errors_
);
108 TEST_F(ProfileOAuth2TokenServiceRequestTest
, Success
) {
109 oauth2_service_
->UpdateCredentials(kAccountId
, kRefreshToken
);
110 scoped_ptr
<ProfileOAuth2TokenServiceRequest
> request(
111 ProfileOAuth2TokenServiceRequest::CreateAndStart(
114 OAuth2TokenService::ScopeSet(),
116 oauth2_service_
->IssueTokenForAllPendingRequests(kAccessToken
,
118 ui_loop_
.RunUntilIdle();
119 EXPECT_EQ(1, consumer_
.number_of_successful_tokens_
);
120 EXPECT_EQ(kAccessToken
, consumer_
.last_token_
);
121 EXPECT_EQ(0, consumer_
.number_of_errors_
);
124 TEST_F(ProfileOAuth2TokenServiceRequestTest
,
125 RequestDeletionBeforeServiceComplete
) {
126 oauth2_service_
->UpdateCredentials(kAccountId
, kRefreshToken
);
127 scoped_ptr
<ProfileOAuth2TokenServiceRequest
> request(
128 ProfileOAuth2TokenServiceRequest::CreateAndStart(
131 OAuth2TokenService::ScopeSet(),
134 oauth2_service_
->IssueTokenForAllPendingRequests(kAccessToken
,
136 ui_loop_
.RunUntilIdle();
137 EXPECT_EQ(0, consumer_
.number_of_successful_tokens_
);
138 EXPECT_EQ(0, consumer_
.number_of_errors_
);
141 TEST_F(ProfileOAuth2TokenServiceRequestTest
,
142 RequestDeletionAfterServiceComplete
) {
143 oauth2_service_
->UpdateCredentials(kAccountId
, kRefreshToken
);
144 scoped_ptr
<ProfileOAuth2TokenServiceRequest
> request(
145 ProfileOAuth2TokenServiceRequest::CreateAndStart(
148 OAuth2TokenService::ScopeSet(),
150 oauth2_service_
->IssueTokenForAllPendingRequests(kAccessToken
,
152 ui_loop_
.RunUntilIdle();
154 EXPECT_EQ(1, consumer_
.number_of_successful_tokens_
);
155 EXPECT_EQ(0, consumer_
.number_of_errors_
);