Skip to content

Commit

Permalink
Format files
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenWeener committed Apr 3, 2023
1 parent 9d0671f commit daf6211
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,31 +548,32 @@ public boolean onRequestPermissionsResult(
@Override
public boolean onActivityResult(final int requestCode, final int resultCode, final Intent data) {
// Off-load result handling to a separate thread as it almost always involves I/O operations.
executor.execute(() -> {
switch (requestCode) {
case REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY:
handleChooseImageResult(resultCode, data);
break;
case REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY:
handleChooseMultiImageResult(resultCode, data);
break;
case REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA:
handleCaptureImageResult(resultCode);
break;
case REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY:
handleChooseVideoResult(resultCode, data);
break;
case REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA:
handleCaptureVideoResult(resultCode);
break;
}
});
executor.execute(
() -> {
switch (requestCode) {
case REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY:
handleChooseImageResult(resultCode, data);
break;
case REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY:
handleChooseMultiImageResult(resultCode, data);
break;
case REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA:
handleCaptureImageResult(resultCode);
break;
case REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY:
handleChooseVideoResult(resultCode, data);
break;
case REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA:
handleCaptureVideoResult(resultCode);
break;
}
});

return requestCode == REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY ||
requestCode == REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY ||
requestCode == REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA ||
requestCode == REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY ||
requestCode == REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA;
return requestCode == REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY
|| requestCode == REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY
|| requestCode == REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA
|| requestCode == REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY
|| requestCode == REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA;
}

private void handleChooseImageResult(int resultCode, Intent data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -354,15 +353,18 @@ public void onRequestPermissionsResult_whenCameraPermissionDenied_finishesWithEr

@Test
public void onActivityResult_whenPickFromGalleryCanceled_finishesWithEmptyList() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null);
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null);

@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> pathListCapture = ArgumentCaptor.forClass(List.class);
Expand All @@ -373,27 +375,33 @@ public void onActivityResult_whenPickFromGalleryCanceled_finishesWithEmptyList()

@Test
public void onActivityResult_whenPickFromGalleryCanceled_storesNothingInCache() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate = createDelegate();

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null);
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_CANCELED, null);

verify(cache, never()).saveResult(any(), any(), any());
}

@Test
public void
onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_finishesWithImagePath() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
Expand All @@ -407,14 +415,17 @@ public void onActivityResult_whenPickFromGalleryCanceled_storesNothingInCache()

@Test
public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_storesImageInCache() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate = createDelegate();

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent);

@SuppressWarnings("unchecked")
ArgumentCaptor<ArrayList<String>> pathListCapture = ArgumentCaptor.forClass(ArrayList.class);
Expand All @@ -425,12 +436,15 @@ public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_stores
@Test
public void
onActivityResult_whenImagePickedFromGallery_andResizeNeeded_finishesWithScaledImagePath() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
Expand All @@ -444,16 +458,19 @@ public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_stores

@Test
public void
onActivityResult_whenVideoPickedFromGallery_andResizeParametersSupplied_finishesWithFilePath() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
onActivityResult_whenVideoPickedFromGallery_andResizeParametersSupplied_finishesWithFilePath() {
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, Activity.RESULT_OK, mockIntent);

@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> pathListCapture = ArgumentCaptor.forClass(List.class);
Expand All @@ -464,15 +481,18 @@ public void onActivityResult_whenImagePickedFromGallery_andNoResizeNeeded_stores

@Test
public void onActivityResult_whenTakeImageWithCameraCanceled_finishesWithEmptyList() {
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, Activity.RESULT_CANCELED, null);
ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, Activity.RESULT_CANCELED, null);

@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> pathListCapture = ArgumentCaptor.forClass(List.class);
Expand All @@ -484,10 +504,13 @@ public void onActivityResult_whenTakeImageWithCameraCanceled_finishesWithEmptyLi
@Test
public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishesWithImagePath() {
when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString");
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(DEFAULT_IMAGE_OPTIONS, null);

Expand All @@ -505,10 +528,13 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes
public void
onActivityResult_whenImageTakenWithCamera_andResizeNeeded_finishesWithScaledImagePath() {
when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString");
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);

Expand All @@ -524,17 +550,20 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes

@Test
public void
onActivityResult_whenVideoTakenWithCamera_andResizeParametersSupplied_finishesWithFilePath() {
onActivityResult_whenVideoTakenWithCamera_andResizeParametersSupplied_finishesWithFilePath() {
when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString");
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);
createDelegateWithPendingResultAndOptions(RESIZE_TRIGGERING_IMAGE_OPTIONS, null);

delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, Activity.RESULT_OK, mockIntent);
ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, Activity.RESULT_OK, mockIntent);

@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> pathListCapture = ArgumentCaptor.forClass(List.class);
Expand All @@ -547,10 +576,13 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes
public void
onActivityResult_whenVideoTakenWithCamera_andMaxDurationParametersSupplied_finishesWithFilePath() {
when(cache.retrievePendingCameraMediaUriPath()).thenReturn("testString");
Mockito.doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
}).when(mockExecutor).execute(any(Runnable.class));
Mockito.doAnswer(
invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
})
.when(mockExecutor)
.execute(any(Runnable.class));
ImagePickerDelegate delegate =
createDelegateWithPendingResultAndOptions(
null, new VideoSelectionOptions.Builder().setMaxDurationSeconds(MAX_DURATION).build());
Expand All @@ -569,7 +601,11 @@ public void onActivityResult_whenImageTakenWithCamera_andNoResizeNeeded_finishes
public void onActivityResult_whenImagePickedFromGallery_returnsTrue() {
ImagePickerDelegate delegate = createDelegate();

boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
boolean isHandled =
delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY,
Activity.RESULT_OK,
mockIntent);

assertTrue(isHandled);
}
Expand All @@ -578,7 +614,11 @@ public void onActivityResult_whenImagePickedFromGallery_returnsTrue() {
public void onActivityResult_whenMultipleImagesPickedFromGallery_returnsTrue() {
ImagePickerDelegate delegate = createDelegate();

boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
boolean isHandled =
delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY,
Activity.RESULT_OK,
mockIntent);

assertTrue(isHandled);
}
Expand All @@ -587,7 +627,11 @@ public void onActivityResult_whenMultipleImagesPickedFromGallery_returnsTrue() {
public void onActivityResult_whenVideoPickerFromGallery_returnsTrue() {
ImagePickerDelegate delegate = createDelegate();

boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY, Activity.RESULT_OK, mockIntent);
boolean isHandled =
delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY,
Activity.RESULT_OK,
mockIntent);

assertTrue(isHandled);
}
Expand All @@ -596,7 +640,11 @@ public void onActivityResult_whenVideoPickerFromGallery_returnsTrue() {
public void onActivityResult_whenImageTakenWithCamera_returnsTrue() {
ImagePickerDelegate delegate = createDelegate();

boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA, Activity.RESULT_OK, mockIntent);
boolean isHandled =
delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA,
Activity.RESULT_OK,
mockIntent);

assertTrue(isHandled);
}
Expand All @@ -605,7 +653,11 @@ public void onActivityResult_whenImageTakenWithCamera_returnsTrue() {
public void onActivityResult_whenVideoTakenWithCamera_returnsTrue() {
ImagePickerDelegate delegate = createDelegate();

boolean isHandled = delegate.onActivityResult(ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA, Activity.RESULT_OK, mockIntent);
boolean isHandled =
delegate.onActivityResult(
ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA,
Activity.RESULT_OK,
mockIntent);

assertTrue(isHandled);
}
Expand Down

0 comments on commit daf6211

Please sign in to comment.