Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[image_picker] Switch unit tests to mock plaform implementation (#5706)
Browse files Browse the repository at this point in the history
`image_picker`'s app-facing tests were never updated during federation to
use a mock platform implementation, and instead were still mocking method
channels. That makes them fragile to implementation details of the
default method channel implementation that is part of another package,
and thus subject to breakage when the method channel changes.

This converts them to using a mock platform implementation, so it's only
testing the layer within this package.

Removes some tests that were testing things that only made sense at the
method channel layer.

Adds argument assertions that there were tests for, but were previously
only enforced in the implementations. As these are API constraints, they
should be enforced at the API layer, not at each implementation's layer
as they currently are.
  • Loading branch information
stuartmorgan authored May 12, 2022
1 parent 4479e26 commit 3eedf6c
Show file tree
Hide file tree
Showing 6 changed files with 476 additions and 578 deletions.
7 changes: 7 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.8.5+3

* Adds argument error assertions to the app-facing package, to ensure
consistency across platform implementations.
* Updates tests to use a mock platform instead of relying on default
method channel implementation internals.

## 0.8.5+2

* Minor fixes for new analysis options.
Expand Down
22 changes: 22 additions & 0 deletions packages/image_picker/image_picker/lib/image_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ class ImagePicker {
int? imageQuality,
CameraDevice preferredCameraDevice = CameraDevice.rear,
}) {
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
imageQuality, 'imageQuality', 'must be between 0 and 100');
}
if (maxWidth != null && maxWidth < 0) {
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
}
if (maxHeight != null && maxHeight < 0) {
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
}

return platform.getImage(
source: source,
maxWidth: maxWidth,
Expand Down Expand Up @@ -245,6 +256,17 @@ class ImagePicker {
double? maxHeight,
int? imageQuality,
}) {
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
throw ArgumentError.value(
imageQuality, 'imageQuality', 'must be between 0 and 100');
}
if (maxWidth != null && maxWidth < 0) {
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
}
if (maxHeight != null && maxHeight < 0) {
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
}

return platform.getMultiImage(
maxWidth: maxWidth,
maxHeight: maxHeight,
Expand Down
4 changes: 3 additions & 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/main/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.5+2
version: 0.8.5+3

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -28,6 +28,8 @@ dependencies:
image_picker_platform_interface: ^2.3.0

dev_dependencies:
build_runner: ^2.1.10
cross_file: ^0.3.1+1 # Mockito generates a direct include.
flutter_test:
sdk: flutter
mockito: ^5.0.0
Expand Down
Loading

0 comments on commit 3eedf6c

Please sign in to comment.