Skip to content

Commit 2674dd5

Browse files
committed
CORE-2088 add tests
1 parent 053d075 commit 2674dd5

File tree

4 files changed

+186
-8
lines changed

4 files changed

+186
-8
lines changed

Branch-SDK/BNCPasteboard.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
@interface BNCPasteboard : NSObject
1414

15-
// For v1, we only allow check on install requests. By default, this is NO
16-
@property (nonatomic,assign) BOOL checkOnInstall;
17-
18-
@property (nonatomic, assign, nullable) NSURL *branchLink;
15+
/*
16+
Indicates if the client wishes to check for Branch links on install. By default, this is NO.
17+
18+
Set via Branch.checkPasteboardOnInstall
19+
Checked by BranchInstallRequest.makeRequest before checking the pasteboard for a Branch link.
20+
*/
21+
@property (nonatomic, assign) BOOL checkOnInstall;
1922

2023
- (BOOL)isUrlOnPasteboard;
21-
2224
- (nullable NSURL *)checkForBranchLink;
2325

2426
+ (BNCPasteboard *)sharedInstance;

Branch-SDK/BNCPasteboard.m

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,36 @@ - (instancetype)init {
2929
return self;
3030
}
3131

32+
- (void)clearPasteboard {
33+
#if !TARGET_OS_TV
34+
if (@available(iOS 10.0, *)) {
35+
// cannot delete items from the pasteboard, but we can put something else on there
36+
[[UIPasteboard generalPasteboard] setString:@""];
37+
}
38+
#endif
39+
}
40+
3241
- (BOOL)isUrlOnPasteboard {
33-
#if !TARGET_OS_TV
42+
#if !TARGET_OS_TV
3443
if (@available(iOS 10.0, *)) {
3544
if ([UIPasteboard.generalPasteboard hasURLs]) {
3645
return YES;
3746
}
3847
}
39-
#endif
48+
#endif
4049
return NO;
4150
}
4251

4352
- (nullable NSURL *)checkForBranchLink {
4453
if ([self isUrlOnPasteboard]) {
54+
#if !TARGET_OS_TV
4555
// triggers the end user toast message
4656
NSURL *tmp = UIPasteboard.generalPasteboard.URL;
4757
if ([Branch isBranchLink:tmp.absoluteString]) {
48-
self.branchLink = tmp;
58+
[self clearPasteboard];
4959
return tmp;
5060
}
61+
#endif
5162
}
5263
return nil;
5364
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
//
2+
// BNCPasteboardTests.m
3+
// Branch-SDK-Tests
4+
//
5+
// Created by Ernest Cho on 7/19/21.
6+
// Copyright © 2021 Branch, Inc. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
#import "BNCPasteboard.h"
11+
12+
@interface BNCPasteboardTests : XCTestCase
13+
14+
@property (nonatomic, assign, readwrite) NSString *testString;
15+
@property (nonatomic, strong, readwrite) NSURL *testBranchURL;
16+
17+
@end
18+
19+
@implementation BNCPasteboardTests
20+
21+
- (void)setUp {
22+
// Put setup code here. This method is called before the invocation of each test method in the class.
23+
self.testString = @"Pasteboard String";
24+
self.testBranchURL = [NSURL URLWithString:@"https://123.app.link"];
25+
}
26+
27+
- (void)tearDown {
28+
// Put teardown code here. This method is called after the invocation of each test method in the class.
29+
}
30+
31+
- (void)addStringToPasteboard {
32+
#if !TARGET_OS_TV
33+
if (@available(iOS 10.0, *)) {
34+
[UIPasteboard.generalPasteboard setString:self.testString];
35+
}
36+
#endif
37+
}
38+
39+
- (void)addBranchURLToPasteboard {
40+
#if !TARGET_OS_TV
41+
if (@available(iOS 10.0, *)) {
42+
[UIPasteboard.generalPasteboard setURL:self.testBranchURL];
43+
}
44+
#endif
45+
}
46+
47+
- (void)addNonBranchURLToPasteboard {
48+
#if !TARGET_OS_TV
49+
if (@available(iOS 10.0, *)) {
50+
[UIPasteboard.generalPasteboard setURL:[NSURL URLWithString:@"https://www.apple.com"]];
51+
}
52+
#endif
53+
}
54+
55+
- (void)clearPasteboard {
56+
#if !TARGET_OS_TV
57+
if (@available(iOS 10.0, *)) {
58+
// cannot delete items from the pasteboard, but we can put something else on there
59+
[[UIPasteboard generalPasteboard] setString:@""];
60+
}
61+
#endif
62+
}
63+
64+
- (NSString *)getStringFromClipboard {
65+
NSString *string = nil;
66+
#if !TARGET_OS_TV
67+
if (@available(iOS 10.0, *)) {
68+
string = [UIPasteboard.generalPasteboard string];
69+
}
70+
#endif
71+
return string;
72+
}
73+
74+
- (NSURL *)getURLFromPasteboard {
75+
NSURL *url = nil;
76+
#if !TARGET_OS_TV
77+
if (@available(iOS 10.0, *)) {
78+
url = [UIPasteboard.generalPasteboard URL];
79+
}
80+
#endif
81+
return url;
82+
}
83+
84+
- (void)testStringUtilityMethods {
85+
86+
// set and retrieve a string
87+
[self addStringToPasteboard];
88+
NSString *tmp = [self getStringFromClipboard];
89+
XCTAssert([self.testString isEqualToString:tmp]);
90+
91+
// overwrite the pasteboard
92+
[self clearPasteboard];
93+
tmp = [self getStringFromClipboard];
94+
XCTAssert([@"" isEqualToString:tmp]);
95+
}
96+
97+
- (void)testURLUtilityMethods {
98+
99+
// set and retrieve a url
100+
[self addBranchURLToPasteboard];
101+
NSURL *tmp = [self getURLFromPasteboard];
102+
XCTAssert([self.testBranchURL.absoluteString isEqualToString:tmp.absoluteString]);
103+
104+
// overwrite the pasteboard
105+
[self clearPasteboard];
106+
tmp = [self getURLFromPasteboard];
107+
XCTAssertNil(tmp);
108+
}
109+
110+
- (void)testDefaultState {
111+
// host app sets this to true, should consider a no-op test host
112+
XCTAssertTrue([BNCPasteboard sharedInstance].checkOnInstall);
113+
}
114+
115+
- (void)testIsUrlOnPasteboard {
116+
XCTAssertFalse([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
117+
118+
[self addBranchURLToPasteboard];
119+
XCTAssertTrue([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
120+
121+
[self clearPasteboard];
122+
XCTAssertFalse([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
123+
}
124+
125+
- (void)testCheckForBranchLink {
126+
[self addBranchURLToPasteboard];
127+
XCTAssertTrue([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
128+
129+
NSURL *tmp = [[BNCPasteboard sharedInstance] checkForBranchLink];
130+
XCTAssert([self.testBranchURL.absoluteString isEqualToString:tmp.absoluteString]);
131+
132+
// confirms Branch link is deleted after use
133+
XCTAssertFalse([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
134+
}
135+
136+
- (void)testCheckForBranchLink_nonBranchLink {
137+
[self addNonBranchURLToPasteboard];
138+
XCTAssertTrue([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
139+
140+
NSURL *tmp = [[BNCPasteboard sharedInstance] checkForBranchLink];
141+
XCTAssertNil(tmp);
142+
143+
// confirms non-Branch link is still on the pasteboard
144+
XCTAssertTrue([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
145+
146+
// deletes non-Branch link from pasteboard
147+
[self clearPasteboard];
148+
XCTAssertFalse([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
149+
}
150+
151+
- (void)testCheckForBranchLink_noLink {
152+
[self addStringToPasteboard];
153+
XCTAssertFalse([[BNCPasteboard sharedInstance] isUrlOnPasteboard]);
154+
155+
NSURL *tmp = [[BNCPasteboard sharedInstance] checkForBranchLink];
156+
XCTAssertNil(tmp);
157+
158+
[self clearPasteboard];
159+
}
160+
161+
@end

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
54FF1F921BD1DC320004CE2E /* BranchLinkProperties.m in Sources */ = {isa = PBXBuildFile; fileRef = 54FF1F901BD1DC320004CE2E /* BranchLinkProperties.m */; };
124124
5F08461123480008005B17E6 /* BNCTuneUtility.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F08460F23480008005B17E6 /* BNCTuneUtility.h */; };
125125
5F08461223480008005B17E6 /* BNCTuneUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F08461023480008005B17E6 /* BNCTuneUtility.m */; };
126+
5F1166C626A60B91004825E3 /* BNCPasteboardTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F1166C526A60B91004825E3 /* BNCPasteboardTests.m */; };
126127
5F205D05231864E800C776D1 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F205D04231864E800C776D1 /* WebKit.framework */; settings = {ATTRIBUTES = (Required, ); }; };
127128
5F205D062318659500C776D1 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5F205D04231864E800C776D1 /* WebKit.framework */; };
128129
5F205D0823186AF700C776D1 /* BNCUserAgentCollectorTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F205D022318641700C776D1 /* BNCUserAgentCollectorTests.m */; };
@@ -440,6 +441,7 @@
440441
54FF1F901BD1DC320004CE2E /* BranchLinkProperties.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BranchLinkProperties.m; sourceTree = "<group>"; };
441442
5F08460F23480008005B17E6 /* BNCTuneUtility.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BNCTuneUtility.h; sourceTree = "<group>"; };
442443
5F08461023480008005B17E6 /* BNCTuneUtility.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCTuneUtility.m; sourceTree = "<group>"; };
444+
5F1166C526A60B91004825E3 /* BNCPasteboardTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCPasteboardTests.m; sourceTree = "<group>"; };
443445
5F2035CB240DDE90004FDC3E /* BNCDisableAdNetworkCalloutsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCDisableAdNetworkCalloutsTests.m; sourceTree = "<group>"; };
444446
5F205D022318641700C776D1 /* BNCUserAgentCollectorTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BNCUserAgentCollectorTests.m; sourceTree = "<group>"; };
445447
5F205D04231864E800C776D1 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
@@ -763,6 +765,7 @@
763765
5F2035CB240DDE90004FDC3E /* BNCDisableAdNetworkCalloutsTests.m */,
764766
5FDB04F324E6156800F2F267 /* BNCSKAdNetworkTests.m */,
765767
5FDF91582581CDF4009BE5A3 /* BNCPartnerParametersTests.m */,
768+
5F1166C526A60B91004825E3 /* BNCPasteboardTests.m */,
766769
);
767770
path = "Branch-SDK-Tests";
768771
sourceTree = "<group>";
@@ -1562,6 +1565,7 @@
15621565
4D1683BD2098C902008819E3 /* BranchNetworkScenario.Test.m in Sources */,
15631566
4D7881FF209CF2D4002B750F /* BNCApplication+BNCTest.m in Sources */,
15641567
5F92B23423835FEB00CA909B /* BNCReachabilityTests.m in Sources */,
1568+
5F1166C626A60B91004825E3 /* BNCPasteboardTests.m in Sources */,
15651569
5F3D671B233062FD00454FF1 /* BNCJsonLoader.m in Sources */,
15661570
4D1683C02098C902008819E3 /* BranchUniversalObject.Test.m in Sources */,
15671571
5F5EC4602374E96B00AAAFC7 /* BNCAppleAdClientTests.m in Sources */,

0 commit comments

Comments
 (0)