diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeMusicSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeMusicSearchExtractorTest.java index 390fcbec13..6e738229b3 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeMusicSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeMusicSearchExtractorTest.java @@ -10,8 +10,10 @@ import org.schabi.newpipe.extractor.NewPipe; import org.schabi.newpipe.extractor.StreamingService; import org.schabi.newpipe.extractor.search.SearchExtractor; +import org.schabi.newpipe.extractor.search.filter.FilterItem; import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory; +import org.schabi.newpipe.extractor.services.youtube.search.filter.YoutubeFilters; import java.net.URLEncoder; @@ -26,7 +28,9 @@ public static class MusicSongs extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -48,7 +52,9 @@ public static class MusicVideos extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_VIDEOS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -70,7 +76,9 @@ public static class MusicAlbums extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_ALBUMS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -92,7 +100,9 @@ public static class MusicPlaylists extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_PLAYLISTS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_PLAYLISTS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -115,7 +125,9 @@ public static class MusicArtists extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_ARTISTS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ARTISTS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -139,7 +151,9 @@ public static class Suggestion extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -163,7 +177,9 @@ public static class CorrectedSearch extends DefaultSearchExtractorTest { @BeforeAll public static void setUp() throws Exception { NewPipe.init(DownloaderTestImpl.getInstance()); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), ""); + final FilterItem item = + getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java index 255fe295d0..1b8ef2c167 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchExtractorTest.java @@ -7,9 +7,6 @@ import static org.schabi.newpipe.extractor.ExtractorAsserts.assertEmptyErrors; import static org.schabi.newpipe.extractor.ServiceList.YouTube; import static org.schabi.newpipe.extractor.services.DefaultTests.assertNoDuplicatedItems; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.CHANNELS; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.PLAYLISTS; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.VIDEOS; import static java.util.Collections.singletonList; import org.junit.jupiter.api.BeforeAll; @@ -24,8 +21,10 @@ import org.schabi.newpipe.extractor.channel.ChannelInfoItem; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.search.SearchExtractor; +import org.schabi.newpipe.extractor.search.filter.FilterItem; import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest; import org.schabi.newpipe.extractor.services.youtube.YoutubeTestsUtils; +import org.schabi.newpipe.extractor.services.youtube.search.filter.YoutubeFilters; import org.schabi.newpipe.extractor.stream.Description; import org.schabi.newpipe.extractor.stream.StreamInfoItem; @@ -72,7 +71,8 @@ public static class Channel extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "channel")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(CHANNELS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_CHANNELS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -96,7 +96,8 @@ public static class Playlists extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "playlist")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(PLAYLISTS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_PLAYLISTS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -120,7 +121,8 @@ public static class Videos extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "videos")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -152,7 +154,8 @@ public static class Suggestion extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "suggestions")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -179,7 +182,8 @@ public static class CorrectedSearch extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "corrected")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -238,7 +242,10 @@ public static class PagingTest { public void duplicatedItemsCheck() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "paging")); - final SearchExtractor extractor = YouTube.getSearchExtractor("cirque du soleil", singletonList(VIDEOS), ""); + + final FilterItem item = DefaultSearchExtractorTest.getFilterItem( + YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + final SearchExtractor extractor = YouTube.getSearchExtractor("cirque du soleil", singletonList(item), null); extractor.fetchPage(); final ListExtractor.InfoItemsPage page1 = extractor.getInitialPage(); @@ -256,7 +263,8 @@ public static class MetaInfoTest extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "metaInfo")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -291,7 +299,9 @@ public static class ChannelVerified extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "verified")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(CHANNELS), ""); + + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_CHANNELS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -328,7 +338,8 @@ public static class VideoUploaderAvatar extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "video_uploader_avatar")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } @@ -359,7 +370,8 @@ public static class VideoDescription extends DefaultSearchExtractorTest { public static void setUp() throws Exception { YoutubeTestsUtils.ensureStateless(); NewPipe.init(DownloaderFactory.getDownloader(RESOURCE_PATH + "video_description")); - extractor = YouTube.getSearchExtractor(QUERY, singletonList(VIDEOS), ""); + final FilterItem item = getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + extractor = YouTube.getSearchExtractor(QUERY, singletonList(item), null); extractor.fetchPage(); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java index 312a49cde3..4815de1d82 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchQHTest.java @@ -1,17 +1,30 @@ package org.schabi.newpipe.extractor.services.youtube.search; +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.services.DefaultSearchExtractorTest; +import org.schabi.newpipe.extractor.services.youtube.search.filter.YoutubeFilters; + +import java.util.Arrays; +import java.util.List; + +import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.schabi.newpipe.extractor.ServiceList.YouTube; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.CHANNELS; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.MUSIC_SONGS; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.PLAYLISTS; -import static org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory.VIDEOS; -import static java.util.Arrays.asList; - -import org.junit.jupiter.api.Test; public class YoutubeSearchQHTest { + public static int getNoOfFilterItems(final List filterContainers) { + return filterContainers.stream() + .mapToInt(filterContainer -> Arrays.stream(filterContainer.getFilterGroups()) + .map(FilterGroup::getFilterItems) + .mapToInt(filterItems -> filterItems.length) + .sum()).sum(); + } + @Test public void testRegularValues() throws Exception { assertEquals("https://www.youtube.com/results?search_query=asdf", YouTube.getSearchQHFactory().fromQuery("asdf").getUrl()); @@ -20,56 +33,73 @@ public void testRegularValues() throws Exception { assertEquals("https://www.youtube.com/results?search_query=G%C3%BCl%C3%BCm", YouTube.getSearchQHFactory().fromQuery("Gülüm").getUrl()); assertEquals("https://www.youtube.com/results?search_query=%3Fj%24%29H%C2%A7B", YouTube.getSearchQHFactory().fromQuery("?j$)H§B").getUrl()); - assertEquals("https://music.youtube.com/search?q=asdf", YouTube.getSearchQHFactory().fromQuery("asdf", asList(new String[]{MUSIC_SONGS}), "").getUrl()); - assertEquals("https://music.youtube.com/search?q=hans", YouTube.getSearchQHFactory().fromQuery("hans", asList(new String[]{MUSIC_SONGS}), "").getUrl()); - assertEquals("https://music.youtube.com/search?q=Poifj%26jaijf", YouTube.getSearchQHFactory().fromQuery("Poifj&jaijf", asList(new String[]{MUSIC_SONGS}), "").getUrl()); - assertEquals("https://music.youtube.com/search?q=G%C3%BCl%C3%BCm", YouTube.getSearchQHFactory().fromQuery("Gülüm", asList(new String[]{MUSIC_SONGS}), "").getUrl()); - assertEquals("https://music.youtube.com/search?q=%3Fj%24%29H%C2%A7B", YouTube.getSearchQHFactory().fromQuery("?j$)H§B", asList(new String[]{MUSIC_SONGS}), "").getUrl()); + final FilterItem item = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS); + assertEquals("https://music.youtube.com/search?q=asdf", YouTube.getSearchQHFactory().fromQuery("asdf", singletonList(item), null).getUrl()); + assertEquals("https://music.youtube.com/search?q=hans", YouTube.getSearchQHFactory().fromQuery("hans", singletonList(item), null).getUrl()); + assertEquals("https://music.youtube.com/search?q=Poifj%26jaijf", YouTube.getSearchQHFactory().fromQuery("Poifj&jaijf", singletonList(item), null).getUrl()); + assertEquals("https://music.youtube.com/search?q=G%C3%BCl%C3%BCm", YouTube.getSearchQHFactory().fromQuery("Gülüm", singletonList(item), null).getUrl()); + assertEquals("https://music.youtube.com/search?q=%3Fj%24%29H%C2%A7B", YouTube.getSearchQHFactory().fromQuery("?j$)H§B", singletonList(item), null).getUrl()); } @Test public void testGetContentFilter() throws Exception { - assertEquals(VIDEOS, YouTube.getSearchQHFactory() - .fromQuery("", asList(new String[]{VIDEOS}), "").getContentFilters().get(0)); - assertEquals(CHANNELS, YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{CHANNELS}), "").getContentFilters().get(0)); + final FilterItem videoFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + final FilterItem channelsFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_CHANNELS); + assertEquals(YoutubeFilters.ID_CF_MAIN_VIDEOS, YouTube.getSearchQHFactory() + .fromQuery("", singletonList(videoFilterItem), null).getContentFilters().get(0).getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_CHANNELS, YouTube.getSearchQHFactory() + .fromQuery("asdf", singletonList(channelsFilterItem), null).getContentFilters().get(0).getIdentifier()); - assertEquals(MUSIC_SONGS, YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{MUSIC_SONGS}), "").getContentFilters().get(0)); + final FilterItem musicSongsFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS, YouTube.getSearchQHFactory() + .fromQuery("asdf", singletonList(musicSongsFilterItem), null).getContentFilters().get(0).getIdentifier()); } @Test public void testWithContentfilter() throws Exception { - assertEquals("https://www.youtube.com/results?search_query=asdf&sp=EgIQAQ%253D%253D", YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{VIDEOS}), "").getUrl()); - assertEquals("https://www.youtube.com/results?search_query=asdf&sp=EgIQAg%253D%253D", YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{CHANNELS}), "").getUrl()); - assertEquals("https://www.youtube.com/results?search_query=asdf&sp=EgIQAw%253D%253D", YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{PLAYLISTS}), "").getUrl()); + final FilterItem videoFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_VIDEOS); + final FilterItem channelsFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_CHANNELS); + final FilterItem playlistsFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_PLAYLISTS); + final FilterItem musicSongsFilterItem = DefaultSearchExtractorTest.getFilterItem(YouTube, YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS); + assertEquals("https://www.youtube.com/results?search_query=asdf&sp=EgIQAQ%3D%3D", YouTube.getSearchQHFactory() + .fromQuery("asdf", singletonList(videoFilterItem), null).getUrl()); + assertEquals("https://www.youtube.com/results?search_query=asdf&sp=EgIQAg%3D%3D", YouTube.getSearchQHFactory() + .fromQuery("asdf", singletonList(channelsFilterItem), null).getUrl()); + assertEquals("https://www.youtube.com/results?search_query=asdf&sp=EgIQAw%3D%3D", YouTube.getSearchQHFactory() + .fromQuery("asdf", singletonList(playlistsFilterItem), null).getUrl()); assertEquals("https://www.youtube.com/results?search_query=asdf", YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{"fjiijie"}), "").getUrl()); + .fromQuery("asdf", singletonList(null), null).getUrl()); assertEquals("https://music.youtube.com/search?q=asdf", YouTube.getSearchQHFactory() - .fromQuery("asdf", asList(new String[]{MUSIC_SONGS}), "").getUrl()); + .fromQuery("asdf", singletonList(musicSongsFilterItem), null).getUrl()); } @Test public void testGetAvailableContentFilter() { - final String[] contentFilter = YouTube.getSearchQHFactory().getAvailableContentFilter(); - assertEquals(8, contentFilter.length); - assertEquals("all", contentFilter[0]); - assertEquals("videos", contentFilter[1]); - assertEquals("channels", contentFilter[2]); - assertEquals("playlists", contentFilter[3]); - assertEquals("music_songs", contentFilter[4]); - assertEquals("music_videos", contentFilter[5]); - assertEquals("music_albums", contentFilter[6]); - assertEquals("music_playlists", contentFilter[7]); + final FilterContainer contentFilter = + YouTube.getSearchQHFactory().getAvailableContentFilter(); + + final int noOfContentFilters = DefaultSearchExtractorTest.getNoOfFilterItems(contentFilter); + final FilterItem[] filterItems = contentFilter.getFilterGroups()[0].getFilterItems(); + assertEquals(10, noOfContentFilters); + assertEquals(YoutubeFilters.ID_CF_MAIN_ALL, filterItems[0].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_VIDEOS, filterItems[1].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_CHANNELS, filterItems[2].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_PLAYLISTS, filterItems[3].getIdentifier()); + assertTrue(filterItems[4] instanceof FilterItem.DividerItem); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_SONGS, filterItems[5].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_VIDEOS, filterItems[6].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ALBUMS, filterItems[7].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_PLAYLISTS, filterItems[8].getIdentifier()); + assertEquals(YoutubeFilters.ID_CF_MAIN_YOUTUBE_MUSIC_ARTISTS, filterItems[9].getIdentifier()); } @Test public void testGetAvailableSortFilter() { - final String[] contentFilter = YouTube.getSearchQHFactory().getAvailableSortFilter(); - assertEquals(0, contentFilter.length); + final FilterContainer contentFilterContainer = + YouTube.getSearchQHFactory().getAvailableContentFilter(); + final int noOfSortFilters = + DefaultSearchExtractorTest.getNoOfSortFilterItems(contentFilterContainer); + assertEquals(24, noOfSortFilters); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeProtoBufferSearchParameterAccessorTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeProtoBufferSearchParameterAccessorTest.java new file mode 100644 index 0000000000..f5e1f875c3 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/filter/YoutubeProtoBufferSearchParameterAccessorTest.java @@ -0,0 +1,259 @@ +// Created by evermind-zz 2022, licensed GNU GPL version 3 or later --> + +package org.schabi.newpipe.extractor.services.youtube.search.filter; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.schabi.newpipe.extractor.services.youtube.search.filter.protobuf.DateFilter; +import org.schabi.newpipe.extractor.services.youtube.search.filter.protobuf.Features; +import org.schabi.newpipe.extractor.services.youtube.search.filter.protobuf.LengthFilter; +import org.schabi.newpipe.extractor.services.youtube.search.filter.protobuf.SearchRequest; +import org.schabi.newpipe.extractor.services.youtube.search.filter.protobuf.SortOrder; +import org.schabi.newpipe.extractor.services.youtube.search.filter.protobuf.TypeFilter; + +import java.io.IOException; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +class YoutubeProtoBufferSearchParameterAccessorTest { + + final DateFilter[] dateFilters = { + DateFilter.hour, + DateFilter.day, + DateFilter.week, + DateFilter.month, + DateFilter.year + }; + final SortOrder[] sortOrders = { + SortOrder.relevance, + SortOrder.rating, + SortOrder.date, + SortOrder.views + }; + final LengthFilter[] lengthFilters = { + LengthFilter.duration_short, + LengthFilter.duration_long, + LengthFilter.duration_medium + }; + final TypeFilter[] typeFilters = { + TypeFilter.video, + TypeFilter.channel, + TypeFilter.playlist, + TypeFilter.movie, + TypeFilter.show + }; + final Features[] features = { + Features.live, + Features.is_4k, + Features.is_hd, + Features.subtitles, + Features.ccommons, + Features.is_360, + Features.is_vr180, + Features.is_3d, + Features.is_hdr, + Features.location, + Features.purchased + }; + YoutubeProtoBufferSearchParameterAccessor.Builder spBuilder; + + @BeforeEach + void setUp() { + spBuilder = new YoutubeProtoBufferSearchParameterAccessor.Builder(); + } + + @AfterEach + void tearDown() { + spBuilder = null; + } + + @Test + void dateFilterTest() throws IOException { + + for (final DateFilter dateFilter : dateFilters) { + spBuilder.setDateFilter(dateFilter); + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + assertEquals(dateFilter.getValue(), decodedSp.filter.date); + } + } + + @Test + void sortFilterTest() throws IOException { + for (final SortOrder sortOrder : sortOrders) { + spBuilder.setSortOrder(sortOrder); + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + assertEquals(sortOrder.getValue(), decodedSp.sorted); + } + } + + @Test + void typeFilterTest() throws IOException { + for (final TypeFilter type : typeFilters) { + spBuilder.setTypeFilter(type); + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + assertEquals(type.getValue(), decodedSp.filter.type); + } + } + + @Test + void lengthFilterTest() throws IOException { + for (final LengthFilter length : lengthFilters) { + spBuilder.setLengthFilter(length); + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + assertEquals(length.getValue(), decodedSp.filter.length); + } + } + + // All filters/features disabled. + @Test + void noneDateSortTypeLengthFeaturesSetTest() throws IOException { + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + + assertNull(decodedSp.filter.date); + assertNull(decodedSp.filter.type); + assertNull(decodedSp.sorted); + assertNull(decodedSp.filter.length); + for (final Features feature : features) { + assertEquals(false, getFeatureState(feature, decodedSp)); + } + } + + @Test + void featuresOneAtATimeTest() throws IOException { + for (final Features feature : features) { + spBuilder.addFeature(feature); + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + assertEquals(true, getFeatureState(feature, decodedSp)); + spBuilder = new YoutubeProtoBufferSearchParameterAccessor.Builder(); + } + } + + @Test + void allFeaturesSetTest() throws IOException { + for (final Features feature : features) { + spBuilder.addFeature(feature); + } + + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + + for (final Features feature : features) { + assertEquals(true, getFeatureState(feature, decodedSp)); + } + } + + @Test + void allOptionsSelectedOnceTesting() throws IOException { + for (final Features feature : features) { + spBuilder = new YoutubeProtoBufferSearchParameterAccessor.Builder(); + spBuilder.addFeature(feature); + + for (final SortOrder sortOrder : sortOrders) { + spBuilder.setSortOrder(sortOrder); + + for (final TypeFilter type : typeFilters) { + spBuilder.setTypeFilter(type); + + for (final LengthFilter length : lengthFilters) { + spBuilder.setLengthFilter(length); + + for (final DateFilter dateFilter : dateFilters) { + spBuilder.setDateFilter(dateFilter); + final YoutubeProtoBufferSearchParameterAccessor what = + spBuilder.build(); + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + + assertEquals(dateFilter.getValue(), decodedSp.filter.date); + assertEquals(type.getValue(), decodedSp.filter.type); + assertEquals(sortOrder.getValue(), decodedSp.sorted); + assertEquals(length.getValue(), decodedSp.filter.length); + assertEquals(true, getFeatureState(feature, decodedSp)); + } + } + } + } + } + } + + @Test + @Disabled + // that is not a real test case but to evaluate new features + void oneFeaturesSetTest() throws IOException { + spBuilder.addFeature(Features.is_vr180); + + final YoutubeProtoBufferSearchParameterAccessor what = spBuilder.build(); + //final String encodedSp = "EgPQAQE%3D"; + final String encodedSp = what.getSp(); + final SearchRequest decodedSp = what.decodeSp(encodedSp); + } + + // helpers + private Boolean getFeatureState(final Features feature, final SearchRequest decodedSp) { + Optional state = Optional.empty(); + switch (feature) { + case live: + state = Optional.of(ifNullThanFalse(decodedSp.filter.live)); + break; + case is_4k: + state = Optional.of(ifNullThanFalse(decodedSp.filter.is_4k)); + break; + case is_hd: + state = Optional.of(ifNullThanFalse(decodedSp.filter.is_hd)); + break; + case subtitles: + state = Optional.of(ifNullThanFalse(decodedSp.filter.subtitles)); + break; + case ccommons: + state = Optional.of(ifNullThanFalse(decodedSp.filter.ccommons)); + break; + case is_360: + state = Optional.of(ifNullThanFalse(decodedSp.filter.is_360)); + break; + case is_vr180: + state = Optional.of(ifNullThanFalse(decodedSp.filter.is_vr180)); + break; + case is_3d: + state = Optional.of(ifNullThanFalse(decodedSp.filter.is_3d)); + break; + case is_hdr: + state = Optional.of(ifNullThanFalse(decodedSp.filter.is_hdr)); + break; + case location: + state = Optional.of(ifNullThanFalse(decodedSp.filter.location)); + break; + case purchased: + state = Optional.of(ifNullThanFalse(decodedSp.filter.purchased)); + break; + } + + if (!state.isPresent()) { + throw new RuntimeException("this should not happen"); + } + return state.get(); + } + + private boolean ifNullThanFalse(final Boolean isFeatureEnabled) { + if (isFeatureEnabled == null) { + return false; + } + return isFeatureEnabled; + } +}