Bladeren bron

Add GIDFakeProfileDataFetcher

pinlu 3 jaren geleden
bovenliggende
commit
c597b5410b

+ 38 - 0
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h

@@ -0,0 +1,38 @@
+/*
+ * Copyright 2022 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#import <Foundation/Foundation.h>
+
+#import "GoogleSignIn/Sources/GIDProfileDataFetcher/API/GIDProfileDataFetcher.h"
+
+@class GIDProfileData;
+
+NS_ASSUME_NONNULL_BEGIN
+
+typedef void (^GIDProfileDataFetcherFakeResponse)(GIDProfileData *_Nullable profileData,
+                                                  NSError *_Nullable error);
+
+typedef void (^GIDProfileDataFetcherTestBlock)(GIDProfileDataFetcherFakeResponse response);
+
+@interface GIDFakeProfileDataFetcher : NSObject <GIDProfileDataFetcher>
+
+/// Set the test block which provides the response value.
+- (void)setTestBlock:(GIDProfileDataFetcherTestBlock)block;
+
+@end
+
+NS_ASSUME_NONNULL_END
+

+ 32 - 0
GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.m

@@ -0,0 +1,32 @@
+#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
+
+#import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDProfileData.h"
+
+#ifdef SWIFT_PACKAGE
+@import AppAuth;
+#else
+#import <AppAuth/AppAuth.h>
+#endif
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface GIDFakeProfileDataFetcher ()
+
+@property(nonatomic) GIDProfileDataFetcherTestBlock testBlock;
+
+@end
+
+@implementation GIDFakeProfileDataFetcher
+
+- (void)fetchProfileDataWithAuthState:(OIDAuthState *)authState
+                           completion:(void (^)(GIDProfileData *_Nullable profileData,
+                                                NSError *_Nullable error))completion {
+  NSAssert(self.testBlock != nil, @"Set the test block before invoking this method.");
+  self.testBlock(^(GIDProfileData *_Nullable profileData, NSError *_Nullable error){
+    completion(profileData,error);
+  });
+}
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 6 - 3
GoogleSignIn/Sources/GIDSignIn.m

@@ -458,12 +458,15 @@ static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
 - (id)initPrivate {
   id<GIDKeychainHandler> keychainHandler = [[GIDKeychainHandler alloc] init];
   id<GIDDataFetcher> dataFetcher = [[GIDDataFetcher alloc] init];
+  id<GIDProfileDataFetcher> profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
   return [self initWithKeychainHandler:keychainHandler
-                           dataFetcher:dataFetcher];
+                           dataFetcher:dataFetcher
+                    profileDataFetcher:profileDataFetcher];
 }
 
 - (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler
-                            dataFetcher:(id<GIDDataFetcher>)dataFetcher{
+                            dataFetcher:(id<GIDDataFetcher>)dataFetcher
+                     profileDataFetcher:(id<GIDProfileDataFetcher>)profoleDataFetcher{
   self = [super init];
   if (self) {
     // Get the bundle of the current executable.
@@ -500,7 +503,7 @@ static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
     
     _keychainHandler = keychainHandler;
     
-    _profileDataFetcher = [[GIDProfileDataFetcher alloc] init];
+    _profileDataFetcher = profoleDataFetcher;
 
     _dataFetcher = dataFetcher;
   }

+ 2 - 0
GoogleSignIn/Sources/GIDSignIn_Private.h

@@ -31,6 +31,7 @@ NS_ASSUME_NONNULL_BEGIN
 
 @protocol GIDDataFetcher;
 @protocol GIDKeychainHandler;
+@protocol GIDProfileDataFetcher;
 
 /// Represents a completion block that takes a `GIDSignInResult` on success or an error if the
 /// operation was unsuccessful.
@@ -52,6 +53,7 @@ typedef void (^GIDDisconnectCompletion)(NSError *_Nullable error);
 /// The designated initializer.
 - (instancetype)initWithKeychainHandler:(id<GIDKeychainHandler>)keychainHandler
                             dataFetcher:(id<GIDDataFetcher>)dataFetcher
+                     profileDataFetcher:(id<GIDProfileDataFetcher>)profoleDataFetcher
     NS_DESIGNATED_INITIALIZER;
 
 /// Authenticates with extra options.

+ 8 - 2
GoogleSignIn/Tests/Unit/GIDSignInTest.m

@@ -32,7 +32,7 @@
 #import "GoogleSignIn/Sources/GIDSignInPreferences.h"
 #import "GoogleSignIn/Sources/GIDKeychainHandler/Implementations/Fakes/GIDFakeKeychainHandler.h"
 #import "GoogleSignIn/Sources/GIDDataFetcher/Implementations/Fakes/GIDFakeDataFetcher.h"
-#import "GoogleSignIn/Sources/GIDDataFetcher/Implementations/GIDDataFetcher.h"
+#import "GoogleSignIn/Sources/GIDProfileDataFetcher/Implementations/Fakes/GIDFakeProfileDataFetcher.h"
 
 #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
 #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
@@ -200,6 +200,9 @@ static NSString *const kNewScope = @"newScope";
   // Fake for |GIDDataFetcher|.
   GIDFakeDataFetcher *_dataFetcher;
   
+  // Fake for |GIDProfileDataFetcher|.
+  GIDFakeProfileDataFetcher *_profileDataFetcher;
+  
 #if TARGET_OS_IOS || TARGET_OS_MACCATALYST
   // Mock |UIViewController|.
   id _presentingViewController;
@@ -311,8 +314,11 @@ static NSString *const kNewScope = @"newScope";
   
   _dataFetcher = [[GIDFakeDataFetcher alloc] init];
   
+  _profileDataFetcher = [[GIDFakeProfileDataFetcher alloc] init];
+  
   _signIn = [[GIDSignIn alloc] initWithKeychainHandler:_keychainHandler
-                                           dataFetcher:_dataFetcher]; 
+                                           dataFetcher:_dataFetcher
+                                    profileDataFetcher:_profileDataFetcher]; 
   _hint = nil;
 
   __weak GIDSignInTest *weakSelf = self;