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) {
if (key.equals(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS)) {
if (UserPreferences.PREF_HIDDEN_DRAWER_ITEMS.equals(key)) {
loadItems();
}
}

View File

@ -100,7 +100,7 @@ public class PlaybackPreferences implements SharedPreferences.OnSharedPreference
}
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());
}
}

View File

@ -13,7 +13,7 @@ import java.security.NoSuchAlgorithmException;
/** Generates valid filenames for a given string. */
public class FileNameGenerator {
@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 char[] validChars =

View File

@ -20,7 +20,7 @@ public class AudioPlayer extends MediaPlayer implements IPlayer {
super(context, true, ClientConfig.USER_AGENT);
PreferenceManager.getDefaultSharedPreferences(context)
.registerOnSharedPreferenceChangeListener((sharedPreferences, key) -> {
if (key.equals(UserPreferences.PREF_MEDIA_PLAYER)) {
if (UserPreferences.PREF_MEDIA_PLAYER.equals(key)) {
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.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 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.assertNotEquals;
import static org.junit.Assert.assertTrue;
@SmallTest
@RunWith(RobolectricTestRunner.class)
public class FilenameGeneratorTest {
public FilenameGeneratorTest() {
@ -24,21 +23,21 @@ public class FilenameGeneratorTest {
}
@Test
public void testGenerateFileName() throws IOException {
public void testGenerateFileName() throws Exception {
String result = FileNameGenerator.generateFileName("abc abc");
assertEquals(result, "abc abc");
createFiles(result);
}
@Test
public void testGenerateFileName1() throws IOException {
public void testGenerateFileName1() throws Exception {
String result = FileNameGenerator.generateFileName("ab/c: <abc");
assertEquals(result, "abc abc");
createFiles(result);
}
@Test
public void testGenerateFileName2() throws IOException {
public void testGenerateFileName2() throws Exception {
String result = FileNameGenerator.generateFileName("abc abc ");
assertEquals(result, "abc abc");
createFiles(result);
@ -69,7 +68,7 @@ public class FilenameGeneratorTest {
}
@Test
public void testLongFilename() throws IOException {
public void testLongFilename() throws Exception {
String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
String result = FileNameGenerator.generateFileName(longName);
assertTrue(result.length() <= FileNameGenerator.MAX_FILENAME_LENGTH);
@ -87,16 +86,13 @@ public class FilenameGeneratorTest {
/**
* 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 testFile = new File(cache, name);
testFile.mkdir();
assertTrue(testFile.mkdir());
assertTrue(testFile.exists());
testFile.delete();
assertTrue(testFile.delete());
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.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Test class for URLChecker
* Test class for {@link URLChecker}
*/
@SmallTest
@RunWith(RobolectricTestRunner.class)
public class URLCheckerTest {
@Test
@ -78,7 +78,7 @@ public class URLCheckerTest {
}
@Test
public void testAntennaPodSubscribeProtocolNoScheme() throws Exception {
public void testAntennaPodSubscribeProtocolNoScheme() {
final String in = "antennapod-subscribe://example.com";
final String out = URLChecker.prepareURL(in);
assertEquals("http://example.com", out);
@ -92,14 +92,14 @@ public class URLCheckerTest {
}
@Test
public void testAntennaPodSubscribeProtocolWithScheme() throws Exception {
public void testAntennaPodSubscribeProtocolWithScheme() {
final String in = "antennapod-subscribe://https://example.com";
final String out = URLChecker.prepareURL(in);
assertEquals("https://example.com", out);
}
@Test
public void testProtocolRelativeUrlIsAbsolute() throws Exception {
public void testProtocolRelativeUrlIsAbsolute() {
final String in = "https://example.com";
final String inBase = "http://examplebase.com";
final String out = URLChecker.prepareURL(in, inBase);
@ -107,7 +107,7 @@ public class URLCheckerTest {
}
@Test
public void testProtocolRelativeUrlIsRelativeHttps() throws Exception {
public void testProtocolRelativeUrlIsRelativeHttps() {
final String in = "//example.com";
final String inBase = "https://examplebase.com";
final String out = URLChecker.prepareURL(in, inBase);
@ -115,7 +115,7 @@ public class URLCheckerTest {
}
@Test
public void testProtocolRelativeUrlIsHttpsWithAPSubscribeProtocol() throws Exception {
public void testProtocolRelativeUrlIsHttpsWithApSubscribeProtocol() {
final String in = "//example.com";
final String inBase = "antennapod-subscribe://https://examplebase.com";
final String out = URLChecker.prepareURL(in, inBase);
@ -123,7 +123,7 @@ public class URLCheckerTest {
}
@Test
public void testProtocolRelativeUrlBaseUrlNull() throws Exception {
public void testProtocolRelativeUrlBaseUrlNull() {
final String in = "example.com";
final String out = URLChecker.prepareURL(in, null);
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 androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
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.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 de.danoeh.antennapod.core.storage.DBReader;
import org.junit.After;
import org.junit.Before;
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.assertFalse;
@ -26,18 +30,28 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
* Test class for timeline.
* Test class for {@link Timeline}.
*/
@SmallTest
@RunWith(RobolectricTestRunner.class)
public class TimelineTest {
private Context context;
MockedStatic<DBReader> dbReaderMock;
@Before
public void setUp() {
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) {
FeedItem item = new FeedItem(0, "Item", "item-id", "http://example.com/item", new Date(), FeedItem.PLAYED, null);
item.setChapters(chapters);
@ -49,7 +63,7 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeHHMMSSNoChapters() {
public void testProcessShownotesAddTimecodeHhmmssNoChapters() {
final String timeStr = "10:11:12";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11 + 12 * 1000;
@ -61,7 +75,7 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeHHMMSSMoreThen24HoursNoChapters() {
public void testProcessShownotesAddTimecodeHhmmssMoreThen24HoursNoChapters() {
final String timeStr = "25:00:00";
final long time = 25 * 60 * 60 * 1000;
@ -73,7 +87,7 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeHHMMNoChapters() {
public void testProcessShownotesAddTimecodeHhmmNoChapters() {
final String timeStr = "10:11";
final long time = 3600 * 1000 * 10 + 60 * 1000 * 11;
@ -85,7 +99,7 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeMMSSNoChapters() {
public void testProcessShownotesAddTimecodeMmssNoChapters() {
final String timeStr = "10:11";
final long time = 10 * 60 * 1000 + 11 * 1000;
@ -97,7 +111,7 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeHMMSSNoChapters() {
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 "
@ -108,7 +122,7 @@ public class TimelineTest {
}
@Test
public void testProcessShownotesAddTimecodeMSSNoChapters() {
public void testProcessShownotesAddTimecodeMssNoChapters() {
final String timeStr = "1:12";
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);
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);
checkLinkCorrect(res, new long[]{10 * 60 * 1000 + 12 * 1000,
60 * 60 * 1000 + 10 * 60 * 1000 + 12 * 1000}, timeStrings);
}
@Test
@ -153,7 +167,7 @@ public class TimelineTest {
+ 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);
checkLinkCorrect(res, new long[]{10 * 60 * 1000 + 12 * 1000, 2 * 60 * 1000 + 12 * 1000}, timeStrings);
}
@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 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 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 FeedDiscoverer
* Test class for {@link FeedDiscoverer}
*/
@RunWith(RobolectricTestRunner.class)
public class FeedDiscovererTest {
private FeedDiscoverer fd;
@ -28,10 +30,11 @@ public class FeedDiscovererTest {
private File testDir;
@Before
public void setUp() throws Exception {
public void setUp() {
fd = new FeedDiscoverer();
testDir = new File(InstrumentationRegistry
.getInstrumentation().getTargetContext().getFilesDir(), "FeedDiscovererTest");
//noinspection ResultOfMethodCallIgnored
testDir.mkdir();
assertTrue(testDir.exists());
}
@ -41,6 +44,7 @@ public class FeedDiscovererTest {
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);
@ -69,7 +73,7 @@ public class FeedDiscovererTest {
} else {
File testFile = new File(testDir, "feed");
FileOutputStream out = new FileOutputStream(testFile);
IOUtils.write(html, out, Charset.forName("UTF-8"));
IOUtils.write(html, out, StandardCharsets.UTF_8);
out.close();
res = fd.findLinks(testFile, base);
}