page.title=Creating Functional Tests trainingnavtop=true @jd:body
AndroidTestingFun.zip
Functional testing involves verifying that individual application components work together as expected by the user. For example, you can create a functional test to verify that an {@link android.app.Activity} correctly launches a target {@link android.app.Activity} when the user performs a UI interaction.
To create a functional test for your {@link android.app.Activity}, your test class should extend {@link android.test.ActivityInstrumentationTestCase2}. Unlike {@link android.test.ActivityUnitTestCase}, tests in {@link android.test.ActivityInstrumentationTestCase2} can communicate with the Android system and send keyboard input and click events to the UI.
For a complete test case example, take a look at {@code SenderActivityTest.java} in the sample app.
Your functional testing goals might include:
You might implement your test method like this:
@MediumTest
public void testSendMessageToReceiverActivity() {
final Button sendToReceiverButton = (Button)
mSenderActivity.findViewById(R.id.send_message_button);
final EditText senderMessageEditText = (EditText)
mSenderActivity.findViewById(R.id.message_input_edit_text);
// Set up an ActivityMonitor
...
// Send string input value
...
// Validate that ReceiverActivity is started
...
// Validate that ReceiverActivity has the correct data
...
// Remove the ActivityMonitor
...
}
The test waits for an {@link android.app.Activity} that matches this monitor, otherwise returns null after a timeout elapses. If {@code ReceiverActivity} was started, the {@link android.app.Instrumentation.ActivityMonitor ActivityMonitor} that you set up earlier receives a hit. You can use the assertion methods to verify that the {@code ReceiverActivity} is indeed started, and that the hit count on the {@link android.app.Instrumentation.ActivityMonitor ActivityMonitor} incremented as expected.
To monitor a single {@link android.app.Activity} in your application, you can register an {@link android.app.Instrumentation.ActivityMonitor ActivityMonitor}. The {@link android.app.Instrumentation.ActivityMonitor ActivityMonitor} is notified by the system whenever an {@link android.app.Activity} that matches your criteria is started. If a match is found, the monitor’s hit count is updated.
Generally, to use an {@link android.app.Instrumentation.ActivityMonitor ActivityMonitor}, you should:
For example:
// Set up an ActivityMonitor
ActivityMonitor receiverActivityMonitor =
getInstrumentation().addMonitor(ReceiverActivity.class.getName(),
null, false);
// Validate that ReceiverActivity is started
TouchUtils.clickView(this, sendToReceiverButton);
ReceiverActivity receiverActivity = (ReceiverActivity)
receiverActivityMonitor.waitForActivityWithTimeout(TIMEOUT_IN_MS);
assertNotNull("ReceiverActivity is null", receiverActivity);
assertEquals("Monitor for ReceiverActivity has not been called",
1, receiverActivityMonitor.getHits());
assertEquals("Activity is of wrong type",
ReceiverActivity.class, receiverActivity.getClass());
// Remove the ActivityMonitor
getInstrumentation().removeMonitor(receiverActivityMonitor);
If your {@link android.app.Activity} has an {@link android.widget.EditText} field, you might want to test that users can enter values into the {@link android.widget.EditText} object.
Generally, to send a string input value to an {@link android.widget.EditText} object in {@link android.test.ActivityInstrumentationTestCase2}, you should:
For example:
// Send string input value
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
senderMessageEditText.requestFocus();
}
});
getInstrumentation().waitForIdleSync();
getInstrumentation().sendStringSync("Hello Android!");
getInstrumentation().waitForIdleSync();