/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.android.exoplayer2.upstream;

import android.net.Uri;
import androidx.annotation.Nullable;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.testutil.DataSourceContractTest;
import com.google.android.exoplayer2.testutil.TestUtil;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;

/** {@link DataSource} contract tests for {@link CacheDataSource}. */
@RunWith(AndroidJUnit4.class)
public class CacheDataSourceContractTest extends DataSourceContractTest {
  private static final byte[] DATA = TestUtil.buildTestData(20);

  @Rule public final TemporaryFolder tempFolder = new TemporaryFolder();

  private Uri simpleUri;
  private FileDataSource fileDataSource;

  @Before
  public void setUp() throws IOException {
    File file = tempFolder.newFile();
    Files.write(Paths.get(file.getAbsolutePath()), DATA);
    simpleUri = Uri.fromFile(file);
  }

  @Override
  protected ImmutableList<TestResource> getTestResources() {
    return ImmutableList.of(
        new TestResource.Builder()
            .setName("simple")
            .setUri(simpleUri)
            .setExpectedBytes(DATA)
            .build());
  }

  @Override
  protected Uri getNotFoundUri() {
    return Uri.fromFile(tempFolder.getRoot().toPath().resolve("nonexistent").toFile());
  }

  @Override
  protected DataSource createDataSource() throws IOException {
    File tempFolder =
        Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
    SimpleCache cache =
        new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    fileDataSource = new FileDataSource();
    return new CacheDataSource(cache, fileDataSource);
  }

  @Override
  @Nullable
  protected DataSource getTransferListenerDataSource() {
    return fileDataSource;
  }
}

@@ -894,24 +894,145 @@
 ntractTest;%0A
+import com.google.android.exoplayer2.testutil.FakeDataSet;%0Aimport com.google.android.exoplayer2.testutil.FakeDataSource;%0A
 import com.g
@@ -1435,143 +1435,24 @@
 ort 
-java.nio.file.Files;%0Aimport java.nio.file.Paths;%0Aimport org.junit.Before;%0Aimport org.junit.Rule;%0Aimport org.junit.rules.TemporaryFolder
+org.junit.Before
 ;%0Aim
@@ -1657,16 +1657,17 @@
 tTest %7B%0A
+%0A
   privat
@@ -1672,138 +1672,120 @@
 ate 
-static final byte%5B%5D DATA = TestUtil.buildTestData(20)
+Uri simpleUri;%0A  private Uri unknownLengthUri;%0A  private byte%5B%5D simpleData
 ;%0A
-%0A
   
-@Rule public final TemporaryFolder tempFolder = new TemporaryFolder()
+private byte%5B%5D unknownLengthData
 ;%0A
-%0A
   pr
@@ -1786,38 +1786,49 @@
 ;%0A  private 
-Uri simpleUri;
+FakeDataSet fakeDataSet;%0A
 %0A  private F
@@ -1827,18 +1827,18 @@
 rivate F
-il
+ak
 eDataSou
@@ -1841,20 +1841,24 @@
 aSource 
-file
+upstream
 DataSour
@@ -1923,133 +1923,532 @@
     
-File file = tempFolder.newFile();%0A    Files.write(Paths.get(file.getAbsolutePath()), DATA);%0A    simpleUri = Uri.fromFile(file
+simpleUri = Uri.parse(%22test://simple.test%22);%0A    unknownLengthUri = Uri.parse(%22test://unknown-length.test%22);%0A    simpleData = TestUtil.buildTestData(/* length= */ 20);%0A    unknownLengthData = TestUtil.buildTestData(/* length= */ 40);%0A    fakeDataSet =%0A        new FakeDataSet()%0A            .newData(simpleUri)%0A            .appendReadData(simpleData)%0A            .endData()%0A            .newData(unknownLengthUri)%0A            .setSimulateUnknownLength(true)%0A            .appendReadData(unknownLengthData)%0A            .endData(
 );%0A 
@@ -2684,12 +2684,201 @@
 tes(
-DATA
+simpleData)%0A            .build(),%0A        new TestResource.Builder()%0A            .setName(%22unknown length%22)%0A            .setUri(unknownLengthUri)%0A            .setExpectedBytes(unknownLengthData
 )%0A  
@@ -2969,78 +2969,37 @@
 Uri.
-fromFile(tempFolder.getRoot().toPath().resolve(%22nonexistent%22).toFile()
+parse(%22test://not-found.test%22
 );%0A 
@@ -3324,20 +3324,24 @@
 ));%0A    
-file
+upstream
 DataSour
@@ -3350,18 +3350,18 @@
  = new F
-il
+ak
 eDataSou
@@ -3364,16 +3364,27 @@
 aSource(
+fakeDataSet
 );%0A    r
@@ -3416,20 +3416,24 @@
 (cache, 
-file
+upstream
 DataSour
@@ -3534,20 +3534,24 @@
  return 
-file
+upstream
 DataSour

Make CacheDataSourceContractTest use FakeDataSource for upstream