FIRMessagingRemoteNotificationsProxyTest.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
  58. - (void)application:(UIApplication *)application
  59. didReceiveRemoteNotification:(NSDictionary *)userInfo
  60. fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
  61. self.remoteNotificationWithFetchHandlerWasCalled = YES;
  62. completionHandler(UIBackgroundFetchResultNewData);
  63. }
  64. #endif // TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION
  65. - (void)application:(GULApplication *)application
  66. didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  67. self.deviceToken = deviceToken;
  68. }
  69. - (void)application:(GULApplication *)application
  70. didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
  71. self.registerForRemoteNotificationsError = error;
  72. }
  73. @end
  74. #pragma mark - Incomplete UNUserNotificationCenterDelegate
  75. @interface IncompleteUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
  76. @end
  77. @implementation IncompleteUserNotificationCenterDelegate
  78. @end
  79. #pragma mark - Fake UNUserNotificationCenterDelegate
  80. @interface FakeUserNotificationCenterDelegate : NSObject <UNUserNotificationCenterDelegate>
  81. @property(nonatomic) BOOL willPresentWasCalled;
  82. @property(nonatomic) BOOL didReceiveResponseWasCalled;
  83. @end
  84. @implementation FakeUserNotificationCenterDelegate
  85. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  86. willPresentNotification:(UNNotification *)notification
  87. withCompletionHandler:
  88. (void (^)(UNNotificationPresentationOptions options))completionHandler
  89. API_AVAILABLE(ios(10.0), macos(10.14), tvos(10.0)) {
  90. self.willPresentWasCalled = YES;
  91. }
  92. #if !TARGET_OS_TV
  93. - (void)userNotificationCenter:(UNUserNotificationCenter *)center
  94. didReceiveNotificationResponse:(UNNotificationResponse *)response
  95. withCompletionHandler:(void (^)(void))completionHandler
  96. API_AVAILABLE(ios(10.0), macos(10.14)) {
  97. self.didReceiveResponseWasCalled = YES;
  98. }
  99. #endif // !TARGET_OS_TV
  100. @end
  101. @interface GULAppDelegateSwizzler (FIRMessagingRemoteNotificationsProxyTest)
  102. + (void)resetProxyOriginalDelegateOnceToken;
  103. @end
  104. #pragma mark - Local, Per-Test Properties
  105. @interface FIRMessagingRemoteNotificationsProxyTest : XCTestCase
  106. @property(nonatomic, strong) FIRMessagingRemoteNotificationsProxy *proxy;
  107. @property(nonatomic, strong) id mockProxyClass;
  108. @property(nonatomic, strong) id mockMessaging;
  109. @property(nonatomic, strong) id mockUserNotificationCenter;
  110. @property(nonatomic, strong) UNUserNotificationCenter *currentNotificationCenter;
  111. @end
  112. @implementation FIRMessagingRemoteNotificationsProxyTest
  113. - (void)setUp {
  114. [super setUp];
  115. [GULAppDelegateSwizzler resetProxyOriginalDelegateOnceToken];
  116. _mockMessaging = OCMClassMock([FIRMessaging class]);
  117. OCMStub([_mockMessaging messaging]).andReturn(_mockMessaging);
  118. _proxy = [[FIRMessagingRemoteNotificationsProxy alloc] init];
  119. _mockProxyClass = OCMClassMock([FIRMessagingRemoteNotificationsProxy class]);
  120. // Update +sharedProxy to always return our test instance
  121. OCMStub([_mockProxyClass sharedProxy]).andReturn(self.proxy);
  122. if (@available(macOS 10.14, iOS 10.0, *)) {
  123. _mockUserNotificationCenter = OCMClassMock([UNUserNotificationCenter class]);
  124. _currentNotificationCenter = _mockUserNotificationCenter;
  125. OCMStub([_mockUserNotificationCenter currentNotificationCenter])
  126. .andDo(^(NSInvocation *invocation) {
  127. __autoreleasing UNUserNotificationCenter *currentNotificationCenter =
  128. self->_currentNotificationCenter;
  129. [invocation setReturnValue:&currentNotificationCenter];
  130. });
  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. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  161. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  162. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  163. [self.proxy swizzleMethodsIfPossible];
  164. NSDictionary *notification = @{@"test" : @""};
  165. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  166. [incompleteAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  167. didReceiveRemoteNotification:notification
  168. fetchCompletionHandler:^(UIBackgroundFetchResult result) {
  169. [expectation fulfill];
  170. }];
  171. [self.mockMessaging verify];
  172. [self waitForExpectationsWithTimeout:0.5 handler:nil];
  173. }
  174. // This test demonstrates the difference between Firebase 10 and 11. In 10 and earlier the
  175. // swizzler inserts the old `didReceiveRemoteNotification` method. In 11, the new.
  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. XCTAssertTrue([incompleteAppDelegate respondsToSelector:remoteNotificationWithFetchHandler]);
  184. #endif // TARGET_OS_IOS || TARGET_OS_TV
  185. SEL remoteNotification = @selector(application:didReceiveRemoteNotification:);
  186. XCTAssertFalse([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:fetchCompletionHandler:
  194. #if TARGET_OS_IOS || TARGET_OS_TV
  195. // Verify our swizzled method was called
  196. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  197. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  198. didReceiveRemoteNotification:notification
  199. fetchCompletionHandler:^(UIBackgroundFetchResult result) {
  200. XCTAssertEqual(result, UIBackgroundFetchResultNewData);
  201. }];
  202. // Verify our original method was called
  203. XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);
  204. [self.mockMessaging verify];
  205. #endif // TARGET_OS_IOS || TARGET_OS_TV
  206. // Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
  207. NSData *deviceToken = [NSData data];
  208. OCMExpect([self.mockMessaging setAPNSToken:deviceToken]);
  209. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  210. didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  211. XCTAssertEqual(appDelegate.deviceToken, deviceToken);
  212. [self.mockMessaging verify];
  213. // Verify application:didFailToRegisterForRemoteNotificationsWithError:
  214. NSError *error = [NSError errorWithDomain:@"tests" code:-1 userInfo:nil];
  215. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  216. didFailToRegisterForRemoteNotificationsWithError:error];
  217. XCTAssertEqual(appDelegate.registerForRemoteNotificationsError, error);
  218. }
  219. #endif // !SWIFT_PACKAGE
  220. - (void)testListeningForDelegateChangesOnInvalidUserNotificationCenter {
  221. if (@available(macOS 10.14, iOS 10.0, *)) {
  222. RandomObject *invalidNotificationCenter = [[RandomObject alloc] init];
  223. OCMStub([self.mockUserNotificationCenter currentNotificationCenter])
  224. .andReturn(invalidNotificationCenter);
  225. [self.proxy swizzleMethodsIfPossible];
  226. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  227. [(id<UNUserNotificationCenterDelegate>)invalidNotificationCenter.delegate
  228. userNotificationCenter:self.mockUserNotificationCenter
  229. willPresentNotification:[UNNotification alloc]
  230. withCompletionHandler:^(UNNotificationPresentationOptions options){
  231. }];
  232. }
  233. }
  234. - (void)testSwizzlingInvalidUserNotificationCenterDelegate {
  235. if (@available(macOS 10.14, iOS 10.0, *)) {
  236. RandomObject *invalidDelegate = [[RandomObject alloc] init];
  237. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(invalidDelegate);
  238. [self.proxy swizzleMethodsIfPossible];
  239. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  240. [invalidDelegate userNotificationCenter:self.mockUserNotificationCenter
  241. willPresentNotification:[UNNotification alloc]
  242. withCompletionHandler:^(UNNotificationPresentationOptions options){
  243. }];
  244. }
  245. }
  246. // Use a fake delegate that doesn't actually implement the needed delegate method.
  247. // Our swizzled method should not be called.
  248. - (void)testIncompleteUserNotificationCenterDelegateMethod {
  249. if (@available(macOS 10.14, iOS 10.0, *)) {
  250. IncompleteUserNotificationCenterDelegate *delegate =
  251. [[IncompleteUserNotificationCenterDelegate alloc] init];
  252. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  253. [self.proxy swizzleMethodsIfPossible];
  254. // Because the incomplete delete does not implement either of the optional delegate methods, we
  255. // should swizzle nothing. If we had swizzled them, then respondsToSelector: would return YES
  256. // even though the delegate does not implement the methods.
  257. SEL willPresentSelector = @selector(userNotificationCenter:
  258. willPresentNotification:withCompletionHandler:);
  259. XCTAssertFalse([delegate respondsToSelector:willPresentSelector]);
  260. SEL didReceiveResponseSelector = @selector(userNotificationCenter:
  261. didReceiveNotificationResponse:withCompletionHandler:);
  262. XCTAssertFalse([delegate respondsToSelector:didReceiveResponseSelector]);
  263. }
  264. }
  265. // Use an object that does actually implement the optional methods. Both should be called.
  266. - (void)testSwizzledUserNotificationsCenterDelegate {
  267. #if !TARGET_OS_TV
  268. FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
  269. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  270. [self.proxy swizzleMethodsIfPossible];
  271. NSDictionary *message = @{@"message" : @""};
  272. // Verify userNotificationCenter:willPresentNotification:withCompletionHandler:
  273. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  274. if (@available(macOS 10.14, iOS 10.0, tvOS 10.0, *)) {
  275. // Invoking delegate method should also invoke our swizzled method
  276. // The swizzled method uses the +sharedProxy, which should be
  277. // returning our proxy.
  278. // Use non-nil, proper classes, otherwise our SDK bails out.
  279. [delegate userNotificationCenter:self.mockUserNotificationCenter
  280. willPresentNotification:[self userNotificationWithMessage:message]
  281. withCompletionHandler:^(NSUInteger options){
  282. }];
  283. // Verify our original method was called
  284. XCTAssertTrue(delegate.willPresentWasCalled);
  285. // Verify our swizzled method was called
  286. [self.mockMessaging verify];
  287. }
  288. if (@available(macOS 10.14, iOS 10.0, *)) {
  289. // Verify userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
  290. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  291. [delegate userNotificationCenter:self.mockUserNotificationCenter
  292. didReceiveNotificationResponse:[self userNotificationResponseWithMessage:message]
  293. withCompletionHandler:^{
  294. }];
  295. // Verify our original method was called
  296. XCTAssertTrue(delegate.didReceiveResponseWasCalled);
  297. // Verify our swizzled method was called
  298. [self.mockMessaging verify];
  299. }
  300. #endif // !TARGET_OS_TV
  301. }
  302. - (id)userNotificationResponseWithMessage:(NSDictionary *)message {
  303. #if !TARGET_OS_TV
  304. if (@available(macOS 10.14, iOS 10.0, *)) {
  305. // Stub out: response.[mock notification above]
  306. id mockNotificationResponse = OCMClassMock([UNNotificationResponse class]);
  307. id mockNotification = [self userNotificationWithMessage:message];
  308. OCMStub([mockNotificationResponse notification]).andReturn(mockNotification);
  309. return mockNotificationResponse;
  310. }
  311. #endif // !TARGET_OS_TV
  312. return nil;
  313. }
  314. - (UNNotification *)userNotificationWithMessage:(NSDictionary *)message
  315. API_AVAILABLE(macos(10.14), ios(10.0)) {
  316. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  317. #if !TARGET_OS_TV
  318. content.userInfo = message;
  319. #endif // !TARGET_OS_TV
  320. id notificationRequest = OCMClassMock([UNNotificationRequest class]);
  321. OCMStub([notificationRequest content]).andReturn(content);
  322. id notification = OCMClassMock([UNNotification class]);
  323. OCMStub([notification request]).andReturn(notificationRequest);
  324. return notification;
  325. }
  326. @end