kvh
kvh

Reputation: 2158

Android testing of the floating view

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.

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

The Floating View

the white-blue icon in rect is the floating view i am talking about.

floating-view

Question

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

How can i access this view in espresso or uiautomator and perform click to it?

Appendix

Test Case

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

Output log

output-log-gist

Bugtags.com

Actually, i am using a sdk called bugtags.com, it's a simple tool for app bug reporting and crash analysis.

Upvotes: 5

Views: 1741

Answers (2)

Ilya Tretyakov
Ilya Tretyakov

Reputation: 7010

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

Upvotes: 10

Josh.M
Josh.M

Reputation: 103

Add an onClickListener on your floatingView

Upvotes: -1

Related Questions