diff --git a/libraries/extractor/src/test/java/androidx/media3/extractor/text/webvtt/WebvttParserTest.java b/libraries/extractor/src/test/java/androidx/media3/extractor/text/webvtt/WebvttParserTest.java index d79127bd0e..47eb45aa3d 100644 --- a/libraries/extractor/src/test/java/androidx/media3/extractor/text/webvtt/WebvttParserTest.java +++ b/libraries/extractor/src/test/java/androidx/media3/extractor/text/webvtt/WebvttParserTest.java @@ -27,6 +27,7 @@ import androidx.media3.common.text.TextAnnotation; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.ColorParser; import androidx.media3.extractor.text.CuesWithTiming; +import androidx.media3.extractor.text.SubtitleParser; import androidx.media3.test.utils.TestUtil; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -76,7 +77,11 @@ public class WebvttParserTest { public void parseEmpty() throws IOException { WebvttParser parser = new WebvttParser(); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), EMPTY_FILE); - assertThrows(IllegalArgumentException.class, () -> parser.parse(bytes)); + assertThrows( + IllegalArgumentException.class, + () -> + parser.parse( + bytes, SubtitleParser.OutputOptions.allCues(), unusedCuesWithTiming -> {})); } @Test @@ -96,6 +101,38 @@ public class WebvttParserTest { assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); } + @Test + public void parseTypical_withStartTime() throws Exception { + ImmutableList allCues = + getCuesForTestAsset(TYPICAL_FILE, SubtitleParser.OutputOptions.onlyCuesAfter(2_000_000)); + + assertThat(allCues).hasSize(1); + + assertThat(allCues.get(0).startTimeUs).isEqualTo(2_345_000L); + assertThat(allCues.get(0).durationUs).isEqualTo(3_456_000L - 2_345_000L); + Cue secondCue = Iterables.getOnlyElement(allCues.get(0).cues); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); + } + + @Test + public void parseTypical_withStartTimeAndRemainingCues() throws Exception { + ImmutableList allCues = + getCuesForTestAsset( + TYPICAL_FILE, SubtitleParser.OutputOptions.cuesAfterThenRemainingCuesBefore(2_000_000)); + + assertThat(allCues).hasSize(2); + + assertThat(allCues.get(0).startTimeUs).isEqualTo(2_345_000L); + assertThat(allCues.get(0).durationUs).isEqualTo(3_456_000L - 2_345_000L); + Cue secondCue = Iterables.getOnlyElement(allCues.get(0).cues); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); + + assertThat(allCues.get(1).startTimeUs).isEqualTo(0L); + assertThat(allCues.get(1).durationUs).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(allCues.get(1).cues); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + } + @Test public void parseWithBom() throws Exception { List allCues = getCuesForTestAsset(WITH_BOM); @@ -558,10 +595,17 @@ public class WebvttParserTest { "Combine ".length(), "Combine 0004".length()); } - private List getCuesForTestAsset(String asset) throws IOException { + private ImmutableList getCuesForTestAsset(String asset) throws IOException { + return getCuesForTestAsset(asset, SubtitleParser.OutputOptions.allCues()); + } + + private ImmutableList getCuesForTestAsset( + String asset, SubtitleParser.OutputOptions outputOptions) throws IOException { WebvttParser parser = new WebvttParser(); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), asset); - return parser.parse(bytes); + ImmutableList.Builder result = ImmutableList.builder(); + parser.parse(bytes, outputOptions, /* output= */ result::add); + return result.build(); } private Spanned getUniqueSpanTextAt(CuesWithTiming cuesWithTiming) {