Search in sources :

Example 21 with FakeDataSet

use of androidx.media3.test.utils.FakeDataSet in project media by androidx.

the class ProgressiveDownloaderTest method download_afterWriteFailureOnClose_succeeds.

@Test
public void download_afterWriteFailureOnClose_succeeds() throws Exception {
    Uri uri = Uri.parse("test:///test.mp4");
    FakeDataSet data = new FakeDataSet();
    data.newData(uri).appendReadData(1024);
    DataSource.Factory upstreamDataSource = new FakeDataSource.Factory().setFakeDataSet(data);
    AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
    true);
    FailOnCloseDataSink.Factory dataSinkFactory = new FailOnCloseDataSink.Factory(downloadCache, failOnClose);
    MediaItem mediaItem = MediaItem.fromUri(uri);
    CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(downloadCache).setCacheWriteDataSinkFactory(dataSinkFactory).setUpstreamDataSourceFactory(upstreamDataSource);
    ProgressiveDownloader downloader = new ProgressiveDownloader(mediaItem, cacheDataSourceFactory);
    TestProgressListener progressListener = new TestProgressListener();
    // Failure expected after 1024 bytes.
    assertThrows(IOException.class, () -> downloader.download(progressListener));
    assertThat(progressListener.bytesDownloaded).isEqualTo(1024);
    failOnClose.set(false);
    // Retry should succeed.
    downloader.download(progressListener);
    assertThat(progressListener.bytesDownloaded).isEqualTo(1024);
}
Also used : FakeDataSet(androidx.media3.test.utils.FakeDataSet) Uri(android.net.Uri) FailOnCloseDataSink(androidx.media3.test.utils.FailOnCloseDataSink) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) DataSource(androidx.media3.datasource.DataSource) FakeDataSource(androidx.media3.test.utils.FakeDataSource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeDataSource(androidx.media3.test.utils.FakeDataSource) MediaItem(androidx.media3.common.MediaItem) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) Test(org.junit.Test)

Example 22 with FakeDataSet

use of androidx.media3.test.utils.FakeDataSet in project media by androidx.

the class CacheWriterTest method cache_afterFailureOnClose_succeeds.

@Test
public void cache_afterFailureOnClose_succeeds() throws Exception {
    FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
    FakeDataSource upstreamDataSource = new FakeDataSource(fakeDataSet);
    AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
    true);
    FailOnCloseDataSink dataSink = new FailOnCloseDataSink(cache, failOnClose);
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstreamDataSource, new FileDataSource(), dataSink, /* flags= */
    0, /* eventListener= */
    null);
    CachingCounters counters = new CachingCounters();
    CacheWriter cacheWriter = new CacheWriter(cacheDataSource, new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
    null, counters);
    // DataSink.close failing must cause the operation to fail rather than succeed.
    assertThrows(IOException.class, cacheWriter::cache);
    // Since all of the bytes were read through the DataSource chain successfully before the sink
    // was closed, the progress listener will have seen all of the bytes being cached, even though
    // this may not really be the case.
    counters.assertValues(/* bytesAlreadyCached= */
    0, /* bytesNewlyCached= */
    100, /* contentLength= */
    100);
    failOnClose.set(false);
    // The bytes will be downloaded again, but cached successfully this time.
    cacheWriter.cache();
    counters.assertValues(/* bytesAlreadyCached= */
    0, /* bytesNewlyCached= */
    100, /* contentLength= */
    100);
    assertCachedData(cache, fakeDataSet);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeDataSource(androidx.media3.test.utils.FakeDataSource) FakeDataSet(androidx.media3.test.utils.FakeDataSet) FileDataSource(androidx.media3.datasource.FileDataSource) DataSpec(androidx.media3.datasource.DataSpec) FailOnCloseDataSink(androidx.media3.test.utils.FailOnCloseDataSink) Test(org.junit.Test)

Example 23 with FakeDataSet

use of androidx.media3.test.utils.FakeDataSet in project media by androidx.

the class CacheWriterTest method cachePolling.

@Test
public void cachePolling() throws Exception {
    final CachingCounters counters = new CachingCounters();
    FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data").appendReadData(TestUtil.buildTestData(100)).appendReadAction(() -> counters.assertValues(0, 100, 300)).appendReadData(TestUtil.buildTestData(100)).appendReadAction(() -> counters.assertValues(0, 200, 300)).appendReadData(TestUtil.buildTestData(100)).endData();
    FakeDataSource dataSource = new FakeDataSource(fakeDataSet);
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
    null, counters);
    cacheWriter.cache();
    counters.assertValues(0, 300, 300);
    assertCachedData(cache, fakeDataSet);
}
Also used : FakeDataSource(androidx.media3.test.utils.FakeDataSource) FakeDataSet(androidx.media3.test.utils.FakeDataSet) DataSpec(androidx.media3.datasource.DataSpec) Test(org.junit.Test)

Example 24 with FakeDataSet

use of androidx.media3.test.utils.FakeDataSet in project media by androidx.

the class CacheWriterTest method cacheUnknownLengthPartialCaching.

@Test
public void cacheUnknownLengthPartialCaching() throws Exception {
    FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data").setSimulateUnknownLength(true).appendReadData(TestUtil.buildTestData(100)).endData();
    FakeDataSource dataSource = new FakeDataSource(fakeDataSet);
    Uri testUri = Uri.parse("test_data");
    DataSpec dataSpec = new DataSpec(testUri, /* position= */
    10, /* length= */
    20);
    CachingCounters counters = new CachingCounters();
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), dataSpec, /* temporaryBuffer= */
    null, counters);
    cacheWriter.cache();
    counters.assertValues(0, 20, 20);
    counters.reset();
    cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), new DataSpec(testUri), /* temporaryBuffer= */
    null, counters);
    cacheWriter.cache();
    counters.assertValues(20, 80, 100);
    assertCachedData(cache, fakeDataSet);
}
Also used : FakeDataSource(androidx.media3.test.utils.FakeDataSource) FakeDataSet(androidx.media3.test.utils.FakeDataSet) DataSpec(androidx.media3.datasource.DataSpec) Uri(android.net.Uri) Test(org.junit.Test)

Example 25 with FakeDataSet

use of androidx.media3.test.utils.FakeDataSet in project media by androidx.

the class CacheDataSourceContractTest method createDataSource.

@Override
protected DataSource createDataSource() throws IOException {
    File tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
    SimpleCache cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    upstreamDataSource = new FakeDataSource(fakeDataSet);
    return new CacheDataSource(cache, upstreamDataSource);
}
Also used : FakeDataSource(androidx.media3.test.utils.FakeDataSource) File(java.io.File)

Aggregations

Test (org.junit.Test)29 FakeDataSet (androidx.media3.test.utils.FakeDataSet)23 DataSpec (androidx.media3.datasource.DataSpec)16 FakeDataSource (androidx.media3.test.utils.FakeDataSource)14 StreamKey (androidx.media3.common.StreamKey)11 Uri (android.net.Uri)6 IOException (java.io.IOException)6 RequestSet (androidx.media3.test.utils.CacheAsserts.RequestSet)5 CacheDataSource (androidx.media3.datasource.cache.CacheDataSource)4 DataSource (androidx.media3.datasource.DataSource)3 NoOpCacheEvictor (androidx.media3.datasource.cache.NoOpCacheEvictor)3 SimpleCache (androidx.media3.datasource.cache.SimpleCache)3 Before (org.junit.Before)3 MediaItem (androidx.media3.common.MediaItem)2 DefaultDownloadIndex (androidx.media3.exoplayer.offline.DefaultDownloadIndex)2 DefaultDownloaderFactory (androidx.media3.exoplayer.offline.DefaultDownloaderFactory)2 DownloadManager (androidx.media3.exoplayer.offline.DownloadManager)2 DummyMainThread (androidx.media3.test.utils.DummyMainThread)2 FailOnCloseDataSink (androidx.media3.test.utils.FailOnCloseDataSink)2 TestDownloadManagerListener (androidx.media3.test.utils.robolectric.TestDownloadManagerListener)2