Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / signin / ios / browser / fake_profile_oauth2_token_service_ios_provider.mm
blob3a6e46fe999169d76db7d5be8adefdefc41e9c84
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 {
31   return accounts_;
34 ProfileOAuth2TokenServiceIOSProvider::AccountInfo
35 FakeProfileOAuth2TokenServiceIOSProvider::GetAccountInfoForEmail(
36     const std::string& email) const {
37   for (const auto& account : accounts_) {
38     if (account.email == email)
39       return account;
40   }
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)
49       return account;
50   }
51   return ProfileOAuth2TokenServiceIOSProvider::AccountInfo();
54 ProfileOAuth2TokenServiceIOSProvider::AccountInfo
55 FakeProfileOAuth2TokenServiceIOSProvider::AddAccount(const std::string& gaia,
56                                                      const std::string& email) {
57   ProfileOAuth2TokenServiceIOSProvider::AccountInfo account;
58   account.gaia = gaia;
59   account.email = email;
60   accounts_.push_back(account);
61   return account;
64 void FakeProfileOAuth2TokenServiceIOSProvider::ClearAccounts() {
65   accounts_.clear();
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);
77   }
78   requests_.clear();
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"
87                                                  code:-1
88                                              userInfo:nil] autorelease];
89     callback.Run(nil, nil, error);
90   }
91   requests_.clear();
94 AuthenticationErrorCategory
95 FakeProfileOAuth2TokenServiceIOSProvider::GetAuthenticationErrorCategory(
96     NSError* error) const {
97   DCHECK(error);
98   return kAuthenticationErrorCategoryAuthorizationErrors;