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:
olly 2017-06-26 04:02:03 -07:00 committed by Oliver Woodman
parent a5eba0162b
commit 2f7de7d3e8
3 changed files with 33 additions and 37 deletions

View File

@ -15,10 +15,8 @@
*/ */
package com.google.android.exoplayer2.upstream; package com.google.android.exoplayer2.upstream;
import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import android.test.MoreAsserts;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
/** /**
@ -27,36 +25,19 @@ import com.google.android.exoplayer2.testutil.TestUtil;
public final class AssetDataSourceTest extends InstrumentationTestCase { public final class AssetDataSourceTest extends InstrumentationTestCase {
private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3"; private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3";
private static final long DATA_LENGTH = 1024;
public void testReadFileUri() throws Exception { public void testReadFileUri() throws Exception {
Context context = getInstrumentation().getContext(); AssetDataSource dataSource = new AssetDataSource(getInstrumentation().getContext());
AssetDataSource dataSource = new AssetDataSource(context); DataSpec dataSpec = new DataSpec(Uri.parse("file:///android_asset/" + DATA_PATH));
Uri assetUri = Uri.parse("file:///android_asset/" + DATA_PATH); TestUtil.assertDataSourceContent(dataSource, dataSpec,
DataSpec dataSpec = new DataSpec(assetUri); TestUtil.getByteArray(getInstrumentation(), DATA_PATH));
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();
}
} }
public void testReadAssetUri() throws Exception { public void testReadAssetUri() throws Exception {
Context context = getInstrumentation().getContext(); AssetDataSource dataSource = new AssetDataSource(getInstrumentation().getContext());
AssetDataSource dataSource = new AssetDataSource(context); DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + DATA_PATH));
Uri assetUri = Uri.parse("asset:///" + DATA_PATH); TestUtil.assertDataSourceContent(dataSource, dataSpec,
DataSpec dataSpec = new DataSpec(assetUri); TestUtil.getByteArray(getInstrumentation(), DATA_PATH));
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();
}
} }
} }

View File

@ -23,7 +23,6 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import android.test.MoreAsserts;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; 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 AUTHORITY = "com.google.android.exoplayer2.core.test";
private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3"; private static final String DATA_PATH = "binary/1024_incrementing_bytes.mp3";
private static final long DATA_LENGTH = 1024;
public void testReadValidUri() throws Exception { public void testReadValidUri() throws Exception {
ContentDataSource dataSource = new ContentDataSource(getInstrumentation().getContext()); ContentDataSource dataSource = new ContentDataSource(getInstrumentation().getContext());
@ -44,14 +42,8 @@ public final class ContentDataSourceTest extends InstrumentationTestCase {
.authority(AUTHORITY) .authority(AUTHORITY)
.path(DATA_PATH).build(); .path(DATA_PATH).build();
DataSpec dataSpec = new DataSpec(contentUri); DataSpec dataSpec = new DataSpec(contentUri);
try { TestUtil.assertDataSourceContent(dataSource, dataSpec,
long length = dataSource.open(dataSpec); TestUtil.getByteArray(getInstrumentation(), DATA_PATH));
assertEquals(DATA_LENGTH, length);
byte[] readData = TestUtil.readToEnd(dataSource);
MoreAsserts.assertEquals(TestUtil.getByteArray(getInstrumentation(), DATA_PATH), readData);
} finally {
dataSource.close();
}
} }
public void testReadInvalidUri() throws Exception { public void testReadInvalidUri() throws Exception {
@ -66,6 +58,7 @@ public final class ContentDataSourceTest extends InstrumentationTestCase {
fail(); fail();
} catch (ContentDataSource.ContentDataSourceException e) { } catch (ContentDataSource.ContentDataSourceException e) {
// Expected. // Expected.
assertTrue(e.getCause() instanceof FileNotFoundException);
} finally { } finally {
dataSource.close(); dataSource.close();
} }

View File

@ -17,12 +17,14 @@ package com.google.android.exoplayer2.testutil;
import android.app.Instrumentation; import android.app.Instrumentation;
import android.test.InstrumentationTestCase; import android.test.InstrumentationTestCase;
import android.test.MoreAsserts;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
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.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap; import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException; import com.google.android.exoplayer2.testutil.FakeExtractorInput.SimulatedIOException;
import com.google.android.exoplayer2.upstream.DataSource; 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.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.IOException; 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();
}
}
} }