FIRMessagingRemoteNotificationsProxyTest.m 16 KB

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