|
1 | 1 | package org.schabi.newpipe.extractor.services.youtube.search; |
2 | 2 |
|
3 | 3 | import org.junit.BeforeClass; |
4 | | -import org.junit.Test; |
5 | 4 | import org.schabi.newpipe.DownloaderTestImpl; |
6 | 5 | import org.schabi.newpipe.extractor.InfoItem; |
7 | | -import org.schabi.newpipe.extractor.ListExtractor; |
8 | 6 | import org.schabi.newpipe.extractor.NewPipe; |
9 | | -import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSearchExtractor; |
| 7 | +import org.schabi.newpipe.extractor.StreamingService; |
| 8 | +import org.schabi.newpipe.extractor.search.SearchExtractor; |
| 9 | +import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest; |
10 | 10 | import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory; |
11 | 11 |
|
12 | | -import java.net.URL; |
13 | | -import java.net.URLDecoder; |
14 | | -import java.util.LinkedHashMap; |
15 | | -import java.util.Map; |
| 12 | +import java.net.URLEncoder; |
16 | 13 |
|
17 | | -import static java.util.Arrays.asList; |
18 | | -import static org.junit.Assert.assertEquals; |
19 | | -import static org.junit.Assert.assertFalse; |
20 | | -import static org.junit.Assert.assertTrue; |
21 | | -import static org.schabi.newpipe.extractor.ServiceList.YouTube; |
22 | | -import static org.schabi.newpipe.extractor.services.DefaultTests.defaultTestRelatedItems; |
23 | | - |
24 | | -public class YoutubeSearchExtractorMusicTest extends YoutubeSearchExtractorBaseTest { |
25 | | - @BeforeClass |
26 | | - public static void setUpClass() throws Exception { |
27 | | - NewPipe.init(DownloaderTestImpl.getInstance()); |
28 | | - extractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("mocromaniac", |
29 | | - asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null); |
30 | | - extractor.fetchPage(); |
31 | | - itemsPage = extractor.getInitialPage(); |
32 | | - } |
| 14 | +import javax.annotation.Nullable; |
33 | 15 |
|
34 | | - @Test |
35 | | - public void testRelatedItems() throws Exception { |
36 | | - defaultTestRelatedItems(extractor); |
37 | | - } |
| 16 | +import static java.util.Collections.singletonList; |
| 17 | +import static org.schabi.newpipe.extractor.ServiceList.YouTube; |
38 | 18 |
|
39 | | - @Test |
40 | | - public void testGetSecondPage() throws Exception { |
41 | | - YoutubeSearchExtractor secondExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("mocromaniac", |
42 | | - asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null); |
43 | | - ListExtractor.InfoItemsPage<InfoItem> secondPage = secondExtractor.getPage(itemsPage.getNextPageUrl()); |
44 | | - assertTrue(Integer.toString(secondPage.getItems().size()), |
45 | | - secondPage.getItems().size() > 10); |
| 19 | +public class YoutubeSearchExtractorMusicTest { |
| 20 | + public static class MusicSongs extends DefaultSearchExtractorTest { |
| 21 | + private static SearchExtractor extractor; |
| 22 | + private static final String QUERY = "mocromaniac"; |
46 | 23 |
|
47 | | - // check if its the same result |
48 | | - boolean equals = true; |
49 | | - for (int i = 0; i < secondPage.getItems().size() |
50 | | - && i < itemsPage.getItems().size(); i++) { |
51 | | - if (!secondPage.getItems().get(i).getUrl().equals( |
52 | | - itemsPage.getItems().get(i).getUrl())) { |
53 | | - equals = false; |
54 | | - } |
| 24 | + @BeforeClass |
| 25 | + public static void setUp() throws Exception { |
| 26 | + NewPipe.init(DownloaderTestImpl.getInstance()); |
| 27 | + extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), ""); |
| 28 | + extractor.fetchPage(); |
55 | 29 | } |
56 | | - assertFalse("First and second page are equal", equals); |
57 | | - } |
58 | 30 |
|
59 | | - @Override |
60 | | - @Test |
61 | | - public void testUrl() throws Exception { |
62 | | - assertTrue(extractor.getUrl(), extractor.getUrl().startsWith("https://music.youtube.com/search?q=")); |
| 31 | + @Override public SearchExtractor extractor() { return extractor; } |
| 32 | + @Override public StreamingService expectedService() { return YouTube; } |
| 33 | + @Override public String expectedName() { return QUERY; } |
| 34 | + @Override public String expectedId() { return QUERY; } |
| 35 | + @Override public String expectedUrlContains() { return "music.youtube.com/search?q=" + QUERY; } |
| 36 | + @Override public String expectedOriginalUrlContains() { return "music.youtube.com/search?q=" + QUERY; } |
| 37 | + @Override public String expectedSearchString() { return QUERY; } |
| 38 | + @Nullable @Override public String expectedSearchSuggestion() { return null; } |
| 39 | + @Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; } |
63 | 40 | } |
64 | 41 |
|
65 | | - @Test |
66 | | - public void testGetSecondPageUrl() throws Exception { |
67 | | - URL url = new URL(extractor.getNextPageUrl()); |
68 | | - |
69 | | - assertEquals(url.getHost(), "music.youtube.com"); |
70 | | - assertEquals(url.getPath(), "/youtubei/v1/search"); |
| 42 | + public static class Suggestion extends DefaultSearchExtractorTest { |
| 43 | + private static SearchExtractor extractor; |
| 44 | + private static final String QUERY = "megaman x3"; |
71 | 45 |
|
72 | | - Map<String, String> queryPairs = new LinkedHashMap<>(); |
73 | | - for (String queryPair : url.getQuery().split("&")) { |
74 | | - int index = queryPair.indexOf("="); |
75 | | - queryPairs.put(URLDecoder.decode(queryPair.substring(0, index), "UTF-8"), |
76 | | - URLDecoder.decode(queryPair.substring(index + 1), "UTF-8")); |
| 46 | + @BeforeClass |
| 47 | + public static void setUp() throws Exception { |
| 48 | + NewPipe.init(DownloaderTestImpl.getInstance()); |
| 49 | + extractor = YouTube.getSearchExtractor(QUERY, singletonList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), ""); |
| 50 | + extractor.fetchPage(); |
77 | 51 | } |
78 | 52 |
|
79 | | - assertEquals(queryPairs.get("ctoken"), queryPairs.get("continuation")); |
80 | | - assertTrue(queryPairs.get("continuation").length() > 5); |
81 | | - assertTrue(queryPairs.get("itct").length() > 5); |
82 | | - assertEquals("json", queryPairs.get("alt")); |
83 | | - assertTrue(queryPairs.get("key").length() > 5); |
84 | | - } |
85 | | - |
86 | | - @Test |
87 | | - public void testSuggestions() throws Exception { |
88 | | - YoutubeSearchExtractor newExtractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("megaman x3", |
89 | | - asList(YoutubeSearchQueryHandlerFactory.MUSIC_SONGS), null); |
90 | | - newExtractor.fetchPage(); |
91 | | - |
92 | | - assertTrue(newExtractor.getInitialPage().getItems().size() > 10); |
93 | | - assertEquals("mega man x3", newExtractor.getSearchSuggestion()); |
| 53 | + @Override public SearchExtractor extractor() { return extractor; } |
| 54 | + @Override public StreamingService expectedService() { return YouTube; } |
| 55 | + @Override public String expectedName() { return QUERY; } |
| 56 | + @Override public String expectedId() { return QUERY; } |
| 57 | + @Override public String expectedUrlContains() { return "music.youtube.com/search?q=" + URLEncoder.encode(QUERY); } |
| 58 | + @Override public String expectedOriginalUrlContains() { return "music.youtube.com/search?q=" + URLEncoder.encode(QUERY); } |
| 59 | + @Override public String expectedSearchString() { return QUERY; } |
| 60 | + @Nullable @Override public String expectedSearchSuggestion() { return "mega man x3"; } |
| 61 | + @Override public InfoItem.InfoType expectedInfoItemType() { return InfoItem.InfoType.STREAM; } |
94 | 62 | } |
95 | 63 | } |
0 commit comments