28
28
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_CREATION_TIMESTAMP_2 ;
29
29
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_FID_1 ;
30
30
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_INSTALLATION_RESPONSE ;
31
+ import static com .google .firebase .installations .FisAndroidTestConstants .TEST_INSTALLATION_RESPONSE_WITH_IID ;
31
32
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_INSTALLATION_TOKEN_RESULT ;
33
+ import static com .google .firebase .installations .FisAndroidTestConstants .TEST_INSTANCE_ID_1 ;
32
34
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_PROJECT_ID ;
33
35
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_REFRESH_TOKEN ;
34
36
import static com .google .firebase .installations .FisAndroidTestConstants .TEST_TOKEN_EXPIRATION_TIMESTAMP ;
52
54
import com .google .firebase .FirebaseApp ;
53
55
import com .google .firebase .FirebaseException ;
54
56
import com .google .firebase .FirebaseOptions ;
57
+ import com .google .firebase .installations .local .IidStore ;
55
58
import com .google .firebase .installations .local .PersistedInstallation ;
56
59
import com .google .firebase .installations .local .PersistedInstallation .RegistrationStatus ;
57
60
import com .google .firebase .installations .local .PersistedInstallationEntry ;
@@ -88,6 +91,7 @@ public class FirebaseInstallationsInstrumentedTest {
88
91
@ Mock private Utils mockUtils ;
89
92
@ Mock private PersistedInstallation mockPersistedInstallation ;
90
93
@ Mock private FirebaseInstallationServiceClient mockClient ;
94
+ @ Mock private IidStore mockIidStore ;
91
95
92
96
private static final PersistedInstallationEntry REGISTERED_INSTALLATION_ENTRY =
93
97
PersistedInstallationEntry .builder ()
@@ -99,6 +103,16 @@ public class FirebaseInstallationsInstrumentedTest {
99
103
.setRegistrationStatus (PersistedInstallation .RegistrationStatus .REGISTERED )
100
104
.build ();
101
105
106
+ private static final PersistedInstallationEntry REGISTERED_IID_ENTRY =
107
+ PersistedInstallationEntry .builder ()
108
+ .setFirebaseInstallationId (TEST_INSTANCE_ID_1 )
109
+ .setAuthToken (TEST_AUTH_TOKEN )
110
+ .setRefreshToken (TEST_REFRESH_TOKEN )
111
+ .setTokenCreationEpochInSecs (TEST_CREATION_TIMESTAMP_2 )
112
+ .setExpiresInSecs (TEST_TOKEN_EXPIRATION_TIMESTAMP )
113
+ .setRegistrationStatus (PersistedInstallation .RegistrationStatus .REGISTERED )
114
+ .build ();
115
+
102
116
private static final PersistedInstallationEntry EXPIRED_AUTH_TOKEN_ENTRY =
103
117
PersistedInstallationEntry .builder ()
104
118
.setFirebaseInstallationId (TEST_FID_1 )
@@ -189,12 +203,20 @@ public void cleanUp() throws Exception {
189
203
persistedInstallation .clear ();
190
204
}
191
205
206
+ private FirebaseInstallations getFirebaseInstallations () {
207
+ return new FirebaseInstallations (
208
+ executor ,
209
+ firebaseApp ,
210
+ backendClientReturnsOk ,
211
+ persistedInstallation ,
212
+ mockUtils ,
213
+ mockIidStore );
214
+ }
215
+
192
216
@ Test
193
217
public void testGetId_PersistedInstallationOk_BackendOk () throws Exception {
194
- when (mockUtils .isAuthTokenExpired (REGISTERED_INSTALLATION_ENTRY )).thenReturn (false );
195
- FirebaseInstallations firebaseInstallations =
196
- new FirebaseInstallations (
197
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
218
+ when (mockUtils .isAuthTokenExpired (REGISTERED_IID_ENTRY )).thenReturn (false );
219
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
198
220
199
221
// No exception, means success.
200
222
assertWithMessage ("getId Task failed." )
@@ -204,7 +226,8 @@ public void testGetId_PersistedInstallationOk_BackendOk() throws Exception {
204
226
persistedInstallation .readPersistedInstallationEntryValue ();
205
227
assertThat (entryValue ).hasFid (TEST_FID_1 );
206
228
207
- // Waiting for Task that registers FID on the FIS Servers
229
+ // getId() returns fid immediately but registers fid asynchronously. Waiting for half a second
230
+ // while we mock fid registration. We dont send an actual request to FIS in tests.
208
231
executor .awaitTermination (500 , TimeUnit .MILLISECONDS );
209
232
210
233
PersistedInstallationEntry updatedInstallationEntry =
@@ -213,15 +236,39 @@ public void testGetId_PersistedInstallationOk_BackendOk() throws Exception {
213
236
assertThat (updatedInstallationEntry ).hasRegistrationStatus (RegistrationStatus .REGISTERED );
214
237
}
215
238
239
+ @ Test
240
+ public void testGetId_migrateIid_successful () throws Exception {
241
+ when (mockIidStore .readIid ()).thenReturn (TEST_INSTANCE_ID_1 );
242
+ when (mockUtils .isAuthTokenExpired (REGISTERED_INSTALLATION_ENTRY )).thenReturn (false );
243
+ when (backendClientReturnsOk .createFirebaseInstallation (
244
+ anyString (), anyString (), anyString (), anyString ()))
245
+ .thenReturn (TEST_INSTALLATION_RESPONSE_WITH_IID );
246
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
247
+
248
+ // No exception, means success.
249
+ assertWithMessage ("getId Task failed." )
250
+ .that (Tasks .await (firebaseInstallations .getId ()))
251
+ .isNotEmpty ();
252
+ PersistedInstallationEntry entryValue =
253
+ persistedInstallation .readPersistedInstallationEntryValue ();
254
+ assertThat (entryValue ).hasFid (TEST_INSTANCE_ID_1 );
255
+
256
+ // Waiting for Task that registers FID on the FIS Servers
257
+ executor .awaitTermination (500 , TimeUnit .MILLISECONDS );
258
+
259
+ PersistedInstallationEntry updatedInstallationEntry =
260
+ persistedInstallation .readPersistedInstallationEntryValue ();
261
+ assertThat (updatedInstallationEntry ).hasFid (TEST_INSTANCE_ID_1 );
262
+ assertThat (updatedInstallationEntry ).hasRegistrationStatus (RegistrationStatus .REGISTERED );
263
+ }
264
+
216
265
@ Test
217
266
public void testGetId_multipleCalls_sameFIDReturned () throws Exception {
218
267
when (mockUtils .isAuthTokenExpired (REGISTERED_INSTALLATION_ENTRY )).thenReturn (false );
219
268
when (backendClientReturnsOk .createFirebaseInstallation (
220
269
anyString (), anyString (), anyString (), anyString ()))
221
270
.thenReturn (TEST_INSTALLATION_RESPONSE );
222
- FirebaseInstallations firebaseInstallations =
223
- new FirebaseInstallations (
224
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
271
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
225
272
226
273
// Call getId multiple times
227
274
Task <String > task1 = firebaseInstallations .getId ();
@@ -249,9 +296,7 @@ public void testGetId_invalidFid_storesValidFidFromResponse() throws Exception {
249
296
// Update local storage with installation entry that has invalid fid.
250
297
persistedInstallation .insertOrUpdatePersistedInstallationEntry (INVALID_INSTALLATION_ENTRY );
251
298
when (mockUtils .isAuthTokenExpired (REGISTERED_INSTALLATION_ENTRY )).thenReturn (false );
252
- FirebaseInstallations firebaseInstallations =
253
- new FirebaseInstallations (
254
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
299
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
255
300
256
301
// No exception, means success.
257
302
assertWithMessage ("getId Task failed." )
@@ -275,7 +320,12 @@ public void testGetId_invalidFid_storesValidFidFromResponse() throws Exception {
275
320
public void testGetId_PersistedInstallationOk_BackendError () throws Exception {
276
321
FirebaseInstallations firebaseInstallations =
277
322
new FirebaseInstallations (
278
- executor , firebaseApp , backendClientReturnsError , persistedInstallation , mockUtils );
323
+ executor ,
324
+ firebaseApp ,
325
+ backendClientReturnsError ,
326
+ persistedInstallation ,
327
+ mockUtils ,
328
+ mockIidStore );
279
329
280
330
Tasks .await (firebaseInstallations .getId ());
281
331
@@ -299,9 +349,7 @@ public void testGetId_ServerError_UnregisteredFID() throws Exception {
299
349
anyString (), anyString (), anyString (), anyString ()))
300
350
.thenReturn (SERVER_ERROR_INSTALLATION_RESPONSE );
301
351
302
- FirebaseInstallations firebaseInstallations =
303
- new FirebaseInstallations (
304
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
352
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
305
353
306
354
Tasks .await (firebaseInstallations .getId ());
307
355
@@ -326,7 +374,8 @@ public void testGetId_PersistedInstallationError_BackendOk() throws InterruptedE
326
374
firebaseApp ,
327
375
backendClientReturnsOk ,
328
376
persistedInstallationReturnsError ,
329
- mockUtils );
377
+ mockUtils ,
378
+ mockIidStore );
330
379
331
380
// Expect exception
332
381
try {
@@ -355,7 +404,7 @@ public void testGetId_fidRegistrationUncheckedException_statusUpdated() throws E
355
404
356
405
FirebaseInstallations firebaseInstallations =
357
406
new FirebaseInstallations (
358
- executor , firebaseApp , mockClient , persistedInstallation , mockUtils );
407
+ executor , firebaseApp , mockClient , persistedInstallation , mockUtils , mockIidStore );
359
408
360
409
Tasks .await (firebaseInstallations .getId ());
361
410
@@ -387,7 +436,7 @@ public void testGetId_expiredAuthTokenUncheckedException_statusUpdated() throws
387
436
388
437
FirebaseInstallations firebaseInstallations =
389
438
new FirebaseInstallations (
390
- executor , firebaseApp , mockClient , persistedInstallation , mockUtils );
439
+ executor , firebaseApp , mockClient , persistedInstallation , mockUtils , mockIidStore );
391
440
392
441
assertWithMessage ("getId Task failed" )
393
442
.that (Tasks .await (firebaseInstallations .getId ()))
@@ -412,9 +461,7 @@ public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception {
412
461
persistedInstallation .insertOrUpdatePersistedInstallationEntry (EXPIRED_AUTH_TOKEN_ENTRY );
413
462
when (mockUtils .isAuthTokenExpired (EXPIRED_AUTH_TOKEN_ENTRY )).thenReturn (true );
414
463
415
- FirebaseInstallations firebaseInstallations =
416
- new FirebaseInstallations (
417
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
464
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
418
465
419
466
assertWithMessage ("getId Task failed" )
420
467
.that (Tasks .await (firebaseInstallations .getId ()))
@@ -439,9 +486,7 @@ public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception {
439
486
@ Test
440
487
public void testGetAuthToken_fidDoesNotExist_successful () throws Exception {
441
488
when (mockUtils .isAuthTokenExpired (REGISTERED_INSTALLATION_ENTRY )).thenReturn (false );
442
- FirebaseInstallations firebaseInstallations =
443
- new FirebaseInstallations (
444
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
489
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
445
490
446
491
Tasks .await (firebaseInstallations .getAuthToken (FirebaseInstallationsApi .DO_NOT_FORCE_REFRESH ));
447
492
@@ -459,7 +504,8 @@ public void testGetAuthToken_PersistedInstallationError_failure() throws Excepti
459
504
firebaseApp ,
460
505
backendClientReturnsOk ,
461
506
persistedInstallationReturnsError ,
462
- mockUtils );
507
+ mockUtils ,
508
+ mockIidStore );
463
509
464
510
// Expect exception
465
511
try {
@@ -485,7 +531,12 @@ public void testGetAuthToken_fidExists_successful() throws Exception {
485
531
486
532
FirebaseInstallations firebaseInstallations =
487
533
new FirebaseInstallations (
488
- executor , firebaseApp , backendClientReturnsOk , mockPersistedInstallation , mockUtils );
534
+ executor ,
535
+ firebaseApp ,
536
+ backendClientReturnsOk ,
537
+ mockPersistedInstallation ,
538
+ mockUtils ,
539
+ mockIidStore );
489
540
490
541
InstallationTokenResult installationTokenResult =
491
542
Tasks .await (
@@ -504,9 +555,7 @@ public void testGetAuthToken_expiredAuthToken_fetchedNewTokenFromFIS() throws Ex
504
555
when (mockUtils .isAuthTokenExpired (EXPIRED_AUTH_TOKEN_ENTRY )).thenReturn (true );
505
556
when (mockUtils .isAuthTokenExpired (UPDATED_AUTH_TOKEN_ENTRY )).thenReturn (false );
506
557
507
- FirebaseInstallations firebaseInstallations =
508
- new FirebaseInstallations (
509
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
558
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
510
559
511
560
InstallationTokenResult installationTokenResult =
512
561
Tasks .await (
@@ -526,9 +575,7 @@ public void testGetAuthToken_unregisteredFid_fetchedNewTokenFromFIS() throws Exc
526
575
persistedInstallation .insertOrUpdatePersistedInstallationEntry (UNREGISTERED_INSTALLATION_ENTRY );
527
576
when (mockUtils .isAuthTokenExpired (REGISTERED_INSTALLATION_ENTRY )).thenReturn (false );
528
577
529
- FirebaseInstallations firebaseInstallations =
530
- new FirebaseInstallations (
531
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
578
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
532
579
533
580
InstallationTokenResult installationTokenResult =
534
581
Tasks .await (
@@ -552,7 +599,12 @@ public void testGetAuthToken_serverError_failure() throws Exception {
552
599
553
600
FirebaseInstallations firebaseInstallations =
554
601
new FirebaseInstallations (
555
- executor , firebaseApp , backendClientReturnsError , mockPersistedInstallation , mockUtils );
602
+ executor ,
603
+ firebaseApp ,
604
+ backendClientReturnsError ,
605
+ mockPersistedInstallation ,
606
+ mockUtils ,
607
+ mockIidStore );
556
608
557
609
// Expect exception
558
610
try {
@@ -579,9 +631,7 @@ public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce(
579
631
when (mockUtils .isAuthTokenExpired (EXPIRED_AUTH_TOKEN_ENTRY )).thenReturn (true );
580
632
when (mockUtils .isAuthTokenExpired (UPDATED_AUTH_TOKEN_ENTRY )).thenReturn (false );
581
633
582
- FirebaseInstallations firebaseInstallations =
583
- new FirebaseInstallations (
584
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
634
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
585
635
586
636
// Call getAuthToken multiple times with DO_NOT_FORCE_REFRESH option
587
637
Task <InstallationTokenResult > task1 =
@@ -630,9 +680,7 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th
630
680
.generateAuthToken (anyString (), anyString (), anyString (), anyString ());
631
681
when (mockUtils .isAuthTokenExpired (any ())).thenReturn (false );
632
682
633
- FirebaseInstallations firebaseInstallations =
634
- new FirebaseInstallations (
635
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
683
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
636
684
637
685
// Call getAuthToken multiple times with FORCE_REFRESH option.
638
686
Task <InstallationTokenResult > task1 =
@@ -659,9 +707,7 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th
659
707
public void testDelete_registeredFID_successful () throws Exception {
660
708
// Update local storage with a registered installation entry
661
709
persistedInstallation .insertOrUpdatePersistedInstallationEntry (REGISTERED_INSTALLATION_ENTRY );
662
- FirebaseInstallations firebaseInstallations =
663
- new FirebaseInstallations (
664
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
710
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
665
711
666
712
Tasks .await (firebaseInstallations .delete ());
667
713
@@ -676,9 +722,7 @@ public void testDelete_registeredFID_successful() throws Exception {
676
722
public void testDelete_unregisteredFID_successful () throws Exception {
677
723
// Update local storage with a unregistered installation entry
678
724
persistedInstallation .insertOrUpdatePersistedInstallationEntry (UNREGISTERED_INSTALLATION_ENTRY );
679
- FirebaseInstallations firebaseInstallations =
680
- new FirebaseInstallations (
681
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
725
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
682
726
683
727
Tasks .await (firebaseInstallations .delete ());
684
728
@@ -691,9 +735,7 @@ public void testDelete_unregisteredFID_successful() throws Exception {
691
735
692
736
@ Test
693
737
public void testDelete_emptyPersistedFidEntry_successful () throws Exception {
694
- FirebaseInstallations firebaseInstallations =
695
- new FirebaseInstallations (
696
- executor , firebaseApp , backendClientReturnsOk , persistedInstallation , mockUtils );
738
+ FirebaseInstallations firebaseInstallations = getFirebaseInstallations ();
697
739
698
740
Tasks .await (firebaseInstallations .delete ());
699
741
@@ -710,7 +752,12 @@ public void testDelete_serverError_failure() throws Exception {
710
752
persistedInstallation .insertOrUpdatePersistedInstallationEntry (REGISTERED_INSTALLATION_ENTRY );
711
753
FirebaseInstallations firebaseInstallations =
712
754
new FirebaseInstallations (
713
- executor , firebaseApp , backendClientReturnsError , persistedInstallation , mockUtils );
755
+ executor ,
756
+ firebaseApp ,
757
+ backendClientReturnsError ,
758
+ persistedInstallation ,
759
+ mockUtils ,
760
+ mockIidStore );
714
761
715
762
// Expect exception
716
763
try {
0 commit comments