|
| 1 | +package org.schabi.newpipe.local.subscription; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertThat; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import androidx.test.core.app.ApplicationProvider; |
| 8 | + |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Rule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.schabi.newpipe.database.AppDatabase; |
| 14 | +import org.schabi.newpipe.database.subscription.SubscriptionEntity; |
| 15 | +import org.schabi.newpipe.extractor.channel.ChannelInfo; |
| 16 | +import org.schabi.newpipe.extractor.exceptions.ExtractionException; |
| 17 | +import org.schabi.newpipe.extractor.stream.StreamInfo; |
| 18 | +import org.schabi.newpipe.testUtil.TestDatabase; |
| 19 | +import org.schabi.newpipe.testUtil.TrampolineSchedulerRule; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.lang.reflect.Method; |
| 23 | + |
| 24 | +public class SubscriptionManagerTest { |
| 25 | + private AppDatabase database; |
| 26 | + private SubscriptionManager manager; |
| 27 | + private SubscriptionEntity subscription; |
| 28 | + private SubscriptionEntity anotherSubscription; |
| 29 | + |
| 30 | + @Rule |
| 31 | + public TrampolineSchedulerRule trampolineScheduler = new TrampolineSchedulerRule(); |
| 32 | + |
| 33 | + @Before |
| 34 | + public void setup() throws ExtractionException, IOException { |
| 35 | + database = TestDatabase.Companion.createReplacingNewPipeDatabase(); |
| 36 | + 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); |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + public void cleanUp() { |
| 48 | + database.close(); |
| 49 | + } |
| 50 | + |
| 51 | + @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 | + |
| 72 | + |
| 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()); |
| 79 | + 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()); |
| 87 | + } |
| 88 | + |
| 89 | + @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); |
| 93 | + 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 | + } |
| 100 | + |
| 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()); |
| 107 | + 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); |
| 112 | + |
| 113 | + assertEquals(subscription.getUrl(), anotherSubscription.getUrl()); |
| 114 | + assertEquals(0, anotherSubscription.getNotificationMode()); |
| 115 | + } |
| 116 | + |
| 117 | + @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); |
| 125 | + |
| 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); |
| 134 | + 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); |
| 143 | + } |
| 144 | +} |
0 commit comments