Commit 0587180f by ibaker Committed by Ian Baker

Make SpannedSubject more fluent

I decided the flags bit was a bit unclear so I played around with this

It's also needed for more 'complex' assertions like colors - I didn't
want to just chuck in a fourth int parameter to create:
hasForegroundColorSpan(int start, int end, int flags, int color)

PiperOrigin-RevId: 287989424
parent 1c0e6978
...@@ -34,8 +34,7 @@ public final class WebvttCueParserTest { ...@@ -34,8 +34,7 @@ public final class WebvttCueParserTest {
+ "This <u.style1.style2 some stuff>is</u> text with <b.foo><i.bar>html</i></b> tags"); + "This <u.style1.style2 some stuff>is</u> text with <b.foo><i.bar>html</i></b> tags");
assertThat(text.toString()).isEqualTo("This is text with html tags"); assertThat(text.toString()).isEqualTo("This is text with html tags");
assertThat(text) assertThat(text).hasUnderlineSpanBetween("This ".length(), "This is".length());
.hasUnderlineSpan("This ".length(), "This is".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
assertThat(text) assertThat(text)
.hasBoldItalicSpan( .hasBoldItalicSpan(
"This is text with ".length(), "This is text with ".length(),
...@@ -59,10 +58,7 @@ public final class WebvttCueParserTest { ...@@ -59,10 +58,7 @@ public final class WebvttCueParserTest {
assertThat(text.toString()).isEqualTo("An unclosed u tag with italic inside"); assertThat(text.toString()).isEqualTo("An unclosed u tag with italic inside");
assertThat(text) assertThat(text)
.hasUnderlineSpan( .hasUnderlineSpanBetween("An ".length(), "An unclosed u tag with italic inside".length());
"An ".length(),
"An unclosed u tag with italic inside".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
assertThat(text) assertThat(text)
.hasItalicSpan( .hasItalicSpan(
"An unclosed u tag with ".length(), "An unclosed u tag with ".length(),
...@@ -81,10 +77,9 @@ public final class WebvttCueParserTest { ...@@ -81,10 +77,9 @@ public final class WebvttCueParserTest {
"An italic tag with unclosed underline".length(), "An italic tag with unclosed underline".length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
assertThat(text) assertThat(text)
.hasUnderlineSpan( .hasUnderlineSpanBetween(
"An italic tag with unclosed ".length(), "An italic tag with unclosed ".length(),
"An italic tag with unclosed underline".length(), "An italic tag with unclosed underline".length());
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
@Test @Test
...@@ -95,8 +90,7 @@ public final class WebvttCueParserTest { ...@@ -95,8 +90,7 @@ public final class WebvttCueParserTest {
assertThat(text.toString()).isEqualTo(expectedText); assertThat(text.toString()).isEqualTo(expectedText);
assertThat(text).hasBoldSpan(0, expectedText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); assertThat(text).hasBoldSpan(0, expectedText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Text between the <u> tags is underlined. // Text between the <u> tags is underlined.
assertThat(text) assertThat(text).hasUnderlineSpanBetween(0, "Overlapping u and".length());
.hasUnderlineSpan(0, "Overlapping u and".length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Only text from <i> to <\\u> is italic (unexpected - but simplifies the parsing). // Only text from <i> to <\\u> is italic (unexpected - but simplifies the parsing).
assertThat(text) assertThat(text)
.hasItalicSpan( .hasItalicSpan(
......
...@@ -15,23 +15,23 @@ ...@@ -15,23 +15,23 @@
*/ */
package com.google.android.exoplayer2.text.webvtt; package com.google.android.exoplayer2.text.webvtt;
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 org.junit.Assert.fail; import static org.junit.Assert.fail;
import android.graphics.Typeface; 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.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
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.ColorParser;
import com.google.common.truth.Expect; import com.google.common.truth.Expect;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
...@@ -403,14 +403,25 @@ public class WebvttDecoderTest { ...@@ -403,14 +403,25 @@ public class WebvttDecoderTest {
Spanned s2 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2345000); Spanned s2 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2345000);
Spanned s3 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 20000000); Spanned s3 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 20000000);
Spanned s4 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 25000000); Spanned s4 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 25000000);
assertThat(s1.getSpans(/* start= */ 0, s1.length(), ForegroundColorSpan.class)).hasLength(1); assertThat(s1)
assertThat(s1.getSpans(/* start= */ 0, s1.length(), BackgroundColorSpan.class)).hasLength(1); .hasForegroundColorSpanBetween(0, s1.length())
assertThat(s2.getSpans(/* start= */ 0, s2.length(), ForegroundColorSpan.class)).hasLength(1); .withColor(ColorParser.parseCssColor("papayawhip"));
assertThat(s3.getSpans(/* start= */ 10, s3.length(), UnderlineSpan.class)).hasLength(1); assertThat(s1)
assertThat(s4.getSpans(/* start= */ 0, /* end= */ 16, BackgroundColorSpan.class)).hasLength(2); .hasBackgroundColorSpanBetween(0, s1.length())
assertThat(s4.getSpans(/* start= */ 17, s4.length(), StyleSpan.class)).hasLength(1); .withColor(ColorParser.parseCssColor("green"));
assertThat(s4.getSpans(/* start= */ 17, s4.length(), StyleSpan.class)[0].getStyle()) assertThat(s2)
.isEqualTo(Typeface.BOLD); .hasForegroundColorSpanBetween(0, s2.length())
.withColor(ColorParser.parseCssColor("peachpuff"));
assertThat(s3).hasUnderlineSpanBetween(10, s3.length());
assertThat(s4)
.hasBackgroundColorSpanBetween(0, 16)
.withColor(ColorParser.parseCssColor("lime"));
assertThat(s4)
.hasBoldSpan(
/* startIndex= */ 17,
/* endIndex= */ s4.length(),
/* flags= */ Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
@Test @Test
......
...@@ -21,9 +21,12 @@ import static com.google.android.exoplayer2.testutil.truth.SpannedSubject.spanne ...@@ -21,9 +21,12 @@ import static com.google.android.exoplayer2.testutil.truth.SpannedSubject.spanne
import static com.google.common.truth.ExpectFailure.assertThat; import static com.google.common.truth.ExpectFailure.assertThat;
import static com.google.common.truth.ExpectFailure.expectFailureAbout; import static com.google.common.truth.ExpectFailure.expectFailureAbout;
import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.Spanned; import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
...@@ -153,7 +156,167 @@ public class SpannedSubjectTest { ...@@ -153,7 +156,167 @@ public class SpannedSubjectTest {
int end = start + "underlined".length(); int end = start + "underlined".length();
spannable.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); spannable.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
assertThat(spannable).hasUnderlineSpan(start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); assertThat(spannable)
.hasUnderlineSpanBetween(start, end)
.withFlags(Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
@Test
public void foregroundColorSpan_success() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new ForegroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
assertThat(spannable)
.hasForegroundColorSpanBetween(start, end)
.withColor(Color.CYAN)
.andFlags(Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
@Test
public void foregroundColorSpan_wrongEndIndex() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new ForegroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
int incorrectEnd = end + 2;
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(spannable)
.hasForegroundColorSpanBetween(start, incorrectEnd)
.withColor(Color.CYAN));
assertThat(expected).factValue("expected").contains("end=" + incorrectEnd);
assertThat(expected).factValue("but found").contains("end=" + end);
}
@Test
public void foregroundColorSpan_wrongColor() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new ForegroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(spannable)
.hasForegroundColorSpanBetween(start, end)
.withColor(Color.BLUE));
assertThat(expected).factValue("value of").contains("foregroundColor");
assertThat(expected).factValue("expected").contains("0xFF0000FF"); // Color.BLUE
assertThat(expected).factValue("but was").contains("0xFF00FFFF"); // Color.CYAN
}
@Test
public void foregroundColorSpan_wrongFlags() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new ForegroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(spannable)
.hasForegroundColorSpanBetween(start, end)
.withColor(Color.CYAN)
.andFlags(Spanned.SPAN_EXCLUSIVE_EXCLUSIVE));
assertThat(expected).factValue("value of").contains("flags");
assertThat(expected)
.factValue("expected to contain")
.contains(String.valueOf(Spanned.SPAN_EXCLUSIVE_EXCLUSIVE));
assertThat(expected)
.factValue("but was")
.contains(String.valueOf(Spanned.SPAN_INCLUSIVE_EXCLUSIVE));
}
@Test
public void backgroundColorSpan_success() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new BackgroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
assertThat(spannable)
.hasBackgroundColorSpanBetween(start, end)
.withColor(Color.CYAN)
.andFlags(Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
@Test
public void backgroundColorSpan_wrongEndIndex() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new BackgroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
int incorrectEnd = end + 2;
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(spannable)
.hasBackgroundColorSpanBetween(start, incorrectEnd)
.withColor(Color.CYAN));
assertThat(expected).factValue("expected").contains("end=" + incorrectEnd);
assertThat(expected).factValue("but found").contains("end=" + end);
}
@Test
public void backgroundColorSpan_wrongColor() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new BackgroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(spannable)
.hasBackgroundColorSpanBetween(start, end)
.withColor(Color.BLUE));
assertThat(expected).factValue("value of").contains("backgroundColor");
assertThat(expected).factValue("expected").contains("0xFF0000FF"); // Color.BLUE
assertThat(expected).factValue("but was").contains("0xFF00FFFF"); // Color.CYAN
}
@Test
public void backgroundColorSpan_wrongFlags() {
SpannableString spannable = SpannableString.valueOf("test with cyan section");
int start = "test with ".length();
int end = start + "cyan".length();
spannable.setSpan(
new BackgroundColorSpan(Color.CYAN), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
AssertionError expected =
expectFailure(
whenTesting ->
whenTesting
.that(spannable)
.hasBackgroundColorSpanBetween(start, end)
.withColor(Color.CYAN)
.andFlags(Spanned.SPAN_EXCLUSIVE_EXCLUSIVE));
assertThat(expected).factValue("value of").contains("flags");
assertThat(expected)
.factValue("expected to contain")
.contains(String.valueOf(Spanned.SPAN_EXCLUSIVE_EXCLUSIVE));
assertThat(expected)
.factValue("but was")
.contains(String.valueOf(Spanned.SPAN_INCLUSIVE_EXCLUSIVE));
} }
private static AssertionError expectFailure( private static AssertionError expectFailure(
......
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