You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+119-2
Original file line number
Diff line number
Diff line change
@@ -86,6 +86,7 @@ final bool isRegistered = ImagePickerPlatform.instance is NativeImagePickerMacOS
86
86
**To checks if the current macOS version supports this implementation**:
87
87
88
88
```dart
89
+
<!-- TODO: Make this instance method? -->
89
90
final bool isSupported = await NativeImagePickerMacOS().isSupported(); // Returns false on non-macOS platforms or if PHPicker is not supported on the current macOS version.
@@ -124,6 +124,8 @@ if (imagePickerImplementation is NativeImagePickerMacOS) {
124
124
> [!TIP]
125
125
> You can use `NativeImagePickerMacOS.registerWith()` to register this implementation. However, this bypasses platform checks, which **may result in runtime errors** if the current platform is not macOS or if the macOS version is unsupported. Instead, use `registerWithIfSupported()` if uncertain.
126
126
127
+
Refer to the [example main.dart](example/lib/main.dart) for a full usage example.
128
+
127
129
## 🌱 Contributing
128
130
129
131
This package uses [pigeon](https://pub.dev/packages/pigeon) for platform communication with the platform host and [mockito](https://pub.dev/packages/mockito) for mocking in unit tests and [swift-format](https://github.com/swiftlang/swift-format) for formatting the Swift code.
@@ -162,3 +164,118 @@ This functionality was originally proposed as a [pull request to `image_picker_m
[Ask a question about using the package](https://github.com/CompileKernel/native-image-picker-macos/discussions/new?category=q-a).
167
+
168
+
## 🧪 Testing
169
+
170
+
> [!TIP]
171
+
> With this approach, you can effectively test this platform implementation with the existing packages that use `image_picker` APIs. All platform-specific calls to `NativeImagePickerMacOS` should use the instance from `ImagePickerPlatform.interface` instead of creating a new `NativeImagePickerMacOS` to work.
172
+
173
+
To override the methods implementation for unit testing, add the dev dependencies:
174
+
175
+
1.[`mockito`](https://pub.dev/packages/mockito) (or [`mocktail`](https://pub.dev/packages/mocktail)): for mocking the instance methods of `NativeImagePickerMacOS`.
176
+
2.[`image_picker_platform_interface`](https://pub.dev/packages/image_picker_platform_interface): for overriding the instance of `ImagePickerPlatform` with the mock instance.
177
+
3.[`build_runner`](https://pub.dev/packages/build_runner): for creating the generated Dart files.
178
+
4.[`plugin_platform_interface`](https://pub.dev/packages/plugin_platform_interface): Since `ImagePickerPlatform` extends `PlatformInterface`, it's required to apply the mixin `MockPlatformInterfaceMixin` to the mock of `NativeImagePickerMacOS` to [ignore an assertation failure that enforces the usage of `extends` instead of `implements`](https://pub.dev/packages/plugin_platform_interface), since mock classes need to extend `Mock` and implement the real class.
179
+
180
+
```shell
181
+
$ flutter pub add dev:mockito dev:image_picker_platform_interface dev:build_runner dev:plugin_platform_interface # Add them as dev-dependencies
However, if you run the tests, you will get the following error:
247
+
248
+
```console
249
+
Assertion failed: "Platform interfaces must not be implemented with `implements`"
250
+
```
251
+
252
+
And that is because by default, all plugin platform interfaces that inherit from `PlatformInterface` must `extends` and not `implements` it to avoid breaking changes (adding new methods to platform interfaces are not considered breaking changes).
253
+
254
+
And mock classes must `implements` the real class rather than `extends` them, a solution is to [provide the mixin `MockPlatformInterfaceMixin` from `plugin_platform_interface` that will override this check](https://pub.dev/packages/plugin_platform_interface#mocking-or-faking-platform-interfaces):
// This doesn't work yet since MockNativeImagePickerMacOS is generated, unlike the mocktail package.
260
+
class MockNativeImagePickerMacOS extends Mock
261
+
with MockPlatformInterfaceMixin
262
+
implements NativeImagePickerMacOS {}
263
+
```
264
+
265
+
And since `MockNativeImagePickerMacOS` is generated, we need a new class that extends the base mock and provides the `MockPlatformInterfaceMixin` for `plugin_platform_interface` to not throw the assertion failure:
266
+
267
+
```dart
268
+
@GenerateNiceMocks([MockSpec<NativeImagePickerMacOS>(as: Symbol('BaseMockNativeImagePickerMacOS'))]) // This name should be different than MockNativeImagePickerMacOS for the mockito generation to success
269
+
import '<current-test-file-name>.mocks.dart'; // REPLACE <current-test-file-name> with the current test file name without extension
0 commit comments