Issue
I have an Activity
that contains two fragments. One fragment has a EditText
and a Button
; when the button is clicked, a callback is invoked with the text from the edit box:
// Not showing implementation of onCreateView()
class HelloEditFragment extends Fragment implements View.OnClickListener {
interface OnListener {
void onValue(String value);
}
private OnListener mListener;
public void onAttach(Context context) {
mListener = (OnListener) context;
}
public void onClick(View v) {
mListener.onValue(mEditText.getText().toString();
}
}
My Activity implements OnListener:
class HelloActivity extends Activity implements HelloEditFragment.OnListener {
public void onValue(String value) {
// ...
}
}
So far my test looks like this:
@RunWith (AndroidJUnit4.class)
public class HelloEditFragmentTest {
@Rule
public ActivityTestRule<HelloActivity> mActivityRule = new ActivityTestRule<> (HelloActivity.class);
@Test
public void testActivity () {
onView (withId (R.id.edit_text_hello_edit)).perform (typeText ("hello"));
onView (withId (R.id.button_hello_edit)).perform (click ());
// Now what?
}
}
How can I check that the onValue()
callback is called when the button in the fragment is clicked? Thanks,
Solution
There won't be an easy way to do that. I would suggest you give up on having the interface
implemented by the Activity
and use a field Listener instead.
E.g. like this:
class HelloActivity extends Activity {
HelloEditFragment.OnListener listener = new HelloEditFragment.OnListener() {
@Override
public void onValue(String value) {
// ...
}
};
public HelloEditFragment.OnListener getListener() {
return listener;
}
//... everything else
}
class HelloEditFragment extends Fragment implements View.OnClickListener {
public void onAttach(Context context) {
if (context instanceof HelloActivity) {
mListener = ((HelloActivity) context).getListener();
}
}
//... everything else (including onDetach with clearing the listener)
}
And then you can easily mock your field listener:
@RunWith (AndroidJUnit4.class)
public class HelloEditFragmentTest {
@Rule
public ActivityTestRule<HelloActivity> mActivityRule = new ActivityTestRule<> (HelloActivity.class);
@Test
public void testActivity () {
HelloEditFragment.OnListener listener = Mockito.mock(HelloEditFragment.OnListener.class);
mActivityRule.getActivity().listener = listener;
onView (withId (R.id.edit_text_hello_edit)).perform (typeText ("hello"));
onView (withId (R.id.button_hello_edit)).perform (click ());
Mockito.verify(listener, Mockito.times(1)).onValue("hello");
Mockito.verifyNoMoreInteractions(listener);
}
}
Answered By - Bartek Lipinski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.