FIRMessagingHandlingTest.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import <FirebaseCore/FIRAppInternal.h>
  19. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  20. #import <FirebaseAnalyticsInterop/FIRAnalyticsInterop.h>
  21. #import <FirebaseMessaging/FIRMessaging.h>
  22. #import "Example/Messaging/Tests/FIRMessagingTestUtilities.h"
  23. #import "Firebase/Messaging/FIRMessaging_Private.h"
  24. #import "Firebase/Messaging/FIRMessagingAnalytics.h"
  25. #import "Firebase/Messaging/FIRMessagingRmqManager.h"
  26. #import "Firebase/Messaging/FIRMessagingSyncMessageManager.h"
  27. extern NSString *const kFIRMessagingFCMTokenFetchAPNSOption;
  28. /// The NSUserDefaults domain for testing.
  29. static NSString *const kFIRMessagingDefaultsTestDomain = @"com.messaging.tests";
  30. @interface FIRMessaging ()
  31. @property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
  32. @property(nonatomic, readwrite, strong) FIRInstanceID *instanceID;
  33. @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager;
  34. - (BOOL)handleContextManagerMessage:(NSDictionary *)message;
  35. - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message;
  36. @end
  37. /*
  38. * This class checks if we handle the received message properly
  39. * based on each type of messages. Checks include duplicate message handling,
  40. * analytics logging, etc.
  41. */
  42. @interface FIRMessagingHandlingTest : XCTestCase
  43. @property(nonatomic, readonly, strong) FIRMessaging *messaging;
  44. @property(nonatomic, strong) FIRMessagingAnalytics *messageAnalytics;
  45. @property(nonatomic, strong) id mockMessaging;
  46. @property(nonatomic, strong) id mockInstanceID;
  47. @property(nonatomic, strong) id mockFirebaseApp;
  48. @property(nonatomic, strong) id mockMessagingAnalytics;
  49. @end
  50. @implementation FIRMessagingHandlingTest
  51. - (void)setUp {
  52. [super setUp];
  53. // Create the messaging instance with all the necessary dependencies.
  54. NSUserDefaults *defaults =
  55. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain];
  56. _messaging = [FIRMessagingTestUtilities messagingForTestsWithUserDefaults:defaults];
  57. _mockFirebaseApp = OCMClassMock([FIRApp class]);
  58. OCMStub([_mockFirebaseApp defaultApp]).andReturn(_mockFirebaseApp);
  59. _mockInstanceID = OCMPartialMock(self.messaging.instanceID);
  60. [[NSUserDefaults standardUserDefaults]
  61. removePersistentDomainForName:[NSBundle mainBundle].bundleIdentifier];
  62. _mockMessaging = OCMPartialMock(_messaging);
  63. _mockMessagingAnalytics = OCMClassMock([FIRMessagingAnalytics class]);
  64. }
  65. - (void)tearDown {
  66. [self.messaging.messagingUserDefaults removePersistentDomainForName:kFIRMessagingDefaultsTestDomain];
  67. self.messaging.shouldEstablishDirectChannel = NO;
  68. self.messaging.defaultFcmToken = nil;
  69. [_mockMessagingAnalytics stopMocking];
  70. [_mockMessaging stopMocking];
  71. [_mockInstanceID stopMocking];
  72. [_mockFirebaseApp stopMocking];
  73. _messaging = nil;
  74. [super tearDown];
  75. }
  76. -(void)testEmptyNotification {
  77. XCTAssertEqualObjects(@(FIRMessagingMessageStatusUnknown), @([_mockMessaging appDidReceiveMessage:@{}].status));
  78. }
  79. -(void)testAPNSDisplayNotification {
  80. NSDictionary *notificationPayload = @{
  81. @"aps": @{
  82. @"alert" : @{
  83. @"body" : @"body of notification",
  84. @"title" : @"title of notification",
  85. }
  86. },
  87. @"gcm.message_id" : @"1566515013484879",
  88. @"gcm.n.e" : @1,
  89. @"google.c.a.c_id" : @"7379928225816991517",
  90. @"google.c.a.e" : @1,
  91. @"google.c.a.ts" : @1566515009,
  92. @"google.c.a.udt" : @0
  93. };
  94. OCMExpect([_mockMessaging handleContextManagerMessage:notificationPayload]);
  95. OCMExpect([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  96. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  97. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  98. @([_messaging appDidReceiveMessage:notificationPayload].status));
  99. OCMVerifyAll(_mockMessaging);
  100. OCMReject([_mockMessaging handleContextManagerMessage:notificationPayload]);
  101. OCMReject([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  102. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  103. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  104. @([_messaging appDidReceiveMessage:notificationPayload].status));
  105. OCMVerifyAll(_mockMessaging);
  106. // Clear database
  107. [_messaging.rmq2Manager deleteSyncMessageWithRmqID:@"1566515013484879"];
  108. }
  109. -(void)testAPNSContentAvailableNotification {
  110. NSDictionary *notificationPayload = @{
  111. @"aps": @{
  112. @"content-available" : @1
  113. },
  114. @"gcm.message_id" : @"1566513591299872",
  115. @"image" : @"bunny.png",
  116. @"google.c.a.e" : @1
  117. };
  118. OCMExpect([_mockMessaging handleContextManagerMessage:notificationPayload]);
  119. OCMExpect([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  120. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  121. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  122. @([_messaging appDidReceiveMessage:notificationPayload].status));
  123. OCMVerifyAll(_mockMessaging);
  124. OCMReject([_mockMessaging handleContextManagerMessage:notificationPayload]);
  125. OCMReject([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  126. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  127. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  128. @([_messaging appDidReceiveMessage:notificationPayload].status));
  129. OCMVerifyAll(_mockMessaging);
  130. [_messaging.rmq2Manager deleteSyncMessageWithRmqID:@"1566513591299872"];
  131. }
  132. -(void)testAPNSContentAvailableContextualNotification {
  133. NSDictionary *notificationPayload = @{
  134. @"aps" : @{
  135. @"content-available": @1
  136. },
  137. @"gcm.message_id": @"1566515531287827",
  138. @"gcm.n.e" : @1,
  139. @"gcm.notification.body" : @"Local time zone message!",
  140. @"gcm.notification.title" : @"Hello",
  141. @"gcms" : @"gcm.gmsproc.cm",
  142. @"google.c.a.c_id" : @"5941428497527920876",
  143. @"google.c.a.e" : @1,
  144. @"google.c.a.ts" : @1566565920,
  145. @"google.c.a.udt" : @1,
  146. @"google.c.cm.cat" : @"com.google.firebase.messaging.testapp.dev",
  147. @"google.c.cm.lt_end" : @"2019-09-20 13:12:00",
  148. @"google.c.cm.lt_start" : @"2019-08-23 13:12:00",
  149. };
  150. OCMExpect([_mockMessaging handleContextManagerMessage:notificationPayload]);
  151. OCMExpect([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  152. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  153. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  154. @([_messaging appDidReceiveMessage:notificationPayload].status));
  155. OCMVerifyAll(_mockMessaging);
  156. OCMReject([_mockMessaging handleContextManagerMessage:notificationPayload]);
  157. OCMReject([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  158. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  159. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  160. @([_messaging appDidReceiveMessage:notificationPayload].status));
  161. OCMVerifyAll(_mockMessaging);
  162. [_messaging.rmq2Manager deleteSyncMessageWithRmqID:@"1566515531287827"];
  163. }
  164. -(void)testContextualLocalNotification {
  165. NSDictionary *notificationPayload = @{
  166. @"gcm.message_id": @"1566515531281975",
  167. @"gcm.n.e" : @1,
  168. @"gcm.notification.body" : @"Local time zone message!",
  169. @"gcm.notification.title" : @"Hello",
  170. @"gcms" : @"gcm.gmsproc.cm",
  171. @"google.c.a.c_id" : @"5941428497527920876",
  172. @"google.c.a.e" : @1,
  173. @"google.c.a.ts" : @1566565920,
  174. @"google.c.a.udt" : @1,
  175. };
  176. OCMExpect([_mockMessaging handleContextManagerMessage:notificationPayload]);
  177. OCMExpect([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  178. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  179. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  180. @([_messaging appDidReceiveMessage:notificationPayload].status));
  181. OCMVerifyAll(_mockMessaging);
  182. OCMReject([_mockMessaging handleContextManagerMessage:notificationPayload]);
  183. OCMReject([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  184. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  185. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  186. @([_messaging appDidReceiveMessage:notificationPayload].status));
  187. OCMVerifyAll(_mockMessaging);
  188. [_messaging.rmq2Manager deleteSyncMessageWithRmqID:@"1566515531281975"];
  189. }
  190. -(void)testMCSNotification {
  191. NSDictionary *notificationPayload = @{
  192. @"from" : @"35006771263",
  193. @"image" : @"bunny.png"
  194. };
  195. OCMExpect([_mockMessaging handleContextManagerMessage:notificationPayload]);
  196. OCMExpect([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  197. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  198. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  199. @([_messaging appDidReceiveMessage:notificationPayload].status));
  200. OCMVerifyAll(_mockMessaging);
  201. OCMExpect([_mockMessaging handleContextManagerMessage:notificationPayload]);
  202. OCMExpect([_mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  203. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  204. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  205. @([_messaging appDidReceiveMessage:notificationPayload].status));
  206. OCMVerifyAll(_mockMessaging);
  207. }
  208. @end