Skip to content

Commit 74ebddc

Browse files
authored
Merge pull request #956 from BranchMetrics/SDK-598-callback-handler-not-always-called
SDK-598 callback handler not always called
2 parents 4187a90 + 76e06ec commit 74ebddc

36 files changed

+1160
-1507
lines changed

Branch-SDK-Tests/BNCAppleReceiptTests.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ @interface BNCAppleReceiptTests : XCTestCase
1616
@implementation BNCAppleReceiptTests
1717

1818
- (void)setUp {
19-
// Put setup code here. This method is called before the invocation of each test method in the class.
19+
2020
}
2121

2222
- (void)tearDown {
23-
// Put teardown code here. This method is called after the invocation of each test method in the class.
23+
2424
}
2525

26-
- (void)testReceiptIsNil {
27-
BNCAppleReceipt *receipt = [BNCAppleReceipt new];
26+
- (void)testReceiptOnSimulator {
27+
BNCAppleReceipt *receipt = [[BNCAppleReceipt alloc] init];
2828
XCTAssertNil([receipt installReceipt]);
2929
XCTAssertFalse([receipt isTestFlight]);
3030
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//
2+
// BNCAppleSearchAdsTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 10/23/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCAppleSearchAds.h"
11+
12+
// expose private methods for unit testing
13+
@interface BNCAppleSearchAds()
14+
15+
- (BOOL)isAppleSearchAdSavedToDictionary:(NSDictionary *)appleSearchAdDetails;
16+
- (BOOL)isDateWithinWindow:(NSDate *)installDate;
17+
- (BOOL)isAdClientAvailable;
18+
- (BOOL)isAppleTestData:(NSDictionary *)appleSearchAdDetails;
19+
20+
- (void)requestAttributionWithCompletion:(void (^_Nullable)(NSDictionary *__nullable attributionDetails, NSError *__nullable error, NSTimeInterval elapsedSeconds))completion;
21+
22+
@end
23+
24+
@interface BNCAppleSearchAdsTests : XCTestCase
25+
26+
@property (nonatomic, strong, readwrite) BNCAppleSearchAds *appleSearchAds;
27+
28+
@end
29+
30+
@implementation BNCAppleSearchAdsTests
31+
32+
- (void)setUp {
33+
self.appleSearchAds = [[BNCAppleSearchAds alloc] init];
34+
}
35+
36+
- (void)tearDown {
37+
38+
}
39+
40+
- (void)testAdClientIsAvailable {
41+
XCTAssertTrue([self.appleSearchAds isAdClientAvailable]);
42+
}
43+
44+
- (void)testDateIsWithinWindow_DistantPast {
45+
XCTAssertFalse([self.appleSearchAds isDateWithinWindow:[NSDate distantPast]]);
46+
}
47+
48+
- (void)testDateIsWithinWindow_DistantFuture {
49+
XCTAssertFalse([self.appleSearchAds isDateWithinWindow:[NSDate distantFuture]]);
50+
}
51+
52+
- (void)testDateIsWithinWindow_Now {
53+
XCTAssertTrue([self.appleSearchAds isDateWithinWindow:[NSDate date]]);
54+
}
55+
56+
- (void)testIsAppleSearchAdSavedToDictionary_Nil {
57+
XCTAssertFalse([self.appleSearchAds isAppleSearchAdSavedToDictionary:nil]);
58+
}
59+
60+
- (void)testIsAppleSearchAdSavedToDictionary_Empty {
61+
XCTAssertFalse([self.appleSearchAds isAppleSearchAdSavedToDictionary:@{}]);
62+
}
63+
64+
- (void)testIsAppleSearchAdSavedToDictionary_NO {
65+
XCTAssertFalse([self.appleSearchAds isAppleSearchAdSavedToDictionary:@{ @"Version3.1" : @{ @"iad-attribution": @(NO) } }]);
66+
}
67+
68+
- (void)testIsAppleSearchAdSavedToDictionary_YES {
69+
XCTAssertTrue([self.appleSearchAds isAppleSearchAdSavedToDictionary:@{ @"Version3.1" : @{ @"iad-attribution": @(YES) } }]);
70+
}
71+
72+
/*
73+
Expected test data from Apple.
74+
75+
"Version3.1" = {
76+
"iad-adgroup-id" = 1234567890;
77+
"iad-adgroup-name" = AdGroupName;
78+
"iad-attribution" = true;
79+
"iad-campaign-id" = 1234567890;
80+
"iad-campaign-name" = CampaignName;
81+
"iad-click-date" = "2019-10-24T00:14:36Z";
82+
"iad-conversion-date" = "2019-10-24T00:14:36Z";
83+
"iad-conversion-type" = Download;
84+
"iad-country-or-region" = US;
85+
"iad-creativeset-id" = 1234567890;
86+
"iad-creativeset-name" = CreativeSetName;
87+
"iad-keyword" = Keyword;
88+
"iad-keyword-id" = KeywordID;
89+
"iad-keyword-matchtype" = Broad;
90+
"iad-lineitem-id" = 1234567890;
91+
"iad-lineitem-name" = LineName;
92+
"iad-org-id" = 1234567890;
93+
"iad-org-name" = OrgName;
94+
"iad-purchase-date" = "2019-10-24T00:14:36Z";
95+
};
96+
*/
97+
- (void)testIsTestData_YES {
98+
NSDictionary *testDataIndicators = @{
99+
@"Version3.1" : @{
100+
@"iad-adgroup-id" : @"1234567890",
101+
@"iad-adgroup-name" : @"AdGroupName",
102+
@"iad-campaign-id" : @"1234567890",
103+
@"iad-campaign-name" : @"CampaignName",
104+
@"iad-org-id" : @"1234567890",
105+
@"iad-org-name" : @"OrgName"
106+
}
107+
};
108+
109+
XCTAssertTrue([self.appleSearchAds isAppleTestData:testDataIndicators]);
110+
}
111+
112+
- (void)testIsTestData_NO {
113+
NSDictionary *testDataIndicators = @{
114+
@"Version3.1" : @{
115+
@"iad-adgroup-id" : @"100",
116+
@"iad-adgroup-name" : @"AdGroupName",
117+
@"iad-campaign-id" : @"1234567890",
118+
@"iad-campaign-name" : @"CampaignName",
119+
@"iad-org-id" : @"1234567890",
120+
@"iad-org-name" : @"OrgName"
121+
}
122+
};
123+
124+
XCTAssertFalse([self.appleSearchAds isAppleTestData:testDataIndicators]);
125+
}
126+
127+
/*
128+
Expected payload varies by simulator or test device. In general, there is a payload of some sort.
129+
130+
This test fails on iOS 10 simulators. iPad simulators never respond. iPhone simulators return an error.
131+
*/
132+
- (void)testRequestAppleSearchAds {
133+
__block XCTestExpectation *expectation = [self expectationWithDescription:@"AppleSearchAds"];
134+
135+
[self.appleSearchAds requestAttributionWithCompletion:^(NSDictionary * _Nullable attributionDetails, NSError * _Nullable error, NSTimeInterval elapsedSeconds) {
136+
XCTAssertNil(error);
137+
XCTAssertTrue(elapsedSeconds > 0);
138+
139+
NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
140+
XCTAssertNotNil(tmpDict);
141+
142+
NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
143+
XCTAssertNotNil(tmpBool);
144+
145+
[expectation fulfill];
146+
}];
147+
148+
[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
149+
NSLog(@"%@", error);
150+
}];
151+
}
152+
153+
@end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// BNCFacebookAppLinksTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 10/24/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCFacebookMock.h"
11+
#import "BNCFacebookAppLinks.h"
12+
13+
@interface BNCFacebookAppLinks()
14+
- (BOOL)isDeepLinkingClassAvailable;
15+
@end
16+
17+
@interface BNCFacebookAppLinksTests : XCTestCase
18+
@property (nonatomic, strong, readwrite) BNCFacebookAppLinks *applinks;
19+
@end
20+
21+
@implementation BNCFacebookAppLinksTests
22+
23+
- (void)setUp {
24+
self.applinks = [BNCFacebookAppLinks new];
25+
}
26+
27+
- (void)tearDown {
28+
29+
}
30+
31+
- (void)testIsDeepLinkingClassAvailable {
32+
XCTAssertFalse([self.applinks isDeepLinkingClassAvailable]);
33+
}
34+
35+
- (void)testRegisterFacebookDeepLinkingClass_String {
36+
[self.applinks registerFacebookDeepLinkingClass:@"HelloWorld"];
37+
XCTAssertFalse([self.applinks isDeepLinkingClassAvailable]);
38+
}
39+
40+
- (void)testRegisterFacebookDeepLinkingClass_Mock {
41+
[self.applinks registerFacebookDeepLinkingClass:[BNCFacebookMock new]];
42+
XCTAssertTrue([self.applinks isDeepLinkingClassAvailable]);
43+
}
44+
45+
- (void)testFetchFacebookAppLink {
46+
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
47+
48+
[self.applinks registerFacebookDeepLinkingClass:[BNCFacebookMock new]];
49+
[self.applinks fetchFacebookAppLinkWithCompletion:^(NSURL * _Nullable appLink, NSError * _Nullable error) {
50+
XCTAssertTrue([[appLink absoluteString] isEqualToString:@"https://branch.io"]);
51+
[expectation fulfill];
52+
}];
53+
54+
[self waitForExpectationsWithTimeout:2 handler:^(NSError * _Nullable error) {
55+
NSLog(@"%@", error);
56+
}];
57+
}
58+
59+
@end

Branch-SDK-Tests/BNCFacebookMock.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// BNCFacebookMock.h
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 10/24/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface BNCFacebookMock : NSObject
14+
15+
- (void)fetchDeferredAppLink:(void (^_Nullable)(NSURL *__nullable appLink, NSError * __nullable error))completion;
16+
17+
@end
18+
19+
NS_ASSUME_NONNULL_END

Branch-SDK-Tests/BNCFacebookMock.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// BNCFacebookMock.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 10/24/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import "BNCFacebookMock.h"
10+
11+
@implementation BNCFacebookMock
12+
13+
- (void)fetchDeferredAppLink:(void (^_Nullable)(NSURL *__nullable appLink, NSError * __nullable error))completion {
14+
if (completion) {
15+
NSURL *url = [NSURL URLWithString:@"https://branch.io"];
16+
completion(url, nil);
17+
}
18+
}
19+
20+
@end

Branch-SDK-Tests/BNCLog.Test.m

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,20 +286,6 @@ - (void) testTripleLengthLogMessages {
286286
BNCLogSetOutputFunction(origPtr);
287287
}
288288

289-
- (void) testLogObject {
290-
BNCLogSetOutputFunction(TestLogProcedure);
291-
NSData *data = [@"Test string." dataUsingEncoding:NSUTF8StringEncoding];
292-
#pragma clang diagnostic push
293-
#pragma clang diagnostic ignored "-Wformat-security"
294-
BNCLog((id)data);
295-
#pragma clang diagnostic pop
296-
BNCLogFlushMessages();
297-
XCTAssert([globalTestLogString bnc_isEqualToMaskedString:
298-
@"[branch.io] BNCLog.Test.m(***) Log: "
299-
"0x**************** <NSConcreteMutableData> "
300-
"<54657374 20737472 696e672e>"]);
301-
}
302-
303289
#pragma mark - Test BNCLogSetOutputToURLRecordWrapSize
304290

305291
extern void BNCLogSetOutputToURLRecordWrapSize(NSURL *_Nullable url, long maxRecords, long recordSize);

Branch-SDK-Tests/BNCURLBlackList.Test.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ - (void) testStandardBlackList {
161161
}
162162
});
163163
[branch clearNetworkQueue];
164-
[branch handleDeepLinkWithNewSession:[NSURL URLWithString:@"https://myapp.app.link/bob/link?oauth=true"]];
164+
[branch handleDeepLink:[NSURL URLWithString:@"https://myapp.app.link/bob/link?oauth=true"]];
165165
[self waitForExpectationsWithTimeout:5.0 handler:nil];
166166
[serverInterfaceMock stopMocking];
167167
[BNCPreferenceHelper preferenceHelper].referringURL = nil;
@@ -200,7 +200,7 @@ - (void) testUserBlackList {
200200
[expectation fulfill];
201201
}
202202
});
203-
[branch handleDeepLinkWithNewSession:[NSURL URLWithString:@"https://myapp.app.link/bob/link"]];
203+
[branch handleDeepLink:[NSURL URLWithString:@"https://myapp.app.link/bob/link"]];
204204
[self waitForExpectationsWithTimeout:5.0 handler:nil];
205205
[serverInterfaceMock stopMocking];
206206
[BNCPreferenceHelper preferenceHelper].referringURL = nil;

Branch-SDK-Tests/BranchDelegate.Test.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,13 @@ - (void) testNotificationsFailure {
210210

211211
// Delegate method. Order: 0.
212212
- (void) branch:(Branch*)branch willStartSessionWithURL:(NSURL*)url {
213-
XCTAssertTrue([NSThread isMainThread]);
214213
XCTAssertTrue(self.notificationOrder == 0);
215214
self.notificationOrder++;
216215
[self.branchWillOpenURLExpectation fulfill];
217216
}
218217

219218
// Notification method. Order: 1.
220219
- (void) branchWillStartSessionNotification:(NSNotification*)notification {
221-
XCTAssertTrue([NSThread isMainThread]);
222220
XCTAssertTrue(self.notificationOrder == 1);
223221
self.notificationOrder++;
224222

Branch-SDK-Tests/BranchNetworkScenario.Test.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#import "Branch.h"
1313
#import "BNCServerRequestQueue.h"
1414
#import "BNCPreferenceHelper.h"
15-
#import "BNCError.h"
15+
#import "NSError+Branch.h"
1616
#import "BranchOpenRequest.h"
1717

1818

@@ -584,7 +584,7 @@ - (void)enqueueTwoNonReplayableRequestsWithFirstFailingBecauseBranchIsDown:(Bran
584584

585585
@synchronized (self) {
586586
if (badRequestCallback) {
587-
NSError * error = [NSError errorWithDomain:BNCErrorDomain code:BNCServerProblemError userInfo:nil];
587+
NSError * error = [NSError branchErrorWithCode:BNCServerProblemError];
588588
badRequestCallback(nil, error);
589589
} else {
590590
XCTAssert(badRequestCallback);
@@ -644,7 +644,7 @@ - (void)enqueueTwoNonReplayableRequestsWithFirstFailingBecauseRequestIsBad:(Bran
644644
sleep(1); // Sleep so that network queue can processes
645645
@synchronized (self) {
646646
if (badRequestCallback) {
647-
badRequestCallback(nil, [NSError errorWithDomain:BNCErrorDomain code:BNCBadRequestError userInfo:nil]);
647+
badRequestCallback(nil, [NSError branchErrorWithCode:BNCBadRequestError]);
648648
badRequestCallback = nil;
649649
} else if (goodRequestCallback) {
650650
goodRequestCallback([[BNCServerResponse alloc] init], nil);

0 commit comments

Comments
 (0)