From 250594827dc9d6aef785e93fb48b6a18e907adcf Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 17 Oct 2019 13:54:58 -0700 Subject: unit test for get feeditem link with fallback --- .../antennapod/core/util/FeedItemUtilTest.java | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 core/src/test/java/de/danoeh/antennapod/core/util/FeedItemUtilTest.java (limited to 'core/src/test/java') 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 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; + } + } +} -- cgit v1.2.3 From 306b6f30a4c416445886af33954fac20dbc29f22 Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 24 Oct 2019 11:32:06 -0700 Subject: rename + refactor QueueSorter to FeedItemPermutors, to support both queue and podcast screen. --- .../core/util/FeedItemPermutorsTest.java | 150 ++++++++++++++++++++ .../antennapod/core/util/QueueSorterTest.java | 156 --------------------- 2 files changed, 150 insertions(+), 156 deletions(-) create mode 100644 core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java delete mode 100644 core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java new file mode 100644 index 000000000..82b3a794e --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java @@ -0,0 +1,150 @@ +package de.danoeh.antennapod.core.util; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.List; + +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.assertTrue; + +/** + * Test class for FeedItemPermutors. + */ +public class FeedItemPermutorsTest { + + @Test + public void testPermutorForRule_EPISODE_TITLE_ASC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting + } + + @Test + public void testPermutorForRule_EPISODE_TITLE_DESC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_Z_A); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting + } + + @Test + public void testPermutorForRule_DATE_ASC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting + } + + @Test + public void testPermutorForRule_DATE_DESC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_NEW_OLD); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting + } + + @Test + public void testPermutorForRule_DURATION_ASC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_SHORT_LONG); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting + } + + @Test + public void testPermutorForRule_DURATION_DESC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting + } + + @Test + public void testPermutorForRule_FEED_TITLE_ASC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_A_Z); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting + } + + @Test + public void testPermutorForRule_FEED_TITLE_DESC() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); + + List itemList = getTestList(); + assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting + permutor.reorder(itemList); + assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting + } + + /** + * Generates a list with test data. + */ + private List getTestList() { + List itemList = new ArrayList<>(); + + Calendar calendar = Calendar.getInstance(); + calendar.set(2019, 0, 1); // January 1st + Feed feed1 = new Feed(null, null, "Feed title 1"); + FeedItem feedItem1 = new FeedItem(1, "Title 1", null, null, calendar.getTime(), 0, feed1); + FeedMedia feedMedia1 = new FeedMedia(0, feedItem1, 1000, 0, 0, null, null, null, true, null, 0, 0); + feedItem1.setMedia(feedMedia1); + itemList.add(feedItem1); + + calendar.set(2019, 2, 1); // March 1st + Feed feed2 = new Feed(null, null, "Feed title 3"); + FeedItem feedItem2 = new FeedItem(3, "Title 3", null, null, calendar.getTime(), 0, feed2); + FeedMedia feedMedia2 = new FeedMedia(0, feedItem2, 3000, 0, 0, null, null, null, true, null, 0, 0); + feedItem2.setMedia(feedMedia2); + itemList.add(feedItem2); + + calendar.set(2019, 1, 1); // February 1st + Feed feed3 = new Feed(null, null, "Feed title 2"); + FeedItem feedItem3 = new FeedItem(2, "Title 2", null, null, calendar.getTime(), 0, feed3); + FeedMedia feedMedia3 = new FeedMedia(0, feedItem3, 2000, 0, 0, null, null, null, true, null, 0, 0); + feedItem3.setMedia(feedMedia3); + itemList.add(feedItem3); + + return itemList; + } + + /** + * Checks if both lists have the same size and the same ID order. + * + * @param itemList Item list. + * @param ids List of IDs. + * @return true if both lists have the same size and the same ID order. + */ + private boolean checkIdOrder(List itemList, long... ids) { + if (itemList.size() != ids.length) { + return false; + } + + for (int i = 0; i < ids.length; i++) { + if (itemList.get(i).getId() != ids[i]) { + return false; + } + } + return true; + } +} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java deleted file mode 100644 index 791b6a75b..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/QueueSorterTest.java +++ /dev/null @@ -1,156 +0,0 @@ -package de.danoeh.antennapod.core.util; - -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; - -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.assertTrue; -import static org.junit.Assert.assertNull; - -/** - * Test class for QueueSorter. - */ -public class QueueSorterTest { - - @Test - public void testPermutorForRule_null() { - assertNull(QueueSorter.getPermutor(null)); - } - - @Test - public void testPermutorForRule_EPISODE_TITLE_ASC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.EPISODE_TITLE_A_Z); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_EPISODE_TITLE_DESC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.EPISODE_TITLE_Z_A); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_DATE_ASC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.DATE_OLD_NEW); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_DATE_DESC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.DATE_NEW_OLD); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_DURATION_ASC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.DURATION_SHORT_LONG); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_DURATION_DESC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.DURATION_LONG_SHORT); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_FEED_TITLE_ASC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.FEED_TITLE_A_Z); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_FEED_TITLE_DESC() { - Permutor permutor = QueueSorter.getPermutor(SortOrder.FEED_TITLE_Z_A); - - List itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - /** - * Generates a list with test data. - */ - private List getTestList() { - List itemList = new ArrayList<>(); - - Calendar calendar = Calendar.getInstance(); - calendar.set(2019, 0, 1); // January 1st - Feed feed1 = new Feed(null, null, "Feed title 1"); - FeedItem feedItem1 = new FeedItem(1, "Title 1", null, null, calendar.getTime(), 0, feed1); - FeedMedia feedMedia1 = new FeedMedia(0, feedItem1, 1000, 0, 0, null, null, null, true, null, 0, 0); - feedItem1.setMedia(feedMedia1); - itemList.add(feedItem1); - - calendar.set(2019, 2, 1); // March 1st - Feed feed2 = new Feed(null, null, "Feed title 3"); - FeedItem feedItem2 = new FeedItem(3, "Title 3", null, null, calendar.getTime(), 0, feed2); - FeedMedia feedMedia2 = new FeedMedia(0, feedItem2, 3000, 0, 0, null, null, null, true, null, 0, 0); - feedItem2.setMedia(feedMedia2); - itemList.add(feedItem2); - - calendar.set(2019, 1, 1); // February 1st - Feed feed3 = new Feed(null, null, "Feed title 2"); - FeedItem feedItem3 = new FeedItem(2, "Title 2", null, null, calendar.getTime(), 0, feed3); - FeedMedia feedMedia3 = new FeedMedia(0, feedItem3, 2000, 0, 0, null, null, null, true, null, 0, 0); - feedItem3.setMedia(feedMedia3); - itemList.add(feedItem3); - - return itemList; - } - - /** - * Checks if both lists have the same size and the same ID order. - * - * @param itemList Item list. - * @param ids List of IDs. - * @return true if both lists have the same size and the same ID order. - */ - private boolean checkIdOrder(List itemList, long... ids) { - if (itemList.size() != ids.length) { - return false; - } - - for (int i = 0; i < ids.length; i++) { - if (itemList.get(i).getId() != ids[i]) { - return false; - } - } - return true; - } -} -- cgit v1.2.3 From bcd4b49dda0a75bb61396ee4c4ab5521c5dc0c26 Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 24 Oct 2019 12:00:14 -0700 Subject: feeditem sort - handle boundary conditions --- .../core/util/FeedItemPermutorsTest.java | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java index 82b3a794e..f66c77c3d 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java @@ -10,6 +10,7 @@ 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; /** @@ -17,6 +18,14 @@ import static org.junit.Assert.assertTrue; */ public class FeedItemPermutorsTest { + @Test + 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 permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); @@ -27,6 +36,19 @@ public class FeedItemPermutorsTest { assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting } + @Test + public void testPermutorForRule_EPISODE_TITLE_ASC_NullTitle() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); + + List 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 permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_Z_A); @@ -47,6 +69,18 @@ public class FeedItemPermutorsTest { assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting } + @Test + public void testPermutorForRule_DATE_ASC_NulPubDatel() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); + + List 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 permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_NEW_OLD); @@ -77,6 +111,18 @@ public class FeedItemPermutorsTest { assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting } + @Test + public void testPermutorForRule_DURATION_DESC_NullMedia() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); + + List 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 permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_A_Z); @@ -97,6 +143,18 @@ public class FeedItemPermutorsTest { assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting } + @Test + public void testPermutorForRule_FEED_TITLE_DESC_NullTitle() { + Permutor permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); + + List 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. */ -- cgit v1.2.3 From 9c895ef15a19b840a227a605814f94d787e5b508 Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 24 Oct 2019 12:17:31 -0700 Subject: Test to ensure Feed only accepts INTRA_FEED sort order --- .../de/danoeh/antennapod/core/feed/FeedTest.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'core/src/test/java') 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)); } -- cgit v1.2.3 From 7687ffb08e19968600a58f3994a82995316d27b6 Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 24 Oct 2019 13:49:32 -0700 Subject: Misc code style fixes. --- .../test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java index f66c77c3d..ccaa77ae8 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java @@ -20,7 +20,7 @@ public class FeedItemPermutorsTest { @Test public void testEnsureNonNullPermutors() { - for(SortOrder sortOrder : SortOrder.values()) { + for (SortOrder sortOrder : SortOrder.values()) { assertNotNull("The permutor for SortOrder " + sortOrder + " is unexpectedly null", FeedItemPermutors.getPermutor(sortOrder)); } -- cgit v1.2.3 From ba27ec6b31314bbcfebf55e38a913d5b10757e83 Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 18 May 2018 14:47:37 -0700 Subject: refactor - DBWriter.addQueueItem() : refactor enqueue position calculation to be a unit-testable component (static inner class) --- .../de/danoeh/antennapod/core/feed/FeedMother.java | 2 +- .../antennapod/core/storage/DBWriterTest.java | 65 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java (limited to 'core/src/test/java') 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/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java new file mode 100644 index 000000000..0d494534c --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -0,0 +1,65 @@ +package de.danoeh.antennapod.core.storage; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMother; +import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator; +import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator.Options; + +import static org.junit.Assert.assertEquals; + +public class DBWriterTest { + + public static class ItemEnqueuePositionCalculatorTest { + + @Test + public void testEnqueueDefault() { + + ItemEnqueuePositionCalculator calculator = + new ItemEnqueuePositionCalculator(new Options()); + + List curQueue = tQueue(); + + int posActual1 = calculator.calcPosition(0, tFI(101), curQueue); + assertEquals("case default, i.e., add to the end", curQueue.size(), posActual1); + + int posActual2 = calculator.calcPosition(1, tFI(102), curQueue); + assertEquals("case default (2nd item)", curQueue.size(), posActual2); + + } + + @Test + public void testEnqueueAtFront() { + + ItemEnqueuePositionCalculator calculator = + new ItemEnqueuePositionCalculator(new Options() + .setEnqueueAtFront(true)); + + List curQueue = tQueue(); + + int posActual1 = calculator.calcPosition(0, tFI(101), curQueue); + assertEquals("case option enqueue at front", 0, posActual1); + + int posActual2 = calculator.calcPosition(1, tFI(102), curQueue); + assertEquals("case option enqueue at front (2nd item)", 1, posActual2); + + } + + private static List tQueue() { + return Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)); + } + + private static FeedItem tFI(int id) { + FeedItem item = new FeedItem(0, "Item" + id, "ItemId" + id, "url", + new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); + return item; + } + + } + +} \ No newline at end of file -- cgit v1.2.3 From bfde3c731514f1a761710d634c39a80ad75bdf10 Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 18 May 2018 15:20:21 -0700 Subject: refactor - DBWriterTest: parametrize the set of tests --- .../antennapod/core/storage/DBWriterTest.java | 58 ++++++++++++---------- 1 file changed, 31 insertions(+), 27 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index 0d494534c..d6ccbba3a 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -1,6 +1,10 @@ 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.Arrays; import java.util.Date; @@ -15,43 +19,43 @@ import static org.junit.Assert.assertEquals; public class DBWriterTest { + @RunWith(Parameterized.class) public static class ItemEnqueuePositionCalculatorTest { - @Test - public void testEnqueueDefault() { - - ItemEnqueuePositionCalculator calculator = - new ItemEnqueuePositionCalculator(new Options()); - - List curQueue = tQueue(); - - int posActual1 = calculator.calcPosition(0, tFI(101), curQueue); - assertEquals("case default, i.e., add to the end", curQueue.size(), posActual1); - - int posActual2 = calculator.calcPosition(1, tFI(102), curQueue); - assertEquals("case default (2nd item)", curQueue.size(), posActual2); - + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable data() { + Options optDefault = new Options(); + Options optEnqAtFront = new Options().setEnqueueAtFront(true); + + return Arrays.asList(new Object[][] { + {"case default, i.e., add to the end", QUEUE_DEFAULT.size(), optDefault , 0}, + {"case default (2nd item)", QUEUE_DEFAULT.size(), optDefault , 1}, + {"case option enqueue at front", 0, optEnqAtFront , 0}, + {"case option enqueue at front (2nd item)", 1, optEnqAtFront , 1} + }); } - @Test - public void testEnqueueAtFront() { + private static final List QUEUE_DEFAULT = Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)); - ItemEnqueuePositionCalculator calculator = - new ItemEnqueuePositionCalculator(new Options() - .setEnqueueAtFront(true)); + @Parameter + public String message; - List curQueue = tQueue(); + @Parameter(1) + public int posExpected; - int posActual1 = calculator.calcPosition(0, tFI(101), curQueue); - assertEquals("case option enqueue at front", 0, posActual1); + @Parameter(2) + public Options options; - int posActual2 = calculator.calcPosition(1, tFI(102), curQueue); - assertEquals("case option enqueue at front (2nd item)", 1, posActual2); + @Parameter(3) + public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. - } - private static List tQueue() { - return Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)); + @Test + public void test() { + ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); + + int posActual = calculator.calcPosition(posAmongAdded, tFI(101), QUEUE_DEFAULT); + assertEquals(message, posExpected , posActual); } private static FeedItem tFI(int id) { -- cgit v1.2.3 From 30f104f40b6ac9de0d0095c2723ec893215cdd15 Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 18 May 2018 20:44:39 -0700 Subject: #2652 (part of): The in-progress podcast at the front of the queue should remain at the front. --- .../antennapod/core/storage/DBWriterTest.java | 66 +++++++++++++++++++--- 1 file changed, 59 insertions(+), 7 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index d6ccbba3a..c1983ec4f 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -11,6 +11,7 @@ import java.util.Date; import java.util.List; 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.storage.DBWriter.ItemEnqueuePositionCalculator; import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator.Options; @@ -26,17 +27,50 @@ public class DBWriterTest { public static Iterable data() { Options optDefault = new Options(); Options optEnqAtFront = new Options().setEnqueueAtFront(true); + Options optKeepInProgressAtFront = + new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true); + // edge case: keep in progress without enabling enqueue at front is meaningless + Options optKeepInProgressAtFrontWithNoEnqueueAtFront = + new Options().setKeepInProgressAtFront(true); + return Arrays.asList(new Object[][] { - {"case default, i.e., add to the end", QUEUE_DEFAULT.size(), optDefault , 0}, - {"case default (2nd item)", QUEUE_DEFAULT.size(), optDefault , 1}, - {"case option enqueue at front", 0, optEnqAtFront , 0}, - {"case option enqueue at front (2nd item)", 1, optEnqAtFront , 1} + {"case default, i.e., add to the end", + QUEUE_DEFAULT.size(), optDefault , 0, QUEUE_DEFAULT}, + {"case default (2nd item)", + QUEUE_DEFAULT.size(), optDefault , 1, QUEUE_DEFAULT}, + {"case option enqueue at front", + 0, optEnqAtFront , 0, QUEUE_DEFAULT}, + {"case option enqueue at front (2nd item)", + 1, optEnqAtFront , 1, QUEUE_DEFAULT}, + {"case option keep in progress at front", + 1, optKeepInProgressAtFront , 0, QUEUE_FRONT_IN_PROGRESS}, + {"case option keep in progress at front (2nd item)", + 2, optKeepInProgressAtFront , 1, QUEUE_FRONT_IN_PROGRESS}, + {"case option keep in progress at front, front item not in progress", + 0, optKeepInProgressAtFront , 0, QUEUE_DEFAULT}, + {"case option keep in progress at front, front item no media at all", + 0, optKeepInProgressAtFront , 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception + {"case option keep in progress at front, but enqueue at front is disabled", + QUEUE_FRONT_IN_PROGRESS.size(), optKeepInProgressAtFrontWithNoEnqueueAtFront , 0, QUEUE_FRONT_IN_PROGRESS}, + {"case empty queue, option default", + 0, optDefault, 0, QUEUE_EMPTY}, + {"case empty queue, option enqueue at front", + 0, optEnqAtFront, 0, QUEUE_EMPTY}, + {"case empty queue, option keep in progress at front", + 0, optKeepInProgressAtFront, 0, QUEUE_EMPTY}, + }); } + private static final List QUEUE_EMPTY = Arrays.asList(); + private static final List QUEUE_DEFAULT = Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)); + private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); + + private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); + @Parameter public String message; @@ -49,21 +83,39 @@ public class DBWriterTest { @Parameter(3) public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. + @Parameter(4) + public List curQueue; + @Test public void test() { ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); - int posActual = calculator.calcPosition(posAmongAdded, tFI(101), QUEUE_DEFAULT); + int posActual = calculator.calcPosition(posAmongAdded, tFI(101), curQueue); assertEquals(message, posExpected , posActual); } private static FeedItem tFI(int id) { + return tFI(id, -1); + } + + private static FeedItem tFI(int id, int position) { + FeedItem item = tFINoMedia(id); + FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); + item.setMedia(media); + + if (position >= 0) { + media.setPosition(position); + } + + return item; + } + + private static FeedItem tFINoMedia(int id) { FeedItem item = new FeedItem(0, "Item" + id, "ItemId" + id, "url", new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); return item; } - } -} \ No newline at end of file +} -- cgit v1.2.3 From 0973efa9436455288acdae870d43ee7bd177f56b Mon Sep 17 00:00:00 2001 From: orionlee Date: Wed, 23 May 2018 14:22:13 -0700 Subject: refactor test - break ItemEnqueuePositionCalculatorTest to be more modular to prepare for testing more complex enqueue options. --- .../antennapod/core/storage/DBWriterTest.java | 148 ++++++++++++--------- 1 file changed, 86 insertions(+), 62 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index c1983ec4f..6d4dc98fd 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -7,6 +7,7 @@ import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -20,86 +21,108 @@ import static org.junit.Assert.assertEquals; public class DBWriterTest { - @RunWith(Parameterized.class) public static class ItemEnqueuePositionCalculatorTest { - @Parameters(name = "{index}: case<{0}>, expected:{1}") - public static Iterable data() { - Options optDefault = new Options(); - Options optEnqAtFront = new Options().setEnqueueAtFront(true); - Options optKeepInProgressAtFront = - new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true); - // edge case: keep in progress without enabling enqueue at front is meaningless - Options optKeepInProgressAtFrontWithNoEnqueueAtFront = - new Options().setKeepInProgressAtFront(true); - - - return Arrays.asList(new Object[][] { - {"case default, i.e., add to the end", - QUEUE_DEFAULT.size(), optDefault , 0, QUEUE_DEFAULT}, - {"case default (2nd item)", - QUEUE_DEFAULT.size(), optDefault , 1, QUEUE_DEFAULT}, - {"case option enqueue at front", - 0, optEnqAtFront , 0, QUEUE_DEFAULT}, - {"case option enqueue at front (2nd item)", - 1, optEnqAtFront , 1, QUEUE_DEFAULT}, - {"case option keep in progress at front", - 1, optKeepInProgressAtFront , 0, QUEUE_FRONT_IN_PROGRESS}, - {"case option keep in progress at front (2nd item)", - 2, optKeepInProgressAtFront , 1, QUEUE_FRONT_IN_PROGRESS}, - {"case option keep in progress at front, front item not in progress", - 0, optKeepInProgressAtFront , 0, QUEUE_DEFAULT}, - {"case option keep in progress at front, front item no media at all", - 0, optKeepInProgressAtFront , 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception - {"case option keep in progress at front, but enqueue at front is disabled", - QUEUE_FRONT_IN_PROGRESS.size(), optKeepInProgressAtFrontWithNoEnqueueAtFront , 0, QUEUE_FRONT_IN_PROGRESS}, - {"case empty queue, option default", - 0, optDefault, 0, QUEUE_EMPTY}, - {"case empty queue, option enqueue at front", - 0, optEnqAtFront, 0, QUEUE_EMPTY}, - {"case empty queue, option keep in progress at front", - 0, optKeepInProgressAtFront, 0, QUEUE_EMPTY}, - - }); - } + @RunWith(Parameterized.class) + public static class IEPCBasicTest { + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable data() { + Options optDefault = new Options(); + Options optEnqAtFront = new Options().setEnqueueAtFront(true); + + return Arrays.asList(new Object[][] { + {"case default, i.e., add to the end", + QUEUE_DEFAULT.size(), optDefault , 0, QUEUE_DEFAULT}, + {"case default (2nd item)", + QUEUE_DEFAULT.size(), optDefault , 1, QUEUE_DEFAULT}, + {"case option enqueue at front", + 0, optEnqAtFront , 0, QUEUE_DEFAULT}, + {"case option enqueue at front (2nd item)", + 1, optEnqAtFront , 1, QUEUE_DEFAULT}, + {"case empty queue, option default", + 0, optDefault, 0, QUEUE_EMPTY}, + {"case empty queue, option enqueue at front", + 0, optEnqAtFront, 0, QUEUE_EMPTY}, + }); + } + + @Parameter + public String message; - private static final List QUEUE_EMPTY = Arrays.asList(); + @Parameter(1) + public int posExpected; - private static final List QUEUE_DEFAULT = Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)); + @Parameter(2) + public Options options; - private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); + @Parameter(3) + public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. - private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); + @Parameter(4) + public List curQueue; - @Parameter - public String message; - @Parameter(1) - public int posExpected; + public static final int TFI_TO_ADD_ID = 101; - @Parameter(2) - public Options options; + /** + * Add a FeedItem with ID {@link #TFI_TO_ADD_ID} with the setup + */ + @Test + public void test() { + ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); - @Parameter(3) - public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. + int posActual = calculator.calcPosition(posAmongAdded, tFI(TFI_TO_ADD_ID), curQueue); + assertEquals(message, posExpected , posActual); + } + + } - @Parameter(4) - public List curQueue; + @RunWith(Parameterized.class) + public static class IEPCKeepInProgressAtFrontTest extends IEPCBasicTest { + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable data() { + Options optKeepInProgressAtFront = + new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true); + // edge case: keep in progress without enabling enqueue at front is meaningless + Options optKeepInProgressAtFrontWithNoEnqueueAtFront = + new Options().setKeepInProgressAtFront(true); + + return Arrays.asList(new Object[][]{ + {"case option keep in progress at front", + 1, optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, + {"case option keep in progress at front (2nd item)", + 2, optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, + {"case option keep in progress at front, front item not in progress", + 0, optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, + {"case option keep in progress at front, front item no media at all", + 0, optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception + {"case option keep in progress at front, but enqueue at front is disabled", + QUEUE_FRONT_IN_PROGRESS.size(), optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, + {"case empty queue, option keep in progress at front", + 0, optKeepInProgressAtFront, 0, QUEUE_EMPTY}, + }); + } + private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); - @Test - public void test() { - ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); + private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); - int posActual = calculator.calcPosition(posAmongAdded, tFI(101), curQueue); - assertEquals(message, posExpected , posActual); } - private static FeedItem tFI(int id) { + // Common helpers: + // - common queue (of items) for tests + // - construct FeedItems for tests + + static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); + + static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); + + + static FeedItem tFI(int id) { return tFI(id, -1); } - private static FeedItem tFI(int id, int position) { + static FeedItem tFI(int id, int position) { FeedItem item = tFINoMedia(id); FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); item.setMedia(media); @@ -111,11 +134,12 @@ public class DBWriterTest { return item; } - private static FeedItem tFINoMedia(int id) { + static FeedItem tFINoMedia(int id) { FeedItem item = new FeedItem(0, "Item" + id, "ItemId" + id, "url", new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); return item; } + } } -- cgit v1.2.3 From 97905e5ed4afc273e6e4165682aa820e50ac05da Mon Sep 17 00:00:00 2001 From: orionlee Date: Wed, 23 May 2018 15:37:03 -0700 Subject: #2448: make podcast episode enqueue position respect download start order --- .../antennapod/core/storage/DBWriterTest.java | 142 +++++++++++++++++++-- 1 file changed, 134 insertions(+), 8 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index 6d4dc98fd..7753f55dc 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -1,16 +1,22 @@ package de.danoeh.antennapod.core.storage; +import android.support.annotation.NonNull; + 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.HashMap; import java.util.List; +import java.util.Map; +import de.danoeh.antennapod.core.feed.FeedFile; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMother; @@ -30,15 +36,15 @@ public class DBWriterTest { Options optDefault = new Options(); Options optEnqAtFront = new Options().setEnqueueAtFront(true); - return Arrays.asList(new Object[][] { + return Arrays.asList(new Object[][]{ {"case default, i.e., add to the end", - QUEUE_DEFAULT.size(), optDefault , 0, QUEUE_DEFAULT}, + QUEUE_DEFAULT.size(), optDefault, 0, QUEUE_DEFAULT}, {"case default (2nd item)", - QUEUE_DEFAULT.size(), optDefault , 1, QUEUE_DEFAULT}, + QUEUE_DEFAULT.size(), optDefault, 1, QUEUE_DEFAULT}, {"case option enqueue at front", - 0, optEnqAtFront , 0, QUEUE_DEFAULT}, + 0, optEnqAtFront, 0, QUEUE_DEFAULT}, {"case option enqueue at front (2nd item)", - 1, optEnqAtFront , 1, QUEUE_DEFAULT}, + 1, optEnqAtFront, 1, QUEUE_DEFAULT}, {"case empty queue, option default", 0, optDefault, 0, QUEUE_EMPTY}, {"case empty queue, option enqueue at front", @@ -72,7 +78,7 @@ public class DBWriterTest { ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); int posActual = calculator.calcPosition(posAmongAdded, tFI(TFI_TO_ADD_ID), curQueue); - assertEquals(message, posExpected , posActual); + assertEquals(message, posExpected, posActual); } } @@ -109,6 +115,126 @@ public class DBWriterTest { } + @RunWith(Parameterized.class) + public static class ItemEnqueuePositionCalculatorPreserveDownloadOrderTest { + + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable data() { + Options optDefault = new Options(); + Options optEnqAtFront = new Options().setEnqueueAtFront(true); + + return Arrays.asList(new Object[][] { + {"download order test, enqueue default", + QUEUE_DEFAULT.size(), QUEUE_DEFAULT.size() + 1, + QUEUE_DEFAULT.size() + 2, QUEUE_DEFAULT.size() + 3, + optDefault, QUEUE_DEFAULT}, + {"download order test, enqueue at front", + 0, 1, + 2, 3, + optEnqAtFront, QUEUE_DEFAULT}, + }); + } + + @Parameter + public String message; + + @Parameter(1) + public int pos101Expected; + + @Parameter(2) + public int pos102Expected; + + // 2XX are for testing bulk insertion cases + @Parameter(3) + public int pos201Expected; + + @Parameter(4) + public int pos202Expected; + + @Parameter(5) + public Options options; + + @Parameter(6) + public List queueInitial; + + @Test + public void testQueueOrderWhenDownloading2Items() { + + // Setup class under test + // + ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); + MockDownloadRequester mockDownloadRequester = new MockDownloadRequester(); + calculator.requester = mockDownloadRequester; + + // Setup initial data + // A shallow copy, as the test code will manipulate the queue + List queue = new ArrayList<>(queueInitial); + + + // Test body + + // User clicks download on feed item 101 + FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); + + int pos101Actual = calculator.calcPosition(0, tFI101, queue); + queue.add(pos101Actual, tFI101); + assertEquals(message + " (1st download)", + pos101Expected, pos101Actual); + + // Then user clicks download on feed item 102 + FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); + int pos102Actual = calculator.calcPosition(0, tFI102, queue); + queue.add(pos102Actual, tFI102); + assertEquals(message + " (2nd download, it should preserve order of download)", + pos102Expected, pos102Actual); + + // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls + + FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); + int pos201Actual = calculator.calcPosition(0, tFI201, queue); + queue.add(pos201Actual, tFI201); + assertEquals(message + " (bulk insertion, 1st item)", pos201Expected, pos201Actual); + + FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); + int pos202Actual = calculator.calcPosition(1, tFI202, queue); + queue.add(pos202Actual, tFI202); + assertEquals(message + " (bulk insertion, 2nd item)", pos202Expected, pos202Actual); + + // TODO: simulate download failure cases. + } + + + private static FeedItem tFI_isDownloading(int id, MockDownloadRequester requester) { + FeedItem item = tFI(id); + FeedMedia media = + new FeedMedia(item, "http://download.url.net/" + id + , 100000 + id, "audio/mp3"); + media.setId(item.getId()); + item.setMedia(media); + + requester.mockDownloadingFile(media, true); + + return item; + } + + private static class MockDownloadRequester implements FeedFileDownloadStatusRequesterInterface { + + private Map downloadingByIds = new HashMap<>(); + + @Override + public synchronized boolean isDownloadingFile(@NonNull FeedFile item) { + return downloadingByIds.getOrDefault(item.getId(), false); + } + + // All other parent methods should not be called + + public void mockDownloadingFile(FeedFile item, boolean isDownloading) { + downloadingByIds.put(item.getId(), isDownloading); + } + } + } + + // Common helpers: // - common queue (of items) for tests // - construct FeedItems for tests @@ -125,6 +251,7 @@ public class DBWriterTest { static FeedItem tFI(int id, int position) { FeedItem item = tFINoMedia(id); FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); + media.setId(item.getId()); item.setMedia(media); if (position >= 0) { @@ -135,11 +262,10 @@ public class DBWriterTest { } static FeedItem tFINoMedia(int id) { - FeedItem item = new FeedItem(0, "Item" + id, "ItemId" + id, "url", + FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); return item; } - } } -- cgit v1.2.3 From fb824b541d4c8c63d351f968e961f2696c5610d2 Mon Sep 17 00:00:00 2001 From: orionlee Date: Wed, 23 May 2018 16:56:46 -0700 Subject: Test cases readability: change expected format from position to the actual queue (list of IDs), to make the test case more readable. --- .../antennapod/core/storage/DBWriterTest.java | 123 +++++++++++++++------ 1 file changed, 91 insertions(+), 32 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index 7753f55dc..afdfc323d 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -15,6 +15,7 @@ import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import de.danoeh.antennapod.core.feed.FeedFile; import de.danoeh.antennapod.core.feed.FeedItem; @@ -38,17 +39,23 @@ public class DBWriterTest { return Arrays.asList(new Object[][]{ {"case default, i.e., add to the end", - QUEUE_DEFAULT.size(), optDefault, 0, QUEUE_DEFAULT}, + concat(QUEUE_DEFAULT_IDS, TFI_ID), + optDefault, 0, QUEUE_DEFAULT}, {"case default (2nd item)", - QUEUE_DEFAULT.size(), optDefault, 1, QUEUE_DEFAULT}, + concat(QUEUE_DEFAULT_IDS, TFI_ID), + optDefault, 1, QUEUE_DEFAULT}, {"case option enqueue at front", - 0, optEnqAtFront, 0, QUEUE_DEFAULT}, + concat(TFI_ID, QUEUE_DEFAULT_IDS), + optEnqAtFront, 0, QUEUE_DEFAULT}, {"case option enqueue at front (2nd item)", - 1, optEnqAtFront, 1, QUEUE_DEFAULT}, + list(11L, TFI_ID, 12L, 13L, 14L), + optEnqAtFront, 1, QUEUE_DEFAULT}, {"case empty queue, option default", - 0, optDefault, 0, QUEUE_EMPTY}, + list(TFI_ID), + optDefault, 0, QUEUE_EMPTY}, {"case empty queue, option enqueue at front", - 0, optEnqAtFront, 0, QUEUE_EMPTY}, + list(TFI_ID), + optEnqAtFront, 0, QUEUE_EMPTY}, }); } @@ -56,7 +63,7 @@ public class DBWriterTest { public String message; @Parameter(1) - public int posExpected; + public List idsExpected; @Parameter(2) public Options options; @@ -68,17 +75,22 @@ public class DBWriterTest { public List curQueue; - public static final int TFI_TO_ADD_ID = 101; + public static final long TFI_ID = 101; /** - * Add a FeedItem with ID {@link #TFI_TO_ADD_ID} with the setup + * Add a FeedItem with ID {@link #TFI_ID} with the setup */ @Test public void test() { ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); - int posActual = calculator.calcPosition(posAmongAdded, tFI(TFI_TO_ADD_ID), curQueue); - assertEquals(message, posExpected, posActual); + // shallow copy to which the test will add items + List queue = new ArrayList<>(curQueue); + + FeedItem tFI = tFI(TFI_ID); + int posActual = calculator.calcPosition(posAmongAdded, tFI, queue); + queue.add(posActual, tFI); + assertEquals(message, idsExpected, toIDs(queue)); } } @@ -95,23 +107,31 @@ public class DBWriterTest { return Arrays.asList(new Object[][]{ {"case option keep in progress at front", - 1, optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, + list(11L, TFI_ID, 12L, 13L), + optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, {"case option keep in progress at front (2nd item)", - 2, optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, + list(11L, 12L, TFI_ID, 13L), + optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, {"case option keep in progress at front, front item not in progress", - 0, optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, + concat(TFI_ID, QUEUE_DEFAULT_IDS), + optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, {"case option keep in progress at front, front item no media at all", - 0, optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception + concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS), + optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception {"case option keep in progress at front, but enqueue at front is disabled", - QUEUE_FRONT_IN_PROGRESS.size(), optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, + concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID), + optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, {"case empty queue, option keep in progress at front", - 0, optKeepInProgressAtFront, 0, QUEUE_EMPTY}, + list(TFI_ID), + optKeepInProgressAtFront, 0, QUEUE_EMPTY}, }); } private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); + private static final List QUEUE_FRONT_IN_PROGRESS_IDS = toIDs(QUEUE_FRONT_IN_PROGRESS); private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); + private static final List QUEUE_FRONT_NO_MEDIA_IDS = toIDs(QUEUE_FRONT_NO_MEDIA); } @@ -123,14 +143,20 @@ public class DBWriterTest { Options optDefault = new Options(); Options optEnqAtFront = new Options().setEnqueueAtFront(true); + // 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", - QUEUE_DEFAULT.size(), QUEUE_DEFAULT.size() + 1, - QUEUE_DEFAULT.size() + 2, QUEUE_DEFAULT.size() + 3, + concat(QUEUE_DEFAULT_IDS, 101L), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L)), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L)), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)), optDefault, QUEUE_DEFAULT}, {"download order test, enqueue at front", - 0, 1, - 2, 3, + concat(101L, QUEUE_DEFAULT_IDS), + concat(list(101L, 102L), QUEUE_DEFAULT_IDS), + concat(list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), + concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), optEnqAtFront, QUEUE_DEFAULT}, }); } @@ -139,17 +165,17 @@ public class DBWriterTest { public String message; @Parameter(1) - public int pos101Expected; + public List idsExpectedAfter101; @Parameter(2) - public int pos102Expected; + public List idsExpectedAfter102; // 2XX are for testing bulk insertion cases @Parameter(3) - public int pos201Expected; + public List idsExpectedAfter201; @Parameter(4) - public int pos202Expected; + public List idsExpectedAfter202; @Parameter(5) public Options options; @@ -179,26 +205,28 @@ public class DBWriterTest { int pos101Actual = calculator.calcPosition(0, tFI101, queue); queue.add(pos101Actual, tFI101); assertEquals(message + " (1st download)", - pos101Expected, pos101Actual); + idsExpectedAfter101, toIDs(queue)); // Then user clicks download on feed item 102 FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); int pos102Actual = calculator.calcPosition(0, tFI102, queue); queue.add(pos102Actual, tFI102); assertEquals(message + " (2nd download, it should preserve order of download)", - pos102Expected, pos102Actual); + idsExpectedAfter102, toIDs(queue)); // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); int pos201Actual = calculator.calcPosition(0, tFI201, queue); queue.add(pos201Actual, tFI201); - assertEquals(message + " (bulk insertion, 1st item)", pos201Expected, pos201Actual); + assertEquals(message + " (bulk insertion, 1st item)", + idsExpectedAfter201, toIDs(queue)); FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); int pos202Actual = calculator.calcPosition(1, tFI202, queue); queue.add(pos202Actual, tFI202); - assertEquals(message + " (bulk insertion, 2nd item)", pos202Expected, pos202Actual); + assertEquals(message + " (bulk insertion, 2nd item)", + idsExpectedAfter202, toIDs(queue)); // TODO: simulate download failure cases. } @@ -242,13 +270,14 @@ public class DBWriterTest { static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); + static final List QUEUE_DEFAULT_IDS = QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); - static FeedItem tFI(int id) { + static FeedItem tFI(long id) { return tFI(id, -1); } - static FeedItem tFI(int id, int position) { + static FeedItem tFI(long id, int position) { FeedItem item = tFINoMedia(id); FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); media.setId(item.getId()); @@ -261,11 +290,41 @@ public class DBWriterTest { return item; } - static FeedItem tFINoMedia(int id) { + static FeedItem tFINoMedia(long id) { FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); return item; } + + // Collections helpers + + static List concat(T item, List list) { + List res = new ArrayList<>(list); + res.add(0, item); + return res; + } + + static List concat(List list, T item) { + List res = new ArrayList<>(list); + res.add(item); + return res; + } + + static List concat(List list1, List list2) { + List res = new ArrayList<>(list1); + res.addAll(list2); + return res; + } + + public static List list(T... a) { + return Arrays.asList(a); + } + + + static List toIDs(List items) { + return items.stream().map(i->i.getId()).collect(Collectors.toList()); + } + } } -- cgit v1.2.3 From ce5aa26878481a5c07bd611d186ddd735b504d5e Mon Sep 17 00:00:00 2001 From: orionlee Date: Wed, 23 May 2018 22:13:01 -0700 Subject: refactoring test - factor out common operations of calc position, add to queue and verify result into common helper. --- .../antennapod/core/storage/DBWriterTest.java | 47 ++++++++++++---------- 1 file changed, 26 insertions(+), 21 deletions(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index afdfc323d..5603f8778 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -86,11 +86,10 @@ public class DBWriterTest { // shallow copy to which the test will add items List queue = new ArrayList<>(curQueue); - FeedItem tFI = tFI(TFI_ID); - int posActual = calculator.calcPosition(posAmongAdded, tFI, queue); - queue.add(posActual, tFI); - assertEquals(message, idsExpected, toIDs(queue)); + doAddToQueueAndAssertResult(message, + calculator, posAmongAdded, tFI, queue, + idsExpected); } } @@ -201,32 +200,27 @@ public class DBWriterTest { // User clicks download on feed item 101 FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); - - int pos101Actual = calculator.calcPosition(0, tFI101, queue); - queue.add(pos101Actual, tFI101); - assertEquals(message + " (1st download)", - idsExpectedAfter101, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (1st download)", + calculator, 0, tFI101, queue, + idsExpectedAfter101); // Then user clicks download on feed item 102 FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); - int pos102Actual = calculator.calcPosition(0, tFI102, queue); - queue.add(pos102Actual, tFI102); - assertEquals(message + " (2nd download, it should preserve order of download)", - idsExpectedAfter102, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", + calculator, 0, tFI102, queue, + idsExpectedAfter102); // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); - int pos201Actual = calculator.calcPosition(0, tFI201, queue); - queue.add(pos201Actual, tFI201); - assertEquals(message + " (bulk insertion, 1st item)", - idsExpectedAfter201, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", + calculator, 0, tFI201, queue, + idsExpectedAfter201); FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); - int pos202Actual = calculator.calcPosition(1, tFI202, queue); - queue.add(pos202Actual, tFI202); - assertEquals(message + " (bulk insertion, 2nd item)", - idsExpectedAfter202, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", + calculator, 0, tFI202, queue, + idsExpectedAfter202); // TODO: simulate download failure cases. } @@ -267,6 +261,17 @@ public class DBWriterTest { // - common queue (of items) for tests // - construct FeedItems for tests + static void doAddToQueueAndAssertResult(String message, + ItemEnqueuePositionCalculator calculator, + int positionAmongAdd, + FeedItem itemToAdd, + List queue, + List idsExpected) { + int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue); + queue.add(posActual, itemToAdd); + assertEquals(message, idsExpected, toIDs(queue)); + } + static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); -- cgit v1.2.3 From 820b0b07939f9997266ea14677639add652e4ea7 Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 28 May 2018 13:34:07 -0700 Subject: test case bug fix: Bulk download 2nd item position should be 1 --- core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index 5603f8778..f0f557f2f 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -219,7 +219,7 @@ public class DBWriterTest { FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", - calculator, 0, tFI202, queue, + calculator, 1, tFI202, queue, idsExpectedAfter202); // TODO: simulate download failure cases. -- cgit v1.2.3 From fb7fb05b5e3d61975d300369271dba385490838a Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 28 May 2018 13:35:34 -0700 Subject: test case tweak: preserve download order test, fix test case name (remove the incomplete expected from test case name) --- core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index f0f557f2f..42860c903 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -137,7 +137,7 @@ public class DBWriterTest { @RunWith(Parameterized.class) public static class ItemEnqueuePositionCalculatorPreserveDownloadOrderTest { - @Parameters(name = "{index}: case<{0}>, expected:{1}") + @Parameters(name = "{index}: case<{0}>") public static Iterable data() { Options optDefault = new Options(); Options optEnqAtFront = new Options().setEnqueueAtFront(true); -- cgit v1.2.3 From 2d1ee52014aa6b171733261a698129f5de3f4036 Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 4 Oct 2019 12:39:56 -0700 Subject: fix imports post androidX migration --- core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index 42860c903..46822de81 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -1,6 +1,6 @@ package de.danoeh.antennapod.core.storage; -import android.support.annotation.NonNull; +import androidx.annotation.NonNull; import org.junit.Test; import org.junit.runner.RunWith; -- cgit v1.2.3 From cd3d20d61338180bfa585fbf8fcc2b13261df709 Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 4 Oct 2019 13:06:29 -0700 Subject: refactor - move ItemEnqueuePositionCalculator to top-level per review. --- .../antennapod/core/storage/DBWriterTest.java | 335 --------------------- .../storage/ItemEnqueuePositionCalculatorTest.java | 330 ++++++++++++++++++++ 2 files changed, 330 insertions(+), 335 deletions(-) delete mode 100644 core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java create mode 100644 core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java (limited to 'core/src/test/java') diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java deleted file mode 100644 index 46822de81..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ /dev/null @@ -1,335 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import androidx.annotation.NonNull; - -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.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import de.danoeh.antennapod.core.feed.FeedFile; -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.storage.DBWriter.ItemEnqueuePositionCalculator; -import de.danoeh.antennapod.core.storage.DBWriter.ItemEnqueuePositionCalculator.Options; - -import static org.junit.Assert.assertEquals; - -public class DBWriterTest { - - public static class ItemEnqueuePositionCalculatorTest { - - @RunWith(Parameterized.class) - public static class IEPCBasicTest { - @Parameters(name = "{index}: case<{0}>, expected:{1}") - public static Iterable data() { - Options optDefault = new Options(); - Options optEnqAtFront = new Options().setEnqueueAtFront(true); - - return Arrays.asList(new Object[][]{ - {"case default, i.e., add to the end", - concat(QUEUE_DEFAULT_IDS, TFI_ID), - optDefault, 0, QUEUE_DEFAULT}, - {"case default (2nd item)", - concat(QUEUE_DEFAULT_IDS, TFI_ID), - optDefault, 1, QUEUE_DEFAULT}, - {"case option enqueue at front", - concat(TFI_ID, QUEUE_DEFAULT_IDS), - optEnqAtFront, 0, QUEUE_DEFAULT}, - {"case option enqueue at front (2nd item)", - list(11L, TFI_ID, 12L, 13L, 14L), - optEnqAtFront, 1, QUEUE_DEFAULT}, - {"case empty queue, option default", - list(TFI_ID), - optDefault, 0, QUEUE_EMPTY}, - {"case empty queue, option enqueue at front", - list(TFI_ID), - optEnqAtFront, 0, QUEUE_EMPTY}, - }); - } - - @Parameter - public String message; - - @Parameter(1) - public List idsExpected; - - @Parameter(2) - public Options options; - - @Parameter(3) - public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. - - @Parameter(4) - public List 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 queue = new ArrayList<>(curQueue); - FeedItem tFI = tFI(TFI_ID); - doAddToQueueAndAssertResult(message, - calculator, posAmongAdded, tFI, queue, - idsExpected); - } - - } - - @RunWith(Parameterized.class) - public static class IEPCKeepInProgressAtFrontTest extends IEPCBasicTest { - @Parameters(name = "{index}: case<{0}>, expected:{1}") - public static Iterable data() { - Options optKeepInProgressAtFront = - new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true); - // edge case: keep in progress without enabling enqueue at front is meaningless - Options optKeepInProgressAtFrontWithNoEnqueueAtFront = - new Options().setKeepInProgressAtFront(true); - - return Arrays.asList(new Object[][]{ - {"case option keep in progress at front", - list(11L, TFI_ID, 12L, 13L), - optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, - {"case option keep in progress at front (2nd item)", - list(11L, 12L, TFI_ID, 13L), - optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, - {"case option keep in progress at front, front item not in progress", - concat(TFI_ID, QUEUE_DEFAULT_IDS), - optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, - {"case option keep in progress at front, front item no media at all", - concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS), - optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception - {"case option keep in progress at front, but enqueue at front is disabled", - concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID), - optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, - {"case empty queue, option keep in progress at front", - list(TFI_ID), - optKeepInProgressAtFront, 0, QUEUE_EMPTY}, - }); - } - - private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); - private static final List QUEUE_FRONT_IN_PROGRESS_IDS = toIDs(QUEUE_FRONT_IN_PROGRESS); - - private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); - private static final List QUEUE_FRONT_NO_MEDIA_IDS = toIDs(QUEUE_FRONT_NO_MEDIA); - - } - - @RunWith(Parameterized.class) - public static class ItemEnqueuePositionCalculatorPreserveDownloadOrderTest { - - @Parameters(name = "{index}: case<{0}>") - public static Iterable data() { - Options optDefault = new Options(); - Options optEnqAtFront = new Options().setEnqueueAtFront(true); - - // 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, 201L)), - concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)), - optDefault, QUEUE_DEFAULT}, - {"download order test, enqueue at front", - concat(101L, QUEUE_DEFAULT_IDS), - concat(list(101L, 102L), QUEUE_DEFAULT_IDS), - concat(list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), - concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), - optEnqAtFront, QUEUE_DEFAULT}, - }); - } - - @Parameter - public String message; - - @Parameter(1) - public List idsExpectedAfter101; - - @Parameter(2) - public List idsExpectedAfter102; - - // 2XX are for testing bulk insertion cases - @Parameter(3) - public List idsExpectedAfter201; - - @Parameter(4) - public List idsExpectedAfter202; - - @Parameter(5) - public Options options; - - @Parameter(6) - public List queueInitial; - - @Test - public void testQueueOrderWhenDownloading2Items() { - - // Setup class under test - // - ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); - MockDownloadRequester mockDownloadRequester = new MockDownloadRequester(); - calculator.requester = mockDownloadRequester; - - // Setup initial data - // A shallow copy, as the test code will manipulate the queue - List queue = new ArrayList<>(queueInitial); - - - // Test body - - // User clicks download on feed item 101 - FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); - doAddToQueueAndAssertResult(message + " (1st download)", - calculator, 0, tFI101, queue, - idsExpectedAfter101); - - // Then user clicks download on feed item 102 - FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); - doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", - calculator, 0, tFI102, queue, - idsExpectedAfter102); - - // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls - - FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); - doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", - calculator, 0, tFI201, queue, - idsExpectedAfter201); - - FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); - doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", - calculator, 1, tFI202, queue, - idsExpectedAfter202); - - // TODO: simulate download failure cases. - } - - - private static FeedItem tFI_isDownloading(int id, MockDownloadRequester requester) { - FeedItem item = tFI(id); - FeedMedia media = - new FeedMedia(item, "http://download.url.net/" + id - , 100000 + id, "audio/mp3"); - media.setId(item.getId()); - item.setMedia(media); - - requester.mockDownloadingFile(media, true); - - return item; - } - - private static class MockDownloadRequester implements FeedFileDownloadStatusRequesterInterface { - - private Map downloadingByIds = new HashMap<>(); - - @Override - public synchronized boolean isDownloadingFile(@NonNull FeedFile item) { - return downloadingByIds.getOrDefault(item.getId(), false); - } - - // All other parent methods should not be called - - public void mockDownloadingFile(FeedFile item, boolean isDownloading) { - downloadingByIds.put(item.getId(), isDownloading); - } - } - } - - - // Common helpers: - // - common queue (of items) for tests - // - construct FeedItems for tests - - static void doAddToQueueAndAssertResult(String message, - ItemEnqueuePositionCalculator calculator, - int positionAmongAdd, - FeedItem itemToAdd, - List queue, - List idsExpected) { - int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue); - queue.add(posActual, itemToAdd); - assertEquals(message, idsExpected, toIDs(queue)); - } - - static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); - - static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); - static final List QUEUE_DEFAULT_IDS = QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); - - - static FeedItem tFI(long id) { - return tFI(id, -1); - } - - static FeedItem tFI(long id, int position) { - FeedItem item = tFINoMedia(id); - FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); - media.setId(item.getId()); - item.setMedia(media); - - if (position >= 0) { - media.setPosition(position); - } - - return item; - } - - static FeedItem tFINoMedia(long id) { - FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", - new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); - return item; - } - - // Collections helpers - - static List concat(T item, List list) { - List res = new ArrayList<>(list); - res.add(0, item); - return res; - } - - static List concat(List list, T item) { - List res = new ArrayList<>(list); - res.add(item); - return res; - } - - static List concat(List list1, List list2) { - List res = new ArrayList<>(list1); - res.addAll(list2); - return res; - } - - public static List list(T... a) { - return Arrays.asList(a); - } - - - static List toIDs(List items) { - return items.stream().map(i->i.getId()).collect(Collectors.toList()); - } - - } - -} 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..1331df67d --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java @@ -0,0 +1,330 @@ +package de.danoeh.antennapod.core.storage; + +import androidx.annotation.NonNull; + +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.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import de.danoeh.antennapod.core.feed.FeedFile; +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.storage.ItemEnqueuePositionCalculator.Options; + +import static org.junit.Assert.assertEquals; + +public class ItemEnqueuePositionCalculatorTest { + + @RunWith(Parameterized.class) + public static class IEPCBasicTest { + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable data() { + Options optDefault = new Options(); + Options optEnqAtFront = new Options().setEnqueueAtFront(true); + + return Arrays.asList(new Object[][]{ + {"case default, i.e., add to the end", + concat(QUEUE_DEFAULT_IDS, TFI_ID), + optDefault, 0, QUEUE_DEFAULT}, + {"case default (2nd item)", + concat(QUEUE_DEFAULT_IDS, TFI_ID), + optDefault, 1, QUEUE_DEFAULT}, + {"case option enqueue at front", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + optEnqAtFront, 0, QUEUE_DEFAULT}, + {"case option enqueue at front (2nd item)", + list(11L, TFI_ID, 12L, 13L, 14L), + optEnqAtFront, 1, QUEUE_DEFAULT}, + {"case empty queue, option default", + list(TFI_ID), + optDefault, 0, QUEUE_EMPTY}, + {"case empty queue, option enqueue at front", + list(TFI_ID), + optEnqAtFront, 0, QUEUE_EMPTY}, + }); + } + + @Parameter + public String message; + + @Parameter(1) + public List idsExpected; + + @Parameter(2) + public Options options; + + @Parameter(3) + public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. + + @Parameter(4) + public List 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 queue = new ArrayList<>(curQueue); + FeedItem tFI = tFI(TFI_ID); + doAddToQueueAndAssertResult(message, + calculator, posAmongAdded, tFI, queue, + idsExpected); + } + + } + + @RunWith(Parameterized.class) + public static class IEPCKeepInProgressAtFrontTest extends IEPCBasicTest { + @Parameters(name = "{index}: case<{0}>, expected:{1}") + public static Iterable data() { + Options optKeepInProgressAtFront = + new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true); + // edge case: keep in progress without enabling enqueue at front is meaningless + Options optKeepInProgressAtFrontWithNoEnqueueAtFront = + new Options().setKeepInProgressAtFront(true); + + return Arrays.asList(new Object[][]{ + {"case option keep in progress at front", + list(11L, TFI_ID, 12L, 13L), + optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, + {"case option keep in progress at front (2nd item)", + list(11L, 12L, TFI_ID, 13L), + optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, + {"case option keep in progress at front, front item not in progress", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, + {"case option keep in progress at front, front item no media at all", + concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS), + optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception + {"case option keep in progress at front, but enqueue at front is disabled", + concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID), + optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, + {"case empty queue, option keep in progress at front", + list(TFI_ID), + optKeepInProgressAtFront, 0, QUEUE_EMPTY}, + }); + } + + private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); + private static final List QUEUE_FRONT_IN_PROGRESS_IDS = toIDs(QUEUE_FRONT_IN_PROGRESS); + + private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); + private static final List QUEUE_FRONT_NO_MEDIA_IDS = toIDs(QUEUE_FRONT_NO_MEDIA); + + } + + @RunWith(Parameterized.class) + public static class ItemEnqueuePositionCalculatorPreserveDownloadOrderTest { + + @Parameters(name = "{index}: case<{0}>") + public static Iterable data() { + Options optDefault = new Options(); + Options optEnqAtFront = new Options().setEnqueueAtFront(true); + + // 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, 201L)), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)), + optDefault, QUEUE_DEFAULT}, + {"download order test, enqueue at front", + concat(101L, QUEUE_DEFAULT_IDS), + concat(list(101L, 102L), QUEUE_DEFAULT_IDS), + concat(list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), + concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), + optEnqAtFront, QUEUE_DEFAULT}, + }); + } + + @Parameter + public String message; + + @Parameter(1) + public List idsExpectedAfter101; + + @Parameter(2) + public List idsExpectedAfter102; + + // 2XX are for testing bulk insertion cases + @Parameter(3) + public List idsExpectedAfter201; + + @Parameter(4) + public List idsExpectedAfter202; + + @Parameter(5) + public Options options; + + @Parameter(6) + public List queueInitial; + + @Test + public void testQueueOrderWhenDownloading2Items() { + + // Setup class under test + // + ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); + MockDownloadRequester mockDownloadRequester = new MockDownloadRequester(); + calculator.requester = mockDownloadRequester; + + // Setup initial data + // A shallow copy, as the test code will manipulate the queue + List queue = new ArrayList<>(queueInitial); + + + // Test body + + // User clicks download on feed item 101 + FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); + doAddToQueueAndAssertResult(message + " (1st download)", + calculator, 0, tFI101, queue, + idsExpectedAfter101); + + // Then user clicks download on feed item 102 + FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); + doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", + calculator, 0, tFI102, queue, + idsExpectedAfter102); + + // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls + + FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); + doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", + calculator, 0, tFI201, queue, + idsExpectedAfter201); + + FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); + doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", + calculator, 1, tFI202, queue, + idsExpectedAfter202); + + // TODO: simulate download failure cases. + } + + + private static FeedItem tFI_isDownloading(int id, MockDownloadRequester requester) { + FeedItem item = tFI(id); + FeedMedia media = + new FeedMedia(item, "http://download.url.net/" + id + , 100000 + id, "audio/mp3"); + media.setId(item.getId()); + item.setMedia(media); + + requester.mockDownloadingFile(media, true); + + return item; + } + + private static class MockDownloadRequester implements FeedFileDownloadStatusRequesterInterface { + + private Map downloadingByIds = new HashMap<>(); + + @Override + public synchronized boolean isDownloadingFile(@NonNull FeedFile item) { + return downloadingByIds.getOrDefault(item.getId(), false); + } + + // All other parent methods should not be called + + public void mockDownloadingFile(FeedFile item, boolean isDownloading) { + downloadingByIds.put(item.getId(), isDownloading); + } + } + } + + + // Common helpers: + // - common queue (of items) for tests + // - construct FeedItems for tests + + static void doAddToQueueAndAssertResult(String message, + ItemEnqueuePositionCalculator calculator, + int positionAmongAdd, + FeedItem itemToAdd, + List queue, + List idsExpected) { + int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue); + queue.add(posActual, itemToAdd); + assertEquals(message, idsExpected, toIDs(queue)); + } + + static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); + + static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); + static final List QUEUE_DEFAULT_IDS = QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); + + + static FeedItem tFI(long id) { + return tFI(id, -1); + } + + static FeedItem tFI(long id, int position) { + FeedItem item = tFINoMedia(id); + FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); + media.setId(item.getId()); + item.setMedia(media); + + if (position >= 0) { + media.setPosition(position); + } + + return item; + } + + static FeedItem tFINoMedia(long id) { + FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", + new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); + return item; + } + + // Collections helpers + + static List concat(T item, List list) { + List res = new ArrayList<>(list); + res.add(0, item); + return res; + } + + static List concat(List list, T item) { + List res = new ArrayList<>(list); + res.add(item); + return res; + } + + static List concat(List list1, List list2) { + List res = new ArrayList<>(list1); + res.addAll(list2); + return res; + } + + static List list(T... a) { + return Arrays.asList(a); + } + + + static List toIDs(List items) { + return items.stream().map(i->i.getId()).collect(Collectors.toList()); + } + +} -- cgit v1.2.3 From 2f82a5d46421a602044b6fcd728e4394a2ad77ea Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 4 Oct 2019 13:13:46 -0700 Subject: refactor - rename FeedFileDownloadStatusRequesterInterface to a more generic DownloadStateProvider. --- .../antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src/test/java') 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 1331df67d..7b5296e8e 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 @@ -186,7 +186,7 @@ public class ItemEnqueuePositionCalculatorTest { // ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); MockDownloadRequester mockDownloadRequester = new MockDownloadRequester(); - calculator.requester = mockDownloadRequester; + calculator.downloadStateProvider = mockDownloadRequester; // Setup initial data // A shallow copy, as the test code will manipulate the queue @@ -236,7 +236,7 @@ public class ItemEnqueuePositionCalculatorTest { return item; } - private static class MockDownloadRequester implements FeedFileDownloadStatusRequesterInterface { + private static class MockDownloadRequester implements DownloadStateProvider { private Map downloadingByIds = new HashMap<>(); -- cgit v1.2.3 From fb6fa010f8b8ba8aca4bb0eec173183bf1163f66 Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 4 Oct 2019 14:22:23 -0700 Subject: Enqueue tweaks - replace custom stub DownloadStateProvider with mockito mocks in test --- .../storage/ItemEnqueuePositionCalculatorTest.java | 41 +++++++--------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'core/src/test/java') 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 7b5296e8e..424240fa8 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,5 @@ package de.danoeh.antennapod.core.storage; -import androidx.annotation.NonNull; - import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -12,18 +10,18 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.stream.Collectors; -import de.danoeh.antennapod.core.feed.FeedFile; 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.storage.ItemEnqueuePositionCalculator.Options; 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 { @@ -185,8 +183,9 @@ public class ItemEnqueuePositionCalculatorTest { // Setup class under test // ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); - MockDownloadRequester mockDownloadRequester = new MockDownloadRequester(); - calculator.downloadStateProvider = mockDownloadRequester; + 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 @@ -196,25 +195,25 @@ public class ItemEnqueuePositionCalculatorTest { // Test body // User clicks download on feed item 101 - FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); + FeedItem tFI101 = tFI_isDownloading(101, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (1st download)", calculator, 0, tFI101, queue, idsExpectedAfter101); // Then user clicks download on feed item 102 - FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); + FeedItem tFI102 = tFI_isDownloading(102, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", calculator, 0, tFI102, queue, idsExpectedAfter102); // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls - FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); + FeedItem tFI201 = tFI_isDownloading(201, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", calculator, 0, tFI201, queue, idsExpectedAfter201); - FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); + FeedItem tFI202 = tFI_isDownloading(202, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", calculator, 1, tFI202, queue, idsExpectedAfter202); @@ -223,7 +222,7 @@ public class ItemEnqueuePositionCalculatorTest { } - private static FeedItem tFI_isDownloading(int id, MockDownloadRequester requester) { + private static FeedItem tFI_isDownloading(int id, DownloadStateProvider stubDownloadStateProvider) { FeedItem item = tFI(id); FeedMedia media = new FeedMedia(item, "http://download.url.net/" + id @@ -231,26 +230,10 @@ public class ItemEnqueuePositionCalculatorTest { media.setId(item.getId()); item.setMedia(media); - requester.mockDownloadingFile(media, true); + stub(stubDownloadStateProvider.isDownloadingFile(media)).toReturn(true); return item; } - - private static class MockDownloadRequester implements DownloadStateProvider { - - private Map downloadingByIds = new HashMap<>(); - - @Override - public synchronized boolean isDownloadingFile(@NonNull FeedFile item) { - return downloadingByIds.getOrDefault(item.getId(), false); - } - - // All other parent methods should not be called - - public void mockDownloadingFile(FeedFile item, boolean isDownloading) { - downloadingByIds.put(item.getId(), isDownloading); - } - } } -- cgit v1.2.3 From 69c00224724875866da6963745712befaca91c9b Mon Sep 17 00:00:00 2001 From: orionlee Date: Fri, 4 Oct 2019 14:06:35 -0700 Subject: code style fixes - naming, indentation, etc. --- .../antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src/test/java') 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 424240fa8..c076ec892 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 @@ -26,7 +26,7 @@ import static org.mockito.Mockito.stub; public class ItemEnqueuePositionCalculatorTest { @RunWith(Parameterized.class) - public static class IEPCBasicTest { + public static class BasicTest { @Parameters(name = "{index}: case<{0}>, expected:{1}") public static Iterable data() { Options optDefault = new Options(); @@ -90,7 +90,7 @@ public class ItemEnqueuePositionCalculatorTest { } @RunWith(Parameterized.class) - public static class IEPCKeepInProgressAtFrontTest extends IEPCBasicTest { + public static class KeepInProgressAtFrontTest extends BasicTest { @Parameters(name = "{index}: case<{0}>, expected:{1}") public static Iterable data() { Options optKeepInProgressAtFront = -- cgit v1.2.3 From d24669d4c1eae8f32d59c85ea2b748b16851a5e3 Mon Sep 17 00:00:00 2001 From: orionlee Date: Sun, 27 Oct 2019 10:17:34 -0700 Subject: refactor extract common FeedItem List to IDs method --- .../core/storage/ItemEnqueuePositionCalculatorTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'core/src/test/java') 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 c076ec892..4b8140083 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 @@ -18,6 +18,7 @@ import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMother; import de.danoeh.antennapod.core.storage.ItemEnqueuePositionCalculator.Options; +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; @@ -122,10 +123,10 @@ public class ItemEnqueuePositionCalculatorTest { } private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); - private static final List QUEUE_FRONT_IN_PROGRESS_IDS = toIDs(QUEUE_FRONT_IN_PROGRESS); + private static final List QUEUE_FRONT_IN_PROGRESS_IDS = getIdList(QUEUE_FRONT_IN_PROGRESS); private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); - private static final List QUEUE_FRONT_NO_MEDIA_IDS = toIDs(QUEUE_FRONT_NO_MEDIA); + private static final List QUEUE_FRONT_NO_MEDIA_IDS = getIdList(QUEUE_FRONT_NO_MEDIA); } @@ -249,7 +250,7 @@ public class ItemEnqueuePositionCalculatorTest { List idsExpected) { int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue); queue.add(posActual, itemToAdd); - assertEquals(message, idsExpected, toIDs(queue)); + assertEquals(message, idsExpected, getIdList(queue)); } static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); @@ -306,8 +307,4 @@ public class ItemEnqueuePositionCalculatorTest { } - static List toIDs(List items) { - return items.stream().map(i->i.getId()).collect(Collectors.toList()); - } - } -- cgit v1.2.3 From 406f1cceb838be0dc468cdaa4c09224bebf27124 Mon Sep 17 00:00:00 2001 From: orionlee Date: Sun, 27 Oct 2019 10:22:41 -0700 Subject: refactor move generic Collection helpers to CollectionTestUtil --- .../storage/ItemEnqueuePositionCalculatorTest.java | 66 +++++++--------------- .../antennapod/core/util/CollectionTestUtil.java | 30 ++++++++++ 2 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java (limited to 'core/src/test/java') 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 4b8140083..a72400adf 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 @@ -17,6 +17,7 @@ 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.storage.ItemEnqueuePositionCalculator.Options; +import de.danoeh.antennapod.core.util.CollectionTestUtil; import static de.danoeh.antennapod.core.util.FeedItemUtil.getIdList; import static org.junit.Assert.assertEquals; @@ -35,22 +36,22 @@ public class ItemEnqueuePositionCalculatorTest { return Arrays.asList(new Object[][]{ {"case default, i.e., add to the end", - concat(QUEUE_DEFAULT_IDS, TFI_ID), + CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, TFI_ID), optDefault, 0, QUEUE_DEFAULT}, {"case default (2nd item)", - concat(QUEUE_DEFAULT_IDS, TFI_ID), + CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, TFI_ID), optDefault, 1, QUEUE_DEFAULT}, {"case option enqueue at front", - concat(TFI_ID, QUEUE_DEFAULT_IDS), + CollectionTestUtil.concat(TFI_ID, QUEUE_DEFAULT_IDS), optEnqAtFront, 0, QUEUE_DEFAULT}, {"case option enqueue at front (2nd item)", - list(11L, TFI_ID, 12L, 13L, 14L), + CollectionTestUtil.list(11L, TFI_ID, 12L, 13L, 14L), optEnqAtFront, 1, QUEUE_DEFAULT}, {"case empty queue, option default", - list(TFI_ID), + CollectionTestUtil.list(TFI_ID), optDefault, 0, QUEUE_EMPTY}, {"case empty queue, option enqueue at front", - list(TFI_ID), + CollectionTestUtil.list(TFI_ID), optEnqAtFront, 0, QUEUE_EMPTY}, }); } @@ -102,22 +103,22 @@ public class ItemEnqueuePositionCalculatorTest { return Arrays.asList(new Object[][]{ {"case option keep in progress at front", - list(11L, TFI_ID, 12L, 13L), + CollectionTestUtil.list(11L, TFI_ID, 12L, 13L), optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, {"case option keep in progress at front (2nd item)", - list(11L, 12L, TFI_ID, 13L), + CollectionTestUtil.list(11L, 12L, TFI_ID, 13L), optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, {"case option keep in progress at front, front item not in progress", - concat(TFI_ID, QUEUE_DEFAULT_IDS), + CollectionTestUtil.concat(TFI_ID, QUEUE_DEFAULT_IDS), optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, {"case option keep in progress at front, front item no media at all", - concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS), + CollectionTestUtil.concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS), optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception {"case option keep in progress at front, but enqueue at front is disabled", - concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID), + CollectionTestUtil.concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID), optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, {"case empty queue, option keep in progress at front", - list(TFI_ID), + CollectionTestUtil.list(TFI_ID), optKeepInProgressAtFront, 0, QUEUE_EMPTY}, }); } @@ -142,16 +143,16 @@ public class ItemEnqueuePositionCalculatorTest { // (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, 201L)), - concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)), + CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, 101L), + CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, CollectionTestUtil.list(101L, 102L)), + CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, CollectionTestUtil.list(101L, 102L, 201L)), + CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, CollectionTestUtil.list(101L, 102L, 201L, 202L)), optDefault, QUEUE_DEFAULT}, {"download order test, enqueue at front", - concat(101L, QUEUE_DEFAULT_IDS), - concat(list(101L, 102L), QUEUE_DEFAULT_IDS), - concat(list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), - concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), + CollectionTestUtil.concat(101L, QUEUE_DEFAULT_IDS), + CollectionTestUtil.concat(CollectionTestUtil.list(101L, 102L), QUEUE_DEFAULT_IDS), + CollectionTestUtil.concat(CollectionTestUtil.list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), + CollectionTestUtil.concat(CollectionTestUtil.list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), optEnqAtFront, QUEUE_DEFAULT}, }); } @@ -282,29 +283,4 @@ public class ItemEnqueuePositionCalculatorTest { return item; } - // Collections helpers - - static List concat(T item, List list) { - List res = new ArrayList<>(list); - res.add(0, item); - return res; - } - - static List concat(List list, T item) { - List res = new ArrayList<>(list); - res.add(item); - return res; - } - - static List concat(List list1, List list2) { - List res = new ArrayList<>(list1); - res.addAll(list2); - return res; - } - - static List list(T... a) { - return Arrays.asList(a); - } - - } 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 List concat(T item, List list) { + List res = new ArrayList<>(list); + res.add(0, item); + return res; + } + + public static List concat(List list, T item) { + List res = new ArrayList<>(list); + res.add(item); + return res; + } + + public static List concat(List list1, List list2) { + List res = new ArrayList<>(list1); + res.addAll(list2); + return res; + } + + public static List list(T... a) { + return Arrays.asList(a); + } +} -- cgit v1.2.3 From bddd2bfa2e02577112203044b3653d028cd55cd5 Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 28 Oct 2019 14:26:10 -0700 Subject: enqueue location: use the new 3-value settings --- .../storage/ItemEnqueuePositionCalculatorTest.java | 170 +++++++++++---------- 1 file changed, 93 insertions(+), 77 deletions(-) (limited to 'core/src/test/java') 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 a72400adf..324f3812f 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 @@ -16,9 +16,16 @@ 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.storage.ItemEnqueuePositionCalculator.Options; -import de.danoeh.antennapod.core.util.CollectionTestUtil; - +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; @@ -31,28 +38,25 @@ public class ItemEnqueuePositionCalculatorTest { public static class BasicTest { @Parameters(name = "{index}: case<{0}>, expected:{1}") public static Iterable data() { - Options optDefault = new Options(); - Options optEnqAtFront = new Options().setEnqueueAtFront(true); - return Arrays.asList(new Object[][]{ {"case default, i.e., add to the end", - CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, TFI_ID), - optDefault, 0, QUEUE_DEFAULT}, + concat(QUEUE_DEFAULT_IDS, TFI_ID), + BACK, 0, QUEUE_DEFAULT}, {"case default (2nd item)", - CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, TFI_ID), - optDefault, 1, QUEUE_DEFAULT}, + concat(QUEUE_DEFAULT_IDS, TFI_ID), + BACK, 1, QUEUE_DEFAULT}, {"case option enqueue at front", - CollectionTestUtil.concat(TFI_ID, QUEUE_DEFAULT_IDS), - optEnqAtFront, 0, QUEUE_DEFAULT}, + concat(TFI_ID, QUEUE_DEFAULT_IDS), + FRONT, 0, QUEUE_DEFAULT}, {"case option enqueue at front (2nd item)", - CollectionTestUtil.list(11L, TFI_ID, 12L, 13L, 14L), - optEnqAtFront, 1, QUEUE_DEFAULT}, + list(11L, TFI_ID, 12L, 13L, 14L), + FRONT, 1, QUEUE_DEFAULT}, {"case empty queue, option default", - CollectionTestUtil.list(TFI_ID), - optDefault, 0, QUEUE_EMPTY}, + list(TFI_ID), + BACK, 0, QUEUE_EMPTY}, {"case empty queue, option enqueue at front", - CollectionTestUtil.list(TFI_ID), - optEnqAtFront, 0, QUEUE_EMPTY}, + list(TFI_ID), + FRONT, 0, QUEUE_EMPTY}, }); } @@ -63,7 +67,7 @@ public class ItemEnqueuePositionCalculatorTest { public List idsExpected; @Parameter(2) - public Options options; + public EnqueueLocation options; @Parameter(3) public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. @@ -71,7 +75,6 @@ public class ItemEnqueuePositionCalculatorTest { @Parameter(4) public List curQueue; - public static final long TFI_ID = 101; /** @@ -85,49 +88,62 @@ public class ItemEnqueuePositionCalculatorTest { List queue = new ArrayList<>(curQueue); FeedItem tFI = tFI(TFI_ID); doAddToQueueAndAssertResult(message, - calculator, posAmongAdded, tFI, queue, + calculator, posAmongAdded, tFI, queue, getCurrentlyPlaying(), idsExpected); } + Playable getCurrentlyPlaying() { return null; } } @RunWith(Parameterized.class) - public static class KeepInProgressAtFrontTest extends BasicTest { + public static class AfterCurrentlyPlayingTest extends BasicTest { @Parameters(name = "{index}: case<{0}>, expected:{1}") public static Iterable data() { - Options optKeepInProgressAtFront = - new Options().setEnqueueAtFront(true).setKeepInProgressAtFront(true); - // edge case: keep in progress without enabling enqueue at front is meaningless - Options optKeepInProgressAtFrontWithNoEnqueueAtFront = - new Options().setKeepInProgressAtFront(true); - return Arrays.asList(new Object[][]{ - {"case option keep in progress at front", - CollectionTestUtil.list(11L, TFI_ID, 12L, 13L), - optKeepInProgressAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, - {"case option keep in progress at front (2nd item)", - CollectionTestUtil.list(11L, 12L, TFI_ID, 13L), - optKeepInProgressAtFront, 1, QUEUE_FRONT_IN_PROGRESS}, - {"case option keep in progress at front, front item not in progress", - CollectionTestUtil.concat(TFI_ID, QUEUE_DEFAULT_IDS), - optKeepInProgressAtFront, 0, QUEUE_DEFAULT}, - {"case option keep in progress at front, front item no media at all", - CollectionTestUtil.concat(TFI_ID, QUEUE_FRONT_NO_MEDIA_IDS), - optKeepInProgressAtFront, 0, QUEUE_FRONT_NO_MEDIA}, // No media should not cause any exception - {"case option keep in progress at front, but enqueue at front is disabled", - CollectionTestUtil.concat(QUEUE_FRONT_IN_PROGRESS_IDS, TFI_ID), - optKeepInProgressAtFrontWithNoEnqueueAtFront, 0, QUEUE_FRONT_IN_PROGRESS}, - {"case empty queue, option keep in progress at front", - CollectionTestUtil.list(TFI_ID), - optKeepInProgressAtFront, 0, QUEUE_EMPTY}, + {"case option after currently playing", + list(11L, TFI_ID, 12L, 13L, 14L), + AFTER_CURRENTLY_PLAYING, 0, QUEUE_DEFAULT, 11L}, + {"case option after currently playing (2nd item)", + list(11L, 12L, TFI_ID, 13L, 14L), + AFTER_CURRENTLY_PLAYING, 1, 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, 0, QUEUE_DEFAULT, 13L}, + {"case option after currently playing, currently playing is not in queue", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + AFTER_CURRENTLY_PLAYING, 0, QUEUE_DEFAULT, 99L}, + {"case option after currently playing, no currentlyPlaying is null", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + AFTER_CURRENTLY_PLAYING, 0, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NULL}, + {"case option after currently playing, currentlyPlaying is externalMedia", + concat(TFI_ID, QUEUE_DEFAULT_IDS), + AFTER_CURRENTLY_PLAYING, 0, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA}, + {"case empty queue, option after currently playing", + list(TFI_ID), + AFTER_CURRENTLY_PLAYING, 0, QUEUE_EMPTY, ID_CURRENTLY_PLAYING_NULL}, }); } - private static final List QUEUE_FRONT_IN_PROGRESS = Arrays.asList(tFI(11, 60000), tFI(12), tFI(13)); - private static final List QUEUE_FRONT_IN_PROGRESS_IDS = getIdList(QUEUE_FRONT_IN_PROGRESS); + @Parameter(5) + public long idCurrentlyPlaying = -1; + + @Override + Playable getCurrentlyPlaying() { + if (ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA == idCurrentlyPlaying) { + return externalMedia(); + } + if (ID_CURRENTLY_PLAYING_NULL == idCurrentlyPlaying) { + return null; + } + return tFI(idCurrentlyPlaying).getMedia(); + } - private static final List QUEUE_FRONT_NO_MEDIA = Arrays.asList(tFINoMedia(11), tFI(12), tFI(13)); - private static final List QUEUE_FRONT_NO_MEDIA_IDS = getIdList(QUEUE_FRONT_NO_MEDIA); + 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; } @@ -136,24 +152,21 @@ public class ItemEnqueuePositionCalculatorTest { @Parameters(name = "{index}: case<{0}>") public static Iterable data() { - Options optDefault = new Options(); - Options optEnqAtFront = new Options().setEnqueueAtFront(true); - // 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", - CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, 101L), - CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, CollectionTestUtil.list(101L, 102L)), - CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, CollectionTestUtil.list(101L, 102L, 201L)), - CollectionTestUtil.concat(QUEUE_DEFAULT_IDS, CollectionTestUtil.list(101L, 102L, 201L, 202L)), - optDefault, QUEUE_DEFAULT}, + concat(QUEUE_DEFAULT_IDS, 101L), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L)), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L)), + concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)), + BACK, QUEUE_DEFAULT}, {"download order test, enqueue at front", - CollectionTestUtil.concat(101L, QUEUE_DEFAULT_IDS), - CollectionTestUtil.concat(CollectionTestUtil.list(101L, 102L), QUEUE_DEFAULT_IDS), - CollectionTestUtil.concat(CollectionTestUtil.list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), - CollectionTestUtil.concat(CollectionTestUtil.list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), - optEnqAtFront, QUEUE_DEFAULT}, + concat(101L, QUEUE_DEFAULT_IDS), + concat(list(101L, 102L), QUEUE_DEFAULT_IDS), + concat(list(101L, 102L, 201L), QUEUE_DEFAULT_IDS), + concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), + FRONT, QUEUE_DEFAULT}, }); } @@ -174,7 +187,7 @@ public class ItemEnqueuePositionCalculatorTest { public List idsExpectedAfter202; @Parameter(5) - public Options options; + public EnqueueLocation options; @Parameter(6) public List queueInitial; @@ -217,7 +230,7 @@ public class ItemEnqueuePositionCalculatorTest { FeedItem tFI202 = tFI_isDownloading(202, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", - calculator, 1, tFI202, queue, + calculator, 1, tFI202, queue, null, idsExpectedAfter202); // TODO: simulate download failure cases. @@ -249,31 +262,34 @@ public class ItemEnqueuePositionCalculatorTest { FeedItem itemToAdd, List queue, List idsExpected) { - int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue); + doAddToQueueAndAssertResult(message, calculator, positionAmongAdd, itemToAdd, queue, null, idsExpected); + } + + static void doAddToQueueAndAssertResult(String message, + ItemEnqueuePositionCalculator calculator, + int positionAmongAdd, + FeedItem itemToAdd, + List queue, + Playable currentlyPlaying, + List idsExpected) { + int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue, currentlyPlaying); queue.add(posActual, itemToAdd); assertEquals(message, idsExpected, getIdList(queue)); } static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); - static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); - static final List QUEUE_DEFAULT_IDS = QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); + static final List QUEUE_DEFAULT = + Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); + static final List QUEUE_DEFAULT_IDS = + QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); static FeedItem tFI(long id) { - return tFI(id, -1); - } - - static FeedItem tFI(long id, int position) { FeedItem item = tFINoMedia(id); FeedMedia media = new FeedMedia(item, "download_url", 1234567, "audio/mpeg"); media.setId(item.getId()); item.setMedia(media); - - if (position >= 0) { - media.setPosition(position); - } - return item; } -- cgit v1.2.3 From e233398753d9b2a131d51d6ddf44c06d9e42a05e Mon Sep 17 00:00:00 2001 From: orionlee Date: Tue, 29 Oct 2019 10:47:57 -0700 Subject: code style fixes: naming, indentation. --- .../storage/ItemEnqueuePositionCalculatorTest.java | 30 ++++++++++------------ 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'core/src/test/java') 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 324f3812f..e8f64524a 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 @@ -86,7 +86,7 @@ public class ItemEnqueuePositionCalculatorTest { // shallow copy to which the test will add items List queue = new ArrayList<>(curQueue); - FeedItem tFI = tFI(TFI_ID); + FeedItem tFI = createFeedItem(TFI_ID); doAddToQueueAndAssertResult(message, calculator, posAmongAdded, tFI, queue, getCurrentlyPlaying(), idsExpected); @@ -135,7 +135,7 @@ public class ItemEnqueuePositionCalculatorTest { if (ID_CURRENTLY_PLAYING_NULL == idCurrentlyPlaying) { return null; } - return tFI(idCurrentlyPlaying).getMedia(); + return createFeedItem(idCurrentlyPlaying).getMedia(); } private static Playable externalMedia() { @@ -210,25 +210,25 @@ public class ItemEnqueuePositionCalculatorTest { // Test body // User clicks download on feed item 101 - FeedItem tFI101 = tFI_isDownloading(101, stubDownloadStateProvider); + FeedItem tFI101 = setAsDownloading(101, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (1st download)", calculator, 0, tFI101, queue, idsExpectedAfter101); // Then user clicks download on feed item 102 - FeedItem tFI102 = tFI_isDownloading(102, stubDownloadStateProvider); + FeedItem tFI102 = setAsDownloading(102, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", calculator, 0, tFI102, queue, idsExpectedAfter102); // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls - FeedItem tFI201 = tFI_isDownloading(201, stubDownloadStateProvider); + FeedItem tFI201 = setAsDownloading(201, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", calculator, 0, tFI201, queue, idsExpectedAfter201); - FeedItem tFI202 = tFI_isDownloading(202, stubDownloadStateProvider); + FeedItem tFI202 = setAsDownloading(202, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", calculator, 1, tFI202, queue, null, idsExpectedAfter202); @@ -237,8 +237,8 @@ public class ItemEnqueuePositionCalculatorTest { } - private static FeedItem tFI_isDownloading(int id, DownloadStateProvider stubDownloadStateProvider) { - FeedItem item = tFI(id); + private static FeedItem setAsDownloading(int id, DownloadStateProvider stubDownloadStateProvider) { + FeedItem item = createFeedItem(id); FeedMedia media = new FeedMedia(item, "http://download.url.net/" + id , 100000 + id, "audio/mp3"); @@ -280,23 +280,19 @@ public class ItemEnqueuePositionCalculatorTest { static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); static final List QUEUE_DEFAULT = - Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14))); + Collections.unmodifiableList(Arrays.asList( + createFeedItem(11), createFeedItem(12), createFeedItem(13), createFeedItem(14))); static final List QUEUE_DEFAULT_IDS = QUEUE_DEFAULT.stream().map(fi -> fi.getId()).collect(Collectors.toList()); - static FeedItem tFI(long id) { - FeedItem item = tFINoMedia(id); + 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; } - static FeedItem tFINoMedia(long id) { - FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", - new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); - return item; - } - } -- cgit v1.2.3 From b80973bc303df5d51c4a677e2d65084eb3b938e8 Mon Sep 17 00:00:00 2001 From: orionlee Date: Thu, 31 Oct 2019 12:59:49 -0700 Subject: refactor - make enqueue position logic more readable per review. --- .../storage/ItemEnqueuePositionCalculatorTest.java | 123 +++++++++------------ 1 file changed, 52 insertions(+), 71 deletions(-) (limited to 'core/src/test/java') 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 e8f64524a..68eb434a4 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 @@ -41,22 +41,16 @@ public class ItemEnqueuePositionCalculatorTest { return Arrays.asList(new Object[][]{ {"case default, i.e., add to the end", concat(QUEUE_DEFAULT_IDS, TFI_ID), - BACK, 0, QUEUE_DEFAULT}, - {"case default (2nd item)", - concat(QUEUE_DEFAULT_IDS, TFI_ID), - BACK, 1, QUEUE_DEFAULT}, + BACK, QUEUE_DEFAULT}, {"case option enqueue at front", concat(TFI_ID, QUEUE_DEFAULT_IDS), - FRONT, 0, QUEUE_DEFAULT}, - {"case option enqueue at front (2nd item)", - list(11L, TFI_ID, 12L, 13L, 14L), - FRONT, 1, QUEUE_DEFAULT}, + FRONT, QUEUE_DEFAULT}, {"case empty queue, option default", list(TFI_ID), - BACK, 0, QUEUE_EMPTY}, + BACK, QUEUE_EMPTY}, {"case empty queue, option enqueue at front", list(TFI_ID), - FRONT, 0, QUEUE_EMPTY}, + FRONT, QUEUE_EMPTY}, }); } @@ -70,9 +64,6 @@ public class ItemEnqueuePositionCalculatorTest { public EnqueueLocation options; @Parameter(3) - public int posAmongAdded; // the position of feed item to be inserted among the list to be inserted. - - @Parameter(4) public List curQueue; public static final long TFI_ID = 101; @@ -88,7 +79,7 @@ public class ItemEnqueuePositionCalculatorTest { List queue = new ArrayList<>(curQueue); FeedItem tFI = createFeedItem(TFI_ID); doAddToQueueAndAssertResult(message, - calculator, posAmongAdded, tFI, queue, getCurrentlyPlaying(), + calculator, tFI, queue, getCurrentlyPlaying(), idsExpected); } @@ -102,40 +93,31 @@ public class ItemEnqueuePositionCalculatorTest { return Arrays.asList(new Object[][]{ {"case option after currently playing", list(11L, TFI_ID, 12L, 13L, 14L), - AFTER_CURRENTLY_PLAYING, 0, QUEUE_DEFAULT, 11L}, - {"case option after currently playing (2nd item)", - list(11L, 12L, TFI_ID, 13L, 14L), - AFTER_CURRENTLY_PLAYING, 1, QUEUE_DEFAULT, 11L}, + 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, 0, QUEUE_DEFAULT, 13L}, + 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, 0, QUEUE_DEFAULT, 99L}, + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 99L}, {"case option after currently playing, no currentlyPlaying is null", concat(TFI_ID, QUEUE_DEFAULT_IDS), - AFTER_CURRENTLY_PLAYING, 0, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NULL}, + 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, 0, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA}, + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA}, {"case empty queue, option after currently playing", list(TFI_ID), - AFTER_CURRENTLY_PLAYING, 0, QUEUE_EMPTY, ID_CURRENTLY_PLAYING_NULL}, + AFTER_CURRENTLY_PLAYING, QUEUE_EMPTY, ID_CURRENTLY_PLAYING_NULL}, }); } - @Parameter(5) - public long idCurrentlyPlaying = -1; + @Parameter(4) + public long idCurrentlyPlaying; @Override Playable getCurrentlyPlaying() { - if (ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA == idCurrentlyPlaying) { - return externalMedia(); - } - if (ID_CURRENTLY_PLAYING_NULL == idCurrentlyPlaying) { - return null; - } - return createFeedItem(idCurrentlyPlaying).getMedia(); + return ItemEnqueuePositionCalculatorTest.getCurrentlyPlaying(idCurrentlyPlaying); } private static Playable externalMedia() { @@ -150,6 +132,11 @@ public class ItemEnqueuePositionCalculatorTest { @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 data() { // Attempts to make test more readable by showing the expected list of ids @@ -158,15 +145,15 @@ public class ItemEnqueuePositionCalculatorTest { {"download order test, enqueue default", concat(QUEUE_DEFAULT_IDS, 101L), concat(QUEUE_DEFAULT_IDS, list(101L, 102L)), - concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L)), - concat(QUEUE_DEFAULT_IDS, list(101L, 102L, 201L, 202L)), - BACK, QUEUE_DEFAULT}, - {"download order test, enqueue at front", + 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, 102L, 201L), QUEUE_DEFAULT_IDS), - concat(list(101L, 102L, 201L, 202L), QUEUE_DEFAULT_IDS), - FRONT, QUEUE_DEFAULT}, + 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), + AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 11L} // 11 is at the front, currently playing }); } @@ -179,18 +166,14 @@ public class ItemEnqueuePositionCalculatorTest { @Parameter(2) public List idsExpectedAfter102; - // 2XX are for testing bulk insertion cases @Parameter(3) - public List idsExpectedAfter201; + public EnqueueLocation options; @Parameter(4) - public List idsExpectedAfter202; + public List queueInitial; @Parameter(5) - public EnqueueLocation options; - - @Parameter(6) - public List queueInitial; + public long idCurrentlyPlaying; @Test public void testQueueOrderWhenDownloading2Items() { @@ -209,30 +192,20 @@ public class ItemEnqueuePositionCalculatorTest { // Test body + Playable currentlyPlaying = getCurrentlyPlaying(idCurrentlyPlaying); + // User clicks download on feed item 101 FeedItem tFI101 = setAsDownloading(101, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (1st download)", - calculator, 0, tFI101, queue, + calculator, tFI101, queue, currentlyPlaying, idsExpectedAfter101); // Then user clicks download on feed item 102 FeedItem tFI102 = setAsDownloading(102, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", - calculator, 0, tFI102, queue, + calculator, tFI102, queue, currentlyPlaying, idsExpectedAfter102); - // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls - - FeedItem tFI201 = setAsDownloading(201, stubDownloadStateProvider); - doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", - calculator, 0, tFI201, queue, - idsExpectedAfter201); - - FeedItem tFI202 = setAsDownloading(202, stubDownloadStateProvider); - doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", - calculator, 1, tFI202, queue, null, - idsExpectedAfter202); - // TODO: simulate download failure cases. } @@ -258,21 +231,11 @@ public class ItemEnqueuePositionCalculatorTest { static void doAddToQueueAndAssertResult(String message, ItemEnqueuePositionCalculator calculator, - int positionAmongAdd, - FeedItem itemToAdd, - List queue, - List idsExpected) { - doAddToQueueAndAssertResult(message, calculator, positionAmongAdd, itemToAdd, queue, null, idsExpected); - } - - static void doAddToQueueAndAssertResult(String message, - ItemEnqueuePositionCalculator calculator, - int positionAmongAdd, FeedItem itemToAdd, List queue, Playable currentlyPlaying, List idsExpected) { - int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue, currentlyPlaying); + int posActual = calculator.calcPosition(queue, currentlyPlaying); queue.add(posActual, itemToAdd); assertEquals(message, idsExpected, getIdList(queue)); } @@ -286,6 +249,24 @@ public class ItemEnqueuePositionCalculatorTest { 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()); -- cgit v1.2.3 From 6e019f72de951acb21cec433a4da6cb64a103082 Mon Sep 17 00:00:00 2001 From: orionlee Date: Tue, 5 Nov 2019 10:14:07 -0800 Subject: code style / comment tweak per review --- .../core/storage/ItemEnqueuePositionCalculatorTest.java | 8 -------- 1 file changed, 8 deletions(-) (limited to 'core/src/test/java') 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 68eb434a4..40ffd26b4 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 @@ -189,17 +189,13 @@ public class ItemEnqueuePositionCalculatorTest { // A shallow copy, as the test code will manipulate the queue List queue = new ArrayList<>(queueInitial); - // Test body - Playable currentlyPlaying = getCurrentlyPlaying(idCurrentlyPlaying); - // User clicks download on feed item 101 FeedItem tFI101 = setAsDownloading(101, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (1st download)", calculator, tFI101, queue, currentlyPlaying, idsExpectedAfter101); - // Then user clicks download on feed item 102 FeedItem tFI102 = setAsDownloading(102, stubDownloadStateProvider); doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", @@ -225,10 +221,6 @@ public class ItemEnqueuePositionCalculatorTest { } - // Common helpers: - // - common queue (of items) for tests - // - construct FeedItems for tests - static void doAddToQueueAndAssertResult(String message, ItemEnqueuePositionCalculator calculator, FeedItem itemToAdd, -- cgit v1.2.3 From 9d6db7b9fc2cbdd66f43dcb61aea9c350a6bcf9f Mon Sep 17 00:00:00 2001 From: orionlee Date: Tue, 5 Nov 2019 10:38:31 -0800 Subject: enqueue respect download order: add test case for download failures. --- .../storage/ItemEnqueuePositionCalculatorTest.java | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'core/src/test/java') 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 40ffd26b4..17b88bdd2 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 @@ -145,14 +145,20 @@ public class ItemEnqueuePositionCalculatorTest { {"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 }); } @@ -167,12 +173,15 @@ public class ItemEnqueuePositionCalculatorTest { public List idsExpectedAfter102; @Parameter(3) - public EnqueueLocation options; + public List idsExpectedAfter103; @Parameter(4) - public List queueInitial; + public EnqueueLocation options; @Parameter(5) + public List queueInitial; + + @Parameter(6) public long idCurrentlyPlaying; @Test @@ -192,32 +201,45 @@ public class ItemEnqueuePositionCalculatorTest { // Test body Playable currentlyPlaying = getCurrentlyPlaying(idCurrentlyPlaying); // User clicks download on feed item 101 - FeedItem tFI101 = setAsDownloading(101, stubDownloadStateProvider); + 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); + 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); - // TODO: simulate download failure cases. } - private static FeedItem setAsDownloading(int id, DownloadStateProvider stubDownloadStateProvider) { + 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); + } - stub(stubDownloadStateProvider.isDownloadingFile(media)).toReturn(true); - + private static FeedItem setAsDownloading(FeedItem item, DownloadStateProvider stubDownloadStateProvider, + boolean isDownloading) { + stub(stubDownloadStateProvider.isDownloadingFile(item.getMedia())).toReturn(isDownloading); return item; } + } -- cgit v1.2.3 From 3c6540b82edfca999ceb98f614d70431a9d8080d Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Fri, 15 Nov 2019 15:41:26 +0100 Subject: Added pubDate test --- .../java/de/danoeh/antennapod/core/feed/FeedItemTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'core/src/test/java') 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. */ -- cgit v1.2.3