|
| 1 | +// |
| 2 | +// BNCODMTests.m |
| 3 | +// Branch-SDK-Tests |
| 4 | +// |
| 5 | +// Created by Nidhi Dixit on 4/16/25. |
| 6 | +// Copyright © 2025 Branch, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import "Branch.h" |
| 11 | +#import "BNCPreferenceHelper.h" |
| 12 | +#import "BNCRequestFactory.h" |
| 13 | +#import "BNCEncodingUtils.h" |
| 14 | +#import "BNCODMInfoCollector.h" |
| 15 | +#import "NSError+Branch.h" |
| 16 | + |
| 17 | +@interface BNCODMTests : XCTestCase |
| 18 | +@property (nonatomic, strong, readwrite) BNCPreferenceHelper *prefHelper; |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation BNCODMTests |
| 22 | + |
| 23 | +- (void)setUp { |
| 24 | + _prefHelper = [BNCPreferenceHelper sharedInstance]; |
| 25 | +} |
| 26 | + |
| 27 | +- (void)testSetODM { |
| 28 | + NSString *odm = @"testODMString"; |
| 29 | + NSDate *firstOpenTS = [NSDate date]; |
| 30 | + [Branch setODMInfo:odm andFirstOpenTimestamp:firstOpenTS]; |
| 31 | + XCTAssertTrue([_prefHelper.odmInfo isEqualToString:odm]); |
| 32 | + XCTAssertTrue([_prefHelper.odmInfoInitDate isEqualToDate:firstOpenTS]); |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +- (void)testSetODMandSDKRequests { |
| 37 | + NSString* requestUUID = [[NSUUID UUID ] UUIDString]; |
| 38 | + NSNumber* requestCreationTimeStamp = BNCWireFormatFromDate([NSDate date]); |
| 39 | + NSString *odm = @"testODMString"; |
| 40 | + NSDate *firstOpenTS = [NSDate date]; |
| 41 | + |
| 42 | + [Branch setODMInfo:odm andFirstOpenTimestamp:firstOpenTS]; |
| 43 | + |
| 44 | + [[Branch getInstance] setConsumerProtectionAttributionLevel:BranchAttributionLevelFull]; |
| 45 | + BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:requestUUID TimeStamp:requestCreationTimeStamp]; |
| 46 | + NSDictionary *jsonInstall = [factory dataForInstallWithURLString:@"https://branch.io"]; |
| 47 | + XCTAssertTrue([odm isEqualToString:[jsonInstall objectForKey:@"odm_info"]]); |
| 48 | + |
| 49 | + NSDictionary *jsonOpen = [factory dataForOpenWithURLString:@"https://branch.io"]; |
| 50 | + XCTAssertTrue([odm isEqualToString:[jsonOpen objectForKey:@"odm_info"]]); |
| 51 | + |
| 52 | + NSDictionary *event = @{@"name": @"ADD_TO_CART"}; |
| 53 | + NSDictionary *jsonEvent = [factory dataForEventWithEventDictionary:[event mutableCopy]]; |
| 54 | + XCTAssertTrue([jsonEvent objectForKey:@"odm_info"] == nil); |
| 55 | + |
| 56 | + [[Branch getInstance] setConsumerProtectionAttributionLevel:BranchAttributionLevelReduced]; |
| 57 | + jsonInstall = [factory dataForInstallWithURLString:@"https://branch.io"]; |
| 58 | + XCTAssertTrue([jsonInstall objectForKey:@"odm_info"] == nil); |
| 59 | + |
| 60 | + jsonOpen = [factory dataForOpenWithURLString:@"https://branch.io"]; |
| 61 | + XCTAssertTrue([jsonOpen objectForKey:@"odm_info"] == nil); |
| 62 | + |
| 63 | + self.prefHelper.odmInfo = nil; |
| 64 | + self.prefHelper.odmInfoInitDate = nil; |
| 65 | +} |
| 66 | + |
| 67 | +- (void)testODMTimeOut { |
| 68 | + |
| 69 | + NSString* requestUUID = [[NSUUID UUID ] UUIDString]; |
| 70 | + NSNumber* requestCreationTimeStamp = BNCWireFormatFromDate([NSDate date]); |
| 71 | + NSString *odm = @"testODMString"; |
| 72 | + NSDate *firstOpenTS = [[NSDate date] dateByAddingTimeInterval:-((180*24*3600) - 5)]; |
| 73 | + |
| 74 | + [Branch setODMInfo:odm andFirstOpenTimestamp:firstOpenTS]; |
| 75 | + |
| 76 | + [[Branch getInstance] setConsumerProtectionAttributionLevel:BranchAttributionLevelFull]; |
| 77 | + BNCRequestFactory *factory = [[BNCRequestFactory alloc] initWithBranchKey:@"key_abcd" UUID:requestUUID TimeStamp:requestCreationTimeStamp]; |
| 78 | + NSDictionary *jsonInstall = [factory dataForInstallWithURLString:@"https://branch.io"]; |
| 79 | + XCTAssertTrue([odm isEqualToString:[jsonInstall objectForKey:@"odm_info"]]); |
| 80 | + |
| 81 | + sleep(10); |
| 82 | + |
| 83 | + NSDictionary *jsonOpen = [factory dataForOpenWithURLString:@"https://branch.io"]; |
| 84 | + XCTAssertTrue(![odm isEqualToString:[jsonOpen objectForKey:@"odm_info"]]); |
| 85 | + |
| 86 | + self.prefHelper.odmInfo = nil; |
| 87 | + self.prefHelper.odmInfoInitDate = nil; |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +- (void) testODMAPIsNotLoaded { |
| 93 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Check if ODCManager class is loaded."]; |
| 94 | + [[BNCODMInfoCollector instance ] loadODMInfoWithTimeOut:DISPATCH_TIME_FOREVER andCompletionHandler:^(NSString * _Nullable odmInfo, NSError * _Nullable error) { |
| 95 | + if (error.code == BNCClassNotFoundError){ |
| 96 | + [expectation fulfill]; |
| 97 | + } |
| 98 | + }]; |
| 99 | + [self waitForExpectationsWithTimeout:15 handler:nil]; |
| 100 | +} |
| 101 | + |
| 102 | +@end |
0 commit comments