简体   繁体   中英

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 )

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.

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.

The Android docs says that SlidingDrawer is a component composed of two children views: the handle, that the users DRAGS ...

So there is a need of use TouchUtils.dragView() methods instead of 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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