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