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

Commit 3eedf6c

Browse files
authored
[image_picker] Switch unit tests to mock plaform implementation (#5706)
`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.
1 parent 4479e26 commit 3eedf6c

File tree

6 files changed

+476
-578
lines changed

6 files changed

+476
-578
lines changed

packages/image_picker/image_picker/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.8.5+3
2+
3+
* Adds argument error assertions to the app-facing package, to ensure
4+
consistency across platform implementations.
5+
* Updates tests to use a mock platform instead of relying on default
6+
method channel implementation internals.
7+
18
## 0.8.5+2
29

310
* Minor fixes for new analysis options.

packages/image_picker/image_picker/lib/image_picker.dart

+22
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,17 @@ class ImagePicker {
207207
int? imageQuality,
208208
CameraDevice preferredCameraDevice = CameraDevice.rear,
209209
}) {
210+
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
211+
throw ArgumentError.value(
212+
imageQuality, 'imageQuality', 'must be between 0 and 100');
213+
}
214+
if (maxWidth != null && maxWidth < 0) {
215+
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
216+
}
217+
if (maxHeight != null && maxHeight < 0) {
218+
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
219+
}
220+
210221
return platform.getImage(
211222
source: source,
212223
maxWidth: maxWidth,
@@ -245,6 +256,17 @@ class ImagePicker {
245256
double? maxHeight,
246257
int? imageQuality,
247258
}) {
259+
if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) {
260+
throw ArgumentError.value(
261+
imageQuality, 'imageQuality', 'must be between 0 and 100');
262+
}
263+
if (maxWidth != null && maxWidth < 0) {
264+
throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative');
265+
}
266+
if (maxHeight != null && maxHeight < 0) {
267+
throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative');
268+
}
269+
248270
return platform.getMultiImage(
249271
maxWidth: maxWidth,
250272
maxHeight: maxHeight,

packages/image_picker/image_picker/pubspec.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for selecting images from the Android and iOS image
33
library, and taking new pictures with the camera.
44
repository: https://github.com/flutter/plugins/tree/main/packages/image_picker/image_picker
55
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+image_picker%22
6-
version: 0.8.5+2
6+
version: 0.8.5+3
77

88
environment:
99
sdk: ">=2.14.0 <3.0.0"
@@ -28,6 +28,8 @@ dependencies:
2828
image_picker_platform_interface: ^2.3.0
2929

3030
dev_dependencies:
31+
build_runner: ^2.1.10
32+
cross_file: ^0.3.1+1 # Mockito generates a direct include.
3133
flutter_test:
3234
sdk: flutter
3335
mockito: ^5.0.0

0 commit comments

Comments
 (0)