Skip to content

Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.

5.5 KB
Raw Blame
package com.gotcha.ui

import android.content.Context
import android.content.Intent
import androidx.health.connect.client.PermissionController
import androidx.test.core.app.ApplicationProvider
import com.gotcha.tools.HealthPermissionState
import com.gotcha.tools.HealthTool
import com.gotcha.tools.ToolResult
import com.gotcha.tools.healthConnectManagePermissionsIntent
import com.gotcha.tools.healthConnectPermissionIntent
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

/**
 * Guards the permission routing table (`openSpecialAccess`) against the exact
 * regression it has already had once: a permission row whose toggle has no
 * route and therefore silently does nothing — the Health Connect row before
 * `ToolResult.HEALTH_CONNECT` was routed.
 *
 * Firing an intent needs a device; picking *which* intent to fire does not,
 * which is the point of keeping the table as a `when` that maps markers to
 * intents.
 */
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [30])
class PermissionsRoutingTest {

    private val context: Context = ApplicationProvider.getApplicationContext()
    private val packageName = context.packageName

    @Test
    fun everySpecialMarkerInTheCatalogHasARoute() {
        val markers = allPermissionGroups()
            .flatMap { it.items }
            .mapNotNull { it.specialMarker }
            .toSet()
        assertTrue("catalog must declare at least one special-access marker", markers.isNotEmpty())

        markers.forEach { marker ->
            // A missing case falls through to `else -> null` and the toggle does nothing —
            // exactly the bug this test exists for.
            val intent = openSpecialAccess(context, marker, packageName)
            when (marker) {
                // Env-dependent no-ops: nothing in the test environment can be launched,
                // but the route must still degrade cleanly instead of throwing.
                ToolResult.VPN_CONSENT,
                ToolResult.TERMUX_ACCESS -> Unit
                ToolResult.HEALTH_CONNECT -> assertEquals(
                    "with no provider installed the toggle must steer to the Play listing",
                    "market://details?id=com.google.android.apps.healthdata",
                    intent?.getData()?.toString()
                )
                else -> assertNotNull("marker '$marker' must have a route", intent)
            }
        }
    }

    @Test
    fun everyGrantableCapabilityHasARoute() {
        // A capability's marker is what makes a *hidden* tool actionable: the
        // tool never runs, so ToolExecutor emits the capability's marker instead.
        // A marker with no route here means the deep-link silently does nothing
        // and the user is back to guessing which setting to enable (issue #76).
        val markers = com.gotcha.tools.Capability.entries.mapNotNull { it.permissionMarker }
        assertTrue("at least one capability must be grantable", markers.isNotEmpty())
        markers.forEach { marker ->
            assertNotNull(
                "capability marker '$marker' must have a route",
                openSpecialAccess(context, marker, packageName)
            )
        }
    }

    @Test
    fun healthConnectPermissionScreenIsTheProviderTargetedIntent() {
        // The exact intent the HEALTH_CONNECT route fires when the provider is available:
        // PermissionController.createRequestPermissionResultContract().createIntent(...). The
        // route itself can't be driven to this branch on the JVM (getSdkStatus needs a real
        // Health Connect provider), so pin down the intent it would fire.
        val intent = PermissionController.createRequestPermissionResultContract()
            .createIntent(context, HealthTool.PERMISSIONS)

        assertEquals(
            "the permission request must target the Health Connect provider",
            "com.google.android.apps.healthdata",
            intent.getPackage()
        )
    }

    @Test
    fun refreshIsASafeNoOpWhenHealthConnectIsAbsent() = runTest {
        // Health Connect is not installed under Robolectric; the resume-refresh
        // path added to PermissionsSection must not throw on such devices.
        assertFalse(HealthPermissionState.refresh(context))
    }

    @Test
    fun healthConnectManagePermissionsIntentTargetsThisApp() {
        // The Android 14+ per-app permission manager, which OEM controllers
        // register even when the SDK's RequestMultiplePermissions screen is not
        // resolvable. Must name the app whose permissions are being managed.
        val intent = healthConnectManagePermissionsIntent(context)
        assertEquals(
            "android.health.connect.action.MANAGE_HEALTH_PERMISSIONS",
            intent.action
        )
        assertEquals(context.packageName, intent.getStringExtra(Intent.EXTRA_PACKAGE_NAME))
    }

    @Test
    fun healthConnectPermissionIntentSteersToPlayListingWhenProviderAbsent() {
        // No Health Connect provider under Robolectric: the helper must pick the
        // Play listing, not crash or fire an intent nothing can handle.
        val intent = healthConnectPermissionIntent(context)
        assertNotNull("there must always be a steer, never a crash", intent)
        assertEquals(
            "market://details?id=com.google.android.apps.healthdata",
            intent?.getData()?.toString()
        )
    }
}