Search code examples
androidoverlayandroid-espressoinstrumented-testtype-application-overlay

How to access application overlay windows from Android tests?


The app I am working on has an application overlay window implemented as floating button. It's defined with type as:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) 
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT else 
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

The problem is that I am unable to access this window from my tests. Neither from Espresso:

onView(withId(R.id.someButton)).perform(click())

or from Instrumented Tests:

device.findObject(
UiSelector().resourceId("com.example.myappname:id/someButton")
).click()

I would like to simulate a simple button press in my tests. The problem is that my code cannot find the views with these code snippets and fails.


Solution

  • Answering my own question based on the answer in the question comments above:

    onView(withId(R.id.someButton)).inRoot(isSystemAlertWindow()).perform(click())