- Learning Android Application Testing
- Paul Blundell Diego Torres Milano
- 214字
- 2021-07-23 19:58:54
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); }
- 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.
- C++程序設(shè)計(jì)教程
- Learn ECMAScript(Second Edition)
- Apache Oozie Essentials
- R語(yǔ)言數(shù)據(jù)分析從入門(mén)到精通
- 信息可視化的藝術(shù):信息可視化在英國(guó)
- 自己動(dòng)手寫(xiě)Java虛擬機(jī)
- SQL語(yǔ)言從入門(mén)到精通
- GitLab Repository Management
- Python機(jī)器學(xué)習(xí)經(jīng)典實(shí)例
- Spring+Spring MVC+MyBatis整合開(kāi)發(fā)實(shí)戰(zhàn)
- 微信小程序項(xiàng)目開(kāi)發(fā)實(shí)戰(zhàn)
- Java Fundamentals
- C語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn)教程與水平考試指導(dǎo)
- TypeScript圖形渲染實(shí)戰(zhàn):2D架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)
- Getting Started with hapi.js