@@ -51,6 +51,7 @@ @interface FIRInstanceID (ExposedForTest)
51
51
@property (nonatomic , readwrite , strong ) FIRInstallations *installations;
52
52
@property (nonatomic , readwrite , copy ) NSString *fcmSenderID;
53
53
@property (nonatomic , readwrite , copy ) NSString *firebaseAppID;
54
+ @property (nonatomic , readwrite , copy ) NSString *defaultFCMToken;
54
55
55
56
- (NSInteger )retryIntervalToFetchDefaultToken ;
56
57
- (BOOL )isFCMAutoInitEnabled ;
@@ -651,7 +652,6 @@ - (void)testDefaultToken_callbackInvokedForUnchangedToken {
651
652
return obj != nil ;
652
653
}]];
653
654
654
- __block NSInteger notificationPostCount = 0 ;
655
655
__block NSString *notificationToken = nil ;
656
656
657
657
// Fetch token once to store token state
@@ -663,8 +663,6 @@ - (void)testDefaultToken_callbackInvokedForUnchangedToken {
663
663
usingBlock: ^(NSNotification *_Nonnull note) {
664
664
// Should have saved token to cache
665
665
cachedTokenInfo = sTokenInfo ;
666
-
667
- notificationPostCount++;
668
666
notificationToken = [[self .instanceID token ] copy ];
669
667
[defaultTokenExpectation fulfill ];
670
668
}];
@@ -721,7 +719,6 @@ - (void)testDefaultTokenFetch_returnValidToken {
721
719
return obj != nil ;
722
720
}]];
723
721
724
- __block int notificationPostCount = 0 ;
725
722
__block NSString *notificationToken = nil ;
726
723
727
724
NSString *notificationName = kFIRInstanceIDTokenRefreshNotification ;
@@ -733,7 +730,6 @@ - (void)testDefaultTokenFetch_returnValidToken {
733
730
// Should have saved token to cache
734
731
cachedTokenInfo = sTokenInfo ;
735
732
736
- notificationPostCount++;
737
733
notificationToken = [[self .instanceID token ] copy ];
738
734
[defaultTokenExpectation fulfill ];
739
735
}];
@@ -800,6 +796,7 @@ - (void)testDefaultTokenFetch_retryFetchToken {
800
796
__block NSString *notificationToken = nil ;
801
797
802
798
NSString *notificationName = kFIRInstanceIDTokenRefreshNotification ;
799
+
803
800
self.tokenRefreshNotificationObserver = [[NSNotificationCenter defaultCenter ]
804
801
addObserverForName: notificationName
805
802
object: nil
@@ -1328,6 +1325,109 @@ - (void)testInstanceIDDelete_keyChainError {
1328
1325
OCMVerifyAll (self.mockTokenManager );
1329
1326
}
1330
1327
1328
+ - (void )testRefreshDifferentTokenFromMessaging {
1329
+ _instanceID.defaultFCMToken = kToken ;
1330
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , kToken );
1331
+ NSString *newTokenFromMessaging = @" a_new_token_from_messaging" ;
1332
+ FIRInstanceIDTokenInfo *cachedTokenInfo =
1333
+ [[FIRInstanceIDTokenInfo alloc ] initWithAuthorizedEntity: kAuthorizedEntity
1334
+ scope: kFIRInstanceIDDefaultTokenScope
1335
+ token: kToken
1336
+ appVersion: @" "
1337
+ firebaseAppID: kGoogleAppID ];
1338
+ OCMStub ([self .mockTokenManager
1339
+ cachedTokenInfoWithAuthorizedEntity: kAuthorizedEntity
1340
+ scope: kFIRInstanceIDDefaultTokenScope ])
1341
+ .andReturn (cachedTokenInfo);
1342
+
1343
+ OCMExpect ([self .mockTokenManager saveDefaultToken: newTokenFromMessaging
1344
+ withOptions: [OCMArg any ]]);
1345
+ [[NSNotificationCenter defaultCenter ]
1346
+ postNotificationName: kFIRInstanceIDMessagingUpdateTokenNotification
1347
+ object: newTokenFromMessaging];
1348
+ OCMVerifyAll (self.mockTokenManager );
1349
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , newTokenFromMessaging);
1350
+ }
1351
+
1352
+ - (void )testRefreshTheSameTokenFromMessaging {
1353
+ _instanceID.defaultFCMToken = kToken ;
1354
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , kToken );
1355
+
1356
+ NSString *newTokenFromMessaging = kToken ;
1357
+ FIRInstanceIDTokenInfo *cachedTokenInfo =
1358
+ [[FIRInstanceIDTokenInfo alloc ] initWithAuthorizedEntity: kAuthorizedEntity
1359
+ scope: kFIRInstanceIDDefaultTokenScope
1360
+ token: kToken
1361
+ appVersion: @" "
1362
+ firebaseAppID: kGoogleAppID ];
1363
+ OCMStub ([self .mockTokenManager
1364
+ cachedTokenInfoWithAuthorizedEntity: kAuthorizedEntity
1365
+ scope: kFIRInstanceIDDefaultTokenScope ])
1366
+ .andReturn (cachedTokenInfo);
1367
+
1368
+ OCMReject ([self .mockTokenManager saveDefaultToken: newTokenFromMessaging
1369
+ withOptions: [OCMArg any ]]);
1370
+ [[NSNotificationCenter defaultCenter ]
1371
+ postNotificationName: kFIRInstanceIDMessagingUpdateTokenNotification
1372
+ object: newTokenFromMessaging];
1373
+ OCMVerifyAll (self.mockTokenManager );
1374
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , newTokenFromMessaging);
1375
+ }
1376
+
1377
+ - (void )testRefreshDifferentTokenInInstanceIDStorage {
1378
+ _instanceID.defaultFCMToken = kToken ;
1379
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , kToken );
1380
+ // New token from messaging is the same as local cache in InstanceID
1381
+ // But the token in InstanceID storage is different
1382
+ NSString *newTokenFromMessaging = kToken ;
1383
+
1384
+ FIRInstanceIDTokenInfo *cachedTokenInfo =
1385
+ [[FIRInstanceIDTokenInfo alloc ] initWithAuthorizedEntity: kAuthorizedEntity
1386
+ scope: kFIRInstanceIDDefaultTokenScope
1387
+ token: @" a_outdated_token_in_storage"
1388
+ appVersion: @" "
1389
+ firebaseAppID: kGoogleAppID ];
1390
+ OCMStub ([self .mockTokenManager
1391
+ cachedTokenInfoWithAuthorizedEntity: kAuthorizedEntity
1392
+ scope: kFIRInstanceIDDefaultTokenScope ])
1393
+ .andReturn (cachedTokenInfo);
1394
+
1395
+ OCMExpect ([self .mockTokenManager saveDefaultToken: newTokenFromMessaging
1396
+ withOptions: [OCMArg any ]]);
1397
+ [[NSNotificationCenter defaultCenter ]
1398
+ postNotificationName: kFIRInstanceIDMessagingUpdateTokenNotification
1399
+ object: newTokenFromMessaging];
1400
+ OCMVerifyAll (self.mockTokenManager );
1401
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , newTokenFromMessaging);
1402
+ }
1403
+
1404
+ - (void )testRefreshNullTokenFromMessaging {
1405
+ _instanceID.defaultFCMToken = kToken ;
1406
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , kToken );
1407
+ // New token from messaging is the same as local cache in InstanceID
1408
+ // But the token in InstanceID storage is different
1409
+ NSString *newTokenFromMessaging = nil ;
1410
+
1411
+ FIRInstanceIDTokenInfo *cachedTokenInfo =
1412
+ [[FIRInstanceIDTokenInfo alloc ] initWithAuthorizedEntity: kAuthorizedEntity
1413
+ scope: kFIRInstanceIDDefaultTokenScope
1414
+ token: kToken
1415
+ appVersion: @" "
1416
+ firebaseAppID: kGoogleAppID ];
1417
+ OCMStub ([self .mockTokenManager
1418
+ cachedTokenInfoWithAuthorizedEntity: kAuthorizedEntity
1419
+ scope: kFIRInstanceIDDefaultTokenScope ])
1420
+ .andReturn (cachedTokenInfo);
1421
+
1422
+ OCMExpect ([self .mockTokenManager saveDefaultToken: newTokenFromMessaging
1423
+ withOptions: [OCMArg any ]]);
1424
+ [[NSNotificationCenter defaultCenter ]
1425
+ postNotificationName: kFIRInstanceIDMessagingUpdateTokenNotification
1426
+ object: newTokenFromMessaging];
1427
+ OCMVerifyAll (self.mockTokenManager );
1428
+ XCTAssertEqualObjects (_instanceID.defaultFCMToken , newTokenFromMessaging);
1429
+ }
1430
+
1331
1431
#pragma mark - Private Helpers
1332
1432
1333
1433
- (void )stubInstallationsToReturnValidID {
0 commit comments