Search in sources :

Example 1 with PodDBAdapter

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

the class UITestUtils method addLocalFeedData.

/**
     * Adds feeds, images and episodes to the local database. This method will also call addHostedFeedData if it has not
     * been called yet.
     *
     * Adds one item of each feed to the queue and to the playback history.
     *
     * This method should NOT be called if the testing class wants to download the hosted feed data.
     *
     * @param downloadEpisodes true if episodes should also be marked as downloaded.
     */
public void addLocalFeedData(boolean downloadEpisodes) throws Exception {
    if (localFeedDataAdded) {
        Log.w(TAG, "addLocalFeedData was called twice on the same instance");
        // might be a flaky test, this is actually not that severe
        return;
    }
    if (!feedDataHosted) {
        addHostedFeedData();
    }
    List<FeedItem> queue = new ArrayList<>();
    for (Feed feed : hostedFeeds) {
        feed.setDownloaded(true);
        if (feed.getImage() != null) {
            FeedImage image = feed.getImage();
            int fileId = Integer.parseInt(StringUtils.substringAfter(image.getDownload_url(), "files/"));
            image.setFile_url(server.accessFile(fileId).getAbsolutePath());
            image.setDownloaded(true);
        }
        if (downloadEpisodes) {
            for (FeedItem item : feed.getItems()) {
                if (item.hasMedia()) {
                    FeedMedia media = item.getMedia();
                    int fileId = Integer.parseInt(StringUtils.substringAfter(media.getDownload_url(), "files/"));
                    media.setFile_url(server.accessFile(fileId).getAbsolutePath());
                    media.setDownloaded(true);
                }
            }
        }
        queue.add(feed.getItems().get(0));
        feed.getItems().get(1).getMedia().setPlaybackCompletionDate(new Date());
    }
    localFeedDataAdded = true;
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.setCompleteFeed(hostedFeeds.toArray(new Feed[hostedFeeds.size()]));
    adapter.setQueue(queue);
    adapter.close();
    EventDistributor.getInstance().sendFeedUpdateBroadcast();
    EventBus.getDefault().post(QueueEvent.setQueue(queue));
}
Also used : PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedImage(de.danoeh.antennapod.core.feed.FeedImage) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) ArrayList(java.util.ArrayList) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 2 with PodDBAdapter

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

the class DBTasksTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    context = getInstrumentation().getTargetContext();
    // create new database
    PodDBAdapter.init(context);
    PodDBAdapter.deleteDatabase();
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    adapter.close();
    UserPreferences.init(context);
}
Also used : PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter)

Example 3 with PodDBAdapter

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

the class DBTasksTest method testUpdateFeedUpdatedFeed.

public void testUpdateFeedUpdatedFeed() {
    final int NUM_ITEMS_OLD = 10;
    final int NUM_ITEMS_NEW = 10;
    final Feed feed = new Feed("url", null, "title");
    feed.setItems(new ArrayList<>());
    for (int i = 0; i < NUM_ITEMS_OLD; i++) {
        feed.getItems().add(new FeedItem(0, "item " + i, "id " + i, "link " + i, new Date(i), FeedItem.PLAYED, feed));
    }
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    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), FeedItem.UNPLAYED, feed));
    }
    final Feed newFeed = DBTasks.updateFeed(context, feed)[0];
    assertTrue(feed != newFeed);
    updatedFeedTest(newFeed, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW);
    final Feed feedFromDB = DBReader.getFeed(newFeed.getId());
    assertNotNull(feedFromDB);
    assertTrue(feedFromDB.getId() == newFeed.getId());
    updatedFeedTest(feedFromDB, feedID, itemIDs, NUM_ITEMS_OLD, NUM_ITEMS_NEW);
}
Also used : PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) ArrayList(java.util.ArrayList) Date(java.util.Date) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 4 with PodDBAdapter

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

the class DBTestUtils method saveFeedlist.

/**
     * Use this method when tests involve chapters.
     */
public static List<Feed> saveFeedlist(int numFeeds, int numItems, boolean withMedia, boolean withChapters, int numChapters) {
    if (numFeeds <= 0) {
        throw new IllegalArgumentException("numFeeds<=0");
    }
    if (numItems < 0) {
        throw new IllegalArgumentException("numItems<0");
    }
    List<Feed> feeds = new ArrayList<>();
    PodDBAdapter adapter = PodDBAdapter.getInstance();
    adapter.open();
    for (int i = 0; i < numFeeds; i++) {
        Feed f = new Feed(0, null, "feed " + i, null, "link" + i, "descr", null, null, null, null, "id" + i, null, null, "url" + i, false, new FlattrStatus(), false, null, null, false);
        f.setItems(new ArrayList<>());
        for (int j = 0; j < numItems; j++) {
            FeedItem item = new FeedItem(0, "item " + j, "id" + j, "link" + j, new Date(), FeedItem.PLAYED, f, withChapters);
            if (withMedia) {
                FeedMedia media = new FeedMedia(item, "url" + j, 1, "audio/mp3");
                item.setMedia(media);
            }
            if (withChapters) {
                List<Chapter> chapters = new ArrayList<>();
                item.setChapters(chapters);
                for (int k = 0; k < numChapters; k++) {
                    chapters.add(new SimpleChapter(k, "item " + j + " chapter " + k, item, "http://example.com"));
                }
            }
            f.getItems().add(item);
        }
        Collections.sort(f.getItems(), new FeedItemPubdateComparator());
        adapter.setCompleteFeed(f);
        Assert.assertTrue(f.getId() != 0);
        for (FeedItem item : f.getItems()) {
            Assert.assertTrue(item.getId() != 0);
        }
        feeds.add(f);
    }
    adapter.close();
    return feeds;
}
Also used : ArrayList(java.util.ArrayList) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) Chapter(de.danoeh.antennapod.core.feed.Chapter) Date(java.util.Date) FlattrStatus(de.danoeh.antennapod.core.util.flattr.FlattrStatus) SimpleChapter(de.danoeh.antennapod.core.feed.SimpleChapter) PodDBAdapter(de.danoeh.antennapod.core.storage.PodDBAdapter) FeedItemPubdateComparator(de.danoeh.antennapod.core.util.comparator.FeedItemPubdateComparator) FeedItem(de.danoeh.antennapod.core.feed.FeedItem) FeedMedia(de.danoeh.antennapod.core.feed.FeedMedia) Feed(de.danoeh.antennapod.core.feed.Feed)

Example 5 with PodDBAdapter

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

the class DBWriterTest method testDeleteFeedWithQueueItems.

public void testDeleteFeedWithQueueItems() throws 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");
    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);
        feed.getItems().add(item);
        File enc = new File(destFolder, "file " + i);
        itemFiles.add(enc);
        FeedMedia media = new FeedMedia(0, item, 1, 1, 1, "mime_type", enc.getAbsolutePath(), "download_url", false, null, 0, 0);
        item.setMedia(media);
    }
    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);
    }
    List<FeedItem> queue = new ArrayList<FeedItem>();
    queue.addAll(feed.getItems());
    adapter.open();
    adapter.setQueue(queue);
    Cursor queueCursor = adapter.getQueueIDCursor();
    assertTrue(queueCursor.getCount() == queue.size());
    queueCursor.close();
    adapter.close();
    DBWriter.deleteFeed(getInstrumentation().getTargetContext(), feed.getId()).get(TIMEOUT, TimeUnit.SECONDS);
    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.getSingleFeedMediaCursor(item.getMedia().getId());
        assertTrue(c.getCount() == 0);
        c.close();
    }
    c = adapter.getQueueCursor();
    assertTrue(c.getCount() == 0);
    c.close();
    adapter.close();
}
Also used : FeedImage(de.danoeh.antennapod.core.feed.FeedImage) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Date(java.util.Date) 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)

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