1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 00:20:32 +00:00

fix: ListHelper tests

This commit is contained in:
ThetaDev 2023-03-19 22:31:31 +01:00
parent 9b8ffdd2aa
commit 61a14765f3
2 changed files with 123 additions and 33 deletions

View File

@ -222,7 +222,8 @@ public final class ListHelper {
final HashMap<String, AudioStream> collectedStreams = new HashMap<>(); final HashMap<String, AudioStream> collectedStreams = new HashMap<>();
final Comparator<AudioStream> cmp = getAudioStreamFormatComparator(context); final Comparator<AudioStream> cmp =
getAudioStreamFormatComparator(isLimitingDataUsage(context));
for (final AudioStream stream : audioStreams) { for (final AudioStream stream : audioStreams) {
if (stream.getDeliveryMethod() == DeliveryMethod.TORRENT) { if (stream.getDeliveryMethod() == DeliveryMethod.TORRENT) {
@ -422,7 +423,7 @@ public final class ListHelper {
* @param comparator The comparator used for determining the max/best/highest ranked value * @param comparator The comparator used for determining the max/best/highest ranked value
* @return Index of audio stream that produces the highest ranked result or -1 if not found * @return Index of audio stream that produces the highest ranked result or -1 if not found
*/ */
private static int getAudioIndexByHighestRank(@Nullable final MediaFormat targetedFormat, static int getAudioIndexByHighestRank(@Nullable final MediaFormat targetedFormat,
@Nullable final List<AudioStream> audioStreams, @Nullable final List<AudioStream> audioStreams,
final Comparator<AudioStream> comparator) { final Comparator<AudioStream> comparator) {
if (audioStreams == null || audioStreams.isEmpty()) { if (audioStreams == null || audioStreams.isEmpty()) {
@ -634,12 +635,11 @@ public final class ListHelper {
/** /**
* Get a {@link Comparator} to compare {@link AudioStream}s by their format and bitrate. * Get a {@link Comparator} to compare {@link AudioStream}s by their format and bitrate.
* *
* @param context App context * @param limitDataUsage choose low bitrate audio stream
* @return Comparator * @return Comparator
*/ */
private static Comparator<AudioStream> getAudioStreamFormatComparator( private static Comparator<AudioStream> getAudioStreamFormatComparator(
@NonNull final Context context) { final boolean limitDataUsage) {
final boolean limitDataUsage = isLimitingDataUsage(context);
final List<MediaFormat> formatRanking = limitDataUsage final List<MediaFormat> formatRanking = limitDataUsage
? AUDIO_FORMAT_EFFICIENCY_RANKING : AUDIO_FORMAT_QUALITY_RANKING; ? AUDIO_FORMAT_EFFICIENCY_RANKING : AUDIO_FORMAT_QUALITY_RANKING;
@ -664,14 +664,32 @@ public final class ListHelper {
@NonNull final Context context) { @NonNull final Context context) {
final SharedPreferences preferences = final SharedPreferences preferences =
PreferenceManager.getDefaultSharedPreferences(context); PreferenceManager.getDefaultSharedPreferences(context);
final Locale preferredLanguage = Localization.getPreferredLocale(context);
final boolean preferOriginalAudio = final boolean preferOriginalAudio =
preferences.getBoolean(context.getString(R.string.prefer_original_audio_key), preferences.getBoolean(context.getString(R.string.prefer_original_audio_key),
false); false);
final boolean preferDescriptiveAudio = final boolean preferDescriptiveAudio =
preferences.getBoolean(context.getString(R.string.prefer_descriptive_audio_key), preferences.getBoolean(context.getString(R.string.prefer_descriptive_audio_key),
false); false);
final String preferredLanguage = Localization.getPreferredLocale(context).getISO3Language();
return getAudioStreamComparator(preferredLanguage, preferOriginalAudio,
preferDescriptiveAudio, isLimitingDataUsage(context));
}
/**
* Get a {@link Comparator} to compare {@link AudioStream}s by their language, format
* and bitrate.
* @param preferredLanguage Preferred audio stream language
* @param preferOriginalAudio Get the original audio track regardless of its language
* @param preferDescriptiveAudio Prefer the descriptive audio track if available
* @param limitDataUsage choose low bitrate audio stream
* @return Comparator
*/
static Comparator<AudioStream> getAudioStreamComparator(final Locale preferredLanguage,
final boolean preferOriginalAudio,
final boolean preferDescriptiveAudio,
final boolean limitDataUsage) {
final String langCode = preferredLanguage.getISO3Language();
final List<AudioTrackType> trackTypeRanking = preferDescriptiveAudio final List<AudioTrackType> trackTypeRanking = preferDescriptiveAudio
? AUDIO_TRACK_TYPE_RANKING_DESCRIPTIVE : AUDIO_TRACK_TYPE_RANKING; ? AUDIO_TRACK_TYPE_RANKING_DESCRIPTIVE : AUDIO_TRACK_TYPE_RANKING;
@ -683,13 +701,13 @@ public final class ListHelper {
return 0; return 0;
}).thenComparing(AudioStream::getAudioLocale, }).thenComparing(AudioStream::getAudioLocale,
Comparator.nullsFirst(Comparator.comparing( Comparator.nullsFirst(Comparator.comparing(
locale -> locale.getISO3Language().equals(preferredLanguage)))) locale -> locale.getISO3Language().equals(langCode))))
.thenComparing(AudioStream::getAudioTrackType, .thenComparing(AudioStream::getAudioTrackType,
Comparator.nullsLast(Comparator.comparingInt(trackTypeRanking::indexOf))) Comparator.nullsLast(Comparator.comparingInt(trackTypeRanking::indexOf)))
.thenComparing(AudioStream::getAudioLocale, .thenComparing(AudioStream::getAudioLocale,
Comparator.nullsFirst(Comparator.comparing( Comparator.nullsFirst(Comparator.comparing(
locale -> locale.getISO3Language().equals( locale -> locale.getISO3Language().equals(
Locale.ENGLISH.getISO3Language())))) Locale.ENGLISH.getISO3Language()))))
.thenComparing(getAudioStreamFormatComparator(context)); .thenComparing(getAudioStreamFormatComparator(limitDataUsage));
} }
} }

View File

@ -3,10 +3,13 @@ package org.schabi.newpipe.util;
import org.junit.Test; import org.junit.Test;
import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.stream.AudioStream; import org.schabi.newpipe.extractor.stream.AudioStream;
import org.schabi.newpipe.extractor.stream.AudioTrackType;
import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.stream.VideoStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@ -29,6 +32,15 @@ public class ListHelperTest {
generateAudioStream("mp3-192", MediaFormat.MP3, 192), generateAudioStream("mp3-192", MediaFormat.MP3, 192),
generateAudioStream("webma-320", MediaFormat.WEBMA, 320)); generateAudioStream("webma-320", MediaFormat.WEBMA, 320));
private static final List<AudioStream> AUDIO_TRACKS_TEST_LIST = List.of(
generateAudioTrack("en.or", "en.or", Locale.ENGLISH, AudioTrackType.ORIGINAL),
generateAudioTrack("en.du", "en.du", Locale.ENGLISH, AudioTrackType.DUBBED),
generateAudioTrack("en.ds", "en.ds", Locale.ENGLISH, AudioTrackType.DESCRIPTIVE),
generateAudioTrack("unknown", null, null, null),
generateAudioTrack("de.du", "de.du", Locale.GERMAN, AudioTrackType.DUBBED),
generateAudioTrack("de.ds", "de.ds", Locale.GERMAN, AudioTrackType.DESCRIPTIVE)
);
private static final List<VideoStream> VIDEO_STREAMS_TEST_LIST = List.of( private static final List<VideoStream> VIDEO_STREAMS_TEST_LIST = List.of(
generateVideoStream("mpeg_4-720", MediaFormat.MPEG_4, "720p", false), generateVideoStream("mpeg_4-720", MediaFormat.MPEG_4, "720p", false),
generateVideoStream("v3gpp-240", MediaFormat.v3GPP, "240p", false), generateVideoStream("v3gpp-240", MediaFormat.v3GPP, "240p", false),
@ -199,24 +211,28 @@ public class ListHelperTest {
@Test @Test
public void getHighestQualityAudioFormatTest() { public void getHighestQualityAudioFormatTest() {
AudioStream stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getHighestQualityAudioIndex( final Comparator<AudioStream> cmp =
MediaFormat.M4A, AUDIO_STREAMS_TEST_LIST)); ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, false);
AudioStream stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.M4A, AUDIO_STREAMS_TEST_LIST, cmp));
assertEquals(320, stream.getAverageBitrate()); assertEquals(320, stream.getAverageBitrate());
assertEquals(MediaFormat.M4A, stream.getFormat()); assertEquals(MediaFormat.M4A, stream.getFormat());
stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getHighestQualityAudioIndex( stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST)); MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST, cmp));
assertEquals(320, stream.getAverageBitrate()); assertEquals(320, stream.getAverageBitrate());
assertEquals(MediaFormat.WEBMA, stream.getFormat()); assertEquals(MediaFormat.WEBMA, stream.getFormat());
stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getHighestQualityAudioIndex( stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST)); MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST, cmp));
assertEquals(192, stream.getAverageBitrate()); assertEquals(192, stream.getAverageBitrate());
assertEquals(MediaFormat.MP3, stream.getFormat()); assertEquals(MediaFormat.MP3, stream.getFormat());
} }
@Test @Test
public void getHighestQualityAudioFormatPreferredAbsent() { public void getHighestQualityAudioFormatPreferredAbsent() {
final Comparator<AudioStream> cmp =
ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, false);
////////////////////////////////////////// //////////////////////////////////////////
// Doesn't contain the preferred format // // Doesn't contain the preferred format //
@ -227,8 +243,8 @@ public class ListHelperTest {
generateAudioStream("webma-192", MediaFormat.WEBMA, 192)); generateAudioStream("webma-192", MediaFormat.WEBMA, 192));
// List doesn't contains this format // List doesn't contains this format
// It should fallback to the highest bitrate audio no matter what format it is // It should fallback to the highest bitrate audio no matter what format it is
AudioStream stream = testList.get(ListHelper.getHighestQualityAudioIndex( AudioStream stream = testList.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.MP3, testList)); MediaFormat.MP3, testList, cmp));
assertEquals(192, stream.getAverageBitrate()); assertEquals(192, stream.getAverageBitrate());
assertEquals(MediaFormat.WEBMA, stream.getFormat()); assertEquals(MediaFormat.WEBMA, stream.getFormat());
@ -246,44 +262,53 @@ public class ListHelperTest {
generateAudioStream("webma-192-4", MediaFormat.WEBMA, 192))); generateAudioStream("webma-192-4", MediaFormat.WEBMA, 192)));
// List doesn't contains this format, it should fallback to the highest bitrate audio and // List doesn't contains this format, it should fallback to the highest bitrate audio and
// the highest quality format. // the highest quality format.
stream = testList.get(ListHelper.getHighestQualityAudioIndex(MediaFormat.MP3, testList)); stream =
testList.get(ListHelper.getAudioIndexByHighestRank(MediaFormat.MP3, testList, cmp));
assertEquals(192, stream.getAverageBitrate()); assertEquals(192, stream.getAverageBitrate());
assertEquals(MediaFormat.M4A, stream.getFormat()); assertEquals(MediaFormat.M4A, stream.getFormat());
// Adding a new format and bitrate. Adding another stream will have no impact since // Adding a new format and bitrate. Adding another stream will have no impact since
// it's not a preferred format. // it's not a preferred format.
testList.add(generateAudioStream("webma-192-5", MediaFormat.WEBMA, 192)); testList.add(generateAudioStream("webma-192-5", MediaFormat.WEBMA, 192));
stream = testList.get(ListHelper.getHighestQualityAudioIndex(MediaFormat.MP3, testList)); stream =
testList.get(ListHelper.getAudioIndexByHighestRank(MediaFormat.MP3, testList, cmp));
assertEquals(192, stream.getAverageBitrate()); assertEquals(192, stream.getAverageBitrate());
assertEquals(MediaFormat.M4A, stream.getFormat()); assertEquals(MediaFormat.M4A, stream.getFormat());
} }
@Test @Test
public void getHighestQualityAudioNull() { public void getHighestQualityAudioNull() {
assertEquals(-1, ListHelper.getHighestQualityAudioIndex(null, null)); final Comparator<AudioStream> cmp =
assertEquals(-1, ListHelper.getHighestQualityAudioIndex(null, new ArrayList<>())); ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, false);
assertEquals(-1, ListHelper.getAudioIndexByHighestRank(null, null, cmp));
assertEquals(-1, ListHelper.getAudioIndexByHighestRank(null, new ArrayList<>(), cmp));
} }
@Test @Test
public void getLowestQualityAudioFormatTest() { public void getLowestQualityAudioFormatTest() {
AudioStream stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getMostCompactAudioIndex( final Comparator<AudioStream> cmp =
MediaFormat.M4A, AUDIO_STREAMS_TEST_LIST)); ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, true);
AudioStream stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.M4A, AUDIO_STREAMS_TEST_LIST, cmp));
assertEquals(128, stream.getAverageBitrate()); assertEquals(128, stream.getAverageBitrate());
assertEquals(MediaFormat.M4A, stream.getFormat()); assertEquals(MediaFormat.M4A, stream.getFormat());
stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getMostCompactAudioIndex( stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST)); MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST, cmp));
assertEquals(64, stream.getAverageBitrate()); assertEquals(64, stream.getAverageBitrate());
assertEquals(MediaFormat.WEBMA, stream.getFormat()); assertEquals(MediaFormat.WEBMA, stream.getFormat());
stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getMostCompactAudioIndex( stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST)); MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST, cmp));
assertEquals(64, stream.getAverageBitrate()); assertEquals(64, stream.getAverageBitrate());
assertEquals(MediaFormat.MP3, stream.getFormat()); assertEquals(MediaFormat.MP3, stream.getFormat());
} }
@Test @Test
public void getLowestQualityAudioFormatPreferredAbsent() { public void getLowestQualityAudioFormatPreferredAbsent() {
final Comparator<AudioStream> cmp =
ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, true);
////////////////////////////////////////// //////////////////////////////////////////
// Doesn't contain the preferred format // // Doesn't contain the preferred format //
@ -294,14 +319,15 @@ public class ListHelperTest {
generateAudioStream("webma-192-1", MediaFormat.WEBMA, 192))); generateAudioStream("webma-192-1", MediaFormat.WEBMA, 192)));
// List doesn't contains this format // List doesn't contains this format
// It should fallback to the most compact audio no matter what format it is. // It should fallback to the most compact audio no matter what format it is.
AudioStream stream = testList.get(ListHelper.getMostCompactAudioIndex( AudioStream stream = testList.get(ListHelper.getAudioIndexByHighestRank(
MediaFormat.MP3, testList)); MediaFormat.MP3, testList, cmp));
assertEquals(128, stream.getAverageBitrate()); assertEquals(128, stream.getAverageBitrate());
assertEquals(MediaFormat.M4A, stream.getFormat()); assertEquals(MediaFormat.M4A, stream.getFormat());
// WEBMA is more compact than M4A // WEBMA is more compact than M4A
testList.add(generateAudioStream("webma-192-2", MediaFormat.WEBMA, 128)); testList.add(generateAudioStream("webma-192-2", MediaFormat.WEBMA, 128));
stream = testList.get(ListHelper.getMostCompactAudioIndex(MediaFormat.MP3, testList)); stream =
testList.get(ListHelper.getAudioIndexByHighestRank(MediaFormat.MP3, testList, cmp));
assertEquals(128, stream.getAverageBitrate()); assertEquals(128, stream.getAverageBitrate());
assertEquals(MediaFormat.WEBMA, stream.getFormat()); assertEquals(MediaFormat.WEBMA, stream.getFormat());
@ -318,20 +344,52 @@ public class ListHelperTest {
generateAudioStream("m4a-192-3", MediaFormat.M4A, 192))); generateAudioStream("m4a-192-3", MediaFormat.M4A, 192)));
// List doesn't contain this format // List doesn't contain this format
// It should fallback to the most compact audio no matter what format it is. // It should fallback to the most compact audio no matter what format it is.
stream = testList.get(ListHelper.getMostCompactAudioIndex(MediaFormat.MP3, testList)); stream = testList.get(
ListHelper.getAudioIndexByHighestRank(MediaFormat.MP3, testList, cmp));
assertEquals(192, stream.getAverageBitrate()); assertEquals(192, stream.getAverageBitrate());
assertEquals(MediaFormat.WEBMA, stream.getFormat()); assertEquals(MediaFormat.WEBMA, stream.getFormat());
// Should be same as above // Should be same as above
stream = testList.get(ListHelper.getMostCompactAudioIndex(null, testList)); stream = testList.get(
ListHelper.getAudioIndexByHighestRank(null, testList, cmp));
assertEquals(192, stream.getAverageBitrate()); assertEquals(192, stream.getAverageBitrate());
assertEquals(MediaFormat.WEBMA, stream.getFormat()); assertEquals(MediaFormat.WEBMA, stream.getFormat());
} }
@Test @Test
public void getLowestQualityAudioNull() { public void getLowestQualityAudioNull() {
assertEquals(-1, ListHelper.getMostCompactAudioIndex(null, null)); final Comparator<AudioStream> cmp =
assertEquals(-1, ListHelper.getMostCompactAudioIndex(null, new ArrayList<>())); ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, false);
assertEquals(-1, ListHelper.getAudioIndexByHighestRank(null, null, cmp));
assertEquals(-1, ListHelper.getAudioIndexByHighestRank(null, new ArrayList<>(), cmp));
}
@Test
public void getAudioTrack() {
// English language
Comparator<AudioStream> cmp =
ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, false);
AudioStream stream = AUDIO_TRACKS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
null, AUDIO_TRACKS_TEST_LIST, cmp));
assertEquals("en.or", stream.getId());
// German language
cmp = ListHelper.getAudioStreamComparator(Locale.GERMAN, false, false, false);
stream = AUDIO_TRACKS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
null, AUDIO_TRACKS_TEST_LIST, cmp));
assertEquals("de.du", stream.getId());
// German language, but prefer original
cmp = ListHelper.getAudioStreamComparator(Locale.GERMAN, true, false, false);
stream = AUDIO_TRACKS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
null, AUDIO_TRACKS_TEST_LIST, cmp));
assertEquals("en.or", stream.getId());
// Prefer descriptive audio
cmp = ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, true, false);
stream = AUDIO_TRACKS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank(
null, AUDIO_TRACKS_TEST_LIST, cmp));
assertEquals("en.ds", stream.getId());
} }
@Test @Test
@ -390,6 +448,20 @@ public class ListHelperTest {
.build(); .build();
} }
private static AudioStream generateAudioTrack(
@NonNull final String id, @Nullable final String trackId,
@Nullable final Locale locale, @Nullable final AudioTrackType trackType) {
return new AudioStream.Builder()
.setId(id)
.setContent("", true)
.setMediaFormat(MediaFormat.M4A)
.setAverageBitrate(128)
.setAudioTrackId(trackId)
.setAudioLocale(locale)
.setAudioTrackType(trackType)
.build();
}
@NonNull @NonNull
private static VideoStream generateVideoStream(@NonNull final String id, private static VideoStream generateVideoStream(@NonNull final String id,
@Nullable final MediaFormat mediaFormat, @Nullable final MediaFormat mediaFormat,