Fix tests, refactoring

v2
Mattia Iavarone 7 years ago
parent a5f940517b
commit fca9d3fbd8
  1. 1
      MIGRATION.md
  2. 78
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewCallbacksTest.java
  3. 41
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java
  4. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -12,5 +12,6 @@
- getSnapshotSize(): removed. The size of snapshots (pictures and videos) is equal to - getSnapshotSize(): removed. The size of snapshots (pictures and videos) is equal to
the preview size as returned by getPreviewSize(). the preview size as returned by getPreviewSize().
- onVideoTaken(): now passing a VideoResult. Use VideoResult.getFile() to access the video file. - onVideoTaken(): now passing a VideoResult. Use VideoResult.getFile() to access the video file.
- onPictureTaken(): now passing a PictureResult. Use PictureResult.getJpeg() to access the jpeg stream.
- CameraUtils.BitmapCallback: has been moved in a separate BitmapCallback class. - CameraUtils.BitmapCallback: has been moved in a separate BitmapCallback class.
- isCapturingVideo(): renamed to isTakingVideo(). - isCapturingVideo(): renamed to isTakingVideo().

@ -141,6 +141,14 @@ public class CameraViewCallbacksTest extends BaseTest {
verify(listener, times(1)).onVideoTaken(null); verify(listener, times(1)).onVideoTaken(null);
} }
@Test
public void testDispatchOnPictureTaken() {
completeTask().when(listener).onPictureTaken(null);
camera.mCameraCallbacks.dispatchOnPictureTaken(null);
assertNotNull(task.await(200));
verify(listener, times(1)).onPictureTaken(null);
}
@Test @Test
public void testDispatchOnZoomChanged() { public void testDispatchOnZoomChanged() {
completeTask().when(listener).onZoomChanged(anyFloat(), any(float[].class), any(PointF[].class)); completeTask().when(listener).onZoomChanged(anyFloat(), any(float[].class), any(PointF[].class));
@ -210,76 +218,6 @@ public class CameraViewCallbacksTest extends BaseTest {
verify(listener, times(1)).onCameraError(error); verify(listener, times(1)).onCameraError(error);
} }
@Test
public void testProcessJpeg() {
int[] viewDim = new int[]{ 200, 200 };
int[] imageDim = new int[]{ 1000, 1600 };
// With crop flag: expect a 1:1 ratio.
int[] output = testProcessImage(true, true, viewDim, imageDim);
LOG.i("testProcessJpeg", output);
assertEquals(output[0], 1000);
assertEquals(output[1], 1000);
// Without crop flag: expect original ratio.
output = testProcessImage(true, false, viewDim, imageDim);
LOG.i("testProcessJpeg", output);
assertEquals(output[0], imageDim[0]);
assertEquals(output[1], imageDim[1]);
}
@Test
public void testProcessYuv() {
int[] viewDim = new int[]{ 200, 200 };
int[] imageDim = new int[]{ 1000, 1600 };
// With crop flag: expect a 1:1 ratio.
int[] output = testProcessImage(false, true, viewDim, imageDim);
LOG.i("testProcessYuv", output);
assertEquals(output[0], 1000);
assertEquals(output[1], 1000);
// Without crop flag: expect original ratio.
output = testProcessImage(false, false, viewDim, imageDim);
LOG.i("testProcessYuv", output);
assertEquals(output[0], imageDim[0]);
assertEquals(output[1], imageDim[1]);
}
private int[] testProcessImage(boolean jpeg, boolean crop, int[] viewDim, int[] imageDim) {
// End our task when onPictureTaken is called. Take note of the result.
final Task<byte[]> jpegTask = new Task<>(true);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
jpegTask.end((byte[]) invocation.getArguments()[0]);
return null;
}
}).when(listener).onPictureTaken(any(PictureResult.class));
// Fake our own dimensions.
camera.setTop(0);
camera.setBottom(viewDim[1]);
camera.setLeft(0);
camera.setRight(viewDim[0]);
// Ensure the image will (not) be cropped.
mockPreview.setIsCropping(crop);
// Create fake JPEG array and trigger the process.
if (jpeg) {
camera.mCameraCallbacks.processPicture(mockJpeg(imageDim[0], imageDim[1]), true, false);
} else {
camera.mCameraCallbacks.processSnapshot(mockYuv(imageDim[0], imageDim[1]), true, false);
}
// Wait for result and get out dimensions.
byte[] result = jpegTask.await(3000);
assertNotNull("Image was processed", result);
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
return new int[]{ bitmap.getWidth(), bitmap.getHeight() };
}
@Test @Test
public void testProcessFrame() { public void testProcessFrame() {
Frame mock = mock(Frame.class); Frame mock = mock(Frame.class);

@ -38,7 +38,7 @@ import static org.mockito.Mockito.mock;
*/ */
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@MediumTest @MediumTest
@Ignore // @Ignore
public class IntegrationTest extends BaseTest { public class IntegrationTest extends BaseTest {
@Rule @Rule
@ -135,10 +135,10 @@ public class IntegrationTest extends BaseTest {
} }
} }
private byte[] waitForPicture(boolean expectSuccess) { private PictureResult waitForPicture(boolean expectSuccess) {
final Task<byte[]> pic = new Task<>(true); final Task<PictureResult> pic = new Task<>(true);
doEndTask(pic, 0).when(listener).onPictureTaken(any(PictureResult.class)); doEndTask(pic, 0).when(listener).onPictureTaken(any(PictureResult.class));
byte[] result = pic.await(5000); PictureResult result = pic.await(5000);
if (expectSuccess) { if (expectSuccess) {
assertNotNull("Can take picture", result); assertNotNull("Can take picture", result);
} else { } else {
@ -528,17 +528,17 @@ public class IntegrationTest extends BaseTest {
@Test @Test
public void testCapturePicture_size() throws Exception { public void testCapturePicture_size() throws Exception {
// TODO v2: might have to change this
waitForOpen(true); waitForOpen(true);
Size size = camera.getPictureSize(); Size size = camera.getPictureSize();
camera.takePicture(); camera.takePicture();
byte[] jpeg = waitForPicture(true); PictureResult result = waitForPicture(true);
Bitmap b = CameraUtils.decodeBitmap(jpeg, Integer.MAX_VALUE, Integer.MAX_VALUE); Bitmap bitmap = CameraUtils.decodeBitmap(result.getJpeg(), Integer.MAX_VALUE, Integer.MAX_VALUE);
// Result can actually have swapped dimensions assertEquals(result.getSize(), size);
// Which one, depends on factors including device physical orientation assertEquals(bitmap.getWidth(), size.getWidth());
assertTrue(b.getWidth() == size.getHeight() || b.getWidth() == size.getWidth()); assertEquals(bitmap.getHeight(), size.getHeight());
assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth()); assertNotNull(result.getJpeg());
assertNull(result.getLocation());
assertFalse(result.isSnapshot());
} }
@Test @Test
@ -564,17 +564,18 @@ public class IntegrationTest extends BaseTest {
@Test @Test
public void testCaptureSnapshot_size() throws Exception { public void testCaptureSnapshot_size() throws Exception {
// TODO v2: might have to change this
waitForOpen(true); waitForOpen(true);
Size size = camera.getPreviewSize(); Size size = camera.getPreviewSize();
camera.takePictureSnapshot(); camera.takePictureSnapshot();
byte[] jpeg = waitForPicture(true);
Bitmap b = CameraUtils.decodeBitmap(jpeg, Integer.MAX_VALUE, Integer.MAX_VALUE); PictureResult result = waitForPicture(true);
// Result can actually have swapped dimensions Bitmap bitmap = CameraUtils.decodeBitmap(result.getJpeg(), Integer.MAX_VALUE, Integer.MAX_VALUE);
// Which one, depends on factors including device physical orientation assertEquals(result.getSize(), size);
assertTrue(b.getWidth() == size.getHeight() || b.getWidth() == size.getWidth()); assertEquals(bitmap.getWidth(), size.getWidth());
assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth()); assertEquals(bitmap.getHeight(), size.getHeight());
assertNotNull(result.getJpeg());
assertNull(result.getLocation());
assertTrue(result.isSnapshot());
} }
//endregion //endregion

@ -47,7 +47,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
public final static int PERMISSION_REQUEST_CODE = 16; public final static int PERMISSION_REQUEST_CODE = 16;
final static boolean DEFAULT_CROP_OUTPUT = false;
final static boolean DEFAULT_PLAY_SOUNDS = true; final static boolean DEFAULT_PLAY_SOUNDS = true;
// Self managed parameters // Self managed parameters
@ -73,7 +72,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
// Threading // Threading
private Handler mUiHandler; private Handler mUiHandler;
private WorkerHandler mWorkerHandler;
private WorkerHandler mFrameProcessorsHandler; private WorkerHandler mFrameProcessorsHandler;
public CameraView(@NonNull Context context) { public CameraView(@NonNull Context context) {
@ -152,7 +150,6 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
mCameraCallbacks = new Callbacks(); mCameraCallbacks = new Callbacks();
mCameraController = instantiateCameraController(mCameraCallbacks); mCameraController = instantiateCameraController(mCameraCallbacks);
mUiHandler = new Handler(Looper.getMainLooper()); mUiHandler = new Handler(Looper.getMainLooper());
mWorkerHandler = WorkerHandler.get("CameraViewWorker");
mFrameProcessorsHandler = WorkerHandler.get("FrameProcessorsWorker"); mFrameProcessorsHandler = WorkerHandler.get("FrameProcessorsWorker");
// Views // Views

Loading…
Cancel
Save