Android 浮动视图测试

Android testing of the floating view

我给WindowManager添加了一个浮动视图,让它可以在屏幕上移动,当我点击这个视图时我可以执行点击事件,一切正常。

但是,我不知道如何在 espresso 或 UIAutomator 中访问此视图。

向 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 white-blue icon in rect is the floating view i am talking about.

问题

浮动视图响应一个点击事件,并执行一些任务,现在我想在AndroidJunit测试中做这个。

我尝试了 Espresso,使用 onView 方法,但是测试用例:

onView(withContentDescription("bugtags_fab_des_normal")).perform(click());

获取:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with content description: is "bugtags_fab_des_normal"

我尝试使用 UIAutomator Viewer,但在视图层次结构中找不到 floatingView。

如何

如何在 espresso 或 uiautomator 中访问此视图并单击它?

附录

测试用例

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

输出日志

output-log-gist

Bugtags.com

实际上,我正在使用一个名为 bugtags.com 的 sdk,它是一个用于应用程序错误报告和崩溃分析的简单工具。

在您的 floatingView

上添加一个 onClickListener

您的视图在 Activity 之外,因此可以使用 inRoot() 方法找到它:

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

此外,您可能应该将这段代码中的 reportImage 更改为 floatingView

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