parentalcontrols: add emulator launch e2e test
- Commit
17b904aad6736889df4c2fd08bc0db85ba4f8849- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 04:21 UTC (17 days ago)
- Parents
eed1390
Parental Controls 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 usage-stats and exact-alarm appops the app declares, then launches the app and asserts real first-screen content appears with the app still in front. The app ships no launcher activity by design (it is reached from Settings > Parental controls), so the launcher intent cannot resolve it; the exported navigation host is launched explicitly by component, which lands on the same default dashboard route the Settings entry opens. The dashboard is read-only and ungated (the parent PIN only guards the editors behind it), so the test asserts the permission-gate title and the PIN-setup title are absent and that the dashboard's master switch row (Controls for this phone) appears. 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 :parentalcontrols:assembleDev :parentalcontrols:assembleDevAndroidTest with the devvm JDK/Android SDK environment. BUILD SUCCESSFUL; the dev androidTest APK is produced. Emulator runtime was not run.
Diff
parentalcontrols/src/androidTest/java/com/vayunmathur/parentalcontrols/ui/LaunchNoCrashTest.kt
diff --git a/parentalcontrols/src/androidTest/java/com/vayunmathur/parentalcontrols/ui/LaunchNoCrashTest.kt b/parentalcontrols/src/androidTest/java/com/vayunmathur/parentalcontrols/ui/LaunchNoCrashTest.ktnew file mode 100644index 0000000000..2309edff51--- /dev/null+++ b/parentalcontrols/src/androidTest/java/com/vayunmathur/parentalcontrols/ui/LaunchNoCrashTest.kt@@ -0,0 +1,147 @@+package com.vayunmathur.parentalcontrols.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 parental-controls dashboard.+ *+ * Parental Controls has no launcher entry by design (it is reached from+ * Settings > Parental controls; see the manifest), so [getLaunchIntentForPackage]+ * cannot resolve it. The exported navigation host is launched explicitly+ * instead, which lands on the same default route (the dashboard) that the+ * Settings entry opens.+ *+ * The dashboard itself is read-only and ungated: the parent PIN only guards+ * the editors behind it, so a cold start must show the dashboard, never the+ * PIN setup or PIN gate. PACKAGE_USAGE_STATS is an appop, granted headlessly+ * below alongside the exact-alarm op the bedtime schedule relies on.+ *+ * 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_showsDashboard() {+ 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+ // neither do the PIN setup or PIN gate, which guard edits, not launch.+ assertNull(+ "permission gate is showing; grants did not clear it",+ device.wait(Until.findObject(By.text(PERMISSION_GATE_TITLE)), GATE_TIMEOUT_MS),+ )+ assertNull(+ "PIN setup is showing on a plain launch; the dashboard should be first",+ device.wait(Until.findObject(By.text(PIN_SETUP_TITLE)), GATE_TIMEOUT_MS),+ )+ // The dashboard's master switch row is showing.+ assertNotNull(+ "parental-controls dashboard never appeared; the app may have crashed on launch",+ device.wait(Until.findObject(By.text(CONTROLS_SWITCH_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 CONTROLS_SWITCH_LABEL = "Controls for this phone"+ private const val PERMISSION_GATE_TITLE = "Grant Required Permissions"+ private const val PIN_SETUP_TITLE = "Set parent PIN"++ /** The explicit host component: this app ships no launcher activity. */+ private const val MAIN_ACTIVITY_CLASS = "com.vayunmathur.parentalcontrols.MainActivity"++ /**+ * Special access this app declares: usage stats (daily caps resume+ * mid-day after a reboot) and exact alarms (the bedtime boundary) 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 GET_USAGE_STATS allow",+ "appops set %s SCHEDULE_EXACT_ALARM allow",+ )+ }+}