Add a basic settings page with a Polymer paper element which loads at chrome://md...
[chromium-blink-merge.git] / google_apis / gaia / oauth2_token_service_request_unittest.cc
blob7fc6080eeec644c146ecddad542a447e9a5fb04b
1 // Copyright 2014 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 "google_apis/gaia/oauth2_token_service_request.h"
7 #include <set>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/threading/thread.h"
13 #include "google_apis/gaia/fake_oauth2_token_service.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"
18 namespace {
20 const char kAccessToken[] = "access_token";
21 const char kAccountId[] = "test_user@gmail.com";
22 const char kScope[] = "SCOPE";
24 class TestingOAuth2TokenServiceConsumer : public OAuth2TokenService::Consumer {
25 public:
26 TestingOAuth2TokenServiceConsumer();
27 ~TestingOAuth2TokenServiceConsumer() override;
29 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
30 const std::string& access_token,
31 const base::Time& expiration_time) override;
32 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
33 const GoogleServiceAuthError& error) override;
35 int num_get_token_success_;
36 int num_get_token_failure_;
37 std::string last_token_;
38 GoogleServiceAuthError last_error_;
41 TestingOAuth2TokenServiceConsumer::TestingOAuth2TokenServiceConsumer()
42 : OAuth2TokenService::Consumer("test"),
43 num_get_token_success_(0),
44 num_get_token_failure_(0),
45 last_error_(GoogleServiceAuthError::AuthErrorNone()) {
48 TestingOAuth2TokenServiceConsumer::~TestingOAuth2TokenServiceConsumer() {
51 void TestingOAuth2TokenServiceConsumer::OnGetTokenSuccess(
52 const OAuth2TokenService::Request* request,
53 const std::string& token,
54 const base::Time& expiration_date) {
55 last_token_ = token;
56 ++num_get_token_success_;
59 void TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(
60 const OAuth2TokenService::Request* request,
61 const GoogleServiceAuthError& error) {
62 last_error_ = error;
63 ++num_get_token_failure_;
66 // A mock implementation of an OAuth2TokenService.
68 // Use SetResponse to vary the response to token requests.
69 class MockOAuth2TokenService : public FakeOAuth2TokenService {
70 public:
71 MockOAuth2TokenService();
72 ~MockOAuth2TokenService() override;
74 void SetResponse(const GoogleServiceAuthError& error,
75 const std::string& access_token,
76 const base::Time& expiration);
78 int num_invalidate_token() const { return num_invalidate_token_; }
80 const std::string& last_token_invalidated() const {
81 return last_token_invalidated_;
84 protected:
85 void FetchOAuth2Token(RequestImpl* request,
86 const std::string& account_id,
87 net::URLRequestContextGetter* getter,
88 const std::string& client_id,
89 const std::string& client_secret,
90 const ScopeSet& scopes) override;
92 void InvalidateOAuth2Token(const std::string& account_id,
93 const std::string& client_id,
94 const ScopeSet& scopes,
95 const std::string& access_token) override;
97 private:
98 GoogleServiceAuthError response_error_;
99 std::string response_access_token_;
100 base::Time response_expiration_;
101 int num_invalidate_token_;
102 std::string last_token_invalidated_;
105 MockOAuth2TokenService::MockOAuth2TokenService()
106 : response_error_(GoogleServiceAuthError::AuthErrorNone()),
107 response_access_token_(kAccessToken),
108 response_expiration_(base::Time::Max()),
109 num_invalidate_token_(0) {
112 MockOAuth2TokenService::~MockOAuth2TokenService() {
115 void MockOAuth2TokenService::SetResponse(const GoogleServiceAuthError& error,
116 const std::string& access_token,
117 const base::Time& expiration) {
118 response_error_ = error;
119 response_access_token_ = access_token;
120 response_expiration_ = expiration;
123 void MockOAuth2TokenService::FetchOAuth2Token(
124 RequestImpl* request,
125 const std::string& account_id,
126 net::URLRequestContextGetter* getter,
127 const std::string& client_id,
128 const std::string& client_secret,
129 const ScopeSet& scopes) {
130 base::MessageLoop::current()->PostTask(
131 FROM_HERE,
132 base::Bind(&OAuth2TokenService::RequestImpl::InformConsumer,
133 request->AsWeakPtr(),
134 response_error_,
135 response_access_token_,
136 response_expiration_));
139 void MockOAuth2TokenService::InvalidateOAuth2Token(
140 const std::string& account_id,
141 const std::string& client_id,
142 const ScopeSet& scopes,
143 const std::string& access_token) {
144 ++num_invalidate_token_;
145 last_token_invalidated_ = access_token;
148 class OAuth2TokenServiceRequestTest : public testing::Test {
149 public:
150 void SetUp() override;
151 void TearDown() override;
153 protected:
154 class Provider : public OAuth2TokenServiceRequest::TokenServiceProvider {
155 public:
156 Provider(const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
157 OAuth2TokenService* token_service);
159 scoped_refptr<base::SingleThreadTaskRunner> GetTokenServiceTaskRunner()
160 override;
161 OAuth2TokenService* GetTokenService() override;
163 private:
164 ~Provider() override;
166 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
167 OAuth2TokenService* token_service_;
170 base::MessageLoop ui_loop_;
171 OAuth2TokenService::ScopeSet scopes_;
172 scoped_ptr<MockOAuth2TokenService> oauth2_service_;
173 scoped_refptr<OAuth2TokenServiceRequest::TokenServiceProvider> provider_;
174 TestingOAuth2TokenServiceConsumer consumer_;
177 void OAuth2TokenServiceRequestTest::SetUp() {
178 scopes_.insert(kScope);
179 oauth2_service_.reset(new MockOAuth2TokenService);
180 oauth2_service_->AddAccount(kAccountId);
181 provider_ =
182 new Provider(base::MessageLoopProxy::current(), oauth2_service_.get());
185 void OAuth2TokenServiceRequestTest::TearDown() {
186 // Run the loop to execute any pending tasks that may free resources.
187 ui_loop_.RunUntilIdle();
190 OAuth2TokenServiceRequestTest::Provider::Provider(
191 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
192 OAuth2TokenService* token_service)
193 : task_runner_(task_runner), token_service_(token_service) {
196 scoped_refptr<base::SingleThreadTaskRunner>
197 OAuth2TokenServiceRequestTest::Provider::GetTokenServiceTaskRunner() {
198 return task_runner_;
201 OAuth2TokenService* OAuth2TokenServiceRequestTest::Provider::GetTokenService() {
202 return token_service_;
205 OAuth2TokenServiceRequestTest::Provider::~Provider() {
208 TEST_F(OAuth2TokenServiceRequestTest, CreateAndStart_Failure) {
209 oauth2_service_->SetResponse(
210 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE),
211 std::string(),
212 base::Time());
213 scoped_ptr<OAuth2TokenServiceRequest> request(
214 OAuth2TokenServiceRequest::CreateAndStart(
215 provider_.get(), kAccountId, scopes_, &consumer_));
216 ui_loop_.RunUntilIdle();
217 EXPECT_EQ(0, consumer_.num_get_token_success_);
218 EXPECT_EQ(1, consumer_.num_get_token_failure_);
219 EXPECT_EQ(GoogleServiceAuthError::SERVICE_UNAVAILABLE,
220 consumer_.last_error_.state());
221 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
224 TEST_F(OAuth2TokenServiceRequestTest, CreateAndStart_Success) {
225 scoped_ptr<OAuth2TokenServiceRequest> request(
226 OAuth2TokenServiceRequest::CreateAndStart(
227 provider_.get(), kAccountId, scopes_, &consumer_));
228 ui_loop_.RunUntilIdle();
229 EXPECT_EQ(1, consumer_.num_get_token_success_);
230 EXPECT_EQ(0, consumer_.num_get_token_failure_);
231 EXPECT_EQ(kAccessToken, consumer_.last_token_);
232 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
235 TEST_F(OAuth2TokenServiceRequestTest,
236 CreateAndStart_DestroyRequestBeforeCompletes) {
237 scoped_ptr<OAuth2TokenServiceRequest> request(
238 OAuth2TokenServiceRequest::CreateAndStart(
239 provider_.get(), kAccountId, scopes_, &consumer_));
240 request.reset();
241 ui_loop_.RunUntilIdle();
242 EXPECT_EQ(0, consumer_.num_get_token_success_);
243 EXPECT_EQ(0, consumer_.num_get_token_failure_);
244 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
247 TEST_F(OAuth2TokenServiceRequestTest,
248 CreateAndStart_DestroyRequestAfterCompletes) {
249 scoped_ptr<OAuth2TokenServiceRequest> request(
250 OAuth2TokenServiceRequest::CreateAndStart(
251 provider_.get(), kAccountId, scopes_, &consumer_));
252 ui_loop_.RunUntilIdle();
253 request.reset();
254 EXPECT_EQ(1, consumer_.num_get_token_success_);
255 EXPECT_EQ(0, consumer_.num_get_token_failure_);
256 EXPECT_EQ(kAccessToken, consumer_.last_token_);
257 EXPECT_EQ(0, oauth2_service_->num_invalidate_token());
260 TEST_F(OAuth2TokenServiceRequestTest, InvalidateToken) {
261 OAuth2TokenServiceRequest::InvalidateToken(
262 provider_.get(), kAccountId, scopes_, kAccessToken);
263 ui_loop_.RunUntilIdle();
264 EXPECT_EQ(0, consumer_.num_get_token_success_);
265 EXPECT_EQ(0, consumer_.num_get_token_failure_);
266 EXPECT_EQ(kAccessToken, oauth2_service_->last_token_invalidated());
267 EXPECT_EQ(1, oauth2_service_->num_invalidate_token());
270 } // namespace