2017-05-15 12:27:07 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright 2017 Google
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
|
|
|
|
|
__TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || \
|
|
|
|
|
__MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14
|
2018-03-29 09:44:48 -06:00
|
|
|
#import <UserNotifications/UserNotifications.h>
|
2017-05-15 12:27:07 -07:00
|
|
|
#endif
|
2018-03-29 09:44:48 -06:00
|
|
|
#import <XCTest/XCTest.h>
|
2017-05-15 12:27:07 -07:00
|
|
|
#import <OCMock/OCMock.h>
|
|
|
|
|
|
|
|
|
|
#import "FIRMessaging.h"
|
|
|
|
|
#import "FIRMessagingRemoteNotificationsProxy.h"
|
|
|
|
|
|
2019-04-04 09:26:12 -04:00
|
|
|
#import <GoogleUtilities/GULAppDelegateSwizzler.h>
|
|
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
#pragma mark - Invalid App Delegate or UNNotificationCenter
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
@interface RandomObject : NSObject
|
|
|
|
|
@property(nonatomic, weak) id delegate;
|
|
|
|
|
@end
|
|
|
|
|
@implementation RandomObject
|
2019-05-30 15:31:38 -07:00
|
|
|
- (void)application:(GULApplication *)application
|
|
|
|
|
didReceiveRemoteNotification:(NSDictionary *)userInfo {
|
2019-03-28 19:06:04 -04:00
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
|
willPresentNotification:(UNNotification *)notification
|
2019-05-30 15:31:38 -07:00
|
|
|
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0), macos(10.14), tvos(10.0)){
|
2019-03-28 19:06:04 -04:00
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_OSX
|
2019-03-28 19:06:04 -04:00
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
|
didReceiveNotificationResponse:(UNNotificationResponse *)response
|
2019-05-30 15:31:38 -07:00
|
|
|
withCompletionHandler:(void(^)(void))completionHandler API_AVAILABLE(macos(10.14), ios(10.0)){
|
2019-03-28 19:06:04 -04:00
|
|
|
}
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark - Incomplete App Delegate
|
2019-05-30 15:31:38 -07:00
|
|
|
@interface IncompleteAppDelegate : NSObject <GULApplicationDelegate>
|
2017-05-15 12:27:07 -07:00
|
|
|
@end
|
|
|
|
|
@implementation IncompleteAppDelegate
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark - Fake AppDelegate
|
2019-05-30 15:31:38 -07:00
|
|
|
@interface FakeAppDelegate : NSObject <GULApplicationDelegate>
|
2017-05-15 12:27:07 -07:00
|
|
|
@property(nonatomic) BOOL remoteNotificationMethodWasCalled;
|
|
|
|
|
@property(nonatomic) BOOL remoteNotificationWithFetchHandlerWasCalled;
|
2019-04-04 09:26:12 -04:00
|
|
|
@property(nonatomic, strong) NSData *deviceToken;
|
|
|
|
|
@property(nonatomic, strong) NSError *registerForRemoteNotificationsError;
|
2017-05-15 12:27:07 -07:00
|
|
|
@end
|
|
|
|
|
@implementation FakeAppDelegate
|
2019-05-30 15:31:38 -07:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
|
|
|
|
|
- (void)application:(GULApplication *)application
|
2017-05-15 12:27:07 -07:00
|
|
|
didReceiveRemoteNotification:(NSDictionary *)userInfo {
|
|
|
|
|
self.remoteNotificationMethodWasCalled = YES;
|
|
|
|
|
}
|
2019-05-30 15:31:38 -07:00
|
|
|
#pragma clang diagnostic pop
|
|
|
|
|
|
|
|
|
|
#if TARGET_OS_IOS || TARGET_OS_TV
|
2017-05-15 12:27:07 -07:00
|
|
|
- (void)application:(UIApplication *)application
|
|
|
|
|
didReceiveRemoteNotification:(NSDictionary *)userInfo
|
|
|
|
|
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
|
|
|
|
|
self.remoteNotificationWithFetchHandlerWasCalled = YES;
|
|
|
|
|
}
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
2019-04-04 09:26:12 -04:00
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
- (void)application:(GULApplication *)application
|
2019-04-04 09:26:12 -04:00
|
|
|
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
|
|
|
|
|
self.deviceToken = deviceToken;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
- (void)application:(GULApplication *)application
|
2019-04-04 09:26:12 -04:00
|
|
|
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
|
|
|
|
|
self.registerForRemoteNotificationsError = error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
@end
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
|
|
|
|
|
__TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || \
|
|
|
|
|
__MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14
|
2017-05-15 12:27:07 -07:00
|
|
|
#pragma mark - Incompete UNUserNotificationCenterDelegate
|
|
|
|
|
@interface IncompleteUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
|
|
|
|
|
@end
|
|
|
|
|
@implementation IncompleteUserNotificationCenterDelegate
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
#pragma mark - Fake UNUserNotificationCenterDelegate
|
|
|
|
|
|
|
|
|
|
@interface FakeUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
|
|
|
|
|
@property(nonatomic) BOOL willPresentWasCalled;
|
2017-10-04 18:39:30 -07:00
|
|
|
@property(nonatomic) BOOL didReceiveResponseWasCalled;
|
2017-05-15 12:27:07 -07:00
|
|
|
@end
|
|
|
|
|
@implementation FakeUserNotificationCenterDelegate
|
|
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
|
willPresentNotification:(UNNotification *)notification
|
|
|
|
|
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))
|
2019-05-30 15:31:38 -07:00
|
|
|
completionHandler API_AVAILABLE(ios(10.0), macos(10.14), tvos(10.0)){
|
2017-05-15 12:27:07 -07:00
|
|
|
self.willPresentWasCalled = YES;
|
|
|
|
|
}
|
2019-05-30 15:31:38 -07:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_OSX
|
2019-03-28 19:06:04 -04:00
|
|
|
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
|
|
|
|
|
didReceiveNotificationResponse:(UNNotificationResponse *)response
|
2019-05-30 15:31:38 -07:00
|
|
|
withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0), macos(10.14)){
|
2017-10-04 18:39:30 -07:00
|
|
|
self.didReceiveResponseWasCalled = YES;
|
|
|
|
|
}
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
2017-05-15 12:27:07 -07:00
|
|
|
@end
|
2019-05-30 15:31:38 -07:00
|
|
|
|
|
|
|
|
#endif
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-04-04 09:26:12 -04:00
|
|
|
@interface GULAppDelegateSwizzler (FIRMessagingRemoteNotificationsProxyTest)
|
|
|
|
|
+ (void)resetProxyOriginalDelegateOnceToken;
|
|
|
|
|
@end
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
#pragma mark - Local, Per-Test Properties
|
|
|
|
|
|
|
|
|
|
@interface FIRMessagingRemoteNotificationsProxyTest : XCTestCase
|
|
|
|
|
|
|
|
|
|
@property(nonatomic, strong) FIRMessagingRemoteNotificationsProxy *proxy;
|
|
|
|
|
@property(nonatomic, strong) id mockProxyClass;
|
2019-03-28 19:06:04 -04:00
|
|
|
@property(nonatomic, strong) id mockMessaging;
|
|
|
|
|
@property(nonatomic, strong) id mockUserNotificationCenter;
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation FIRMessagingRemoteNotificationsProxyTest
|
|
|
|
|
|
|
|
|
|
- (void)setUp {
|
|
|
|
|
[super setUp];
|
2019-04-04 09:26:12 -04:00
|
|
|
|
|
|
|
|
[GULAppDelegateSwizzler resetProxyOriginalDelegateOnceToken];
|
|
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
_mockMessaging = OCMClassMock([FIRMessaging class]);
|
|
|
|
|
OCMStub([_mockMessaging messaging]).andReturn(_mockMessaging);
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
_proxy = [[FIRMessagingRemoteNotificationsProxy alloc] init];
|
|
|
|
|
_mockProxyClass = OCMClassMock([FIRMessagingRemoteNotificationsProxy class]);
|
2019-03-28 19:06:04 -04:00
|
|
|
// Update +sharedProxy to always return our test instance
|
|
|
|
|
OCMStub([_mockProxyClass sharedProxy]).andReturn(self.proxy);
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
_mockUserNotificationCenter = OCMPartialMock([UNUserNotificationCenter currentNotificationCenter]);
|
|
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)tearDown {
|
|
|
|
|
[_mockProxyClass stopMocking];
|
|
|
|
|
_mockProxyClass = nil;
|
|
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
[_mockMessaging stopMocking];
|
|
|
|
|
_mockMessaging = nil;
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
[_mockUserNotificationCenter stopMocking];
|
|
|
|
|
_mockUserNotificationCenter = nil;
|
|
|
|
|
}
|
2019-04-04 09:26:12 -04:00
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
_proxy = nil;
|
|
|
|
|
[super tearDown];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#pragma mark - Method Swizzling Tests
|
|
|
|
|
|
|
|
|
|
- (void)testSwizzlingNonAppDelegate {
|
2019-03-28 19:06:04 -04:00
|
|
|
RandomObject *invalidAppDelegate = [[RandomObject alloc] init];
|
2019-05-30 15:31:38 -07:00
|
|
|
[[GULAppDelegateSwizzler sharedApplication] setDelegate:(id<GULApplicationDelegate>)invalidAppDelegate];
|
2019-03-28 19:06:04 -04:00
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
2017-05-15 12:27:07 -07:00
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
|
|
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[invalidAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
|
2019-05-30 15:31:38 -07:00
|
|
|
didReceiveRemoteNotification:@{}];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
|
2019-03-28 19:06:04 -04:00
|
|
|
IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
|
2019-05-28 11:36:31 -07:00
|
|
|
[[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
|
2019-03-28 19:06:04 -04:00
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
2018-12-17 10:50:20 -08:00
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
NSDictionary *notification = @{@"test" : @""};
|
|
|
|
|
OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2019-05-28 11:36:31 -07:00
|
|
|
[incompleteAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
|
2019-03-28 19:06:04 -04:00
|
|
|
didReceiveRemoteNotification:notification];
|
2019-05-30 15:31:38 -07:00
|
|
|
#pragma clang diagnostic pop
|
2019-03-28 19:06:04 -04:00
|
|
|
|
|
|
|
|
[self.mockMessaging verify];
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
|
|
|
|
|
IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
|
2019-05-28 11:36:31 -07:00
|
|
|
[[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
|
2019-03-28 19:06:04 -04:00
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_TV
|
2017-05-15 12:27:07 -07:00
|
|
|
SEL remoteNotificationWithFetchHandler =
|
2019-04-04 09:26:12 -04:00
|
|
|
@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);
|
2017-05-15 12:27:07 -07:00
|
|
|
XCTAssertFalse([incompleteAppDelegate respondsToSelector:remoteNotificationWithFetchHandler]);
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
2019-04-04 09:26:12 -04:00
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
SEL remoteNotification = @selector(application:didReceiveRemoteNotification:);
|
|
|
|
|
XCTAssertTrue([incompleteAppDelegate respondsToSelector:remoteNotification]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testSwizzledAppDelegateRemoteNotificationMethods {
|
|
|
|
|
FakeAppDelegate *appDelegate = [[FakeAppDelegate alloc] init];
|
2019-05-28 11:36:31 -07:00
|
|
|
[[GULAppDelegateSwizzler sharedApplication] setDelegate:appDelegate];
|
2019-03-28 19:06:04 -04:00
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
|
|
|
|
|
|
|
|
|
NSDictionary *notification = @{@"test" : @""};
|
|
|
|
|
|
|
|
|
|
//Test application:didReceiveRemoteNotification:
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
// Verify our swizzled method was called
|
2019-03-28 19:06:04 -04:00
|
|
|
OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
2019-05-28 11:36:31 -07:00
|
|
|
[appDelegate application:[GULAppDelegateSwizzler sharedApplication]
|
2019-03-28 19:06:04 -04:00
|
|
|
didReceiveRemoteNotification:notification];
|
2019-05-30 15:31:38 -07:00
|
|
|
#pragma clang diagnostic pop
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
// Verify our original method was called
|
|
|
|
|
XCTAssertTrue(appDelegate.remoteNotificationMethodWasCalled);
|
2019-03-28 19:06:04 -04:00
|
|
|
[self.mockMessaging verify];
|
|
|
|
|
|
|
|
|
|
//Test application:didReceiveRemoteNotification:fetchCompletionHandler:
|
2019-05-30 15:31:38 -07:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_TV
|
2017-05-15 12:27:07 -07:00
|
|
|
// Verify our swizzled method was called
|
2019-03-28 19:06:04 -04:00
|
|
|
OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
|
|
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[appDelegate application:[GULAppDelegateSwizzler sharedApplication]
|
2019-03-28 19:06:04 -04:00
|
|
|
didReceiveRemoteNotification:notification
|
|
|
|
|
fetchCompletionHandler:^(UIBackgroundFetchResult result) {}];
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
// Verify our original method was called
|
|
|
|
|
XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);
|
2019-03-05 11:57:10 -08:00
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
[self.mockMessaging verify];
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
2019-04-04 09:26:12 -04:00
|
|
|
|
|
|
|
|
// Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
|
|
|
|
|
NSData *deviceToken = [NSData data];
|
|
|
|
|
|
|
|
|
|
OCMExpect([self.mockMessaging setAPNSToken:deviceToken]);
|
|
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[appDelegate application:[GULAppDelegateSwizzler sharedApplication]
|
2019-04-04 09:26:12 -04:00
|
|
|
didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(appDelegate.deviceToken, deviceToken);
|
|
|
|
|
[self.mockMessaging verify];
|
|
|
|
|
|
|
|
|
|
// Verify application:didFailToRegisterForRemoteNotificationsWithError:
|
|
|
|
|
NSError *error = [NSError errorWithDomain:@"tests" code:-1 userInfo:nil];
|
|
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[appDelegate application:[GULAppDelegateSwizzler sharedApplication]
|
2019-04-04 09:26:12 -04:00
|
|
|
didFailToRegisterForRemoteNotificationsWithError:error];
|
|
|
|
|
|
|
|
|
|
XCTAssertEqual(appDelegate.registerForRemoteNotificationsError, error);
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testListeningForDelegateChangesOnInvalidUserNotificationCenter {
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
RandomObject *invalidNotificationCenter = [[RandomObject alloc] init];
|
|
|
|
|
OCMStub([self.mockUserNotificationCenter currentNotificationCenter]).andReturn(invalidNotificationCenter);
|
|
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[(id<UNUserNotificationCenterDelegate>)invalidNotificationCenter.delegate
|
|
|
|
|
userNotificationCenter:self.mockUserNotificationCenter
|
|
|
|
|
willPresentNotification:OCMClassMock([UNNotification class])
|
|
|
|
|
withCompletionHandler:^(UNNotificationPresentationOptions options) {
|
|
|
|
|
}];
|
|
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testSwizzlingInvalidUserNotificationCenterDelegate {
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
RandomObject *invalidDelegate = [[RandomObject alloc] init];
|
|
|
|
|
OCMStub([self.mockUserNotificationCenter delegate]).andReturn(invalidDelegate);
|
|
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[invalidDelegate
|
|
|
|
|
userNotificationCenter:self.mockUserNotificationCenter
|
|
|
|
|
willPresentNotification:OCMClassMock([UNNotification class])
|
|
|
|
|
withCompletionHandler:^(UNNotificationPresentationOptions options) {
|
|
|
|
|
}];
|
|
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)testSwizzlingUserNotificationsCenterDelegate {
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
|
|
|
|
|
OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
|
|
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
NSDictionary *message = @{@"message": @""};
|
|
|
|
|
id notification = [self userNotificationWithMessage:message];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[delegate
|
|
|
|
|
userNotificationCenter:self.mockUserNotificationCenter
|
|
|
|
|
willPresentNotification:notification
|
|
|
|
|
withCompletionHandler:^(UNNotificationPresentationOptions options) {
|
|
|
|
|
}];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
[self.mockMessaging verify];
|
|
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use a fake delegate that doesn't actually implement the needed delegate method.
|
2017-10-04 18:39:30 -07:00
|
|
|
// Our swizzled method should not be called.
|
2017-05-15 12:27:07 -07:00
|
|
|
|
|
|
|
|
- (void)testIncompleteUserNotificationCenterDelegateMethod {
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
IncompleteUserNotificationCenterDelegate *delegate =
|
|
|
|
|
[[IncompleteUserNotificationCenterDelegate alloc] init];
|
|
|
|
|
OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
|
|
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
|
|
|
|
// Because the incomplete delete does not implement either of the optional delegate methods, we
|
|
|
|
|
// should swizzle nothing. If we had swizzled them, then respondsToSelector: would return YES
|
|
|
|
|
// even though the delegate does not implement the methods.
|
|
|
|
|
SEL willPresentSelector = @selector(userNotificationCenter:willPresentNotification:withCompletionHandler:);
|
|
|
|
|
XCTAssertFalse([delegate respondsToSelector:willPresentSelector]);
|
|
|
|
|
SEL didReceiveResponseSelector =
|
|
|
|
|
@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:);
|
|
|
|
|
XCTAssertFalse([delegate respondsToSelector:didReceiveResponseSelector]);
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 18:39:30 -07:00
|
|
|
// Use an object that does actually implement the optional methods. Both should be called.
|
2017-05-15 12:27:07 -07:00
|
|
|
- (void)testSwizzledUserNotificationsCenterDelegate {
|
2019-05-28 11:36:31 -07:00
|
|
|
FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
|
|
|
|
|
OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
|
|
|
|
|
[self.proxy swizzleMethodsIfPossible];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
NSDictionary *message = @{@"message": @""};
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
// Verify userNotificationCenter:willPresentNotification:withCompletionHandler:
|
|
|
|
|
OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, tvOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
// Invoking delegate method should also invoke our swizzled method
|
|
|
|
|
// The swizzled method uses the +sharedProxy, which should be
|
|
|
|
|
// returning our proxy.
|
|
|
|
|
// Use non-nil, proper classes, otherwise our SDK bails out.
|
|
|
|
|
[delegate userNotificationCenter:self.mockUserNotificationCenter
|
|
|
|
|
willPresentNotification:[self userNotificationWithMessage:message]
|
|
|
|
|
withCompletionHandler:^(NSUInteger options) {}];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
// Verify our original method was called
|
|
|
|
|
XCTAssertTrue(delegate.willPresentWasCalled);
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
// Verify our swizzled method was called
|
|
|
|
|
[self.mockMessaging verify];
|
2019-05-30 15:31:38 -07:00
|
|
|
}
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2019-05-28 11:36:31 -07:00
|
|
|
// Verify userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
|
2019-05-30 15:31:38 -07:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_OSX
|
2019-05-28 11:36:31 -07:00
|
|
|
[delegate userNotificationCenter:self.mockUserNotificationCenter
|
|
|
|
|
didReceiveNotificationResponse:[self userNotificationResponseWithMessage:message]
|
|
|
|
|
withCompletionHandler:^{}];
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
// Verify our original method was called
|
|
|
|
|
XCTAssertTrue(delegate.didReceiveResponseWasCalled);
|
2019-03-28 19:06:04 -04:00
|
|
|
|
2019-05-28 11:36:31 -07:00
|
|
|
// Verify our swizzled method was called
|
|
|
|
|
[self.mockMessaging verify];
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
|
|
|
|
}
|
2017-05-15 12:27:07 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-28 19:06:04 -04:00
|
|
|
- (id)userNotificationResponseWithMessage:(NSDictionary *)message {
|
2019-05-30 15:31:38 -07:00
|
|
|
#if TARGET_OS_IOS || TARGET_OS_OSX
|
|
|
|
|
if (@available(macOS 10.14, iOS 10.0, *)) {
|
2017-10-04 18:39:30 -07:00
|
|
|
// Stub out: response.[mock notification above]
|
2019-05-28 11:36:31 -07:00
|
|
|
id mockNotificationResponse = OCMClassMock([UNNotificationResponse class]);
|
|
|
|
|
id mockNotification = [self userNotificationWithMessage:message];
|
|
|
|
|
OCMStub([mockNotificationResponse notification]).andReturn(mockNotification);
|
|
|
|
|
return mockNotificationResponse;
|
|
|
|
|
}
|
2019-05-30 15:31:38 -07:00
|
|
|
#endif
|
2019-05-28 11:36:31 -07:00
|
|
|
return nil;
|
2019-03-28 19:06:04 -04:00
|
|
|
}
|
|
|
|
|
|
2019-05-30 15:31:38 -07:00
|
|
|
- (UNNotification *)userNotificationWithMessage:(NSDictionary *)message API_AVAILABLE(macos(10.14), ios(10.0)){
|
2019-03-28 19:06:04 -04:00
|
|
|
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
|
|
|
|
|
content.userInfo = message;
|
|
|
|
|
id notificationRequest = OCMClassMock([UNNotificationRequest class]);
|
|
|
|
|
OCMStub([notificationRequest content]).andReturn(content);
|
|
|
|
|
id notification = OCMClassMock([UNNotification class]);
|
|
|
|
|
OCMStub([notification request]).andReturn(notificationRequest);
|
2019-05-30 15:31:38 -07:00
|
|
|
return notification;
|
2017-10-04 18:39:30 -07:00
|
|
|
}
|
|
|
|
|
|
2017-05-15 12:27:07 -07:00
|
|
|
@end
|