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 /core/src/test/java/de/danoeh | |
parent | 4e47691e70e85736c7eeb30ce02c73176e565a86 (diff) | |
download | AntennaPod-edb440a5a9a05e24c344a71b272b9238217e9c55.zip |
Restructure related UI classes together (#7044)
Diffstat (limited to 'core/src/test/java/de/danoeh')
7 files changed, 0 insertions, 964 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java deleted file mode 100644 index 966351a5e..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/feed/VolumeAdaptionSettingTest.java +++ /dev/null @@ -1,129 +0,0 @@ -package de.danoeh.antennapod.core.feed; - -import de.danoeh.antennapod.model.feed.VolumeAdaptionSetting; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; - -public class VolumeAdaptionSettingTest { - - @Before - public void setUp() throws Exception { - VolumeAdaptionSetting.setBoostSupported(false); - } - - @After - public void tearDown() throws Exception { - VolumeAdaptionSetting.setBoostSupported(null); - } - - @Test - public void mapOffToInteger() { - VolumeAdaptionSetting setting = VolumeAdaptionSetting.OFF; - assertThat(setting.toInteger(), is(equalTo(0))); - } - - @Test - public void mapLightReductionToInteger() { - VolumeAdaptionSetting setting = VolumeAdaptionSetting.LIGHT_REDUCTION; - - assertThat(setting.toInteger(), is(equalTo(1))); - } - - @Test - public void mapHeavyReductionToInteger() { - VolumeAdaptionSetting setting = VolumeAdaptionSetting.HEAVY_REDUCTION; - - assertThat(setting.toInteger(), is(equalTo(2))); - } - - @Test - public void mapLightBoostToInteger() { - VolumeAdaptionSetting setting = VolumeAdaptionSetting.LIGHT_BOOST; - - assertThat(setting.toInteger(), is(equalTo(3))); - } - - @Test - public void mapMediumBoostToInteger() { - VolumeAdaptionSetting setting = VolumeAdaptionSetting.MEDIUM_BOOST; - - assertThat(setting.toInteger(), is(equalTo(4))); - } - - @Test - public void mapHeavyBoostToInteger() { - VolumeAdaptionSetting setting = VolumeAdaptionSetting.HEAVY_BOOST; - - assertThat(setting.toInteger(), is(equalTo(5))); - } - - @Test - public void mapIntegerToVolumeAdaptionSetting() { - assertThat(VolumeAdaptionSetting.fromInteger(0), is(equalTo(VolumeAdaptionSetting.OFF))); - assertThat(VolumeAdaptionSetting.fromInteger(1), is(equalTo(VolumeAdaptionSetting.LIGHT_REDUCTION))); - assertThat(VolumeAdaptionSetting.fromInteger(2), is(equalTo(VolumeAdaptionSetting.HEAVY_REDUCTION))); - assertThat(VolumeAdaptionSetting.fromInteger(3), is(equalTo(VolumeAdaptionSetting.LIGHT_BOOST))); - assertThat(VolumeAdaptionSetting.fromInteger(4), is(equalTo(VolumeAdaptionSetting.MEDIUM_BOOST))); - assertThat(VolumeAdaptionSetting.fromInteger(5), is(equalTo(VolumeAdaptionSetting.HEAVY_BOOST))); - } - - @Test(expected = IllegalArgumentException.class) - public void cannotMapNegativeValues() { - VolumeAdaptionSetting.fromInteger(-1); - } - - @Test(expected = IllegalArgumentException.class) - public void cannotMapValuesOutOfRange() { - VolumeAdaptionSetting.fromInteger(6); - } - - @Test - public void noAdaptionIfTurnedOff() { - float adaptionFactor = VolumeAdaptionSetting.OFF.getAdaptionFactor(); - assertEquals(1.0f, adaptionFactor, 0.01f); - } - - @Test - public void lightReductionYieldsHigherValueThanHeavyReduction() { - float lightReductionFactor = VolumeAdaptionSetting.LIGHT_REDUCTION.getAdaptionFactor(); - - float heavyReductionFactor = VolumeAdaptionSetting.HEAVY_REDUCTION.getAdaptionFactor(); - - assertTrue("Light reduction must have higher factor than heavy reduction", lightReductionFactor > heavyReductionFactor); - } - - @Test - public void lightBoostYieldsHigherValueThanLightReduction() { - float lightReductionFactor = VolumeAdaptionSetting.LIGHT_REDUCTION.getAdaptionFactor(); - - float lightBoostFactor = VolumeAdaptionSetting.LIGHT_BOOST.getAdaptionFactor(); - - assertTrue("Light boost must have higher factor than light reduction", lightBoostFactor > lightReductionFactor); - } - - @Test - public void mediumBoostYieldsHigherValueThanLightBoost() { - float lightBoostFactor = VolumeAdaptionSetting.LIGHT_BOOST.getAdaptionFactor(); - - float mediumBoostFactor = VolumeAdaptionSetting.MEDIUM_BOOST.getAdaptionFactor(); - - assertTrue("Medium boost must have higher factor than light boost", mediumBoostFactor > lightBoostFactor); - } - - @Test - public void heavyBoostYieldsHigherValueThanMediumBoost() { - float mediumBoostFactor = VolumeAdaptionSetting.MEDIUM_BOOST.getAdaptionFactor(); - - float heavyBoostFactor = VolumeAdaptionSetting.HEAVY_BOOST.getAdaptionFactor(); - - assertTrue("Heavy boost must have higher factor than medium boost", heavyBoostFactor > mediumBoostFactor); - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/mapper/FeedCursorMapperTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/mapper/FeedCursorMapperTest.java deleted file mode 100644 index 87ade0c6f..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/mapper/FeedCursorMapperTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package de.danoeh.antennapod.core.storage.mapper; - -import android.content.ContentValues; -import android.content.Context; -import android.database.Cursor; - -import androidx.test.platform.app.InstrumentationRegistry; - -import de.danoeh.antennapod.storage.database.PodDBAdapter; -import de.danoeh.antennapod.storage.database.mapper.FeedCursorMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import de.danoeh.antennapod.model.feed.Feed; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -@RunWith(RobolectricTestRunner.class) -public class FeedCursorMapperTest { - private PodDBAdapter adapter; - - @Before - public void setUp() { - Context context = InstrumentationRegistry.getInstrumentation().getContext(); - - PodDBAdapter.init(context); - adapter = PodDBAdapter.getInstance(); - - writeFeedToDatabase(); - } - - @After - public void tearDown() { - PodDBAdapter.tearDownTests(); - } - - @SuppressWarnings("ConstantConditions") - @Test - public void testFromCursor() { - try (Cursor cursor = adapter.getAllFeedsCursor()) { - cursor.moveToNext(); - Feed feed = FeedCursorMapper.convert(cursor); - assertTrue(feed.getId() >= 0); - assertEquals("feed custom title", feed.getTitle()); - assertEquals("feed custom title", feed.getCustomTitle()); - assertEquals("feed link", feed.getLink()); - assertEquals("feed description", feed.getDescription()); - assertEquals("feed payment link", feed.getPaymentLinks().get(0).url); - assertEquals("feed author", feed.getAuthor()); - assertEquals("feed language", feed.getLanguage()); - assertEquals("feed image url", feed.getImageUrl()); - assertEquals("feed file url", feed.getLocalFileUrl()); - assertEquals("feed download url", feed.getDownloadUrl()); - assertEquals(42, feed.getLastRefreshAttempt()); - assertEquals("feed last update", feed.getLastModified()); - assertEquals("feed type", feed.getType()); - assertEquals("feed identifier", feed.getFeedIdentifier()); - assertTrue(feed.isPaged()); - assertEquals("feed next page link", feed.getNextPageLink()); - assertTrue(feed.getItemFilter().showUnplayed); - assertEquals(1, feed.getSortOrder().code); - assertTrue(feed.hasLastUpdateFailed()); - } - } - - /** - * Insert test data to the database. - * Uses raw database insert instead of adapter.setCompleteFeed() to avoid testing the Feed class - * against itself. - */ - private void writeFeedToDatabase() { - ContentValues values = new ContentValues(); - values.put(PodDBAdapter.KEY_TITLE, "feed title"); - values.put(PodDBAdapter.KEY_CUSTOM_TITLE, "feed custom title"); - values.put(PodDBAdapter.KEY_LINK, "feed link"); - values.put(PodDBAdapter.KEY_DESCRIPTION, "feed description"); - values.put(PodDBAdapter.KEY_PAYMENT_LINK, "feed payment link"); - values.put(PodDBAdapter.KEY_AUTHOR, "feed author"); - values.put(PodDBAdapter.KEY_LANGUAGE, "feed language"); - values.put(PodDBAdapter.KEY_IMAGE_URL, "feed image url"); - - values.put(PodDBAdapter.KEY_FILE_URL, "feed file url"); - values.put(PodDBAdapter.KEY_DOWNLOAD_URL, "feed download url"); - values.put(PodDBAdapter.KEY_LAST_REFRESH_ATTEMPT, 42); - values.put(PodDBAdapter.KEY_LASTUPDATE, "feed last update"); - values.put(PodDBAdapter.KEY_TYPE, "feed type"); - values.put(PodDBAdapter.KEY_FEED_IDENTIFIER, "feed identifier"); - - values.put(PodDBAdapter.KEY_IS_PAGED, true); - values.put(PodDBAdapter.KEY_NEXT_PAGE_LINK, "feed next page link"); - values.put(PodDBAdapter.KEY_HIDE, "unplayed"); - values.put(PodDBAdapter.KEY_SORT_ORDER, "1"); - values.put(PodDBAdapter.KEY_LAST_UPDATE_FAILED, true); - - adapter.insertTestData(PodDBAdapter.TABLE_NAME_FEEDS, values); - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/ConverterTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/ConverterTest.java deleted file mode 100644 index 2e4ead5e6..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/ConverterTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.danoeh.antennapod.core.util; - -import de.danoeh.antennapod.ui.common.Converter; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Test class for converter - */ -public class ConverterTest { - - @Test - public void testGetDurationStringLong() { - String expected = "13:05:10"; - int input = 47110000; - assertEquals(expected, Converter.getDurationStringLong(input)); - } - - @Test - public void testGetDurationStringShort() { - String expected = "13:05"; - assertEquals(expected, Converter.getDurationStringShort(47110000, true)); - assertEquals(expected, Converter.getDurationStringShort(785000, false)); - } - - @Test - public void testDurationStringLongToMs() { - String input = "01:20:30"; - long expected = 4830000; - assertEquals(expected, Converter.durationStringLongToMs(input)); - } - - @Test - public void testDurationStringShortToMs() { - String input = "8:30"; - assertEquals(30600000, Converter.durationStringShortToMs(input, true)); - assertEquals(510000, Converter.durationStringShortToMs(input, false)); - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java deleted file mode 100644 index 4ebff066e..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/FeedItemPermutorsTest.java +++ /dev/null @@ -1,231 +0,0 @@ -package de.danoeh.antennapod.core.util; - -import de.danoeh.antennapod.model.feed.SortOrder; -import de.danoeh.antennapod.storage.database.FeedItemPermutors; -import de.danoeh.antennapod.storage.database.Permutor; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Calendar; -import java.util.List; - -import de.danoeh.antennapod.model.feed.Feed; -import de.danoeh.antennapod.model.feed.FeedItem; -import de.danoeh.antennapod.model.feed.FeedMedia; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Test class for FeedItemPermutors. - */ -public class FeedItemPermutorsTest { - - @Test - public void testEnsureNonNullPermutors() { - for (SortOrder sortOrder : SortOrder.values()) { - assertNotNull("The permutor for SortOrder " + sortOrder + " is unexpectedly null", - FeedItemPermutors.getPermutor(sortOrder)); - } - } - - @Test - public void testPermutorForRule_EPISODE_TITLE_ASC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_EPISODE_TITLE_ASC_NullTitle() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_A_Z); - - List<FeedItem> itemList = getTestList(); - itemList.get(2) // itemId 2 - .setTitle(null); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting - } - - - @Test - public void testPermutorForRule_EPISODE_TITLE_DESC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.EPISODE_TITLE_Z_A); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_DATE_ASC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_DATE_ASC_NulPubDatel() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_OLD_NEW); - - List<FeedItem> itemList = getTestList(); - itemList.get(2) // itemId 2 - .setPubDate(null); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting - } - - @Test - public void testPermutorForRule_DATE_DESC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DATE_NEW_OLD); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_DURATION_ASC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_SHORT_LONG); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_DURATION_DESC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_size_asc() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.SIZE_SMALL_LARGE); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_size_desc() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.SIZE_LARGE_SMALL); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_DURATION_DESC_NullMedia() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.DURATION_LONG_SHORT); - - List<FeedItem> itemList = getTestList(); - itemList.get(1) // itemId 3 - .setMedia(null); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting - } - - @Test - public void testPermutorForRule_FEED_TITLE_ASC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_A_Z); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 1, 2, 3)); // after sorting - } - - @Test - public void testPermutorForRule_FEED_TITLE_DESC() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); - - List<FeedItem> itemList = getTestList(); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 3, 2, 1)); // after sorting - } - - @Test - public void testPermutorForRule_FEED_TITLE_DESC_NullTitle() { - Permutor<FeedItem> permutor = FeedItemPermutors.getPermutor(SortOrder.FEED_TITLE_Z_A); - - List<FeedItem> itemList = getTestList(); - itemList.get(1) // itemId 3 - .getFeed().setTitle(null); - assertTrue(checkIdOrder(itemList, 1, 3, 2)); // before sorting - permutor.reorder(itemList); - assertTrue(checkIdOrder(itemList, 2, 1, 3)); // after sorting - } - - /** - * Generates a list with test data. - */ - private List<FeedItem> getTestList() { - List<FeedItem> itemList = new ArrayList<>(); - - Calendar calendar = Calendar.getInstance(); - calendar.set(2019, 0, 1); // January 1st - Feed feed1 = new Feed(null, null, "Feed title 1"); - FeedItem feedItem1 = new FeedItem(1, "Title 1", null, null, calendar.getTime(), 0, feed1); - FeedMedia feedMedia1 = new FeedMedia(0, feedItem1, 1000, 0, 100, null, null, null, true, null, 0, 0); - feedItem1.setMedia(feedMedia1); - itemList.add(feedItem1); - - calendar.set(2019, 2, 1); // March 1st - Feed feed2 = new Feed(null, null, "Feed title 3"); - FeedItem feedItem2 = new FeedItem(3, "Title 3", null, null, calendar.getTime(), 0, feed2); - FeedMedia feedMedia2 = new FeedMedia(0, feedItem2, 3000, 0, 300, null, null, null, true, null, 0, 0); - feedItem2.setMedia(feedMedia2); - itemList.add(feedItem2); - - calendar.set(2019, 1, 1); // February 1st - Feed feed3 = new Feed(null, null, "Feed title 2"); - FeedItem feedItem3 = new FeedItem(2, "Title 2", null, null, calendar.getTime(), 0, feed3); - FeedMedia feedMedia3 = new FeedMedia(0, feedItem3, 2000, 0, 200, null, null, null, true, null, 0, 0); - feedItem3.setMedia(feedMedia3); - itemList.add(feedItem3); - - return itemList; - } - - /** - * Checks if both lists have the same size and the same ID order. - * - * @param itemList Item list. - * @param ids List of IDs. - * @return <code>true</code> if both lists have the same size and the same ID order. - */ - private boolean checkIdOrder(List<FeedItem> itemList, long... ids) { - if (itemList.size() != ids.length) { - return false; - } - - for (int i = 0; i < ids.length; i++) { - if (itemList.get(i).getId() != ids[i]) { - return false; - } - } - return true; - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/FilenameGeneratorTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/FilenameGeneratorTest.java deleted file mode 100644 index 4c225322a..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/FilenameGeneratorTest.java +++ /dev/null @@ -1,99 +0,0 @@ -package de.danoeh.antennapod.core.util; - -import androidx.test.platform.app.InstrumentationRegistry; -import android.text.TextUtils; - -import java.io.File; - -import de.danoeh.antennapod.net.download.serviceinterface.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; - -@RunWith(RobolectricTestRunner.class) -public class FilenameGeneratorTest { - - public FilenameGeneratorTest() { - super(); - } - - @Test - public void testGenerateFileName() throws Exception { - String result = FileNameGenerator.generateFileName("abc abc"); - assertEquals(result, "abc abc"); - createFiles(result); - } - - @Test - public void testGenerateFileName1() throws Exception { - String result = FileNameGenerator.generateFileName("ab/c: <abc"); - assertEquals(result, "abc abc"); - createFiles(result); - } - - @Test - public void testGenerateFileName2() throws Exception { - String result = FileNameGenerator.generateFileName("abc abc "); - assertEquals(result, "abc abc"); - createFiles(result); - } - - @Test - public void testFeedTitleContainsApostrophe() { - String result = FileNameGenerator.generateFileName("Feed's Title ..."); - assertEquals("Feeds Title", result); - } - - @Test - public void testFeedTitleContainsDash() { - String result = FileNameGenerator.generateFileName("Left - Right"); - assertEquals("Left - Right", result); - } - - @Test - public void testFeedTitleContainsAccents() { - String result = FileNameGenerator.generateFileName("Äàáâãå"); - assertEquals("Aaaaaa", result); - } - - @Test - public void testInvalidInput() { - String result = FileNameGenerator.generateFileName("???"); - assertFalse(TextUtils.isEmpty(result)); - } - - @Test - 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); - createFiles(result); - } - - @Test - public void testLongFilenameNotEquals() { - // Verify that the name is not just trimmed and different suffixes end up with the same name - String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH); - String result1 = FileNameGenerator.generateFileName(longName + "a"); - String result2 = FileNameGenerator.generateFileName(longName + "b"); - assertNotEquals(result1, result2); - } - - /** - * Tests if files can be created. - */ - private void createFiles(String name) throws Exception { - File cache = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir(); - File testFile = new File(cache, name); - assertTrue(testFile.mkdir()); - assertTrue(testFile.exists()); - assertTrue(testFile.delete()); - assertTrue(testFile.createNewFile()); - } -} diff --git a/core/src/test/java/de/danoeh/antennapod/core/util/gui/ShownotesCleanerTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/gui/ShownotesCleanerTest.java deleted file mode 100644 index 527d0e0a3..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/gui/ShownotesCleanerTest.java +++ /dev/null @@ -1,236 +0,0 @@ -package de.danoeh.antennapod.core.util.gui; - -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/core/src/test/java/de/danoeh/antennapod/core/util/syndication/FeedDiscovererTest.java b/core/src/test/java/de/danoeh/antennapod/core/util/syndication/FeedDiscovererTest.java deleted file mode 100644 index 3df5230cc..000000000 --- a/core/src/test/java/de/danoeh/antennapod/core/util/syndication/FeedDiscovererTest.java +++ /dev/null @@ -1,128 +0,0 @@ -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 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); - } -} |