FIRMessagingRemoteNotificationsProxyTest.m 15 KB

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