Android Floating View Testing

I add a floating view to the WindowManager and make it move around the screen, and I can execute the click event, when I click this view, everything works fine.

However, I do not know how to access this view in espresso or UIAutomator .

Add View to WindowManager

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); 

Floating view

the blue and white icon in the rectangle is the floating view I'm talking about.

floating view

Question

A floating view responds to a click event and performs some task, now I want to do this in an AndroidJunit test.

  • Espresso

I am trying to use Espresso using the onView method, but a 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" 
  • UIAutomator

I am trying UIAutomator Viewer but I cannot find viewView hierarchy in view.

how

How can I access this view in espresso or uiautomator and press it?

application

Test case

 @Test public void testInvoke() { onView(withContentDescription("bugtags_fab_des_normal")).perform(click()); } 

Output log

output log

Bugtags.com

In fact, I am using sdk called bugtags.com , it is a simple tool for reporting application errors and analyzing crashes.

+5
source share
2 answers

Your view is outside the Activity , so it can be found using the inRoot() method:

 @Test public void checkClickOnFloatingButton() { onView(withContentDescription("bugtags_fab_des_normal")).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).perform(click()); } 

Also, you should probably change reportImage to floatingView in this code snippet:

 ImageView floatingView = new ImageView(mContext); reportImage.setContentDescription("bugtags_fab_des_normal"); // <---`reportImage` to `floatingView` mWindowManager.addView(floatingView, layoutParams); 
+8
source

Add onClickListener to your floatingView

-1
source

Source: https://habr.com/ru/post/1240661/


All Articles