Better GridLinesLayoutTest

pull/37/head
Mattia Iavarone 8 years ago
parent 9daa144e2d
commit 5ea3fc8649
  1. 5
      .travis.yml
  2. 44
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java
  3. 75
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java
  4. 8
      cameraview/src/main/views/com/otaliastudios/cameraview/GridLinesLayout.java

@ -33,6 +33,7 @@ install:
- echo $ANDROID_HOME # We assume this is correctly set when setting path - echo $ANDROID_HOME # We assume this is correctly set when setting path
- sdkmanager --list || true # Look at the packages - sdkmanager --list || true # Look at the packages
- echo yes | sdkmanager "tools" # Ensure tools is updated - echo yes | sdkmanager "tools" # Ensure tools is updated
- echo yes | sdkmanager "emulator" # Ensure emulator is present
# Install emulator # Install emulator
- export EMULATOR="system-images;android-$EMULATOR_API;$EMULATOR_TAG;$EMULATOR_ABI" - export EMULATOR="system-images;android-$EMULATOR_API;$EMULATOR_TAG;$EMULATOR_ABI"
@ -54,7 +55,9 @@ before_script:
- adb shell input keyevent 82 & # Dispatch event - adb shell input keyevent 82 & # Dispatch event
script: script:
- ./gradlew clean testDebugUnitTest connectedCheck mergedCoverageReport - ./gradlew clean testDebugUnitTest
- ./gradlew --info connectedCheck # Want info here
- ./gradlew mergedCoverageReport
after_success: after_success:
- bash <(curl -s https://codecov.io/bash) -s "*/build/reports/mergedCoverageReport/" - bash <(curl -s https://codecov.io/bash) -s "*/build/reports/mergedCoverageReport/"

@ -2,8 +2,6 @@ package com.otaliastudios.cameraview;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.drawable.ColorDrawable;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest; import android.support.test.filters.MediumTest;
import android.support.test.rule.ActivityTestRule; import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
@ -14,6 +12,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class) @RunWith(AndroidJUnit4.class)
@MediumTest @MediumTest
@ -32,47 +31,44 @@ public class GridLinesLayoutTest extends BaseTest {
TestActivity a = rule.getActivity(); TestActivity a = rule.getActivity();
layout = new GridLinesLayout(a); layout = new GridLinesLayout(a);
layout.setGridMode(Grid.OFF); layout.setGridMode(Grid.OFF);
layout.drawTask.listen();
a.getContentView().addView(layout); 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 @Test
public void testOff() { public void testOff() {
layout.drawTask.listen(); int linesDrawn = setGridAndWait(Grid.OFF);
layout.setGridMode(Grid.OFF); assertEquals(linesDrawn, 0);
layout.drawTask.await();
verify(layout.vert, never()).draw(any(Canvas.class));
verify(layout.horiz, never()).draw(any(Canvas.class));
} }
@Test @Test
public void test3x3() { public void test3x3() {
layout.drawTask.listen(); int linesDrawn = setGridAndWait(Grid.DRAW_3X3);
layout.setGridMode(Grid.DRAW_3X3); assertEquals(linesDrawn, 2);
layout.drawTask.await();
verify(layout.vert, times(2)).draw(any(Canvas.class));
verify(layout.horiz, times(2)).draw(any(Canvas.class));
} }
@Test @Test
public void testPhi() { public void testPhi() {
layout.drawTask.listen(); int linesDrawn = setGridAndWait(Grid.DRAW_PHI);
layout.setGridMode(Grid.DRAW_PHI); assertEquals(linesDrawn, 2);
layout.drawTask.await();
verify(layout.vert, times(2)).draw(any(Canvas.class));
verify(layout.horiz, times(2)).draw(any(Canvas.class));
} }
@Test @Test
public void test4x4() { public void test4x4() {
layout.drawTask.listen(); int linesDrawn = setGridAndWait(Grid.DRAW_4X4);
layout.setGridMode(Grid.DRAW_4X4); assertEquals(linesDrawn, 3);
layout.drawTask.await();
verify(layout.vert, times(3)).draw(any(Canvas.class));
verify(layout.horiz, times(3)).draw(any(Canvas.class));
} }
} }

@ -39,7 +39,6 @@ public class IntegrationTest extends BaseTest {
@Override @Override
public void run() { public void run() {
camera = new CameraView(rule.getActivity()) { camera = new CameraView(rule.getActivity()) {
@Override @Override
protected CameraController instantiateCameraController(CameraCallbacks callbacks, Preview preview) { protected CameraController instantiateCameraController(CameraCallbacks callbacks, Preview preview) {
controller = new Camera1(callbacks, preview); controller = new Camera1(callbacks, preview);
@ -115,7 +114,7 @@ public class IntegrationTest extends BaseTest {
//region test open/close //region test open/close
@Test //-@Test
public void testOpenClose() throws Exception { public void testOpenClose() throws Exception {
// Starting and stopping are hard to get since they happen on another thread. // Starting and stopping are hard to get since they happen on another thread.
assertEquals(controller.getState(), CameraController.STATE_STOPPED); assertEquals(controller.getState(), CameraController.STATE_STOPPED);
@ -129,7 +128,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(controller.getState(), CameraController.STATE_STOPPED); assertEquals(controller.getState(), CameraController.STATE_STOPPED);
} }
@Test //-@Test
public void testOpenTwice() { public void testOpenTwice() {
camera.start(); camera.start();
waitForOpen(true); waitForOpen(true);
@ -137,14 +136,14 @@ public class IntegrationTest extends BaseTest {
waitForOpen(false); waitForOpen(false);
} }
@Test //-@Test
public void testCloseTwice() { public void testCloseTwice() {
camera.stop(); camera.stop();
waitForClose(false); waitForClose(false);
} }
// @Test // @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... // There must be something wrong with the emulated camera...
// Like stopPreview() and release() are not really sync calls? // Like stopPreview() and release() are not really sync calls?
public void testConcurrentCalls() throws Exception { public void testConcurrentCalls() throws Exception {
@ -161,7 +160,7 @@ public class IntegrationTest extends BaseTest {
assertTrue("Handles concurrent calls to start & stop, " + latch.getCount(), did); assertTrue("Handles concurrent calls to start & stop, " + latch.getCount(), did);
} }
@Test //-@Test
public void testStartInitializesOptions() { public void testStartInitializesOptions() {
assertNull(camera.getCameraOptions()); assertNull(camera.getCameraOptions());
assertNull(camera.getExtraProperties()); assertNull(camera.getExtraProperties());
@ -176,7 +175,7 @@ public class IntegrationTest extends BaseTest {
//region test Facing/SessionType //region test Facing/SessionType
// Test things that should reset the camera. // Test things that should reset the camera.
@Test //-@Test
public void testSetFacing() throws Exception { public void testSetFacing() throws Exception {
camera.start(); camera.start();
CameraOptions o = waitForOpen(true); CameraOptions o = waitForOpen(true);
@ -194,7 +193,7 @@ public class IntegrationTest extends BaseTest {
} }
} }
@Test //-@Test
public void testSetSessionType() throws Exception { public void testSetSessionType() throws Exception {
camera.setSessionType(SessionType.PICTURE); camera.setSessionType(SessionType.PICTURE);
camera.start(); camera.start();
@ -217,7 +216,7 @@ public class IntegrationTest extends BaseTest {
//region test Set Parameters //region test Set Parameters
// When camera is open, parameters will be set only if supported. // When camera is open, parameters will be set only if supported.
@Test //-@Test
public void testSetZoom() { public void testSetZoom() {
camera.start(); camera.start();
CameraOptions options = waitForOpen(true); CameraOptions options = waitForOpen(true);
@ -228,7 +227,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(can ? newValue : oldValue, camera.getZoom(), 0f); assertEquals(can ? newValue : oldValue, camera.getZoom(), 0f);
} }
@Test //-@Test
public void testSetExposureCorrection() { public void testSetExposureCorrection() {
camera.start(); camera.start();
CameraOptions options = waitForOpen(true); CameraOptions options = waitForOpen(true);
@ -239,7 +238,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(can ? newValue : oldValue, camera.getExposureCorrection(), 0f); assertEquals(can ? newValue : oldValue, camera.getExposureCorrection(), 0f);
} }
@Test //-@Test
public void testSetFlash() { public void testSetFlash() {
camera.start(); camera.start();
CameraOptions options = waitForOpen(true); CameraOptions options = waitForOpen(true);
@ -256,7 +255,7 @@ public class IntegrationTest extends BaseTest {
} }
} }
@Test //-@Test
public void testSetWhiteBalance() { public void testSetWhiteBalance() {
camera.start(); camera.start();
CameraOptions options = waitForOpen(true); CameraOptions options = waitForOpen(true);
@ -273,7 +272,7 @@ public class IntegrationTest extends BaseTest {
} }
} }
@Test //-@Test
public void testSetHdr() { public void testSetHdr() {
camera.start(); camera.start();
CameraOptions options = waitForOpen(true); CameraOptions options = waitForOpen(true);
@ -290,7 +289,7 @@ public class IntegrationTest extends BaseTest {
} }
} }
@Test //-@Test
public void testSetAudio() { public void testSetAudio() {
// TODO: when permissions are managed, check that Audio.ON triggers the audio permission // TODO: when permissions are managed, check that Audio.ON triggers the audio permission
camera.start(); 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 //endregion
//region testSetVideoQuality //region testSetVideoQuality
// This can be tricky because can trigger layout changes. // This can be tricky because can trigger layout changes.
// @Test(expected = IllegalStateException.class) // @Test(expected = IllegalStateException.class)
// TODO: Removing. See comments below in the video region. Video is // TODO: Can't run on Travis, MediaRecorder not supported.
// not reliable on emulators, MediaRecorder might not be supported. // Error while starting MediaRecorder. java.lang.RuntimeException: start failed.
@Test
public void testSetVideoQuality_whileRecording() { public void testSetVideoQuality_whileRecording() {
camera.setSessionType(SessionType.VIDEO); camera.setSessionType(SessionType.VIDEO);
camera.setVideoQuality(VideoQuality.HIGHEST); camera.setVideoQuality(VideoQuality.HIGHEST);
@ -317,9 +328,10 @@ public class IntegrationTest extends BaseTest {
waitForOpen(true); waitForOpen(true);
camera.startCapturingVideo(null); camera.startCapturingVideo(null);
camera.setVideoQuality(VideoQuality.LOWEST); camera.setVideoQuality(VideoQuality.LOWEST);
throw new RuntimeException(CameraLogger.lastTag + ":" + CameraLogger.lastMessage);
} }
@Test //-@Test
public void testSetVideoQuality_whileInPictureSessionType() { public void testSetVideoQuality_whileInPictureSessionType() {
camera.setSessionType(SessionType.PICTURE); camera.setSessionType(SessionType.PICTURE);
camera.setVideoQuality(VideoQuality.HIGHEST); camera.setVideoQuality(VideoQuality.HIGHEST);
@ -329,7 +341,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(camera.getVideoQuality(), VideoQuality.LOWEST); assertEquals(camera.getVideoQuality(), VideoQuality.LOWEST);
} }
@Test //-@Test
public void testSetVideoQuality_whileNotStarted() { public void testSetVideoQuality_whileNotStarted() {
camera.setVideoQuality(VideoQuality.HIGHEST); camera.setVideoQuality(VideoQuality.HIGHEST);
assertEquals(camera.getVideoQuality(), VideoQuality.HIGHEST); assertEquals(camera.getVideoQuality(), VideoQuality.HIGHEST);
@ -337,7 +349,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(camera.getVideoQuality(), VideoQuality.LOWEST); assertEquals(camera.getVideoQuality(), VideoQuality.LOWEST);
} }
@Test //-@Test
public void testSetVideoQuality_shouldRecompute() { public void testSetVideoQuality_shouldRecompute() {
// If video quality changes bring to a new capture size, // If video quality changes bring to a new capture size,
// this might bring to a new aspect ratio, // this might bring to a new aspect ratio,
@ -350,7 +362,9 @@ public class IntegrationTest extends BaseTest {
//region test startVideo //region test startVideo
// @Test(expected = IllegalStateException.class) // @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() { public void testStartVideo_whileInPictureMode() {
camera.setSessionType(SessionType.PICTURE); camera.setSessionType(SessionType.PICTURE);
camera.start(); camera.start();
@ -359,8 +373,9 @@ public class IntegrationTest extends BaseTest {
} }
// @Test // @Test
// 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,
// as documented. Or the File passed is invalid. Hard to know, this works locally. // Error while starting MediaRecorder. java.lang.RuntimeException: start failed.
// as documented. This works locally though.
public void testStartEndVideo() { public void testStartEndVideo() {
camera.setSessionType(SessionType.VIDEO); camera.setSessionType(SessionType.VIDEO);
camera.start(); camera.start();
@ -369,7 +384,7 @@ public class IntegrationTest extends BaseTest {
waitForVideo(true); // waits 2000 waitForVideo(true); // waits 2000
} }
@Test //-@Test
public void testEndVideo_withoutStarting() { public void testEndVideo_withoutStarting() {
camera.setSessionType(SessionType.VIDEO); camera.setSessionType(SessionType.VIDEO);
camera.start(); camera.start();
@ -383,7 +398,7 @@ public class IntegrationTest extends BaseTest {
//region startAutoFocus //region startAutoFocus
// TODO: won't test onStopAutoFocus because that is not guaranteed to be called // TODO: won't test onStopAutoFocus because that is not guaranteed to be called
@Test //-@Test
public void testStartAutoFocus() { public void testStartAutoFocus() {
camera.start(); camera.start();
CameraOptions o = waitForOpen(true); CameraOptions o = waitForOpen(true);
@ -399,13 +414,13 @@ public class IntegrationTest extends BaseTest {
//region capture //region capture
@Test //-@Test
public void testCapturePicture_beforeStarted() { public void testCapturePicture_beforeStarted() {
camera.capturePicture(); camera.capturePicture();
waitForPicture(false); waitForPicture(false);
} }
@Test //-@Test
public void testCapturePicture_concurrentCalls() throws Exception { public void testCapturePicture_concurrentCalls() throws Exception {
// Second take should fail. // Second take should fail.
camera.start(); camera.start();
@ -421,7 +436,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(latch.getCount(), 1); assertEquals(latch.getCount(), 1);
} }
@Test //-@Test
public void testCapturePicture_size() throws Exception { public void testCapturePicture_size() throws Exception {
camera.setCropOutput(false); camera.setCropOutput(false);
camera.start(); camera.start();
@ -437,13 +452,13 @@ public class IntegrationTest extends BaseTest {
assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth()); assertTrue(b.getHeight() == size.getHeight() || b.getHeight() == size.getWidth());
} }
@Test //-@Test
public void testCaptureSnapshot_beforeStarted() { public void testCaptureSnapshot_beforeStarted() {
camera.captureSnapshot(); camera.captureSnapshot();
waitForPicture(false); waitForPicture(false);
} }
@Test //-@Test
public void testCaptureSnapshot_concurrentCalls() throws Exception { public void testCaptureSnapshot_concurrentCalls() throws Exception {
// Second take should fail. // Second take should fail.
camera.start(); camera.start();
@ -459,7 +474,7 @@ public class IntegrationTest extends BaseTest {
assertEquals(latch.getCount(), 1); assertEquals(latch.getCount(), 1);
} }
@Test //-@Test
public void testCaptureSnapshot_size() throws Exception { public void testCaptureSnapshot_size() throws Exception {
camera.setCropOutput(false); camera.setCropOutput(false);
camera.start(); camera.start();

@ -16,11 +16,11 @@ class GridLinesLayout extends View {
private Grid gridMode; private Grid gridMode;
Drawable horiz; private Drawable horiz;
Drawable vert; private Drawable vert;
private final float width; private final float width;
Task<Void> drawTask = new Task<>(); Task<Integer> drawTask = new Task<>();
private final static float GOLDEN_RATIO_INV = 0.61803398874989f; private final static float GOLDEN_RATIO_INV = 0.61803398874989f;
@ -92,6 +92,6 @@ class GridLinesLayout extends View {
vert.draw(canvas); vert.draw(canvas);
canvas.translate(- pos * getWidth(), 0); canvas.translate(- pos * getWidth(), 0);
} }
drawTask.end(null); drawTask.end(count);
} }
} }

Loading…
Cancel
Save