summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/de/danoeh/antennapod/storage/DBTasks.java4
-rw-r--r--src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java85
2 files changed, 88 insertions, 1 deletions
diff --git a/src/de/danoeh/antennapod/storage/DBTasks.java b/src/de/danoeh/antennapod/storage/DBTasks.java
index 741699bdf..ba2e743a8 100644
--- a/src/de/danoeh/antennapod/storage/DBTasks.java
+++ b/src/de/danoeh/antennapod/storage/DBTasks.java
@@ -28,6 +28,7 @@ import de.danoeh.antennapod.service.download.DownloadStatus;
import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.NetworkUtils;
import de.danoeh.antennapod.util.QueueAccess;
+import de.danoeh.antennapod.util.comparator.FeedItemPubdateComparator;
import de.danoeh.antennapod.util.exception.MediaFileNotFoundException;
/**
@@ -568,6 +569,7 @@ public final class DBTasks {
Log.d(TAG, "Feed with title " + newFeed.getTitle()
+ " already exists. Syncing new with existing one.");
+ Collections.sort(newFeed.getItems(), new FeedItemPubdateComparator());
savedFeed.setItems(DBReader.getFeedItemList(context, savedFeed));
if (savedFeed.compareWithOther(newFeed)) {
if (AppConfig.DEBUG)
@@ -585,7 +587,7 @@ public final class DBTasks {
final int i = idx;
item.setFeed(savedFeed);
savedFeed.getItems().add(i, item);
- DBWriter.markItemRead(context, item.getId(), false);
+ item.setRead(false);
} else {
oldItem.updateFromOther(item);
}
diff --git a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java
index 6dd9ef92b..7631a5787 100644
--- a/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java
+++ b/src/instrumentationTest/de/test/antennapod/storage/DBTasksTest.java
@@ -8,12 +8,14 @@ import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.preferences.UserPreferences;
+import de.danoeh.antennapod.storage.DBReader;
import de.danoeh.antennapod.storage.DBTasks;
import de.danoeh.antennapod.storage.PodDBAdapter;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
@@ -164,4 +166,87 @@ public class DBTasksTest extends InstrumentationTestCase {
assertTrue(file.exists());
}
}
+
+ public void testUpdateFeedNewFeed() {
+ final Context context = getInstrumentation().getTargetContext();
+ final int NUM_ITEMS = 10;
+
+ Feed feed = new Feed("url", new Date(), "title");
+ feed.setItems(new ArrayList<FeedItem>());
+ for (int i = 0; i < NUM_ITEMS; i++) {
+ feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(), false, feed));
+ }
+ Feed newFeed = DBTasks.updateFeed(context, feed);
+
+ assertTrue(newFeed == feed);
+ assertTrue(feed.getId() != 0);
+ for (FeedItem item : feed.getItems()) {
+ assertFalse(item.isRead());
+ assertTrue(item.getId() != 0);
+ }
+ }
+
+ public void testUpdateFeedUpdatedFeed() {
+ final Context context = getInstrumentation().getTargetContext();
+ final int NUM_ITEMS_OLD = 10;
+ final int NUM_ITEMS_NEW = 10;
+
+ final Feed feed = new Feed("url", new Date(), "title");
+ feed.setItems(new ArrayList<FeedItem>());
+ for (int i = 0; i < NUM_ITEMS_OLD; i++) {
+ feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(i), true, feed));
+ }
+ PodDBAdapter adapter = new PodDBAdapter(context);
+ 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<Long>();
+ for (FeedItem item : feed.getItems()) {
+ assertTrue(item.getId() != 0);
+ itemIDs.add(item.getId());
+ item.setId(0);
+ }
+
+ for (int i = NUM_ITEMS_OLD; i < NUM_ITEMS_NEW + NUM_ITEMS_OLD; i++) {
+ feed.getItems().add(0, new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(i), true, feed));
+ }
+
+ final Feed newFeed = DBTasks.updateFeed(context, feed);
+ assertTrue(feed != newFeed);
+
+ updatedFeedTest(newFeed, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW);
+
+ final Feed feedFromDB = DBReader.getFeed(context, newFeed.getId());
+ assertNotNull(feedFromDB);
+ assertTrue(feedFromDB.getId() == newFeed.getId());
+ updatedFeedTest(feedFromDB, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW);
+ }
+
+ private void updatedFeedTest(final Feed newFeed, long feedID, List<Long> itemIDs, final int NUM_ITEMS_OLD, final int NUM_ITEMS_NEW) {
+ assertTrue(newFeed.getId() == feedID);
+ assertTrue(newFeed.getItems().size() == NUM_ITEMS_NEW + NUM_ITEMS_OLD);
+ Collections.reverse(newFeed.getItems());
+ Date lastDate = new Date(0);
+ for (int i = 0; i < NUM_ITEMS_OLD; i++) {
+ FeedItem item = newFeed.getItems().get(i);
+ assertTrue(item.getFeed() == newFeed);
+ assertTrue(item.getId() == itemIDs.get(i));
+ assertTrue(item.isRead());
+ assertTrue(item.getPubDate().getTime() >= lastDate.getTime());
+ lastDate = item.getPubDate();
+ }
+ for (int i = NUM_ITEMS_OLD; i < NUM_ITEMS_NEW + NUM_ITEMS_OLD; i++) {
+ FeedItem item = newFeed.getItems().get(i);
+ assertTrue(item.getFeed() == newFeed);
+ assertTrue(item.getId() != 0);
+ assertFalse(item.isRead());
+ assertTrue(item.getPubDate().getTime() >= lastDate.getTime());
+ lastDate = item.getPubDate();
+ }
+ }
}