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:
* Transformer:
* 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:
* Video:
* Text:

View File

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

View File

@ -540,7 +540,7 @@ public abstract class DataSourceContractTest {
}
@Test
public void getUri_returnsNonNullValueOnlyWhileOpen() throws Exception {
public void getUri_returnsExpectedValueOnlyWhileOpen() throws Exception {
forAllTestResourcesAndDataSources(
(resource, dataSource) -> {
try {
@ -548,7 +548,7 @@ public abstract class DataSourceContractTest {
dataSource.open(new DataSpec(resource.getUri()));
assertThat(dataSource.getUri()).isNotNull();
assertThat(dataSource.getUri()).isEqualTo(resource.getResolvedUri());
} finally {
dataSource.close();
}
@ -740,11 +740,13 @@ public abstract class DataSourceContractTest {
@Nullable private final String name;
private final Uri uri;
private final Uri resolvedUri;
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.uri = uri;
this.resolvedUri = resolvedUri;
this.expectedBytes = expectedBytes;
}
@ -754,11 +756,19 @@ public abstract class DataSourceContractTest {
return name;
}
/** Returns the URI where the resource is available. */
/** Returns the URI where the resource should be requested from. */
public Uri getUri() {
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. */
public byte[] getExpectedBytes() {
return expectedBytes;
@ -768,6 +778,7 @@ public abstract class DataSourceContractTest {
public static final class Builder {
private @MonotonicNonNull String name;
private @MonotonicNonNull Uri uri;
private @MonotonicNonNull Uri resolvedUri;
private byte @MonotonicNonNull [] expectedBytes;
/**
@ -779,19 +790,38 @@ public abstract class DataSourceContractTest {
return this;
}
/** Sets the URI where this resource is located. */
/** Sets the URI where this resource should be requested from. */
@CanIgnoreReturnValue
public Builder setUri(String 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
public Builder setUri(Uri uri) {
this.uri = uri;
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.
*
@ -805,7 +835,11 @@ public abstract class DataSourceContractTest {
}
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),
createTestResource("gzip enabled", GZIP_ENABLED),
createTestResource("gzip forced", GZIP_FORCED),
createTestResource(
"302 redirect", REDIRECTS_TO_RANGE_SUPPORTED, /* server= */ redirectionServer));
new DataSourceContractTest.TestResource.Builder()
.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() {
@ -142,14 +147,9 @@ public class HttpDataSourceTestEnv extends ExternalResource {
private DataSourceContractTest.TestResource createTestResource(
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()
.setName(name)
.setUri(Uri.parse(server.url(resource.getPath()).toString()))
.setUri(Uri.parse(originServer.url(resource.getPath()).toString()))
.setExpectedBytes(resource.getData())
.build();
}