Skip to content

Commit

Permalink
Introduce FakeExtractorInput (based loosely on RecordableExtractorInp…
Browse files Browse the repository at this point in the history
…ut).

- Use it to simplify a bunch of tests.
- Will also replace RecordableExtractorInput in a subsequent CL.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117220030
  • Loading branch information
ojw28 committed Jun 15, 2016
1 parent a1a48ab commit 300b58e
Show file tree
Hide file tree
Showing 5 changed files with 306 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
*/
package com.google.android.exoplayer.extractor.webm;

import com.google.android.exoplayer.C;
import com.google.android.exoplayer.extractor.DefaultExtractorInput;
import com.google.android.exoplayer.extractor.ExtractorInput;
import com.google.android.exoplayer.testutil.FakeDataSource;
import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DataSpec;

import android.net.Uri;
import com.google.android.exoplayer.testutil.FakeExtractorInput;
import com.google.android.exoplayer.testutil.TestUtil;

import junit.framework.TestCase;

Expand Down Expand Up @@ -140,22 +135,15 @@ private static void assertEvents(ExtractorInput input, List<String> expectedEven

/**
* Helper to build an {@link ExtractorInput} from byte data.
* <p>
* Each argument must be able to cast to a byte value.
*
* @param data Zero or more integers with values between {@code 0x00} and {@code 0xFF}.
* @return An {@link ExtractorInput} from which the data can be read.
* @throws IOException If an error occurs creating the input.
*/
private static ExtractorInput createTestInput(int... data) throws IOException {
byte[] bytes = new byte[data.length];
for (int i = 0; i < data.length; i++) {
bytes[i] = (byte) data[i];
}
DataSource dataSource = new FakeDataSource.Builder().appendReadData(bytes).build();
dataSource.open(new DataSpec(Uri.parse("http://www.google.com")));
ExtractorInput input = new DefaultExtractorInput(dataSource, 0, C.LENGTH_UNBOUNDED);
return input;
private static ExtractorInput createTestInput(int... data) {
return new FakeExtractorInput.Builder()
.setData(TestUtil.createByteArray(data))
.setSimulateUnknownLength(true)
.build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,20 @@
package com.google.android.exoplayer.extractor.webm;

import com.google.android.exoplayer.C;
import com.google.android.exoplayer.extractor.DefaultExtractorInput;
import com.google.android.exoplayer.extractor.ExtractorInput;
import com.google.android.exoplayer.testutil.FakeDataSource;
import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DataSpec;

import android.net.Uri;
import com.google.android.exoplayer.testutil.FakeExtractorInput;
import com.google.android.exoplayer.testutil.FakeExtractorInput.SimulatedIOException;

import junit.framework.TestCase;

import java.io.EOFException;
import java.io.IOException;
import java.util.Arrays;

/**
* Tests for {@link VarintReader}.
*/
public class VarintReaderTest extends TestCase {
public final class VarintReaderTest extends TestCase {

private static final String TEST_URI = "http://www.google.com";
private static final byte MAX_BYTE = (byte) 0xFF;

private static final byte[] DATA_1_BYTE_0 = new byte[] {(byte) 0x80};
Expand Down Expand Up @@ -88,12 +82,10 @@ public class VarintReaderTest extends TestCase {

public void testReadVarintEndOfInputAtStart() throws IOException, InterruptedException {
VarintReader reader = new VarintReader();
// Build an input, and read to the end.
DataSource dataSource = buildDataSource(new byte[1]);
dataSource.open(new DataSpec(Uri.parse(TEST_URI)));
ExtractorInput input = new DefaultExtractorInput(dataSource, 0, C.LENGTH_UNBOUNDED);
int bytesRead = input.read(new byte[1], 0, 1);
assertEquals(1, bytesRead);
// Build an input with no data.
ExtractorInput input = new FakeExtractorInput.Builder()
.setSimulateUnknownLength(true)
.build();
// End of input allowed.
long result = reader.readUnsignedVarint(input, true, false, 8);
assertEquals(C.RESULT_END_OF_INPUT, result);
Expand All @@ -108,9 +100,10 @@ public void testReadVarintEndOfInputAtStart() throws IOException, InterruptedExc

public void testReadVarintExceedsMaximumAllowedLength() throws IOException, InterruptedException {
VarintReader reader = new VarintReader();
DataSource dataSource = buildDataSource(DATA_8_BYTE_0);
dataSource.open(new DataSpec(Uri.parse(TEST_URI)));
ExtractorInput input = new DefaultExtractorInput(dataSource, 0, C.LENGTH_UNBOUNDED);
ExtractorInput input = new FakeExtractorInput.Builder()
.setData(DATA_8_BYTE_0)
.setSimulateUnknownLength(true)
.build();
long result = reader.readUnsignedVarint(input, false, true, 4);
assertEquals(C.RESULT_MAX_LENGTH_EXCEEDED, result);
}
Expand Down Expand Up @@ -189,55 +182,33 @@ public void testReadVarintFlaky() throws IOException, InterruptedException {

private static void testReadVarint(VarintReader reader, boolean removeMask, byte[] data,
int expectedLength, long expectedValue) throws IOException, InterruptedException {
DataSource dataSource = buildDataSource(data);
dataSource.open(new DataSpec(Uri.parse(TEST_URI)));
ExtractorInput input = new DefaultExtractorInput(dataSource, 0, C.LENGTH_UNBOUNDED);
ExtractorInput input = new FakeExtractorInput.Builder()
.setData(data)
.setSimulateUnknownLength(true)
.build();
long result = reader.readUnsignedVarint(input, false, removeMask, 8);
assertEquals(expectedLength, input.getPosition());
assertEquals(expectedValue, result);
}

private static void testReadVarintFlaky(VarintReader reader, boolean removeMask, byte[] data,
int expectedLength, long expectedValue) throws IOException, InterruptedException {
DataSource dataSource = buildFlakyDataSource(data);
ExtractorInput input = null;
long position = 0;
ExtractorInput input = new FakeExtractorInput.Builder()
.setData(data)
.setSimulateUnknownLength(true)
.setSimulateIOErrors(true)
.setSimulatePartialReads(true)
.build();
long result = -1;
while (result == -1) {
dataSource.open(new DataSpec(Uri.parse(TEST_URI), position, C.LENGTH_UNBOUNDED, null));
input = new DefaultExtractorInput(dataSource, position, C.LENGTH_UNBOUNDED);
try {
result = reader.readUnsignedVarint(input, false, removeMask, 8);
position = input.getPosition();
} catch (IOException e) {
// Expected. We'll try again from the position that the input was advanced to.
position = input.getPosition();
dataSource.close();
} catch (SimulatedIOException e) {
// Expected.
}
}
assertEquals(expectedLength, input.getPosition());
assertEquals(expectedValue, result);
}

private static DataSource buildDataSource(byte[] data) {
FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadData(data);
return builder.build();
}

private static DataSource buildFlakyDataSource(byte[] data) {
FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadError(new IOException("A"));
builder.appendReadData(new byte[] {data[0]});
if (data.length > 1) {
builder.appendReadError(new IOException("B"));
builder.appendReadData(new byte[] {data[1]});
}
if (data.length > 2) {
builder.appendReadError(new IOException("C"));
builder.appendReadData(Arrays.copyOfRange(data, 2, data.length));
}
return builder.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class FakeDataSource implements DataSource {
private int currentSegmentIndex;
private long bytesRemaining;

public FakeDataSource(boolean simulateUnknownLength, ArrayList<Segment> segments) {
private FakeDataSource(boolean simulateUnknownLength, ArrayList<Segment> segments) {
this.simulateUnknownLength = simulateUnknownLength;
this.segments = segments;
long totalLength = 0;
Expand Down Expand Up @@ -163,7 +163,7 @@ public boolean isErrorSegment() {
/**
* Builder of {@link FakeDataSource} instances.
*/
public static class Builder {
public static final class Builder {

private final ArrayList<Segment> segments;
private boolean simulateUnknownLength;
Expand Down
Loading

0 comments on commit 300b58e

Please sign in to comment.