FIRMessagingRemoteNotificationsProxyTest.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
  17. __TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14
  18. #import <UserNotifications/UserNotifications.h>
  19. #endif
  20. #import <XCTest/XCTest.h>
  21. #import "OCMock.h"
  22. #import <GoogleUtilities/GULAppDelegateSwizzler.h>
  23. #import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
  24. #import "FirebaseMessaging/Sources/FIRMessagingRemoteNotificationsProxy.h"
  25. #pragma mark - Invalid App Delegate or UNNotificationCenter
  26. @interface RandomObject : NSObject
  27. @property(nonatomic, weak) id delegate;
  28. @end
  29. @implementation RandomObject
  30. - (void)application:(GULApplication *)application
  31. didReceiveRemoteNotification:(NSDictionary *)userInfo {
  32. }
  33. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  34. willPresentNotification:(UNNotification *)notification
  35. withCompletionHandler:
  36. (void (^)(UNNotificationPresentationOptions options))completionHandler
  37. API_AVAILABLE(ios(10.0), macos(10.14), tvos(10.0)) {
  38. }
  39. #if TARGET_OS_IOS || TARGET_OS_OSX
  40. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  41. didReceiveNotificationResponse:(UNNotificationResponse *)response
  42. withCompletionHandler:(void (^)(void))completionHandler
  43. API_AVAILABLE(macos(10.14), ios(10.0)) {
  44. }
  45. #endif
  46. @end
  47. #pragma mark - Incomplete App Delegate
  48. @interface IncompleteAppDelegate : NSObject <GULApplicationDelegate>
  49. @end
  50. @implementation IncompleteAppDelegate
  51. @end
  52. #pragma mark - Fake AppDelegate
  53. @interface FakeAppDelegate : NSObject <GULApplicationDelegate>
  54. @property(nonatomic) BOOL remoteNotificationMethodWasCalled;
  55. @property(nonatomic) BOOL remoteNotificationWithFetchHandlerWasCalled;
  56. @property(nonatomic, strong) NSData *deviceToken;
  57. @property(nonatomic, strong) NSError *registerForRemoteNotificationsError;
  58. @end
  59. @implementation FakeAppDelegate
  60. #pragma clang diagnostic push
  61. #pragma clang diagnostic ignored "-Wdeprecated-implementations"
  62. - (void)application:(GULApplication *)application
  63. didReceiveRemoteNotification:(NSDictionary *)userInfo {
  64. self.remoteNotificationMethodWasCalled = YES;
  65. }
  66. #pragma clang diagnostic pop
  67. #if TARGET_OS_IOS || TARGET_OS_TV
  68. - (void)application:(UIApplication *)application
  69. didReceiveRemoteNotification:(NSDictionary *)userInfo
  70. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  71. self.remoteNotificationWithFetchHandlerWasCalled = YES;
  72. }
  73. #endif
  74. - (void)application:(GULApplication *)application
  75. didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  76. self.deviceToken = deviceToken;
  77. }
  78. - (void)application:(GULApplication *)application
  79. didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  80. self.registerForRemoteNotificationsError = error;
  81. }
  82. @end
  83. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
  84. __TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14
  85. #pragma mark - Incompete UNUserNotificationCenterDelegate
  86. @interface IncompleteUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
  87. @end
  88. @implementation IncompleteUserNotificationCenterDelegate
  89. @end
  90. #pragma mark - Fake UNUserNotificationCenterDelegate
  91. @interface FakeUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
  92. @property(nonatomic) BOOL willPresentWasCalled;
  93. @property(nonatomic) BOOL didReceiveResponseWasCalled;
  94. @end
  95. @implementation FakeUserNotificationCenterDelegate
  96. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  97. willPresentNotification:(UNNotification *)notification
  98. withCompletionHandler:
  99. (void (^)(UNNotificationPresentationOptions options))completionHandler
  100. API_AVAILABLE(ios(10.0), macos(10.14), tvos(10.0)) {
  101. self.willPresentWasCalled = YES;
  102. }
  103. #if TARGET_OS_IOS || TARGET_OS_OSX
  104. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  105. didReceiveNotificationResponse:(UNNotificationResponse *)response
  106. withCompletionHandler:(void (^)(void))completionHandler
  107. API_AVAILABLE(ios(10.0), macos(10.14)) {
  108. self.didReceiveResponseWasCalled = YES;
  109. }
  110. #endif
  111. @end
  112. #endif
  113. @interface GULAppDelegateSwizzler (FIRMessagingRemoteNotificationsProxyTest)
  114. + (void)resetProxyOriginalDelegateOnceToken;
  115. @end
  116. #pragma mark - Local, Per-Test Properties
  117. @interface FIRMessagingRemoteNotificationsProxyTest : XCTestCase
  118. @property(nonatomic, strong) FIRMessagingRemoteNotificationsProxy *proxy;
  119. @property(nonatomic, strong) id mockProxyClass;
  120. @property(nonatomic, strong) id mockMessaging;
  121. @property(nonatomic, strong) id mockUserNotificationCenter;
  122. @end
  123. @implementation FIRMessagingRemoteNotificationsProxyTest
  124. - (void)setUp {
  125. [super setUp];
  126. [GULAppDelegateSwizzler resetProxyOriginalDelegateOnceToken];
  127. _mockMessaging = OCMClassMock([FIRMessaging class]);
  128. OCMStub([_mockMessaging messaging]).andReturn(_mockMessaging);
  129. _proxy = [[FIRMessagingRemoteNotificationsProxy alloc] init];
  130. _mockProxyClass = OCMClassMock([FIRMessagingRemoteNotificationsProxy class]);
  131. // Update +sharedProxy to always return our test instance
  132. OCMStub([_mockProxyClass sharedProxy]).andReturn(self.proxy);
  133. if (@available(macOS 10.14, iOS 10.0, *)) {
  134. _mockUserNotificationCenter = OCMClassMock([UNUserNotificationCenter class]);
  135. OCMStub([_mockUserNotificationCenter currentNotificationCenter])
  136. .andReturn(_mockUserNotificationCenter);
  137. }
  138. }
  139. - (void)tearDown {
  140. [_mockProxyClass stopMocking];
  141. _mockProxyClass = nil;
  142. [_mockMessaging stopMocking];
  143. _mockMessaging = nil;
  144. if (@available(macOS 10.14, iOS 10.0, *)) {
  145. [_mockUserNotificationCenter stopMocking];
  146. _mockUserNotificationCenter = nil;
  147. }
  148. _proxy = nil;
  149. [super tearDown];
  150. }
  151. #pragma mark - Method Swizzling Tests
  152. - (void)testSwizzlingNonAppDelegate {
  153. RandomObject *invalidAppDelegate = [[RandomObject alloc] init];
  154. [[GULAppDelegateSwizzler sharedApplication]
  155. setDelegate:(id<GULApplicationDelegate>)invalidAppDelegate];
  156. [self.proxy swizzleMethodsIfPossible];
  157. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  158. [invalidAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  159. didReceiveRemoteNotification:@{}];
  160. }
  161. #if !SWIFT_PACKAGE
  162. // The next 3 tests depend on a sharedApplication which is not available in the Swift PM test env.
  163. - (void)testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
  164. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  165. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  166. [self.proxy swizzleMethodsIfPossible];
  167. NSDictionary *notification = @{@"test" : @""};
  168. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  169. #pragma clang diagnostic push
  170. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  171. [incompleteAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  172. didReceiveRemoteNotification:notification];
  173. #pragma clang diagnostic pop
  174. [self.mockMessaging verify];
  175. }
  176. - (void)testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
  177. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  178. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  179. [self.proxy swizzleMethodsIfPossible];
  180. #if TARGET_OS_IOS || TARGET_OS_TV
  181. SEL remoteNotificationWithFetchHandler = @selector(application:
  182. didReceiveRemoteNotification:fetchCompletionHandler:);
  183. XCTAssertFalse([incompleteAppDelegate respondsToSelector:remoteNotificationWithFetchHandler]);
  184. #endif
  185. SEL remoteNotification = @selector(application:didReceiveRemoteNotification:);
  186. XCTAssertTrue([incompleteAppDelegate respondsToSelector:remoteNotification]);
  187. }
  188. - (void)testSwizzledAppDelegateRemoteNotificationMethods {
  189. FakeAppDelegate *appDelegate = [[FakeAppDelegate alloc] init];
  190. [[GULAppDelegateSwizzler sharedApplication] setDelegate:appDelegate];
  191. [self.proxy swizzleMethodsIfPossible];
  192. NSDictionary *notification = @{@"test" : @""};
  193. // Test application:didReceiveRemoteNotification:
  194. // Verify our swizzled method was called
  195. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  196. #pragma clang diagnostic push
  197. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  198. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  199. didReceiveRemoteNotification:notification];
  200. #pragma clang diagnostic pop
  201. // Verify our original method was called
  202. XCTAssertTrue(appDelegate.remoteNotificationMethodWasCalled);
  203. [self.mockMessaging verify];
  204. // Test application:didReceiveRemoteNotification:fetchCompletionHandler:
  205. #if TARGET_OS_IOS || TARGET_OS_TV
  206. // Verify our swizzled method was called
  207. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  208. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  209. didReceiveRemoteNotification:notification
  210. fetchCompletionHandler:^(UIBackgroundFetchResult result){
  211. }];
  212. // Verify our original method was called
  213. XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);
  214. [self.mockMessaging verify];
  215. #endif
  216. // Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
  217. NSData *deviceToken = [NSData data];
  218. OCMExpect([self.mockMessaging setAPNSToken:deviceToken]);
  219. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  220. didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  221. XCTAssertEqual(appDelegate.deviceToken, deviceToken);
  222. [self.mockMessaging verify];
  223. // Verify application:didFailToRegisterForRemoteNotificationsWithError:
  224. NSError *error = [NSError errorWithDomain:@"tests" code:-1 userInfo:nil];
  225. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  226. didFailToRegisterForRemoteNotificationsWithError:error];
  227. XCTAssertEqual(appDelegate.registerForRemoteNotificationsError, error);
  228. }
  229. #endif
  230. - (void)testListeningForDelegateChangesOnInvalidUserNotificationCenter {
  231. if (@available(macOS 10.14, iOS 10.0, *)) {
  232. RandomObject *invalidNotificationCenter = [[RandomObject alloc] init];
  233. OCMStub([self.mockUserNotificationCenter currentNotificationCenter])
  234. .andReturn(invalidNotificationCenter);
  235. [self.proxy swizzleMethodsIfPossible];
  236. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  237. [(id<UNUserNotificationCenterDelegate>)invalidNotificationCenter.delegate
  238. userNotificationCenter:self.mockUserNotificationCenter
  239. willPresentNotification:OCMClassMock([UNNotification class])
  240. withCompletionHandler:^(UNNotificationPresentationOptions options){
  241. }];
  242. }
  243. }
  244. - (void)testSwizzlingInvalidUserNotificationCenterDelegate {
  245. if (@available(macOS 10.14, iOS 10.0, *)) {
  246. RandomObject *invalidDelegate = [[RandomObject alloc] init];
  247. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(invalidDelegate);
  248. [self.proxy swizzleMethodsIfPossible];
  249. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  250. [invalidDelegate userNotificationCenter:self.mockUserNotificationCenter
  251. willPresentNotification:OCMClassMock([UNNotification class])
  252. withCompletionHandler:^(UNNotificationPresentationOptions options){
  253. }];
  254. }
  255. }
  256. // Use a fake delegate that doesn't actually implement the needed delegate method.
  257. // Our swizzled method should not be called.
  258. - (void)testIncompleteUserNotificationCenterDelegateMethod {
  259. if (@available(macOS 10.14, iOS 10.0, *)) {
  260. IncompleteUserNotificationCenterDelegate *delegate =
  261. [[IncompleteUserNotificationCenterDelegate alloc] init];
  262. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  263. [self.proxy swizzleMethodsIfPossible];
  264. // Because the incomplete delete does not implement either of the optional delegate methods, we
  265. // should swizzle nothing. If we had swizzled them, then respondsToSelector: would return YES
  266. // even though the delegate does not implement the methods.
  267. SEL willPresentSelector = @selector(userNotificationCenter:
  268. willPresentNotification:withCompletionHandler:);
  269. XCTAssertFalse([delegate respondsToSelector:willPresentSelector]);
  270. SEL didReceiveResponseSelector = @selector(userNotificationCenter:
  271. didReceiveNotificationResponse:withCompletionHandler:);
  272. XCTAssertFalse([delegate respondsToSelector:didReceiveResponseSelector]);
  273. }
  274. }
  275. // Use an object that does actually implement the optional methods. Both should be called.
  276. - (void)testSwizzledUserNotificationsCenterDelegate {
  277. #if TARGET_OS_IOS || TARGET_OS_OSX
  278. FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
  279. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  280. [self.proxy swizzleMethodsIfPossible];
  281. NSDictionary *message = @{@"message" : @""};
  282. // Verify userNotificationCenter:willPresentNotification:withCompletionHandler:
  283. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  284. if (@available(macOS 10.14, iOS 10.0, tvOS 10.0, *)) {
  285. // Invoking delegate method should also invoke our swizzled method
  286. // The swizzled method uses the +sharedProxy, which should be
  287. // returning our proxy.
  288. // Use non-nil, proper classes, otherwise our SDK bails out.
  289. [delegate userNotificationCenter:self.mockUserNotificationCenter
  290. willPresentNotification:[self userNotificationWithMessage:message]
  291. withCompletionHandler:^(NSUInteger options){
  292. }];
  293. // Verify our original method was called
  294. XCTAssertTrue(delegate.willPresentWasCalled);
  295. // Verify our swizzled method was called
  296. [self.mockMessaging verify];
  297. }
  298. if (@available(macOS 10.14, iOS 10.0, *)) {
  299. // Verify userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
  300. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  301. [delegate userNotificationCenter:self.mockUserNotificationCenter
  302. didReceiveNotificationResponse:[self userNotificationResponseWithMessage:message]
  303. withCompletionHandler:^{
  304. }];
  305. // Verify our original method was called
  306. XCTAssertTrue(delegate.didReceiveResponseWasCalled);
  307. // Verify our swizzled method was called
  308. [self.mockMessaging verify];
  309. }
  310. #endif
  311. }
  312. - (id)userNotificationResponseWithMessage:(NSDictionary *)message {
  313. #if TARGET_OS_IOS || TARGET_OS_OSX
  314. if (@available(macOS 10.14, iOS 10.0, *)) {
  315. // Stub out: response.[mock notification above]
  316. id mockNotificationResponse = OCMClassMock([UNNotificationResponse class]);
  317. id mockNotification = [self userNotificationWithMessage:message];
  318. OCMStub([mockNotificationResponse notification]).andReturn(mockNotification);
  319. return mockNotificationResponse;
  320. }
  321. #endif
  322. return nil;
  323. }
  324. - (UNNotification *)userNotificationWithMessage:(NSDictionary *)message
  325. API_AVAILABLE(macos(10.14), ios(10.0)) {
  326. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  327. #if TARGET_OS_IOS || TARGET_OS_OSX
  328. content.userInfo = message;
  329. #endif
  330. id notificationRequest = OCMClassMock([UNNotificationRequest class]);
  331. OCMStub([notificationRequest content]).andReturn(content);
  332. id notification = OCMClassMock([UNNotification class]);
  333. OCMStub([notification request]).andReturn(notificationRequest);
  334. return notification;
  335. }
  336. @end