DownloadRequest implements Parcelable { public DownloadRequest copyWithMergedRequest(DownloadRequest newRequest) { Assertions.checkArgument(id.equals(newRequest.id)); Assertions.checkArgument(type.equals(newRequest.type)); List<StreamKey> mergedKeys; if (streamKeys.isEmpty() || newRequest.streamKeys.isEmpty()) { mergedKeys = Collections.emptyList(); } else { mergedKeys = new ArrayList<>(streamKeys); for (int i = 0; i < newRequest.streamKeys.size(); i++) { StreamKey newKey = newRequest.streamKeys.get(i); if (!mergedKeys.contains(newKey)) { mergedKeys.add(newKey); } } } return new DownloadRequest( id, type, newRequest.uri, mergedKeys, newRequest.customCacheKey, newRequest.data); } DownloadRequest(
      String id,
      String type,
      Uri uri,
      List<StreamKey> streamKeys,
      @Nullable String customCacheKey,
      @Nullable byte[] data);   DownloadRequest(Parcel in); DownloadRequest copyWithId(String id); DownloadRequest copyWithMergedRequest(DownloadRequest newRequest); @Override String toString(); @Override boolean equals(@Nullable Object o); @Override final int hashCode(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final String TYPE_PROGRESSIVE; static final String TYPE_DASH; static final String TYPE_HLS; static final String TYPE_SS; final String id; final String type; final Uri uri; final List<StreamKey> streamKeys; @Nullable final String customCacheKey; final byte[] data; static final Parcelable.Creator<DownloadRequest> CREATOR; }
@Test public void testMergeRequests_withDifferentIds_fails() { DownloadRequest request1 = new DownloadRequest( "id1", TYPE_DASH, uri1, Collections.emptyList(), null, null); DownloadRequest request2 = new DownloadRequest( "id2", TYPE_DASH, uri2, Collections.emptyList(), null, null); try { request1.copyWithMergedRequest(request2); fail(); } catch (IllegalArgumentException e) { } }
@Test public void testMergeRequests_withDifferentTypes_fails() { DownloadRequest request1 = new DownloadRequest( "id1", TYPE_DASH, uri1, Collections.emptyList(), null, null); DownloadRequest request2 = new DownloadRequest( "id1", TYPE_HLS, uri1, Collections.emptyList(), null, null); try { request1.copyWithMergedRequest(request2); fail(); } catch (IllegalArgumentException e) { } }
@Test public void testMergeRequest_withSameRequest() { DownloadRequest request1 = createRequest(uri1, new StreamKey(0, 0, 0)); DownloadRequest mergedRequest = request1.copyWithMergedRequest(request1); assertEqual(request1, mergedRequest); }
@Test public void testMergeRequests_withEmptyStreamKeys() { DownloadRequest request1 = createRequest(uri1, new StreamKey(0, 0, 0)); DownloadRequest request2 = createRequest(uri1); DownloadRequest mergedRequest = request1.copyWithMergedRequest(request2); assertThat(mergedRequest.streamKeys).isEmpty(); mergedRequest = request2.copyWithMergedRequest(request1); assertThat(mergedRequest.streamKeys).isEmpty(); }
@Test public void testMergeRequests_withOverlappingStreamKeys() { StreamKey streamKey1 = new StreamKey(0, 1, 2); StreamKey streamKey2 = new StreamKey(3, 4, 5); StreamKey streamKey3 = new StreamKey(6, 7, 8); DownloadRequest request1 = createRequest(uri1, streamKey1, streamKey2); DownloadRequest request2 = createRequest(uri1, streamKey2, streamKey3); DownloadRequest mergedRequest = request1.copyWithMergedRequest(request2); assertThat(mergedRequest.streamKeys).containsExactly(streamKey1, streamKey2, streamKey3); mergedRequest = request2.copyWithMergedRequest(request1); assertThat(mergedRequest.streamKeys).containsExactly(streamKey2, streamKey3, streamKey1); }
@Test public void testMergeRequests_withDifferentFields() { byte[] data1 = new byte[] {0, 1, 2}; byte[] data2 = new byte[] {3, 4, 5}; DownloadRequest request1 = new DownloadRequest( "id1", TYPE_PROGRESSIVE, uri1, Collections.emptyList(), "key1", data1); DownloadRequest request2 = new DownloadRequest( "id1", TYPE_PROGRESSIVE, uri2, Collections.emptyList(), "key2", data2); DownloadRequest mergedRequest = request1.copyWithMergedRequest(request2); assertThat(mergedRequest.uri).isEqualTo(uri2); assertThat(mergedRequest.customCacheKey).isEqualTo("key2"); assertThat(mergedRequest.data).isEqualTo(data2); mergedRequest = request2.copyWithMergedRequest(request1); assertThat(mergedRequest.uri).isEqualTo(uri1); assertThat(mergedRequest.customCacheKey).isEqualTo("key1"); assertThat(mergedRequest.data).isEqualTo(data1); }