Skip to content

Commit 594be97

Browse files
authored
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.
1 parent 29fff8e commit 594be97

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

FirebaseDynamicLinks/Sources/FIRDynamicLinks.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,7 @@ - (BOOL)handleIncomingCustomSchemeDeepLink:(NSURL *)url {
510510

511511
- (void)passRetrievedDynamicLinkToApplication:(NSURL *)url {
512512
id<UIApplicationDelegate> applicationDelegate = [UIApplication sharedApplication].delegate;
513-
if (applicationDelegate &&
514-
[applicationDelegate respondsToSelector:@selector(application:openURL:options:)]) {
513+
if ([self isOpenUrlMethodPresentInAppDelegate:applicationDelegate]) {
515514
// pass url directly to application delegate to avoid hop into
516515
// iOS handling of the universal links
517516
[applicationDelegate application:[UIApplication sharedApplication] openURL:url options:@{}];
@@ -521,6 +520,11 @@ - (void)passRetrievedDynamicLinkToApplication:(NSURL *)url {
521520
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
522521
}
523522

523+
- (BOOL)isOpenUrlMethodPresentInAppDelegate:(id<UIApplicationDelegate>)applicationDelegate {
524+
return applicationDelegate &&
525+
[applicationDelegate respondsToSelector:@selector(application:openURL:options:)];
526+
}
527+
524528
- (void)handlePendingDynamicLinkRetrievalFailureWithErrorCode:(NSInteger)errorCode
525529
errorDescription:(NSString *)errorDescription
526530
underlyingError:(nullable NSError *)underlyingError {

FirebaseDynamicLinks/Tests/Unit/FIRDynamicLinksTest.m

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ - (BOOL)setUpWithLaunchOptions:(nullable NSDictionary *)launchOptions
7979
urlScheme:(nullable NSString *)urlScheme
8080
userDefaults:(nullable NSUserDefaults *)userDefaults;
8181
- (BOOL)canParseUniversalLinkURL:(nullable NSURL *)url;
82+
- (void)passRetrievedDynamicLinkToApplication:(NSURL *)url;
83+
- (BOOL)isOpenUrlMethodPresentInAppDelegate:(id<UIApplicationDelegate>)applicationDelegate;
8284
@end
8385

8486
@interface FakeShortLinkResolver : FIRDynamicLinkNetworking
@@ -1119,7 +1121,7 @@ - (void)test_retrievePendingDeepLinkShouldSetkFIRDLOpenURLKeyRegardlessOfFailure
11191121
apiKey:kAPIKey
11201122
urlScheme:nil
11211123
userDefaults:[NSUserDefaults standardUserDefaults]];
1122-
FIRDynamicLinks<FIRDLRetrievalProcessDelegate> *deleagte =
1124+
FIRDynamicLinks<FIRDLRetrievalProcessDelegate> *delegate =
11231125
(FIRDynamicLinks<FIRDLRetrievalProcessDelegate> *)self.service;
11241126

11251127
// Error Result to pass
@@ -1131,13 +1133,36 @@ - (void)test_retrievePendingDeepLinkShouldSetkFIRDLOpenURLKeyRegardlessOfFailure
11311133

11321134
FIRDLDefaultRetrievalProcessV2 *defaultRetrievalProcess = [FIRDLDefaultRetrievalProcessV2 alloc];
11331135

1134-
[deleagte retrievalProcess:defaultRetrievalProcess completedWithResult:result];
1136+
[delegate retrievalProcess:defaultRetrievalProcess completedWithResult:result];
11351137

11361138
NSString *kFIRDLOpenURLKey = @"com.google.appinvite.openURL";
11371139
XCTAssertEqual([[NSUserDefaults standardUserDefaults] boolForKey:kFIRDLOpenURLKey], YES,
11381140
@"kFIRDLOpenURL key should be set regardless of failures");
11391141
}
11401142

1143+
- (void)test_passRetrievedDynamicLinkToApplicationDelegatesProperly {
1144+
// Creating ApplicationDelegate partial mock object.
1145+
id applicationDelegate = OCMPartialMock([UIApplication sharedApplication].delegate);
1146+
// Creating FIRDynamicLinks partial mock object.
1147+
id firebaseDynamicLinks = OCMPartialMock(self.service);
1148+
// Stubbing Application delegate to return YES when application:openURL:options method is called.
1149+
// Not sure why this is required as we are not concerned about its return, but without this, the
1150+
// test will throw NSInvalidArgumentException with message "unrecognized selector sent to
1151+
// instance".
1152+
OCMStub([applicationDelegate application:[OCMArg any] openURL:[OCMArg any] options:[OCMArg any]])
1153+
.andReturn(YES);
1154+
// Stubbing firebase dynamiclinks instance to return YES when isOpenUrlMethodPresentInAppDelegate
1155+
// is called.
1156+
OCMStub([firebaseDynamicLinks isOpenUrlMethodPresentInAppDelegate:[OCMArg any]]).andReturn(YES);
1157+
1158+
// Executing the function with a URL.
1159+
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
1160+
[firebaseDynamicLinks passRetrievedDynamicLinkToApplication:url];
1161+
1162+
// Verifying the application:openURL:options method is called in AppDelegate.
1163+
OCMVerify([applicationDelegate application:[OCMArg any] openURL:url options:[OCMArg any]]);
1164+
}
1165+
11411166
#pragma mark - Self-diagnose tests
11421167

11431168
- (void)testSelfDiagnoseWithNilCompletion {

0 commit comments

Comments
 (0)