@@ -20,13 +20,15 @@ - (void)writeObjectToDefaults:(NSString *)key value:(NSObject *)value;
2020
2121@interface BranchClassTests : XCTestCase
2222@property (nonatomic , strong ) Branch *branch;
23+ @property (nonatomic , strong , readwrite ) BNCPreferenceHelper *prefHelper;
2324@end
2425
2526@implementation BranchClassTests
2627
2728- (void )setUp {
2829 [super setUp ];
2930 self.branch = [Branch getInstance ];
31+ self.prefHelper = [BNCPreferenceHelper sharedInstance ];
3032}
3133
3234- (void )tearDown {
@@ -261,5 +263,197 @@ - (void)testSetConsumerProtectionAttributionLevel {
261263
262264}
263265
266+ - (void )testBranchSetSDKWaitTimeForThirdPartyAPIs {
267+ // Test Branch instance method for setting timeout
268+ NSTimeInterval testTimeout = 2.0 ;
269+ [Branch setSDKWaitTimeForThirdPartyAPIs: testTimeout];
270+
271+ // Verify it was set in the preference helper
272+ XCTAssertEqual (self.prefHelper .thirdPartyAPIsWaitTime , testTimeout,
273+ @" Branch setSDKWaitTimeForThirdPartyAPIs should update preference helper" );
274+ }
275+
276+ - (void )testBranchSetSDKWaitTimeForThirdPartyAPIsMultipleValues {
277+ // Test setting multiple different values
278+ NSArray *testValues = @[@0.5 , @1.0 , @1.5 , @3.0 , @5.0 ];
279+
280+ for (NSNumber *timeoutValue in testValues) {
281+ NSTimeInterval timeout = [timeoutValue doubleValue ];
282+ [Branch setSDKWaitTimeForThirdPartyAPIs: timeout];
283+
284+ XCTAssertEqual (self.prefHelper .thirdPartyAPIsWaitTime , timeout,
285+ @" Branch setSDKWaitTimeForThirdPartyAPIs should handle value %.1f " , timeout);
286+ }
287+ }
288+
289+ - (void )testTimeoutIntegrationWithPreferenceHelper {
290+ // Test that Branch and PreferenceHelper work together correctly
291+ NSTimeInterval branchTimeout = 1.8 ;
292+ NSTimeInterval directTimeout = 2.3 ;
293+
294+ // Set via Branch
295+ [Branch setSDKWaitTimeForThirdPartyAPIs: branchTimeout];
296+ XCTAssertEqual (self.prefHelper .thirdPartyAPIsWaitTime , branchTimeout,
297+ @" Wait Time set via Branch should be readable from PreferenceHelper" );
298+
299+ // Set directly on PreferenceHelper
300+ self.prefHelper .thirdPartyAPIsWaitTime = directTimeout;
301+ XCTAssertEqual (self.prefHelper .thirdPartyAPIsWaitTime , directTimeout,
302+ @" Wait time set directly should be readable via Branch" );
303+ }
304+
305+ - (void )testTimeoutValueConsistency {
306+ // Test that the same instance maintains consistent values
307+ NSTimeInterval testTimeout = 1.25 ;
308+
309+ [Branch setSDKWaitTimeForThirdPartyAPIs: testTimeout];
310+
311+ // Read multiple times to ensure consistency
312+ for (int i = 0 ; i < 5 ; i++) {
313+ XCTAssertEqual (self.prefHelper .thirdPartyAPIsWaitTime , testTimeout,
314+ @" Timeout value should remain consistent across multiple reads" );
315+ }
316+ }
317+
318+ - (void )testBranchSetSDKWaitTimeForThirdPartyAPIsInvalidLowValues {
319+
320+ NSArray *invalidLowValues = @[@0.0 , @-1.0 , @-0.5 ];
321+ NSTimeInterval originalTimeout = [BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime ;
322+
323+ for (NSNumber *timeoutValue in invalidLowValues) {
324+ NSTimeInterval timeout = [timeoutValue doubleValue ];
325+ [Branch setSDKWaitTimeForThirdPartyAPIs: timeout];
326+ XCTAssertEqual ([BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime , originalTimeout,
327+ @" Branch setSDKWaitTimeForThirdPartyAPIs should reject invalid low value %.3f " , timeout);
328+ }
329+ }
330+
331+ - (void )testBranchsetSDKWaitTimeForThirdPartyAPIsInvalidHighValues {
332+
333+ NSArray *invalidHighValues = @[@10.1 , @15.0 , @30.0 , @60.0 ];
334+ NSTimeInterval originalTimeout = [BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime ;
335+
336+ for (NSNumber *timeoutValue in invalidHighValues) {
337+ NSTimeInterval timeout = [timeoutValue doubleValue ];
338+ [Branch setSDKWaitTimeForThirdPartyAPIs: timeout];
339+ XCTAssertEqual ([BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime , originalTimeout,
340+ @" Branch setSDKWaitTimeForThirdPartyAPIs should reject invalid high value %.3f " , timeout);
341+ }
342+ }
343+
344+ - (void )testBranchSetSDKWaitTimeForThirdPartyAPIsBoundaryValues {
345+
346+ // Test exactly 10.0 (should be valid)
347+ [Branch setSDKWaitTimeForThirdPartyAPIs: 10.0 ];
348+ XCTAssertEqual ([BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime , 10.0 ,
349+ @" Timeout of exactly 10.0 seconds should be valid" );
350+
351+ // Test just over 10.0 (should be invalid)
352+ [Branch setSDKWaitTimeForThirdPartyAPIs: 10.0001 ];
353+ XCTAssertEqual ([BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime , 10.0 ,
354+ @" Timeout of 10.0001 seconds should be rejected" );
355+
356+ // Test very small positive value (should be valid)
357+ [Branch setSDKWaitTimeForThirdPartyAPIs: 0.0001 ];
358+ XCTAssertEqual ([BNCPreferenceHelper sharedInstance ].thirdPartyAPIsWaitTime , 0.0001 ,
359+ @" Very small positive timeout should be valid" );
360+ }
361+
362+ - (void )testSetAnonID {
363+ NSString *expectedAnonID = @" static-test-anon-id-12345" ;
364+
365+ [Branch setAnonID: expectedAnonID];
366+
367+ NSString *actualAnonID = [BNCPreferenceHelper sharedInstance ].anonID ;
368+ XCTAssertEqualObjects (actualAnonID, expectedAnonID, @" setAnonID should set valid string" );
369+ }
370+
371+ - (void )testSetAnonID_UpdateExistingValue {
372+ NSString *initialAnonID = @" initial-anon-id" ;
373+ NSString *updatedAnonID = @" updated-anon-id" ;
374+
375+ [Branch setAnonID: initialAnonID];
376+ XCTAssertEqualObjects ([BNCPreferenceHelper sharedInstance ].anonID , initialAnonID);
377+
378+ [Branch setAnonID: updatedAnonID];
379+ XCTAssertEqualObjects ([BNCPreferenceHelper sharedInstance ].anonID , updatedAnonID);
380+ }
381+
382+ - (void )testSetAnonID_NilValue {
383+ NSString *initialAnonID = @" initial-anon-id" ;
384+
385+ [Branch setAnonID: initialAnonID];
386+ XCTAssertEqualObjects ([BNCPreferenceHelper sharedInstance ].anonID , initialAnonID);
387+
388+ [Branch setAnonID: nil ];
389+ XCTAssertEqualObjects ([BNCPreferenceHelper sharedInstance ].anonID , initialAnonID);
390+ }
391+
392+ - (void )testSetAnonID_EmptyString {
393+ NSString *emptyAnonID = @" " ;
394+
395+ [Branch setAnonID: emptyAnonID];
396+
397+ NSString *actualAnonID = [BNCPreferenceHelper sharedInstance ].anonID ;
398+ XCTAssertEqualObjects (actualAnonID, emptyAnonID, @" Static setAnonID should accept empty string" );
399+ }
400+
401+ - (void )testSetAnonID_NonStringObject {
402+ NSString *initialAnonID = @" initial-anon-id" ;
403+
404+ [Branch setAnonID: initialAnonID];
405+ XCTAssertEqualObjects ([BNCPreferenceHelper sharedInstance ].anonID , initialAnonID);
406+
407+ // Try to set with a non-string object (NSNumber)
408+ NSNumber *nonStringValue = @123 ;
409+ [Branch setAnonID: (NSString *)nonStringValue];
410+
411+ XCTAssertEqualObjects ([BNCPreferenceHelper sharedInstance ].anonID , initialAnonID);
412+ }
413+
414+ - (void )testSetAnonID_LongString {
415+ NSString *longAnonID = @" very-long-anon-id-with-many-characters-to-test-string-handling-1234567890-abcdefghijklmnopqrstuvwxyz" ;
416+
417+ [Branch setAnonID: longAnonID];
418+
419+ NSString *actualAnonID = [BNCPreferenceHelper sharedInstance ].anonID ;
420+ XCTAssertEqualObjects (actualAnonID, longAnonID, @" setAnonID should handle long strings" );
421+ }
422+
423+ - (void )testSetAnonID_SpecialCharacters {
424+ NSString *specialCharAnonID = @" static-anon-id-with-special-chars-!@#$%^&*()_+-=[]{}|;:,.<>?" ;
425+
426+ [Branch setAnonID: specialCharAnonID];
427+
428+ NSString *actualAnonID = [BNCPreferenceHelper sharedInstance ].anonID ;
429+ XCTAssertEqualObjects (actualAnonID, specialCharAnonID, @" setAnonID should handle special characters" );
430+ }
431+
432+ - (void )testSetAnonID_ThreadSafety {
433+ NSString *anonID1 = @" thread-test-anon-id-1" ;
434+ NSString *anonID2 = @" thread-test-anon-id-2" ;
435+
436+ // Test that the method is thread-safe by calling it from different queues
437+ dispatch_group_t group = dispatch_group_create ();
438+
439+ dispatch_group_enter (group);
440+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
441+ [Branch setAnonID: anonID1];
442+ dispatch_group_leave (group);
443+ });
444+
445+ dispatch_group_enter (group);
446+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
447+ [Branch setAnonID: anonID2];
448+ dispatch_group_leave (group);
449+ });
450+
451+ dispatch_group_wait (group, DISPATCH_TIME_FOREVER);
452+
453+ // Verify that one of the values was set (we can't predict which due to concurrency)
454+ NSString *finalAnonID = [BNCPreferenceHelper sharedInstance ].anonID ;
455+ XCTAssertTrue ([finalAnonID isEqualToString: anonID1] || [finalAnonID isEqualToString: anonID2],
456+ @" One of the anonID values should be set" );
457+ }
264458
265459@end
0 commit comments