As a watered down example, say I have an activity, called
StartActivityForResult, with a button which launches ChildActivty:
private View.OnClickListener onStart = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(StartActivityForResult.this,
ChildActivity.class);
StartActivityForResult.this.startActivityForResult(intent,
R.id.child_request);
} };
StartActivityForResult has the following `onActivityResult()` method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
int result =
data.getIntExtra(StartActivityForResult.this.getString(R.string.result), -1);
String msg = "requestCode=" + requestCode + ", resultCode=" +
resultCode + ", result=" + result;
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
Now I want to write a JUnit test to make sure that a result is correctly
handled by this method. My first attempt is something like this:
@UiThreadTest
public void testStartButtonOnClick() {
Assert.assertTrue(this.startButton.performClick());
Instrumentation.ActivityResult result = new
Instrumentation.ActivityResult(Activity.RESULT_OK, null);
Assert.assertNotNull(result);
Instrumentation.ActivityMonitor am = new
Instrumentation.ActivityMonitor(ChildActivity.class.getName(), result, true);
Assert.assertNotNull(am);
Activity childActivity =
this.getInstrumentation().waitForMonitorWithTimeout(am, TIME_OUT);
Assert.assertNotNull(childActivity);
Assert.fail("How do I check that StartActivityForResult correctly
handles the returned result?"); }
This has two problems. First the call to `performClick()` doesn't seem to call
my `onClick` handler. Also, I need access to the values that I normally get as
parameters to `onActivityResult()`. I'm very new to Android development.
However, it seems like this would be a common occurrence in many apps. How do I
go about fixing my test so that I can ensure that my Activity class behaves
correctly?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en