Skip to content

Commit c1d517c

Browse files
manny-jimenezManny Jimenez
and
Manny Jimenez
authored
Adding unit tests to ApkUpdater class (#3594)
* Adding unit tests to ApkUpdater class * Adding another test * Fixing tests * Fixing up spaces * Responding to comments * Removing unncessary mock * adding comment Co-authored-by: Manny Jimenez <[email protected]>
1 parent 79847b9 commit c1d517c

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

firebase-appdistribution/src/main/java/com/google/firebase/appdistribution/ApkUpdater.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ private long downloadToDisk(
246246
return bytesDownloaded;
247247
}
248248

249-
private void validateJarFile(
250-
File apkFile, long totalSize, boolean showNotification, long bytesDownloaded)
249+
@VisibleForTesting
250+
void validateJarFile(File apkFile, long totalSize, boolean showNotification, long bytesDownloaded)
251251
throws FirebaseAppDistributionException {
252252
try {
253253
new JarFile(apkFile).close();

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/ApkUpdaterTest.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import static com.google.firebase.appdistribution.TestUtils.applyToForegroundActivityTaskAnswer;
1919
import static org.junit.Assert.assertThrows;
2020
import static org.mockito.ArgumentMatchers.any;
21+
import static org.mockito.ArgumentMatchers.anyBoolean;
22+
import static org.mockito.ArgumentMatchers.anyLong;
23+
import static org.mockito.Mockito.doNothing;
2124
import static org.mockito.Mockito.doReturn;
2225
import static org.mockito.Mockito.verify;
2326
import static org.mockito.Mockito.verifyNoInteractions;
@@ -31,10 +34,12 @@
3134
import com.google.firebase.FirebaseApp;
3235
import com.google.firebase.FirebaseOptions;
3336
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
37+
import java.io.ByteArrayInputStream;
3438
import java.io.File;
3539
import java.io.IOException;
3640
import java.util.ArrayList;
3741
import java.util.List;
42+
import java.util.concurrent.ExecutionException;
3843
import java.util.concurrent.Executor;
3944
import java.util.concurrent.Executors;
4045
import javax.net.ssl.HttpsURLConnection;
@@ -55,6 +60,7 @@ public class ApkUpdaterTest {
5560
private static final String TEST_PROJECT_ID = "777777777777";
5661
private static final String TEST_URL = "https://test-url";
5762
private static final String TEST_CODE_HASH = "abcdefghijklmnopqrstuvwxyz";
63+
private static final String TEST_FILE = "test_file";
5864
private static final long TEST_FILE_LENGTH = 1000;
5965
private TestActivity activity;
6066

@@ -150,24 +156,59 @@ public void updateApk_whenCannotReadInputStream_setsDownloadFailure() throws Exc
150156
IOException caughtException = new IOException("error");
151157
when(mockHttpsUrlConnection.getInputStream()).thenThrow(caughtException);
152158

153-
UpdateTaskImpl updateTask = apkUpdater.updateApk(TEST_RELEASE, false);
159+
UpdateTaskImpl updateTask = apkUpdater.updateApk(TEST_RELEASE, true);
154160
updateTask.addOnCompleteListener(testExecutor, onCompleteListener);
155161
FirebaseAppDistributionException e =
156162
assertThrows(FirebaseAppDistributionException.class, () -> onCompleteListener.await());
157163

158164
assertThat(e.getErrorCode()).isEqualTo(Status.DOWNLOAD_FAILURE);
159165
assertThat(e).hasMessageThat().contains("Failed to download APK");
160166
assertThat(e).hasCauseThat().isEqualTo(caughtException);
167+
verify(mockNotificationsManager).updateNotification(0, 0, R.string.download_failed);
161168
}
162169

163170
@Test
164-
public void updateApk_whenInstallSuccessful_setsResult() throws Exception {
165-
doReturn(Tasks.forResult(mockFile)).when(apkUpdater).downloadApk(TEST_RELEASE, false);
171+
public void updateApk_whenSuccessfullyUpdated_notificationsSetCorrectly()
172+
throws FirebaseAppDistributionException, ExecutionException, InterruptedException,
173+
IOException {
174+
doReturn(new ByteArrayInputStream(TEST_FILE.getBytes()))
175+
.when(mockHttpsUrlConnection)
176+
.getInputStream();
177+
doNothing().when(apkUpdater).validateJarFile(any(), anyLong(), anyBoolean(), anyLong());
166178
when(mockApkInstaller.installApk(any(), any())).thenReturn(Tasks.forResult(null));
167-
UpdateTaskImpl updateTask = apkUpdater.updateApk(TEST_RELEASE, false);
179+
180+
UpdateTask updateTask = apkUpdater.updateApk(TEST_RELEASE, true);
168181
updateTask.addOnCompleteListener(testExecutor, onCompleteListener);
182+
List<UpdateProgress> events = new ArrayList<>();
183+
updateTask.addOnProgressListener(testExecutor, events::add);
169184
onCompleteListener.await();
170-
assertThat(updateTask.isSuccessful()).isTrue();
185+
186+
assertThat(events).hasSize(3);
187+
assertThat(events.get(0).getUpdateStatus()).isEqualTo(UpdateStatus.PENDING);
188+
assertThat(events.get(1).getUpdateStatus()).isEqualTo(UpdateStatus.DOWNLOADING);
189+
assertThat(events.get(1).getApkBytesDownloaded()).isEqualTo(TEST_FILE.length());
190+
assertThat(events.get(2).getUpdateStatus()).isEqualTo(UpdateStatus.DOWNLOADED);
191+
}
192+
193+
@Test
194+
public void updateApk_invalidJarFile_throwsException() throws IOException {
195+
doReturn(new ByteArrayInputStream(TEST_FILE.getBytes()))
196+
.when(mockHttpsUrlConnection)
197+
.getInputStream();
198+
when(mockApkInstaller.installApk(any(), any())).thenReturn(Tasks.forResult(null));
199+
200+
// If validateJarFile is not mocked it will be called with an invalid jar file.
201+
UpdateTask updateTask = apkUpdater.updateApk(TEST_RELEASE, true);
202+
updateTask.addOnCompleteListener(testExecutor, onCompleteListener);
203+
FirebaseAppDistributionException e =
204+
assertThrows(FirebaseAppDistributionException.class, () -> onCompleteListener.await());
205+
206+
assertThat(e.getErrorCode()).isEqualTo(Status.DOWNLOAD_FAILURE);
207+
assertThat(updateTask.isSuccessful()).isFalse();
208+
209+
// Verify that the notification in validateJarFile is set.
210+
verify(mockNotificationsManager)
211+
.updateNotification(0, TEST_FILE.length(), R.string.download_failed);
171212
}
172213

173214
@Test

0 commit comments

Comments
 (0)