|
| 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 |
0 commit comments