Run more util tests with Robolectric (#4815)

This commit is contained in:
Herbert Reiter 2021-01-02 17:49:30 +01:00 committed by GitHub
parent 486de46b8f
commit 542dbd190c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 56 deletions

View File

@ -75,7 +75,7 @@ public class NavListAdapter extends BaseAdapter
} }
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS)) { if (UserPreferences.PREF_HIDDEN_DRAWER_ITEMS.equals(key)) {
loadItems(); loadItems();
} }
} }

View File

@ -100,7 +100,7 @@ public class PlaybackPreferences implements SharedPreferences.OnSharedPreference
} }
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(PREF_CURRENT_PLAYER_STATUS)) { if (PREF_CURRENT_PLAYER_STATUS.equals(key)) {
EventBus.getDefault().post(new PlayerStatusEvent()); EventBus.getDefault().post(new PlayerStatusEvent());
} }
} }

View File

@ -13,7 +13,7 @@ import java.security.NoSuchAlgorithmException;
/** Generates valid filenames for a given string. */ /** Generates valid filenames for a given string. */
public class FileNameGenerator { public class FileNameGenerator {
@VisibleForTesting @VisibleForTesting
public static final int MAX_FILENAME_LENGTH = 255; // Limited by ext4 public static final int MAX_FILENAME_LENGTH = 242; // limited by CircleCI
private static final int MD5_HEX_LENGTH = 32; private static final int MD5_HEX_LENGTH = 32;
private static final char[] validChars = private static final char[] validChars =

View File

@ -20,7 +20,7 @@ public class AudioPlayer extends MediaPlayer implements IPlayer {
super(context, true, ClientConfig.USER_AGENT); super(context, true, ClientConfig.USER_AGENT);
PreferenceManager.getDefaultSharedPreferences(context) PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> { .registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> {
if (key.equals(UserPreferences.PREF_MEDIA_PLAYER)) { if (UserPreferences.PREF_MEDIA_PLAYER.equals(key)) {
checkMpi(); checkMpi();
} }
}); });

View File

@ -1,22 +1,21 @@
package de.test.antennapod.util; package de.danoeh.antennapod.core.util;
import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import android.text.TextUtils; import android.text.TextUtils;
import java.io.File; import java.io.File;
import java.io.IOException;
import de.danoeh.antennapod.core.util.FileNameGenerator;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@SmallTest @RunWith(RobolectricTestRunner.class)
public class FilenameGeneratorTest { public class FilenameGeneratorTest {
public FilenameGeneratorTest() { public FilenameGeneratorTest() {
@ -24,21 +23,21 @@ public class FilenameGeneratorTest {
} }
@Test @Test
public void testGenerateFileName() throws IOException { public void testGenerateFileName() throws Exception {
String result = FileNameGenerator.generateFileName("abc abc"); String result = FileNameGenerator.generateFileName("abc abc");
assertEquals(result, "abc abc"); assertEquals(result, "abc abc");
createFiles(result); createFiles(result);
} }
@Test @Test
public void testGenerateFileName1() throws IOException { public void testGenerateFileName1() throws Exception {
String result = FileNameGenerator.generateFileName("ab/c: <abc"); String result = FileNameGenerator.generateFileName("ab/c: <abc");
assertEquals(result, "abc abc"); assertEquals(result, "abc abc");
createFiles(result); createFiles(result);
} }
@Test @Test
public void testGenerateFileName2() throws IOException { public void testGenerateFileName2() throws Exception {
String result = FileNameGenerator.generateFileName("abc abc "); String result = FileNameGenerator.generateFileName("abc abc ");
assertEquals(result, "abc abc"); assertEquals(result, "abc abc");
createFiles(result); createFiles(result);
@ -69,7 +68,7 @@ public class FilenameGeneratorTest {
} }
@Test @Test
public void testLongFilename() throws IOException { public void testLongFilename() throws Exception {
String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH); String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
String result = FileNameGenerator.generateFileName(longName); String result = FileNameGenerator.generateFileName(longName);
assertTrue(result.length() <= FileNameGenerator.MAX_FILENAME_LENGTH); assertTrue(result.length() <= FileNameGenerator.MAX_FILENAME_LENGTH);
@ -87,16 +86,13 @@ public class FilenameGeneratorTest {
/** /**
* Tests if files can be created. * Tests if files can be created.
*
* @throws IOException
*/ */
private void createFiles(String name) throws IOException { private void createFiles(String name) throws Exception {
File cache = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir(); File cache = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir();
File testFile = new File(cache, name); File testFile = new File(cache, name);
testFile.mkdir(); assertTrue(testFile.mkdir());
assertTrue(testFile.exists()); assertTrue(testFile.exists());
testFile.delete(); assertTrue(testFile.delete());
assertTrue(testFile.createNewFile()); assertTrue(testFile.createNewFile());
} }
} }

View File

@ -1,17 +1,17 @@
package de.test.antennapod.util; package de.danoeh.antennapod.core.util;
import androidx.test.filters.SmallTest;
import de.danoeh.antennapod.core.util.URLChecker;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
* Test class for URLChecker * Test class for {@link URLChecker}
*/ */
@SmallTest @RunWith(RobolectricTestRunner.class)
public class URLCheckerTest { public class URLCheckerTest {
@Test @Test
@ -78,7 +78,7 @@ public class URLCheckerTest {
} }
@Test @Test
public void testAntennaPodSubscribeProtocolNoScheme() throws Exception { public void testAntennaPodSubscribeProtocolNoScheme() {
final String in = "antennapod-subscribe://example.com"; final String in = "antennapod-subscribe://example.com";
final String out = URLChecker.prepareURL(in); final String out = URLChecker.prepareURL(in);
assertEquals("http://example.com", out); assertEquals("http://example.com", out);
@ -92,14 +92,14 @@ public class URLCheckerTest {
} }
@Test @Test
public void testAntennaPodSubscribeProtocolWithScheme() throws Exception { public void testAntennaPodSubscribeProtocolWithScheme() {
final String in = "antennapod-subscribe://https://example.com"; final String in = "antennapod-subscribe://https://example.com";
final String out = URLChecker.prepareURL(in); final String out = URLChecker.prepareURL(in);
assertEquals("https://example.com", out); assertEquals("https://example.com", out);
} }
@Test @Test
public void testProtocolRelativeUrlIsAbsolute() throws Exception { public void testProtocolRelativeUrlIsAbsolute() {
final String in = "https://example.com"; final String in = "https://example.com";
final String inBase = "http://examplebase.com"; final String inBase = "http://examplebase.com";
final String out = URLChecker.prepareURL(in, inBase); final String out = URLChecker.prepareURL(in, inBase);
@ -107,7 +107,7 @@ public class URLCheckerTest {
} }
@Test @Test
public void testProtocolRelativeUrlIsRelativeHttps() throws Exception { public void testProtocolRelativeUrlIsRelativeHttps() {
final String in = "//example.com"; final String in = "//example.com";
final String inBase = "https://examplebase.com"; final String inBase = "https://examplebase.com";
final String out = URLChecker.prepareURL(in, inBase); final String out = URLChecker.prepareURL(in, inBase);
@ -115,7 +115,7 @@ public class URLCheckerTest {
} }
@Test @Test
public void testProtocolRelativeUrlIsHttpsWithAPSubscribeProtocol() throws Exception { public void testProtocolRelativeUrlIsHttpsWithApSubscribeProtocol() {
final String in = "//example.com"; final String in = "//example.com";
final String inBase = "antennapod-subscribe://https://examplebase.com"; final String inBase = "antennapod-subscribe://https://examplebase.com";
final String out = URLChecker.prepareURL(in, inBase); final String out = URLChecker.prepareURL(in, inBase);
@ -123,7 +123,7 @@ public class URLCheckerTest {
} }
@Test @Test
public void testProtocolRelativeUrlBaseUrlNull() throws Exception { public void testProtocolRelativeUrlBaseUrlNull() {
final String in = "example.com"; final String in = "example.com";
final String out = URLChecker.prepareURL(in, null); final String out = URLChecker.prepareURL(in, null);
assertEquals("http://example.com", out); assertEquals("http://example.com", out);

View File

@ -1,9 +1,8 @@
package de.test.antennapod.util.playback; package de.danoeh.antennapod.core.util.playback;
import android.content.Context; import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
@ -15,10 +14,15 @@ import java.util.List;
import de.danoeh.antennapod.core.feed.Chapter; import de.danoeh.antennapod.core.feed.Chapter;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.util.playback.Playable; import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.util.playback.Timeline;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -26,18 +30,28 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
* Test class for timeline. * Test class for {@link Timeline}.
*/ */
@SmallTest @RunWith(RobolectricTestRunner.class)
public class TimelineTest { public class TimelineTest {
private Context context; private Context context;
MockedStatic<DBReader> dbReaderMock;
@Before @Before
public void setUp() { public void setUp() {
context = InstrumentationRegistry.getInstrumentation().getTargetContext(); context = InstrumentationRegistry.getInstrumentation().getTargetContext();
// mock DBReader, because Timeline.processShownotes() calls FeedItem.loadShownotes()
// which calls DBReader.loadDescriptionOfFeedItem(), but we don't need the database here
dbReaderMock = Mockito.mockStatic(DBReader.class);
} }
@After
public void tearDown() {
dbReaderMock.close();
}
@SuppressWarnings("SameParameterValue")
private Playable newTestPlayable(List<Chapter> chapters, String shownotes, int duration) { 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); FeedItem item = new FeedItem(0, "Item", "item-id", "http://example.com/item", new Date(), FeedItem.PLAYED, null);
item.setChapters(chapters); item.setChapters(chapters);
@ -49,7 +63,7 @@ public class TimelineTest {
} }
@Test @Test
public void testProcessShownotesAddTimecodeHHMMSSNoChapters() { public void testProcessShownotesAddTimecodeHhmmssNoChapters() {
final String timeStr = "10:11:12"; final String timeStr = "10:11:12";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000; final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000;
@ -61,7 +75,7 @@ public class TimelineTest {
} }
@Test @Test
public void testProcessShownotesAddTimecodeHHMMSSMoreThen24HoursNoChapters() { public void testProcessShownotesAddTimecodeHhmmssMoreThen24HoursNoChapters() {
final String timeStr = "25:00:00"; final String timeStr = "25:00:00";
final long time = 25 * 60 * 60 * 1000; final long time = 25 * 60 * 60 * 1000;
@ -73,7 +87,7 @@ public class TimelineTest {
} }
@Test @Test
public void testProcessShownotesAddTimecodeHHMMNoChapters() { public void testProcessShownotesAddTimecodeHhmmNoChapters() {
final String timeStr = "10:11"; final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11; final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
@ -85,7 +99,7 @@ public class TimelineTest {
} }
@Test @Test
public void testProcessShownotesAddTimecodeMMSSNoChapters() { public void testProcessShownotesAddTimecodeMmssNoChapters() {
final String timeStr = "10:11"; final String timeStr = "10:11";
final long time = 10 * 60 * 1000 + 11 * 1000; final long time = 10 * 60 * 1000 + 11 * 1000;
@ -97,7 +111,7 @@ public class TimelineTest {
} }
@Test @Test
public void testProcessShownotesAddTimecodeHMMSSNoChapters() { public void testProcessShownotesAddTimecodeHmmssNoChapters() {
final String timeStr = "2:11:12"; final String timeStr = "2:11:12";
final long time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000; final long time = 2 * 60 * 60 * 1000 + 11 * 60 * 1000 + 12 * 1000;
Playable p = newTestPlayable(null, "<p> Some test text with a timecode " Playable p = newTestPlayable(null, "<p> Some test text with a timecode "
@ -108,7 +122,7 @@ public class TimelineTest {
} }
@Test @Test
public void testProcessShownotesAddTimecodeMSSNoChapters() { public void testProcessShownotesAddTimecodeMssNoChapters() {
final String timeStr = "1:12"; final String timeStr = "1:12";
final long time = 60 * 1000 + 12 * 1000; final long time = 60 * 1000 + 12 * 1000;
@ -139,8 +153,8 @@ public class TimelineTest {
+ timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 2 * 60 * 60 * 1000); + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 2 * 60 * 60 * 1000);
Timeline t = new Timeline(context, p); Timeline t = new Timeline(context, p);
String res = t.processShownotes(); String res = t.processShownotes();
checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000, checkLinkCorrect(res, new long[]{10 * 60 * 1000 + 12 * 1000,
60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000 }, timeStrings); 60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000}, timeStrings);
} }
@Test @Test
@ -153,7 +167,7 @@ public class TimelineTest {
+ timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 3 * 60 * 60 * 1000); + timeStrings[0] + " here. Hey look another one " + timeStrings[1] + " here!</p>", 3 * 60 * 60 * 1000);
Timeline t = new Timeline(context, p); Timeline t = new Timeline(context, p);
String res = t.processShownotes(); String res = t.processShownotes();
checkLinkCorrect(res, new long[]{ 10 * 60 * 1000 + 12 * 1000, 2 * 60 * 1000 + 12 * 1000 }, timeStrings); checkLinkCorrect(res, new long[]{10 * 60 * 1000 + 12 * 1000, 2 * 60 * 1000 + 12 * 1000}, timeStrings);
} }
@Test @Test

View File

@ -1,26 +1,28 @@
package de.test.antennapod.util.syndication; package de.danoeh.antennapod.core.util.syndication;
import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.platform.app.InstrumentationRegistry;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; 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.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
* Test class for FeedDiscoverer * Test class for {@link FeedDiscoverer}
*/ */
@RunWith(RobolectricTestRunner.class)
public class FeedDiscovererTest { public class FeedDiscovererTest {
private FeedDiscoverer fd; private FeedDiscoverer fd;
@ -28,10 +30,11 @@ public class FeedDiscovererTest {
private File testDir; private File testDir;
@Before @Before
public void setUp() throws Exception { public void setUp() {
fd = new FeedDiscoverer(); fd = new FeedDiscoverer();
testDir = new File(InstrumentationRegistry testDir = new File(InstrumentationRegistry
.getInstrumentation().getTargetContext().getFilesDir(), "FeedDiscovererTest"); .getInstrumentation().getTargetContext().getFilesDir(), "FeedDiscovererTest");
//noinspection ResultOfMethodCallIgnored
testDir.mkdir(); testDir.mkdir();
assertTrue(testDir.exists()); assertTrue(testDir.exists());
} }
@ -41,6 +44,7 @@ public class FeedDiscovererTest {
FileUtils.deleteDirectory(testDir); FileUtils.deleteDirectory(testDir);
} }
@SuppressWarnings("SameParameterValue")
private String createTestHtmlString(String rel, String type, String href, String title) { 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>", 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); rel, type, href, title);
@ -69,7 +73,7 @@ public class FeedDiscovererTest {
} else { } else {
File testFile = new File(testDir, "feed"); File testFile = new File(testDir, "feed");
FileOutputStream out = new FileOutputStream(testFile); FileOutputStream out = new FileOutputStream(testFile);
IOUtils.write(html, out, Charset.forName("UTF-8")); IOUtils.write(html, out, StandardCharsets.UTF_8);
out.close(); out.close();
res = fd.findLinks(testFile, base); res = fd.findLinks(testFile, base);
} }