|
| 1 | +package io.flutter.plugins.camera.media; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNotNull; |
| 4 | +import static org.mockito.Mockito.*; |
| 5 | + |
| 6 | +import android.media.CamcorderProfile; |
| 7 | +import android.media.MediaRecorder; |
| 8 | +import java.io.IOException; |
| 9 | +import java.lang.reflect.Constructor; |
| 10 | +import org.junit.Test; |
| 11 | +import org.mockito.InOrder; |
| 12 | + |
| 13 | +public class MediaRecorderBuilderTest { |
| 14 | + @Test |
| 15 | + public void ctor_test() { |
| 16 | + MediaRecorderBuilder builder = |
| 17 | + new MediaRecorderBuilder(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P), ""); |
| 18 | + |
| 19 | + assertNotNull(builder); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + public void build_Should_set_values_in_correct_order_When_audio_is_disabled() throws IOException { |
| 24 | + CamcorderProfile recorderProfile = getEmptyCamcorderProfile(); |
| 25 | + MediaRecorderBuilder.MediaRecorderFactory mockFactory = |
| 26 | + mock(MediaRecorderBuilder.MediaRecorderFactory.class); |
| 27 | + MediaRecorder mockMediaRecorder = mock(MediaRecorder.class); |
| 28 | + String outputFilePath = "mock_video_file_path"; |
| 29 | + int mediaOrientation = 1; |
| 30 | + MediaRecorderBuilder builder = |
| 31 | + new MediaRecorderBuilder(recorderProfile, outputFilePath, mockFactory) |
| 32 | + .setEnableAudio(false) |
| 33 | + .setMediaOrientation(mediaOrientation); |
| 34 | + |
| 35 | + when(mockFactory.makeMediaRecorder()).thenReturn(mockMediaRecorder); |
| 36 | + |
| 37 | + MediaRecorder recorder = builder.build(); |
| 38 | + |
| 39 | + InOrder inOrder = inOrder(recorder); |
| 40 | + inOrder.verify(recorder).setVideoSource(MediaRecorder.VideoSource.SURFACE); |
| 41 | + inOrder.verify(recorder).setOutputFormat(recorderProfile.fileFormat); |
| 42 | + inOrder.verify(recorder).setVideoEncoder(recorderProfile.videoCodec); |
| 43 | + inOrder.verify(recorder).setVideoEncodingBitRate(recorderProfile.videoBitRate); |
| 44 | + inOrder.verify(recorder).setVideoFrameRate(recorderProfile.videoFrameRate); |
| 45 | + inOrder |
| 46 | + .verify(recorder) |
| 47 | + .setVideoSize(recorderProfile.videoFrameWidth, recorderProfile.videoFrameHeight); |
| 48 | + inOrder.verify(recorder).setOutputFile(outputFilePath); |
| 49 | + inOrder.verify(recorder).setOrientationHint(mediaOrientation); |
| 50 | + inOrder.verify(recorder).prepare(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void build_Should_set_values_in_correct_order_When_audio_is_enabled() throws IOException { |
| 55 | + CamcorderProfile recorderProfile = getEmptyCamcorderProfile(); |
| 56 | + MediaRecorderBuilder.MediaRecorderFactory mockFactory = |
| 57 | + mock(MediaRecorderBuilder.MediaRecorderFactory.class); |
| 58 | + MediaRecorder mockMediaRecorder = mock(MediaRecorder.class); |
| 59 | + String outputFilePath = "mock_video_file_path"; |
| 60 | + int mediaOrientation = 1; |
| 61 | + MediaRecorderBuilder builder = |
| 62 | + new MediaRecorderBuilder(recorderProfile, outputFilePath, mockFactory) |
| 63 | + .setEnableAudio(true) |
| 64 | + .setMediaOrientation(mediaOrientation); |
| 65 | + |
| 66 | + when(mockFactory.makeMediaRecorder()).thenReturn(mockMediaRecorder); |
| 67 | + |
| 68 | + MediaRecorder recorder = builder.build(); |
| 69 | + |
| 70 | + InOrder inOrder = inOrder(recorder); |
| 71 | + inOrder.verify(recorder).setAudioSource(MediaRecorder.AudioSource.MIC); |
| 72 | + inOrder.verify(recorder).setAudioEncodingBitRate(recorderProfile.audioBitRate); |
| 73 | + inOrder.verify(recorder).setVideoSource(MediaRecorder.VideoSource.SURFACE); |
| 74 | + inOrder.verify(recorder).setOutputFormat(recorderProfile.fileFormat); |
| 75 | + inOrder.verify(recorder).setAudioEncoder(recorderProfile.audioCodec); |
| 76 | + inOrder.verify(recorder).setVideoEncoder(recorderProfile.videoCodec); |
| 77 | + inOrder.verify(recorder).setVideoEncodingBitRate(recorderProfile.videoBitRate); |
| 78 | + inOrder.verify(recorder).setAudioSamplingRate(recorderProfile.audioSampleRate); |
| 79 | + inOrder.verify(recorder).setVideoFrameRate(recorderProfile.videoFrameRate); |
| 80 | + inOrder |
| 81 | + .verify(recorder) |
| 82 | + .setVideoSize(recorderProfile.videoFrameWidth, recorderProfile.videoFrameHeight); |
| 83 | + inOrder.verify(recorder).setOutputFile(outputFilePath); |
| 84 | + inOrder.verify(recorder).setOrientationHint(mediaOrientation); |
| 85 | + inOrder.verify(recorder).prepare(); |
| 86 | + } |
| 87 | + |
| 88 | + private CamcorderProfile getEmptyCamcorderProfile() { |
| 89 | + try { |
| 90 | + Constructor<CamcorderProfile> constructor = |
| 91 | + CamcorderProfile.class.getDeclaredConstructor( |
| 92 | + int.class, int.class, int.class, int.class, int.class, int.class, int.class, |
| 93 | + int.class, int.class, int.class, int.class, int.class); |
| 94 | + |
| 95 | + constructor.setAccessible(true); |
| 96 | + return constructor.newInstance(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); |
| 97 | + } catch (Exception ignored) { |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | +} |
0 commit comments