Skip to content

Commit

Permalink
Merge branch 'master' into SDK-2182-tuist-testing-and-validation
Browse files Browse the repository at this point in the history
# Conflicts:
#	Branch-TestBed/Branch-TestBed.xcodeproj/project.pbxproj
#	BranchSDK.xcodeproj/project.pbxproj
#	Sources/BranchSDK/BNCLog.m
#	Sources/BranchSDK/BNCServerInterface.m
#	Sources/BranchSDK/Branch.m
#	Sources/BranchSDK/BranchLogger.h
#	Sources/BranchSDK/BranchLogger.m
#	Sources/BranchSDK/Private/BNCLog.h
  • Loading branch information
echo-branch committed Feb 15, 2024
2 parents 7d95b4c + 577089b commit a8f50d0
Show file tree
Hide file tree
Showing 54 changed files with 743 additions and 903 deletions.
39 changes: 39 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BNCAPIServerTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "BNCSystemObserver.h"
#import "BNCConfig.h"
#import "BranchConstants.h"
#import "Branch.h"

@interface BNCAPIServerTest : XCTestCase

Expand Down Expand Up @@ -370,4 +371,42 @@ - (void)testValidationServiceURL_EUTracking {
XCTAssertTrue([url hasPrefix:expectedUrlPrefix]);
}

- (void)testDefaultAPIURL {
BNCServerAPI *serverAPI = [BNCServerAPI new];
XCTAssertNil(serverAPI.customAPIURL);

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);
}

- (void)testSetAPIURL_Example {
NSString *url = @"https://www.example.com";
[Branch setAPIUrl:url];

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [url stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);

[Branch setAPIUrl:BNC_API_URL];
}

- (void)testSetAPIURL_InvalidHttp {
NSString *url = @"Invalid://www.example.com";
[Branch setAPIUrl:url];

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);
}

- (void)testSetAPIURL_InvalidEmpty {
NSString *url = @"";
[Branch setAPIUrl:url];

NSString *storedUrl = [[BNCServerAPI sharedInstance] installServiceURL];
NSString *expectedUrl = [BNC_API_URL stringByAppendingString: @"/v1/install"];
XCTAssertEqualObjects(storedUrl, expectedUrl);
}

@end
43 changes: 0 additions & 43 deletions Branch-TestBed/Branch-SDK-Tests/BNCLogTests.m

This file was deleted.

28 changes: 0 additions & 28 deletions Branch-TestBed/Branch-SDK-Tests/BNCPreferenceHelperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,6 @@ - (void)testURLSkipList {
XCTAssert([filterDesc isEqualToString:valueDesc]);
}

- (void)testSetAPIURL_Example {

NSString *url = @"https://www.example.com/";
[self.prefHelper setBranchAPIURL:url] ;

NSString *urlStored = self.prefHelper.branchAPIURL ;
XCTAssert([url isEqualToString:urlStored]);
}

- (void)testSetAPIURL_InvalidHttp {

NSString *url = @"Invalid://www.example.com/";
[self.prefHelper setBranchAPIURL:url] ;

NSString *urlStored = self.prefHelper.branchAPIURL ;
XCTAssert(![url isEqualToString:urlStored]);
XCTAssert([urlStored isEqualToString:BNC_API_URL]);
}

- (void)testSetAPIURL_InvalidEmpty {

[self.prefHelper setBranchAPIURL:@""] ;

NSString *urlStored = self.prefHelper.branchAPIURL ;
XCTAssert(![urlStored isEqualToString:@""]);
XCTAssert([urlStored isEqualToString:BNC_API_URL]);
}

- (void)testSetCDNBaseURL_Example {

NSString *url = @"https://www.example.com/";
Expand Down
2 changes: 0 additions & 2 deletions Branch-TestBed/Branch-SDK-Tests/BNCTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/

#import "BNCTestCase.h"
#import "BNCLog.h"
#import "Branch.h"
#import "BNCApplication+BNCTest.h"

Expand Down Expand Up @@ -96,7 +95,6 @@ - (double) systemVersion {

+ (void) initialize {
if (self != [BNCTestCase self]) return;
BNCLogSetDisplayLevel(BNCLogLevelAll);

savedRandomizedBundleToken = [BNCPreferenceHelper sharedInstance].randomizedBundleToken;
[Branch clearAll];
Expand Down
21 changes: 21 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BranchClassTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#import "BNCAppGroupsData.h"
#import "BNCPartnerParameters.h"

@interface BNCPreferenceHelper(Test)
// Expose internal private method to clear EEA data
- (void)writeObjectToDefaults:(NSString *)key value:(NSObject *)value;
@end

@interface BranchClassTests : XCTestCase
@property (nonatomic, strong) Branch *branch;
@end
Expand Down Expand Up @@ -220,4 +225,20 @@ - (void)testGetLongURLWithParamsAndChannelAndTagsAndFeatureAndStageAndAlias {
XCTAssertEqualObjects(generatedURL, expectedURL, @"URL should match the expected format");
}

- (void)testSetDMAParamsForEEA {
XCTAssertFalse([[BNCPreferenceHelper sharedInstance] eeaRegionInitialized]);

[Branch setDMAParamsForEEA:FALSE AdPersonalizationConsent:TRUE AdUserDataUsageConsent:TRUE];
XCTAssertTrue([[BNCPreferenceHelper sharedInstance] eeaRegionInitialized]);
XCTAssertFalse([BNCPreferenceHelper sharedInstance].eeaRegion);
XCTAssertTrue([BNCPreferenceHelper sharedInstance].adPersonalizationConsent);
XCTAssertTrue([BNCPreferenceHelper sharedInstance].adUserDataUsageConsent);

// Manually clear values after testing
// By design, this API is meant to be set once and always set. However, in a test scenario it needs to be cleared.
[[BNCPreferenceHelper sharedInstance] writeObjectToDefaults:@"bnc_dma_eea" value:nil];
[[BNCPreferenceHelper sharedInstance] writeObjectToDefaults:@"bnc_dma_ad_personalization" value:nil];
[[BNCPreferenceHelper sharedInstance] writeObjectToDefaults:@"bnc_dma_ad_user_data" value:nil];
}

@end
109 changes: 109 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BranchLoggerTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//
// BranchLoggerTests.m
// Branch-SDK-Tests
//
// Created by Nipun Singh on 2/5/24.
// Copyright © 2024 Branch, Inc. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "BranchLogger.h"
#import "Branch.h"

@interface BranchLoggerTests : XCTestCase
@end

@implementation BranchLoggerTests

- (void)testEnableLoggingSetsCorrectDefaultLevel {
[[Branch getInstance] enableLogging];
XCTAssertEqual([BranchLogger shared].logLevelThreshold, BranchLogLevelDebug, "Default log level should be Debug.");
}

- (void)testLogLevelThresholdBlocksLowerLevels {
BranchLogger *logger = [BranchLogger new];
logger.loggingEnabled = true;
logger.logLevelThreshold = BranchLogLevelDebug;

XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation for message that should pass the threshold"];

logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
if ([message isEqualToString:@"[BranchSDK][Debug][BranchLoggerTests testLogLevelThresholdBlocksLowerLevels] This message should trigger the log callback."] && logLevel >= BranchLogLevelDebug) {
[expectation fulfill];
} else if (logLevel == BranchLogLevelVerbose) {
XCTFail();
}
};

[logger logVerbose:@"This verbose message should not trigger the log callback."];
[logger logDebug:@"This message should trigger the log callback."];

[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)testLogCallbackExecutesWithCorrectParameters {
XCTestExpectation *expectation = [self expectationWithDescription:@"Log callback expectation"];
NSString *expectedMessage = @"[BranchSDK][Info][BranchLoggerTests testLogCallbackExecutesWithCorrectParameters] Test message";
BranchLogLevel expectedLevel = BranchLogLevelInfo;

BranchLogger *logger = [BranchLogger new];

logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
XCTAssertEqualObjects(message, expectedMessage, "Logged message does not match expected message.");
XCTAssertEqual(logLevel, expectedLevel, "Logged level does not match expected level.");
XCTAssertNil(error, "Error should be nil.");
[expectation fulfill];
};

logger.loggingEnabled = YES;
logger.logLevelThreshold = BranchLogLevelInfo;
[logger logInfo:@"Test message"];

[self waitForExpectationsWithTimeout:1 handler:nil];
}

- (void)testLogLevelSpecificityFiltersLowerLevels {
BranchLogger *logger = [BranchLogger new];
logger.loggingEnabled = YES;
logger.logLevelThreshold = BranchLogLevelWarning;

XCTestExpectation *verboseExpectation = [self expectationWithDescription:@"Verbose log callback"];
verboseExpectation.inverted = YES;
XCTestExpectation *errorExpectation = [self expectationWithDescription:@"Error log callback"];

__block NSUInteger callbackCount = 0;
logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
if (logLevel == BranchLogLevelVerbose) {
[verboseExpectation fulfill];
} else if (logLevel == BranchLogLevelError) {
[errorExpectation fulfill];
}
callbackCount++;
};

[logger logVerbose:@"This should not be logged due to log level threshold."];
[logger logError:@"This should be logged" error:nil];

[self waitForExpectations:@[verboseExpectation, errorExpectation] timeout:2];
XCTAssertEqual(callbackCount, 1, "Only one log callback should have been invoked.");
}

- (void)testErrorLoggingIncludesErrorDetails {
BranchLogger *logger = [BranchLogger new];
logger.loggingEnabled = YES;

XCTestExpectation *expectation = [self expectationWithDescription:@"Error log includes error details"];

NSError *testError = [NSError errorWithDomain:@"TestDomain" code:42 userInfo:@{NSLocalizedDescriptionKey: @"Test error description"}];
logger.logCallback = ^(NSString * _Nonnull message, BranchLogLevel logLevel, NSError * _Nullable error) {
if ([message containsString:@"Test error description"] && error == testError) {
[expectation fulfill];
}
};

[logger logError:@"Testing error logging" error:testError];

[self waitForExpectationsWithTimeout:1 handler:nil];
}

@end
1 change: 0 additions & 1 deletion Branch-TestBed/Branch-SDK-Tests/BranchOpenRequestTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#import "BNCTestCase.h"
#import "Branch.h"
#import "BNCLog.h"
#import "BNCApplication+BNCTest.h"
#import "BranchOpenRequest.h"
#import "BranchConstants.h"
Expand Down
Loading

0 comments on commit a8f50d0

Please sign in to comment.