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 "components/signin/ios/browser/fake_profile_oauth2_token_service_ios_provider.h"
7 #include <Foundation/Foundation.h>
9 #include "base/logging.h"
10 #include "base/strings/sys_string_conversions.h"
12 FakeProfileOAuth2TokenServiceIOSProvider::
13 FakeProfileOAuth2TokenServiceIOSProvider() {
16 FakeProfileOAuth2TokenServiceIOSProvider::
17 ~FakeProfileOAuth2TokenServiceIOSProvider() {
20 void FakeProfileOAuth2TokenServiceIOSProvider::GetAccessToken(
21 const std::string& account_id,
22 const std::string& client_id,
23 const std::string& client_secret,
24 const std::set<std::string>& scopes,
25 const AccessTokenCallback& callback) {
26 requests_.push_back(AccessTokenRequest(account_id, callback));
29 std::vector<ProfileOAuth2TokenServiceIOSProvider::AccountInfo>
30 FakeProfileOAuth2TokenServiceIOSProvider::GetAllAccounts() const {
34 ProfileOAuth2TokenServiceIOSProvider::AccountInfo
35 FakeProfileOAuth2TokenServiceIOSProvider::GetAccountInfoForEmail(
36 const std::string& email) const {
37 for (const auto& account : accounts_) {
38 if (account.email == email)
41 return ProfileOAuth2TokenServiceIOSProvider::AccountInfo();
44 ProfileOAuth2TokenServiceIOSProvider::AccountInfo
45 FakeProfileOAuth2TokenServiceIOSProvider::GetAccountInfoForGaia(
46 const std::string& gaia) const {
47 for (const auto& account : accounts_) {
48 if (account.gaia == gaia)
51 return ProfileOAuth2TokenServiceIOSProvider::AccountInfo();
54 ProfileOAuth2TokenServiceIOSProvider::AccountInfo
55 FakeProfileOAuth2TokenServiceIOSProvider::AddAccount(const std::string& gaia,
56 const std::string& email) {
57 ProfileOAuth2TokenServiceIOSProvider::AccountInfo account;
59 account.email = email;
60 accounts_.push_back(account);
64 void FakeProfileOAuth2TokenServiceIOSProvider::ClearAccounts() {
68 void FakeProfileOAuth2TokenServiceIOSProvider::
69 IssueAccessTokenForAllRequests() {
70 for (auto i = requests_.begin(); i != requests_.end(); ++i) {
71 std::string account_id = i->first;
72 AccessTokenCallback callback = i->second;
73 NSString* access_token = [NSString
74 stringWithFormat:@"fake_access_token [account=%s]", account_id.c_str()];
75 NSDate* one_hour_from_now = [NSDate dateWithTimeIntervalSinceNow:3600];
76 callback.Run(access_token, one_hour_from_now, nil);
81 void FakeProfileOAuth2TokenServiceIOSProvider::
82 IssueAccessTokenErrorForAllRequests() {
83 for (auto i = requests_.begin(); i != requests_.end(); ++i) {
84 std::string account_id = i->first;
85 AccessTokenCallback callback = i->second;
86 NSError* error = [[[NSError alloc] initWithDomain:@"fake_access_token_error"
88 userInfo:nil] autorelease];
89 callback.Run(nil, nil, error);
94 AuthenticationErrorCategory
95 FakeProfileOAuth2TokenServiceIOSProvider::GetAuthenticationErrorCategory(
96 NSError* error) const {
98 return kAuthenticationErrorCategoryAuthorizationErrors;