FIRMessagingRemoteNotificationsProxyTest.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 <OCMock/OCMock.h>
  21. #import <XCTest/XCTest.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. #if !TARGET_OS_WATCH // TODO(chliangGoogle) Figure out why WKExtension is not recognized here.
  153. - (void)testSwizzlingNonAppDelegate {
  154. RandomObject *invalidAppDelegate = [[RandomObject alloc] init];
  155. [[GULAppDelegateSwizzler sharedApplication]
  156. setDelegate:(id<GULApplicationDelegate>)invalidAppDelegate];
  157. [self.proxy swizzleMethodsIfPossible];
  158. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  159. [invalidAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  160. didReceiveRemoteNotification:@{}];
  161. }
  162. #endif
  163. #if !SWIFT_PACKAGE
  164. // The next 3 tests depend on a sharedApplication which is not available in the Swift PM test env.
  165. - (void)testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
  166. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  167. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  168. [self.proxy swizzleMethodsIfPossible];
  169. NSDictionary *notification = @{@"test" : @""};
  170. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  171. #pragma clang diagnostic push
  172. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  173. [incompleteAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  174. didReceiveRemoteNotification:notification];
  175. #pragma clang diagnostic pop
  176. [self.mockMessaging verify];
  177. }
  178. - (void)testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
  179. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  180. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  181. [self.proxy swizzleMethodsIfPossible];
  182. #if TARGET_OS_IOS || TARGET_OS_TV
  183. SEL remoteNotificationWithFetchHandler = @selector(application:
  184. didReceiveRemoteNotification:fetchCompletionHandler:);
  185. XCTAssertFalse([incompleteAppDelegate respondsToSelector:remoteNotificationWithFetchHandler]);
  186. #endif
  187. SEL remoteNotification = @selector(application:didReceiveRemoteNotification:);
  188. XCTAssertTrue([incompleteAppDelegate respondsToSelector:remoteNotification]);
  189. }
  190. - (void)testSwizzledAppDelegateRemoteNotificationMethods {
  191. FakeAppDelegate *appDelegate = [[FakeAppDelegate alloc] init];
  192. [[GULAppDelegateSwizzler sharedApplication] setDelegate:appDelegate];
  193. [self.proxy swizzleMethodsIfPossible];
  194. NSDictionary *notification = @{@"test" : @""};
  195. // Test application:didReceiveRemoteNotification:
  196. // Verify our swizzled method was called
  197. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  198. #pragma clang diagnostic push
  199. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  200. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  201. didReceiveRemoteNotification:notification];
  202. #pragma clang diagnostic pop
  203. // Verify our original method was called
  204. XCTAssertTrue(appDelegate.remoteNotificationMethodWasCalled);
  205. [self.mockMessaging verify];
  206. // Test application:didReceiveRemoteNotification:fetchCompletionHandler:
  207. #if TARGET_OS_IOS || TARGET_OS_TV
  208. // Verify our swizzled method was called
  209. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  210. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  211. didReceiveRemoteNotification:notification
  212. fetchCompletionHandler:^(UIBackgroundFetchResult result){
  213. }];
  214. // Verify our original method was called
  215. XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);
  216. [self.mockMessaging verify];
  217. #endif
  218. // Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
  219. NSData *deviceToken = [NSData data];
  220. OCMExpect([self.mockMessaging setAPNSToken:deviceToken]);
  221. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  222. didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  223. XCTAssertEqual(appDelegate.deviceToken, deviceToken);
  224. [self.mockMessaging verify];
  225. // Verify application:didFailToRegisterForRemoteNotificationsWithError:
  226. NSError *error = [NSError errorWithDomain:@"tests" code:-1 userInfo:nil];
  227. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  228. didFailToRegisterForRemoteNotificationsWithError:error];
  229. XCTAssertEqual(appDelegate.registerForRemoteNotificationsError, error);
  230. }
  231. #endif
  232. - (void)testListeningForDelegateChangesOnInvalidUserNotificationCenter {
  233. if (@available(macOS 10.14, iOS 10.0, *)) {
  234. RandomObject *invalidNotificationCenter = [[RandomObject alloc] init];
  235. OCMStub([self.mockUserNotificationCenter currentNotificationCenter])
  236. .andReturn(invalidNotificationCenter);
  237. [self.proxy swizzleMethodsIfPossible];
  238. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  239. [(id<UNUserNotificationCenterDelegate>)invalidNotificationCenter.delegate
  240. userNotificationCenter:self.mockUserNotificationCenter
  241. willPresentNotification:OCMClassMock([UNNotification class])
  242. withCompletionHandler:^(UNNotificationPresentationOptions options){
  243. }];
  244. }
  245. }
  246. - (void)testSwizzlingInvalidUserNotificationCenterDelegate {
  247. if (@available(macOS 10.14, iOS 10.0, *)) {
  248. RandomObject *invalidDelegate = [[RandomObject alloc] init];
  249. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(invalidDelegate);
  250. [self.proxy swizzleMethodsIfPossible];
  251. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  252. [invalidDelegate userNotificationCenter:self.mockUserNotificationCenter
  253. willPresentNotification:OCMClassMock([UNNotification class])
  254. withCompletionHandler:^(UNNotificationPresentationOptions options){
  255. }];
  256. }
  257. }
  258. // Use a fake delegate that doesn't actually implement the needed delegate method.
  259. // Our swizzled method should not be called.
  260. - (void)testIncompleteUserNotificationCenterDelegateMethod {
  261. if (@available(macOS 10.14, iOS 10.0, *)) {
  262. IncompleteUserNotificationCenterDelegate *delegate =
  263. [[IncompleteUserNotificationCenterDelegate alloc] init];
  264. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  265. [self.proxy swizzleMethodsIfPossible];
  266. // Because the incomplete delete does not implement either of the optional delegate methods, we
  267. // should swizzle nothing. If we had swizzled them, then respondsToSelector: would return YES
  268. // even though the delegate does not implement the methods.
  269. SEL willPresentSelector = @selector(userNotificationCenter:
  270. willPresentNotification:withCompletionHandler:);
  271. XCTAssertFalse([delegate respondsToSelector:willPresentSelector]);
  272. SEL didReceiveResponseSelector = @selector(userNotificationCenter:
  273. didReceiveNotificationResponse:withCompletionHandler:);
  274. XCTAssertFalse([delegate respondsToSelector:didReceiveResponseSelector]);
  275. }
  276. }
  277. // Use an object that does actually implement the optional methods. Both should be called.
  278. - (void)testSwizzledUserNotificationsCenterDelegate {
  279. #if TARGET_OS_IOS || TARGET_OS_OSX
  280. FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
  281. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  282. [self.proxy swizzleMethodsIfPossible];
  283. NSDictionary *message = @{@"message" : @""};
  284. // Verify userNotificationCenter:willPresentNotification:withCompletionHandler:
  285. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  286. if (@available(macOS 10.14, iOS 10.0, tvOS 10.0, *)) {
  287. // Invoking delegate method should also invoke our swizzled method
  288. // The swizzled method uses the +sharedProxy, which should be
  289. // returning our proxy.
  290. // Use non-nil, proper classes, otherwise our SDK bails out.
  291. [delegate userNotificationCenter:self.mockUserNotificationCenter
  292. willPresentNotification:[self userNotificationWithMessage:message]
  293. withCompletionHandler:^(NSUInteger options){
  294. }];
  295. // Verify our original method was called
  296. XCTAssertTrue(delegate.willPresentWasCalled);
  297. // Verify our swizzled method was called
  298. [self.mockMessaging verify];
  299. }
  300. if (@available(macOS 10.14, iOS 10.0, *)) {
  301. // Verify userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
  302. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  303. [delegate userNotificationCenter:self.mockUserNotificationCenter
  304. didReceiveNotificationResponse:[self userNotificationResponseWithMessage:message]
  305. withCompletionHandler:^{
  306. }];
  307. // Verify our original method was called
  308. XCTAssertTrue(delegate.didReceiveResponseWasCalled);
  309. // Verify our swizzled method was called
  310. [self.mockMessaging verify];
  311. }
  312. #endif
  313. }
  314. - (id)userNotificationResponseWithMessage:(NSDictionary *)message {
  315. #if TARGET_OS_IOS || TARGET_OS_OSX
  316. if (@available(macOS 10.14, iOS 10.0, *)) {
  317. // Stub out: response.[mock notification above]
  318. id mockNotificationResponse = OCMClassMock([UNNotificationResponse class]);
  319. id mockNotification = [self userNotificationWithMessage:message];
  320. OCMStub([mockNotificationResponse notification]).andReturn(mockNotification);
  321. return mockNotificationResponse;
  322. }
  323. #endif
  324. return nil;
  325. }
  326. - (UNNotification *)userNotificationWithMessage:(NSDictionary *)message
  327. API_AVAILABLE(macos(10.14), ios(10.0)) {
  328. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  329. #if TARGET_OS_IOS || TARGET_OS_OSX
  330. content.userInfo = message;
  331. #endif
  332. id notificationRequest = OCMClassMock([UNNotificationRequest class]);
  333. OCMStub([notificationRequest content]).andReturn(content);
  334. id notification = OCMClassMock([UNNotification class]);
  335. OCMStub([notification request]).andReturn(notificationRequest);
  336. return notification;
  337. }
  338. @end