Migrate Tx3gDecoderTest to Guava and SpannedSubject

#minor-release

PiperOrigin-RevId: 340249019
This commit is contained in:
ibaker 2020-11-02 17:17:17 +00:00 committed by Ian Baker
parent 2d022a21b3
commit 07455da2f5

View File

@ -15,24 +15,18 @@
*/ */
package com.google.android.exoplayer2.text.tx3g; package com.google.android.exoplayer2.text.tx3g;
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 android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannedString; import android.text.SpannedString;
import android.text.style.ForegroundColorSpan;
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.C; import com.google.android.exoplayer2.C;
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.Subtitle;
import com.google.android.exoplayer2.text.SubtitleDecoderException; import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -57,197 +51,184 @@ public final class Tx3gDecoderTest {
"media/tx3g/initialization_all_defaults"; "media/tx3g/initialization_all_defaults";
@Test @Test
public void decodeNoSubtitle() throws IOException, SubtitleDecoderException { public void decodeNoSubtitle() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), NO_SUBTITLE); byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), NO_SUBTITLE);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
assertThat(subtitle.getCues(0)).isEmpty(); assertThat(subtitle.getCues(0)).isEmpty();
} }
@Test @Test
public void decodeJustText() throws IOException, SubtitleDecoderException { public void decodeJustText() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_JUST_TEXT); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_JUST_TEXT);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertThat(text).hasNoSpans();
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void decodeWithStyl() throws IOException, SubtitleDecoderException { public void decodeWithStyl() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(3); assertThat(text).hasBoldItalicSpanBetween(0, 6);
StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(text).hasUnderlineSpanBetween(0, 6);
assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
findSpan(text, 0, 6, UnderlineSpan.class);
ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN);
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void decodeWithStylAllDefaults() throws IOException, SubtitleDecoderException { public void decodeWithStylAllDefaults() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray( TestUtil.getByteArray(
ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL_ALL_DEFAULTS); ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL_ALL_DEFAULTS);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertThat(text).hasNoSpans();
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void decodeUtf16BeNoStyl() throws IOException, SubtitleDecoderException { public void decodeUtf16BeNoStyl() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_UTF16_BE_NO_STYL); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_UTF16_BE_NO_STYL);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("你好"); assertThat(text.toString()).isEqualTo("你好");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertThat(text).hasNoSpans();
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void decodeUtf16LeNoStyl() throws IOException, SubtitleDecoderException { public void decodeUtf16LeNoStyl() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_UTF16_LE_NO_STYL); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_UTF16_LE_NO_STYL);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("你好"); assertThat(text.toString()).isEqualTo("你好");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(0); assertThat(text).hasNoSpans();
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void decodeWithMultipleStyl() throws IOException, SubtitleDecoderException { public void decodeWithMultipleStyl() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray( TestUtil.getByteArray(
ApplicationProvider.getApplicationContext(), SAMPLE_WITH_MULTIPLE_STYL); ApplicationProvider.getApplicationContext(), SAMPLE_WITH_MULTIPLE_STYL);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("Line 2\nLine 3"); assertThat(text.toString()).isEqualTo("Line 2\nLine 3");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(4); assertThat(text).hasItalicSpanBetween(0, 5);
StyleSpan styleSpan = findSpan(text, 0, 5, StyleSpan.class); assertThat(text).hasUnderlineSpanBetween(7, 12);
assertThat(styleSpan.getStyle()).isEqualTo(Typeface.ITALIC); assertThat(text).hasForegroundColorSpanBetween(0, 5).withColor(Color.GREEN);
findSpan(text, 7, 12, UnderlineSpan.class); assertThat(text).hasForegroundColorSpanBetween(7, 12).withColor(Color.GREEN);
ForegroundColorSpan colorSpan = findSpan(text, 0, 5, ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN);
colorSpan = findSpan(text, 7, 12, ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN);
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void decodeWithOtherExtension() throws IOException, SubtitleDecoderException { public void decodeWithOtherExtension() throws Exception {
Tx3gDecoder decoder = new Tx3gDecoder(Collections.emptyList()); Tx3gDecoder decoder = new Tx3gDecoder(ImmutableList.of());
byte[] bytes = byte[] bytes =
TestUtil.getByteArray( TestUtil.getByteArray(
ApplicationProvider.getApplicationContext(), SAMPLE_WITH_OTHER_EXTENSION); ApplicationProvider.getApplicationContext(), SAMPLE_WITH_OTHER_EXTENSION);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(2); assertThat(text).hasBoldSpanBetween(0, 6);
StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD);
ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN);
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
@Test @Test
public void initializationDecodeWithStyl() throws IOException, SubtitleDecoderException { public void initializationDecodeWithStyl() throws Exception {
byte[] initBytes = byte[] initBytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION);
Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes));
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(5); assertThat(text).hasBoldItalicSpanBetween(0, 7);
StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertThat(text).hasUnderlineSpanBetween(0, 7);
assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); assertThat(text).hasTypefaceSpanBetween(0, 7).withFamily(C.SERIF_NAME);
findSpan(text, 0, text.length(), UnderlineSpan.class); // TODO(internal b/171984212): Fix Tx3gDecoder to avoid overlapping spans of the same type.
TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertThat(text).hasForegroundColorSpanBetween(0, 7).withColor(Color.RED);
assertThat(typefaceSpan.getFamily()).isEqualTo(C.SERIF_NAME); assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.RED);
colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN);
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1f);
} }
@Test @Test
public void initializationDecodeWithTbox() throws IOException, SubtitleDecoderException { public void initializationDecodeWithTbox() throws Exception {
byte[] initBytes = byte[] initBytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), INITIALIZATION);
Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes));
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_TBOX); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_TBOX);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(4); assertThat(text).hasBoldItalicSpanBetween(0, 7);
StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertThat(text).hasUnderlineSpanBetween(0, 7);
assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); assertThat(text).hasTypefaceSpanBetween(0, 7).withFamily(C.SERIF_NAME);
findSpan(text, 0, text.length(), UnderlineSpan.class); assertThat(text).hasForegroundColorSpanBetween(0, 7).withColor(Color.RED);
TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class);
assertThat(typefaceSpan.getFamily()).isEqualTo(C.SERIF_NAME);
ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.RED);
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1875f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1875f);
} }
@Test @Test
public void initializationAllDefaultsDecodeWithStyl() public void initializationAllDefaultsDecodeWithStyl() throws Exception {
throws IOException, SubtitleDecoderException {
byte[] initBytes = byte[] initBytes =
TestUtil.getByteArray( TestUtil.getByteArray(
ApplicationProvider.getApplicationContext(), INITIALIZATION_ALL_DEFAULTS); ApplicationProvider.getApplicationContext(), INITIALIZATION_ALL_DEFAULTS);
Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes));
byte[] bytes = byte[] bytes =
TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL); TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), SAMPLE_WITH_STYL);
Subtitle subtitle = decoder.decode(bytes, bytes.length, false); Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text);
assertThat(text.toString()).isEqualTo("CC Test"); assertThat(text.toString()).isEqualTo("CC Test");
assertThat(text.getSpans(0, text.length(), Object.class)).hasLength(3); assertThat(text).hasBoldItalicSpanBetween(0, 6);
StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertThat(text).hasUnderlineSpanBetween(0, 6);
assertThat(styleSpan.getStyle()).isEqualTo(Typeface.BOLD_ITALIC); assertThat(text).hasForegroundColorSpanBetween(0, 6).withColor(Color.GREEN);
findSpan(text, 0, 6, UnderlineSpan.class);
ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class);
assertThat(colorSpan.getForegroundColor()).isEqualTo(Color.GREEN);
assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f);
} }
private static <T> T findSpan(
SpannedString testObject, int expectedStart, int expectedEnd, Class<T> expectedType) {
T[] spans = testObject.getSpans(0, testObject.length(), expectedType);
for (T span : spans) {
if (testObject.getSpanStart(span) == expectedStart
&& testObject.getSpanEnd(span) == expectedEnd) {
return span;
}
}
fail("Span not found.");
return null;
}
private static void assertFractionalLinePosition(Cue cue, float expectedFraction) { private static void assertFractionalLinePosition(Cue cue, float expectedFraction) {
assertThat(cue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION); assertThat(cue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION);
assertThat(cue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START); assertThat(cue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);
assertThat(Math.abs(expectedFraction - cue.line) < 1e-6).isTrue(); assertThat(cue.line).isWithin(1e-6f).of(expectedFraction);
} }
} }