astronomy: rewrite launch test on UiDevice polling
- Commit
a7bc820fa185e8bd0dab49e1624badabccb050bd- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-21 07:32 UTC (1 month ago)
- Parents
8f1d056
Diff
astronomy/src/androidTest/java/com/vayunmathur/astronomy/ui/LaunchNoCrashTest.kt
diff --git a/astronomy/src/androidTest/java/com/vayunmathur/astronomy/ui/LaunchNoCrashTest.kt b/astronomy/src/androidTest/java/com/vayunmathur/astronomy/ui/LaunchNoCrashTest.ktindex 5065346454..0fcb1d3959 100644--- a/astronomy/src/androidTest/java/com/vayunmathur/astronomy/ui/LaunchNoCrashTest.kt+++ b/astronomy/src/androidTest/java/com/vayunmathur/astronomy/ui/LaunchNoCrashTest.kt@@ -1,21 +1,17 @@ package com.vayunmathur.astronomy.ui +import android.app.UiAutomation+import android.content.Intent import android.content.pm.PackageManager-import android.os.Build-import androidx.compose.ui.test.assertCountEquals-import androidx.compose.ui.test.assertIsDisplayed-import androidx.compose.ui.test.junit4.v2.createEmptyComposeRule-import androidx.compose.ui.test.onAllNodesWithText-import androidx.compose.ui.test.onNodeWithContentDescription-import androidx.compose.ui.test.onNodeWithText-import androidx.lifecycle.Lifecycle-import androidx.test.core.app.ActivityScenario import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry-import com.vayunmathur.astronomy.MainActivity+import androidx.test.uiautomator.By+import androidx.test.uiautomator.UiDevice+import androidx.test.uiautomator.Until import java.io.IOException import org.junit.Assert.assertEquals-import org.junit.Rule+import org.junit.Assert.assertNotNull+import org.junit.Assert.assertNull import org.junit.Test import org.junit.runner.RunWith @@ -28,52 +24,54 @@ import org.junit.runner.RunWith * headless `pm grant`s, so the gate clears without touching system UI. The sky * map itself is a canvas with icon-button chrome, so the test keys on the * Search action's content description.+ *+ * UiDevice polling, not compose-test: compose's Espresso idle-sync reflects on+ * hidden InputManager APIs that no longer exist on current releases, so every+ * compose-rule test dies before its first line. Content-description polling+ * works for Compose and non-Compose screens alike. */ @RunWith(AndroidJUnit4::class) class LaunchNoCrashTest { - @get:Rule- val composeRule = createEmptyComposeRule()- @Test fun launch_grantsPermissions_showsSkyMap() {- grantAllRequestedPermissions()-- ActivityScenario.launch(MainActivity::class.java).use { scenario ->- composeRule.waitUntil(timeoutMillis = CONTENT_TIMEOUT_MS) {- try {- composeRule.onNodeWithContentDescription(SEARCH_ACTION).assertExists()- true- } catch (ignored: AssertionError) {- false- }- }- // Past the permission screen: the gate wall is gone and the sky- // map chrome is showing.- composeRule.onAllNodesWithText(PERMISSION_GATE_TITLE).assertCountEquals(0)- composeRule.onNodeWithContentDescription(SEARCH_ACTION).assertIsDisplayed()- // A crash during launch would fail the launch/wait above; the RESUMED- // check makes the no-crash bar explicit.- assertEquals(Lifecycle.State.RESUMED, scenario.state)- }- }-- private fun grantAllRequestedPermissions() { val instrumentation = InstrumentationRegistry.getInstrumentation()+ val device = UiDevice.getInstance(instrumentation) val targetContext = instrumentation.targetContext val packageName = targetContext.packageName- val info = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {- targetContext.packageManager.getPackageInfo(- packageName,- PackageManager.PackageInfoFlags.of(PackageManager.GET_PERMISSIONS.toLong()),- )- } else {- @Suppress("DEPRECATION")- targetContext.packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS)++ grantAllRequestedPermissions(instrumentation.uiAutomation, targetContext.packageManager, packageName)++ val intent = targetContext.packageManager.getLaunchIntentForPackage(packageName)+ assertNotNull("no launcher intent for $packageName", intent)+ intent!!.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)+ targetContext.startActivity(intent)++ // Past the permission screen: the gate wall never appears and the sky+ // map chrome is showing.+ assertNull(+ "permission gate is showing; grants did not clear it",+ device.wait(Until.findObject(By.text(PERMISSION_GATE_TITLE)), GATE_TIMEOUT_MS),+ )+ assertNotNull(+ "sky map never appeared; the app may have crashed on launch",+ device.wait(Until.findObject(By.desc(SEARCH_ACTION)), CONTENT_TIMEOUT_MS),+ )+ // Still alive in front: the process did not crash after painting content.+ assertEquals(packageName, device.currentPackageName)+ }++ private fun grantAllRequestedPermissions(+ uiAutomation: UiAutomation,+ packageManager: PackageManager,+ packageName: String,+ ) {+ val info = try {+ packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS)+ } catch (e: PackageManager.NameNotFoundException) {+ throw AssertionError("package $packageName not installed", e) }- val requested = info.requestedPermissions ?: return- val uiAutomation = instrumentation.uiAutomation- for (permission in requested) {+ for (permission in info.requestedPermissions.orEmpty()) { try { uiAutomation.executeShellCommand("pm grant $packageName $permission").close() } catch (ignored: SecurityException) {@@ -83,11 +81,31 @@ class LaunchNoCrashTest { // content assertions below report it. } }+ for (command in SPECIAL_ACCESS_COMMANDS) {+ try {+ uiAutomation.executeShellCommand(command.format(packageName)).close()+ } catch (ignored: SecurityException) {+ // Appop missing on this release: the gate assertion reports it.+ } catch (ignored: IOException) {+ // Shell pipe failed: the gate assertion below reports it.+ }+ } } private companion object { private const val CONTENT_TIMEOUT_MS = 15_000L+ private const val GATE_TIMEOUT_MS = 5_000L private const val SEARCH_ACTION = "Search" private const val PERMISSION_GATE_TITLE = "Grant Required Permissions"++ /**+ * Special access behind some apps' gates: exact alarms + full-screen intent+ * have no pm-grantable permission, but the shell can flip their appops.+ * Ignored where the appop does not exist; the gate assertion reports it.+ */+ private val SPECIAL_ACCESS_COMMANDS = arrayOf(+ "appops set %s SCHEDULE_EXACT_ALARM allow",+ "appops set %s USE_FULL_SCREEN_INTENT allow",+ ) } }