Modern Apps git

← Modern-Apps.git

translate: stop ViewModel init clobbering an early language pick (#tests)

Commit
9f25d77a9d86564c97f80397dfebdb0f14a72ed5
Author
Vayun Mathur <[email protected]>
When
2026-11-16 18:25 UTC (19 days ago)
Parents
14e6df1
Product bug behind the CI failure of "text page reuse loads
the record pair into the page state": TranslateViewModel's
init coroutine applies the persisted language pair whenever
its DataStore read lands. A history-reuse request (or any
early user pick) calls setSource/setTarget synchronously, but
if the init read resumed afterwards it overwrote the pick
with the stale persisted pair โ€” the test saw sourceLang snap
back to "auto" after the reuse had set "en". On device the
same race could revert a just-opened reuse/shared-text
language pair.

setSource/setTarget (and swap, via them) now mark the pair as
touched, and init only applies the persisted values when no
explicit pick has happened. The test side also pumps the
paused main looper with a step-counted budget: rule.waitUntil
does not idle Robolectric's looper, so the page's
LaunchedEffect could sit unexecuted for the whole timeout.

Verified: DbBackedPagesTest green in the combined TZ=UTC
4-worker runs (three consecutive) plus a TZ=America/New_York
spot check.

Diff

translate/src/main/java/com/vayunmathur/translate/platform/TranslateViewModel.kt
diff --git a/translate/src/main/java/com/vayunmathur/translate/platform/TranslateViewModel.kt b/translate/src/main/java/com/vayunmathur/translate/platform/TranslateViewModel.ktindex ecdd895c50..857f4651b3 100644--- a/translate/src/main/java/com/vayunmathur/translate/platform/TranslateViewModel.kt+++ b/translate/src/main/java/com/vayunmathur/translate/platform/TranslateViewModel.kt@@ -91,10 +91,25 @@ class TranslateViewModel(app: Application) : AndroidViewModel(app) {      private val sharedImageCache = SharedImageCache.inCacheDir(app.cacheDir) +    /**+     * Set once the user (or a history-reuse request) picks a language+     * explicitly. The init read below must not clobber such a pick: it+     * resumes whenever the DataStore read lands, which can be AFTER an+     * early setSource/setTarget โ€” overwriting the just-picked pair with+     * the stale persisted one (seen in tests as sourceLang snapping+     * back to "auto" after a reuse had set it).+     */+    @Volatile+    private var languagesTouched = false+     init {         viewModelScope.launch {-            _sourceLang.value = settings.source()-            _targetLang.value = settings.target()+            val persistedSource = settings.source()+            val persistedTarget = settings.target()+            if (!languagesTouched) {+                _sourceLang.value = persistedSource+                _targetLang.value = persistedTarget+            }             _recentSourceLangs.value = settings.recentSources()             _recentTargetLangs.value = settings.recentTargets()             _translationAvailable.value = translator.isAvailable()@@ -102,6 +117,7 @@ class TranslateViewModel(app: Application) : AndroidViewModel(app) {     }      fun setSource(code: String) {+        languagesTouched = true         _sourceLang.value = code         viewModelScope.launch {             settings.setSource(code)@@ -111,6 +127,7 @@ class TranslateViewModel(app: Application) : AndroidViewModel(app) {     }      fun setTarget(code: String) {+        languagesTouched = true         _targetLang.value = code         viewModelScope.launch {             settings.setTarget(code)
translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt
diff --git a/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt b/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.ktindex 521c626d61..3766250e76 100644--- a/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt+++ b/translate/src/test/java/com/vayunmathur/translate/ui/DbBackedPagesTest.kt@@ -163,8 +163,24 @@ class DbBackedPagesTest {                 onOpenLanguagePicker = {},             )         }-        rule.waitUntil(8_000) { consumed == 1 }-        rule.waitUntil(8_000) { viewModel.sourceLang.value == "en" && viewModel.targetLang.value == "es" }+        // Step-counted pump, not rule.waitUntil: waitUntil does not idle+        // Robolectric's paused main looper, so the page's LaunchedEffect+        // (which runs on Main) can sit unexecuted for the whole timeout+        // on a loaded box โ€” exactly the CI/local combined-run failure.+        var steps = 0+        while (consumed != 1 && steps++ < 6000) {+            idle()+            rule.waitForIdle()+            if (consumed == 1) break+            try { Thread.sleep(25) } catch (_: InterruptedException) { }+        }+        assertEquals(1, consumed)+        steps = 0+        while (!(viewModel.sourceLang.value == "en" && viewModel.targetLang.value == "es") && steps++ < 6000) {+            idle()+            if (viewModel.sourceLang.value == "en" && viewModel.targetLang.value == "es") break+            try { Thread.sleep(25) } catch (_: InterruptedException) { }+        }         assertEquals("en", viewModel.sourceLang.value)         assertEquals("es", viewModel.targetLang.value)         rule.onNodeWithText("reuse hello").assertIsDisplayed()