Move Extractor test assertion methods to ExtractorAsserts class.

This cleans up test the TestUtil class that in large parts consisted of
assertions for Extractor tests.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160829066
This commit is contained in:
tonihei 2017-07-03 07:42:57 -07:00 committed by Oliver Woodman
parent ad3d1e0cf2
commit 05a77eef5d
15 changed files with 327 additions and 290 deletions

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.ext.flac;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link FlacExtractor}. * Unit test for {@link FlacExtractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public class FlacExtractorTest extends InstrumentationTestCase { public class FlacExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new FlacExtractor(); return new FlacExtractor();

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.flv;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link FlvExtractor}. * Unit test for {@link FlvExtractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class FlvExtractorTest extends InstrumentationTestCase { public final class FlvExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new FlvExtractor(); return new FlvExtractor();

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.mkv;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Tests for {@link MatroskaExtractor}. * Tests for {@link MatroskaExtractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class MatroskaExtractorTest extends InstrumentationTestCase { public final class MatroskaExtractorTest extends InstrumentationTestCase {
public void testMkvSample() throws Exception { public void testMkvSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new MatroskaExtractor(); return new MatroskaExtractor();
@ -34,7 +35,7 @@ public final class MatroskaExtractorTest extends InstrumentationTestCase {
} }
public void testWebmSubsampleEncryption() throws Exception { public void testWebmSubsampleEncryption() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new MatroskaExtractor(); return new MatroskaExtractor();
@ -43,7 +44,7 @@ public final class MatroskaExtractorTest extends InstrumentationTestCase {
} }
public void testWebmSubsampleEncryptionWithAltrefFrames() throws Exception { public void testWebmSubsampleEncryptionWithAltrefFrames() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new MatroskaExtractor(); return new MatroskaExtractor();

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.mp3;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link Mp3Extractor}. * Unit test for {@link Mp3Extractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class Mp3ExtractorTest extends InstrumentationTestCase { public final class Mp3ExtractorTest extends InstrumentationTestCase {
public void testMp3Sample() throws Exception { public void testMp3Sample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new Mp3Extractor(); return new Mp3Extractor();
@ -34,7 +35,7 @@ public final class Mp3ExtractorTest extends InstrumentationTestCase {
} }
public void testTrimmedMp3Sample() throws Exception { public void testTrimmedMp3Sample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new Mp3Extractor(); return new Mp3Extractor();

View File

@ -18,7 +18,8 @@ package com.google.android.exoplayer2.extractor.mp4;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link FragmentedMp4Extractor}. * Unit test for {@link FragmentedMp4Extractor}.
@ -26,26 +27,28 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class FragmentedMp4ExtractorTest extends InstrumentationTestCase { public final class FragmentedMp4ExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(getExtractorFactory(), "mp4/sample_fragmented.mp4", getInstrumentation()); ExtractorAsserts
.assertOutput(getExtractorFactory(), "mp4/sample_fragmented.mp4", getInstrumentation());
} }
public void testSampleWithSeiPayloadParsing() throws Exception { public void testSampleWithSeiPayloadParsing() throws Exception {
// Enabling the CEA-608 track enables SEI payload parsing. // Enabling the CEA-608 track enables SEI payload parsing.
TestUtil.assertOutput(getExtractorFactory(FragmentedMp4Extractor.FLAG_ENABLE_CEA608_TRACK), ExtractorAsserts.assertOutput(
getExtractorFactory(FragmentedMp4Extractor.FLAG_ENABLE_CEA608_TRACK),
"mp4/sample_fragmented_sei.mp4", getInstrumentation()); "mp4/sample_fragmented_sei.mp4", getInstrumentation());
} }
public void testAtomWithZeroSize() throws Exception { public void testAtomWithZeroSize() throws Exception {
TestUtil.assertThrows(getExtractorFactory(), "mp4/sample_fragmented_zero_size_atom.mp4", ExtractorAsserts.assertThrows(getExtractorFactory(), "mp4/sample_fragmented_zero_size_atom.mp4",
getInstrumentation(), ParserException.class); getInstrumentation(), ParserException.class);
} }
private static TestUtil.ExtractorFactory getExtractorFactory() { private static ExtractorFactory getExtractorFactory() {
return getExtractorFactory(0); return getExtractorFactory(0);
} }
private static TestUtil.ExtractorFactory getExtractorFactory(final int flags) { private static ExtractorFactory getExtractorFactory(final int flags) {
return new TestUtil.ExtractorFactory() { return new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new FragmentedMp4Extractor(flags, null); return new FragmentedMp4Extractor(flags, null);

View File

@ -18,7 +18,8 @@ package com.google.android.exoplayer2.extractor.mp4;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Tests for {@link Mp4Extractor}. * Tests for {@link Mp4Extractor}.
@ -27,7 +28,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class Mp4ExtractorTest extends InstrumentationTestCase { public final class Mp4ExtractorTest extends InstrumentationTestCase {
public void testMp4Sample() throws Exception { public void testMp4Sample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new Mp4Extractor(); return new Mp4Extractor();

View File

@ -17,9 +17,10 @@ package com.google.android.exoplayer2.extractor.ogg;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
import com.google.android.exoplayer2.testutil.FakeExtractorInput; import com.google.android.exoplayer2.testutil.FakeExtractorInput;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.testutil.TestUtil.ExtractorFactory;
import java.io.IOException; import java.io.IOException;
/** /**
@ -35,20 +36,21 @@ public final class OggExtractorTest extends InstrumentationTestCase {
}; };
public void testOpus() throws Exception { public void testOpus() throws Exception {
TestUtil.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear.opus", getInstrumentation()); ExtractorAsserts.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear.opus", getInstrumentation());
} }
public void testFlac() throws Exception { public void testFlac() throws Exception {
TestUtil.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_flac.ogg", getInstrumentation()); ExtractorAsserts.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_flac.ogg", getInstrumentation());
} }
public void testFlacNoSeektable() throws Exception { public void testFlacNoSeektable() throws Exception {
TestUtil.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_flac_noseektable.ogg", ExtractorAsserts.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_flac_noseektable.ogg",
getInstrumentation()); getInstrumentation());
} }
public void testVorbis() throws Exception { public void testVorbis() throws Exception {
TestUtil.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_vorbis.ogg", getInstrumentation()); ExtractorAsserts.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_vorbis.ogg",
getInstrumentation());
} }
public void testSniffVorbis() throws Exception { public void testSniffVorbis() throws Exception {

View File

@ -19,7 +19,8 @@ import android.annotation.TargetApi;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
/** /**
@ -29,8 +30,8 @@ import com.google.android.exoplayer2.util.MimeTypes;
public final class RawCcExtractorTest extends InstrumentationTestCase { public final class RawCcExtractorTest extends InstrumentationTestCase {
public void testRawCcSample() throws Exception { public void testRawCcSample() throws Exception {
TestUtil.assertOutput( ExtractorAsserts.assertOutput(
new TestUtil.ExtractorFactory() { new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new RawCcExtractor( return new RawCcExtractor(

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.ts;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link Ac3Extractor}. * Unit test for {@link Ac3Extractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class Ac3ExtractorTest extends InstrumentationTestCase { public final class Ac3ExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new Ac3Extractor(); return new Ac3Extractor();

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.ts;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link AdtsExtractor}. * Unit test for {@link AdtsExtractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class AdtsExtractorTest extends InstrumentationTestCase { public final class AdtsExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new AdtsExtractor(); return new AdtsExtractor();

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.ts;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link PsExtractor}. * Unit test for {@link PsExtractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class PsExtractorTest extends InstrumentationTestCase { public final class PsExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new PsExtractor(); return new PsExtractor();

View File

@ -25,6 +25,8 @@ import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.EsInfo; import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.EsInfo;
import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.TrackIdGenerator; import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.TrackIdGenerator;
import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
import com.google.android.exoplayer2.testutil.FakeExtractorInput; import com.google.android.exoplayer2.testutil.FakeExtractorInput;
import com.google.android.exoplayer2.testutil.FakeExtractorOutput; import com.google.android.exoplayer2.testutil.FakeExtractorOutput;
import com.google.android.exoplayer2.testutil.FakeTrackOutput; import com.google.android.exoplayer2.testutil.FakeTrackOutput;
@ -43,7 +45,7 @@ public final class TsExtractorTest extends InstrumentationTestCase {
private static final int TS_SYNC_BYTE = 0x47; // First byte of each TS packet. private static final int TS_SYNC_BYTE = 0x47; // First byte of each TS packet.
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new TsExtractor(); return new TsExtractor();
@ -65,7 +67,7 @@ public final class TsExtractorTest extends InstrumentationTestCase {
writeJunkData(out, random.nextInt(TS_PACKET_SIZE - 1) + 1); writeJunkData(out, random.nextInt(TS_PACKET_SIZE - 1) + 1);
fileData = out.toByteArray(); fileData = out.toByteArray();
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new TsExtractor(); return new TsExtractor();

View File

@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.wav;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.ExtractorAsserts;
import com.google.android.exoplayer2.testutil.ExtractorAsserts.ExtractorFactory;
/** /**
* Unit test for {@link WavExtractor}. * Unit test for {@link WavExtractor}.
@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class WavExtractorTest extends InstrumentationTestCase { public final class WavExtractorTest extends InstrumentationTestCase {
public void testSample() throws Exception { public void testSample() throws Exception {
TestUtil.assertOutput(new TestUtil.ExtractorFactory() { ExtractorAsserts.assertOutput(new ExtractorFactory() {
@Override @Override
public Extractor create() { public Extractor create() {
return new WavExtractor(); return new WavExtractor();

View File

@ -0,0 +1,272 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.testutil;
import android.app.Instrumentation;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException;
import com.google.android.exoplayer2.util.Assertions;
import java.io.IOException;
import java.util.Arrays;
import junit.framework.Assert;
/**
* Assertion methods for {@link Extractor}.
*/
public final class ExtractorAsserts {
/**
* A factory for {@link Extractor} instances.
*/
public interface ExtractorFactory {
Extractor create();
}
private static final String DUMP_EXTENSION = ".dump";
private static final String UNKNOWN_LENGTH_EXTENSION = ".unklen" + DUMP_EXTENSION;
/**
* Calls {@link #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean,
* boolean)} with all possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param sampleFile The path to the input sample.
* @param instrumentation To be used to load the sample file.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean, boolean)
*/
public static void assertOutput(ExtractorFactory factory, String sampleFile,
Instrumentation instrumentation) throws IOException, InterruptedException {
byte[] fileData = TestUtil.getByteArray(instrumentation, sampleFile);
assertOutput(factory, sampleFile, fileData, instrumentation);
}
/**
* Calls {@link #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean,
* boolean)} with all possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param sampleFile The path to the input sample.
* @param fileData Content of the input file.
* @param instrumentation To be used to load the sample file.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean, boolean)
*/
public static void assertOutput(ExtractorFactory factory, String sampleFile, byte[] fileData,
Instrumentation instrumentation) throws IOException, InterruptedException {
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, false, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, false, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, true, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, true, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, false, true);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, false, true);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, true, true);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, true, true);
}
/**
* Asserts that {@code extractor} consumes {@code sampleFile} successfully and its output equals
* to a prerecorded output dump file with the name {@code sampleFile} + "{@value
* #DUMP_EXTENSION}". If {@code simulateUnknownLength} is true and {@code sampleFile} + "{@value
* #UNKNOWN_LENGTH_EXTENSION}" exists, it's preferred.
*
* @param extractor The {@link Extractor} to be tested.
* @param sampleFile The path to the input sample.
* @param fileData Content of the input file.
* @param instrumentation To be used to load the sample file.
* @param simulateIOErrors If true simulates IOErrors.
* @param simulateUnknownLength If true simulates unknown input length.
* @param simulatePartialReads If true simulates partial reads.
* @return The {@link FakeExtractorOutput} used in the test.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
*/
public static FakeExtractorOutput assertOutput(Extractor extractor, String sampleFile,
byte[] fileData, Instrumentation instrumentation, boolean simulateIOErrors,
boolean simulateUnknownLength, boolean simulatePartialReads) throws IOException,
InterruptedException {
FakeExtractorInput input = new FakeExtractorInput.Builder().setData(fileData)
.setSimulateIOErrors(simulateIOErrors)
.setSimulateUnknownLength(simulateUnknownLength)
.setSimulatePartialReads(simulatePartialReads).build();
Assert.assertTrue(TestUtil.sniffTestData(extractor, input));
input.resetPeekPosition();
FakeExtractorOutput extractorOutput = consumeTestData(extractor, input, 0, true);
if (simulateUnknownLength
&& assetExists(instrumentation, sampleFile + UNKNOWN_LENGTH_EXTENSION)) {
extractorOutput.assertOutput(instrumentation, sampleFile + UNKNOWN_LENGTH_EXTENSION);
} else {
extractorOutput.assertOutput(instrumentation, sampleFile + ".0" + DUMP_EXTENSION);
}
SeekMap seekMap = extractorOutput.seekMap;
if (seekMap.isSeekable()) {
long durationUs = seekMap.getDurationUs();
for (int j = 0; j < 4; j++) {
long timeUs = (durationUs * j) / 3;
long position = seekMap.getPosition(timeUs);
input.setPosition((int) position);
for (int i = 0; i < extractorOutput.numberOfTracks; i++) {
extractorOutput.trackOutputs.valueAt(i).clear();
}
consumeTestData(extractor, input, timeUs, extractorOutput, false);
extractorOutput.assertOutput(instrumentation, sampleFile + '.' + j + DUMP_EXTENSION);
}
}
return extractorOutput;
}
/**
* Calls {@link #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)} with all
* possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param sampleFile The path to the input sample.
* @param instrumentation To be used to load the sample file.
* @param expectedThrowable Expected {@link Throwable} class.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)
*/
public static void assertThrows(ExtractorFactory factory, String sampleFile,
Instrumentation instrumentation, Class<? extends Throwable> expectedThrowable)
throws IOException, InterruptedException {
byte[] fileData = TestUtil.getByteArray(instrumentation, sampleFile);
assertThrows(factory, fileData, expectedThrowable);
}
/**
* Calls {@link #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)} with all
* possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param fileData Content of the input file.
* @param expectedThrowable Expected {@link Throwable} class.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)
*/
public static void assertThrows(ExtractorFactory factory, byte[] fileData,
Class<? extends Throwable> expectedThrowable) throws IOException, InterruptedException {
assertThrows(factory.create(), fileData, expectedThrowable, false, false, false);
assertThrows(factory.create(), fileData, expectedThrowable, true, false, false);
assertThrows(factory.create(), fileData, expectedThrowable, false, true, false);
assertThrows(factory.create(), fileData, expectedThrowable, true, true, false);
assertThrows(factory.create(), fileData, expectedThrowable, false, false, true);
assertThrows(factory.create(), fileData, expectedThrowable, true, false, true);
assertThrows(factory.create(), fileData, expectedThrowable, false, true, true);
assertThrows(factory.create(), fileData, expectedThrowable, true, true, true);
}
/**
* Asserts {@code extractor} throws {@code expectedThrowable} while consuming {@code sampleFile}.
*
* @param extractor The {@link Extractor} to be tested.
* @param fileData Content of the input file.
* @param expectedThrowable Expected {@link Throwable} class.
* @param simulateIOErrors If true simulates IOErrors.
* @param simulateUnknownLength If true simulates unknown input length.
* @param simulatePartialReads If true simulates partial reads.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
*/
public static void assertThrows(Extractor extractor, byte[] fileData,
Class<? extends Throwable> expectedThrowable, boolean simulateIOErrors,
boolean simulateUnknownLength, boolean simulatePartialReads) throws IOException,
InterruptedException {
FakeExtractorInput input = new FakeExtractorInput.Builder().setData(fileData)
.setSimulateIOErrors(simulateIOErrors)
.setSimulateUnknownLength(simulateUnknownLength)
.setSimulatePartialReads(simulatePartialReads).build();
try {
consumeTestData(extractor, input, 0, true);
throw new AssertionError(expectedThrowable.getSimpleName() + " expected but not thrown");
} catch (Throwable throwable) {
if (expectedThrowable.equals(throwable.getClass())) {
return; // Pass!
}
throw throwable;
}
}
private ExtractorAsserts() {}
private static FakeExtractorOutput consumeTestData(Extractor extractor, FakeExtractorInput input,
long timeUs, boolean retryFromStartIfLive) throws IOException, InterruptedException {
FakeExtractorOutput output = new FakeExtractorOutput();
extractor.init(output);
consumeTestData(extractor, input, timeUs, output, retryFromStartIfLive);
return output;
}
private static void consumeTestData(Extractor extractor, FakeExtractorInput input, long timeUs,
FakeExtractorOutput output, boolean retryFromStartIfLive)
throws IOException, InterruptedException {
extractor.seek(input.getPosition(), timeUs);
PositionHolder seekPositionHolder = new PositionHolder();
int readResult = Extractor.RESULT_CONTINUE;
while (readResult != Extractor.RESULT_END_OF_INPUT) {
try {
// Extractor.read should not read seekPositionHolder.position. Set it to a value that's
// likely to cause test failure if a read does occur.
seekPositionHolder.position = Long.MIN_VALUE;
readResult = extractor.read(input, seekPositionHolder);
if (readResult == Extractor.RESULT_SEEK) {
long seekPosition = seekPositionHolder.position;
Assertions.checkState(0 <= seekPosition && seekPosition <= Integer.MAX_VALUE);
input.setPosition((int) seekPosition);
}
} catch (SimulatedIOException e) {
if (!retryFromStartIfLive) {
continue;
}
boolean isOnDemand = input.getLength() != C.LENGTH_UNSET
|| (output.seekMap != null && output.seekMap.getDurationUs() != C.TIME_UNSET);
if (isOnDemand) {
continue;
}
input.setPosition(0);
for (int i = 0; i < output.numberOfTracks; i++) {
output.trackOutputs.valueAt(i).clear();
}
extractor.seek(0, 0);
}
}
}
private static boolean assetExists(Instrumentation instrumentation, String fileName)
throws IOException {
int i = fileName.lastIndexOf('/');
String path = i >= 0 ? fileName.substring(0, i) : "";
String file = i >= 0 ? fileName.substring(i + 1) : fileName;
return Arrays.asList(instrumentation.getContext().getResources().getAssets().list(path))
.contains(file);
}
}

View File

@ -21,8 +21,6 @@ import android.test.MoreAsserts;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.source.MediaSource.Listener; import com.google.android.exoplayer2.source.MediaSource.Listener;
import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException; import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException;
@ -42,23 +40,8 @@ import org.mockito.MockitoAnnotations;
*/ */
public class TestUtil { public class TestUtil {
/**
* A factory for {@link Extractor} instances.
*/
public interface ExtractorFactory {
Extractor create();
}
private static final String DUMP_EXTENSION = ".dump";
private static final String UNKNOWN_LENGTH_EXTENSION = ".unklen" + DUMP_EXTENSION;
private TestUtil() {} private TestUtil() {}
public static boolean sniffTestData(Extractor extractor, byte[] data)
throws IOException, InterruptedException {
return sniffTestData(extractor, newExtractorInput(data));
}
public static boolean sniffTestData(Extractor extractor, FakeExtractorInput input) public static boolean sniffTestData(Extractor extractor, FakeExtractorInput input)
throws IOException, InterruptedException { throws IOException, InterruptedException {
while (true) { while (true) {
@ -86,54 +69,6 @@ public class TestUtil {
return Arrays.copyOf(data, position); return Arrays.copyOf(data, position);
} }
public static FakeExtractorOutput consumeTestData(Extractor extractor, FakeExtractorInput input,
long timeUs) throws IOException, InterruptedException {
return consumeTestData(extractor, input, timeUs, false);
}
public static FakeExtractorOutput consumeTestData(Extractor extractor, FakeExtractorInput input,
long timeUs, boolean retryFromStartIfLive) throws IOException, InterruptedException {
FakeExtractorOutput output = new FakeExtractorOutput();
extractor.init(output);
consumeTestData(extractor, input, timeUs, output, retryFromStartIfLive);
return output;
}
private static void consumeTestData(Extractor extractor, FakeExtractorInput input, long timeUs,
FakeExtractorOutput output, boolean retryFromStartIfLive)
throws IOException, InterruptedException {
extractor.seek(input.getPosition(), timeUs);
PositionHolder seekPositionHolder = new PositionHolder();
int readResult = Extractor.RESULT_CONTINUE;
while (readResult != Extractor.RESULT_END_OF_INPUT) {
try {
// Extractor.read should not read seekPositionHolder.position. Set it to a value that's
// likely to cause test failure if a read does occur.
seekPositionHolder.position = Long.MIN_VALUE;
readResult = extractor.read(input, seekPositionHolder);
if (readResult == Extractor.RESULT_SEEK) {
long seekPosition = seekPositionHolder.position;
Assertions.checkState(0 <= seekPosition && seekPosition <= Integer.MAX_VALUE);
input.setPosition((int) seekPosition);
}
} catch (SimulatedIOException e) {
if (!retryFromStartIfLive) {
continue;
}
boolean isOnDemand = input.getLength() != C.LENGTH_UNSET
|| (output.seekMap != null && output.seekMap.getDurationUs() != C.TIME_UNSET);
if (isOnDemand) {
continue;
}
input.setPosition(0);
for (int i = 0; i < output.numberOfTracks; i++) {
output.trackOutputs.valueAt(i).clear();
}
extractor.seek(0, 0);
}
}
}
public static byte[] buildTestData(int length) { public static byte[] buildTestData(int length) {
return buildTestData(length, length); return buildTestData(length, length);
} }
@ -193,15 +128,6 @@ public class TestUtil {
MockitoAnnotations.initMocks(instrumentationTestCase); MockitoAnnotations.initMocks(instrumentationTestCase);
} }
public static boolean assetExists(Instrumentation instrumentation, String fileName)
throws IOException {
int i = fileName.lastIndexOf('/');
String path = i >= 0 ? fileName.substring(0, i) : "";
String file = i >= 0 ? fileName.substring(i + 1) : fileName;
return Arrays.asList(instrumentation.getContext().getResources().getAssets().list(path))
.contains(file);
}
public static byte[] getByteArray(Instrumentation instrumentation, String fileName) public static byte[] getByteArray(Instrumentation instrumentation, String fileName)
throws IOException { throws IOException {
return Util.toByteArray(getInputStream(instrumentation, fileName)); return Util.toByteArray(getInputStream(instrumentation, fileName));
@ -217,10 +143,6 @@ public class TestUtil {
return new String(getByteArray(instrumentation, fileName)); return new String(getByteArray(instrumentation, fileName));
} }
private static FakeExtractorInput newExtractorInput(byte[] data) {
return new FakeExtractorInput.Builder().setData(data).build();
}
/** /**
* Extracts the timeline from a media source. * Extracts the timeline from a media source.
*/ */
@ -247,180 +169,6 @@ public class TestUtil {
return listener.timeline; return listener.timeline;
} }
/**
* Calls {@link #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean,
* boolean)} with all possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param sampleFile The path to the input sample.
* @param instrumentation To be used to load the sample file.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean, boolean)
*/
public static void assertOutput(ExtractorFactory factory, String sampleFile,
Instrumentation instrumentation) throws IOException, InterruptedException {
byte[] fileData = getByteArray(instrumentation, sampleFile);
assertOutput(factory, sampleFile, fileData, instrumentation);
}
/**
* Calls {@link #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean,
* boolean)} with all possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param sampleFile The path to the input sample.
* @param fileData Content of the input file.
* @param instrumentation To be used to load the sample file.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertOutput(Extractor, String, byte[], Instrumentation, boolean, boolean, boolean)
*/
public static void assertOutput(ExtractorFactory factory, String sampleFile, byte[] fileData,
Instrumentation instrumentation) throws IOException, InterruptedException {
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, false, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, false, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, true, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, true, false);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, false, true);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, false, true);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, false, true, true);
assertOutput(factory.create(), sampleFile, fileData, instrumentation, true, true, true);
}
/**
* Asserts that {@code extractor} consumes {@code sampleFile} successfully and its output equals
* to a prerecorded output dump file with the name {@code sampleFile} + "{@value
* #DUMP_EXTENSION}". If {@code simulateUnknownLength} is true and {@code sampleFile} + "{@value
* #UNKNOWN_LENGTH_EXTENSION}" exists, it's preferred.
*
* @param extractor The {@link Extractor} to be tested.
* @param sampleFile The path to the input sample.
* @param fileData Content of the input file.
* @param instrumentation To be used to load the sample file.
* @param simulateIOErrors If true simulates IOErrors.
* @param simulateUnknownLength If true simulates unknown input length.
* @param simulatePartialReads If true simulates partial reads.
* @return The {@link FakeExtractorOutput} used in the test.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
*/
public static FakeExtractorOutput assertOutput(Extractor extractor, String sampleFile,
byte[] fileData, Instrumentation instrumentation, boolean simulateIOErrors,
boolean simulateUnknownLength, boolean simulatePartialReads) throws IOException,
InterruptedException {
FakeExtractorInput input = new FakeExtractorInput.Builder().setData(fileData)
.setSimulateIOErrors(simulateIOErrors)
.setSimulateUnknownLength(simulateUnknownLength)
.setSimulatePartialReads(simulatePartialReads).build();
Assert.assertTrue(sniffTestData(extractor, input));
input.resetPeekPosition();
FakeExtractorOutput extractorOutput = consumeTestData(extractor, input, 0, true);
if (simulateUnknownLength
&& assetExists(instrumentation, sampleFile + UNKNOWN_LENGTH_EXTENSION)) {
extractorOutput.assertOutput(instrumentation, sampleFile + UNKNOWN_LENGTH_EXTENSION);
} else {
extractorOutput.assertOutput(instrumentation, sampleFile + ".0" + DUMP_EXTENSION);
}
SeekMap seekMap = extractorOutput.seekMap;
if (seekMap.isSeekable()) {
long durationUs = seekMap.getDurationUs();
for (int j = 0; j < 4; j++) {
long timeUs = (durationUs * j) / 3;
long position = seekMap.getPosition(timeUs);
input.setPosition((int) position);
for (int i = 0; i < extractorOutput.numberOfTracks; i++) {
extractorOutput.trackOutputs.valueAt(i).clear();
}
consumeTestData(extractor, input, timeUs, extractorOutput, false);
extractorOutput.assertOutput(instrumentation, sampleFile + '.' + j + DUMP_EXTENSION);
}
}
return extractorOutput;
}
/**
* Calls {@link #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)} with all
* possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param sampleFile The path to the input sample.
* @param instrumentation To be used to load the sample file.
* @param expectedThrowable Expected {@link Throwable} class.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)
*/
public static void assertThrows(ExtractorFactory factory, String sampleFile,
Instrumentation instrumentation, Class<? extends Throwable> expectedThrowable)
throws IOException, InterruptedException {
byte[] fileData = getByteArray(instrumentation, sampleFile);
assertThrows(factory, fileData, expectedThrowable);
}
/**
* Calls {@link #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)} with all
* possible combinations of "simulate" parameters.
*
* @param factory An {@link ExtractorFactory} which creates instances of the {@link Extractor}
* class which is to be tested.
* @param fileData Content of the input file.
* @param expectedThrowable Expected {@link Throwable} class.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
* @see #assertThrows(Extractor, byte[], Class, boolean, boolean, boolean)
*/
public static void assertThrows(ExtractorFactory factory, byte[] fileData,
Class<? extends Throwable> expectedThrowable) throws IOException, InterruptedException {
assertThrows(factory.create(), fileData, expectedThrowable, false, false, false);
assertThrows(factory.create(), fileData, expectedThrowable, true, false, false);
assertThrows(factory.create(), fileData, expectedThrowable, false, true, false);
assertThrows(factory.create(), fileData, expectedThrowable, true, true, false);
assertThrows(factory.create(), fileData, expectedThrowable, false, false, true);
assertThrows(factory.create(), fileData, expectedThrowable, true, false, true);
assertThrows(factory.create(), fileData, expectedThrowable, false, true, true);
assertThrows(factory.create(), fileData, expectedThrowable, true, true, true);
}
/**
* Asserts {@code extractor} throws {@code expectedThrowable} while consuming {@code sampleFile}.
*
* @param extractor The {@link Extractor} to be tested.
* @param fileData Content of the input file.
* @param expectedThrowable Expected {@link Throwable} class.
* @param simulateIOErrors If true simulates IOErrors.
* @param simulateUnknownLength If true simulates unknown input length.
* @param simulatePartialReads If true simulates partial reads.
* @throws IOException If reading from the input fails.
* @throws InterruptedException If interrupted while reading from the input.
*/
public static void assertThrows(Extractor extractor, byte[] fileData,
Class<? extends Throwable> expectedThrowable, boolean simulateIOErrors,
boolean simulateUnknownLength, boolean simulatePartialReads) throws IOException,
InterruptedException {
FakeExtractorInput input = new FakeExtractorInput.Builder().setData(fileData)
.setSimulateIOErrors(simulateIOErrors)
.setSimulateUnknownLength(simulateUnknownLength)
.setSimulatePartialReads(simulatePartialReads).build();
try {
consumeTestData(extractor, input, 0, true);
throw new AssertionError(expectedThrowable.getSimpleName() + " expected but not thrown");
} catch (Throwable throwable) {
if (expectedThrowable.equals(throwable.getClass())) {
return; // Pass!
}
throw throwable;
}
}
/** /**
* Asserts that data read from a {@link DataSource} matches {@code expected}. * Asserts that data read from a {@link DataSource} matches {@code expected}.
* *