FIRMessagingContextManagerServiceTest.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <XCTest/XCTest.h>
  18. #import "Firebase/Messaging/FIRMessagingContextManagerService.h"
  19. @interface FIRMessagingContextManagerServiceTest : XCTestCase
  20. @property(nonatomic, readwrite, strong) NSDateFormatter *dateFormatter;
  21. @property(nonatomic, readwrite, strong) NSMutableArray *scheduledLocalNotifications;
  22. @end
  23. @implementation FIRMessagingContextManagerServiceTest
  24. - (void)setUp {
  25. [super setUp];
  26. self.dateFormatter = [[NSDateFormatter alloc] init];
  27. self.dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  28. [self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  29. self.scheduledLocalNotifications = [NSMutableArray array];
  30. [self mockSchedulingLocalNotifications];
  31. }
  32. - (void)tearDown {
  33. [super tearDown];
  34. }
  35. /**
  36. * Test invalid context manager message, missing lt_start string.
  37. */
  38. - (void)testInvalidContextManagerMessage_missingStartTime {
  39. NSDictionary *message = @{
  40. @"hello" : @"world",
  41. };
  42. XCTAssertFalse([FIRMessagingContextManagerService isContextManagerMessage:message]);
  43. }
  44. /**
  45. * Test valid context manager message.
  46. */
  47. - (void)testValidContextManagerMessage {
  48. NSDictionary *message = @{
  49. kFIRMessagingContextManagerLocalTimeStart: @"2015-12-12 00:00:00",
  50. @"hello" : @"world",
  51. };
  52. XCTAssertTrue([FIRMessagingContextManagerService isContextManagerMessage:message]);
  53. }
  54. // TODO: Enable these tests. They fail because we cannot schedule local
  55. // notifications on OSX without permission. It's better to mock AppDelegate's
  56. // scheduleLocalNotification to mock scheduling behavior.
  57. /**
  58. * Context Manager message with future start date should be successfully scheduled.
  59. */
  60. - (void)testMessageWithFutureStartTime {
  61. #if TARGET_OS_IOS
  62. NSString *messageIdentifier = @"fcm-cm-test1";
  63. NSString *startTimeString = @"2020-01-12 12:00:00"; // way into the future
  64. NSDictionary *message = @{
  65. kFIRMessagingContextManagerLocalTimeStart: startTimeString,
  66. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  67. @"id": messageIdentifier,
  68. @"hello" : @"world"
  69. };
  70. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  71. XCTAssertEqual(self.scheduledLocalNotifications.count, 1);
  72. #pragma clang diagnostic push
  73. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  74. UILocalNotification *notification = [self.scheduledLocalNotifications firstObject];
  75. #pragma clang diagnostic pop
  76. NSDate *date = [self.dateFormatter dateFromString:startTimeString];
  77. XCTAssertEqual([notification.fireDate compare:date], NSOrderedSame);
  78. #endif
  79. }
  80. /**
  81. * Context Manager message with past end date should not be scheduled.
  82. */
  83. - (void)testMessageWithPastEndTime {
  84. #if TARGET_OS_IOS
  85. NSString *messageIdentifier = @"fcm-cm-test1";
  86. NSString *startTimeString = @"2010-01-12 12:00:00"; // way into the past
  87. NSString *endTimeString = @"2011-01-12 12:00:00"; // way into the past
  88. NSDictionary *message = @{
  89. kFIRMessagingContextManagerLocalTimeStart: startTimeString,
  90. kFIRMessagingContextManagerLocalTimeEnd : endTimeString,
  91. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  92. @"id": messageIdentifier,
  93. @"hello" : @"world"
  94. };
  95. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  96. XCTAssertEqual(self.scheduledLocalNotifications.count, 0);
  97. #endif
  98. }
  99. /**
  100. * Context Manager message with past start and future end date should be successfully
  101. * scheduled.
  102. */
  103. - (void)testMessageWithPastStartAndFutureEndTime {
  104. #if TARGET_OS_IOS
  105. NSString *messageIdentifier = @"fcm-cm-test1";
  106. NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-1000]; // past
  107. NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:1000]; // future
  108. NSString *startTimeString = [self.dateFormatter stringFromDate:startDate];
  109. NSString *endTimeString = [self.dateFormatter stringFromDate:endDate];
  110. NSDictionary *message = @{
  111. kFIRMessagingContextManagerLocalTimeStart : startTimeString,
  112. kFIRMessagingContextManagerLocalTimeEnd : endTimeString,
  113. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  114. @"id": messageIdentifier,
  115. @"hello" : @"world"
  116. };
  117. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  118. XCTAssertEqual(self.scheduledLocalNotifications.count, 1);
  119. #pragma clang diagnostic push
  120. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  121. UILocalNotification *notification = [self.scheduledLocalNotifications firstObject];
  122. #pragma clang diagnostic pop
  123. // schedule notification after start date
  124. XCTAssertEqual([notification.fireDate compare:startDate], NSOrderedDescending);
  125. // schedule notification after end date
  126. XCTAssertEqual([notification.fireDate compare:endDate], NSOrderedAscending);
  127. #endif
  128. }
  129. /**
  130. * Test correctly parsing user data in local notifications.
  131. */
  132. - (void)testTimedNotificationsUserInfo {
  133. #if TARGET_OS_IOS
  134. NSString *messageIdentifierKey = @"message.id";
  135. NSString *messageIdentifier = @"fcm-cm-test1";
  136. NSString *startTimeString = @"2020-01-12 12:00:00"; // way into the future
  137. NSString *customDataKey = @"hello";
  138. NSString *customData = @"world";
  139. NSDictionary *message = @{
  140. kFIRMessagingContextManagerLocalTimeStart : startTimeString,
  141. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  142. messageIdentifierKey : messageIdentifier,
  143. customDataKey : customData,
  144. };
  145. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  146. XCTAssertEqual(self.scheduledLocalNotifications.count, 1);
  147. #pragma clang diagnostic push
  148. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  149. UILocalNotification *notification = [self.scheduledLocalNotifications firstObject];
  150. #pragma clang diagnostic pop
  151. XCTAssertEqualObjects(notification.userInfo[messageIdentifierKey], messageIdentifier);
  152. XCTAssertEqualObjects(notification.userInfo[customDataKey], customData);
  153. #endif
  154. }
  155. #pragma mark - Private Helpers
  156. - (void)mockSchedulingLocalNotifications {
  157. #if TARGET_OS_IOS
  158. id mockApplication = OCMPartialMock([UIApplication sharedApplication]);
  159. #pragma clang diagnostic push
  160. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  161. __block UILocalNotification *notificationToSchedule;
  162. [[[mockApplication stub]
  163. andDo:^(NSInvocation *invocation) {
  164. // Mock scheduling a notification
  165. if (notificationToSchedule) {
  166. [self.scheduledLocalNotifications addObject:notificationToSchedule];
  167. }
  168. }] scheduleLocalNotification:[OCMArg checkWithBlock:^BOOL(id obj) {
  169. if ([obj isKindOfClass:[UILocalNotification class]]) {
  170. notificationToSchedule = obj;
  171. return YES;
  172. }
  173. return NO;
  174. }]];
  175. #pragma clang diagnostic pop
  176. #endif
  177. }
  178. @end