Move test asset ContentProvider to testutil

PiperOrigin-RevId: 402772598
This commit is contained in:
olly 2021-10-13 10:28:51 +01:00 committed by Oliver Woodman
parent 91da6a3434
commit 02719fd5b9
4 changed files with 23 additions and 15 deletions

View File

@ -27,8 +27,8 @@
tools:ignore="MissingApplicationIcon,HardcodedDebugMode" tools:ignore="MissingApplicationIcon,HardcodedDebugMode"
android:usesCleartextTraffic="true"> android:usesCleartextTraffic="true">
<provider <provider
android:authorities="com.google.android.exoplayer2.core.test" android:authorities="com.google.android.exoplayer2.testutil.AssetContentProvider"
android:name="com.google.android.exoplayer2.upstream.TestContentProvider"/> android:name="com.google.android.exoplayer2.testutil.AssetContentProvider"/>
</application> </application>
<instrumentation <instrumentation

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.upstream;
import android.net.Uri; import android.net.Uri;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.testutil.AssetContentProvider;
import com.google.android.exoplayer2.testutil.DataSourceContractTest; import com.google.android.exoplayer2.testutil.DataSourceContractTest;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -41,18 +42,18 @@ public final class ContentDataSourceContractTest extends DataSourceContractTest
return ImmutableList.of( return ImmutableList.of(
new TestResource.Builder() new TestResource.Builder()
.setName("simple (pipe=false)") .setName("simple (pipe=false)")
.setUri(TestContentProvider.buildUri(DATA_PATH, /* pipeMode= */ false)) .setUri(AssetContentProvider.buildUri(DATA_PATH, /* pipeMode= */ false))
.setExpectedBytes(completeData) .setExpectedBytes(completeData)
.build(), .build(),
new TestResource.Builder() new TestResource.Builder()
.setName("simple (pipe=true)") .setName("simple (pipe=true)")
.setUri(TestContentProvider.buildUri(DATA_PATH, /* pipeMode= */ true)) .setUri(AssetContentProvider.buildUri(DATA_PATH, /* pipeMode= */ true))
.setExpectedBytes(completeData) .setExpectedBytes(completeData)
.build()); .build());
} }
@Override @Override
protected Uri getNotFoundUri() { protected Uri getNotFoundUri() {
return TestContentProvider.buildUri("not/a/real/path", /* pipeMode= */ false); return AssetContentProvider.buildUri("not/a/real/path", /* pipeMode= */ false);
} }
} }

View File

@ -22,6 +22,7 @@ import android.net.Uri;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.AssetContentProvider;
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;
@ -69,7 +70,7 @@ public final class ContentDataSourceTest {
public void readInvalidUri() throws Exception { public void readInvalidUri() throws Exception {
ContentDataSource dataSource = ContentDataSource dataSource =
new ContentDataSource(ApplicationProvider.getApplicationContext()); new ContentDataSource(ApplicationProvider.getApplicationContext());
Uri contentUri = TestContentProvider.buildUri("does/not.exist", false); Uri contentUri = AssetContentProvider.buildUri("does/not.exist", false);
DataSpec dataSpec = new DataSpec(contentUri); DataSpec dataSpec = new DataSpec(contentUri);
try { try {
dataSource.open(dataSpec); dataSource.open(dataSpec);
@ -83,7 +84,7 @@ public final class ContentDataSourceTest {
} }
private static void assertData(int offset, int length, boolean pipeMode) throws IOException { private static void assertData(int offset, int length, boolean pipeMode) throws IOException {
Uri contentUri = TestContentProvider.buildUri(DATA_PATH, pipeMode); Uri contentUri = AssetContentProvider.buildUri(DATA_PATH, pipeMode);
ContentDataSource dataSource = ContentDataSource dataSource =
new ContentDataSource(ApplicationProvider.getApplicationContext()); new ContentDataSource(ApplicationProvider.getApplicationContext());
try { try {

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.google.android.exoplayer2.upstream; package com.google.android.exoplayer2.testutil;
import android.content.ContentProvider; import android.content.ContentProvider;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -26,16 +26,17 @@ import android.os.ParcelFileDescriptor;
import android.system.ErrnoException; import android.system.ErrnoException;
import android.system.OsConstants; import android.system.OsConstants;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.util.Util;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
/** A {@link ContentProvider} for tests of {@link ContentDataSource}. */ /** A {@link ContentProvider} for reading asset data. */
public final class TestContentProvider extends ContentProvider public final class AssetContentProvider extends ContentProvider
implements ContentProvider.PipeDataWriter<Object> { implements ContentProvider.PipeDataWriter<Object> {
private static final String AUTHORITY = "com.google.android.exoplayer2.core.test"; private static final String AUTHORITY =
"com.google.android.exoplayer2.testutil.AssetContentProvider";
private static final String PARAM_PIPE_MODE = "pipe-mode"; private static final String PARAM_PIPE_MODE = "pipe-mode";
public static Uri buildUri(String filePath, boolean pipeMode) { public static Uri buildUri(String filePath, boolean pipeMode) {
@ -45,7 +46,7 @@ public final class TestContentProvider extends ContentProvider
.authority(AUTHORITY) .authority(AUTHORITY)
.path(filePath); .path(filePath);
if (pipeMode) { if (pipeMode) {
builder.appendQueryParameter(TestContentProvider.PARAM_PIPE_MODE, "1"); builder.appendQueryParameter(PARAM_PIPE_MODE, "1");
} }
return builder.build(); return builder.build();
} }
@ -116,8 +117,7 @@ public final class TestContentProvider extends ContentProvider
byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri)); byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri));
outputStream.write(data); outputStream.write(data);
} catch (IOException e) { } catch (IOException e) {
if (e.getCause() instanceof ErrnoException if (isBrokenPipe(e)) {
&& ((ErrnoException) e.getCause()).errno == OsConstants.EPIPE) {
// Swallow the exception if it's caused by a broken pipe - this indicates the reader has // Swallow the exception if it's caused by a broken pipe - this indicates the reader has
// closed the pipe and is therefore no longer interested in the data being written. // closed the pipe and is therefore no longer interested in the data being written.
// [See internal b/186728171]. // [See internal b/186728171].
@ -130,4 +130,10 @@ public final class TestContentProvider extends ContentProvider
private static String getFileName(Uri uri) { private static String getFileName(Uri uri) {
return uri.getPath().replaceFirst("/", ""); return uri.getPath().replaceFirst("/", "");
} }
private static boolean isBrokenPipe(IOException e) {
return Util.SDK_INT >= 21
&& e.getCause() instanceof ErrnoException
&& ((ErrnoException) e.getCause()).errno == OsConstants.EPIPE;
}
} }