Clean up DownloadManagerTest

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195364299
This commit is contained in:
olly 2018-05-03 21:01:15 -07:00 committed by Oliver Woodman
parent 08e56394c5
commit d4d1fd64b3
4 changed files with 94 additions and 139 deletions

View File

@ -19,22 +19,18 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.net.Uri; import android.net.Uri;
import android.os.ConditionVariable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState; import com.google.android.exoplayer2.offline.DownloadManager.TaskState;
import com.google.android.exoplayer2.offline.DownloadManager.TaskState.State; import com.google.android.exoplayer2.offline.DownloadManager.TaskState.State;
import com.google.android.exoplayer2.testutil.DummyMainThread; import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.RobolectricUtil; 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.DummyDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; 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.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.After; import org.junit.After;
@ -77,7 +73,6 @@ public class DownloadManagerTest {
uri3 = Uri.parse("http://abc.com/media3"); uri3 = Uri.parse("http://abc.com/media3");
dummyMainThread = new DummyMainThread(); dummyMainThread = new DummyMainThread();
actionFile = Util.createTempFile(RuntimeEnvironment.application, "ExoPlayerTest"); actionFile = Util.createTempFile(RuntimeEnvironment.application, "ExoPlayerTest");
downloadManagerListener = new TestDownloadManagerListener();
setUpDownloadManager(100); setUpDownloadManager(100);
} }
@ -437,6 +432,8 @@ public class DownloadManagerTest {
MIN_RETRY_COUNT, MIN_RETRY_COUNT,
actionFile, actionFile,
ProgressiveDownloadAction.DESERIALIZER); ProgressiveDownloadAction.DESERIALIZER);
downloadManagerListener =
new TestDownloadManagerListener(downloadManager, dummyMainThread);
downloadManager.addListener(downloadManagerListener); downloadManager.addListener(downloadManagerListener);
downloadManager.startDownloads(); downloadManager.startDownloads();
} }
@ -498,50 +495,13 @@ public class DownloadManagerTest {
dummyMainThread.runOnMainThread(r); 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 class FakeDownloadAction extends DownloadAction {
private final FakeDownloader downloader; private final FakeDownloader downloader;
private final BlockingQueue<Integer> states;
private FakeDownloadAction(Uri uri, boolean isRemoveAction) { private FakeDownloadAction(Uri uri, boolean isRemoveAction) {
super("Fake", /* version= */ 0, uri, isRemoveAction, /* data= */ null); super("Fake", /* version= */ 0, uri, isRemoveAction, /* data= */ null);
this.downloader = new FakeDownloader(isRemoveAction); this.downloader = new FakeDownloader(isRemoveAction);
this.states = new ArrayBlockingQueue<>(10);
} }
@Override @Override
@ -558,7 +518,7 @@ public class DownloadManagerTest {
return downloader; return downloader;
} }
private FakeDownloadAction post() throws Throwable { private FakeDownloadAction post() {
runOnMainThread( runOnMainThread(
new Runnable() { new Runnable() {
@Override @Override
@ -597,33 +557,15 @@ public class DownloadManagerTest {
} }
private FakeDownloadAction assertState(@State int expectedState) { private FakeDownloadAction assertState(@State int expectedState) {
ArrayList<Integer> receivedStates = new ArrayList<>();
while (true) { while (true) {
Integer state = null; Integer state = null;
try { try {
state = states.poll(ASSERT_TRUE_TIMEOUT, TimeUnit.MILLISECONDS); state = downloadManagerListener.pollStateChange(this, ASSERT_TRUE_TIMEOUT);
} catch (InterruptedException e) { } catch (InterruptedException e) {
fail(e.getMessage()); fail(e.getMessage());
} }
if (state != null) { if (expectedState == state) {
if (expectedState == state) { return this;
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));
} }
} }
} }
@ -637,10 +579,6 @@ public class DownloadManagerTest {
downloader.ignoreInterrupts = true; downloader.ignoreInterrupts = true;
return this; return this;
} }
private void onStateChange(int state) {
states.add(state);
}
} }
private static class FakeDownloader implements Downloader { private static class FakeDownloader implements Downloader {

View File

@ -31,6 +31,7 @@ import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
import com.google.android.exoplayer2.testutil.FakeDataSource; import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.RobolectricUtil; 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.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.DataSource.Factory; import com.google.android.exoplayer2.upstream.DataSource.Factory;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor; import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;

View File

@ -36,6 +36,7 @@ import com.google.android.exoplayer2.testutil.DummyMainThread;
import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSet;
import com.google.android.exoplayer2.testutil.FakeDataSource; import com.google.android.exoplayer2.testutil.FakeDataSource;
import com.google.android.exoplayer2.testutil.RobolectricUtil; 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.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor; import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
@ -73,7 +74,7 @@ public class DownloadServiceDashTest {
private DummyMainThread dummyMainThread; private DummyMainThread dummyMainThread;
@Before @Before
public void setUp() throws Exception { public void setUp() throws IOException {
dummyMainThread = new DummyMainThread(); dummyMainThread = new DummyMainThread();
context = RuntimeEnvironment.application; context = RuntimeEnvironment.application;
tempFolder = Util.createTempDirectory(context, "ExoPlayerTest"); tempFolder = Util.createTempDirectory(context, "ExoPlayerTest");
@ -110,76 +111,68 @@ public class DownloadServiceDashTest {
fakeRepresentationKey1 = new RepresentationKey(0, 0, 0); fakeRepresentationKey1 = new RepresentationKey(0, 0, 0);
fakeRepresentationKey2 = new RepresentationKey(0, 1, 0); fakeRepresentationKey2 = new RepresentationKey(0, 1, 0);
try { dummyMainThread.runOnMainThread(
dummyMainThread.runOnMainThread( new Runnable() {
new Runnable() { @Override
@Override public void run() {
public void run() { File actionFile;
File actionFile; try {
try { actionFile = Util.createTempFile(context, "ExoPlayerTest");
actionFile = Util.createTempFile(context, "ExoPlayerTest"); } catch (IOException e) {
} catch (IOException e) { throw new RuntimeException(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();
} }
}); actionFile.delete();
} catch (Throwable throwable) { final DownloadManager dashDownloadManager =
throw new Exception(throwable); 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 @After
public void tearDown() throws Exception { public void tearDown() {
try { dummyMainThread.runOnMainThread(
dummyMainThread.runOnMainThread( new Runnable() {
new Runnable() { @Override
@Override public void run() {
public void run() { dashDownloadService.onDestroy();
dashDownloadService.onDestroy(); }
} });
});
} catch (Throwable throwable) {
throw new Exception(throwable);
}
Util.recursiveDelete(tempFolder); Util.recursiveDelete(tempFolder);
dummyMainThread.release(); dummyMainThread.release();
} }

View File

@ -13,22 +13,26 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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 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.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.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** A {@link DownloadManager.Listener} for testing. */ /** 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 static final int TIMEOUT = 1000;
private final DownloadManager downloadManager; private final DownloadManager downloadManager;
private final DummyMainThread dummyMainThread; private final DummyMainThread dummyMainThread;
private final HashMap<DownloadAction, ArrayBlockingQueue<Integer>> actionStates;
private CountDownLatch downloadFinishedCondition; private CountDownLatch downloadFinishedCondition;
private Throwable downloadError; private Throwable downloadError;
@ -36,6 +40,15 @@ import java.util.concurrent.TimeUnit;
DownloadManager downloadManager, DummyMainThread dummyMainThread) { DownloadManager downloadManager, DummyMainThread dummyMainThread) {
this.downloadManager = downloadManager; this.downloadManager = downloadManager;
this.dummyMainThread = dummyMainThread; 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 @Override
@ -44,6 +57,7 @@ import java.util.concurrent.TimeUnit;
if (taskState.state == DownloadManager.TaskState.STATE_FAILED && downloadError == null) { if (taskState.state == DownloadManager.TaskState.STATE_FAILED && downloadError == null) {
downloadError = taskState.error; downloadError = taskState.error;
} }
getStateQueue(taskState.action).add(taskState.state);
} }
@Override @Override
@ -75,4 +89,13 @@ import java.util.concurrent.TimeUnit;
throw new Exception(downloadError); 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);
}
}
} }