1+ package org .schabi .newpipe .extractor .services .youtube .search ;
2+
3+ import static java .util .Collections .singletonList ;
4+ import static org .junit .Assert .assertEquals ;
5+ import static org .junit .Assert .assertNotNull ;
6+ import static org .junit .Assert .assertTrue ;
7+ import static org .schabi .newpipe .extractor .ServiceList .YouTube ;
8+
9+ import java .util .HashSet ;
10+ import java .util .List ;
11+ import java .util .Set ;
12+
13+ import org .junit .BeforeClass ;
14+ import org .junit .Test ;
15+ import org .schabi .newpipe .DownloaderTestImpl ;
16+ import org .schabi .newpipe .extractor .InfoItem ;
17+ import org .schabi .newpipe .extractor .ListExtractor ;
18+ import org .schabi .newpipe .extractor .NewPipe ;
19+ import org .schabi .newpipe .extractor .services .youtube .extractors .YoutubeSearchExtractor ;
20+ import org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeSearchQueryHandlerFactory ;
21+
22+ public class YoutubeSearchPagingTest {
23+ private static ListExtractor .InfoItemsPage <InfoItem > page1 ;
24+ private static ListExtractor .InfoItemsPage <InfoItem > page2 ;
25+ private static Set <String > urlList1 ;
26+ private static Set <String > urlList2 ;
27+
28+ @ BeforeClass
29+ public static void setUpClass () throws Exception {
30+ NewPipe .init (DownloaderTestImpl .getInstance ());
31+
32+ YoutubeSearchExtractor extractor = (YoutubeSearchExtractor ) YouTube .getSearchExtractor ("cirque du soleil" ,
33+ singletonList (YoutubeSearchQueryHandlerFactory .VIDEOS ), null );
34+
35+ extractor .fetchPage ();
36+ page1 = extractor .getInitialPage ();
37+ urlList1 = extractUrls (page1 .getItems ());
38+ assertEquals ("page with items loaded" , 20 , page1 .getItems ().size ());
39+ assertEquals ("distinct videos" , 20 , urlList1 .size ());
40+
41+ assertTrue ("has more page" , page1 .hasNextPage ());
42+ assertNotNull ("has next page url" , page1 .getNextPageUrl ());
43+ page2 = extractor .getPage (page1 .getNextPageUrl ());
44+ urlList2 = extractUrls (page2 .getItems ());
45+ }
46+
47+ private static Set <String > extractUrls (List <InfoItem > list ) {
48+ Set <String > result = new HashSet <>();
49+ for (InfoItem item : list ) {
50+ result .add (item .getUrl ());
51+ }
52+ return result ;
53+ }
54+
55+ @ Test
56+ public void firstPageOk () {
57+ assertEquals ("page with items loaded" , 20 , page1 .getItems ().size ());
58+ assertEquals ("distinct videos" , 20 , urlList1 .size ());
59+ }
60+
61+ @ Test
62+ public void secondPageLength () {
63+ assertEquals ("one page" , 20 , page2 .getItems ().size ());
64+ }
65+
66+ @ Test
67+ public void secondPageUniqueVideos () {
68+ assertEquals ("distinct videos" , 20 , urlList2 .size ());
69+ }
70+
71+ @ Test
72+ public void noRepeatingVideosInPages () {
73+ Set <String > intersection = new HashSet <>(urlList2 );
74+ intersection .retainAll (urlList1 );
75+ assertEquals ("empty intersection" , 0 , intersection .size ());
76+ }
77+
78+ }
0 commit comments