13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
16
+ #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 || \
17
+ __MAC_OS_X_VERSION_MAX_ALLOWED >= __MAC_10_14 || __TV_OS_VERSION_MAX_ALLOWED >= __TV_10_0 || \
18
+ __WATCH_OS_VERSION_MAX_ALLOWED >= __WATCHOS_3_0 || TARGET_OS_MACCATALYST
19
+ #import < UserNotifications/UserNotifications.h>
20
+ #endif
17
21
#import < OCMock/OCMock.h>
18
22
#import < XCTest/XCTest.h>
19
23
20
24
#import " FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
21
25
26
+ static NSString *const kBody = @" Save 20% o ff!" ;
27
+ static NSString *const kUserInfoKey1 = @" level" ;
28
+ static NSString *const kUserInfoKey2 = @" isPayUser" ;
29
+ static NSString *const kUserInfoValue1 = @" 5" ;
30
+ static NSString *const kUserInfoValue2 = @" Yes" ;
31
+ static NSString *const kMessageIdentifierKey = @" gcm.message_id" ;
32
+ static NSString *const kMessageIdentifierValue = @" 1584748495200141" ;
33
+
22
34
@interface FIRMessagingContextManagerServiceTest : XCTestCase
23
35
24
36
@property (nonatomic , readwrite , strong ) NSDateFormatter *dateFormatter;
25
37
@property (nonatomic , readwrite , strong ) NSMutableArray *scheduledLocalNotifications;
38
+ @property (nonatomic , readwrite , strong )
39
+ NSMutableArray <UNNotificationRequest *> *requests API_AVAILABLE (ios(10.0 ));
26
40
27
41
@end
28
42
@@ -33,7 +47,11 @@ - (void)setUp {
33
47
self.dateFormatter = [[NSDateFormatter alloc ] init ];
34
48
self.dateFormatter .locale = [NSLocale localeWithLocaleIdentifier: @" en_US_POSIX" ];
35
49
[self .dateFormatter setDateFormat: @" yyyy-MM-dd HH:mm:ss" ];
36
- self.scheduledLocalNotifications = [NSMutableArray array ];
50
+ self.scheduledLocalNotifications = [[NSMutableArray alloc ] init ];
51
+ if (@available (macOS 10.14 , iOS 10.0 , watchOS 3.0 , tvOS 10.0 , *)) {
52
+ self.requests = [[NSMutableArray alloc ] init ];
53
+ }
54
+
37
55
[self mockSchedulingLocalNotifications ];
38
56
}
39
57
@@ -62,34 +80,44 @@ - (void)testValidContextManagerMessage {
62
80
XCTAssertTrue ([FIRMessagingContextManagerService isContextManagerMessage: message]);
63
81
}
64
82
65
- // TODO: Enable these tests. They fail because we cannot schedule local
66
- // notifications on OSX without permission. It's better to mock AppDelegate's
67
- // scheduleLocalNotification to mock scheduling behavior.
68
-
69
83
/* *
70
84
* Context Manager message with future start date should be successfully scheduled.
71
85
*/
72
86
- (void )testMessageWithFutureStartTime {
73
- #if TARGET_OS_IOS
74
- NSString *messageIdentifier = @" fcm-cm-test1" ;
75
87
// way into the future
76
88
NSString *startTimeString = [self .dateFormatter stringFromDate: [NSDate distantFuture ]];
77
89
NSDictionary *message = @{
78
90
kFIRMessagingContextManagerLocalTimeStart : startTimeString,
79
- kFIRMessagingContextManagerBodyKey : @" Hello world!" ,
80
- @" id" : messageIdentifier,
81
- @" hello" : @" world"
91
+ kFIRMessagingContextManagerBodyKey : kBody ,
92
+ kMessageIdentifierKey : kMessageIdentifierValue ,
93
+ kUserInfoKey1 : kUserInfoValue1 ,
94
+ kUserInfoKey2 : kUserInfoValue2
82
95
};
83
-
84
96
XCTAssertTrue ([FIRMessagingContextManagerService handleContextManagerMessage: message]);
85
97
98
+ if (@available (macOS 10.14 , iOS 10.0 , watchOS 3.0 , tvOS 10.0 , *)) {
99
+ XCTAssertEqual (self.requests .count , 1 );
100
+ UNNotificationRequest *request = self.requests .firstObject ;
101
+ XCTAssertEqualObjects (request.identifier , kMessageIdentifierValue );
102
+ #if TARGET_OS_IOS || TARGET_OS_WATCH || TARGET_OS_OSX
103
+ XCTAssertEqualObjects (request.content .body , kBody );
104
+ XCTAssertEqualObjects (request.content .userInfo [kUserInfoKey1 ], kUserInfoValue1 );
105
+ XCTAssertEqualObjects (request.content .userInfo [kUserInfoKey2 ], kUserInfoValue2 );
106
+ #endif
107
+ return ;
108
+ }
109
+
110
+ #if TARGET_OS_IOS
86
111
XCTAssertEqual (self.scheduledLocalNotifications .count , 1 );
87
112
#pragma clang diagnostic push
88
113
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
89
- UILocalNotification *notification = [ self .scheduledLocalNotifications firstObject ] ;
114
+ UILocalNotification *notification = self.scheduledLocalNotifications . firstObject ;
90
115
#pragma clang diagnostic pop
91
116
NSDate *date = [self .dateFormatter dateFromString: startTimeString];
92
117
XCTAssertEqual ([notification.fireDate compare: date], NSOrderedSame);
118
+ XCTAssertEqualObjects (notification.alertBody , kBody );
119
+ XCTAssertEqualObjects (notification.userInfo [kUserInfoKey1 ], kUserInfoValue1 );
120
+ XCTAssertEqualObjects (notification.userInfo [kUserInfoKey2 ], kUserInfoValue2 );
93
121
#endif
94
122
}
95
123
@@ -98,18 +126,21 @@ - (void)testMessageWithFutureStartTime {
98
126
*/
99
127
- (void )testMessageWithPastEndTime {
100
128
#if TARGET_OS_IOS
101
- NSString *messageIdentifier = @" fcm-cm-test1" ;
102
129
NSString *startTimeString = @" 2010-01-12 12:00:00" ; // way into the past
103
130
NSString *endTimeString = @" 2011-01-12 12:00:00" ; // way into the past
104
131
NSDictionary *message = @{
105
132
kFIRMessagingContextManagerLocalTimeStart : startTimeString,
106
133
kFIRMessagingContextManagerLocalTimeEnd : endTimeString,
107
- kFIRMessagingContextManagerBodyKey : @" Hello world! " ,
108
- @" id " : messageIdentifier ,
134
+ kFIRMessagingContextManagerBodyKey : kBody ,
135
+ kMessageIdentifierKey : kMessageIdentifierValue ,
109
136
@" hello" : @" world"
110
137
};
111
138
112
139
XCTAssertTrue ([FIRMessagingContextManagerService handleContextManagerMessage: message]);
140
+ if (@available (macOS 10.14 , iOS 10.0 , watchOS 3.0 , tvOS 10.0 , *)) {
141
+ XCTAssertEqual (self.requests .count , 0 );
142
+ return ;
143
+ }
113
144
XCTAssertEqual (self.scheduledLocalNotifications .count , 0 );
114
145
#endif
115
146
}
@@ -120,7 +151,6 @@ - (void)testMessageWithPastEndTime {
120
151
*/
121
152
- (void )testMessageWithPastStartAndFutureEndTime {
122
153
#if TARGET_OS_IOS
123
- NSString *messageIdentifier = @" fcm-cm-test1" ;
124
154
NSDate *startDate = [NSDate dateWithTimeIntervalSinceNow: -1000 ]; // past
125
155
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow: 1000 ]; // future
126
156
NSString *startTimeString = [self .dateFormatter stringFromDate: startDate];
@@ -129,13 +159,23 @@ - (void)testMessageWithPastStartAndFutureEndTime {
129
159
NSDictionary *message = @{
130
160
kFIRMessagingContextManagerLocalTimeStart : startTimeString,
131
161
kFIRMessagingContextManagerLocalTimeEnd : endTimeString,
132
- kFIRMessagingContextManagerBodyKey : @" Hello world!" ,
133
- @" id" : messageIdentifier,
134
- @" hello" : @" world"
162
+ kFIRMessagingContextManagerBodyKey : kBody ,
163
+ kMessageIdentifierKey : kMessageIdentifierValue ,
164
+ kUserInfoKey1 : kUserInfoValue1 ,
165
+ kUserInfoKey2 : kUserInfoValue2
135
166
};
136
167
137
168
XCTAssertTrue ([FIRMessagingContextManagerService handleContextManagerMessage: message]);
138
169
170
+ if (@available (macOS 10.14 , iOS 10.0 , watchOS 3.0 , tvOS 10.0 , *)) {
171
+ XCTAssertEqual (self.requests .count , 1 );
172
+ UNNotificationRequest *request = self.requests .firstObject ;
173
+ XCTAssertEqualObjects (request.identifier , kMessageIdentifierValue );
174
+ XCTAssertEqualObjects (request.content .body , kBody );
175
+ XCTAssertEqualObjects (request.content .userInfo [kUserInfoKey1 ], kUserInfoValue1 );
176
+ XCTAssertEqualObjects (request.content .userInfo [kUserInfoKey2 ], kUserInfoValue2 );
177
+ return ;
178
+ }
139
179
XCTAssertEqual (self.scheduledLocalNotifications .count , 1 );
140
180
#pragma clang diagnostic push
141
181
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
@@ -145,6 +185,8 @@ - (void)testMessageWithPastStartAndFutureEndTime {
145
185
XCTAssertEqual ([notification.fireDate compare: startDate], NSOrderedDescending);
146
186
// schedule notification after end date
147
187
XCTAssertEqual ([notification.fireDate compare: endDate], NSOrderedAscending);
188
+ XCTAssertEqualObjects (notification.userInfo [kUserInfoKey1 ], kUserInfoValue1 );
189
+ XCTAssertEqualObjects (notification.userInfo [kUserInfoKey2 ], kUserInfoValue2 );
148
190
#endif
149
191
}
150
192
@@ -153,35 +195,58 @@ - (void)testMessageWithPastStartAndFutureEndTime {
153
195
*/
154
196
- (void )testTimedNotificationsUserInfo {
155
197
#if TARGET_OS_IOS
156
- NSString *messageIdentifierKey = @" message.id" ;
157
- NSString *messageIdentifier = @" fcm-cm-test1" ;
158
198
// way into the future
159
199
NSString *startTimeString = [self .dateFormatter stringFromDate: [NSDate distantFuture ]];
160
200
161
- NSString *customDataKey = @" hello" ;
162
- NSString *customData = @" world" ;
163
201
NSDictionary *message = @{
164
202
kFIRMessagingContextManagerLocalTimeStart : startTimeString,
165
- kFIRMessagingContextManagerBodyKey : @" Hello world!" ,
166
- messageIdentifierKey : messageIdentifier,
167
- customDataKey : customData,
203
+ kFIRMessagingContextManagerBodyKey : kBody ,
204
+ kMessageIdentifierKey : kMessageIdentifierValue ,
205
+ kUserInfoKey1 : kUserInfoValue1 ,
206
+ kUserInfoKey2 : kUserInfoValue2
168
207
};
169
208
170
209
XCTAssertTrue ([FIRMessagingContextManagerService handleContextManagerMessage: message]);
171
-
210
+ if (@available (macOS 10.14 , iOS 10.0 , watchOS 3.0 , tvOS 10.0 , *)) {
211
+ XCTAssertEqual (self.requests .count , 1 );
212
+ UNNotificationRequest *request = self.requests .firstObject ;
213
+ XCTAssertEqualObjects (request.identifier , kMessageIdentifierValue );
214
+ XCTAssertEqualObjects (request.content .body , kBody );
215
+ XCTAssertEqualObjects (request.content .userInfo [kUserInfoKey1 ], kUserInfoValue1 );
216
+ XCTAssertEqualObjects (request.content .userInfo [kUserInfoKey2 ], kUserInfoValue2 );
217
+ return ;
218
+ }
172
219
XCTAssertEqual (self.scheduledLocalNotifications .count , 1 );
173
220
#pragma clang diagnostic push
174
221
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
175
222
UILocalNotification *notification = [self .scheduledLocalNotifications firstObject ];
176
223
#pragma clang diagnostic pop
177
- XCTAssertEqualObjects (notification.userInfo [messageIdentifierKey ], messageIdentifier );
178
- XCTAssertEqualObjects (notification.userInfo [customDataKey ], customData );
224
+ XCTAssertEqualObjects (notification.userInfo [kUserInfoKey1 ], kUserInfoValue1 );
225
+ XCTAssertEqualObjects (notification.userInfo [kUserInfoKey2 ], kUserInfoValue2 );
179
226
#endif
180
227
}
181
228
182
229
#pragma mark - Private Helpers
183
230
184
231
- (void )mockSchedulingLocalNotifications {
232
+ if (@available (macOS 10.14 , iOS 10.0 , watchOS 3.0 , tvOS 10.0 , *)) {
233
+ id mockNotificationCenter =
234
+ OCMPartialMock ([UNUserNotificationCenter currentNotificationCenter ]);
235
+ __block UNNotificationRequest *request;
236
+ [[[mockNotificationCenter stub ] andDo: ^(NSInvocation *invocation) {
237
+ [self .requests addObject: request];
238
+ }] addNotificationRequest: [OCMArg checkWithBlock: ^BOOL (id obj) {
239
+ if ([obj isKindOfClass: [UNNotificationRequest class ]]) {
240
+ request = obj;
241
+ [self .requests addObject: request];
242
+ return YES ;
243
+ }
244
+ return NO ;
245
+ }]
246
+ withCompletionHandler: ^(NSError *_Nullable error){
247
+ }];
248
+ return ;
249
+ }
185
250
#if TARGET_OS_IOS
186
251
id mockApplication = OCMPartialMock ([UIApplication sharedApplication ]);
187
252
#pragma clang diagnostic push
0 commit comments