From 5ea3fc8649925154611f1232c23e49c979c7db87 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Mon, 11 Sep 2017 16:51:41 +0200 Subject: [PATCH] Better GridLinesLayoutTest --- .travis.yml | 5 +- .../cameraview/GridLinesLayoutTest.java | 44 +++++------ .../cameraview/IntegrationTest.java | 75 +++++++++++-------- .../cameraview/GridLinesLayout.java | 8 +- 4 files changed, 73 insertions(+), 59 deletions(-) diff --git a/.travis.yml b/.travis.yml index cf2f7396..9ff67bc9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,6 +33,7 @@ install: - echo $ANDROID_HOME # We assume this is correctly set when setting path - sdkmanager --list || true # Look at the packages - echo yes | sdkmanager "tools" # Ensure tools is updated + - echo yes | sdkmanager "emulator" # Ensure emulator is present # Install emulator - export EMULATOR="system-images;android-$EMULATOR_API;$EMULATOR_TAG;$EMULATOR_ABI" @@ -54,7 +55,9 @@ before_script: - adb shell input keyevent 82 & # Dispatch event script: - - ./gradlew clean testDebugUnitTest connectedCheck mergedCoverageReport + - ./gradlew clean testDebugUnitTest + - ./gradlew --info connectedCheck # Want info here + - ./gradlew mergedCoverageReport after_success: - bash <(curl -s https://codecov.io/bash) -s "*/build/reports/mergedCoverageReport/" diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java index 8869ce10..30260606 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java @@ -2,8 +2,6 @@ package com.otaliastudios.cameraview; import android.graphics.Canvas; -import android.graphics.drawable.ColorDrawable; -import android.support.test.InstrumentationRegistry; import android.support.test.filters.MediumTest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; @@ -14,6 +12,7 @@ import org.junit.Test; import org.junit.runner.RunWith; import static org.mockito.Mockito.*; +import static org.junit.Assert.*; @RunWith(AndroidJUnit4.class) @MediumTest @@ -32,47 +31,44 @@ public class GridLinesLayoutTest extends BaseTest { TestActivity a = rule.getActivity(); layout = new GridLinesLayout(a); layout.setGridMode(Grid.OFF); + layout.drawTask.listen(); a.getContentView().addView(layout); - layout.vert = mock(ColorDrawable.class); - layout.horiz = mock(ColorDrawable.class); } }); + // Wait for first draw. + layout.drawTask.await(1000); + } + + private int setGridAndWait(Grid value) { + layout.drawTask.listen(); + layout.setGridMode(value); + Integer result = layout.drawTask.await(1000); + assertNotNull(result); + return result; } @Test public void testOff() { - layout.drawTask.listen(); - layout.setGridMode(Grid.OFF); - layout.drawTask.await(); - verify(layout.vert, never()).draw(any(Canvas.class)); - verify(layout.horiz, never()).draw(any(Canvas.class)); + int linesDrawn = setGridAndWait(Grid.OFF); + assertEquals(linesDrawn, 0); } @Test public void test3x3() { - layout.drawTask.listen(); - layout.setGridMode(Grid.DRAW_3X3); - layout.drawTask.await(); - verify(layout.vert, times(2)).draw(any(Canvas.class)); - verify(layout.horiz, times(2)).draw(any(Canvas.class)); + int linesDrawn = setGridAndWait(Grid.DRAW_3X3); + assertEquals(linesDrawn, 2); } @Test public void testPhi() { - layout.drawTask.listen(); - layout.setGridMode(Grid.DRAW_PHI); - layout.drawTask.await(); - verify(layout.vert, times(2)).draw(any(Canvas.class)); - verify(layout.horiz, times(2)).draw(any(Canvas.class)); + int linesDrawn = setGridAndWait(Grid.DRAW_PHI); + assertEquals(linesDrawn, 2); } @Test public void test4x4() { - layout.drawTask.listen(); - layout.setGridMode(Grid.DRAW_4X4); - layout.drawTask.await(); - verify(layout.vert, times(3)).draw(any(Canvas.class)); - verify(layout.horiz, times(3)).draw(any(Canvas.class)); + int linesDrawn = setGridAndWait(Grid.DRAW_4X4); + assertEquals(linesDrawn, 3); } } diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java index d34f950f..9af7a17e 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java @@ -39,7 +39,6 @@ public class IntegrationTest extends BaseTest { @Override public void run() { camera = new CameraView(rule.getActivity()) { - @Override protected CameraController instantiateCameraController(CameraCallbacks callbacks, Preview preview) { controller = new Camera1(callbacks, preview); @@ -115,7 +114,7 @@ public class IntegrationTest extends BaseTest { //region test open/close - @Test + //-@Test public void testOpenClose() throws Exception { // Starting and stopping are hard to get since they happen on another thread. assertEquals(controller.getState(), CameraController.STATE_STOPPED); @@ -129,7 +128,7 @@ public class IntegrationTest extends BaseTest { assertEquals(controller.getState(), CameraController.STATE_STOPPED); } - @Test + //-@Test public void testOpenTwice() { camera.start(); waitForOpen(true); @@ -137,14 +136,14 @@ public class IntegrationTest extends BaseTest { waitForOpen(false); } - @Test + //-@Test public void testCloseTwice() { camera.stop(); waitForClose(false); } // @Test - // This works great on the device but crashes on the emulator. + // TODO: This works great on the device but crashes on the emulator. // There must be something wrong with the emulated camera... // Like stopPreview() and release() are not really sync calls? public void testConcurrentCalls() throws Exception { @@ -161,7 +160,7 @@ public class IntegrationTest extends BaseTest { assertTrue("Handles concurrent calls to start & stop, " + latch.getCount(), did); } - @Test + //-@Test public void testStartInitializesOptions() { assertNull(camera.getCameraOptions()); assertNull(camera.getExtraProperties()); @@ -176,7 +175,7 @@ public class IntegrationTest extends BaseTest { //region test Facing/SessionType // Test things that should reset the camera. - @Test + //-@Test public void testSetFacing() throws Exception { camera.start(); CameraOptions o = waitForOpen(true); @@ -194,7 +193,7 @@ public class IntegrationTest extends BaseTest { } } - @Test + //-@Test public void testSetSessionType() throws Exception { camera.setSessionType(SessionType.PICTURE); camera.start(); @@ -217,7 +216,7 @@ public class IntegrationTest extends BaseTest { //region test Set Parameters // When camera is open, parameters will be set only if supported. - @Test + //-@Test public void testSetZoom() { camera.start(); CameraOptions options = waitForOpen(true); @@ -228,7 +227,7 @@ public class IntegrationTest extends BaseTest { assertEquals(can ? newValue : oldValue, camera.getZoom(), 0f); } - @Test + //-@Test public void testSetExposureCorrection() { camera.start(); CameraOptions options = waitForOpen(true); @@ -239,7 +238,7 @@ public class IntegrationTest extends BaseTest { assertEquals(can ? newValue : oldValue, camera.getExposureCorrection(), 0f); } - @Test + //-@Test public void testSetFlash() { camera.start(); CameraOptions options = waitForOpen(true); @@ -256,7 +255,7 @@ public class IntegrationTest extends BaseTest { } } - @Test + //-@Test public void testSetWhiteBalance() { camera.start(); CameraOptions options = waitForOpen(true); @@ -273,7 +272,7 @@ public class IntegrationTest extends BaseTest { } } - @Test + //-@Test public void testSetHdr() { camera.start(); CameraOptions options = waitForOpen(true); @@ -290,7 +289,7 @@ public class IntegrationTest extends BaseTest { } } - @Test + //-@Test public void testSetAudio() { // TODO: when permissions are managed, check that Audio.ON triggers the audio permission camera.start(); @@ -302,14 +301,26 @@ public class IntegrationTest extends BaseTest { } } + //-@Test + public void testSetLocation() { + camera.start(); + waitForOpen(true); + camera.setLocation(10d, 2d); + assertNotNull(camera.getLocation()); + assertEquals(camera.getLocation().getLatitude(), 10d, 0d); + assertEquals(camera.getLocation().getLongitude(), 2d, 0d); + // This also ensures there are no crashes when attaching it to camera parameters. + } + //endregion //region testSetVideoQuality // This can be tricky because can trigger layout changes. // @Test(expected = IllegalStateException.class) - // TODO: Removing. See comments below in the video region. Video is - // not reliable on emulators, MediaRecorder might not be supported. + // TODO: Can't run on Travis, MediaRecorder not supported. + // Error while starting MediaRecorder. java.lang.RuntimeException: start failed. + @Test public void testSetVideoQuality_whileRecording() { camera.setSessionType(SessionType.VIDEO); camera.setVideoQuality(VideoQuality.HIGHEST); @@ -317,9 +328,10 @@ public class IntegrationTest extends BaseTest { waitForOpen(true); camera.startCapturingVideo(null); camera.setVideoQuality(VideoQuality.LOWEST); + throw new RuntimeException(CameraLogger.lastTag + ":" + CameraLogger.lastMessage); } - @Test + //-@Test public void testSetVideoQuality_whileInPictureSessionType() { camera.setSessionType(SessionType.PICTURE); camera.setVideoQuality(VideoQuality.HIGHEST); @@ -329,7 +341,7 @@ public class IntegrationTest extends BaseTest { assertEquals(camera.getVideoQuality(), VideoQuality.LOWEST); } - @Test + //-@Test public void testSetVideoQuality_whileNotStarted() { camera.setVideoQuality(VideoQuality.HIGHEST); assertEquals(camera.getVideoQuality(), VideoQuality.HIGHEST); @@ -337,7 +349,7 @@ public class IntegrationTest extends BaseTest { assertEquals(camera.getVideoQuality(), VideoQuality.LOWEST); } - @Test + //-@Test public void testSetVideoQuality_shouldRecompute() { // If video quality changes bring to a new capture size, // this might bring to a new aspect ratio, @@ -350,7 +362,9 @@ public class IntegrationTest extends BaseTest { //region test startVideo // @Test(expected = IllegalStateException.class) - // TODO: fails on Travis. Might be that some emulators can't deal with MediaRecorder + // TODO: fails on Travis. Some emulators can't deal with MediaRecorder + // Error while starting MediaRecorder. java.lang.RuntimeException: start failed. + // as documented. This works locally though. public void testStartVideo_whileInPictureMode() { camera.setSessionType(SessionType.PICTURE); camera.start(); @@ -359,8 +373,9 @@ public class IntegrationTest extends BaseTest { } // @Test - // TODO: fails on Travis. Might be that some emulators can't deal with MediaRecorder, - // as documented. Or the File passed is invalid. Hard to know, this works locally. + // TODO: fails on Travis. Some emulators can't deal with MediaRecorder, + // Error while starting MediaRecorder. java.lang.RuntimeException: start failed. + // as documented. This works locally though. public void testStartEndVideo() { camera.setSessionType(SessionType.VIDEO); camera.start(); @@ -369,7 +384,7 @@ public class IntegrationTest extends BaseTest { waitForVideo(true); // waits 2000 } - @Test + //-@Test public void testEndVideo_withoutStarting() { camera.setSessionType(SessionType.VIDEO); camera.start(); @@ -383,7 +398,7 @@ public class IntegrationTest extends BaseTest { //region startAutoFocus // TODO: won't test onStopAutoFocus because that is not guaranteed to be called - @Test + //-@Test public void testStartAutoFocus() { camera.start(); CameraOptions o = waitForOpen(true); @@ -399,13 +414,13 @@ public class IntegrationTest extends BaseTest { //region capture - @Test + //-@Test public void testCapturePicture_beforeStarted() { camera.capturePicture(); waitForPicture(false); } - @Test + //-@Test public void testCapturePicture_concurrentCalls() throws Exception { // Second take should fail. camera.start(); @@ -421,7 +436,7 @@ public class IntegrationTest extends BaseTest { assertEquals(latch.getCount(), 1); } - @Test + //-@Test public void testCapturePicture_size() throws Exception { camera.setCropOutput(false); camera.start(); @@ -437,13 +452,13 @@ public class IntegrationTest extends BaseTest { assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth()); } - @Test + //-@Test public void testCaptureSnapshot_beforeStarted() { camera.captureSnapshot(); waitForPicture(false); } - @Test + //-@Test public void testCaptureSnapshot_concurrentCalls() throws Exception { // Second take should fail. camera.start(); @@ -459,7 +474,7 @@ public class IntegrationTest extends BaseTest { assertEquals(latch.getCount(), 1); } - @Test + //-@Test public void testCaptureSnapshot_size() throws Exception { camera.setCropOutput(false); camera.start(); diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java index 0d7784ea..c84cdcae 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java @@ -16,11 +16,11 @@ class GridLinesLayout extends View { private Grid gridMode; - Drawable horiz; - Drawable vert; + private Drawable horiz; + private Drawable vert; private final float width; - Task drawTask = new Task<>(); + Task drawTask = new Task<>(); private final static float GOLDEN_RATIO_INV = 0.61803398874989f; @@ -92,6 +92,6 @@ class GridLinesLayout extends View { vert.draw(canvas); canvas.translate(- pos * getWidth(), 0); } - drawTask.end(null); + drawTask.end(count); } }