[#11930] Converting to Kotlin

This commit is contained in:
Thiago F. G. Albuquerque 2025-02-24 20:57:06 -03:00
parent 76a02d5858
commit 998d84de6c
5 changed files with 183 additions and 223 deletions

View file

@ -1,132 +0,0 @@
package org.schabi.newpipe.local.playlist;
import static org.schabi.newpipe.local.playlist.ExportPlaylist.export;
import static org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS;
import static org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST;
import androidx.annotation.NonNull;
import org.junit.Assert;
import org.junit.Test;
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry;
import org.schabi.newpipe.database.stream.model.StreamEntity;
import org.schabi.newpipe.extractor.stream.StreamType;
import java.util.List;
import java.util.stream.Stream;
public class ExportPlaylistTest {
@Test
public void exportAsYouTubeTempPlaylist() {
final List<PlaylistStreamEntry> playlist = asPlaylist(
"https://www.youtube.com/watch?v=1",
"https://soundcloud.com/cautious-clayofficial/cold-war-2", // non-Youtube URLs should be
"https://www.youtube.com/watch?v=2", // ignored
"https://www.youtube.com/watch?v=3"
);
final String url = export(YOUTUBE_TEMP_PLAYLIST, playlist, null);
Assert.assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url);
}
@Test
public void exportMoreThan50Items() {
/*
* Playlist has more than 50 items => take the last 50
* (YouTube limitation)
*/
final List<Integer> ids = List.of(
-1, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50
);
final List<PlaylistStreamEntry> playlist = asPlaylist(
ids.stream()
.map(id -> "https://www.youtube.com/watch?v=" + id)
);
final String url = export(YOUTUBE_TEMP_PLAYLIST, playlist, null);
Assert.assertEquals(
"http://www.youtube.com/watch_videos?video_ids="
+ "1,2,3,4,5,6,7,8,9,10,"
+ "11,12,13,14,15,16,17,18,19,20,"
+ "21,22,23,24,25,26,27,28,29,30,"
+ "31,32,33,34,35,36,37,38,39,40,"
+ "41,42,43,44,45,46,47,48,49,50",
url
);
}
@Test
public void exportJustUrls() {
final List<PlaylistStreamEntry> playlist = asPlaylist(
"https://www.youtube.com/watch?v=1",
"https://www.youtube.com/watch?v=2",
"https://www.youtube.com/watch?v=3"
);
final String exported = export(JUST_URLS, playlist, null);
Assert.assertEquals("""
https://www.youtube.com/watch?v=1
https://www.youtube.com/watch?v=2
https://www.youtube.com/watch?v=3""", exported);
}
@NonNull
static List<PlaylistStreamEntry> asPlaylist(final String... urls) {
return asPlaylist(Stream.of(urls));
}
@NonNull
static List<PlaylistStreamEntry> asPlaylist(final Stream<String> urls) {
return urls
.map(ExportPlaylistTest::newPlaylistStreamEntry)
.toList();
}
@NonNull
private static PlaylistStreamEntry newPlaylistStreamEntry(final String url) {
return new PlaylistStreamEntry(newStreamEntity(url), 0, 0, 0);
}
@NonNull
static StreamEntity newStreamEntity(final String url) {
return new StreamEntity(
0,
1,
url,
"Title",
StreamType.VIDEO_STREAM,
100,
"Uploader",
null,
null,
null,
null,
null,
null
);
}
}

View file

@ -0,0 +1,110 @@
package org.schabi.newpipe.local.playlist
import android.content.Context
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.Mockito.mock
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
import org.schabi.newpipe.database.stream.model.StreamEntity
import org.schabi.newpipe.extractor.stream.StreamType
import org.schabi.newpipe.local.playlist.PlayListShareMode.JUST_URLS
import org.schabi.newpipe.local.playlist.PlayListShareMode.YOUTUBE_TEMP_PLAYLIST
import java.util.stream.Stream
class ExportPlaylistTest {
@Test
fun exportAsYouTubeTempPlaylist() {
val playlist = asPlaylist(
"https://www.youtube.com/watch?v=1",
"https://soundcloud.com/cautious-clayofficial/cold-war-2", // non-Youtube URLs should be ignored
"https://www.youtube.com/watch?v=2",
"https://www.youtube.com/watch?v=3"
)
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
assertEquals("http://www.youtube.com/watch_videos?video_ids=1,2,3", url)
}
@Test
fun exportMoreThan50Items() {
/*
* Playlist has more than 50 items => take the last 50
* (YouTube limitation)
*/
val ids = listOf(
-1, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50
)
val playlist = asPlaylist(
ids.stream()
.map { id: Int -> "https://www.youtube.com/watch?v=$id" }
)
val url = export(YOUTUBE_TEMP_PLAYLIST, playlist, mock(Context::class.java))
assertEquals(
"http://www.youtube.com/watch_videos?video_ids=" +
"1,2,3,4,5,6,7,8,9,10," +
"11,12,13,14,15,16,17,18,19,20," +
"21,22,23,24,25,26,27,28,29,30," +
"31,32,33,34,35,36,37,38,39,40," +
"41,42,43,44,45,46,47,48,49,50",
url
)
}
@Test
fun exportJustUrls() {
val playlist = asPlaylist(
"https://www.youtube.com/watch?v=1",
"https://www.youtube.com/watch?v=2",
"https://www.youtube.com/watch?v=3"
)
val exported = export(JUST_URLS, playlist, mock(Context::class.java))
assertEquals(
"""
https://www.youtube.com/watch?v=1
https://www.youtube.com/watch?v=2
https://www.youtube.com/watch?v=3
""".trimIndent(),
exported
)
}
}
fun asPlaylist(vararg urls: String): List<PlaylistStreamEntry> {
return asPlaylist(Stream.of(*urls))
}
fun asPlaylist(urls: Stream<String>): List<PlaylistStreamEntry> {
return urls
.map { url: String -> newPlaylistStreamEntry(url) }
.toList()
}
fun newPlaylistStreamEntry(url: String): PlaylistStreamEntry {
return PlaylistStreamEntry(newStreamEntity(url), 0, 0, 0)
}
fun newStreamEntity(url: String): StreamEntity {
return StreamEntity(
0,
1,
url,
"Title",
StreamType.VIDEO_STREAM,
100,
"Uploader"
)
}