|
53 | 53 | import io.flutter.plugins.camera.features.sensororientation.SensorOrientationFeature; |
54 | 54 | import io.flutter.plugins.camera.features.zoomlevel.ZoomLevelFeature; |
55 | 55 | import io.flutter.plugins.camera.media.ImageStreamReader; |
| 56 | +import io.flutter.plugins.camera.media.MediaRecorderBuilder; |
56 | 57 | import io.flutter.view.TextureRegistry; |
57 | 58 | import java.io.Closeable; |
58 | 59 | import java.io.File; |
@@ -207,6 +208,14 @@ public void before() { |
207 | 208 |
|
208 | 209 | camera.captureSession = mockCaptureSession; |
209 | 210 | camera.previewRequestBuilder = mockPreviewRequestBuilder; |
| 211 | + |
| 212 | + final SensorOrientationFeature mockSensorOrientationFeature = |
| 213 | + mockCameraFeatureFactory.createSensorOrientationFeature( |
| 214 | + mockCameraProperties, mockActivity, mockDartMessenger); |
| 215 | + final DeviceOrientationManager mockDeviceOrientationManager = |
| 216 | + mock(DeviceOrientationManager.class); |
| 217 | + when(mockSensorOrientationFeature.getDeviceOrientationManager()) |
| 218 | + .thenReturn(mockDeviceOrientationManager); |
210 | 219 | } |
211 | 220 |
|
212 | 221 | @After |
@@ -712,6 +721,109 @@ public void resumeVideoRecording_shouldCallPauseWhenRecording() { |
712 | 721 | verify(mockMediaRecorder, times(1)).resume(); |
713 | 722 | } |
714 | 723 |
|
| 724 | + @Test |
| 725 | + public void startVideoRecording_usesCustomPath() throws IOException, CameraAccessException { |
| 726 | + final String customPath = |
| 727 | + new File(System.getProperty("java.io.tmpdir"), "custom_video.mp4").getAbsolutePath(); |
| 728 | + final MediaRecorder mockMediaRecorder = mock(MediaRecorder.class); |
| 729 | + |
| 730 | + final ArrayList<CaptureRequest.Builder> mockRequestBuilders = new ArrayList<>(); |
| 731 | + CaptureRequest.Builder mockRequestBuilder = mock(CaptureRequest.Builder.class); |
| 732 | + mockRequestBuilders.add(mockRequestBuilder); |
| 733 | + final SurfaceTexture mockSurfaceTexture = mock(SurfaceTexture.class); |
| 734 | + final Size mockSize = mock(Size.class); |
| 735 | + final ImageReader mockPictureImageReader = mock(ImageReader.class); |
| 736 | + camera.pictureImageReader = mockPictureImageReader; |
| 737 | + final CameraDeviceWrapper fakeCamera = |
| 738 | + new FakeCameraDeviceWrapper(mockRequestBuilders, mockCaptureSession); |
| 739 | + |
| 740 | + camera.cameraDevice = fakeCamera; |
| 741 | + |
| 742 | + TextureRegistry.SurfaceTextureEntry cameraFlutterTexture = camera.flutterTexture; |
| 743 | + ResolutionFeature resolutionFeature = mockCameraFeatureFactory.mockResolutionFeature; |
| 744 | + |
| 745 | + assertNotNull(cameraFlutterTexture); |
| 746 | + when(cameraFlutterTexture.surfaceTexture()).thenReturn(mockSurfaceTexture); |
| 747 | + |
| 748 | + assertNotNull(resolutionFeature); |
| 749 | + when(resolutionFeature.getPreviewSize()).thenReturn(mockSize); |
| 750 | + |
| 751 | + try (MockedConstruction<MediaRecorderBuilder> mockedBuilder = |
| 752 | + Mockito.mockConstruction( |
| 753 | + MediaRecorderBuilder.class, |
| 754 | + (mock, context) -> { |
| 755 | + MediaRecorderBuilder.RecordingParameters params = |
| 756 | + (MediaRecorderBuilder.RecordingParameters) context.arguments().get(1); |
| 757 | + assertEquals(customPath, params.outputFilePath); |
| 758 | + when(mock.setEnableAudio(anyBoolean())).thenReturn(mock); |
| 759 | + when(mock.setMediaOrientation(anyInt())).thenReturn(mock); |
| 760 | + when(mock.build()).thenReturn(mockMediaRecorder); |
| 761 | + })) { |
| 762 | + |
| 763 | + camera.startVideoRecording(null, customPath); |
| 764 | + |
| 765 | + assertEquals(1, mockedBuilder.constructed().size()); |
| 766 | + } |
| 767 | + } |
| 768 | + |
| 769 | + @SuppressWarnings("try") |
| 770 | + @Test |
| 771 | + public void startVideoRecording_errorsOnInvalidPath() throws IOException, CameraAccessException { |
| 772 | + final String invalidPath = "/non/existent/path.mp4"; |
| 773 | + |
| 774 | + try (MockedConstruction<MediaRecorderBuilder> mockedBuilder = |
| 775 | + Mockito.mockConstruction( |
| 776 | + MediaRecorderBuilder.class, |
| 777 | + (mock, context) -> { |
| 778 | + when(mock.setEnableAudio(anyBoolean())).thenReturn(mock); |
| 779 | + when(mock.setMediaOrientation(anyInt())).thenReturn(mock); |
| 780 | + when(mock.build()).thenThrow(new IOException("Invalid path")); |
| 781 | + })) { |
| 782 | + |
| 783 | + Messages.FlutterError error = |
| 784 | + assertThrows( |
| 785 | + Messages.FlutterError.class, |
| 786 | + () -> { |
| 787 | + camera.startVideoRecording(null, invalidPath); |
| 788 | + }); |
| 789 | + // Assert the IOError from validateOutputPath which is thrown prior to building. |
| 790 | + assertEquals("IOError", error.code); |
| 791 | + } |
| 792 | + } |
| 793 | + |
| 794 | + @Test |
| 795 | + public void startVideoRecording_errorsOnDirectoryPath() { |
| 796 | + Messages.FlutterError error = |
| 797 | + assertThrows( |
| 798 | + Messages.FlutterError.class, |
| 799 | + () -> { |
| 800 | + camera.startVideoRecording(null, "/"); |
| 801 | + }); |
| 802 | + assertEquals("IOError", error.code); |
| 803 | + } |
| 804 | + |
| 805 | + @Test |
| 806 | + public void startVideoRecording_errorsOnNonExistentParentPath() { |
| 807 | + Messages.FlutterError error = |
| 808 | + assertThrows( |
| 809 | + Messages.FlutterError.class, |
| 810 | + () -> { |
| 811 | + camera.startVideoRecording(null, "/non/existent/parent/file.mp4"); |
| 812 | + }); |
| 813 | + assertEquals("IOError", error.code); |
| 814 | + } |
| 815 | + |
| 816 | + @Test |
| 817 | + public void startVideoRecording_errorsOnInvalidExtension() { |
| 818 | + Messages.FlutterError error = |
| 819 | + assertThrows( |
| 820 | + Messages.FlutterError.class, |
| 821 | + () -> { |
| 822 | + camera.startVideoRecording(null, "file.txt"); |
| 823 | + }); |
| 824 | + assertEquals("IOError", error.code); |
| 825 | + } |
| 826 | + |
715 | 827 | @Test |
716 | 828 | public void setDescriptionWhileRecording_errorsWhenUnsupported() { |
717 | 829 | MediaRecorder mockMediaRecorder = mock(MediaRecorder.class); |
@@ -940,9 +1052,9 @@ public void startVideoRecording_shouldPullStreamsFromMediaRecorderAndImageReader |
940 | 1052 |
|
941 | 1053 | when(cameraFlutterTexture.surfaceTexture()).thenReturn(mockSurfaceTexture); |
942 | 1054 | when(resolutionFeature.getPreviewSize()).thenReturn(mockSize); |
943 | | - doNothing().when(cameraSpy).prepareRecording(); |
| 1055 | + doNothing().when(cameraSpy).prepareRecording(any()); |
944 | 1056 |
|
945 | | - cameraSpy.startVideoRecording(null); |
| 1057 | + cameraSpy.startVideoRecording(null, null); |
946 | 1058 | verify(mockMediaRecorder, times(1)) |
947 | 1059 | .getSurface(); // stream pulled from media recorder's surface. |
948 | 1060 | verify( |
@@ -1362,7 +1474,7 @@ public void startVideoRecording_shouldApplySettingsToMediaRecorder() |
1362 | 1474 | assertNotNull(resolutionFeature); |
1363 | 1475 | when(resolutionFeature.getPreviewSize()).thenReturn(mockSize); |
1364 | 1476 |
|
1365 | | - camera.startVideoRecording(null); |
| 1477 | + camera.startVideoRecording(null, null); |
1366 | 1478 |
|
1367 | 1479 | // region Check that FPS parameter affects AE range at which the camera captures frames. |
1368 | 1480 | assertEquals(camera.cameraFeatures.getFpsRange().getValue().getLower(), Integer.valueOf(fps)); |
|
0 commit comments