# UiController

Artifact: [androidx.test:monitor](https://developer.android.com/jetpack/androidx/releases/test) [View Source](https://cs.android.com/search?q=file:androidx/test/platform/ui/UiController.java+class:androidx.test.platform.ui.UiController)

*** ** * ** ***


```
public interface UiController
```

<br />

*** ** * ** ***

Provides base-level UI operations (such as injection of `https://developer.android.com/reference/android/view/MotionEvent.html`s) that can be used to build user actions such as clicks, scrolls, swipes, etc. This replaces parts of the android Instrumentation class that provides similar functionality. However, it provides a more advanced synchronization mechanism for test actions. The key differentiators are:

- test actions are assumed to be called on the main thread
- after a test action is initiated, execution blocks until all messages in the main message queue have been cleared.

## Summary

| ### Public methods |
|---|---|
| `abstract boolean` | `https://developer.android.com/reference/androidx/test/platform/ui/UiController#injectKeyEvent(android.view.KeyEvent)(https://developer.android.com/reference/android/view/KeyEvent.html event)` Injects a key event into the application. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/test/platform/ui/UiController#injectMotionEvent(android.view.MotionEvent)(https://developer.android.com/reference/android/view/MotionEvent.html event)` Injects a motion event into the application. |
| `abstract boolean` | `https://developer.android.com/reference/androidx/test/platform/ui/UiController#injectString(java.lang.String)(https://developer.android.com/reference/java/lang/String.html str)` Types a string into the application using series of `https://developer.android.com/reference/android/view/KeyEvent.html`s. |
| `abstract void` | `https://developer.android.com/reference/androidx/test/platform/ui/UiController#loopMainThreadForAtLeast(long)(long millisDelay)` Loops the main thread for a specified period of time. |
| `abstract void` | `https://developer.android.com/reference/androidx/test/platform/ui/UiController#loopMainThreadUntilIdle()()` Loops the main thread until the application goes idle. |

## Public methods

### injectKeyEvent

```
abstract boolean injectKeyEvent(KeyEvent event)
```

Injects a key event into the application.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/view/KeyEvent.html event` | the (non-null!) event to inject |

| Returns |
|---|---|
| `boolean` | true if the event was injected, false otherwise |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/test/platform/ui/InjectEventSecurityException` | if the event couldn't be injected because it would interact with another application. |

### injectMotionEvent

```
abstract boolean injectMotionEvent(MotionEvent event)
```

Injects a motion event into the application.

| Parameters |
|---|---|
| `https://developer.android.com/reference/android/view/MotionEvent.html event` | the (non-null!) event to inject |

| Returns |
|---|---|
| `boolean` | true if the event was injected, false otherwise |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/test/platform/ui/InjectEventSecurityException` | if the event couldn't be injected because it would interact with another application. |

### injectString

```
abstract boolean injectString(String str)
```

Types a string into the application using series of `https://developer.android.com/reference/android/view/KeyEvent.html`s. It is up to the implementor to decide how to map the string to `https://developer.android.com/reference/android/view/KeyEvent.html` objects. If you need specific control over the key events generated use `https://developer.android.com/reference/androidx/test/platform/ui/UiController#injectKeyEvent(android.view.KeyEvent)`.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html str` | the (non-null!) string to type |

| Returns |
|---|---|
| `boolean` | true if the string was injected, false otherwise |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/test/platform/ui/InjectEventSecurityException` | if the events couldn't be injected because it would interact with another application. |

### loopMainThreadForAtLeast

```
abstract void loopMainThreadForAtLeast(long millisDelay)
```

Loops the main thread for a specified period of time.

Control may not return immediately, instead it'll return after the provided delay has passed and the queue is in an idle state again.

| Parameters |
|---|---|
| `long millisDelay` | time to spend in looping the main thread |

### loopMainThreadUntilIdle

```
abstract void loopMainThreadUntilIdle()
```

Loops the main thread until the application goes idle.

An empty task is immediately inserted into the task queue to ensure that if we're idle at this moment we'll return instantly.