Skip to content

Commit 59c079f

Browse files
added tests, cleaned up AppDelegate (removed my own debug code)
1 parent 6173293 commit 59c079f

File tree

4 files changed

+122
-15
lines changed

4 files changed

+122
-15
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//
2+
// BNCDelayedInitSessionTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Benas Klastaitis on 12/3/19.
6+
// Copyright © 2019 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "Branch.h"
11+
12+
@interface BNCDelayedInitSessionTests : XCTestCase
13+
@property (nonatomic, strong, readwrite) Branch *branch;
14+
@end
15+
16+
@implementation BNCDelayedInitSessionTests
17+
18+
19+
- (void)setUp {
20+
self.branch = [Branch getInstance];
21+
}
22+
23+
- (void)tearDown {}
24+
25+
- (void)testDispatchInitSession {
26+
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
27+
dispatch_block_t initBlock = dispatch_block_create(DISPATCH_BLOCK_ASSIGN_CURRENT, ^{
28+
[self.branch initSessionWithLaunchOptions:nil
29+
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
30+
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
31+
[expectation fulfill];
32+
}];
33+
});
34+
35+
[self.branch dispatchInitSession:initBlock After:2];
36+
37+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
38+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
39+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 2);// is initialized
40+
});
41+
42+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
43+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
44+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 0);// uninitialized
45+
});
46+
47+
48+
[self waitForExpectationsWithTimeout:4 handler:^(NSError * _Nullable error) {
49+
NSLog(@"%@", error);
50+
}];
51+
52+
53+
}
54+
55+
- (void)testCancelDelayedInitSession {
56+
dispatch_block_t initBlock = dispatch_block_create(DISPATCH_BLOCK_ASSIGN_CURRENT, ^{
57+
[self.branch initSessionWithLaunchOptions:nil
58+
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
59+
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
60+
}];
61+
});
62+
63+
[self.branch dispatchInitSession:initBlock After:1];
64+
[self.branch cancelDelayedInitSession];
65+
66+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
67+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
68+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 0);// uninitialized
69+
});
70+
}
71+
72+
- (void)testInvokeDelayedInitSession {
73+
__block XCTestExpectation *expectation = [self expectationWithDescription:@""];
74+
dispatch_block_t initBlock = dispatch_block_create(DISPATCH_BLOCK_ASSIGN_CURRENT, ^{
75+
[self.branch initSessionWithLaunchOptions:nil
76+
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
77+
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
78+
[expectation fulfill];
79+
}];
80+
});
81+
82+
[self.branch dispatchInitSession:initBlock After:4];
83+
84+
[self.branch invokeDelayedInitSession];
85+
86+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
87+
id initializationStatus = [self.branch valueForKey:@"initializationStatus"];
88+
XCTAssertTrue([self enumIntValueFromId:initializationStatus] == 2);// uninitialized
89+
});
90+
91+
[self waitForExpectationsWithTimeout:4 handler:^(NSError * _Nullable error) {
92+
NSLog(@"%@", error);
93+
}];
94+
}
95+
96+
-(int)enumIntValueFromId:(id)enumValueId {
97+
if (![enumValueId respondsToSelector:@selector(intValue)])
98+
return -1;
99+
100+
return [enumValueId intValue];
101+
}
102+
103+
@end

Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
4683F0751B20A73F00A432E7 /* CreditHistoryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EAA790119E89F67008D4A83 /* CreditHistoryViewController.m */; };
2525
4683F0761B20A73F00A432E7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 670016731940F51400A9E103 /* AppDelegate.m */; };
2626
46DC406E1B2A328900D2D203 /* AdSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 67BBCF271A69E49A009C7DAE /* AdSupport.framework */; };
27+
4A0C5F982397176500AE6628 /* BNCDelayedInitSessionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A0C5F942397006800AE6628 /* BNCDelayedInitSessionTests.m */; };
2728
4D130E731EE0C7B100A69A0A /* BNCNetworkService.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E4D1EE0C7B000A69A0A /* BNCNetworkService.m */; };
2829
4D130E761EE0C7B100A69A0A /* BNCServerInterface.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E501EE0C7B000A69A0A /* BNCServerInterface.m */; };
2930
4D130E781EE0C7B100A69A0A /* BNCServerRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 4D130E521EE0C7B000A69A0A /* BNCServerRequest.m */; };
@@ -301,6 +302,7 @@
301302
466D5A0F1B5991E3009DB845 /* BNCContentDiscoveryManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCContentDiscoveryManager.h; sourceTree = "<group>"; };
302303
466D5A101B5991E3009DB845 /* BNCContentDiscoveryManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCContentDiscoveryManager.m; sourceTree = "<group>"; };
303304
46DBB42F1B335A9B00642FC8 /* BranchDeepLinkingController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BranchDeepLinkingController.h; sourceTree = "<group>"; };
305+
4A0C5F942397006800AE6628 /* BNCDelayedInitSessionTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCDelayedInitSessionTests.m; sourceTree = "<group>"; };
304306
4D130E4C1EE0C7B000A69A0A /* BNCNetworkService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCNetworkService.h; sourceTree = "<group>"; };
305307
4D130E4D1EE0C7B000A69A0A /* BNCNetworkService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BNCNetworkService.m; sourceTree = "<group>"; };
306308
4D130E4E1EE0C7B000A69A0A /* BNCNetworkServiceProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BNCNetworkServiceProtocol.h; sourceTree = "<group>"; };
@@ -761,6 +763,7 @@
761763
5F8B7B3C21B5F5CD009CE0A6 /* Branch-SDK-Unhosted-Tests */ = {
762764
isa = PBXGroup;
763765
children = (
766+
4A0C5F942397006800AE6628 /* BNCDelayedInitSessionTests.m */,
764767
5FC7327622DE9A44006E6FBC /* BNCServerInterfaceTests.m */,
765768
5F67F48D228F535500067429 /* BNCEncodingUtilsTests.m */,
766769
5F8B7B4621B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m */,
@@ -1355,6 +1358,7 @@
13551358
files = (
13561359
5F67F48E228F535500067429 /* BNCEncodingUtilsTests.m in Sources */,
13571360
5F8B7B4721B5F5F0009CE0A6 /* Branch_setBranchKeyTests.m in Sources */,
1361+
4A0C5F982397176500AE6628 /* BNCDelayedInitSessionTests.m in Sources */,
13581362
5F3D671C233062FD00454FF1 /* BNCJsonLoader.m in Sources */,
13591363
5FC7327722DE9A44006E6FBC /* BNCServerInterfaceTests.m in Sources */,
13601364
);

Branch-TestBed/Branch-TestBed.xcodeproj/xcshareddata/xcschemes/Branch-SDK-Tests.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@
7676
</Test>
7777
</SkippedTests>
7878
</TestableReference>
79+
<TestableReference
80+
skipped = "NO">
81+
<BuildableReference
82+
BuildableIdentifier = "primary"
83+
BlueprintIdentifier = "5F8B7B3A21B5F5CD009CE0A6"
84+
BuildableName = "Branch-SDK-Unhosted-Tests.xctest"
85+
BlueprintName = "Branch-SDK-Unhosted-Tests"
86+
ReferencedContainer = "container:Branch-TestBed.xcodeproj">
87+
</BuildableReference>
88+
</TestableReference>
7989
</Testables>
8090
</TestAction>
8191
<LaunchAction

Branch-TestBed/Branch-TestBed/AppDelegate.m

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,11 @@ - (BOOL)application:(UIApplication *)application
4545
*/
4646

4747
// [branch setIdentity:@"Bobby Branch"];
48-
dispatch_block_t initBlock = dispatch_block_create(DISPATCH_BLOCK_ASSIGN_CURRENT, ^{
49-
[branch initSessionWithLaunchOptions:launchOptions
50-
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
51-
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
52-
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
53-
}];
54-
NSLog(@"BranchSDK: initBlock has been invoked %@", @"success");
55-
});
56-
[branch dispatchInitSession:initBlock After:5];
57-
58-
// pretend that something prerequisite task completed in 2 seconds
59-
dispatch_time_t twoSecFromNow = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC));
60-
dispatch_after(twoSecFromNow, dispatch_get_main_queue(), ^{
61-
[branch invokeDelayedInitSession];
62-
});
48+
[branch initSessionWithLaunchOptions:launchOptions
49+
andRegisterDeepLinkHandlerUsingBranchUniversalObject:
50+
^ (BranchUniversalObject * _Nullable universalObject, BranchLinkProperties * _Nullable linkProperties, NSError * _Nullable error) {
51+
[self handleDeepLinkObject:universalObject linkProperties:linkProperties error:error];
52+
}];
6353

6454
// Push notification support (Optional)
6555
[self registerForPushNotifications:application];

0 commit comments

Comments
 (0)