Jelajahi Sumber

Adding test to make sure pending dynamic link is getting passed to App delegate method. (#6819)

Adding unit test to make sure the pending dynamic link is getting delivered to specific method in App delegate.
Eldhose M Babu 5 tahun lalu
induk
melakukan
594be97236

+ 6 - 2
FirebaseDynamicLinks/Sources/FIRDynamicLinks.m

@@ -510,8 +510,7 @@ static const NSInteger FIRErrorCodeDurableDeepLinkFailed = -119;
 
 - (void)passRetrievedDynamicLinkToApplication:(NSURL *)url {
   id<UIApplicationDelegate> applicationDelegate = [UIApplication sharedApplication].delegate;
-  if (applicationDelegate &&
-      [applicationDelegate respondsToSelector:@selector(application:openURL:options:)]) {
+  if ([self isOpenUrlMethodPresentInAppDelegate:applicationDelegate]) {
     // pass url directly to application delegate to avoid hop into
     // iOS handling of the universal links
     [applicationDelegate application:[UIApplication sharedApplication] openURL:url options:@{}];
@@ -521,6 +520,11 @@ static const NSInteger FIRErrorCodeDurableDeepLinkFailed = -119;
   [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
 }
 
+- (BOOL)isOpenUrlMethodPresentInAppDelegate:(id<UIApplicationDelegate>)applicationDelegate {
+  return applicationDelegate &&
+         [applicationDelegate respondsToSelector:@selector(application:openURL:options:)];
+}
+
 - (void)handlePendingDynamicLinkRetrievalFailureWithErrorCode:(NSInteger)errorCode
                                              errorDescription:(NSString *)errorDescription
                                               underlyingError:(nullable NSError *)underlyingError {

+ 27 - 2
FirebaseDynamicLinks/Tests/Unit/FIRDynamicLinksTest.m

@@ -79,6 +79,8 @@ typedef NSURL * (^FakeShortLinkResolverHandler)(NSURL *shortLink);
                      urlScheme:(nullable NSString *)urlScheme
                   userDefaults:(nullable NSUserDefaults *)userDefaults;
 - (BOOL)canParseUniversalLinkURL:(nullable NSURL *)url;
+- (void)passRetrievedDynamicLinkToApplication:(NSURL *)url;
+- (BOOL)isOpenUrlMethodPresentInAppDelegate:(id<UIApplicationDelegate>)applicationDelegate;
 @end
 
 @interface FakeShortLinkResolver : FIRDynamicLinkNetworking
@@ -1119,7 +1121,7 @@ static NSString *const kInfoPlistCustomDomainsKey = @"FirebaseDynamicLinksCustom
                                 apiKey:kAPIKey
                              urlScheme:nil
                           userDefaults:[NSUserDefaults standardUserDefaults]];
-  FIRDynamicLinks<FIRDLRetrievalProcessDelegate> *deleagte =
+  FIRDynamicLinks<FIRDLRetrievalProcessDelegate> *delegate =
       (FIRDynamicLinks<FIRDLRetrievalProcessDelegate> *)self.service;
 
   // Error Result to pass
@@ -1131,13 +1133,36 @@ static NSString *const kInfoPlistCustomDomainsKey = @"FirebaseDynamicLinksCustom
 
   FIRDLDefaultRetrievalProcessV2 *defaultRetrievalProcess = [FIRDLDefaultRetrievalProcessV2 alloc];
 
-  [deleagte retrievalProcess:defaultRetrievalProcess completedWithResult:result];
+  [delegate retrievalProcess:defaultRetrievalProcess completedWithResult:result];
 
   NSString *kFIRDLOpenURLKey = @"com.google.appinvite.openURL";
   XCTAssertEqual([[NSUserDefaults standardUserDefaults] boolForKey:kFIRDLOpenURLKey], YES,
                  @"kFIRDLOpenURL key should be set regardless of failures");
 }
 
+- (void)test_passRetrievedDynamicLinkToApplicationDelegatesProperly {
+  // Creating ApplicationDelegate partial mock object.
+  id applicationDelegate = OCMPartialMock([UIApplication sharedApplication].delegate);
+  // Creating FIRDynamicLinks partial mock object.
+  id firebaseDynamicLinks = OCMPartialMock(self.service);
+  // Stubbing Application delegate to return YES when application:openURL:options method is called.
+  // Not sure why this is required as we are not concerned about its return, but without this, the
+  // test will throw NSInvalidArgumentException with message "unrecognized selector sent to
+  // instance".
+  OCMStub([applicationDelegate application:[OCMArg any] openURL:[OCMArg any] options:[OCMArg any]])
+      .andReturn(YES);
+  // Stubbing firebase dynamiclinks instance to return YES when isOpenUrlMethodPresentInAppDelegate
+  // is called.
+  OCMStub([firebaseDynamicLinks isOpenUrlMethodPresentInAppDelegate:[OCMArg any]]).andReturn(YES);
+
+  // Executing the function with a URL.
+  NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
+  [firebaseDynamicLinks passRetrievedDynamicLinkToApplication:url];
+
+  // Verifying the application:openURL:options method is called in AppDelegate.
+  OCMVerify([applicationDelegate application:[OCMArg any] openURL:url options:[OCMArg any]]);
+}
+
 #pragma mark - Self-diagnose tests
 
 - (void)testSelfDiagnoseWithNilCompletion {