summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/util/playback/TimelineTest.java
blob: ed37b7daad7c14618a71cd6241e9c4cb982e54ab (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package de.test.antennapod.util.playback;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.Date;
import java.util.List;

import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.Timeline;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Test class for timeline.
 */
@SmallTest
public class TimelineTest {

    private Context context;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getInstrumentation().getTargetContext();
    }

    private Playable newTestPlayable(List<Chapter> chapters, String shownotes, int duration) {
        FeedItem item = new FeedItem(0, "Item", "item-id", "http://example.com/item", new Date(), FeedItem.PLAYED, null);
        item.setChapters(chapters);
        item.setContentEncoded(shownotes);
        FeedMedia media = new FeedMedia(item, "http://example.com/episode", 100, "audio/mp3");
        media.setDuration(duration);
        item.setMedia(media);
        return media;
    }

    @Test
    public void testProcessShownotesAddTimecodeHHMMSSNoChapters() {
        final String timeStr = "10:11:12";
        final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStr + " here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeHHMMSSMoreThen24HoursNoChapters() {
        final String timeStr = "25:00:00";
        final long time = 25 * 60 * 60 * 1000;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStr + " here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeHHMMNoChapters() {
        final String timeStr = "10:11";
        final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStr + " here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeMMSSNoChapters() {
        final String timeStr = "10:11";
        final long time = 10 * 60 * 1000 + 11 * 1000;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStr + " here.</p>", 11 * 60 * 1000);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeHMMSSNoChapters() {
        final String timeStr = "2:11:12";
        final long time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStr + " here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeMSSNoChapters() {
        final String timeStr = "1:12";
        final long time = 60 * 1000 + 12 * 1000;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStr + " here.</p>", 2 * 60 * 1000);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddNoTimecodeDuration() {
        final String timeStr = "2:11:12";
        final int time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
        String originalText = "<p> Some test text with a timecode " + timeStr + " here.</p>";
        Playable p = newTestPlayable(null, originalText, time);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        Document d = Jsoup.parse(res);
        assertEquals("Should not parse time codes that equal duration", 0, d.body().getElementsByTag("a").size());
    }

    @Test
    public void testProcessShownotesAddTimecodeMultipleFormatsNoChapters() {
        final String[] timeStrings = new String[]{ "10:12", "1:10:12" };

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 2 * 60 * 60 * 1000);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000,
                60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000 }, timeStrings);
    }

    @Test
    public void testProcessShownotesAddTimecodeMultipleShortFormatNoChapters() {

        // One of these timecodes fits as HH:MM and one does not so both should be parsed as MM:SS.
        final String[] timeStrings = new String[]{ "10:12", "2:12" };

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
                + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 3 * 60 * 60 * 1000);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000, 2 * 60 * 1000 + 12 * 1000 }, timeStrings);
    }

    @Test
    public void testProcessShownotesAddTimecodeParentheses() {
        final String timeStr = "10:11";
        final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode ("
                + timeStr + ") here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeBrackets() {
        final String timeStr = "10:11";
        final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode ["
                + timeStr + "] here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAddTimecodeAngleBrackets() {
        final String timeStr = "10:11";
        final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;

        Playable p = newTestPlayable(null, "<p> Some test text with a timecode <"
                + timeStr + "> here.</p>", Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[]{time}, new String[]{timeStr});
    }

    @Test
    public void testProcessShownotesAndInvalidTimecode()  {
        final String[] timeStrs = new String[] {"2:1", "0:0", "000", "00", "00:000"};

        StringBuilder shownotes = new StringBuilder("<p> Some test text with timecodes ");
        for (String timeStr : timeStrs) {
            shownotes.append(timeStr).append(" ");
        }
        shownotes.append("here.</p>");

        Playable p = newTestPlayable(null, shownotes.toString(), Integer.MAX_VALUE);
        Timeline t = new Timeline(context, p);
        String res = t.processShownotes();
        checkLinkCorrect(res, new long[0], new String[0]);
    }

    private void checkLinkCorrect(String res, long[] timecodes, String[] timecodeStr) {
        assertNotNull(res);
        Document d = Jsoup.parse(res);
        Elements links = d.body().getElementsByTag("a");
        int countedLinks = 0;
        for (Element link : links) {
            String href = link.attributes().get("href");
            String text = link.text();
            if (href.startsWith("antennapod://")) {
                assertTrue(href.endsWith(String.valueOf(timecodes[countedLinks])));
                assertEquals(timecodeStr[countedLinks], text);
                countedLinks++;
                assertTrue("Contains too many links: " + countedLinks + " > "
                        + timecodes.length, countedLinks <= timecodes.length);
            }
        }
        assertEquals(timecodes.length, countedLinks);
    }

    @Test
    public void testIsTimecodeLink() {
        assertFalse(Timeline.isTimecodeLink(null));
        assertFalse(Timeline.isTimecodeLink("http://antennapod/timecode/123123"));
        assertFalse(Timeline.isTimecodeLink("antennapod://timecode/"));
        assertFalse(Timeline.isTimecodeLink("antennapod://123123"));
        assertFalse(Timeline.isTimecodeLink("antennapod://timecode/123123a"));
        assertTrue(Timeline.isTimecodeLink("antennapod://timecode/123"));
        assertTrue(Timeline.isTimecodeLink("antennapod://timecode/1"));
    }

    @Test
    public void testGetTimecodeLinkTime() {
        assertEquals(-1, Timeline.getTimecodeLinkTime(null));
        assertEquals(-1, Timeline.getTimecodeLinkTime("http://timecode/123"));
        assertEquals(123, Timeline.getTimecodeLinkTime("antennapod://timecode/123"));

    }
}