Skip to content

Commit 7e77503

Browse files
committed
Fixing fidchangelistener behavior to get invoked for new fid creations as well
1 parent 13eb0f6 commit 7e77503

2 files changed

Lines changed: 71 additions & 7 deletions

File tree

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,11 @@ private void doNetworkCallIfNecessary(boolean forceRefresh) {
392392
// be registered with the server or the FID is registered but we need a fresh authtoken.
393393
// Registering will also result in a fresh authtoken. Do the appropriate step here.
394394
PersistedInstallationEntry updatedPrefs;
395+
boolean isNewFID = false;
395396
try {
396397
if (prefs.isErrored() || prefs.isUnregistered()) {
397398
updatedPrefs = registerFidWithServer(prefs);
399+
isNewFID = true;
398400
} else if (forceRefresh || utils.isAuthTokenExpired(prefs)) {
399401
updatedPrefs = fetchAuthTokenFromServer(prefs);
400402
} else {
@@ -409,8 +411,12 @@ private void doNetworkCallIfNecessary(boolean forceRefresh) {
409411
// Store the prefs to persist the result of the previous step.
410412
insertOrUpdatePrefs(updatedPrefs);
411413

412-
// Update FidListener if a fid has changed.
413-
updateFidListener(prefs, updatedPrefs);
414+
// Update FidListener if a fid is new or has changed.
415+
if (isNewFID
416+
|| !TextUtils.equals(
417+
prefs.getFirebaseInstallationId(), updatedPrefs.getFirebaseInstallationId())) {
418+
updateFidListener(updatedPrefs);
419+
}
414420

415421
prefs = updatedPrefs;
416422

@@ -431,11 +437,10 @@ private void doNetworkCallIfNecessary(boolean forceRefresh) {
431437
}
432438
}
433439

434-
private synchronized void updateFidListener(
435-
PersistedInstallationEntry prefs, PersistedInstallationEntry updatedPrefs) {
436-
if (fidListeners.size() != 0
437-
&& !TextUtils.equals(
438-
prefs.getFirebaseInstallationId(), updatedPrefs.getFirebaseInstallationId())) {
440+
private synchronized void updateFidListener(@NonNull PersistedInstallationEntry updatedPrefs) {
441+
if (!fidListeners.isEmpty()
442+
&& !TextUtils.isEmpty(updatedPrefs.getFirebaseInstallationId())
443+
&& updatedPrefs.isRegistered()) {
439444
// Update all the registered FidListener about fid changes.
440445
for (FidListener listener : fidListeners) {
441446
listener.onFidChanged(updatedPrefs.getFirebaseInstallationId());

firebase-installations/src/test/java/com/google/firebase/installations/FirebaseInstallationsTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,65 @@ public void testFidListener_fidChanged_successful() throws Exception {
500500
assertNull(fidListener2.getLatestFid());
501501
}
502502

503+
@Test
504+
public void testFidListener_fidRegistered_sameFid_successful() throws Exception {
505+
when(mockIidStore.readIid()).thenReturn(null);
506+
when(mockIidStore.readToken()).thenReturn(null);
507+
when(mockBackend.createFirebaseInstallation(
508+
anyString(), anyString(), anyString(), anyString(), any()))
509+
.thenReturn(TEST_INSTALLATION_RESPONSE);
510+
511+
FakeFidListener fidListener = new FakeFidListener();
512+
513+
// Register the FidListener
514+
firebaseInstallations.registerFidListener(fidListener);
515+
516+
// Do the actual getId() call under test.
517+
TestOnCompleteListener<String> onCompleteListener = new TestOnCompleteListener<>();
518+
Task<String> task = firebaseInstallations.getId();
519+
520+
task.addOnCompleteListener(backgroundExecutor, onCompleteListener);
521+
String fid = onCompleteListener.await();
522+
assertWithMessage("getId Task failed.").that(fid).isEqualTo(TEST_FID_1);
523+
524+
// Waiting for Task that registers FID on the FIS Servers
525+
backgroundExecutor.awaitTermination(500, TimeUnit.MILLISECONDS);
526+
PersistedInstallationEntry entry = persistedInstallation.readPersistedInstallationEntryValue();
527+
assertThat(entry.getFirebaseInstallationId()).isEqualTo(TEST_FID_1);
528+
529+
// Verify FidListener receives fid registration notification.
530+
assertThat(fidListener.getLatestFid()).isEqualTo(TEST_FID_1);
531+
}
532+
533+
@Test
534+
public void testFidListener_alreadyRegistered_noFidChange_notNotified() throws Exception {
535+
persistedInstallation.insertOrUpdatePersistedInstallationEntry(
536+
PersistedInstallationEntry.INSTANCE.withRegisteredFid(
537+
TEST_FID_1,
538+
TEST_REFRESH_TOKEN,
539+
utils.currentTimeInSecs(),
540+
TEST_AUTH_TOKEN,
541+
TEST_TOKEN_EXPIRATION_TIMESTAMP));
542+
543+
FakeFidListener fidListener = new FakeFidListener();
544+
545+
// Register the FidListener
546+
firebaseInstallations.registerFidListener(fidListener);
547+
548+
// Do the actual getId() call under test.
549+
TestOnCompleteListener<String> onCompleteListener = new TestOnCompleteListener<>();
550+
Task<String> task = firebaseInstallations.getId();
551+
task.addOnCompleteListener(backgroundExecutor, onCompleteListener);
552+
String fid = onCompleteListener.await();
553+
assertWithMessage("getId Task failed.").that(fid).isEqualTo(TEST_FID_1);
554+
555+
backgroundExecutor.awaitTermination(500, TimeUnit.MILLISECONDS);
556+
557+
// Verify FidListener does not receive any notification since the FID was already registered
558+
// and didn't change.
559+
assertNull(fidListener.getLatestFid());
560+
}
561+
503562
@Test
504563
public void testGetId_migrateIid_successful() throws Exception {
505564
when(mockIidStore.readIid()).thenReturn(TEST_INSTANCE_ID_1);

0 commit comments

Comments
 (0)