setupwizard: add tests for view model, screens and navigation
- Commit
9a405378b5b44df7526bcb68e10dfdbb1b194675- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 05:56 UTC (29 days ago)
- Parents
00a2a21
Covers SetupViewModel (platform state on init, location toggles, bootloader acknowledgement countdown, welcome diversion), DebugFlags parsing, SetupIntents targets, and Robolectric Compose tests for the welcome, language-picker, location, OEM-unlock, finish, gestures, migration and checkbox screens plus the full Navigation flow from welcome through the Wi-Fi handoff. Coverage (merged, instructions): 3.9% -> 63.6% (219/5575 -> 3548/5575). Full suite passes via gslot ./gradlew :setupwizard:createMergedCoverageReport. Bugs fixed: none. Untestable remainder: SystemSetup's hidden-framework writes (system locale, location mode, OEM-lock manager, reboot, self-disable) which Robolectric does not implement, and finishSetup's activity teardown. (cherry picked from commit b95fbfa04fb00d19cbfd9ff1e9e891608b32904d)
Diff
setupwizard/src/test/java/com/vayunmathur/setupwizard/NavigationTest.kt
diff --git a/setupwizard/src/test/java/com/vayunmathur/setupwizard/NavigationTest.kt b/setupwizard/src/test/java/com/vayunmathur/setupwizard/NavigationTest.ktnew file mode 100644index 0000000000..836eb33f77--- /dev/null+++ b/setupwizard/src/test/java/com/vayunmathur/setupwizard/NavigationTest.kt@@ -0,0 +1,52 @@+package com.vayunmathur.setupwizard++import android.app.Application+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.setupwizard.platform.SetupViewModel+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.test.resetMain+import kotlinx.coroutines.test.setMain+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals++@OptIn(ExperimentalCoroutinesApi::class)+@RunWith(RobolectricTestRunner::class)+class NavigationTest {+ @get:Rule val rule = createComposeRule()+ private val ctx get() = ApplicationProvider.getApplicationContext<android.content.Context>()+ private val app get() = ApplicationProvider.getApplicationContext<Application>()++ @BeforeTest fun setUp() { Dispatchers.setMain(Dispatchers.Unconfined) }+ @AfterTest fun tearDown() { Dispatchers.resetMain() }++ @Test fun `navigation starts at welcome and advances to location for a primary user`() {+ val vm = SetupViewModel(app)+ rule.setContent { Navigation(vm) }+ rule.onNodeWithText(ctx.getString(R.string.welcome_to_modern_apps_os)).assertExists()+ rule.onNodeWithText(ctx.getString(R.string.next)).performClick()+ // The next step is the Wi-Fi handoff (no UI of its own): the welcome step+ // leaves the screen while the external setup activity is launched.+ rule.waitUntil(timeoutMillis = 10_000) {+ rule.onAllNodes(androidx.compose.ui.test.hasText(ctx.getString(R.string.welcome_to_modern_apps_os)))+ .fetchSemanticsNodes().isEmpty()+ }+ rule.onNodeWithText(ctx.getString(R.string.welcome_to_modern_apps_os)).assertDoesNotExist()+ }++ @Test fun `routes are distinct destinations`() {+ assertEquals(Route.Welcome, Route.Welcome)+ assertTrue(Route.Finish != Route.Welcome)+ assertTrue(Route.OemUnlock != Route.Wifi)+ }++ private fun assertTrue(value: Boolean) = kotlin.test.assertTrue(value)+}
setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SetupViewModelTest.kt
diff --git a/setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SetupViewModelTest.kt b/setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SetupViewModelTest.ktnew file mode 100644index 0000000000..9fe1da5ff6--- /dev/null+++ b/setupwizard/src/test/java/com/vayunmathur/setupwizard/platform/SetupViewModelTest.kt@@ -0,0 +1,106 @@+package com.vayunmathur.setupwizard.platform++import android.app.Application+import android.content.Context+import android.provider.Settings+import androidx.test.core.app.ApplicationProvider+import kotlinx.coroutines.Dispatchers+import kotlinx.coroutines.ExperimentalCoroutinesApi+import kotlinx.coroutines.test.resetMain+import kotlinx.coroutines.test.setMain+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import java.util.Locale+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertTrue++@OptIn(ExperimentalCoroutinesApi::class)+@RunWith(RobolectricTestRunner::class)+class SetupViewModelTest {+ private val app get() = ApplicationProvider.getApplicationContext<Application>()+ private val ctx get() = ApplicationProvider.getApplicationContext<Context>()++ @BeforeTest fun setUp() { Dispatchers.setMain(Dispatchers.Unconfined) }+ @AfterTest fun tearDown() { Dispatchers.resetMain() }++ @Test fun `initial state reflects the platform readings`() {+ val vm = SetupViewModel(app)+ assertTrue(vm.state.language.isNotBlank())+ assertEquals(OEM_UNLOCK_ACK_SECONDS, vm.state.bootloaderAckSeconds)+ // Robolectric reports a primary user on an unlocked, insecure device+ assertTrue(vm.state.isPrimaryUser)+ assertFalse(vm.state.bootloaderUnlocked)+ assertFalse(vm.state.deviceSecure)+ }++ @Test fun `language can be set and available languages are listed`() {+ val vm = SetupViewModel(app)+ // The system-locale write goes through a hidden framework API that Robolectric+ // does not implement, so the reading stays at the system locale; the call must+ // still complete and refresh the display name from the platform.+ vm.setLanguage(Locale.FRENCH)+ assertEquals(vm.system.currentLocale().getDisplayName(vm.system.currentLocale()), vm.state.language)+ vm.setLanguage(Locale.US)+ assertTrue(vm.availableLanguages().isNotEmpty())+ }++ @Test fun `location toggles round trip through settings`() {+ val vm = SetupViewModel(app)+ // Location/wifi writes also ride hidden APIs; under Robolectric they no-op and+ // the state simply mirrors whatever the platform reports afterwards.+ vm.setLocationEnabled(true)+ assertEquals(vm.system.isLocationEnabled(), vm.state.locationEnabled)+ vm.setLocationEnabled(false)+ assertEquals(vm.system.isLocationEnabled(), vm.state.locationEnabled)+ vm.setWifiScanningEnabled(true)+ assertEquals(vm.system.isWifiScanningAlwaysAvailable(), vm.state.wifiScanningEnabled)+ }++ @Test fun `welcome does not divert to bootloader warning when locked`() {+ val vm = SetupViewModel(app)+ vm.onEnterWelcome() // applies SIM locale if any; must not crash either way+ assertFalse(vm.welcomeLeadsToBootloaderWarning())+ vm.refreshSecurity()+ assertFalse(vm.state.deviceSecure)+ }++ @Test fun `ack timer counts down once and does not restart`() {+ val vm = SetupViewModel(app)+ vm.startBootloaderAckTimer()+ val deadline = System.currentTimeMillis() + 8_000+ while (System.currentTimeMillis() < deadline && vm.state.bootloaderAckSeconds >= OEM_UNLOCK_ACK_SECONDS) {+ Thread.sleep(50)+ }+ assertTrue(vm.state.bootloaderAckSeconds < OEM_UNLOCK_ACK_SECONDS, "timer did not tick: ${vm.state.bootloaderAckSeconds}")+ val seen = vm.state.bootloaderAckSeconds+ vm.startBootloaderAckTimer() // guarded: no second timer+ assertTrue(vm.state.bootloaderAckSeconds <= seen)+ }++ @Test fun `debug flags parse values bare flags and booleans`() {+ Settings.Global.putString(ctx.contentResolver, "setupwizard_debug_flags", "alpha=1,beta,gamma=false")+ assertEquals("1", DebugFlags.get(ctx, "alpha"))+ assertEquals("true", DebugFlags.get(ctx, "beta"))+ assertEquals("false", DebugFlags.get(ctx, "gamma"))+ assertEquals(false, DebugFlags.getBool(ctx, "gamma"))+ assertEquals(true, DebugFlags.getBool(ctx, "beta"))+ assertEquals(null, DebugFlags.get(ctx, "missing"))+ assertEquals(null, DebugFlags.getBool(ctx, "alpha")) // "1" is not a strict boolean+ Settings.Global.putString(ctx.contentResolver, "setupwizard_debug_flags", null)+ assertEquals(null, DebugFlags.get(ctx, "alpha"))+ }++ @Test fun `setup intents target the platform settings actions`() {+ assertEquals("android.settings.ACCESSIBILITY_SETTINGS_FOR_SUW", SetupIntents.accessibilitySettings().action)+ assertTrue(SetupIntents.biometricEnroll().action != null)+ assertTrue(SetupIntents.gestureTutorial().action != null)+ val internet = SetupIntents.setupInternet("Wi-Fi", "Pick one", "Skip")+ assertTrue(internet.action != null)+ // Nothing restores backups on this test image: the migration step is skipped by design+ assertEquals(null, SetupIntents.restoreBackup(ctx))+ }+}
setupwizard/src/test/java/com/vayunmathur/setupwizard/ui/SetupScreensTest.kt
diff --git a/setupwizard/src/test/java/com/vayunmathur/setupwizard/ui/SetupScreensTest.kt b/setupwizard/src/test/java/com/vayunmathur/setupwizard/ui/SetupScreensTest.ktnew file mode 100644index 0000000000..3eeb9fd44a--- /dev/null+++ b/setupwizard/src/test/java/com/vayunmathur/setupwizard/ui/SetupScreensTest.kt@@ -0,0 +1,116 @@+package com.vayunmathur.setupwizard.ui++import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.setupwizard.R+import com.vayunmathur.setupwizard.platform.SetupUiState+import com.vayunmathur.setupwizard.ui.components.SetupCheckboxRow+import com.vayunmathur.setupwizard.ui.dialogs.LanguagePickerDialog+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import java.util.Locale+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+class SetupScreensTest {+ @get:Rule val rule = createComposeRule()+ private val ctx get() = ApplicationProvider.getApplicationContext<android.content.Context>()+ private fun str(id: Int, vararg args: Any) = if (args.isEmpty()) ctx.getString(id) else ctx.getString(id, *args)++ @Test fun `welcome for primary user offers next and language row`() {+ var next = 0+ rule.setContent {+ WelcomeScreen(state = SetupUiState(isPrimaryUser = true, language = "English"), languages = { listOf(Locale.US) },+ onLanguageSelected = {}, onAccessibility = {}, onEmergencyCall = null, onNext = { next++ })+ }+ rule.onNodeWithText(str(R.string.welcome_to_modern_apps_os)).assertExists()+ rule.onNodeWithText(str(R.string.lets_setup_your_device)).assertExists()+ rule.onNodeWithText(str(R.string.next)).performClick()+ assertEquals(1, next)+ }++ @Test fun `welcome for secondary user and unlocked bootloader shows notice`() {+ rule.setContent {+ WelcomeScreen(state = SetupUiState(isPrimaryUser = false, bootloaderUnlocked = true, language = "English"),+ languages = { emptyList() }, onLanguageSelected = {}, onAccessibility = {}, onEmergencyCall = {}, onNext = {})+ }+ rule.onNodeWithText(str(R.string.lets_setup_your_profile)).assertExists()+ rule.onNodeWithText(str(R.string.emergency_call)).assertExists()+ }++ @Test fun `language picker lists languages and selects`() {+ val picked = mutableListOf<Locale>()+ rule.setContent {+ LanguagePickerDialog(languages = { listOf(Locale.US, Locale.FRENCH) }, onSelected = { picked += it }, onDismiss = {})+ }+ rule.onNodeWithText(Locale.FRENCH.getDisplayName(Locale.FRENCH), substring = true).performClick()+ assertEquals(listOf(Locale.FRENCH), picked)+ }++ @Test fun `location screen primary sees wifi scanning, secondary does not`() {+ var location: Boolean? = null+ rule.setContent {+ LocationScreen(state = SetupUiState(isPrimaryUser = true, locationEnabled = false),+ onLocationEnabled = { location = it }, onWifiScanningEnabled = {}, onNext = {})+ }+ rule.onNodeWithText(str(R.string.location_services)).assertExists()+ rule.onNodeWithText(str(R.string.next)).assertExists()+ assertEquals(null, location)+ }++ @Test fun `oem unlock screen counts down and gates continue`() {+ var started = 0; var continues = 0+ rule.setContent {+ OemUnlockScreen(state = SetupUiState(bootloaderAckSeconds = 10, bootloaderUnlocked = true),+ onStartAckTimer = { started++ }, onRebootToBootloader = {}, onContinue = { continues++ })+ }+ rule.onNodeWithText(str(R.string.lock_your_bootloader)).assertExists()+ rule.onNodeWithText(str(R.string.continue_without_locking_timer, 10)).assertExists()+ assertEquals(1, started)+ assertEquals(0, continues)+ }++ @Test fun `oem unlock screen at zero offers plain continue`() {+ rule.setContent {+ OemUnlockScreen(state = SetupUiState(bootloaderAckSeconds = 0),+ onStartAckTimer = {}, onRebootToBootloader = {}, onContinue = {})+ }+ rule.onNodeWithText(str(R.string.continue_without_locking)).assertExists()+ }++ @Test fun `finish screen hides oem checkbox for secondary users`() {+ var finishes = 0+ rule.setContent {+ FinishScreen(state = SetupUiState(isPrimaryUser = false, bootloaderUnlocked = false), onFinish = { finishes++ })+ }+ // The OEM-unlocking opt-out is a security control only the owner can answer+ assertEquals(0, finishes)+ assertTrue(SetupUiState(isPrimaryUser = false).disableOemUnlockingVisible.not())+ }++ @Test fun `gestures and migration screens route their actions`() {+ var tried = 0; var skipped = 0+ rule.setContent { GesturesScreen(onTryIt = { tried++ }, onSkip = { skipped++ }) }+ assertEquals(0, tried + skipped)+ }++ @Test fun `migration screen offers restore and skip`() {+ var restores = 0; var skips = 0+ rule.setContent { MigrationScreen(onRestore = { restores++ }, onSkip = { skips++ }) }+ assertEquals(0, restores + skips)+ }++ @Test fun `checkbox row toggles`() {+ var checked: Boolean? = null+ rule.setContent {+ SetupCheckboxRow(checked = false, onCheckedChange = { checked = it }, label = "Turn off OEM unlocking", description = "Recommended")+ }+ rule.onNodeWithText("Turn off OEM unlocking").performClick()+ assertEquals(true, checked)+ }+}