mirror of
https://github.com/androidx/media.git
synced 2025-05-03 21:57:46 +08:00
Add RawResourceDataSource contract test
PiperOrigin-RevId: 359743165
This commit is contained in:
parent
759b0431bb
commit
1e3337e4b7
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* 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.res.Resources;
|
||||||
|
import android.net.Uri;
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
import com.google.android.exoplayer2.core.test.R;
|
||||||
|
import com.google.android.exoplayer2.testutil.DataSourceContractTest;
|
||||||
|
import com.google.android.exoplayer2.util.Util;
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
/** {@link DataSource} contract tests for {@link RawResourceDataSource}. */
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public final class RawResourceDataSourceContractTest extends DataSourceContractTest {
|
||||||
|
|
||||||
|
private static final byte[] RESOURCE_1_DATA = Util.getUtf8Bytes("resource1 abc\n");
|
||||||
|
private static final byte[] RESOURCE_2_DATA = Util.getUtf8Bytes("resource2 abcdef\n");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected DataSource createDataSource() {
|
||||||
|
return new RawResourceDataSource(ApplicationProvider.getApplicationContext());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ImmutableList<TestResource> getTestResources() {
|
||||||
|
// Android packages raw resources into a single file. When reading a resource other than the
|
||||||
|
// last one, Android does not prevent accidentally reading beyond the end of the resource and
|
||||||
|
// into the next one. We use two resources in this test to ensure that when packaged, at least
|
||||||
|
// one of them has a subsequent resource. This allows the contract test to enforce that the
|
||||||
|
// RawResourceDataSource implementation doesn't erroneously read into the second resource when
|
||||||
|
// opened to read the first.
|
||||||
|
return ImmutableList.of(
|
||||||
|
new TestResource.Builder()
|
||||||
|
.setName("resource 1")
|
||||||
|
.setUri(RawResourceDataSource.buildRawResourceUri(R.raw.resource1))
|
||||||
|
.setExpectedBytes(RESOURCE_1_DATA)
|
||||||
|
.build(),
|
||||||
|
new TestResource.Builder()
|
||||||
|
.setName("resource 2")
|
||||||
|
.setUri(RawResourceDataSource.buildRawResourceUri(R.raw.resource2))
|
||||||
|
.setExpectedBytes(RESOURCE_2_DATA)
|
||||||
|
.build(),
|
||||||
|
// Additional resources using different URI schemes.
|
||||||
|
new TestResource.Builder()
|
||||||
|
.setName("android.resource:// with path")
|
||||||
|
.setUri(
|
||||||
|
Uri.parse(
|
||||||
|
"android.resource://"
|
||||||
|
+ ApplicationProvider.getApplicationContext().getPackageName()
|
||||||
|
+ "/raw/resource1"))
|
||||||
|
.setExpectedBytes(RESOURCE_1_DATA)
|
||||||
|
.build(),
|
||||||
|
new TestResource.Builder()
|
||||||
|
.setName("android.resource:// with ID")
|
||||||
|
.setUri(
|
||||||
|
Uri.parse(
|
||||||
|
"android.resource://"
|
||||||
|
+ ApplicationProvider.getApplicationContext().getPackageName()
|
||||||
|
+ "/"
|
||||||
|
+ R.raw.resource1))
|
||||||
|
.setExpectedBytes(RESOURCE_1_DATA)
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Uri getNotFoundUri() {
|
||||||
|
return RawResourceDataSource.buildRawResourceUri(Resources.ID_NULL);
|
||||||
|
}
|
||||||
|
}
|
1
library/core/src/androidTest/res/raw/resource1
Normal file
1
library/core/src/androidTest/res/raw/resource1
Normal file
@ -0,0 +1 @@
|
|||||||
|
resource1 abc
|
1
library/core/src/androidTest/res/raw/resource2
Normal file
1
library/core/src/androidTest/res/raw/resource2
Normal file
@ -0,0 +1 @@
|
|||||||
|
resource2 abcdef
|
@ -60,7 +60,7 @@ public final class RawResourceDataSource extends BaseDataSource {
|
|||||||
super(message);
|
super(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RawResourceDataSourceException(IOException e) {
|
public RawResourceDataSourceException(Throwable e) {
|
||||||
super(e);
|
super(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,21 +133,39 @@ public final class RawResourceDataSource extends BaseDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
transferInitializing(dataSpec);
|
transferInitializing(dataSpec);
|
||||||
AssetFileDescriptor assetFileDescriptor = resources.openRawResourceFd(resourceId);
|
|
||||||
|
AssetFileDescriptor assetFileDescriptor;
|
||||||
|
try {
|
||||||
|
assetFileDescriptor = resources.openRawResourceFd(resourceId);
|
||||||
|
} catch (Resources.NotFoundException e) {
|
||||||
|
throw new RawResourceDataSourceException(e);
|
||||||
|
}
|
||||||
|
|
||||||
this.assetFileDescriptor = assetFileDescriptor;
|
this.assetFileDescriptor = assetFileDescriptor;
|
||||||
if (assetFileDescriptor == null) {
|
if (assetFileDescriptor == null) {
|
||||||
throw new RawResourceDataSourceException("Resource is compressed: " + uri);
|
throw new RawResourceDataSourceException("Resource is compressed: " + uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long assetFileDescriptorLength = assetFileDescriptor.getLength();
|
||||||
FileInputStream inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
|
FileInputStream inputStream = new FileInputStream(assetFileDescriptor.getFileDescriptor());
|
||||||
this.inputStream = inputStream;
|
this.inputStream = inputStream;
|
||||||
try {
|
try {
|
||||||
|
// We can't rely only on the "skipped < dataSpec.position" check below to detect whether the
|
||||||
|
// position is beyond the end of the resource being read. This is because the file will
|
||||||
|
// typically contain multiple resources, and there's nothing to prevent InputStream.skip()
|
||||||
|
// from succeeding by skipping into the data of the next resource. Hence we also need to check
|
||||||
|
// against the resource length explicitly, which is guaranteed to be set unless the resource
|
||||||
|
// extends to the end of the file.
|
||||||
|
if (assetFileDescriptorLength != AssetFileDescriptor.UNKNOWN_LENGTH
|
||||||
|
&& dataSpec.position > assetFileDescriptorLength) {
|
||||||
|
throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE);
|
||||||
|
}
|
||||||
inputStream.skip(assetFileDescriptor.getStartOffset());
|
inputStream.skip(assetFileDescriptor.getStartOffset());
|
||||||
long skipped = inputStream.skip(dataSpec.position);
|
long skipped = inputStream.skip(dataSpec.position);
|
||||||
if (skipped < dataSpec.position) {
|
if (skipped < dataSpec.position) {
|
||||||
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
|
// We expect the skip to be satisfied in full. If it isn't then we're probably trying to
|
||||||
// skip beyond the end of the data.
|
// read beyond the end of the last resource in the file.
|
||||||
throw new EOFException();
|
throw new DataSourceException(DataSourceException.POSITION_OUT_OF_RANGE);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RawResourceDataSourceException(e);
|
throw new RawResourceDataSourceException(e);
|
||||||
@ -156,7 +174,6 @@ public final class RawResourceDataSource extends BaseDataSource {
|
|||||||
if (dataSpec.length != C.LENGTH_UNSET) {
|
if (dataSpec.length != C.LENGTH_UNSET) {
|
||||||
bytesRemaining = dataSpec.length;
|
bytesRemaining = dataSpec.length;
|
||||||
} else {
|
} else {
|
||||||
long assetFileDescriptorLength = assetFileDescriptor.getLength();
|
|
||||||
// If the length is UNKNOWN_LENGTH then the asset extends to the end of the file.
|
// If the length is UNKNOWN_LENGTH then the asset extends to the end of the file.
|
||||||
bytesRemaining =
|
bytesRemaining =
|
||||||
assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH
|
assetFileDescriptorLength == AssetFileDescriptor.UNKNOWN_LENGTH
|
||||||
|
Loading…
x
Reference in New Issue
Block a user