ProjectedTestRule
@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 |
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 |
final int |
Returns the currently set Projected layout param flags, reflecting the state after calls to |
final boolean |
This property can be used to control whether the device is connected or not. |
final void |
launchTestProjectedDeviceActivity(Launches a test |
final void |
sendProjectedInputEvent(Emits a |
final void |
setAudioDevices(@NonNull List<@NonNull AudioDeviceInfo> value)This property can be used to control the Projected audio devices, as returned by |
final void |
setBatteryState(@NonNull BatteryState batteryState)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 |
setLifecycleState(@NonNull Lifecycle.State value)This property can be used to control the lifecycle state of the Projected device. |
final void |
This property can be used to control the |
final void |
setShouldThrowIllegalStateExceptionWhenCreatingControllers(Controls whether |
final boolean |
Controls whether |
Public constructors
Public methods
apply
public @NonNull Statement apply(Statement base, Description description)
getAudioDevices
public final @NonNull List<@NonNull AudioDeviceInfo> getAudioDevices()
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
public final @NonNull Set<@NonNull ProjectedDeviceController.Capability> getCapabilities()
This property can be used to control the Projected device capabilities. By default, the set of capabilities includes the Capability.CAPABILITY_VISUAL_UI.
getLifecycleState
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
public final @NonNull Set<@NonNull ProjectedDisplayController.PresentationMode> getPresentationModeFlags()
This property can be used to control the androidx.xr.projected.ProjectedDisplayController.PresentationMode flags. By default, all the presentation mode flags are set.
getProjectedLayoutParamFlags
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
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
public final void launchTestProjectedDeviceActivity(
@NonNull Function1<@NonNull Activity, Unit> block
)
Launches a test Activity on the Projected device.
sendProjectedInputEvent
public final void sendProjectedInputEvent(
@NonNull ProjectedInputEvent.ProjectedInputAction projectedInputAction
)
Emits a ProjectedInputEvent with the given ProjectedInputAction to androidx.xr.projected.ProjectedActivityCompat.projectedInputEvents.
setAudioDevices
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
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
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
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
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
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
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
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.