Skip to content

Commit f6ce083

Browse files
Merge pull request #974 from BranchMetrics/SDK-679
SDK-679
2 parents 93af5bc + 3003bb2 commit f6ce083

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

Branch-SDK-Tests/BNCFacebookAppLinksTests.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@
99
#import <XCTest/XCTest.h>
1010
#import "BNCFacebookMock.h"
1111
#import "BNCFacebookAppLinks.h"
12+
#import "Branch.h"
1213

1314
@interface BNCFacebookAppLinks()
1415
- (BOOL)isDeepLinkingClassAvailable;
1516
@end
1617

1718
@interface BNCFacebookAppLinksTests : XCTestCase
1819
@property (nonatomic, strong, readwrite) BNCFacebookAppLinks *applinks;
20+
@property (nonatomic, strong, readwrite) Branch *branch;
21+
@property (nonatomic, strong, readwrite) BNCPreferenceHelper *preferenceHelper;
1922
@end
2023

2124
@implementation BNCFacebookAppLinksTests
2225

2326
- (void)setUp {
27+
self.branch = [Branch getInstance];
2428
self.applinks = [BNCFacebookAppLinks new];
29+
self.preferenceHelper = [BNCPreferenceHelper preferenceHelper];
2530
}
2631

2732
- (void)tearDown {
@@ -56,4 +61,32 @@ - (void)testFetchFacebookAppLink {
5661
}];
5762
}
5863

64+
// check if FBSDKAppLinkUtility.fetchDeferredAppLink is called on the main thread
65+
// https://developers.facebook.com/docs/reference/ios/current/class/FBSDKAppLinkUtility
66+
- (void)testCheckFacebookAppLinks {
67+
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
68+
69+
[self.branch registerFacebookDeepLinkingClass:[BNCFacebookMock new]];
70+
71+
// checkFacebookAppLinks is not public, so use reflection to call it
72+
SEL selector = NSSelectorFromString(@"checkFacebookAppLinks");
73+
((void (*)(id, SEL))[self.branch methodForSelector:selector])(self.branch, selector);
74+
75+
// wait 2 secs, then fulfill expectation, if checkFacebookAppLinks succeeded in setting
76+
// BNCPreferenceHelper's property 'faceBookAppLink'
77+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
78+
if ([self.preferenceHelper faceBookAppLink]) {
79+
XCTAssertTrue([[self.preferenceHelper faceBookAppLink].absoluteString isEqualToString:@"https://branch.io"]);
80+
[expectation fulfill];
81+
} else {
82+
XCTFail(@"BNCPreferenceHelper.faceBookAppLink is nil after 2 seconds");
83+
}
84+
});
85+
86+
// wait 3 secs, then check if expectation's been fulfilled
87+
[self waitForExpectationsWithTimeout:3 handler:^(NSError * _Nullable error) {
88+
NSLog(@"%@", error);
89+
}];
90+
}
91+
5992
@end

Branch-SDK-Tests/BNCFacebookMock.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
//
88

99
#import "BNCFacebookMock.h"
10+
#import "NSError+Branch.h"
1011

1112
@implementation BNCFacebookMock
1213

1314
- (void)fetchDeferredAppLink:(void (^_Nullable)(NSURL *__nullable appLink, NSError * __nullable error))completion {
1415
if (completion) {
15-
NSURL *url = [NSURL URLWithString:@"https://branch.io"];
16-
completion(url, nil);
16+
if (![NSThread isMainThread]) {
17+
// fetchDeferredAppLink must be called from main thread
18+
// https://developers.facebook.com/docs/reference/ios/current/class/FBSDKAppLinkUtility
19+
completion(nil, [NSError branchErrorWithCode:BNCGeneralError localizedMessage:@"fetchDeferredAppLink must be called from main thread"]);
20+
} else {
21+
completion([NSURL URLWithString:@"https://branch.io"], nil);
22+
}
1723
}
1824
}
1925

Branch-SDK-Tests/BranchDelegate.Test.m

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ - (void) testNotificationsSuccess {
3030

3131
self.expectFailure = NO;
3232
self.notificationOrder = 0;
33-
self.branchWillOpenURLExpectation =
34-
[self expectationWithDescription:@"branchWillOpenURLExpectation"];
35-
self.branchWillOpenURLNotificationExpectation =
36-
[self expectationWithDescription:@"branchWillOpenURLNotificationExpectation"];
37-
self.branchDidOpenURLExpectation =
38-
[self expectationWithDescription:@"branchDidOpenURLExpectation"];
39-
self.branchDidOpenURLNotificationExpectation =
40-
[self expectationWithDescription:@"branchDidOpenURLNotificationExpectation"];
4133

4234
[[NSNotificationCenter defaultCenter]
4335
addObserver:self
@@ -125,14 +117,6 @@ - (void) testNotificationsFailure {
125117

126118
self.expectFailure = YES;
127119
self.notificationOrder = 0;
128-
self.branchWillOpenURLExpectation =
129-
[self expectationWithDescription:@"branchWillOpenURLExpectation"];
130-
self.branchWillOpenURLNotificationExpectation =
131-
[self expectationWithDescription:@"branchWillOpenURLNotificationExpectation"];
132-
self.branchDidOpenURLExpectation =
133-
[self expectationWithDescription:@"branchDidOpenURLExpectation"];
134-
self.branchDidOpenURLNotificationExpectation =
135-
[self expectationWithDescription:@"branchDidOpenURLNotificationExpectation"];
136120

137121
[[NSNotificationCenter defaultCenter]
138122
addObserver:self
@@ -329,4 +313,15 @@ - (void) branchDidStartSessionNotification:(NSNotification*)notification {
329313
[self.branchDidOpenURLNotificationExpectation fulfill];
330314
}
331315

316+
- (void)setUp {
317+
self.branchWillOpenURLExpectation =
318+
[self expectationWithDescription:@"branchWillOpenURLExpectation"];
319+
self.branchWillOpenURLNotificationExpectation =
320+
[self expectationWithDescription:@"branchWillOpenURLNotificationExpectation"];
321+
self.branchDidOpenURLExpectation =
322+
[self expectationWithDescription:@"branchDidOpenURLExpectation"];
323+
self.branchDidOpenURLNotificationExpectation =
324+
[self expectationWithDescription:@"branchDidOpenURLNotificationExpectation"];
325+
}
326+
332327
@end

Branch-SDK/Branch-SDK/BNCFacebookAppLinks.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ - (void)fetchFacebookAppLinkWithCompletion:(void (^_Nullable)(NSURL *__nullable
6363
}
6464
};
6565

66+
dispatch_async(dispatch_get_main_queue(), ^{
6667
((void (*)(id, SEL, void (^ __nullable)(NSURL *__nullable appLink, NSError * __nullable error)))[self.appLinkUtility methodForSelector:self.fetchDeferredAppLink])(self.appLinkUtility, self.fetchDeferredAppLink, completionBlock);
68+
});
6769
}
6870

6971
@end

0 commit comments

Comments
 (0)