Skip to content

Commit

Permalink
Merge ContentDataSource fixes + tests from GitHub
Browse files Browse the repository at this point in the history
https://github.com/google/ExoPlayer/pull/2963/files
8bb6439

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160128047
  • Loading branch information
ojw28 committed Jun 30, 2017
1 parent a5eba01 commit 2f7de7d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -27,36 +25,19 @@
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand All @@ -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());
Expand All @@ -44,14 +42,8 @@ public void testReadValidUri() throws Exception {
.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 {
Expand All @@ -66,6 +58,7 @@ public void testReadInvalidUri() throws Exception {
fail();
} catch (ContentDataSource.ContentDataSourceException e) {
// Expected.
assertTrue(e.getCause() instanceof FileNotFoundException);
} finally {
dataSource.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

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;
Expand Down Expand Up @@ -390,4 +392,24 @@ public static void assertThrows(Extractor extractor, byte[] fileData,
}
}

/**
* 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();
}
}

}

0 comments on commit 2f7de7d

Please sign in to comment.