Skip to content

Commit

Permalink
Add testSwizzleExistingSelectors
Browse files Browse the repository at this point in the history
Add test to ensure our swizzling still calls the original methods.
  • Loading branch information
jkasten2 committed May 20, 2022
1 parent 1a2b961 commit 9a6c8c4
Showing 1 changed file with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,57 @@ @interface AppDelegateForInfiniteLoopTest : UIResponder<UIApplicationDelegate>
@implementation AppDelegateForInfiniteLoopTest
@end

@interface AppDelegateForExistingSelectorsTest : UIResponder<UIApplicationDelegate> {
@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<UIApplicationDelegate> orignalDelegate;

@interface UIApplicationDelegateSwizzlingTest : XCTestCase
Expand Down Expand Up @@ -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<UIApplicationDelegate> 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

0 comments on commit 9a6c8c4

Please sign in to comment.