Skip to content

Commit

Permalink
Adding unit tests to ApkUpdater class (firebase#3594)
Browse files Browse the repository at this point in the history
* 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 <mannyjimenez@google.com>
  • Loading branch information
manny-jimenez and Manny Jimenez authored Mar 30, 2022
1 parent 79847b9 commit c1d517c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ private long downloadToDisk(
return bytesDownloaded;
}

private void validateJarFile(
File apkFile, long totalSize, boolean showNotification, long bytesDownloaded)
@VisibleForTesting
void validateJarFile(File apkFile, long totalSize, boolean showNotification, long bytesDownloaded)
throws FirebaseAppDistributionException {
try {
new JarFile(apkFile).close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import static com.google.firebase.appdistribution.TestUtils.applyToForegroundActivityTaskAnswer;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
Expand All @@ -31,10 +34,12 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.net.ssl.HttpsURLConnection;
Expand All @@ -55,6 +60,7 @@ public class ApkUpdaterTest {
private static final String TEST_PROJECT_ID = "777777777777";
private static final String TEST_URL = "https://test-url";
private static final String TEST_CODE_HASH = "abcdefghijklmnopqrstuvwxyz";
private static final String TEST_FILE = "test_file";
private static final long TEST_FILE_LENGTH = 1000;
private TestActivity activity;

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

UpdateTaskImpl updateTask = apkUpdater.updateApk(TEST_RELEASE, false);
UpdateTaskImpl updateTask = apkUpdater.updateApk(TEST_RELEASE, true);
updateTask.addOnCompleteListener(testExecutor, onCompleteListener);
FirebaseAppDistributionException e =
assertThrows(FirebaseAppDistributionException.class, () -> onCompleteListener.await());

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

@Test
public void updateApk_whenInstallSuccessful_setsResult() throws Exception {
doReturn(Tasks.forResult(mockFile)).when(apkUpdater).downloadApk(TEST_RELEASE, false);
public void updateApk_whenSuccessfullyUpdated_notificationsSetCorrectly()
throws FirebaseAppDistributionException, ExecutionException, InterruptedException,
IOException {
doReturn(new ByteArrayInputStream(TEST_FILE.getBytes()))
.when(mockHttpsUrlConnection)
.getInputStream();
doNothing().when(apkUpdater).validateJarFile(any(), anyLong(), anyBoolean(), anyLong());
when(mockApkInstaller.installApk(any(), any())).thenReturn(Tasks.forResult(null));
UpdateTaskImpl updateTask = apkUpdater.updateApk(TEST_RELEASE, false);

UpdateTask updateTask = apkUpdater.updateApk(TEST_RELEASE, true);
updateTask.addOnCompleteListener(testExecutor, onCompleteListener);
List<UpdateProgress> events = new ArrayList<>();
updateTask.addOnProgressListener(testExecutor, events::add);
onCompleteListener.await();
assertThat(updateTask.isSuccessful()).isTrue();

assertThat(events).hasSize(3);
assertThat(events.get(0).getUpdateStatus()).isEqualTo(UpdateStatus.PENDING);
assertThat(events.get(1).getUpdateStatus()).isEqualTo(UpdateStatus.DOWNLOADING);
assertThat(events.get(1).getApkBytesDownloaded()).isEqualTo(TEST_FILE.length());
assertThat(events.get(2).getUpdateStatus()).isEqualTo(UpdateStatus.DOWNLOADED);
}

@Test
public void updateApk_invalidJarFile_throwsException() throws IOException {
doReturn(new ByteArrayInputStream(TEST_FILE.getBytes()))
.when(mockHttpsUrlConnection)
.getInputStream();
when(mockApkInstaller.installApk(any(), any())).thenReturn(Tasks.forResult(null));

// If validateJarFile is not mocked it will be called with an invalid jar file.
UpdateTask updateTask = apkUpdater.updateApk(TEST_RELEASE, true);
updateTask.addOnCompleteListener(testExecutor, onCompleteListener);
FirebaseAppDistributionException e =
assertThrows(FirebaseAppDistributionException.class, () -> onCompleteListener.await());

assertThat(e.getErrorCode()).isEqualTo(Status.DOWNLOAD_FAILURE);
assertThat(updateTask.isSuccessful()).isFalse();

// Verify that the notification in validateJarFile is set.
verify(mockNotificationsManager)
.updateNotification(0, TEST_FILE.length(), R.string.download_failed);
}

@Test
Expand Down

0 comments on commit c1d517c

Please sign in to comment.