Skip to content

Commit

Permalink
Merge pull request #1346 from BranchMetrics/SDK-2163
Browse files Browse the repository at this point in the history
[SDK-2163] Added new BranchLogger class
  • Loading branch information
echo-branch authored Feb 15, 2024
2 parents 6a43f49 + 1c4df07 commit 577089b
Show file tree
Hide file tree
Showing 44 changed files with 553 additions and 570 deletions.
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

0 comments on commit 577089b

Please sign in to comment.