| OLD | NEW |
| (Empty) |
| 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/oauth2_token_service_request.h" | |
| 5 | |
| 6 #include <set> | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 #include "base/threading/thread.h" | |
| 10 #include "chrome/browser/signin/oauth2_token_service.h" | |
| 11 #include "chrome/browser/signin/oauth2_token_service_factory.h" | |
| 12 #include "chrome/browser/signin/token_service_factory.h" | |
| 13 #include "chrome/test/base/testing_profile.h" | |
| 14 #include "content/public/test/test_browser_thread.h" | |
| 15 #include "google_apis/gaia/google_service_auth_error.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class TestingOAuth2TokenServiceConsumer : public OAuth2TokenService::Consumer { | |
| 21 public: | |
| 22 TestingOAuth2TokenServiceConsumer(); | |
| 23 virtual ~TestingOAuth2TokenServiceConsumer(); | |
| 24 | |
| 25 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
| 26 const std::string& access_token, | |
| 27 const base::Time& expiration_time) OVERRIDE; | |
| 28 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
| 29 const GoogleServiceAuthError& error) OVERRIDE; | |
| 30 | |
| 31 std::string last_token_; | |
| 32 int number_of_correct_tokens_; | |
| 33 GoogleServiceAuthError last_error_; | |
| 34 int number_of_errors_; | |
| 35 }; | |
| 36 | |
| 37 TestingOAuth2TokenServiceConsumer::TestingOAuth2TokenServiceConsumer() | |
| 38 : number_of_correct_tokens_(0), | |
| 39 last_error_(GoogleServiceAuthError::AuthErrorNone()), | |
| 40 number_of_errors_(0) { | |
| 41 } | |
| 42 | |
| 43 TestingOAuth2TokenServiceConsumer::~TestingOAuth2TokenServiceConsumer() { | |
| 44 } | |
| 45 | |
| 46 void TestingOAuth2TokenServiceConsumer::OnGetTokenSuccess( | |
| 47 const OAuth2TokenService::Request* request, | |
| 48 const std::string& token, | |
| 49 const base::Time& expiration_date) { | |
| 50 last_token_ = token; | |
| 51 ++number_of_correct_tokens_; | |
| 52 } | |
| 53 | |
| 54 void TestingOAuth2TokenServiceConsumer::OnGetTokenFailure( | |
| 55 const OAuth2TokenService::Request* request, | |
| 56 const GoogleServiceAuthError& error) { | |
| 57 last_error_ = error; | |
| 58 ++number_of_errors_; | |
| 59 } | |
| 60 | |
| 61 class MockOAuth2TokenService : public OAuth2TokenService { | |
| 62 public: | |
| 63 class Request : public OAuth2TokenService::Request, | |
| 64 public base::SupportsWeakPtr<Request> { | |
| 65 public: | |
| 66 Request(OAuth2TokenService::Consumer* consumer, | |
| 67 GoogleServiceAuthError error, | |
| 68 std::string access_token); | |
| 69 virtual ~Request(); | |
| 70 | |
| 71 void InformConsumer() const; | |
| 72 | |
| 73 private: | |
| 74 OAuth2TokenService::Consumer* consumer_; | |
| 75 GoogleServiceAuthError error_; | |
| 76 std::string access_token_; | |
| 77 base::Time expiration_date_; | |
| 78 }; | |
| 79 | |
| 80 MockOAuth2TokenService(); | |
| 81 virtual ~MockOAuth2TokenService(); | |
| 82 | |
| 83 virtual scoped_ptr<OAuth2TokenService::Request> StartRequest( | |
| 84 const std::set<std::string>& scopes, | |
| 85 OAuth2TokenService::Consumer* consumer) OVERRIDE; | |
| 86 | |
| 87 void SetExpectation(bool success, std::string oauth2_access_token); | |
| 88 | |
| 89 private: | |
| 90 static void InformConsumer( | |
| 91 base::WeakPtr<MockOAuth2TokenService::Request> request); | |
| 92 | |
| 93 bool success_; | |
| 94 std::string oauth2_access_token_; | |
| 95 }; | |
| 96 | |
| 97 MockOAuth2TokenService::Request::Request( | |
| 98 OAuth2TokenService::Consumer* consumer, | |
| 99 GoogleServiceAuthError error, | |
| 100 std::string access_token) | |
| 101 : consumer_(consumer), | |
| 102 error_(error), | |
| 103 access_token_(access_token) { | |
| 104 } | |
| 105 | |
| 106 MockOAuth2TokenService::Request::~Request() { | |
| 107 } | |
| 108 | |
| 109 void MockOAuth2TokenService::Request::InformConsumer() const { | |
| 110 if (error_.state() == GoogleServiceAuthError::NONE) | |
| 111 consumer_->OnGetTokenSuccess(this, access_token_, expiration_date_); | |
| 112 else | |
| 113 consumer_->OnGetTokenFailure(this, error_); | |
| 114 } | |
| 115 | |
| 116 MockOAuth2TokenService::MockOAuth2TokenService() | |
| 117 : success_(true), | |
| 118 oauth2_access_token_(std::string("success token")) { | |
| 119 } | |
| 120 | |
| 121 MockOAuth2TokenService::~MockOAuth2TokenService() { | |
| 122 } | |
| 123 | |
| 124 void MockOAuth2TokenService::SetExpectation(bool success, | |
| 125 std::string oauth2_access_token) { | |
| 126 success_ = success; | |
| 127 oauth2_access_token_ = oauth2_access_token; | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 void MockOAuth2TokenService::InformConsumer( | |
| 132 base::WeakPtr<MockOAuth2TokenService::Request> request) { | |
| 133 if (request) | |
| 134 request->InformConsumer(); | |
| 135 } | |
| 136 | |
| 137 scoped_ptr<OAuth2TokenService::Request> MockOAuth2TokenService::StartRequest( | |
| 138 const std::set<std::string>& scopes, | |
| 139 OAuth2TokenService::Consumer* consumer) { | |
| 140 scoped_ptr<Request> request; | |
| 141 if (success_) { | |
| 142 request.reset(new MockOAuth2TokenService::Request( | |
| 143 consumer, | |
| 144 GoogleServiceAuthError(GoogleServiceAuthError::NONE), | |
| 145 oauth2_access_token_)); | |
| 146 } else { | |
| 147 request.reset(new MockOAuth2TokenService::Request( | |
| 148 consumer, | |
| 149 GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_UNAVAILABLE), | |
| 150 std::string())); | |
| 151 } | |
| 152 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 153 &MockOAuth2TokenService::InformConsumer, request->AsWeakPtr())); | |
| 154 return request.PassAs<OAuth2TokenService::Request>(); | |
| 155 } | |
| 156 | |
| 157 static ProfileKeyedService* CreateOAuth2TokenService(Profile* profile) { | |
| 158 return new MockOAuth2TokenService(); | |
| 159 } | |
| 160 | |
| 161 class OAuth2TokenServiceRequestTest : public testing::Test { | |
| 162 public: | |
| 163 virtual void SetUp() OVERRIDE; | |
| 164 | |
| 165 protected: | |
| 166 MessageLoop ui_loop_; | |
| 167 scoped_ptr<content::TestBrowserThread> ui_thread_; | |
| 168 | |
| 169 scoped_ptr<Profile> profile_; | |
| 170 TestingOAuth2TokenServiceConsumer consumer_; | |
| 171 MockOAuth2TokenService* oauth2_service_; | |
| 172 | |
| 173 scoped_ptr<OAuth2TokenServiceRequest> request_; | |
| 174 }; | |
| 175 | |
| 176 void OAuth2TokenServiceRequestTest::SetUp() { | |
| 177 ui_thread_.reset(new content::TestBrowserThread(content::BrowserThread::UI, | |
| 178 &ui_loop_)); | |
| 179 profile_.reset(new TestingProfile()); | |
| 180 OAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | |
| 181 profile_.get(), &CreateOAuth2TokenService); | |
| 182 oauth2_service_ = (MockOAuth2TokenService*) | |
| 183 OAuth2TokenServiceFactory::GetForProfile(profile_.get()); | |
| 184 } | |
| 185 | |
| 186 TEST_F(OAuth2TokenServiceRequestTest, | |
| 187 Failure) { | |
| 188 oauth2_service_->SetExpectation(false, std::string()); | |
| 189 scoped_ptr<OAuth2TokenServiceRequest> request( | |
| 190 OAuth2TokenServiceRequest::CreateAndStart( | |
| 191 profile_.get(), | |
| 192 std::set<std::string>(), | |
| 193 &consumer_)); | |
| 194 ui_loop_.RunUntilIdle(); | |
| 195 EXPECT_EQ(0, consumer_.number_of_correct_tokens_); | |
| 196 EXPECT_EQ(1, consumer_.number_of_errors_); | |
| 197 } | |
| 198 | |
| 199 TEST_F(OAuth2TokenServiceRequestTest, | |
| 200 Success) { | |
| 201 scoped_ptr<OAuth2TokenServiceRequest> request( | |
| 202 OAuth2TokenServiceRequest::CreateAndStart( | |
| 203 profile_.get(), | |
| 204 std::set<std::string>(), | |
| 205 &consumer_)); | |
| 206 ui_loop_.RunUntilIdle(); | |
| 207 EXPECT_EQ(1, consumer_.number_of_correct_tokens_); | |
| 208 EXPECT_EQ("success token", consumer_.last_token_); | |
| 209 EXPECT_EQ(0, consumer_.number_of_errors_); | |
| 210 } | |
| 211 | |
| 212 TEST_F(OAuth2TokenServiceRequestTest, | |
| 213 RequestDeletionBeforeServiceComplete) { | |
| 214 scoped_ptr<OAuth2TokenServiceRequest> request( | |
| 215 OAuth2TokenServiceRequest::CreateAndStart( | |
| 216 profile_.get(), | |
| 217 std::set<std::string>(), | |
| 218 &consumer_)); | |
| 219 request.reset(); | |
| 220 ui_loop_.RunUntilIdle(); | |
| 221 EXPECT_EQ(0, consumer_.number_of_correct_tokens_); | |
| 222 EXPECT_EQ(0, consumer_.number_of_errors_); | |
| 223 } | |
| 224 | |
| 225 TEST_F(OAuth2TokenServiceRequestTest, | |
| 226 RequestDeletionAfterServiceComplete) { | |
| 227 scoped_ptr<OAuth2TokenServiceRequest> request( | |
| 228 OAuth2TokenServiceRequest::CreateAndStart( | |
| 229 profile_.get(), | |
| 230 std::set<std::string>(), | |
| 231 &consumer_)); | |
| 232 ui_loop_.RunUntilIdle(); | |
| 233 request.reset(); | |
| 234 EXPECT_EQ(1, consumer_.number_of_correct_tokens_); | |
| 235 EXPECT_EQ(0, consumer_.number_of_errors_); | |
| 236 } | |
| 237 | |
| 238 } // namespace | |
| OLD | NEW |