emergency: add emulator launch e2e test
- Commit
3008b967310634e17831afb600065d3d312ec91c- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 04:21 UTC (17 days ago)
- Parents
b149510
Emergency had no instrumented launch coverage, unlike clock, camera and notes. This adds the repo-standard single launch-no-crash test: it clears the package data for a clean start, grants every requested runtime permission headlessly, flips the exact-alarm appop the app declares, then launches the app and asserts real first-screen content appears with the app still in front. The app has no launcher entry (it is reached from Settings > Safety & emergency and the lock screen; its only MAIN filter is a Settings first-run suggestion), so the launcher intent cannot resolve it; the exported info host is launched explicitly by component, which lands on the same default read-only view card the emergency-assistance entry opens. On a clean start that card shows 'No emergency information provided.', which the test asserts alongside the absence of the permission-gate title. The SOS countdown is a separate activity and is never started by this test, so launching cannot dial anything. UiDevice text polling is used instead of the Compose test rule, matching the existing launch tests. No production code changes. Verified compile-only (the emulator runs centrally): ./gradlew :emergency:assembleDev :emergency:assembleDevAndroidTest with the devvm JDK/Android SDK environment. BUILD SUCCESSFUL; the dev androidTest APK is produced. Emulator runtime was not run.
Diff
emergency/src/androidTest/java/com/vayunmathur/emergency/ui/LaunchNoCrashTest.kt
diff --git a/emergency/src/androidTest/java/com/vayunmathur/emergency/ui/LaunchNoCrashTest.kt b/emergency/src/androidTest/java/com/vayunmathur/emergency/ui/LaunchNoCrashTest.ktnew file mode 100644index 0000000000..a6417eb3a6--- /dev/null+++ b/emergency/src/androidTest/java/com/vayunmathur/emergency/ui/LaunchNoCrashTest.kt@@ -0,0 +1,136 @@+package com.vayunmathur.emergency.ui++import android.app.UiAutomation+import android.content.ComponentName+import android.content.Intent+import android.content.pm.PackageManager+import androidx.test.ext.junit.runners.AndroidJUnit4+import androidx.test.platform.app.InstrumentationRegistry+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.Assert.assertNotNull+import org.junit.Assert.assertNull+import org.junit.Test+import org.junit.runner.RunWith++/**+ * Launch-no-crash test: clears the app's data, grants every requested permission, and+ * asserts it lands on the emergency-info view card.+ *+ * Emergency has no launcher entry (it is reached from Settings > Safety &+ * emergency and the lock screen; its only MAIN filter is a Settings+ * first-run suggestion), so [getLaunchIntentForPackage] cannot resolve it.+ * The exported info host is launched explicitly instead, which lands on the+ * same default route (the read-only view card) that the emergency-assistance+ * entry opens. The SOS countdown is a separate activity and is never started+ * here: launching the info host cannot dial anything.+ *+ * 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. Text polling works for Compose+ * and non-Compose screens alike.+ */+@RunWith(AndroidJUnit4::class)+class LaunchNoCrashTest {++ @Test+ fun launch_grantsPermissions_showsEmergencyInfo() {+ val instrumentation = InstrumentationRegistry.getInstrumentation()+ val device = UiDevice.getInstance(instrumentation)+ val targetContext = instrumentation.targetContext+ val packageName = targetContext.packageName++ clearAppData(instrumentation.uiAutomation, packageName)++ grantAllRequestedPermissions(instrumentation.uiAutomation, targetContext.packageManager, packageName)++ // No launcher intent exists for this package (see the class doc), so+ // the exported host is addressed by component instead.+ val intent = Intent(Intent.ACTION_MAIN)+ .setComponent(ComponentName(packageName, MAIN_ACTIVITY_CLASS))+ .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)+ targetContext.startActivity(intent)++ // Past any permission wall: the shared gate title never appears and+ // the empty view card is showing (a fresh install has no info yet).+ assertNull(+ "permission gate is showing; grants did not clear it",+ device.wait(Until.findObject(By.text(PERMISSION_GATE_TITLE)), GATE_TIMEOUT_MS),+ )+ assertNotNull(+ "emergency info card never appeared; the app may have crashed on launch",+ device.wait(Until.findObject(By.text(EMPTY_INFO_LABEL)), CONTENT_TIMEOUT_MS),+ )+ // Still alive in front: the process did not crash after painting content.+ assertEquals(packageName, device.currentPackageName)+ }+++ /**+ * Starts from clean app state: wipes the target package's data (which also+ * resets its grants, so this runs before the grant pass).+ */+ private fun clearAppData(uiAutomation: UiAutomation, packageName: String) {+ try {+ uiAutomation.executeShellCommand("pm clear $packageName").close()+ } catch (ignored: SecurityException) {+ // Cannot clear headlessly: the content assertions below report stale state.+ } catch (ignored: IOException) {+ // Shell pipe failed: the content assertions below report it.+ }+ }++ 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)+ }+ for (permission in info.requestedPermissions.orEmpty()) {+ try {+ uiAutomation.executeShellCommand("pm grant $packageName $permission").close()+ } catch (ignored: SecurityException) {+ // Not a headlessly grantable permission: nothing to do.+ } catch (ignored: IOException) {+ // Shell pipe failed: the permission stays ungranted and the+ // 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 EMPTY_INFO_LABEL = "No emergency information provided."+ private const val PERMISSION_GATE_TITLE = "Grant Required Permissions"++ /** The explicit host component: this app ships no launcher activity. */+ private const val MAIN_ACTIVITY_CLASS = "com.vayunmathur.emergency.MainActivity"++ /**+ * Special access this app declares: exact alarms (the SOS schedule)+ * have no pm-grantable permission, but the shell can flip their+ * appops. Ignored where the appop does not exist; the content+ * assertion reports it.+ */+ private val SPECIAL_ACCESS_COMMANDS = arrayOf(+ "appops set %s SCHEDULE_EXACT_ALARM allow",+ )+ }+}