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