PhotoTaker { public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (resultCode == Activity.RESULT_CANCELED) { onResultCanceled(requestCode); return; } if (resultCode != Activity.RESULT_OK) { onResultNotOk(requestCode); return; } if (data == null) { onResultNotOk(requestCode); } switch (requestCode) { case CAPTURE_IMAGE: captureAction.onResult(data); break; case PICK_IMAGE: pickAction.onResult(data); break; case CROP_IMAGE: { cropAction.onResult(data); break; } } } PhotoTaker(@NonNull Activity activity, @NonNull PhotoSize photoSize);  PhotoTaker(@NonNull Fragment fragment, @NonNull PhotoSize photoSize); void onActivityResult(int requestCode, int resultCode, @Nullable Intent data); void doCropImage(@NonNull Uri uri); void showDialog(); void captureImage(); void pickImage(); void setListener(@NonNull PhotoTakerListener listener); static final int CAPTURE_IMAGE; static final int CROP_IMAGE; static final int PICK_IMAGE; }
@Test public void onResultCancel() throws Exception { taker.onActivityResult(CROP_IMAGE, RESULT_CANCELED, null); verify(listener).onCancel(CROP_IMAGE); taker.onActivityResult(CAPTURE_IMAGE, RESULT_CANCELED, null); verify(listener).onCancel(CAPTURE_IMAGE); }
@Test public void onResultOfOtherAction() throws Exception { taker.onActivityResult(OTHER_ACTION, RESULT_FIRST_USER, null); verify(listener, never()).onError(anyInt()); taker.onActivityResult(OTHER_ACTION, RESULT_CANCELED, null); verify(listener, never()).onCancel(anyInt()); }
@Test public void onResultOk() throws Exception { taker.cropAction = mock(PhotoTaker.Action.class); taker.captureAction = mock(PhotoTaker.Action.class); taker.pickAction = mock(PhotoTaker.Action.class); Intent intent = mock(Intent.class); taker.onActivityResult(CROP_IMAGE, RESULT_OK, intent); verify(taker.cropAction).onResult(intent); taker.onActivityResult(CAPTURE_IMAGE, RESULT_OK, intent); verify(taker.captureAction).onResult(intent); taker.onActivityResult(PICK_IMAGE, RESULT_OK, intent); verify(taker.pickAction).onResult(intent); }