bulk remove from queue - backend - implement the new API
This commit is contained in:
parent
70287a740e
commit
364c9a308d
|
@ -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<FeedItem> 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<PodDBAdapter> action) {
|
||||
PodDBAdapter adapter = PodDBAdapter.getInstance();
|
||||
try {
|
||||
adapter.open();
|
||||
action.accept(adapter);
|
||||
} finally {
|
||||
adapter.close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void assertQueueByItemIds(
|
||||
String message,
|
||||
long... itemIdsExpected
|
||||
) {
|
||||
List<FeedItem> queue = DBReader.getQueue();
|
||||
List<Long> itemIdsActualList = toItemIds(queue);
|
||||
List<Long> itemIdsExpectedList = new ArrayList<Long>(itemIdsExpected.length);
|
||||
for (long id : itemIdsExpected) {
|
||||
itemIdsExpectedList.add(id);
|
||||
}
|
||||
|
||||
assertEquals(message, itemIdsExpectedList, itemIdsActualList);
|
||||
}
|
||||
|
||||
private static List<Long> toItemIds(List<FeedItem> items) {
|
||||
List<Long> itemIds = new ArrayList<Long>(items.size());
|
||||
for(FeedItem item : items) {
|
||||
itemIds.add(item.getId());
|
||||
}
|
||||
return itemIds;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue