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-2163] Added new BranchLogger class #1346

Merged
merged 12 commits into from
Feb 15, 2024
43 changes: 0 additions & 43 deletions Branch-TestBed/Branch-SDK-Tests/BNCLogTests.m

This file was deleted.

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
12 changes: 11 additions & 1 deletion 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 @@ -221,14 +226,19 @@ - (void)testGetLongURLWithParamsAndChannelAndTagsAndFeatureAndStageAndAlias {
}

- (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
Loading