From 9add3c764ebe50f8780394cf4e805bc8472538cc Mon Sep 17 00:00:00 2001 From: olly Date: Fri, 8 Jan 2021 17:54:05 +0000 Subject: [PATCH] Fix flaky DownloadManager test PiperOrigin-RevId: 350782940 --- .../exoplayer2/offline/DownloadManagerTest.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java index 4dd80b6c7c..da78da6af2 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadManagerTest.java @@ -24,6 +24,8 @@ import androidx.annotation.Nullable; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.offline.DownloadManager.Listener; +import com.google.android.exoplayer2.robolectric.TestDownloadManagerListener; import com.google.android.exoplayer2.scheduler.Requirements; import com.google.android.exoplayer2.testutil.DownloadBuilder; import com.google.android.exoplayer2.testutil.DummyMainThread; @@ -621,11 +623,23 @@ public class DownloadManagerTest { downloadRemover.assertRemoveStarted(); // Re-add the download with a stop reason. + ConditionVariable downloadManagerIdleCondition = new ConditionVariable(); runOnMainThread( - () -> downloadManager.addDownload(createDownloadRequest(ID1), /* stopReason= */ 1234)); + () -> { + downloadManager.addListener( + new Listener() { + @Override + public void onIdle(DownloadManager downloadManager) { + downloadManagerIdleCondition.open(); + } + }); + downloadManager.addDownload(createDownloadRequest(ID1), /* stopReason= */ 1234); + }); downloadRemover.finish(); + assertThat(downloadManagerIdleCondition.block(TIMEOUT_MS)).isTrue(); + assertDownloadIndexSize(1); // We expect one downloader for the initial download, and one for the removal. A downloader // should not be created when the download is re-added, since a stop reason is specified.