|
86 | 86 | import com.google.firebase.installations.InstallationTokenResult;
|
87 | 87 | import java.util.ArrayList;
|
88 | 88 | import java.util.List;
|
| 89 | +import java.util.concurrent.CountDownLatch; |
89 | 90 | import java.util.concurrent.ExecutionException;
|
90 | 91 | import java.util.concurrent.ExecutorService;
|
91 | 92 | import java.util.concurrent.Executors;
|
@@ -364,11 +365,29 @@ public void updateIfNewReleaseAvailable_whenNoReleaseAvailable_updateDialogNotSh
|
364 | 365 | @Test
|
365 | 366 | public void updateIfNewReleaseAvailable_whenActivityBackgrounded_updateDialogNotShown()
|
366 | 367 | throws InterruptedException {
|
367 |
| - when(mockNewReleaseFetcher.checkForNewRelease()).thenReturn(Tasks.forResult(null)); |
368 |
| - |
| 368 | + // Block thread making the request on a latch, which gives us time to add listeners to the |
| 369 | + // returned UpdateTask in time to get all the progress updates |
| 370 | + CountDownLatch countDownLatch = new CountDownLatch(1); |
| 371 | + when(mockNewReleaseFetcher.checkForNewRelease()) |
| 372 | + .thenAnswer( |
| 373 | + invocation -> { |
| 374 | + try { |
| 375 | + countDownLatch.await(); |
| 376 | + } catch (InterruptedException e) { |
| 377 | + throw new AssertionError("Interrupted while waiting in mock"); |
| 378 | + } |
| 379 | + return Tasks.forResult(null); |
| 380 | + }); |
| 381 | + |
| 382 | + // Start the update |
369 | 383 | UpdateTask task = firebaseAppDistribution.updateIfNewReleaseAvailable();
|
370 | 384 |
|
371 |
| - List<UpdateProgress> progressEvents = awaitProgressEvents(task, 1); |
| 385 | + // Listen for progress events |
| 386 | + List<UpdateProgress> progressEvents = new ArrayList<>(); |
| 387 | + task.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add); |
| 388 | + countDownLatch.countDown(); |
| 389 | + awaitProgressEvents(progressEvents, 1); |
| 390 | + |
372 | 391 | assertEquals(UpdateStatus.NEW_RELEASE_NOT_AVAILABLE, progressEvents.get(0).getUpdateStatus());
|
373 | 392 | assertNull(ShadowAlertDialog.getLatestAlertDialog());
|
374 | 393 | }
|
|
0 commit comments