I add a floating view to the WindowManager, and make it movable around the screen, and i can perform click event when i click this view, everything works fine.
However, I don't know how to access this view in espresso or UIAutomator.
final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
type,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT
);
ImageView floatingView = new ImageView(mContext);
floatingView.setContentDescription("bugtags_fab_des_normal");
mWindowManager.addView(floatingView, layoutParams);
the white-blue icon in rect is the floating view i am talking about.
The floating view response a click event, and perform some task, now i want to do this in AndroidJunit test.
I try Espresso, using onView method, but the test case:
onView(withContentDescription("bugtags_fab_des_normal")).perform(click());
Get:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with content description: is "bugtags_fab_des_normal"
I try UIAutomator Viewer, but i can't find the floatingView in view hierarchy.
How can i access this view in espresso or uiautomator and perform click to it?
@Test
public void testInvoke() {
onView(withContentDescription("bugtags_fab_des_normal")).perform(click());
}
output-log-gist
Actually, i am using a sdk called bugtags.com, it's a simple tool for app bug reporting and crash analysis.
Your view is outside of Activity
so can find it by using inRoot()
method:
@Test
public void checkClickOnFloatingButton() {
onView(withContentDescription("bugtags_fab_des_normal")).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).perform(click());
}
Also you probably should change reportImage
to floatingView
in this piece of code:
ImageView floatingView = new ImageView(mContext);
reportImage.setContentDescription("bugtags_fab_des_normal"); // <---`reportImage` to `floatingView`
mWindowManager.addView(floatingView, layoutParams);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With