FIRMessagingContextManagerServiceTest.m 6.6 KB

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