You need ActivityMonitor, it helps you moniotor newly opened activity during instrumentation, check out the pseudo code below:
public void testOpenNextActivity() {
// register next activity that need to be monitored.
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(NextActivity.class.getName(), null, false);
// open current activity.
MyActivity myActivity = getActivity();
final Button button = (Button) myActivity.findViewById(com.company.R.id.open_next_activity);
myActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// click button and open next activity.
button.performClick();
}
});
//Watch for the timeout
//example values 5000 if in ms, or 5 if it's in seconds.
NextActivity nextActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5000);
// next activity is opened and captured.
assertNotNull(nextActivity);
nextActivity .finish();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…