diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java new file mode 100644 index 0000000000..0e43a7f9e7 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultFiltersTest.java @@ -0,0 +1,422 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.schabi.newpipe.extractor.search.filter.FilterContainer; +import org.schabi.newpipe.extractor.search.filter.FilterGroup; +import org.schabi.newpipe.extractor.search.filter.FilterItem; +import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +public abstract class DefaultFiltersTest { + + /** + * Each derived class has to implement {@link #setupPriorTesting()}. + * Its return value will initialize this variable. + */ + protected SearchFiltersBase searchFilterBase; + + /** + * Set the {@link #genericTester(List, boolean)} into result display mode instead of asserting. + * + * + */ + protected boolean doNotCallAssertButShowResult = false; + + /** + * Setup the to be tested class. It has to be derived from {@link SearchFiltersBase}. + * + * Additional set up should also occur here. + * + * @return the to be tested derived {@link SearchFiltersBase} object. + */ + protected abstract SearchFiltersBase setupPriorTesting(); + + /** + * Setup for Test {@link #emptyContentFilterTest()} + * + * @param base the object to set up. + * @param emptyContentFilterExpectation + */ + protected abstract void emptyContentFilterTestSetup( + SearchFiltersBase base, + StringBuilder emptyContentFilterExpectation); + + @BeforeEach + void setUp() { + searchFilterBase = setupPriorTesting(); + } + + @AfterEach + void tearDown() { + searchFilterBase = null; + } + + /** + * Test {@link SearchFiltersBase#evaluateSelectedFilters(String)} with no content filters set. + */ + @Test + void emptyContentFilterTest() { + final StringBuilder emptyContentFilterTestExpectation = new StringBuilder(); + emptyContentFilterTestSetup(searchFilterBase, emptyContentFilterTestExpectation); + final String result = searchFilterBase.evaluateSelectedFilters("test"); + assertEquals(emptyContentFilterTestExpectation.toString(), result); + } + + @Test + void noSortFilterItemShouldBeNull() { + final List sortFilters = getAllSortFiltersList(); + for (final FilterItem item : sortFilters) { + assertNotNull(item); + assertNotNull(item.getName()); + } + } + + @Test + void noContentFilterItemShouldBeNull() { + final List contentFilters = getAllContentFiltersList(); + checkThatNoFilterItemAndItsNameIsNull(contentFilters); + } + + @Test + void validContentFilterTest() { + final List cfInputAndExpectedData = new ArrayList<>(); + // get input and expected result data + validContentFilterSetup(cfInputAndExpectedData); + + genericTester(cfInputAndExpectedData, doNotCallAssertButShowResult); + } + + protected abstract void validContentFilterSetup( + List validContentFiltersAndExpectedResults); + + /** + * {@link #validContentFilterAllSortFiltersTestSetup(List)}} + */ + @Test + void validContentFilterAndSortFiltersTest() { + final List validSortFiltersAndExpectedResults = new ArrayList<>(); + validContentFilterAllSortFiltersTestSetup(validSortFiltersAndExpectedResults); + + genericTester(validSortFiltersAndExpectedResults, doNotCallAssertButShowResult); + } + + /** + * For the list with {@link InputAndExpectedResultData} each entry should contain the same + * content filter but each entry a different sort filter. + * + * @param validContentFilterAllSortFiltersExpectedResults the list with data described above + */ + protected abstract void validContentFilterAllSortFiltersTestSetup( + List validContentFilterAllSortFiltersExpectedResults); + + @Test + void validSortFiltersTest() { + final List validAllSortFilters = new ArrayList<>(); + validAllSortFilterSetup(validAllSortFilters); + + genericTester(validAllSortFilters, doNotCallAssertButShowResult); + } + + protected abstract void validAllSortFilterSetup( + List validAllSortFilters); + + /** + * {@link #validContentFilterWithAllSortFiltersTestSetup(List)}} + */ + @Test + void validContentFilterWithAllSortFiltersTest() { + final List validContentFiltersWithExpectedResult = + new ArrayList<>(); + validContentFilterWithAllSortFiltersTestSetup( + validContentFiltersWithExpectedResult); + + genericTester(validContentFiltersWithExpectedResult, doNotCallAssertButShowResult); + } + + /** + * One {@link InputAndExpectedResultData} should consist of one content filter and all + * sort filters possible. Next entry should have different content filter, etc. + * + * @param validContentFiltersWithExpectedResult the list with data described above + */ + protected abstract void validContentFilterWithAllSortFiltersTestSetup( + List validContentFiltersWithExpectedResult); + + /** + * Test if content filter that claim to have corresponding + * sort filters are true to their claim. + *

+ * We need to setup {@link #contentFiltersThatHaveCorrespondingSortFiltersTestSetup(List)} + */ + @Test + void contentFiltersWithSortFilterTest() { + final FilterContainer filterContainer = searchFilterBase.getContentFilters(); + + final List contentFiltersThatHaveCorrespondingSortFilters = new ArrayList<>(); + contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + contentFiltersThatHaveCorrespondingSortFilters); + + for (final FilterGroup cfGroup : filterContainer.getFilterGroups()) { + if (cfGroup.getFilterItems().length > 0) { + // if below id is having a sortFilterVariant it should be the same + // variant than the one placed as superset to this group + final int id = cfGroup.getFilterItems()[0].getIdentifier(); + if (contentFiltersThatHaveCorrespondingSortFilters.contains(id)) { + final FilterContainer sortFilterVariant = + searchFilterBase.getContentFilterSortFilterVariant(id); + assertEquals(sortFilterVariant, cfGroup.getAllSortFilters()); + } + } + + for (final FilterItem item : cfGroup.getFilterItems()) { + final FilterContainer sortFilterVariant = + searchFilterBase.getContentFilterSortFilterVariant(item.getIdentifier()); + if (contentFiltersThatHaveCorrespondingSortFilters.contains(item.getIdentifier())) { + assertNotNull(sortFilterVariant); + assertNotNull(cfGroup.getAllSortFilters()); + checkThatNoFilterItemAndItsNameIsNull(getAllFiltersList(sortFilterVariant)); + checkThatNoFilterItemAndItsNameIsNull( + getAllFiltersList(cfGroup.getAllSortFilters())); + } else { + assertNull(sortFilterVariant); + } + } + } + } + + protected abstract void contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + List contentFiltersThatHaveCorrespondingSortFilters); + + // helpers + private void checkThatNoFilterItemAndItsNameIsNull(final List filterItems) { + for (final FilterItem item : filterItems) { + assertNotNull(item); + assertNotNull(item.getName()); + } + } + + protected void generateAllSortFiltersList() { + searchFilterBase.setSelectedSortFilter(getAllSortFiltersList()); + } + + protected List getAllSortFiltersList() { + final FilterContainer contentFilterContainer = searchFilterBase.getContentFilters(); + final List filterItems = new ArrayList<>(); + // for all content filter groups count available corresponding sort filters + for (final FilterGroup group : contentFilterContainer.getFilterGroups()) { + final FilterContainer sortFilterContainer = group.getAllSortFilters(); + if (null != sortFilterContainer) { + for (final FilterGroup filterGroup : sortFilterContainer.getFilterGroups()) { + filterItems.addAll(Arrays.asList(filterGroup.getFilterItems())); + } + } + } + return filterItems; + } + + protected List getAllSortFiltersIdsList() { + final FilterContainer contentFilterContainer = searchFilterBase.getContentFilters(); + final List filterItems = new ArrayList<>(); + for (final FilterGroup group : contentFilterContainer.getFilterGroups()) { + final FilterContainer sortFilterContainer = group.getAllSortFilters(); + if (null != sortFilterContainer) { + for (final FilterGroup filterGroup : sortFilterContainer.getFilterGroups()) { + for (final FilterItem item : filterGroup.getFilterItems()) { + filterItems.add(item.getIdentifier()); + } + } + } + } + return filterItems; + } + + protected List getAllContentFiltersList() { + return getAllFiltersList(searchFilterBase.getContentFilters()); + } + + protected List getAllFiltersList(final FilterContainer filterContainer) { + final List filterItemList = new ArrayList<>(); + for (final FilterGroup cfGroup : filterContainer.getFilterGroups()) { + filterItemList.addAll(Arrays.asList(cfGroup.getFilterItems())); + } + return filterItemList; + } + + protected List getAllFiltersList(final List filterContainers) { + final List filterItemList = new ArrayList<>(); + for (final FilterContainer filterContainer : filterContainers) { + filterItemList.addAll(getAllFiltersList(filterContainer)); + } + return filterItemList; + } + + protected int getNoOfFilterItems(final List filterContainers) { + return filterContainers.stream() + .mapToInt(DefaultSearchExtractorTest::getNoOfFilterItems).sum(); + } + + /** + * A generic test method to make testing the various combinations easier. + * + * @param inputAndExpectedResultDataList the input test and expected result data + * @param showMode if true it will display the generated result. If false + * the asserts will be called (normal test behaviour) + */ + protected void genericTester( + final List inputAndExpectedResultDataList, + final boolean showMode) { + + for (final InputAndExpectedResultData testData : inputAndExpectedResultDataList) { + + // (1) create input filter data + final List contentFilterItems = new ArrayList<>(); + final List sortFilterItems = new ArrayList<>(); + if (testData.testContentFilters != null) { + for (final Integer id : testData.testContentFilters) { + contentFilterItems.add(searchFilterBase.getFilterItem(id)); + } + } + if (testData.testSortFilters != null) { + for (final Integer id : testData.testSortFilters) { + sortFilterItems.add(searchFilterBase.getFilterItem(id)); + } + } + + // (2) set input filter data + searchFilterBase.setSelectedContentFilter(contentFilterItems); + searchFilterBase.setSelectedSortFilter(sortFilterItems); + + // (3) evaluate data + String result = genericTesterEvaluator(testData); + if (testData.resultFixer != null) { + result = testData.resultFixer.findAndReplace(result); + } + // (4.1) run additional tests for content filters if available + if (testData.testContentFilters != null) { + for (final Integer id : testData.testContentFilters) { + if (testData.additionalContentResultChecker != null) { + testData.additionalContentResultChecker + .checkFilterItem(searchFilterBase.getFilterItem(id), showMode); + } + } + } + // (4.2) run additional tests for sort filters if available + if (testData.testSortFilters != null) { + for (final Integer id : testData.testSortFilters) { + if (testData.additionalSortResultChecker != null) { + testData.additionalSortResultChecker + .checkFilterItem(searchFilterBase.getFilterItem(id), showMode); + } + } + } + if (showMode) { + System.out.println("result=\"" + result + "\""); + } else if (testData.expectedResult != null) { + assertEquals(testData.expectedResult, result); + } + } + } + + /** + * The actual evaluation of the filters. + *

+ * Some services use different methods so override in the derived class. + * + * @param testData the input testdata + * @return the result of the test + */ + protected String genericTesterEvaluator(final InputAndExpectedResultData testData) { + return searchFilterBase.evaluateSelectedFilters(testData.searchString); + } + + protected abstract static class AdditionalResultChecker { + public abstract void checkFilterItem(FilterItem filterItem, boolean showMode); + } + + protected static class InputAndExpectedResultData { + + public final AdditionalResultChecker additionalContentResultChecker; + public final AdditionalResultChecker additionalSortResultChecker; + public final String searchString; + public final List testContentFilters; + public final List testSortFilters; + /** + * Note: if null this expectedResult will not be asserted. -> Test will pass. + */ + public final String expectedResult; + public final ResultFixer resultFixer; + + public InputAndExpectedResultData( + final String searchString, + final List testContentFilters, + final List testSortFilters, + final String expectedResult, + final AdditionalResultChecker additionalContentResultChecker, + final AdditionalResultChecker additionalSortResultChecker) { + this(searchString, + testContentFilters, + testSortFilters, + expectedResult, + additionalContentResultChecker, + additionalSortResultChecker, + new ResultFixer()); + } + + public InputAndExpectedResultData( + final String searchString, + final List testContentFilters, + final List testSortFilters, + final String expectedResult, + final AdditionalResultChecker additionalContentResultChecker, + final AdditionalResultChecker additionalSortResultChecker, + final ResultFixer resultFixer) { + this.searchString = searchString; + this.testContentFilters = testContentFilters; + this.testSortFilters = testSortFilters; + this.expectedResult = expectedResult; + this.additionalContentResultChecker = additionalContentResultChecker; + this.additionalSortResultChecker = additionalSortResultChecker; + this.resultFixer = resultFixer; + } + } + + public static class ResultFixer { + public Map searchAndReplaceMap = new HashMap<>(); + + /** + * Make results match expected value. + *

+ * Eg. Peertube calculates in {@link org.schabi.newpipe.extractor.services.peertube.search.filter.PeertubeFilters.PeertubePublishedDateFilterItem} + * the date to filter when something was published. So we have each moment we call a test + * problems with the expected results. This method tries to fix that by + * reading {@link #searchAndReplaceMap} search and replace data. + * + * @param result the string which should be altered + * @return the altered string + */ + @SuppressWarnings("checkstyle:FinalParameters") + String findAndReplace(String result) { + for (final Map.Entry entry : searchAndReplaceMap.entrySet()) { + result = result.replaceAll(entry.getKey(), entry.getValue()); + } + return result; + } + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/search/filter/BandcampFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/search/filter/BandcampFiltersTest.java new file mode 100644 index 0000000000..59b0993b28 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/bandcamp/search/filter/BandcampFiltersTest.java @@ -0,0 +1,96 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.bandcamp.search.filter; + +import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; +import org.schabi.newpipe.extractor.services.DefaultFiltersTest; + +import java.util.List; + +import static java.util.Collections.singletonList; + +class BandcampFiltersTest extends DefaultFiltersTest { + + @Override + protected SearchFiltersBase setupPriorTesting() { + doNotCallAssertButShowResult = false; + return new BandcampFilters(); + } + + @Override + protected String genericTesterEvaluator(final InputAndExpectedResultData testData) { + return searchFilterBase.evaluateSelectedContentFilters(); + } + + @Override + protected void emptyContentFilterTestSetup( + final SearchFiltersBase base, + final StringBuilder emptyContentFilterExpectation) { + } + + @Override + protected void validContentFilterSetup( + final List validContentFiltersAndExpectedResults) { + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(BandcampFilters.ID_CF_MAIN_ALL), + null, + "", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(BandcampFilters.ID_CF_MAIN_ARTISTS), + null, + "&item_type=b", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(BandcampFilters.ID_CF_MAIN_ALBUMS), + null, + "&item_type=a", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(BandcampFilters.ID_CF_MAIN_TRACKS), + null, + "&item_type=t", + null, + null, + null + )); + } + + @Override + protected void validContentFilterAllSortFiltersTestSetup( + final List + validContentFilterAllSortFiltersExpectedResults) { + // we have no sort filters for this service + } + + @Override + protected void validAllSortFilterSetup( + final List validAllSortFilters) { + // we have no sort filters for this service + } + + @Override + protected void validContentFilterWithAllSortFiltersTestSetup( + final List validContentFiltersWithExpectedResult) { + // we have no sort filters for this service + } + + @Override + protected void contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + final List contentFiltersThatHaveCorrespondingSortFilters) { + // we have no sort filters for this service + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/search/filter/MediaCCCFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/search/filter/MediaCCCFiltersTest.java new file mode 100644 index 0000000000..3bdc599e69 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/media_ccc/search/filter/MediaCCCFiltersTest.java @@ -0,0 +1,82 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.media_ccc.search.filter; + +import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; +import org.schabi.newpipe.extractor.services.DefaultFiltersTest; + +import java.util.List; + +import static java.util.Collections.singletonList; + +class MediaCCCFiltersTest extends DefaultFiltersTest { + + @Override + protected SearchFiltersBase setupPriorTesting() { + doNotCallAssertButShowResult = false; + return new MediaCCCFilters(); + } + + @Override + protected void emptyContentFilterTestSetup( + final SearchFiltersBase base, + final StringBuilder emptyContentFilterExpectation) { + } + + @Override + protected void validContentFilterSetup( + final List validContentFiltersAndExpectedResults) { + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(MediaCCCFilters.ID_CF_MAIN_ALL), + null, + "", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(MediaCCCFilters.ID_CF_MAIN_CONFERENCES), + null, + "", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(MediaCCCFilters.ID_CF_MAIN_EVENTS), + null, + "", + null, + null, + null + )); + } + + @Override + protected void validContentFilterAllSortFiltersTestSetup( + final List + validContentFilterAllSortFiltersExpectedResults) { + // we have no sort filters for this service + } + + @Override + protected void validAllSortFilterSetup( + final List validAllSortFilters) { + // we have no sort filters for this service + } + + @Override + protected void validContentFilterWithAllSortFiltersTestSetup( + final List validContentFiltersWithExpectedResult) { + // we have no sort filters for this service + } + + @Override + protected void contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + final List contentFiltersThatHaveCorrespondingSortFilters) { + // we have no sort filters for this service + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/filter/PeertubeFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/filter/PeertubeFiltersTest.java new file mode 100644 index 0000000000..540f0b2afe --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/search/filter/PeertubeFiltersTest.java @@ -0,0 +1,813 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.peertube.search.filter; + +import org.junit.jupiter.api.Test; +import org.schabi.newpipe.extractor.search.filter.FilterItem; +import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; +import org.schabi.newpipe.extractor.services.DefaultFiltersTest; + +import java.lang.reflect.Field; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class PeertubeFiltersTest extends DefaultFiltersTest { + + ResultFixer fixResults; + + PeertubeFiltersTest() { + + } + + @Override + protected SearchFiltersBase setupPriorTesting() { + fixResults = new ResultFixer(); + fixResults.searchAndReplaceMap.put("startDate=([0-9:T\\-\\.])*", "startDate=X"); + return new PeertubeFilters(); + } + + @Override + protected void emptyContentFilterTestSetup(final SearchFiltersBase base, + final StringBuilder emptyContentFilterExpectation) { + } + + @Override + protected void validContentFilterWithAllSortFiltersTestSetup( + final List validContentFiltersWithExpectedResult) { + + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_ALL), + getAllSortFiltersIdsList(), + "&sort=match&sort=name&sort=duration&sort=publishedAt&sort=createdAt&sort=views&sort=likes&isLive=true&isLive=false&nsfw=true&nsfw=false&startDate=X&startDate=X&startDate=X&startDate=X&durationMax=240&durationMin=240&durationMax=600&durationMin=600", + null, + null, + fixResults + )); + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + getAllSortFiltersIdsList(), + "&sort=match&sort=name&sort=duration&sort=publishedAt&sort=createdAt&sort=views&sort=likes&isLive=true&isLive=false&nsfw=true&nsfw=false&startDate=X&startDate=X&startDate=X&startDate=X&durationMax=240&durationMin=240&durationMax=600&durationMin=600&resultType=videos", + null, + null, + fixResults + )); + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_CHANNELS), + getAllSortFiltersIdsList(), + "&sort=match&sort=name&sort=duration&sort=publishedAt&sort=createdAt&sort=views&sort=likes&isLive=true&isLive=false&nsfw=true&nsfw=false&startDate=X&startDate=X&startDate=X&startDate=X&durationMax=240&durationMin=240&durationMax=600&durationMin=600&resultType=channels", + null, + null, + fixResults + )); + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_PLAYLISTS), + getAllSortFiltersIdsList(), + "&sort=match&sort=name&sort=duration&sort=publishedAt&sort=createdAt&sort=views&sort=likes&isLive=true&isLive=false&nsfw=true&nsfw=false&startDate=X&startDate=X&startDate=X&startDate=X&durationMax=240&durationMin=240&durationMax=600&durationMin=600&resultType=playlists", + null, + null, + fixResults + )); + } + + @Override + protected void validContentFilterSetup( + final List validContentFiltersAndExpectedResults) { + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_ALL), + null, + "", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + null, + "&resultType=videos", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_CHANNELS), + null, + "&resultType=channels", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_PLAYLISTS), + null, + "&resultType=playlists", + null, + null, + null + )); + } + + @Override + protected void validAllSortFilterSetup( + final List validAllSortFilters) { + + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_RELEVANCE), + "&sort=-match", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_NAME), + "&sort=-name", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_DURATION), + "&sort=-duration", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_PUBLISH_DATE), + "&sort=-publishedAt", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_CREATION_DATE), + "&sort=-createdAt", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_VIEWS), + "&sort=-views", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SORT_BY_LIKES), + "&sort=-likes", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_KIND_ALL), + "", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_KIND_LIVE), + "&isLive=true", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_KIND_VOD_VIDEOS), + "&isLive=false", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SENSITIVE_ALL), + "", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SENSITIVE_YES), + "&nsfw=true", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_SENSITIVE_NO), + "&nsfw=false", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_ALL), + "", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_TODAY), + "&startDate=X", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_7_DAYS), + "&startDate=X", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_30_DAYS), + "&startDate=X", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_YEAR), + "&startDate=X", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_DURATION_ALL), + "", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_DURATION_SHORT), + "&durationMax=240", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_DURATION_MEDIUM), + "&durationMin=240&durationMax=600", + null, + null, + fixResults + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_DURATION_LONG), + "&durationMin=600", + null, + null, + fixResults + )); + + } + + @Override + protected void validContentFilterAllSortFiltersTestSetup( + final List + validContentFilterAllSortFiltersExpectedResults) { + // We do not include ID_SF_SORT_ORDER_ASCENDING as we test this special in another testcase + // --> validSortFiltersWithAscendingTest() + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_RELEVANCE), + "&sort=-match&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_NAME), + "&sort=-name&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_DURATION), + "&sort=-duration&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_PUBLISH_DATE), + "&sort=-publishedAt&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_CREATION_DATE), + "&sort=-createdAt&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_VIEWS), + "&sort=-views&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SORT_BY_LIKES), + "&sort=-likes&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_KIND_ALL), + "&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_KIND_LIVE), + "&isLive=true&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_KIND_VOD_VIDEOS), + "&isLive=false&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SENSITIVE_ALL), + "&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SENSITIVE_YES), + "&nsfw=true&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_SENSITIVE_NO), + "&nsfw=false&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_PUBLISHED_ALL), + "&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_PUBLISHED_TODAY), + "&startDate=X&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_7_DAYS), + "&startDate=X&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_30_DAYS), + "&startDate=X&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_YEAR), + "&startDate=X&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_DURATION_ALL), + "&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_DURATION_SHORT), + "&durationMax=240&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_DURATION_MEDIUM), + "&durationMin=240&durationMax=600&resultType=videos", + null, + null, + fixResults + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(PeertubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(PeertubeFilters.ID_SF_DURATION_LONG), + "&durationMin=600&resultType=videos", + null, + null, + fixResults + )); + + } + + @Override + protected void contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + final List contentFiltersThatHaveCorrespondingSortFilters) { + + contentFiltersThatHaveCorrespondingSortFilters.add(PeertubeFilters.ID_CF_MAIN_ALL); + contentFiltersThatHaveCorrespondingSortFilters.add(PeertubeFilters.ID_CF_MAIN_VIDEOS); + } + + /** + * Here we test if ID_SF_SORT_ORDER_ASCENDING changes ID_SF_SORT_* accordingly. + *

+ *

    + *
  • In ascending mode we expect eg. ID_SF_SORT_BY_RELEVANCE to result + * in "&sort=match" and not in "&sort=-match". + *
  • Additionally we test some other ID_SF_* to check that nothing changes there. + *
+ */ + @Test + public void validSortFiltersWithAscendingTest() { + + final List validSortFiltersWithAscending = new ArrayList<>(); + + // set input and expected result data + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_RELEVANCE, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=match", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_NAME, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=name", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_DURATION, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=duration", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_PUBLISH_DATE, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=publishedAt", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_CREATION_DATE, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=createdAt", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_VIEWS, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=views", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SORT_BY_LIKES, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&sort=likes", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_KIND_LIVE, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&isLive=true", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_SENSITIVE_NO, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&nsfw=false", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_PUBLISHED_LAST_YEAR, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&startDate=X", + null, + null, + fixResults + )); + validSortFiltersWithAscending.add(new InputAndExpectedResultData( + null, + null, + Arrays.asList( + PeertubeFilters.ID_SF_DURATION_LONG, + PeertubeFilters.ID_SF_SORT_ORDER_ASCENDING + ), + "&durationMin=600", + null, + null, + fixResults + )); + + // run tests + genericTester(validSortFiltersWithAscending, doNotCallAssertButShowResult); + } + + @Test + public void publishedDateCalculationTest() { + + final List publishedDateCalculation = new ArrayList<>(); + + publishedDateCalculation.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_ALL), + null, + null, + new SortFilterCheckDateCalculation(), + null + )); + publishedDateCalculation.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_TODAY), + null, + null, + new SortFilterCheckDateCalculation(), + null + )); + publishedDateCalculation.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_7_DAYS), + null, + null, + new SortFilterCheckDateCalculation(), + null + )); + publishedDateCalculation.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_30_DAYS), + null, + null, + new SortFilterCheckDateCalculation(), + null + )); + publishedDateCalculation.add(new InputAndExpectedResultData( + null, + null, + singletonList(PeertubeFilters.ID_SF_PUBLISHED_LAST_YEAR), + null, + null, + new SortFilterCheckDateCalculation(), + null + )); + + genericTester(publishedDateCalculation, doNotCallAssertButShowResult); + } + + private static class SortFilterCheckDateCalculation extends AdditionalResultChecker { + + private static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS"; + + @Override + public void checkFilterItem(final FilterItem filterItem, + final boolean showMode) { + + if (filterItem instanceof PeertubeFilters.PeertubePublishedDateFilterItem) { + final PeertubeFilters.PeertubePublishedDateFilterItem dateFilterItem = + (PeertubeFilters.PeertubePublishedDateFilterItem) filterItem; + + final String expectedDate = calculateDate(getDays(dateFilterItem)); + + if (!showMode) { + final String result = dateFilterItem.getQueryData(); + if ("".equals(result)) { // the all case -> PeertubeFilters.ID_SF_PUBLISHED_ALL + assertTrue(result.contains(expectedDate)); + } else { + try { // convert to millies since epoch to approx compare + final long expectedDateInMillis = parseDateGetEpochMillis(expectedDate); + final long resultDateInMillis = parseDateGetEpochMillis(result); + assertTrue(Math.abs(expectedDateInMillis - resultDateInMillis) < 2000); + } catch (final ParseException e) { + throw new RuntimeException(e); + } + } + } else { + System.out.println("approx=\"" + expectedDate + "\""); + } + } + } + + private String calculateDate(final int days) { + if (days != PeertubeFilters.PeertubePublishedDateFilterItem.NO_DAYS_SET) { + final LocalDateTime localDateTime = LocalDateTime.now().minusDays(days); + + return "startDate=" + localDateTime.format( + DateTimeFormatter.ofPattern(DATE_PATTERN)); + } else { + return ""; + } + } + + private long parseDateGetEpochMillis(final String inputDate) throws ParseException { + // 10 = length of startDate= + final String date = inputDate.substring(10); + + final SimpleDateFormat fmt = new SimpleDateFormat(DATE_PATTERN); + return fmt.parse(date).getTime(); + } + + private int getDays(final PeertubeFilters.PeertubePublishedDateFilterItem item) { + try { + final Field days = item.getClass().getDeclaredField("days"); + days.setAccessible(true); + return days.getInt(item); + } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) { + throw new RuntimeException("could not find field \"days\""); + } + } + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/filter/SoundcloudFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/filter/SoundcloudFiltersTest.java new file mode 100644 index 0000000000..0a46481705 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/search/filter/SoundcloudFiltersTest.java @@ -0,0 +1,366 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.soundcloud.search.filter; + +import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; +import org.schabi.newpipe.extractor.services.DefaultFiltersTest; + +import java.util.List; + +import static java.util.Collections.singletonList; + +class SoundcloudFiltersTest extends DefaultFiltersTest { + + @Override + protected SearchFiltersBase setupPriorTesting() { + doNotCallAssertButShowResult = false; + return new SoundcloudFilters(); + } + + @Override + protected String genericTesterEvaluator(final InputAndExpectedResultData testData) { + return "(CF)" + + searchFilterBase.evaluateSelectedContentFilters() + + "|(SF)" + + searchFilterBase.evaluateSelectedSortFilters(); + } + + @Override + protected void emptyContentFilterTestSetup( + final SearchFiltersBase base, + final StringBuilder emptyContentFilterExpectation) { + } + + @Override + protected void validContentFilterSetup( + final List validContentFiltersAndExpectedResults) { + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_ALL), + null, + "(CF)|(SF)", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + null, + "(CF)/tracks|(SF)", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_USERS), + null, + "(CF)/users|(SF)", + null, + null, + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_PLAYLISTS), + null, + "(CF)/playlists|(SF)", + null, + null, + null + )); + } + + @Override + protected void validContentFilterAllSortFiltersTestSetup( + final List + validContentFilterAllSortFiltersExpectedResults) { + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DATE_ALL), + "(CF)/tracks|(SF)", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_HOUR), + "(CF)/tracks|(SF)&filter.created_at=last_hour", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_DAY), + "(CF)/tracks|(SF)&filter.created_at=last_day", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_WEEK), + "(CF)/tracks|(SF)&filter.created_at=last_week", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_MONTH), + "(CF)/tracks|(SF)&filter.created_at=last_month", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_YEAR), + "(CF)/tracks|(SF)&filter.created_at=last_year", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DURATION_ALL), + "(CF)/tracks|(SF)", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DURATION_SHORT), + "(CF)/tracks|(SF)&filter.duration=short", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DURATION_MEDIUM), + "(CF)/tracks|(SF)&filter.duration=medium", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DURATION_LONG), + "(CF)/tracks|(SF)&filter.duration=long", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_DURATION_EPIC), + "(CF)/tracks|(SF)&filter.duration=epic", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_LICENSE_ALL), + "(CF)/tracks|(SF)", + null, + null, + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + singletonList(SoundcloudFilters.ID_SF_LICENSE_COMMERCE), + "(CF)/tracks|(SF)&filter.license=to_modify_commercially", + null, + null, + null + )); + } + + @Override + protected void validAllSortFilterSetup( + final List validAllSortFilters) { + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DATE_ALL), + "(CF)|(SF)", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_HOUR), + "(CF)|(SF)&filter.created_at=last_hour", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_DAY), + "(CF)|(SF)&filter.created_at=last_day", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_WEEK), + "(CF)|(SF)&filter.created_at=last_week", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_MONTH), + "(CF)|(SF)&filter.created_at=last_month", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DATE_LAST_YEAR), + "(CF)|(SF)&filter.created_at=last_year", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DURATION_ALL), + "(CF)|(SF)", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DURATION_SHORT), + "(CF)|(SF)&filter.duration=short", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DURATION_MEDIUM), + "(CF)|(SF)&filter.duration=medium", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DURATION_LONG), + "(CF)|(SF)&filter.duration=long", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_DURATION_EPIC), + "(CF)|(SF)&filter.duration=epic", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_LICENSE_ALL), + "(CF)|(SF)", + null, + null, + null + )); + validAllSortFilters.add(new InputAndExpectedResultData( + null, + null, + singletonList(SoundcloudFilters.ID_SF_LICENSE_COMMERCE), + "(CF)|(SF)&filter.license=to_modify_commercially", + null, + null, + null + )); + } + + @Override + protected void validContentFilterWithAllSortFiltersTestSetup( + final List validContentFiltersWithExpectedResult) { + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_ALL), + getAllSortFiltersIdsList(), + "(CF)|(SF)&filter.created_at=last_hour&filter.created_at=last_day&filter.created_at=last_week&filter.created_at=last_month&filter.created_at=last_year&filter.duration=short&filter.duration=medium&filter.duration=long&filter.duration=epic&filter.license=to_modify_commercially", + null, + null, + null + )); + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_TRACKS), + getAllSortFiltersIdsList(), + "(CF)/tracks|(SF)&filter.created_at=last_hour&filter.created_at=last_day&filter.created_at=last_week&filter.created_at=last_month&filter.created_at=last_year&filter.duration=short&filter.duration=medium&filter.duration=long&filter.duration=epic&filter.license=to_modify_commercially", + null, + null, + null + )); + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_USERS), + getAllSortFiltersIdsList(), + "(CF)/users|(SF)&filter.created_at=last_hour&filter.created_at=last_day&filter.created_at=last_week&filter.created_at=last_month&filter.created_at=last_year&filter.duration=short&filter.duration=medium&filter.duration=long&filter.duration=epic&filter.license=to_modify_commercially", + null, + null, + null + )); + validContentFiltersWithExpectedResult.add(new InputAndExpectedResultData( + null, + singletonList(SoundcloudFilters.ID_CF_MAIN_PLAYLISTS), + getAllSortFiltersIdsList(), + "(CF)/playlists|(SF)&filter.created_at=last_hour&filter.created_at=last_day&filter.created_at=last_week&filter.created_at=last_month&filter.created_at=last_year&filter.duration=short&filter.duration=medium&filter.duration=long&filter.duration=epic&filter.license=to_modify_commercially", + null, + null, + null + )); + } + + @Override + protected void contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + final List contentFiltersThatHaveCorrespondingSortFilters) { + contentFiltersThatHaveCorrespondingSortFilters.add(SoundcloudFilters.ID_CF_MAIN_TRACKS); + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFiltersTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFiltersTest.java new file mode 100644 index 0000000000..e9012ca8ef --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeFiltersTest.java @@ -0,0 +1,430 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later + +package org.schabi.newpipe.extractor.services.youtube.search.filter; + +import org.schabi.newpipe.extractor.search.filter.FilterItem; +import org.schabi.newpipe.extractor.search.filter.SearchFiltersBase; +import org.schabi.newpipe.extractor.services.DefaultFiltersTest; + +import java.util.List; + +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class YoutubeFiltersTest extends DefaultFiltersTest { + + @Override + protected SearchFiltersBase setupPriorTesting() { + return new YoutubeFilters(); + } + + @Override + protected void emptyContentFilterTestSetup(final SearchFiltersBase base, + final StringBuilder emptyContentFilterExpectation) { + emptyContentFilterExpectation.append("https://www.youtube.com/results?search_query=test"); + base.setSelectedSortFilter(getAllSortFiltersList()); + } + + @Override + protected void validContentFilterWithAllSortFiltersTestSetup( + final List validContentFiltersWithAllSortFilters) { + + // set all content filters and results + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_ALL), + getAllSortFiltersIdsList(), + "https://www.youtube.com/results?search_query=test&sp=CAMSGwgFGAIgASgBMAE4AUABcAF4AbgBAcgBAdABAQ%3D%3D", + new CheckParam("CAMSGwgFGAIgASgBMAE4AUABcAF4AbgBAcgBAdABAQ%3D%3D"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + getAllSortFiltersIdsList(), + "https://www.youtube.com/results?search_query=test&sp=CAMSHQgFEAEYAiABKAEwATgBQAFwAXgBuAEByAEB0AEB", + new CheckParam("CAMSHQgFEAEYAiABKAEwATgBQAFwAXgBuAEByAEB0AEB"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_CHANNELS), + getAllSortFiltersIdsList(), + "https://www.youtube.com/results?search_query=test&sp=CAMSHQgFEAIYAiABKAEwATgBQAFwAXgBuAEByAEB0AEB", + new CheckParam("CAMSHQgFEAIYAiABKAEwATgBQAFwAXgBuAEByAEB0AEB"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_PLAYLISTS), + getAllSortFiltersIdsList(), + "https://www.youtube.com/results?search_query=test&sp=CAMSHQgFEAMYAiABKAEwATgBQAFwAXgBuAEByAEB0AEB", + new CheckParam("CAMSHQgFEAMYAiABKAEwATgBQAFwAXgBuAEByAEB0AEB"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS), + getAllSortFiltersIdsList(), + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIARAAGAAgACgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS), + getAllSortFiltersIdsList(), + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABABGAAgACgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS), + getAllSortFiltersIdsList(), + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABAAGAEgACgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_PLAYLISTS), + getAllSortFiltersIdsList(), + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABAAGAAgACgBMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersWithAllSortFilters.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ARTISTS), + getAllSortFiltersIdsList(), + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABAAGAAgASgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + } + + @Override + protected void validContentFilterSetup( + final List validContentFiltersAndExpectedResults) { + + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_ALL), + null, + "https://www.youtube.com/results?search_query=test&sp=EgA%3D", + new CheckParam("EgA%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + null, + "https://www.youtube.com/results?search_query=test&sp=EgIQAQ%3D%3D", + new CheckParam("EgIQAQ%3D%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_CHANNELS), + null, + "https://www.youtube.com/results?search_query=test&sp=EgIQAg%3D%3D", + new CheckParam("EgIQAg%3D%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_PLAYLISTS), + null, + "https://www.youtube.com/results?search_query=test&sp=EgIQAw%3D%3D", + new CheckParam("EgIQAw%3D%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS), + null, + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIARAAGAAgACgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS), + null, + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABABGAAgACgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS), + null, + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABAAGAEgACgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_PLAYLISTS), + null, + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABAAGAAgACgBMABqChAEEAUQAxAKEAk%3D"), + null + )); + validContentFiltersAndExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ARTISTS), + null, + "https://music.youtube.com/search?q=test", + new CheckParam("Eg-KAQwIABAAGAAgASgAMABqChAEEAUQAxAKEAk%3D"), + null + )); + } + + @Override + protected void validAllSortFilterSetup( + final List validAllSortFilters) { + // no implementation here for youtube. As we cannot just have sort filters without + // content filters + } + + @Override + protected void validContentFilterAllSortFiltersTestSetup( + final List + validContentFilterAllSortFiltersExpectedResults) { + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_SORT_BY_RELEVANCE), + "https://www.youtube.com/results?search_query=test&sp=CAASAhAB", + new CheckParam("CAASAhAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_SORT_BY_RATING), + "https://www.youtube.com/results?search_query=test&sp=CAESAhAB", + new CheckParam("CAESAhAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_SORT_BY_DATE), + "https://www.youtube.com/results?search_query=test&sp=CAISAhAB", + new CheckParam("CAISAhAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_SORT_BY_VIEWS), + "https://www.youtube.com/results?search_query=test&sp=CAMSAhAB", + new CheckParam("CAMSAhAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_UPLOAD_DATE_ALL), + "https://www.youtube.com/results?search_query=test&sp=EgIQAQ%3D%3D", + new CheckParam("EgIQAQ%3D%3D"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_UPLOAD_DATE_HOUR), + "https://www.youtube.com/results?search_query=test&sp=EgQIARAB", + new CheckParam("EgQIARAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_UPLOAD_DATE_DAY), + "https://www.youtube.com/results?search_query=test&sp=EgQIAhAB", + new CheckParam("EgQIAhAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_UPLOAD_DATE_WEEK), + "https://www.youtube.com/results?search_query=test&sp=EgQIAxAB", + new CheckParam("EgQIAxAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_UPLOAD_DATE_MONTH), + "https://www.youtube.com/results?search_query=test&sp=EgQIBBAB", + new CheckParam("EgQIBBAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_UPLOAD_DATE_YEAR), + "https://www.youtube.com/results?search_query=test&sp=EgQIBRAB", + new CheckParam("EgQIBRAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_DURATION_ALL), + "https://www.youtube.com/results?search_query=test&sp=EgIQAQ%3D%3D", + new CheckParam("EgIQAQ%3D%3D"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_DURATION_SHORT), + "https://www.youtube.com/results?search_query=test&sp=EgQQARgB", + new CheckParam("EgQQARgB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_DURATION_MEDIUM), + "https://www.youtube.com/results?search_query=test&sp=EgQQARgD", + new CheckParam("EgQQARgD"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_DURATION_LONG), + "https://www.youtube.com/results?search_query=test&sp=EgQQARgC", + new CheckParam("EgQQARgC"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_LIVE), + "https://www.youtube.com/results?search_query=test&sp=EgQQAUAB", + new CheckParam("EgQQAUAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_4K), + "https://www.youtube.com/results?search_query=test&sp=EgQQAXAB", + new CheckParam("EgQQAXAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_HD), + "https://www.youtube.com/results?search_query=test&sp=EgQQASAB", + new CheckParam("EgQQASAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_SUBTITLES), + "https://www.youtube.com/results?search_query=test&sp=EgQQASgB", + new CheckParam("EgQQASgB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_CCOMMONS), + "https://www.youtube.com/results?search_query=test&sp=EgQQATAB", + new CheckParam("EgQQATAB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_360), + "https://www.youtube.com/results?search_query=test&sp=EgQQAXgB", + new CheckParam("EgQQAXgB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_VR180), + "https://www.youtube.com/results?search_query=test&sp=EgUQAdABAQ%3D%3D", + new CheckParam("EgUQAdABAQ%3D%3D"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_3D), + "https://www.youtube.com/results?search_query=test&sp=EgQQATgB", + new CheckParam("EgQQATgB"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_HDR), + "https://www.youtube.com/results?search_query=test&sp=EgUQAcgBAQ%3D%3D", + new CheckParam("EgUQAcgBAQ%3D%3D"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_LOCATION), + "https://www.youtube.com/results?search_query=test&sp=EgUQAbgBAQ%3D%3D", + new CheckParam("EgUQAbgBAQ%3D%3D"), + null + )); + validContentFilterAllSortFiltersExpectedResults.add(new InputAndExpectedResultData( + "test", + singletonList(YoutubeFilters.ID_CF_MAIN_VIDEOS), + singletonList(YoutubeFilters.ID_SF_FEATURES_PURCHASED), + "https://www.youtube.com/results?search_query=test&sp=EgQQAUgB", + new CheckParam("EgQQAUgB"), + null + )); + } + + @Override + protected void contentFiltersThatHaveCorrespondingSortFiltersTestSetup( + final List contentFiltersThatHaveCorrespondingSortFilters) { + contentFiltersThatHaveCorrespondingSortFilters.add(YoutubeFilters.ID_CF_MAIN_ALL); + contentFiltersThatHaveCorrespondingSortFilters.add(YoutubeFilters.ID_CF_MAIN_VIDEOS); + contentFiltersThatHaveCorrespondingSortFilters.add(YoutubeFilters.ID_CF_MAIN_CHANNELS); + contentFiltersThatHaveCorrespondingSortFilters.add(YoutubeFilters.ID_CF_MAIN_PLAYLISTS); + } + + static class CheckParam extends AdditionalResultChecker { + private final String expectedResult; + + CheckParam(final String expectedResult) { + this.expectedResult = expectedResult; + } + + @Override + public void checkFilterItem(final FilterItem filterItem, + final boolean showMode) { + if (filterItem instanceof YoutubeFilters.YoutubeContentFilterItem) { + final YoutubeFilters.YoutubeContentFilterItem contentFilterItem = + (YoutubeFilters.YoutubeContentFilterItem) filterItem; + if (showMode) { + System.out.println("extendedResult=\"" + contentFilterItem.getParams() + "\""); + } else { + assertEquals(this.expectedResult, contentFilterItem.getParams()); + } + } else { + throw new RuntimeException("Illegal FilterItem found: " + filterItem.getClass()); + } + } + } +}