Skip to content

Commit 06ea74c

Browse files
committed
Add better tests for youtube search items
1 parent 0ffd4d9 commit 06ea74c

5 files changed

Lines changed: 64 additions & 72 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.schabi.newpipe.extractor.services.youtube;
2+
3+
import org.junit.Test;
4+
import org.schabi.newpipe.extractor.InfoItem;
5+
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
6+
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
7+
import org.schabi.newpipe.extractor.search.SearchResult;
8+
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
9+
10+
import static org.junit.Assert.*;
11+
import static org.junit.Assert.assertTrue;
12+
import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl;
13+
14+
public abstract class BaseYoutubeSearchTest {
15+
16+
protected static SearchResult result;
17+
18+
@Test
19+
public void testResultList() {
20+
assertFalse("Got empty result list", result.resultList.isEmpty());
21+
for(InfoItem infoItem: result.resultList) {
22+
assertIsSecureUrl(infoItem.getUrl());
23+
assertIsSecureUrl(infoItem.getThumbnailUrl());
24+
assertFalse(infoItem.getName().isEmpty());
25+
assertFalse("Name is probably a URI: " + infoItem.getName(),
26+
infoItem.getName().contains("://"));
27+
if(infoItem instanceof StreamInfoItem) {
28+
// test stream item
29+
StreamInfoItem streamInfoItem = (StreamInfoItem) infoItem;
30+
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
31+
assertFalse(streamInfoItem.getUploadDate().isEmpty());
32+
assertFalse(streamInfoItem.getUploaderName().isEmpty());
33+
} else if(infoItem instanceof ChannelInfoItem) {
34+
// Nothing special to check?
35+
} else if(infoItem instanceof PlaylistInfoItem) {
36+
// test playlist item
37+
long streamCount = ((PlaylistInfoItem) infoItem).getStreamCount();
38+
assertTrue(streamCount > 0);
39+
} else {
40+
fail("Unknown infoItem type: " + infoItem);
41+
}
42+
}
43+
}
44+
45+
@Test
46+
public void testResultErrors() {
47+
assertNotNull(result.errors);
48+
if (!result.errors.isEmpty()) {
49+
for (Throwable error : result.errors) {
50+
error.printStackTrace();
51+
}
52+
}
53+
assertTrue(result.errors.isEmpty());
54+
}
55+
}

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineAllTest.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@
4040
/**
4141
* Test for {@link SearchEngine}
4242
*/
43-
public class YoutubeSearchEngineAllTest {
44-
private static SearchResult result;
43+
public class YoutubeSearchEngineAllTest extends BaseYoutubeSearchTest {
4544

4645
@BeforeClass
4746
public static void setUpClass() throws Exception {
@@ -53,29 +52,13 @@ public static void setUpClass() throws Exception {
5352
}
5453

5554
@Test
56-
public void testResultList() {
57-
final List<InfoItem> results = result.getResults();
58-
assertFalse("Results are empty: " + results, results.isEmpty());
59-
60-
InfoItem firstInfoItem = results.get(0);
55+
public void testResultList_FirstElement() {
56+
InfoItem firstInfoItem = result.getResults().get(0);
6157

6258
// THe channel should be the first item
6359
assertTrue(firstInfoItem instanceof ChannelInfoItem);
6460
assertEquals("name", "PewDiePie", firstInfoItem.name);
6561
assertEquals("url","https://www.youtube.com/user/PewDiePie", firstInfoItem.url);
66-
67-
for(InfoItem item: results) {
68-
assertIsValidUrl(item.url);
69-
}
70-
71-
}
72-
73-
@Test
74-
public void testResultErrors() {
75-
for (Throwable error : result.getErrors()) {
76-
error.printStackTrace();
77-
}
78-
assertTrue(result.getErrors().isEmpty());
7962
}
8063

8164
@Ignore

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineChannelTest.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
/**
3838
* Test for {@link SearchEngine}
3939
*/
40-
public class YoutubeSearchEngineChannelTest {
41-
private static SearchResult result;
40+
public class YoutubeSearchEngineChannelTest extends BaseYoutubeSearchTest {
4241

4342
@BeforeClass
4443
public static void setUp() throws Exception {
@@ -51,28 +50,13 @@ public static void setUp() throws Exception {
5150
.getSearchResult();
5251
}
5352

54-
@Test
55-
public void testResultList() {
56-
assertFalse(result.resultList.isEmpty());
57-
for(InfoItem item: result.getResults()) {
58-
assertIsValidUrl(item.url);
59-
}
60-
}
61-
6253
@Test
6354
public void testResultsItemType() {
6455
for (InfoItem infoItem : result.resultList) {
6556
assertEquals(InfoItem.InfoType.CHANNEL, infoItem.info_type);
6657
}
6758
}
6859

69-
@Test
70-
public void testResultErrors() {
71-
assertNotNull(result.errors);
72-
if (!result.errors.isEmpty()) for (Throwable error : result.errors) error.printStackTrace();
73-
assertTrue(result.errors.isEmpty());
74-
}
75-
7660
@Ignore
7761
@Test
7862
public void testSuggestion() {

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEnginePlaylistTest.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.schabi.newpipe.Downloader;
77
import org.schabi.newpipe.extractor.InfoItem;
88
import org.schabi.newpipe.extractor.NewPipe;
9+
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
910
import org.schabi.newpipe.extractor.search.SearchEngine;
1011
import org.schabi.newpipe.extractor.search.SearchResult;
1112

@@ -37,8 +38,7 @@
3738
/**
3839
* Test for {@link SearchEngine}
3940
*/
40-
public class YoutubeSearchEnginePlaylistTest {
41-
private static SearchResult result;
41+
public class YoutubeSearchEnginePlaylistTest extends BaseYoutubeSearchTest {
4242

4343
@BeforeClass
4444
public static void setUp() throws Exception {
@@ -52,27 +52,13 @@ public static void setUp() throws Exception {
5252
}
5353

5454
@Test
55-
public void testResultList() {
56-
assertFalse(result.resultList.isEmpty());
57-
for(InfoItem item: result.getResults()) {
58-
assertIsValidUrl(item.url);
59-
}
60-
}
61-
62-
@Test
63-
public void testUserItemType() {
55+
public void testInfoItemType() {
6456
for (InfoItem infoItem : result.resultList) {
57+
assertTrue(infoItem instanceof PlaylistInfoItem);
6558
assertEquals(InfoItem.InfoType.PLAYLIST, infoItem.info_type);
6659
}
6760
}
6861

69-
@Test
70-
public void testResultErrors() {
71-
assertNotNull(result.errors);
72-
if (!result.errors.isEmpty()) for (Throwable error : result.errors) error.printStackTrace();
73-
assertTrue(result.errors.isEmpty());
74-
}
75-
7662
@Ignore
7763
@Test
7864
public void testSuggestion() {

src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeSearchEngineStreamTest.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
/**
3838
* Test for {@link SearchEngine}
3939
*/
40-
public class YoutubeSearchEngineStreamTest {
41-
private static SearchResult result;
40+
public class YoutubeSearchEngineStreamTest extends BaseYoutubeSearchTest {
4241

4342
@BeforeClass
4443
public static void setUp() throws Exception {
@@ -51,28 +50,13 @@ public static void setUp() throws Exception {
5150
.getSearchResult();
5251
}
5352

54-
@Test
55-
public void testResultList() {
56-
assertFalse(result.resultList.isEmpty());
57-
for(InfoItem item: result.getResults()) {
58-
assertIsValidUrl(item.url);
59-
}
60-
}
61-
6253
@Test
6354
public void testResultsItemType() {
6455
for (InfoItem infoItem : result.resultList) {
6556
assertEquals(InfoItem.InfoType.STREAM, infoItem.info_type);
6657
}
6758
}
6859

69-
@Test
70-
public void testResultErrors() {
71-
assertNotNull(result.errors);
72-
if (!result.errors.isEmpty()) for (Throwable error : result.errors) error.printStackTrace();
73-
assertTrue(result.errors.isEmpty());
74-
}
75-
7660
@Ignore
7761
@Test
7862
public void testSuggestion() {

0 commit comments

Comments
 (0)