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 flaky tests blocking CI (#159)

Rishabh Bajpai6a05984e59ff
diff --git a/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt b/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt
index 03464fb..7a5801c 100644
--- a/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt
+++ b/app/src/test/java/com/gotcha/data/GotchaStorageTest.kt
@@ -1,5 +1,6 @@
package com.gotcha.data
+import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
@@ -22,6 +23,13 @@ class GotchaStorageTest {
GotchaStorage.rootPath = tmp.newFolder("Gotcha").absolutePath
}
+ @After
+ fun tearDown() {
+ // The override is process-wide: leave it set and later classes
+ // resolve their files against this deleted folder.
+ GotchaStorage.resetRootPathForTests()
+ }
+
// ---- slugify ----
@Test
diff --git a/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt b/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt
index 6d832c2..b29e1bb 100644
--- a/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt
+++ b/app/src/test/java/com/gotcha/data/SettingsChangeNotifierTest.kt
@@ -81,21 +81,22 @@ class SettingsChangeNotifierTest {
val fired = intArrayOf(0)
val watcher = coalescedSettingsListener { fired[0]++ }
listener = watcher
- val prefs = settingsChangeNotifier(context)
- prefs.registerOnSharedPreferenceChangeListener(watcher)
// What SettingsRepository.save looks like from here: every key in one
- // apply, each with a new value (re-encryption gives every one a new IV).
- prefs.edit().apply { repeat(58) { putString("burst_key_$it", "v$it") } }.apply()
+ // apply, each with a new value (re-encryption gives every one a new
+ // IV). Drive the listener directly: framework delivery timing (sync
+ // vs looper-posted per key) varies by environment, but the burst the
+ // coalescer must collapse is always 58 synchronous notifications.
+ // Delivery itself is covered by `a write by one caller...` above.
+ repeat(58) { watcher.onSharedPreferenceChanged(null, "burst_key_$it") }
shadowOf(Looper.getMainLooper()).idle()
// Per key, MainActivity re-read the settings 58 times inside one tap
// and the skin picker ANR'd on a slow phone.
assertEquals("one save must be one callback", 1, fired[0])
- prefs.edit().putString("burst_key_0", "again").apply()
+ watcher.onSharedPreferenceChanged(null, "burst_key_0")
shadowOf(Looper.getMainLooper()).idle()
assertEquals("a later save must still be heard", 2, fired[0])
- prefs.unregisterOnSharedPreferenceChangeListener(watcher)
}
}
diff --git a/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt b/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
index a9c22a0..a233d6a 100644
--- a/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
+++ b/app/src/test/java/com/gotcha/tools/PodcastToolTest.kt
@@ -61,6 +61,10 @@ class PodcastToolTest {
@Before
fun setUp() {
+ // GotchaStorage roots are process-wide: another class's override
+ // would point this test's files (and FileProvider's roots) at a
+ // foreign sandbox. Start from the ambient default every time.
+ GotchaStorage.resetRootPathForTests()
ShadowExternalStorageManager.granted = true
// FileProvider caches its path strategy statically per authority
// across the whole test task while each test gets a fresh sandbox
@@ -72,6 +76,7 @@ class PodcastToolTest {
@After
fun tearDown() {
+ GotchaStorage.resetRootPathForTests()
ShadowExternalStorageManager.resetGranted()
FileProviderCache.clear()
}