Issue
In my application, when the user clicks on a "Register" Button in , the RegisterActivity is launched. Once the user fills in the form, the details are posted to a web service and if registration succeeds, RegisterActivity finsihes with RESULT_OK
. This is summarized in the code sample below:
public void submitRegistration() {
showProgressDialog(R.string.registration, R.string.please_wait);
getWebApi().register(buildRegistrationFromUI(), new Callback<ApiResponse>() {
@Override
public void success(ApiResponse apiResponse, Response response) {
hideProgressDialog();
setResult(RESULT_OK);
finish();
}
@Override
public void failure(RetrofitError error) {
hideProgressDialog();
showErrorDialog(ApiError.parse(error));
}
});
}
Using Espresso, How can I check that the activity finished with setResult(RESULT_OK)
.
Please Note: I do NOT want to create a mock intent, I want to check the intent result status.
Solution
All the setResult(...) method does is to change the values of fields in the Activity class
public final void setResult(int resultCode, Intent data) {
synchronized (this) {
mResultCode = resultCode;
mResultData = data;
}
}
So we can use Java Reflection to access the mResultCode field to test if the value has indeed been set to RESULT_OK.
@Rule
public ActivityTestRule<ContactsActivity> mActivityRule = new ActivityTestRule<>(
ContactsActivity.class);
@Test
public void testResultOk() throws NoSuchFieldException, IllegalAccessException {
Field f = Activity.class.getDeclaredField("mResultCode"); //NoSuchFieldException
f.setAccessible(true);
int mResultCode = f.getInt(mActivityRule.getActivity());
assertTrue("The result code is not ok. ", mResultCode == Activity.RESULT_OK);
}
Answered By - Eric Liu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.