Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/agent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,22 @@ jobs:
working-directory: agent

- name: Run instrumentation tests
id: instrumentation-tests
run: |
./gradlew :app:pixel7Api37StandardDebugAndroidTest -PGEMINI_API_KEY=dummy_key \
-Pandroid.experimental.testOptions.managedDevices.emulator.showKernelLogging=true \
-Pandroid.testoptions.manageddevices.emulator.gpu="swiftshader_indirect"
working-directory: agent

- name: Upload instrumentation test results
if: failure() && steps.instrumentation-tests.outcome == 'failure'
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: agent-instrumentation-test-results
path: |
agent/app/build/outputs/androidTest-results/managedDevice
agent/app/build/reports/androidTests/managedDevice

- name: Build Debug and Release APKs
run: ./gradlew assembleDebug assembleRelease -PGEMINI_API_KEY=dummy_key
working-directory: agent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright 2026 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.appfunctions.agent

import android.content.Context
import android.os.ParcelFileDescriptor
import androidx.appfunctions.AppFunctionManager
import androidx.appfunctions.AppFunctionName
import androidx.appfunctions.AppFunctionSearchSpec
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import com.example.appfunctions.agent.data.FakeAppFunctionService
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import org.junit.Assert.fail
import org.junit.rules.ExternalResource
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeSource

/**
* Puts the agent's own [FakeAppFunctionService.FUNCTION_ID_FAKE_FUNCTION] in a known state for an
* instrumented test.
*
* Before each test, it adopts the shell identity that reading and executing app functions needs,
* waits until the freshly installed app's functions are indexed (through the same APIs the tests
* call) and resets the function to its default, enabled state. After each test, it resets the
* state again, so a test that disables the function can't affect the next one, and drops the
* shell identity.
*/
class FakeAppFunctionRule : ExternalResource() {
val context: Context = ApplicationProvider.getApplicationContext()
val appFunctionManager: AppFunctionManager =
checkNotNull(AppFunctionManager.getInstance(context)) {
"AppFunctionManager is not available on this device"
}
val functionName =
AppFunctionName(context.packageName, FakeAppFunctionService.FUNCTION_ID_FAKE_FUNCTION)

private val uiAutomation
get() = InstrumentationRegistry.getInstrumentation().uiAutomation

override fun before() {
uiAutomation.adoptShellPermissionIdentity(EXECUTE_APP_FUNCTIONS)
try {
runBlocking {
awaitUntil("fakeFunction to be indexed", INDEXING_TIMEOUT, ::describeIndexedFunctions) {
val functions =
appFunctionManager.searchAppFunctions(
AppFunctionSearchSpec(packageNames = setOf(context.packageName)),
)
functions.any { it.name == functionName } &&
appFunctionManager.getAppFunctionStates(listOf(functionName)).isNotEmpty()
}
setEnabledState(AppFunctionManager.APP_FUNCTION_STATE_DEFAULT)
awaitUntil("fakeFunction to be enabled") {
appFunctionManager.getAppFunctionStates(listOf(functionName)).single().isEnabled
}
}
} catch (e: Throwable) {
// after() doesn't run when before() fails.
uiAutomation.dropShellPermissionIdentity()
throw e
}
}

override fun after() {
try {
runBlocking { setEnabledState(AppFunctionManager.APP_FUNCTION_STATE_DEFAULT) }
} finally {
uiAutomation.dropShellPermissionIdentity()
}
}

/** Enables or disables the fake function at runtime, as an app does with its own functions. */
suspend fun setEnabled(enabled: Boolean) {
setEnabledState(
if (enabled) {
AppFunctionManager.APP_FUNCTION_STATE_ENABLED
} else {
AppFunctionManager.APP_FUNCTION_STATE_DISABLED
},
)
}

/**
* Enables or disables the fake function, as [setEnabled] does, and returns the result of
* [awaitChange], which waits until the code under test shows the change. Fails the test after
* [timeout].
*
* A flow built on [AppFunctionManager.observeAppFunctions] registers its platform observer only
* when it is collected, and the observer isn't notified of earlier changes.
* `GetAppFunctionsUseCase` emits a first result before that, so a change made right after that
* result can be missed. Until [awaitChange] returns, the function is therefore switched to the
* other state and back every [RETOGGLE_INTERVAL].
*/
suspend fun <T> setEnabledAndAwait(
enabled: Boolean,
timeout: Duration = 30.seconds,
awaitChange: suspend () -> T,
): T =
coroutineScope {
setEnabled(enabled)
val retoggling =
launch {
while (true) {
delay(RETOGGLE_INTERVAL)
// Not cancellable, so that the function is always left in the requested state.
withContext(NonCancellable) {
setEnabled(!enabled)
setEnabled(enabled)
}
}
}
try {
withTimeout(timeout) { awaitChange() }
} finally {
retoggling.cancelAndJoin()
}
}

private suspend fun setEnabledState(state: Int) {
appFunctionManager.setAppFunctionEnabled(functionName.functionIdentifier, state)
}

private fun describeIndexedFunctions(): String {
val command = "cmd app_function list-app-functions --package ${context.packageName}"
val output =
ParcelFileDescriptor.AutoCloseInputStream(uiAutomation.executeShellCommand(command))
.bufferedReader()
.use { it.readText() }
return "Output of '$command': $output"
}

private companion object {
const val EXECUTE_APP_FUNCTIONS = "android.permission.EXECUTE_APP_FUNCTIONS"

/** Indexing a freshly installed app can take a while on a slow emulator. */
val INDEXING_TIMEOUT = 60.seconds

/**
* How long a state change may take to show before it is made again. Long enough for the
* observer's notification, the use case's debounce and its queries; making the change again
* while it is still on its way does no harm.
*/
val RETOGGLE_INTERVAL = 5.seconds
}
}

/**
* Polls [condition] until it is true. Fails the test after [timeout], with [describeState] and the
* last error thrown by [condition], if any.
*/
suspend fun awaitUntil(
description: String,
timeout: Duration = 30.seconds,
describeState: () -> String = { "" },
condition: suspend () -> Boolean,
) {
val start = TimeSource.Monotonic.markNow()
var lastError: Exception? = null
while (true) {
val isMet =
try {
condition()
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
lastError = e
false
}
if (isMet) return
if (start.elapsedNow() > timeout) {
fail("Timed out after $timeout waiting for $description. Last error: $lastError. ${describeState()}")
}
delay(500.milliseconds)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,73 +15,56 @@
*/
package com.example.appfunctions.agent.domain

import android.content.Context
import android.os.ParcelFileDescriptor
import androidx.appfunctions.AppFunctionManager
import androidx.appfunctions.AppFunctionName
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.example.appfunctions.agent.data.FakeAppFunctionService
import com.example.appfunctions.agent.FakeAppFunctionRule
import com.example.appfunctions.agent.awaitUntil
import com.example.appfunctions.agent.domain.appfunction.GetAppFunctionStatesUseCase
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class GetAppFunctionStatesUseCaseTest {
private lateinit var context: Context
private lateinit var appFunctionManager: AppFunctionManager
private lateinit var useCase: GetAppFunctionStatesUseCase
@get:Rule
val fakeFunction = FakeAppFunctionRule()

@Before
fun setup() {
context = ApplicationProvider.getApplicationContext()
appFunctionManager = AppFunctionManager.getInstance(context)!!
useCase = GetAppFunctionStatesUseCase(appFunctionManager)
private val useCase by lazy { GetAppFunctionStatesUseCase(fakeFunction.appFunctionManager) }

// Adopt shell permission identity
val uiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation
uiAutomation.adoptShellPermissionIdentity("android.permission.EXECUTE_APP_FUNCTIONS")
@Test
fun invoke_returnsFakeFunctionState() =
runBlocking {
val result = useCase(listOf(fakeFunction.functionName))

val found = result.find { it.functionName == fakeFunction.functionName }
assertNotNull("Should find fakeFunction from ${fakeFunction.functionName}", found)
assertTrue("The function should be enabled", checkNotNull(found).isEnabled)
}

waitForFakeFunctionIndexed()
}
@Test
fun invoke_functionDisabledAtRuntime_returnsDisabledState() =
runBlocking {
fakeFunction.setEnabled(false)

private fun waitForFakeFunctionIndexed() {
val uiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation
val command = "cmd app_function list-app-functions --package ${context.packageName}"
var output = ""
val timeoutMs = 10_000L
val startTime = System.currentTimeMillis()
while (System.currentTimeMillis() - startTime < timeoutMs) {
output =
ParcelFileDescriptor.AutoCloseInputStream(
uiAutomation.executeShellCommand(command),
).bufferedReader().use { it.readText() }
if (output.contains("fakeFunction")) {
return
awaitUntil("the use case to report fakeFunction as disabled") {
!useCase(listOf(fakeFunction.functionName)).single().isEnabled
}
Thread.sleep(500)
}
fail("Timed out waiting for fakeFunction to be indexed. Output of '$command': $output")
}

@Test
fun invoke_returnsFakeFunctionState() =
fun invoke_functionReEnabledAtRuntime_returnsEnabledState() =
runBlocking {
val targetFunctionName =
AppFunctionName(
context.packageName,
FakeAppFunctionService.FUNCTION_ID_FAKE_FUNCTION,
)
fakeFunction.setEnabled(false)
awaitUntil("the use case to report fakeFunction as disabled") {
!useCase(listOf(fakeFunction.functionName)).single().isEnabled
}

val result = useCase(listOf(targetFunctionName))
fakeFunction.setEnabled(true)

val found = result.find { it.functionName == targetFunctionName }
assertTrue("Should find fakeFunction from $targetFunctionName", found != null)
assertTrue("The function should be enabled", checkNotNull(found).isEnabled)
awaitUntil("the use case to report fakeFunction as enabled") {
useCase(listOf(fakeFunction.functionName)).single().isEnabled
}
}
}
Loading
Loading