From ab9a1148269266a2285f0aee85ba87404232e142 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Wed, 4 Sep 2019 21:19:08 +0200 Subject: [PATCH] Fix tests --- cameraview/build.gradle | 5 +++- .../engine/CameraIntegration2Test.java | 27 +++++++++++++++++-- .../engine/CameraIntegrationTest.java | 18 ++++++++----- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/cameraview/build.gradle b/cameraview/build.gradle index 93e82cf4..4d47c6cf 100644 --- a/cameraview/build.gradle +++ b/cameraview/build.gradle @@ -240,9 +240,12 @@ task mergedCoverageReport(type: JacocoReport) { classFilter.add('**/com/otaliastudios/cameraview/engine/CameraEngine**.*') classFilter.add('**/com/otaliastudios/cameraview/engine/Camera1Engine**.*') classFilter.add('**/com/otaliastudios/cameraview/engine/Camera2Engine**.*') + classFilter.add('**/com/otaliastudios/cameraview/engine/action/**.*') + classFilter.add('**/com/otaliastudios/cameraview/engine/lock/**.*') + classFilter.add('**/com/otaliastudios/cameraview/engine/meter/**.*') classFilter.add('**/com/otaliastudios/cameraview/picture/**.*') classFilter.add('**/com/otaliastudios/cameraview/video/**.*') - // TODO these below could be testable ALSO outside of the integration tests + // TODO these below could be easily testable ALSO outside of the integration tests classFilter.add('**/com/otaliastudios/cameraview/video/encoding/**.*') } // We don't test OpenGL filters. diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegration2Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegration2Test.java index 1995eeee..53eb9aa9 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegration2Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegration2Test.java @@ -1,7 +1,12 @@ package com.otaliastudios.cameraview.engine; +import android.hardware.camera2.CaptureRequest; +import android.hardware.camera2.TotalCaptureResult; + import com.otaliastudios.cameraview.DoNotRunOnTravis; import com.otaliastudios.cameraview.controls.Engine; +import com.otaliastudios.cameraview.engine.action.ActionHolder; +import com.otaliastudios.cameraview.engine.action.BaseAction; import org.junit.Ignore; import org.junit.Test; @@ -11,6 +16,8 @@ import androidx.annotation.NonNull; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; +import java.util.concurrent.CountDownLatch; + /** * These tests work great on real devices, and are the only way to test actual CameraEngine * implementation - we really need to open the camera device. @@ -29,7 +36,23 @@ public class CameraIntegration2Test extends CameraIntegrationTest { } @Override - public void testFrameProcessing_afterVideo() throws Exception { - super.testFrameProcessing_afterVideo(); + protected void onOpenSync() { + super.onOpenSync(); + // Extra wait for the first frame to be dispatched. + // This is because various classes require getLastResult to be non-null + // and that's typically the case in a real app. + Camera2Engine engine = (Camera2Engine) controller; + final CountDownLatch latch = new CountDownLatch(1); + new BaseAction() { + @Override + public void onCaptureCompleted(@NonNull ActionHolder holder, + @NonNull CaptureRequest request, + @NonNull TotalCaptureResult result) { + super.onCaptureCompleted(holder, request, result); + latch.countDown(); + setState(STATE_COMPLETED); + } + }.start(engine); + try { latch.await(); } catch (InterruptedException ignore) {} } } diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java index 9f95dfe4..f6f80e6d 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java @@ -71,7 +71,7 @@ public abstract class CameraIntegrationTest extends BaseTest { public ActivityTestRule rule = new ActivityTestRule<>(TestActivity.class); private CameraView camera; - private CameraEngine controller; + protected CameraEngine controller; private CameraListener listener; private Op uiExceptionOp; @@ -136,7 +136,6 @@ public abstract class CameraIntegrationTest extends BaseTest { } } - @SuppressWarnings("StatementWithEmptyBody") private CameraOptions openSync(boolean expectSuccess) { camera.open(); final Op open = new Op<>(true); @@ -144,17 +143,22 @@ public abstract class CameraIntegrationTest extends BaseTest { CameraOptions result = open.await(DELAY); if (expectSuccess) { assertNotNull("Can open", result); - // Extra wait for the bind and preview state, so we run tests in a fully operational - // state. If we didn't do so, we could have null values, for example, in getPictureSize - // or in getSnapshotSize. - while (controller.getBindState() != CameraEngine.STATE_STARTED) {} - while (controller.getPreviewState() != CameraEngine.STATE_STARTED) {} + onOpenSync(); } else { assertNull("Should not open", result); } return result; } + @SuppressWarnings("StatementWithEmptyBody") + protected void onOpenSync() { + // Extra wait for the bind and preview state, so we run tests in a fully operational + // state. If we didn't do so, we could have null values, for example, in getPictureSize + // or in getSnapshotSize. + while (controller.getBindState() != CameraEngine.STATE_STARTED) {} + while (controller.getPreviewState() != CameraEngine.STATE_STARTED) {} + } + private void closeSync(boolean expectSuccess) { camera.close(); final Op close = new Op<>(true);