Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / signin / profile_oauth2_token_service_request_unittest.cc
blobb9c41053e1f6f5c1ef493cc3e33a0fa9fa1c3e20
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"
6 #include <set>
7 #include <string>
8 #include <vector>
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"
18 namespace {
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 {
25 public:
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) {
55 last_token_ = token;
56 ++number_of_successful_tokens_;
59 void TestingOAuth2TokenServiceConsumer::OnGetTokenFailure(
60 const OAuth2TokenService::Request* request,
61 const GoogleServiceAuthError& error) {
62 last_error_ = error;
63 ++number_of_errors_;
66 class ProfileOAuth2TokenServiceRequestTest : public testing::Test {
67 public:
68 virtual void SetUp() OVERRIDE;
70 protected:
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,
83 &ui_loop_));
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,
94 Failure) {
95 scoped_ptr<ProfileOAuth2TokenServiceRequest> request(
96 ProfileOAuth2TokenServiceRequest::CreateAndStart(
97 profile_.get(),
98 std::string(),
99 OAuth2TokenService::ScopeSet(),
100 &consumer_));
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(
112 profile_.get(),
113 kAccountId,
114 OAuth2TokenService::ScopeSet(),
115 &consumer_));
116 oauth2_service_->IssueTokenForAllPendingRequests(kAccessToken,
117 base::Time::Max());
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(
129 profile_.get(),
130 kAccountId,
131 OAuth2TokenService::ScopeSet(),
132 &consumer_));
133 request.reset();
134 oauth2_service_->IssueTokenForAllPendingRequests(kAccessToken,
135 base::Time::Max());
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(
146 profile_.get(),
147 kAccountId,
148 OAuth2TokenService::ScopeSet(),
149 &consumer_));
150 oauth2_service_->IssueTokenForAllPendingRequests(kAccessToken,
151 base::Time::Max());
152 ui_loop_.RunUntilIdle();
153 request.reset();
154 EXPECT_EQ(1, consumer_.number_of_successful_tokens_);
155 EXPECT_EQ(0, consumer_.number_of_errors_);
158 } // namespace