Skip to content

Commit

Permalink
Merge pull request #1118 from BranchMetrics/CORE-1946-fix-old-Apple-S…
Browse files Browse the repository at this point in the history
…earch-Ads-tests

CORE-1946 fix unit tests for old Apple Search Ads
  • Loading branch information
echo-branch authored May 18, 2021
2 parents 172bd5d + 5d0b11c commit 0b36763
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 56 deletions.
33 changes: 21 additions & 12 deletions Branch-TestBed/Branch-SDK-Tests/BNCAppleAdClientTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,29 @@ - (void)testRequestAttribution {

BNCAppleAdClient *adClient = [BNCAppleAdClient new];
[adClient requestAttributionDetailsWithBlock:^(NSDictionary<NSString *,NSObject *> * _Nonnull attributionDetails, NSError * _Nonnull error) {
XCTAssertNil(error);

id tmp = [attributionDetails objectForKey:@"Version3.1"];
if ([tmp isKindOfClass:NSDictionary.class]) {
NSDictionary *tmpDict = (NSDictionary *)tmp;
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

if (@available(iOS 14.5, *)) {
// Need ATT permission to use get old Apple Search Ads info
XCTAssertNotNil(error);
XCTAssert([@"The app is not authorized for ad tracking" isEqualToString:error.localizedDescription]);
[expectation fulfill];

} else {
XCTFail(@"Did not find Search Ads attribution");
XCTAssertNil(error);

id tmp = [attributionDetails objectForKey:@"Version3.1"];
if ([tmp isKindOfClass:NSDictionary.class]) {
NSDictionary *tmpDict = (NSDictionary *)tmp;
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);
} else {
XCTFail(@"Did not find Search Ads attribution");
}

[expectation fulfill];
}

[expectation fulfill];
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
Expand Down
127 changes: 83 additions & 44 deletions Branch-TestBed/Branch-SDK-Tests/BNCAppleSearchAdsTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,24 @@ - (void)testRequestAppleSearchAds {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"AppleSearchAds"];

[self.appleSearchAds requestAttributionWithCompletion:^(NSDictionary * _Nullable attributionDetails, NSError * _Nullable error, NSTimeInterval elapsedSeconds) {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

[expectation fulfill];
if (@available(iOS 14.5, *)) {
// Need ATT permission to use get old Apple Search Ads info
XCTAssertNotNil(error);
XCTAssertTrue(elapsedSeconds > 0);
[expectation fulfill];

} else {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

[expectation fulfill];
}
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
Expand All @@ -216,16 +224,25 @@ - (void)testRequestAppleSearchAdsWithRetry_0 {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"AppleSearchAds"];

[self.appleSearchAds requestAttributionWithMaxAttempts:0 completion:^(NSDictionary * _Nullable attributionDetails, NSError * _Nullable error, NSTimeInterval elapsedSeconds) {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

[expectation fulfill];
if (@available(iOS 14.5, *)) {
// Need ATT permission to use get old Apple Search Ads info
XCTAssertNotNil(error);
XCTAssert([@"The app is not authorized for ad tracking" isEqualToString:error.localizedDescription]);
XCTAssertTrue(elapsedSeconds > 0);
[expectation fulfill];

} else {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

[expectation fulfill];
}
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
Expand All @@ -238,16 +255,25 @@ - (void)testRequestAppleSearchAdsWithRetry_1 {
__block XCTestExpectation *expectation = [self expectationWithDescription:@"AppleSearchAds"];

[self.appleSearchAds requestAttributionWithMaxAttempts:1 completion:^(NSDictionary * _Nullable attributionDetails, NSError * _Nullable error, NSTimeInterval elapsedSeconds) {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

[expectation fulfill];
if (@available(iOS 14.5, *)) {
// Need ATT permission to use get old Apple Search Ads info
XCTAssertNotNil(error);
XCTAssert([@"The app is not authorized for ad tracking" isEqualToString:error.localizedDescription]);
XCTAssertTrue(elapsedSeconds > 0);
[expectation fulfill];

} else {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

[expectation fulfill];
}
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
Expand All @@ -265,7 +291,7 @@ - (void)testRequestAppleSearchAdsWithRetry_NoResponse {
XCTAssertNotNil(error);
XCTAssertTrue(elapsedSeconds > 0);
XCTAssertNil(attributionDetails);

[expectation fulfill];
}];

Expand All @@ -283,19 +309,32 @@ - (void)testRequestAppleSearchAdsWithRetry_3 {
self.appleSearchAds.adClient = mock;

[self.appleSearchAds requestAttributionWithMaxAttempts:3 completion:^(NSDictionary * _Nullable attributionDetails, NSError * _Nullable error, NSTimeInterval elapsedSeconds) {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

// verifies things were ignored
XCTAssert(mock.ignoreCount == 2);

[expectation fulfill];
if (@available(iOS 14.5, *)) {
// Need ATT permission to use get old Apple Search Ads info
XCTAssertNotNil(error);
XCTAssert([@"The app is not authorized for ad tracking" isEqualToString:error.localizedDescription]);
XCTAssertTrue(elapsedSeconds > 0);

// verifies things were ignored
XCTAssert(mock.ignoreCount == 2);

[expectation fulfill];

} else {
XCTAssertNil(error);
XCTAssertTrue(elapsedSeconds > 0);

NSDictionary *tmpDict = [attributionDetails objectForKey:@"Version3.1"];
XCTAssertNotNil(tmpDict);

NSNumber *tmpBool = [tmpDict objectForKey:@"iad-attribution"];
XCTAssertNotNil(tmpBool);

// verifies things were ignored
XCTAssert(mock.ignoreCount == 2);

[expectation fulfill];
}
}];

[self waitForExpectationsWithTimeout:5 handler:^(NSError * _Nullable error) {
Expand Down

0 comments on commit 0b36763

Please sign in to comment.