Merge pull request #4894 from damoasda/robolectric-DbReaderTest

Run DbReaderTest etc. with Robolectric
This commit is contained in:
ByteHamster 2021-01-24 12:01:10 +01:00 committed by GitHub
commit 72ab17f153
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 128 additions and 101 deletions

View File

@ -1,5 +1,6 @@
package de.test.antennapod.storage;
package de.danoeh.antennapod.core.storage;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
@ -11,27 +12,34 @@ import java.util.Date;
import java.util.List;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import de.danoeh.antennapod.core.ApplicationCallbacks;
import de.danoeh.antennapod.core.ClientConfig;
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.PlaybackPreferences;
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 org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static de.test.antennapod.storage.DBTestUtils.saveFeedlist;
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;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Test class for DBTasks
* Test class for DBTasks.
*/
@SmallTest
public class DBCleanupTests {
@RunWith(RobolectricTestRunner.class)
public class DbCleanupTests {
static final int EPISODE_CACHE_SIZE = 5;
private int cleanupAlgorithm;
@ -39,7 +47,7 @@ public class DBCleanupTests {
private File destFolder;
public DBCleanupTests() {
public DbCleanupTests() {
setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_DEFAULT);
}
@ -47,24 +55,11 @@ public class DBCleanupTests {
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 {
public void setUp() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
destFolder = new File(context.getCacheDir(), "DDCleanupTests");
destFolder = new File(context.getCacheDir(), "DbCleanupTests");
//noinspection ResultOfMethodCallIgnored
destFolder.mkdir();
cleanupDestFolder(destFolder);
assertNotNull(destFolder);
@ -78,24 +73,46 @@ public class DBCleanupTests {
adapter.open();
adapter.close();
SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()).edit();
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);
Application app = (Application) context;
ClientConfig.applicationCallbacks = mock(ApplicationCallbacks.class);
when(ClientConfig.applicationCallbacks.getApplicationInstance()).thenReturn(app);
}
@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 NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
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(NUM_ITEMS, feed, items, files, FeedItem.PLAYED, false, false);
populateItems(numItems, feed, items, files, FeedItem.PLAYED, false, false);
DBTasks.performAutoCleanup(context);
for (int i = 0; i < files.size(); i++) {
@ -107,6 +124,7 @@ public class DBCleanupTests {
}
}
@SuppressWarnings("SameParameterValue")
void populateItems(final int numItems, Feed feed, List<FeedItem> items,
List<File> files, int itemState, boolean addToQueue,
boolean addToFavorites) throws IOException {
@ -121,7 +139,8 @@ public class DBCleanupTests {
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));
item.setMedia(new FeedMedia(0, item, 1, 0, 1L, "m",
f.getAbsolutePath(), "url", true, playbackCompletionDate, 0, 0));
items.add(item);
}
@ -139,19 +158,20 @@ public class DBCleanupTests {
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 NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
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(NUM_ITEMS, feed, items, files, FeedItem.UNPLAYED, false, false);
populateItems(numItems, feed, items, files, FeedItem.UNPLAYED, false, false);
DBTasks.performAutoCleanup(context);
for (File file : files) {
@ -161,13 +181,13 @@ public class DBCleanupTests {
@Test
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue() throws IOException {
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
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(NUM_ITEMS, feed, items, files, FeedItem.PLAYED, true, false);
populateItems(numItems, feed, items, files, FeedItem.PLAYED, true, false);
DBTasks.performAutoCleanup(context);
for (File file : files) {
@ -176,9 +196,9 @@ public class DBCleanupTests {
}
/**
* 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
* 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 {
@ -188,6 +208,7 @@ public class DBCleanupTests {
// 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.setFile_url("file");
PodDBAdapter adapter = PodDBAdapter.getInstance();
@ -200,13 +221,13 @@ public class DBCleanupTests {
@Test
public void testPerformAutoCleanupShouldNotDeleteBecauseFavorite() throws IOException {
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
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(NUM_ITEMS, feed, items, files, FeedItem.PLAYED, false, true);
populateItems(numItems, feed, items, files, FeedItem.PLAYED, false, true);
DBTasks.performAutoCleanup(context);
for (File file : files) {

View File

@ -1,16 +1,17 @@
package de.test.antennapod.storage;
package de.danoeh.antennapod.core.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 org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -18,28 +19,26 @@ import static org.junit.Assert.assertTrue;
/**
* Tests that the APQueueCleanupAlgorithm is working correctly.
*/
@SmallTest
public class DBQueueCleanupAlgorithmTest extends DBCleanupTests {
@RunWith(RobolectricTestRunner.class)
public class DbQueueCleanupAlgorithmTest extends DbCleanupTests {
private static final String TAG = "DBQueueCleanupAlgorithmTest";
public 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
* if they aren't in the queue.
*/
@Test
public void testPerformAutoCleanupHandleUnplayed() throws IOException {
final int NUM_ITEMS = EPISODE_CACHE_SIZE * 2;
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(NUM_ITEMS, feed, items, files, FeedItem.UNPLAYED, false, false);
populateItems(numItems, feed, items, files, FeedItem.UNPLAYED, false, false);
DBTasks.performAutoCleanup(context);
for (int i = 0; i < files.size(); i++) {

View File

@ -1,4 +1,6 @@
package de.test.antennapod.storage;
package de.danoeh.antennapod.core.storage;
import android.content.Context;
import java.util.ArrayList;
import java.util.Date;
@ -6,18 +8,18 @@ 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.preferences.UserPreferences;
import de.danoeh.antennapod.core.util.LongList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static de.test.antennapod.storage.DBTestUtils.saveFeedlist;
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;
@ -25,26 +27,30 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Test class for DBReader
* Test class for DBReader.
*/
@SmallTest
public class DBReaderTest {
@After
public void tearDown() throws Exception {
assertTrue(PodDBAdapter.deleteDatabase());
}
@SuppressWarnings("ConstantConditions")
@RunWith(RobolectricTestRunner.class)
public class DbReaderTest {
@Before
public void setUp() throws Exception {
// create new database
PodDBAdapter.init(InstrumentationRegistry.getInstrumentation().getTargetContext());
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();
}
@Test
public void testGetFeedList() {
List<Feed> feeds = saveFeedlist(10, 0, false);
@ -136,6 +142,7 @@ public class DBReaderTest {
}
}
@SuppressWarnings("SameParameterValue")
private List<FeedItem> saveQueue(int numItems) {
if (numItems <= 0) {
throw new IllegalArgumentException("numItems<=0");
@ -162,7 +169,7 @@ public class DBReaderTest {
}
@Test
public void testGetQueueIDList() {
public void testGetQueueIdList() {
final int numItems = 10;
List<FeedItem> queue = saveQueue(numItems);
LongList ids = DBReader.getQueueIDList();
@ -187,6 +194,7 @@ public class DBReaderTest {
}
}
@SuppressWarnings("SameParameterValue")
private List<FeedItem> saveDownloadedItems(int numItems) {
if (numItems <= 0) {
throw new IllegalArgumentException("numItems<=0");
@ -219,16 +227,17 @@ public class DBReaderTest {
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) {
List<FeedItem> downloadedSaved = DBReader.getDownloadedItems();
assertNotNull(downloadedSaved);
assertEquals(downloaded.size(), downloadedSaved.size());
for (FeedItem item : downloadedSaved) {
assertNotNull(item.getMedia());
assertTrue(item.getMedia().isDownloaded());
assertNotNull(item.getMedia().getDownload_url());
}
}
@SuppressWarnings("SameParameterValue")
private List<FeedItem> saveNewItems(int numItems) {
List<Feed> feeds = saveFeedlist(numItems, numItems, true);
List<FeedItem> items = new ArrayList<>();
@ -285,7 +294,7 @@ public class DBReaderTest {
final int numReturnedItems = Math.min(playedItems, DBReader.PLAYBACK_HISTORY_SIZE);
final int numFeeds = 1;
Feed feed = DBTestUtils.saveFeedlist(numFeeds, numItems, true).get(0);
Feed feed = DbTestUtils.saveFeedlist(numFeeds, numItems, true).get(0);
long[] ids = new long[playedItems];
PodDBAdapter adapter = PodDBAdapter.getInstance();
@ -310,31 +319,31 @@ public class DBReaderTest {
@Test
public void testGetNavDrawerDataQueueEmptyNoUnreadItems() {
final int NUM_FEEDS = 10;
final int NUM_ITEMS = 10;
DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true);
final int numFeeds = 10;
final int numItems = 10;
DbTestUtils.saveFeedlist(numFeeds, numItems, true);
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
assertEquals(NUM_FEEDS, navDrawerData.feeds.size());
assertEquals(numFeeds, 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);
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 < NUM_NEW; i++) {
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 < NUM_QUEUE; i++) {
for (int i = 0; i < numQueue; i++) {
FeedItem item = feeds.get(1).getItems().get(i);
queue.add(item);
}
@ -343,14 +352,14 @@ public class DBReaderTest {
adapter.close();
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
assertEquals(NUM_FEEDS, navDrawerData.feeds.size());
assertEquals(NUM_NEW, navDrawerData.numNewItems);
assertEquals(NUM_QUEUE, navDrawerData.queueSize);
assertEquals(numFeeds, navDrawerData.feeds.size());
assertEquals(numNew, navDrawerData.numNewItems);
assertEquals(numQueue, navDrawerData.queueSize);
}
@Test
public void testGetFeedItemlistCheckChaptersFalse() throws Exception {
List<Feed> feeds = DBTestUtils.saveFeedlist(10, 10, false, false, 0);
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());
@ -359,7 +368,7 @@ public class DBReaderTest {
}
@Test
public void testGetFeedItemlistCheckChaptersTrue() throws Exception {
public void testGetFeedItemlistCheckChaptersTrue() {
List<Feed> feeds = saveFeedlist(10, 10, false, true, 10);
for (Feed feed : feeds) {
for (FeedItem item : feed.getItems()) {
@ -369,7 +378,7 @@ public class DBReaderTest {
}
@Test
public void testLoadChaptersOfFeedItemNoChapters() throws Exception {
public void testLoadChaptersOfFeedItemNoChapters() {
List<Feed> feeds = saveFeedlist(1, 3, false, false, 0);
saveFeedlist(1, 3, false, true, 3);
for (Feed feed : feeds) {
@ -383,25 +392,25 @@ public class DBReaderTest {
}
@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);
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(NUM_CHAPTERS, item.getChapters().size());
assertEquals(numChapters, item.getChapters().size());
}
}
}
@Test
public void testGetItemWithChapters() throws Exception {
final int NUM_CHAPTERS = 3;
List<Feed> feeds = saveFeedlist(1, 1, false, true, NUM_CHAPTERS);
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));

View File

@ -1,4 +1,4 @@
package de.test.antennapod.storage;
package de.danoeh.antennapod.core.storage;
import java.util.ArrayList;
import java.util.Collections;
@ -10,7 +10,6 @@ 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;
@ -18,9 +17,8 @@ import static org.junit.Assert.assertTrue;
/**
* Utility methods for DB* tests.
*/
class DBTestUtils {
abstract class DbTestUtils {
private DBTestUtils(){}
/**
* Use this method when tests don't involve chapters.
*/