diff options
author | Max Bechtold <max.bechtold@andrena.de> | 2019-12-21 11:12:39 +0100 |
---|---|---|
committer | Max Bechtold <max.bechtold@andrena.de> | 2019-12-21 11:52:01 +0100 |
commit | 9ca5cab24678873881709c5c554c1ce54c5db220 (patch) | |
tree | f97dd7474a03a5ad6155aa95400b9e152f6d9d09 /core/src/test/java/de/danoeh/antennapod | |
parent | 87cca61dcd8337facf695f03f1330c55bfa85168 (diff) | |
parent | 46731178b4f0590edf7bcb14e5dcec02f2e98f57 (diff) | |
download | AntennaPod-9ca5cab24678873881709c5c554c1ce54c5db220.zip |
Merge remote-tracking branch 'upstream/develop' into feat/simple-adjust-volume-per-feed
Diffstat (limited to 'core/src/test/java/de/danoeh/antennapod')
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java | 13 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java | 2 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java | 24 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java | 293 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java | 30 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java (renamed from core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java) | 78 | ||||
-rw-r--r-- | core/src/test/java/de/danoeh/antennapod/core/util/FeedItemUtilTest.java | 73 |
7 files changed, 499 insertions, 14 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java index 857827219..6bd753561 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java @@ -3,6 +3,9 @@ package de.danoeh.antennapod.core.feed; import org.junit.Before; import org.junit.Test; +import java.text.SimpleDateFormat; +import java.util.Date; + import static de.danoeh.antennapod.core.feed.FeedItemMother.anyFeedItemWithImage; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -40,6 +43,16 @@ public class FeedItemTest { assertFeedItemImageWasUpdated(); } + @Test + public void testUpdateFromOther_dateChanged() throws Exception { + Date originalDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("1952-03-11 00:00:00"); + Date changedDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("1952-03-11 00:42:42"); + original.setPubDate(originalDate); + changedFeedItem.setPubDate(changedDate); + original.updateFromOther(changedFeedItem); + assertEquals(changedDate.getTime(), original.getPubDate().getTime()); + } + /** * Test that a played item loses that state after being marked as new. */ diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java index f46797d28..991495a3f 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java @@ -1,6 +1,6 @@ package de.danoeh.antennapod.core.feed; -class FeedMother { +public class FeedMother { public static final String IMAGE_URL = "http://example.com/image"; public static Feed anyFeed() { diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java index 4717041f4..88b342850 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java @@ -3,10 +3,13 @@ package de.danoeh.antennapod.core.feed; import org.junit.Before; import org.junit.Test; +import de.danoeh.antennapod.core.util.SortOrder; + import static de.danoeh.antennapod.core.feed.FeedMother.anyFeed; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; public class FeedTest { @@ -59,6 +62,27 @@ public class FeedTest { feedImageWasUpdated(); } + @Test + public void testSetSortOrder_OnlyIntraFeedSortAllowed() throws Exception { + for (SortOrder sortOrder : SortOrder.values()) { + if (sortOrder.scope == SortOrder.Scope.INTRA_FEED) { + original.setSortOrder(sortOrder); // should be okay + } else { + try { + original.setSortOrder(sortOrder); + fail("SortOrder " + sortOrder + " should not be allowed on a feed"); + } catch (IllegalArgumentException iae) { + // expected exception + } + } + } + } + + @Test + public void testSetSortOrder_NullAllowed() throws Exception { + original.setSortOrder(null); // should be okay + } + private void feedHasNotChanged() { assertFalse(original.compareWithOther(changedFeed)); } 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 new file mode 100644 index 000000000..17b88bdd2 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java @@ -0,0 +1,293 @@ +package de.danoeh.antennapod.core.storage; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameter; +import org.junit.runners.Parameterized.Parameters; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.feed.FeedMother; +import de.danoeh.antennapod.core.feed.MediaType; +import de.danoeh.antennapod.core.preferences.UserPreferences.EnqueueLocation; +import de.danoeh.antennapod.core.util.playback.ExternalMedia; +import de.danoeh.antennapod.core.util.playback.Playable; + +import static de.danoeh.antennapod.core.preferences.UserPreferences.EnqueueLocation.AFTER_CURRENTLY_PLAYING; +import static de.danoeh.antennapod.core.preferences.UserPreferences.EnqueueLocation.BACK; +import static de.danoeh.antennapod.core.preferences.UserPreferences.EnqueueLocation.FRONT; +import static de.danoeh.antennapod.core.util.CollectionTestUtil.concat; +import static de.danoeh.antennapod.core.util.CollectionTestUtil.list; +import static de.danoeh.antennapod.core.util.FeedItemUtil.getIdList; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.stub; + +public class ItemEnqueuePositionCalculatorTest { + + @RunWith(Parameterized.class) + public static class BasicTest { + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"case default, i.e., add to the end", + concat(QUEUE_DEFAULT_IDS, TFI_ID), + BACK, QUEUE_DEFAULT}, + {"case option enqueue at front", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + FRONT, QUEUE_DEFAULT}, + {"case empty queue, option default", + list(TFI_ID), + BACK, QUEUE_EMPTY}, + {"case empty queue, option enqueue at front", + list(TFI_ID), + FRONT, QUEUE_EMPTY}, + }); + } + + @Parameter + public String message; + + @Parameter(1) + public List<Long> idsExpected; + + @Parameter(2) + public EnqueueLocation options; + + @Parameter(3) + public List<FeedItem> curQueue; + + public static final long TFI_ID = 101; + + /** + * Add a FeedItem with ID {@link #TFI_ID} with the setup + */ + @Test + public void test() { + ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); + + // shallow copy to which the test will add items + List<FeedItem> queue = new ArrayList<>(curQueue); + FeedItem tFI = createFeedItem(TFI_ID); + doAddToQueueAndAssertResult(message, + calculator, tFI, queue, getCurrentlyPlaying(), + idsExpected); + } + + Playable getCurrentlyPlaying() { return null; } + } + + @RunWith(Parameterized.class) + public static class AfterCurrentlyPlayingTest extends BasicTest { + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable<Object[]> data() { + return Arrays.asList(new Object[][]{ + {"case option after currently playing", + list(11L, TFI_ID, 12L, 13L, 14L), + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 11L}, + {"case option after currently playing, currently playing in the middle of the queue", + list(11L, 12L, 13L, TFI_ID, 14L), + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 13L}, + {"case option after currently playing, currently playing is not in queue", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 99L}, + {"case option after currently playing, no currentlyPlaying is null", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NULL}, + {"case option after currently playing, currentlyPlaying is externalMedia", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA}, + {"case empty queue, option after currently playing", + list(TFI_ID), + AFTER_CURRENTLY_PLAYING, QUEUE_EMPTY, ID_CURRENTLY_PLAYING_NULL}, + }); + } + + @Parameter(4) + public long idCurrentlyPlaying; + + @Override + Playable getCurrentlyPlaying() { + return ItemEnqueuePositionCalculatorTest.getCurrentlyPlaying(idCurrentlyPlaying); + } + + private static Playable externalMedia() { + return new ExternalMedia("http://example.com/episode.mp3", MediaType.AUDIO); + } + + private static final long ID_CURRENTLY_PLAYING_NULL = -1L; + private static final long ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA = -9999L; + + } + + @RunWith(Parameterized.class) + public static class ItemEnqueuePositionCalculatorPreserveDownloadOrderTest { + + /** + * 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 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() { + + // Setup class under test + // + ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); + DownloadStateProvider stubDownloadStateProvider = mock(DownloadStateProvider.class); + stub(stubDownloadStateProvider.isDownloadingFile(any(FeedMedia.class))).toReturn(false); + calculator.downloadStateProvider = stubDownloadStateProvider; + + // Setup initial data + // A shallow copy, as the test code will manipulate the queue + List<FeedItem> queue = new ArrayList<>(queueInitial); + + // Test body + Playable currentlyPlaying = getCurrentlyPlaying(idCurrentlyPlaying); + // User clicks download on feed item 101 + FeedItem tFI101 = setAsDownloading(101, stubDownloadStateProvider, true); + doAddToQueueAndAssertResult(message + " (1st download)", + calculator, tFI101, queue, currentlyPlaying, + idsExpectedAfter101); + // Then user clicks download on feed item 102 + FeedItem tFI102 = setAsDownloading(102, stubDownloadStateProvider, true); + doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", + calculator, tFI102, queue, currentlyPlaying, + idsExpectedAfter102); + // simulate download failure case for 102 + setAsDownloading(tFI102, stubDownloadStateProvider, false); + // Then user clicks download on feed item 103 + FeedItem tFI103 = setAsDownloading(103, stubDownloadStateProvider, true); + doAddToQueueAndAssertResult(message + + " (3rd download, with 2nd download failed; " + + "it should be behind 1st download (unless enqueueLocation is BACK)", + calculator, tFI103, queue, currentlyPlaying, + idsExpectedAfter103); + + } + + + private static FeedItem setAsDownloading(int id, DownloadStateProvider stubDownloadStateProvider, + boolean isDownloading) { + FeedItem item = createFeedItem(id); + FeedMedia media = + new FeedMedia(item, "http://download.url.net/" + id + , 100000 + id, "audio/mp3"); + media.setId(item.getId()); + item.setMedia(media); + return setAsDownloading(item, stubDownloadStateProvider, isDownloading); + } + + private static FeedItem setAsDownloading(FeedItem item, DownloadStateProvider stubDownloadStateProvider, + boolean isDownloading) { + stub(stubDownloadStateProvider.isDownloadingFile(item.getMedia())).toReturn(isDownloading); + return item; + } + + } + + + static void doAddToQueueAndAssertResult(String message, + ItemEnqueuePositionCalculator calculator, + FeedItem itemToAdd, + List<FeedItem> queue, + Playable currentlyPlaying, + List<Long> idsExpected) { + int posActual = calculator.calcPosition(queue, currentlyPlaying); + queue.add(posActual, itemToAdd); + assertEquals(message, idsExpected, getIdList(queue)); + } + + static final List<FeedItem> QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); + + static final List<FeedItem> QUEUE_DEFAULT = + Collections.unmodifiableList(Arrays.asList( + createFeedItem(11), createFeedItem(12), createFeedItem(13), createFeedItem(14))); + static final List<Long> QUEUE_DEFAULT_IDS = + QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); + + + static Playable getCurrentlyPlaying(long idCurrentlyPlaying) { + if (ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA == idCurrentlyPlaying) { + return externalMedia(); + } + if (ID_CURRENTLY_PLAYING_NULL == idCurrentlyPlaying) { + return null; + } + return createFeedItem(idCurrentlyPlaying).getMedia(); + } + + static Playable externalMedia() { + return new ExternalMedia("http://example.com/episode.mp3", MediaType.AUDIO); + } + + static final long ID_CURRENTLY_PLAYING_NULL = -1L; + static final long ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA = -9999L; + + + static FeedItem createFeedItem(long id) { + FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", + new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); + FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); + media.setId(item.getId()); + item.setMedia(media); + return item; + } + +} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java b/core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java new file mode 100644 index 000000000..21f1ef5d4 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java @@ -0,0 +1,30 @@ +package de.danoeh.antennapod.core.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class CollectionTestUtil { + + public static <T> List<? extends T> concat(T item, List<? extends T> list) { + List<T> res = new ArrayList<>(list); + res.add(0, item); + return res; + } + + public static <T> List<? extends T> concat(List<? extends T> list, T item) { + List<T> res = new ArrayList<>(list); + res.add(item); + return res; + } + + public static <T> List<? extends T> concat(List<? extends T> list1, List<? extends T> list2) { + List<T> res = new ArrayList<>(list1); + res.addAll(list2); + return res; + } + + public static <T> List<T> list(T... a) { + return Arrays.asList(a); + } +} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java index 791b6a75b..ccaa77ae8 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java @@ -10,22 +10,25 @@ import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.assertNull; /** - * Test class for QueueSorter. + * Test class for FeedItemPermutors. */ -public class QueueSorterTest { +public class FeedItemPermutorsTest { @Test - public void testPermutorForRule_null() { - assertNull(QueueSorter.getPermutor(null)); + public void testEnsureNonNullPermutors() { + for (SortOrder sortOrder : SortOrder.values()) { + assertNotNull("The permutor for SortOrder " + sortOrder + " is unexpectedly null", + FeedItemPermutors.getPermutor(sortOrder)); + } } @Test public void testPermutorForRule_EPISODE_TITLE_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.EPISODE_TITLE_A_Z); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -34,8 +37,21 @@ public class QueueSorterTest { } @Test + public void testPermutorForRule_EPISODE_TITLE_ASC_NullTitle() { + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); + + List<FeedItem> itemList = getTestList(); + itemList.get(2) // itemId 2 + .setTitle(null); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting + } + + + @Test public void testPermutorForRule_EPISODE_TITLE_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.EPISODE_TITLE_Z_A); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_Z_A); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -45,7 +61,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DATE_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DATE_OLD_NEW); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -54,8 +70,20 @@ public class QueueSorterTest { } @Test + public void testPermutorForRule_DATE_ASC_NulPubDatel() { + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); + + List<FeedItem> itemList = getTestList(); + itemList.get(2) // itemId 2 + .setPubDate(null); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting + } + + @Test public void testPermutorForRule_DATE_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DATE_NEW_OLD); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_NEW_OLD); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -65,7 +93,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DURATION_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DURATION_SHORT_LONG); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_SHORT_LONG); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -75,7 +103,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_DURATION_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.DURATION_LONG_SHORT); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -84,8 +112,20 @@ public class QueueSorterTest { } @Test + public void testPermutorForRule_DURATION_DESC_NullMedia() { + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); + + List<FeedItem> itemList = getTestList(); + itemList.get(1) // itemId 3 + .setMedia(null); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting + } + + @Test public void testPermutorForRule_FEED_TITLE_ASC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.FEED_TITLE_A_Z); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_A_Z); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -95,7 +135,7 @@ public class QueueSorterTest { @Test public void testPermutorForRule_FEED_TITLE_DESC() { - Permutor<FeedItem> permutor = QueueSorter.getPermutor(SortOrder.FEED_TITLE_Z_A); + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); List<FeedItem> itemList = getTestList(); assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting @@ -103,6 +143,18 @@ public class QueueSorterTest { assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting } + @Test + public void testPermutorForRule_FEED_TITLE_DESC_NullTitle() { + Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); + + List<FeedItem> itemList = getTestList(); + itemList.get(1) // itemId 3 + .getFeed().setTitle(null); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting + } + /** * Generates a list with test data. */ diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemUtilTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemUtilTest.java new file mode 100644 index 000000000..0b64cb10f --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemUtilTest.java @@ -0,0 +1,73 @@ +package de.danoeh.antennapod.core.util; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +import java.util.Arrays; +import java.util.Collection; + +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedItem; + +import static org.junit.Assert.assertEquals; + +public class FeedItemUtilTest { + + + @RunWith(Parameterized.class) + public static class LinkWithFallbackTest { + private static final String FEED_LINK = "http://example.com"; + private static final String ITEM_LINK = "http://example.com/feedItem1"; + + @Parameters + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][] { + { "average", + FEED_LINK, ITEM_LINK, ITEM_LINK }, + { "null item link - fallback to feed", + FEED_LINK, null, FEED_LINK}, + { "empty item link - same as null", + FEED_LINK, "", FEED_LINK}, + { "blank item link - same as null", + FEED_LINK, " ", FEED_LINK}, + { "fallback, but feed link is null too", + null, null, null }, + { "fallback - but empty feed link - same as null", + "", null, null}, + { "fallback - but blank feed link - same as null", + " ", null, null} + }); + } + + private final String msg; + private final String feedLink; + private final String itemLink; + private final String expected; + + public LinkWithFallbackTest(String msg, String feedLink, String itemLink, String expected) { + this.msg = msg; + this.feedLink = feedLink; + this.itemLink = itemLink; + this.expected = expected; + } + + + @Test + public void testLinkWithFallback() { + String actual = FeedItemUtil.getLinkWithFallback(createFeedItem(feedLink, itemLink)); + assertEquals(msg, expected, actual); + } + + private static FeedItem createFeedItem(String feedLink, String itemLink) { + Feed feed = new Feed(); + feed.setLink(feedLink); + FeedItem feedItem = new FeedItem(); + feedItem.setLink(itemLink); + feedItem.setFeed(feed); + feed.setItems(Arrays.asList(feedItem)); + return feedItem; + } + } +} |