DataSourceContractTest: Add tests for resolved vs original URI

PiperOrigin-RevId: 688076205
This commit is contained in:
ibaker 2024-10-21 04:03:19 -07:00 committed by Copybara-Service
parent f2ecca3b6a
commit 74bbd7727d
4 changed files with 54 additions and 15 deletions

View File

@ -6,6 +6,10 @@
* ExoPlayer: * ExoPlayer:
* Transformer: * Transformer:
* DataSource: * DataSource:
* `DataSourceContractTest`: Assert that `DataSource.getUri()` returns the
resolved URI (as documented). Where this is different to the requested
URI, tests can indicate this using the new
`DataSourceContractTest.TestResource.Builder.setResolvedUri()` method.
* Audio: * Audio:
* Video: * Video:
* Text: * Text:

View File

@ -51,6 +51,7 @@ public class ResolvingDataSourceContractTest extends DataSourceContractTest {
new TestResource.Builder() new TestResource.Builder()
.setName("simple") .setName("simple")
.setUri(URI) .setUri(URI)
.setResolvedUri(RESOLVED_URI)
.setExpectedBytes(simpleData) .setExpectedBytes(simpleData)
.build()); .build());
} }

View File

@ -540,7 +540,7 @@ public abstract class DataSourceContractTest {
} }
@Test @Test
public void getUri_returnsNonNullValueOnlyWhileOpen() throws Exception { public void getUri_returnsExpectedValueOnlyWhileOpen() throws Exception {
forAllTestResourcesAndDataSources( forAllTestResourcesAndDataSources(
(resource, dataSource) -> { (resource, dataSource) -> {
try { try {
@ -548,7 +548,7 @@ public abstract class DataSourceContractTest {
dataSource.open(new DataSpec(resource.getUri())); dataSource.open(new DataSpec(resource.getUri()));
assertThat(dataSource.getUri()).isNotNull(); assertThat(dataSource.getUri()).isEqualTo(resource.getResolvedUri());
} finally { } finally {
dataSource.close(); dataSource.close();
} }
@ -740,11 +740,13 @@ public abstract class DataSourceContractTest {
@Nullable private final String name; @Nullable private final String name;
private final Uri uri; private final Uri uri;
private final Uri resolvedUri;
private final byte[] expectedBytes; private final byte[] expectedBytes;
private TestResource(@Nullable String name, Uri uri, byte[] expectedBytes) { private TestResource(@Nullable String name, Uri uri, Uri resolvedUri, byte[] expectedBytes) {
this.name = name; this.name = name;
this.uri = uri; this.uri = uri;
this.resolvedUri = resolvedUri;
this.expectedBytes = expectedBytes; this.expectedBytes = expectedBytes;
} }
@ -754,11 +756,19 @@ public abstract class DataSourceContractTest {
return name; return name;
} }
/** Returns the URI where the resource is available. */ /** Returns the URI where the resource should be requested from. */
public Uri getUri() { public Uri getUri() {
return uri; return uri;
} }
/**
* Returns the URI where the resource is served from. This is equal to {@link #getUri()} unless
* redirection occurred when opening the resource.
*/
public Uri getResolvedUri() {
return resolvedUri;
}
/** Returns the expected contents of this resource. */ /** Returns the expected contents of this resource. */
public byte[] getExpectedBytes() { public byte[] getExpectedBytes() {
return expectedBytes; return expectedBytes;
@ -768,6 +778,7 @@ public abstract class DataSourceContractTest {
public static final class Builder { public static final class Builder {
private @MonotonicNonNull String name; private @MonotonicNonNull String name;
private @MonotonicNonNull Uri uri; private @MonotonicNonNull Uri uri;
private @MonotonicNonNull Uri resolvedUri;
private byte @MonotonicNonNull [] expectedBytes; private byte @MonotonicNonNull [] expectedBytes;
/** /**
@ -779,19 +790,38 @@ public abstract class DataSourceContractTest {
return this; return this;
} }
/** Sets the URI where this resource is located. */ /** Sets the URI where this resource should be requested from. */
@CanIgnoreReturnValue @CanIgnoreReturnValue
public Builder setUri(String uri) { public Builder setUri(String uri) {
return setUri(Uri.parse(uri)); return setUri(Uri.parse(uri));
} }
/** Sets the URI where this resource is located. */ /** Sets the URI where this resource should be requested from. */
@CanIgnoreReturnValue @CanIgnoreReturnValue
public Builder setUri(Uri uri) { public Builder setUri(Uri uri) {
this.uri = uri; this.uri = uri;
return this; return this;
} }
/**
* Sets the URI where this resource is served from. This only needs to be explicitly set if
* it's different to {@link #setUri(Uri)}. See {@link #getResolvedUri()}.
*/
@CanIgnoreReturnValue
public Builder setResolvedUri(String uri) {
return setResolvedUri(Uri.parse(uri));
}
/**
* Sets the URI where this resource is served from. This only needs to be explicitly set if
* it's different to {@link #setUri(Uri)}. See {@link #getResolvedUri()}.
*/
@CanIgnoreReturnValue
public Builder setResolvedUri(Uri uri) {
this.resolvedUri = uri;
return this;
}
/** /**
* Sets the expected contents of this resource. * Sets the expected contents of this resource.
* *
@ -805,7 +835,11 @@ public abstract class DataSourceContractTest {
} }
public TestResource build() { public TestResource build() {
return new TestResource(name, checkNotNull(uri), checkNotNull(expectedBytes)); return new TestResource(
name,
checkNotNull(uri),
resolvedUri != null ? resolvedUri : uri,
checkNotNull(expectedBytes));
} }
} }
} }

View File

@ -93,8 +93,13 @@ public class HttpDataSourceTestEnv extends ExternalResource {
"range not supported, length unknown", RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN), "range not supported, length unknown", RANGE_NOT_SUPPORTED_LENGTH_UNKNOWN),
createTestResource("gzip enabled", GZIP_ENABLED), createTestResource("gzip enabled", GZIP_ENABLED),
createTestResource("gzip forced", GZIP_FORCED), createTestResource("gzip forced", GZIP_FORCED),
createTestResource( new DataSourceContractTest.TestResource.Builder()
"302 redirect", REDIRECTS_TO_RANGE_SUPPORTED, /* server= */ redirectionServer)); .setName("302 redirect")
.setUri(
Uri.parse(redirectionServer.url(REDIRECTS_TO_RANGE_SUPPORTED.getPath()).toString()))
.setResolvedUri(originServer.url(RANGE_SUPPORTED.getPath()).toString())
.setExpectedBytes(REDIRECTS_TO_RANGE_SUPPORTED.getData())
.build());
} }
public String getNonexistentUrl() { public String getNonexistentUrl() {
@ -142,14 +147,9 @@ public class HttpDataSourceTestEnv extends ExternalResource {
private DataSourceContractTest.TestResource createTestResource( private DataSourceContractTest.TestResource createTestResource(
String name, WebServerDispatcher.Resource resource) { String name, WebServerDispatcher.Resource resource) {
return createTestResource(name, resource, originServer);
}
private static DataSourceContractTest.TestResource createTestResource(
String name, WebServerDispatcher.Resource resource, MockWebServer server) {
return new DataSourceContractTest.TestResource.Builder() return new DataSourceContractTest.TestResource.Builder()
.setName(name) .setName(name)
.setUri(Uri.parse(server.url(resource.getPath()).toString())) .setUri(Uri.parse(originServer.url(resource.getPath()).toString()))
.setExpectedBytes(resource.getData()) .setExpectedBytes(resource.getData())
.build(); .build();
} }