|
| 1 | +/* |
| 2 | + * Copyright (C) 2016 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.android.exoplayer2.text.ssa; |
| 17 | + |
| 18 | +import android.test.InstrumentationTestCase; |
| 19 | +import com.google.android.exoplayer2.testutil.TestUtil; |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.ArrayList; |
| 22 | + |
| 23 | +/** |
| 24 | + * Unit test for {@link SsaDecoder}. |
| 25 | + */ |
| 26 | +public final class SsaDecoderTest extends InstrumentationTestCase { |
| 27 | + |
| 28 | + private static final String EMPTY = "ssa/empty"; |
| 29 | + private static final String TYPICAL = "ssa/typical"; |
| 30 | + private static final String TYPICAL_HEADER_ONLY = "ssa/typical_header"; |
| 31 | + private static final String TYPICAL_DIALOGUE_ONLY = "ssa/typical_dialogue"; |
| 32 | + private static final String TYPICAL_FORMAT_ONLY = "ssa/typical_format"; |
| 33 | + private static final String INVALID_TIMECODES = "ssa/invalid_timecodes"; |
| 34 | + private static final String NO_END_TIMECODES = "ssa/no_end_timecodes"; |
| 35 | + |
| 36 | + public void testDecodeEmpty() throws IOException { |
| 37 | + SsaDecoder decoder = new SsaDecoder(); |
| 38 | + byte[] bytes = TestUtil.getByteArray(getInstrumentation(), EMPTY); |
| 39 | + SsaSubtitle subtitle = decoder.decode(bytes, bytes.length, false); |
| 40 | + |
| 41 | + assertEquals(0, subtitle.getEventTimeCount()); |
| 42 | + assertTrue(subtitle.getCues(0).isEmpty()); |
| 43 | + } |
| 44 | + |
| 45 | + public void testDecodeTypical() throws IOException { |
| 46 | + SsaDecoder decoder = new SsaDecoder(); |
| 47 | + byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL); |
| 48 | + SsaSubtitle subtitle = decoder.decode(bytes, bytes.length, false); |
| 49 | + |
| 50 | + assertEquals(6, subtitle.getEventTimeCount()); |
| 51 | + assertTypicalCue1(subtitle, 0); |
| 52 | + assertTypicalCue2(subtitle, 2); |
| 53 | + assertTypicalCue3(subtitle, 4); |
| 54 | + } |
| 55 | + |
| 56 | + public void testDecodeTypicalWithInitializationData() throws IOException { |
| 57 | + byte[] headerBytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_HEADER_ONLY); |
| 58 | + byte[] formatBytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_FORMAT_ONLY); |
| 59 | + ArrayList<byte[]> initializationData = new ArrayList<>(); |
| 60 | + initializationData.add(formatBytes); |
| 61 | + initializationData.add(headerBytes); |
| 62 | + SsaDecoder decoder = new SsaDecoder(initializationData); |
| 63 | + byte[] bytes = TestUtil.getByteArray(getInstrumentation(), TYPICAL_DIALOGUE_ONLY); |
| 64 | + SsaSubtitle subtitle = decoder.decode(bytes, bytes.length, false); |
| 65 | + |
| 66 | + assertEquals(6, subtitle.getEventTimeCount()); |
| 67 | + assertTypicalCue1(subtitle, 0); |
| 68 | + assertTypicalCue2(subtitle, 2); |
| 69 | + assertTypicalCue3(subtitle, 4); |
| 70 | + } |
| 71 | + |
| 72 | + public void testDecodeInvalidTimecodes() throws IOException { |
| 73 | + // Parsing should succeed, parsing the third cue only. |
| 74 | + SsaDecoder decoder = new SsaDecoder(); |
| 75 | + byte[] bytes = TestUtil.getByteArray(getInstrumentation(), INVALID_TIMECODES); |
| 76 | + SsaSubtitle subtitle = decoder.decode(bytes, bytes.length, false); |
| 77 | + |
| 78 | + assertEquals(2, subtitle.getEventTimeCount()); |
| 79 | + assertTypicalCue3(subtitle, 0); |
| 80 | + } |
| 81 | + |
| 82 | + public void testDecodeNoEndTimecodes() throws IOException { |
| 83 | + SsaDecoder decoder = new SsaDecoder(); |
| 84 | + byte[] bytes = TestUtil.getByteArray(getInstrumentation(), NO_END_TIMECODES); |
| 85 | + SsaSubtitle subtitle = decoder.decode(bytes, bytes.length, false); |
| 86 | + |
| 87 | + assertEquals(3, subtitle.getEventTimeCount()); |
| 88 | + |
| 89 | + assertEquals(0, subtitle.getEventTime(0)); |
| 90 | + assertEquals("This is the first subtitle.", |
| 91 | + subtitle.getCues(subtitle.getEventTime(0)).get(0).text.toString()); |
| 92 | + |
| 93 | + assertEquals(2340000, subtitle.getEventTime(1)); |
| 94 | + assertEquals("This is the second subtitle \nwith a newline \nand another.", |
| 95 | + subtitle.getCues(subtitle.getEventTime(1)).get(0).text.toString()); |
| 96 | + |
| 97 | + assertEquals(4560000, subtitle.getEventTime(2)); |
| 98 | + assertEquals("This is the third subtitle, with a comma.", |
| 99 | + subtitle.getCues(subtitle.getEventTime(2)).get(0).text.toString()); |
| 100 | + } |
| 101 | + |
| 102 | + private static void assertTypicalCue1(SsaSubtitle subtitle, int eventIndex) { |
| 103 | + assertEquals(0, subtitle.getEventTime(eventIndex)); |
| 104 | + assertEquals("This is the first subtitle.", |
| 105 | + subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()); |
| 106 | + assertEquals(1230000, subtitle.getEventTime(eventIndex + 1)); |
| 107 | + } |
| 108 | + |
| 109 | + private static void assertTypicalCue2(SsaSubtitle subtitle, int eventIndex) { |
| 110 | + assertEquals(2340000, subtitle.getEventTime(eventIndex)); |
| 111 | + assertEquals("This is the second subtitle \nwith a newline \nand another.", |
| 112 | + subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()); |
| 113 | + assertEquals(3450000, subtitle.getEventTime(eventIndex + 1)); |
| 114 | + } |
| 115 | + |
| 116 | + private static void assertTypicalCue3(SsaSubtitle subtitle, int eventIndex) { |
| 117 | + assertEquals(4560000, subtitle.getEventTime(eventIndex)); |
| 118 | + assertEquals("This is the third subtitle, with a comma.", |
| 119 | + subtitle.getCues(subtitle.getEventTime(eventIndex)).get(0).text.toString()); |
| 120 | + assertEquals(8900000, subtitle.getEventTime(eventIndex + 1)); |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments