summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/service/download/DownloadServiceTest.java
blob: d4f75cfe8f201adfcf2720134ab99b540e5139aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package de.test.antennapod.service.download;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.awaitility.Awaitility;
import org.awaitility.core.ConditionTimeoutException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.service.download.DownloadService;
import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.service.download.Downloader;
import de.danoeh.antennapod.core.service.download.StubDownloader;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBWriter;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.Consumer;

import static de.test.antennapod.util.feed.FeedItemEventListener.withFeedItemEventListener;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @see HttpDownloaderTest for the test of actual download (and saving the file)
 */
@RunWith(AndroidJUnit4.class)
public class DownloadServiceTest {

    private CountDownLatch latch = null;
    private Feed testFeed = null;
    private FeedMedia testMedia11 = null;

    private DownloadService.DownloaderFactory origFactory = null;

    @Before
    public void setUp() throws Exception {
        origFactory = DownloadService.getDownloaderFactory();
        testFeed = setUpTestFeeds();
        testMedia11 = testFeed.getItemAtIndex(0).getMedia();
    }

    private Feed setUpTestFeeds() throws Exception {
        Feed feed = new Feed("url", null, "Test Feed title 1");
        List<FeedItem> items = new ArrayList<>();
        feed.setItems(items);
        FeedItem item1 = new FeedItem(0, "Item 1-1", "Item 1-1", "url", new Date(), FeedItem.NEW, feed);
        items.add(item1);
        FeedMedia media1 = new FeedMedia(0, item1, 123, 1, 1, "audio/mp3", null, "http://example.com/episode.mp3", false, null, 0, 0);
        item1.setMedia(media1);

        DBWriter.setFeedItem(item1).get();
        return feed;
    }


    @After
    public void tearDown() throws Exception {
        DownloadService.setDownloaderFactory(origFactory);
    }

    @Test
    public void testEventsGeneratedCaseMediaDownloadSuccess() throws Exception {
        // create a stub download that returns successful
        //
        // OPEN: Ideally, I'd like the download time long enough so that multiple in-progress DownloadEvents
        // are generated (to simulate typical download), but it'll make download time quite long (1-2 seconds)
        // to do so
        DownloadService.setDownloaderFactory(new StubDownloaderFactory(50, downloadStatus -> {
           downloadStatus.setSuccessful();
        }));

        withFeedItemEventListener(feedItemEventListener -> {
            try {
                assertEquals(0, feedItemEventListener.getEvents().size());
                assertFalse("The media in test should not yet been downloaded",
                        DBReader.getFeedMedia(testMedia11.getId()).isDownloaded());

                DownloadRequester.getInstance().downloadMedia(InstrumentationRegistry.getTargetContext(),
                        testMedia11);
                Awaitility.await()
                        .atMost(1000, TimeUnit.MILLISECONDS)
                        .until(() -> feedItemEventListener.getEvents().size() > 0);
                assertTrue("After media download has completed, FeedMedia object in db should indicate so.",
                        DBReader.getFeedMedia(testMedia11.getId()).isDownloaded());
            } catch (ConditionTimeoutException cte) {
                fail("The expected FeedItemEvent (for media download complete) has not been posted. "
                        + cte.getMessage());
            }
        });
    }

    private static class StubDownloaderFactory implements DownloadService.DownloaderFactory {
        private final long downloadTime;

        @NonNull
        private final Consumer<DownloadStatus> onDownloadComplete;

        StubDownloaderFactory(long downloadTime, @NonNull Consumer<DownloadStatus> onDownloadComplete) {
            this.downloadTime = downloadTime;
            this.onDownloadComplete = onDownloadComplete;
        }

        @Nullable
        @Override
        public Downloader create(@NonNull DownloadRequest request) {
            return new StubDownloader(request, downloadTime, onDownloadComplete);
        }
    }

}