From 61a14765f3bc23c93a004df03b063c4c6014706e Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Sun, 19 Mar 2023 22:31:31 +0100 Subject: [PATCH] fix: ListHelper tests --- .../org/schabi/newpipe/util/ListHelper.java | 34 +++-- .../schabi/newpipe/util/ListHelperTest.java | 122 ++++++++++++++---- 2 files changed, 123 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java index 1be020eae..002a8014e 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ListHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ListHelper.java @@ -222,7 +222,8 @@ public final class ListHelper { final HashMap collectedStreams = new HashMap<>(); - final Comparator cmp = getAudioStreamFormatComparator(context); + final Comparator cmp = + getAudioStreamFormatComparator(isLimitingDataUsage(context)); for (final AudioStream stream : audioStreams) { 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 * @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 audioStreams, final Comparator comparator) { 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. * - * @param context App context + * @param limitDataUsage choose low bitrate audio stream * @return Comparator */ private static Comparator getAudioStreamFormatComparator( - @NonNull final Context context) { - final boolean limitDataUsage = isLimitingDataUsage(context); + final boolean limitDataUsage) { final List formatRanking = limitDataUsage ? AUDIO_FORMAT_EFFICIENCY_RANKING : AUDIO_FORMAT_QUALITY_RANKING; @@ -664,14 +664,32 @@ public final class ListHelper { @NonNull final Context context) { final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + final Locale preferredLanguage = Localization.getPreferredLocale(context); final boolean preferOriginalAudio = preferences.getBoolean(context.getString(R.string.prefer_original_audio_key), false); final boolean preferDescriptiveAudio = preferences.getBoolean(context.getString(R.string.prefer_descriptive_audio_key), 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 getAudioStreamComparator(final Locale preferredLanguage, + final boolean preferOriginalAudio, + final boolean preferDescriptiveAudio, + final boolean limitDataUsage) { + final String langCode = preferredLanguage.getISO3Language(); final List trackTypeRanking = preferDescriptiveAudio ? AUDIO_TRACK_TYPE_RANKING_DESCRIPTIVE : AUDIO_TRACK_TYPE_RANKING; @@ -683,13 +701,13 @@ public final class ListHelper { return 0; }).thenComparing(AudioStream::getAudioLocale, Comparator.nullsFirst(Comparator.comparing( - locale -> locale.getISO3Language().equals(preferredLanguage)))) + locale -> locale.getISO3Language().equals(langCode)))) .thenComparing(AudioStream::getAudioTrackType, Comparator.nullsLast(Comparator.comparingInt(trackTypeRanking::indexOf))) .thenComparing(AudioStream::getAudioLocale, Comparator.nullsFirst(Comparator.comparing( locale -> locale.getISO3Language().equals( Locale.ENGLISH.getISO3Language())))) - .thenComparing(getAudioStreamFormatComparator(context)); + .thenComparing(getAudioStreamFormatComparator(limitDataUsage)); } } diff --git a/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java b/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java index 8a75b1b4e..173892e5a 100644 --- a/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java +++ b/app/src/test/java/org/schabi/newpipe/util/ListHelperTest.java @@ -3,10 +3,13 @@ package org.schabi.newpipe.util; import org.junit.Test; import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.stream.AudioStream; +import org.schabi.newpipe.extractor.stream.AudioTrackType; import org.schabi.newpipe.extractor.stream.VideoStream; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; +import java.util.Locale; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -29,6 +32,15 @@ public class ListHelperTest { generateAudioStream("mp3-192", MediaFormat.MP3, 192), generateAudioStream("webma-320", MediaFormat.WEBMA, 320)); + private static final List 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 VIDEO_STREAMS_TEST_LIST = List.of( generateVideoStream("mpeg_4-720", MediaFormat.MPEG_4, "720p", false), generateVideoStream("v3gpp-240", MediaFormat.v3GPP, "240p", false), @@ -199,24 +211,28 @@ public class ListHelperTest { @Test public void getHighestQualityAudioFormatTest() { - AudioStream stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getHighestQualityAudioIndex( - MediaFormat.M4A, AUDIO_STREAMS_TEST_LIST)); + final Comparator cmp = + 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(MediaFormat.M4A, stream.getFormat()); - stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getHighestQualityAudioIndex( - MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST)); + stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank( + MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST, cmp)); assertEquals(320, stream.getAverageBitrate()); assertEquals(MediaFormat.WEBMA, stream.getFormat()); - stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getHighestQualityAudioIndex( - MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST)); + stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank( + MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST, cmp)); assertEquals(192, stream.getAverageBitrate()); assertEquals(MediaFormat.MP3, stream.getFormat()); } @Test public void getHighestQualityAudioFormatPreferredAbsent() { + final Comparator cmp = + ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, false); ////////////////////////////////////////// // Doesn't contain the preferred format // @@ -227,8 +243,8 @@ public class ListHelperTest { generateAudioStream("webma-192", MediaFormat.WEBMA, 192)); // List doesn't contains this format // It should fallback to the highest bitrate audio no matter what format it is - AudioStream stream = testList.get(ListHelper.getHighestQualityAudioIndex( - MediaFormat.MP3, testList)); + AudioStream stream = testList.get(ListHelper.getAudioIndexByHighestRank( + MediaFormat.MP3, testList, cmp)); assertEquals(192, stream.getAverageBitrate()); assertEquals(MediaFormat.WEBMA, stream.getFormat()); @@ -246,44 +262,53 @@ public class ListHelperTest { generateAudioStream("webma-192-4", MediaFormat.WEBMA, 192))); // List doesn't contains this format, it should fallback to the highest bitrate audio and // 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(MediaFormat.M4A, stream.getFormat()); // Adding a new format and bitrate. Adding another stream will have no impact since // it's not a preferred format. 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(MediaFormat.M4A, stream.getFormat()); } @Test public void getHighestQualityAudioNull() { - assertEquals(-1, ListHelper.getHighestQualityAudioIndex(null, null)); - assertEquals(-1, ListHelper.getHighestQualityAudioIndex(null, new ArrayList<>())); + final Comparator cmp = + 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 getLowestQualityAudioFormatTest() { - AudioStream stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getMostCompactAudioIndex( - MediaFormat.M4A, AUDIO_STREAMS_TEST_LIST)); + final Comparator cmp = + 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(MediaFormat.M4A, stream.getFormat()); - stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getMostCompactAudioIndex( - MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST)); + stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank( + MediaFormat.WEBMA, AUDIO_STREAMS_TEST_LIST, cmp)); assertEquals(64, stream.getAverageBitrate()); assertEquals(MediaFormat.WEBMA, stream.getFormat()); - stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getMostCompactAudioIndex( - MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST)); + stream = AUDIO_STREAMS_TEST_LIST.get(ListHelper.getAudioIndexByHighestRank( + MediaFormat.MP3, AUDIO_STREAMS_TEST_LIST, cmp)); assertEquals(64, stream.getAverageBitrate()); assertEquals(MediaFormat.MP3, stream.getFormat()); } @Test public void getLowestQualityAudioFormatPreferredAbsent() { + final Comparator cmp = + ListHelper.getAudioStreamComparator(Locale.ENGLISH, false, false, true); ////////////////////////////////////////// // Doesn't contain the preferred format // @@ -294,14 +319,15 @@ public class ListHelperTest { generateAudioStream("webma-192-1", MediaFormat.WEBMA, 192))); // List doesn't contains this format // It should fallback to the most compact audio no matter what format it is. - AudioStream stream = testList.get(ListHelper.getMostCompactAudioIndex( - MediaFormat.MP3, testList)); + AudioStream stream = testList.get(ListHelper.getAudioIndexByHighestRank( + MediaFormat.MP3, testList, cmp)); assertEquals(128, stream.getAverageBitrate()); assertEquals(MediaFormat.M4A, stream.getFormat()); // WEBMA is more compact than M4A 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(MediaFormat.WEBMA, stream.getFormat()); @@ -318,20 +344,52 @@ public class ListHelperTest { generateAudioStream("m4a-192-3", MediaFormat.M4A, 192))); // List doesn't contain this format // 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(MediaFormat.WEBMA, stream.getFormat()); // 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(MediaFormat.WEBMA, stream.getFormat()); } @Test public void getLowestQualityAudioNull() { - assertEquals(-1, ListHelper.getMostCompactAudioIndex(null, null)); - assertEquals(-1, ListHelper.getMostCompactAudioIndex(null, new ArrayList<>())); + final Comparator cmp = + 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 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 @@ -390,6 +448,20 @@ public class ListHelperTest { .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 private static VideoStream generateVideoStream(@NonNull final String id, @Nullable final MediaFormat mediaFormat,