@RequiresApi(value = 34)
@ExperimentalProjectedApi
class ProjectedTestRule : TestRule


Test rule for the Projected clients.

This rule sets up a virtual device and a virtual display using VirtualDeviceManager before each test method and ensures they are properly torn down afterward. This allows testing of components that interact with Projected devices.

Usage:

@RunWith(AndroidJUnit4::class)
class MyProjectedFeatureTest {

@get:Rule val projectedTestRule = ProjectedTestRule()

private val context: ContextWrapper = ApplicationProvider.getApplicationContext()

@Test
fun testFeatureWithProjectedDevice() {
// A Projected device is automatically created before this test.
assertThat(ProjectedContext.createProjectedDeviceContext(context)).isNotNull()
}

@Test
fun testFeatureWithoutProjectedDevice() {
// Manually disconnect the device for this specific test.
projectedTestRule.isDeviceConnected = false
// Test behavior when no projected device is connected.
assertThrows(IllegalStateException::class.java) {
assertThat(ProjectedContext.createProjectedDeviceContext(context)).isNull()
}
}
}

The Projected device is connected by default at the start of each test. The connection state can be controlled via the isDeviceConnected property.

Summary

Public constructors

Public functions

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

Launches a test Activity on the Projected device.

Unit

Emits a ProjectedInputEvent with the given ProjectedInputAction to androidx.xr.projected.ProjectedActivityCompat.projectedInputEvents.

Unit

Updates battery state to the one provided.

Public properties

List<AudioDeviceInfo>

This property can be used to control the Projected audio devices, as returned by androidx.xr.projected.ProjectedDeviceController.audioDevices.

Set<ProjectedDeviceController.Capability>

This property can be used to control the Projected device capabilities.

Boolean

This property can be used to control whether the device is connected or not.

Lifecycle.State

This property can be used to control the lifecycle state of the Projected device.

Set<ProjectedDisplayController.PresentationMode>

This property can be used to control the androidx.xr.projected.ProjectedDisplayController.PresentationMode flags.

Int

Returns the currently set Projected layout param flags, reflecting the state after calls to androidx.xr.projected.ProjectedDisplayController.addLayoutParamsFlags and androidx.xr.projected.ProjectedDisplayController.removeLayoutParamsFlags.

Boolean

Controls whether androidx.xr.projected.ProjectedActivityCompat.create or androidx.xr.projected.ProjectedDeviceController.create throw an IllegalStateException when called.

Public constructors

ProjectedTestRule

Added in 1.0.0-alpha09
ProjectedTestRule()

Public functions

apply

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

launchTestProjectedDeviceActivity

Added in 1.0.0-alpha09
fun launchTestProjectedDeviceActivity(block: (Activity) -> Unit): Unit

Launches a test Activity on the Projected device.

Parameters
block: (Activity) -> Unit

The block to execute on the test activity.

setBatteryState

Added in 1.0.0-alpha09
fun setBatteryState(batteryState: BatteryState): Unit

Updates battery state to the one provided. Calling this function notifies listeners registered using the ProjectedDeviceController.addBatteryStateChangedListener() API.

Public properties

audioDevices

Added in 1.0.0-alpha09
var audioDevicesList<AudioDeviceInfo>

This property can be used to control the Projected audio devices, as returned by androidx.xr.projected.ProjectedDeviceController.audioDevices. By default, the list of audio devices includes a single input and a single output audio device.

capabilities

Added in 1.0.0-alpha09
var capabilitiesSet<ProjectedDeviceController.Capability>

This property can be used to control the Projected device capabilities. By default, the set of capabilities includes the Capability.CAPABILITY_VISUAL_UI.

isDeviceConnected

Added in 1.0.0-alpha09
var isDeviceConnectedBoolean

This property can be used to control whether the device is connected or not. By default, the device is disconnected.

lifecycleState

Added in 1.0.0-alpha09
var lifecycleStateLifecycle.State

This property can be used to control the lifecycle state of the Projected device. By default, the Projected device is in the Lifecycle.State.INITIALIZED state.

presentationModeFlags

Added in 1.0.0-alpha09
var presentationModeFlagsSet<ProjectedDisplayController.PresentationMode>

This property can be used to control the androidx.xr.projected.ProjectedDisplayController.PresentationMode flags. By default, all the presentation mode flags are set.

projectedLayoutParamFlags

Added in 1.0.0-alpha09
val projectedLayoutParamFlagsInt

Returns the currently set Projected layout param flags, reflecting the state after calls to androidx.xr.projected.ProjectedDisplayController.addLayoutParamsFlags and androidx.xr.projected.ProjectedDisplayController.removeLayoutParamsFlags.

shouldThrowIllegalStateExceptionWhenCreatingControllers

Added in 1.0.0-alpha09
var shouldThrowIllegalStateExceptionWhenCreatingControllersBoolean

Controls whether androidx.xr.projected.ProjectedActivityCompat.create or androidx.xr.projected.ProjectedDeviceController.create throw an IllegalStateException when called. By default, the exception is not being thrown.