Skip to content

Commit 88cea33

Browse files
manny-jimenezManny Jimenez
andauthored
Fad removing update app functionality to firebaseAppDistribution class (#3167)
* Fad removing update app functionality to firebaseAppDistribution class * lint fixes * Moving updateApp tests to firebase app distribution * Responding to feedback and moving tests to their respective clients * Fixing rebase and changing updateApp to private Co-authored-by: Manny Jimenez <[email protected]>
1 parent d0048fd commit 88cea33

File tree

6 files changed

+120
-234
lines changed

6 files changed

+120
-234
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/FirebaseAppDistribution.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.firebase.app.distribution;
1616

1717
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.AUTHENTICATION_FAILURE;
18+
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.UPDATE_NOT_AVAILABLE;
1819
import static com.google.firebase.app.distribution.TaskUtils.safeSetTaskException;
1920
import static com.google.firebase.app.distribution.TaskUtils.safeSetTaskResult;
2021

@@ -41,8 +42,9 @@ public class FirebaseAppDistribution {
4142
private final FirebaseApp firebaseApp;
4243
private final TesterSignInClient testerSignInClient;
4344
private final CheckForNewReleaseClient checkForNewReleaseClient;
44-
private final UpdateAppClient updateAppClient;
4545
private final FirebaseAppDistributionLifecycleNotifier lifecycleNotifier;
46+
private final UpdateApkClient updateApkClient;
47+
private final UpdateAabClient updateAabClient;
4648
private final SignInStorage signInStorage;
4749

4850
private final Object updateIfNewReleaseTaskLock = new Object();
@@ -65,13 +67,15 @@ public class FirebaseAppDistribution {
6567
@NonNull FirebaseApp firebaseApp,
6668
@NonNull TesterSignInClient testerSignInClient,
6769
@NonNull CheckForNewReleaseClient checkForNewReleaseClient,
68-
@NonNull UpdateAppClient updateAppClient,
70+
@NonNull UpdateApkClient updateApkClient,
71+
@NonNull UpdateAabClient updateAabClient,
6972
@NonNull SignInStorage signInStorage,
7073
@NonNull FirebaseAppDistributionLifecycleNotifier lifecycleNotifier) {
7174
this.firebaseApp = firebaseApp;
7275
this.testerSignInClient = testerSignInClient;
7376
this.checkForNewReleaseClient = checkForNewReleaseClient;
74-
this.updateAppClient = updateAppClient;
77+
this.updateApkClient = updateApkClient;
78+
this.updateAabClient = updateAabClient;
7579
this.signInStorage = signInStorage;
7680
this.lifecycleNotifier = lifecycleNotifier;
7781
lifecycleNotifier.addOnActivityDestroyedListener(this::onActivityDestroyed);
@@ -88,7 +92,8 @@ public FirebaseAppDistribution(
8892
new TesterSignInClient(firebaseApp, firebaseInstallationsApi, signInStorage),
8993
new CheckForNewReleaseClient(
9094
firebaseApp, new FirebaseAppDistributionTesterApiClient(), firebaseInstallationsApi),
91-
new UpdateAppClient(firebaseApp),
95+
new UpdateApkClient(firebaseApp, new InstallApkClient()),
96+
new UpdateAabClient(),
9297
signInStorage,
9398
lifecycleNotifier);
9499
}
@@ -240,9 +245,26 @@ private UpdateTask updateApp(boolean showDownloadInNotificationManager) {
240245
Constants.ErrorMessages.AUTHENTICATION_ERROR, AUTHENTICATION_FAILURE));
241246
return updateTask;
242247
}
243-
244248
synchronized (cachedNewReleaseLock) {
245-
return this.updateAppClient.updateApp(cachedNewRelease, showDownloadInNotificationManager);
249+
if (cachedNewRelease == null) {
250+
LogWrapper.getInstance().v("New release not found.");
251+
return getErrorUpdateTask(
252+
new FirebaseAppDistributionException(
253+
Constants.ErrorMessages.NOT_FOUND_ERROR, UPDATE_NOT_AVAILABLE));
254+
}
255+
if (cachedNewRelease.getDownloadUrl() == null) {
256+
LogWrapper.getInstance().v("Download failed to execute");
257+
return getErrorUpdateTask(
258+
new FirebaseAppDistributionException(
259+
Constants.ErrorMessages.DOWNLOAD_URL_NOT_FOUND,
260+
FirebaseAppDistributionException.Status.DOWNLOAD_FAILURE));
261+
}
262+
263+
if (cachedNewRelease.getBinaryType() == BinaryType.AAB) {
264+
return this.updateAabClient.updateAab(cachedNewRelease);
265+
} else {
266+
return this.updateApkClient.updateApk(cachedNewRelease, showDownloadInNotificationManager);
267+
}
246268
}
247269
}
248270

@@ -383,4 +405,10 @@ private void dismissUpdateDialog() {
383405
updateDialogShown = false;
384406
}
385407
}
408+
409+
private UpdateTask getErrorUpdateTask(Exception e) {
410+
UpdateTaskImpl updateTask = new UpdateTaskImpl();
411+
updateTask.setException(e);
412+
return updateTask;
413+
}
386414
}

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/UpdateAppClient.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/FirebaseAppDistributionTest.java

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.AUTHENTICATION_FAILURE;
1919
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.INSTALLATION_CANCELED;
2020
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertFalse;
2122
import static org.junit.Assert.assertNotNull;
2223
import static org.junit.Assert.assertNull;
2324
import static org.junit.Assert.assertTrue;
25+
import static org.mockito.Mockito.doReturn;
2426
import static org.mockito.Mockito.never;
2527
import static org.mockito.Mockito.spy;
2628
import static org.mockito.Mockito.times;
@@ -48,10 +50,13 @@
4850
import com.google.firebase.installations.InstallationTokenResult;
4951
import java.util.ArrayList;
5052
import java.util.List;
53+
import java.util.concurrent.Executor;
54+
import java.util.concurrent.Executors;
5155
import org.junit.Before;
5256
import org.junit.Test;
5357
import org.junit.runner.RunWith;
5458
import org.mockito.Mock;
59+
import org.mockito.Mockito;
5560
import org.mockito.MockitoAnnotations;
5661
import org.robolectric.Robolectric;
5762
import org.robolectric.RobolectricTestRunner;
@@ -66,6 +71,7 @@ public class FirebaseAppDistributionTest {
6671
private static final String TEST_AUTH_TOKEN = "fad.auth.token";
6772
private static final String TEST_IAS_ARTIFACT_ID = "ias-artifact-id";
6873
private static final String IAS_ARTIFACT_ID_KEY = "com.android.vending.internal.apk.id";
74+
private static final String TEST_URL = "https://test-url";
6975
private static final long INSTALLED_VERSION_CODE = 2;
7076

7177
private static final AppDistributionReleaseInternal.Builder TEST_RELEASE_NEWER_AAB_INTERNAL =
@@ -74,7 +80,7 @@ public class FirebaseAppDistributionTest {
7480
.setDisplayVersion("3.0")
7581
.setReleaseNotes("Newer version.")
7682
.setBinaryType(BinaryType.AAB)
77-
.setDownloadUrl("https://test-url");
83+
.setDownloadUrl(TEST_URL);
7884

7985
private static final AppDistributionRelease TEST_RELEASE_NEWER_AAB =
8086
AppDistributionRelease.builder()
@@ -84,16 +90,28 @@ public class FirebaseAppDistributionTest {
8490
.setBinaryType(BinaryType.AAB)
8591
.build();
8692

93+
private static final AppDistributionReleaseInternal.Builder TEST_RELEASE_NEWER_APK_INTERNAL =
94+
AppDistributionReleaseInternal.builder()
95+
.setBuildVersion("3")
96+
.setDisplayVersion("3.0")
97+
.setReleaseNotes("Newer version.")
98+
.setBinaryType(BinaryType.APK)
99+
.setDownloadUrl(TEST_URL);
100+
87101
private FirebaseAppDistribution firebaseAppDistribution;
88102
private TestActivity activity;
89103

90104
@Mock private InstallationTokenResult mockInstallationTokenResult;
91105
@Mock private TesterSignInClient mockTesterSignInClient;
92106
@Mock private CheckForNewReleaseClient mockCheckForNewReleaseClient;
93-
@Mock private UpdateAppClient mockUpdateAppClient;
107+
@Mock private InstallApkClient mockInstallApkClient;
108+
private UpdateApkClient mockUpdateApkClient;
109+
@Mock private UpdateAabClient mockUpdateAabClient;
94110
@Mock private SignInStorage mockSignInStorage;
95111
@Mock private FirebaseAppDistributionLifecycleNotifier mockLifecycleNotifier;
96112

113+
Executor testExecutor = Executors.newSingleThreadExecutor();
114+
97115
static class TestActivity extends Activity {}
98116

99117
@Before
@@ -112,13 +130,17 @@ public void setup() {
112130
.setApiKey(TEST_API_KEY)
113131
.build());
114132

133+
this.mockUpdateApkClient =
134+
Mockito.spy(new UpdateApkClient(testExecutor, firebaseApp, mockInstallApkClient));
135+
115136
firebaseAppDistribution =
116137
spy(
117138
new FirebaseAppDistribution(
118139
firebaseApp,
119140
mockTesterSignInClient,
120141
mockCheckForNewReleaseClient,
121-
mockUpdateAppClient,
142+
mockUpdateApkClient,
143+
mockUpdateAabClient,
122144
mockSignInStorage,
123145
mockLifecycleNotifier));
124146

@@ -220,7 +242,7 @@ public void updateToNewRelease_whenNewAabReleaseAvailable_showsUpdateDialog() {
220242
AppDistributionReleaseInternal newRelease = TEST_RELEASE_NEWER_AAB_INTERNAL.build();
221243
when(mockCheckForNewReleaseClient.checkForNewRelease()).thenReturn(Tasks.forResult(newRelease));
222244
firebaseAppDistribution.setCachedNewRelease(newRelease);
223-
when(mockUpdateAppClient.updateApp(newRelease, true)).thenReturn(new UpdateTaskImpl());
245+
doReturn(new UpdateTaskImpl()).when(mockUpdateAabClient).updateAab(newRelease);
224246

225247
firebaseAppDistribution.updateIfNewReleaseAvailable();
226248

@@ -374,9 +396,8 @@ public void updateToNewRelease_receiveProgressUpdateFromUpdateApp() {
374396
AppDistributionReleaseInternal newRelease = TEST_RELEASE_NEWER_AAB_INTERNAL.build();
375397
when(mockCheckForNewReleaseClient.checkForNewRelease()).thenReturn(Tasks.forResult(newRelease));
376398
firebaseAppDistribution.setCachedNewRelease(newRelease);
377-
378399
UpdateTaskImpl mockTask = new UpdateTaskImpl();
379-
when(mockUpdateAppClient.updateApp(newRelease, true)).thenReturn(mockTask);
400+
when(mockUpdateAabClient.updateAab(newRelease)).thenReturn(mockTask);
380401
mockTask.updateProgress(
381402
UpdateProgress.builder()
382403
.setApkFileTotalBytes(1)
@@ -415,4 +436,45 @@ public void taskCancelledOnScreenRotation() {
415436
assertEquals("Update canceled", e.getMessage());
416437
assertEquals(INSTALLATION_CANCELED, e.getErrorCode());
417438
}
439+
440+
@Test
441+
public void updateAppTask_whenNoReleaseAvailable_throwsError() {
442+
firebaseAppDistribution.setCachedNewRelease(null);
443+
when(mockSignInStorage.getSignInStatus()).thenReturn(true);
444+
445+
UpdateTask updateTask = firebaseAppDistribution.updateApp();
446+
447+
assertFalse(updateTask.isSuccessful());
448+
assertTrue(updateTask.getException() instanceof FirebaseAppDistributionException);
449+
FirebaseAppDistributionException ex =
450+
(FirebaseAppDistributionException) updateTask.getException();
451+
assertEquals(FirebaseAppDistributionException.Status.UPDATE_NOT_AVAILABLE, ex.getErrorCode());
452+
assertEquals(Constants.ErrorMessages.NOT_FOUND_ERROR, ex.getMessage());
453+
}
454+
455+
@Test
456+
public void updateApp_withAabReleaseAvailable_returnsSameAabTask() {
457+
AppDistributionReleaseInternal release = TEST_RELEASE_NEWER_AAB_INTERNAL.build();
458+
firebaseAppDistribution.setCachedNewRelease(release);
459+
UpdateTaskImpl updateTaskToReturn = new UpdateTaskImpl();
460+
doReturn(updateTaskToReturn).when(mockUpdateAabClient).updateAab(release);
461+
when(mockSignInStorage.getSignInStatus()).thenReturn(true);
462+
463+
UpdateTask updateTask = firebaseAppDistribution.updateApp();
464+
465+
assertEquals(updateTask, updateTaskToReturn);
466+
}
467+
468+
@Test
469+
public void updateApp_withApkReleaseAvailable_returnsSameApkTask() {
470+
when(mockSignInStorage.getSignInStatus()).thenReturn(true);
471+
AppDistributionReleaseInternal release = TEST_RELEASE_NEWER_APK_INTERNAL.build();
472+
firebaseAppDistribution.setCachedNewRelease(release);
473+
UpdateTaskImpl updateTaskToReturn = new UpdateTaskImpl();
474+
doReturn(updateTaskToReturn).when(mockUpdateApkClient).updateApk(release, false);
475+
476+
UpdateTask updateTask = firebaseAppDistribution.updateApp();
477+
478+
assertEquals(updateTask, updateTaskToReturn);
479+
}
418480
}

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/UpdateAabClientTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,14 @@ public void updateAppTask_onAppResume_setsUpdateCancelled()
145145
assertThrows(FirebaseAppDistributionException.class, onCompleteListener::await);
146146
assertEquals(ReleaseUtils.convertToAppDistributionRelease(newRelease), exception.getRelease());
147147
}
148+
149+
@Test
150+
public void updateApp_whenCalledMultipleTimesWithAAB_returnsSameUpdateTask() {
151+
AppDistributionReleaseInternal newRelease = TEST_RELEASE_NEWER_AAB_INTERNAL.build();
152+
153+
UpdateTask updateTask1 = updateAabClient.updateAab(newRelease);
154+
UpdateTask updateTask2 = updateAabClient.updateAab(newRelease);
155+
156+
assertEquals(updateTask1, updateTask2);
157+
}
148158
}

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/UpdateApkClientTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,12 @@ public void postProgressUpdate_whenErrorStatus_updatesNotificationsManagerWithEr
209209
updateApkClient.postUpdateProgress(1000, 900, UpdateStatus.DOWNLOADING, false);
210210
assertEquals(0, shadowNotificationManager.size());
211211
}
212+
213+
@Test
214+
public void updateApp_whenCalledMultipleTimesWithApk_returnsSameUpdateTask() {
215+
UpdateTask updateTask1 = updateApkClient.updateApk(TEST_RELEASE, false);
216+
UpdateTask updateTask2 = updateApkClient.updateApk(TEST_RELEASE, false);
217+
218+
assertEquals(updateTask1, updateTask2);
219+
}
212220
}

0 commit comments

Comments
 (0)