From 5d01aa50338c2505df6141d8146db300c3e0aa7c Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Thu, 12 Dec 2019 19:24:01 +0100 Subject: [PATCH] Test --- .../engine/CameraIntegrationTest.java | 70 ++++++++++++++----- .../cameraview/tools/RetryRule.java | 1 + .../video/encoding/TextureMediaEncoder.java | 10 ++- 3 files changed, 60 insertions(+), 21 deletions(-) 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 a4ab0c01..24ae88a4 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/engine/CameraIntegrationTest.java @@ -33,6 +33,8 @@ import com.otaliastudios.cameraview.tools.Op; import com.otaliastudios.cameraview.internal.utils.WorkerHandler; import com.otaliastudios.cameraview.overlay.Overlay; import com.otaliastudios.cameraview.size.Size; +import com.otaliastudios.cameraview.tools.Retry; +import com.otaliastudios.cameraview.tools.RetryRule; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -73,9 +75,13 @@ public abstract class CameraIntegrationTest extends Base @Rule public ActivityTestRule activityRule = new ActivityTestRule<>(TestActivity.class); + @Rule + public RetryRule retryRule = new RetryRule(3); + protected CameraView camera; protected E controller; private CameraListener listener; + private Op error; @BeforeClass public static void grant() { @@ -104,27 +110,36 @@ public abstract class CameraIntegrationTest extends Base return controller; } }; - listener = mock(CameraListener.class); camera.setExperimental(true); camera.setEngine(getEngine()); - camera.addCameraListener(listener); activityRule.getActivity().inflate(camera); + } + }); - // Ensure that important CameraExceptions are thrown, otherwise they are just - // logged and the test goes on. - camera.addCameraListener(new CameraListener() { - @Override - public void onCameraError(@NonNull CameraException exception) { - super.onCameraError(exception); - if (exception.isUnrecoverable()) { - LOG.e("[UNRECOVERABLE CAMERAEXCEPTION]", "Got unrecoverable ", - "exception, should throw to help RecoverCameraRule."); - // TODO find a good solution - } - } - }); + listener = mock(CameraListener.class); + camera.addCameraListener(listener); + + error = new Op<>(); + WorkerHandler crashThread = WorkerHandler.get("CrashThread"); + crashThread.getThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(@NonNull Thread thread, @NonNull Throwable exception) { + error.controller().end(exception); } }); + controller.mCrashHandler = crashThread.getHandler(); + + camera.addCameraListener(new CameraListener() { + @Override + public void onCameraError(@NonNull CameraException exception) { + super.onCameraError(exception); + if (exception.isUnrecoverable()) { + LOG.e("[UNRECOVERABLE CAMERAEXCEPTION]", + "Got unrecoverable exception, not clear what to do in a test."); + } + } + }); + } @After @@ -317,6 +332,13 @@ public abstract class CameraIntegrationTest extends Base } } + private void waitForError() throws Throwable { + Throwable throwable = error.await(DELAY); + if (throwable != null) { + throw throwable; + } + } + //region test open/close @Test @@ -554,15 +576,16 @@ public abstract class CameraIntegrationTest extends Base //region test takeVideo - // TODO @Test(expected = RuntimeException.class) + @Test(expected = RuntimeException.class) public void testStartVideo_whileInPictureMode() throws Throwable { camera.setMode(Mode.PICTURE); openSync(true); takeVideoSync(false); - // waitForError(); + waitForError(); } @Test + @Retry(emulatorOnly = true) public void testStartEndVideo() { camera.setMode(Mode.VIDEO); openSync(true); @@ -571,6 +594,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testStartEndVideoSnapshot() { // TODO should check api level for snapshot? openSync(true); @@ -579,6 +603,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testStartEndVideo_withManualStop() { camera.setMode(Mode.VIDEO); openSync(true); @@ -598,6 +623,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testStartEndVideoSnapshot_withManualStop() { openSync(true); takeVideoSnapshotSync(true); @@ -624,6 +650,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testEndVideo_withMaxSize() { camera.setMode(Mode.VIDEO); camera.setVideoSize(SizeSelectors.maxArea(480 * 360)); @@ -637,6 +664,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testEndVideoSnapshot_withMaxSize() { openSync(true); camera.setSnapshotMaxWidth(480); @@ -653,6 +681,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testEndVideo_withMaxDuration() { camera.setMode(Mode.VIDEO); camera.setVideoMaxDuration(4000); @@ -662,6 +691,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testEndVideoSnapshot_withMaxDuration() { camera.setVideoMaxDuration(4000); openSync(true); @@ -758,12 +788,12 @@ public abstract class CameraIntegrationTest extends Base } } - // TODO @Test(expected = RuntimeException.class) + @Test(expected = RuntimeException.class) public void testCapturePicture_whileInVideoMode() throws Throwable { camera.setMode(Mode.VIDEO); openSync(true); camera.takePicture(); - // waitForError(); + waitForError(); camera.takePicture(); } @@ -916,6 +946,7 @@ public abstract class CameraIntegrationTest extends Base @Test + @Retry(emulatorOnly = true) public void testFrameProcessing_afterVideo() throws Exception { FrameProcessor processor = mock(FrameProcessor.class); camera.addFrameProcessor(processor); @@ -964,6 +995,7 @@ public abstract class CameraIntegrationTest extends Base } @Test + @Retry(emulatorOnly = true) public void testOverlay_forVideoSnapshot() { Overlay overlay = mock(Overlay.class); when(overlay.drawsOn(any(Overlay.Target.class))).thenReturn(true); diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/RetryRule.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/RetryRule.java index de0e8478..583ec9e3 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/RetryRule.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/tools/RetryRule.java @@ -38,6 +38,7 @@ public class RetryRule implements TestRule { } catch (Throwable throwable) { LOG.e("[RETRY] Test failed.", retries.get(), "retries available..."); + LOG.e("*******************************************************"); caught = throwable; } } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/TextureMediaEncoder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/TextureMediaEncoder.java index 22853bd2..f5251aaf 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/TextureMediaEncoder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/TextureMediaEncoder.java @@ -192,7 +192,7 @@ public class TextureMediaEncoder extends VideoMediaEncoder { "timestampUs:", frame.timestampUs(), "hasReachedMaxLength:", hasReachedMaxLength(), "thread:", Thread.currentThread(), - "- rendering."); + "- drawing."); // 1. We must scale this matrix like GlCameraPreview does, because it might have some // cropping. Scaling takes place with respect to the (0, 0, 0) point, so we must apply @@ -223,6 +223,12 @@ public class TextureMediaEncoder extends VideoMediaEncoder { Matrix.translateM(mConfig.overlayDrawer.getTransform(), 0, -0.5F, -0.5F, 0); } + LOG.i("onEvent -", + "frameNumber:", mFrameNumber, + "timestampUs:", frame.timestampUs(), + "hasReachedMaxLength:", hasReachedMaxLength(), + "thread:", Thread.currentThread(), + "- gl rendering."); mViewport.drawFrame(frame.timestampUs(), mConfig.textureId, transform); if (mConfig.hasOverlay()) { mConfig.overlayDrawer.render(frame.timestampUs()); @@ -235,7 +241,7 @@ public class TextureMediaEncoder extends VideoMediaEncoder { "timestampUs:", frame.timestampUs(), "hasReachedMaxLength:", hasReachedMaxLength(), "thread:", Thread.currentThread(), - "- rendered."); + "- gl rendered."); } @Override