Wednesday, December 22, 2010

Testing startActivityForResult() and onActivityResult()

Android provides a simple way to invoke an activity and receive a result back from it when it exits using Activity.startActivityForResult(). As of Robolectric 0.9.5, it's easy to put this under test.

Let's say you have one activity which displays the user's name, and a second activity which asks the user for their name:

class NameShowingActivity extends Activity {
  private static final int NAME_RESPONSE = 1;
 
  TextView nameView;
 
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    nameView = new TextView(this);
  }
 
  void askForUserName() {
    startActivityForResult(
        new Intent(this, NameChooserActivity.class), NAME_RESPONSE);
  }
 
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == NAME_RESPONSE) {
      if (resultCode == RESULT_OK) {
        nameView.setText(data.getStringExtra("name"));
      } else if (resultCode == RESULT_CANCELED) {
        nameView.setText("No name given.");
      }
    }
  }
}

Let's test it! The askForName() method launches our name chooser activity, which will eventually respond with the entered name. We want to ensure that the name view has been updated with that name:

@RunWith(RobolectricTestRunner.class)
public class NameShowingActivityTest {
 
  private NameShowingActivity nameShowingActivity;
 
  @Before
  public void setUp() throws Exception {
    nameShowingActivity = new NameShowingActivity();
    nameShowingActivity.onCreate(null);
  }
 
  @Test
  public void shouldFillNameViewFromChooserResponse() throws Exception {
    nameShowingActivity.askForUserName();
 
    shadowOf(nameShowingActivity).receiveResult(
        new Intent(nameShowingActivity, NameChooserActivity.class),
        Activity.RESULT_OK,
        new Intent().putExtra("name", "Joe"));
 
    assertEquals("Joe", nameShowingActivity.nameView.getText());
  }
}

When you call receiveResult() on your activity's shadow, Robolectric notices that the activity is listening for a response from the NameChooserActivity, so it passes the result intent (the second one) to your activity's onActivityResult() method.

But we're not testing the RESULT_CANCELLED branch of our code. Let's test that too:

public void shouldDisplayErrorMessageInNameViewWhenUserCancels() throws Exception {
    nameShowingActivity.askForUserName();
 
    shadowOf(nameShowingActivity).receiveResult(
        new Intent(nameShowingActivity, NameChooserActivity.class),
        Activity.RESULT_CANCELED,
        null);
 
    assertEquals("No name given.", nameShowingActivity.nameView.getText());
  }