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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations