From 542dbd190c6aa4b01c1b644e5c107333817442cd Mon Sep 17 00:00:00 2001 From: Herbert Reiter <46045854+damoasda@users.noreply.github.com> Date: Sat, 2 Jan 2021 17:49:30 +0100 Subject: Run more util tests with Robolectric (#4815) --- .../antennapod/util/FilenameGeneratorTest.java | 102 --------- .../de/test/antennapod/util/URLCheckerTest.java | 157 ------------- .../antennapod/util/playback/TimelineTest.java | 248 --------------------- .../util/syndication/FeedDiscovererTest.java | 124 ----------- .../danoeh/antennapod/adapter/NavListAdapter.java | 2 +- 5 files changed, 1 insertion(+), 632 deletions(-) delete mode 100644 app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java delete mode 100644 app/src/androidTest/java/de/test/antennapod/util/URLCheckerTest.java delete mode 100644 app/src/androidTest/java/de/test/antennapod/util/playback/TimelineTest.java delete mode 100644 app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java (limited to 'app') diff --git a/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java b/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java deleted file mode 100644 index f376c75a5..000000000 --- a/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package de.test.antennapod.util; - -import androidx.test.platform.app.InstrumentationRegistry; -import androidx.test.filters.SmallTest; -import android.text.TextUtils; - -import java.io.File; -import java.io.IOException; - -import de.danoeh.antennapod.core.util.FileNameGenerator; -import org.apache.commons.lang3.StringUtils; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; - -@SmallTest -public class FilenameGeneratorTest { - - public FilenameGeneratorTest() { - super(); - } - - @Test - public void testGenerateFileName() throws IOException { - String result = FileNameGenerator.generateFileName("abc abc"); - assertEquals(result, "abc abc"); - createFiles(result); - } - - @Test - public void testGenerateFileName1() throws IOException { - String result = FileNameGenerator.generateFileName("ab/c: 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, "

Some test text with a timecode " - + timeStr + " here.

", 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, "

Some test text with a timecode " - + timeStr + " here.

", 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, "

Some test text with a timecode " - + timeStr + " here.

", 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, "

Some test text with a timecode " - + timeStr + " here.

", 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, "

Some test text with a timecode " - + timeStr + " here.

", 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, "

Some test text with a timecode " - + timeStr + " here.

", 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 = "

Some test text with a timecode " + timeStr + " here.

"; - 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, "

Some test text with a timecode " - + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!

", 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, "

Some test text with a timecode " - + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!

", 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, "

Some test text with a timecode (" - + timeStr + ") here.

", 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, "

Some test text with a timecode [" - + timeStr + "] here.

", 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, "

Some test text with a timecode <" - + timeStr + "> here.

", 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("

Some test text with timecodes "); - for (String timeStr : timeStrs) { - shownotes.append(timeStr).append(" "); - } - shownotes.append("here.

"); - - 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")); - - } -} diff --git a/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java b/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java deleted file mode 100644 index b213a5efa..000000000 --- a/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java +++ /dev/null @@ -1,124 +0,0 @@ -package de.test.antennapod.util.syndication; - -import androidx.test.platform.app.InstrumentationRegistry; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.IOUtils; - -import java.io.File; -import java.io.FileOutputStream; -import java.nio.charset.Charset; -import java.util.Map; - -import de.danoeh.antennapod.core.util.syndication.FeedDiscoverer; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Test class for FeedDiscoverer - */ -public class FeedDiscovererTest { - - private FeedDiscoverer fd; - - private File testDir; - - @Before - public void setUp() throws Exception { - fd = new FeedDiscoverer(); - testDir = new File(InstrumentationRegistry - .getInstrumentation().getTargetContext().getFilesDir(), "FeedDiscovererTest"); - testDir.mkdir(); - assertTrue(testDir.exists()); - } - - @After - public void tearDown() throws Exception { - FileUtils.deleteDirectory(testDir); - } - - private String createTestHtmlString(String rel, String type, String href, String title) { - return String.format("Test", - rel, type, href, title); - } - - private String createTestHtmlString(String rel, String type, String href) { - return String.format("Test", - 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 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, Charset.forName("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); - } -} diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java index 92ed7b052..f8507ba74 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java @@ -75,7 +75,7 @@ public class NavListAdapter extends BaseAdapter } public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if (key.equals(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS)) { + if (UserPreferences.PREF_HIDDEN_DRAWER_ITEMS.equals(key)) { loadItems(); } } -- cgit v1.2.3