Testing¶
The module dpadrecyclerview-testing
includes useful Espresso helpers for UI tests that require DPAD navigation.
Check the Espresso training guide if you're not familiar with this testing framework.
The official project sample over at Github also contains example UI tests here
Dispatching key events¶
KeyEvents
provides some utility methods for easily pressing keys a certain amount of times.
KeyEvents.click()
KeyEvents.back()
KeyEvents.pressDown(times = 5)
// 50 ms between each key press
KeyEvents.pressUp(times = 5, delay = 50)
Speeding up rate of key events¶
DisableIdleTimeoutRule
is a rule useful for removing any artificial delays produced automatically by UiAutomator when injecting key events into the test application.
ViewActions¶
DpadViewActions
contains the following:
getViewBounds
: returns the bounds of a view in the coordinate-space of the root view of the windowgetRelativeViewBounds
: returns the bounds of a view in the coordinate-space of the parent viewclearFocus
: clears the focus of a view if something else can take focus in its placerequestFocus
: requests focus of a view
DpadRecyclerViewActions
contains the following:
scrollTo
: scrolls to a specific itemView using KeyEventsscrollToHolder
: scrolls to a specific ViewHolder using KeyEventsselectLastPosition
: selects the last position of the adapterselectPosition
: selects a given position or position-subPosition pairselectSubPosition
: selects a given subPosition for the current selected positiongetItemViewBounds
: returns the bounds of the itemView of a ViewHolder at a given position in the coordinate-space of the root view of the windowwaitForAdapterUpdate
: loops the main thread until there's a given amount of adapter updateswaitForIdleScroll
: loops the main thread until theDpadRecyclerView
scroll state is not idleexecute
: perform a generic action on theDpadRecyclerView
Example:
Espresso.onView(withId(R.id.recyclerView))
.perform(DpadRecyclerViewActions.scrollTo<RecyclerView.ViewHolder>(
hasDescendant(withText("Some title"))
))
ViewAssertions¶
DpadRecyclerViewAssertions
:
isFocused
: checks if a ViewHolder at a given position is focusedisSelected
: checks if a ViewHolder at a given position or position-subPosition pair is selected
DpadViewAssertions
:
isFocused
andisNotFocused
: checks if a View is focusedhasFocus
anddoesNotHaveFocus
: checks if a View or one of its descendants has focus
Example: