Modern Apps git

← Modern-Apps.git

auto: hoist test ViewModel construction out of setContent

Commit
4f367d39b254fc6a389a77f7e8febbd191a83aac
Author
Vayun Mathur <[email protected]>
When
2026-11-08 04:54 UTC (28 days ago)
Parents
a0e3608
What: construct AutoViewModel and PairingViewModel in local vals before
rule.setContent in NavigationAndTelephonyTest's navigation test, instead
of constructing them inside the setContent composable lambda.

Why: :auto:lintDev failed with two ViewModelConstructorInComposable
errors (androidx.lifecycle) on NavigationAndTelephonyTest.kt:27, which
breaks CI for the whole module.

Root cause: ViewModel constructors must not run inside a composable —
each recomposition would build a new instance outside the ViewModelStore
lifecycle. The test passed constructor calls directly in the setContent
lambda, tripping the lint check.

Rejected alternatives: adding a lint suppression or baseline entry would
hide a real lifecycle misuse and leave CI one exemption heavier; using
androidx.lifecycle.viewmodel.compose.viewModel() inside the content would
change what the test exercises (store-owned instances vs plain ones) for
no benefit.

User-visible effect: none — test-only change, no production code touched.

Verification: reproduced with :auto:lint (2 errors before the fix);
after the fix :auto:lint and :auto:testDevUnitTest both pass
(BUILD SUCCESSFUL).

Diff

auto/src/test/java/com/vayunmathur/auto/NavigationAndTelephonyTest.kt
diff --git a/auto/src/test/java/com/vayunmathur/auto/NavigationAndTelephonyTest.kt b/auto/src/test/java/com/vayunmathur/auto/NavigationAndTelephonyTest.ktindex f0d33302a9..d5083f4849 100644--- a/auto/src/test/java/com/vayunmathur/auto/NavigationAndTelephonyTest.kt+++ b/auto/src/test/java/com/vayunmathur/auto/NavigationAndTelephonyTest.kt@@ -24,7 +24,9 @@ class NavigationAndTelephonyTest {     @BeforeTest fun reset() { AutoSessionState.onDisconnected() }      @Test fun `navigation home renders the auto screen`() {-        rule.setContent { Navigation(AutoViewModel(), PairingViewModel()) }+        val autoVm = AutoViewModel()+        val pairingVm = PairingViewModel()+        rule.setContent { Navigation(autoVm, pairingVm) }         rule.onNodeWithText(ctx.getString(AutoR.string.state_disconnected)).assertExists()         rule.onNodeWithText(ctx.getString(AutoR.string.connect_hint)).assertExists()     }