From 364c9a308d1c18f4bb2d11f38a32781957fb5e9f Mon Sep 17 00:00:00 2001 From: orionlee Date: Tue, 2 Apr 2019 15:59:27 -0700 Subject: [PATCH] bulk remove from queue - backend - implement the new API --- .../test/antennapod/storage/DBWriterTest.java | 108 +++++++++++++++--- .../antennapod/core/storage/DBWriter.java | 12 +- 2 files changed, 96 insertions(+), 24 deletions(-) diff --git a/app/src/androidTest/java/de/test/antennapod/storage/DBWriterTest.java b/app/src/androidTest/java/de/test/antennapod/storage/DBWriterTest.java index b80482c53..9676d8956 100644 --- a/app/src/androidTest/java/de/test/antennapod/storage/DBWriterTest.java +++ b/app/src/androidTest/java/de/test/antennapod/storage/DBWriterTest.java @@ -7,6 +7,8 @@ import android.preference.PreferenceManager; import android.test.InstrumentationTestCase; import android.util.Log; +import org.awaitility.Awaitility; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -24,8 +26,7 @@ import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.PodDBAdapter; - -import org.awaitility.Awaitility; +import de.danoeh.antennapod.core.util.Consumer; /** * Test class for DBWriter @@ -574,24 +575,11 @@ public class DBWriterTest extends InstrumentationTestCase { public void testRemoveQueueItem() throws InterruptedException, ExecutionException, TimeoutException { final int NUM_ITEMS = 10; final Context context = getInstrumentation().getTargetContext(); - Feed feed = new Feed("url", null, "title"); - feed.setItems(new ArrayList<>()); - for (int i = 0; i < NUM_ITEMS; i++) { - FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.PLAYED, feed); - feed.getItems().add(item); - } + Feed feed = createTestFeed(NUM_ITEMS); - PodDBAdapter adapter = PodDBAdapter.getInstance(); - adapter.open(); - adapter.setCompleteFeed(feed); - adapter.close(); - - for (FeedItem item : feed.getItems()) { - assertTrue(item.getId() != 0); - } for (int removeIndex = 0; removeIndex < NUM_ITEMS; removeIndex++) { final FeedItem item = feed.getItems().get(removeIndex); - adapter = PodDBAdapter.getInstance(); + PodDBAdapter adapter = PodDBAdapter.getInstance(); adapter.open(); adapter.setQueue(feed.getItems()); adapter.close(); @@ -616,6 +604,43 @@ public class DBWriterTest extends InstrumentationTestCase { } } + public void testRemoveQueueItemMultipleItems() throws InterruptedException, ExecutionException, TimeoutException { + // Setup test data + // + final int NUM_ITEMS = 5; + final int NUM_IN_QUEUE = NUM_ITEMS - 1; // the last one not in queue for boundary condition + final Context context = getInstrumentation().getTargetContext(); + Feed feed = createTestFeed(NUM_ITEMS); + + List itemsToAdd = feed.getItems().subList(0, NUM_IN_QUEUE); + withPodDB(adapter -> adapter.setQueue(itemsToAdd) ); + + // Actual tests + // + + // Use array rather than List to make codes more succinct + Long[] itemIds = toItemIds(feed.getItems()).toArray(new Long[0]); + + DBWriter.removeQueueItem(context, false, + itemIds[1], itemIds[3]).get(TIMEOUT, TimeUnit.SECONDS); + assertQueueByItemIds("Average case - 2 items removed successfully", + itemIds[0], itemIds[2]); + + DBWriter.removeQueueItem(context, false).get(TIMEOUT, TimeUnit.SECONDS); + assertQueueByItemIds("Boundary case - no items supplied. queue should see no change", + itemIds[0], itemIds[2]); + + DBWriter.removeQueueItem(context, false, + itemIds[0], itemIds[4], -1L).get(TIMEOUT, TimeUnit.SECONDS); + assertQueueByItemIds("Boundary case - items not in queue ignored", + itemIds[2]); + + DBWriter.removeQueueItem(context, false, + itemIds[2], -1L).get(TIMEOUT, TimeUnit.SECONDS); + assertQueueByItemIds("Boundary case - invalid itemIds ignored"); // the queue is empty + + } + public void testMoveQueueItem() throws InterruptedException, ExecutionException, TimeoutException { final int NUM_ITEMS = 10; Feed feed = new Feed("url", null, "title"); @@ -713,4 +738,53 @@ public class DBWriterTest extends InstrumentationTestCase { assertTrue(item.isPlayed()); } } + + private static Feed createTestFeed(int numItems) { + Feed feed = new Feed("url", null, "title"); + feed.setItems(new ArrayList<>()); + for (int i = 0; i < numItems; i++) { + FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.PLAYED, feed); + feed.getItems().add(item); + } + + withPodDB(adapter -> adapter.setCompleteFeed(feed)); + + for (FeedItem item : feed.getItems()) { + assertTrue(item.getId() != 0); + } + return feed; + } + + private static void withPodDB(Consumer action) { + PodDBAdapter adapter = PodDBAdapter.getInstance(); + try { + adapter.open(); + action.accept(adapter); + } finally { + adapter.close(); + } + } + + private static void assertQueueByItemIds( + String message, + long... itemIdsExpected + ) { + List queue = DBReader.getQueue(); + List itemIdsActualList = toItemIds(queue); + List itemIdsExpectedList = new ArrayList(itemIdsExpected.length); + for (long id : itemIdsExpected) { + itemIdsExpectedList.add(id); + } + + assertEquals(message, itemIdsExpectedList, itemIdsActualList); + } + + private static List toItemIds(List items) { + List itemIds = new ArrayList(items.size()); + for(FeedItem item : items) { + itemIds.add(item.getId()); + } + return itemIds; + } + } diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java index b2f28008a..909b46414 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DBWriter.java @@ -6,7 +6,6 @@ import android.content.Intent; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; -import android.widget.Toast; import org.shredzone.flattr4j.model.Flattr; @@ -434,6 +433,11 @@ public class DBWriter { return dbExec.submit(() -> removeQueueItemSynchronous(context, performAutoDownload, item.getId())); } + public static Future removeQueueItem(final Context context, final boolean performAutoDownload, + final long... itemIds) { + return dbExec.submit(() -> removeQueueItemSynchronous(context, performAutoDownload, itemIds)); + } + private static void removeQueueItemSynchronous(final Context context, final boolean performAutoDownload, final long... itemIds) { @@ -484,12 +488,6 @@ public class DBWriter { } } - public static Future removeQueueItem(final Context context, final boolean performAutoDownload, - final long... itemIds) { - Toast.makeText(context, "To implement: remove from queue. ids=" + itemIds, Toast.LENGTH_SHORT).show(); - return dbExec.submit(() -> { }); - } - public static Future addFavoriteItem(final FeedItem item) { return dbExec.submit(() -> { final PodDBAdapter adapter = PodDBAdapter.getInstance().open();