# ProjectedTestRule

Artifact: [androidx.xr.projected:projected-testing](https://developer.android.com/jetpack/androidx/releases/xr-projected) [View Source](https://cs.android.com/search?q=file:androidx/xr/projected/testing/ProjectedTestRule.kt+class:androidx.xr.projected.testing.ProjectedTestRule) Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

*** ** * ** ***


```
@RequiresApi(value = 34)
@ExperimentalProjectedApi
public final class ProjectedTestRule implements TestRule
```

<br />

*** ** * ** ***

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 `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#isDeviceConnected()` property.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#ProjectedTestRule()()` |

| ### Public methods |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description)(https://junit.org/junit4/javadoc/latest/org/junit/runners/model/Statement.html base, https://junit.org/junit4/javadoc/latest/org/junit/runner/Description.html description)` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/media/AudioDeviceInfo.html>` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#getAudioDevices()()` This property can be used to control the Projected audio devices, as returned by `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController#audioDevices()`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController.Capability>` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#getCapabilities()()` This property can be used to control the Projected device capabilities. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#getLifecycleState()()` This property can be used to control the lifecycle state of the Projected device. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController.PresentationMode>` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#getPresentationModeFlags()()` This property can be used to control the `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController.PresentationMode` flags. |
| `final int` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#getProjectedLayoutParamFlags()()` Returns the currently set Projected layout param flags, reflecting the state after calls to `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController#addLayoutParamsFlags(kotlin.Int)` and `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController#removeLayoutParamsFlags(kotlin.Int)`. |
| `final https://developer.android.com/reference/android/app/PendingIntent.html` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#getRegisteredInputReceiver()()` Retrieves the `https://developer.android.com/reference/android/app/PendingIntent.html` most recently registered by the application as the input receiver, or null if none is registered. |
| `final boolean` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#isDeviceConnected()()` This property can be used to control whether the device is connected or not. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#launchTestProjectedDeviceActivity(kotlin.Function1)( @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block )` Launches a test `https://developer.android.com/reference/android/app/Activity.html` on the Projected device. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#sendProjectedInputEvent(androidx.xr.projected.ProjectedInputEvent.ProjectedInputAction)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedInputEvent.ProjectedInputAction projectedInputAction )` Emits a with the given `https://developer.android.com/reference/androidx/xr/projected/ProjectedInputEvent.ProjectedInputAction` to `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#projectedInputEvents()`. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setAudioDevices(kotlin.collections.List)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/media/AudioDeviceInfo.html> value)` This property can be used to control the Projected audio devices, as returned by `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController#audioDevices()`. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setBatteryState(androidx.xr.projected.BatteryState)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/BatteryState batteryState)` Updates battery state to the one provided. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setCapabilities(kotlin.collections.Set)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController.Capability> value )` This property can be used to control the Projected device capabilities. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setDeviceConnected(kotlin.Boolean)(boolean value)` This property can be used to control whether the device is connected or not. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setLifecycleState(androidx.lifecycle.Lifecycle.State)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State value)` This property can be used to control the lifecycle state of the Projected device. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setPresentationModeFlags(kotlin.collections.Set)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController.PresentationMode> value )` This property can be used to control the `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController.PresentationMode` flags. |
| `final void` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#setShouldThrowIllegalStateExceptionWhenCreatingControllers(kotlin.Boolean)( boolean value )` Controls whether `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context)` or `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController#create(android.content.Context)` throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` when called. |
| `final boolean` | `https://developer.android.com/reference/androidx/xr/projected/testing/ProjectedTestRule#shouldThrowIllegalStateExceptionWhenCreatingControllers()()` Controls whether `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context)` or `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController#create(android.content.Context)` throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` when called. |

## Public constructors

### ProjectedTestRule

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public ProjectedTestRule()
```

## Public methods

### apply

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public @NonNull Statement apply(Statement base, Description description)
```

### getAudioDevices

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final @NonNull List<@NonNull AudioDeviceInfo> getAudioDevices()
```

This property can be used to control the Projected audio devices, as returned by `https://developer.android.com/reference/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-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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 `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController.Capability#CAPABILITY_VISUAL_UI()`.

### getLifecycleState

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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 `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#INITIALIZED` state.

### getPresentationModeFlags

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final @NonNull Set<@NonNull ProjectedDisplayController.PresentationMode> getPresentationModeFlags()
```

This property can be used to control the `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController.PresentationMode` flags. By default, all the presentation mode flags are set.

### getProjectedLayoutParamFlags

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final int getProjectedLayoutParamFlags()
```

Returns the currently set Projected layout param flags, reflecting the state after calls to `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController#addLayoutParamsFlags(kotlin.Int)` and `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController#removeLayoutParamsFlags(kotlin.Int)`.

### getRegisteredInputReceiver

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final PendingIntent getRegisteredInputReceiver()
```

Retrieves the `https://developer.android.com/reference/android/app/PendingIntent.html` most recently registered by the application as the input receiver, or null if none is registered.

Reflects the current state resulting from calls to `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#setActivityAsInputReceiver(android.app.PendingIntent)` and `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#clearActivityAsInputReceiver()`.

### isDeviceConnected

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final void launchTestProjectedDeviceActivity(
    @NonNull Function1<@NonNull Activity, Unit> block
)
```

Launches a test `https://developer.android.com/reference/android/app/Activity.html` on the Projected device.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/app/Activity.html, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> block` | The block to execute on the test activity. |

### sendProjectedInputEvent

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final void sendProjectedInputEvent(
    @NonNull ProjectedInputEvent.ProjectedInputAction projectedInputAction
)
```

Emits a with the given `https://developer.android.com/reference/androidx/xr/projected/ProjectedInputEvent.ProjectedInputAction` to `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#projectedInputEvents()`.

### setAudioDevices

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final void setAudioDevices(@NonNull List<@NonNull AudioDeviceInfo> value)
```

This property can be used to control the Projected audio devices, as returned by `https://developer.android.com/reference/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-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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 `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController.Capability#CAPABILITY_VISUAL_UI()`.

### setDeviceConnected

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
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 `https://developer.android.com/reference/androidx/lifecycle/Lifecycle.State#INITIALIZED` state.

### setPresentationModeFlags

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final void setPresentationModeFlags(
    @NonNull Set<@NonNull ProjectedDisplayController.PresentationMode> value
)
```

This property can be used to control the `https://developer.android.com/reference/androidx/xr/projected/ProjectedDisplayController.PresentationMode` flags. By default, all the presentation mode flags are set.

### setShouldThrowIllegalStateExceptionWhenCreatingControllers

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final void setShouldThrowIllegalStateExceptionWhenCreatingControllers(
    boolean value
)
```

Controls whether `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context)` or `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController#create(android.content.Context)` throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` when called. By default, the exception is not being thrown.

### shouldThrowIllegalStateExceptionWhenCreatingControllers

Added in [1.0.0-alpha14](https://developer.android.com/jetpack/androidx/releases/xr-projected#1.0.0-alpha14)

```
public final boolean shouldThrowIllegalStateExceptionWhenCreatingControllers()
```

Controls whether `https://developer.android.com/reference/androidx/xr/projected/ProjectedActivityCompat#create(android.content.Context)` or `https://developer.android.com/reference/androidx/xr/projected/ProjectedDeviceController#create(android.content.Context)` throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html` when called. By default, the exception is not being thrown.