FIRMessagingHandlingTest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. - (BOOL)handleContextManagerMessage:(NSDictionary *)message;
  34. - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message;
  35. @end
  36. /*
  37. * This class checks if we handle the received message properly
  38. * based on each type of messages. Checks include duplicate message handling,
  39. * analytics logging, etc.
  40. */
  41. @interface FIRMessagingHandlingTest : XCTestCase
  42. @property(nonatomic, strong) FIRMessagingAnalytics *messageAnalytics;
  43. @property(nonatomic, strong) id mockFirebaseApp;
  44. @property(nonatomic, strong) id mockMessagingAnalytics;
  45. @property(nonatomic, strong) FIRMessagingTestUtilities *testUtil;
  46. @end
  47. @implementation FIRMessagingHandlingTest
  48. - (void)setUp {
  49. [super setUp];
  50. // Create the messaging instance with all the necessary dependencies.
  51. NSUserDefaults *defaults =
  52. [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingDefaultsTestDomain];
  53. _testUtil = [[FIRMessagingTestUtilities alloc] initWithUserDefaults:defaults withRMQManager:YES];
  54. _mockFirebaseApp = OCMClassMock([FIRApp class]);
  55. OCMStub([_mockFirebaseApp defaultApp]).andReturn(_mockFirebaseApp);
  56. [[NSUserDefaults standardUserDefaults]
  57. removePersistentDomainForName:[NSBundle mainBundle].bundleIdentifier];
  58. _mockMessagingAnalytics = OCMClassMock([FIRMessagingAnalytics class]);
  59. }
  60. - (void)tearDown {
  61. [_testUtil cleanupAfterTest];
  62. [_mockMessagingAnalytics stopMocking];
  63. [_mockFirebaseApp stopMocking];
  64. [super tearDown];
  65. }
  66. -(void)testEmptyNotification {
  67. XCTAssertEqualObjects(@(FIRMessagingMessageStatusUnknown), @([_testUtil.mockMessaging appDidReceiveMessage:@{}].status));
  68. }
  69. -(void)testAPNSDisplayNotification {
  70. NSDictionary *notificationPayload = @{
  71. @"aps": @{
  72. @"alert" : @{
  73. @"body" : @"body of notification",
  74. @"title" : @"title of notification",
  75. }
  76. },
  77. @"gcm.message_id" : @"1566515013484879",
  78. @"gcm.n.e" : @1,
  79. @"google.c.a.c_id" : @"7379928225816991517",
  80. @"google.c.a.e" : @1,
  81. @"google.c.a.ts" : @1566515009,
  82. @"google.c.a.udt" : @0
  83. };
  84. OCMExpect([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  85. OCMExpect([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  86. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  87. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  88. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  89. OCMVerifyAll(_testUtil.mockMessaging);
  90. OCMReject([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  91. OCMReject([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  92. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  93. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  94. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  95. OCMVerifyAll(_testUtil.mockMessaging);
  96. }
  97. -(void)testAPNSContentAvailableNotification {
  98. NSDictionary *notificationPayload = @{
  99. @"aps": @{
  100. @"content-available" : @1
  101. },
  102. @"gcm.message_id" : @"1566513591299872",
  103. @"image" : @"bunny.png",
  104. @"google.c.a.e" : @1
  105. };
  106. OCMExpect([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  107. OCMExpect([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  108. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  109. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  110. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  111. OCMVerifyAll(_testUtil.mockMessaging);
  112. OCMReject([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  113. OCMReject([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  114. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  115. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  116. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  117. OCMVerifyAll(_testUtil.mockMessaging);
  118. }
  119. -(void)testAPNSContentAvailableContextualNotification {
  120. NSDictionary *notificationPayload = @{
  121. @"aps" : @{
  122. @"content-available": @1
  123. },
  124. @"gcm.message_id": @"1566515531287827",
  125. @"gcm.n.e" : @1,
  126. @"gcm.notification.body" : @"Local time zone message!",
  127. @"gcm.notification.title" : @"Hello",
  128. @"gcms" : @"gcm.gmsproc.cm",
  129. @"google.c.a.c_id" : @"5941428497527920876",
  130. @"google.c.a.e" : @1,
  131. @"google.c.a.ts" : @1566565920,
  132. @"google.c.a.udt" : @1,
  133. @"google.c.cm.cat" : @"com.google.firebase.messaging.testapp.dev",
  134. @"google.c.cm.lt_end" : @"2019-09-20 13:12:00",
  135. @"google.c.cm.lt_start" : @"2019-08-23 13:12:00",
  136. };
  137. OCMExpect([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  138. OCMExpect([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  139. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  140. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  141. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  142. OCMVerifyAll(_testUtil.mockMessaging);
  143. OCMReject([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  144. OCMReject([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  145. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  146. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  147. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  148. OCMVerifyAll(_testUtil.mockMessaging);
  149. }
  150. -(void)testContextualLocalNotification {
  151. NSDictionary *notificationPayload = @{
  152. @"gcm.message_id": @"1566515531281975",
  153. @"gcm.n.e" : @1,
  154. @"gcm.notification.body" : @"Local time zone message!",
  155. @"gcm.notification.title" : @"Hello",
  156. @"gcms" : @"gcm.gmsproc.cm",
  157. @"google.c.a.c_id" : @"5941428497527920876",
  158. @"google.c.a.e" : @1,
  159. @"google.c.a.ts" : @1566565920,
  160. @"google.c.a.udt" : @1,
  161. };
  162. OCMExpect([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  163. OCMExpect([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  164. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  165. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  166. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  167. OCMVerifyAll(_testUtil.mockMessaging);
  168. OCMReject([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  169. OCMReject([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  170. OCMReject([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  171. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  172. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  173. OCMVerifyAll(_testUtil.mockMessaging);
  174. }
  175. -(void)testMCSNotification {
  176. NSDictionary *notificationPayload = @{
  177. @"from" : @"35006771263",
  178. @"image" : @"bunny.png"
  179. };
  180. OCMExpect([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  181. OCMExpect([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  182. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  183. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  184. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  185. OCMVerifyAll(_testUtil.mockMessaging);
  186. OCMExpect([_testUtil.mockMessaging handleContextManagerMessage:notificationPayload]);
  187. OCMExpect([_testUtil.mockMessaging handleIncomingLinkIfNeededFromMessage:notificationPayload]);
  188. OCMExpect([_mockMessagingAnalytics logMessage:notificationPayload toAnalytics:[OCMArg any]]);
  189. XCTAssertEqualObjects(@(FIRMessagingMessageStatusNew),
  190. @([_testUtil.messaging appDidReceiveMessage:notificationPayload].status));
  191. OCMVerifyAll(_testUtil.mockMessaging);
  192. }
  193. @end