Clean up DownloadManagerTest
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=195364299
This commit is contained in:
parent
08e56394c5
commit
d4d1fd64b3
@ -19,22 +19,18 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.net.Uri;
|
||||
import android.os.ConditionVariable;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager.TaskState.State;
|
||||
import com.google.android.exoplayer2.testutil.DummyMainThread;
|
||||
import com.google.android.exoplayer2.testutil.RobolectricUtil;
|
||||
import com.google.android.exoplayer2.testutil.TestDownloadManagerListener;
|
||||
import com.google.android.exoplayer2.upstream.DummyDataSource;
|
||||
import com.google.android.exoplayer2.upstream.cache.Cache;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.After;
|
||||
@ -77,7 +73,6 @@ public class DownloadManagerTest {
|
||||
uri3 = Uri.parse("http://abc.com/media3");
|
||||
dummyMainThread = new DummyMainThread();
|
||||
actionFile = Util.createTempFile(RuntimeEnvironment.application, "ExoPlayerTest");
|
||||
downloadManagerListener = new TestDownloadManagerListener();
|
||||
setUpDownloadManager(100);
|
||||
}
|
||||
|
||||
@ -437,6 +432,8 @@ public class DownloadManagerTest {
|
||||
MIN_RETRY_COUNT,
|
||||
actionFile,
|
||||
ProgressiveDownloadAction.DESERIALIZER);
|
||||
downloadManagerListener =
|
||||
new TestDownloadManagerListener(downloadManager, dummyMainThread);
|
||||
downloadManager.addListener(downloadManagerListener);
|
||||
downloadManager.startDownloads();
|
||||
}
|
||||
@ -498,50 +495,13 @@ public class DownloadManagerTest {
|
||||
dummyMainThread.runOnMainThread(r);
|
||||
}
|
||||
|
||||
private static final class TestDownloadManagerListener implements DownloadManager.Listener {
|
||||
|
||||
private ConditionVariable downloadFinishedCondition;
|
||||
private Throwable downloadError;
|
||||
|
||||
private TestDownloadManagerListener() {
|
||||
downloadFinishedCondition = new ConditionVariable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskStateChanged(DownloadManager downloadManager, TaskState taskState) {
|
||||
if (taskState.state == TaskState.STATE_FAILED && downloadError == null) {
|
||||
downloadError = taskState.error;
|
||||
}
|
||||
((FakeDownloadAction) taskState.action).onStateChange(taskState.state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIdle(DownloadManager downloadManager) {
|
||||
downloadFinishedCondition.open();
|
||||
}
|
||||
|
||||
private void clearDownloadError() {
|
||||
this.downloadError = null;
|
||||
}
|
||||
|
||||
private void blockUntilTasksCompleteAndThrowAnyDownloadError() throws Throwable {
|
||||
assertThat(downloadFinishedCondition.block(ASSERT_TRUE_TIMEOUT)).isTrue();
|
||||
downloadFinishedCondition.close();
|
||||
if (downloadError != null) {
|
||||
throw new Exception(downloadError);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class FakeDownloadAction extends DownloadAction {
|
||||
|
||||
private final FakeDownloader downloader;
|
||||
private final BlockingQueue<Integer> states;
|
||||
|
||||
private FakeDownloadAction(Uri uri, boolean isRemoveAction) {
|
||||
super("Fake", /* version= */ 0, uri, isRemoveAction, /* data= */ null);
|
||||
this.downloader = new FakeDownloader(isRemoveAction);
|
||||
this.states = new ArrayBlockingQueue<>(10);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -558,7 +518,7 @@ public class DownloadManagerTest {
|
||||
return downloader;
|
||||
}
|
||||
|
||||
private FakeDownloadAction post() throws Throwable {
|
||||
private FakeDownloadAction post() {
|
||||
runOnMainThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
@ -597,33 +557,15 @@ public class DownloadManagerTest {
|
||||
}
|
||||
|
||||
private FakeDownloadAction assertState(@State int expectedState) {
|
||||
ArrayList<Integer> receivedStates = new ArrayList<>();
|
||||
while (true) {
|
||||
Integer state = null;
|
||||
try {
|
||||
state = states.poll(ASSERT_TRUE_TIMEOUT, TimeUnit.MILLISECONDS);
|
||||
state = downloadManagerListener.pollStateChange(this, ASSERT_TRUE_TIMEOUT);
|
||||
} catch (InterruptedException e) {
|
||||
fail(e.getMessage());
|
||||
}
|
||||
if (state != null) {
|
||||
if (expectedState == state) {
|
||||
return this;
|
||||
}
|
||||
receivedStates.add(state);
|
||||
} else {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < receivedStates.size(); i++) {
|
||||
if (i > 0) {
|
||||
sb.append(',');
|
||||
}
|
||||
sb.append(TaskState.getStateString(receivedStates.get(i)));
|
||||
}
|
||||
fail(
|
||||
String.format(
|
||||
Locale.US,
|
||||
"expected:<%s> but was:<%s>",
|
||||
TaskState.getStateString(expectedState),
|
||||
sb));
|
||||
if (expectedState == state) {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -637,10 +579,6 @@ public class DownloadManagerTest {
|
||||
downloader.ignoreInterrupts = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
private void onStateChange(int state) {
|
||||
states.add(state);
|
||||
}
|
||||
}
|
||||
|
||||
private static class FakeDownloader implements Downloader {
|
||||
|
@ -31,6 +31,7 @@ import com.google.android.exoplayer2.testutil.DummyMainThread;
|
||||
import com.google.android.exoplayer2.testutil.FakeDataSet;
|
||||
import com.google.android.exoplayer2.testutil.FakeDataSource;
|
||||
import com.google.android.exoplayer2.testutil.RobolectricUtil;
|
||||
import com.google.android.exoplayer2.testutil.TestDownloadManagerListener;
|
||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||
import com.google.android.exoplayer2.upstream.DataSource.Factory;
|
||||
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
|
||||
|
@ -36,6 +36,7 @@ import com.google.android.exoplayer2.testutil.DummyMainThread;
|
||||
import com.google.android.exoplayer2.testutil.FakeDataSet;
|
||||
import com.google.android.exoplayer2.testutil.FakeDataSource;
|
||||
import com.google.android.exoplayer2.testutil.RobolectricUtil;
|
||||
import com.google.android.exoplayer2.testutil.TestDownloadManagerListener;
|
||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
|
||||
@ -73,7 +74,7 @@ public class DownloadServiceDashTest {
|
||||
private DummyMainThread dummyMainThread;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
public void setUp() throws IOException {
|
||||
dummyMainThread = new DummyMainThread();
|
||||
context = RuntimeEnvironment.application;
|
||||
tempFolder = Util.createTempDirectory(context, "ExoPlayerTest");
|
||||
@ -110,76 +111,68 @@ public class DownloadServiceDashTest {
|
||||
fakeRepresentationKey1 = new RepresentationKey(0, 0, 0);
|
||||
fakeRepresentationKey2 = new RepresentationKey(0, 1, 0);
|
||||
|
||||
try {
|
||||
dummyMainThread.runOnMainThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
File actionFile;
|
||||
try {
|
||||
actionFile = Util.createTempFile(context, "ExoPlayerTest");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
actionFile.delete();
|
||||
final DownloadManager dashDownloadManager =
|
||||
new DownloadManager(
|
||||
new DownloaderConstructorHelper(cache, fakeDataSourceFactory),
|
||||
1,
|
||||
3,
|
||||
actionFile,
|
||||
DashDownloadAction.DESERIALIZER);
|
||||
downloadManagerListener =
|
||||
new TestDownloadManagerListener(dashDownloadManager, dummyMainThread);
|
||||
dashDownloadManager.addListener(downloadManagerListener);
|
||||
dashDownloadManager.startDownloads();
|
||||
|
||||
dashDownloadService =
|
||||
new DownloadService(/*foregroundNotificationId=*/ 1) {
|
||||
|
||||
@Override
|
||||
protected DownloadManager getDownloadManager() {
|
||||
return dashDownloadManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Notification getForegroundNotification(TaskState[] taskStates) {
|
||||
return Mockito.mock(Notification.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected Scheduler getScheduler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected Requirements getRequirements() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
dashDownloadService.onCreate();
|
||||
dummyMainThread.runOnMainThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
File actionFile;
|
||||
try {
|
||||
actionFile = Util.createTempFile(context, "ExoPlayerTest");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
} catch (Throwable throwable) {
|
||||
throw new Exception(throwable);
|
||||
}
|
||||
actionFile.delete();
|
||||
final DownloadManager dashDownloadManager =
|
||||
new DownloadManager(
|
||||
new DownloaderConstructorHelper(cache, fakeDataSourceFactory),
|
||||
1,
|
||||
3,
|
||||
actionFile,
|
||||
DashDownloadAction.DESERIALIZER);
|
||||
downloadManagerListener =
|
||||
new TestDownloadManagerListener(dashDownloadManager, dummyMainThread);
|
||||
dashDownloadManager.addListener(downloadManagerListener);
|
||||
dashDownloadManager.startDownloads();
|
||||
|
||||
dashDownloadService =
|
||||
new DownloadService(/*foregroundNotificationId=*/ 1) {
|
||||
|
||||
@Override
|
||||
protected DownloadManager getDownloadManager() {
|
||||
return dashDownloadManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Notification getForegroundNotification(TaskState[] taskStates) {
|
||||
return Mockito.mock(Notification.class);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected Scheduler getScheduler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
protected Requirements getRequirements() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
dashDownloadService.onCreate();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
try {
|
||||
dummyMainThread.runOnMainThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
dashDownloadService.onDestroy();
|
||||
}
|
||||
});
|
||||
} catch (Throwable throwable) {
|
||||
throw new Exception(throwable);
|
||||
}
|
||||
public void tearDown() {
|
||||
dummyMainThread.runOnMainThread(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
dashDownloadService.onDestroy();
|
||||
}
|
||||
});
|
||||
Util.recursiveDelete(tempFolder);
|
||||
dummyMainThread.release();
|
||||
}
|
||||
|
@ -13,22 +13,26 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.google.android.exoplayer2.source.dash.offline;
|
||||
package com.google.android.exoplayer2.testutil;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import com.google.android.exoplayer2.offline.DownloadAction;
|
||||
import com.google.android.exoplayer2.offline.DownloadManager;
|
||||
import com.google.android.exoplayer2.testutil.DummyMainThread;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/** A {@link DownloadManager.Listener} for testing. */
|
||||
/* package */ final class TestDownloadManagerListener implements DownloadManager.Listener {
|
||||
public final class TestDownloadManagerListener implements DownloadManager.Listener {
|
||||
|
||||
private static final int TIMEOUT = 1000;
|
||||
|
||||
private final DownloadManager downloadManager;
|
||||
private final DummyMainThread dummyMainThread;
|
||||
private final HashMap<DownloadAction, ArrayBlockingQueue<Integer>> actionStates;
|
||||
|
||||
private CountDownLatch downloadFinishedCondition;
|
||||
private Throwable downloadError;
|
||||
|
||||
@ -36,6 +40,15 @@ import java.util.concurrent.TimeUnit;
|
||||
DownloadManager downloadManager, DummyMainThread dummyMainThread) {
|
||||
this.downloadManager = downloadManager;
|
||||
this.dummyMainThread = dummyMainThread;
|
||||
actionStates = new HashMap<>();
|
||||
}
|
||||
|
||||
public int pollStateChange(DownloadAction action, long timeoutMs) throws InterruptedException {
|
||||
return getStateQueue(action).poll(timeoutMs, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
public void clearDownloadError() {
|
||||
this.downloadError = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -44,6 +57,7 @@ import java.util.concurrent.TimeUnit;
|
||||
if (taskState.state == DownloadManager.TaskState.STATE_FAILED && downloadError == null) {
|
||||
downloadError = taskState.error;
|
||||
}
|
||||
getStateQueue(taskState.action).add(taskState.state);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -75,4 +89,13 @@ import java.util.concurrent.TimeUnit;
|
||||
throw new Exception(downloadError);
|
||||
}
|
||||
}
|
||||
|
||||
private ArrayBlockingQueue<Integer> getStateQueue(DownloadAction action) {
|
||||
synchronized (actionStates) {
|
||||
if (!actionStates.containsKey(action)) {
|
||||
actionStates.put(action, new ArrayBlockingQueue<Integer>(10));
|
||||
}
|
||||
return actionStates.get(action);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user