Skip to content

Commit aa8cea4

Browse files
committed
Refactor YouTube Music search tests
1 parent dd434cc commit aa8cea4

3 files changed

Lines changed: 52 additions & 79 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,11 @@ public String getUploaderName() throws ParsingException {
395395
@Override
396396
public String getUploaderUrl() throws ParsingException {
397397
if (searchType.equals(MUSIC_VIDEOS)) return null;
398-
String url = getUrlFromNavigationEndpoint(info.getArray("flexColumns")
398+
JsonObject navigationEndpoint = info.getArray("flexColumns")
399399
.getObject(1).getObject("musicResponsiveListItemFlexColumnRenderer")
400-
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint"));
400+
.getObject("text").getArray("runs").getObject(0).getObject("navigationEndpoint");
401+
if (navigationEndpoint == null) return null;
402+
String url = getUrlFromNavigationEndpoint(navigationEndpoint);
401403
if (url != null && !url.isEmpty()) return url;
402404
throw new ParsingException("Could not get uploader url");
403405
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ public static void defaultTestListOfItems(StreamingService expectedService, List
3737
if (item instanceof StreamInfoItem) {
3838
StreamInfoItem streamInfoItem = (StreamInfoItem) item;
3939
assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName());
40-
assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
41-
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
40+
41+
// assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
42+
if (streamInfoItem.getUploaderUrl() != null && !streamInfoItem.getUploaderUrl().isEmpty()) {
43+
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
44+
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
45+
}
4246

4347
assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM);
44-
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
4548

4649
final String textualUploadDate = streamInfoItem.getTextualUploadDate();
4750
if (textualUploadDate != null && !textualUploadDate.isEmpty()) {
Lines changed: 42 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,63 @@
11
package org.schabi.newpipe.extractor.services.youtube.search;
22

33
import org.junit.BeforeClass;
4-
import org.junit.Test;
54
import org.schabi.newpipe.DownloaderTestImpl;
65
import org.schabi.newpipe.extractor.InfoItem;
7-
import org.schabi.newpipe.extractor.ListExtractor;
86
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;
1010
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
1111

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;
1613

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;
3315

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;
3818

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";
4623

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();
5529
}
56-
assertFalse("First and second page are equal", equals);
57-
}
5830

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; }
6340
}
6441

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";
7145

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();
7751
}
7852

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; }
9462
}
9563
}

0 commit comments

Comments
 (0)