summaryrefslogtreecommitdiff
path: root/storage/database
diff options
context:
space:
mode:
authorByteHamster <ByteHamster@users.noreply.github.com>2024-04-04 22:26:53 +0200
committerGitHub <noreply@github.com>2024-04-04 22:26:53 +0200
commit2143ab135182434911d4554a8ef08115eaa0d2d0 (patch)
treeb380fdf38b34a032c5ea0ed5b5fa22dce7433723 /storage/database
parent0288d4e51eb7eef565be8d814fb8c152383e5031 (diff)
downloadAntennaPod-2143ab135182434911d4554a8ef08115eaa0d2d0.zip
Move some tests from core module to their respective module (#7059)
Diffstat (limited to 'storage/database')
-rw-r--r--storage/database/src/test/java/de/danoeh/antennapod/storage/database/CollectionTestUtil.java30
-rw-r--r--storage/database/src/test/java/de/danoeh/antennapod/storage/database/FeedItemDuplicateGuesserTest.java70
-rw-r--r--storage/database/src/test/java/de/danoeh/antennapod/storage/database/ItemEnqueuePositionCalculatorTest.java182
3 files changed, 282 insertions, 0 deletions
diff --git a/storage/database/src/test/java/de/danoeh/antennapod/storage/database/CollectionTestUtil.java b/storage/database/src/test/java/de/danoeh/antennapod/storage/database/CollectionTestUtil.java
new file mode 100644
index 000000000..244348c2a
--- /dev/null
+++ b/storage/database/src/test/java/de/danoeh/antennapod/storage/database/CollectionTestUtil.java
@@ -0,0 +1,30 @@
+package de.danoeh.antennapod.storage.database;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class CollectionTestUtil {
+
+ public static <T> List<? extends T> concat(T item, List<? extends T> list) {
+ List<T> res = new ArrayList<>(list);
+ res.add(0, item);
+ return res;
+ }
+
+ public static <T> List<? extends T> concat(List<? extends T> list, T item) {
+ List<T> res = new ArrayList<>(list);
+ res.add(item);
+ return res;
+ }
+
+ public static <T> List<? extends T> concat(List<? extends T> list1, List<? extends T> list2) {
+ List<T> res = new ArrayList<>(list1);
+ res.addAll(list2);
+ return res;
+ }
+
+ public static <T> List<T> list(T... a) {
+ return Arrays.asList(a);
+ }
+}
diff --git a/storage/database/src/test/java/de/danoeh/antennapod/storage/database/FeedItemDuplicateGuesserTest.java b/storage/database/src/test/java/de/danoeh/antennapod/storage/database/FeedItemDuplicateGuesserTest.java
new file mode 100644
index 000000000..52140bccf
--- /dev/null
+++ b/storage/database/src/test/java/de/danoeh/antennapod/storage/database/FeedItemDuplicateGuesserTest.java
@@ -0,0 +1,70 @@
+package de.danoeh.antennapod.storage.database;
+
+import de.danoeh.antennapod.model.feed.FeedItem;
+import de.danoeh.antennapod.model.feed.FeedMedia;
+import de.danoeh.antennapod.storage.database.FeedItemDuplicateGuesser;
+import org.junit.Test;
+
+import java.util.Date;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test class for {@link FeedItemDuplicateGuesser}.
+ */
+public class FeedItemDuplicateGuesserTest {
+ private static final long MINUTES = 1000 * 60;
+ private static final long DAYS = 24 * 60 * MINUTES;
+
+ @Test
+ public void testSameId() {
+ assertTrue(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id", "Title1", "example.com/episode1", 0, 5 * MINUTES, "audio/*"),
+ item("id", "Title2", "example.com/episode2", 0, 20 * MINUTES, "video/*")));
+ }
+
+ @Test
+ public void testDuplicateDownloadUrl() {
+ assertTrue(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title1", "example.com/episode", 0, 5 * MINUTES, "audio/*"),
+ item("id2", "Title2", "example.com/episode", 0, 5 * MINUTES, "audio/*")));
+ assertFalse(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title1", "example.com/episode1", 0, 5 * MINUTES, "audio/*"),
+ item("id2", "Title2", "example.com/episode2", 0, 5 * MINUTES, "audio/*")));
+ }
+
+ @Test
+ public void testOtherAttributes() {
+ assertTrue(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title", "example.com/episode1", 10, 5 * MINUTES, "audio/*"),
+ item("id2", "Title", "example.com/episode2", 10, 5 * MINUTES, "audio/*")));
+ assertTrue(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title", "example.com/episode1", 10, 5 * MINUTES, "audio/*"),
+ item("id2", "Title", "example.com/episode2", 20, 6 * MINUTES, "audio/*")));
+ assertFalse(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title", "example.com/episode1", 10, 5 * MINUTES, "audio/*"),
+ item("id2", "Title", "example.com/episode2", 10, 5 * MINUTES, "video/*")));
+ assertTrue(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title", "example.com/episode1", 10, 5 * MINUTES, "audio/mpeg"),
+ item("id2", "Title", "example.com/episode2", 10, 5 * MINUTES, "audio/mp3")));
+ assertFalse(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title", "example.com/episode1", 5 * DAYS, 5 * MINUTES, "audio/*"),
+ item("id2", "Title", "example.com/episode2", 2 * DAYS, 5 * MINUTES, "audio/*")));
+ }
+
+ @Test
+ public void testNoMediaType() {
+ assertTrue(FeedItemDuplicateGuesser.seemDuplicates(
+ item("id1", "Title", "example.com/episode1", 2 * DAYS, 5 * MINUTES, ""),
+ item("id2", "Title", "example.com/episode2", 2 * DAYS, 5 * MINUTES, "")));
+ }
+
+ private FeedItem item(String guid, String title, String downloadUrl,
+ long date, long duration, String mime) {
+ FeedItem item = new FeedItem(0, title, guid, "link", new Date(date), FeedItem.PLAYED, null);
+ FeedMedia media = new FeedMedia(item, downloadUrl, duration, mime);
+ item.setMedia(media);
+ return item;
+ }
+} \ No newline at end of file
diff --git a/storage/database/src/test/java/de/danoeh/antennapod/storage/database/ItemEnqueuePositionCalculatorTest.java b/storage/database/src/test/java/de/danoeh/antennapod/storage/database/ItemEnqueuePositionCalculatorTest.java
new file mode 100644
index 000000000..9b519b6aa
--- /dev/null
+++ b/storage/database/src/test/java/de/danoeh/antennapod/storage/database/ItemEnqueuePositionCalculatorTest.java
@@ -0,0 +1,182 @@
+package de.danoeh.antennapod.storage.database;
+
+import de.danoeh.antennapod.model.feed.Feed;
+import de.danoeh.antennapod.model.playback.RemoteMedia;
+import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface;
+import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterfaceStub;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import de.danoeh.antennapod.model.feed.FeedItem;
+import de.danoeh.antennapod.model.feed.FeedMedia;
+import de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation;
+import de.danoeh.antennapod.model.playback.Playable;
+
+import static de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation.AFTER_CURRENTLY_PLAYING;
+import static de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation.BACK;
+import static de.danoeh.antennapod.storage.preferences.UserPreferences.EnqueueLocation.FRONT;
+import static de.danoeh.antennapod.storage.database.CollectionTestUtil.concat;
+import static de.danoeh.antennapod.storage.database.CollectionTestUtil.list;
+import static java.util.Collections.emptyList;
+import static org.junit.Assert.assertEquals;
+
+public class ItemEnqueuePositionCalculatorTest {
+
+ @RunWith(Parameterized.class)
+ public static class BasicTest {
+ @Parameters(name = "{index}: case<{0}>, expected:{1}")
+ public static Iterable<Object[]> data() {
+ return Arrays.asList(new Object[][]{
+ {"case default, i.e., add to the end",
+ concat(QUEUE_DEFAULT_IDS, TFI_ID),
+ BACK, QUEUE_DEFAULT},
+ {"case option enqueue at front",
+ concat(TFI_ID, QUEUE_DEFAULT_IDS),
+ FRONT, QUEUE_DEFAULT},
+ {"case empty queue, option default",
+ list(TFI_ID),
+ BACK, QUEUE_EMPTY},
+ {"case empty queue, option enqueue at front",
+ list(TFI_ID),
+ FRONT, QUEUE_EMPTY},
+ });
+ }
+
+ @Parameter
+ public String message;
+
+ @Parameter(1)
+ public List<Long> idsExpected;
+
+ @Parameter(2)
+ public EnqueueLocation options;
+
+ @Parameter(3)
+ public List<FeedItem> curQueue;
+
+ public static final long TFI_ID = 101;
+
+ /**
+ * Add a FeedItem with ID {@link #TFI_ID} with the setup
+ */
+ @Test
+ public void test() {
+ DownloadServiceInterface.setImpl(new DownloadServiceInterfaceStub());
+ ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options);
+
+ // shallow copy to which the test will add items
+ List<FeedItem> queue = new ArrayList<>(curQueue);
+ FeedItem tFI = createFeedItem(TFI_ID);
+ doAddToQueueAndAssertResult(message,
+ calculator, tFI, queue, getCurrentlyPlaying(),
+ idsExpected);
+ }
+
+ Playable getCurrentlyPlaying() {
+ return null;
+ }
+ }
+
+ @RunWith(Parameterized.class)
+ public static class AfterCurrentlyPlayingTest extends BasicTest {
+ @Parameters(name = "{index}: case<{0}>, expected:{1}")
+ public static Iterable<Object[]> data() {
+ return Arrays.asList(new Object[][]{
+ {"case option after currently playing",
+ list(11L, TFI_ID, 12L, 13L, 14L),
+ AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 11L},
+ {"case option after currently playing, currently playing in the middle of the queue",
+ list(11L, 12L, 13L, TFI_ID, 14L),
+ AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 13L},
+ {"case option after currently playing, currently playing is not in queue",
+ concat(TFI_ID, QUEUE_DEFAULT_IDS),
+ AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, 99L},
+ {"case option after currently playing, no currentlyPlaying is null",
+ concat(TFI_ID, QUEUE_DEFAULT_IDS),
+ AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NULL},
+ {"case option after currently playing, currentlyPlaying is not a feedMedia",
+ concat(TFI_ID, QUEUE_DEFAULT_IDS),
+ AFTER_CURRENTLY_PLAYING, QUEUE_DEFAULT, ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA},
+ {"case empty queue, option after currently playing",
+ list(TFI_ID),
+ AFTER_CURRENTLY_PLAYING, QUEUE_EMPTY, ID_CURRENTLY_PLAYING_NULL},
+ });
+ }
+
+ @Parameter(4)
+ public long idCurrentlyPlaying;
+
+ @Override
+ Playable getCurrentlyPlaying() {
+ return ItemEnqueuePositionCalculatorTest.getCurrentlyPlaying(idCurrentlyPlaying);
+ }
+
+ private static final long ID_CURRENTLY_PLAYING_NULL = -1L;
+ private static final long ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA = -9999L;
+
+ }
+
+ static void doAddToQueueAndAssertResult(String message,
+ ItemEnqueuePositionCalculator calculator,
+ FeedItem itemToAdd,
+ List<FeedItem> queue,
+ Playable currentlyPlaying,
+ List<Long> idsExpected) {
+ int posActual = calculator.calcPosition(queue, currentlyPlaying);
+ queue.add(posActual, itemToAdd);
+ assertEquals(message, idsExpected.size(), queue.size());
+ for (int i = 0; i < idsExpected.size(); i++) {
+ assertEquals(message, (long) idsExpected.get(i), queue.get(i).getId());
+ }
+ }
+
+ static final List<FeedItem> QUEUE_EMPTY = Collections.unmodifiableList(emptyList());
+
+ static final List<FeedItem> QUEUE_DEFAULT =
+ Collections.unmodifiableList(Arrays.asList(
+ createFeedItem(11), createFeedItem(12), createFeedItem(13), createFeedItem(14)));
+ static final List<Long> QUEUE_DEFAULT_IDS =
+ QUEUE_DEFAULT.stream().map(FeedItem::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 RemoteMedia(createFeedItem(0));
+ }
+
+ static final long ID_CURRENTLY_PLAYING_NULL = -1L;
+ static final long ID_CURRENTLY_PLAYING_NOT_FEEDMEDIA = -9999L;
+
+
+ static FeedItem createFeedItem(long id) {
+ Feed feed = new Feed(0, null, "title", "http://example.com", "This is the description",
+ "http://example.com/payment", "Daniel", "en", null, "http://example.com/feed",
+ "http://example.com/image", null, "http://example.com/feed", System.currentTimeMillis());
+ FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url",
+ new Date(), FeedItem.PLAYED, feed);
+ FeedMedia media = new FeedMedia(item, "http://download.url.net/" + id, 1234567, "audio/mpeg");
+ media.setId(item.getId());
+ item.setMedia(media);
+ return item;
+ }
+
+}