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

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.

主站蜘蛛池模板: 精河县| 响水县| 横山县| 普宁市| 平邑县| 舒城县| 繁昌县| 溧水县| 营口市| 吉水县| 梅河口市| 鸡东县| 察隅县| 荆州市| 中超| 樟树市| 新闻| 澄迈县| 铁岭县| 郸城县| 南郑县| 澄城县| 乌拉特后旗| 东莞市| 六安市| 中牟县| 榆中县| 牡丹江市| 惠东县| 永川市| 宁明县| 普宁市| 敦化市| 临安市| 延吉市| 芒康县| 南川市| 锡林郭勒盟| 伽师县| 枞阳县| 广河县|