OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.
h" | 5 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.
h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
| 9 #include "base/mac/scoped_block.h" |
9 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "google_apis/gaia/gaia_auth_util.h" |
10 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | 12 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
11 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" | 13 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" |
| 14 #include "ios/public/provider/chrome/browser/signin/signin_resources_provider.h" |
| 15 |
| 16 using ::testing::_; |
| 17 using ::testing::Invoke; |
| 18 |
| 19 namespace { |
| 20 |
| 21 void FakeGetAccessToken(ChromeIdentity*, |
| 22 const std::string&, |
| 23 const std::string&, |
| 24 const std::set<std::string>&, |
| 25 const ios::AccessTokenCallback& callback) { |
| 26 base::mac::ScopedBlock<ios::AccessTokenCallback> safe_callback( |
| 27 [callback copy]); |
| 28 |
| 29 // |GetAccessToken| is normally an asynchronous operation (that requires some |
| 30 // network calls), this is replicated here by dispatching it. |
| 31 dispatch_async(dispatch_get_main_queue(), ^{ |
| 32 // Token and expiration date. It should be larger than typical test |
| 33 // execution because tests usually setup mock to expect one token request |
| 34 // and then rely on access token being served from cache. |
| 35 NSTimeInterval expiration = 60.0; |
| 36 NSDate* expiresDate = [NSDate dateWithTimeIntervalSinceNow:expiration]; |
| 37 NSString* token = [expiresDate description]; |
| 38 |
| 39 safe_callback.get()(token, expiresDate, nil); |
| 40 }); |
| 41 } |
| 42 |
| 43 UIImage* FakeGetCachedAvatarForIdentity(ChromeIdentity*) { |
| 44 ios::SigninResourcesProvider* provider = |
| 45 ios::GetChromeBrowserProvider()->GetSigninResourcesProvider(); |
| 46 return provider ? provider->GetDefaultAvatar() : nil; |
| 47 } |
| 48 |
| 49 void FakeGetAvatarForIdentity(ChromeIdentity* identity, |
| 50 ios::GetAvatarCallback callback) { |
| 51 // |GetAvatarForIdentity| is normally an asynchronous operation, this is |
| 52 // replicated here by dispatching it. |
| 53 dispatch_async(dispatch_get_main_queue(), ^{ |
| 54 callback(FakeGetCachedAvatarForIdentity(identity)); |
| 55 }); |
| 56 } |
| 57 |
| 58 void FakeGetHostedDomainForIdentity(ChromeIdentity* identity, |
| 59 ios::GetHostedDomainCallback callback) { |
| 60 NSString* domain = base::SysUTF8ToNSString(gaia::ExtractDomainName( |
| 61 gaia::CanonicalizeEmail(base::SysNSStringToUTF8(identity.userEmail)))); |
| 62 |
| 63 // |GetHostedDomainForIdentity| is normally an asynchronous operation , this |
| 64 // is replicated here by dispatching it. |
| 65 dispatch_async(dispatch_get_main_queue(), ^{ |
| 66 callback(domain, nil); |
| 67 }); |
| 68 } |
| 69 } |
12 | 70 |
13 namespace ios { | 71 namespace ios { |
14 | 72 |
15 NSString* const kIdentityEmailFormat = @"%@@foo.com"; | 73 NSString* const kIdentityEmailFormat = @"%@@foo.com"; |
16 NSString* const kIdentityGaiaIDFormat = @"%@ID"; | 74 NSString* const kIdentityGaiaIDFormat = @"%@ID"; |
17 | 75 |
18 FakeChromeIdentityService::FakeChromeIdentityService() | 76 FakeChromeIdentityService::FakeChromeIdentityService() |
19 : identities_([[NSMutableArray alloc] init]) {} | 77 : identities_([[NSMutableArray alloc] init]) {} |
20 | 78 |
21 FakeChromeIdentityService::~FakeChromeIdentityService() {} | 79 FakeChromeIdentityService::~FakeChromeIdentityService() {} |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 FireIdentityListChanged(); | 123 FireIdentityListChanged(); |
66 if (callback) { | 124 if (callback) { |
67 // Forgetting an identity is normally an asynchronous operation (that | 125 // Forgetting an identity is normally an asynchronous operation (that |
68 // require some network calls), this is replicated here by dispatching it. | 126 // require some network calls), this is replicated here by dispatching it. |
69 dispatch_async(dispatch_get_main_queue(), ^{ | 127 dispatch_async(dispatch_get_main_queue(), ^{ |
70 callback(nil); | 128 callback(nil); |
71 }); | 129 }); |
72 } | 130 } |
73 } | 131 } |
74 | 132 |
| 133 void FakeChromeIdentityService::SetUpForIntegrationTests() { |
| 134 ON_CALL(*this, GetAccessToken(_, _, _, _, _)) |
| 135 .WillByDefault(Invoke(FakeGetAccessToken)); |
| 136 |
| 137 ON_CALL(*this, GetAvatarForIdentity(_, _)) |
| 138 .WillByDefault(Invoke(FakeGetAvatarForIdentity)); |
| 139 |
| 140 ON_CALL(*this, GetCachedAvatarForIdentity(_)) |
| 141 .WillByDefault(Invoke(FakeGetCachedAvatarForIdentity)); |
| 142 |
| 143 ON_CALL(*this, GetHostedDomainForIdentity(_, _)) |
| 144 .WillByDefault(Invoke(FakeGetHostedDomainForIdentity)); |
| 145 } |
| 146 |
75 void FakeChromeIdentityService::AddIdentities(NSArray* identitiesNames) { | 147 void FakeChromeIdentityService::AddIdentities(NSArray* identitiesNames) { |
76 for (NSString* name in identitiesNames) { | 148 for (NSString* name in identitiesNames) { |
77 NSString* email = [NSString stringWithFormat:kIdentityEmailFormat, name]; | 149 NSString* email = [NSString stringWithFormat:kIdentityEmailFormat, name]; |
78 NSString* gaiaID = [NSString stringWithFormat:kIdentityGaiaIDFormat, name]; | 150 NSString* gaiaID = [NSString stringWithFormat:kIdentityGaiaIDFormat, name]; |
79 [identities_ addObject:[FakeChromeIdentity identityWithEmail:email | 151 [identities_ addObject:[FakeChromeIdentity identityWithEmail:email |
80 gaiaID:gaiaID | 152 gaiaID:gaiaID |
81 name:name]]; | 153 name:name]]; |
82 } | 154 } |
83 } | 155 } |
84 | 156 |
85 void FakeChromeIdentityService::AddIdentity(ChromeIdentity* identity) { | 157 void FakeChromeIdentityService::AddIdentity(ChromeIdentity* identity) { |
86 [identities_ addObject:identity]; | 158 [identities_ addObject:identity]; |
87 FireIdentityListChanged(); | 159 FireIdentityListChanged(); |
88 } | 160 } |
89 | 161 |
90 } // namespace ios | 162 } // namespace ios |
OLD | NEW |