Modern Apps git

← Modern-Apps.git

euicc: add emulator launch e2e test

Commit
f4f355c15a96c98746338480f3651288b8ca5dee
Author
Vayun Mathur <[email protected]>
When
2026-11-19 04:21 UTC (17 days ago)
Parents
2699e86
Add the module's only emulator e2e test, LaunchNoCrashTest, following
the repo UiDevice-polling pattern: clear the package data for a clean
start, grant every requested permission (plus the standard special
appops), launch, assert the "Grant Required Permissions" gate title
never appears, assert first-screen content, and assert the app stays
in front.

Euicc has no launcher intent by design: Settings reaches it through
LuiActivity, which is guarded by BIND_EUICC_SERVICE so only the
platform can call it. The test therefore uses the narrowest
deterministic direct entry, the permission-free exported
AddEsimActivity launched explicitly with no activation code, which
the app itself routes to Route.AddSim. Its "Scan QR code" action
(R.string.scan_qr_button) is asserted. That screen paints without
touching the eUICC, so no SIM/eSIM hardware is required on an
emulator. Privileged LPA permissions stay inert when sideloaded.

Verified compile only, no emulator run:
./gradlew :euicc:assembleDev :euicc:assembleDevAndroidTest
(with the prescribed JDK/Android SDK/proxy environment) passed.
No production code, config, lint or detekt changes.

Diff

euicc/src/androidTest/java/com/vayunmathur/euicc/ui/LaunchNoCrashTest.kt
diff --git a/euicc/src/androidTest/java/com/vayunmathur/euicc/ui/LaunchNoCrashTest.kt b/euicc/src/androidTest/java/com/vayunmathur/euicc/ui/LaunchNoCrashTest.ktnew file mode 100644index 0000000000..fa1f4ff0d0--- /dev/null+++ b/euicc/src/androidTest/java/com/vayunmathur/euicc/ui/LaunchNoCrashTest.kt@@ -0,0 +1,136 @@+package com.vayunmathur.euicc.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: launches the app, grants every requested permission, and+ * asserts it lands on real content rather than sitting on a permission screen.+ *+ * Euicc has no launcher intent by design: the platform opens it from Settings+ * through LuiActivity, which is guarded by BIND_EUICC_SERVICE, so only the+ * platform (not a test, and not the launcher) can reach it. The narrowest+ * deterministic direct entry is therefore the permission-free, exported+ * AddEsimActivity launched explicitly with no activation code: the app itself+ * routes a missing or malformed code to Route.AddSim (see AddEsimActivity), so+ * the test deterministically lands on the Add SIM screen. That screen paints+ * without touching the eUICC, which is the right emulator expectation: an+ * emulator has no eSIM hardware or profiles, and this entry requires neither.+ * Privileged LPA permissions (WRITE_EMBEDDED_SUBSCRIPTIONS and friends) are+ * inert when sideloaded, so the grant pass only clears the runtime ones.+ *+ * 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_showsAddSimWithoutCrash() {+        val instrumentation = InstrumentationRegistry.getInstrumentation()+        val device = UiDevice.getInstance(instrumentation)+        val targetContext = instrumentation.targetContext+        val packageName = targetContext.packageName++        // Clean app state first: every run starts from the fresh-install+        // first screen this test asserts, not whatever a previous run left.+        clearAppData(instrumentation.uiAutomation, packageName)++        grantAllRequestedPermissions(instrumentation.uiAutomation, targetContext.packageManager, packageName)++        // No launcher intent exists for this package (see class doc): launch the+        // permission-free AddEsimActivity explicitly, with no code in the intent.+        val intent = Intent()+            .setComponent(ComponentName(packageName, ADD_ESIM_ACTIVITY))+            .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+        // Add SIM screen's scan action 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(+            "add-sim content never appeared; the app may have crashed on launch",+            device.wait(Until.findObject(By.text(SCAN_QR_BUTTON)), CONTENT_TIMEOUT_MS),+        )+        // Still alive in front: the process did not crash after painting content.+        assertEquals(packageName, device.currentPackageName)+    }++    private fun clearAppData(uiAutomation: UiAutomation, packageName: String) {+        try {+            uiAutomation.executeShellCommand("pm clear $packageName").close()+        } catch (ignored: SecurityException) {+            // Shell cannot clear the package: the content assertions below+            // report whatever state the app actually starts in.+        } catch (ignored: IOException) {+            // Shell pipe failed: same reporting path as above.+        }+    }++    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 SCAN_QR_BUTTON = "Scan QR code"+        private const val PERMISSION_GATE_TITLE = "Grant Required Permissions"+        private const val ADD_ESIM_ACTIVITY = "com.vayunmathur.euicc.platform.AddEsimActivity"++        /**+         * 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",+        )+    }+}