Merge pull request #4894 from damoasda/robolectric-DbReaderTest
Run DbReaderTest etc. with Robolectric
This commit is contained in:
commit
72ab17f153
|
@ -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.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
|
@ -11,27 +12,34 @@ import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry;
|
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.Feed;
|
||||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
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.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.storage.DBTasks;
|
|
||||||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
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
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class DBCleanupTests {
|
public class DbCleanupTests {
|
||||||
|
|
||||||
static final int EPISODE_CACHE_SIZE = 5;
|
static final int EPISODE_CACHE_SIZE = 5;
|
||||||
private int cleanupAlgorithm;
|
private int cleanupAlgorithm;
|
||||||
|
|
||||||
|
@ -39,7 +47,7 @@ public class DBCleanupTests {
|
||||||
|
|
||||||
private File destFolder;
|
private File destFolder;
|
||||||
|
|
||||||
public DBCleanupTests() {
|
public DbCleanupTests() {
|
||||||
setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_DEFAULT);
|
setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,24 +55,11 @@ public class DBCleanupTests {
|
||||||
this.cleanupAlgorithm = 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
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() {
|
||||||
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
|
||||||
destFolder = new File(context.getCacheDir(), "DDCleanupTests");
|
destFolder = new File(context.getCacheDir(), "DbCleanupTests");
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
destFolder.mkdir();
|
destFolder.mkdir();
|
||||||
cleanupDestFolder(destFolder);
|
cleanupDestFolder(destFolder);
|
||||||
assertNotNull(destFolder);
|
assertNotNull(destFolder);
|
||||||
|
@ -78,24 +73,46 @@ public class DBCleanupTests {
|
||||||
adapter.open();
|
adapter.open();
|
||||||
adapter.close();
|
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_CACHE_SIZE, Integer.toString(EPISODE_CACHE_SIZE));
|
||||||
prefEdit.putString(UserPreferences.PREF_EPISODE_CLEANUP, Integer.toString(cleanupAlgorithm));
|
prefEdit.putString(UserPreferences.PREF_EPISODE_CLEANUP, Integer.toString(cleanupAlgorithm));
|
||||||
prefEdit.putBoolean(UserPreferences.PREF_ENABLE_AUTODL, true);
|
prefEdit.putBoolean(UserPreferences.PREF_ENABLE_AUTODL, true);
|
||||||
prefEdit.commit();
|
prefEdit.commit();
|
||||||
|
|
||||||
UserPreferences.init(context);
|
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
|
@Test
|
||||||
public void testPerformAutoCleanupShouldDelete() throws IOException {
|
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");
|
Feed feed = new Feed("url", null, "title");
|
||||||
List<FeedItem> items = new ArrayList<>();
|
List<FeedItem> items = new ArrayList<>();
|
||||||
feed.setItems(items);
|
feed.setItems(items);
|
||||||
List<File> files = new ArrayList<>();
|
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);
|
DBTasks.performAutoCleanup(context);
|
||||||
for (int i = 0; i < files.size(); i++) {
|
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,
|
void populateItems(final int numItems, Feed feed, List<FeedItem> items,
|
||||||
List<File> files, int itemState, boolean addToQueue,
|
List<File> files, int itemState, boolean addToQueue,
|
||||||
boolean addToFavorites) throws IOException {
|
boolean addToFavorites) throws IOException {
|
||||||
|
@ -121,7 +139,8 @@ public class DBCleanupTests {
|
||||||
File f = new File(destFolder, "file " + i);
|
File f = new File(destFolder, "file " + i);
|
||||||
assertTrue(f.createNewFile());
|
assertTrue(f.createNewFile());
|
||||||
files.add(f);
|
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);
|
items.add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,19 +158,20 @@ public class DBCleanupTests {
|
||||||
assertTrue(feed.getId() != 0);
|
assertTrue(feed.getId() != 0);
|
||||||
for (FeedItem item : items) {
|
for (FeedItem item : items) {
|
||||||
assertTrue(item.getId() != 0);
|
assertTrue(item.getId() != 0);
|
||||||
|
//noinspection ConstantConditions
|
||||||
assertTrue(item.getMedia().getId() != 0);
|
assertTrue(item.getMedia().getId() != 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPerformAutoCleanupHandleUnplayed() throws IOException {
|
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");
|
Feed feed = new Feed("url", null, "title");
|
||||||
List<FeedItem> items = new ArrayList<>();
|
List<FeedItem> items = new ArrayList<>();
|
||||||
feed.setItems(items);
|
feed.setItems(items);
|
||||||
List<File> files = new ArrayList<>();
|
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);
|
DBTasks.performAutoCleanup(context);
|
||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
|
@ -161,13 +181,13 @@ public class DBCleanupTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue() throws IOException {
|
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");
|
Feed feed = new Feed("url", null, "title");
|
||||||
List<FeedItem> items = new ArrayList<>();
|
List<FeedItem> items = new ArrayList<>();
|
||||||
feed.setItems(items);
|
feed.setItems(items);
|
||||||
List<File> files = new ArrayList<>();
|
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);
|
DBTasks.performAutoCleanup(context);
|
||||||
for (File file : files) {
|
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
|
* Reproduces a bug where DBTasks.performAutoCleanup(android.content.Context) would use the ID
|
||||||
* call to DBWriter.deleteFeedMediaOfItem instead of the ID of the FeedMedia. This would cause the wrong item to be deleted.
|
* of the FeedItem in the call to DBWriter.deleteFeedMediaOfItem instead of the ID of the FeedMedia.
|
||||||
* @throws IOException
|
* This would cause the wrong item to be deleted.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException {
|
public void testPerformAutoCleanupShouldNotDeleteBecauseInQueue_withFeedsWithNoMedia() throws IOException {
|
||||||
|
@ -188,6 +208,7 @@ public class DBCleanupTests {
|
||||||
// add candidate for performAutoCleanup
|
// add candidate for performAutoCleanup
|
||||||
List<Feed> feeds = saveFeedlist(1, 1, true);
|
List<Feed> feeds = saveFeedlist(1, 1, true);
|
||||||
FeedMedia m = feeds.get(0).getItems().get(0).getMedia();
|
FeedMedia m = feeds.get(0).getItems().get(0).getMedia();
|
||||||
|
//noinspection ConstantConditions
|
||||||
m.setDownloaded(true);
|
m.setDownloaded(true);
|
||||||
m.setFile_url("file");
|
m.setFile_url("file");
|
||||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||||
|
@ -200,13 +221,13 @@ public class DBCleanupTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPerformAutoCleanupShouldNotDeleteBecauseFavorite() throws IOException {
|
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");
|
Feed feed = new Feed("url", null, "title");
|
||||||
List<FeedItem> items = new ArrayList<>();
|
List<FeedItem> items = new ArrayList<>();
|
||||||
feed.setItems(items);
|
feed.setItems(items);
|
||||||
List<File> files = new ArrayList<>();
|
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);
|
DBTasks.performAutoCleanup(context);
|
||||||
for (File file : files) {
|
for (File file : files) {
|
|
@ -1,16 +1,17 @@
|
||||||
package de.test.antennapod.storage;
|
package de.danoeh.antennapod.core.storage;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import androidx.test.filters.SmallTest;
|
|
||||||
import de.danoeh.antennapod.core.feed.Feed;
|
import de.danoeh.antennapod.core.feed.Feed;
|
||||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.storage.DBTasks;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -18,28 +19,26 @@ import static org.junit.Assert.assertTrue;
|
||||||
/**
|
/**
|
||||||
* Tests that the APQueueCleanupAlgorithm is working correctly.
|
* Tests that the APQueueCleanupAlgorithm is working correctly.
|
||||||
*/
|
*/
|
||||||
@SmallTest
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class DBQueueCleanupAlgorithmTest extends DBCleanupTests {
|
public class DbQueueCleanupAlgorithmTest extends DbCleanupTests {
|
||||||
|
|
||||||
private static final String TAG = "DBQueueCleanupAlgorithmTest";
|
public DbQueueCleanupAlgorithmTest() {
|
||||||
|
|
||||||
public DBQueueCleanupAlgorithmTest() {
|
|
||||||
setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_QUEUE);
|
setCleanupAlgorithm(UserPreferences.EPISODE_CLEANUP_QUEUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For APQueueCleanupAlgorithm we expect even unplayed episodes to be deleted if needed
|
* 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
|
@Test
|
||||||
public void testPerformAutoCleanupHandleUnplayed() throws IOException {
|
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");
|
Feed feed = new Feed("url", null, "title");
|
||||||
List<FeedItem> items = new ArrayList<>();
|
List<FeedItem> items = new ArrayList<>();
|
||||||
feed.setItems(items);
|
feed.setItems(items);
|
||||||
List<File> files = new ArrayList<>();
|
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);
|
DBTasks.performAutoCleanup(context);
|
||||||
for (int i = 0; i < files.size(); i++) {
|
for (int i = 0; i < files.size(); i++) {
|
|
@ -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.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
@ -6,18 +8,18 @@ import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
import androidx.test.platform.app.InstrumentationRegistry;
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
import androidx.test.filters.SmallTest;
|
|
||||||
import de.danoeh.antennapod.core.feed.Feed;
|
import de.danoeh.antennapod.core.feed.Feed;
|
||||||
import de.danoeh.antennapod.core.feed.FeedItem;
|
import de.danoeh.antennapod.core.feed.FeedItem;
|
||||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||||
import de.danoeh.antennapod.core.storage.DBReader;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
|
||||||
import de.danoeh.antennapod.core.util.LongList;
|
import de.danoeh.antennapod.core.util.LongList;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
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.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
@ -25,26 +27,30 @@ import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test class for DBReader
|
* Test class for DBReader.
|
||||||
*/
|
*/
|
||||||
@SmallTest
|
@SuppressWarnings("ConstantConditions")
|
||||||
public class DBReaderTest {
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class DbReaderTest {
|
||||||
@After
|
|
||||||
public void tearDown() throws Exception {
|
|
||||||
assertTrue(PodDBAdapter.deleteDatabase());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() {
|
||||||
// create new database
|
Context context = InstrumentationRegistry.getInstrumentation().getContext();
|
||||||
PodDBAdapter.init(InstrumentationRegistry.getInstrumentation().getTargetContext());
|
UserPreferences.init(context);
|
||||||
|
|
||||||
|
PodDBAdapter.init(context);
|
||||||
PodDBAdapter.deleteDatabase();
|
PodDBAdapter.deleteDatabase();
|
||||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||||
adapter.open();
|
adapter.open();
|
||||||
adapter.close();
|
adapter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
PodDBAdapter.tearDownTests();
|
||||||
|
DBWriter.tearDownTests();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFeedList() {
|
public void testGetFeedList() {
|
||||||
List<Feed> feeds = saveFeedlist(10, 0, false);
|
List<Feed> feeds = saveFeedlist(10, 0, false);
|
||||||
|
@ -136,6 +142,7 @@ public class DBReaderTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("SameParameterValue")
|
||||||
private List<FeedItem> saveQueue(int numItems) {
|
private List<FeedItem> saveQueue(int numItems) {
|
||||||
if (numItems <= 0) {
|
if (numItems <= 0) {
|
||||||
throw new IllegalArgumentException("numItems<=0");
|
throw new IllegalArgumentException("numItems<=0");
|
||||||
|
@ -162,7 +169,7 @@ public class DBReaderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetQueueIDList() {
|
public void testGetQueueIdList() {
|
||||||
final int numItems = 10;
|
final int numItems = 10;
|
||||||
List<FeedItem> queue = saveQueue(numItems);
|
List<FeedItem> queue = saveQueue(numItems);
|
||||||
LongList ids = DBReader.getQueueIDList();
|
LongList ids = DBReader.getQueueIDList();
|
||||||
|
@ -187,6 +194,7 @@ public class DBReaderTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("SameParameterValue")
|
||||||
private List<FeedItem> saveDownloadedItems(int numItems) {
|
private List<FeedItem> saveDownloadedItems(int numItems) {
|
||||||
if (numItems <= 0) {
|
if (numItems <= 0) {
|
||||||
throw new IllegalArgumentException("numItems<=0");
|
throw new IllegalArgumentException("numItems<=0");
|
||||||
|
@ -219,16 +227,17 @@ public class DBReaderTest {
|
||||||
public void testGetDownloadedItems() {
|
public void testGetDownloadedItems() {
|
||||||
final int numItems = 10;
|
final int numItems = 10;
|
||||||
List<FeedItem> downloaded = saveDownloadedItems(numItems);
|
List<FeedItem> downloaded = saveDownloadedItems(numItems);
|
||||||
List<FeedItem> downloaded_saved = DBReader.getDownloadedItems();
|
List<FeedItem> downloadedSaved = DBReader.getDownloadedItems();
|
||||||
assertNotNull(downloaded_saved);
|
assertNotNull(downloadedSaved);
|
||||||
assertEquals(downloaded.size(), downloaded_saved.size());
|
assertEquals(downloaded.size(), downloadedSaved.size());
|
||||||
for (FeedItem item : downloaded_saved) {
|
for (FeedItem item : downloadedSaved) {
|
||||||
assertNotNull(item.getMedia());
|
assertNotNull(item.getMedia());
|
||||||
assertTrue(item.getMedia().isDownloaded());
|
assertTrue(item.getMedia().isDownloaded());
|
||||||
assertNotNull(item.getMedia().getDownload_url());
|
assertNotNull(item.getMedia().getDownload_url());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("SameParameterValue")
|
||||||
private List<FeedItem> saveNewItems(int numItems) {
|
private List<FeedItem> saveNewItems(int numItems) {
|
||||||
List<Feed> feeds = saveFeedlist(numItems, numItems, true);
|
List<Feed> feeds = saveFeedlist(numItems, numItems, true);
|
||||||
List<FeedItem> items = new ArrayList<>();
|
List<FeedItem> items = new ArrayList<>();
|
||||||
|
@ -285,7 +294,7 @@ public class DBReaderTest {
|
||||||
final int numReturnedItems = Math.min(playedItems, DBReader.PLAYBACK_HISTORY_SIZE);
|
final int numReturnedItems = Math.min(playedItems, DBReader.PLAYBACK_HISTORY_SIZE);
|
||||||
final int numFeeds = 1;
|
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];
|
long[] ids = new long[playedItems];
|
||||||
|
|
||||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||||
|
@ -310,31 +319,31 @@ public class DBReaderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetNavDrawerDataQueueEmptyNoUnreadItems() {
|
public void testGetNavDrawerDataQueueEmptyNoUnreadItems() {
|
||||||
final int NUM_FEEDS = 10;
|
final int numFeeds = 10;
|
||||||
final int NUM_ITEMS = 10;
|
final int numItems = 10;
|
||||||
DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true);
|
DbTestUtils.saveFeedlist(numFeeds, numItems, true);
|
||||||
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
|
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
|
||||||
assertEquals(NUM_FEEDS, navDrawerData.feeds.size());
|
assertEquals(numFeeds, navDrawerData.feeds.size());
|
||||||
assertEquals(0, navDrawerData.numNewItems);
|
assertEquals(0, navDrawerData.numNewItems);
|
||||||
assertEquals(0, navDrawerData.queueSize);
|
assertEquals(0, navDrawerData.queueSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetNavDrawerDataQueueNotEmptyWithUnreadItems() {
|
public void testGetNavDrawerDataQueueNotEmptyWithUnreadItems() {
|
||||||
final int NUM_FEEDS = 10;
|
final int numFeeds = 10;
|
||||||
final int NUM_ITEMS = 10;
|
final int numItems = 10;
|
||||||
final int NUM_QUEUE = 1;
|
final int numQueue = 1;
|
||||||
final int NUM_NEW = 2;
|
final int numNew = 2;
|
||||||
List<Feed> feeds = DBTestUtils.saveFeedlist(NUM_FEEDS, NUM_ITEMS, true);
|
List<Feed> feeds = DbTestUtils.saveFeedlist(numFeeds, numItems, true);
|
||||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||||
adapter.open();
|
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);
|
FeedItem item = feeds.get(0).getItems().get(i);
|
||||||
item.setNew();
|
item.setNew();
|
||||||
adapter.setSingleFeedItem(item);
|
adapter.setSingleFeedItem(item);
|
||||||
}
|
}
|
||||||
List<FeedItem> queue = new ArrayList<>();
|
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);
|
FeedItem item = feeds.get(1).getItems().get(i);
|
||||||
queue.add(item);
|
queue.add(item);
|
||||||
}
|
}
|
||||||
|
@ -343,14 +352,14 @@ public class DBReaderTest {
|
||||||
adapter.close();
|
adapter.close();
|
||||||
|
|
||||||
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
|
DBReader.NavDrawerData navDrawerData = DBReader.getNavDrawerData();
|
||||||
assertEquals(NUM_FEEDS, navDrawerData.feeds.size());
|
assertEquals(numFeeds, navDrawerData.feeds.size());
|
||||||
assertEquals(NUM_NEW, navDrawerData.numNewItems);
|
assertEquals(numNew, navDrawerData.numNewItems);
|
||||||
assertEquals(NUM_QUEUE, navDrawerData.queueSize);
|
assertEquals(numQueue, navDrawerData.queueSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFeedItemlistCheckChaptersFalse() throws Exception {
|
public void testGetFeedItemlistCheckChaptersFalse() {
|
||||||
List<Feed> feeds = DBTestUtils.saveFeedlist(10, 10, false, false, 0);
|
List<Feed> feeds = DbTestUtils.saveFeedlist(10, 10, false, false, 0);
|
||||||
for (Feed feed : feeds) {
|
for (Feed feed : feeds) {
|
||||||
for (FeedItem item : feed.getItems()) {
|
for (FeedItem item : feed.getItems()) {
|
||||||
assertFalse(item.hasChapters());
|
assertFalse(item.hasChapters());
|
||||||
|
@ -359,7 +368,7 @@ public class DBReaderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetFeedItemlistCheckChaptersTrue() throws Exception {
|
public void testGetFeedItemlistCheckChaptersTrue() {
|
||||||
List<Feed> feeds = saveFeedlist(10, 10, false, true, 10);
|
List<Feed> feeds = saveFeedlist(10, 10, false, true, 10);
|
||||||
for (Feed feed : feeds) {
|
for (Feed feed : feeds) {
|
||||||
for (FeedItem item : feed.getItems()) {
|
for (FeedItem item : feed.getItems()) {
|
||||||
|
@ -369,7 +378,7 @@ public class DBReaderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadChaptersOfFeedItemNoChapters() throws Exception {
|
public void testLoadChaptersOfFeedItemNoChapters() {
|
||||||
List<Feed> feeds = saveFeedlist(1, 3, false, false, 0);
|
List<Feed> feeds = saveFeedlist(1, 3, false, false, 0);
|
||||||
saveFeedlist(1, 3, false, true, 3);
|
saveFeedlist(1, 3, false, true, 3);
|
||||||
for (Feed feed : feeds) {
|
for (Feed feed : feeds) {
|
||||||
|
@ -383,25 +392,25 @@ public class DBReaderTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLoadChaptersOfFeedItemWithChapters() throws Exception {
|
public void testLoadChaptersOfFeedItemWithChapters() {
|
||||||
final int NUM_CHAPTERS = 3;
|
final int numChapters = 3;
|
||||||
DBTestUtils.saveFeedlist(1, 3, false, false, 0);
|
DbTestUtils.saveFeedlist(1, 3, false, false, 0);
|
||||||
List<Feed> feeds = saveFeedlist(1, 3, false, true, NUM_CHAPTERS);
|
List<Feed> feeds = saveFeedlist(1, 3, false, true, numChapters);
|
||||||
for (Feed feed : feeds) {
|
for (Feed feed : feeds) {
|
||||||
for (FeedItem item : feed.getItems()) {
|
for (FeedItem item : feed.getItems()) {
|
||||||
assertTrue(item.hasChapters());
|
assertTrue(item.hasChapters());
|
||||||
item.setChapters(DBReader.loadChaptersOfFeedItem(item));
|
item.setChapters(DBReader.loadChaptersOfFeedItem(item));
|
||||||
assertTrue(item.hasChapters());
|
assertTrue(item.hasChapters());
|
||||||
assertNotNull(item.getChapters());
|
assertNotNull(item.getChapters());
|
||||||
assertEquals(NUM_CHAPTERS, item.getChapters().size());
|
assertEquals(numChapters, item.getChapters().size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetItemWithChapters() throws Exception {
|
public void testGetItemWithChapters() {
|
||||||
final int NUM_CHAPTERS = 3;
|
final int numChapters = 3;
|
||||||
List<Feed> feeds = saveFeedlist(1, 1, false, true, NUM_CHAPTERS);
|
List<Feed> feeds = saveFeedlist(1, 1, false, true, numChapters);
|
||||||
FeedItem item1 = feeds.get(0).getItems().get(0);
|
FeedItem item1 = feeds.get(0).getItems().get(0);
|
||||||
FeedItem item2 = DBReader.getFeedItem(item1.getId());
|
FeedItem item2 = DBReader.getFeedItem(item1.getId());
|
||||||
item2.setChapters(DBReader.loadChaptersOfFeedItem(item2));
|
item2.setChapters(DBReader.loadChaptersOfFeedItem(item2));
|
|
@ -1,4 +1,4 @@
|
||||||
package de.test.antennapod.storage;
|
package de.danoeh.antennapod.core.storage;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
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.FeedItem;
|
||||||
import de.danoeh.antennapod.core.feed.FeedMedia;
|
import de.danoeh.antennapod.core.feed.FeedMedia;
|
||||||
import de.danoeh.antennapod.core.feed.SimpleChapter;
|
import de.danoeh.antennapod.core.feed.SimpleChapter;
|
||||||
import de.danoeh.antennapod.core.storage.PodDBAdapter;
|
|
||||||
import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator;
|
import de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
@ -18,9 +17,8 @@ import static org.junit.Assert.assertTrue;
|
||||||
/**
|
/**
|
||||||
* Utility methods for DB* tests.
|
* Utility methods for DB* tests.
|
||||||
*/
|
*/
|
||||||
class DBTestUtils {
|
abstract class DbTestUtils {
|
||||||
|
|
||||||
private DBTestUtils(){}
|
|
||||||
/**
|
/**
|
||||||
* Use this method when tests don't involve chapters.
|
* Use this method when tests don't involve chapters.
|
||||||
*/
|
*/
|
Loading…
Reference in New Issue