mirror of
https://github.com/androidx/media.git
synced 2025-05-15 19:49:50 +08:00
Simplify DownloadActionTest legacy tests using assets
PiperOrigin-RevId: 222819933
This commit is contained in:
parent
30e753ea1d
commit
a31a3f3c73
BIN
library/core/src/test/assets/download-actions/dash-download-v0
Normal file
BIN
library/core/src/test/assets/download-actions/dash-download-v0
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/dash-remove-v0
Normal file
BIN
library/core/src/test/assets/download-actions/dash-remove-v0
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/hls-download-v0
Normal file
BIN
library/core/src/test/assets/download-actions/hls-download-v0
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/hls-download-v1
Normal file
BIN
library/core/src/test/assets/download-actions/hls-download-v1
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/hls-remove-v0
Normal file
BIN
library/core/src/test/assets/download-actions/hls-remove-v0
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/hls-remove-v1
Normal file
BIN
library/core/src/test/assets/download-actions/hls-remove-v1
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/ss-download-v0
Normal file
BIN
library/core/src/test/assets/download-actions/ss-download-v0
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/ss-download-v1
Normal file
BIN
library/core/src/test/assets/download-actions/ss-download-v1
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/ss-remove-v0
Normal file
BIN
library/core/src/test/assets/download-actions/ss-remove-v0
Normal file
Binary file not shown.
BIN
library/core/src/test/assets/download-actions/ss-remove-v1
Normal file
BIN
library/core/src/test/assets/download-actions/ss-remove-v1
Normal file
Binary file not shown.
@ -19,17 +19,16 @@ import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_DASH;
|
|||||||
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_HLS;
|
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_HLS;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_PROGRESSIVE;
|
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_PROGRESSIVE;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_SS;
|
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_SS;
|
||||||
import static com.google.android.exoplayer2.offline.DownloadAction.fromByteArray;
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import com.google.android.exoplayer2.testutil.TestUtil;
|
import com.google.android.exoplayer2.testutil.TestUtil;
|
||||||
import com.google.android.exoplayer2.util.Assertions;
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -37,6 +36,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
/** Unit tests for {@link DownloadAction}. */
|
/** Unit tests for {@link DownloadAction}. */
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@ -143,7 +143,7 @@ public class DownloadActionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testStreamSerialization() throws Exception {
|
public void testSerializerWriteRead() throws Exception {
|
||||||
assertStreamSerializationRoundTrip(
|
assertStreamSerializationRoundTrip(
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_DASH,
|
TYPE_DASH,
|
||||||
@ -170,88 +170,88 @@ public class DownloadActionTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerProgressiveVersion0() throws Exception {
|
public void testSerializerProgressiveVersion0() throws Exception {
|
||||||
assertLegacySerializationRoundTrip(
|
assertDeserialization(
|
||||||
|
"progressive-download-v0",
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data),
|
TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data));
|
||||||
/* version= */ 0);
|
assertDeserialization(
|
||||||
assertLegacySerializationRoundTrip(
|
"progressive-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123", data),
|
DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123", data));
|
||||||
/* version= */ 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerDashVersion0() throws Exception {
|
public void testSerializerDashVersion0() throws Exception {
|
||||||
assertLegacySerializationRoundTrip(
|
assertDeserialization(
|
||||||
|
"dash-download-v0",
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_DASH,
|
TYPE_DASH,
|
||||||
uri1,
|
uri1,
|
||||||
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
||||||
/* customCacheKey= */ null,
|
/* customCacheKey= */ null,
|
||||||
data),
|
data));
|
||||||
/* version= */ 0);
|
assertDeserialization(
|
||||||
assertLegacySerializationRoundTrip(
|
"dash-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null, data),
|
DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null, data));
|
||||||
/* version= */ 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerHlsVersion0() throws Exception {
|
public void testSerializerHlsVersion0() throws Exception {
|
||||||
assertLegacySerializationRoundTrip(
|
assertDeserialization(
|
||||||
|
"hls-download-v0",
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_HLS,
|
TYPE_HLS,
|
||||||
uri1,
|
uri1,
|
||||||
toList(new StreamKey(0, 1), new StreamKey(2, 3)),
|
toList(new StreamKey(0, 1), new StreamKey(2, 3)),
|
||||||
/* customCacheKey= */ null,
|
/* customCacheKey= */ null,
|
||||||
data),
|
data));
|
||||||
/* version= */ 0);
|
assertDeserialization(
|
||||||
assertLegacySerializationRoundTrip(
|
"hls-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data),
|
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data));
|
||||||
/* version= */ 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerHlsVersion1() throws Exception {
|
public void testSerializerHlsVersion1() throws Exception {
|
||||||
assertLegacySerializationRoundTrip(
|
assertDeserialization(
|
||||||
|
"hls-download-v1",
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_HLS,
|
TYPE_HLS,
|
||||||
uri1,
|
uri1,
|
||||||
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
||||||
/* customCacheKey= */ null,
|
/* customCacheKey= */ null,
|
||||||
data),
|
data));
|
||||||
/* version= */ 1);
|
assertDeserialization(
|
||||||
assertLegacySerializationRoundTrip(
|
"hls-remove-v1",
|
||||||
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data),
|
DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data));
|
||||||
/* version= */ 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerSsVersion0() throws Exception {
|
public void testSerializerSsVersion0() throws Exception {
|
||||||
assertLegacySerializationRoundTrip(
|
assertDeserialization(
|
||||||
|
"ss-download-v0",
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_SS,
|
TYPE_SS,
|
||||||
uri1,
|
uri1,
|
||||||
toList(new StreamKey(0, 1), new StreamKey(2, 3)),
|
toList(new StreamKey(0, 1), new StreamKey(2, 3)),
|
||||||
/* customCacheKey= */ null,
|
/* customCacheKey= */ null,
|
||||||
data),
|
data));
|
||||||
/* version= */ 0);
|
assertDeserialization(
|
||||||
assertLegacySerializationRoundTrip(
|
"ss-remove-v0",
|
||||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data),
|
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data));
|
||||||
/* version= */ 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSerializerSsVersion1() throws Exception {
|
public void testSerializerSsVersion1() throws Exception {
|
||||||
assertLegacySerializationRoundTrip(
|
assertDeserialization(
|
||||||
|
"ss-download-v1",
|
||||||
DownloadAction.createDownloadAction(
|
DownloadAction.createDownloadAction(
|
||||||
TYPE_SS,
|
TYPE_SS,
|
||||||
uri1,
|
uri1,
|
||||||
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)),
|
||||||
/* customCacheKey= */ null,
|
/* customCacheKey= */ null,
|
||||||
data),
|
data));
|
||||||
/* version= */ 1);
|
assertDeserialization(
|
||||||
assertLegacySerializationRoundTrip(
|
"ss-remove-v1",
|
||||||
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data),
|
DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data));
|
||||||
/* version= */ 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private DownloadAction createDownloadAction(Uri uri, StreamKey... keys) {
|
private DownloadAction createDownloadAction(Uri uri, StreamKey... keys) {
|
||||||
@ -278,69 +278,24 @@ public class DownloadActionTest {
|
|||||||
DataOutputStream output = new DataOutputStream(out);
|
DataOutputStream output = new DataOutputStream(out);
|
||||||
action.serializeToStream(output);
|
action.serializeToStream(output);
|
||||||
|
|
||||||
assertEqual(action, deserializeActionFromStream(out));
|
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
||||||
|
DataInputStream input = new DataInputStream(in);
|
||||||
|
DownloadAction deserializedAction = DownloadAction.deserializeFromStream(input);
|
||||||
|
|
||||||
|
assertEqual(action, deserializedAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertArraySerializationRoundTrip(DownloadAction action) throws IOException {
|
private static void assertArraySerializationRoundTrip(DownloadAction action) throws IOException {
|
||||||
assertEqual(action, fromByteArray(action.toByteArray()));
|
assertEqual(action, DownloadAction.fromByteArray(action.toByteArray()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Remove this method and add assets for legacy serialized actions.
|
private static void assertDeserialization(String fileName, DownloadAction expectedAction)
|
||||||
private static void assertLegacySerializationRoundTrip(DownloadAction action, int version)
|
|
||||||
throws IOException {
|
throws IOException {
|
||||||
String type = action.type;
|
InputStream input =
|
||||||
Assertions.checkState(
|
TestUtil.getInputStream(RuntimeEnvironment.application, "download-actions/" + fileName);
|
||||||
version == 0 || ((TYPE_HLS.equals(type) || TYPE_SS.equals(type)) && version == 1));
|
DownloadAction deserializedAction = DownloadAction.deserializeFromStream(input);
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
DataOutputStream output = new DataOutputStream(out);
|
|
||||||
DataOutputStream dataOutputStream = new DataOutputStream(output);
|
|
||||||
dataOutputStream.writeUTF(type);
|
|
||||||
dataOutputStream.writeInt(version);
|
|
||||||
dataOutputStream.writeUTF(action.uri.toString());
|
|
||||||
dataOutputStream.writeBoolean(action.isRemoveAction);
|
|
||||||
dataOutputStream.writeInt(action.data.length);
|
|
||||||
dataOutputStream.write(action.data);
|
|
||||||
boolean isLegacyProgressive = version == 0 && TYPE_PROGRESSIVE.equals(type);
|
|
||||||
if (isLegacyProgressive) {
|
|
||||||
// Serialized version 0 progressive actions did not contain keys.
|
|
||||||
Assertions.checkState(action.keys.isEmpty());
|
|
||||||
} else {
|
|
||||||
dataOutputStream.writeInt(action.keys.size());
|
|
||||||
for (int i = 0; i < action.keys.size(); i++) {
|
|
||||||
StreamKey key = action.keys.get(i);
|
|
||||||
// Serialized version 0 HLS/SS actions did not contain key period indices.
|
|
||||||
boolean isLegacyHlsOrSs = version == 0 && (TYPE_HLS.equals(type) || TYPE_SS.equals(type));
|
|
||||||
if (isLegacyHlsOrSs) {
|
|
||||||
// Serialized version 0 progressive actions did not contain keys.
|
|
||||||
Assertions.checkState(key.periodIndex == 0);
|
|
||||||
} else {
|
|
||||||
dataOutputStream.writeInt(key.periodIndex);
|
|
||||||
}
|
|
||||||
dataOutputStream.writeInt(key.groupIndex);
|
|
||||||
dataOutputStream.writeInt(key.trackIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
boolean isLegacySegmented =
|
|
||||||
version < 2 && (TYPE_DASH.equals(type) || TYPE_HLS.equals(type) || TYPE_SS.equals(type));
|
|
||||||
if (isLegacySegmented) {
|
|
||||||
// Serialized version 0 and 1 DASH/HLS/SS actions did not contain a custom cache key.
|
|
||||||
Assertions.checkState(action.customCacheKey == null);
|
|
||||||
} else {
|
|
||||||
dataOutputStream.writeBoolean(action.customCacheKey != null);
|
|
||||||
if (action.customCacheKey != null) {
|
|
||||||
dataOutputStream.writeUTF(action.customCacheKey);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dataOutputStream.flush();
|
|
||||||
|
|
||||||
assertEqual(action, deserializeActionFromStream(out));
|
assertEqual(deserializedAction, expectedAction);
|
||||||
}
|
|
||||||
|
|
||||||
private static DownloadAction deserializeActionFromStream(ByteArrayOutputStream out)
|
|
||||||
throws IOException {
|
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
|
|
||||||
DataInputStream input = new DataInputStream(in);
|
|
||||||
return DownloadAction.deserializeFromStream(input);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<StreamKey> toList(StreamKey... keys) {
|
private static List<StreamKey> toList(StreamKey... keys) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user