public void testDecodeWithMultipleStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_MULTIPLE_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("Line 2\nLine 3", text.toString()); assertEquals(4, text.getSpans(0, text.length(), Object.class).length); StyleSpan styleSpan = findSpan(text, 0, 5, StyleSpan.class); assertEquals(Typeface.ITALIC, styleSpan.getStyle()); findSpan(text, 7, 12, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 5, ForegroundColorSpan.class); assertEquals(Color.GREEN, colorSpan.getForegroundColor()); colorSpan = findSpan(text, 7, 12, ForegroundColorSpan.class); assertEquals(Color.GREEN, colorSpan.getForegroundColor()); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
public void testInitializationDecodeWithStyl() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(getInstrumentation(), INITIALIZATION); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(5, text.getSpans(0, text.length(), Object.class).length); StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertEquals(Typeface.BOLD_ITALIC, styleSpan.getStyle()); findSpan(text, 0, text.length(), UnderlineSpan.class); TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertEquals(C.SERIF_NAME, typefaceSpan.getFamily()); ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class); assertEquals(Color.RED, colorSpan.getForegroundColor()); colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertEquals(Color.GREEN, colorSpan.getForegroundColor()); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1f); }
public void testInitializationDecodeWithTbox() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(getInstrumentation(), INITIALIZATION); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_TBOX); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(4, text.getSpans(0, text.length(), Object.class).length); StyleSpan styleSpan = findSpan(text, 0, text.length(), StyleSpan.class); assertEquals(Typeface.BOLD_ITALIC, styleSpan.getStyle()); findSpan(text, 0, text.length(), UnderlineSpan.class); TypefaceSpan typefaceSpan = findSpan(text, 0, text.length(), TypefaceSpan.class); assertEquals(C.SERIF_NAME, typefaceSpan.getFamily()); ForegroundColorSpan colorSpan = findSpan(text, 0, text.length(), ForegroundColorSpan.class); assertEquals(Color.RED, colorSpan.getForegroundColor()); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.1875f); }
public void testInitializationAllDefaultsDecodeWithStyl() throws IOException, SubtitleDecoderException { byte[] initBytes = TestUtil.getByteArray(getInstrumentation(), INITIALIZATION_ALL_DEFAULTS); Tx3gDecoder decoder = new Tx3gDecoder(Collections.singletonList(initBytes)); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(3, text.getSpans(0, text.length(), Object.class).length); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertEquals(Typeface.BOLD_ITALIC, styleSpan.getStyle()); findSpan(text, 0, 6, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertEquals(Color.GREEN, colorSpan.getForegroundColor()); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Override protected Subtitle decode(byte[] bytes, int length, boolean reset) throws SubtitleDecoderException { parsableByteArray.reset(bytes, length); String cueTextString = readSubtitleText(parsableByteArray); if (cueTextString.isEmpty()) { return Tx3gSubtitle.EMPTY; } // Attach default styles. SpannableStringBuilder cueText = new SpannableStringBuilder(cueTextString); attachFontFace(cueText, defaultFontFace, DEFAULT_FONT_FACE, 0, cueText.length(), SPAN_PRIORITY_LOW); attachColor(cueText, defaultColorRgba, DEFAULT_COLOR, 0, cueText.length(), SPAN_PRIORITY_LOW); attachFontFamily(cueText, defaultFontFamily, DEFAULT_FONT_FAMILY, 0, cueText.length(), SPAN_PRIORITY_LOW); float verticalPlacement = defaultVerticalPlacement; // Find and attach additional styles. while (parsableByteArray.bytesLeft() >= SIZE_ATOM_HEADER) { int position = parsableByteArray.getPosition(); int atomSize = parsableByteArray.readInt(); int atomType = parsableByteArray.readInt(); if (atomType == TYPE_STYL) { assertTrue(parsableByteArray.bytesLeft() >= SIZE_SHORT); int styleRecordCount = parsableByteArray.readUnsignedShort(); for (int i = 0; i < styleRecordCount; i++) { applyStyleRecord(parsableByteArray, cueText); } } else if (atomType == TYPE_TBOX && customVerticalPlacement) { assertTrue(parsableByteArray.bytesLeft() >= SIZE_SHORT); int requestedVerticalPlacement = parsableByteArray.readUnsignedShort(); verticalPlacement = (float) requestedVerticalPlacement / calculatedVideoTrackHeight; verticalPlacement = Util.constrainValue(verticalPlacement, 0.0f, 0.95f); } parsableByteArray.setPosition(position + atomSize); } return new Tx3gSubtitle(new Cue(cueText, null, verticalPlacement, Cue.LINE_TYPE_FRACTION, Cue.ANCHOR_TYPE_START, Cue.DIMEN_UNSET, Cue.TYPE_UNSET, Cue.DIMEN_UNSET)); }
@Override protected Subtitle decode(byte[] bytes, int length) { parsableByteArray.reset(bytes, length); int textLength = parsableByteArray.readUnsignedShort(); if (textLength == 0) { return Tx3gSubtitle.EMPTY; } String cueText = parsableByteArray.readString(textLength); return new Tx3gSubtitle(new Cue(cueText)); }
public void testTwoCuesSample() throws SubtitleDecoderException { Mp4WebvttDecoder decoder = new Mp4WebvttDecoder(); Subtitle result = decoder.decode(DOUBLE_CUE_SAMPLE, DOUBLE_CUE_SAMPLE.length); Cue firstExpectedCue = new Cue("Hello World"); Cue secondExpectedCue = new Cue("Bye Bye"); assertMp4WebvttSubtitleEquals(result, firstExpectedCue, secondExpectedCue); }
/** * Asserts that the Subtitle's cues (which are all part of the event at t=0) are equal to the * expected Cues. * * @param subtitle The {@link Subtitle} to check. * @param expectedCues The expected {@link Cue}s. */ private static void assertMp4WebvttSubtitleEquals(Subtitle subtitle, Cue... expectedCues) { assertEquals(1, subtitle.getEventTimeCount()); assertEquals(0, subtitle.getEventTime(0)); List<Cue> subtitleCues = subtitle.getCues(0); assertEquals(expectedCues.length, subtitleCues.size()); for (int i = 0; i < subtitleCues.size(); i++) { assertCueEquals(expectedCues[i], subtitleCues.get(i)); } }
public void testDecodeJustText() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_JUST_TEXT); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(0, text.getSpans(0, text.length(), Object.class).length); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
public void testDecodeWithStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(3, text.getSpans(0, text.length(), Object.class).length); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertEquals(Typeface.BOLD_ITALIC, styleSpan.getStyle()); findSpan(text, 0, 6, UnderlineSpan.class); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertEquals(Color.GREEN, colorSpan.getForegroundColor()); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
public void testDecodeWithStylAllDefaults() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_STYL_ALL_DEFAULTS); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(0, text.getSpans(0, text.length(), Object.class).length); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
public void testDecodeUtf16BeNoStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_UTF16_BE_NO_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("你好", text.toString()); assertEquals(0, text.getSpans(0, text.length(), Object.class).length); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
public void testDecodeUtf16LeNoStyl() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_UTF16_LE_NO_STYL); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("你好", text.toString()); assertEquals(0, text.getSpans(0, text.length(), Object.class).length); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
public void testDecodeWithOtherExtension() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), SAMPLE_WITH_OTHER_EXTENSION); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); SpannedString text = new SpannedString(subtitle.getCues(0).get(0).text); assertEquals("CC Test", text.toString()); assertEquals(2, text.getSpans(0, text.length(), Object.class).length); StyleSpan styleSpan = findSpan(text, 0, 6, StyleSpan.class); assertEquals(Typeface.BOLD, styleSpan.getStyle()); ForegroundColorSpan colorSpan = findSpan(text, 0, 6, ForegroundColorSpan.class); assertEquals(Color.GREEN, colorSpan.getForegroundColor()); assertFractionalLinePosition(subtitle.getCues(0).get(0), 0.85f); }
@Test public void testSingleCueSample() throws SubtitleDecoderException { Mp4WebvttDecoder decoder = new Mp4WebvttDecoder(); Subtitle result = decoder.decode(SINGLE_CUE_SAMPLE, SINGLE_CUE_SAMPLE.length, false); Cue expectedCue = new Cue("Hello World"); // Line feed must be trimmed by the decoder assertMp4WebvttSubtitleEquals(result, expectedCue); }
@Test public void testTwoCuesSample() throws SubtitleDecoderException { Mp4WebvttDecoder decoder = new Mp4WebvttDecoder(); Subtitle result = decoder.decode(DOUBLE_CUE_SAMPLE, DOUBLE_CUE_SAMPLE.length, false); Cue firstExpectedCue = new Cue("Hello World"); Cue secondExpectedCue = new Cue("Bye Bye"); assertMp4WebvttSubtitleEquals(result, firstExpectedCue, secondExpectedCue); }
/** * Asserts that the Subtitle's cues (which are all part of the event at t=0) are equal to the * expected Cues. * * @param subtitle The {@link Subtitle} to check. * @param expectedCues The expected {@link Cue}s. */ private static void assertMp4WebvttSubtitleEquals(Subtitle subtitle, Cue... expectedCues) { assertThat(subtitle.getEventTimeCount()).isEqualTo(1); assertThat(subtitle.getEventTime(0)).isEqualTo(0); List<Cue> subtitleCues = subtitle.getCues(0); assertThat(subtitleCues).hasSize(expectedCues.length); for (int i = 0; i < subtitleCues.size(); i++) { assertCueEquals(expectedCues[i], subtitleCues.get(i)); } }
@Override protected Subtitle createSubtitle() { lastCues = cues; return new CeaSubtitle(cues); }
@Override protected Subtitle createSubtitle() { lastCaptionString = captionString; return new CeaSubtitle(new Cue(captionString)); }
public void testSingleCueSample() throws SubtitleDecoderException { Mp4WebvttDecoder decoder = new Mp4WebvttDecoder(); Subtitle result = decoder.decode(SINGLE_CUE_SAMPLE, SINGLE_CUE_SAMPLE.length); Cue expectedCue = new Cue("Hello World"); // Line feed must be trimmed by the decoder assertMp4WebvttSubtitleEquals(result, expectedCue); }
public void testNoCueSample() throws SubtitleDecoderException { Mp4WebvttDecoder decoder = new Mp4WebvttDecoder(); Subtitle result = decoder.decode(NO_CUE_SAMPLE, NO_CUE_SAMPLE.length); assertMp4WebvttSubtitleEquals(result, new Cue[0]); }
public void testDecodeNoSubtitle() throws IOException, SubtitleDecoderException { Tx3gDecoder decoder = new Tx3gDecoder(Collections.<byte[]>emptyList()); byte[] bytes = TestUtil.getByteArray(getInstrumentation(), NO_SUBTITLE); Subtitle subtitle = decoder.decode(bytes, bytes.length, false); assertTrue(subtitle.getCues(0).isEmpty()); }
@Test public void testNoCueSample() throws SubtitleDecoderException { Mp4WebvttDecoder decoder = new Mp4WebvttDecoder(); Subtitle result = decoder.decode(NO_CUE_SAMPLE, NO_CUE_SAMPLE.length, false); assertMp4WebvttSubtitleEquals(result); }
/** * Creates a {@link Subtitle} from the available data. */ protected abstract Subtitle createSubtitle();