diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2024-03-31 18:40:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-31 18:40:15 +0200 |
commit | edb440a5a9a05e24c344a71b272b9238217e9c55 (patch) | |
tree | 13623ca7d0dac052ac35d693aac940d0727c87f9 /app/src/test/java/de | |
parent | 4e47691e70e85736c7eeb30ce02c73176e565a86 (diff) | |
download | AntennaPod-edb440a5a9a05e24c344a71b272b9238217e9c55.zip |
Restructure related UI classes together (#7044)
Diffstat (limited to 'app/src/test/java/de')
-rw-r--r-- | app/src/test/java/de/danoeh/antennapod/ui/cleaner/ShownotesCleanerTest.java | 236 | ||||
-rw-r--r-- | app/src/test/java/de/danoeh/antennapod/ui/screen/onlinefeedview/FeedDiscovererTest.java | 128 |
2 files changed, 364 insertions, 0 deletions
diff --git a/app/src/test/java/de/danoeh/antennapod/ui/cleaner/ShownotesCleanerTest.java b/app/src/test/java/de/danoeh/antennapod/ui/cleaner/ShownotesCleanerTest.java new file mode 100644 index 000000000..cf7d9db34 --- /dev/null +++ b/app/src/test/java/de/danoeh/antennapod/ui/cleaner/ShownotesCleanerTest.java @@ -0,0 +1,236 @@ +package de.danoeh.antennapod.ui.cleaner; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +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 {@link ShownotesCleaner}. + */ +@RunWith(RobolectricTestRunner.class) +public class ShownotesCleanerTest { + + private Context context; + + @Before + public void setUp() { + context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + } + + @Test + public void testProcessShownotesAddTimecodeHhmmssNoChapters() { + final String timeStr = "10:11:12"; + final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000; + + String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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; + + String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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; + + String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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; + + String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, 11 * 60 * 1000); + 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; + + String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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; + + String shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, 2 * 60 * 1000); + 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 shownotes = "<p> Some test text with a timecode " + timeStr + " here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, time); + 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" }; + + String shownotes = "<p> Some test text with a timecode " + timeStrings[0] + + " here. Hey look another one " + timeStrings[1] + " here!</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, 2 * 60 * 60 * 1000); + 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" }; + + String shownotes = "<p> Some test text with a timecode " + timeStrings[0] + + " here. Hey look another one " + timeStrings[1] + " here!</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, 3 * 60 * 60 * 1000); + 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; + + String shownotes = "<p> Some test text with a timecode (" + timeStr + ") here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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; + + String shownotes = "<p> Some test text with a timecode [" + timeStr + "] here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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; + + String shownotes = "<p> Some test text with a timecode <" + timeStr + "> here.</p>"; + ShownotesCleaner t = new ShownotesCleaner(context, shownotes, Integer.MAX_VALUE); + 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>"); + + ShownotesCleaner t = new ShownotesCleaner(context, shownotes.toString(), Integer.MAX_VALUE); + 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(ShownotesCleaner.isTimecodeLink(null)); + assertFalse(ShownotesCleaner.isTimecodeLink("http://antennapod/timecode/123123")); + assertFalse(ShownotesCleaner.isTimecodeLink("antennapod://timecode/")); + assertFalse(ShownotesCleaner.isTimecodeLink("antennapod://123123")); + assertFalse(ShownotesCleaner.isTimecodeLink("antennapod://timecode/123123a")); + assertTrue(ShownotesCleaner.isTimecodeLink("antennapod://timecode/123")); + assertTrue(ShownotesCleaner.isTimecodeLink("antennapod://timecode/1")); + } + + @Test + public void testGetTimecodeLinkTime() { + assertEquals(-1, ShownotesCleaner.getTimecodeLinkTime(null)); + assertEquals(-1, ShownotesCleaner.getTimecodeLinkTime("http://timecode/123")); + assertEquals(123, ShownotesCleaner.getTimecodeLinkTime("antennapod://timecode/123")); + } + + @Test + public void testCleanupColors() { + final String input = "/* /* */ .foo { text-decoration: underline;color:#f00;font-weight:bold;}" + + "#bar { text-decoration: underline;color:#f00;font-weight:bold; }" + + "div {text-decoration: underline; color /* */ : /* */ #f00 /* */; font-weight:bold; }" + + "#foobar { /* color: */ text-decoration: underline; /* color: */font-weight:bold /* ; */; }" + + "baz { background-color:#f00;border: solid 2px;border-color:#0f0;text-decoration: underline; }"; + final String expected = " .foo { text-decoration: underline;font-weight:bold;}" + + "#bar { text-decoration: underline;font-weight:bold; }" + + "div {text-decoration: underline; font-weight:bold; }" + + "#foobar { text-decoration: underline; font-weight:bold ; }" + + "baz { background-color:#f00;border: solid 2px;border-color:#0f0;text-decoration: underline; }"; + assertEquals(expected, ShownotesCleaner.cleanStyleTag(input)); + } +} diff --git a/app/src/test/java/de/danoeh/antennapod/ui/screen/onlinefeedview/FeedDiscovererTest.java b/app/src/test/java/de/danoeh/antennapod/ui/screen/onlinefeedview/FeedDiscovererTest.java new file mode 100644 index 000000000..ee9f8a6d5 --- /dev/null +++ b/app/src/test/java/de/danoeh/antennapod/ui/screen/onlinefeedview/FeedDiscovererTest.java @@ -0,0 +1,128 @@ +package de.danoeh.antennapod.ui.screen.onlinefeedview; + +import androidx.test.platform.app.InstrumentationRegistry; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.io.File; +import java.io.FileOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +/** + * Test class for {@link FeedDiscoverer} + */ +@RunWith(RobolectricTestRunner.class) +public class FeedDiscovererTest { + + private FeedDiscoverer fd; + + private File testDir; + + @Before + public void setUp() { + fd = new FeedDiscoverer(); + testDir = new File(InstrumentationRegistry + .getInstrumentation().getTargetContext().getFilesDir(), "FeedDiscovererTest"); + //noinspection ResultOfMethodCallIgnored + testDir.mkdir(); + assertTrue(testDir.exists()); + } + + @After + public void tearDown() throws Exception { + FileUtils.deleteDirectory(testDir); + } + + @SuppressWarnings("SameParameterValue") + private String createTestHtmlString(String rel, String type, String href, String title) { + return String.format("<html><head><title>Test</title><link rel=\"%s\" type=\"%s\" href=\"%s\" title=\"%s\"></head><body></body></html>", + rel, type, href, title); + } + + private String createTestHtmlString(String rel, String type, String href) { + return String.format("<html><head><title>Test</title><link rel=\"%s\" type=\"%s\" href=\"%s\"></head><body></body></html>", + rel, type, href); + } + + private void checkFindUrls(boolean isAlternate, boolean isRss, boolean withTitle, boolean isAbsolute, boolean fromString) throws Exception { + final String title = "Test title"; + final String hrefAbs = "http://example.com/feed"; + final String hrefRel = "/feed"; + final String base = "http://example.com"; + + final String rel = (isAlternate) ? "alternate" : "feed"; + final String type = (isRss) ? "application/rss+xml" : "application/atom+xml"; + final String href = (isAbsolute) ? hrefAbs : hrefRel; + + Map<String, String> res; + String html = (withTitle) ? createTestHtmlString(rel, type, href, title) + : createTestHtmlString(rel, type, href); + if (fromString) { + res = fd.findLinks(html, base); + } else { + File testFile = new File(testDir, "feed"); + FileOutputStream out = new FileOutputStream(testFile); + IOUtils.write(html, out, StandardCharsets.UTF_8); + out.close(); + res = fd.findLinks(testFile, base); + } + + assertNotNull(res); + assertEquals(1, res.size()); + for (String key : res.keySet()) { + assertEquals(hrefAbs, key); + } + assertTrue(res.containsKey(hrefAbs)); + if (withTitle) { + assertEquals(title, res.get(hrefAbs)); + } else { + assertEquals(href, res.get(hrefAbs)); + } + } + + @Test + public void testAlternateRSSWithTitleAbsolute() throws Exception { + checkFindUrls(true, true, true, true, true); + } + + @Test + public void testAlternateRSSWithTitleRelative() throws Exception { + checkFindUrls(true, true, true, false, true); + } + + @Test + public void testAlternateRSSNoTitleAbsolute() throws Exception { + checkFindUrls(true, true, false, true, true); + } + + @Test + public void testAlternateRSSNoTitleRelative() throws Exception { + checkFindUrls(true, true, false, false, true); + } + + @Test + public void testAlternateAtomWithTitleAbsolute() throws Exception { + checkFindUrls(true, false, true, true, true); + } + + @Test + public void testFeedAtomWithTitleAbsolute() throws Exception { + checkFindUrls(false, false, true, true, true); + } + + @Test + public void testAlternateRSSWithTitleAbsoluteFromFile() throws Exception { + checkFindUrls(true, true, true, true, false); + } +} |