FIRMessagingContextManagerServiceTest.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "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. UILocalNotification *notification = [self.scheduledLocalNotifications firstObject];
  73. NSDate *date = [self.dateFormatter dateFromString:startTimeString];
  74. XCTAssertEqual([notification.fireDate compare:date], NSOrderedSame);
  75. #endif
  76. }
  77. /**
  78. * Context Manager message with past end date should not be scheduled.
  79. */
  80. - (void)testMessageWithPastEndTime {
  81. #if TARGET_OS_IOS
  82. NSString *messageIdentifier = @"fcm-cm-test1";
  83. NSString *startTimeString = @"2010-01-12 12:00:00"; // way into the past
  84. NSString *endTimeString = @"2011-01-12 12:00:00"; // way into the past
  85. NSDictionary *message = @{
  86. kFIRMessagingContextManagerLocalTimeStart: startTimeString,
  87. kFIRMessagingContextManagerLocalTimeEnd : endTimeString,
  88. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  89. @"id": messageIdentifier,
  90. @"hello" : @"world"
  91. };
  92. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  93. XCTAssertEqual(self.scheduledLocalNotifications.count, 0);
  94. #endif
  95. }
  96. /**
  97. * Context Manager message with past start and future end date should be successfully
  98. * scheduled.
  99. */
  100. - (void)testMessageWithPastStartAndFutureEndTime {
  101. #if TARGET_OS_IOS
  102. NSString *messageIdentifier = @"fcm-cm-test1";
  103. NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow:-1000]; // past
  104. NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:1000]; // future
  105. NSString *startTimeString = [self.dateFormatter stringFromDate:startDate];
  106. NSString *endTimeString = [self.dateFormatter stringFromDate:endDate];
  107. NSDictionary *message = @{
  108. kFIRMessagingContextManagerLocalTimeStart : startTimeString,
  109. kFIRMessagingContextManagerLocalTimeEnd : endTimeString,
  110. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  111. @"id": messageIdentifier,
  112. @"hello" : @"world"
  113. };
  114. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  115. XCTAssertEqual(self.scheduledLocalNotifications.count, 1);
  116. UILocalNotification *notification = [self.scheduledLocalNotifications firstObject];
  117. // schedule notification after start date
  118. XCTAssertEqual([notification.fireDate compare:startDate], NSOrderedDescending);
  119. // schedule notification after end date
  120. XCTAssertEqual([notification.fireDate compare:endDate], NSOrderedAscending);
  121. #endif
  122. }
  123. /**
  124. * Test correctly parsing user data in local notifications.
  125. */
  126. - (void)testTimedNotificationsUserInfo {
  127. #if TARGET_OS_IOS
  128. NSString *messageIdentifierKey = @"message.id";
  129. NSString *messageIdentifier = @"fcm-cm-test1";
  130. NSString *startTimeString = @"2020-01-12 12:00:00"; // way into the future
  131. NSString *customDataKey = @"hello";
  132. NSString *customData = @"world";
  133. NSDictionary *message = @{
  134. kFIRMessagingContextManagerLocalTimeStart : startTimeString,
  135. kFIRMessagingContextManagerBodyKey : @"Hello world!",
  136. messageIdentifierKey : messageIdentifier,
  137. customDataKey : customData,
  138. };
  139. XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
  140. XCTAssertEqual(self.scheduledLocalNotifications.count, 1);
  141. UILocalNotification *notification = [self.scheduledLocalNotifications firstObject];
  142. XCTAssertEqualObjects(notification.userInfo[messageIdentifierKey], messageIdentifier);
  143. XCTAssertEqualObjects(notification.userInfo[customDataKey], customData);
  144. #endif
  145. }
  146. #pragma mark - Private Helpers
  147. - (void)mockSchedulingLocalNotifications {
  148. #if TARGET_OS_IOS
  149. id mockApplication = OCMPartialMock([UIApplication sharedApplication]);
  150. __block UILocalNotification *notificationToSchedule;
  151. [[[mockApplication stub]
  152. andDo:^(NSInvocation *invocation) {
  153. // Mock scheduling a notification
  154. if (notificationToSchedule) {
  155. [self.scheduledLocalNotifications addObject:notificationToSchedule];
  156. }
  157. }] scheduleLocalNotification:[OCMArg checkWithBlock:^BOOL(id obj) {
  158. if ([obj isKindOfClass:[UILocalNotification class]]) {
  159. notificationToSchedule = obj;
  160. return YES;
  161. }
  162. return NO;
  163. }]];
  164. #endif
  165. }
  166. @end