Modern Apps git

← Modern-Apps.git

notes: clear FileProvider path-strategy cache between share tests

Commit
47c1fe2a8dd5c863bf3c3680b614d5b4fa49aeac
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:23 UTC (28 days ago)
Parents
f9e92ad
FileProvider caches its path strategy per authority in a static map, but
Robolectric gives every test method a fresh temp cacheDir. Whichever test
touched FileProvider first won the cache, and every later
getUriForFile call resolved against a deleted directory, failing with
IllegalArgumentException. This was a test-isolation artifact, not an app
bug: the same sanitized title passes solo, and the failure hit any test
touching FileProvider, not just hostile titles.

The test setup now clears FileProvider's static sCache via reflection
before each test (guarded, so a future core rename fails loudly in the
tests rather than silently). Full NoteStoresTest class passes 12/12 with
this in place.

Verified: full :notes:testDevUnitTest suite green (153 tests, 0 failures).

Diff

notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt
diff --git a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.ktindex fd49817012..7849da0f28 100644--- a/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt+++ b/notes/src/test/java/com/vayunmathur/notes/platform/NoteStoresTest.kt@@ -24,7 +24,18 @@ class NoteStoresTest {     private val context: Context get() = ApplicationProvider.getApplicationContext()     private val imagesDir: File get() = File(context.filesDir, "note_images") -    @BeforeTest fun clean() { imagesDir.deleteRecursively() }+    @BeforeTest fun clean() {+        imagesDir.deleteRecursively()+        // androidx FileProvider caches its path strategy per authority in a static map, but+        // Robolectric gives every test a fresh temp cacheDir, so a strategy cached by an+        // earlier test points at a deleted dir and later getUriForFile calls fail with+        // IllegalArgumentException. Clear it so each test resolves against its own dirs.+        try {+            val cache = Class.forName("androidx.core.content.FileProvider")+                .getDeclaredField("sCache").apply { isAccessible = true }.get(null) as MutableMap<*, *>+            cache.clear()+        } catch (_: Exception) { /* sCache renamed in a future core version: tests will say so */ }+    }      private fun writeImageFile(name: String, w: Int = 40, h: Int = 20): File {         imagesDir.mkdirs()