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

Commit ed48cff

Browse files
authored
[camera_web] Add dispose implementation (#4203)
1 parent ab95335 commit ed48cff

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

packages/camera/camera_web/example/integration_test/camera_web_test.dart

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,11 +684,54 @@ void main() {
684684
);
685685
});
686686

687-
testWidgets('dispose throws UnimplementedError', (tester) async {
688-
expect(
689-
() => CameraPlatform.instance.dispose(cameraId),
690-
throwsUnimplementedError,
691-
);
687+
group('dispose', () {
688+
testWidgets(
689+
'throws CameraException '
690+
'with notFound error '
691+
'if the camera does not exist', (tester) async {
692+
expect(
693+
() => CameraPlatform.instance.dispose(cameraId),
694+
throwsA(
695+
isA<CameraException>().having(
696+
(e) => e.code,
697+
'code',
698+
CameraErrorCodes.notFound,
699+
),
700+
),
701+
);
702+
});
703+
704+
testWidgets('disposes the correct camera', (tester) async {
705+
const firstCameraId = 0;
706+
const secondCameraId = 1;
707+
708+
final firstCamera = MockCamera();
709+
final secondCamera = MockCamera();
710+
711+
when(firstCamera.dispose).thenAnswer((_) => Future.value());
712+
when(secondCamera.dispose).thenAnswer((_) => Future.value());
713+
714+
// Save cameras in the camera plugin.
715+
(CameraPlatform.instance as CameraPlugin).cameras.addAll({
716+
firstCameraId: firstCamera,
717+
secondCameraId: secondCamera,
718+
});
719+
720+
// Dispose the first camera.
721+
await CameraPlatform.instance.dispose(firstCameraId);
722+
723+
// The first camera should be disposed.
724+
verify(firstCamera.dispose).called(1);
725+
verifyNever(secondCamera.dispose);
726+
727+
// The first camera should be removed from the camera plugin.
728+
expect(
729+
(CameraPlatform.instance as CameraPlugin).cameras,
730+
equals({
731+
secondCameraId: secondCamera,
732+
}),
733+
);
734+
});
692735
});
693736

694737
group('getCamera', () {

packages/camera/camera_web/lib/src/camera_web.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ class CameraPlugin extends CameraPlatform {
367367
}
368368

369369
@override
370-
Future<void> dispose(int cameraId) {
371-
throw UnimplementedError('dispose() is not implemented.');
370+
Future<void> dispose(int cameraId) async {
371+
getCamera(cameraId).dispose();
372+
cameras.remove(cameraId);
372373
}
373374

374375
/// Returns a media video stream for the device with the given [deviceId].

0 commit comments

Comments
 (0)