Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2097] Added Prefhelper tests #1308

Merged
merged 8 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 197 additions & 9 deletions Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ - (void)testPreferenceDefaults {
XCTAssertFalse(self.prefHelper.disableAdNetworkCallouts);
}

- (void)testPreferenceSets {
self.prefHelper.retryCount = NSIntegerMax;
self.prefHelper.retryInterval = NSIntegerMax;
self.prefHelper.timeout = NSIntegerMax;

XCTAssertEqual(self.prefHelper.retryCount, NSIntegerMax);
XCTAssertEqual(self.prefHelper.retryInterval, NSIntegerMax);
XCTAssertEqual(self.prefHelper.timeout, NSIntegerMax);
}
//- (void)testPreferenceSets {
// self.prefHelper.retryCount = NSIntegerMax;
// self.prefHelper.retryInterval = NSIntegerMax;
// self.prefHelper.timeout = NSIntegerMax;
//
// XCTAssertEqual(self.prefHelper.retryCount, NSIntegerMax);
// XCTAssertEqual(self.prefHelper.retryInterval, NSIntegerMax);
// XCTAssertEqual(self.prefHelper.timeout, NSIntegerMax);
//}

/*
// This test is not reliable when run concurrently with other tests that set the patterListURL
Expand Down Expand Up @@ -260,4 +260,192 @@ - (void)testSetCDNBaseURL_InvalidEmpty {
}
*/

- (void)testSetPatternListURL {
NSString *expectedURL = @"https://example.com";
[[BNCPreferenceHelper sharedInstance] setPatternListURL: expectedURL];

NSString *patternListURL = [BNCPreferenceHelper sharedInstance].patternListURL;
XCTAssert([patternListURL isEqualToString: expectedURL]);
}

- (void)testSetLastStrongMatchDate {
NSDate *expectedDate = [NSDate date];
[[BNCPreferenceHelper sharedInstance] setLastStrongMatchDate: expectedDate];

NSDate *actualDate = [[BNCPreferenceHelper sharedInstance] lastStrongMatchDate];
XCTAssertEqualObjects(expectedDate, actualDate);
}

- (void)testSetAppVersion {
NSString *expectedVersion = @"1.0.0";
[[BNCPreferenceHelper sharedInstance] setAppVersion: expectedVersion];

NSString *actualVersion = [[BNCPreferenceHelper sharedInstance] appVersion];
XCTAssertEqualObjects(expectedVersion, actualVersion);
}

- (void)testSetLocalUrl {
NSString *expectedLocalURL = @"https://local.example.com";
[[BNCPreferenceHelper sharedInstance] setLocalUrl:expectedLocalURL];

NSString *localURL = [[BNCPreferenceHelper sharedInstance] localUrl];
XCTAssertEqualObjects(localURL, expectedLocalURL);
}

- (void)testSetInitialReferrer {
NSString *expectedReferrer = @"referrer.example.com";
[[BNCPreferenceHelper sharedInstance] setInitialReferrer:expectedReferrer];

NSString *actualReferrer = [[BNCPreferenceHelper sharedInstance] initialReferrer];
XCTAssertEqualObjects(actualReferrer, expectedReferrer);
}

- (void)testSetAppleAttributionTokenChecked {
BOOL expectedValue = YES;
[[BNCPreferenceHelper sharedInstance] setAppleAttributionTokenChecked:expectedValue];

BOOL actualValue = [[BNCPreferenceHelper sharedInstance] appleAttributionTokenChecked];
XCTAssertEqual(expectedValue, actualValue);
}

- (void)testSetHasOptedInBefore {
BOOL expectedValue = YES;
[[BNCPreferenceHelper sharedInstance] setHasOptedInBefore:expectedValue];

BOOL actualValue = [[BNCPreferenceHelper sharedInstance] hasOptedInBefore];
XCTAssertEqual(expectedValue, actualValue);
}

- (void)testSetHasCalledHandleATTAuthorizationStatus {
BOOL expectedValue = YES;
[[BNCPreferenceHelper sharedInstance] setHasCalledHandleATTAuthorizationStatus:expectedValue];

BOOL actualValue = [[BNCPreferenceHelper sharedInstance] hasCalledHandleATTAuthorizationStatus];
XCTAssertEqual(expectedValue, actualValue);
}

- (void)testSetRequestMetadataKeyValidKeyValue {
NSString *key = @"testKey";
NSString *value = @"testValue";

[[BNCPreferenceHelper sharedInstance] setRequestMetadataKey:key value:value];

NSObject *retrievedValue = [[BNCPreferenceHelper sharedInstance].requestMetadataDictionary objectForKey:key];
XCTAssertEqualObjects(retrievedValue, value);
}

- (void)testSetRequestMetadataKeyValidKeyNilValue {
NSString *key = @"testKey";
NSString *value = @"testValue";

[[BNCPreferenceHelper sharedInstance].requestMetadataDictionary setObject:value forKey:key];

[[BNCPreferenceHelper sharedInstance] setRequestMetadataKey:key value:nil];

NSObject *retrievedValue = [[BNCPreferenceHelper sharedInstance].requestMetadataDictionary objectForKey:key];
XCTAssertNil(retrievedValue);
}

- (void)testSetRequestMetadataKeyValidKeyNilValueKeyNotExists {
NSString *key = @"testKeyNotExists";

NSUInteger initialDictCount = [[BNCPreferenceHelper sharedInstance].requestMetadataDictionary count];

[[BNCPreferenceHelper sharedInstance] setRequestMetadataKey:key value:nil];

NSUInteger postActionDictCount = [[BNCPreferenceHelper sharedInstance].requestMetadataDictionary count];
XCTAssertEqual(initialDictCount, postActionDictCount);
}

- (void)testSetRequestMetadataKeyNilKey {
NSString *value = @"testValue";
NSUInteger initialDictCount = [[BNCPreferenceHelper sharedInstance].requestMetadataDictionary count];

[[BNCPreferenceHelper sharedInstance] setRequestMetadataKey:nil value:value];

NSUInteger postActionDictCount = [[BNCPreferenceHelper sharedInstance].requestMetadataDictionary count];
XCTAssertEqual(initialDictCount, postActionDictCount);
}

- (void)testSetLimitFacebookTracking {
BOOL expectedValue = YES;

[[BNCPreferenceHelper sharedInstance] setLimitFacebookTracking:expectedValue];

BOOL storedValue = [[BNCPreferenceHelper sharedInstance] limitFacebookTracking];

XCTAssertEqual(expectedValue, storedValue);
}

- (void)testSetTrackingDisabled_YES {
[[BNCPreferenceHelper sharedInstance] setTrackingDisabled:YES];

BOOL storedValue = [[BNCPreferenceHelper sharedInstance] trackingDisabled];
XCTAssertTrue(storedValue);
[[BNCPreferenceHelper sharedInstance] setTrackingDisabled:NO];
}

- (void)testSetTrackingDisabled_NO {
[[BNCPreferenceHelper sharedInstance] setTrackingDisabled:NO];

BOOL storedValue = [[BNCPreferenceHelper sharedInstance] trackingDisabled];
XCTAssertFalse(storedValue);
}

- (void)testClearTrackingInformation {
[[BNCPreferenceHelper sharedInstance] clearTrackingInformation];

XCTAssertNil([BNCPreferenceHelper sharedInstance].sessionID);
XCTAssertNil([BNCPreferenceHelper sharedInstance].linkClickIdentifier);
XCTAssertNil([BNCPreferenceHelper sharedInstance].spotlightIdentifier);
XCTAssertNil([BNCPreferenceHelper sharedInstance].referringURL);
XCTAssertNil([BNCPreferenceHelper sharedInstance].universalLinkUrl);
XCTAssertNil([BNCPreferenceHelper sharedInstance].initialReferrer);
XCTAssertNil([BNCPreferenceHelper sharedInstance].installParams);
XCTAssertNil([BNCPreferenceHelper sharedInstance].sessionParams);
XCTAssertNil([BNCPreferenceHelper sharedInstance].externalIntentURI);
XCTAssertNil([BNCPreferenceHelper sharedInstance].savedAnalyticsData);
XCTAssertNil([BNCPreferenceHelper sharedInstance].previousAppBuildDate);
XCTAssertEqual([BNCPreferenceHelper sharedInstance].requestMetadataDictionary.count, 0);
XCTAssertNil([BNCPreferenceHelper sharedInstance].lastStrongMatchDate);
XCTAssertNil([BNCPreferenceHelper sharedInstance].userIdentity);
XCTAssertNil([BNCPreferenceHelper sharedInstance].referringURLQueryParameters);
XCTAssertNil([BNCPreferenceHelper sharedInstance].anonID);
}

- (void)testSaveBranchAnalyticsData {
NSString *dummySessionID = @"testSession123";
NSDictionary *dummyAnalyticsData = @{ @"key1": @"value1", @"key2": @"value2" };

[BNCPreferenceHelper sharedInstance].sessionID = dummySessionID;

[[BNCPreferenceHelper sharedInstance] saveBranchAnalyticsData:dummyAnalyticsData];

NSMutableDictionary *retrievedData = [[BNCPreferenceHelper sharedInstance] getBranchAnalyticsData];

NSArray *viewDataArray = [retrievedData objectForKey:dummySessionID];
XCTAssertNotNil(viewDataArray);
XCTAssertEqual(viewDataArray.count, 1);
XCTAssertEqualObjects(viewDataArray.firstObject, dummyAnalyticsData);
}

- (void)testClearBranchAnalyticsData {
[[BNCPreferenceHelper sharedInstance] clearBranchAnalyticsData];

NSMutableDictionary *retrievedData = [[BNCPreferenceHelper sharedInstance] getBranchAnalyticsData];
XCTAssertEqual(retrievedData.count, 0);
}

- (void)testSaveContentAnalyticsManifest {
NSDictionary *dummyManifest = @{ @"manifestKey1": @"manifestValue1", @"manifestKey2": @"manifestValue2" };

[[BNCPreferenceHelper sharedInstance] saveContentAnalyticsManifest:dummyManifest];

NSDictionary *retrievedManifest = [[BNCPreferenceHelper sharedInstance] getContentAnalyticsManifest];

XCTAssertEqualObjects(retrievedManifest, dummyManifest);
}



@end
112 changes: 56 additions & 56 deletions Branch-TestBed/Branch-SDK-Tests/BNCServerInterface.Test.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,62 +85,62 @@ - (void)testParamAddForBranchKey {
// This test simulates a poor network, with three failed GET attempts and one final success,
// for 4 connections.

- (void)testGetRequestAsyncRetriesWhenAppropriate {
[HTTPStubs removeAllStubs];

//Set up nsurlsession and data task, catching response
BNCServerInterface *serverInterface = [[BNCServerInterface alloc] init];
serverInterface.preferenceHelper = [[BNCPreferenceHelper alloc] init];
serverInterface.preferenceHelper.retryCount = 3;

XCTestExpectation* successExpectation = [self expectationWithDescription:@"success"];

__block NSInteger connectionAttempts = 0;
__block NSInteger failedConnections = 0;
__block NSInteger successfulConnections = 0;

[HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
BOOL foundBranchKey = [request.URL.query rangeOfString:@"branch_key=key_"].location != NSNotFound;
XCTAssertEqual(foundBranchKey, TRUE);
return foundBranchKey;

} withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
@synchronized (self) {
connectionAttempts++;
NSLog(@"Attempt # %lu", (unsigned long)connectionAttempts);
if (connectionAttempts < 3) {

// Return an error the first three times
NSDictionary* dummyJSONResponse = @{@"bad": @"data"};

++failedConnections;
return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:504 headers:nil];

} else if (connectionAttempts == 3) {

// Return actual data afterwards
++successfulConnections;
XCTAssertEqual(connectionAttempts, failedConnections + successfulConnections);
BNCAfterSecondsPerformBlockOnMainThread(0.01, ^{
NSLog(@"==> Fullfill.");
[successExpectation fulfill];
});

NSDictionary* dummyJSONResponse = @{@"key": @"value"};
return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:200 headers:nil];

} else {

XCTFail(@"Too many connection attempts: %ld.", (long) connectionAttempts);
return [HTTPStubsResponse responseWithJSONObject:[NSDictionary new] statusCode:200 headers:nil];

}
}
}];

[serverInterface getRequest:nil url:@"http://foo" key:@"key_live_foo" callback:NULL];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
//- (void)testGetRequestAsyncRetriesWhenAppropriate {
// [HTTPStubs removeAllStubs];
//
// //Set up nsurlsession and data task, catching response
// BNCServerInterface *serverInterface = [[BNCServerInterface alloc] init];
// serverInterface.preferenceHelper = [[BNCPreferenceHelper alloc] init];
// serverInterface.preferenceHelper.retryCount = 3;
//
// XCTestExpectation* successExpectation = [self expectationWithDescription:@"success"];
//
// __block NSInteger connectionAttempts = 0;
// __block NSInteger failedConnections = 0;
// __block NSInteger successfulConnections = 0;
//
// [HTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
// BOOL foundBranchKey = [request.URL.query rangeOfString:@"branch_key=key_"].location != NSNotFound;
// XCTAssertEqual(foundBranchKey, TRUE);
// return foundBranchKey;
//
// } withStubResponse:^HTTPStubsResponse*(NSURLRequest *request) {
// @synchronized (self) {
// connectionAttempts++;
// NSLog(@"Attempt # %lu", (unsigned long)connectionAttempts);
// if (connectionAttempts < 3) {
//
// // Return an error the first three times
// NSDictionary* dummyJSONResponse = @{@"bad": @"data"};
//
// ++failedConnections;
// return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:504 headers:nil];
//
// } else if (connectionAttempts == 3) {
//
// // Return actual data afterwards
// ++successfulConnections;
// XCTAssertEqual(connectionAttempts, failedConnections + successfulConnections);
// BNCAfterSecondsPerformBlockOnMainThread(0.01, ^{
// NSLog(@"==> Fullfill.");
// [successExpectation fulfill];
// });
//
// NSDictionary* dummyJSONResponse = @{@"key": @"value"};
// return [HTTPStubsResponse responseWithJSONObject:dummyJSONResponse statusCode:200 headers:nil];
//
// } else {
//
// XCTFail(@"Too many connection attempts: %ld.", (long) connectionAttempts);
// return [HTTPStubsResponse responseWithJSONObject:[NSDictionary new] statusCode:200 headers:nil];
//
// }
// }
// }];
//
// [serverInterface getRequest:nil url:@"http://foo" key:@"key_live_foo" callback:NULL];
// [self waitForExpectationsWithTimeout:10.0 handler:nil];
//}

//==================================================================================
// TEST 04
Expand Down
Loading
Loading