From 5c1d142d79f53afbe7dfbf33f67e072b321fb597 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Sun, 10 Sep 2017 20:54:42 +0200 Subject: [PATCH] Add picture tests --- .../CameraControllerIntegrationTest.java | 94 ++++++++++++++++++- codecov.yml | 6 +- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java index 9e4275c4..eaebb4d7 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraControllerIntegrationTest.java @@ -2,6 +2,8 @@ package com.otaliastudios.cameraview; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.PointF; import android.support.test.filters.MediumTest; import android.support.test.rule.ActivityTestRule; @@ -115,6 +117,19 @@ public class CameraControllerIntegrationTest extends BaseTest { return result; } + private byte[] waitForPicture(boolean expectSuccess) { + final Task pic = new Task<>(); + pic.listen(); + doEndTask(pic, 0).when(listener).onPictureTaken(any(byte[].class)); + byte[] result = pic.await(2000); + if (expectSuccess) { + assertNotNull("Can take picture", result); + } else { + assertNull("Should not take picture", result); + } + return result; + } + //region test open/close @Test @@ -384,7 +399,82 @@ public class CameraControllerIntegrationTest extends BaseTest { //endregion - // TODO: capturePicture + //region capture + + @Test + public void testCapturePicture_beforeStarted() { + camera.capturePicture(); + waitForPicture(false); + } + + @Test + public void testCapturePicture_concurrentCalls() throws Exception { + // Second take should fail. + camera.start(); + waitForOpen(true); + + CountDownLatch latch = new CountDownLatch(2); + doCountDown(latch).when(listener).onPictureTaken(any(byte[].class)); + + camera.capturePicture(); + camera.capturePicture(); + boolean did = latch.await(4, TimeUnit.SECONDS); + assertFalse(did); + assertEquals(latch.getCount(), 1); + } + + @Test + public void testCapturePicture_size() throws Exception { + camera.setCropOutput(false); + camera.start(); + waitForOpen(true); + + Size size = camera.getCaptureSize(); + camera.capturePicture(); + byte[] jpeg = waitForPicture(true); + Bitmap b = CameraUtils.decodeBitmap(jpeg); + // Result can actually have swapped dimensions + // Which one, depends on factors including device physical orientation + assertTrue(b.getWidth() == size.getHeight() || b.getWidth() == size.getWidth()); + assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth()); + } + + @Test + public void testCaptureSnapshot_beforeStarted() { + camera.captureSnapshot(); + waitForPicture(false); + } + + @Test + public void testCaptureSnapshot_concurrentCalls() throws Exception { + // Second take should fail. + camera.start(); + waitForOpen(true); + + CountDownLatch latch = new CountDownLatch(2); + doCountDown(latch).when(listener).onPictureTaken(any(byte[].class)); + + camera.captureSnapshot(); + camera.captureSnapshot(); + boolean did = latch.await(4, TimeUnit.SECONDS); + assertFalse(did); + assertEquals(latch.getCount(), 1); + } + + @Test + public void testCaptureSnapshot_size() throws Exception { + camera.setCropOutput(false); + camera.start(); + waitForOpen(true); + + Size size = camera.getPreviewSize(); + camera.captureSnapshot(); + byte[] jpeg = waitForPicture(true); + Bitmap b = CameraUtils.decodeBitmap(jpeg); + // Result can actually have swapped dimensions + // Which one, depends on factors including device physical orientation + assertTrue(b.getWidth() == size.getHeight() || b.getWidth() == size.getWidth()); + assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth()); + } - // TODO: captureSnapshot } diff --git a/codecov.yml b/codecov.yml index 8bbf156c..9e81d1dc 100644 --- a/codecov.yml +++ b/codecov.yml @@ -1,15 +1,15 @@ coverage: precision: 1 round: down - range: "30...100" + range: "40...100" status: project: default: - target: 50% + target: 65% patch: default: - target: 60% + target: 70% changes: no comment: