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

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.

主站蜘蛛池模板: 顺平县| 二连浩特市| 修文县| 葫芦岛市| 莲花县| 池州市| 温州市| 汾西县| 安塞县| 达孜县| 平阴县| 鞍山市| 满洲里市| 凤翔县| 高尔夫| 湖南省| 平湖市| 中山市| 晋中市| 上蔡县| 平罗县| 霞浦县| 大关县| 鄱阳县| 江油市| 乌兰浩特市| 大邑县| 青龙| 伊春市| 和田市| 邯郸市| 连云港市| 临武县| 五指山市| 汉中市| 梁河县| 皋兰县| 西乌珠穆沁旗| 平舆县| 潜山县| 海安县|