Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.1+3

* Fix image picker causing a crash when the cache directory is deleted.

## 0.8.1+2

* Update the example app to support the multi-image feature.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ private File createTemporaryWritableFile(String suffix) {
File image;

try {
externalFilesDirectory.mkdirs();
image = File.createTempFile(filename, suffix, externalFilesDirectory);
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,18 @@ public void takeImageWithCamera_WhenCameraPermissionNotPresent_RequestsForPermis
when(mockIntentResolver.resolveActivity(any(Intent.class))).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.takeImageWithCamera(mockMethodCall, mockResult);

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA));
try (MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class)) {
mockStaticFile
.when(() -> File.createTempFile(any(), any(), any()))
.thenReturn(new File("/tmpfile"));

delegate.takeImageWithCamera(mockMethodCall, mockResult);

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA));
}
}

@Test
Expand All @@ -169,11 +176,18 @@ public void takeImageWithCamera_WhenCameraPermissionNotPresent_RequestsForPermis
when(mockIntentResolver.resolveActivity(any(Intent.class))).thenReturn(true);

ImagePickerDelegate delegate = createDelegate();
delegate.takeImageWithCamera(mockMethodCall, mockResult);

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA));
try (MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class)) {
mockStaticFile
.when(() -> File.createTempFile(any(), any(), any()))
.thenReturn(new File("/tmpfile"));

delegate.takeImageWithCamera(mockMethodCall, mockResult);

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA));
}
}

@Test
Expand All @@ -195,17 +209,18 @@ public void takeImageWithCamera_WritesImageToCacheDirectory() {
when(mockPermissionManager.isPermissionGranted(Manifest.permission.CAMERA)).thenReturn(true);
when(mockIntentResolver.resolveActivity(any(Intent.class))).thenReturn(true);

MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class);
mockStaticFile
.when(() -> File.createTempFile(any(), any(), any()))
.thenReturn(new File("/tmpfile"));
try (MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class)) {
mockStaticFile
.when(() -> File.createTempFile(any(), any(), any()))
.thenReturn(new File("/tmpfile"));

ImagePickerDelegate delegate = createDelegate();
delegate.takeImageWithCamera(mockMethodCall, mockResult);
ImagePickerDelegate delegate = createDelegate();
delegate.takeImageWithCamera(mockMethodCall, mockResult);

mockStaticFile.verify(
() -> File.createTempFile(any(), eq(".jpg"), eq(new File("/image_picker_cache"))),
times(1));
mockStaticFile.verify(
() -> File.createTempFile(any(), eq(".jpg"), eq(new File("/image_picker_cache"))),
times(1));
}
}

@Test
Expand All @@ -225,32 +240,44 @@ public void onRequestPermissionsResult_WhenCameraPermissionDenied_FinishesWithEr
public void
onRequestTakeVideoPermissionsResult_WhenCameraPermissionGranted_LaunchesTakeVideoWithCameraIntent() {
when(mockIntentResolver.resolveActivity(any(Intent.class))).thenReturn(true);

ImagePickerDelegate delegate = createDelegateWithPendingResultAndMethodCall();
delegate.onRequestPermissionsResult(
ImagePickerDelegate.REQUEST_CAMERA_VIDEO_PERMISSION,
new String[] {Manifest.permission.CAMERA},
new int[] {PackageManager.PERMISSION_GRANTED});

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA));
try (MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class)) {
mockStaticFile
.when(() -> File.createTempFile(any(), any(), any()))
.thenReturn(new File("/tmpfile"));

delegate.onRequestPermissionsResult(
ImagePickerDelegate.REQUEST_CAMERA_VIDEO_PERMISSION,
new String[] {Manifest.permission.CAMERA},
new int[] {PackageManager.PERMISSION_GRANTED});

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA));
}
}

@Test
public void
onRequestTakeImagePermissionsResult_WhenCameraPermissionGranted_LaunchesTakeWithCameraIntent() {
when(mockIntentResolver.resolveActivity(any(Intent.class))).thenReturn(true);

ImagePickerDelegate delegate = createDelegateWithPendingResultAndMethodCall();
delegate.onRequestPermissionsResult(
ImagePickerDelegate.REQUEST_CAMERA_IMAGE_PERMISSION,
new String[] {Manifest.permission.CAMERA},
new int[] {PackageManager.PERMISSION_GRANTED});

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA));
try (MockedStatic<File> mockStaticFile = Mockito.mockStatic(File.class)) {
mockStaticFile
.when(() -> File.createTempFile(any(), any(), any()))
.thenReturn(new File("/tmpfile"));

delegate.onRequestPermissionsResult(
ImagePickerDelegate.REQUEST_CAMERA_IMAGE_PERMISSION,
new String[] {Manifest.permission.CAMERA},
new int[] {PackageManager.PERMISSION_GRANTED});

verify(mockActivity)
.startActivityForResult(
any(Intent.class), eq(ImagePickerDelegate.REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA));
}
}

@Test
Expand Down Expand Up @@ -380,7 +407,7 @@ private ImagePickerDelegate createDelegate() {
private ImagePickerDelegate createDelegateWithPendingResultAndMethodCall() {
return new ImagePickerDelegate(
mockActivity,
null,
new File("/image_picker_cache"),
mockImageResizer,
mockResult,
mockMethodCall,
Expand Down
2 changes: 1 addition & 1 deletion packages/image_picker/image_picker/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
library, and taking new pictures with the camera.
repository: https://github.com/flutter/plugins/tree/master/packages/image_picker/image_picker
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
version: 0.8.1+2
version: 0.8.1+3

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down