From 93f2dd8deb5ed3f64985a8e9a3b1ce3d9b22ce8a Mon Sep 17 00:00:00 2001 From: ibaker Date: Mon, 21 Oct 2019 09:59:44 +0100 Subject: [PATCH] Switch test usages of ConditionVariable#block(timeout) to CountDownLatch ConditionVariable.block(timeout) doesn't work in Robolectric, because it relies on the system clock which doesn't advance. PiperOrigin-RevId: 275798281 --- .../offline/DownloadHelperTest.java | 11 +- .../source/ConcatenatingMediaSourceTest.java | 106 +++++++++--------- .../dash/offline/DownloadManagerDashTest.java | 14 ++- .../exoplayer2/testutil/DummyMainThread.java | 13 ++- .../testutil/MediaSourceTestRunner.java | 26 +++-- .../testutil/TestDownloadManagerListener.java | 26 ++--- 6 files changed, 106 insertions(+), 90 deletions(-) diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadHelperTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadHelperTest.java index 111edc7af8..f99864440d 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadHelperTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadHelperTest.java @@ -40,13 +40,14 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo; import com.google.android.exoplayer2.trackselection.TrackSelection; import com.google.android.exoplayer2.upstream.Allocator; -import com.google.android.exoplayer2.util.ConditionVariable; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import org.junit.Before; import org.junit.Test; @@ -412,21 +413,21 @@ public class DownloadHelperTest { private static void prepareDownloadHelper(DownloadHelper downloadHelper) throws Exception { AtomicReference prepareException = new AtomicReference<>(null); - ConditionVariable preparedCondition = new ConditionVariable(); + CountDownLatch preparedLatch = new CountDownLatch(1); downloadHelper.prepare( new Callback() { @Override public void onPrepared(DownloadHelper helper) { - preparedCondition.open(); + preparedLatch.countDown(); } @Override public void onPrepareError(DownloadHelper helper, IOException e) { prepareException.set(e); - preparedCondition.open(); + preparedLatch.countDown(); } }); - while (!preparedCondition.block(0)) { + while (!preparedLatch.await(0, TimeUnit.MILLISECONDS)) { shadowMainLooper().idleFor(shadowMainLooper().getNextScheduledTaskTime()); } if (prepareException.get() != null) { diff --git a/library/core/src/test/java/com/google/android/exoplayer2/source/ConcatenatingMediaSourceTest.java b/library/core/src/test/java/com/google/android/exoplayer2/source/ConcatenatingMediaSourceTest.java index 39f36a991b..d63c85ac71 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/source/ConcatenatingMediaSourceTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/source/ConcatenatingMediaSourceTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; -import android.os.ConditionVariable; import android.os.Handler; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.C; @@ -39,6 +38,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -407,23 +407,23 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackBeforePreparationAddSingle() { - ConditionVariable runnableInvoked = new ConditionVariable(); + public void testCustomCallbackBeforePreparationAddSingle() throws Exception { + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( () -> mediaSource.addMediaSource( - createFakeMediaSource(), new Handler(), runnableInvoked::open)); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + createFakeMediaSource(), new Handler(), runnableInvoked::countDown)); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test - public void testCustomCallbackBeforePreparationAddMultiple() { - ConditionVariable runnableInvoked = new ConditionVariable(); + public void testCustomCallbackBeforePreparationAddMultiple() throws Exception { + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( @@ -431,31 +431,31 @@ public final class ConcatenatingMediaSourceTest { mediaSource.addMediaSources( Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}), new Handler(), - runnableInvoked::open)); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + runnableInvoked::countDown)); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test - public void testCustomCallbackBeforePreparationAddSingleWithIndex() { - ConditionVariable runnableInvoked = new ConditionVariable(); + public void testCustomCallbackBeforePreparationAddSingleWithIndex() throws Exception { + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( () -> mediaSource.addMediaSource( - /* index */ 0, createFakeMediaSource(), new Handler(), runnableInvoked::open)); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + /* index */ 0, createFakeMediaSource(), new Handler(), runnableInvoked::countDown)); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test - public void testCustomCallbackBeforePreparationAddMultipleWithIndex() { - ConditionVariable runnableInvoked = new ConditionVariable(); + public void testCustomCallbackBeforePreparationAddMultipleWithIndex() throws Exception { + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( @@ -464,32 +464,32 @@ public final class ConcatenatingMediaSourceTest { /* index */ 0, Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}), new Handler(), - runnableInvoked::open)); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + runnableInvoked::countDown)); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test - public void testCustomCallbackBeforePreparationRemove() { - ConditionVariable runnableInvoked = new ConditionVariable(); + public void testCustomCallbackBeforePreparationRemove() throws Exception { + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( () -> { mediaSource.addMediaSource(createFakeMediaSource()); - mediaSource.removeMediaSource(/* index */ 0, new Handler(), runnableInvoked::open); + mediaSource.removeMediaSource(/* index */ 0, new Handler(), runnableInvoked::countDown); }); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test - public void testCustomCallbackBeforePreparationMove() { - ConditionVariable runnableInvoked = new ConditionVariable(); + public void testCustomCallbackBeforePreparationMove() throws Exception { + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( @@ -497,16 +497,16 @@ public final class ConcatenatingMediaSourceTest { mediaSource.addMediaSources( Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()})); mediaSource.moveMediaSource( - /* fromIndex */ 1, /* toIndex */ 0, new Handler(), runnableInvoked::open); + /* fromIndex */ 1, /* toIndex */ 0, new Handler(), runnableInvoked::countDown); }); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test - public void testCustomCallbackAfterPreparationAddSingle() throws IOException { + public void testCustomCallbackAfterPreparationAddSingle() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); try { testRunner.prepareSource(); @@ -522,7 +522,7 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackAfterPreparationAddMultiple() throws IOException { + public void testCustomCallbackAfterPreparationAddMultiple() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); try { testRunner.prepareSource(); @@ -542,7 +542,7 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackAfterPreparationAddSingleWithIndex() throws IOException { + public void testCustomCallbackAfterPreparationAddSingleWithIndex() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); try { testRunner.prepareSource(); @@ -559,7 +559,7 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackAfterPreparationAddMultipleWithIndex() throws IOException { + public void testCustomCallbackAfterPreparationAddMultipleWithIndex() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); try { testRunner.prepareSource(); @@ -580,7 +580,7 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackAfterPreparationRemove() throws IOException { + public void testCustomCallbackAfterPreparationRemove() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); try { testRunner.prepareSource(); @@ -598,7 +598,7 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackAfterPreparationMove() throws IOException { + public void testCustomCallbackAfterPreparationMove() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); try { testRunner.prepareSource(); @@ -622,9 +622,9 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testCustomCallbackIsCalledAfterRelease() throws IOException { + public void testCustomCallbackIsCalledAfterRelease() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); - ConditionVariable callbackCalledCondition = new ConditionVariable(); + CountDownLatch callbackCalledCondition = new CountDownLatch(1); try { dummyMainThread.runOnMainThread( () -> { @@ -635,10 +635,13 @@ public final class ConcatenatingMediaSourceTest { /* currentIndex= */ 0, /* newIndex= */ 1, new Handler(), - callbackCalledCondition::open); + callbackCalledCondition::countDown); mediaSource.releaseSource(caller); }); - assertThat(callbackCalledCondition.block(MediaSourceTestRunner.TIMEOUT_MS)).isTrue(); + assertThat( + callbackCalledCondition.await( + MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS)) + .isTrue(); } finally { dummyMainThread.release(); } @@ -871,7 +874,7 @@ public final class ConcatenatingMediaSourceTest { } @Test - public void testClear() throws IOException { + public void testClear() throws Exception { DummyMainThread dummyMainThread = new DummyMainThread(); final FakeMediaSource preparedChildSource = createFakeMediaSource(); final FakeMediaSource unpreparedChildSource = new FakeMediaSource(/* timeline= */ null); @@ -1028,7 +1031,7 @@ public final class ConcatenatingMediaSourceTest { @Test public void testCustomCallbackBeforePreparationSetShuffleOrder() throws Exception { - ConditionVariable runnableInvoked = new ConditionVariable(); + CountDownLatch runnableInvoked = new CountDownLatch(1); DummyMainThread dummyMainThread = new DummyMainThread(); dummyMainThread.runOnMainThread( @@ -1036,11 +1039,11 @@ public final class ConcatenatingMediaSourceTest { mediaSource.setShuffleOrder( new ShuffleOrder.UnshuffledShuffleOrder(/* length= */ 0), new Handler(), - runnableInvoked::open)); - runnableInvoked.block(MediaSourceTestRunner.TIMEOUT_MS); + runnableInvoked::countDown)); + runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS); dummyMainThread.release(); - assertThat(runnableInvoked.block(0)).isTrue(); + assertThat(runnableInvoked.getCount()).isEqualTo(0); } @Test @@ -1107,14 +1110,14 @@ public final class ConcatenatingMediaSourceTest { private static final class TimelineGrabber implements Runnable { private final MediaSourceTestRunner testRunner; - private final ConditionVariable finishedCondition; + private final CountDownLatch finishedLatch; private Timeline timeline; private AssertionError error; public TimelineGrabber(MediaSourceTestRunner testRunner) { this.testRunner = testRunner; - finishedCondition = new ConditionVariable(); + finishedLatch = new CountDownLatch(1); } @Override @@ -1124,11 +1127,12 @@ public final class ConcatenatingMediaSourceTest { } catch (AssertionError e) { error = e; } - finishedCondition.open(); + finishedLatch.countDown(); } - public Timeline assertTimelineChangeBlocking() { - assertThat(finishedCondition.block(MediaSourceTestRunner.TIMEOUT_MS)).isTrue(); + public Timeline assertTimelineChangeBlocking() throws InterruptedException { + assertThat(finishedLatch.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS)) + .isTrue(); if (error != null) { throw error; } diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java index bc75df6acf..9ca6a24185 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java @@ -23,7 +23,6 @@ import static com.google.android.exoplayer2.testutil.CacheAsserts.assertCachedDa import static com.google.common.truth.Truth.assertThat; import android.content.Context; -import android.os.ConditionVariable; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.offline.DefaultDownloadIndex; @@ -47,6 +46,8 @@ import com.google.android.exoplayer2.util.Util; import java.io.File; import java.util.ArrayList; import java.util.Collections; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Ignore; @@ -61,7 +62,7 @@ import org.robolectric.shadows.ShadowLog; @LooperMode(LooperMode.Mode.PAUSED) public class DownloadManagerDashTest { - private static final int ASSERT_TRUE_TIMEOUT = 1000; + private static final int ASSERT_TRUE_TIMEOUT_MS = 1000; private SimpleCache cache; private File tempFolder; @@ -204,16 +205,17 @@ public class DownloadManagerDashTest { @Test public void testHandleInterferingRemoveAction() throws Throwable { - final ConditionVariable downloadInProgressCondition = new ConditionVariable(); + CountDownLatch downloadInProgressLatch = new CountDownLatch(1); fakeDataSet .newData("audio_segment_2") - .appendReadAction(downloadInProgressCondition::open) + .appendReadAction(downloadInProgressLatch::countDown) .appendReadData(TestUtil.buildTestData(5)) .endData(); handleDownloadRequest(fakeStreamKey1); - assertThat(downloadInProgressCondition.block(ASSERT_TRUE_TIMEOUT)).isTrue(); + assertThat(downloadInProgressLatch.await(ASSERT_TRUE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) + .isTrue(); handleRemoveAction(); @@ -261,7 +263,7 @@ public class DownloadManagerDashTest { downloadManagerListener = new TestDownloadManagerListener( - downloadManager, dummyMainThread, /* timeout= */ 3000); + downloadManager, dummyMainThread, /* timeoutMs= */ 3000); downloadManager.resumeDownloads(); }); } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DummyMainThread.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DummyMainThread.java index 63a761a5cf..9678e03b40 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DummyMainThread.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DummyMainThread.java @@ -17,11 +17,12 @@ package com.google.android.exoplayer2.testutil; import static com.google.common.truth.Truth.assertThat; -import android.os.ConditionVariable; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import com.google.android.exoplayer2.util.Util; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; /** Helper class to simulate main/UI thread in tests. */ @@ -90,7 +91,7 @@ public final class DummyMainThread { Util.sneakyThrow(e); } } else { - ConditionVariable finishedCondition = new ConditionVariable(); + CountDownLatch finishedLatch = new CountDownLatch(1); AtomicReference thrown = new AtomicReference<>(); handler.post( () -> { @@ -99,9 +100,13 @@ public final class DummyMainThread { } catch (Throwable t) { thrown.set(t); } - finishedCondition.open(); + finishedLatch.countDown(); }); - assertThat(finishedCondition.block(timeoutMs)).isTrue(); + try { + assertThat(finishedLatch.await(timeoutMs, TimeUnit.MILLISECONDS)).isTrue(); + } catch (InterruptedException e) { + Util.sneakyThrow(e); + } if (thrown.get() != null) { Util.sneakyThrow(thrown.get()); } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/MediaSourceTestRunner.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/MediaSourceTestRunner.java index 29d1b0bd75..8eaa1df9c6 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/MediaSourceTestRunner.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/MediaSourceTestRunner.java @@ -89,8 +89,8 @@ public class MediaSourceTestRunner { * @param runnable The {@link Runnable} to run. */ public void runOnPlaybackThread(final Runnable runnable) { - final Throwable[] throwable = new Throwable[1]; - final ConditionVariable finishedCondition = new ConditionVariable(); + Throwable[] throwable = new Throwable[1]; + CountDownLatch finishedLatch = new CountDownLatch(1); playbackHandler.post( () -> { try { @@ -98,10 +98,14 @@ public class MediaSourceTestRunner { } catch (Throwable e) { throwable[0] = e; } finally { - finishedCondition.open(); + finishedLatch.countDown(); } }); - assertThat(finishedCondition.block(TIMEOUT_MS)).isTrue(); + try { + assertThat(finishedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); + } catch (InterruptedException e) { + Util.sneakyThrow(e); + } if (throwable[0] != null) { Util.sneakyThrow(throwable[0]); } @@ -169,14 +173,14 @@ public class MediaSourceTestRunner { */ public CountDownLatch preparePeriod(final MediaPeriod mediaPeriod, final long positionUs) { final ConditionVariable prepareCalled = new ConditionVariable(); - final CountDownLatch preparedCountDown = new CountDownLatch(1); + final CountDownLatch preparedLatch = new CountDownLatch(1); runOnPlaybackThread( () -> { mediaPeriod.prepare( new MediaPeriod.Callback() { @Override public void onPrepared(MediaPeriod mediaPeriod1) { - preparedCountDown.countDown(); + preparedLatch.countDown(); } @Override @@ -188,7 +192,7 @@ public class MediaSourceTestRunner { prepareCalled.open(); }); prepareCalled.block(); - return preparedCountDown; + return preparedLatch; } /** @@ -267,8 +271,8 @@ public class MediaSourceTestRunner { throws InterruptedException { MediaPeriod mediaPeriod = createPeriod(mediaPeriodId); assertThat(lastCreatedMediaPeriod.getAndSet(/* newValue= */ null)).isEqualTo(mediaPeriodId); - CountDownLatch preparedCondition = preparePeriod(mediaPeriod, 0); - assertThat(preparedCondition.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); + CountDownLatch preparedLatch = preparePeriod(mediaPeriod, 0); + assertThat(preparedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); // MediaSource is supposed to support multiple calls to createPeriod without an intervening call // to releasePeriod. MediaPeriodId secondMediaPeriodId = @@ -280,8 +284,8 @@ public class MediaSourceTestRunner { MediaPeriod secondMediaPeriod = createPeriod(secondMediaPeriodId); assertThat(lastCreatedMediaPeriod.getAndSet(/* newValue= */ null)) .isEqualTo(secondMediaPeriodId); - CountDownLatch secondPreparedCondition = preparePeriod(secondMediaPeriod, 0); - assertThat(secondPreparedCondition.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); + CountDownLatch secondPreparedLatch = preparePeriod(secondMediaPeriod, 0); + assertThat(secondPreparedLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue(); // Release the periods. releasePeriod(mediaPeriod); assertThat(lastReleasedMediaPeriod.getAndSet(/* newValue= */ null)).isEqualTo(mediaPeriodId); diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java index 4c334992b5..c42071d0a2 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java @@ -18,7 +18,6 @@ package com.google.android.exoplayer2.testutil; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; -import android.os.ConditionVariable; import com.google.android.exoplayer2.offline.Download; import com.google.android.exoplayer2.offline.Download.State; import com.google.android.exoplayer2.offline.DownloadManager; @@ -30,31 +29,31 @@ import java.util.concurrent.TimeUnit; /** A {@link DownloadManager.Listener} for testing. */ public final class TestDownloadManagerListener implements DownloadManager.Listener { - private static final int TIMEOUT = 1000; - private static final int INITIALIZATION_TIMEOUT = 10000; + private static final int TIMEOUT_MS = 1000; + private static final int INITIALIZATION_TIMEOUT_MS = 10_000; private static final int STATE_REMOVED = -1; private final DownloadManager downloadManager; private final DummyMainThread dummyMainThread; private final HashMap> downloadStates; - private final ConditionVariable initializedCondition; - private final int timeout; + private final CountDownLatch initializedCondition; + private final int timeoutMs; private CountDownLatch downloadFinishedCondition; @Download.FailureReason private int failureReason; public TestDownloadManagerListener( DownloadManager downloadManager, DummyMainThread dummyMainThread) { - this(downloadManager, dummyMainThread, TIMEOUT); + this(downloadManager, dummyMainThread, TIMEOUT_MS); } public TestDownloadManagerListener( - DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeout) { + DownloadManager downloadManager, DummyMainThread dummyMainThread, int timeoutMs) { this.downloadManager = downloadManager; this.dummyMainThread = dummyMainThread; - this.timeout = timeout; + this.timeoutMs = timeoutMs; downloadStates = new HashMap<>(); - initializedCondition = new ConditionVariable(); + initializedCondition = new CountDownLatch(1); downloadManager.addListener(this); } @@ -64,12 +63,13 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen @Override public void onInitialized(DownloadManager downloadManager) { - initializedCondition.open(); + initializedCondition.countDown(); } - public void waitUntilInitialized() { + public void waitUntilInitialized() throws InterruptedException { if (!downloadManager.isInitialized()) { - assertThat(initializedCondition.block(INITIALIZATION_TIMEOUT)).isTrue(); + assertThat(initializedCondition.await(INITIALIZATION_TIMEOUT_MS, TimeUnit.MILLISECONDS)) + .isTrue(); } } @@ -115,7 +115,7 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen downloadFinishedCondition.countDown(); } }); - assertThat(downloadFinishedCondition.await(timeout, TimeUnit.MILLISECONDS)).isTrue(); + assertThat(downloadFinishedCondition.await(timeoutMs, TimeUnit.MILLISECONDS)).isTrue(); } private ArrayBlockingQueue getStateQueue(String taskId) {