diff --git a/extensions/flac/src/androidTest/java/com/google/android/exoplayer2/ext/flac/FlacExtractorTest.java b/extensions/flac/src/androidTest/java/com/google/android/exoplayer2/ext/flac/FlacExtractorTest.java index 4196f1ea63..5954985100 100644 --- a/extensions/flac/src/androidTest/java/com/google/android/exoplayer2/ext/flac/FlacExtractorTest.java +++ b/extensions/flac/src/androidTest/java/com/google/android/exoplayer2/ext/flac/FlacExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.ext.flac; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public class FlacExtractorTest extends InstrumentationTestCase { public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new FlacExtractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/flv/FlvExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/flv/FlvExtractorTest.java index 321181621e..4587c98317 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/flv/FlvExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/flv/FlvExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.flv; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class FlvExtractorTest extends InstrumentationTestCase { public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new FlvExtractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractorTest.java index 48eee69b50..57beec3ac6 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mkv/MatroskaExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.mkv; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class MatroskaExtractorTest extends InstrumentationTestCase { public void testMkvSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new MatroskaExtractor(); @@ -34,7 +35,7 @@ public final class MatroskaExtractorTest extends InstrumentationTestCase { } public void testWebmSubsampleEncryption() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new MatroskaExtractor(); @@ -43,7 +44,7 @@ public final class MatroskaExtractorTest extends InstrumentationTestCase { } public void testWebmSubsampleEncryptionWithAltrefFrames() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new MatroskaExtractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp3/Mp3ExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp3/Mp3ExtractorTest.java index c70710f1ee..3ad6a74bc9 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp3/Mp3ExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp3/Mp3ExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.mp3; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class Mp3ExtractorTest extends InstrumentationTestCase { public void testMp3Sample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new Mp3Extractor(); @@ -34,7 +35,7 @@ public final class Mp3ExtractorTest extends InstrumentationTestCase { } public void testTrimmedMp3Sample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new Mp3Extractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/FragmentedMp4ExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/FragmentedMp4ExtractorTest.java index 95ad8b446e..d8da8760e4 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/FragmentedMp4ExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/FragmentedMp4ExtractorTest.java @@ -18,7 +18,8 @@ package com.google.android.exoplayer2.extractor.mp4; import android.test.InstrumentationTestCase; import com.google.android.exoplayer2.ParserException; 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}. @@ -26,26 +27,28 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class FragmentedMp4ExtractorTest extends InstrumentationTestCase { 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 { // 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()); } 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); } - private static TestUtil.ExtractorFactory getExtractorFactory() { + private static ExtractorFactory getExtractorFactory() { return getExtractorFactory(0); } - private static TestUtil.ExtractorFactory getExtractorFactory(final int flags) { - return new TestUtil.ExtractorFactory() { + private static ExtractorFactory getExtractorFactory(final int flags) { + return new ExtractorFactory() { @Override public Extractor create() { return new FragmentedMp4Extractor(flags, null); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/Mp4ExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/Mp4ExtractorTest.java index 6ad777da70..a534d6dd24 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/Mp4ExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/mp4/Mp4ExtractorTest.java @@ -18,7 +18,8 @@ package com.google.android.exoplayer2.extractor.mp4; import android.annotation.TargetApi; import android.test.InstrumentationTestCase; 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}. @@ -27,7 +28,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class Mp4ExtractorTest extends InstrumentationTestCase { public void testMp4Sample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new Mp4Extractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ogg/OggExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ogg/OggExtractorTest.java index 04a6131652..26b7991869 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ogg/OggExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ogg/OggExtractorTest.java @@ -17,9 +17,10 @@ package com.google.android.exoplayer2.extractor.ogg; import android.test.InstrumentationTestCase; 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.TestUtil; -import com.google.android.exoplayer2.testutil.TestUtil.ExtractorFactory; import java.io.IOException; /** @@ -35,20 +36,21 @@ public final class OggExtractorTest extends InstrumentationTestCase { }; 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 { - 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 { - TestUtil.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_flac_noseektable.ogg", + ExtractorAsserts.assertOutput(OGG_EXTRACTOR_FACTORY, "ogg/bear_flac_noseektable.ogg", getInstrumentation()); } 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 { diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/rawcc/RawCcExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/rawcc/RawCcExtractorTest.java index 4e99e2745e..5a9d60512c 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/rawcc/RawCcExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/rawcc/RawCcExtractorTest.java @@ -19,7 +19,8 @@ import android.annotation.TargetApi; import android.test.InstrumentationTestCase; import com.google.android.exoplayer2.Format; 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; /** @@ -29,8 +30,8 @@ import com.google.android.exoplayer2.util.MimeTypes; public final class RawCcExtractorTest extends InstrumentationTestCase { public void testRawCcSample() throws Exception { - TestUtil.assertOutput( - new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput( + new ExtractorFactory() { @Override public Extractor create() { return new RawCcExtractor( diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/Ac3ExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/Ac3ExtractorTest.java index ab44e3aed3..1c18e44373 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/Ac3ExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/Ac3ExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.ts; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class Ac3ExtractorTest extends InstrumentationTestCase { public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new Ac3Extractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/AdtsExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/AdtsExtractorTest.java index e30a863d07..bc05be6fa8 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/AdtsExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/AdtsExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.ts; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class AdtsExtractorTest extends InstrumentationTestCase { public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new AdtsExtractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/PsExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/PsExtractorTest.java index ef97bef0ff..e6937ccbc8 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/PsExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/PsExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.ts; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class PsExtractorTest extends InstrumentationTestCase { public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new PsExtractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/TsExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/TsExtractorTest.java index efd653b8d9..09c9facab0 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/TsExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/ts/TsExtractorTest.java @@ -25,6 +25,8 @@ import com.google.android.exoplayer2.extractor.PositionHolder; import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.ts.TsPayloadReader.EsInfo; 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.FakeExtractorOutput; 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. public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new TsExtractor(); @@ -65,7 +67,7 @@ public final class TsExtractorTest extends InstrumentationTestCase { writeJunkData(out, random.nextInt(TS_PACKET_SIZE - 1) + 1); fileData = out.toByteArray(); - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new TsExtractor(); diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/wav/WavExtractorTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/wav/WavExtractorTest.java index a416d644b7..7c969fd386 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/wav/WavExtractorTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/extractor/wav/WavExtractorTest.java @@ -17,7 +17,8 @@ package com.google.android.exoplayer2.extractor.wav; import android.test.InstrumentationTestCase; 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}. @@ -25,7 +26,7 @@ import com.google.android.exoplayer2.testutil.TestUtil; public final class WavExtractorTest extends InstrumentationTestCase { public void testSample() throws Exception { - TestUtil.assertOutput(new TestUtil.ExtractorFactory() { + ExtractorAsserts.assertOutput(new ExtractorFactory() { @Override public Extractor create() { return new WavExtractor(); diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExtractorAsserts.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExtractorAsserts.java new file mode 100644 index 0000000000..fb78b3a634 --- /dev/null +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/ExtractorAsserts.java @@ -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 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 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 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); + } + +} diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java index 9a26c415f4..5819a4b711 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/TestUtil.java @@ -21,8 +21,6 @@ import android.test.MoreAsserts; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Timeline; 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.Listener; import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException; @@ -42,23 +40,8 @@ import org.mockito.MockitoAnnotations; */ 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() {} - public static boolean sniffTestData(Extractor extractor, byte[] data) - throws IOException, InterruptedException { - return sniffTestData(extractor, newExtractorInput(data)); - } - public static boolean sniffTestData(Extractor extractor, FakeExtractorInput input) throws IOException, InterruptedException { while (true) { @@ -86,54 +69,6 @@ public class TestUtil { 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) { return buildTestData(length, length); } @@ -193,15 +128,6 @@ public class TestUtil { 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) throws IOException { return Util.toByteArray(getInputStream(instrumentation, fileName)); @@ -217,10 +143,6 @@ public class TestUtil { 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. */ @@ -247,180 +169,6 @@ public class TestUtil { 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 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 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 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}. *