Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

fix: clear FileProvider static cache between Robolectric tests

Rishabh Bajpai4f5cae5f3f33
diff --git a/app/src/test/java/com/gotcha/testsupport/FileProviderCache.kt b/app/src/test/java/com/gotcha/testsupport/FileProviderCache.kt
new file mode 100644
index 0000000..c54c631
--- /dev/null
+++ b/app/src/test/java/com/gotcha/testsupport/FileProviderCache.kt
@@ -0,0 +1,25 @@
+package com.gotcha.testsupport
+
+import androidx.core.content.FileProvider
+
+/**
+ * AndroidX `FileProvider` caches its parsed `file_paths.xml` strategy in a
+ * static map keyed by authority. Every Robolectric test gets a fresh
+ * application sandbox, but the JVM (and the cache) is shared across the
+ * whole Gradle test task — so whichever test calls `getUriForFile` first
+ * pins every later test's lookups to ITS sandbox, and files from other
+ * sandboxes wrongly resolve as "outside the mapped roots".
+ *
+ * Call [clear] from `@Before` (and `@After`, for the next class) in every
+ * test that touches `FileProvider`, same discipline as
+ * [ShadowExternalStorageManager.resetGranted].
+ */
+object FileProviderCache {
+ fun clear() {
+ val field = FileProvider::class.java.getDeclaredField("sCache").apply {
+ isAccessible = true
+ }
+ @Suppress("UNCHECKED_CAST")
+ (field.get(null) as MutableMap<*, *>).clear()
+ }
+}
diff --git a/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt b/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
index adfc86d..a9c22a0 100644
--- a/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
@@ -8,6 +8,7 @@ import com.gotcha.audio.ModelCategory
import com.gotcha.audio.VoiceInfo
import com.gotcha.data.GotchaStorage
import com.gotcha.data.Settings
+import com.gotcha.testsupport.FileProviderCache
import com.gotcha.testsupport.ShadowExternalStorageManager
import kotlinx.coroutines.runBlocking
import org.junit.After
@@ -61,6 +62,10 @@ class PodcastToolTest {
@Before
fun setUp() {
ShadowExternalStorageManager.granted = true
+ // FileProvider caches its path strategy statically per authority
+ // across the whole test task while each test gets a fresh sandbox
+ // (see FileProviderCache) — clear it so this test resolves ITS roots.
+ FileProviderCache.clear()
context = ApplicationProvider.getApplicationContext()
GotchaStorage.podcastsRoot().deleteRecursively()
}
@@ -68,6 +73,7 @@ class PodcastToolTest {
@After
fun tearDown() {
ShadowExternalStorageManager.resetGranted()
+ FileProviderCache.clear()
}
private fun tool(
diff --git a/app/src/test/java/com/gotcha/updater/AppUpdateManagerRobolectricTest.kt b/app/src/test/java/com/gotcha/updater/AppUpdateManagerRobolectricTest.kt
index 005a507..373043d 100644
--- a/app/src/test/java/com/gotcha/updater/AppUpdateManagerRobolectricTest.kt
+++ b/app/src/test/java/com/gotcha/updater/AppUpdateManagerRobolectricTest.kt
@@ -5,10 +5,13 @@ import android.content.Intent
import android.net.Uri
import android.provider.Settings
import androidx.test.core.app.ApplicationProvider
+import com.gotcha.testsupport.FileProviderCache
import kotlinx.coroutines.test.runTest
+import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
+import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@@ -28,6 +31,17 @@ class AppUpdateManagerRobolectricTest {
private val updateManager = AppUpdateManager()
private val context: Context = ApplicationProvider.getApplicationContext()
+ @Before
+ fun clearFileProviderCache() {
+ // Same static-cache poisoning as PodcastToolTest (see FileProviderCache):
+ // each test gets a fresh sandbox but FileProvider remembers the first
+ // caller's roots for the whole test task.
+ FileProviderCache.clear()
+ }
+
+ @After
+ fun clearFileProviderCacheAfter() = FileProviderCache.clear()
+
@Test
fun downloadUpdate_untrustedUrl_returnsFailureBeforeAnyNetworkUse() = runTest {
val info = AppUpdateInfo(