Skip to content

Commit c597b54

Browse files
committed
Add GIDFakeProfileDataFetcher
1 parent 5363b40 commit c597b54

5 files changed

Lines changed: 86 additions & 5 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h"
20+
21+
@class GIDProfileData;
22+
23+
NS_ASSUME_NONNULL_BEGIN
24+
25+
typedef void (^GIDProfileDataFetcherFakeResponse)(GIDProfileData *_Nullable profileData,
26+
NSError *_Nullable error);
27+
28+
typedef void (^GIDProfileDataFetcherTestBlock)(GIDProfileDataFetcherFakeResponse response);
29+
30+
@interface GIDFakeProfileDataFetcher : NSObject <GIDProfileDataFetcher>
31+
32+
/// Set the test block which provides the response value.
33+
- (void)setTestBlock:(GIDProfileDataFetcherTestBlock)block;
34+
35+
@end
36+
37+
NS_ASSUME_NONNULL_END
38+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
2+
3+
#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
4+
5+
#ifdef SWIFT_PACKAGE
6+
@import AppAuth;
7+
#else
8+
#import <AppAuth/AppAuth.h>
9+
#endif
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface GIDFakeProfileDataFetcher ()
14+
15+
@property(nonatomic) GIDProfileDataFetcherTestBlock testBlock;
16+
17+
@end
18+
19+
@implementation GIDFakeProfileDataFetcher
20+
21+
- (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState
22+
completion:(void (^)(GIDProfileData *_Nullable profileData,
23+
NSError *_Nullable error))completion {
24+
NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
25+
self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error){
26+
completion(profileData,error);
27+
});
28+
}
29+
30+
@end
31+
32+
NS_ASSUME_NONNULL_END

GoogleSignIn/Sources/GIDSignIn.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,12 +458,15 @@ + (GIDSignIn *)sharedInstance {
458458
- (id)initPrivate {
459459
id<GIDKeychainHandler> keychainHandler = [[GIDKeychainHandler alloc] init];
460460
id<GIDDataFetcher> dataFetcher = [[GIDDataFetcher alloc] init];
461+
id<GIDProfileDataFetcher> profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
461462
return [self initWithKeychainHandler:keychainHandler
462-
dataFetcher:dataFetcher];
463+
dataFetcher:dataFetcher
464+
profileDataFetcher:profileDataFetcher];
463465
}
464466

465467
- (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler
466-
dataFetcher:(id<GIDDataFetcher>)dataFetcher{
468+
dataFetcher:(id<GIDDataFetcher>)dataFetcher
469+
profileDataFetcher:(id<GIDProfileDataFetcher>)profoleDataFetcher{
467470
self = [super init];
468471
if (self) {
469472
// Get the bundle of the current executable.
@@ -500,7 +503,7 @@ - (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler
500503

501504
_keychainHandler = keychainHandler;
502505

503-
_profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
506+
_profileDataFetcher = profoleDataFetcher;
504507

505508
_dataFetcher = dataFetcher;
506509
}

GoogleSignIn/Sources/GIDSignIn_Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
3131

3232
@protocol GIDDataFetcher;
3333
@protocol GIDKeychainHandler;
34+
@protocol GIDProfileDataFetcher;
3435

3536
/// Represents a completion block that takes a `GIDSignInResult` on success or an error if the
3637
/// operation was unsuccessful.
@@ -52,6 +53,7 @@ typedef void (^GIDDisconnectCompletion)(NSError *_Nullable error);
5253
/// The designated initializer.
5354
- (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler
5455
dataFetcher:(id<GIDDataFetcher>)dataFetcher
56+
profileDataFetcher:(id<GIDProfileDataFetcher>)profoleDataFetcher
5557
NS_DESIGNATED_INITIALIZER;
5658

5759
/// Authenticates with extra options.

GoogleSignIn/Tests/Unit/GIDSignInTest.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#import "GoogleSignIn/Sources/GIDSignInPreferences.h"
3333
#import "GoogleSignIn/Sources/GIDKeychainHandler/Implementations/Fakes/GIDFakeKeychainHandler.h"
3434
#import "GoogleSignIn/Sources/GIDDataFetcher/Implementations/Fakes/GIDFakeDataFetcher.h"
35-
#import "GoogleSignIn/Sources/GIDDataFetcher/Implementations/GIDDataFetcher.h"
35+
#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
3636

3737
#if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
3838
#import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
@@ -200,6 +200,9 @@ @interface GIDSignInTest : XCTestCase {
200200
// Fake for |GIDDataFetcher|.
201201
GIDFakeDataFetcher *_dataFetcher;
202202

203+
// Fake for |GIDProfileDataFetcher|.
204+
GIDFakeProfileDataFetcher *_profileDataFetcher;
205+
203206
#if TARGET_OS_IOS || TARGET_OS_MACCATALYST
204207
// Mock |UIViewController|.
205208
id _presentingViewController;
@@ -311,8 +314,11 @@ - (void)setUp {
311314

312315
_dataFetcher = [[GIDFakeDataFetcher alloc] init];
313316

317+
_profileDataFetcher = [[GIDFakeProfileDataFetcher alloc] init];
318+
314319
_signIn = [[GIDSignIn alloc] initWithKeychainHandler:_keychainHandler
315-
dataFetcher:_dataFetcher];
320+
dataFetcher:_dataFetcher
321+
profileDataFetcher:_profileDataFetcher];
316322
_hint = nil;
317323

318324
__weak GIDSignInTest *weakSelf = self;

0 commit comments

Comments
 (0)