11package org .schabi .newpipe .local .subscription ;
22
33import static org .junit .Assert .assertEquals ;
4- import static org .junit .Assert .assertThat ;
5- import static org .junit .Assert .assertTrue ;
64
75import androidx .test .core .app .ApplicationProvider ;
86
119import org .junit .Rule ;
1210import org .junit .Test ;
1311import org .schabi .newpipe .database .AppDatabase ;
12+ import org .schabi .newpipe .database .feed .model .FeedGroupEntity ;
13+ import org .schabi .newpipe .database .stream .model .StreamEntity ;
1414import org .schabi .newpipe .database .subscription .SubscriptionEntity ;
1515import org .schabi .newpipe .extractor .channel .ChannelInfo ;
1616import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
17- import org .schabi .newpipe .extractor .stream .StreamInfo ;
17+ import org .schabi .newpipe .extractor .localization .DateWrapper ;
18+ import org .schabi .newpipe .extractor .stream .StreamInfoItem ;
19+ import org .schabi .newpipe .extractor .stream .StreamType ;
1820import org .schabi .newpipe .testUtil .TestDatabase ;
1921import org .schabi .newpipe .testUtil .TrampolineSchedulerRule ;
2022
2123import java .io .IOException ;
22- import java .lang .reflect .Method ;
24+ import java .time .OffsetDateTime ;
25+ import java .util .Comparator ;
26+ import java .util .List ;
2327
2428public class SubscriptionManagerTest {
2529 private AppDatabase database ;
2630 private SubscriptionManager manager ;
27- private SubscriptionEntity subscription ;
28- private SubscriptionEntity anotherSubscription ;
2931
3032 @ Rule
3133 public TrampolineSchedulerRule trampolineScheduler = new TrampolineSchedulerRule ();
3234
35+
36+ private SubscriptionEntity getAssertOneSubscriptionEntity () {
37+ final List <SubscriptionEntity > entities = manager
38+ .getSubscriptions (FeedGroupEntity .GROUP_ALL_ID , "" , false )
39+ .blockingFirst ();
40+ assertEquals (1 , entities .size ());
41+ return entities .get (0 );
42+ }
43+
44+
3345 @ Before
34- public void setup () throws ExtractionException , IOException {
46+ public void setup () {
3547 database = TestDatabase .Companion .createReplacingNewPipeDatabase ();
3648 manager = new SubscriptionManager (ApplicationProvider .getApplicationContext ());
37-
38- ChannelInfo info = ChannelInfo .getInfo ("https://www.youtube.com/c/3blue1brown" );
39- subscription = SubscriptionEntity .from (info );
40- manager .insertSubscription (subscription , info );
41- var entities = manager .getSubscriptions (-1L , subscription .getName (), false ).blockingFirst ();
42- assertEquals (1 , entities .size ());
43- anotherSubscription = entities .get (0 );
4449 }
4550
4651 @ After
@@ -49,96 +54,66 @@ public void cleanUp() {
4954 }
5055
5156 @ Test
52- public void testInsert1 () {
53- //This test fails because the uid of those two SubscriptionEntity aren't the same.
54- assertEquals (subscription , anotherSubscription );
55- }
56-
57- @ Test
58- public void testInsert2 () {
59- assertEquals (subscription .getUid (), anotherSubscription .getUid ());
60- }
61-
62- @ Test
63- public void testInsert3 () {
64- assertEquals (subscription .getServiceId (), anotherSubscription .getServiceId ());
65- assertEquals (subscription .getUrl (), anotherSubscription .getUrl ());
66- assertEquals (subscription .getName (), anotherSubscription .getName ());
67- assertEquals (subscription .getAvatarUrl (), anotherSubscription .getAvatarUrl ());
68- assertEquals (subscription .getSubscriberCount (), anotherSubscription .getSubscriberCount ());
69- assertEquals (subscription .getDescription (), anotherSubscription .getDescription ());
70- }
71-
57+ public void testInsert () throws ExtractionException , IOException {
58+ final ChannelInfo info = ChannelInfo .getInfo ("https://www.youtube.com/c/3blue1brown" );
59+ final SubscriptionEntity subscription = SubscriptionEntity .from (info );
7260
73- @ Test
74- public void testUpdateNotificationMode1 () throws ExtractionException , IOException {
75- var info = ChannelInfo .getInfo ("https://www.youtube.com/c/veritasium" );
76- var subscription = SubscriptionEntity .from (info );
77- subscription .setNotificationMode (0 );
78- assertEquals (0 , subscription .getNotificationMode ());
7961 manager .insertSubscription (subscription , info );
80- manager .updateNotificationMode (subscription .getServiceId (), subscription .getUrl (), 1 );
81- var entities = manager .getSubscriptions (-1L , subscription .getName (), false ).blockingFirst ();
82- assertEquals (1 , entities .size ());
83- var anotherSubscription = entities .get (0 );
84-
85- assertEquals (subscription .getUrl (), anotherSubscription .getUrl ());
86- assertEquals (1 , anotherSubscription .getNotificationMode ());
62+ final SubscriptionEntity readSubscription = getAssertOneSubscriptionEntity ();
63+
64+ // the uid has changed, since the uid is chosen upon inserting, but the rest should match
65+ assertEquals (subscription .getServiceId (), readSubscription .getServiceId ());
66+ assertEquals (subscription .getUrl (), readSubscription .getUrl ());
67+ assertEquals (subscription .getName (), readSubscription .getName ());
68+ assertEquals (subscription .getAvatarUrl (), readSubscription .getAvatarUrl ());
69+ assertEquals (subscription .getSubscriberCount (), readSubscription .getSubscriberCount ());
70+ assertEquals (subscription .getDescription (), readSubscription .getDescription ());
8771 }
8872
8973 @ Test
90- public void testUpdateNotificationMode2 () throws ExtractionException , IOException {
91- var info = ChannelInfo .getInfo ("https://www.youtube.com/c/Radiohead " );
92- var subscription = SubscriptionEntity .from (info );
74+ public void testUpdateNotificationMode () throws ExtractionException , IOException {
75+ final ChannelInfo info = ChannelInfo .getInfo ("https://www.youtube.com/c/veritasium " );
76+ final SubscriptionEntity subscription = SubscriptionEntity .from (info );
9377 subscription .setNotificationMode (0 );
94- assertEquals (0 , subscription .getNotificationMode ());
95- manager .insertSubscription (subscription , info );
96- manager .updateNotificationMode (subscription .getServiceId (), subscription .getUrl (), 1 );
97-
98- assertEquals (1 , subscription .getNotificationMode ());
99- }
10078
101- @ Test
102- public void testUpdateNotificationMode3 () throws ExtractionException , IOException {
103- var info = ChannelInfo .getInfo ("https://www.youtube.com/c/LinusTechTips" );
104- var subscription = SubscriptionEntity .from (info );
105- subscription .setNotificationMode (1 );
106- assertEquals (1 , subscription .getNotificationMode ());
10779 manager .insertSubscription (subscription , info );
108- manager .updateNotificationMode (subscription .getServiceId (), subscription .getUrl (), 0 );
109- var entities = manager .getSubscriptions (-1L , subscription .getName (), false ).blockingFirst ();
110- assertEquals (1 , entities .size ());
111- var anotherSubscription = entities .get (0 );
80+ manager .updateNotificationMode (subscription .getServiceId (), subscription .getUrl (), 1 )
81+ .blockingAwait ();
82+ final SubscriptionEntity anotherSubscription = getAssertOneSubscriptionEntity ();
11283
84+ assertEquals (0 , subscription .getNotificationMode ());
11385 assertEquals (subscription .getUrl (), anotherSubscription .getUrl ());
114- assertEquals (0 , anotherSubscription .getNotificationMode ());
86+ assertEquals (1 , anotherSubscription .getNotificationMode ());
11587 }
11688
11789 @ Test
118- public void testUpdateNotificationMode4 () throws ExtractionException , IOException {
119- var info = ChannelInfo .getInfo ("https://www.youtube.com/c/JetBrainsTV" );
120- var subscription = SubscriptionEntity .from (info );
121- subscription .setNotificationMode (1 );
122- assertEquals (1 , subscription .getNotificationMode ());
123- manager .insertSubscription (subscription , info );
124- manager .updateNotificationMode (subscription .getServiceId (), subscription .getUrl (), 0 );
90+ public void testRememberRecentStreams () throws ExtractionException , IOException {
91+ final ChannelInfo info = ChannelInfo .getInfo ("https://www.youtube.com/c/Polyphia" );
92+ final List <StreamInfoItem > relatedItems = List .of (
93+ new StreamInfoItem (0 , "a" , "b" , StreamType .VIDEO_STREAM ),
94+ new StreamInfoItem (1 , "c" , "d" , StreamType .AUDIO_STREAM ),
95+ new StreamInfoItem (2 , "e" , "f" , StreamType .AUDIO_LIVE_STREAM ),
96+ new StreamInfoItem (3 , "g" , "h" , StreamType .LIVE_STREAM ));
97+ relatedItems .forEach (item -> {
98+ // these two fields must be non-null for the insert to succeed
99+ item .setUploaderUrl (info .getUrl ());
100+ item .setUploaderName (info .getName ());
101+ // the upload date must not be too much in the past for the item to actually be inserted
102+ item .setUploadDate (new DateWrapper (OffsetDateTime .now ()));
103+ });
104+ info .setRelatedItems (relatedItems );
105+ final SubscriptionEntity subscription = SubscriptionEntity .from (info );
125106
126- assertEquals (0 , subscription .getNotificationMode ());
127- }
128-
129- @ Test
130- public void testRememberAllStreams () throws ExtractionException , IOException {
131- database .streamDAO ().deleteAll ();
132- var info = ChannelInfo .getInfo ("https://www.youtube.com/c/Polyphia" );
133- var subscription = SubscriptionEntity .from (info );
134107 manager .insertSubscription (subscription , info );
135-
136- // var Stream1 = StreamInfo.getInfo("https://www.youtube.com/watch?v=Z5NoQg8LdDk");
137- // var Stream2 = StreamInfo.getInfo("https://www.youtube.com/watch?v=2hln1TOQUZ0");
138- // var Stream3 = StreamInfo.getInfo("https://www.youtube.com/watch?v=9_gkpYORQLU");
139- // var Stream4 = StreamInfo.getInfo("https://www.youtube.com/watch?v=per9Wz0N-QA");
140-
141- var streams = database .streamDAO ().getAll ().blockingFirst ();
142- assertTrue (streams .size () >= 4 );
108+ final List <StreamEntity > streams = database .streamDAO ().getAll ().blockingFirst ();
109+
110+ assertEquals (4 , streams .size ());
111+ streams .sort (Comparator .comparing (StreamEntity ::getServiceId ));
112+ for (int i = 0 ; i < 4 ; i ++) {
113+ assertEquals (relatedItems .get (0 ).getServiceId (), streams .get (0 ).getServiceId ());
114+ assertEquals (relatedItems .get (0 ).getUrl (), streams .get (0 ).getUrl ());
115+ assertEquals (relatedItems .get (0 ).getName (), streams .get (0 ).getTitle ());
116+ assertEquals (relatedItems .get (0 ).getStreamType (), streams .get (0 ).getStreamType ());
117+ }
143118 }
144119}
0 commit comments