Files
firebase-ios-sdk/Example/DynamicLinks/Tests/FIRDynamicLinksTest.m
T

1034 lines
43 KiB
Objective-C

/*
* Copyright 2018 Google
*
* 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 <XCTest/XCTest.h>
#import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
#import <FirebaseCore/FIRApp.h>
#import <FirebaseCore/FIROptions.h>
#import <GoogleUtilities/GULSwizzler.h>
#import "DynamicLinks/FIRDLRetrievalProcessFactory.h"
#import "DynamicLinks/FIRDynamicLink+Private.h"
#import "DynamicLinks/FIRDynamicLinkNetworking+Private.h"
#import "DynamicLinks/FIRDynamicLinks+FirstParty.h"
#import "DynamicLinks/FIRDynamicLinks+Private.h"
#import "DynamicLinks/Utilities/FDLUtilities.h"
#import "OCMock.h"
static NSString *const kAPIKey = @"myAPIKey";
static NSString *const kClientID = @"myClientID.apps.googleusercontent.com";
static NSString *const kStructuredLinkFmtFreeform = @"%@://google/link/%@";
static NSString *const kStructuredLinkFmtDeepLink = @"%@://google/link/?deep_link_id=%@";
static NSString *const kStructuredLinkFmtInvitation = @"%@://google/link/?invitation_id=%@";
static NSString *const kStructuredLinkFmtInvitationWeak =
@"%@://google/link/?invitation_id=%@&match_type=weak";
static NSString *const kStructuredLinkFmtDeepLinkAndInvitation =
@"%@://google/link/?deep_link_id=%@&invitation_id=%@";
static NSString *const kStructuredUniversalLinkFmtFreeForm = @"https://goo.gl/app/sample%@";
static NSString *const kStructuredUniversalLinkFmtDeepLink =
@"https://goo.gl/app/sample?link=%@&isi=585027354";
static NSString *const kStructuredUniversalLinkFmtSubdomain = @"https://sample.page.link%@";
static NSString *const kStructuredUniversalLinkFmtSubdomainDeepLink =
@"https://sample.page.link?link=%@&isi=585027354";
static NSString *const kURLScheme = @"gindeeplinkurl";
static const NSTimeInterval kAsyncTestTimout = 0.5;
/**
* This string was generated by percent-encoding the Tactile URL for the Tokyo American Club in
* Tokyo, and then replacing a '%2B' with a '+' to verify that the '+' does not cause our parsing to
* fail and double-encoding one value to verify that only one decoding pass is run.
*/
NSString *kEncodedComplicatedURLString =
@"https%3A%2F%2Fwww.google.com%252Fmaps%2Fplace%2FTokyo+Am"
@"erican%2BClub%2F%4035.658578%2C139.741588%2C3a%2C75y%2C90t%2Fdata%3D%213m8%211e2%213m6%211s42"
@"66698%212e1%213e10%216s%252F%252Fstorage.googleapis.com%252Fstatic.panoramio.com%252Fphotos%2"
@"52Fmedium%252F4266698.jpg%217i640%218i480%214m2%213m1%211s0x0000000000000000%3A0x1b8b8130c791"
@"48e1%216m1%211e1";
/** This string was generated by percent-decoding kEncodedComplicatedURLString. */
NSString *kDecodedComplicatedURLString =
@"https://www.google.com%2Fmaps/place/Tokyo+American+Club/"
@"@35.658578,139.741588,3a,75y,90t/data=!3m8!1e2!3m6!1s4266698!2e1!3e10!6s%2F%2Fstorage.googlea"
@"pis.com%2Fstatic.panoramio.com%2Fphotos%2Fmedium%2F4266698.jpg!7i640!8i480!4m2!3m1!1s0x000000"
@"0000000000:0x1b8b8130c79148e1!6m1!1e1";
static void *kOpenURLHandlerKey = &kOpenURLHandlerKey;
typedef NSURL * (^FakeShortLinkResolverHandler)(NSURL *shortLink);
@interface FIRDynamicLinks (FIRApp)
- (void)configureDynamicLinks:(FIRApp *)app;
- (BOOL)setUpWithLaunchOptions:(nullable NSDictionary *)launchOptions
apiKey:(NSString *)apiKey
clientID:(NSString *)clientID
urlScheme:(nullable NSString *)urlScheme
userDefaults:(nullable NSUserDefaults *)userDefaults;
@end
@interface FakeShortLinkResolver : FIRDynamicLinkNetworking
+ (instancetype)resolverWithBlock:(FakeShortLinkResolverHandler)resolverHandler;
@end
@implementation FakeShortLinkResolver {
FakeShortLinkResolverHandler _resolverHandler;
}
+ (instancetype)resolverWithBlock:(FakeShortLinkResolverHandler)resolverHandler {
FakeShortLinkResolver *resolver = [[self alloc] init];
resolver->_resolverHandler = [resolverHandler copy];
return resolver;
}
- (void)resolveShortLink:(NSURL *)url completion:(FIRDynamicLinkResolverHandler)completion {
if (_resolverHandler && completion) {
NSURL *resolvedLink = _resolverHandler(url);
completion(resolvedLink, nil);
}
}
@end
// dummy protocol to prevent compile warning
@protocol DummyProtocol <NSObject>
@property(atomic, assign) BOOL retrievingPendingDynamicLink;
@property(nonatomic, readonly) FIRDynamicLinkNetworking *dynamicLinkNetworking;
- (void)handlePendingDynamicLinkRetrievalFailureWithErrorCode:(NSInteger)errorCode
errorDescription:(NSString *)errorDescription
underlyingError:(nullable NSError *)underlyingError;
@end
// Swizzle DynamicLinks.dynamicLinkNetworking property to return fake resolver.
static void SwizzleDynamicLinkNetworking(id linkResolver) {
id (^dynamicLinkNetworkingBlock)(void) = ^id(void) {
return linkResolver;
};
[GULSwizzler swizzleClass:[FIRDynamicLinks class]
selector:@selector(dynamicLinkNetworking)
isClassSelector:NO
withBlock:dynamicLinkNetworkingBlock];
}
static void SwizzleDynamicLinkNetworkingWithMock() {
id linkResolver = OCMPartialMock([[FIRDynamicLinkNetworking alloc] initWithAPIKey:kAPIKey
clientID:kClientID
URLScheme:kURLScheme]);
[[linkResolver stub] resolveShortLink:OCMOCK_ANY completion:OCMOCK_ANY];
SwizzleDynamicLinkNetworking(linkResolver);
}
static void UnswizzleDynamicLinkNetworking() {
[GULSwizzler unswizzleClass:[FIRDynamicLinks class]
selector:@selector(dynamicLinkNetworking)
isClassSelector:NO];
}
@interface FIRDynamicLinksTest : XCTestCase
// An instance of |GINDurableDeepLinkService| used for testing.
@property(nonatomic, strong) FIRDynamicLinks *service;
// An instance of |NSUserDefaults| that have all default values removed.
@property(nonatomic, strong) NSUserDefaults *userDefaults;
// FIRAnalytics mock. Necessary because we don't call [FIRAPP configure].
@property(nonatomic, strong) id analytics;
@end
@implementation FIRDynamicLinksTest
// Disable deprecated warning for internal methods.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#pragma mark - Test lifecycle
- (void)setUp {
[super setUp];
self.service = [[FIRDynamicLinks alloc] init];
self.userDefaults = [[NSUserDefaults alloc] init];
[self.userDefaults removePersistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]];
self.analytics = OCMProtocolMock(@protocol(FIRAnalyticsInterop));
}
- (void)tearDown {
self.service = nil;
self.userDefaults = nil;
[self.analytics stopMocking];
self.analytics = nil;
[super tearDown];
}
#pragma mark - Set Up.
- (void)testURLScheme_AllNil {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
BOOL setUpSucceed = [self.service setUpWithLaunchOptions:nil
apiKey:nil
clientID:nil
urlScheme:nil
userDefaults:nil];
#pragma clang diagnostic pop
XCTAssertFalse(setUpSucceed, @"Should fail when apiKey and clientID are both nil.");
}
- (void)testURLScheme_NoApiKey {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
BOOL setUpSucceed = [self.service setUpWithLaunchOptions:nil
apiKey:nil
clientID:kClientID
urlScheme:nil
userDefaults:nil];
#pragma clang diagnostic pop
XCTAssertFalse(setUpSucceed, @"Should fail when apiKey is nil.");
}
- (void)testURLScheme_NoClientID {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
BOOL setUpSucceed = [self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:nil
urlScheme:nil
userDefaults:nil];
#pragma clang diagnostic pop
XCTAssertFalse(setUpSucceed, @"Should fail when clientID is nil.");
}
- (void)testURLScheme_MinimumParameters {
BOOL setUpSucceed = [self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:nil
userDefaults:nil];
XCTAssertTrue(setUpSucceed, @"Should not fail when both apiKey and clientID are set.");
}
- (void)testFactoryMethodReturnsProperClassObject {
[FIRApp configure];
id service = [FIRDynamicLinks dynamicLinks];
XCTAssertNotNil(service, @"Factory method returned nil");
XCTAssertEqualObjects([service class], [FIRDynamicLinks class],
@"Factory returned incorrect class object");
}
- (void)testURLScheme_LaunchOptionsWithCustomSchemeURL {
NSString *deepLinkString =
[NSString stringWithFormat:kStructuredLinkFmtDeepLink,
[[NSBundle mainBundle] bundleIdentifier], @"abc123"];
NSDictionary *launchOptions =
@{UIApplicationLaunchOptionsURLKey : [NSURL URLWithString:deepLinkString]};
[self.userDefaults setBool:NO forKey:kFIRDLReadDeepLinkAfterInstallKey];
[self.service setUpWithLaunchOptions:launchOptions
apiKey:kAPIKey
clientID:kClientID
urlScheme:nil
userDefaults:self.userDefaults];
XCTAssertTrue([self.userDefaults boolForKey:kFIRDLReadDeepLinkAfterInstallKey]);
}
- (void)testURLScheme_LaunchOptionsWithUniversalLinkURL {
NSString *deepLinkString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtDeepLink, @"abc123"];
NSDictionary *launchOptions =
@{UIApplicationLaunchOptionsURLKey : [NSURL URLWithString:deepLinkString]};
[self.userDefaults setBool:NO forKey:kFIRDLReadDeepLinkAfterInstallKey];
[self.service setUpWithLaunchOptions:launchOptions
apiKey:kAPIKey
clientID:kClientID
urlScheme:nil
userDefaults:self.userDefaults];
XCTAssertTrue([self.userDefaults boolForKey:kFIRDLReadDeepLinkAfterInstallKey]);
}
- (void)testURLScheme_LaunchOptionsWithInvalidURLWillNotResetUserDefaultsFlag {
NSDictionary *launchOptions =
@{UIApplicationLaunchOptionsURLKey : [NSURL URLWithString:@"https://www.google.com"]};
[self.userDefaults setBool:YES forKey:kFIRDLReadDeepLinkAfterInstallKey];
[self.service setUpWithLaunchOptions:launchOptions
apiKey:kAPIKey
clientID:kClientID
urlScheme:nil
userDefaults:self.userDefaults];
XCTAssertTrue([self.userDefaults boolForKey:kFIRDLReadDeepLinkAfterInstallKey]);
}
- (void)testURLScheme_Nil {
BOOL setUpSucceed = [self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:nil
userDefaults:nil];
XCTAssertTrue(setUpSucceed);
XCTAssertEqualObjects(self.service.URLScheme, [NSBundle mainBundle].bundleIdentifier);
}
- (void)testURLScheme_EmptyString {
BOOL setUpSucceed = [self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:@""
userDefaults:nil];
XCTAssertTrue(setUpSucceed);
XCTAssertEqualObjects(self.service.URLScheme, [NSBundle mainBundle].bundleIdentifier);
}
- (void)testURLScheme_NonNil {
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
XCTAssertEqualObjects(self.service.URLScheme, kURLScheme,
@"URLScheme should be the same as in argument");
}
// TODO (b/63079414) Re-enable this
- (void)DISABLED_testConfigNamedFIRApp {
[self removeAllFIRApps];
id deepLinkServicePartialMock = OCMPartialMock([FIRDynamicLinks dynamicLinks]);
[[deepLinkServicePartialMock reject] configureDynamicLinks:[OCMArg any]];
[FIRApp configureWithName:@"NonDefaultName" options:[FIROptions defaultOptions]];
[deepLinkServicePartialMock stopMocking];
}
// TODO (b/37855379) re-enable the test
- (void)DISABLED_testConfigForFIRApp {
[self removeAllFIRApps];
id deepLinkServicePartialMock = OCMPartialMock([FIRDynamicLinks dynamicLinks]);
[FIRApp configure];
OCMVerify([deepLinkServicePartialMock configureDynamicLinks:[OCMArg any]]);
[deepLinkServicePartialMock stopMocking];
}
#pragma mark - dynamicLinkFromCustomSchemeURL
- (void)testCustomScheme_NoDeepLink {
NSString *urlString =
[NSString stringWithFormat:kStructuredLinkFmtFreeform, kURLScheme, @"abc123xyz"];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertNil(dynamicLink, @"invite should be nil since there is no parameter.");
}
- (void)testCustomScheme_DeepLinkOnly {
NSString *deepLinkString = @"https://developers.google.com/products/";
NSString *urlString =
[NSString stringWithFormat:kStructuredLinkFmtDeepLink, kURLScheme, deepLinkString];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertEqualObjects(dynamicLink.url.absoluteString, deepLinkString);
XCTAssertNil(dynamicLink.inviteId);
}
- (void)testCustomScheme_InvitationOnly {
NSString *invitationId = @"213920940217491274389172947";
NSString *urlString =
[NSString stringWithFormat:kStructuredLinkFmtInvitation, kURLScheme, invitationId];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertNil(dynamicLink.url);
XCTAssertEqualObjects(dynamicLink.inviteId, invitationId);
}
- (void)testCustomScheme_DeepLinkAndInvitation {
NSString *deepLinkString = @"https://developers.google.com/products/";
NSString *invitationId = @"21392094021749127-4389172947";
NSString *urlString = [NSString stringWithFormat:kStructuredLinkFmtDeepLinkAndInvitation,
[[NSBundle mainBundle] bundleIdentifier],
deepLinkString, invitationId];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertEqualObjects(dynamicLink.url.absoluteString, deepLinkString);
XCTAssertEqualObjects(dynamicLink.inviteId, invitationId);
}
- (void)testCustomScheme_FirstTimeOpenedWithCustomSchemeShouldGetStrongMatch {
NSString *invitationId = @"21392094021749127-4389172947";
NSString *urlString =
[NSString stringWithFormat:kStructuredLinkFmtInvitation, kURLScheme, invitationId];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertEqual(dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceStrong,
@"matchConfidence should be strong when app is first opened with custom scheme.");
XCTAssertNil(dynamicLink.url);
XCTAssertEqualObjects(dynamicLink.inviteId, invitationId);
}
- (void)testCustomScheme_FirstTimeOpenedFromFingerprintCodepathShouldGetWeakMatch {
NSString *invitationId = @"21392094021749127-4389172947";
NSString *urlString =
[NSString stringWithFormat:kStructuredLinkFmtInvitationWeak, kURLScheme, invitationId];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertEqual(
dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceWeak,
@"matchConfidence should be weak when app is first opened from fingerprint codepath.");
XCTAssertNil(dynamicLink.url);
XCTAssertEqualObjects(dynamicLink.inviteId, invitationId);
}
- (void)testCustomScheme_StrongMatch {
NSString *invitationId = @"21392094021749127-4389172947";
NSString *urlString =
[NSString stringWithFormat:kStructuredLinkFmtInvitation, kURLScheme, invitationId];
NSURL *url = [NSURL URLWithString:urlString];
// Simulate opening the app.
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFIRDLReadDeepLinkAfterInstallKey];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertEqual(dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceStrong,
@"matchConfidence should be strong when opening an url after app is installed.");
XCTAssertNil(dynamicLink.url);
XCTAssertEqualObjects(dynamicLink.inviteId, invitationId);
}
- (void)testLinkParamWithPlus {
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
NSString *durableDeepLinkString =
[NSString stringWithFormat:@"gindeeplinkurl://google/link?deep_link_id=%@",
kEncodedComplicatedURLString];
NSURL *durabledeepLinkURL = [NSURL URLWithString:durableDeepLinkString];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:durabledeepLinkURL];
NSString *deepLinkURLString = dynamicLink.url.absoluteString;
XCTAssertEqualObjects(kDecodedComplicatedURLString, deepLinkURLString,
@"ddl url parameter and deep link url should be the same");
}
#pragma mark - dynamicLinkFromUniversalLinkURL
- (void)testUniversalLink_NoDeepLink {
NSString *webPageURLString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtFreeForm, @"/abc123"];
NSURL *url = [NSURL URLWithString:webPageURLString];
NSUserActivity *activity =
[[NSUserActivity alloc] initWithActivityType:NSUserActivityTypeBrowsingWeb];
activity.webpageURL = [NSURL URLWithString:webPageURLString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertNil(dynamicLink, @"invite should be nil since there is no parameter.");
}
- (void)testDynamicLinkFromUniversalLinkURLWithSpecialCharacters {
NSString *durableDeepLinkString =
[NSString stringWithFormat:@"https://xyz.page.link/?link=%@", kEncodedComplicatedURLString];
NSURL *durabledeepLinkURL = [NSURL URLWithString:durableDeepLinkString];
SwizzleDynamicLinkNetworkingWithMock();
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:durabledeepLinkURL];
NSString *deepLinkURLString = dynamicLink.url.absoluteString;
XCTAssertEqualObjects(kDecodedComplicatedURLString, deepLinkURLString,
@"ddl url parameter and deep link url should be the same");
UnswizzleDynamicLinkNetworking();
}
- (void)testDynamicLinkFromUniversalLinkURLWithEncodedCharacters {
NSString *durableDeepLinkString =
[NSString stringWithFormat:@"https://xyz.page.link/?link=%@", kEncodedComplicatedURLString];
NSURL *durabledeepLinkURL = [NSURL URLWithString:durableDeepLinkString];
SwizzleDynamicLinkNetworkingWithMock();
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:durabledeepLinkURL];
NSString *deepLinkURLString = dynamicLink.url.absoluteString;
XCTAssertEqualObjects(kDecodedComplicatedURLString, deepLinkURLString,
@"ddl url parameter and deep link url should be the same");
UnswizzleDynamicLinkNetworking();
}
- (void)testUniversalLink_DeepLink {
NSString *deepLinkString = @"https://www.google.com/maps/place/Minneapolis";
NSString *webPageURLString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtDeepLink, deepLinkString];
NSURL *url = [NSURL URLWithString:webPageURLString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
SwizzleDynamicLinkNetworkingWithMock();
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:url];
XCTAssertEqual(dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceStrong);
XCTAssertEqualObjects(dynamicLink.url.absoluteString, deepLinkString);
UnswizzleDynamicLinkNetworking();
}
- (void)testUniversalLink_DeepLinkWithParameters {
NSString *deepLinkString = @"https://www.google.com?key1%3Dvalue1%26key2%3Dvalue2";
NSString *parsedDeepLinkString = @"https://www.google.com?key1=value1&key2=value2";
NSString *webPageURLString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtDeepLink, deepLinkString];
NSURL *url = [NSURL URLWithString:webPageURLString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
SwizzleDynamicLinkNetworkingWithMock();
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:url];
XCTAssertEqual(dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceStrong);
XCTAssertEqualObjects(dynamicLink.url.absoluteString, parsedDeepLinkString);
UnswizzleDynamicLinkNetworking();
}
- (void)testResolveLinkReturnsDLWithNilMinAppVersionWhenNotPresent {
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
NSString *urlString = @"http://reinl.page.link/t4ionvr";
NSURL *url = [NSURL URLWithString:urlString];
void (^executeRequestBlock)(id, NSDictionary *, NSString *, FIRNetworkRequestCompletionHandler) =
^(id p1, NSDictionary *requestBody, NSString *requestURLString,
FIRNetworkRequestCompletionHandler handler) {
NSDictionary *dictionary = @{kFDLResolvedLinkDeepLinkURLKey : kEncodedComplicatedURLString};
NSData *data = FIRDataWithDictionary(dictionary, nil);
handler(data, nil);
};
SEL executeRequestSelector = @selector(executeOnePlatformRequest:forURL:completionHandler:);
[GULSwizzler swizzleClass:[FIRDynamicLinkNetworking class]
selector:executeRequestSelector
isClassSelector:NO
withBlock:executeRequestBlock];
XCTestExpectation *expectation = [self expectationWithDescription:@"handler called"];
[self.service
handleUniversalLink:url
completion:^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
XCTAssertNil(dynamicLink.minimumAppVersion,
@"Min app version not nil when not present.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
}
- (void)testResolveLinkReturnsDLWithMinAppVersionWhenPresent {
NSString *expectedMinVersion = @"8g5u3e";
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
NSString *urlString = @"http://reinl.page.link/t4ionvr";
NSURL *url = [NSURL URLWithString:urlString];
void (^executeRequestBlock)(id, NSDictionary *, NSString *, FIRNetworkRequestCompletionHandler) =
^(id p1, NSDictionary *requestBody, NSString *requestURLString,
FIRNetworkRequestCompletionHandler handler) {
NSDictionary *dictionary = @{
kFDLResolvedLinkDeepLinkURLKey : kEncodedComplicatedURLString,
kFDLResolvedLinkMinAppVersionKey : expectedMinVersion,
};
NSData *data = FIRDataWithDictionary(dictionary, nil);
handler(data, nil);
};
SEL executeRequestSelector = @selector(executeOnePlatformRequest:forURL:completionHandler:);
[GULSwizzler swizzleClass:[FIRDynamicLinkNetworking class]
selector:executeRequestSelector
isClassSelector:NO
withBlock:executeRequestBlock];
XCTestExpectation *expectation = [self expectationWithDescription:@"handler called"];
[self.service
handleUniversalLink:url
completion:^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
XCTAssertEqualObjects(expectedMinVersion, dynamicLink.minimumAppVersion,
@"min app version did not match imv parameter.");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
}
- (void)testUniversalLinkWithSubdomain_NoDeepLink {
NSString *webPageURLString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtSubdomain, @"/abc123"];
NSURL *url = [NSURL URLWithString:webPageURLString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertNil(dynamicLink, @"deepLink should be nil since there is no parameter.");
}
- (void)testDynamicLinkFromCustomSchemeURLReturnsDLWithNilMinimumVersion {
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
NSURL *url = FIRDLDeepLinkURLWithInviteID(nil, kEncodedComplicatedURLString, nil, nil, nil, NO,
nil, nil, kURLScheme, nil);
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
XCTAssertNil(dynamicLink.minimumAppVersion, @"Min app version was not nil when not set.");
}
- (void)testDynamicLinkFromCustomSchemeURLReturnsDLMinimumVersion {
NSString *expectedMinVersion = @"03-9g03hfd";
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
NSURL *url = FIRDLDeepLinkURLWithInviteID(nil, kEncodedComplicatedURLString, nil, nil, nil, NO,
nil, expectedMinVersion, kURLScheme, nil);
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromCustomSchemeURL:url];
NSString *minVersion = dynamicLink.minimumAppVersion;
XCTAssertEqualObjects(expectedMinVersion, minVersion,
@"Min version didn't match the min app version parameter");
}
- (void)testDynamicLinkFromUniversalLinkURLReturnsDLWithNilMinimumVersion {
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
NSURL *url = FIRDLDeepLinkURLWithInviteID(nil, kEncodedComplicatedURLString, nil, nil, nil, NO,
nil, nil, kURLScheme, nil);
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:url];
NSString *minVersion = dynamicLink.minimumAppVersion;
XCTAssertNil(minVersion, @"Min app version was not nil when not set.");
}
- (void)testDynamicLinkFromUniversalLinkURLReturnsDLMinimumVersion {
NSString *expectedMinVersion = @"03-9g03hfd";
NSString *urlSuffix =
[NSString stringWithFormat:@"%@&imv=%@", kEncodedComplicatedURLString, expectedMinVersion];
NSString *urlString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtSubdomainDeepLink, urlSuffix];
NSURL *url = [NSURL URLWithString:urlString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:url];
NSString *minVersion = dynamicLink.minimumAppVersion;
XCTAssertEqualObjects(expectedMinVersion, minVersion, @"Min version didn't match imv= parameter");
}
- (void)testUniversalLinkWithSubdomain_DeepLink {
NSString *deepLinkString = @"https://www.google.com/maps/place/Minneapolis";
NSString *webPageURLString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtSubdomainDeepLink, deepLinkString];
NSURL *url = [NSURL URLWithString:webPageURLString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
SwizzleDynamicLinkNetworkingWithMock();
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:url];
XCTAssertEqual(dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceStrong);
XCTAssertEqualObjects(dynamicLink.url.absoluteString, deepLinkString);
UnswizzleDynamicLinkNetworking();
}
- (void)testUniversalLinkWithSubdomain_DeepLinkWithParameters {
NSString *deepLinkString = @"https://www.google.com?key1%3Dvalue1%26key2%3Dvalue2";
NSString *parsedDeepLinkString = @"https://www.google.com?key1=value1&key2=value2";
NSString *webPageURLString =
[NSString stringWithFormat:kStructuredUniversalLinkFmtSubdomainDeepLink, deepLinkString];
NSURL *url = [NSURL URLWithString:webPageURLString];
[self.service setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
FIRDynamicLink *dynamicLink = [self.service dynamicLinkFromUniversalLinkURL:url];
XCTAssertEqual(dynamicLink.matchConfidence, FIRDynamicLinkMatchConfidenceStrong);
XCTAssertEqualObjects(dynamicLink.url.absoluteString, parsedDeepLinkString);
}
- (void)testMatchesUnversalLinkWithLongDurableLink {
NSString *urlString =
@"https://sample.page.link?link=https://google.com/test&ibi=com.google.sample&ius=79306483";
NSURL *url = [NSURL URLWithString:urlString];
BOOL matchesShort = [self.service matchesShortLinkFormat:url];
XCTAssertFalse(matchesShort, @"Long Durable Link should not match short link format");
}
- (void)testMatchesUnversalLinkWithShortDurableLink {
NSString *urlString = @"https://sample.page.link/79g49s";
NSURL *url = [NSURL URLWithString:urlString];
BOOL matchesShort = [self.service matchesShortLinkFormat:url];
XCTAssertTrue(matchesShort, @"Short Durable Link didn't match short link");
}
- (void)testMatchesUnversalLinkWithAppInvite {
NSString *urlString = @"https://sample.page.link/i/79g49s";
NSURL *url = [NSURL URLWithString:urlString];
BOOL matchesShort = [self.service matchesShortLinkFormat:url];
XCTAssertTrue(matchesShort, @"AppInvite didn't match short link");
}
- (void)testDoesNotMatchesShortLinkFormatWithNonDDLDomains {
NSArray<NSString *> *urlStrings = @[
@"https://www.google.com/maps/place/@1,1/My+Home/", @"https://mydomain.com/t439gfde",
@"https://goo.gl/309dht4", @"https://59eh.goo.gl/309dht4", @"https://app.59eh.goo.gl/309dht4",
@"https://goo.gl/i/309dht4", @"https://page.link/i/309dht4", @"https://fjo3eh.goo.gl/i/309dht4",
@"https://app.fjo3eh.goo.gl/i/309dht4", @"https://1234.page.link/link/dismiss"
];
for (NSString *urlString in urlStrings) {
NSURL *url = [NSURL URLWithString:urlString];
BOOL matchesShortLinkFormat = [self.service matchesShortLinkFormat:url];
XCTAssertFalse(matchesShortLinkFormat,
@"Non-DDL domain URL matched short link format with URL: %@", url);
}
}
- (void)testHandleUniversalLinkWithShortLink {
NSString *shortLinkString = @"https://sample.page.link/549igo";
NSString *bundleID = [NSBundle mainBundle].bundleIdentifier;
NSString *customSchemeURLString = [NSString
stringWithFormat:kStructuredLinkFmtDeepLink, bundleID, kEncodedComplicatedURLString];
XCTestExpectation *handleLinkCompletionExpectation =
[self expectationWithDescription:@"handleLink"];
XCTestExpectation *linkResolverCompletionExpectation =
[self expectationWithDescription:@"linkResolver"];
FakeShortLinkResolver *resolver =
[FakeShortLinkResolver resolverWithBlock:^NSURL *(NSURL *shortLink) {
[linkResolverCompletionExpectation fulfill];
return [NSURL URLWithString:customSchemeURLString];
}];
SwizzleDynamicLinkNetworking(resolver);
[self.service
handleUniversalLink:[NSURL URLWithString:shortLinkString]
completion:^(FIRDynamicLink *_Nonnull dynamicLink, NSError *_Nullable error) {
NSString *returnedURLString = dynamicLink.url.absoluteString;
XCTAssertEqualObjects(kDecodedComplicatedURLString, returnedURLString,
@"Handle universal link returned unexpected link");
[handleLinkCompletionExpectation fulfill];
}];
[self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
UnswizzleDynamicLinkNetworking();
}
- (void)testHandleUniversalLinkWithLongLink {
NSString *longLinkString = [NSString
stringWithFormat:@"https://sample.page.link?link=%@&ibi=com.google.sample&ius=79306483",
kEncodedComplicatedURLString];
XCTestExpectation *handleLinkCompletionExpectation =
[self expectationWithDescription:@"handleLink"];
__block NSUInteger resolverInvocationsCount = 0;
// should not be used.
FakeShortLinkResolver *resolver =
[FakeShortLinkResolver resolverWithBlock:^NSURL *(NSURL *shortLink) {
resolverInvocationsCount++;
return [NSURL URLWithString:kDecodedComplicatedURLString];
}];
id handleUniversalLinkBlock = ^(FIRDynamicLink *_Nonnull dynamicLink, NSError *_Nullable error) {
[handleLinkCompletionExpectation fulfill];
NSURL *expectedResolvedLink = [NSURL URLWithString:kDecodedComplicatedURLString];
XCTAssertEqualObjects(expectedResolvedLink, dynamicLink.url,
@"Resolve short link returned unexpected link");
};
SwizzleDynamicLinkNetworking(resolver);
[self.service handleUniversalLink:[NSURL URLWithString:longLinkString]
completion:handleUniversalLinkBlock];
[self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
// It is expected to call resolveLink once for logging.
XCTAssertEqual(resolverInvocationsCount, 1,
@"handleUniversalLink called resolveLink more than once");
UnswizzleDynamicLinkNetworking();
}
- (void)testHandleUniversalLinkCallsHandleUniversalLinkResolver {
XCTestExpectation *handleLinkCompletionExpectation =
[self expectationWithDescription:@"handleLink"];
void (^replacementBlock)(void) = ^{
[handleLinkCompletionExpectation fulfill];
};
SEL selectorToSwizzle = @selector(handleUniversalLink:completion:);
[GULSwizzler swizzleClass:[FIRDynamicLinks class]
selector:selectorToSwizzle
isClassSelector:NO
withBlock:replacementBlock];
NSURL *url = [NSURL URLWithString:@"https://google.com"];
[self.service
handleUniversalLink:url
completion:^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error){
}];
[self waitForExpectationsWithTimeout:kAsyncTestTimout handler:nil];
[GULSwizzler unswizzleClass:[FIRDynamicLinks class]
selector:selectorToSwizzle
isClassSelector:NO];
}
- (void)testHandleUniversalLinkCompletionReturnsNoForNonDDL {
NSArray<NSString *> *urlStrings = @[
@"https://www.google.com/maps/place/@1,1/My+Home/", @"https://mydomain.com/t439gfde",
@"https://goo.gl/309dht4", @"https://59eh.goo.gl/309dht4", @"https://app.59eh.goo.gl/309dht4",
@"https://goo.gl/i/309dht4", @"https://page.link/i/309dht4", @"https://fjo3eh.goo.gl/i/309dht4",
@"https://app.fjo3eh.goo.gl/i/309dht4", @"https://1234.page.link/link/dismiss"
];
[urlStrings enumerateObjectsUsingBlock:^(NSString *_Nonnull urlString, NSUInteger idx,
BOOL *_Nonnull stop) {
NSURL *url = [NSURL URLWithString:urlString];
BOOL handled = [self.service
handleUniversalLink:url
completion:^(FIRDynamicLink *_Nullable dynamicLink, NSError *_Nullable error) {
XCTAssertNil(dynamicLink, @"Non DDL returned FIRDynamicLink");
}];
XCTAssertFalse(handled, @"Non DDL Universal Link was handled");
}];
}
- (void)test_ensureInternalMethodsNotRenamed {
// sanity check to ensure these methods has not been renamed
// we relaying on these to be the same for tests to work properly
// we are not exposing these methods in internal headers as we do not have "internal headers"
// 1P apps can import all of our headers
XCTAssertTrue([self.service
respondsToSelector:@selector
(handlePendingDynamicLinkRetrievalFailureWithErrorCode:errorDescription:underlyingError:)]);
XCTAssertTrue([self.service respondsToSelector:@selector(setRetrievingPendingDynamicLink:)]);
XCTAssertTrue([self.service respondsToSelector:@selector(dynamicLinkNetworking)]);
}
- (void)testCheckForPendingDynamicLinkReturnsImmediatelyIfAlreadyRead {
id mockService = OCMPartialMock(self.service);
[[mockService expect] handlePendingDynamicLinkRetrievalFailureWithErrorCode:-1
errorDescription:[OCMArg any]
underlyingError:[OCMArg any]];
[[mockService reject] setRetrievingPendingDynamicLink:YES];
[mockService setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:nil
userDefaults:[NSUserDefaults standardUserDefaults]];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFIRDLReadDeepLinkAfterInstallKey];
[mockService checkForPendingDynamicLink];
[mockService verify];
[mockService stopMocking];
}
- (void)test_multipleRequestsToRetrievePendingDeepLinkShouldNotCrash {
id mockService = OCMPartialMock(self.service);
[[mockService expect] handlePendingDynamicLinkRetrievalFailureWithErrorCode:-1
errorDescription:[OCMArg any]
underlyingError:[OCMArg any]];
// swizzle method to prevent actual retrieval, this will ensure that first pending link
// retrieval will stuck and second will fail with error
id (^replacementBlock)(void) = (id) ^ { return nil; };
SEL selectorToSwizzle = @selector(automaticRetrievalProcess);
[GULSwizzler swizzleClass:[FIRDLRetrievalProcessFactory class]
selector:selectorToSwizzle
isClassSelector:NO
withBlock:replacementBlock];
[mockService setUpWithLaunchOptions:nil
apiKey:kAPIKey
clientID:kClientID
urlScheme:kURLScheme
userDefaults:self.userDefaults];
[mockService checkForPendingDynamicLink];
// we should not crash here
[mockService checkForPendingDynamicLink];
[mockService verify];
[mockService stopMocking];
[GULSwizzler unswizzleClass:[FIRDLRetrievalProcessFactory class]
selector:selectorToSwizzle
isClassSelector:NO];
}
#pragma mark - Self-diagnose tests
- (void)testSelfDiagnoseWithNilCompletion {
[FIRDynamicLinks performDiagnosticsWithCompletion:nil];
}
- (void)testSelfDiagnoseCompletionCalled {
XCTestExpectation *expectation =
[self expectationWithDescription:@"Self diagnose completion block must be called"];
[FIRDynamicLinks
performDiagnosticsWithCompletion:^(NSString *_Nonnull diagnosticOutput, BOOL hasErrors) {
XCTAssert(diagnosticOutput.length > 0, @"Diagnostic expected to provide output");
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:2.0 handler:nil];
}
#pragma mark - Private Helpers
- (void)removeAllFIRApps {
NSDictionary *apps = [FIRApp allApps];
for (FIRApp *app in apps.allValues) {
[app deleteApp:^(BOOL succes) {
if (!succes) {
NSLog(@"Error deleting FIRApp before tests - config tests may fail.");
}
}];
}
}
#pragma clang pop
@end