diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2024-04-04 22:26:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-04 22:26:53 +0200 |
commit | 2143ab135182434911d4554a8ef08115eaa0d2d0 (patch) | |
tree | b380fdf38b34a032c5ea0ed5b5fa22dce7433723 /core/src/test | |
parent | 0288d4e51eb7eef565be8d814fb8c152383e5031 (diff) | |
download | AntennaPod-2143ab135182434911d4554a8ef08115eaa0d2d0.zip |
Move some tests from core module to their respective module (#7059)
Diffstat (limited to 'core/src/test')
19 files changed, 0 insertions, 2963 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedFilterTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedFilterTest.java deleted file mode 100644 index cc8e1636c..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedFilterTest.java +++ /dev/null @@ -1,159 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.ui.common.Converter; -import de.danoeh.antennapod.model.feed.FeedFilter; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; - -import org.junit.Test; - -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(Converter.durationStringShortToMs("05:00", false)); - 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(Converter.durationStringShortToMs("02:00", false)); - 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/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java deleted file mode 100644 index cd96dca89..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemMother.java +++ /dev/null @@ -1,18 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.FeedItem; - -import java.util.Date; - -import static de.danoeh.antennapod.core.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/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java deleted file mode 100644 index 62775b84b..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedItemTest.java +++ /dev/null @@ -1,139 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.FeedItem; -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; - -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/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java deleted file mode 100644 index 35a85af02..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java +++ /dev/null @@ -1,15 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.FeedMedia; - -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/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java deleted file mode 100644 index 43d855cc4..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; -import org.junit.Before; -import org.junit.Test; - -import static de.danoeh.antennapod.core.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/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java deleted file mode 100644 index 7616f8f91..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMother.java +++ /dev/null @@ -1,14 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.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/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java deleted file mode 100644 index 078f29ad7..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.Feed; -import org.junit.Before; -import org.junit.Test; - -import de.danoeh.antennapod.model.feed.SortOrder; - -import static de.danoeh.antennapod.core.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 diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithmTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithmTest.java deleted file mode 100644 index 946181d52..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/APCleanupAlgorithmTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import org.junit.Test; - -import java.text.SimpleDateFormat; -import java.util.Date; - -import static org.junit.Assert.assertEquals; - -public class APCleanupAlgorithmTest { - - @Test - public void testCalcMostRecentDateForDeletion() throws Exception { - APCleanupAlgorithm algo = new APCleanupAlgorithm(24); - Date curDateForTest = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse("2018-11-13T14:08:56-0800"); - Date resExpected = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").parse("2018-11-12T14:08:56-0800"); - Date resActual = algo.calcMostRecentDateForDeletion(curDateForTest); - assertEquals("cutoff for retaining most recent 1 day", resExpected, resActual); - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbCleanupTests.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbCleanupTests.java deleted file mode 100644 index a29a87b05..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbCleanupTests.java +++ /dev/null @@ -1,234 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import android.content.Context; -import android.content.SharedPreferences; -import androidx.preference.PreferenceManager; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import androidx.test.platform.app.InstrumentationRegistry; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; -import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager; -import de.danoeh.antennapod.storage.database.DBWriter; -import de.danoeh.antennapod.storage.preferences.PlaybackPreferences; -import de.danoeh.antennapod.storage.preferences.SynchronizationSettings; -import de.danoeh.antennapod.storage.preferences.UserPreferences; - -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import static de.danoeh.antennapod.core.storage.DbTestUtils.saveFeedlist; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Test class for DBTasks. - */ -@RunWith(RobolectricTestRunner.class) -public class DbCleanupTests { - - static final int EPISODE_CACHE_SIZE = 5; - private int cleanupAlgorithm; - - Context context; - - private File destFolder; - - public DbCleanupTests() { - setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_DEFAULT); - } - - protected void setCleanupAlgorithm(int cleanupAlgorithm) { - this.cleanupAlgorithm = cleanupAlgorithm; - } - - @Before - public void setUp() { - context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - destFolder = new File(context.getCacheDir(), "DbCleanupTests"); - //noinspection ResultOfMethodCallIgnored - destFolder.mkdir(); - cleanupDestFolder(destFolder); - assertNotNull(destFolder); - assertTrue(destFolder.exists()); - assertTrue(destFolder.canWrite()); - - // create new database - PodDBAdapter.init(context); - PodDBAdapter.deleteDatabase(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.close(); - - SharedPreferences.Editor prefEdit = PreferenceManager - .getDefaultSharedPreferences(context.getApplicationContext()).edit(); - prefEdit.putString(UserPreferences.PREF_EPISODE_CACHE_SIZE, Integer.toString(EPISODE_CACHE_SIZE)); - prefEdit.putString(UserPreferences.PREF_EPISODE_CLEANUP, Integer.toString(cleanupAlgorithm)); - prefEdit.putBoolean(UserPreferences.PREF_ENABLE_AUTODL, true); - prefEdit.commit(); - - UserPreferences.init(context); - PlaybackPreferences.init(context); - SynchronizationSettings.init(context); - AutoDownloadManager.setInstance(new AutoDownloadManagerImpl()); - } - - @After - public void tearDown() { - cleanupDestFolder(destFolder); - assertTrue(destFolder.delete()); - - DBWriter.tearDownTests(); - PodDBAdapter.tearDownTests(); - } - - private void cleanupDestFolder(File destFolder) { - //noinspection ConstantConditions - for (File f : destFolder.listFiles()) { - assertTrue(f.delete()); - } - } - - @Test - public void testPerformAutoCleanupShouldDelete() throws IOException { - final int numItems = EPISODE_CACHE_SIZE * 2; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numItems, feed, items, files, FeedItem.PLAYED, false, false); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (int i = 0; i < files.size(); i++) { - if (i < EPISODE_CACHE_SIZE) { - assertTrue(files.get(i).exists()); - } else { - assertFalse(files.get(i).exists()); - } - } - } - - @SuppressWarnings("SameParameterValue") - void populateItems(final int numItems, Feed feed, List<FeedItem> items, - List<File> files, int itemState, boolean addToQueue, - boolean addToFavorites) throws IOException { - for (int i = 0; i < numItems; i++) { - Date itemDate = new Date(numItems - i); - Date playbackCompletionDate = null; - if (itemState == FeedItem.PLAYED) { - playbackCompletionDate = itemDate; - } - FeedItem item = new FeedItem(0, "title", "id" + i, "link", itemDate, itemState, feed); - - File f = new File(destFolder, "file " + i); - assertTrue(f.createNewFile()); - files.add(f); - item.setMedia(new FeedMedia(0, item, 1, 0, 1L, "m", - f.getAbsolutePath(), "url", true, playbackCompletionDate, 0, 0)); - items.add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - if (addToQueue) { - adapter.setQueue(items); - } - if (addToFavorites) { - adapter.setFavorites(items); - } - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : items) { - assertTrue(item.getId() != 0); - //noinspection ConstantConditions - assertTrue(item.getMedia().getId() != 0); - } - } - - @Test - public void testPerformAutoCleanupHandleUnplayed() throws IOException { - final int numItems = EPISODE_CACHE_SIZE * 2; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numItems, feed, items, files, FeedItem.UNPLAYED, false, false); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (File file : files) { - assertTrue(file.exists()); - } - } - - @Test - public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue() throws IOException { - final int numItems = EPISODE_CACHE_SIZE * 2; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numItems, feed, items, files, FeedItem.PLAYED, true, false); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (File file : files) { - assertTrue(file.exists()); - } - } - - /** - * Reproduces a bug where DBTasks.performAutoCleanup(android.content.Context) would use the ID - * of the FeedItem in the call to DBWriter.deleteFeedMediaOfItem instead of the ID of the FeedMedia. - * This would cause the wrong item to be deleted. - */ - @Test - public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException { - // add feed with no enclosures so that item ID != media ID - saveFeedlist(1, 10, false); - - // add candidate for performAutoCleanup - List<Feed> feeds = saveFeedlist(1, 1, true); - FeedMedia m = feeds.get(0).getItems().get(0).getMedia(); - //noinspection ConstantConditions - m.setDownloaded(true); - m.setLocalFileUrl("file"); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setMedia(m); - adapter.close(); - - testPerformAutoCleanupShouldNotDeleteBecauseInQueue(); - } - - @Test - public void testPerformAutoCleanupShouldNotDeleteBecauseFavorite() throws IOException { - final int numItems = EPISODE_CACHE_SIZE * 2; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numItems, feed, items, files, FeedItem.PLAYED, false, true); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (File file : files) { - assertTrue(file.exists()); - } - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbNullCleanupAlgorithmTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbNullCleanupAlgorithmTest.java deleted file mode 100644 index bbcf362b9..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbNullCleanupAlgorithmTest.java +++ /dev/null @@ -1,125 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import android.content.Context; -import android.content.SharedPreferences; -import androidx.preference.PreferenceManager; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import androidx.test.platform.app.InstrumentationRegistry; -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; -import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager; -import de.danoeh.antennapod.storage.database.DBWriter; -import de.danoeh.antennapod.storage.preferences.UserPreferences; -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Tests that the APNullCleanupAlgorithm is working correctly. - */ -@RunWith(RobolectricTestRunner.class) -public class DbNullCleanupAlgorithmTest { - - private static final int EPISODE_CACHE_SIZE = 5; - - private Context context; - - private File destFolder; - - @Before - public void setUp() { - context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - destFolder = context.getExternalCacheDir(); - cleanupDestFolder(destFolder); - assertNotNull(destFolder); - assertTrue(destFolder.exists()); - assertTrue(destFolder.canWrite()); - - // create new database - PodDBAdapter.init(context); - PodDBAdapter.deleteDatabase(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.close(); - - SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context - .getApplicationContext()).edit(); - prefEdit.putString(UserPreferences.PREF_EPISODE_CACHE_SIZE, Integer.toString(EPISODE_CACHE_SIZE)); - prefEdit.putString(UserPreferences.PREF_EPISODE_CLEANUP, - Integer.toString(UserPreferences.EPISODE_CLEANUP_NULL)); - prefEdit.commit(); - - UserPreferences.init(context); - AutoDownloadManager.setInstance(new AutoDownloadManagerImpl()); - } - - @After - public void tearDown() { - DBWriter.tearDownTests(); - PodDBAdapter.deleteDatabase(); - PodDBAdapter.tearDownTests(); - - cleanupDestFolder(destFolder); - assertTrue(destFolder.delete()); - } - - private void cleanupDestFolder(File destFolder) { - //noinspection ConstantConditions - for (File f : destFolder.listFiles()) { - assertTrue(f.delete()); - } - } - - /** - * A test with no items in the queue, but multiple items downloaded. - * The null algorithm should never delete any items, even if they're played and not in the queue. - */ - @Test - public void testPerformAutoCleanupShouldNotDelete() throws IOException { - final int numItems = EPISODE_CACHE_SIZE * 2; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - for (int i = 0; i < numItems; i++) { - FeedItem item = new FeedItem(0, "title", "id" + i, "link", new Date(), FeedItem.PLAYED, feed); - - File f = new File(destFolder, "file " + i); - assertTrue(f.createNewFile()); - files.add(f); - item.setMedia(new FeedMedia(0, item, 1, 0, 1L, "m", f.getAbsolutePath(), "url", true, - new Date(numItems - i), 0, 0)); - items.add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : items) { - assertTrue(item.getId() != 0); - //noinspection ConstantConditions - assertTrue(item.getMedia().getId() != 0); - } - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (int i = 0; i < files.size(); i++) { - assertTrue(files.get(i).exists()); - } - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbQueueCleanupAlgorithmTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbQueueCleanupAlgorithmTest.java deleted file mode 100644 index 04762b123..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbQueueCleanupAlgorithmTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager; -import de.danoeh.antennapod.storage.preferences.UserPreferences; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Tests that the APQueueCleanupAlgorithm is working correctly. - */ -@RunWith(RobolectricTestRunner.class) -public class DbQueueCleanupAlgorithmTest extends DbCleanupTests { - - public DbQueueCleanupAlgorithmTest() { - setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_QUEUE); - AutoDownloadManager.setInstance(new AutoDownloadManagerImpl()); - } - - /** - * For APQueueCleanupAlgorithm we expect even unplayed episodes to be deleted if needed - * if they aren't in the queue. - */ - @Test - public void testPerformAutoCleanupHandleUnplayed() throws IOException { - final int numItems = EPISODE_CACHE_SIZE * 2; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numItems, feed, items, files, FeedItem.UNPLAYED, false, false); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (int i = 0; i < files.size(); i++) { - if (i < EPISODE_CACHE_SIZE) { - assertTrue(files.get(i).exists()); - } else { - assertFalse(files.get(i).exists()); - } - } - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java deleted file mode 100644 index 17e023da5..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbReaderTest.java +++ /dev/null @@ -1,526 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import android.content.Context; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Date; -import java.util.List; -import java.util.Random; - -import androidx.test.platform.app.InstrumentationRegistry; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedCounter; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedItemFilter; -import de.danoeh.antennapod.model.feed.FeedMedia; -import de.danoeh.antennapod.model.feed.FeedOrder; -import de.danoeh.antennapod.model.feed.SortOrder; -import de.danoeh.antennapod.storage.database.DBReader; -import de.danoeh.antennapod.storage.database.DBWriter; -import de.danoeh.antennapod.storage.database.NavDrawerData; -import de.danoeh.antennapod.storage.preferences.UserPreferences; -import de.danoeh.antennapod.storage.database.LongList; -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.experimental.runners.Enclosed; -import org.junit.runner.RunWith; -import org.robolectric.ParameterizedRobolectricTestRunner; -import org.robolectric.RobolectricTestRunner; - -import static de.danoeh.antennapod.core.storage.DbTestUtils.saveFeedlist; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -/** - * Test class for DBReader. - */ -@SuppressWarnings("ConstantConditions") -@RunWith(Enclosed.class) -public class DbReaderTest { - @Ignore("Not a test") - public static class TestBase { - @Before - public void setUp() { - Context context = InstrumentationRegistry.getInstrumentation().getContext(); - UserPreferences.init(context); - - PodDBAdapter.init(context); - PodDBAdapter.deleteDatabase(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.close(); - } - - @After - public void tearDown() { - PodDBAdapter.tearDownTests(); - DBWriter.tearDownTests(); - } - } - - @RunWith(RobolectricTestRunner.class) - public static class SingleTests extends TestBase { - @Test - public void testGetFeedList() { - List<Feed> feeds = saveFeedlist(10, 0, false); - List<Feed> savedFeeds = DBReader.getFeedList(); - assertNotNull(savedFeeds); - assertEquals(feeds.size(), savedFeeds.size()); - for (int i = 0; i < feeds.size(); i++) { - assertEquals(feeds.get(i).getId(), savedFeeds.get(i).getId()); - } - } - - @Test - public void testGetFeedListSortOrder() { - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - - final long lastRefreshed = System.currentTimeMillis(); - Feed feed1 = new Feed(0, null, "A", "link", "d", null, null, null, "rss", "A", null, "", "", lastRefreshed); - Feed feed2 = new Feed(0, null, "b", "link", "d", null, null, null, "rss", "b", null, "", "", lastRefreshed); - Feed feed3 = new Feed(0, null, "C", "link", "d", null, null, null, "rss", "C", null, "", "", lastRefreshed); - Feed feed4 = new Feed(0, null, "d", "link", "d", null, null, null, "rss", "d", null, "", "", lastRefreshed); - adapter.setCompleteFeed(feed1); - adapter.setCompleteFeed(feed2); - adapter.setCompleteFeed(feed3); - adapter.setCompleteFeed(feed4); - assertTrue(feed1.getId() != 0); - assertTrue(feed2.getId() != 0); - assertTrue(feed3.getId() != 0); - assertTrue(feed4.getId() != 0); - - adapter.close(); - - List<Feed> saved = DBReader.getFeedList(); - assertNotNull(saved); - assertEquals("Wrong size: ", 4, saved.size()); - - assertEquals("Wrong id of feed 1: ", feed1.getId(), saved.get(0).getId()); - assertEquals("Wrong id of feed 2: ", feed2.getId(), saved.get(1).getId()); - assertEquals("Wrong id of feed 3: ", feed3.getId(), saved.get(2).getId()); - assertEquals("Wrong id of feed 4: ", feed4.getId(), saved.get(3).getId()); - } - - @Test - public void testFeedListDownloadUrls() { - List<Feed> feeds = saveFeedlist(10, 0, false); - List<String> urls = DBReader.getFeedListDownloadUrls(); - assertNotNull(urls); - assertEquals(feeds.size(), urls.size()); - for (int i = 0; i < urls.size(); i++) { - assertEquals(urls.get(i), feeds.get(i).getDownloadUrl()); - } - } - - @Test - public void testLoadFeedDataOfFeedItemlist() { - final int numFeeds = 10; - final int numItems = 1; - List<Feed> feeds = saveFeedlist(numFeeds, numItems, false); - List<FeedItem> items = new ArrayList<>(); - for (Feed f : feeds) { - for (FeedItem item : f.getItems()) { - item.setFeed(null); - item.setFeedId(f.getId()); - items.add(item); - } - } - DBReader.loadAdditionalFeedItemListData(items); - for (int i = 0; i < numFeeds; i++) { - for (int j = 0; j < numItems; j++) { - FeedItem item = feeds.get(i).getItems().get(j); - assertNotNull(item.getFeed()); - assertEquals(feeds.get(i).getId(), item.getFeed().getId()); - assertEquals(item.getFeed().getId(), item.getFeedId()); - } - } - } - - @Test - public void testGetFeedItemList() { - final int numFeeds = 1; - final int numItems = 10; - Feed feed = saveFeedlist(numFeeds, numItems, false).get(0); - List<FeedItem> items = feed.getItems(); - feed.setItems(null); - List<FeedItem> savedItems = DBReader.getFeedItemList(feed, - FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD); - assertNotNull(savedItems); - assertEquals(items.size(), savedItems.size()); - for (int i = 0; i < savedItems.size(); i++) { - assertEquals(savedItems.get(i).getId(), items.get(i).getId()); - } - } - - @SuppressWarnings("SameParameterValue") - private List<FeedItem> saveQueue(int numItems) { - if (numItems <= 0) { - throw new IllegalArgumentException("numItems<=0"); - } - List<Feed> feeds = saveFeedlist(numItems, numItems, false); - List<FeedItem> allItems = new ArrayList<>(); - for (Feed f : feeds) { - allItems.addAll(f.getItems()); - } - // take random items from every feed - Random random = new Random(); - List<FeedItem> queue = new ArrayList<>(); - while (queue.size() < numItems) { - int index = random.nextInt(numItems); - if (!queue.contains(allItems.get(index))) { - queue.add(allItems.get(index)); - } - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setQueue(queue); - adapter.close(); - return queue; - } - - @Test - public void testGetQueueIdList() { - final int numItems = 10; - List<FeedItem> queue = saveQueue(numItems); - LongList ids = DBReader.getQueueIDList(); - assertNotNull(ids); - assertEquals(ids.size(), queue.size()); - for (int i = 0; i < queue.size(); i++) { - assertTrue(ids.get(i) != 0); - assertEquals(ids.get(i), queue.get(i).getId()); - } - } - - @Test - public void testGetQueue() { - final int numItems = 10; - List<FeedItem> queue = saveQueue(numItems); - List<FeedItem> savedQueue = DBReader.getQueue(); - assertNotNull(savedQueue); - assertEquals(savedQueue.size(), queue.size()); - for (int i = 0; i < queue.size(); i++) { - assertTrue(savedQueue.get(i).getId() != 0); - assertEquals(savedQueue.get(i).getId(), queue.get(i).getId()); - } - } - - @SuppressWarnings("SameParameterValue") - private List<FeedItem> saveDownloadedItems(int numItems) { - if (numItems <= 0) { - throw new IllegalArgumentException("numItems<=0"); - } - List<Feed> feeds = saveFeedlist(numItems, numItems, true); - List<FeedItem> items = new ArrayList<>(); - for (Feed f : feeds) { - items.addAll(f.getItems()); - } - List<FeedItem> downloaded = new ArrayList<>(); - Random random = new Random(); - - while (downloaded.size() < numItems) { - int i = random.nextInt(numItems); - if (!downloaded.contains(items.get(i))) { - FeedItem item = items.get(i); - item.getMedia().setDownloaded(true); - item.getMedia().setLocalFileUrl("file" + i); - downloaded.add(item); - } - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.storeFeedItemlist(downloaded); - adapter.close(); - return downloaded; - } - - @Test - public void testGetDownloadedItems() { - final int numItems = 10; - List<FeedItem> downloaded = saveDownloadedItems(numItems); - List<FeedItem> downloadedSaved = DBReader.getEpisodes(0, Integer.MAX_VALUE, - new FeedItemFilter(FeedItemFilter.DOWNLOADED), SortOrder.DATE_NEW_OLD); - assertNotNull(downloadedSaved); - assertEquals(downloaded.size(), downloadedSaved.size()); - for (FeedItem item : downloadedSaved) { - assertNotNull(item.getMedia()); - assertTrue(item.getMedia().isDownloaded()); - assertNotNull(item.getMedia().getDownloadUrl()); - } - } - - @SuppressWarnings("SameParameterValue") - private List<FeedItem> saveNewItems(int numItems) { - List<Feed> feeds = saveFeedlist(numItems, numItems, true); - List<FeedItem> items = new ArrayList<>(); - for (Feed f : feeds) { - items.addAll(f.getItems()); - } - List<FeedItem> newItems = new ArrayList<>(); - Random random = new Random(); - - while (newItems.size() < numItems) { - int i = random.nextInt(numItems); - if (!newItems.contains(items.get(i))) { - FeedItem item = items.get(i); - item.setNew(); - newItems.add(item); - } - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.storeFeedItemlist(newItems); - adapter.close(); - return newItems; - } - - @Test - public void testGetNewItemIds() { - final int numItems = 10; - - List<FeedItem> newItems = saveNewItems(numItems); - long[] unreadIds = new long[newItems.size()]; - for (int i = 0; i < newItems.size(); i++) { - unreadIds[i] = newItems.get(i).getId(); - } - List<FeedItem> newItemsSaved = DBReader.getEpisodes(0, Integer.MAX_VALUE, - new FeedItemFilter(FeedItemFilter.NEW), SortOrder.DATE_NEW_OLD); - assertNotNull(newItemsSaved); - assertEquals(newItemsSaved.size(), newItems.size()); - for (FeedItem feedItem : newItemsSaved) { - long savedId = feedItem.getId(); - boolean found = false; - for (long id : unreadIds) { - if (id == savedId) { - found = true; - break; - } - } - assertTrue(found); - } - } - - @Test - public void testGetPlaybackHistoryLength() { - final int totalItems = 100; - - Feed feed = DbTestUtils.saveFeedlist(1, totalItems, true).get(0); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - for (int playedItems : Arrays.asList(0, 1, 20, 100)) { - adapter.open(); - for (int i = 0; i < playedItems; ++i) { - FeedMedia m = feed.getItems().get(i).getMedia(); - m.setPlaybackCompletionDate(new Date(i + 1)); - - adapter.setFeedMediaPlaybackCompletionDate(m); - } - adapter.close(); - - long len = DBReader.getTotalEpisodeCount(new FeedItemFilter(FeedItemFilter.IS_IN_HISTORY)); - assertEquals("Wrong size: ", (int) len, playedItems); - } - - } - - @Test - public void testGetNavDrawerDataQueueEmptyNoUnreadItems() { - final int numFeeds = 10; - final int numItems = 10; - DbTestUtils.saveFeedlist(numFeeds, numItems, true); - NavDrawerData navDrawerData = DBReader.getNavDrawerData( - UserPreferences.getSubscriptionsFilter(), FeedOrder.COUNTER, FeedCounter.SHOW_NEW); - assertEquals(numFeeds, navDrawerData.items.size()); - assertEquals(0, navDrawerData.numNewItems); - assertEquals(0, navDrawerData.queueSize); - } - - @Test - public void testGetNavDrawerDataQueueNotEmptyWithUnreadItems() { - final int numFeeds = 10; - final int numItems = 10; - final int numQueue = 1; - final int numNew = 2; - List<Feed> feeds = DbTestUtils.saveFeedlist(numFeeds, numItems, true); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - for (int i = 0; i < numNew; i++) { - FeedItem item = feeds.get(0).getItems().get(i); - item.setNew(); - adapter.setSingleFeedItem(item); - } - List<FeedItem> queue = new ArrayList<>(); - for (int i = 0; i < numQueue; i++) { - FeedItem item = feeds.get(1).getItems().get(i); - queue.add(item); - } - adapter.setQueue(queue); - - adapter.close(); - - NavDrawerData navDrawerData = DBReader.getNavDrawerData( - UserPreferences.getSubscriptionsFilter(), FeedOrder.COUNTER, FeedCounter.SHOW_NEW); - assertEquals(numFeeds, navDrawerData.items.size()); - assertEquals(numNew, navDrawerData.numNewItems); - assertEquals(numQueue, navDrawerData.queueSize); - } - - @Test - public void testGetFeedItemlistCheckChaptersFalse() { - List<Feed> feeds = DbTestUtils.saveFeedlist(10, 10, false, false, 0); - for (Feed feed : feeds) { - for (FeedItem item : feed.getItems()) { - assertFalse(item.hasChapters()); - } - } - } - - @Test - public void testGetFeedItemlistCheckChaptersTrue() { - List<Feed> feeds = saveFeedlist(10, 10, false, true, 10); - for (Feed feed : feeds) { - for (FeedItem item : feed.getItems()) { - assertTrue(item.hasChapters()); - } - } - } - - @Test - public void testLoadChaptersOfFeedItemNoChapters() { - List<Feed> feeds = saveFeedlist(1, 3, false, false, 0); - saveFeedlist(1, 3, false, true, 3); - for (Feed feed : feeds) { - for (FeedItem item : feed.getItems()) { - assertFalse(item.hasChapters()); - item.setChapters(DBReader.loadChaptersOfFeedItem(item)); - assertFalse(item.hasChapters()); - assertNull(item.getChapters()); - } - } - } - - @Test - public void testLoadChaptersOfFeedItemWithChapters() { - final int numChapters = 3; - DbTestUtils.saveFeedlist(1, 3, false, false, 0); - List<Feed> feeds = saveFeedlist(1, 3, false, true, numChapters); - for (Feed feed : feeds) { - for (FeedItem item : feed.getItems()) { - assertTrue(item.hasChapters()); - item.setChapters(DBReader.loadChaptersOfFeedItem(item)); - assertTrue(item.hasChapters()); - assertNotNull(item.getChapters()); - assertEquals(numChapters, item.getChapters().size()); - } - } - } - - @Test - public void testGetItemWithChapters() { - final int numChapters = 3; - List<Feed> feeds = saveFeedlist(1, 1, false, true, numChapters); - FeedItem item1 = feeds.get(0).getItems().get(0); - FeedItem item2 = DBReader.getFeedItem(item1.getId()); - item2.setChapters(DBReader.loadChaptersOfFeedItem(item2)); - assertTrue(item2.hasChapters()); - assertEquals(item1.getChapters().size(), item2.getChapters().size()); - for (int i = 0; i < item1.getChapters().size(); i++) { - assertEquals(item1.getChapters().get(i).getId(), item2.getChapters().get(i).getId()); - } - } - - @Test - public void testGetItemByEpisodeUrl() { - List<Feed> feeds = saveFeedlist(1, 1, true); - FeedItem item1 = feeds.get(0).getItems().get(0); - FeedItem feedItemByEpisodeUrl = DBReader.getFeedItemByGuidOrEpisodeUrl(null, - item1.getMedia().getDownloadUrl()); - assertEquals(item1.getItemIdentifier(), feedItemByEpisodeUrl.getItemIdentifier()); - } - - @Test - public void testGetItemByGuid() { - List<Feed> feeds = saveFeedlist(1, 1, true); - FeedItem item1 = feeds.get(0).getItems().get(0); - - FeedItem feedItemByGuid = DBReader.getFeedItemByGuidOrEpisodeUrl(item1.getItemIdentifier(), - item1.getMedia().getDownloadUrl()); - assertEquals(item1.getItemIdentifier(), feedItemByGuid.getItemIdentifier()); - } - - } - - @RunWith(ParameterizedRobolectricTestRunner.class) - public static class PlaybackHistoryTest extends TestBase { - - private int paramOffset; - private int paramLimit; - - @ParameterizedRobolectricTestRunner.Parameters - public static Collection<Object[]> data() { - List<Integer> limits = Arrays.asList(1, 20, 100); - List<Integer> offsets = Arrays.asList(0, 10, 20); - Object[][] rv = new Object[limits.size() * offsets.size()][2]; - int i = 0; - for (int offset : offsets) { - for (int limit : limits) { - rv[i][0] = offset; - rv[i][1] = limit; - i++; - } - } - - return Arrays.asList(rv); - } - - public PlaybackHistoryTest(int offset, int limit) { - this.paramOffset = offset; - this.paramLimit = limit; - - } - - @Test - public void testGetPlaybackHistory() { - final int numItems = (paramLimit + 1) * 2; - final int playedItems = paramLimit + 1; - final int numReturnedItems = Math.min(Math.max(playedItems - paramOffset, 0), paramLimit); - final int numFeeds = 1; - - Feed feed = DbTestUtils.saveFeedlist(numFeeds, numItems, true).get(0); - long[] ids = new long[playedItems]; - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - for (int i = 0; i < playedItems; i++) { - FeedMedia m = feed.getItems().get(i).getMedia(); - m.setPlaybackCompletionDate(new Date(i + 1)); - adapter.setFeedMediaPlaybackCompletionDate(m); - ids[ids.length - 1 - i] = m.getItem().getId(); - } - adapter.close(); - - List<FeedItem> saved = DBReader.getEpisodes(paramOffset, paramLimit, - new FeedItemFilter(FeedItemFilter.IS_IN_HISTORY), SortOrder.COMPLETION_DATE_NEW_OLD); - assertNotNull(saved); - assertEquals(String.format("Wrong size with offset %d and limit %d: ", - paramOffset, paramLimit), - numReturnedItems, saved.size()); - for (int i = 0; i < numReturnedItems; i++) { - FeedItem item = saved.get(i); - assertNotNull(item.getMedia().getPlaybackCompletionDate()); - assertEquals(String.format("Wrong sort order with offset %d and limit %d: ", - paramOffset, paramLimit), - item.getId(), ids[paramOffset + i]); - } - } - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java deleted file mode 100644 index 1f90dd7a9..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbTasksTest.java +++ /dev/null @@ -1,249 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import android.content.Context; - -import androidx.test.platform.app.InstrumentationRegistry; - -import de.danoeh.antennapod.storage.database.DBReader; -import de.danoeh.antennapod.storage.database.DBWriter; -import de.danoeh.antennapod.storage.database.FeedDatabaseWriter; -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; -import de.danoeh.antennapod.storage.preferences.PlaybackPreferences; -import de.danoeh.antennapod.storage.preferences.UserPreferences; - -import static java.util.Collections.singletonList; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; - -/** - * Test class for {@link FeedDatabaseWriter}. - */ -@RunWith(RobolectricTestRunner.class) -public class DbTasksTest { - private Context context; - - @Before - public void setUp() { - context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - UserPreferences.init(context); - PlaybackPreferences.init(context); - - // create new database - PodDBAdapter.init(context); - PodDBAdapter.deleteDatabase(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.close(); - } - - @After - public void tearDown() { - DBWriter.tearDownTests(); - PodDBAdapter.tearDownTests(); - } - - @Test - public void testUpdateFeedNewFeed() { - final int numItems = 10; - - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < numItems; i++) { - feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, - new Date(), FeedItem.UNPLAYED, feed)); - } - Feed newFeed = FeedDatabaseWriter.updateFeed(context, feed, false); - - assertEquals(feed.getId(), newFeed.getId()); - assertTrue(feed.getId() != 0); - for (FeedItem item : feed.getItems()) { - assertFalse(item.isPlayed()); - assertTrue(item.getId() != 0); - } - } - - /** Two feeds with the same title, but different download URLs should be treated as different feeds. */ - @Test - public void testUpdateFeedSameTitle() { - - Feed feed1 = new Feed("url1", null, "title"); - Feed feed2 = new Feed("url2", null, "title"); - - feed1.setItems(new ArrayList<>()); - feed2.setItems(new ArrayList<>()); - - Feed savedFeed1 = FeedDatabaseWriter.updateFeed(context, feed1, false); - Feed savedFeed2 = FeedDatabaseWriter.updateFeed(context, feed2, false); - - assertTrue(savedFeed1.getId() != savedFeed2.getId()); - } - - @Test - public void testUpdateFeedUpdatedFeed() { - final int numItemsOld = 10; - final int numItemsNew = 10; - - final Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < numItemsOld; i++) { - feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, - new Date(i), FeedItem.PLAYED, feed)); - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - // ensure that objects have been saved in db, then reset - assertTrue(feed.getId() != 0); - final long feedID = feed.getId(); - feed.setId(0); - List<Long> itemIDs = new ArrayList<>(); - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - itemIDs.add(item.getId()); - item.setId(0); - } - - for (int i = numItemsOld; i < numItemsNew + numItemsOld; i++) { - feed.getItems().add(0, new FeedItem(0, "item " + i, "id " + i, "link " + i, - new Date(i), FeedItem.UNPLAYED, feed)); - } - - final Feed newFeed = FeedDatabaseWriter.updateFeed(context, feed, false); - assertNotSame(newFeed, feed); - - updatedFeedTest(newFeed, feedID, itemIDs, numItemsOld, numItemsNew); - - final Feed feedFromDB = DBReader.getFeed(newFeed.getId()); - assertNotNull(feedFromDB); - assertEquals(newFeed.getId(), feedFromDB.getId()); - updatedFeedTest(feedFromDB, feedID, itemIDs, numItemsOld, numItemsNew); - } - - @Test - public void testUpdateFeedMediaUrlResetState() { - final Feed feed = new Feed("url", null, "title"); - FeedItem item = new FeedItem(0, "item", "id", "link", new Date(), FeedItem.PLAYED, feed); - feed.setItems(singletonList(item)); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - // ensure that objects have been saved in db, then reset - assertTrue(feed.getId() != 0); - assertTrue(item.getId() != 0); - - FeedMedia media = new FeedMedia(item, "url", 1024, "mime/type"); - item.setMedia(media); - List<FeedItem> list = new ArrayList<>(); - list.add(item); - feed.setItems(list); - - final Feed newFeed = FeedDatabaseWriter.updateFeed(context, feed, false); - assertNotSame(newFeed, feed); - - final Feed feedFromDB = DBReader.getFeed(newFeed.getId()); - final FeedItem feedItemFromDB = feedFromDB.getItems().get(0); - assertTrue(feedItemFromDB.isNew()); - } - - @Test - public void testUpdateFeedRemoveUnlistedItems() { - final Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < 10; i++) { - feed.getItems().add( - new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(i), FeedItem.PLAYED, feed)); - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - // delete some items - feed.getItems().subList(0, 2).clear(); - Feed newFeed = FeedDatabaseWriter.updateFeed(context, feed, true); - assertEquals(8, newFeed.getItems().size()); // 10 - 2 = 8 items - - Feed feedFromDB = DBReader.getFeed(newFeed.getId()); - assertEquals(8, feedFromDB.getItems().size()); // 10 - 2 = 8 items - } - - @Test - public void testUpdateFeedSetDuplicate() { - final Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < 10; i++) { - FeedItem item = - new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(i), FeedItem.PLAYED, feed); - FeedMedia media = new FeedMedia(item, "download url " + i, 123, "media/mp3"); - item.setMedia(media); - feed.getItems().add(item); - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - // change the guid of the first item, but leave the download url the same - FeedItem item = feed.getItemAtIndex(0); - item.setItemIdentifier("id 0-duplicate"); - item.setTitle("item 0 duplicate"); - Feed newFeed = FeedDatabaseWriter.updateFeed(context, feed, false); - assertEquals(10, newFeed.getItems().size()); // id 1-duplicate replaces because the stream url is the same - - Feed feedFromDB = DBReader.getFeed(newFeed.getId()); - assertEquals(10, feedFromDB.getItems().size()); // id1-duplicate should override id 1 - - FeedItem updatedItem = feedFromDB.getItemAtIndex(9); - assertEquals("item 0 duplicate", updatedItem.getTitle()); - assertEquals("id 0-duplicate", updatedItem.getItemIdentifier()); // Should use the new ID for sync etc - } - - - @SuppressWarnings("SameParameterValue") - private void updatedFeedTest(final Feed newFeed, long feedID, List<Long> itemIDs, - int numItemsOld, int numItemsNew) { - assertEquals(feedID, newFeed.getId()); - assertEquals(numItemsNew + numItemsOld, newFeed.getItems().size()); - Collections.reverse(newFeed.getItems()); - Date lastDate = new Date(0); - for (int i = 0; i < numItemsOld; i++) { - FeedItem item = newFeed.getItems().get(i); - assertSame(newFeed, item.getFeed()); - assertEquals((long) itemIDs.get(i), item.getId()); - assertTrue(item.isPlayed()); - assertTrue(item.getPubDate().getTime() >= lastDate.getTime()); - lastDate = item.getPubDate(); - } - for (int i = numItemsOld; i < numItemsNew + numItemsOld; i++) { - FeedItem item = newFeed.getItems().get(i); - assertSame(newFeed, item.getFeed()); - assertTrue(item.getId() != 0); - assertFalse(item.isPlayed()); - assertTrue(item.getPubDate().getTime() >= lastDate.getTime()); - lastDate = item.getPubDate(); - } - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DbTestUtils.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DbTestUtils.java deleted file mode 100644 index 0dd9c1396..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbTestUtils.java +++ /dev/null @@ -1,74 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import de.danoeh.antennapod.model.feed.Chapter; -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; -import de.danoeh.antennapod.storage.database.PodDBAdapter; - -import static org.junit.Assert.assertTrue; - -/** - * Utility methods for DB* tests. - */ -abstract class DbTestUtils { - - /** - * Use this method when tests don't involve chapters. - */ - public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia) { - return saveFeedlist(numFeeds, numItems, withMedia, false, 0); - } - - /** - * Use this method when tests involve chapters. - */ - public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia, - boolean withChapters, int numChapters) { - if (numFeeds <= 0) { - throw new IllegalArgumentException("numFeeds<=0"); - } - if (numItems < 0) { - throw new IllegalArgumentException("numItems<0"); - } - - List<Feed> feeds = new ArrayList<>(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - for (int i = 0; i < numFeeds; i++) { - Feed f = new Feed(0, null, "feed " + i, "link" + i, "descr", null, null, - null, null, "id" + i, null, null, "url" + i, System.currentTimeMillis()); - f.setItems(new ArrayList<>()); - for (int j = 0; j < numItems; j++) { - FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(), - FeedItem.PLAYED, f, withChapters); - if (withMedia) { - FeedMedia media = new FeedMedia(item, "url" + j, 1, "audio/mp3"); - item.setMedia(media); - } - if (withChapters) { - List<Chapter> chapters = new ArrayList<>(); - item.setChapters(chapters); - for (int k = 0; k < numChapters; k++) { - chapters.add(new Chapter(k, "item " + j + " chapter " + k, - "http://example.com", "http://example.com/image.png")); - } - } - f.getItems().add(item); - } - adapter.setCompleteFeed(f); - assertTrue(f.getId() != 0); - for (FeedItem item : f.getItems()) { - assertTrue(item.getId() != 0); - } - feeds.add(f); - } - adapter.close(); - - return feeds; - } -} 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 806b8f08f..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DbWriterTest.java +++ /dev/null @@ -1,832 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import android.content.Context; -import android.content.SharedPreferences; -import android.database.Cursor; -import android.util.Log; - -import androidx.core.util.Consumer; -import androidx.preference.PreferenceManager; -import androidx.test.platform.app.InstrumentationRegistry; - -import de.danoeh.antennapod.model.feed.FeedItemFilter; -import de.danoeh.antennapod.model.feed.SortOrder; -import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface; -import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterfaceStub; -import de.danoeh.antennapod.storage.database.DBReader; -import de.danoeh.antennapod.storage.database.DBWriter; -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import org.awaitility.Awaitility; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.io.File; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Locale; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; -import de.danoeh.antennapod.storage.preferences.PlaybackPreferences; -import de.danoeh.antennapod.storage.preferences.UserPreferences; -import de.danoeh.antennapod.core.util.FeedItemUtil; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -/** - * Test class for {@link DBWriter}. - */ -@RunWith(RobolectricTestRunner.class) -public class DbWriterTest { - - private static final String TAG = "DBWriterTest"; - private static final String TEST_FOLDER = "testDBWriter"; - private static final long TIMEOUT = 5L; - - private Context context; - - @Before - public void setUp() { - context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - UserPreferences.init(context); - PlaybackPreferences.init(context); - DownloadServiceInterface.setImpl(new DownloadServiceInterfaceStub()); - - // create new database - PodDBAdapter.init(context); - PodDBAdapter.deleteDatabase(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.close(); - - SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences( - context.getApplicationContext()).edit(); - prefEdit.putBoolean(UserPreferences.PREF_DELETE_REMOVES_FROM_QUEUE, true).commit(); - } - - @After - public void tearDown() { - PodDBAdapter.tearDownTests(); - DBWriter.tearDownTests(); - - File testDir = context.getExternalFilesDir(TEST_FOLDER); - assertNotNull(testDir); - for (File f : testDir.listFiles()) { - //noinspection ResultOfMethodCallIgnored - f.delete(); - } - } - - @Test - public void testSetFeedMediaPlaybackInformation() throws Exception { - final int position = 50; - final long lastPlayedTime = 1000; - final int playedDuration = 60; - final int duration = 100; - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - FeedItem item = new FeedItem(0, "Item", "Item", "url", new Date(), FeedItem.PLAYED, feed); - items.add(item); - FeedMedia media = new FeedMedia(0, item, duration, 1, 1, "mime_type", - "dummy path", "download_url", true, null, 0, 0); - item.setMedia(media); - - DBWriter.setFeedItem(item).get(TIMEOUT, TimeUnit.SECONDS); - - media.setPosition(position); - media.setLastPlayedTime(lastPlayedTime); - media.setPlayedDuration(playedDuration); - - DBWriter.setFeedMediaPlaybackInformation(item.getMedia()).get(TIMEOUT, TimeUnit.SECONDS); - - FeedItem itemFromDb = DBReader.getFeedItem(item.getId()); - FeedMedia mediaFromDb = itemFromDb.getMedia(); - - assertEquals(position, mediaFromDb.getPosition()); - assertEquals(lastPlayedTime, mediaFromDb.getLastPlayedTime()); - assertEquals(playedDuration, mediaFromDb.getPlayedDuration()); - assertEquals(duration, mediaFromDb.getDuration()); - } - - @Test - public void testDeleteFeedMediaOfItemFileExists() throws Exception { - File dest = new File(context.getExternalFilesDir(TEST_FOLDER), "testFile"); - - assertTrue(dest.createNewFile()); - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - FeedItem item = new FeedItem(0, "Item", "Item", "url", new Date(), FeedItem.PLAYED, feed); - - FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", - dest.getAbsolutePath(), "download_url", true, null, 0, 0); - item.setMedia(media); - - items.add(item); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - assertTrue(media.getId() != 0); - assertTrue(item.getId() != 0); - - DBWriter.deleteFeedMediaOfItem(context, media) - .get(TIMEOUT, TimeUnit.SECONDS); - media = DBReader.getFeedMedia(media.getId()); - assertNotNull(media); - assertFalse(dest.exists()); - assertFalse(media.isDownloaded()); - assertNull(media.getLocalFileUrl()); - } - - @Test - public void testDeleteFeedMediaOfItemRemoveFromQueue() throws Exception { - assertTrue(UserPreferences.shouldDeleteRemoveFromQueue()); - - File dest = new File(context.getExternalFilesDir(TEST_FOLDER), "testFile"); - - assertTrue(dest.createNewFile()); - - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - FeedItem item = new FeedItem(0, "Item", "Item", "url", new Date(), FeedItem.UNPLAYED, feed); - - FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", - dest.getAbsolutePath(), "download_url", true, null, 0, 0); - item.setMedia(media); - - items.add(item); - List<FeedItem> queue = new ArrayList<>(); - queue.add(item); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.setQueue(queue); - adapter.close(); - assertTrue(media.getId() != 0); - assertTrue(item.getId() != 0); - queue = DBReader.getQueue(); - assertFalse(queue.isEmpty()); - - DBWriter.deleteFeedMediaOfItem(context, media); - Awaitility.await().timeout(2, TimeUnit.SECONDS).until(() -> !dest.exists()); - media = DBReader.getFeedMedia(media.getId()); - assertNotNull(media); - assertFalse(dest.exists()); - assertFalse(media.isDownloaded()); - assertNull(media.getLocalFileUrl()); - Awaitility.await().timeout(2, TimeUnit.SECONDS).until(() -> DBReader.getQueue().isEmpty()); - } - - @Test - public void testDeleteFeed() throws Exception { - File destFolder = context.getExternalFilesDir(TEST_FOLDER); - assertNotNull(destFolder); - - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - - List<File> itemFiles = new ArrayList<>(); - // create items with downloaded media files - for (int i = 0; i < 10; i++) { - FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed); - feed.getItems().add(item); - - File enc = new File(destFolder, "file " + i); - assertTrue(enc.createNewFile()); - - itemFiles.add(enc); - FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", - enc.getAbsolutePath(), "download_url", true, null, 0, 0); - item.setMedia(media); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - assertTrue(item.getMedia().getId() != 0); - } - - DBWriter.deleteFeed(context, feed.getId()).get(TIMEOUT, TimeUnit.SECONDS); - - // check if files still exist - for (File f : itemFiles) { - assertFalse(f.exists()); - } - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor c = adapter.getFeedCursor(feed.getId()); - assertEquals(0, c.getCount()); - c.close(); - for (FeedItem item : feed.getItems()) { - c = adapter.getFeedItemCursor(String.valueOf(item.getId())); - assertEquals(0, c.getCount()); - c.close(); - c = adapter.getFeedItemFromMediaIdCursor(item.getMedia().getId()); - assertEquals(0, c.getCount()); - c.close(); - } - adapter.close(); - } - - @Test - public void testDeleteFeedNoItems() throws Exception { - File destFolder = context.getExternalFilesDir(TEST_FOLDER); - assertNotNull(destFolder); - - Feed feed = new Feed("url", null, "title"); - feed.setItems(null); - feed.setImageUrl("url"); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - - DBWriter.deleteFeed(context, feed.getId()).get(TIMEOUT, TimeUnit.SECONDS); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor c = adapter.getFeedCursor(feed.getId()); - assertEquals(0, c.getCount()); - c.close(); - adapter.close(); - } - - @Test - public void testDeleteFeedNoFeedMedia() throws Exception { - File destFolder = context.getExternalFilesDir(TEST_FOLDER); - assertNotNull(destFolder); - - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - - feed.setImageUrl("url"); - - // create items - for (int i = 0; i < 10; i++) { - FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed); - feed.getItems().add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - } - - DBWriter.deleteFeed(context, feed.getId()).get(TIMEOUT, TimeUnit.SECONDS); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor c = adapter.getFeedCursor(feed.getId()); - assertEquals(0, c.getCount()); - c.close(); - for (FeedItem item : feed.getItems()) { - c = adapter.getFeedItemCursor(String.valueOf(item.getId())); - assertEquals(0, c.getCount()); - c.close(); - } - adapter.close(); - } - - @Test - public void testDeleteFeedWithQueueItems() throws Exception { - File destFolder = context.getExternalFilesDir(TEST_FOLDER); - assertNotNull(destFolder); - - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - - feed.setImageUrl("url"); - - // create items with downloaded media files - for (int i = 0; i < 10; i++) { - FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed); - feed.getItems().add(item); - File enc = new File(destFolder, "file " + i); - FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", - enc.getAbsolutePath(), "download_url", false, null, 0, 0); - item.setMedia(media); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - assertTrue(item.getMedia().getId() != 0); - } - - List<FeedItem> queue = new ArrayList<>(feed.getItems()); - adapter.open(); - adapter.setQueue(queue); - - Cursor queueCursor = adapter.getQueueIDCursor(); - assertEquals(queue.size(), queueCursor.getCount()); - queueCursor.close(); - - adapter.close(); - DBWriter.deleteFeed(context, feed.getId()).get(TIMEOUT, TimeUnit.SECONDS); - adapter.open(); - - Cursor c = adapter.getFeedCursor(feed.getId()); - assertEquals(0, c.getCount()); - c.close(); - for (FeedItem item : feed.getItems()) { - c = adapter.getFeedItemCursor(String.valueOf(item.getId())); - assertEquals(0, c.getCount()); - c.close(); - c = adapter.getFeedItemFromMediaIdCursor(item.getMedia().getId()); - assertEquals(0, c.getCount()); - c.close(); - } - c = adapter.getQueueCursor(); - assertEquals(0, c.getCount()); - c.close(); - adapter.close(); - } - - @Test - public void testDeleteFeedNoDownloadedFiles() throws Exception { - File destFolder = context.getExternalFilesDir(TEST_FOLDER); - assertNotNull(destFolder); - - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - - feed.setImageUrl("url"); - - // create items with downloaded media files - for (int i = 0; i < 10; i++) { - FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed); - feed.getItems().add(item); - File enc = new File(destFolder, "file " + i); - FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", - enc.getAbsolutePath(), "download_url", false, null, 0, 0); - item.setMedia(media); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - assertTrue(item.getMedia().getId() != 0); - } - - DBWriter.deleteFeed(context, feed.getId()).get(TIMEOUT, TimeUnit.SECONDS); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor c = adapter.getFeedCursor(feed.getId()); - assertEquals(0, c.getCount()); - c.close(); - for (FeedItem item : feed.getItems()) { - c = adapter.getFeedItemCursor(String.valueOf(item.getId())); - assertEquals(0, c.getCount()); - c.close(); - c = adapter.getFeedItemFromMediaIdCursor(item.getMedia().getId()); - assertEquals(0, c.getCount()); - c.close(); - } - adapter.close(); - } - - @Test - public void testDeleteFeedItems() throws Exception { - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - feed.setImageUrl("url"); - - // create items - for (int i = 0; i < 10; i++) { - FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - List<FeedItem> itemsToDelete = feed.getItems().subList(0, 2); - DBWriter.deleteFeedItems(context, itemsToDelete).get(TIMEOUT, TimeUnit.SECONDS); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - for (int i = 0; i < feed.getItems().size(); i++) { - FeedItem feedItem = feed.getItems().get(i); - Cursor c = adapter.getFeedItemCursor(String.valueOf(feedItem.getId())); - if (i < 2) { - assertEquals(0, c.getCount()); - } else { - assertEquals(1, c.getCount()); - } - c.close(); - } - adapter.close(); - } - - private FeedMedia playbackHistorySetup(Date playbackCompletionDate) { - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed); - FeedMedia media = new FeedMedia(0, item, 10, 0, 1, "mime", null, - "url", false, playbackCompletionDate, 0, 0); - feed.getItems().add(item); - item.setMedia(media); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - assertTrue(media.getId() != 0); - return media; - } - - @Test - public void testAddItemToPlaybackHistoryNotPlayedYet() throws Exception { - FeedMedia media = playbackHistorySetup(null); - DBWriter.addItemToPlaybackHistory(media).get(TIMEOUT, TimeUnit.SECONDS); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - media = DBReader.getFeedMedia(media.getId()); - adapter.close(); - - assertNotNull(media); - assertNotNull(media.getPlaybackCompletionDate()); - } - - @Test - public void testAddItemToPlaybackHistoryAlreadyPlayed() throws Exception { - final long oldDate = 0; - - FeedMedia media = playbackHistorySetup(new Date(oldDate)); - DBWriter.addItemToPlaybackHistory(media).get(TIMEOUT, TimeUnit.SECONDS); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - media = DBReader.getFeedMedia(media.getId()); - adapter.close(); - - assertNotNull(media); - assertNotNull(media.getPlaybackCompletionDate()); - assertNotEquals(media.getPlaybackCompletionDate().getTime(), oldDate); - } - - @SuppressWarnings("SameParameterValue") - private Feed queueTestSetupMultipleItems(final int numItems) throws Exception { - UserPreferences.setEnqueueLocation(UserPreferences.EnqueueLocation.BACK); - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < numItems; i++) { - FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.PLAYED, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - } - List<Future<?>> futures = new ArrayList<>(); - for (FeedItem item : feed.getItems()) { - futures.add(DBWriter.addQueueItem(context, item)); - } - for (Future<?> f : futures) { - f.get(TIMEOUT, TimeUnit.SECONDS); - } - return feed; - } - - @Test - public void testAddQueueItemSingleItem() throws Exception { - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(item.getId() != 0); - DBWriter.addQueueItem(context, item).get(TIMEOUT, TimeUnit.SECONDS); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor cursor = adapter.getQueueIDCursor(); - assertTrue(cursor.moveToFirst()); - assertEquals(item.getId(), cursor.getLong(0)); - cursor.close(); - adapter.close(); - } - - @Test - public void testAddQueueItemSingleItemAlreadyInQueue() throws Exception { - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(item.getId() != 0); - DBWriter.addQueueItem(context, item).get(TIMEOUT, TimeUnit.SECONDS); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor cursor = adapter.getQueueIDCursor(); - assertTrue(cursor.moveToFirst()); - assertEquals(item.getId(), cursor.getLong(0)); - cursor.close(); - adapter.close(); - - DBWriter.addQueueItem(context, item).get(TIMEOUT, TimeUnit.SECONDS); - adapter = PodDBAdapter.getInstance(); - adapter.open(); - cursor = adapter.getQueueIDCursor(); - assertTrue(cursor.moveToFirst()); - assertEquals(item.getId(), cursor.getLong(0)); - assertEquals(1, cursor.getCount()); - cursor.close(); - adapter.close(); - } - - @Test - public void testAddQueueItemMultipleItems() throws Exception { - final int numItems = 10; - - Feed feed; - feed = queueTestSetupMultipleItems(numItems); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor cursor = adapter.getQueueIDCursor(); - assertTrue(cursor.moveToFirst()); - assertEquals(numItems, cursor.getCount()); - List<Long> expectedIds; - expectedIds = FeedItemUtil.getIdList(feed.getItems()); - List<Long> actualIds = new ArrayList<>(); - for (int i = 0; i < numItems; i++) { - assertTrue(cursor.moveToPosition(i)); - actualIds.add(cursor.getLong(0)); - } - cursor.close(); - adapter.close(); - assertEquals("Bulk add to queue: result order should be the same as the order given", - expectedIds, actualIds); - } - - @Test - public void testClearQueue() throws Exception { - final int numItems = 10; - - queueTestSetupMultipleItems(numItems); - DBWriter.clearQueue().get(TIMEOUT, TimeUnit.SECONDS); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor cursor = adapter.getQueueIDCursor(); - assertFalse(cursor.moveToFirst()); - cursor.close(); - adapter.close(); - } - - @Test - public void testRemoveQueueItem() throws Exception { - final int numItems = 10; - Feed feed = createTestFeed(numItems); - - for (int removeIndex = 0; removeIndex < numItems; removeIndex++) { - final FeedItem item = feed.getItems().get(removeIndex); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setQueue(feed.getItems()); - adapter.close(); - - DBWriter.removeQueueItem(context, false, item).get(TIMEOUT, TimeUnit.SECONDS); - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor queue = adapter.getQueueIDCursor(); - assertEquals(numItems - 1, queue.getCount()); - for (int i = 0; i < queue.getCount(); i++) { - assertTrue(queue.moveToPosition(i)); - final long queueID = queue.getLong(0); - assertTrue(queueID != item.getId()); // removed item is no longer in queue - boolean idFound = false; - for (FeedItem other : feed.getItems()) { // items that were not removed are still in the queue - idFound = idFound | (other.getId() == queueID); - } - assertTrue(idFound); - } - queue.close(); - adapter.close(); - } - } - - @Test - public void testRemoveQueueItemMultipleItems() throws Exception { - final int numItems = 5; - final int numInQueue = numItems - 1; // the last one not in queue for boundary condition - Feed feed = createTestFeed(numItems); - - List<FeedItem> itemsToAdd = feed.getItems().subList(0, numInQueue); - withPodDB(adapter -> adapter.setQueue(itemsToAdd)); - - // Actual tests - // - - // Use array rather than List to make codes more succinct - Long[] itemIds = toItemIds(feed.getItems()).toArray(new Long[0]); - - DBWriter.removeQueueItem(context, false, - itemIds[1], itemIds[3]).get(TIMEOUT, TimeUnit.SECONDS); - assertQueueByItemIds("Average case - 2 items removed successfully", - itemIds[0], itemIds[2]); - - DBWriter.removeQueueItem(context, false).get(TIMEOUT, TimeUnit.SECONDS); - assertQueueByItemIds("Boundary case - no items supplied. queue should see no change", - itemIds[0], itemIds[2]); - - DBWriter.removeQueueItem(context, false, - itemIds[0], itemIds[4], -1L).get(TIMEOUT, TimeUnit.SECONDS); - assertQueueByItemIds("Boundary case - items not in queue ignored", - itemIds[2]); - - DBWriter.removeQueueItem(context, false, - itemIds[2], -1L).get(TIMEOUT, TimeUnit.SECONDS); - assertQueueByItemIds("Boundary case - invalid itemIds ignored"); // the queue is empty - - } - - @Test - public void testMoveQueueItem() throws Exception { - final int numItems = 10; - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < numItems; i++) { - FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, - new Date(), FeedItem.PLAYED, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - } - for (int from = 0; from < numItems; from++) { - for (int to = 0; to < numItems; to++) { - if (from == to) { - continue; - } - Log.d(TAG, String.format(Locale.US, "testMoveQueueItem: From=%d, To=%d", from, to)); - final long fromID = feed.getItems().get(from).getId(); - - adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setQueue(feed.getItems()); - adapter.close(); - - DBWriter.moveQueueItem(from, to, false).get(TIMEOUT, TimeUnit.SECONDS); - adapter = PodDBAdapter.getInstance(); - adapter.open(); - Cursor queue = adapter.getQueueIDCursor(); - assertEquals(numItems, queue.getCount()); - assertTrue(queue.moveToPosition(from)); - assertNotEquals(fromID, queue.getLong(0)); - assertTrue(queue.moveToPosition(to)); - assertEquals(fromID, queue.getLong(0)); - - queue.close(); - adapter.close(); - } - } - } - - @Test - public void testRemoveAllNewFlags() throws Exception { - final int numItems = 10; - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < numItems; i++) { - FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, - new Date(), FeedItem.NEW, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - } - - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - assertTrue(feed.getId() != 0); - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - } - - DBWriter.removeAllNewFlags().get(); - List<FeedItem> loadedItems = DBReader.getFeedItemList(feed, - FeedItemFilter.unfiltered(), SortOrder.DATE_NEW_OLD); - for (FeedItem item : loadedItems) { - assertFalse(item.isNew()); - } - } - - private static Feed createTestFeed(int numItems) { - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < numItems; i++) { - FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, - new Date(), FeedItem.PLAYED, feed); - item.setMedia(new FeedMedia(item, "", 0, "")); - feed.getItems().add(item); - } - - withPodDB(adapter -> adapter.setCompleteFeed(feed)); - - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - } - return feed; - } - - private static void withPodDB(Consumer<PodDBAdapter> action) { - PodDBAdapter adapter = PodDBAdapter.getInstance(); - try { - adapter.open(); - action.accept(adapter); - } finally { - adapter.close(); - } - } - - private static void assertQueueByItemIds(String message, long... itemIdsExpected) { - List<FeedItem> queue = DBReader.getQueue(); - List<Long> itemIdsActualList = toItemIds(queue); - List<Long> itemIdsExpectedList = new ArrayList<>(itemIdsExpected.length); - for (long id : itemIdsExpected) { - itemIdsExpectedList.add(id); - } - - assertEquals(message, itemIdsExpectedList, itemIdsActualList); - } - - private static List<Long> toItemIds(List<FeedItem> items) { - List<Long> itemIds = new ArrayList<>(items.size()); - for (FeedItem item : items) { - itemIds.add(item.getId()); - } - return itemIds; - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithmTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithmTest.java deleted file mode 100644 index df26b9e4f..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/ExceptFavoriteCleanupAlgorithmTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.storage.preferences.UserPreferences; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Tests that the APFavoriteCleanupAlgorithm is working correctly. - */ -@RunWith(RobolectricTestRunner.class) -public class ExceptFavoriteCleanupAlgorithmTest extends DbCleanupTests { - private final int numberOfItems = EPISODE_CACHE_SIZE * 2; - - public ExceptFavoriteCleanupAlgorithmTest() { - setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_EXCEPT_FAVORITE); - AutoDownloadManager.setInstance(new AutoDownloadManagerImpl()); - } - - @Test - public void testPerformAutoCleanupHandleUnplayed() throws IOException { - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numberOfItems, feed, items, files, FeedItem.UNPLAYED, false, false); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (int i = 0; i < files.size(); i++) { - if (i < EPISODE_CACHE_SIZE) { - assertTrue("Only enough items should be deleted", files.get(i).exists()); - } else { - assertFalse("Expected episode to be deleted", files.get(i).exists()); - } - } - } - - @Test - public void testPerformAutoCleanupDeletesQueued() throws IOException { - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numberOfItems, feed, items, files, FeedItem.UNPLAYED, true, false); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (int i = 0; i < files.size(); i++) { - if (i < EPISODE_CACHE_SIZE) { - assertTrue("Only enough items should be deleted", files.get(i).exists()); - } else { - assertFalse("Queued episodes should be deleted", files.get(i).exists()); - } - } - } - - @Test - public void testPerformAutoCleanupSavesFavorited() throws IOException { - Feed feed = new Feed("url", null, "title"); - List<FeedItem> items = new ArrayList<>(); - feed.setItems(items); - List<File> files = new ArrayList<>(); - populateItems(numberOfItems, feed, items, files, FeedItem.UNPLAYED, false, true); - - AutoDownloadManager.getInstance().performAutoCleanup(context); - for (int i = 0; i < files.size(); i++) { - assertTrue("Favorite episodes should should not be deleted", files.get(i).exists()); - } - } - - @Override - public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue() throws IOException { - // Yes it should - } - - @Override - public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException { - // Yes it should - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/FeedItemDuplicateGuesserTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/FeedItemDuplicateGuesserTest.java deleted file mode 100644 index 38dbe8caa..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/FeedItemDuplicateGuesserTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -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/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java deleted file mode 100644 index dfad714a8..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/ItemEnqueuePositionCalculatorTest.java +++ /dev/null @@ -1,178 +0,0 @@ -package de.danoeh.antennapod.core.storage; - -import de.danoeh.antennapod.model.playback.RemoteMedia; -import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface; -import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterfaceStub; -import de.danoeh.antennapod.storage.database.ItemEnqueuePositionCalculator; -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.core.feed.FeedMother; -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.core.util.CollectionTestUtil.concat; -import static de.danoeh.antennapod.core.util.CollectionTestUtil.list; -import static de.danoeh.antennapod.core.util.FeedItemUtil.getIdList; -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, getIdList(queue)); - } - - 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) { - FeedItem item = new FeedItem(id, "Item" + id, "ItemId" + id, "url", - new Date(), FeedItem.PLAYED, FeedMother.anyFeed()); - FeedMedia media = new FeedMedia(item, "http://download.url.net/" + id, 1234567, "audio/mpeg"); - media.setId(item.getId()); - item.setMedia(media); - return item; - } - -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java b/core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java deleted file mode 100644 index 21f1ef5d4..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/CollectionTestUtil.java +++ /dev/null @@ -1,30 +0,0 @@ -package de.danoeh.antennapod.core.util; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class CollectionTestUtil { - - public static <T> List<? extends T> concat(T item, List<? extends T> list) { - List<T> res = new ArrayList<>(list); - res.add(0, item); - return res; - } - - public static <T> List<? extends T> concat(List<? extends T> list, T item) { - List<T> res = new ArrayList<>(list); - res.add(item); - return res; - } - - public static <T> List<? extends T> concat(List<? extends T> list1, List<? extends T> list2) { - List<T> res = new ArrayList<>(list1); - res.addAll(list2); - return res; - } - - public static <T> List<T> list(T... a) { - return Arrays.asList(a); - } -} |