Modern Apps git

← Modern-Apps.git

updater: reset TestSysProps after tests that set build system properties

Commit
a0e360863963813c9d302d88fac0d8d9feba5d8b
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:35 UTC (28 days ago)
Parents
955a6bb
The full :updater:testDevUnitTest suite failed
UpdaterScreenTest.an available update names the target build with an
IllegalStateException from EspressoLink.runUntilIdle, while every test
class passed in isolation. Root cause: TestSysProps (in
FakeHttpsConnection.kt) sets JVM-global system properties such as
ro.build.fingerprint, and those leak across Robolectric sandboxes —
each new sandbox initializes android.os.Build from the polluted value.
SystemBuildCheckerTest.check reports a newer build as available sets
ro.build.fingerprint to a shiba release fingerprint and never restored
it. When it ran before UpdaterScreenTest, compose's
hasRobolectricFingerprint check returned false, so the test picked
EspressoLink instead of RobolectricIdlingStrategy and runUntilIdle
threw on the test thread. Execution order across classes is
nondeterministic, which is why the failure only appeared in full-suite
runs. Confirmed with a deterministic in-class repro (since removed).

TestSysProps now snapshots every managed key JVM-globally on first
set() and exposes reset(), which restores all managed keys
(ro.build.version.incremental, ro.build.date.utc, ro.product.device,
ro.build.fingerprint, ro.maos.ota.server). reset() is called from the
tearDown of every test class that touches TestSysProps:
SystemBuildCheckerTest, UpdateCheckWorkerTest, UpdaterViewModelTest,
UpdateInstallServiceTest, and both classes in
MainActivityNavigationTest. This makes the suite order-independent.

Verified with two consecutive full :updater:testDevUnitTest runs
(151 tests, green), plus :updater:compileDevKotlin, :updater:lint and
:updater:checkDevAarMetadata, all green.

Diff

updater/src/test/java/com/vayunmathur/updater/MainActivityNavigationTest.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/MainActivityNavigationTest.kt b/updater/src/test/java/com/vayunmathur/updater/MainActivityNavigationTest.ktindex 5a62e5a165..a21a705a2d 100644--- a/updater/src/test/java/com/vayunmathur/updater/MainActivityNavigationTest.kt+++ b/updater/src/test/java/com/vayunmathur/updater/MainActivityNavigationTest.kt@@ -9,6 +9,7 @@ import androidx.compose.ui.test.performClick import com.vayunmathur.library.ui.DynamicTheme import com.vayunmathur.updater.platform.TestSysProps import com.vayunmathur.updater.platform.UpdaterViewModel+import kotlin.test.AfterTest import kotlin.test.Test import org.junit.Rule import org.junit.runner.RunWith@@ -22,6 +23,11 @@ class MainActivityNavigationTest {     @get:Rule     val composeRule = createComposeRule() +    @AfterTest+    fun tearDown() {+        TestSysProps.reset()+    }+     @Test     fun `navigation hosts the updater screen for the home route`() {         TestSysProps.setRunningBuild(dateUtcSeconds = 1000L)@@ -38,6 +44,11 @@ class MainActivityNavigationTest { @RunWith(RobolectricTestRunner::class) class MainActivityTest { +    @AfterTest+    fun tearDown() {+        TestSysProps.reset()+    }+     @Test     fun `the activity launches, schedules checks and renders the home route`() {         runCatching {
updater/src/test/java/com/vayunmathur/updater/platform/FakeHttpsConnection.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/platform/FakeHttpsConnection.kt b/updater/src/test/java/com/vayunmathur/updater/platform/FakeHttpsConnection.ktindex 08119cdcd4..2ea92d07ed 100644--- a/updater/src/test/java/com/vayunmathur/updater/platform/FakeHttpsConnection.kt+++ b/updater/src/test/java/com/vayunmathur/updater/platform/FakeHttpsConnection.kt@@ -50,11 +50,52 @@ class FakeHttpsConnection(  /** Sets a hidden `android.os.SystemProperties` value the way a real build would report it. */ object TestSysProps {+    private val managedKeys = listOf(+        "ro.build.version.incremental",+        "ro.build.date.utc",+        "ro.product.device",+        "ro.build.fingerprint",+        "ro.maos.ota.server",+    )+    private const val backupPrefix = "updater.testsysprops.orig."+    private const val backupDone = "updater.testsysprops.backupDone"+     fun set(key: String, value: String) {+        backupOnce()+        sysPropsSet(key, value)+    }++    /**+     * Restores every managed key to the value it held before the first [set] call in this+     * JVM. Robolectric's `android.os.SystemProperties` state is JVM-global, so without this+     * a fake `ro.build.fingerprint` leaks into later test classes' sandboxes, where it+     * changes `Build.FINGERPRINT` and breaks Compose's Robolectric fingerprint check.+     * Call from `@AfterTest` in every test that mutates system properties.+     */+    fun reset() {+        for (key in managedKeys) {+            sysPropsSet(key, System.getProperty(backupPrefix + key) ?: "")+        }+    }++    private fun backupOnce() {+        if (System.getProperty(backupDone) == "true") return+        for (key in managedKeys) {+            System.setProperty(backupPrefix + key, sysPropsGet(key) ?: "")+        }+        System.setProperty(backupDone, "true")+    }++    private fun sysPropsSet(key: String, value: String) {         val cls = Class.forName("android.os.SystemProperties")         cls.getMethod("set", String::class.java, String::class.java).invoke(null, key, value)     } +    private fun sysPropsGet(key: String): String? {+        val cls = Class.forName("android.os.SystemProperties")+        return cls.getMethod("get", String::class.java).invoke(null, key) as String?+    }+     fun setRunningBuild(build: String = "2026090500", dateUtcSeconds: Long = 1000L, device: String = "shiba") {         set("ro.build.version.incremental", build)         set("ro.build.date.utc", dateUtcSeconds.toString())
updater/src/test/java/com/vayunmathur/updater/platform/SystemBuildCheckerTest.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/platform/SystemBuildCheckerTest.kt b/updater/src/test/java/com/vayunmathur/updater/platform/SystemBuildCheckerTest.ktindex c7a28ae953..917ebd6d16 100644--- a/updater/src/test/java/com/vayunmathur/updater/platform/SystemBuildCheckerTest.kt+++ b/updater/src/test/java/com/vayunmathur/updater/platform/SystemBuildCheckerTest.kt@@ -16,6 +16,7 @@ class SystemBuildCheckerTest {     @AfterTest     fun tearDown() {         UpdateChecker.connectionOverride = null+        TestSysProps.reset()     }      @Test
updater/src/test/java/com/vayunmathur/updater/platform/UpdateCheckWorkerTest.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/platform/UpdateCheckWorkerTest.kt b/updater/src/test/java/com/vayunmathur/updater/platform/UpdateCheckWorkerTest.ktindex 8df43518cb..fbdae07a56 100644--- a/updater/src/test/java/com/vayunmathur/updater/platform/UpdateCheckWorkerTest.kt+++ b/updater/src/test/java/com/vayunmathur/updater/platform/UpdateCheckWorkerTest.kt@@ -42,6 +42,7 @@ class UpdateCheckWorkerTest {     @AfterTest     fun tearDown() {         UpdateChecker.connectionOverride = null+        TestSysProps.reset()     }      private fun fakeBody(body: String, code: Int = 200) {
updater/src/test/java/com/vayunmathur/updater/platform/UpdaterViewModelTest.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/platform/UpdaterViewModelTest.kt b/updater/src/test/java/com/vayunmathur/updater/platform/UpdaterViewModelTest.ktindex f392ae8f82..b711b17a14 100644--- a/updater/src/test/java/com/vayunmathur/updater/platform/UpdaterViewModelTest.kt+++ b/updater/src/test/java/com/vayunmathur/updater/platform/UpdaterViewModelTest.kt@@ -38,6 +38,7 @@ class UpdaterViewModelTest {     @AfterTest     fun tearDown() {         UpdateChecker.connectionOverride = null+        TestSysProps.reset()     }      private fun awaitState(vm: UpdaterViewModel, condition: (UpdaterUiState) -> Boolean) {
updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt
diff --git a/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt b/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.ktindex bfb2d18954..509cdc5ee9 100644--- a/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt+++ b/updater/src/test/java/com/vayunmathur/updater/service/UpdateInstallServiceTest.kt@@ -82,6 +82,7 @@ class UpdateInstallServiceTest {         OtaInstaller.signatureCheckOverride = null         OtaInstaller.payloadEngineOverride = null         OtaInstaller.careMapPathOverride = null+        TestSysProps.reset()         staged.delete(); careMap.delete()     }