Merge ContentDataSource fixes + tests from GitHub
https://github.com/google/ExoPlayer/pull/2963/files
8bb643976f
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160128047
This commit is contained in:
parent
a5eba0162b
commit
2f7de7d3e8
@ -15,10 +15,8 @@
|
||||
*/
|
||||
package com.google.android.exoplayer2.upstream;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.test.InstrumentationTestCase;
|
||||
import android.test.MoreAsserts;
|
||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||
|
||||
/**
|
||||
@ -27,36 +25,19 @@ import com.google.android.exoplayer2.testutil.TestUtil;
|
||||
public final class AssetDataSourceTest extends InstrumentationTestCase {
|
||||
|
||||
private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3";
|
||||
private static final long DATA_LENGTH = 1024;
|
||||
|
||||
public void testReadFileUri() throws Exception {
|
||||
Context context = getInstrumentation().getContext();
|
||||
AssetDataSource dataSource = new AssetDataSource(context);
|
||||
Uri assetUri = Uri.parse("file:///android_asset/" + DATA_PATH);
|
||||
DataSpec dataSpec = new DataSpec(assetUri);
|
||||
try {
|
||||
long length = dataSource.open(dataSpec);
|
||||
assertEquals(DATA_LENGTH, length);
|
||||
byte[] readData = TestUtil.readToEnd(dataSource);
|
||||
MoreAsserts.assertEquals(TestUtil.getByteArray(getInstrumentation(), DATA_PATH), readData);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
AssetDataSource dataSource = new AssetDataSource(getInstrumentation().getContext());
|
||||
DataSpec dataSpec = new DataSpec(Uri.parse("file:///android_asset/" + DATA_PATH));
|
||||
TestUtil.assertDataSourceContent(dataSource, dataSpec,
|
||||
TestUtil.getByteArray(getInstrumentation(), DATA_PATH));
|
||||
}
|
||||
|
||||
public void testReadAssetUri() throws Exception {
|
||||
Context context = getInstrumentation().getContext();
|
||||
AssetDataSource dataSource = new AssetDataSource(context);
|
||||
Uri assetUri = Uri.parse("asset:///" + DATA_PATH);
|
||||
DataSpec dataSpec = new DataSpec(assetUri);
|
||||
try {
|
||||
long length = dataSource.open(dataSpec);
|
||||
assertEquals(DATA_LENGTH, length);
|
||||
byte[] readData = TestUtil.readToEnd(dataSource);
|
||||
MoreAsserts.assertEquals(TestUtil.getByteArray(getInstrumentation(), DATA_PATH), readData);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
AssetDataSource dataSource = new AssetDataSource(getInstrumentation().getContext());
|
||||
DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + DATA_PATH));
|
||||
TestUtil.assertDataSourceContent(dataSource, dataSpec,
|
||||
TestUtil.getByteArray(getInstrumentation(), DATA_PATH));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.test.InstrumentationTestCase;
|
||||
import android.test.MoreAsserts;
|
||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
@ -35,7 +34,6 @@ public final class ContentDataSourceTest extends InstrumentationTestCase {
|
||||
|
||||
private static final String AUTHORITY = "com.google.android.exoplayer2.core.test";
|
||||
private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3";
|
||||
private static final long DATA_LENGTH = 1024;
|
||||
|
||||
public void testReadValidUri() throws Exception {
|
||||
ContentDataSource dataSource = new ContentDataSource(getInstrumentation().getContext());
|
||||
@ -44,14 +42,8 @@ public final class ContentDataSourceTest extends InstrumentationTestCase {
|
||||
.authority(AUTHORITY)
|
||||
.path(DATA_PATH).build();
|
||||
DataSpec dataSpec = new DataSpec(contentUri);
|
||||
try {
|
||||
long length = dataSource.open(dataSpec);
|
||||
assertEquals(DATA_LENGTH, length);
|
||||
byte[] readData = TestUtil.readToEnd(dataSource);
|
||||
MoreAsserts.assertEquals(TestUtil.getByteArray(getInstrumentation(), DATA_PATH), readData);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
TestUtil.assertDataSourceContent(dataSource, dataSpec,
|
||||
TestUtil.getByteArray(getInstrumentation(), DATA_PATH));
|
||||
}
|
||||
|
||||
public void testReadInvalidUri() throws Exception {
|
||||
@ -66,6 +58,7 @@ public final class ContentDataSourceTest extends InstrumentationTestCase {
|
||||
fail();
|
||||
} catch (ContentDataSource.ContentDataSourceException e) {
|
||||
// Expected.
|
||||
assertTrue(e.getCause() instanceof FileNotFoundException);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
|
@ -17,12 +17,14 @@ package com.google.android.exoplayer2.testutil;
|
||||
|
||||
import android.app.Instrumentation;
|
||||
import android.test.InstrumentationTestCase;
|
||||
import android.test.MoreAsserts;
|
||||
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.upstream.DataSource;
|
||||
import com.google.android.exoplayer2.upstream.DataSpec;
|
||||
import com.google.android.exoplayer2.util.Assertions;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
import java.io.IOException;
|
||||
@ -390,4 +392,24 @@ public class TestUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that data read from a {@link DataSource} matches {@code expected}.
|
||||
*
|
||||
* @param dataSource The {@link DataSource} through which to read.
|
||||
* @param dataSpec The {@link DataSpec} to use when opening the {@link DataSource}.
|
||||
* @param expectedData The expected data.
|
||||
* @throws IOException If an error occurs reading fom the {@link DataSource}.
|
||||
*/
|
||||
public static void assertDataSourceContent(DataSource dataSource, DataSpec dataSpec,
|
||||
byte[] expectedData) throws IOException {
|
||||
try {
|
||||
long length = dataSource.open(dataSpec);
|
||||
Assert.assertEquals(length, expectedData.length);
|
||||
byte[] readData = TestUtil.readToEnd(dataSource);
|
||||
MoreAsserts.assertEquals(expectedData, readData);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user