Skip to content

Commit

Permalink
Introduce FakeChromeIdentity and FakeChromeIdentityService.
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ios/provider/ios_provider_chrome.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
'target_name': 'ios_provider_chrome_browser_test_support',
'type': 'static_library',
'sources': [
'../public/provider/chrome/browser/signin/fake_chrome_identity.h',
'../public/provider/chrome/browser/signin/fake_chrome_identity.mm',
'../public/provider/chrome/browser/signin/fake_chrome_identity_service.h',
'../public/provider/chrome/browser/signin/fake_chrome_identity_service.mm',
'../public/provider/chrome/browser/test_chrome_browser_provider.h',
'../public/provider/chrome/browser/test_chrome_browser_provider.mm',
'../public/provider/chrome/browser/test_chrome_provider_initializer.h',
Expand All @@ -61,6 +65,7 @@
'dependencies': [
'../../base/base.gyp:base',
'../../components/components.gyp:signin_ios_browser_test_support',
'../../testing/gmock.gyp:gmock',
'../../testing/gtest.gyp:gtest',
'ios_provider_chrome_browser',
],
Expand Down
5 changes: 5 additions & 0 deletions ios/public/provider/chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ source_set("test_support") {
testonly = true

sources = [
"signin/fake_chrome_identity.h",
"signin/fake_chrome_identity.mm",
"signin/fake_chrome_identity_service.h",
"signin/fake_chrome_identity_service.mm",
"test_chrome_browser_provider.h",
"test_chrome_browser_provider.mm",
"test_chrome_provider_initializer.h",
Expand All @@ -56,6 +60,7 @@ source_set("test_support") {
":browser",
"//base",
"//components/signin/ios/browser:test_support",
"//testing/gmock",
"//testing/gtest",
]
}
21 changes: 21 additions & 0 deletions ios/public/provider/chrome/browser/signin/fake_chrome_identity.h
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 ios/public/provider/chrome/browser/signin/fake_chrome_identity.mm
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
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_
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "ios/public/provider/chrome/browser/test_chrome_browser_provider.h"

#include "base/logging.h"
#include "ios/public/provider/chrome/browser/signin/chrome_identity_service.h"
#include "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.h"
#import "ios/public/provider/chrome/browser/test_updatable_resource_provider.h"

namespace ios {

TestChromeBrowserProvider::TestChromeBrowserProvider()
: chrome_identity_service_(new ios::ChromeIdentityService),
: chrome_identity_service_(new ios::FakeChromeIdentityService),
test_updatable_resource_provider_(new TestUpdatableResourceProvider) {}

TestChromeBrowserProvider::~TestChromeBrowserProvider() {}
Expand Down

0 comments on commit ba5e239

Please sign in to comment.