From 2143ab135182434911d4554a8ef08115eaa0d2d0 Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Thu, 4 Apr 2024 22:26:53 +0200 Subject: Move some tests from core module to their respective module (#7059) --- .../antennapod/model/feed/FeedFilterTest.java | 156 +++++++++++++++++++++ .../antennapod/model/feed/FeedItemMother.java | 16 +++ .../danoeh/antennapod/model/feed/FeedItemTest.java | 138 ++++++++++++++++++ .../antennapod/model/feed/FeedMediaMother.java | 13 ++ .../antennapod/model/feed/FeedMediaTest.java | 72 ++++++++++ .../danoeh/antennapod/model/feed/FeedMother.java | 12 ++ .../de/danoeh/antennapod/model/feed/FeedTest.java | 58 ++++++++ 7 files changed, 465 insertions(+) create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedFilterTest.java create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemMother.java create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemTest.java create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaMother.java create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaTest.java create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedMother.java create mode 100644 model/src/test/java/de/danoeh/antennapod/model/feed/FeedTest.java (limited to 'model/src') diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedFilterTest.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedFilterTest.java new file mode 100644 index 000000000..c58450643 --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedFilterTest.java @@ -0,0 +1,156 @@ +package de.danoeh.antennapod.model.feed; + +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class FeedFilterTest { + + @Test + public void testNullFilter() { + FeedFilter filter = new FeedFilter(); + FeedItem item = new FeedItem(); + item.setTitle("Hello world"); + + assertFalse(filter.excludeOnly()); + assertFalse(filter.includeOnly()); + assertEquals("", filter.getExcludeFilterRaw()); + assertEquals("", filter.getIncludeFilterRaw()); + assertTrue(filter.shouldAutoDownload(item)); + } + + @Test + public void testBasicIncludeFilter() { + String includeFilter = "Hello"; + FeedFilter filter = new FeedFilter(includeFilter, ""); + FeedItem item = new FeedItem(); + item.setTitle("Hello world"); + + FeedItem item2 = new FeedItem(); + item2.setTitle("Don't include me"); + + assertFalse(filter.excludeOnly()); + assertTrue(filter.includeOnly()); + assertEquals("", filter.getExcludeFilterRaw()); + assertEquals(includeFilter, filter.getIncludeFilterRaw()); + assertTrue(filter.shouldAutoDownload(item)); + assertFalse(filter.shouldAutoDownload(item2)); + } + + @Test + public void testBasicExcludeFilter() { + String excludeFilter = "Hello"; + FeedFilter filter = new FeedFilter("", excludeFilter); + FeedItem item = new FeedItem(); + item.setTitle("Hello world"); + + FeedItem item2 = new FeedItem(); + item2.setTitle("Item2"); + + assertTrue(filter.excludeOnly()); + assertFalse(filter.includeOnly()); + assertEquals(excludeFilter, filter.getExcludeFilterRaw()); + assertEquals("", filter.getIncludeFilterRaw()); + assertFalse(filter.shouldAutoDownload(item)); + assertTrue(filter.shouldAutoDownload(item2)); + } + + @Test + public void testComplexIncludeFilter() { + String includeFilter = "Hello \n\"Two words\""; + FeedFilter filter = new FeedFilter(includeFilter, ""); + FeedItem item = new FeedItem(); + item.setTitle("hello world"); + + FeedItem item2 = new FeedItem(); + item2.setTitle("Two three words"); + + FeedItem item3 = new FeedItem(); + item3.setTitle("One two words"); + + assertFalse(filter.excludeOnly()); + assertTrue(filter.includeOnly()); + assertEquals("", filter.getExcludeFilterRaw()); + assertEquals(includeFilter, filter.getIncludeFilterRaw()); + assertTrue(filter.shouldAutoDownload(item)); + assertFalse(filter.shouldAutoDownload(item2)); + assertTrue(filter.shouldAutoDownload(item3)); + } + + @Test + public void testComplexExcludeFilter() { + String excludeFilter = "Hello \"Two words\""; + FeedFilter filter = new FeedFilter("", excludeFilter); + FeedItem item = new FeedItem(); + item.setTitle("hello world"); + + FeedItem item2 = new FeedItem(); + item2.setTitle("One three words"); + + FeedItem item3 = new FeedItem(); + item3.setTitle("One two words"); + + assertTrue(filter.excludeOnly()); + assertFalse(filter.includeOnly()); + assertEquals(excludeFilter, filter.getExcludeFilterRaw()); + assertEquals("", filter.getIncludeFilterRaw()); + assertFalse(filter.shouldAutoDownload(item)); + assertTrue(filter.shouldAutoDownload(item2)); + assertFalse(filter.shouldAutoDownload(item3)); + } + + @Test + public void testComboFilter() { + String includeFilter = "Hello world"; + String excludeFilter = "dislike"; + FeedFilter filter = new FeedFilter(includeFilter, excludeFilter); + + FeedItem download = new FeedItem(); + download.setTitle("Hello everyone!"); + // because, while it has words from the include filter it also has exclude words + FeedItem doNotDownload = new FeedItem(); + doNotDownload.setTitle("I dislike the world"); + // because it has no words from the include filter + FeedItem doNotDownload2 = new FeedItem(); + doNotDownload2.setTitle("no words to include"); + + assertTrue(filter.hasExcludeFilter()); + assertTrue(filter.hasIncludeFilter()); + assertTrue(filter.shouldAutoDownload(download)); + assertFalse(filter.shouldAutoDownload(doNotDownload)); + assertFalse(filter.shouldAutoDownload(doNotDownload2)); + } + + @Test + public void testMinimalDurationFilter() { + FeedItem download = new FeedItem(); + download.setTitle("Hello friend!"); + FeedMedia downloadMedia = FeedMediaMother.anyFeedMedia(); + downloadMedia.setDuration((int) TimeUnit.MILLISECONDS.convert(5, TimeUnit.MINUTES)); + download.setMedia(downloadMedia); + // because duration of the media in unknown + FeedItem download2 = new FeedItem(); + download2.setTitle("Hello friend!"); + FeedMedia unknownDurationMedia = FeedMediaMother.anyFeedMedia(); + download2.setMedia(unknownDurationMedia); + // because it is not long enough + FeedItem doNotDownload = new FeedItem(); + doNotDownload.setTitle("Hello friend!"); + FeedMedia doNotDownloadMedia = FeedMediaMother.anyFeedMedia(); + doNotDownloadMedia.setDuration((int) TimeUnit.MILLISECONDS.convert(2, TimeUnit.MINUTES)); + doNotDownload.setMedia(doNotDownloadMedia); + + int minimalDurationFilter = 3 * 60; + FeedFilter filter = new FeedFilter("", "", minimalDurationFilter); + + assertTrue(filter.hasMinimalDurationFilter()); + assertTrue(filter.shouldAutoDownload(download)); + assertFalse(filter.shouldAutoDownload(doNotDownload)); + assertTrue(filter.shouldAutoDownload(download2)); + } + +} diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemMother.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemMother.java new file mode 100644 index 000000000..cf271b31d --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemMother.java @@ -0,0 +1,16 @@ +package de.danoeh.antennapod.model.feed; + +import java.util.Date; + +import static de.danoeh.antennapod.model.feed.FeedMother.anyFeed; + +class FeedItemMother { + private static final String IMAGE_URL = "http://example.com/image"; + + static FeedItem anyFeedItemWithImage() { + FeedItem item = new FeedItem(0, "Item", "Item", "url", new Date(), FeedItem.PLAYED, anyFeed()); + item.setImageUrl(IMAGE_URL); + return item; + } + +} diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemTest.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemTest.java new file mode 100644 index 000000000..633176eb2 --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedItemTest.java @@ -0,0 +1,138 @@ +package de.danoeh.antennapod.model.feed; + +import org.junit.Before; +import org.junit.Test; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import static de.danoeh.antennapod.model.feed.FeedItemMother.anyFeedItemWithImage; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class FeedItemTest { + + private static final String TEXT_LONG = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; + private static final String TEXT_SHORT = "Lorem ipsum"; + + private FeedItem original; + private FeedItem changedFeedItem; + + @Before + public void setUp() { + original = anyFeedItemWithImage(); + changedFeedItem = anyFeedItemWithImage(); + } + + @Test + public void testUpdateFromOther_feedItemImageDownloadUrlChanged() { + setNewFeedItemImageDownloadUrl(); + original.updateFromOther(changedFeedItem); + assertFeedItemImageWasUpdated(); + } + + @Test + public void testUpdateFromOther_feedItemImageRemoved() { + feedItemImageRemoved(); + original.updateFromOther(changedFeedItem); + assertFeedItemImageWasNotUpdated(); + } + + @Test + public void testUpdateFromOther_feedItemImageAdded() { + original.setImageUrl(null); + setNewFeedItemImageDownloadUrl(); + original.updateFromOther(changedFeedItem); + 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. + */ + @Test + public void testMarkPlayedItemAsNew_itemNotPlayed() { + original.setPlayed(true); + original.setNew(); + + assertFalse(original.isPlayed()); + } + + /** + * Test that a new item loses that state after being marked as played. + */ + @Test + public void testMarkNewItemAsPlayed_itemNotNew() { + original.setNew(); + original.setPlayed(true); + + assertFalse(original.isNew()); + } + + /** + * Test that a new item loses that state after being marked as not played. + */ + @Test + public void testMarkNewItemAsNotPlayed_itemNotNew() { + original.setNew(); + original.setPlayed(false); + + assertFalse(original.isNew()); + } + + private void setNewFeedItemImageDownloadUrl() { + changedFeedItem.setImageUrl("http://example.com/new_picture"); + } + + private void feedItemImageRemoved() { + changedFeedItem.setImageUrl(null); + } + + private void assertFeedItemImageWasUpdated() { + assertEquals(original.getImageUrl(), changedFeedItem.getImageUrl()); + } + + private void assertFeedItemImageWasNotUpdated() { + assertEquals(anyFeedItemWithImage().getImageUrl(), original.getImageUrl()); + } + + /** + * If one of `description` or `content:encoded` is null, use the other one. + */ + @Test + public void testShownotesNullValues() { + testShownotes(null, TEXT_LONG); + testShownotes(TEXT_LONG, null); + } + + /** + * If `description` is reasonably longer than `content:encoded`, use `description`. + */ + @Test + public void testShownotesLength() { + testShownotes(TEXT_SHORT, TEXT_LONG); + testShownotes(TEXT_LONG, TEXT_SHORT); + } + + /** + * Checks if the shownotes equal TEXT_LONG, using the given `description` and `content:encoded`. + * + * @param description Description of the feed item + * @param contentEncoded `content:encoded` of the feed item + */ + private void testShownotes(String description, String contentEncoded) { + FeedItem item = new FeedItem(); + item.setDescriptionIfLonger(description); + item.setDescriptionIfLonger(contentEncoded); + assertEquals(TEXT_LONG, item.getDescription()); + } +} diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaMother.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaMother.java new file mode 100644 index 000000000..f18f43f18 --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaMother.java @@ -0,0 +1,13 @@ +package de.danoeh.antennapod.model.feed; + +class FeedMediaMother { + + private static final String EPISODE_URL = "http://example.com/episode"; + private static final long SIZE = 42; + private static final String MIME_TYPE = "audio/mp3"; + + static FeedMedia anyFeedMedia() { + return new FeedMedia(null, EPISODE_URL, SIZE, MIME_TYPE); + } + +} diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaTest.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaTest.java new file mode 100644 index 000000000..ef2792edc --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMediaTest.java @@ -0,0 +1,72 @@ +package de.danoeh.antennapod.model.feed; + +import org.junit.Before; +import org.junit.Test; + +import static de.danoeh.antennapod.model.feed.FeedMediaMother.anyFeedMedia; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class FeedMediaTest { + + private FeedMedia media; + + @Before + public void setUp() { + media = anyFeedMedia(); + } + + /** + * Downloading a media from a not new and not played item should not change the item state. + */ + @Test + public void testDownloadMediaOfNotNewAndNotPlayedItem_unchangedItemState() { + FeedItem item = mock(FeedItem.class); + when(item.isNew()).thenReturn(false); + when(item.isPlayed()).thenReturn(false); + + media.setItem(item); + media.setDownloaded(true); + + verify(item, never()).setNew(); + verify(item, never()).setPlayed(true); + verify(item, never()).setPlayed(false); + } + + /** + * Downloading a media from a played item (thus not new) should not change the item state. + */ + @Test + public void testDownloadMediaOfPlayedItem_unchangedItemState() { + FeedItem item = mock(FeedItem.class); + when(item.isNew()).thenReturn(false); + when(item.isPlayed()).thenReturn(true); + + media.setItem(item); + media.setDownloaded(true); + + verify(item, never()).setNew(); + verify(item, never()).setPlayed(true); + verify(item, never()).setPlayed(false); + } + + /** + * Downloading a media from a new item (thus not played) should change the item to not played. + */ + @Test + public void testDownloadMediaOfNewItem_changedToNotPlayedItem() { + FeedItem item = mock(FeedItem.class); + when(item.isNew()).thenReturn(true); + when(item.isPlayed()).thenReturn(false); + + media.setItem(item); + media.setDownloaded(true); + + verify(item).setPlayed(false); + verify(item, never()).setNew(); + verify(item, never()).setPlayed(true); + } + +} diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMother.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMother.java new file mode 100644 index 000000000..ddb3b7fa8 --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedMother.java @@ -0,0 +1,12 @@ +package de.danoeh.antennapod.model.feed; + +public class FeedMother { + public static final String IMAGE_URL = "http://example.com/image"; + + public static Feed anyFeed() { + return new Feed(0, null, "title", "http://example.com", "This is the description", + "http://example.com/payment", "Daniel", "en", null, "http://example.com/feed", IMAGE_URL, + null, "http://example.com/feed", System.currentTimeMillis()); + } + +} diff --git a/model/src/test/java/de/danoeh/antennapod/model/feed/FeedTest.java b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedTest.java new file mode 100644 index 000000000..34c5e76d9 --- /dev/null +++ b/model/src/test/java/de/danoeh/antennapod/model/feed/FeedTest.java @@ -0,0 +1,58 @@ +package de.danoeh.antennapod.model.feed; + +import org.junit.Before; +import org.junit.Test; + +import static de.danoeh.antennapod.model.feed.FeedMother.anyFeed; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +public class FeedTest { + + private Feed original; + private Feed changedFeed; + + @Before + public void setUp() { + original = anyFeed(); + changedFeed = anyFeed(); + } + + @Test + public void testUpdateFromOther_feedImageDownloadUrlChanged() { + changedFeed.setImageUrl("http://example.com/new_picture"); + original.updateFromOther(changedFeed); + assertEquals(original.getImageUrl(), changedFeed.getImageUrl()); + } + + @Test + public void testUpdateFromOther_feedImageRemoved() { + changedFeed.setImageUrl(null); + original.updateFromOther(changedFeed); + assertEquals(anyFeed().getImageUrl(), original.getImageUrl()); + } + + @Test + public void testUpdateFromOther_feedImageAdded() { + original.setImageUrl(null); + changedFeed.setImageUrl("http://example.com/new_picture"); + original.updateFromOther(changedFeed); + assertEquals(original.getImageUrl(), changedFeed.getImageUrl()); + } + + @Test + public void testSetSortOrder_OnlyIntraFeedSortAllowed() { + for (SortOrder sortOrder : SortOrder.values()) { + if (sortOrder.scope == SortOrder.Scope.INTRA_FEED) { + original.setSortOrder(sortOrder); // should be okay + } else { + assertThrows(IllegalArgumentException.class, () -> original.setSortOrder(sortOrder)); + } + } + } + + @Test + public void testSetSortOrder_NullAllowed() { + original.setSortOrder(null); // should be okay + } +} \ No newline at end of file -- cgit v1.2.3