Merge "Fixed startActivity() Bug in LaunchActivityTest"

This commit is contained in:
Stephan Linzner
2013-07-15 11:23:37 +00:00
committed by Android (Google) Code Review
2 changed files with 22 additions and 15 deletions

View File

@@ -41,6 +41,7 @@ public class LaunchActivity extends Activity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
startActivity(NextActivity.makeIntent(LaunchActivity.this, STRING_PAYLOAD)); startActivity(NextActivity.makeIntent(LaunchActivity.this, STRING_PAYLOAD));
finish();
} }
}); });
} }

View File

@@ -29,8 +29,7 @@ import android.widget.Button;
*/ */
public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> { public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
private LaunchActivity mLaunchActivity; private Intent mLaunchIntent;
private Button mLaunchNextButton;
public LaunchActivityTest() { public LaunchActivityTest() {
super(LaunchActivity.class); super(LaunchActivity.class);
@@ -41,13 +40,8 @@ public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
protected void setUp() throws Exception { protected void setUp() throws Exception {
super.setUp(); super.setUp();
//Create an intent to launch target Activity //Create an intent to launch target Activity
final Intent intent = new Intent(getInstrumentation().getTargetContext(), mLaunchIntent = new Intent(getInstrumentation().getTargetContext(),
LaunchActivity.class); LaunchActivity.class);
//Start the activity under test in isolation, without values for savedInstanceState and
//lastNonConfigurationInstance
mLaunchActivity = startActivity(intent, null, null);
mLaunchNextButton = (Button) mLaunchActivity.findViewById(R.id.launch_next_activity_button);
} }
/** /**
@@ -55,33 +49,45 @@ public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
*/ */
@MediumTest @MediumTest
public void testPreconditions() { public void testPreconditions() {
assertNotNull("mLaunchActivity is null", mLaunchActivity); //Start the activity under test in isolation, without values for savedInstanceState and
assertNotNull("mLaunchNextButton is null", mLaunchNextButton); //lastNonConfigurationInstance
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
assertNotNull("mLaunchActivity is null", getActivity());
assertNotNull("mLaunchNextButton is null", launchNextButton);
} }
@MediumTest @MediumTest
public void testLaunchNextActivityButton_labelText() { public void testLaunchNextActivityButton_labelText() {
final String expectedButtonText = mLaunchActivity.getString(R.string.label_launch_next); startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
final String expectedButtonText = getActivity().getString(R.string.label_launch_next);
assertEquals("Unexpected button label text", expectedButtonText, assertEquals("Unexpected button label text", expectedButtonText,
mLaunchNextButton.getText()); launchNextButton.getText());
} }
@MediumTest @MediumTest
public void testNextActivityWasLaunchedWithIntent() { public void testNextActivityWasLaunchedWithIntent() {
startActivity(mLaunchIntent, null, null);
final Button launchNextButton = (Button) getActivity().findViewById(R.id.launch_next_activity_button);
//Because this is an isolated ActivityUnitTestCase we have to directly click the //Because this is an isolated ActivityUnitTestCase we have to directly click the
//button from code //button from code
mLaunchNextButton.performClick(); launchNextButton.performClick();
// Get the intent for the next started activity // Get the intent for the next started activity
final Intent launchIntent = getStartedActivityIntent(); final Intent launchIntent = getStartedActivityIntent();
//Verify the intent was not null. //Verify the intent was not null.
assertNotNull("Intent was null", launchIntent); assertNotNull("Intent was null", launchIntent);
//Verify that LaunchActivity was finished after button click
assertTrue(isFinishCalled());
final String payload = launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY); final String payload = launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY);
//Verify that payload data was added to the intent //Verify that payload data was added to the intent
assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD
, payload); , payload);
} }
} }