From ab95e3f38807d63774b253e7f4337dcfd71e31bb Mon Sep 17 00:00:00 2001 From: ibaker Date: Thu, 16 Jul 2020 13:45:10 +0100 Subject: [PATCH] Migrate usages of deprecated SimpleCache constructors I duplicated some methods in SimpleCacheTest to ensure we keep testing the deprecated code paths for now. PiperOrigin-RevId: 321548802 --- .../upstream/cache/CacheDataSourceTest.java | 3 +- .../upstream/cache/CacheDataSourceTest2.java | 6 +- .../upstream/cache/CacheWriterTest.java | 3 +- .../upstream/cache/SimpleCacheTest.java | 79 ++++++++++++++++--- .../dash/offline/DashDownloaderTest.java | 3 +- .../dash/offline/DownloadManagerDashTest.java | 4 +- .../dash/offline/DownloadServiceDashTest.java | 3 +- .../source/hls/offline/HlsDownloaderTest.java | 4 +- .../playbacktests/gts/DashDownloadTest.java | 5 +- 9 files changed, 93 insertions(+), 17 deletions(-) diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java index 652a5643a7..a94db25159 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest.java @@ -81,7 +81,8 @@ public final class CacheDataSourceTest { tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest"); - cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider()); upstreamDataSource = new FakeDataSource(); } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest2.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest2.java index 0a1f800983..e55d16541a 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest2.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheDataSourceTest2.java @@ -156,7 +156,11 @@ public final class CacheDataSourceTest2 { private static CacheDataSource buildCacheDataSource(Context context, DataSource upstreamSource, boolean useAesEncryption) throws CacheException { File cacheDir = context.getExternalCacheDir(); - Cache cache = new SimpleCache(new File(cacheDir, EXO_CACHE_DIR), new NoOpCacheEvictor()); + Cache cache = + new SimpleCache( + new File(cacheDir, EXO_CACHE_DIR), + new NoOpCacheEvictor(), + TestUtil.getInMemoryDatabaseProvider()); emptyCache(cache); // Source and cipher diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheWriterTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheWriterTest.java index d0cc42b062..cc688c4675 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheWriterTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheWriterTest.java @@ -96,7 +96,8 @@ public final class CacheWriterTest { mockCache.init(); tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest"); - cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider()); } @After diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java index fce14794eb..482c95bfd2 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/SimpleCacheTest.java @@ -24,6 +24,7 @@ import static org.mockito.Mockito.doAnswer; import android.net.Uri; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.google.android.exoplayer2.database.DatabaseProvider; import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.upstream.cache.Cache.CacheException; import com.google.android.exoplayer2.util.Util; @@ -38,7 +39,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; /** Unit tests for {@link SimpleCache}. */ @RunWith(AndroidJUnit4.class) @@ -50,18 +50,23 @@ public class SimpleCacheTest { private File testDir; private File cacheDir; + private DatabaseProvider databaseProvider; @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + public void createTestDir() throws Exception { testDir = Util.createTempFile(ApplicationProvider.getApplicationContext(), "SimpleCacheTest"); assertThat(testDir.delete()).isTrue(); assertThat(testDir.mkdirs()).isTrue(); cacheDir = new File(testDir, "cache"); } + @Before + public void createDatabaseProvider() { + databaseProvider = TestUtil.getInMemoryDatabaseProvider(); + } + @After - public void tearDown() { + public void deleteTestDir() { Util.recursiveDelete(testDir); } @@ -96,7 +101,33 @@ public class SimpleCacheTest { } @Test - public void newInstance_withExistingCacheDirectory_loadsCachedData() throws Exception { + @SuppressWarnings("deprecation") // Testing deprecated behaviour + public void newInstance_withExistingCacheDirectory_withoutDatabase_loadsCachedData() + throws Exception { + SimpleCache simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); + + // Write some data and metadata to the cache. + CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET); + addCache(simpleCache, KEY_1, 0, 15); + simpleCache.releaseHoleSpan(holeSpan); + ContentMetadataMutations mutations = new ContentMetadataMutations(); + ContentMetadataMutations.setRedirectedUri(mutations, Uri.parse("https://redirect.google.com")); + simpleCache.applyContentMetadataMutations(KEY_1, mutations); + simpleCache.release(); + + // Create a new instance pointing to the same directory. + simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); + + // Read the cached data and metadata back. + CacheSpan fileSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET); + assertCachedDataReadCorrect(fileSpan); + assertThat(ContentMetadata.getRedirectedUri(simpleCache.getContentMetadata(KEY_1))) + .isEqualTo(Uri.parse("https://redirect.google.com")); + } + + @Test + public void newInstance_withExistingCacheDirectory_withDatabase_loadsCachedData() + throws Exception { SimpleCache simpleCache = getSimpleCache(); // Write some data and metadata to the cache. @@ -127,8 +158,10 @@ public class SimpleCacheTest { } @Test - public void newInstance_withExistingCacheDirectory_resolvesInconsistentState() throws Exception { - SimpleCache simpleCache = getSimpleCache(); + @SuppressWarnings("deprecation") // Testing deprecated behaviour + public void newInstance_withExistingCacheDirectory_withoutDatabase_resolvesInconsistentState() + throws Exception { + SimpleCache simpleCache = new SimpleCache(testDir, new NoOpCacheEvictor()); CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET); addCache(simpleCache, KEY_1, 0, 15); @@ -149,6 +182,30 @@ public class SimpleCacheTest { } @Test + public void newInstance_withExistingCacheDirectory_withDatabase_resolvesInconsistentState() + throws Exception { + SimpleCache simpleCache = new SimpleCache(testDir, new NoOpCacheEvictor(), databaseProvider); + + CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET); + addCache(simpleCache, KEY_1, 0, 15); + simpleCache.releaseHoleSpan(holeSpan); + simpleCache.removeSpan(simpleCache.getCachedSpans(KEY_1).first()); + + // Don't release the cache. This means the index file won't have been written to disk after the + // span was removed. Move the cache directory instead, so we can reload it without failing the + // folder locking check. + File cacheDir2 = new File(testDir, "cache2"); + cacheDir.renameTo(cacheDir2); + + // Create a new instance pointing to the new directory. + simpleCache = new SimpleCache(cacheDir2, new NoOpCacheEvictor(), databaseProvider); + + // The entry for KEY_1 should have been removed when the cache was reloaded. + assertThat(simpleCache.getCachedSpans(KEY_1)).isEmpty(); + } + + @Test + @SuppressWarnings("deprecation") // Encrypted index is deprecated public void newInstance_withEncryptedIndex() throws Exception { SimpleCache simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY); CacheSpan holeSpan = simpleCache.startReadWrite(KEY_1, 0, LENGTH_UNSET); @@ -165,6 +222,7 @@ public class SimpleCacheTest { } @Test + @SuppressWarnings("deprecation") // Encrypted index is deprecated public void newInstance_withEncryptedIndexAndWrongKey_clearsCache() throws Exception { SimpleCache simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY); @@ -183,6 +241,7 @@ public class SimpleCacheTest { } @Test + @SuppressWarnings("deprecation") // Encrypted index is deprecated public void newInstance_withEncryptedIndexAndNoKey_clearsCache() throws Exception { SimpleCache simpleCache = getEncryptedSimpleCache(ENCRYPTED_INDEX_KEY); @@ -619,7 +678,7 @@ public class SimpleCacheTest { @Test public void usingReleasedCache_throwsException() { - SimpleCache simpleCache = new SimpleCache(cacheDir, new NoOpCacheEvictor()); + SimpleCache simpleCache = getSimpleCache(); simpleCache.release(); assertThrows( IllegalStateException.class, @@ -627,9 +686,11 @@ public class SimpleCacheTest { } private SimpleCache getSimpleCache() { - return new SimpleCache(cacheDir, new NoOpCacheEvictor()); + return new SimpleCache(cacheDir, new NoOpCacheEvictor(), databaseProvider); } + @Deprecated + @SuppressWarnings("deprecation") // Testing deprecated behaviour. private SimpleCache getEncryptedSimpleCache(byte[] secretKey) { return new SimpleCache(cacheDir, new NoOpCacheEvictor(), secretKey); } diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java index 23fb9cbe3d..17c6da74dc 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DashDownloaderTest.java @@ -70,7 +70,8 @@ public class DashDownloaderTest { MockitoAnnotations.initMocks(this); tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest"); - cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider()); progressListener = new ProgressListener(); } diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java index 76b7a76a66..d2756780f8 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadManagerDashTest.java @@ -82,7 +82,9 @@ public class DownloadManagerDashTest { tempFolder = Util.createTempDirectory(context, "ExoPlayerTest"); File cacheFolder = new File(tempFolder, "cache"); cacheFolder.mkdir(); - cache = new SimpleCache(cacheFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache( + cacheFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider()); MockitoAnnotations.initMocks(this); fakeDataSet = new FakeDataSet() diff --git a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java index 9a3e5a4aff..1257a70a07 100644 --- a/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java +++ b/library/dash/src/test/java/com/google/android/exoplayer2/source/dash/offline/DownloadServiceDashTest.java @@ -79,7 +79,8 @@ public class DownloadServiceDashTest { testThread = new DummyMainThread(); context = ApplicationProvider.getApplicationContext(); tempFolder = Util.createTempDirectory(context, "ExoPlayerTest"); - cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider()); Runnable pauseAction = () -> { diff --git a/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java b/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java index 7b5577d22d..1fc51ab498 100644 --- a/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java +++ b/library/hls/src/test/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloaderTest.java @@ -47,6 +47,7 @@ import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist; import com.google.android.exoplayer2.testutil.CacheAsserts.RequestSet; import com.google.android.exoplayer2.testutil.FakeDataSet; import com.google.android.exoplayer2.testutil.FakeDataSource; +import com.google.android.exoplayer2.testutil.TestUtil; import com.google.android.exoplayer2.upstream.DummyDataSource; import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; @@ -76,7 +77,8 @@ public class HlsDownloaderTest { public void setUp() throws Exception { tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest"); - cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider()); progressListener = new ProgressListener(); fakeDataSet = new FakeDataSet() diff --git a/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashDownloadTest.java b/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashDownloadTest.java index 6e9e55e1c1..c14295b027 100644 --- a/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashDownloadTest.java +++ b/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashDownloadTest.java @@ -21,6 +21,7 @@ import android.net.Uri; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.rule.ActivityTestRule; import com.google.android.exoplayer2.MediaItem; +import com.google.android.exoplayer2.database.ExoDatabaseProvider; import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.source.dash.DashUtil; import com.google.android.exoplayer2.source.dash.manifest.AdaptationSet; @@ -69,7 +70,9 @@ public final class DashDownloadTest { .setAudioVideoFormats( DashTestData.AAC_AUDIO_REPRESENTATION_ID, DashTestData.H264_CDD_FIXED); tempFolder = Util.createTempDirectory(testRule.getActivity(), "ExoPlayerTest"); - cache = new SimpleCache(tempFolder, new NoOpCacheEvictor()); + cache = + new SimpleCache( + tempFolder, new NoOpCacheEvictor(), new ExoDatabaseProvider(testRule.getActivity())); httpDataSourceFactory = new DefaultHttpDataSourceFactory("ExoPlayer", null); offlineDataSourceFactory = new CacheDataSource.Factory().setCache(cache); }