Commit 30beb9b3 by olly Committed by Oliver Woodman

Add DownloadAction merge tests

PiperOrigin-RevId: 242851294
parent 8688bd2d
......@@ -29,7 +29,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/** Contains the necessary parameters for a download action. */
/** Defines content to be downloaded. */
public final class DownloadAction implements Parcelable {
/** Thrown when the encoded action data belongs to an unsupported DownloadAction type. */
......@@ -108,14 +108,20 @@ public final class DownloadAction implements Parcelable {
}
/**
* Returns the result of merging {@code newAction} into this action.
* Returns the result of merging {@code newAction} into this action. The actions must have the
* same {@link #id} and {@link #type}.
*
* @param newAction The new action.
* <p>If the actions have different {@link #uri}, {@link #customCacheKey} and {@link #data}
* values, then those from the action being merged are included in the result.
*
* @param newAction The action being merged.
* @return The merged result.
* @throws IllegalArgumentException If the actions do not have the same {@link #id} and {@link
* #type}.
*/
public DownloadAction copyWithMergedAction(DownloadAction newAction) {
Assertions.checkState(id.equals(newAction.id));
Assertions.checkState(type.equals(newAction.type));
Assertions.checkArgument(id.equals(newAction.id));
Assertions.checkArgument(type.equals(newAction.type));
List<StreamKey> mergedKeys;
if (streamKeys.isEmpty() || newAction.streamKeys.isEmpty()) {
// If either streamKeys is empty then all streams should be downloaded.
......
......@@ -16,7 +16,9 @@
package com.google.android.exoplayer2.offline;
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_DASH;
import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_HLS;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import android.net.Uri;
import android.os.Parcel;
......@@ -42,6 +44,128 @@ public class DownloadActionTest {
}
@Test
public void testMergeActions_withDifferentIds_fails() {
DownloadAction action1 =
new DownloadAction(
"id1",
TYPE_DASH,
uri1,
/* streamKeys= */ Collections.emptyList(),
/* customCacheKey= */ null,
/* data= */ null);
DownloadAction action2 =
new DownloadAction(
"id2",
TYPE_DASH,
uri2,
/* streamKeys= */ Collections.emptyList(),
/* customCacheKey= */ null,
/* data= */ null);
try {
action1.copyWithMergedAction(action2);
fail();
} catch (IllegalArgumentException e) {
// Expected.
}
}
@Test
public void testMergeActions_withDifferentTypes_fails() {
DownloadAction action1 =
new DownloadAction(
"id1",
TYPE_DASH,
uri1,
/* streamKeys= */ Collections.emptyList(),
/* customCacheKey= */ null,
/* data= */ null);
DownloadAction action2 =
new DownloadAction(
"id1",
TYPE_HLS,
uri1,
/* streamKeys= */ Collections.emptyList(),
/* customCacheKey= */ null,
/* data= */ null);
try {
action1.copyWithMergedAction(action2);
fail();
} catch (IllegalArgumentException e) {
// Expected.
}
}
@Test
public void testMergeAction_withSameAction() {
DownloadAction action1 = createAction(uri1, new StreamKey(0, 0, 0));
DownloadAction mergedAction = action1.copyWithMergedAction(action1);
assertEqual(action1, mergedAction);
}
@Test
public void testMergeActions_withEmptyStreamKeys() {
DownloadAction action1 = createAction(uri1, new StreamKey(0, 0, 0));
DownloadAction action2 = createAction(uri1);
// If either of the actions have empty streamKeys, the merge should have empty streamKeys.
DownloadAction mergedAction = action1.copyWithMergedAction(action2);
assertThat(mergedAction.streamKeys).isEmpty();
mergedAction = action2.copyWithMergedAction(action1);
assertThat(mergedAction.streamKeys).isEmpty();
}
@Test
public void testMergeActions_withOverlappingStreamKeys() {
StreamKey streamKey1 = new StreamKey(0, 1, 2);
StreamKey streamKey2 = new StreamKey(3, 4, 5);
StreamKey streamKey3 = new StreamKey(6, 7, 8);
DownloadAction action1 = createAction(uri1, streamKey1, streamKey2);
DownloadAction action2 = createAction(uri1, streamKey2, streamKey3);
// Merged streamKeys should be in their original order without duplicates.
DownloadAction mergedAction = action1.copyWithMergedAction(action2);
assertThat(mergedAction.streamKeys).containsExactly(streamKey1, streamKey2, streamKey3);
mergedAction = action2.copyWithMergedAction(action1);
assertThat(mergedAction.streamKeys).containsExactly(streamKey2, streamKey3, streamKey1);
}
@Test
public void testMergeActions_withDifferentFields() {
byte[] data1 = new byte[] {0, 1, 2};
byte[] data2 = new byte[] {3, 4, 5};
DownloadAction action1 =
new DownloadAction(
"id1",
TYPE_DASH,
uri1,
/* streamKeys= */ Collections.emptyList(),
"key1",
/* data= */ data1);
DownloadAction action2 =
new DownloadAction(
"id1",
TYPE_DASH,
uri2,
/* streamKeys= */ Collections.emptyList(),
"key2",
/* data= */ data2);
// uri, customCacheKey and data should be from the action being merged.
DownloadAction mergedAction = action1.copyWithMergedAction(action2);
assertThat(mergedAction.uri).isEqualTo(uri2);
assertThat(mergedAction.customCacheKey).isEqualTo("key2");
assertThat(mergedAction.data).isEqualTo(data2);
mergedAction = action2.copyWithMergedAction(action1);
assertThat(mergedAction.uri).isEqualTo(uri1);
assertThat(mergedAction.customCacheKey).isEqualTo("key1");
assertThat(mergedAction.data).isEqualTo(data1);
}
@Test
public void testParcelable() {
ArrayList<StreamKey> streamKeys = new ArrayList<>();
streamKeys.add(new StreamKey(1, 2, 3));
......@@ -74,29 +198,29 @@ public class DownloadActionTest {
DownloadAction action3 = createAction(uri1);
assertEqual(action2, action3);
DownloadAction action6 = createAction(uri1);
DownloadAction action4 = createAction(uri1);
DownloadAction action5 = createAction(uri1, new StreamKey(0, 0, 0));
assertNotEqual(action4, action5);
DownloadAction action6 = createAction(uri1, new StreamKey(0, 1, 1));
DownloadAction action7 = createAction(uri1, new StreamKey(0, 0, 0));
assertNotEqual(action6, action7);
DownloadAction action8 = createAction(uri1, new StreamKey(0, 1, 1));
DownloadAction action9 = createAction(uri1, new StreamKey(0, 0, 0));
DownloadAction action8 = createAction(uri1);
DownloadAction action9 = createAction(uri2);
assertNotEqual(action8, action9);
DownloadAction action10 = createAction(uri1);
DownloadAction action11 = createAction(uri2);
assertNotEqual(action10, action11);
DownloadAction action10 = createAction(uri1, new StreamKey(0, 0, 0), new StreamKey(0, 1, 1));
DownloadAction action11 = createAction(uri1, new StreamKey(0, 1, 1), new StreamKey(0, 0, 0));
assertEqual(action10, action11);
DownloadAction action12 = createAction(uri1, new StreamKey(0, 0, 0), new StreamKey(0, 1, 1));
DownloadAction action12 = createAction(uri1, new StreamKey(0, 0, 0));
DownloadAction action13 = createAction(uri1, new StreamKey(0, 1, 1), new StreamKey(0, 0, 0));
assertEqual(action12, action13);
DownloadAction action14 = createAction(uri1, new StreamKey(0, 0, 0));
DownloadAction action15 = createAction(uri1, new StreamKey(0, 1, 1), new StreamKey(0, 0, 0));
assertNotEqual(action14, action15);
assertNotEqual(action12, action13);
DownloadAction action16 = createAction(uri1);
DownloadAction action17 = createAction(uri1);
assertEqual(action16, action17);
DownloadAction action14 = createAction(uri1);
DownloadAction action15 = createAction(uri1);
assertEqual(action14, action15);
}
private static void assertNotEqual(DownloadAction action1, DownloadAction action2) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment