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