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