UiController


interface UiController


Provides base-level UI operations (such as injection of MotionEvents) 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

Injects a key event into the application.

Boolean

Injects a motion event into the application.

Boolean

Types a string into the application using series of KeyEvents.

Unit

Loops the main thread for a specified period of time.

Unit

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!

the (non-null!) event to inject

Returns
Boolean

true if the event was injected, false otherwise

Throws
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!

the (non-null!) event to inject

Returns
Boolean

true if the event was injected, false otherwise

Throws
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 KeyEvents. It is up to the implementor to decide how to map the string to KeyEvent objects. If you need specific control over the key events generated use injectKeyEvent.

Parameters
str: String!

the (non-null!) string to type

Returns
Boolean

true if the string was injected, false otherwise

Throws
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

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.