FIRMessagingContextManagerServiceTest.m 6.5 KB

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