diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2023-05-05 23:09:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-05 23:09:03 +0200 |
commit | 6d7bfef8a5fe8180f13904739996bb2b8de8a0d4 (patch) | |
tree | 84f246b74fe7254678788e9f206d81d1a30ffa5e /core/src/test/java/de/danoeh | |
parent | 4c286931cd2dbd9038022f808f9d8a73ccbb6759 (diff) | |
download | AntennaPod-6d7bfef8a5fe8180f13904739996bb2b8de8a0d4.zip |
Download Service Rewrite (#6420)
Diffstat (limited to 'core/src/test/java/de/danoeh')
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java | 59 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java | 95 |
2 files changed, 3 insertions, 151 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java index d66bd2360..6a7e51bac 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java @@ -13,7 +13,6 @@ import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Date; import java.util.List; @@ -26,7 +25,6 @@ import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.PlaybackPreferences; import de.danoeh.antennapod.storage.preferences.UserPreferences; -import static de.danoeh.antennapod.core.util.FeedItemUtil.getIdList; import static java.util.Collections.singletonList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -255,63 +253,6 @@ public class DbTasksTest { } } - @Test - public void testAddQueueItemsInDownload_EnqueueEnabled() throws Exception { - // Setup test data / environment - UserPreferences.setEnqueueDownloadedEpisodes(true); - UserPreferences.setEnqueueLocation(UserPreferences.EnqueueLocation.BACK); - - List<FeedItem> fis1 = createSavedFeed("Feed 1", 2).getItems(); - List<FeedItem> fis2 = createSavedFeed("Feed 2", 3).getItems(); - - DBWriter.addQueueItem(context, fis1.get(0), fis2.get(0)).get(); - // the first item fis1.get(0) is already in the queue - FeedItem[] itemsToDownload = new FeedItem[]{ fis1.get(0), fis1.get(1), fis2.get(2), fis2.get(1) }; - - // Expectations: - List<FeedItem> expectedEnqueued = Arrays.asList(fis1.get(1), fis2.get(2), fis2.get(1)); - List<FeedItem> expectedQueue = new ArrayList<>(); - expectedQueue.addAll(DBReader.getQueue()); - expectedQueue.addAll(expectedEnqueued); - - // Run actual test and assert results - List<? extends FeedItem> actualEnqueued = - DBTasks.enqueueFeedItemsToDownload(context, Arrays.asList(itemsToDownload)); - - assertEqualsByIds("Only items not in the queue are enqueued", expectedEnqueued, actualEnqueued); - assertEqualsByIds("Queue has new items appended", expectedQueue, DBReader.getQueue()); - } - - @Test - public void testAddQueueItemsInDownload_EnqueueDisabled() throws Exception { - // Setup test data / environment - UserPreferences.setEnqueueDownloadedEpisodes(false); - - List<FeedItem> fis1 = createSavedFeed("Feed 1", 2).getItems(); - List<FeedItem> fis2 = createSavedFeed("Feed 2", 3).getItems(); - - DBWriter.addQueueItem(context, fis1.get(0), fis2.get(0)).get(); - FeedItem[] itemsToDownload = new FeedItem[]{ fis1.get(0), fis1.get(1), fis2.get(2), fis2.get(1) }; - - // Expectations: - List<FeedItem> expectedEnqueued = Collections.emptyList(); - List<FeedItem> expectedQueue = DBReader.getQueue(); - - // Run actual test and assert results - List<? extends FeedItem> actualEnqueued = - DBTasks.enqueueFeedItemsToDownload(context, Arrays.asList(itemsToDownload)); - - assertEqualsByIds("No item is enqueued", expectedEnqueued, actualEnqueued); - assertEqualsByIds("Queue is unchanged", expectedQueue, DBReader.getQueue()); - } - - private void assertEqualsByIds(String msg, List<? extends FeedItem> expected, List<? extends FeedItem> actual) { - // assert only the IDs, so that any differences are easily to spot. - List<Long> expectedIds = getIdList(expected); - List<Long> actualIds = getIdList(actual); - assertEquals(msg, expectedIds, actualIds); - } - private Feed createSavedFeed(String title, int numFeedItems) { final Feed feed = new Feed("url", null, title); diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java index 376e0e65c..2594fabf6 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java @@ -1,7 +1,8 @@ package de.danoeh.antennapod.core.storage; -import de.danoeh.antennapod.core.service.download.DownloadService; import de.danoeh.antennapod.model.playback.RemoteMedia; +import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface; +import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterfaceStub; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -21,8 +22,6 @@ import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMother; import de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation; import de.danoeh.antennapod.model.playback.Playable; -import org.mockito.MockedStatic; -import org.mockito.Mockito; import static de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation.AFTER_CURRENTLY_PLAYING; import static de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation.BACK; @@ -74,6 +73,7 @@ public class ItemEnqueuePositionCalculatorTest { */ @Test public void test() { + DownloadServiceInterface.setImpl(new DownloadServiceInterfaceStub()); ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); // shallow copy to which the test will add items @@ -128,95 +128,6 @@ public class ItemEnqueuePositionCalculatorTest { } - @RunWith(Parameterized.class) - public static class PreserveDownloadOrderTest { - /** - * The test covers the use case that when user initiates multiple downloads in succession, - * resulting in multiple addQueueItem() calls in succession. - * the items in the queue will be in the same order as the order user taps to download - */ - @Parameters(name = "{index}: case<{0}>") - public static Iterable<Object[]> data() { - // Attempts to make test more readable by showing the expected list of ids - // (rather than the expected positions) - return Arrays.asList(new Object[][] { - {"download order test, enqueue default", - concat(QUEUE_DEFAULT_IDS, 101L), - concat(QUEUE_DEFAULT_IDS, list(101L, 102L)), - concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 103L)), - BACK, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NULL}, - {"download order test, enqueue at front (currently playing has no effect)", - concat(101L, QUEUE_DEFAULT_IDS), - concat(list(101L, 102L), QUEUE_DEFAULT_IDS), - concat(list(101L, 103L, 102L), QUEUE_DEFAULT_IDS), - // ^ 103 is put ahead of 102, after 102 failed. - // It is a limitation as the logic can't tell 102 download has failed - // (as opposed to simply being enqueued) - FRONT, QUEUE_DEFAULT, 11L}, // 11 is at the front, currently playing - {"download order test, enqueue after currently playing", - list(11L, 101L, 12L, 13L, 14L), - list(11L, 101L, 102L, 12L, 13L, 14L), - list(11L, 101L, 103L, 102L, 12L, 13L, 14L), - AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 11L} // 11 is at the front, currently playing - }); - } - - @Parameter - public String message; - - @Parameter(1) - public List<Long> idsExpectedAfter101; - - @Parameter(2) - public List<Long> idsExpectedAfter102; - - @Parameter(3) - public List<Long> idsExpectedAfter103; - - @Parameter(4) - public EnqueueLocation options; - - @Parameter(5) - public List<FeedItem> queueInitial; - - @Parameter(6) - public long idCurrentlyPlaying; - - @Test - public void testQueueOrderWhenDownloading2Items() { - ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); - try (MockedStatic<DownloadService> downloadServiceMock = Mockito.mockStatic(DownloadService.class)) { - List<FeedItem> queue = new ArrayList<>(queueInitial); - - // Test body - Playable currentlyPlaying = getCurrentlyPlaying(idCurrentlyPlaying); - // User clicks download on feed item 101 - FeedItem feedItem101 = createFeedItem(101); - downloadServiceMock.when(() -> - DownloadService.isDownloadingFile(feedItem101.getMedia().getDownload_url())).thenReturn(true); - doAddToQueueAndAssertResult(message + " (1st download)", - calculator, feedItem101, queue, currentlyPlaying, idsExpectedAfter101); - // Then user clicks download on feed item 102 - FeedItem feedItem102 = createFeedItem(102); - downloadServiceMock.when(() -> - DownloadService.isDownloadingFile(feedItem102.getMedia().getDownload_url())).thenReturn(true); - doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", - calculator, feedItem102, queue, currentlyPlaying, idsExpectedAfter102); - // simulate download failure case for 102 - downloadServiceMock.when(() -> - DownloadService.isDownloadingFile(feedItem102.getMedia().getDownload_url())).thenReturn(false); - // Then user clicks download on feed item 103 - FeedItem feedItem103 = createFeedItem(103); - downloadServiceMock.when(() -> - DownloadService.isDownloadingFile(feedItem103.getMedia().getDownload_url())).thenReturn(true); - doAddToQueueAndAssertResult(message - + " (3rd download, with 2nd download failed; " - + "it should be behind 1st download (unless enqueueLocation is BACK)", - calculator, feedItem103, queue, currentlyPlaying, idsExpectedAfter103); - } - } - } - static void doAddToQueueAndAssertResult(String message, ItemEnqueuePositionCalculator calculator, FeedItem itemToAdd, |