Sub topic 16
Testing and Verifying Camera and Image Picker Features
This chapter assumes you know Dart async and await, Flutter widget tests, StatefulWidget lifecycle methods, and how to add packages with pubspec.yaml. You will test your app logic with fakes, then verify permissions and camera behavior on real devices.
Test the app around the plugins
Camera and picker plugins communicate with Android and iOS. A normal unit test should not open a gallery or activate a camera. Instead, put plugin calls behind a small service and replace that service with a fake during tests.
For example, define the part of the app that you need:
import 'package:image_picker/image_picker.dart';
abstract class PhotoPicker {
Future<XFile?> pickImage();
}
class ImagePickerPhotoPicker implements PhotoPicker {
final ImagePicker _picker = ImagePicker();
@override
Future<XFile?> pickImage() {
return _picker.pickImage(source: ImageSource.gallery);
}
}Your widget can receive a PhotoPicker in its constructor. A fake can return a known XFile path without opening a platform UI:
class FakePhotoPicker implements PhotoPicker {
FakePhotoPicker(this.result);
final XFile? result;
@override
Future<XFile?> pickImage() async => result;
}A widget test can then verify loading, cancellation, and success states. The test checks your code rather than the operating system's gallery.
import 'package:flutter_test/flutter_test.dart';
import 'package:image_picker/image_picker.dart';
void main() {
test('fake picker returns a selected image', () async {
final picker = FakePhotoPicker(XFile('/tmp/photo.jpg'));
final result = await picker.pickImage();
expect(result?.path, '/tmp/photo.jpg');
});
test('fake picker represents cancellation', () async {
final picker = FakePhotoPicker(null);
expect(await picker.pickImage(), isNull);
});
}Run automated tests with:
flutter testVerify the platform setup
A passing unit test does not prove that permissions, manifest entries, or platform plugin registration are correct. Test these cases on Android and iOS:
- Grant gallery or camera permission.
- Deny permission the first time.
- Deny permission permanently, then open the system settings.
- Cancel the gallery without selecting a file.
- Select a large image and confirm resizing or compression.
- Pause and resume the app while the camera is open.
- Rotate the device if your layout supports rotation.
- Capture an image, display it, copy it, and upload it.
Use a physical device for camera quality, focus, flash, orientation, and lifecycle behavior. An emulator is useful for repeatable UI checks, but it may use a simulated camera and may not reproduce hardware failures.
Test camera behavior as an integration flow
Camera initialization and capture are asynchronous. Verify that the screen shows a loading state before initialize() finishes, an error state when initialization throws CameraException, and CameraPreview only after successful initialization.
Test controller disposal by navigating away from the screen and by sending the app to the background. A resumed screen should create or initialize a valid controller again. Do not assert exact camera hardware counts. Devices differ.
A practical release check is to build and install the app using the same configuration as production:
flutter build apk --release
flutter build ipa --releaseThe iOS command requires macOS and the appropriate signing setup.
Exercise
Why should a picker widget use a fake service in a widget test?
Show answer
The fake makes the test deterministic and verifies the widget's behavior without opening a real gallery or depending on platform permissions.