官术网_书友最值得收藏!

The TouchUtils class

Sometimes, when testing UIs, it is helpful to simulate different kinds of touch events. These touch events can be generated in many different ways, but probably android.test.TouchUtils is the simplest to use. This class provides reusable methods to generate touch events in test cases that are derived from InstrumentationTestCase.

The featured methods allow a simulated interaction with the UI under test. The TouchUtils class provides the infrastructure to inject the events using the correct UI or main thread, so no special handling is needed, and you don't need to annotate the test using @UIThreadTest.

TouchUtils supports the following:

  • Clicking on a View and releasing it
  • Tapping on a View (touching it and quickly releasing)
  • Long-clicking on a View
  • Dragging the screen
  • Dragging Views

The following test represents a typical usage of TouchUtils:

    public void testListScrolling() {
        listView.scrollTo(0, 0);

        TouchUtils.dragQuarterScreenUp(this, activity); 
        int actualItemPosition = listView.getFirstVisiblePosition();

        assertTrue("Wrong position", actualItemPosition > 0);
    }

This test does the following:

  • Repositions the list at the beginning to start from a known condition
  • Scrolls the list
  • Checks for the first visible position to see that it was correctly scrolled

Even the most complex UIs can be tested in that way, and it would help you detect a variety of conditions that could potentially affect the user experience.

主站蜘蛛池模板: 清远市| 新津县| 宁晋县| 临洮县| 周宁县| 简阳市| 辛集市| 河间市| 农安县| 化德县| 镇江市| 崇明县| 方城县| 三门峡市| 封开县| 邹城市| 保山市| 德州市| 鹰潭市| 额济纳旗| 廊坊市| 雅江县| 松江区| 于田县| 郴州市| 新晃| 屯昌县| 莲花县| 和平区| 广饶县| 涿鹿县| 丽江市| 孙吴县| 崇信县| 繁峙县| 德州市| 文化| 盐源县| 高阳县| 卢湾区| 东明县|