Better tests

pull/170/head
Mattia Iavarone 8 years ago
parent 2d51c7f58f
commit eabf89e5c9
  1. 30
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java
  2. 10
      cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java
  3. 15
      cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java

@ -206,6 +206,7 @@ public class CameraViewTest extends BaseTest {
@Test @Test
public void testGestureAction_zoom() { public void testGestureAction_zoom() {
mockController.mockStarted(true); mockController.mockStarted(true);
mockController.mZoomChanged = false;
MotionEvent event = MotionEvent.obtain(0L, 0L, 0, 0f, 0f, 0); MotionEvent event = MotionEvent.obtain(0L, 0L, 0, 0f, 0f, 0);
ui(new Runnable() { ui(new Runnable() {
@Override @Override
@ -214,12 +215,18 @@ public class CameraViewTest extends BaseTest {
public boolean onTouchEvent(MotionEvent event) { return true; } public boolean onTouchEvent(MotionEvent event) { return true; }
}; };
cameraView.mPinchGestureLayout.setGestureType(Gesture.PINCH); cameraView.mPinchGestureLayout.setGestureType(Gesture.PINCH);
cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM);
} }
}); });
// Need to set a strange value first, because we don't dispatch if oldValue = newValue.
mockController.mZoomValue = -1; // If factor is 0, we return the same value. The controller should not be notified.
mockController.mZoomChanged = false; cameraView.mPinchGestureLayout.mFactor = 0f;
cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); cameraView.dispatchTouchEvent(event);
assertFalse(mockController.mZoomChanged);
// For larger factors, the value is scaled. The controller should be notified.
cameraView.mPinchGestureLayout.mFactor = 1f;
cameraView.dispatchTouchEvent(event); cameraView.dispatchTouchEvent(event);
assertTrue(mockController.mZoomChanged); assertTrue(mockController.mZoomChanged);
} }
@ -232,7 +239,7 @@ public class CameraViewTest extends BaseTest {
when(o.getExposureCorrectionMaxValue()).thenReturn(10f); when(o.getExposureCorrectionMaxValue()).thenReturn(10f);
mockController.setMockCameraOptions(o); mockController.setMockCameraOptions(o);
mockController.mockStarted(true); mockController.mockStarted(true);
mockController.mExposureCorrectionChanged = false;
MotionEvent event = MotionEvent.obtain(0L, 0L, 0, 0f, 0f, 0); MotionEvent event = MotionEvent.obtain(0L, 0L, 0, 0f, 0f, 0);
ui(new Runnable() { ui(new Runnable() {
@Override @Override
@ -241,12 +248,17 @@ public class CameraViewTest extends BaseTest {
public boolean onTouchEvent(MotionEvent event) { return true; } public boolean onTouchEvent(MotionEvent event) { return true; }
}; };
cameraView.mScrollGestureLayout.setGestureType(Gesture.SCROLL_HORIZONTAL); cameraView.mScrollGestureLayout.setGestureType(Gesture.SCROLL_HORIZONTAL);
cameraView.mapGesture(Gesture.SCROLL_HORIZONTAL, GestureAction.EXPOSURE_CORRECTION);
} }
}); });
// Need to set a strange value first, because we don't dispatch if oldValue = newValue.
mockController.mExposureCorrectionValue = -50f; // If factor is 0, we return the same value. The controller should not be notified.
mockController.mExposureCorrectionChanged = false; cameraView.mScrollGestureLayout.mFactor = 0f;
cameraView.mapGesture(Gesture.SCROLL_HORIZONTAL, GestureAction.EXPOSURE_CORRECTION); cameraView.dispatchTouchEvent(event);
assertFalse(mockController.mExposureCorrectionChanged);
// For larger factors, the value is scaled. The controller should be notified.
cameraView.mScrollGestureLayout.mFactor = 1f;
cameraView.dispatchTouchEvent(event); cameraView.dispatchTouchEvent(event);
assertTrue(mockController.mExposureCorrectionChanged); assertTrue(mockController.mExposureCorrectionChanged);
} }

@ -3,12 +3,8 @@ package com.otaliastudios.cameraview;
import android.content.Context; import android.content.Context;
import android.graphics.PointF; import android.graphics.PointF;
import android.os.Build; import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.ScaleGestureDetector; import android.view.ScaleGestureDetector;
import android.view.View;
class PinchGestureLayout extends GestureLayout { class PinchGestureLayout extends GestureLayout {
@ -16,7 +12,7 @@ class PinchGestureLayout extends GestureLayout {
ScaleGestureDetector mDetector; ScaleGestureDetector mDetector;
private boolean mNotify; private boolean mNotify;
private float mAdditionFactor = 0; /* tests */ float mFactor = 0;
public PinchGestureLayout(Context context) { public PinchGestureLayout(Context context) {
@ -32,7 +28,7 @@ class PinchGestureLayout extends GestureLayout {
@Override @Override
public boolean onScale(ScaleGestureDetector detector) { public boolean onScale(ScaleGestureDetector detector) {
mNotify = true; mNotify = true;
mAdditionFactor = ((detector.getScaleFactor() - 1) * ADD_SENSITIVITY); mFactor = ((detector.getScaleFactor() - 1) * ADD_SENSITIVITY);
return true; return true;
} }
}); });
@ -75,7 +71,7 @@ class PinchGestureLayout extends GestureLayout {
@Override @Override
public float scaleValue(float currValue, float minValue, float maxValue) { public float scaleValue(float currValue, float minValue, float maxValue) {
float add = mAdditionFactor; float add = mFactor;
// ^ This works well if minValue = 0, maxValue = 1. // ^ This works well if minValue = 0, maxValue = 1.
// Account for the different range: // Account for the different range:
add *= (maxValue - minValue); add *= (maxValue - minValue);

@ -1,16 +1,9 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context; import android.content.Context;
import android.graphics.PointF; import android.graphics.PointF;
import android.util.Log;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
class ScrollGestureLayout extends GestureLayout { class ScrollGestureLayout extends GestureLayout {
@ -19,7 +12,7 @@ class ScrollGestureLayout extends GestureLayout {
private GestureDetector mDetector; private GestureDetector mDetector;
private boolean mNotify; private boolean mNotify;
private float mDistance; /* tests */ float mFactor;
public ScrollGestureLayout(Context context) { public ScrollGestureLayout(Context context) {
@ -47,8 +40,8 @@ class ScrollGestureLayout extends GestureLayout {
horizontal = mType == Gesture.SCROLL_HORIZONTAL; horizontal = mType == Gesture.SCROLL_HORIZONTAL;
} }
mPoints[1].set(e2.getX(), e2.getY()); mPoints[1].set(e2.getX(), e2.getY());
mDistance = horizontal ? (distanceX / getWidth()) : (distanceY / getHeight()); mFactor = horizontal ? (distanceX / getWidth()) : (distanceY / getHeight());
mDistance = horizontal ? -mDistance : mDistance; // When vertical, up = positive mFactor = horizontal ? -mFactor : mFactor; // When vertical, up = positive
mNotify = true; mNotify = true;
return true; return true;
} }
@ -80,7 +73,7 @@ class ScrollGestureLayout extends GestureLayout {
@Override @Override
public float scaleValue(float currValue, float minValue, float maxValue) { public float scaleValue(float currValue, float minValue, float maxValue) {
float delta = mDistance; // -1 ... 1 float delta = mFactor; // -1 ... 1
// ^ This works well if minValue = 0, maxValue = 1. // ^ This works well if minValue = 0, maxValue = 1.
// Account for the different range: // Account for the different range:

Loading…
Cancel
Save