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

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.

主站蜘蛛池模板: 汨罗市| 大名县| 祥云县| 栾城县| 南通市| 色达县| 都兰县| 扶沟县| 读书| 高淳县| 长子县| 肃南| 南通市| 榆树市| 丹江口市| 六枝特区| 杂多县| 博白县| 鄂托克前旗| 绥芬河市| 石景山区| 台中市| 宜兰市| 留坝县| 松潘县| 德化县| 丽江市| 巴林左旗| 宁远县| 康乐县| 罗城| 锦屏县| 崇信县| 淮安市| 保定市| 锦屏县| 上犹县| 宁德市| 和平县| 昔阳县| 揭西县|