diff options
author | orionlee <orionlee@yahoo.com> | 2019-10-26 20:03:41 -0700 |
---|---|---|
committer | orionlee <orionlee@yahoo.com> | 2019-11-06 14:03:26 -0800 |
commit | dc6221fb8250edfd937ff39edffa141c8205fb5b (patch) | |
tree | 3f3a137a4de70935585c6bdb73500dcb3c5d6d3c /app/src/androidTest/java/de/test/antennapod | |
parent | a6e5cd144d0734a81f37a69e1301901b473cb6b5 (diff) | |
download | AntennaPod-dc6221fb8250edfd937ff39edffa141c8205fb5b.zip |
respect download order - dequeue upon cancelling download
Diffstat (limited to 'app/src/androidTest/java/de/test/antennapod')
-rw-r--r-- | app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java index 93723bc78..d0e14d70a 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java @@ -5,7 +5,6 @@ import androidx.annotation.Nullable; import androidx.test.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4; -import de.danoeh.antennapod.core.service.download.DownloaderFactory; import org.awaitility.Awaitility; import org.awaitility.core.ConditionTimeoutException; import org.junit.After; @@ -27,6 +26,7 @@ import de.danoeh.antennapod.core.service.download.DownloadRequest; import de.danoeh.antennapod.core.service.download.DownloadService; import de.danoeh.antennapod.core.service.download.DownloadStatus; import de.danoeh.antennapod.core.service.download.Downloader; +import de.danoeh.antennapod.core.service.download.DownloaderFactory; import de.danoeh.antennapod.core.service.download.StubDownloader; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; @@ -59,7 +59,9 @@ public class DownloadServiceTest { } private Feed setUpTestFeeds() throws Exception { - Feed feed = new Feed("url", null, "Test Feed title 1"); + // To avoid complication in case of test failures, leaving behind orphaned + // media files: add a timestamp so that each test run will have its own directory for media files. + Feed feed = new Feed("url", null, "Test Feed title 1 " + System.currentTimeMillis()); List<FeedItem> items = new ArrayList<>(); feed.setItems(items); FeedItem item1 = new FeedItem(0, "Item 1-1", "Item 1-1", "url", new Date(), FeedItem.NEW, feed); @@ -123,6 +125,70 @@ public class DownloadServiceTest { }); } + @Test + public void testCancelDownload_UndoEnqueue_Normal() throws Exception { + doTestCancelDownload_UndoEnqueue(false); + } + + @Test + public void testCancelDownload_UndoEnqueue_AlreadyInQueue() throws Exception { + doTestCancelDownload_UndoEnqueue(true); + } + + private void doTestCancelDownload_UndoEnqueue(boolean itemAlreadyInQueue) throws Exception { + // let download takes longer to ensure the test can cancel the download in time + DownloadService.setDownloaderFactory(new StubDownloaderFactory(150, downloadStatus -> { + downloadStatus.setSuccessful(); + })); + UserPreferences.setEnqueueDownloadedEpisodes(true); + UserPreferences.setEnableAutodownload(false); + + final long item1Id = testMedia11.getItem().getId(); + if (itemAlreadyInQueue) { + // simulate item already in queue condition + DBWriter.addQueueItem(InstrumentationRegistry.getTargetContext(), false, item1Id).get(); + assertTrue(DBReader.getQueueIDList().contains(item1Id)); + } else { + assertFalse(DBReader.getQueueIDList().contains(item1Id)); + } + + withFeedItemEventListener(feedItemEventListener -> { + try { + DownloadRequester.getInstance().downloadMedia(false, InstrumentationRegistry.getTargetContext(), + testMedia11.getItem()); + if (itemAlreadyInQueue) { + Awaitility.await("download service receives the request - " + + "no event is expected before cancel is issued") + .atLeast(100, TimeUnit.MILLISECONDS) + .until(() -> true); + } else { + Awaitility.await("item enqueue event") + .atMost(1000, TimeUnit.MILLISECONDS) + .until(() -> feedItemEventListener.getEvents().size() >= 1); + } + DownloadRequester.getInstance().cancelDownload(InstrumentationRegistry.getTargetContext(), + testMedia11); + final int totalNumEventsExpected = itemAlreadyInQueue ? 1 : 3; + Awaitility.await("item dequeue event + download termination event") + .atMost(1000, TimeUnit.MILLISECONDS) + .until(() ->feedItemEventListener.getEvents().size() >= totalNumEventsExpected); + assertFalse("The download should have been canceled", + DBReader.getFeedMedia(testMedia11.getId()).isDownloaded()); + if (itemAlreadyInQueue) { + assertTrue("The FeedItem should still be in the queue after the download is cancelled." + + " It's there before download.", + DBReader.getQueueIDList().contains(item1Id)); + } else { + assertFalse("The FeedItem should not be in the queue after the download is cancelled.", + DBReader.getQueueIDList().contains(item1Id)); + } + } catch (ConditionTimeoutException cte) { + fail("The expected FeedItemEvent (for media download complete) has not been posted. " + + cte.getMessage()); + } + }); + } + private static class StubDownloaderFactory implements DownloaderFactory { private final long downloadTime; |