Search in sources :

Example 36 with PodDBAdapter

use of de.danoeh.antennapod.core.storage.PodDBAdapter in project AntennaPod by AntennaPod.

the class DBWriterTest method testRemoveQueueItem.

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);
    }
    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();
        adapter.open();
        adapter.setQueue(feed.getItems());
        adapter.close();
        DBWriter.removeQueueItem(context, item, false).get(TIMEOUT, TimeUnit.SECONDS);
        adapter = PodDBAdapter.getInstance();
        adapter.open();
        Cursor queue = adapter.getQueueIDCursor();
        assertTrue(queue.getCount() == NUM_ITEMS - 1);
        for (int i = 0; i < queue.getCount(); i++) {
            assertTrue(queue.moveToPosition(i));
            final long queueID = queue.getLong(0);
            // removed item is no longer in queue
            assertTrue(queueID != item.getId());
            boolean idFound = false;
            for (FeedItem other : feed.getItems()) {
                // items that were not removed are still in the queue
                idFound = idFound | (other.getId() == queueID);
            }
            assertTrue(idFound);
        }
        queue.close();
        adapter.close();
    }
}
Also used : Context(android.content.Context) PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Cursor(android.database.Cursor) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 37 with PodDBAdapter

use of de.danoeh.antennapod.core.storage.PodDBAdapter in project AntennaPod by AntennaPod.

the class DBWriterTest method testDeleteFeedWithItemImages.

public void testDeleteFeedWithItemImages() throws InterruptedException, ExecutionException, TimeoutException, IOException {
    File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER);
    assertNotNull(destFolder);
    Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<>());
    // create Feed image
    File imgFile = new File(destFolder, "image");
    assertTrue(imgFile.createNewFile());
    FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true);
    image.setOwner(feed);
    feed.setImage(image);
    // create items with images
    for (int i = 0; i < 10; i++) {
        FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed);
        feed.getItems().add(item);
        File itemImageFile = new File(destFolder, "item-image-" + i);
        FeedImage itemImage = new FeedImage(0, "item-image" + i, itemImageFile.getAbsolutePath(), "url", true);
        item.setImage(itemImage);
    }
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(feed);
    adapter.close();
    assertTrue(feed.getId() != 0);
    assertTrue(feed.getImage().getId() != 0);
    for (FeedItem item : feed.getItems()) {
        assertTrue(item.getId() != 0);
        assertTrue(item.getImage().getId() != 0);
    }
    DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
    // check if files still exist
    assertFalse(imgFile.exists());
    adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor c = adapter.getFeedCursor(feed.getId());
    assertTrue(c.getCount() == 0);
    c.close();
    c = adapter.getImageCursor(String.valueOf(image.getId()));
    assertTrue(c.getCount() == 0);
    c.close();
    for (FeedItem item : feed.getItems()) {
        c = adapter.getFeedItemCursor(String.valueOf(item.getId()));
        assertTrue(c.getCount() == 0);
        c.close();
        c = adapter.getImageCursor(String.valueOf(item.getImage().getId()));
        assertEquals(0, c.getCount());
        c.close();
    }
    adapter.close();
}
Also used : PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Cursor(android.database.Cursor) File(java.io.File) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 38 with PodDBAdapter

use of de.danoeh.antennapod.core.storage.PodDBAdapter in project AntennaPod by AntennaPod.

the class DBWriterTest method testMarkFeedRead.

public void testMarkFeedRead() throws InterruptedException, ExecutionException, TimeoutException {
    final int NUM_ITEMS = 10;
    Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<FeedItem>());
    for (int i = 0; i < NUM_ITEMS; i++) {
        FeedItem item = new FeedItem(0, "title " + i, "id " + i, "link " + i, new Date(), FeedItem.UNPLAYED, feed);
        feed.getItems().add(item);
    }
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(feed);
    adapter.close();
    assertTrue(feed.getId() != 0);
    for (FeedItem item : feed.getItems()) {
        assertTrue(item.getId() != 0);
    }
    DBWriter.markFeedRead(feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
    List<FeedItem> loadedItems = DBReader.getFeedItemList(feed);
    for (FeedItem item : loadedItems) {
        assertTrue(item.isPlayed());
    }
}
Also used : PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 39 with PodDBAdapter

use of de.danoeh.antennapod.core.storage.PodDBAdapter in project AntennaPod by AntennaPod.

the class DBWriterTest method testDeleteFeed.

public void testDeleteFeed() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    File destFolder = getInstrumentation().getTargetContext().getExternalFilesDir(TEST_FOLDER);
    assertNotNull(destFolder);
    Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<>());
    // create Feed image
    File imgFile = new File(destFolder, "image");
    assertTrue(imgFile.createNewFile());
    FeedImage image = new FeedImage(0, "image", imgFile.getAbsolutePath(), "url", true);
    image.setOwner(feed);
    feed.setImage(image);
    List<File> itemFiles = new ArrayList<File>();
    // create items with downloaded media files
    for (int i = 0; i < 10; i++) {
        FeedItem item = new FeedItem(0, "Item " + i, "Item" + i, "url", new Date(), FeedItem.PLAYED, feed, true);
        feed.getItems().add(item);
        File enc = new File(destFolder, "file " + i);
        assertTrue(enc.createNewFile());
        itemFiles.add(enc);
        FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", true, null, 0, 0);
        item.setMedia(media);
        item.setChapters(new ArrayList<Chapter>());
        item.getChapters().add(new SimpleChapter(0, "item " + i, item, "example.com"));
    }
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(feed);
    adapter.close();
    assertTrue(feed.getId() != 0);
    assertTrue(feed.getImage().getId() != 0);
    for (FeedItem item : feed.getItems()) {
        assertTrue(item.getId() != 0);
        assertTrue(item.getMedia().getId() != 0);
        assertTrue(item.getChapters().get(0).getId() != 0);
    }
    DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
    // check if files still exist
    assertFalse(imgFile.exists());
    for (File f : itemFiles) {
        assertFalse(f.exists());
    }
    adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor c = adapter.getFeedCursor(feed.getId());
    assertEquals(0, c.getCount());
    c.close();
    c = adapter.getImageCursor(String.valueOf(image.getId()));
    assertEquals(0, c.getCount());
    c.close();
    for (FeedItem item : feed.getItems()) {
        c = adapter.getFeedItemCursor(String.valueOf(item.getId()));
        assertEquals(0, c.getCount());
        c.close();
        c = adapter.getSingleFeedMediaCursor(item.getMedia().getId());
        assertEquals(0, c.getCount());
        c.close();
        c = adapter.getSimpleChaptersOfFeedItemCursor(item);
        assertEquals(0, c.getCount());
        c.close();
    }
    adapter.close();
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) ArrayList(java.util.ArrayList) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) Chapter(de.danoeh.antennapod.core.feed.Chapter) Cursor(android.database.Cursor) Date(java.util.Date) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) File(java.io.File) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 40 with PodDBAdapter

use of de.danoeh.antennapod.core.storage.PodDBAdapter in project AntennaPod by AntennaPod.

the class DBWriterTest method testAddQueueItemSingleItem.

public void testAddQueueItemSingleItem() throws InterruptedException, ExecutionException, TimeoutException {
    final Context context = getInstrumentation().getTargetContext();
    Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<>());
    FeedItem item = new FeedItem(0, "title", "id", "link", new Date(), FeedItem.PLAYED, feed);
    feed.getItems().add(item);
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(feed);
    adapter.close();
    assertTrue(item.getId() != 0);
    DBWriter.addQueueItem(context, item).get(TIMEOUT, TimeUnit.SECONDS);
    adapter = PodDBAdapter.getInstance();
    adapter.open();
    Cursor cursor = adapter.getQueueIDCursor();
    assertTrue(cursor.moveToFirst());
    assertTrue(cursor.getLong(0) == item.getId());
    cursor.close();
    adapter.close();
}
Also used : Context(android.content.Context) PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) Cursor(android.database.Cursor) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Aggregations

PodDBAdapter (de.danoeh.antennapod.core.storage.PodDBAdapter)45 Feed (de.danoeh.antennapod.core.feed.Feed)32 FeedItem (de.danoeh.antennapod.core.feed.FeedItem)28 Date (java.util.Date)25 FeedMedia (de.danoeh.antennapod.core.feed.FeedMedia)16 ArrayList (java.util.ArrayList)14 Cursor (android.database.Cursor)13 File (java.io.File)11 Context (android.content.Context)10 FeedImage (de.danoeh.antennapod.core.feed.FeedImage)7 SharedPreferences (android.content.SharedPreferences)4 Solo (com.robotium.solo.Solo)3 Random (java.util.Random)3 FlakyTest (android.test.FlakyTest)2 Chapter (de.danoeh.antennapod.core.feed.Chapter)2 SimpleChapter (de.danoeh.antennapod.core.feed.SimpleChapter)2 FeedPreferences (de.danoeh.antennapod.core.feed.FeedPreferences)1 DBReader (de.danoeh.antennapod.core.storage.DBReader)1 FeedItemPubdateComparator (de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator)1 FlattrStatus (de.danoeh.antennapod.core.util.flattr.FlattrStatus)1