@RequiresApi(value = 34)
class AppFunctionTestRule : TestRule


A JUnit TestRule for setting up an environment to exercise AppFunction APIs in unit or Robolectric tests.

Prefer real system-level testing where possible. This rule is intended only for local tests that simulate cross-app interactions via AppFunctions.

Any functions annotated with androidx.appfunctions.service.AppFunction in test code will be automatically registered in this environment during initialization, provided the appfunctions-compiler is applied to the test configuration with the appfunctions:aggregateAppFunctions compiler option set to true.

Example Gradle Setup

dependencies {
...
kspTest("androidx.appfunctions:appfunctions-compiler:xx.xx.xx")
}


ksp {
arg("appfunctions:aggregateAppFunctions", "true")
}

Example: Testing App Functions in the Same App

package com.example.appfunctions

//
Sample functions under test.
class ExampleFunctions {
@AppFunction
suspend fun add(a: Int, b: Int): Int = a + b
}

// Test file.
class ExampleFunctionsTest {
@get:Rule val appFunctionTestRule = AppFunctionTestRule(context)
private val appFunctionManagerCompat = appFunctionTestRule.getAppFunctionManagerCompat()

@Test
fun addFunction_returnsCorrectSum() = runBlocking {
val appFunctionPackageMetadata = appFunctionManagerCompat.observeAppFunctions(
AppFunctionSearchSpec(
packageNames = listOf(packageName),
schemaName = schemaName
)
)
.first()
.single()
val addFunctionMetadata = appFunctionPackageMetadata.appFunctions.single()

val response = appFunctionManagerCompat.executeAppFunction(
ExecuteAppFunctionRequest(...)
)

// assert on returned response.
}
}

Example: Testing App Function Execution in an Agent

package com.example.agent.appfunctions

class
AppFunctionsAgent(
private val appFunctionManagerCompat: AppFunctionManagerCompat
) {
suspend fun executeAppFunction(
packageName: String,
schemaName: String,
params: Any
): AppFunctionData {
val appFunctionPackageMetadata = appFunctionManagerCompat
.observeAppFunctions(
AppFunctionSearchSpec(
packageNames = listOf(packageName),
schemaName = schemaName
)
)
.first()
.single()

val appFunctionMetadata = appFunctionPackageMetadata.appFunctions.single()

val request = ExecuteAppFunctionRequest(...)

val response = appFunctionManagerCompat.executeAppFunction(request)


// return response.
}
}

// Test file.
class TestFunctions {
@AppFunction
fun testFun(parameters: TestParam): TestReturn { ... }
}

class AppFunctionsAgentTest {
@get:Rule val appFunctionTestRule = AppFunctionTestRule(context)
private val appFunctionsAgent = AppFunctionsAgent(
appFunctionTestRule.getAppFunctionManagerCompat()
)

@Test
fun testFun_returnsExpectedResult() = runBlocking {
val response = appFunctionsAgent.executeAppFunction(
context.packageName,
TestFunctionsIds.TEST_FUN_ID,
TestParam()
)

// assert on response.
}
}

Summary

Public constructors

Public functions

open Statement
apply(base: Statement?, description: Description?)
AppFunctionManagerCompat

Returns an AppFunctionManagerCompat instance for interacting with AppFunctions registered via the test rule.

Public constructors

AppFunctionTestRule

Added in 1.0.0-alpha03
AppFunctionTestRule(context: Context)

Public functions

apply

Added in 1.0.0-alpha03
open fun apply(base: Statement?, description: Description?): Statement

getAppFunctionManagerCompat

Added in 1.0.0-alpha03
fun getAppFunctionManagerCompat(): AppFunctionManagerCompat

Returns an AppFunctionManagerCompat instance for interacting with AppFunctions registered via the test rule.