From eabf89e5c934fc2d5e106f7b0b41440d162e6b32 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Thu, 1 Mar 2018 00:21:12 +0100 Subject: [PATCH] Better tests --- .../cameraview/CameraViewTest.java | 30 +++++++++++++------ .../cameraview/PinchGestureLayout.java | 10 ++----- .../cameraview/ScrollGestureLayout.java | 15 +++------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index f5e99fd9..b3f14516 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -206,6 +206,7 @@ public class CameraViewTest extends BaseTest { @Test public void testGestureAction_zoom() { mockController.mockStarted(true); + mockController.mZoomChanged = false; MotionEvent event = MotionEvent.obtain(0L, 0L, 0, 0f, 0f, 0); ui(new Runnable() { @Override @@ -214,12 +215,18 @@ public class CameraViewTest extends BaseTest { public boolean onTouchEvent(MotionEvent event) { return true; } }; 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; - mockController.mZoomChanged = false; - cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); + + // If factor is 0, we return the same value. The controller should not be notified. + cameraView.mPinchGestureLayout.mFactor = 0f; + 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); assertTrue(mockController.mZoomChanged); } @@ -232,7 +239,7 @@ public class CameraViewTest extends BaseTest { when(o.getExposureCorrectionMaxValue()).thenReturn(10f); mockController.setMockCameraOptions(o); mockController.mockStarted(true); - + mockController.mExposureCorrectionChanged = false; MotionEvent event = MotionEvent.obtain(0L, 0L, 0, 0f, 0f, 0); ui(new Runnable() { @Override @@ -241,12 +248,17 @@ public class CameraViewTest extends BaseTest { public boolean onTouchEvent(MotionEvent event) { return true; } }; 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; - mockController.mExposureCorrectionChanged = false; - cameraView.mapGesture(Gesture.SCROLL_HORIZONTAL, GestureAction.EXPOSURE_CORRECTION); + + // If factor is 0, we return the same value. The controller should not be notified. + cameraView.mScrollGestureLayout.mFactor = 0f; + 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); assertTrue(mockController.mExposureCorrectionChanged); } diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java index 8ba8ddec..1cb8acdb 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/PinchGestureLayout.java @@ -3,12 +3,8 @@ package com.otaliastudios.cameraview; import android.content.Context; import android.graphics.PointF; 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.ScaleGestureDetector; -import android.view.View; class PinchGestureLayout extends GestureLayout { @@ -16,7 +12,7 @@ class PinchGestureLayout extends GestureLayout { ScaleGestureDetector mDetector; private boolean mNotify; - private float mAdditionFactor = 0; + /* tests */ float mFactor = 0; public PinchGestureLayout(Context context) { @@ -32,7 +28,7 @@ class PinchGestureLayout extends GestureLayout { @Override public boolean onScale(ScaleGestureDetector detector) { mNotify = true; - mAdditionFactor = ((detector.getScaleFactor() - 1) * ADD_SENSITIVITY); + mFactor = ((detector.getScaleFactor() - 1) * ADD_SENSITIVITY); return true; } }); @@ -75,7 +71,7 @@ class PinchGestureLayout extends GestureLayout { @Override public float scaleValue(float currValue, float minValue, float maxValue) { - float add = mAdditionFactor; + float add = mFactor; // ^ This works well if minValue = 0, maxValue = 1. // Account for the different range: add *= (maxValue - minValue); diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java b/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java index d0c8a25e..f8e3b17a 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/ScrollGestureLayout.java @@ -1,16 +1,9 @@ package com.otaliastudios.cameraview; -import android.animation.Animator; -import android.animation.AnimatorListenerAdapter; import android.content.Context; import android.graphics.PointF; -import android.util.Log; import android.view.GestureDetector; -import android.view.LayoutInflater; import android.view.MotionEvent; -import android.view.View; -import android.widget.FrameLayout; -import android.widget.ImageView; class ScrollGestureLayout extends GestureLayout { @@ -19,7 +12,7 @@ class ScrollGestureLayout extends GestureLayout { private GestureDetector mDetector; private boolean mNotify; - private float mDistance; + /* tests */ float mFactor; public ScrollGestureLayout(Context context) { @@ -47,8 +40,8 @@ class ScrollGestureLayout extends GestureLayout { horizontal = mType == Gesture.SCROLL_HORIZONTAL; } mPoints[1].set(e2.getX(), e2.getY()); - mDistance = horizontal ? (distanceX / getWidth()) : (distanceY / getHeight()); - mDistance = horizontal ? -mDistance : mDistance; // When vertical, up = positive + mFactor = horizontal ? (distanceX / getWidth()) : (distanceY / getHeight()); + mFactor = horizontal ? -mFactor : mFactor; // When vertical, up = positive mNotify = true; return true; } @@ -80,7 +73,7 @@ class ScrollGestureLayout extends GestureLayout { @Override 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. // Account for the different range: