diff --git a/library/core/src/test/assets/webvtt/with_positioning b/library/core/src/test/assets/webvtt/with_positioning index 6bb86b7c93..7db327ca62 100644 --- a/library/core/src/test/assets/webvtt/with_positioning +++ b/library/core/src/test/assets/webvtt/with_positioning @@ -8,12 +8,12 @@ This is the first subtitle. NOTE Wrong position provided. It should be provided as a percentage value -00:02.345 --> 00:03.456 position:10 align:end size:35% +00:02.345 --> 00:03.456 position:10 align:end This is the second subtitle. NOTE Line as percentage and line alignment -00:04.000 --> 00:05.000 line:45%,end align:middle size:35% +00:04.000 --> 00:05.000 line:45%,end align:middle This is the third subtitle. NOTE Line as absolute negative number and without line alignment. @@ -23,10 +23,10 @@ This is the fourth subtitle. NOTE The position and positioning alignment should be inherited from align. -00:07.000 --> 00:08.000 align:right +00:08.000 --> 00:09.000 align:right This is the fifth subtitle. NOTE In newer drafts, align:middle has been replaced by align:center -00:10.000 --> 00:11.000 line:45%,end align:center size:35% +00:10.000 --> 00:11.000 align:center This is the sixth subtitle. diff --git a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttDecoderTest.java b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttDecoderTest.java index b33439f4f3..e07c412fd7 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttDecoderTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/text/webvtt/WebvttDecoderTest.java @@ -25,16 +25,15 @@ import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.text.style.TypefaceSpan; -import androidx.annotation.Nullable; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.SubtitleDecoderException; import com.google.android.exoplayer2.util.ColorParser; +import com.google.common.collect.Iterables; import com.google.common.truth.Expect; import java.io.IOException; -import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -76,350 +75,254 @@ public class WebvttDecoderTest { public void testDecodeTypical() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_FILE); - // Test event count. assertThat(subtitle.getEventTimeCount()).isEqualTo(4); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); } @Test public void testDecodeWithBom() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_BOM); - // Test event count. assertThat(subtitle.getEventTimeCount()).isEqualTo(4); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); } @Test public void testDecodeTypicalWithBadTimestamps() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_BAD_TIMESTAMPS); - // Test event count. assertThat(subtitle.getEventTimeCount()).isEqualTo(4); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); } @Test public void testDecodeTypicalWithIds() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_IDS_FILE); - // Test event count. assertThat(subtitle.getEventTimeCount()).isEqualTo(4); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); } @Test public void testDecodeTypicalWithComments() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_COMMENTS_FILE); - // test event count assertThat(subtitle.getEventTimeCount()).isEqualTo(4); - // test cues - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(0 + 1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(2 + 1)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); } @Test public void testDecodeWithTags() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_TAGS_FILE); - // Test event count. assertThat(subtitle.getEventTimeCount()).isEqualTo(8); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 4, - /* startTimeUs= */ 4000000, - /* endTimeUs= */ 5000000, - "This is the third subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 6, - /* startTimeUs= */ 6000000, - /* endTimeUs= */ 7000000, - "This is the &subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); + + assertThat(subtitle.getEventTime(4)).isEqualTo(4_000_000L); + assertThat(subtitle.getEventTime(5)).isEqualTo(5_000_000L); + Cue thirdCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4))); + assertThat(thirdCue.text.toString()).isEqualTo("This is the third subtitle."); + + assertThat(subtitle.getEventTime(6)).isEqualTo(6_000_000L); + assertThat(subtitle.getEventTime(7)).isEqualTo(7_000_000L); + Cue fourthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(6))); + assertThat(fourthCue.text.toString()).isEqualTo("This is the &subtitle."); } @Test public void testDecodeWithPositioning() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_POSITIONING_FILE); - // Test event count. + assertThat(subtitle.getEventTimeCount()).isEqualTo(12); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle.", - Alignment.ALIGN_NORMAL, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.1f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_START, - /* size= */ 0.35f, - /* verticalType= */ Cue.TYPE_UNSET); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle.", - Alignment.ALIGN_OPPOSITE, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_END, - /* size= */ 0.35f, - /* verticalType= */ Cue.TYPE_UNSET); - assertCue( - subtitle, - /* eventTimeIndex= */ 4, - /* startTimeUs= */ 4000000, - /* endTimeUs= */ 5000000, - "This is the third subtitle.", - Alignment.ALIGN_CENTER, - /* line= */ 0.45f, - /* lineType= */ Cue.LINE_TYPE_FRACTION, - /* lineAnchor= */ Cue.ANCHOR_TYPE_END, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 0.35f, - /* verticalType= */ Cue.TYPE_UNSET); - assertCue( - subtitle, - /* eventTimeIndex= */ 6, - /* startTimeUs= */ 6000000, - /* endTimeUs= */ 7000000, - "This is the fourth subtitle.", - Alignment.ALIGN_CENTER, - /* line= */ -11.0f, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 1.0f, - /* verticalType= */ Cue.TYPE_UNSET); - assertCue( - subtitle, - /* eventTimeIndex= */ 8, - /* startTimeUs= */ 7000000, - /* endTimeUs= */ 8000000, - "This is the fifth subtitle.", - Alignment.ALIGN_OPPOSITE, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 1.0f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_END, - /* size= */ 1.0f, - /* verticalType= */ Cue.TYPE_UNSET); - assertCue( - subtitle, - /* eventTimeIndex= */ 10, - /* startTimeUs= */ 10000000, - /* endTimeUs= */ 11000000, - "This is the sixth subtitle.", - Alignment.ALIGN_CENTER, - /* line= */ 0.45f, - /* lineType= */ Cue.LINE_TYPE_FRACTION, - /* lineAnchor= */ Cue.ANCHOR_TYPE_END, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 0.35f, - /* verticalType= */ Cue.TYPE_UNSET); + + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + assertThat(firstCue.position).isEqualTo(0.1f); + assertThat(firstCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_START); + assertThat(firstCue.textAlignment).isEqualTo(Alignment.ALIGN_NORMAL); + assertThat(firstCue.size).isEqualTo(0.35f); + // Unspecified values should use WebVTT defaults + assertThat(firstCue.line).isEqualTo(Cue.DIMEN_UNSET); + assertThat(firstCue.lineType).isEqualTo(Cue.LINE_TYPE_NUMBER); + assertThat(firstCue.verticalType).isEqualTo(Cue.TYPE_UNSET); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle."); + // Position is invalid so defaults to 0.5 + assertThat(secondCue.position).isEqualTo(0.5f); + assertThat(secondCue.textAlignment).isEqualTo(Alignment.ALIGN_OPPOSITE); + + assertThat(subtitle.getEventTime(4)).isEqualTo(4_000_000L); + assertThat(subtitle.getEventTime(5)).isEqualTo(5_000_000L); + Cue thirdCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4))); + assertThat(thirdCue.text.toString()).isEqualTo("This is the third subtitle."); + assertThat(thirdCue.line).isEqualTo(0.45f); + assertThat(thirdCue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION); + assertThat(thirdCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END); + assertThat(thirdCue.textAlignment).isEqualTo(Alignment.ALIGN_CENTER); + // Derived from `align:middle`: + assertThat(thirdCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE); + + assertThat(subtitle.getEventTime(6)).isEqualTo(6_000_000L); + assertThat(subtitle.getEventTime(7)).isEqualTo(7_000_000L); + Cue fourthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(6))); + assertThat(fourthCue.text.toString()).isEqualTo("This is the fourth subtitle."); + assertThat(fourthCue.line).isEqualTo(-11f); + assertThat(fourthCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START); + assertThat(fourthCue.textAlignment).isEqualTo(Alignment.ALIGN_CENTER); + // Derived from `align:middle`: + assertThat(fourthCue.position).isEqualTo(0.5f); + assertThat(fourthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE); + + assertThat(subtitle.getEventTime(8)).isEqualTo(8_000_000L); + assertThat(subtitle.getEventTime(9)).isEqualTo(9_000_000L); + Cue fifthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(8))); + assertThat(fifthCue.text.toString()).isEqualTo("This is the fifth subtitle."); + assertThat(fifthCue.textAlignment).isEqualTo(Alignment.ALIGN_OPPOSITE); + // Derived from `align:right`: + assertThat(fifthCue.position).isEqualTo(1.0f); + assertThat(fifthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_END); + + assertThat(subtitle.getEventTime(10)).isEqualTo(10_000_000L); + assertThat(subtitle.getEventTime(11)).isEqualTo(11_000_000L); + Cue sixthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(10))); + assertThat(sixthCue.text.toString()).isEqualTo("This is the sixth subtitle."); + assertThat(sixthCue.textAlignment).isEqualTo(Alignment.ALIGN_CENTER); + // Derived from `align:center`: + assertThat(sixthCue.position).isEqualTo(0.5f); + assertThat(sixthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE); } @Test public void testDecodeWithVertical() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_VERTICAL_FILE); - // Test event count. + assertThat(subtitle.getEventTimeCount()).isEqualTo(6); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "Vertical right-to-left (e.g. Japanese)", - Alignment.ALIGN_CENTER, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 1.0f, - Cue.VERTICAL_TYPE_RL); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "Vertical left-to-right (e.g. Mongolian)", - Alignment.ALIGN_CENTER, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 1.0f, - Cue.VERTICAL_TYPE_LR); - assertCue( - subtitle, - /* eventTimeIndex= */ 4, - /* startTimeUs= */ 4000000, - /* endTimeUs= */ 5000000, - "No vertical setting (i.e. horizontal)", - Alignment.ALIGN_CENTER, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 1.0f, - /* verticalType= */ Cue.TYPE_UNSET); + + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("Vertical right-to-left (e.g. Japanese)"); + assertThat(firstCue.verticalType).isEqualTo(Cue.VERTICAL_TYPE_RL); + + assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("Vertical left-to-right (e.g. Mongolian)"); + assertThat(secondCue.verticalType).isEqualTo(Cue.VERTICAL_TYPE_LR); + + assertThat(subtitle.getEventTime(4)).isEqualTo(4_000_000L); + assertThat(subtitle.getEventTime(5)).isEqualTo(5_000_000L); + Cue thirdCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4))); + assertThat(thirdCue.text.toString()).isEqualTo("No vertical setting (i.e. horizontal)"); + assertThat(thirdCue.verticalType).isEqualTo(Cue.TYPE_UNSET); } @Test public void testDecodeWithBadCueHeader() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_BAD_CUE_HEADER_FILE); - // Test event count. assertThat(subtitle.getEventTimeCount()).isEqualTo(4); - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 4000000, - /* endTimeUs= */ 5000000, - "This is the third subtitle."); + assertThat(subtitle.getEventTime(0)).isEqualTo(0L); + assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L); + Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0))); + assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle."); + + assertThat(subtitle.getEventTime(2)).isEqualTo(4_000_000L); + assertThat(subtitle.getEventTime(3)).isEqualTo(5_000_000L); + Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2))); + assertThat(secondCue.text.toString()).isEqualTo("This is the third subtitle."); } @Test public void testWebvttWithCssStyle() throws Exception { WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_STYLES); - // Test event count. - assertThat(subtitle.getEventTimeCount()).isEqualTo(8); - - // Test cues. - assertCue( - subtitle, - /* eventTimeIndex= */ 0, - /* startTimeUs= */ 0, - /* endTimeUs= */ 1234000, - "This is the first subtitle."); - assertCue( - subtitle, - /* eventTimeIndex= */ 2, - /* startTimeUs= */ 2345000, - /* endTimeUs= */ 3456000, - "This is the second subtitle."); - - Spanned s1 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 0); - Spanned s2 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2345000); - Spanned s3 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 20000000); - Spanned s4 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 25000000); - assertThat(s1) - .hasForegroundColorSpanBetween(0, s1.length()) + Spanned firstCueText = getUniqueSpanTextAt(subtitle, 0); + assertThat(firstCueText.toString()).isEqualTo("This is the first subtitle."); + assertThat(firstCueText) + .hasForegroundColorSpanBetween(0, firstCueText.length()) .withColor(ColorParser.parseCssColor("papayawhip")); - assertThat(s1) - .hasBackgroundColorSpanBetween(0, s1.length()) + assertThat(firstCueText) + .hasBackgroundColorSpanBetween(0, firstCueText.length()) .withColor(ColorParser.parseCssColor("green")); - assertThat(s2) - .hasForegroundColorSpanBetween(0, s2.length()) + + Spanned secondCueText = getUniqueSpanTextAt(subtitle, 2_345_000); + assertThat(secondCueText.toString()).isEqualTo("This is the second subtitle."); + assertThat(secondCueText) + .hasForegroundColorSpanBetween(0, secondCueText.length()) .withColor(ColorParser.parseCssColor("peachpuff")); - assertThat(s3).hasUnderlineSpanBetween(10, s3.length()); - assertThat(s4) - .hasBackgroundColorSpanBetween(0, 16) + Spanned thirdCueText = getUniqueSpanTextAt(subtitle, 20_000_000); + assertThat(thirdCueText.toString()).isEqualTo("This is a reference by element"); + assertThat(thirdCueText).hasUnderlineSpanBetween("This is a ".length(), thirdCueText.length()); + + Spanned fourthCueText = getUniqueSpanTextAt(subtitle, 25_000_000); + assertThat(fourthCueText.toString()).isEqualTo("You are an idiot\nYou don't have the guts"); + assertThat(fourthCueText) + .hasBackgroundColorSpanBetween(0, "You are an idiot".length()) .withColor(ColorParser.parseCssColor("lime")); - assertThat(s4).hasBoldSpanBetween(/* startIndex= */ 17, /* endIndex= */ s4.length()); + assertThat(fourthCueText) + .hasBoldSpanBetween("You are an idiot\n".length(), fourthCueText.length()); } @Test @@ -486,61 +389,4 @@ public class WebvttDecoderTest { private Spanned getUniqueSpanTextAt(WebvttSubtitle sub, long timeUs) { return (Spanned) sub.getCues(timeUs).get(0).text; } - - private void assertCue( - WebvttSubtitle subtitle, int eventTimeIndex, long startTimeUs, long endTimeUs, String text) { - assertCue( - subtitle, - eventTimeIndex, - startTimeUs, - endTimeUs, - text, - /* textAlignment= */ Alignment.ALIGN_CENTER, - /* line= */ Cue.DIMEN_UNSET, - /* lineType= */ Cue.LINE_TYPE_NUMBER, - /* lineAnchor= */ Cue.ANCHOR_TYPE_START, - /* position= */ 0.5f, - /* positionAnchor= */ Cue.ANCHOR_TYPE_MIDDLE, - /* size= */ 1.0f, - /* verticalType= */ Cue.TYPE_UNSET); - } - - private void assertCue( - WebvttSubtitle subtitle, - int eventTimeIndex, - long startTimeUs, - long endTimeUs, - String text, - @Nullable Alignment textAlignment, - float line, - @Cue.LineType int lineType, - @Cue.AnchorType int lineAnchor, - float position, - @Cue.AnchorType int positionAnchor, - float size, - @Cue.VerticalType int verticalType) { - expect - .withMessage("startTimeUs") - .that(subtitle.getEventTime(eventTimeIndex)) - .isEqualTo(startTimeUs); - expect - .withMessage("endTimeUs") - .that(subtitle.getEventTime(eventTimeIndex + 1)) - .isEqualTo(endTimeUs); - List cues = subtitle.getCues(subtitle.getEventTime(eventTimeIndex)); - assertThat(cues).hasSize(1); - // Assert cue properties. - Cue cue = cues.get(0); - expect.withMessage("cue.text").that(cue.text.toString()).isEqualTo(text); - expect.withMessage("cue.textAlignment").that(cue.textAlignment).isEqualTo(textAlignment); - expect.withMessage("cue.line").that(cue.line).isEqualTo(line); - expect.withMessage("cue.lineType").that(cue.lineType).isEqualTo(lineType); - expect.withMessage("cue.lineAnchor").that(cue.lineAnchor).isEqualTo(lineAnchor); - expect.withMessage("cue.position").that(cue.position).isEqualTo(position); - expect.withMessage("cue.positionAnchor").that(cue.positionAnchor).isEqualTo(positionAnchor); - expect.withMessage("cue.size").that(cue.size).isEqualTo(size); - expect.withMessage("cue.verticalType").that(cue.verticalType).isEqualTo(verticalType); - - assertThat(expect.hasFailures()).isFalse(); - } }