diff --git a/library/core/src/androidTest/AndroidManifest.xml b/library/core/src/androidTest/AndroidManifest.xml index 04a07c4d50..4ffc92ac24 100644 --- a/library/core/src/androidTest/AndroidManifest.xml +++ b/library/core/src/androidTest/AndroidManifest.xml @@ -26,7 +26,7 @@ tools:ignore="MissingApplicationIcon,HardcodedDebugMode"> + android:name="com.google.android.exoplayer2.upstream.TestContentProvider"/> getTestResources() throws Exception { + byte[] completeData = + TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), DATA_PATH); + return ImmutableList.of( + new TestResource.Builder() + .setName("simple (pipe=false)") + .setUri(TestContentProvider.buildUri(DATA_PATH, /* pipeMode= */ false)) + .setExpectedBytes(completeData) + .build(), + new TestResource.Builder() + .setName("simple (pipe=true)") + .setUri(TestContentProvider.buildUri(DATA_PATH, /* pipeMode= */ true)) + .setExpectedBytes(completeData) + .resolvesToUnknownLength() + .build()); + } + + @Override + protected Uri getNotFoundUri() { + return TestContentProvider.buildUri("not/a/real/path", /* pipeMode= */ false); + } +} diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java index 22442ca85f..79b661e717 100644 --- a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/ContentDataSourceTest.java @@ -19,15 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.Assert.fail; import static org.junit.Assert.assertThrows; -import android.content.ContentProvider; -import android.content.ContentResolver; -import android.content.ContentValues; -import android.content.res.AssetFileDescriptor; -import android.database.Cursor; import android.net.Uri; -import android.os.Bundle; -import android.os.ParcelFileDescriptor; -import androidx.annotation.Nullable; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.C; @@ -35,7 +27,6 @@ import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.upstream.ContentDataSource.ContentDataSourceException; import java.io.EOFException; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.Arrays; import org.junit.Test; @@ -45,7 +36,6 @@ import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) public final class ContentDataSourceTest { - private static final String AUTHORITY = "com.google.android.exoplayer2.core.test"; private static final String DATA_PATH = "media/mp3/1024_incrementing_bytes.mp3"; @Test @@ -141,98 +131,4 @@ public final class ContentDataSourceTest { } } - /** - * A {@link ContentProvider} for the test. - */ - public static final class TestContentProvider extends ContentProvider - implements ContentProvider.PipeDataWriter { - - private static final String PARAM_PIPE_MODE = "pipe-mode"; - - public static Uri buildUri(String filePath, boolean pipeMode) { - Uri.Builder builder = new Uri.Builder() - .scheme(ContentResolver.SCHEME_CONTENT) - .authority(AUTHORITY) - .path(filePath); - if (pipeMode) { - builder.appendQueryParameter(TestContentProvider.PARAM_PIPE_MODE, "1"); - } - return builder.build(); - } - - @Override - public boolean onCreate() { - return true; - } - - @Override - public Cursor query( - Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { - throw new UnsupportedOperationException(); - } - - @Override - public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { - if (uri.getPath() == null) { - return null; - } - try { - String fileName = getFileName(uri); - boolean pipeMode = uri.getQueryParameter(PARAM_PIPE_MODE) != null; - if (pipeMode) { - ParcelFileDescriptor fileDescriptor = openPipeHelper(uri, null, null, null, this); - return new AssetFileDescriptor(fileDescriptor, 0, C.LENGTH_UNSET); - } else { - return getContext().getAssets().openFd(fileName); - } - } catch (IOException e) { - FileNotFoundException exception = new FileNotFoundException(e.getMessage()); - exception.initCause(e); - throw exception; - } - } - - @Override - public String getType(Uri uri) { - throw new UnsupportedOperationException(); - } - - @Override - public Uri insert(Uri uri, ContentValues values) { - throw new UnsupportedOperationException(); - } - - @Override - public int delete(Uri uri, String selection, String[] selectionArgs) { - throw new UnsupportedOperationException(); - } - - @Override - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { - throw new UnsupportedOperationException(); - } - - @Override - public void writeDataToPipe( - ParcelFileDescriptor output, - Uri uri, - String mimeType, - @Nullable Bundle opts, - @Nullable Object args) { - try { - byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri)); - FileOutputStream outputStream = new FileOutputStream(output.getFileDescriptor()); - outputStream.write(data); - outputStream.close(); - } catch (IOException e) { - throw new RuntimeException("Error writing to pipe", e); - } - } - - private static String getFileName(Uri uri) { - return uri.getPath().replaceFirst("/", ""); - } - - } - } diff --git a/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java new file mode 100644 index 0000000000..42bfd178e2 --- /dev/null +++ b/library/core/src/androidTest/java/com/google/android/exoplayer2/upstream/TestContentProvider.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2020 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.upstream; + +import android.content.ContentProvider; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.res.AssetFileDescriptor; +import android.database.Cursor; +import android.net.Uri; +import android.os.Bundle; +import android.os.ParcelFileDescriptor; +import androidx.annotation.Nullable; +import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.testutil.TestUtil; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +/** A {@link ContentProvider} for tests of {@link ContentDataSource}. */ +public final class TestContentProvider extends ContentProvider + implements ContentProvider.PipeDataWriter { + + private static final String AUTHORITY = "com.google.android.exoplayer2.core.test"; + private static final String PARAM_PIPE_MODE = "pipe-mode"; + + public static Uri buildUri(String filePath, boolean pipeMode) { + Uri.Builder builder = + new Uri.Builder() + .scheme(ContentResolver.SCHEME_CONTENT) + .authority(AUTHORITY) + .path(filePath); + if (pipeMode) { + builder.appendQueryParameter(TestContentProvider.PARAM_PIPE_MODE, "1"); + } + return builder.build(); + } + + @Override + public boolean onCreate() { + return true; + } + + @Override + public Cursor query( + Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + throw new UnsupportedOperationException(); + } + + @Override + public AssetFileDescriptor openAssetFile(Uri uri, String mode) throws FileNotFoundException { + if (uri.getPath() == null) { + return null; + } + try { + String fileName = getFileName(uri); + boolean pipeMode = uri.getQueryParameter(PARAM_PIPE_MODE) != null; + if (pipeMode) { + ParcelFileDescriptor fileDescriptor = + openPipeHelper( + uri, /* mimeType= */ null, /* opts= */ null, /* args= */ null, /* func= */ this); + return new AssetFileDescriptor( + fileDescriptor, /* startOffset= */ 0, /* length= */ C.LENGTH_UNSET); + } else { + return getContext().getAssets().openFd(fileName); + } + } catch (IOException e) { + FileNotFoundException exception = new FileNotFoundException(e.getMessage()); + exception.initCause(e); + throw exception; + } + } + + @Override + public String getType(Uri uri) { + throw new UnsupportedOperationException(); + } + + @Override + public Uri insert(Uri uri, ContentValues values) { + throw new UnsupportedOperationException(); + } + + @Override + public int delete(Uri uri, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + + @Override + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + + @Override + public void writeDataToPipe( + ParcelFileDescriptor output, + Uri uri, + String mimeType, + @Nullable Bundle opts, + @Nullable Object args) { + try { + byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri)); + FileOutputStream outputStream = new FileOutputStream(output.getFileDescriptor()); + outputStream.write(data); + outputStream.close(); + } catch (IOException e) { + throw new RuntimeException("Error writing to pipe", e); + } + } + + private static String getFileName(Uri uri) { + return uri.getPath().replaceFirst("/", ""); + } + +} diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java index ac11f11724..707928c048 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/DataSourceContractTest.java @@ -55,7 +55,7 @@ public abstract class DataSourceContractTest { @Rule public final AdditionalFailureInfo additionalFailureInfo = new AdditionalFailureInfo(); /** Creates and returns an instance of the {@link DataSource}. */ - protected abstract DataSource createDataSource(); + protected abstract DataSource createDataSource() throws Exception; /** * Returns {@link TestResource} instances. @@ -66,7 +66,7 @@ public abstract class DataSourceContractTest { * If multiple resources are returned, it's recommended to disambiguate them using {@link * TestResource.Builder#setName(String)}. */ - protected abstract ImmutableList getTestResources(); + protected abstract ImmutableList getTestResources() throws Exception; /** * Returns a {@link Uri} that doesn't resolve.
If multiple resources are returned, it's recommended to disambiguate them using {@link * TestResource.Builder#setName(String)}. */ - protected abstract ImmutableList getTestResources(); + protected abstract ImmutableList getTestResources() throws Exception; /** * Returns a {@link Uri} that doesn't resolve.