1

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.

Crocodile
  • 5,724
  • 11
  • 41
  • 67
  • 1
    My mistake. I was skimming and forgot about your title. I don't think it's bad practice to repeat your title question in your post's body :) – starball Dec 30 '22 at 23:59
  • 1
    Both Espresso and Ui Automator default to the current window. For Espresso, you could try `inRoot(isSystemAlertWindow())` -- see [the docs](https://developer.android.com/reference/androidx/test/espresso/matcher/RootMatchers) and [this SO answer](https://stackoverflow.com/a/58170225/115145) (which is for a dialog, which is another form of window). Or [try this approach](https://stackoverflow.com/a/39302503/115145). – CommonsWare Dec 31 '22 at 00:02
  • @CommonsWare thank you! I searched so much for this. `onView(withId(R.id.someButton)).inRoot(isSystemAlertWindow()).perform(click())`worked like a charm. – Crocodile Dec 31 '22 at 00:15
  • Glad it worked! You might want to convert that solution into an answer -- I could not find one that referenced `isSystemAlertWindow()`. – CommonsWare Dec 31 '22 at 00:27
  • Sure @CommonsWare I can answer my own question with your solution, unless you meant something else by converting. – Crocodile Dec 31 '22 at 00:32

1 Answers1

1

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

onView(withId(R.id.someButton)).inRoot(isSystemAlertWindow()).perform(click())
Crocodile
  • 5,724
  • 11
  • 41
  • 67