In addition to JUnit, Android provides an Instrumentation testing framework. You can use Instrumentation to simulate press, lift, screen click, scroll, and other events to effectively control the Activity for automated testing.
Instrumentation is the base class for executing the application instrumentation code. When the application is running, instrumentation is enabled. Instrumentation is initialized before any application is running. It can be used to monitor the interaction between the system and the application.
1. Description in Manifest. xml:
<Instrumentation
Android: name = "android. test. InstrumentationTestRunner"
Android: label = "Tests for My App"
Android: targetPackage = "com. my. test"/>
<Span style = "font-size: 18px;"> <uses-permission android: name = "android. permission. INJECT_EVENTS"/> </span>
2. Analog sending buttons:
/**
* Enter the required key value.
* @ Param keyCode
*/
Private void sendKeyCode (final int keyCode ){
New Thread (){
Public void run (){
Try {
Instrumentation inst = new Instrumentation ();
Inst. sendKeyDownUpSync (keyCode );
} Catch (Exception e ){
Log. e ("Exception when sendPointerSync", e. toString ());
}
}
}. Start ();
}
}
3. Simulate mouse events:
New Thread (){
Public void run (){
Try {
Instrumentation inst = new Instrumentation ();
Inst. sendPointerSync (MotionEvent. obtain (SystemClock. uptimeMillis (),
SystemClock. uptimeMillis (),
MotionEvent. ACTION_DOWN, 240,400, 0 ));
Inst. sendPointerSync (MotionEvent. obtain (SystemClock. uptimeMillis (),
SystemClock. uptimeMillis (),
Motionevents. ACTION_UP, 240,400, 0 ));
} Catch (Exception e ){
Log. e ("Exception when sendPointerSync", e. toString ());
}
}
}. Start ();