FIRMessagingRemoteNotificationsProxyTest.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. @end
  111. @implementation FIRMessagingRemoteNotificationsProxyTest
  112. - (void)setUp {
  113. [super setUp];
  114. [GULAppDelegateSwizzler resetProxyOriginalDelegateOnceToken];
  115. _mockMessaging = OCMClassMock([FIRMessaging class]);
  116. OCMStub([_mockMessaging messaging]).andReturn(_mockMessaging);
  117. _proxy = [[FIRMessagingRemoteNotificationsProxy alloc] init];
  118. _mockProxyClass = OCMClassMock([FIRMessagingRemoteNotificationsProxy class]);
  119. // Update +sharedProxy to always return our test instance
  120. OCMStub([_mockProxyClass sharedProxy]).andReturn(self.proxy);
  121. if (@available(macOS 10.14, iOS 10.0, *)) {
  122. _mockUserNotificationCenter = OCMClassMock([UNUserNotificationCenter class]);
  123. OCMStub([_mockUserNotificationCenter currentNotificationCenter])
  124. .andReturn(_mockUserNotificationCenter);
  125. }
  126. }
  127. - (void)tearDown {
  128. [_mockProxyClass stopMocking];
  129. _mockProxyClass = nil;
  130. [_mockMessaging stopMocking];
  131. _mockMessaging = nil;
  132. if (@available(macOS 10.14, iOS 10.0, *)) {
  133. [_mockUserNotificationCenter stopMocking];
  134. _mockUserNotificationCenter = nil;
  135. }
  136. _proxy = nil;
  137. [super tearDown];
  138. }
  139. #pragma mark - Method Swizzling Tests
  140. #if !TARGET_OS_WATCH // TODO(chliangGoogle) Figure out why WKExtension is not recognized here.
  141. - (void)testSwizzlingNonAppDelegate {
  142. RandomObject *invalidAppDelegate = [[RandomObject alloc] init];
  143. [[GULAppDelegateSwizzler sharedApplication]
  144. setDelegate:(id<GULApplicationDelegate>)invalidAppDelegate];
  145. [self.proxy swizzleMethodsIfPossible];
  146. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  147. [invalidAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  148. didReceiveRemoteNotification:@{}];
  149. }
  150. #endif // !TARGET_OS_WATCH
  151. #if !SWIFT_PACKAGE
  152. // The next 3 tests depend on a sharedApplication which is not available in the Swift PM test env.
  153. - (void)testSwizzledIncompleteAppDelegateRemoteNotificationMethod {
  154. XCTestExpectation *expectation = [self expectationWithDescription:@"completion"];
  155. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  156. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  157. [self.proxy swizzleMethodsIfPossible];
  158. NSDictionary *notification = @{@"test" : @""};
  159. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  160. [incompleteAppDelegate application:[GULAppDelegateSwizzler sharedApplication]
  161. didReceiveRemoteNotification:notification
  162. fetchCompletionHandler:^(UIBackgroundFetchResult result) {
  163. [expectation fulfill];
  164. }];
  165. [self.mockMessaging verify];
  166. [self waitForExpectationsWithTimeout:0.5 handler:nil];
  167. }
  168. // This test demonstrates the difference between Firebase 10 and 11. In 10 and earlier the
  169. // swizzler inserts the old `didReceiveRemoteNotification` method. In 11, the new.
  170. - (void)testIncompleteAppDelegateRemoteNotificationWithFetchHandlerMethod {
  171. IncompleteAppDelegate *incompleteAppDelegate = [[IncompleteAppDelegate alloc] init];
  172. [[GULAppDelegateSwizzler sharedApplication] setDelegate:incompleteAppDelegate];
  173. [self.proxy swizzleMethodsIfPossible];
  174. #if TARGET_OS_IOS || TARGET_OS_TV
  175. SEL remoteNotificationWithFetchHandler = @selector(application:
  176. didReceiveRemoteNotification:fetchCompletionHandler:);
  177. XCTAssertTrue([incompleteAppDelegate respondsToSelector:remoteNotificationWithFetchHandler]);
  178. #endif // TARGET_OS_IOS || TARGET_OS_TV
  179. SEL remoteNotification = @selector(application:didReceiveRemoteNotification:);
  180. XCTAssertFalse([incompleteAppDelegate respondsToSelector:remoteNotification]);
  181. }
  182. - (void)testSwizzledAppDelegateRemoteNotificationMethods {
  183. FakeAppDelegate *appDelegate = [[FakeAppDelegate alloc] init];
  184. [[GULAppDelegateSwizzler sharedApplication] setDelegate:appDelegate];
  185. [self.proxy swizzleMethodsIfPossible];
  186. NSDictionary *notification = @{@"test" : @""};
  187. // Test application:didReceiveRemoteNotification:fetchCompletionHandler:
  188. #if TARGET_OS_IOS || TARGET_OS_TV
  189. // Verify our swizzled method was called
  190. OCMExpect([self.mockMessaging appDidReceiveMessage:notification]);
  191. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  192. didReceiveRemoteNotification:notification
  193. fetchCompletionHandler:^(UIBackgroundFetchResult result) {
  194. XCTAssertEqual(result, UIBackgroundFetchResultNewData);
  195. }];
  196. // Verify our original method was called
  197. XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);
  198. [self.mockMessaging verify];
  199. #endif // TARGET_OS_IOS || TARGET_OS_TV
  200. // Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
  201. NSData *deviceToken = [NSData data];
  202. OCMExpect([self.mockMessaging setAPNSToken:deviceToken]);
  203. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  204. didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
  205. XCTAssertEqual(appDelegate.deviceToken, deviceToken);
  206. [self.mockMessaging verify];
  207. // Verify application:didFailToRegisterForRemoteNotificationsWithError:
  208. NSError *error = [NSError errorWithDomain:@"tests" code:-1 userInfo:nil];
  209. [appDelegate application:[GULAppDelegateSwizzler sharedApplication]
  210. didFailToRegisterForRemoteNotificationsWithError:error];
  211. XCTAssertEqual(appDelegate.registerForRemoteNotificationsError, error);
  212. }
  213. #endif // !SWIFT_PACKAGE
  214. - (void)testListeningForDelegateChangesOnInvalidUserNotificationCenter {
  215. if (@available(macOS 10.14, iOS 10.0, *)) {
  216. RandomObject *invalidNotificationCenter = [[RandomObject alloc] init];
  217. OCMStub([self.mockUserNotificationCenter currentNotificationCenter])
  218. .andReturn(invalidNotificationCenter);
  219. [self.proxy swizzleMethodsIfPossible];
  220. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  221. [(id<UNUserNotificationCenterDelegate>)invalidNotificationCenter.delegate
  222. userNotificationCenter:self.mockUserNotificationCenter
  223. willPresentNotification:[UNNotification alloc]
  224. withCompletionHandler:^(UNNotificationPresentationOptions options){
  225. }];
  226. }
  227. }
  228. - (void)testSwizzlingInvalidUserNotificationCenterDelegate {
  229. if (@available(macOS 10.14, iOS 10.0, *)) {
  230. RandomObject *invalidDelegate = [[RandomObject alloc] init];
  231. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(invalidDelegate);
  232. [self.proxy swizzleMethodsIfPossible];
  233. OCMReject([self.mockMessaging appDidReceiveMessage:[OCMArg any]]);
  234. [invalidDelegate userNotificationCenter:self.mockUserNotificationCenter
  235. willPresentNotification:[UNNotification alloc]
  236. withCompletionHandler:^(UNNotificationPresentationOptions options){
  237. }];
  238. }
  239. }
  240. // Use a fake delegate that doesn't actually implement the needed delegate method.
  241. // Our swizzled method should not be called.
  242. - (void)testIncompleteUserNotificationCenterDelegateMethod {
  243. if (@available(macOS 10.14, iOS 10.0, *)) {
  244. IncompleteUserNotificationCenterDelegate *delegate =
  245. [[IncompleteUserNotificationCenterDelegate alloc] init];
  246. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  247. [self.proxy swizzleMethodsIfPossible];
  248. // Because the incomplete delete does not implement either of the optional delegate methods, we
  249. // should swizzle nothing. If we had swizzled them, then respondsToSelector: would return YES
  250. // even though the delegate does not implement the methods.
  251. SEL willPresentSelector = @selector(userNotificationCenter:
  252. willPresentNotification:withCompletionHandler:);
  253. XCTAssertFalse([delegate respondsToSelector:willPresentSelector]);
  254. SEL didReceiveResponseSelector = @selector(userNotificationCenter:
  255. didReceiveNotificationResponse:withCompletionHandler:);
  256. XCTAssertFalse([delegate respondsToSelector:didReceiveResponseSelector]);
  257. }
  258. }
  259. // Use an object that does actually implement the optional methods. Both should be called.
  260. - (void)testSwizzledUserNotificationsCenterDelegate {
  261. #if !TARGET_OS_TV
  262. FakeUserNotificationCenterDelegate *delegate = [[FakeUserNotificationCenterDelegate alloc] init];
  263. OCMStub([self.mockUserNotificationCenter delegate]).andReturn(delegate);
  264. [self.proxy swizzleMethodsIfPossible];
  265. NSDictionary *message = @{@"message" : @""};
  266. // Verify userNotificationCenter:willPresentNotification:withCompletionHandler:
  267. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  268. if (@available(macOS 10.14, iOS 10.0, tvOS 10.0, *)) {
  269. // Invoking delegate method should also invoke our swizzled method
  270. // The swizzled method uses the +sharedProxy, which should be
  271. // returning our proxy.
  272. // Use non-nil, proper classes, otherwise our SDK bails out.
  273. [delegate userNotificationCenter:self.mockUserNotificationCenter
  274. willPresentNotification:[self userNotificationWithMessage:message]
  275. withCompletionHandler:^(NSUInteger options){
  276. }];
  277. // Verify our original method was called
  278. XCTAssertTrue(delegate.willPresentWasCalled);
  279. // Verify our swizzled method was called
  280. [self.mockMessaging verify];
  281. }
  282. if (@available(macOS 10.14, iOS 10.0, *)) {
  283. // Verify userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
  284. OCMExpect([self.mockMessaging appDidReceiveMessage:message]);
  285. [delegate userNotificationCenter:self.mockUserNotificationCenter
  286. didReceiveNotificationResponse:[self userNotificationResponseWithMessage:message]
  287. withCompletionHandler:^{
  288. }];
  289. // Verify our original method was called
  290. XCTAssertTrue(delegate.didReceiveResponseWasCalled);
  291. // Verify our swizzled method was called
  292. [self.mockMessaging verify];
  293. }
  294. #endif // !TARGET_OS_TV
  295. }
  296. - (id)userNotificationResponseWithMessage:(NSDictionary *)message {
  297. #if !TARGET_OS_TV
  298. if (@available(macOS 10.14, iOS 10.0, *)) {
  299. // Stub out: response.[mock notification above]
  300. id mockNotificationResponse = OCMClassMock([UNNotificationResponse class]);
  301. id mockNotification = [self userNotificationWithMessage:message];
  302. OCMStub([mockNotificationResponse notification]).andReturn(mockNotification);
  303. return mockNotificationResponse;
  304. }
  305. #endif // !TARGET_OS_TV
  306. return nil;
  307. }
  308. - (UNNotification *)userNotificationWithMessage:(NSDictionary *)message
  309. API_AVAILABLE(macos(10.14), ios(10.0)) {
  310. UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
  311. #if !TARGET_OS_TV
  312. content.userInfo = message;
  313. #endif // !TARGET_OS_TV
  314. id notificationRequest = OCMClassMock([UNNotificationRequest class]);
  315. OCMStub([notificationRequest content]).andReturn(content);
  316. id notification = OCMClassMock([UNNotification class]);
  317. OCMStub([notification request]).andReturn(notificationRequest);
  318. return notification;
  319. }
  320. @end