Upstream PO2TS_IOS unit test.
BUG=NONE Review URL: https://codereview.chromium.org/234573003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263249 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
@ -115,6 +115,7 @@
|
||||
'signin/core/browser/mutable_profile_oauth2_token_service_unittest.cc',
|
||||
'signin/core/browser/signin_error_controller_unittest.cc',
|
||||
'signin/core/browser/webdata/token_service_table_unittest.cc',
|
||||
'signin/ios/browser/profile_oauth2_token_service_ios_unittest.mm',
|
||||
'storage_monitor/image_capture_device_manager_unittest.mm',
|
||||
'storage_monitor/media_storage_util_unittest.cc',
|
||||
'storage_monitor/media_transfer_protocol_device_observer_linux_unittest.cc',
|
||||
@ -298,6 +299,11 @@
|
||||
['include', '^translate/'],
|
||||
['include', '^variations/'],
|
||||
],
|
||||
'dependencies': [
|
||||
# Dependencies of signin
|
||||
'components.gyp:signin_ios_browser',
|
||||
'../ios/ios_tests.gyp:test_support_ios',
|
||||
],
|
||||
'actions': [
|
||||
{
|
||||
'action_name': 'copy_test_data',
|
||||
|
6
components/signin/core/browser/DEPS
Normal file
6
components/signin/core/browser/DEPS
Normal file
@ -0,0 +1,6 @@
|
||||
specific_include_rules = {
|
||||
# Needed for test wire-up on iOS.
|
||||
'.*test.*': [
|
||||
"+ios/public/test/fake_profile_oauth2_token_service_ios_provider.h",
|
||||
],
|
||||
}
|
@ -9,6 +9,10 @@
|
||||
#include "components/webdata/common/web_database_service.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
#if defined(OS_IOS)
|
||||
#include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h"
|
||||
#endif
|
||||
|
||||
TestSigninClient::TestSigninClient()
|
||||
: request_context_(new net::TestURLRequestContextGetter(
|
||||
base::MessageLoopProxy::current())) {
|
||||
@ -56,8 +60,14 @@ void TestSigninClient::SetCookieChangedCallback(
|
||||
|
||||
#if defined(OS_IOS)
|
||||
ios::ProfileOAuth2TokenServiceIOSProvider* TestSigninClient::GetIOSProvider() {
|
||||
// Just returns NULL for now. It should be changed to return an
|
||||
// |ios::FakeProfileOAuth2TokenServiceIOSProvider|.
|
||||
return NULL;
|
||||
return GetIOSProviderAsFake();
|
||||
}
|
||||
|
||||
ios::FakeProfileOAuth2TokenServiceIOSProvider*
|
||||
TestSigninClient::GetIOSProviderAsFake() {
|
||||
if (!iosProvider_) {
|
||||
iosProvider_.reset(new ios::FakeProfileOAuth2TokenServiceIOSProvider());
|
||||
}
|
||||
return iosProvider_.get();
|
||||
}
|
||||
#endif
|
||||
|
@ -9,9 +9,14 @@
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/files/scoped_temp_dir.h"
|
||||
#include "base/memory/ref_counted.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "components/signin/core/browser/signin_client.h"
|
||||
#include "net/url_request/url_request_test_util.h"
|
||||
|
||||
#if defined(OS_IOS)
|
||||
#include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h"
|
||||
#endif
|
||||
|
||||
// An implementation of SigninClient for use in unittests. Instantiates test
|
||||
// versions of the various objects that SigninClient is required to provide as
|
||||
// part of its interface.
|
||||
@ -50,6 +55,10 @@ class TestSigninClient : public SigninClient {
|
||||
virtual void SetCookieChangedCallback(const CookieChangedCallback& callback)
|
||||
OVERRIDE;
|
||||
|
||||
#if defined(OS_IOS)
|
||||
ios::FakeProfileOAuth2TokenServiceIOSProvider* GetIOSProviderAsFake();
|
||||
#endif
|
||||
|
||||
private:
|
||||
// Loads the token database.
|
||||
void LoadDatabase();
|
||||
@ -58,6 +67,10 @@ class TestSigninClient : public SigninClient {
|
||||
scoped_refptr<net::TestURLRequestContextGetter> request_context_;
|
||||
scoped_refptr<TokenWebData> database_;
|
||||
|
||||
#if defined(OS_IOS)
|
||||
scoped_ptr<ios::FakeProfileOAuth2TokenServiceIOSProvider> iosProvider_;
|
||||
#endif
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(TestSigninClient);
|
||||
};
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
include_rules = [
|
||||
"+ios/public/provider/components/signin",
|
||||
"+ios/public/test",
|
||||
]
|
||||
|
@ -16,14 +16,17 @@ namespace ios{
|
||||
class ProfileOAuth2TokenServiceIOSProvider;
|
||||
}
|
||||
|
||||
// A specialization of ProfileOAuth2TokenService for OS_IOS. It fetches access
|
||||
// tokens from the SSOAuth library if the user is signed in using shared
|
||||
// authentication or defaults to the parent class
|
||||
// |MutableProfileOAuth2TokenService| for pre-SSO signed in users.
|
||||
// A specialization of ProfileOAuth2TokenService that will be returned by
|
||||
// ProfileOAuth2TokenServiceFactory for OS_IOS when iOS authentication service
|
||||
// is used to lookup OAuth2 tokens.
|
||||
//
|
||||
// See |ProfileOAuth2TokenService| for usage details.
|
||||
//
|
||||
// Note: Requests should be started from the UI thread. To start a
|
||||
// request from aother thread, please use ProfileOAuth2TokenServiceRequest.
|
||||
class ProfileOAuth2TokenServiceIOS : public MutableProfileOAuth2TokenService {
|
||||
public:
|
||||
ProfileOAuth2TokenServiceIOS();
|
||||
virtual ~ProfileOAuth2TokenServiceIOS();
|
||||
|
||||
// KeyedService
|
||||
@ -84,10 +87,6 @@ class ProfileOAuth2TokenServiceIOS : public MutableProfileOAuth2TokenService {
|
||||
void ForceInvalidGrantResponses();
|
||||
|
||||
protected:
|
||||
friend class ProfileOAuth2TokenServiceFactory;
|
||||
|
||||
ProfileOAuth2TokenServiceIOS();
|
||||
|
||||
virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(
|
||||
const std::string& account_id,
|
||||
net::URLRequestContextGetter* getter,
|
||||
|
@ -453,6 +453,7 @@ void ProfileOAuth2TokenServiceIOS::AddOrUpdateAccount(
|
||||
const std::string& account_id) {
|
||||
DCHECK(thread_checker_.CalledOnValidThread());
|
||||
DCHECK(!account_id.empty());
|
||||
DCHECK(!use_legacy_token_service_);
|
||||
|
||||
bool account_present = accounts_.count(account_id) > 0;
|
||||
if (account_present && accounts_[account_id]->GetAuthStatus().state() ==
|
||||
@ -476,6 +477,7 @@ void ProfileOAuth2TokenServiceIOS::RemoveAccount(
|
||||
const std::string& account_id) {
|
||||
DCHECK(thread_checker_.CalledOnValidThread());
|
||||
DCHECK(!account_id.empty());
|
||||
DCHECK(!use_legacy_token_service_);
|
||||
|
||||
if (accounts_.count(account_id) > 0) {
|
||||
CancelRequestsForAccount(account_id);
|
||||
|
@ -0,0 +1,276 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "base/run_loop.h"
|
||||
#include "components/signin/core/browser/profile_oauth2_token_service.h"
|
||||
#include "components/signin/core/browser/test_signin_client.h"
|
||||
#include "components/signin/ios/browser/profile_oauth2_token_service_ios.h"
|
||||
#include "google_apis/gaia/gaia_urls.h"
|
||||
#include "google_apis/gaia/oauth2_token_service_test_util.h"
|
||||
#include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h"
|
||||
#include "net/url_request/test_url_fetcher_factory.h"
|
||||
#include "testing/gtest/include/gtest/gtest.h"
|
||||
|
||||
class ProfileOAuth2TokenServiceIOSTest : public testing::Test,
|
||||
public OAuth2TokenService::Consumer,
|
||||
public OAuth2TokenService::Observer {
|
||||
public:
|
||||
ProfileOAuth2TokenServiceIOSTest()
|
||||
: OAuth2TokenService::Consumer("test_consumer_id"),
|
||||
factory_(NULL),
|
||||
token_available_count_(0),
|
||||
token_revoked_count_(0),
|
||||
tokens_loaded_count_(0),
|
||||
access_token_success_(0),
|
||||
access_token_failure_(0),
|
||||
last_access_token_error_(GoogleServiceAuthError::NONE) {}
|
||||
|
||||
virtual void SetUp() OVERRIDE {
|
||||
factory_.SetFakeResponse(GaiaUrls::GetInstance()->oauth2_revoke_url(),
|
||||
"",
|
||||
net::HTTP_OK,
|
||||
net::URLRequestStatus::SUCCESS);
|
||||
fake_provider_ = client_.GetIOSProviderAsFake();
|
||||
fake_provider_->set_using_shared_authentication(true);
|
||||
oauth2_service_.Initialize(&client_);
|
||||
oauth2_service_.AddObserver(this);
|
||||
}
|
||||
|
||||
virtual void TearDown() OVERRIDE {
|
||||
oauth2_service_.RemoveObserver(this);
|
||||
oauth2_service_.Shutdown();
|
||||
}
|
||||
|
||||
// OAuth2TokenService::Consumer implementation.
|
||||
virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
|
||||
const std::string& access_token,
|
||||
const base::Time& expiration_time) OVERRIDE {
|
||||
++access_token_success_;
|
||||
}
|
||||
|
||||
virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request,
|
||||
const GoogleServiceAuthError& error) OVERRIDE {
|
||||
++access_token_failure_;
|
||||
last_access_token_error_ = error;
|
||||
};
|
||||
|
||||
// OAuth2TokenService::Observer implementation.
|
||||
virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE {
|
||||
++token_available_count_;
|
||||
}
|
||||
virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE {
|
||||
++token_revoked_count_;
|
||||
}
|
||||
virtual void OnRefreshTokensLoaded() OVERRIDE { ++tokens_loaded_count_; }
|
||||
|
||||
void ResetObserverCounts() {
|
||||
token_available_count_ = 0;
|
||||
token_revoked_count_ = 0;
|
||||
tokens_loaded_count_ = 0;
|
||||
token_available_count_ = 0;
|
||||
access_token_failure_ = 0;
|
||||
}
|
||||
|
||||
protected:
|
||||
base::MessageLoop message_loop_;
|
||||
net::FakeURLFetcherFactory factory_;
|
||||
TestSigninClient client_;
|
||||
ios::FakeProfileOAuth2TokenServiceIOSProvider* fake_provider_;
|
||||
ProfileOAuth2TokenServiceIOS oauth2_service_;
|
||||
TestingOAuth2TokenServiceConsumer consumer_;
|
||||
int token_available_count_;
|
||||
int token_revoked_count_;
|
||||
int tokens_loaded_count_;
|
||||
int access_token_success_;
|
||||
int access_token_failure_;
|
||||
GoogleServiceAuthError last_access_token_error_;
|
||||
};
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest, LoadRevokeCredentialsOneAccount) {
|
||||
fake_provider_->AddAccount("account_id");
|
||||
oauth2_service_.LoadCredentials("account_id");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
EXPECT_EQ(1, token_available_count_);
|
||||
EXPECT_EQ(1, tokens_loaded_count_);
|
||||
EXPECT_EQ(0, token_revoked_count_);
|
||||
EXPECT_EQ(1U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id"));
|
||||
|
||||
ResetObserverCounts();
|
||||
oauth2_service_.RevokeAllCredentials();
|
||||
EXPECT_EQ(0, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(1, token_revoked_count_);
|
||||
EXPECT_EQ(0U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
}
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest,
|
||||
LoadRevokeCredentialsMultipleAccounts) {
|
||||
fake_provider_->AddAccount("account_id_1");
|
||||
fake_provider_->AddAccount("account_id_2");
|
||||
fake_provider_->AddAccount("account_id_3");
|
||||
oauth2_service_.LoadCredentials("account_id_1");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
EXPECT_EQ(3, token_available_count_);
|
||||
EXPECT_EQ(1, tokens_loaded_count_);
|
||||
EXPECT_EQ(0, token_revoked_count_);
|
||||
EXPECT_EQ(3U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_2"));
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_3"));
|
||||
|
||||
ResetObserverCounts();
|
||||
oauth2_service_.RevokeAllCredentials();
|
||||
EXPECT_EQ(0, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(3, token_revoked_count_);
|
||||
EXPECT_EQ(0U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2"));
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_3"));
|
||||
}
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest, ReloadCredentials) {
|
||||
fake_provider_->AddAccount("account_id_1");
|
||||
fake_provider_->AddAccount("account_id_2");
|
||||
fake_provider_->AddAccount("account_id_3");
|
||||
oauth2_service_.LoadCredentials("account_id_1");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
|
||||
// Change the accounts.
|
||||
ResetObserverCounts();
|
||||
fake_provider_->ClearAccounts();
|
||||
fake_provider_->AddAccount("account_id_1");
|
||||
fake_provider_->AddAccount("account_id_4");
|
||||
oauth2_service_.ReloadCredentials();
|
||||
|
||||
EXPECT_EQ(1, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(2, token_revoked_count_);
|
||||
EXPECT_EQ(2U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2"));
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_3"));
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_4"));
|
||||
}
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest, StartRequestSuccess) {
|
||||
fake_provider_->AddAccount("account_id_1");
|
||||
oauth2_service_.LoadCredentials("account_id_1");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
|
||||
// Fetch access tokens.
|
||||
ResetObserverCounts();
|
||||
OAuth2TokenService::ScopeSet scopes;
|
||||
scopes.insert("scope");
|
||||
scoped_ptr<OAuth2TokenService::Request> request(
|
||||
oauth2_service_.StartRequest("account_id_1", scopes, this));
|
||||
EXPECT_EQ(0, access_token_success_);
|
||||
EXPECT_EQ(0, access_token_failure_);
|
||||
|
||||
ResetObserverCounts();
|
||||
fake_provider_->IssueAccessTokenForAllRequests();
|
||||
base::RunLoop().RunUntilIdle();
|
||||
EXPECT_EQ(1, access_token_success_);
|
||||
EXPECT_EQ(0, access_token_failure_);
|
||||
}
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest, StartRequestFailure) {
|
||||
fake_provider_->AddAccount("account_id_1");
|
||||
oauth2_service_.LoadCredentials("account_id_1");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
|
||||
// Fetch access tokens.
|
||||
ResetObserverCounts();
|
||||
OAuth2TokenService::ScopeSet scopes;
|
||||
scopes.insert("scope");
|
||||
scoped_ptr<OAuth2TokenService::Request> request(
|
||||
oauth2_service_.StartRequest("account_id_1", scopes, this));
|
||||
EXPECT_EQ(0, access_token_success_);
|
||||
EXPECT_EQ(0, access_token_failure_);
|
||||
|
||||
ResetObserverCounts();
|
||||
fake_provider_->IssueAccessTokenErrorForAllRequests();
|
||||
base::RunLoop().RunUntilIdle();
|
||||
EXPECT_EQ(0, access_token_success_);
|
||||
EXPECT_EQ(1, access_token_failure_);
|
||||
}
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest, Migration) {
|
||||
fake_provider_->set_using_shared_authentication(false);
|
||||
oauth2_service_.LoadCredentials("account_id_1");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
|
||||
ResetObserverCounts();
|
||||
oauth2_service_.UpdateCredentials("account_id_1", "pre_sso_refresh_token_1");
|
||||
oauth2_service_.UpdateCredentials("account_id_2", "pre_sso_refresh_token_2");
|
||||
EXPECT_EQ(2, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(0, token_revoked_count_);
|
||||
EXPECT_EQ(2U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_2"));
|
||||
EXPECT_EQ("pre_sso_refresh_token_1",
|
||||
oauth2_service_.GetRefreshTokenWhenNotUsingSharedAuthentication(
|
||||
"account_id_1"));
|
||||
EXPECT_EQ("pre_sso_refresh_token_2",
|
||||
oauth2_service_.GetRefreshTokenWhenNotUsingSharedAuthentication(
|
||||
"account_id_2"));
|
||||
|
||||
ResetObserverCounts();
|
||||
oauth2_service_.StartUsingSharedAuthentication();
|
||||
EXPECT_EQ(0, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(2, token_revoked_count_);
|
||||
EXPECT_EQ(0U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2"));
|
||||
|
||||
ResetObserverCounts();
|
||||
fake_provider_->AddAccount("account_id_1");
|
||||
oauth2_service_.ReloadCredentials();
|
||||
EXPECT_EQ(1, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(0, token_revoked_count_);
|
||||
EXPECT_EQ(1U, oauth2_service_.GetAccounts().size());
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
EXPECT_FALSE(oauth2_service_.RefreshTokenIsAvailable("account_id_2"));
|
||||
}
|
||||
|
||||
TEST_F(ProfileOAuth2TokenServiceIOSTest, ForceInvalidGrantResponses) {
|
||||
fake_provider_->set_using_shared_authentication(false);
|
||||
oauth2_service_.LoadCredentials("account_id_1");
|
||||
base::RunLoop().RunUntilIdle();
|
||||
oauth2_service_.UpdateCredentials("account_id_1", "pre_sso_refresh_token_1");
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
|
||||
// First call revokes the existing token and then updates the credentials
|
||||
// with a fake token.
|
||||
ResetObserverCounts();
|
||||
oauth2_service_.ForceInvalidGrantResponses();
|
||||
EXPECT_EQ(1, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(1, token_revoked_count_);
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
|
||||
// Fetching access tokens fails with invalid grant responses.
|
||||
OAuth2TokenService::ScopeSet scopes;
|
||||
scopes.insert("scope");
|
||||
scoped_ptr<OAuth2TokenService::Request> request(
|
||||
oauth2_service_.StartRequest("account_id_1", scopes, this));
|
||||
base::RunLoop().RunUntilIdle();
|
||||
EXPECT_EQ(0, access_token_success_);
|
||||
EXPECT_EQ(1, access_token_failure_);
|
||||
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
|
||||
last_access_token_error_.state());
|
||||
|
||||
// Second call to force invalid grant responses is ignored.
|
||||
ResetObserverCounts();
|
||||
oauth2_service_.ForceInvalidGrantResponses();
|
||||
EXPECT_EQ(0, token_available_count_);
|
||||
EXPECT_EQ(0, tokens_loaded_count_);
|
||||
EXPECT_EQ(0, token_revoked_count_);
|
||||
EXPECT_TRUE(oauth2_service_.RefreshTokenIsAvailable("account_id_1"));
|
||||
}
|
24
ios/ios_tests.gyp
Normal file
24
ios/ios_tests.gyp
Normal file
@ -0,0 +1,24 @@
|
||||
# Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style license that can be
|
||||
# found in the LICENSE file.
|
||||
{
|
||||
'variables': {
|
||||
'chromium_code': 1,
|
||||
},
|
||||
'targets': [
|
||||
{
|
||||
'target_name': 'test_support_ios',
|
||||
'type': 'static_library',
|
||||
'sources': [
|
||||
'public/test/fake_profile_oauth2_token_service_ios_provider.h',
|
||||
'public/test/fake_profile_oauth2_token_service_ios_provider.mm',
|
||||
],
|
||||
'dependencies': [
|
||||
'<(DEPTH)/testing/gtest.gyp:gtest',
|
||||
],
|
||||
'include_dirs': [
|
||||
'<(DEPTH)',
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef IOS_TEST_MOCK_PROFILE_OAUTH2_TOKEN_SERVICE_PROVIDER_IOS_H_
|
||||
#define IOS_TEST_MOCK_PROFILE_OAUTH2_TOKEN_SERVICE_PROVIDER_IOS_H_
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "ios/public/provider/components/signin/browser/profile_oauth2_token_service_ios_provider.h"
|
||||
|
||||
namespace ios {
|
||||
|
||||
// Mock class of ProfileOAuth2TokenServiceIOSProvider for testing.
|
||||
class FakeProfileOAuth2TokenServiceIOSProvider
|
||||
: public ProfileOAuth2TokenServiceIOSProvider {
|
||||
public:
|
||||
FakeProfileOAuth2TokenServiceIOSProvider();
|
||||
virtual ~FakeProfileOAuth2TokenServiceIOSProvider();
|
||||
|
||||
// ProfileOAuth2TokenServiceIOSProvider
|
||||
virtual bool IsUsingSharedAuthentication() const OVERRIDE;
|
||||
virtual void InitializeSharedAuthentication() OVERRIDE;
|
||||
|
||||
virtual void GetAccessToken(const std::string& account_id,
|
||||
const std::string& client_id,
|
||||
const std::string& client_secret,
|
||||
const std::set<std::string>& scopes,
|
||||
const AccessTokenCallback& callback) OVERRIDE;
|
||||
|
||||
virtual std::vector<std::string> GetAllAccountIds() OVERRIDE;
|
||||
|
||||
virtual AuthenticationErrorCategory GetAuthenticationErrorCategory(
|
||||
NSError* error) const OVERRIDE;
|
||||
|
||||
// Methods to configure this fake provider.
|
||||
void AddAccount(const std::string& account_id);
|
||||
void SetAccounts(const std::vector<std::string>& accounts);
|
||||
void ClearAccounts();
|
||||
void set_using_shared_authentication(bool is_using_shared_authentication) {
|
||||
is_using_shared_authentication_ = is_using_shared_authentication;
|
||||
}
|
||||
|
||||
// Issues access token responses.
|
||||
void IssueAccessTokenForAllRequests();
|
||||
void IssueAccessTokenErrorForAllRequests();
|
||||
|
||||
private:
|
||||
typedef std::pair<std::string, AccessTokenCallback> AccessTokenRequest;
|
||||
|
||||
std::vector<std::string> accounts_;
|
||||
bool is_using_shared_authentication_;
|
||||
std::vector<AccessTokenRequest> requests_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(FakeProfileOAuth2TokenServiceIOSProvider);
|
||||
};
|
||||
|
||||
} // namespace ios
|
||||
|
||||
#endif // IOS_TEST_PROVIDER_CHROME_BROWSER_SIGNIN_MOCK_PROFILE_OAUTH2_TOKEN_SERVICE_PROVIDER_IOS_H_
|
@ -0,0 +1,91 @@
|
||||
// Copyright 2014 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "ios/public/test/fake_profile_oauth2_token_service_ios_provider.h"
|
||||
|
||||
#include <Foundation/Foundation.h>
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
|
||||
namespace ios {
|
||||
|
||||
FakeProfileOAuth2TokenServiceIOSProvider::
|
||||
FakeProfileOAuth2TokenServiceIOSProvider()
|
||||
: is_using_shared_authentication_(true) {}
|
||||
|
||||
FakeProfileOAuth2TokenServiceIOSProvider::
|
||||
~FakeProfileOAuth2TokenServiceIOSProvider() {}
|
||||
|
||||
void FakeProfileOAuth2TokenServiceIOSProvider::GetAccessToken(
|
||||
const std::string& account_id,
|
||||
const std::string& client_id,
|
||||
const std::string& client_secret,
|
||||
const std::set<std::string>& scopes,
|
||||
const AccessTokenCallback& callback) {
|
||||
DCHECK(is_using_shared_authentication_);
|
||||
requests_.push_back(AccessTokenRequest(account_id, callback));
|
||||
}
|
||||
|
||||
std::vector<std::string>
|
||||
FakeProfileOAuth2TokenServiceIOSProvider::GetAllAccountIds() {
|
||||
return accounts_;
|
||||
}
|
||||
|
||||
void FakeProfileOAuth2TokenServiceIOSProvider::AddAccount(
|
||||
const std::string& account_id) {
|
||||
accounts_.push_back(account_id);
|
||||
}
|
||||
|
||||
void FakeProfileOAuth2TokenServiceIOSProvider::SetAccounts(
|
||||
const std::vector<std::string>& accounts) {
|
||||
accounts_ = accounts;
|
||||
}
|
||||
|
||||
void FakeProfileOAuth2TokenServiceIOSProvider::ClearAccounts() {
|
||||
accounts_.clear();
|
||||
}
|
||||
|
||||
void
|
||||
FakeProfileOAuth2TokenServiceIOSProvider::IssueAccessTokenForAllRequests() {
|
||||
for (auto i = requests_.begin(); i != requests_.end(); ++i) {
|
||||
std::string account_id = i->first;
|
||||
AccessTokenCallback callback = i->second;
|
||||
NSString* access_token = [NSString
|
||||
stringWithFormat:@"fake_access_token [account=%s]", account_id.c_str()];
|
||||
NSDate* one_hour_from_now = [NSDate dateWithTimeIntervalSinceNow:3600];
|
||||
callback.Run(access_token, one_hour_from_now, nil);
|
||||
}
|
||||
requests_.clear();
|
||||
}
|
||||
|
||||
void FakeProfileOAuth2TokenServiceIOSProvider::
|
||||
IssueAccessTokenErrorForAllRequests() {
|
||||
for (auto i = requests_.begin(); i != requests_.end(); ++i) {
|
||||
std::string account_id = i->first;
|
||||
AccessTokenCallback callback = i->second;
|
||||
NSError* error = [[[NSError alloc] initWithDomain:@"fake_access_token_error"
|
||||
code:-1
|
||||
userInfo:nil] autorelease];
|
||||
callback.Run(nil, nil, error);
|
||||
}
|
||||
requests_.clear();
|
||||
}
|
||||
|
||||
bool FakeProfileOAuth2TokenServiceIOSProvider::IsUsingSharedAuthentication()
|
||||
const {
|
||||
return is_using_shared_authentication_;
|
||||
}
|
||||
|
||||
void
|
||||
FakeProfileOAuth2TokenServiceIOSProvider::InitializeSharedAuthentication() {}
|
||||
|
||||
AuthenticationErrorCategory
|
||||
FakeProfileOAuth2TokenServiceIOSProvider::GetAuthenticationErrorCategory(
|
||||
NSError* error) const {
|
||||
DCHECK(error);
|
||||
return ios::kAuthenticationErrorCategoryAuthorizationErrors;
|
||||
}
|
||||
|
||||
} // namespace ios
|
Reference in New Issue
Block a user