Merge pull request #4819 from damoasda/robolectric-DbTasksTest

Run DbTasksTest with Robolectric
This commit is contained in:
ByteHamster 2021-01-01 14:40:14 +01:00 committed by GitHub
commit dd541ced44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 31 deletions

View File

@ -1,13 +1,15 @@
package de.test.antennapod.storage; package de.danoeh.antennapod.core.storage;
import android.app.Application;
import android.content.Context; import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
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 java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -15,14 +17,13 @@ import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
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.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import static de.danoeh.antennapod.core.util.FeedItemUtil.getIdList; import static de.danoeh.antennapod.core.util.FeedItemUtil.getIdList;
import static java.util.Collections.singletonList; import static java.util.Collections.singletonList;
@ -32,22 +33,25 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame; import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
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 {@link DBTasks}.
*/ */
@SmallTest @RunWith(RobolectricTestRunner.class)
public class DBTasksTest { public class DbTasksTest {
private Context context; private Context context;
@After
public void tearDown() throws Exception {
assertTrue(PodDBAdapter.deleteDatabase());
}
@Before @Before
public void setUp() throws Exception { public void setUp() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext(); context = InstrumentationRegistry.getInstrumentation().getTargetContext();
UserPreferences.init(context);
PlaybackPreferences.init(context);
Application app = (Application) context;
ClientConfig.applicationCallbacks = mock(ApplicationCallbacks.class);
when(ClientConfig.applicationCallbacks.getApplicationInstance()).thenReturn(app);
// create new database // create new database
PodDBAdapter.init(context); PodDBAdapter.init(context);
@ -55,18 +59,23 @@ public class DBTasksTest {
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
adapter.close(); adapter.close();
}
UserPreferences.init(context); @After
public void tearDown() {
DBWriter.tearDownTests();
PodDBAdapter.tearDownTests();
} }
@Test @Test
public void testUpdateFeedNewFeed() { public void testUpdateFeedNewFeed() {
final int NUM_ITEMS = 10; final int numItems = 10;
Feed feed = new Feed("url", null, "title"); Feed feed = new Feed("url", null, "title");
feed.setItems(new ArrayList<>()); feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS; i++) { 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.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i,
new Date(), FeedItem.UNPLAYED, feed));
} }
Feed newFeed = DBTasks.updateFeed(context, feed, false); Feed newFeed = DBTasks.updateFeed(context, feed, false);
@ -96,13 +105,14 @@ public class DBTasksTest {
@Test @Test
public void testUpdateFeedUpdatedFeed() { public void testUpdateFeedUpdatedFeed() {
final int NUM_ITEMS_OLD = 10; final int numItemsOld = 10;
final int NUM_ITEMS_NEW = 10; final int numItemsNew = 10;
final Feed feed = new Feed("url", null, "title"); final Feed feed = new Feed("url", null, "title");
feed.setItems(new ArrayList<>()); feed.setItems(new ArrayList<>());
for (int i = 0; i < NUM_ITEMS_OLD; i++) { 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)); feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i,
new Date(i), FeedItem.PLAYED, feed));
} }
PodDBAdapter adapter = PodDBAdapter.getInstance(); PodDBAdapter adapter = PodDBAdapter.getInstance();
adapter.open(); adapter.open();
@ -120,19 +130,20 @@ public class DBTasksTest {
item.setId(0); item.setId(0);
} }
for (int i = NUM_ITEMS_OLD; i < NUM_ITEMS_NEW + NUM_ITEMS_OLD; i++) { 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)); feed.getItems().add(0, new FeedItem(0, "item " + i, "id " + i, "link " + i,
new Date(i), FeedItem.UNPLAYED, feed));
} }
final Feed newFeed = DBTasks.updateFeed(context, feed, false); final Feed newFeed = DBTasks.updateFeed(context, feed, false);
assertNotSame(newFeed, feed); assertNotSame(newFeed, feed);
updatedFeedTest(newFeed, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW); updatedFeedTest(newFeed, feedID, itemIDs, numItemsOld, numItemsNew);
final Feed feedFromDB = DBReader.getFeed(newFeed.getId()); final Feed feedFromDB = DBReader.getFeed(newFeed.getId());
assertNotNull(feedFromDB); assertNotNull(feedFromDB);
assertEquals(newFeed.getId(), feedFromDB.getId()); assertEquals(newFeed.getId(), feedFromDB.getId());
updatedFeedTest(feedFromDB, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW); updatedFeedTest(feedFromDB, feedID, itemIDs, numItemsOld, numItemsNew);
} }
@Test @Test
@ -186,12 +197,14 @@ public class DBTasksTest {
assertEquals(8, feedFromDB.getItems().size()); // 10 - 2 = 8 items assertEquals(8, feedFromDB.getItems().size()); // 10 - 2 = 8 items
} }
private void updatedFeedTest(final Feed newFeed, long feedID, List<Long> itemIDs, final int NUM_ITEMS_OLD, final int NUM_ITEMS_NEW) { @SuppressWarnings("SameParameterValue")
private void updatedFeedTest(final Feed newFeed, long feedID, List<Long> itemIDs,
int numItemsOld, int numItemsNew) {
assertEquals(feedID, newFeed.getId()); assertEquals(feedID, newFeed.getId());
assertEquals(NUM_ITEMS_NEW + NUM_ITEMS_OLD, newFeed.getItems().size()); assertEquals(numItemsNew + numItemsOld, newFeed.getItems().size());
Collections.reverse(newFeed.getItems()); Collections.reverse(newFeed.getItems());
Date lastDate = new Date(0); Date lastDate = new Date(0);
for (int i = 0; i < NUM_ITEMS_OLD; i++) { for (int i = 0; i < numItemsOld; i++) {
FeedItem item = newFeed.getItems().get(i); FeedItem item = newFeed.getItems().get(i);
assertSame(newFeed, item.getFeed()); assertSame(newFeed, item.getFeed());
assertEquals((long) itemIDs.get(i), item.getId()); assertEquals((long) itemIDs.get(i), item.getId());
@ -199,7 +212,7 @@ public class DBTasksTest {
assertTrue(item.getPubDate().getTime() >= lastDate.getTime()); assertTrue(item.getPubDate().getTime() >= lastDate.getTime());
lastDate = item.getPubDate(); lastDate = item.getPubDate();
} }
for (int i = NUM_ITEMS_OLD; i < NUM_ITEMS_NEW + NUM_ITEMS_OLD; i++) { for (int i = numItemsOld; i < numItemsNew + numItemsOld; i++) {
FeedItem item = newFeed.getItems().get(i); FeedItem item = newFeed.getItems().get(i);
assertSame(newFeed, item.getFeed()); assertSame(newFeed, item.getFeed());
assertTrue(item.getId() != 0); assertTrue(item.getId() != 0);