forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FakeChromeIdentity and FakeChromeIdentityService.
By using those two fakes, unit tests won't have to depends on the fake of the downstream implementations. This gives a better abstraction and removes one of the dependencies for upstreaming. BUG=626233 Review-Url: https://codereview.chromium.org/2123273003 Cr-Commit-Position: refs/heads/master@{#404372}
- Loading branch information
bzanotti
authored and
Commit bot
committed
Jul 8, 2016
1 parent
dd8383a
commit ba5e239
Showing
7 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
ios/public/provider/chrome/browser/signin/fake_chrome_identity.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2016 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_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_FAKE_CHROME_IDENTITY_H_ | ||
#define IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_FAKE_CHROME_IDENTITY_H_ | ||
|
||
#import "ios/public/provider/chrome/browser/signin/chrome_identity.h" | ||
|
||
// A fake ChromeIdentity used for testing. | ||
@interface FakeChromeIdentity : ChromeIdentity | ||
|
||
// Returns an autoreleased ChromeIdentity based on |email|, |gaiaID| and |name|. | ||
// The |hashedGaiaID| property will be derived from |name|. | ||
+ (FakeChromeIdentity*)identityWithEmail:(NSString*)email | ||
gaiaID:(NSString*)gaiaID | ||
name:(NSString*)name; | ||
|
||
@end | ||
|
||
#endif // IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_FAKE_CHROME_IDENTITY_H_ |
54 changes: 54 additions & 0 deletions
54
ios/public/provider/chrome/browser/signin/fake_chrome_identity.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2016 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. | ||
|
||
#import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" | ||
|
||
#import "base/mac/scoped_nsobject.h" | ||
|
||
@implementation FakeChromeIdentity { | ||
base::scoped_nsobject<NSString> _userEmail; | ||
base::scoped_nsobject<NSString> _gaiaID; | ||
base::scoped_nsobject<NSString> _userFullName; | ||
base::scoped_nsobject<NSString> _hashedGaiaID; | ||
} | ||
|
||
+ (FakeChromeIdentity*)identityWithEmail:(NSString*)email | ||
gaiaID:(NSString*)gaiaID | ||
name:(NSString*)name { | ||
return | ||
[[[FakeChromeIdentity alloc] initWithEmail:email gaiaID:gaiaID name:name] | ||
autorelease]; | ||
} | ||
|
||
- (instancetype)initWithEmail:(NSString*)email | ||
gaiaID:(NSString*)gaiaID | ||
name:(NSString*)name { | ||
self = [super init]; | ||
if (self) { | ||
_userEmail.reset([email copy]); | ||
_gaiaID.reset([gaiaID copy]); | ||
_userFullName.reset([name copy]); | ||
_hashedGaiaID.reset( | ||
[[NSString stringWithFormat:@"%@_hashID", name] retain]); | ||
} | ||
return self; | ||
} | ||
|
||
- (NSString*)userEmail { | ||
return _userEmail; | ||
} | ||
|
||
- (NSString*)gaiaID { | ||
return _gaiaID; | ||
} | ||
|
||
- (NSString*)userFullName { | ||
return _userFullName; | ||
} | ||
|
||
- (NSString*)hashedGaiaID { | ||
return _hashedGaiaID; | ||
} | ||
|
||
@end |
64 changes: 64 additions & 0 deletions
64
ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2016 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_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_FAKE_CHROME_IDENTITY_SERVICE_H_ | ||
#define IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_FAKE_CHROME_IDENTITY_SERVICE_H_ | ||
|
||
#include "ios/public/provider/chrome/browser/signin/chrome_identity_service.h" | ||
|
||
#include "base/mac/scoped_nsobject.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
|
||
@class NSMutableArray; | ||
|
||
namespace ios { | ||
|
||
// A fake ChromeIdentityService used for testing. | ||
class FakeChromeIdentityService : public ChromeIdentityService { | ||
public: | ||
FakeChromeIdentityService(); | ||
~FakeChromeIdentityService(); | ||
|
||
// Convenience method that returns the instance of | ||
// |FakeChromeIdentityService| from the ChromeBrowserProvider. | ||
static FakeChromeIdentityService* GetInstanceFromChromeProvider(); | ||
|
||
// ChromeIdentityService implementation. | ||
bool IsValidIdentity(ChromeIdentity* identity) const override; | ||
ChromeIdentity* GetIdentityWithGaiaID( | ||
const std::string& gaia_id) const override; | ||
bool HasIdentities() const override; | ||
NSArray* GetAllIdentities() const override; | ||
NSArray* GetAllIdentitiesSortedForDisplay() const override; | ||
void ForgetIdentity(ChromeIdentity* identity, | ||
ForgetIdentityCallback callback) override; | ||
|
||
MOCK_METHOD5(GetAccessToken, | ||
void(ChromeIdentity* identity, | ||
const std::string& client_id, | ||
const std::string& client_secret, | ||
const std::set<std::string>& scopes, | ||
const ios::AccessTokenCallback& callback)); | ||
|
||
MOCK_METHOD1(GetMDMDeviceStatus, | ||
ios::MDMDeviceStatus(NSDictionary* user_info)); | ||
|
||
MOCK_METHOD3(HandleMDMNotification, | ||
bool(ChromeIdentity* identity, | ||
NSDictionary* user_info, | ||
ios::MDMStatusCallback callback)); | ||
|
||
// Adds the identities given their name. | ||
void AddIdentities(NSArray* identitiesNames); | ||
|
||
// Adds |identity| to the available identities. | ||
void AddIdentity(ChromeIdentity* identity); | ||
|
||
private: | ||
base::scoped_nsobject<NSMutableArray> identities_; | ||
}; | ||
|
||
} // namespace ios | ||
|
||
#endif // IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_FAKE_CHROME_IDENTITY_SERVICE_H_ |
90 changes: 90 additions & 0 deletions
90
ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2016 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. | ||
|
||
#import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#include "base/strings/sys_string_conversions.h" | ||
#include "ios/public/provider/chrome/browser/chrome_browser_provider.h" | ||
#import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" | ||
|
||
namespace ios { | ||
|
||
NSString* const kIdentityEmailFormat = @"%@@foo.com"; | ||
NSString* const kIdentityGaiaIDFormat = @"%@ID"; | ||
|
||
FakeChromeIdentityService::FakeChromeIdentityService() | ||
: identities_([[NSMutableArray alloc] init]) {} | ||
|
||
FakeChromeIdentityService::~FakeChromeIdentityService() {} | ||
|
||
// static | ||
FakeChromeIdentityService* | ||
FakeChromeIdentityService::GetInstanceFromChromeProvider() { | ||
return static_cast<ios::FakeChromeIdentityService*>( | ||
ios::GetChromeBrowserProvider()->GetChromeIdentityService()); | ||
} | ||
|
||
bool FakeChromeIdentityService::IsValidIdentity( | ||
ChromeIdentity* identity) const { | ||
return [identities_ indexOfObject:identity] != NSNotFound; | ||
} | ||
|
||
ChromeIdentity* FakeChromeIdentityService::GetIdentityWithGaiaID( | ||
const std::string& gaia_id) const { | ||
NSString* gaiaID = base::SysUTF8ToNSString(gaia_id); | ||
NSUInteger index = | ||
[identities_ indexOfObjectPassingTest:^BOOL(ChromeIdentity* obj, | ||
NSUInteger, BOOL* stop) { | ||
return [[obj gaiaID] isEqualToString:gaiaID]; | ||
}]; | ||
if (index == NSNotFound) { | ||
return nil; | ||
} | ||
return [identities_ objectAtIndex:index]; | ||
} | ||
|
||
bool FakeChromeIdentityService::HasIdentities() const { | ||
return [identities_ count] > 0; | ||
} | ||
|
||
NSArray* FakeChromeIdentityService::GetAllIdentities() const { | ||
return identities_; | ||
} | ||
|
||
NSArray* FakeChromeIdentityService::GetAllIdentitiesSortedForDisplay() const { | ||
return identities_; | ||
} | ||
|
||
void FakeChromeIdentityService::ForgetIdentity( | ||
ChromeIdentity* identity, | ||
ForgetIdentityCallback callback) { | ||
[identities_ removeObject:identity]; | ||
FireIdentityListChanged(); | ||
if (callback) { | ||
// Forgetting an identity is normally an asynchronous operation (that | ||
// require some network calls), this is replicated here by dispatching it. | ||
dispatch_async(dispatch_get_main_queue(), ^{ | ||
callback(nil); | ||
}); | ||
} | ||
} | ||
|
||
void FakeChromeIdentityService::AddIdentities(NSArray* identitiesNames) { | ||
for (NSString* name in identitiesNames) { | ||
NSString* email = [NSString stringWithFormat:kIdentityEmailFormat, name]; | ||
NSString* gaiaID = [NSString stringWithFormat:kIdentityGaiaIDFormat, name]; | ||
[identities_ addObject:[FakeChromeIdentity identityWithEmail:email | ||
gaiaID:gaiaID | ||
name:name]]; | ||
} | ||
} | ||
|
||
void FakeChromeIdentityService::AddIdentity(ChromeIdentity* identity) { | ||
[identities_ addObject:identity]; | ||
FireIdentityListChanged(); | ||
} | ||
|
||
} // namespace ios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters