Fixed startActivity() Bug in LaunchActivityTest
Activity under test was called in setUp instead of each test method.
Now each test method makes a separate call to startActivity() like
mentioned in our documentation.
Change-Id: I41df74cd5dae30e57cb481bb85714e361a9189e8
Signed-off-by: Stephan Linzner <slinzner@google.com>
diff --git a/samples/training/testingfun/app/src/com/example/android/testingfun/lesson4/LaunchActivity.java b/samples/training/testingfun/app/src/com/example/android/testingfun/lesson4/LaunchActivity.java
index 6459a56..8f1fb9b 100644
--- a/samples/training/testingfun/app/src/com/example/android/testingfun/lesson4/LaunchActivity.java
+++ b/samples/training/testingfun/app/src/com/example/android/testingfun/lesson4/LaunchActivity.java
@@ -41,6 +41,7 @@
@Override
public void onClick(View v) {
startActivity(NextActivity.makeIntent(LaunchActivity.this, STRING_PAYLOAD));
+ finish();
}
});
}
diff --git a/samples/training/testingfun/app/tests/src/com/example/android/testingfun/tests/lesson4/LaunchActivityTest.java b/samples/training/testingfun/app/tests/src/com/example/android/testingfun/tests/lesson4/LaunchActivityTest.java
index 27606eb..7d472c3 100644
--- a/samples/training/testingfun/app/tests/src/com/example/android/testingfun/tests/lesson4/LaunchActivityTest.java
+++ b/samples/training/testingfun/app/tests/src/com/example/android/testingfun/tests/lesson4/LaunchActivityTest.java
@@ -29,8 +29,7 @@
*/
public class LaunchActivityTest extends ActivityUnitTestCase<LaunchActivity> {
- private LaunchActivity mLaunchActivity;
- private Button mLaunchNextButton;
+ private Intent mLaunchIntent;
public LaunchActivityTest() {
super(LaunchActivity.class);
@@ -41,13 +40,8 @@
protected void setUp() throws Exception {
super.setUp();
//Create an intent to launch target Activity
- final Intent intent = new Intent(getInstrumentation().getTargetContext(),
+ mLaunchIntent = new Intent(getInstrumentation().getTargetContext(),
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 @@
*/
@MediumTest
public void testPreconditions() {
- assertNotNull("mLaunchActivity is null", mLaunchActivity);
- assertNotNull("mLaunchNextButton is null", mLaunchNextButton);
+ //Start the activity under test in isolation, without values for savedInstanceState and
+ //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
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,
- mLaunchNextButton.getText());
+ launchNextButton.getText());
}
@MediumTest
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
//button from code
- mLaunchNextButton.performClick();
+ launchNextButton.performClick();
// Get the intent for the next started activity
final Intent launchIntent = getStartedActivityIntent();
//Verify the intent was not null.
assertNotNull("Intent was null", launchIntent);
+ //Verify that LaunchActivity was finished after button click
+ assertTrue(isFinishCalled());
+
final String payload = launchIntent.getStringExtra(NextActivity.EXTRAS_PAYLOAD_KEY);
//Verify that payload data was added to the intent
assertEquals("Payload is empty", LaunchActivity.STRING_PAYLOAD
, payload);
}
-}
+}
\ No newline at end of file