diff options
author | msarda@chromium.org <msarda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-11 15:36:53 +0000 |
---|---|---|
committer | msarda@chromium.org <msarda@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-11 15:36:53 +0000 |
commit | b472aee06a9d122e8ed38d1e88a0a92d9a6d84e8 (patch) | |
tree | e10012531c39956cf701b28741a7886ba3b178bf /ios | |
parent | 7b6e533d6fd37a43ec29d257fee5fa138dd942e1 (diff) | |
download | chromium_src-b472aee06a9d122e8ed38d1e88a0a92d9a6d84e8.zip chromium_src-b472aee06a9d122e8ed38d1e88a0a92d9a6d84e8.tar.gz chromium_src-b472aee06a9d122e8ed38d1e88a0a92d9a6d84e8.tar.bz2 |
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
Diffstat (limited to 'ios')
-rw-r--r-- | ios/ios_tests.gyp | 24 | ||||
-rw-r--r-- | ios/public/test/fake_profile_oauth2_token_service_ios_provider.h | 63 | ||||
-rw-r--r-- | ios/public/test/fake_profile_oauth2_token_service_ios_provider.mm | 91 |
3 files changed, 178 insertions, 0 deletions
diff --git a/ios/ios_tests.gyp b/ios/ios_tests.gyp new file mode 100644 index 0000000..563fe49 --- /dev/null +++ b/ios/ios_tests.gyp @@ -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)', + ], + }, + ], +} diff --git a/ios/public/test/fake_profile_oauth2_token_service_ios_provider.h b/ios/public/test/fake_profile_oauth2_token_service_ios_provider.h new file mode 100644 index 0000000..2ca953a --- /dev/null +++ b/ios/public/test/fake_profile_oauth2_token_service_ios_provider.h @@ -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_ diff --git a/ios/public/test/fake_profile_oauth2_token_service_ios_provider.mm b/ios/public/test/fake_profile_oauth2_token_service_ios_provider.mm new file mode 100644 index 0000000..e6ff73d --- /dev/null +++ b/ios/public/test/fake_profile_oauth2_token_service_ios_provider.mm @@ -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 |