FIRMessagingHandlingTest.m 11 KB

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