From a31a3f3c73246b2ea49d6c2c8c0e7875459c2ba0 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 26 Nov 2018 15:13:16 +0000 Subject: [PATCH] Simplify DownloadActionTest legacy tests using assets PiperOrigin-RevId: 222819933 --- .../assets/download-actions/dash-download-v0 | Bin 0 -> 94 bytes .../assets/download-actions/dash-remove-v0 | Bin 0 -> 70 bytes .../assets/download-actions/hls-download-v0 | Bin 0 -> 85 bytes .../assets/download-actions/hls-download-v1 | Bin 0 -> 93 bytes .../assets/download-actions/hls-remove-v0 | Bin 0 -> 69 bytes .../assets/download-actions/hls-remove-v1 | Bin 0 -> 69 bytes .../download-actions/progressive-download-v0 | Bin 0 -> 82 bytes .../download-actions/progressive-remove-v0 | Bin 0 -> 82 bytes .../assets/download-actions/ss-download-v0 | Bin 0 -> 84 bytes .../assets/download-actions/ss-download-v1 | Bin 0 -> 92 bytes .../test/assets/download-actions/ss-remove-v0 | Bin 0 -> 68 bytes .../test/assets/download-actions/ss-remove-v1 | Bin 0 -> 68 bytes .../offline/DownloadActionTest.java | 145 ++++++------------ 13 files changed, 50 insertions(+), 95 deletions(-) create mode 100644 library/core/src/test/assets/download-actions/dash-download-v0 create mode 100644 library/core/src/test/assets/download-actions/dash-remove-v0 create mode 100644 library/core/src/test/assets/download-actions/hls-download-v0 create mode 100644 library/core/src/test/assets/download-actions/hls-download-v1 create mode 100644 library/core/src/test/assets/download-actions/hls-remove-v0 create mode 100644 library/core/src/test/assets/download-actions/hls-remove-v1 create mode 100644 library/core/src/test/assets/download-actions/progressive-download-v0 create mode 100644 library/core/src/test/assets/download-actions/progressive-remove-v0 create mode 100644 library/core/src/test/assets/download-actions/ss-download-v0 create mode 100644 library/core/src/test/assets/download-actions/ss-download-v1 create mode 100644 library/core/src/test/assets/download-actions/ss-remove-v0 create mode 100644 library/core/src/test/assets/download-actions/ss-remove-v1 diff --git a/library/core/src/test/assets/download-actions/dash-download-v0 b/library/core/src/test/assets/download-actions/dash-download-v0 new file mode 100644 index 0000000000000000000000000000000000000000..980f0000b7ab3253f9fd35d8e6c06e86f2b54383 GIT binary patch literal 94 zcmZQzNl7fuU;qKZjFOT9D}DWv)Z!9-L%q_XOpvI;;cF?o%#R)_P|~VdWz>DjcApmC iV^((aDFxFar}{fQC;_Tr0x1LnMj!^UnSq!Eh*<&lRu+E% literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/dash-remove-v0 b/library/core/src/test/assets/download-actions/dash-remove-v0 new file mode 100644 index 0000000000000000000000000000000000000000..03e37b1e0de90a8a4a8569888240b8fdea654e2e GIT binary patch literal 70 zcmZQzNl7fuU;qKZjFOT9D}DWv)Z!9-L%q_XOh%xn!r^NvyUdRsDp1m@S!L9H%66X? V-(yyG^C<<>BB%O0JSYKa1pwQC7HkkDX`MlFG(#f(Kpm9Ey@H6GbkLsma@zI=%E57t(sLv-KT8# lY4JT~WjCKvFfDSbzr%wPpc*ESLLguSVi21dh*^M`6#&*W7A61y literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/hls-remove-v0 b/library/core/src/test/assets/download-actions/hls-remove-v0 new file mode 100644 index 0000000000000000000000000000000000000000..163dfed1bfb03d30af96afaeda1bb0d1a7568199 GIT binary patch literal 69 zcmZQz&d4cd00F^_l9B=|ef^Tu;u3vBz0#sgMxdy|;cF?o%#R)_P|~VdWz>DjcApmC UV^((aDFxFar}{fQC;@2&0K<6}6#xJL literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/hls-remove-v1 b/library/core/src/test/assets/download-actions/hls-remove-v1 new file mode 100644 index 0000000000000000000000000000000000000000..35fbd3271867515e32712899cbd8a38134de785e GIT binary patch literal 69 zcmZQz&d4cdU|?Wm5X>kkDX`MlFG(#f(Kpm9Ey`pBiYpwxma@zI=%E57t(sLv-KT8# XY4JT~WjCKvFfDSbzr%wPpc)1M!?qR{ literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/progressive-download-v0 b/library/core/src/test/assets/download-actions/progressive-download-v0 new file mode 100644 index 0000000000000000000000000000000000000000..618419e1834c57409b857dd7d7b4084e3d1d41d9 GIT binary patch literal 82 zcmZSJE-1=RFG?*g&MZr100F^_l9B=|ef^Tu;u3vBz0#sgkf_4pYbm?Tj~*&e(yCcy i)P2f!pBCR^R(A6#1=Av@`a3)*VPs&-POUUFG6n!c;~R7U literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/progressive-remove-v0 b/library/core/src/test/assets/download-actions/progressive-remove-v0 new file mode 100644 index 0000000000000000000000000000000000000000..7a522b85f4cb345ec110dc64f876670a1e54a6a1 GIT binary patch literal 82 zcmZSJE-1=RFG?*g&MZr100F^_l9B=|ef^Tu;u3vBz0#sgMxdy|;cF?o%#R)_P|~Vd jWz>DjcApmCV^((aDFxFar}{fQC}Ct^%TBE{G%^MNL=hWx literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/ss-download-v0 b/library/core/src/test/assets/download-actions/ss-download-v0 new file mode 100644 index 0000000000000000000000000000000000000000..da0d2f6be56ae2e8e6ffa2f09829f20e0049388f GIT binary patch literal 84 zcmZQzDlTRK0l|!tk^(Dz{gTw;5`9Cx(xOa|sKViEDZ9*%9x71Ms##^!ead#97T;r5 bcJnC((;}z(J3J@>s$l{t1Oi4N2C*|)vdjGFp#mkXnpH;Kr)>9W k@jYf`H=j~4Epn>A!-Eo_8YYlJAYcSy5Stl@S%8=o0JBpS!~g&Q literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/ss-remove-v0 b/library/core/src/test/assets/download-actions/ss-remove-v0 new file mode 100644 index 0000000000000000000000000000000000000000..a3811cd4ce0b0130f6d2c40a69ef02a503ea24be GIT binary patch literal 68 zcmZQzDlTRK0l|!tk^(Dz{gTw;5`9Cx(xOa8ps2#(Ybm?Tj~*&e(yCcy)P2f!pBCR^ TR(A6#1=Av@`a3)*0ciyQsmK+x literal 0 HcmV?d00001 diff --git a/library/core/src/test/assets/download-actions/ss-remove-v1 b/library/core/src/test/assets/download-actions/ss-remove-v1 new file mode 100644 index 0000000000000000000000000000000000000000..fd3c8c194c95464e77923993a8c2ffdc60eea99e GIT binary patch literal 68 zcmZQzDlTSVU|?hr%qS@-u+rBrNi8nXH`FUF%47tJD;&O-vdjGFp#mkXnpH;Kr)>9W W@jYf`H=j~4Epn>A!-Eo_8U_HW1r@Xa literal 0 HcmV?d00001 diff --git a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java index dc34d7e1a3..01976583bc 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadActionTest.java @@ -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_PROGRESSIVE; 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 android.net.Uri; import com.google.android.exoplayer2.testutil.TestUtil; -import com.google.android.exoplayer2.util.Assertions; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -37,6 +36,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; /** Unit tests for {@link DownloadAction}. */ @RunWith(RobolectricTestRunner.class) @@ -143,7 +143,7 @@ public class DownloadActionTest { } @Test - public void testStreamSerialization() throws Exception { + public void testSerializerWriteRead() throws Exception { assertStreamSerializationRoundTrip( DownloadAction.createDownloadAction( TYPE_DASH, @@ -170,88 +170,88 @@ public class DownloadActionTest { @Test public void testSerializerProgressiveVersion0() throws Exception { - assertLegacySerializationRoundTrip( + assertDeserialization( + "progressive-download-v0", DownloadAction.createDownloadAction( - TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data), - /* version= */ 0); - assertLegacySerializationRoundTrip( - DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123", data), - /* version= */ 0); + TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key123", data)); + assertDeserialization( + "progressive-remove-v0", + DownloadAction.createRemoveAction(TYPE_PROGRESSIVE, uri1, "key123", data)); } @Test public void testSerializerDashVersion0() throws Exception { - assertLegacySerializationRoundTrip( + assertDeserialization( + "dash-download-v0", DownloadAction.createDownloadAction( TYPE_DASH, uri1, toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), /* customCacheKey= */ null, - data), - /* version= */ 0); - assertLegacySerializationRoundTrip( - DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null, data), - /* version= */ 0); + data)); + assertDeserialization( + "dash-remove-v0", + DownloadAction.createRemoveAction(TYPE_DASH, uri1, /* customCacheKey= */ null, data)); } @Test public void testSerializerHlsVersion0() throws Exception { - assertLegacySerializationRoundTrip( + assertDeserialization( + "hls-download-v0", DownloadAction.createDownloadAction( TYPE_HLS, uri1, toList(new StreamKey(0, 1), new StreamKey(2, 3)), /* customCacheKey= */ null, - data), - /* version= */ 0); - assertLegacySerializationRoundTrip( - DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data), - /* version= */ 0); + data)); + assertDeserialization( + "hls-remove-v0", + DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data)); } @Test public void testSerializerHlsVersion1() throws Exception { - assertLegacySerializationRoundTrip( + assertDeserialization( + "hls-download-v1", DownloadAction.createDownloadAction( TYPE_HLS, uri1, toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), /* customCacheKey= */ null, - data), - /* version= */ 1); - assertLegacySerializationRoundTrip( - DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data), - /* version= */ 1); + data)); + assertDeserialization( + "hls-remove-v1", + DownloadAction.createRemoveAction(TYPE_HLS, uri1, /* customCacheKey= */ null, data)); } @Test public void testSerializerSsVersion0() throws Exception { - assertLegacySerializationRoundTrip( + assertDeserialization( + "ss-download-v0", DownloadAction.createDownloadAction( TYPE_SS, uri1, toList(new StreamKey(0, 1), new StreamKey(2, 3)), /* customCacheKey= */ null, - data), - /* version= */ 0); - assertLegacySerializationRoundTrip( - DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data), - /* version= */ 0); + data)); + assertDeserialization( + "ss-remove-v0", + DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data)); } @Test public void testSerializerSsVersion1() throws Exception { - assertLegacySerializationRoundTrip( + assertDeserialization( + "ss-download-v1", DownloadAction.createDownloadAction( TYPE_SS, uri1, toList(new StreamKey(0, 1, 2), new StreamKey(3, 4, 5)), /* customCacheKey= */ null, - data), - /* version= */ 1); - assertLegacySerializationRoundTrip( - DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data), - /* version= */ 1); + data)); + assertDeserialization( + "ss-remove-v1", + DownloadAction.createRemoveAction(TYPE_SS, uri1, /* customCacheKey= */ null, data)); } private DownloadAction createDownloadAction(Uri uri, StreamKey... keys) { @@ -278,69 +278,24 @@ public class DownloadActionTest { DataOutputStream output = new DataOutputStream(out); 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 { - 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 assertLegacySerializationRoundTrip(DownloadAction action, int version) + private static void assertDeserialization(String fileName, DownloadAction expectedAction) throws IOException { - String type = action.type; - Assertions.checkState( - version == 0 || ((TYPE_HLS.equals(type) || TYPE_SS.equals(type)) && version == 1)); - 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(); + InputStream input = + TestUtil.getInputStream(RuntimeEnvironment.application, "download-actions/" + fileName); + DownloadAction deserializedAction = DownloadAction.deserializeFromStream(input); - assertEqual(action, deserializeActionFromStream(out)); - } - - private static DownloadAction deserializeActionFromStream(ByteArrayOutputStream out) - throws IOException { - ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); - DataInputStream input = new DataInputStream(in); - return DownloadAction.deserializeFromStream(input); + assertEqual(deserializedAction, expectedAction); } private static List toList(StreamKey... keys) {