Commit 448db894 by ibaker Committed by Oliver Woodman

Add TypefaceSpan and hasNoFooSpanBetween() support to SpannedSubject

Use these to migrate the last WebvttDecoderTest method to SpannedSubject

PiperOrigin-RevId: 288688620
parent ee091e6a
...@@ -20,7 +20,7 @@ STYLE ...@@ -20,7 +20,7 @@ STYLE
id id
00:00.000 --> 00:01.001 00:00.000 --> 00:01.001
This should be underlined and <lang.class1.class2> courier and violet. This should be underlined and <lang.class1.class2>courier and violet.
íd íd
00:02.000 --> 00:02.001 00:02.000 --> 00:02.001
...@@ -31,10 +31,10 @@ _id ...@@ -31,10 +31,10 @@ _id
This <lang.class.another>should be courier and bold. This <lang.class.another>should be courier and bold.
00:04.000 --> 00:04.001 00:04.000 --> 00:04.001
This <v Strider Trancos> shouldn't be bold.</v> This <v Strider Trancos>shouldn't be bold.</v>
This <v.class.clazz Strider Trancos> should be bold. This <v.class.clazz Strider Trancos>should be bold.
anId anId
00:05.000 --> 00:05.001 00:05.000 --> 00:05.001
This is <v.class1.class3.class2 Pipo> specific </v> This is <v.class1.class3.class2 Pipo>specific</v>
<v.class1.class3.class2 Robert> But this is more italic</v> <v.class1.class3.class2 Robert>But this is more italic</v>
...@@ -19,17 +19,14 @@ import static com.google.android.exoplayer2.testutil.truth.SpannedSubject.assert ...@@ -19,17 +19,14 @@ import static com.google.android.exoplayer2.testutil.truth.SpannedSubject.assert
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.graphics.Typeface;
import android.text.Layout.Alignment; import android.text.Layout.Alignment;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
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.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 com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.truth.Expect; import com.google.common.truth.Expect;
...@@ -328,41 +325,39 @@ public class WebvttDecoderTest { ...@@ -328,41 +325,39 @@ public class WebvttDecoderTest {
@Test @Test
public void testWithComplexCssSelectors() throws Exception { public void testWithComplexCssSelectors() throws Exception {
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_COMPLEX_SELECTORS); WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_COMPLEX_SELECTORS);
Spanned text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 0); Spanned firstCueText = getUniqueSpanTextAt(subtitle, /* timeUs= */ 0);
assertThat(text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class)) assertThat(firstCueText)
.hasLength(1); .hasForegroundColorSpanBetween(
assertThat( "This should be underlined and ".length(), firstCueText.length())
text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class)[0] .withColor(ColorParser.parseCssColor("violet"));
.getForegroundColor()) assertThat(firstCueText)
.isEqualTo(0xFFEE82EE); .hasTypefaceSpanBetween("This should be underlined and ".length(), firstCueText.length())
assertThat(text.getSpans(/* start= */ 30, text.length(), TypefaceSpan.class)).hasLength(1); .withFamily("courier");
assertThat(text.getSpans(/* start= */ 30, text.length(), TypefaceSpan.class)[0].getFamily())
.isEqualTo("courier"); Spanned secondCueText = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2_000_000);
assertThat(secondCueText)
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2000000); .hasTypefaceSpanBetween("This ".length(), secondCueText.length())
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)).hasLength(1); .withFamily("courier");
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)[0].getFamily()) assertThat(secondCueText)
.isEqualTo("courier"); .hasNoForegroundColorSpanBetween("This ".length(), secondCueText.length());
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2500000); Spanned thirdCueText = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2_500_000);
assertThat(text.getSpans(/* start= */ 5, text.length(), StyleSpan.class)).hasLength(1); assertThat(thirdCueText).hasBoldSpanBetween("This ".length(), thirdCueText.length());
assertThat(text.getSpans(/* start= */ 5, text.length(), StyleSpan.class)[0].getStyle()) assertThat(thirdCueText)
.isEqualTo(Typeface.BOLD); .hasTypefaceSpanBetween("This ".length(), thirdCueText.length())
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)).hasLength(1); .withFamily("courier");
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)[0].getFamily())
.isEqualTo("courier"); Spanned fourthCueText = getUniqueSpanTextAt(subtitle, /* timeUs= */ 4_000_000);
assertThat(fourthCueText)
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 4000000); .hasNoStyleSpanBetween("This ".length(), "shouldn't be bold.".length());
assertThat(text.getSpans(/* start= */ 6, /* end= */ 22, StyleSpan.class)).hasLength(0); assertThat(fourthCueText)
assertThat(text.getSpans(/* start= */ 30, text.length(), StyleSpan.class)).hasLength(1); .hasBoldSpanBetween("This shouldn't be bold.\nThis ".length(), fourthCueText.length());
assertThat(text.getSpans(/* start= */ 30, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.BOLD); Spanned fifthCueText = getUniqueSpanTextAt(subtitle, /* timeUs= */ 5_000_000);
assertThat(fifthCueText)
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 5000000); .hasNoStyleSpanBetween("This is ".length(), "This is specific".length());
assertThat(text.getSpans(/* start= */ 9, /* end= */ 17, StyleSpan.class)).hasLength(0); assertThat(fifthCueText)
assertThat(text.getSpans(/* start= */ 19, text.length(), StyleSpan.class)).hasLength(1); .hasItalicSpanBetween("This is specific\n".length(), fifthCueText.length());
assertThat(text.getSpans(/* start= */ 19, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.ITALIC);
} }
@Test @Test
...@@ -387,6 +382,6 @@ public class WebvttDecoderTest { ...@@ -387,6 +382,6 @@ public class WebvttDecoderTest {
} }
private Spanned getUniqueSpanTextAt(WebvttSubtitle sub, long timeUs) { private Spanned getUniqueSpanTextAt(WebvttSubtitle sub, long timeUs) {
return (Spanned) sub.getCues(timeUs).get(0).text; return (Spanned) Assertions.checkNotNull(sub.getCues(timeUs).get(0).text);
} }
} }
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