diff options
author | Herbert Reiter <46045854+damoasda@users.noreply.github.com> | 2021-01-23 23:12:01 +0100 |
---|---|---|
committer | Herbert Reiter <46045854+damoasda@users.noreply.github.com> | 2021-01-23 23:12:01 +0100 |
commit | a87c505380ee9f4ce0247a8636a78881cc622a58 (patch) | |
tree | 7a76a9bfe8a573c58f1d49e0cf8a41fdb43da4d7 /app/src/androidTest | |
parent | 35d010caa23124d12d8487fefadf6ad64beb50dc (diff) | |
download | AntennaPod-a87c505380ee9f4ce0247a8636a78881cc622a58.zip |
Run DbReaderTest etc. with Robolectric
Diffstat (limited to 'app/src/androidTest')
4 files changed, 0 insertions, 759 deletions
diff --git a/app/src/androidTest/java/de/test/antennapod/storage/DBCleanupTests.java b/app/src/androidTest/java/de/test/antennapod/storage/DBCleanupTests.java deleted file mode 100644 index 339d3cea9..000000000 --- a/app/src/androidTest/java/de/test/antennapod/storage/DBCleanupTests.java +++ /dev/null @@ -1,216 +0,0 @@ -package de.test.antennapod.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 androidx.test.filters.SmallTest; -import de.danoeh.antennapod.core.feed.Feed; -import de.danoeh.antennapod.core.feed.FeedItem; -import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.storage.DBTasks; -import de.danoeh.antennapod.core.storage.PodDBAdapter; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static de.test.antennapod.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 - */ -@SmallTest -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; - } - - @After - public void tearDown() throws Exception { - assertTrue(PodDBAdapter.deleteDatabase()); - - cleanupDestFolder(destFolder); - assertTrue(destFolder.delete()); - } - - private void cleanupDestFolder(File destFolder) { - for (File f : destFolder.listFiles()) { - assertTrue(f.delete()); - } - } - - @Before - public void setUp() throws Exception { - context = InstrumentationRegistry.getInstrumentation().getTargetContext(); - destFolder = new File(context.getCacheDir(), "DDCleanupTests"); - 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); - } - - @Test - public void testPerformAutoCleanupShouldDelete() throws IOException { - final int NUM_ITEMS = 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(NUM_ITEMS, feed, items, files, FeedItem.PLAYED, false, false); - - DBTasks.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()); - } - } - } - - 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", "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); - assertTrue(item.getMedia().getId() != 0); - } - } - - @Test - public void testPerformAutoCleanupHandleUnplayed() throws IOException { - final int NUM_ITEMS = 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(NUM_ITEMS, feed, items, files, FeedItem.UNPLAYED, false, false); - - DBTasks.performAutoCleanup(context); - for (File file : files) { - assertTrue(file.exists()); - } - } - - @Test - public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue() throws IOException { - final int NUM_ITEMS = 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(NUM_ITEMS, feed, items, files, FeedItem.PLAYED, true, false); - - DBTasks.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. - * @throws IOException - */ - @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(); - m.setDownloaded(true); - m.setFile_url("file"); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setMedia(m); - adapter.close(); - - testPerformAutoCleanupShouldNotDeleteBecauseInQueue(); - } - - @Test - public void testPerformAutoCleanupShouldNotDeleteBecauseFavorite() throws IOException { - final int NUM_ITEMS = 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(NUM_ITEMS, feed, items, files, FeedItem.PLAYED, false, true); - - DBTasks.performAutoCleanup(context); - for (File file : files) { - assertTrue(file.exists()); - } - } -} diff --git a/app/src/androidTest/java/de/test/antennapod/storage/DBQueueCleanupAlgorithmTest.java b/app/src/androidTest/java/de/test/antennapod/storage/DBQueueCleanupAlgorithmTest.java deleted file mode 100644 index de810c701..000000000 --- a/app/src/androidTest/java/de/test/antennapod/storage/DBQueueCleanupAlgorithmTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package de.test.antennapod.storage; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import androidx.test.filters.SmallTest; -import de.danoeh.antennapod.core.feed.Feed; -import de.danoeh.antennapod.core.feed.FeedItem; -import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.storage.DBTasks; -import org.junit.Test; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * Tests that the APQueueCleanupAlgorithm is working correctly. - */ -@SmallTest -public class DBQueueCleanupAlgorithmTest extends DBCleanupTests { - - private static final String TAG = "DBQueueCleanupAlgorithmTest"; - - public DBQueueCleanupAlgorithmTest() { - setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_QUEUE); - } - - /** - * 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 NUM_ITEMS = 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(NUM_ITEMS, feed, items, files, FeedItem.UNPLAYED, false, false); - - DBTasks.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/app/src/androidTest/java/de/test/antennapod/storage/DBReaderTest.java b/app/src/androidTest/java/de/test/antennapod/storage/DBReaderTest.java deleted file mode 100644 index 8811d9b31..000000000 --- a/app/src/androidTest/java/de/test/antennapod/storage/DBReaderTest.java +++ /dev/null @@ -1,411 +0,0 @@ -package de.test.antennapod.storage; - -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Random; - -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.filters.SmallTest; -import de.danoeh.antennapod.core.feed.Feed; -import de.danoeh.antennapod.core.feed.FeedItem; -import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.storage.DBReader; -import de.danoeh.antennapod.core.storage.PodDBAdapter; -import de.danoeh.antennapod.core.util.LongList; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static de.test.antennapod.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 - */ -@SmallTest -public class DBReaderTest { - - @After - public void tearDown() throws Exception { - assertTrue(PodDBAdapter.deleteDatabase()); - } - - @Before - public void setUp() throws Exception { - // create new database - PodDBAdapter.init(InstrumentationRegistry.getInstrumentation().getTargetContext()); - PodDBAdapter.deleteDatabase(); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.close(); - } - - @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(); - - Feed feed1 = new Feed(0, null, "A", "link", "d", null, null, null, "rss", "A", null, "", "", true); - Feed feed2 = new Feed(0, null, "b", "link", "d", null, null, null, "rss", "b", null, "", "", true); - Feed feed3 = new Feed(0, null, "C", "link", "d", null, null, null, "rss", "C", null, "", "", true); - Feed feed4 = new Feed(0, null, "d", "link", "d", null, null, null, "rss", "d", null, "", "", true); - 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).getDownload_url()); - } - } - - @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); - assertNotNull(savedItems); - assertEquals(items.size(), savedItems.size()); - for (int i = 0; i < savedItems.size(); i++) { - assertEquals(savedItems.get(i).getId(), items.get(i).getId()); - } - } - - 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()); - } - } - - 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().setFile_url("file" + i); - downloaded.add(item); - } - } - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setFeedItemlist(downloaded); - adapter.close(); - return downloaded; - } - - @Test - public void testGetDownloadedItems() { - final int numItems = 10; - List<FeedItem> downloaded = saveDownloadedItems(numItems); - List<FeedItem> downloaded_saved = DBReader.getDownloadedItems(); - assertNotNull(downloaded_saved); - assertEquals(downloaded.size(), downloaded_saved.size()); - for (FeedItem item : downloaded_saved) { - assertNotNull(item.getMedia()); - assertTrue(item.getMedia().isDownloaded()); - assertNotNull(item.getMedia().getDownload_url()); - } - } - - 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.setFeedItemlist(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.getNewItemsList(0, Integer.MAX_VALUE); - 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 testGetPlaybackHistory() { - final int numItems = (DBReader.PLAYBACK_HISTORY_SIZE + 1) * 2; - final int playedItems = DBReader.PLAYBACK_HISTORY_SIZE + 1; - final int numReturnedItems = Math.min(playedItems, DBReader.PLAYBACK_HISTORY_SIZE); - 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.getPlaybackHistory(); - assertNotNull(saved); - assertEquals("Wrong size: ", numReturnedItems, saved.size()); - for (int i = 0; i < numReturnedItems; i++) { - FeedItem item = saved.get(i); - assertNotNull(item.getMedia().getPlaybackCompletionDate()); - assertEquals("Wrong sort order: ", item.getId(), ids[i]); - } - } - - @Test - public void testGetNavDrawerDataQueueEmptyNoUnreadItems() { - final int NUM_FEEDS = 10; - final int NUM_ITEMS = 10; - DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true); - DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData(); - assertEquals(NUM_FEEDS, navDrawerData.feeds.size()); - assertEquals(0, navDrawerData.numNewItems); - assertEquals(0, navDrawerData.queueSize); - } - - @Test - public void testGetNavDrawerDataQueueNotEmptyWithUnreadItems() { - final int NUM_FEEDS = 10; - final int NUM_ITEMS = 10; - final int NUM_QUEUE = 1; - final int NUM_NEW = 2; - List<Feed> feeds = DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - for (int i = 0; i < NUM_NEW; i++) { - FeedItem item = feeds.get(0).getItems().get(i); - item.setNew(); - adapter.setSingleFeedItem(item); - } - List<FeedItem> queue = new ArrayList<>(); - for (int i = 0; i < NUM_QUEUE; i++) { - FeedItem item = feeds.get(1).getItems().get(i); - queue.add(item); - } - adapter.setQueue(queue); - - adapter.close(); - - DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData(); - assertEquals(NUM_FEEDS, navDrawerData.feeds.size()); - assertEquals(NUM_NEW, navDrawerData.numNewItems); - assertEquals(NUM_QUEUE, navDrawerData.queueSize); - } - - @Test - public void testGetFeedItemlistCheckChaptersFalse() throws Exception { - 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() throws Exception { - 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() throws Exception { - 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() throws Exception { - final int NUM_CHAPTERS = 3; - DBTestUtils.saveFeedlist(1, 3, false, false, 0); - List<Feed> feeds = saveFeedlist(1, 3, false, true, NUM_CHAPTERS); - for (Feed feed : feeds) { - for (FeedItem item : feed.getItems()) { - assertTrue(item.hasChapters()); - item.setChapters(DBReader.loadChaptersOfFeedItem(item)); - assertTrue(item.hasChapters()); - assertNotNull(item.getChapters()); - assertEquals(NUM_CHAPTERS, item.getChapters().size()); - } - } - } - - @Test - public void testGetItemWithChapters() throws Exception { - final int NUM_CHAPTERS = 3; - List<Feed> feeds = saveFeedlist(1, 1, false, true, NUM_CHAPTERS); - 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(), item2.getChapters()); - } -} diff --git a/app/src/androidTest/java/de/test/antennapod/storage/DBTestUtils.java b/app/src/androidTest/java/de/test/antennapod/storage/DBTestUtils.java deleted file mode 100644 index 840a7d01f..000000000 --- a/app/src/androidTest/java/de/test/antennapod/storage/DBTestUtils.java +++ /dev/null @@ -1,79 +0,0 @@ -package de.test.antennapod.storage; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -import de.danoeh.antennapod.core.feed.Chapter; -import de.danoeh.antennapod.core.feed.Feed; -import de.danoeh.antennapod.core.feed.FeedItem; -import de.danoeh.antennapod.core.feed.FeedMedia; -import de.danoeh.antennapod.core.feed.SimpleChapter; -import de.danoeh.antennapod.core.storage.PodDBAdapter; -import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator; - -import static org.junit.Assert.assertTrue; - -/** - * Utility methods for DB* tests. - */ -class DBTestUtils { - - private 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, false); - 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 SimpleChapter(k, "item " + j + " chapter " + k, - "http://example.com", "http://example.com/image.png")); - } - } - f.getItems().add(item); - } - Collections.sort(f.getItems(), new FeedItemPubdateComparator()); - adapter.setCompleteFeed(f); - assertTrue(f.getId() != 0); - for (FeedItem item : f.getItems()) { - assertTrue(item.getId() != 0); - } - feeds.add(f); - } - adapter.close(); - - return feeds; - } -} |