From d4d1fd64b3fb455fb7a9a6064d209e3410c4bcc9 Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 3 May 2018 21:01:15 -0700 Subject: [PATCH] Clean up DownloadManagerTest ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=195364299 --- .../offline/DownloadManagerTest.java | 76 +---------- .../dash/offline/DownloadManagerDashTest.java | 1 + .../dash/offline/DownloadServiceDashTest.java | 127 +++++++++--------- .../TestDownloadManagerListener.java | 29 +++- 4 files changed, 94 insertions(+), 139 deletions(-) rename {library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline => testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil}/TestDownloadManagerListener.java (71%) 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 515725a6fd..76f9ea362a 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 @@ -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 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 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 { 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 28257db03d..d022e1a855 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 @@ -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; diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java index 4eaac98ec6..cf6737e602 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java @@ -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(); } diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/TestDownloadManagerListener.java b/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java similarity index 71% rename from library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/TestDownloadManagerListener.java rename to testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java index f173901418..7e843ae983 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/TestDownloadManagerListener.java +++ b/testutils_robolectric/src/main/java/com/google/android/exoplayer2/testutil/TestDownloadManagerListener.java @@ -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> 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 getStateQueue(DownloadAction action) { + synchronized (actionStates) { + if (!actionStates.containsKey(action)) { + actionStates.put(action, new ArrayBlockingQueue(10)); + } + return actionStates.get(action); + } + } }