@RequiresApi(value = 34)
@ExperimentalProjectedApi
public final class ProjectedTestRule implements 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 methods

@NonNull Statement
apply(Statement base, Description description)
final @NonNull List<@NonNull AudioDeviceInfo>

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

final @NonNull Set<@NonNull ProjectedDeviceController.Capability>

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

final @NonNull Lifecycle.State

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

final @NonNull Set<@NonNull ProjectedDisplayController.PresentationMode>

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

final 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.

final boolean

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

final void

Launches a test Activity on the Projected device.

final void

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

final void

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

final void

Updates battery state to the one provided.

final void

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

final void
setDeviceConnected(boolean value)

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

final void

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

final void

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

final void

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

final 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
public ProjectedTestRule()

Public methods

apply

Added in 1.0.0-alpha09
public @NonNull Statement apply(Statement base, Description description)

getAudioDevices

Added in 1.0.0-alpha09
public final @NonNull List<@NonNull AudioDeviceInfogetAudioDevices()

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.

getCapabilities

Added in 1.0.0-alpha09
public final @NonNull Set<@NonNull ProjectedDeviceController.CapabilitygetCapabilities()

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

getLifecycleState

Added in 1.0.0-alpha09
public final @NonNull Lifecycle.State getLifecycleState()

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.

getPresentationModeFlags

Added in 1.0.0-alpha09
public final @NonNull Set<@NonNull ProjectedDisplayController.PresentationModegetPresentationModeFlags()

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

getProjectedLayoutParamFlags

Added in 1.0.0-alpha09
public final int getProjectedLayoutParamFlags()

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.

isDeviceConnected

Added in 1.0.0-alpha09
public final boolean isDeviceConnected()

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

launchTestProjectedDeviceActivity

Added in 1.0.0-alpha09
public final void launchTestProjectedDeviceActivity(
    @NonNull Function1<@NonNull ActivityUnit> block
)

Launches a test Activity on the Projected device.

Parameters
@NonNull Function1<@NonNull ActivityUnit> block

The block to execute on the test activity.

setAudioDevices

Added in 1.0.0-alpha09
public final void setAudioDevices(@NonNull List<@NonNull AudioDeviceInfo> value)

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.

setBatteryState

Added in 1.0.0-alpha09
public final void setBatteryState(@NonNull BatteryState batteryState)

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

setCapabilities

Added in 1.0.0-alpha09
public final void setCapabilities(
    @NonNull Set<@NonNull ProjectedDeviceController.Capability> value
)

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

setDeviceConnected

Added in 1.0.0-alpha09
public final void setDeviceConnected(boolean value)

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

setLifecycleState

Added in 1.0.0-alpha09
public final void setLifecycleState(@NonNull Lifecycle.State value)

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.

setPresentationModeFlags

Added in 1.0.0-alpha09
public final void setPresentationModeFlags(
    @NonNull Set<@NonNull ProjectedDisplayController.PresentationMode> value
)

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

setShouldThrowIllegalStateExceptionWhenCreatingControllers

Added in 1.0.0-alpha09
public final void setShouldThrowIllegalStateExceptionWhenCreatingControllers(
    boolean value
)

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.

shouldThrowIllegalStateExceptionWhenCreatingControllers

Added in 1.0.0-alpha09
public final boolean shouldThrowIllegalStateExceptionWhenCreatingControllers()

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.