- 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.
- Hyper-V 2016 Best Practices
- JavaScript高效圖形編程
- Practical Data Science Cookbook(Second Edition)
- 基于Swift語言的iOS App 商業實戰教程
- Learning Network Forensics
- Spring Boot進階:原理、實戰與面試題分析
- Learning Unity 2D Game Development by Example
- Python深度學習:模型、方法與實現
- SQL Server 2008 R2數據庫技術及應用(第3版)
- Nagios Core Administration Cookbook(Second Edition)
- 現代C:概念剖析和編程實踐
- Mastering OAuth 2.0
- HTML5游戲開發實戰
- SAS編程演義
- Internet of Things with Arduino Cookbook