|
5 | 5 | import 'dart:html';
|
6 | 6 | import 'dart:ui';
|
7 | 7 |
|
| 8 | +import 'package:async/async.dart'; |
8 | 9 | import 'package:camera_platform_interface/camera_platform_interface.dart';
|
9 | 10 | import 'package:camera_web/camera_web.dart';
|
10 | 11 | import 'package:camera_web/src/camera.dart';
|
@@ -33,13 +34,8 @@ void main() {
|
33 | 34 | window = MockWindow();
|
34 | 35 | navigator = MockNavigator();
|
35 | 36 | mediaDevices = MockMediaDevices();
|
36 |
| - videoElement = VideoElement() |
37 |
| - ..src = |
38 |
| - 'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4' |
39 |
| - ..preload = 'true' |
40 |
| - ..width = 10 |
41 |
| - ..height = 10 |
42 |
| - ..crossOrigin = 'anonymous'; |
| 37 | + |
| 38 | + videoElement = getVideoElementWithBlankStream(Size(10, 10)); |
43 | 39 |
|
44 | 40 | cameraSettings = MockCameraSettings();
|
45 | 41 |
|
@@ -327,21 +323,18 @@ void main() {
|
327 | 323 | const ultraHighResolutionSize = Size(3840, 2160);
|
328 | 324 | const maxResolutionSize = Size(3840, 2160);
|
329 | 325 |
|
330 |
| - late CameraDescription cameraDescription; |
331 |
| - late CameraMetadata cameraMetadata; |
332 |
| - |
333 |
| - setUp(() { |
334 |
| - cameraDescription = CameraDescription( |
335 |
| - name: 'name', |
336 |
| - lensDirection: CameraLensDirection.front, |
337 |
| - sensorOrientation: 0, |
338 |
| - ); |
| 326 | + final cameraDescription = CameraDescription( |
| 327 | + name: 'name', |
| 328 | + lensDirection: CameraLensDirection.front, |
| 329 | + sensorOrientation: 0, |
| 330 | + ); |
339 | 331 |
|
340 |
| - cameraMetadata = CameraMetadata( |
341 |
| - deviceId: 'deviceId', |
342 |
| - facingMode: 'user', |
343 |
| - ); |
| 332 | + final cameraMetadata = CameraMetadata( |
| 333 | + deviceId: 'deviceId', |
| 334 | + facingMode: 'user', |
| 335 | + ); |
344 | 336 |
|
| 337 | + setUp(() { |
345 | 338 | // Add metadata for the camera description.
|
346 | 339 | (CameraPlatform.instance as CameraPlugin)
|
347 | 340 | .camerasMetadata[cameraDescription] = cameraMetadata;
|
@@ -434,11 +427,38 @@ void main() {
|
434 | 427 | });
|
435 | 428 | });
|
436 | 429 |
|
437 |
| - testWidgets('initializeCamera throws UnimplementedError', (tester) async { |
438 |
| - expect( |
439 |
| - () => CameraPlatform.instance.initializeCamera(cameraId), |
440 |
| - throwsUnimplementedError, |
441 |
| - ); |
| 430 | + group('initializeCamera', () { |
| 431 | + testWidgets( |
| 432 | + 'throws CameraException ' |
| 433 | + 'with notFound error ' |
| 434 | + 'if the camera does not exist', (tester) async { |
| 435 | + expect( |
| 436 | + () => CameraPlatform.instance.initializeCamera(cameraId), |
| 437 | + throwsA( |
| 438 | + isA<CameraException>().having( |
| 439 | + (e) => e.code, |
| 440 | + 'code', |
| 441 | + CameraErrorCodes.notFound, |
| 442 | + ), |
| 443 | + ), |
| 444 | + ); |
| 445 | + }); |
| 446 | + |
| 447 | + testWidgets('initializes and plays the camera', (tester) async { |
| 448 | + final camera = MockCamera(); |
| 449 | + |
| 450 | + when(camera.getVideoSize).thenAnswer((_) => Future.value(Size(10, 10))); |
| 451 | + when(camera.initialize).thenAnswer((_) => Future.value()); |
| 452 | + when(camera.play).thenAnswer((_) => Future.value()); |
| 453 | + |
| 454 | + // Save the camera in the camera plugin. |
| 455 | + (CameraPlatform.instance as CameraPlugin).cameras[cameraId] = camera; |
| 456 | + |
| 457 | + await CameraPlatform.instance.initializeCamera(cameraId); |
| 458 | + |
| 459 | + verify(camera.initialize).called(1); |
| 460 | + verify(camera.play).called(1); |
| 461 | + }); |
442 | 462 | });
|
443 | 463 |
|
444 | 464 | testWidgets('lockCaptureOrientation throws UnimplementedError',
|
@@ -628,13 +648,78 @@ void main() {
|
628 | 648 | );
|
629 | 649 | });
|
630 | 650 |
|
| 651 | + group('getCamera', () { |
| 652 | + testWidgets('returns the correct camera', (tester) async { |
| 653 | + final camera = Camera(textureId: cameraId, window: window); |
| 654 | + |
| 655 | + // Save the camera in the camera plugin. |
| 656 | + (CameraPlatform.instance as CameraPlugin).cameras[cameraId] = camera; |
| 657 | + |
| 658 | + expect( |
| 659 | + (CameraPlatform.instance as CameraPlugin).getCamera(cameraId), |
| 660 | + equals(camera), |
| 661 | + ); |
| 662 | + }); |
| 663 | + |
| 664 | + testWidgets( |
| 665 | + 'throws CameraException ' |
| 666 | + 'with notFound error ' |
| 667 | + 'if the camera does not exist', (tester) async { |
| 668 | + expect( |
| 669 | + () => (CameraPlatform.instance as CameraPlugin).getCamera(cameraId), |
| 670 | + throwsA( |
| 671 | + isA<CameraException>().having( |
| 672 | + (e) => e.code, |
| 673 | + 'code', |
| 674 | + CameraErrorCodes.notFound, |
| 675 | + ), |
| 676 | + ), |
| 677 | + ); |
| 678 | + }); |
| 679 | + }); |
| 680 | + |
631 | 681 | group('events', () {
|
632 |
| - testWidgets('onCameraInitialized throws UnimplementedError', |
633 |
| - (tester) async { |
| 682 | + testWidgets( |
| 683 | + 'onCameraInitialized emits a CameraInitializedEvent ' |
| 684 | + 'on initializeCamera', (tester) async { |
| 685 | + // Mock the camera to use a blank video stream of size 1280x720. |
| 686 | + const videoSize = Size(1280, 720); |
| 687 | + |
| 688 | + videoElement = getVideoElementWithBlankStream(videoSize); |
| 689 | + |
| 690 | + when( |
| 691 | + () => mediaDevices.getUserMedia(any()), |
| 692 | + ).thenAnswer((_) async => videoElement.captureStream()); |
| 693 | + |
| 694 | + final camera = Camera( |
| 695 | + textureId: cameraId, |
| 696 | + window: window, |
| 697 | + ); |
| 698 | + |
| 699 | + // Save the camera in the camera plugin. |
| 700 | + (CameraPlatform.instance as CameraPlugin).cameras[cameraId] = camera; |
| 701 | + |
| 702 | + final Stream<CameraInitializedEvent> eventStream = |
| 703 | + CameraPlatform.instance.onCameraInitialized(cameraId); |
| 704 | + |
| 705 | + final streamQueue = StreamQueue(eventStream); |
| 706 | + |
| 707 | + await CameraPlatform.instance.initializeCamera(cameraId); |
| 708 | + |
634 | 709 | expect(
|
635 |
| - () => CameraPlatform.instance.onCameraInitialized(cameraId), |
636 |
| - throwsUnimplementedError, |
| 710 | + await streamQueue.next, |
| 711 | + CameraInitializedEvent( |
| 712 | + cameraId, |
| 713 | + videoSize.width, |
| 714 | + videoSize.height, |
| 715 | + ExposureMode.auto, |
| 716 | + false, |
| 717 | + FocusMode.auto, |
| 718 | + false, |
| 719 | + ), |
637 | 720 | );
|
| 721 | + |
| 722 | + await streamQueue.cancel(); |
638 | 723 | });
|
639 | 724 |
|
640 | 725 | testWidgets('onCameraResolutionChanged throws UnimplementedError',
|
|
0 commit comments