简体   繁体   English

在单元测试中,TouchUtils.clickView()在SlidingDrawer Button上不起作用

[英]TouchUtils.clickView() does not work on SlidingDrawer Button in unit tests

I am trying to implemment automated tests for my app. 我正在尝试限制我的应用程序的自动化测试。

I have sliding drawer in view which opens when touched, inside is a button. 我的滑动抽屉在打开时被触摸,里面是一个按钮。 What I want to do is to touch sliding drawer to open it, touch button inside and close sliding drawer touching it again. 我要做的是触摸滑动抽屉以将其打开,触摸内部的按钮,然后再次关闭滑动抽屉以对其进行触摸。

Here is piece of code of my tests ( ActivityInstrumentationTestCase2 ) 这是我的测试代码( ActivityInstrumentationTestCase2

ToggleButton insideBtn = (ToggleButton) mActivity.findViewById(R.id.sliding_btn);
Button drawerBtn = (Button) mActivity.findViewById(R.id.drawer_btn);
TouchUtils.tapView(this,drawerBtn); 
TouchUtils.tapView(this, insideBtn);
TouchUtils.tapView(this, drawerBtn);
...some assertion ...

I have found that this code is sometimes working, sometimes not. 我发现该代码有时有效,有时无效。 The problem is that drawer is not opening after touch causing that insideBtn cannot be touched. 问题是触摸后抽屉没有打开,导致无法触摸insideBtn

So I tried this code: 所以我尝试了这段代码:

ToggleButton insideBtn = (ToggleButton) mActivity.findViewById(R.id.sliding_btn);
Button drawerBtn = (Button) mActivity.findViewById(R.id.drawer_btn);
SlidingDrawer drawer = (SlidingDrawer) mActivity.findViewById(R.id.drawer);
while(!drawer.isOpened()) {
    TouchUtils.tapView(this,drawerBtn);
}   
TouchUtils.tapView(this, insideBtn);
TouchUtils.tapView(this, drawerBtn);
...some assertion ...

But the result is that sometimes when test is working it works at first time so loop is not needed. 但是结果是有时测试工作时它会在第一次运行,因此不需要循环。 But when it is not working loop is working till infinity - none touch cause drawer to be opened. 但是,当它不工作时,回路会一直工作到无穷远-没有触摸会导致抽屉打开。

Do you have any ideas how to write this test to work reliably? 您对如何编写此测试以使其可靠工作有任何想法吗?

I have found the solution. 我找到了解决方案。 The error is in the wrong selection of event used to open SlidingDrawer. 错误在于用于打开SlidingDrawer的事件选择错误。

The Android docs says that SlidingDrawer is a component composed of two children views: the handle, that the users DRAGS ... Android文档说SlidingDrawer是一个由两个子视图组成的组件:句柄,用户DRAGS ...

So there is a need of use TouchUtils.dragView() methods instead of TouchUtils.tapView() 因此,需要使用TouchUtils.dragView()方法代替TouchUtils.tapView()

Here is a working example: 这是一个工作示例:

ToggleButton insideBtn = (ToggleButton) mActivity.findViewById(R.id.sliding_btn);
Button drawerBtn = (Button) mActivity.findViewById(R.id.drawer_btn);
TouchUtils.dragViewToTop(this,drawerBtn); 
TouchUtils.tapView(this, insideBtn);
TouchUtils.dragViewToBottom(this,mActivity,drawerBtn);
...Some assertion...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM