17
17
#import < XCTest/XCTest.h>
18
18
#import < OCMock/OCMock.h>
19
19
20
+ #import < FirebaseInstanceID/FirebaseInstanceID.h>
21
+
20
22
#import " FIRMessaging.h"
21
23
#import " FIRMessagingClient.h"
22
24
#import " FIRMessagingPubSub.h"
29
31
@" fE1e1PZJFSQ:APA91bFAOjp1ahBWn9rTlbjArwBEm_"
30
32
@" yUTTzK6dhIvLqzqqCSabaa4TQVM0pGTmF6r7tmMHPe6VYiGMHuCwJFgj5v97xl78sUNMLwuPPhoci8z_"
31
33
@" QGlCrTbxCFGzEUfvA3fGpGgIVQU2W6" ;
34
+ static NSString *const kFakeID = @" fE1e1PZJFSQ" ;
32
35
33
36
NSString *const kFIRMessagingTestsServiceSuiteName = @" com.messaging.test_serviceTest" ;
34
37
35
38
@interface FIRMessaging () <FIRMessagingClientDelegate>
36
39
@property (nonatomic , readwrite , strong ) FIRMessagingClient *client;
37
40
@property (nonatomic , readwrite , strong ) FIRMessagingPubSub *pubsub;
38
41
@property (nonatomic , readwrite , strong ) NSString *defaultFcmToken;
42
+ @property (nonatomic , readwrite , strong ) FIRInstanceID *instanceID;
39
43
40
44
@end
41
45
@@ -45,8 +49,15 @@ @interface FIRMessagingPubSub ()
45
49
46
50
@end
47
51
52
+ @interface FIRInstanceIDResult (ExposedForTest)
53
+ @property (nonatomic , readwrite , copy ) NSString *instanceID;
54
+ @property (nonatomic , readwrite , copy ) NSString *token;
55
+ @end
56
+
48
57
@interface FIRMessagingServiceTest : XCTestCase {
49
58
FIRMessaging *_messaging;
59
+ FIRInstanceIDResult *_result;
60
+ id _mockInstanceID;
50
61
id _mockPubSub;
51
62
}
52
63
@@ -55,18 +66,24 @@ @interface FIRMessagingServiceTest : XCTestCase {
55
66
@implementation FIRMessagingServiceTest
56
67
57
68
- (void )setUp {
69
+ [super setUp ];
58
70
NSUserDefaults *defaults = [[NSUserDefaults alloc ] initWithSuiteName: kFIRMessagingTestsServiceSuiteName ];
59
71
_messaging = [FIRMessagingTestUtilities messagingForTestsWithUserDefaults: defaults];
60
72
_messaging.defaultFcmToken = kFakeToken ;
61
73
_mockPubSub = OCMPartialMock (_messaging.pubsub );
62
74
[_mockPubSub setClient: nil ];
63
- [super setUp ];
75
+
76
+ _mockInstanceID = OCMPartialMock (_messaging.instanceID );
77
+ _result = [[FIRInstanceIDResult alloc ] init ];
78
+ _result.token = kFakeToken ;
79
+ _result.instanceID = kFakeID ;
64
80
}
65
81
66
82
- (void )tearDown {
83
+ [_mockInstanceID stopMocking ];
84
+ [_mockPubSub stopMocking ];
67
85
[_messaging.messagingUserDefaults removePersistentDomainForName: kFIRMessagingTestsServiceSuiteName ];
68
86
_messaging = nil ;
69
- [_mockPubSub stopMocking ];
70
87
[super tearDown ];
71
88
}
72
89
@@ -211,7 +228,8 @@ - (void)testUnsubscribeWithInvalidTopic {
211
228
}
212
229
213
230
- (void )testSubscribeWithNoTopicPrefix {
214
-
231
+ OCMStub ([_mockInstanceID
232
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
215
233
NSString *topicName = @" topicWithoutPrefix" ;
216
234
NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic: topicName];
217
235
OCMExpect (
@@ -221,13 +239,17 @@ - (void)testSubscribeWithNoTopicPrefix {
221
239
}
222
240
223
241
- (void )testSubscribeWithTopicPrefix {
242
+ OCMStub ([_mockInstanceID
243
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
224
244
NSString *topicName = @" /topics/topicWithoutPrefix" ;
225
245
OCMExpect ([_mockPubSub subscribeToTopic: [OCMArg isEqual: topicName] handler: [OCMArg any ]]);
226
246
[_messaging subscribeToTopic: topicName];
227
247
OCMVerifyAll (_mockPubSub);
228
248
}
229
249
230
250
- (void )testUnsubscribeWithNoTopicPrefix {
251
+ OCMStub ([_mockInstanceID
252
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
231
253
NSString *topicName = @" topicWithoutPrefix" ;
232
254
NSString *topicNameWithPrefix = [FIRMessagingPubSub addPrefixToTopic: topicName];
233
255
OCMExpect (
@@ -237,13 +259,17 @@ - (void)testUnsubscribeWithNoTopicPrefix {
237
259
}
238
260
239
261
- (void )testUnsubscribeWithTopicPrefix {
262
+ OCMStub ([_mockInstanceID
263
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
240
264
NSString *topicName = @" /topics/topicWithPrefix" ;
241
265
OCMExpect ([_mockPubSub unsubscribeFromTopic: [OCMArg isEqual: topicName] handler: [OCMArg any ]]);
242
266
[_messaging unsubscribeFromTopic: topicName];
243
267
OCMVerifyAll (_mockPubSub);
244
268
}
245
269
246
270
- (void )testSubscriptionCompletionHandlerWithSuccess {
271
+ OCMStub ([_mockInstanceID
272
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
247
273
OCMStub ([_mockPubSub subscribeToTopic: [OCMArg any ]
248
274
handler: ([OCMArg invokeBlockWithArgs: [NSNull null ], nil ])]);
249
275
XCTestExpectation *subscriptionCompletionExpectation =
@@ -259,6 +285,8 @@ - (void)testSubscriptionCompletionHandlerWithSuccess {
259
285
}
260
286
261
287
- (void )testUnsubscribeCompletionHandlerWithSuccess {
288
+ OCMStub ([_mockInstanceID
289
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
262
290
OCMStub ([_mockPubSub unsubscribeFromTopic: [OCMArg any ]
263
291
handler: ([OCMArg invokeBlockWithArgs: [NSNull null ], nil ])]);
264
292
XCTestExpectation *unsubscriptionCompletionExpectation =
@@ -274,6 +302,8 @@ - (void)testUnsubscribeCompletionHandlerWithSuccess {
274
302
}
275
303
276
304
- (void )testSubscriptionCompletionHandlerWithInvalidTopicName {
305
+ OCMStub ([_mockInstanceID
306
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
277
307
XCTestExpectation *subscriptionCompletionExpectation =
278
308
[self expectationWithDescription: @" Subscription is complete" ];
279
309
[_messaging subscribeToTopic: @" !@#$%^&*()"
@@ -288,6 +318,8 @@ - (void)testSubscriptionCompletionHandlerWithInvalidTopicName {
288
318
}
289
319
290
320
- (void )testUnsubscribeCompletionHandlerWithInvalidTopicName {
321
+ OCMStub ([_mockInstanceID
322
+ instanceIDWithHandler: ([OCMArg invokeBlockWithArgs: _result, [NSNull null ], nil ])]);
291
323
XCTestExpectation *unsubscriptionCompletionExpectation =
292
324
[self expectationWithDescription: @" Unsubscription is complete" ];
293
325
[_messaging unsubscribeFromTopic: @" !@#$%^&*()"
@@ -336,4 +368,42 @@ - (void)testFIRMessagingSDKLocaleInFIRMessagingService {
336
368
}
337
369
}
338
370
371
+
372
+ - (void )testSubscribeFailedWithInvalidToken {
373
+ // Mock get token is failed with FIRMessagingErrorUnknown error.
374
+ XCTestExpectation *subscriptionCompletionExpectation =
375
+ [self expectationWithDescription: @" Subscription is complete" ];
376
+ OCMStub ([_mockInstanceID
377
+ instanceIDWithHandler:
378
+ ([OCMArg
379
+ invokeBlockWithArgs: [NSNull null ],
380
+ [NSError errorWithFCMErrorCode: kFIRMessagingErrorCodeUnknown ],
381
+ nil ])]);
382
+ [_messaging subscribeToTopic: @" Apple"
383
+ completion: ^(NSError *_Nullable error) {
384
+ XCTAssertNotNil (error);
385
+ XCTAssertEqual (error.code , kFIRMessagingErrorCodeUnknown );
386
+ [subscriptionCompletionExpectation fulfill ];
387
+ }];
388
+ [self waitForExpectationsWithTimeout: 0.2 handler: nil ];
389
+ }
390
+
391
+ - (void )testUnsubscribeFailedWithInvalidToken {
392
+ OCMStub ([_mockInstanceID
393
+ instanceIDWithHandler:
394
+ ([OCMArg
395
+ invokeBlockWithArgs: [NSNull null ],
396
+ [NSError errorWithFCMErrorCode: kFIRMessagingErrorCodeUnknown ],
397
+ nil ])]);
398
+ XCTestExpectation *unsubscriptionCompletionExpectation =
399
+ [self expectationWithDescription: @" Unsubscription is complete" ];
400
+
401
+ [_messaging unsubscribeFromTopic: @" news"
402
+ completion: ^(NSError *_Nullable error) {
403
+ XCTAssertNotNil (error);
404
+ XCTAssertEqual (error.code , kFIRMessagingErrorCodeUnknown );
405
+ [unsubscriptionCompletionExpectation fulfill ];
406
+ }];
407
+ [self waitForExpectationsWithTimeout: 0.2 handler: nil ];
408
+ }
339
409
@end
0 commit comments