Commit 2d494861 by olly Committed by Oliver Woodman

Cleanup and document TestDownloadManagerListener

PiperOrigin-RevId: 308843488
parent 4580b524
...@@ -100,7 +100,7 @@ public class DownloadManagerTest { ...@@ -100,7 +100,7 @@ public class DownloadManagerTest {
assertCompleted(ID1); assertCompleted(ID1);
assertDownloaderCount(ID1, 1); assertDownloaderCount(ID1, 1);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertThat(downloadManager.getCurrentDownloads()).isEmpty(); assertThat(downloadManager.getCurrentDownloads()).isEmpty();
} }
...@@ -117,7 +117,7 @@ public class DownloadManagerTest { ...@@ -117,7 +117,7 @@ public class DownloadManagerTest {
assertRemoved(ID1); assertRemoved(ID1);
assertDownloaderCount(ID1, 2); assertDownloaderCount(ID1, 2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertThat(downloadManager.getCurrentDownloads()).isEmpty(); assertThat(downloadManager.getCurrentDownloads()).isEmpty();
} }
...@@ -134,7 +134,7 @@ public class DownloadManagerTest { ...@@ -134,7 +134,7 @@ public class DownloadManagerTest {
downloader.assertStartCount(MIN_RETRY_COUNT + 1); downloader.assertStartCount(MIN_RETRY_COUNT + 1);
assertFailed(ID1); assertFailed(ID1);
downloadManagerListener.blockUntilTasksComplete(); downloadManagerListener.blockUntilIdle();
assertCurrentDownloadCount(0); assertCurrentDownloadCount(0);
} }
...@@ -153,7 +153,7 @@ public class DownloadManagerTest { ...@@ -153,7 +153,7 @@ public class DownloadManagerTest {
downloader.assertStartCount(MIN_RETRY_COUNT + 1); downloader.assertStartCount(MIN_RETRY_COUNT + 1);
assertCompleted(ID1); assertCompleted(ID1);
downloadManagerListener.blockUntilTasksComplete(); downloadManagerListener.blockUntilIdle();
assertCurrentDownloadCount(0); assertCurrentDownloadCount(0);
} }
...@@ -174,7 +174,7 @@ public class DownloadManagerTest { ...@@ -174,7 +174,7 @@ public class DownloadManagerTest {
downloader.assertStartCount(tooManyRetries + 1); downloader.assertStartCount(tooManyRetries + 1);
assertCompleted(ID1); assertCompleted(ID1);
downloadManagerListener.blockUntilTasksComplete(); downloadManagerListener.blockUntilIdle();
} }
@Test @Test
...@@ -192,7 +192,7 @@ public class DownloadManagerTest { ...@@ -192,7 +192,7 @@ public class DownloadManagerTest {
downloader2.unblock(); downloader2.unblock();
downloader2.assertCompleted(); downloader2.assertCompleted();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -212,7 +212,7 @@ public class DownloadManagerTest { ...@@ -212,7 +212,7 @@ public class DownloadManagerTest {
downloader2.unblock(); downloader2.unblock();
downloader2.assertCompleted(); downloader2.assertCompleted();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -230,7 +230,7 @@ public class DownloadManagerTest { ...@@ -230,7 +230,7 @@ public class DownloadManagerTest {
assertRemoved(ID1); assertRemoved(ID1);
assertDownloaderCount(ID1, 2); assertDownloaderCount(ID1, 2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -238,13 +238,13 @@ public class DownloadManagerTest { ...@@ -238,13 +238,13 @@ public class DownloadManagerTest {
// Finish one download and keep one running. // Finish one download and keep one running.
postDownloadRequest(ID1); postDownloadRequest(ID1);
getDownloader(ID1, 0).unblock(); getDownloader(ID1, 0).unblock();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
postDownloadRequest(ID2); postDownloadRequest(ID2);
postRemoveAllRequest(); postRemoveAllRequest();
getDownloader(ID1, 1).unblock(); getDownloader(ID1, 1).unblock();
getDownloader(ID2, 1).unblock(); getDownloader(ID2, 1).unblock();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertRemoved(ID1); assertRemoved(ID1);
assertRemoved(ID2); assertRemoved(ID2);
...@@ -270,7 +270,7 @@ public class DownloadManagerTest { ...@@ -270,7 +270,7 @@ public class DownloadManagerTest {
assertCompleted(ID1); assertCompleted(ID1);
assertDownloaderCount(ID1, 2); assertDownloaderCount(ID1, 2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -287,7 +287,7 @@ public class DownloadManagerTest { ...@@ -287,7 +287,7 @@ public class DownloadManagerTest {
assertCompleted(ID1); assertCompleted(ID1);
assertCompleted(ID2); assertCompleted(ID2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -307,7 +307,7 @@ public class DownloadManagerTest { ...@@ -307,7 +307,7 @@ public class DownloadManagerTest {
assertCompleted(ID1); assertCompleted(ID1);
assertCompleted(ID2); assertCompleted(ID2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -327,7 +327,7 @@ public class DownloadManagerTest { ...@@ -327,7 +327,7 @@ public class DownloadManagerTest {
assertCompleted(ID1); assertCompleted(ID1);
assertRemoved(ID2); assertRemoved(ID2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -351,7 +351,7 @@ public class DownloadManagerTest { ...@@ -351,7 +351,7 @@ public class DownloadManagerTest {
assertCompleted(ID1); assertCompleted(ID1);
assertCompleted(ID2); assertCompleted(ID2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -412,7 +412,7 @@ public class DownloadManagerTest { ...@@ -412,7 +412,7 @@ public class DownloadManagerTest {
downloader5.assertStarted(); downloader5.assertStarted();
downloader5.unblock(); downloader5.unblock();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -431,7 +431,7 @@ public class DownloadManagerTest { ...@@ -431,7 +431,7 @@ public class DownloadManagerTest {
downloader.assertStarted(); downloader.assertStarted();
downloader.unblock(); downloader.unblock();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -450,7 +450,7 @@ public class DownloadManagerTest { ...@@ -450,7 +450,7 @@ public class DownloadManagerTest {
downloader.unblock(); downloader.unblock();
assertRemoving(ID1); assertRemoving(ID1);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -476,7 +476,7 @@ public class DownloadManagerTest { ...@@ -476,7 +476,7 @@ public class DownloadManagerTest {
downloader2.assertStarted(); downloader2.assertStarted();
downloader2.unblock(); downloader2.unblock();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
} }
@Test @Test
...@@ -563,8 +563,7 @@ public class DownloadManagerTest { ...@@ -563,8 +563,7 @@ public class DownloadManagerTest {
downloadManager.setMinRetryCount(MIN_RETRY_COUNT); downloadManager.setMinRetryCount(MIN_RETRY_COUNT);
downloadManager.setRequirements(new Requirements(0)); downloadManager.setRequirements(new Requirements(0));
downloadManager.resumeDownloads(); downloadManager.resumeDownloads();
downloadManagerListener = downloadManagerListener = new TestDownloadManagerListener(downloadManager);
new TestDownloadManagerListener(downloadManager, dummyMainThread);
}); });
downloadManagerListener.blockUntilInitialized(); downloadManagerListener.blockUntilInitialized();
} catch (Throwable throwable) { } catch (Throwable throwable) {
...@@ -632,31 +631,31 @@ public class DownloadManagerTest { ...@@ -632,31 +631,31 @@ public class DownloadManagerTest {
} }
private void assertDownloading(String id) { private void assertDownloading(String id) {
downloadManagerListener.assertState(id, Download.STATE_DOWNLOADING, TIMEOUT_MS); downloadManagerListener.assertState(id, Download.STATE_DOWNLOADING);
} }
private void assertCompleted(String id) { private void assertCompleted(String id) {
downloadManagerListener.assertState(id, Download.STATE_COMPLETED, TIMEOUT_MS); downloadManagerListener.assertState(id, Download.STATE_COMPLETED);
} }
private void assertRemoving(String id) { private void assertRemoving(String id) {
downloadManagerListener.assertState(id, Download.STATE_REMOVING, TIMEOUT_MS); downloadManagerListener.assertState(id, Download.STATE_REMOVING);
} }
private void assertFailed(String id) { private void assertFailed(String id) {
downloadManagerListener.assertState(id, Download.STATE_FAILED, TIMEOUT_MS); downloadManagerListener.assertState(id, Download.STATE_FAILED);
} }
private void assertQueued(String id) { private void assertQueued(String id) {
downloadManagerListener.assertState(id, Download.STATE_QUEUED, TIMEOUT_MS); downloadManagerListener.assertState(id, Download.STATE_QUEUED);
} }
private void assertStopped(String id) { private void assertStopped(String id) {
downloadManagerListener.assertState(id, Download.STATE_STOPPED, TIMEOUT_MS); downloadManagerListener.assertState(id, Download.STATE_STOPPED);
} }
private void assertRemoved(String id) { private void assertRemoved(String id) {
downloadManagerListener.assertRemoved(id, TIMEOUT_MS); downloadManagerListener.assertRemoved(id);
} }
private void assertCurrentDownloadCount(int expectedCount) { private void assertCurrentDownloadCount(int expectedCount) {
......
...@@ -147,14 +147,14 @@ public class DownloadManagerDashTest { ...@@ -147,14 +147,14 @@ public class DownloadManagerDashTest {
dummyMainThread.runOnMainThread(this::createDownloadManager); dummyMainThread.runOnMainThread(this::createDownloadManager);
// Block on the test thread. // Block on the test thread.
blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertCachedData(cache, fakeDataSet); assertCachedData(cache, fakeDataSet);
} }
@Test @Test
public void handleDownloadRequest_downloadSuccess() throws Throwable { public void handleDownloadRequest_downloadSuccess() throws Throwable {
handleDownloadRequest(fakeStreamKey1, fakeStreamKey2); handleDownloadRequest(fakeStreamKey1, fakeStreamKey2);
blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data")); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
...@@ -162,7 +162,7 @@ public class DownloadManagerDashTest { ...@@ -162,7 +162,7 @@ public class DownloadManagerDashTest {
public void handleDownloadRequest_withRequest_downloadSuccess() throws Throwable { public void handleDownloadRequest_withRequest_downloadSuccess() throws Throwable {
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
handleDownloadRequest(fakeStreamKey2); handleDownloadRequest(fakeStreamKey2);
blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data")); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
...@@ -173,23 +173,17 @@ public class DownloadManagerDashTest { ...@@ -173,23 +173,17 @@ public class DownloadManagerDashTest {
.appendReadAction(() -> handleDownloadRequest(fakeStreamKey2)) .appendReadAction(() -> handleDownloadRequest(fakeStreamKey2))
.appendReadData(TestUtil.buildTestData(5)) .appendReadData(TestUtil.buildTestData(5))
.endData(); .endData();
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data")); assertCachedData(cache, new RequestSet(fakeDataSet).useBoundedDataSpecFor("audio_init_data"));
} }
@Test @Test
public void handleRemoveAction_blockUntilTaskCompleted_noDownloadedData() throws Throwable { public void handleRemoveAction_blockUntilTaskCompleted_noDownloadedData() throws Throwable {
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
blockUntilTasksCompleteAndThrowAnyDownloadError();
handleRemoveAction(); handleRemoveAction();
downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCacheEmpty(cache); assertCacheEmpty(cache);
} }
...@@ -197,9 +191,7 @@ public class DownloadManagerDashTest { ...@@ -197,9 +191,7 @@ public class DownloadManagerDashTest {
public void handleRemoveAction_beforeDownloadFinish_noDownloadedData() throws Throwable { public void handleRemoveAction_beforeDownloadFinish_noDownloadedData() throws Throwable {
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
handleRemoveAction(); handleRemoveAction();
downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCacheEmpty(cache); assertCacheEmpty(cache);
} }
...@@ -211,23 +203,15 @@ public class DownloadManagerDashTest { ...@@ -211,23 +203,15 @@ public class DownloadManagerDashTest {
.appendReadAction(downloadInProgressLatch::countDown) .appendReadAction(downloadInProgressLatch::countDown)
.appendReadData(TestUtil.buildTestData(5)) .appendReadData(TestUtil.buildTestData(5))
.endData(); .endData();
handleDownloadRequest(fakeStreamKey1); handleDownloadRequest(fakeStreamKey1);
assertThat(downloadInProgressLatch.await(ASSERT_TRUE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) assertThat(downloadInProgressLatch.await(ASSERT_TRUE_TIMEOUT_MS, TimeUnit.MILLISECONDS))
.isTrue(); .isTrue();
handleRemoveAction(); handleRemoveAction();
downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
blockUntilTasksCompleteAndThrowAnyDownloadError();
assertCacheEmpty(cache); assertCacheEmpty(cache);
} }
private void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable {
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError();
}
private void handleDownloadRequest(StreamKey... keys) { private void handleDownloadRequest(StreamKey... keys) {
DownloadRequest request = getDownloadRequest(keys); DownloadRequest request = getDownloadRequest(keys);
runOnMainThread(() -> downloadManager.addDownload(request)); runOnMainThread(() -> downloadManager.addDownload(request));
...@@ -262,9 +246,7 @@ public class DownloadManagerDashTest { ...@@ -262,9 +246,7 @@ public class DownloadManagerDashTest {
new DownloadManager( new DownloadManager(
ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory); ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
downloadManager.setRequirements(new Requirements(0)); downloadManager.setRequirements(new Requirements(0));
downloadManagerListener = new TestDownloadManagerListener(downloadManager);
downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread);
downloadManager.resumeDownloads(); downloadManager.resumeDownloads();
}); });
} }
......
...@@ -121,8 +121,7 @@ public class DownloadServiceDashTest { ...@@ -121,8 +121,7 @@ public class DownloadServiceDashTest {
final DownloadManager dashDownloadManager = final DownloadManager dashDownloadManager =
new DownloadManager( new DownloadManager(
ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory); ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
downloadManagerListener = downloadManagerListener = new TestDownloadManagerListener(dashDownloadManager);
new TestDownloadManagerListener(dashDownloadManager, dummyMainThread);
dashDownloadManager.resumeDownloads(); dashDownloadManager.resumeDownloads();
dashDownloadService = dashDownloadService =
...@@ -160,7 +159,7 @@ public class DownloadServiceDashTest { ...@@ -160,7 +159,7 @@ public class DownloadServiceDashTest {
downloadKeys(fakeStreamKey1); downloadKeys(fakeStreamKey1);
downloadKeys(fakeStreamKey2); downloadKeys(fakeStreamKey2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertCachedData(cache, fakeDataSet); assertCachedData(cache, fakeDataSet);
} }
...@@ -170,11 +169,11 @@ public class DownloadServiceDashTest { ...@@ -170,11 +169,11 @@ public class DownloadServiceDashTest {
public void removeAction() throws Throwable { public void removeAction() throws Throwable {
downloadKeys(fakeStreamKey1, fakeStreamKey2); downloadKeys(fakeStreamKey1, fakeStreamKey2);
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
removeAll(); removeAll();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertCacheEmpty(cache); assertCacheEmpty(cache);
} }
...@@ -187,7 +186,7 @@ public class DownloadServiceDashTest { ...@@ -187,7 +186,7 @@ public class DownloadServiceDashTest {
removeAll(); removeAll();
downloadManagerListener.blockUntilTasksCompleteAndThrowAnyDownloadError(); downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
assertCacheEmpty(cache); assertCacheEmpty(cache);
} }
...@@ -221,5 +220,4 @@ public class DownloadServiceDashTest { ...@@ -221,5 +220,4 @@ public class DownloadServiceDashTest {
dashDownloadService.onStartCommand(startIntent, 0, 0); dashDownloadService.onStartCommand(startIntent, 0, 0);
}); });
} }
} }
...@@ -18,55 +18,87 @@ package com.google.android.exoplayer2.testutil; ...@@ -18,55 +18,87 @@ package com.google.android.exoplayer2.testutil;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.os.Handler;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.offline.Download; import com.google.android.exoplayer2.offline.Download;
import com.google.android.exoplayer2.offline.Download.State;
import com.google.android.exoplayer2.offline.DownloadManager; import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.util.ConditionVariable; import com.google.android.exoplayer2.util.ConditionVariable;
import java.util.HashMap; import java.util.HashMap;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** A {@link DownloadManager.Listener} for testing. */ /**
* Allows tests to block for, and assert properties of, calls from a {@link DownloadManager} to its
* {@link DownloadManager.Listener}.
*/
public final class TestDownloadManagerListener implements DownloadManager.Listener { public final class TestDownloadManagerListener implements DownloadManager.Listener {
private static final int TIMEOUT_MS = 10_000; private static final int TIMEOUT_MS = 10_000;
private static final int STATE_REMOVED = -1; private static final int STATE_REMOVED = -1;
private final DownloadManager downloadManager; private final DownloadManager downloadManager;
private final DummyMainThread dummyMainThread; private final HashMap<String, LinkedBlockingQueue<Integer>> downloadStates;
private final HashMap<String, ArrayBlockingQueue<Integer>> downloadStates;
private final ConditionVariable initializedCondition; private final ConditionVariable initializedCondition;
private final ConditionVariable idleCondition; private final ConditionVariable idleCondition;
@Download.FailureReason private int failureReason; @Download.FailureReason private int failureReason;
public TestDownloadManagerListener( public TestDownloadManagerListener(DownloadManager downloadManager) {
DownloadManager downloadManager, DummyMainThread dummyMainThread) {
this.downloadManager = downloadManager; this.downloadManager = downloadManager;
this.dummyMainThread = dummyMainThread;
downloadStates = new HashMap<>(); downloadStates = new HashMap<>();
initializedCondition = TestUtil.createRobolectricConditionVariable(); initializedCondition = TestUtil.createRobolectricConditionVariable();
idleCondition = TestUtil.createRobolectricConditionVariable(); idleCondition = TestUtil.createRobolectricConditionVariable();
downloadManager.addListener(this); downloadManager.addListener(this);
} }
@Nullable /** Blocks until the manager is initialized. */
public Integer pollStateChange(String taskId, long timeoutMs) throws InterruptedException { public void blockUntilInitialized() throws InterruptedException {
return getStateQueue(taskId).poll(timeoutMs, TimeUnit.MILLISECONDS); assertThat(initializedCondition.block(TIMEOUT_MS)).isTrue();
} }
@Override /** Blocks until the manager is idle. */
public void onInitialized(DownloadManager downloadManager) { public void blockUntilIdle() throws InterruptedException {
initializedCondition.open(); idleCondition.close();
// If the manager is already idle the condition will be opened by the code immediately below.
// Else it will be opened by onIdle().
ConditionVariable checkedOnMainThread = TestUtil.createRobolectricConditionVariable();
new Handler(downloadManager.getApplicationLooper())
.post(
() -> {
if (downloadManager.isIdle()) {
idleCondition.open();
checkedOnMainThread.open();
}
});
checkedOnMainThread.block(TIMEOUT_MS);
assertThat(idleCondition.block(TIMEOUT_MS)).isTrue();
} }
public void blockUntilInitialized() throws InterruptedException { /** Blocks until the manager is idle and throws if any of the downloads failed. */
if (!downloadManager.isInitialized()) { public void blockUntilIdleAndThrowAnyFailure() throws Exception {
assertThat(initializedCondition.block(TIMEOUT_MS)).isTrue(); blockUntilIdle();
if (failureReason != Download.FAILURE_REASON_NONE) {
throw new Exception("Failure reason: " + failureReason);
} }
} }
/** Asserts that the specified download transitions to the specified state. */
public void assertState(String id, @Download.State int state) {
assertStateInternal(id, state);
}
/** Asserts that the specified download is removed. */
public void assertRemoved(String id) {
assertStateInternal(id, STATE_REMOVED);
}
// DownloadManager.Listener implementation.
@Override
public void onInitialized(DownloadManager downloadManager) {
initializedCondition.open();
}
@Override @Override
public void onDownloadChanged(DownloadManager downloadManager, Download download) { public void onDownloadChanged(DownloadManager downloadManager, Download download) {
if (download.state == Download.STATE_FAILED) { if (download.state == Download.STATE_FAILED) {
...@@ -81,57 +113,17 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -81,57 +113,17 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
} }
@Override @Override
public synchronized void onIdle(DownloadManager downloadManager) { public void onIdle(DownloadManager downloadManager) {
idleCondition.open(); idleCondition.open();
} }
/** // Internal logic.
* Blocks until all remove and download tasks are complete and throws an exception if there was an
* error.
*/
public void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable {
blockUntilTasksComplete();
if (failureReason != Download.FAILURE_REASON_NONE) {
throw new Exception("Failure reason: " + failureReason);
}
}
/** Blocks until all remove and download tasks are complete. Task errors are ignored. */
public void blockUntilTasksComplete() throws InterruptedException {
idleCondition.close();
dummyMainThread.runOnMainThread(
() -> {
if (downloadManager.isIdle()) {
idleCondition.open();
}
});
assertThat(idleCondition.block(TIMEOUT_MS)).isTrue();
}
private ArrayBlockingQueue<Integer> getStateQueue(String taskId) {
synchronized (downloadStates) {
@Nullable ArrayBlockingQueue<Integer> stateQueue = downloadStates.get(taskId);
if (stateQueue == null) {
stateQueue = new ArrayBlockingQueue<>(10);
downloadStates.put(taskId, stateQueue);
}
return stateQueue;
}
}
public void assertRemoved(String taskId, int timeoutMs) { private void assertStateInternal(String id, int expectedState) {
assertStateInternal(taskId, STATE_REMOVED, timeoutMs);
}
public void assertState(String taskId, @State int expectedState, int timeoutMs) {
assertStateInternal(taskId, expectedState, timeoutMs);
}
private void assertStateInternal(String taskId, int expectedState, int timeoutMs) {
while (true) { while (true) {
@Nullable Integer state = null; @Nullable Integer state = null;
try { try {
state = pollStateChange(taskId, timeoutMs); state = getStateQueue(id).poll(TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) { } catch (InterruptedException e) {
fail("Interrupted: " + e.getMessage()); fail("Interrupted: " + e.getMessage());
} }
...@@ -144,4 +136,15 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen ...@@ -144,4 +136,15 @@ public final class TestDownloadManagerListener implements DownloadManager.Listen
} }
} }
} }
private LinkedBlockingQueue<Integer> getStateQueue(String id) {
synchronized (downloadStates) {
@Nullable LinkedBlockingQueue<Integer> stateQueue = downloadStates.get(id);
if (stateQueue == null) {
stateQueue = new LinkedBlockingQueue<>();
downloadStates.put(id, stateQueue);
}
return stateQueue;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment