# UiController

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/test/platform/ui/UiController "View this page in Java")

<br />

```
interface UiController
```

<br />

*** ** * ** ***

Provides base-level UI operations (such as injection of[MotionEvent](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 functions                                 |
|-------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [injectKeyEvent](https://developer.android.com/reference/kotlin/androidx/test/platform/ui/UiController#injectKeyEvent(android.view.KeyEvent))`(event: `[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)`!)` Injects a key event into the application.                                                                                   |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [injectMotionEvent](https://developer.android.com/reference/kotlin/androidx/test/platform/ui/UiController#injectMotionEvent(android.view.MotionEvent))`(event: `[MotionEvent](https://developer.android.com/reference/android/view/MotionEvent.html)`!)` Injects a motion event into the application.                                                                 |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [injectString](https://developer.android.com/reference/kotlin/androidx/test/platform/ui/UiController#injectString(java.lang.String))`(str: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!)` Types a string into the application using series of[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)s. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [loopMainThreadForAtLeast](https://developer.android.com/reference/kotlin/androidx/test/platform/ui/UiController#loopMainThreadForAtLeast(long))`(millisDelay: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Loops the main thread for a specified period of time.                                                                |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)       | [loopMainThreadUntilIdle](https://developer.android.com/reference/kotlin/androidx/test/platform/ui/UiController#loopMainThreadUntilIdle())`()` Loops the main thread until the application goes idle.                                                                                                                                                                 |

## Public functions

### injectKeyEvent

```
fun injectKeyEvent(event: KeyEvent!): Boolean
```

Injects a key event into the application.  

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

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | true if the event was injected, false otherwise |

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

### injectMotionEvent

```
fun injectMotionEvent(event: MotionEvent!): Boolean
```

Injects a motion event into the application.  

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

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | true if the event was injected, false otherwise |

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

### injectString

```
fun injectString(str: String!): Boolean
```

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

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|--------------------------------|
| `str: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`!` | the (non-null!) string to type |

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|--------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | true if the string was injected, false otherwise |

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

### loopMainThreadForAtLeast

```
fun loopMainThreadForAtLeast(millisDelay: Long): Unit
```

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                                          |
|----------------------------------------------------------------------------------------------|------------------------------------------|
| `millisDelay: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | time to spend in looping the main thread |

### loopMainThreadUntilIdle

```
fun loopMainThreadUntilIdle(): Unit
```

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.