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"
android:usesCleartextTraffic="true">
<provider
android:authorities="com.google.android.exoplayer2.core.test"
android:name="com.google.android.exoplayer2.upstream.TestContentProvider"/>
android:authorities="com.google.android.exoplayer2.testutil.AssetContentProvider"
android:name="com.google.android.exoplayer2.testutil.AssetContentProvider"/>
</application>
<instrumentation

View File

@ -18,6 +18,7 @@ package com.google.android.exoplayer2.upstream;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
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.TestUtil;
import com.google.common.collect.ImmutableList;
@ -41,18 +42,18 @@ public final class ContentDataSourceContractTest extends DataSourceContractTest
return ImmutableList.of(
new TestResource.Builder()
.setName("simple (pipe=false)")
.setUri(TestContentProvider.buildUri(DATA_PATH, /* pipeMode= */ false))
.setUri(AssetContentProvider.buildUri(DATA_PATH, /* pipeMode= */ false))
.setExpectedBytes(completeData)
.build(),
new TestResource.Builder()
.setName("simple (pipe=true)")
.setUri(TestContentProvider.buildUri(DATA_PATH, /* pipeMode= */ true))
.setUri(AssetContentProvider.buildUri(DATA_PATH, /* pipeMode= */ true))
.setExpectedBytes(completeData)
.build());
}
@Override
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.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.testutil.AssetContentProvider;
import com.google.android.exoplayer2.testutil.TestUtil;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -69,7 +70,7 @@ public final class ContentDataSourceTest {
public void readInvalidUri() throws Exception {
ContentDataSource dataSource =
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);
try {
dataSource.open(dataSpec);
@ -83,7 +84,7 @@ public final class ContentDataSourceTest {
}
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 =
new ContentDataSource(ApplicationProvider.getApplicationContext());
try {

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.exoplayer2.upstream;
package com.google.android.exoplayer2.testutil;
import android.content.ContentProvider;
import android.content.ContentResolver;
@ -26,16 +26,17 @@ import android.os.ParcelFileDescriptor;
import android.system.ErrnoException;
import android.system.OsConstants;
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.FileOutputStream;
import java.io.IOException;
/** A {@link ContentProvider} for tests of {@link ContentDataSource}. */
public final class TestContentProvider extends ContentProvider
/** A {@link ContentProvider} for reading asset data. */
public final class AssetContentProvider extends ContentProvider
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";
public static Uri buildUri(String filePath, boolean pipeMode) {
@ -45,7 +46,7 @@ public final class TestContentProvider extends ContentProvider
.authority(AUTHORITY)
.path(filePath);
if (pipeMode) {
builder.appendQueryParameter(TestContentProvider.PARAM_PIPE_MODE, "1");
builder.appendQueryParameter(PARAM_PIPE_MODE, "1");
}
return builder.build();
}
@ -116,8 +117,7 @@ public final class TestContentProvider extends ContentProvider
byte[] data = TestUtil.getByteArray(getContext(), getFileName(uri));
outputStream.write(data);
} catch (IOException e) {
if (e.getCause() instanceof ErrnoException
&& ((ErrnoException) e.getCause()).errno == OsConstants.EPIPE) {
if (isBrokenPipe(e)) {
// 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.
// [See internal b/186728171].
@ -130,4 +130,10 @@ public final class TestContentProvider extends ContentProvider
private static String getFileName(Uri uri) {
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;
}
}