# UiController

Summary:[Methods](https://developer.android.com/reference/android/support/test/espresso/UiController#pubmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/test/espresso/UiController#)  

# UiController

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


interface
UiController
`  
`


`

|--------------------------------------------|
| android.support.test.espresso.UiController |

|---|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/test/espresso/UiController#)Known Indirect Subclasses [IdlingUiController](https://developer.android.com/reference/android/support/test/espresso/base/IdlingUiController),[InterruptableUiController](https://developer.android.com/reference/android/support/test/espresso/base/InterruptableUiController) |-----------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [IdlingUiController](https://developer.android.com/reference/android/support/test/espresso/base/IdlingUiController)               | Similar to[UiController](https://developer.android.com/reference/android/support/test/espresso/UiController)but with one additional method that enables getting an instance of[IdlingResourceRegistry](https://developer.android.com/reference/android/support/test/espresso/base/IdlingResourceRegistry). | | [InterruptableUiController](https://developer.android.com/reference/android/support/test/espresso/base/InterruptableUiController) | Similar to[UiController](https://developer.android.com/reference/android/support/test/espresso/UiController)but with one additional method that enables interrupting Espresso tasks.                                                                                                                       | ||

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

<br />

## Summary

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

## Public methods

### injectKeyEvent

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

Injects a key event into the application.

<br />

|                         Parameters                          ||
|---------|----------------------------------------------------|
| `event` | `KeyEvent`: the (non-null!) event to inject <br /> |

|                              Returns                              ||
|-----------|--------------------------------------------------------|
| `boolean` | true if the event was injected, false otherwise <br /> |

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

### injectMotionEvent

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

Injects a motion event into the application.

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `event` | `MotionEvent`: the (non-null!) event to inject <br /> |

|                              Returns                              ||
|-----------|--------------------------------------------------------|
| `boolean` | true if the event was injected, false otherwise <br /> |

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

### injectString

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

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(KeyEvent)](https://developer.android.com/reference/android/support/test/espresso/UiController#injectKeyEvent(android.view.KeyEvent)).

<br />

|                       Parameters                       ||
|-------|-------------------------------------------------|
| `str` | `String`: the (non-null!) string to type <br /> |

|                              Returns                               ||
|-----------|---------------------------------------------------------|
| `boolean` | true if the string was injected, false otherwise <br /> |

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

### loopMainThreadForAtLeast

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

<br />

|                               Parameters                               ||
|---------------|---------------------------------------------------------|
| `millisDelay` | `long`: time to spend in looping the main thread <br /> |

### loopMainThreadUntilIdle

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

<br />

-

  ## Interfaces

  - [EspressoException](https://developer.android.com/reference/android/support/test/espresso/EspressoException)
  - [FailureHandler](https://developer.android.com/reference/android/support/test/espresso/FailureHandler)
  - [IdlingResource](https://developer.android.com/reference/android/support/test/espresso/IdlingResource)
  - [IdlingResource.ResourceCallback](https://developer.android.com/reference/android/support/test/espresso/IdlingResource.ResourceCallback)
  - [UiController](https://developer.android.com/reference/android/support/test/espresso/UiController)
  - [ViewAction](https://developer.android.com/reference/android/support/test/espresso/ViewAction)
  - [ViewAssertion](https://developer.android.com/reference/android/support/test/espresso/ViewAssertion)
  - [ViewFinder](https://developer.android.com/reference/android/support/test/espresso/ViewFinder)
  - [ViewInteractionComponent](https://developer.android.com/reference/android/support/test/espresso/ViewInteractionComponent)
-

  ## Classes

  - [AmbiguousViewMatcherException.Builder](https://developer.android.com/reference/android/support/test/espresso/AmbiguousViewMatcherException.Builder)
  - [DataInteraction](https://developer.android.com/reference/android/support/test/espresso/DataInteraction)
  - [DataInteraction.DisplayDataMatcher](https://developer.android.com/reference/android/support/test/espresso/DataInteraction.DisplayDataMatcher)
  - [Espresso](https://developer.android.com/reference/android/support/test/espresso/Espresso)
  - [IdlingPolicies](https://developer.android.com/reference/android/support/test/espresso/IdlingPolicies)
  - [IdlingPolicy](https://developer.android.com/reference/android/support/test/espresso/IdlingPolicy)
  - [IdlingRegistry](https://developer.android.com/reference/android/support/test/espresso/IdlingRegistry)
  - [NoMatchingViewException.Builder](https://developer.android.com/reference/android/support/test/espresso/NoMatchingViewException.Builder)
  - [PerformException.Builder](https://developer.android.com/reference/android/support/test/espresso/PerformException.Builder)
  - [Root](https://developer.android.com/reference/android/support/test/espresso/Root)
  - [Root.Builder](https://developer.android.com/reference/android/support/test/espresso/Root.Builder)
  - [ViewInteraction](https://developer.android.com/reference/android/support/test/espresso/ViewInteraction)
-

  ## Exceptions

  - [AmbiguousViewMatcherException](https://developer.android.com/reference/android/support/test/espresso/AmbiguousViewMatcherException)
  - [AppNotIdleException](https://developer.android.com/reference/android/support/test/espresso/AppNotIdleException)
  - [IdlingResourceTimeoutException](https://developer.android.com/reference/android/support/test/espresso/IdlingResourceTimeoutException)
  - [InjectEventSecurityException](https://developer.android.com/reference/android/support/test/espresso/InjectEventSecurityException)
  - [NoActivityResumedException](https://developer.android.com/reference/android/support/test/espresso/NoActivityResumedException)
  - [NoMatchingRootException](https://developer.android.com/reference/android/support/test/espresso/NoMatchingRootException)
  - [NoMatchingViewException](https://developer.android.com/reference/android/support/test/espresso/NoMatchingViewException)
  - [PerformException](https://developer.android.com/reference/android/support/test/espresso/PerformException)