From 5ed61229c97ec269e25855da642dffd30d12d438 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Thu, 1 Mar 2018 18:50:33 +0100 Subject: [PATCH] Better tests --- .../cameraview/CameraViewTest.java | 14 ++++ .../cameraview/IntegrationTest.java | 23 ++++++- .../cameraview/MockCameraController.java | 10 --- .../com/otaliastudios/cameraview/Camera1.java | 10 --- .../com/otaliastudios/cameraview/Camera2.java | 10 --- .../cameraview/CameraController.java | 21 ++++-- .../otaliastudios/cameraview/CameraView.java | 65 +++++++++++++------ 7 files changed, 97 insertions(+), 56 deletions(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index b3f14516..818cc67d 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -96,6 +96,8 @@ public class CameraViewTest extends BaseTest { assertEquals(cameraView.getLocation(), null); assertEquals(cameraView.getExposureCorrection(), 0f, 0f); assertEquals(cameraView.getZoom(), 0f, 0f); + assertEquals(cameraView.getVideoMaxDuration(), 0, 0); + assertEquals(cameraView.getVideoMaxSize(), 0, 0); // Self managed assertEquals(cameraView.getPlaySounds(), CameraView.DEFAULT_PLAY_SOUNDS); @@ -567,6 +569,18 @@ public class CameraViewTest extends BaseTest { assertEquals(result, source); } + @Test + public void testVideoMaxSize() { + cameraView.setVideoMaxSize(5000); + assertEquals(cameraView.getVideoMaxSize(), 5000); + } + + @Test + public void testVideoMaxDuration() { + cameraView.setVideoMaxDuration(5000); + assertEquals(cameraView.getVideoMaxDuration(), 5000); + } + //endregion //region Lists of listeners and processors diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java index f1c5c2dc..4e42ce62 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java @@ -5,6 +5,7 @@ import android.graphics.Bitmap; import android.graphics.PointF; import android.hardware.Camera; import android.os.Build; +import android.os.Handler; import android.support.test.filters.MediumTest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; @@ -129,9 +130,9 @@ public class IntegrationTest extends BaseTest { doEndTask(video, true).when(listener).onVideoTaken(any(File.class)); Boolean result = video.await(8000); if (expectSuccess) { - assertNotNull("Can take video", result); + assertNotNull("Should end video", result); } else { - assertNull("Should not take video", result); + assertNull("Should not end video", result); } } @@ -463,6 +464,24 @@ public class IntegrationTest extends BaseTest { waitForVideoEnd(false); } + @Test + public void testEndVideo_withMaxSize() { + camera.setSessionType(SessionType.VIDEO); + camera.setVideoMaxSize(500*1000); // 0.5 mb + waitForOpen(true); + waitForVideoStart(); + waitForVideoEnd(true); + } + + @Test + public void testEndVideo_withMaxDuration() { + camera.setSessionType(SessionType.VIDEO); + camera.setVideoMaxDuration(4000); + waitForOpen(true); + waitForVideoStart(); + waitForVideoEnd(true); + } + //endregion //region startAutoFocus diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java index 950fa5a8..5a4b8f08 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraController.java @@ -125,16 +125,6 @@ public class MockCameraController extends CameraController { public void onBufferAvailable(byte[] buffer) { } - @Override - void setVideoMaxSize(long videoMaxSizeInBytes) { - - } - - @Override - void setVideoMaxDuration(int videoMaxDurationMillis) { - - } - @Override void setPlaySounds(boolean playSounds) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index a4fe058c..ed0bede5 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -876,16 +876,6 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera return result; } - @Override - void setVideoMaxSize(long videoMaxSizeInBytes) { - mVideoMaxSize = videoMaxSizeInBytes; - } - - @Override - void setVideoMaxDuration(int videoMaxDurationMillis) { - mVideoMaxDuration = videoMaxDurationMillis; - } - @Override void setPlaySounds(boolean playSounds) { final boolean old = mPlaySounds; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index 3ed92e0e..396fe150 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -115,16 +115,6 @@ class Camera2 extends CameraController { } - @Override - void setVideoMaxSize(long videoMaxSizeInBytes) { - - } - - @Override - void setVideoMaxDuration(int videoMaxDurationMillis) { - - } - @Override void setPlaySounds(boolean playSounds) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 10fe8423..5dd2350e 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -277,6 +277,15 @@ abstract class CameraController implements mPictureSizeSelector = selector; } + final void setVideoMaxSize(long videoMaxSizeBytes) { + mVideoMaxSize = videoMaxSizeBytes; + } + + final void setVideoMaxDuration(int videoMaxDurationMillis) { + mVideoMaxDuration = videoMaxDurationMillis; + } + + //endregion //region Abstract setters and APIs @@ -321,10 +330,6 @@ abstract class CameraController implements abstract void startAutoFocus(@Nullable Gesture gesture, PointF point); - abstract void setVideoMaxSize(long videoMaxSizeInBytes); - - abstract void setVideoMaxDuration(int videoMaxDurationMillis); - abstract void setPlaySounds(boolean playSounds); //endregion @@ -357,6 +362,14 @@ abstract class CameraController implements return mVideoQuality; } + final long getVideoMaxSize() { + return mVideoMaxSize; + } + + final int getVideoMaxDuration() { + return mVideoMaxDuration; + } + final SessionType getSessionType() { return mSessionType; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index af476dbc..9e0705e1 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -1208,8 +1208,7 @@ public class CameraView extends FrameLayout { /** - * Starts recording a video with selected options, in a file called - * "video.mp4" in the default folder. + * Starts recording a video, in a file called "video.mp4" in the default folder. * This is discouraged, please use {@link #startCapturingVideo(File)} instead. * * @deprecated see {@link #startCapturingVideo(File)} @@ -1221,7 +1220,7 @@ public class CameraView extends FrameLayout { /** - * Starts recording a video with selected options. Video will be written to the given file, + * Starts recording a video. Video will be written to the given file, * so callers should ensure they have appropriate permissions to write to the file. * * @param file a file where the video will be saved @@ -1242,29 +1241,29 @@ public class CameraView extends FrameLayout { /** - * Starts recording a video with selected options. Video will be written to the given file, + * Starts recording a video. Video will be written to the given file, * so callers should ensure they have appropriate permissions to write to the file. - * Recording will be automatically stopped after durationMillis, unless - * {@link #stopCapturingVideo()} is not called meanwhile. + * Recording will be automatically stopped after the given duration, overriding + * temporarily any duration limit set by {@link #setVideoMaxDuration(int)}. * * @param file a file where the video will be saved - * @param durationMillis video max duration - * @throws IllegalArgumentException if durationMillis is less than 500 milliseconds + * @param durationMillis recording max duration * - * @deprecated This is not reliable. Please use {@link #setVideoMaxDuration(int)}. + * @deprecated use {@link #setVideoMaxDuration(int)} instead. */ @Deprecated public void startCapturingVideo(File file, long durationMillis) { - if (durationMillis < 500) { - throw new IllegalArgumentException("Video duration can't be < 500 milliseconds"); - } - startCapturingVideo(file); - mUiHandler.postDelayed(new Runnable() { + // TODO: v2: change signature to int, or remove (better). + final int old = getVideoMaxDuration(); + addCameraListener(new CameraListener() { @Override - public void run() { - stopCapturingVideo(); + public void onVideoTaken(File video) { + setVideoMaxDuration(old); + removeCameraListener(this); } - }, durationMillis); + }); + setVideoMaxDuration((int) durationMillis); + startCapturingVideo(file); } @@ -1341,9 +1340,6 @@ public class CameraView extends FrameLayout { } } - //endregion - - //region Sounds @SuppressLint("NewApi") private void playSound(int soundType) { @@ -1353,6 +1349,7 @@ public class CameraView extends FrameLayout { } } + /** * Controls whether CameraView should play sound effects on certain * events (picture taken, focus complete). Note that: @@ -1366,6 +1363,7 @@ public class CameraView extends FrameLayout { mCameraController.setPlaySounds(playSounds); } + /** * Gets the current sound effect behavior. * @@ -1376,6 +1374,7 @@ public class CameraView extends FrameLayout { return mPlaySounds; } + /** * Sets the maximum size in bytes for recorded video files. * Once this size is reached, the recording will automatically stop. @@ -1387,6 +1386,19 @@ public class CameraView extends FrameLayout { mCameraController.setVideoMaxSize(videoMaxSizeInBytes); } + + /** + * Returns the maximum size in bytes for recorded video files, or 0 + * if no size was set. + * + * @see #setVideoMaxSize(long) + * @return the maximum size in bytes + */ + public long getVideoMaxSize() { + return mCameraController.getVideoMaxSize(); + } + + /** * Sets the maximum duration in milliseconds for video recordings. * Once this duration is reached, the recording will automatically stop. @@ -1398,6 +1410,19 @@ public class CameraView extends FrameLayout { mCameraController.setVideoMaxDuration(videoMaxDurationMillis); } + + /** + * Returns the maximum duration in milliseconds for video recordings, or 0 + * if no limit was set. + * + * @see #setVideoMaxDuration(int) + * @return the maximum duration in milliseconds + */ + public int getVideoMaxDuration() { + return mCameraController.getVideoMaxDuration(); + } + + /** * Returns true if the camera is currently recording a video * @return boolean indicating if the camera is recording a video