Commit 6923316c by ibaker Committed by Ian Baker

Migrate TtmlDecoderTest to use SpannedSubject

PiperOrigin-RevId: 290712014
parent 4da0d0a4
...@@ -15,26 +15,19 @@ ...@@ -15,26 +15,19 @@
*/ */
package com.google.android.exoplayer2.text.ttml; package com.google.android.exoplayer2.text.ttml;
import static com.google.android.exoplayer2.testutil.truth.SpannedSubject.assertThat;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage; import static com.google.common.truth.Truth.assertWithMessage;
import android.text.Layout; import android.text.Layout;
import android.text.Spannable; import android.text.Spanned;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.Subtitle;
import com.google.android.exoplayer2.text.SubtitleDecoderException; import com.google.android.exoplayer2.text.SubtitleDecoderException;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.ColorParser; import com.google.android.exoplayer2.util.ColorParser;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
...@@ -71,35 +64,37 @@ public final class TtmlDecoderTest { ...@@ -71,35 +64,37 @@ public final class TtmlDecoderTest {
@Test @Test
public void testInlineAttributes() throws IOException, SubtitleDecoderException { public void testInlineAttributes() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INLINE_ATTRIBUTES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INLINE_ATTRIBUTES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
TtmlNode root = subtitle.getRoot(); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
TtmlNode body = queryChildrenForTag(root, TtmlNode.TAG_BODY, 0); Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
TtmlNode firstDiv = queryChildrenForTag(body, TtmlNode.TAG_DIV, 0); assertThat(spanned.toString()).isEqualTo("text 1");
TtmlStyle firstPStyle = queryChildrenForTag(firstDiv, TtmlNode.TAG_P, 0).style; assertThat(spanned).hasTypefaceSpanBetween(0, spanned.length()).withFamily("serif");
assertThat(firstPStyle.getFontColor()).isEqualTo(ColorParser.parseTtmlColor("yellow")); assertThat(spanned).hasBoldItalicSpanBetween(0, spanned.length());
assertThat(firstPStyle.getBackgroundColor()).isEqualTo(ColorParser.parseTtmlColor("blue")); assertThat(spanned).hasUnderlineSpanBetween(0, spanned.length());
assertThat(firstPStyle.getFontFamily()).isEqualTo("serif"); assertThat(spanned)
assertThat(firstPStyle.getStyle()).isEqualTo(TtmlStyle.STYLE_BOLD_ITALIC); .hasBackgroundColorSpanBetween(0, spanned.length())
assertThat(firstPStyle.isUnderline()).isTrue(); .withColor(ColorParser.parseTtmlColor("blue"));
assertThat(spanned)
.hasForegroundColorSpanBetween(0, spanned.length())
.withColor(ColorParser.parseTtmlColor("yellow"));
} }
@Test @Test
public void testInheritInlineAttributes() throws IOException, SubtitleDecoderException { public void testInheritInlineAttributes() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INLINE_ATTRIBUTES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INLINE_ATTRIBUTES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
assertSpans(
subtitle, Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
20, assertThat(spanned.toString()).isEqualTo("text 2");
"text 2", assertThat(spanned).hasTypefaceSpanBetween(0, spanned.length()).withFamily("sansSerif");
"sansSerif", assertThat(spanned).hasItalicSpanBetween(0, spanned.length());
TtmlStyle.STYLE_ITALIC, assertThat(spanned).hasStrikethroughSpanBetween(0, spanned.length());
0xFF00FFFF, assertThat(spanned).hasBackgroundColorSpanBetween(0, spanned.length()).withColor(0xFF00FFFF);
ColorParser.parseTtmlColor("lime"), assertThat(spanned)
false, .hasForegroundColorSpanBetween(0, spanned.length())
true, .withColor(ColorParser.parseTtmlColor("lime"));
null);
} }
/** /**
...@@ -117,153 +112,171 @@ public final class TtmlDecoderTest { ...@@ -117,153 +112,171 @@ public final class TtmlDecoderTest {
@Test @Test
public void testLime() throws IOException, SubtitleDecoderException { public void testLime() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INLINE_ATTRIBUTES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INLINE_ATTRIBUTES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
assertSpans(
subtitle, Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
20, assertThat(spanned.toString()).isEqualTo("text 2");
"text 2", assertThat(spanned).hasTypefaceSpanBetween(0, spanned.length()).withFamily("sansSerif");
"sansSerif", assertThat(spanned).hasItalicSpanBetween(0, spanned.length());
TtmlStyle.STYLE_ITALIC, assertThat(spanned).hasStrikethroughSpanBetween(0, spanned.length());
0xFF00FFFF, assertThat(spanned).hasBackgroundColorSpanBetween(0, spanned.length()).withColor(0xFF00FFFF);
0xFF00FF00, assertThat(spanned).hasForegroundColorSpanBetween(0, spanned.length()).withColor(0xFF00FF00);
false,
true,
null);
} }
@Test @Test
public void testInheritGlobalStyle() throws IOException, SubtitleDecoderException { public void testInheritGlobalStyle() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_STYLE_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_STYLE_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(2); assertThat(subtitle.getEventTimeCount()).isEqualTo(2);
assertSpans(
subtitle, Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
10, assertThat(spanned.toString()).isEqualTo("text 1");
"text 1", assertThat(spanned).hasTypefaceSpanBetween(0, spanned.length()).withFamily("serif");
"serif", assertThat(spanned).hasBoldItalicSpanBetween(0, spanned.length());
TtmlStyle.STYLE_BOLD_ITALIC, assertThat(spanned).hasUnderlineSpanBetween(0, spanned.length());
0xFF0000FF, assertThat(spanned).hasBackgroundColorSpanBetween(0, spanned.length()).withColor(0xFF0000FF);
0xFFFFFF00, assertThat(spanned).hasForegroundColorSpanBetween(0, spanned.length()).withColor(0xFFFFFF00);
true,
false,
null);
} }
@Test @Test
public void testInheritGlobalStyleOverriddenByInlineAttributes() public void testInheritGlobalStyleOverriddenByInlineAttributes()
throws IOException, SubtitleDecoderException { throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_STYLE_OVERRIDE_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_STYLE_OVERRIDE_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
assertSpans( Spanned firstCueText = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
subtitle, assertThat(firstCueText.toString()).isEqualTo("text 1");
10, assertThat(firstCueText).hasTypefaceSpanBetween(0, firstCueText.length()).withFamily("serif");
"text 1", assertThat(firstCueText).hasBoldItalicSpanBetween(0, firstCueText.length());
"serif", assertThat(firstCueText).hasUnderlineSpanBetween(0, firstCueText.length());
TtmlStyle.STYLE_BOLD_ITALIC, assertThat(firstCueText)
0xFF0000FF, .hasBackgroundColorSpanBetween(0, firstCueText.length())
0xFFFFFF00, .withColor(0xFF0000FF);
true, assertThat(firstCueText)
false, .hasForegroundColorSpanBetween(0, firstCueText.length())
null); .withColor(0xFFFFFF00);
assertSpans(
subtitle, Spanned secondCueText = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
20, assertThat(secondCueText.toString()).isEqualTo("text 2");
"text 2", assertThat(secondCueText)
"sansSerif", .hasTypefaceSpanBetween(0, secondCueText.length())
TtmlStyle.STYLE_ITALIC, .withFamily("sansSerif");
0xFFFF0000, assertThat(secondCueText).hasItalicSpanBetween(0, secondCueText.length());
0xFFFFFF00, assertThat(secondCueText).hasUnderlineSpanBetween(0, secondCueText.length());
true, assertThat(secondCueText)
false, .hasBackgroundColorSpanBetween(0, secondCueText.length())
null); .withColor(0xFFFF0000);
assertThat(secondCueText)
.hasForegroundColorSpanBetween(0, secondCueText.length())
.withColor(0xFFFFFF00);
} }
@Test @Test
public void testInheritGlobalAndParent() throws IOException, SubtitleDecoderException { public void testInheritGlobalAndParent() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_GLOBAL_AND_PARENT_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_GLOBAL_AND_PARENT_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
assertSpans( Spanned firstCueText = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
subtitle, assertThat(firstCueText.toString()).isEqualTo("text 1");
10, assertThat(firstCueText)
"text 1", .hasTypefaceSpanBetween(0, firstCueText.length())
"sansSerif", .withFamily("sansSerif");
TtmlStyle.STYLE_NORMAL, assertThat(firstCueText).hasStrikethroughSpanBetween(0, firstCueText.length());
0xFFFF0000, assertThat(firstCueText)
ColorParser.parseTtmlColor("lime"), .hasBackgroundColorSpanBetween(0, firstCueText.length())
false, .withColor(0xFFFF0000);
true, assertThat(firstCueText)
Layout.Alignment.ALIGN_CENTER); .hasForegroundColorSpanBetween(0, firstCueText.length())
assertSpans( .withColor(ColorParser.parseTtmlColor("lime"));
subtitle, assertThat(firstCueText)
20, .hasAlignmentSpanBetween(0, firstCueText.length())
"text 2", .withAlignment(Layout.Alignment.ALIGN_CENTER);
"serif",
TtmlStyle.STYLE_BOLD_ITALIC, Spanned secondCueText = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
0xFF0000FF, assertThat(secondCueText.toString()).isEqualTo("text 2");
0xFFFFFF00, assertThat(secondCueText).hasTypefaceSpanBetween(0, secondCueText.length()).withFamily("serif");
true, assertThat(secondCueText).hasBoldItalicSpanBetween(0, secondCueText.length());
true, assertThat(secondCueText).hasUnderlineSpanBetween(0, secondCueText.length());
Layout.Alignment.ALIGN_CENTER); assertThat(secondCueText).hasStrikethroughSpanBetween(0, secondCueText.length());
assertThat(secondCueText)
.hasBackgroundColorSpanBetween(0, secondCueText.length())
.withColor(0xFF0000FF);
assertThat(secondCueText)
.hasForegroundColorSpanBetween(0, secondCueText.length())
.withColor(0xFFFFFF00);
assertThat(secondCueText)
.hasAlignmentSpanBetween(0, secondCueText.length())
.withAlignment(Layout.Alignment.ALIGN_CENTER);
} }
@Test @Test
public void testInheritMultipleStyles() throws IOException, SubtitleDecoderException { public void testInheritMultipleStyles() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(12); assertThat(subtitle.getEventTimeCount()).isEqualTo(12);
assertSpans(
subtitle, Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
10, assertThat(spanned.toString()).isEqualTo("text 1");
"text 1", assertThat(spanned).hasTypefaceSpanBetween(0, spanned.length()).withFamily("sansSerif");
"sansSerif", assertThat(spanned).hasBoldItalicSpanBetween(0, spanned.length());
TtmlStyle.STYLE_BOLD_ITALIC, assertThat(spanned).hasStrikethroughSpanBetween(0, spanned.length());
0xFF0000FF, assertThat(spanned).hasBackgroundColorSpanBetween(0, spanned.length()).withColor(0xFF0000FF);
0xFFFFFF00, assertThat(spanned).hasForegroundColorSpanBetween(0, spanned.length()).withColor(0xFFFFFF00);
false,
true,
null);
} }
@Test @Test
public void testInheritMultipleStylesWithoutLocalAttributes() public void testInheritMultipleStylesWithoutLocalAttributes()
throws IOException, SubtitleDecoderException { throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(12); assertThat(subtitle.getEventTimeCount()).isEqualTo(12);
assertSpans(
subtitle, Spanned secondCueText = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
20, assertThat(secondCueText.toString()).isEqualTo("text 2");
"text 2", assertThat(secondCueText)
"sansSerif", .hasTypefaceSpanBetween(0, secondCueText.length())
TtmlStyle.STYLE_BOLD_ITALIC, .withFamily("sansSerif");
0xFF0000FF, assertThat(secondCueText).hasBoldItalicSpanBetween(0, secondCueText.length());
0xFF000000, assertThat(secondCueText).hasStrikethroughSpanBetween(0, secondCueText.length());
false, assertThat(secondCueText)
true, .hasBackgroundColorSpanBetween(0, secondCueText.length())
null); .withColor(0xFF0000FF);
assertThat(secondCueText)
.hasForegroundColorSpanBetween(0, secondCueText.length())
.withColor(0xFF000000);
} }
@Test @Test
public void testMergeMultipleStylesWithParentStyle() public void testMergeMultipleStylesWithParentStyle()
throws IOException, SubtitleDecoderException { throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(12); assertThat(subtitle.getEventTimeCount()).isEqualTo(12);
assertSpans(
subtitle, Spanned thirdCueText = getOnlyCueTextAtTimeUs(subtitle, 30_000_000);
30, assertThat(thirdCueText.toString()).isEqualTo("text 2.5");
"text 2.5", assertThat(thirdCueText)
"sansSerifInline", .hasTypefaceSpanBetween(0, thirdCueText.length())
TtmlStyle.STYLE_ITALIC, .withFamily("sansSerifInline");
0xFFFF0000, assertThat(thirdCueText).hasItalicSpanBetween(0, thirdCueText.length());
0xFFFFFF00, assertThat(thirdCueText).hasUnderlineSpanBetween(0, thirdCueText.length());
true, assertThat(thirdCueText).hasStrikethroughSpanBetween(0, thirdCueText.length());
true, assertThat(thirdCueText)
null); .hasBackgroundColorSpanBetween(0, thirdCueText.length())
.withColor(0xFFFF0000);
assertThat(thirdCueText)
.hasForegroundColorSpanBetween(0, thirdCueText.length())
.withColor(0xFFFFFF00);
} }
@Test @Test
public void testMultipleRegions() throws IOException, SubtitleDecoderException { public void testMultipleRegions() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(MULTIPLE_REGIONS_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(MULTIPLE_REGIONS_TTML_FILE);
List<Cue> cues = subtitle.getCues(1000000);
List<Cue> cues = subtitle.getCues(1_000_000);
assertThat(cues).hasSize(2); assertThat(cues).hasSize(2);
Cue cue = cues.get(0); Cue cue = cues.get(0);
assertThat(cue.text.toString()).isEqualTo("lorem"); assertThat(cue.text.toString()).isEqualTo("lorem");
...@@ -277,17 +290,13 @@ public final class TtmlDecoderTest { ...@@ -277,17 +290,13 @@ public final class TtmlDecoderTest {
assertThat(cue.line).isEqualTo(10f / 100f); assertThat(cue.line).isEqualTo(10f / 100f);
assertThat(cue.size).isEqualTo(20f / 100f); assertThat(cue.size).isEqualTo(20f / 100f);
cues = subtitle.getCues(5000000); cue = getOnlyCueAtTimeUs(subtitle, 5_000_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text.toString()).isEqualTo("ipsum"); assertThat(cue.text.toString()).isEqualTo("ipsum");
assertThat(cue.position).isEqualTo(40f / 100f); assertThat(cue.position).isEqualTo(40f / 100f);
assertThat(cue.line).isEqualTo(40f / 100f); assertThat(cue.line).isEqualTo(40f / 100f);
assertThat(cue.size).isEqualTo(20f / 100f); assertThat(cue.size).isEqualTo(20f / 100f);
cues = subtitle.getCues(9000000); cue = getOnlyCueAtTimeUs(subtitle, 9_000_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text.toString()).isEqualTo("dolor"); assertThat(cue.text.toString()).isEqualTo("dolor");
assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET); assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET);
assertThat(cue.line).isEqualTo(Cue.DIMEN_UNSET); assertThat(cue.line).isEqualTo(Cue.DIMEN_UNSET);
...@@ -297,19 +306,16 @@ public final class TtmlDecoderTest { ...@@ -297,19 +306,16 @@ public final class TtmlDecoderTest {
// assertEquals(80f / 100f, cue.line); // assertEquals(80f / 100f, cue.line);
// assertEquals(1f, cue.size); // assertEquals(1f, cue.size);
cues = subtitle.getCues(21000000); cue = getOnlyCueAtTimeUs(subtitle, 21_000_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text.toString()).isEqualTo("She first said this"); assertThat(cue.text.toString()).isEqualTo("She first said this");
assertThat(cue.position).isEqualTo(45f / 100f); assertThat(cue.position).isEqualTo(45f / 100f);
assertThat(cue.line).isEqualTo(45f / 100f); assertThat(cue.line).isEqualTo(45f / 100f);
assertThat(cue.size).isEqualTo(35f / 100f); assertThat(cue.size).isEqualTo(35f / 100f);
cues = subtitle.getCues(25000000);
cue = cues.get(0); cue = getOnlyCueAtTimeUs(subtitle, 25_000_000);
assertThat(cue.text.toString()).isEqualTo("She first said this\nThen this"); assertThat(cue.text.toString()).isEqualTo("She first said this\nThen this");
cues = subtitle.getCues(29000000);
assertThat(cues).hasSize(1); cue = getOnlyCueAtTimeUs(subtitle, 29_000_000);
cue = cues.get(0);
assertThat(cue.text.toString()).isEqualTo("She first said this\nThen this\nFinally this"); assertThat(cue.text.toString()).isEqualTo("She first said this\nThen this\nFinally this");
assertThat(cue.position).isEqualTo(45f / 100f); assertThat(cue.position).isEqualTo(45f / 100f);
assertThat(cue.line).isEqualTo(45f / 100f); assertThat(cue.line).isEqualTo(45f / 100f);
...@@ -318,6 +324,7 @@ public final class TtmlDecoderTest { ...@@ -318,6 +324,7 @@ public final class TtmlDecoderTest {
@Test @Test
public void testEmptyStyleAttribute() throws IOException, SubtitleDecoderException { public void testEmptyStyleAttribute() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(12); assertThat(subtitle.getEventTimeCount()).isEqualTo(12);
TtmlNode root = subtitle.getRoot(); TtmlNode root = subtitle.getRoot();
...@@ -330,6 +337,7 @@ public final class TtmlDecoderTest { ...@@ -330,6 +337,7 @@ public final class TtmlDecoderTest {
@Test @Test
public void testNonexistingStyleId() throws IOException, SubtitleDecoderException { public void testNonexistingStyleId() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(12); assertThat(subtitle.getEventTimeCount()).isEqualTo(12);
TtmlNode root = subtitle.getRoot(); TtmlNode root = subtitle.getRoot();
...@@ -343,6 +351,7 @@ public final class TtmlDecoderTest { ...@@ -343,6 +351,7 @@ public final class TtmlDecoderTest {
public void testNonExistingAndExistingStyleIdWithRedundantSpaces() public void testNonExistingAndExistingStyleIdWithRedundantSpaces()
throws IOException, SubtitleDecoderException { throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(INHERIT_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(12); assertThat(subtitle.getEventTimeCount()).isEqualTo(12);
TtmlNode root = subtitle.getRoot(); TtmlNode root = subtitle.getRoot();
...@@ -356,6 +365,7 @@ public final class TtmlDecoderTest { ...@@ -356,6 +365,7 @@ public final class TtmlDecoderTest {
@Test @Test
public void testMultipleChaining() throws IOException, SubtitleDecoderException { public void testMultipleChaining() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(CHAIN_MULTIPLE_STYLES_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(CHAIN_MULTIPLE_STYLES_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(2); assertThat(subtitle.getEventTimeCount()).isEqualTo(2);
Map<String, TtmlStyle> globalStyles = subtitle.getGlobalStyles(); Map<String, TtmlStyle> globalStyles = subtitle.getGlobalStyles();
...@@ -379,6 +389,7 @@ public final class TtmlDecoderTest { ...@@ -379,6 +389,7 @@ public final class TtmlDecoderTest {
@Test @Test
public void testNoUnderline() throws IOException, SubtitleDecoderException { public void testNoUnderline() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(NO_UNDERLINE_LINETHROUGH_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(NO_UNDERLINE_LINETHROUGH_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
TtmlNode root = subtitle.getRoot(); TtmlNode root = subtitle.getRoot();
...@@ -394,6 +405,7 @@ public final class TtmlDecoderTest { ...@@ -394,6 +405,7 @@ public final class TtmlDecoderTest {
@Test @Test
public void testNoLinethrough() throws IOException, SubtitleDecoderException { public void testNoLinethrough() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(NO_UNDERLINE_LINETHROUGH_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(NO_UNDERLINE_LINETHROUGH_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
TtmlNode root = subtitle.getRoot(); TtmlNode root = subtitle.getRoot();
...@@ -409,93 +421,80 @@ public final class TtmlDecoderTest { ...@@ -409,93 +421,80 @@ public final class TtmlDecoderTest {
@Test @Test
public void testFontSizeSpans() throws IOException, SubtitleDecoderException { public void testFontSizeSpans() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(10); assertThat(subtitle.getEventTimeCount()).isEqualTo(10);
List<Cue> cues = subtitle.getCues(10 * 1000000); Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
assertThat(cues).hasSize(1); assertThat(String.valueOf(spanned)).isEqualTo("text 1");
SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasAbsoluteSizeSpanBetween(0, spanned.length()).withAbsoluteSize(32);
assertThat(String.valueOf(spannable)).isEqualTo("text 1");
assertAbsoluteFontSize(spannable, 32);
cues = subtitle.getCues(20 * 1000000); spanned = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
assertThat(cues).hasSize(1); assertThat(spanned.toString()).isEqualTo("text 2");
spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasRelativeSizeSpanBetween(0, spanned.length()).withSizeChange(2.2f);
assertThat(String.valueOf(cues.get(0).text)).isEqualTo("text 2");
assertRelativeFontSize(spannable, 2.2f);
cues = subtitle.getCues(30 * 1000000); spanned = getOnlyCueTextAtTimeUs(subtitle, 30_000_000);
assertThat(cues).hasSize(1); assertThat(spanned.toString()).isEqualTo("text 3");
spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasRelativeSizeSpanBetween(0, spanned.length()).withSizeChange(1.5f);
assertThat(String.valueOf(cues.get(0).text)).isEqualTo("text 3");
assertRelativeFontSize(spannable, 1.5f);
cues = subtitle.getCues(40 * 1000000); spanned = getOnlyCueTextAtTimeUs(subtitle, 40_000_000);
assertThat(cues).hasSize(1); assertThat(spanned.toString()).isEqualTo("two values");
spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasAbsoluteSizeSpanBetween(0, spanned.length()).withAbsoluteSize(16);
assertThat(String.valueOf(cues.get(0).text)).isEqualTo("two values");
assertAbsoluteFontSize(spannable, 16);
cues = subtitle.getCues(50 * 1000000); spanned = getOnlyCueTextAtTimeUs(subtitle, 50_000_000);
assertThat(cues).hasSize(1); assertThat(spanned.toString()).isEqualTo("leading dot");
spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasRelativeSizeSpanBetween(0, spanned.length()).withSizeChange(0.5f);
assertThat(String.valueOf(cues.get(0).text)).isEqualTo("leading dot");
assertRelativeFontSize(spannable, 0.5f);
} }
@Test @Test
public void testFontSizeWithMissingUnitIsIgnored() throws IOException, SubtitleDecoderException { public void testFontSizeWithMissingUnitIsIgnored() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_MISSING_UNIT_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_MISSING_UNIT_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(2); assertThat(subtitle.getEventTimeCount()).isEqualTo(2);
List<Cue> cues = subtitle.getCues(10 * 1000000);
assertThat(cues).hasSize(1); Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned.toString()).isEqualTo("no unit");
assertThat(String.valueOf(spannable)).isEqualTo("no unit"); assertThat(spanned).hasNoRelativeSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class)).hasLength(0); assertThat(spanned).hasNoAbsoluteSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class)).hasLength(0);
} }
@Test @Test
public void testFontSizeWithInvalidValueIsIgnored() throws IOException, SubtitleDecoderException { public void testFontSizeWithInvalidValueIsIgnored() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_INVALID_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_INVALID_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(6); assertThat(subtitle.getEventTimeCount()).isEqualTo(6);
List<Cue> cues = subtitle.getCues(10 * 1000000); Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
assertThat(cues).hasSize(1); assertThat(String.valueOf(spanned)).isEqualTo("invalid");
SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasNoRelativeSizeSpanBetween(0, spanned.length());
assertThat(String.valueOf(spannable)).isEqualTo("invalid"); assertThat(spanned).hasNoAbsoluteSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class)).hasLength(0);
assertThat(spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class)).hasLength(0);
cues = subtitle.getCues(20 * 1000000); spanned = getOnlyCueTextAtTimeUs(subtitle, 20_000_000);
assertThat(cues).hasSize(1); assertThat(String.valueOf(spanned)).isEqualTo("invalid");
spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasNoRelativeSizeSpanBetween(0, spanned.length());
assertThat(String.valueOf(spannable)).isEqualTo("invalid"); assertThat(spanned).hasNoAbsoluteSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class)).hasLength(0);
assertThat(spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class)).hasLength(0);
cues = subtitle.getCues(30 * 1000000); spanned = getOnlyCueTextAtTimeUs(subtitle, 30_000_000);
assertThat(cues).hasSize(1); assertThat(String.valueOf(spanned)).isEqualTo("invalid dot");
spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(spanned).hasNoRelativeSizeSpanBetween(0, spanned.length());
assertThat(String.valueOf(spannable)).isEqualTo("invalid dot"); assertThat(spanned).hasNoAbsoluteSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class)).hasLength(0);
assertThat(spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class)).hasLength(0);
} }
@Test @Test
public void testFontSizeWithEmptyValueIsIgnored() throws IOException, SubtitleDecoderException { public void testFontSizeWithEmptyValueIsIgnored() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_EMPTY_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(FONT_SIZE_EMPTY_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(2); assertThat(subtitle.getEventTimeCount()).isEqualTo(2);
List<Cue> cues = subtitle.getCues(10 * 1000000);
assertThat(cues).hasSize(1); Spanned spanned = getOnlyCueTextAtTimeUs(subtitle, 10_000_000);
SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text; assertThat(String.valueOf(spanned)).isEqualTo("empty");
assertThat(String.valueOf(spannable)).isEqualTo("empty"); assertThat(spanned).hasNoRelativeSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class)).hasLength(0); assertThat(spanned).hasNoAbsoluteSizeSpanBetween(0, spanned.length());
assertThat(spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class)).hasLength(0);
} }
@Test @Test
public void testFrameRate() throws IOException, SubtitleDecoderException { public void testFrameRate() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(FRAME_RATE_TTML_FILE); TtmlSubtitle subtitle = getSubtitle(FRAME_RATE_TTML_FILE);
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
assertThat(subtitle.getEventTime(0)).isEqualTo(1_000_000); assertThat(subtitle.getEventTime(0)).isEqualTo(1_000_000);
assertThat(subtitle.getEventTime(1)).isEqualTo(1_010_000); assertThat(subtitle.getEventTime(1)).isEqualTo(1_010_000);
...@@ -507,9 +506,7 @@ public final class TtmlDecoderTest { ...@@ -507,9 +506,7 @@ public final class TtmlDecoderTest {
public void testBitmapPercentageRegion() throws IOException, SubtitleDecoderException { public void testBitmapPercentageRegion() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(BITMAP_REGION_FILE); TtmlSubtitle subtitle = getSubtitle(BITMAP_REGION_FILE);
List<Cue> cues = subtitle.getCues(1000000); Cue cue = getOnlyCueAtTimeUs(subtitle, 1_000_000);
assertThat(cues).hasSize(1);
Cue cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(24f / 100f); assertThat(cue.position).isEqualTo(24f / 100f);
...@@ -517,9 +514,7 @@ public final class TtmlDecoderTest { ...@@ -517,9 +514,7 @@ public final class TtmlDecoderTest {
assertThat(cue.size).isEqualTo(51f / 100f); assertThat(cue.size).isEqualTo(51f / 100f);
assertThat(cue.bitmapHeight).isEqualTo(12f / 100f); assertThat(cue.bitmapHeight).isEqualTo(12f / 100f);
cues = subtitle.getCues(4000000); cue = getOnlyCueAtTimeUs(subtitle, 4_000_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(21f / 100f); assertThat(cue.position).isEqualTo(21f / 100f);
...@@ -527,9 +522,7 @@ public final class TtmlDecoderTest { ...@@ -527,9 +522,7 @@ public final class TtmlDecoderTest {
assertThat(cue.size).isEqualTo(57f / 100f); assertThat(cue.size).isEqualTo(57f / 100f);
assertThat(cue.bitmapHeight).isEqualTo(6f / 100f); assertThat(cue.bitmapHeight).isEqualTo(6f / 100f);
cues = subtitle.getCues(7500000); cue = getOnlyCueAtTimeUs(subtitle, 7_500_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(24f / 100f); assertThat(cue.position).isEqualTo(24f / 100f);
...@@ -542,9 +535,7 @@ public final class TtmlDecoderTest { ...@@ -542,9 +535,7 @@ public final class TtmlDecoderTest {
public void testBitmapPixelRegion() throws IOException, SubtitleDecoderException { public void testBitmapPixelRegion() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(BITMAP_PIXEL_REGION_FILE); TtmlSubtitle subtitle = getSubtitle(BITMAP_PIXEL_REGION_FILE);
List<Cue> cues = subtitle.getCues(1000000); Cue cue = getOnlyCueAtTimeUs(subtitle, 1_000_000);
assertThat(cues).hasSize(1);
Cue cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(307f / 1280f); assertThat(cue.position).isEqualTo(307f / 1280f);
...@@ -552,9 +543,7 @@ public final class TtmlDecoderTest { ...@@ -552,9 +543,7 @@ public final class TtmlDecoderTest {
assertThat(cue.size).isEqualTo(653f / 1280f); assertThat(cue.size).isEqualTo(653f / 1280f);
assertThat(cue.bitmapHeight).isEqualTo(86f / 720f); assertThat(cue.bitmapHeight).isEqualTo(86f / 720f);
cues = subtitle.getCues(4000000); cue = getOnlyCueAtTimeUs(subtitle, 4_000_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(269f / 1280f); assertThat(cue.position).isEqualTo(269f / 1280f);
...@@ -567,9 +556,7 @@ public final class TtmlDecoderTest { ...@@ -567,9 +556,7 @@ public final class TtmlDecoderTest {
public void testBitmapUnsupportedRegion() throws IOException, SubtitleDecoderException { public void testBitmapUnsupportedRegion() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(BITMAP_UNSUPPORTED_REGION_FILE); TtmlSubtitle subtitle = getSubtitle(BITMAP_UNSUPPORTED_REGION_FILE);
List<Cue> cues = subtitle.getCues(1000000); Cue cue = getOnlyCueAtTimeUs(subtitle, 1_000_000);
assertThat(cues).hasSize(1);
Cue cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET); assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET);
...@@ -577,9 +564,7 @@ public final class TtmlDecoderTest { ...@@ -577,9 +564,7 @@ public final class TtmlDecoderTest {
assertThat(cue.size).isEqualTo(Cue.DIMEN_UNSET); assertThat(cue.size).isEqualTo(Cue.DIMEN_UNSET);
assertThat(cue.bitmapHeight).isEqualTo(Cue.DIMEN_UNSET); assertThat(cue.bitmapHeight).isEqualTo(Cue.DIMEN_UNSET);
cues = subtitle.getCues(4000000); cue = getOnlyCueAtTimeUs(subtitle, 4_000_000);
assertThat(cues).hasSize(1);
cue = cues.get(0);
assertThat(cue.text).isNull(); assertThat(cue.text).isNull();
assertThat(cue.bitmap).isNotNull(); assertThat(cue.bitmap).isNotNull();
assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET); assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET);
...@@ -592,122 +577,29 @@ public final class TtmlDecoderTest { ...@@ -592,122 +577,29 @@ public final class TtmlDecoderTest {
public void testVerticalText() throws IOException, SubtitleDecoderException { public void testVerticalText() throws IOException, SubtitleDecoderException {
TtmlSubtitle subtitle = getSubtitle(VERTICAL_TEXT_FILE); TtmlSubtitle subtitle = getSubtitle(VERTICAL_TEXT_FILE);
List<Cue> firstCues = subtitle.getCues(10_000_000); Cue firstCue = getOnlyCueAtTimeUs(subtitle, 10_000_000);
assertThat(firstCues).hasSize(1);
Cue firstCue = firstCues.get(0);
assertThat(firstCue.verticalType).isEqualTo(Cue.VERTICAL_TYPE_RL); assertThat(firstCue.verticalType).isEqualTo(Cue.VERTICAL_TYPE_RL);
List<Cue> secondCues = subtitle.getCues(20_000_000); Cue secondCue = getOnlyCueAtTimeUs(subtitle, 20_000_000);
assertThat(secondCues).hasSize(1);
Cue secondCue = secondCues.get(0);
assertThat(secondCue.verticalType).isEqualTo(Cue.VERTICAL_TYPE_LR); assertThat(secondCue.verticalType).isEqualTo(Cue.VERTICAL_TYPE_LR);
List<Cue> thirdCues = subtitle.getCues(30_000_000); Cue thirdCue = getOnlyCueAtTimeUs(subtitle, 30_000_000);
assertThat(thirdCues).hasSize(1);
Cue thirdCue = thirdCues.get(0);
assertThat(thirdCue.verticalType).isEqualTo(Cue.TYPE_UNSET); assertThat(thirdCue.verticalType).isEqualTo(Cue.TYPE_UNSET);
} }
private void assertSpans( private static Spanned getOnlyCueTextAtTimeUs(Subtitle subtitle, long timeUs) {
TtmlSubtitle subtitle, Cue cue = getOnlyCueAtTimeUs(subtitle, timeUs);
int second, assertThat(cue.text).isInstanceOf(Spanned.class);
String text, return (Spanned) Assertions.checkNotNull(cue.text);
String font,
int fontStyle,
int backgroundColor,
int color,
boolean isUnderline,
boolean isLinethrough,
Layout.Alignment alignment) {
long timeUs = second * 1000000L;
List<Cue> cues = subtitle.getCues(timeUs);
assertThat(cues).hasSize(1);
assertThat(String.valueOf(cues.get(0).text)).isEqualTo(text);
assertWithMessage("single cue expected for timeUs: " + timeUs).that(cues.size()).isEqualTo(1);
SpannableStringBuilder spannable = (SpannableStringBuilder) cues.get(0).text;
assertFont(spannable, font);
assertStyle(spannable, fontStyle);
assertUnderline(spannable, isUnderline);
assertStrikethrough(spannable, isLinethrough);
assertUnderline(spannable, isUnderline);
assertBackground(spannable, backgroundColor);
assertForeground(spannable, color);
assertAlignment(spannable, alignment);
} }
private void assertAbsoluteFontSize(Spannable spannable, int absoluteFontSize) { private static Cue getOnlyCueAtTimeUs(Subtitle subtitle, long timeUs) {
AbsoluteSizeSpan[] absoluteSizeSpans = List<Cue> cues = subtitle.getCues(timeUs);
spannable.getSpans(0, spannable.length(), AbsoluteSizeSpan.class); assertThat(cues).hasSize(1);
assertThat(absoluteSizeSpans).hasLength(1); return cues.get(0);
assertThat(absoluteSizeSpans[0].getSize()).isEqualTo(absoluteFontSize);
}
private void assertRelativeFontSize(Spannable spannable, float relativeFontSize) {
RelativeSizeSpan[] relativeSizeSpans =
spannable.getSpans(0, spannable.length(), RelativeSizeSpan.class);
assertThat(relativeSizeSpans).hasLength(1);
assertThat(relativeSizeSpans[0].getSizeChange()).isEqualTo(relativeFontSize);
}
private void assertFont(Spannable spannable, String font) {
TypefaceSpan[] typefaceSpans = spannable.getSpans(0, spannable.length(), TypefaceSpan.class);
assertThat(typefaceSpans[typefaceSpans.length - 1].getFamily()).isEqualTo(font);
}
private void assertStyle(Spannable spannable, int fontStyle) {
StyleSpan[] styleSpans = spannable.getSpans(0, spannable.length(), StyleSpan.class);
assertThat(styleSpans[styleSpans.length - 1].getStyle()).isEqualTo(fontStyle);
}
private void assertUnderline(Spannable spannable, boolean isUnderline) {
UnderlineSpan[] underlineSpans = spannable.getSpans(0, spannable.length(), UnderlineSpan.class);
assertWithMessage(isUnderline ? "must be underlined" : "must not be underlined")
.that(underlineSpans)
.hasLength(isUnderline ? 1 : 0);
}
private void assertStrikethrough(Spannable spannable, boolean isStrikethrough) {
StrikethroughSpan[] striketroughSpans =
spannable.getSpans(0, spannable.length(), StrikethroughSpan.class);
assertWithMessage(isStrikethrough ? "must be strikethrough" : "must not be strikethrough")
.that(striketroughSpans)
.hasLength(isStrikethrough ? 1 : 0);
}
private void assertBackground(Spannable spannable, int backgroundColor) {
BackgroundColorSpan[] backgroundColorSpans =
spannable.getSpans(0, spannable.length(), BackgroundColorSpan.class);
if (backgroundColor != 0) {
assertThat(backgroundColorSpans[backgroundColorSpans.length - 1].getBackgroundColor())
.isEqualTo(backgroundColor);
} else {
assertThat(backgroundColorSpans).hasLength(0);
}
}
private void assertForeground(Spannable spannable, int foregroundColor) {
ForegroundColorSpan[] foregroundColorSpans =
spannable.getSpans(0, spannable.length(), ForegroundColorSpan.class);
assertThat(foregroundColorSpans[foregroundColorSpans.length - 1].getForegroundColor())
.isEqualTo(foregroundColor);
}
private void assertAlignment(Spannable spannable, Layout.Alignment alignment) {
if (alignment != null) {
AlignmentSpan.Standard[] alignmentSpans =
spannable.getSpans(0, spannable.length(), AlignmentSpan.Standard.class);
assertThat(alignmentSpans).hasLength(1);
assertThat(alignmentSpans[0].getAlignment()).isEqualTo(alignment);
} else {
assertThat(spannable.getSpans(0, spannable.length(), AlignmentSpan.Standard.class))
.hasLength(0);
}
} }
private TtmlNode queryChildrenForTag(TtmlNode node, String tag, int pos) { private static TtmlNode queryChildrenForTag(TtmlNode node, String tag, int pos) {
int count = 0; int count = 0;
for (int i = 0; i < node.getChildCount(); i++) { for (int i = 0; i < node.getChildCount(); i++) {
if (tag.equals(node.getChild(i).tag)) { if (tag.equals(node.getChild(i).tag)) {
...@@ -719,7 +611,8 @@ public final class TtmlDecoderTest { ...@@ -719,7 +611,8 @@ public final class TtmlDecoderTest {
throw new IllegalStateException("tag not found"); throw new IllegalStateException("tag not found");
} }
private TtmlSubtitle getSubtitle(String file) throws IOException, SubtitleDecoderException { private static TtmlSubtitle getSubtitle(String file)
throws IOException, SubtitleDecoderException {
TtmlDecoder ttmlDecoder = new TtmlDecoder(); TtmlDecoder ttmlDecoder = new TtmlDecoder();
byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), file); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), file);
return (TtmlSubtitle) ttmlDecoder.decode(bytes, bytes.length, false); return (TtmlSubtitle) ttmlDecoder.decode(bytes, bytes.length, false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment