From 9a6c8c4a40c1447ed06613240d65430b093e9cf8 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Fri, 20 May 2022 02:53:44 -0700 Subject: [PATCH] Add testSwizzleExistingSelectors Add test to ensure our swizzling still calls the original methods. --- .../UIApplicationDelegateSwizzlingTests.m | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/iOS_SDK/OneSignalSDK/UnitTests/UIApplicationDelegateSwizzlingTests.m b/iOS_SDK/OneSignalSDK/UnitTests/UIApplicationDelegateSwizzlingTests.m index 331725017..e0768880e 100644 --- a/iOS_SDK/OneSignalSDK/UnitTests/UIApplicationDelegateSwizzlingTests.m +++ b/iOS_SDK/OneSignalSDK/UnitTests/UIApplicationDelegateSwizzlingTests.m @@ -85,6 +85,57 @@ @interface AppDelegateForInfiniteLoopTest : UIResponder @implementation AppDelegateForInfiniteLoopTest @end +@interface AppDelegateForExistingSelectorsTest : UIResponder { + @public NSMutableDictionary *selectorCallsDict; +} +@end +@implementation AppDelegateForExistingSelectorsTest +- (instancetype)init { + self = [super init]; + selectorCallsDict = [NSMutableDictionary new]; + return self; +} + +- (void)application:(UIApplication *)application + didReceiveRemoteNotification:(NSDictionary *)userInfo + fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { + SEL thisSelector = @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:); + [selectorCallsDict + setObject:@(true) + forKey:NSStringFromSelector(thisSelector) + ]; +} + +- (void)application:(UIApplication *)application + didFailToRegisterForRemoteNotificationsWithError:(NSError*)err +{ + SEL thisSelector = @selector(application:didFailToRegisterForRemoteNotificationsWithError:); + [selectorCallsDict + setObject:@(true) + forKey:NSStringFromSelector(thisSelector) + ]; +} + +- (void)application:(UIApplication *)application + didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)data +{ + SEL thisSelector = @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:); + [selectorCallsDict + setObject:@(true) + forKey:NSStringFromSelector(thisSelector) + ]; +} + +- (void)applicationWillTerminate:(UIApplication *)application +{ + SEL thisSelector = @selector(applicationWillTerminate:); + [selectorCallsDict + setObject:@(true) + forKey:NSStringFromSelector(thisSelector) + ]; +} +@end + static id orignalDelegate; @interface UIApplicationDelegateSwizzlingTest : XCTestCase @@ -187,4 +238,46 @@ - (void)testDoubleSwizzleInfiniteLoop { // 4. Call something to confirm we don't get stuck in an infinite call loop [localOrignalDelegate applicationWillTerminate:UIApplication.sharedApplication]; } + +- (void)testSwizzleExistingSelectors { + AppDelegateForExistingSelectorsTest* myAppDelegate = [AppDelegateForExistingSelectorsTest new]; + UIApplication.sharedApplication.delegate = myAppDelegate; + id appDelegate = UIApplication.sharedApplication.delegate; + + [appDelegate + application:UIApplication.sharedApplication + didReceiveRemoteNotification:@{} + fetchCompletionHandler:^(UIBackgroundFetchResult result){}]; + XCTAssertTrue([myAppDelegate->selectorCallsDict + objectForKey:NSStringFromSelector( + @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:) + ) + ]); + + [appDelegate + application:UIApplication.sharedApplication + didFailToRegisterForRemoteNotificationsWithError:[NSError new]]; + XCTAssertTrue([myAppDelegate->selectorCallsDict + objectForKey:NSStringFromSelector( + @selector(application:didFailToRegisterForRemoteNotificationsWithError:) + ) + ]); + + [appDelegate + application:UIApplication.sharedApplication + didRegisterForRemoteNotificationsWithDeviceToken:[NSData new]]; + XCTAssertTrue([myAppDelegate->selectorCallsDict + objectForKey:NSStringFromSelector( + @selector(application:didRegisterForRemoteNotificationsWithDeviceToken:) + ) + ]); + + [appDelegate + applicationWillTerminate:UIApplication.sharedApplication]; + XCTAssertTrue([myAppDelegate->selectorCallsDict + objectForKey:NSStringFromSelector( + @selector(applicationWillTerminate:) + ) + ]); +} @end