MarkerLayout tests

pull/493/head
Mattia Iavarone 5 years ago
parent ecd2cdba13
commit e7727fb65c
  1. 15
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewCallbacksTest.java
  2. 42
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java
  3. 80
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/markers/MarkerLayoutTest.java
  4. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  5. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/markers/MarkerLayout.java

@ -17,6 +17,9 @@ import com.otaliastudios.cameraview.gesture.Gesture;
import com.otaliastudios.cameraview.gesture.GestureAction;
import com.otaliastudios.cameraview.internal.utils.Task;
import com.otaliastudios.cameraview.engine.MockCameraEngine;
import com.otaliastudios.cameraview.markers.AutoFocusMarker;
import com.otaliastudios.cameraview.markers.AutoFocusTrigger;
import com.otaliastudios.cameraview.markers.MarkerLayout;
import com.otaliastudios.cameraview.preview.MockCameraPreview;
import com.otaliastudios.cameraview.preview.CameraPreview;
@ -34,6 +37,7 @@ import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyFloat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.floatThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@ -190,6 +194,10 @@ public class CameraViewCallbacksTest extends BaseTest {
// Enable tap gesture.
// Can't mock package protected. camera.mTapGestureLayout = mock(TapGestureLayout.class);
camera.mapGesture(Gesture.TAP, GestureAction.AUTO_FOCUS);
AutoFocusMarker marker = mock(AutoFocusMarker.class);
MarkerLayout markerLayout = mock(MarkerLayout.class);
camera.setAutoFocusMarker(marker);
camera.mMarkerLayout = markerLayout;
PointF point = new PointF();
completeTask().when(listener).onAutoFocusStart(point);
@ -197,7 +205,8 @@ public class CameraViewCallbacksTest extends BaseTest {
assertNotNull(task.await(200));
verify(listener, times(1)).onAutoFocusStart(point);
// Can't mock package protected. verify(camera.mTapGestureLayout, times(1)).onAutoFocusStart(point);
verify(marker, times(1)).onAutoFocusStart(AutoFocusTrigger.GESTURE, point);
verify(markerLayout, times(1)).onEvent(MarkerLayout.TYPE_AUTOFOCUS, any(PointF[].class));
}
@Test
@ -205,6 +214,8 @@ public class CameraViewCallbacksTest extends BaseTest {
// Enable tap gesture.
// Can't mock package protected. camera.mTapGestureLayout = mock(TapGestureLayout.class);
camera.mapGesture(Gesture.TAP, GestureAction.AUTO_FOCUS);
AutoFocusMarker marker = mock(AutoFocusMarker.class);
camera.setAutoFocusMarker(marker);
PointF point = new PointF();
boolean success = true;
@ -213,6 +224,8 @@ public class CameraViewCallbacksTest extends BaseTest {
assertNotNull(task.await(200));
verify(listener, times(1)).onAutoFocusEnd(success, point);
verify(marker, times(1)).onAutoFocusEnd(AutoFocusTrigger.GESTURE, success, point);
// Can't mock package protected. verify(camera.mTapGestureLayout, times(1)).onAutoFocusEnd(success);
}

@ -3,6 +3,7 @@ package com.otaliastudios.cameraview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.PointF;
import android.location.Location;
import androidx.annotation.NonNull;
import androidx.test.ext.junit.runners.AndroidJUnit4;
@ -29,16 +30,26 @@ import com.otaliastudios.cameraview.gesture.PinchGestureLayout;
import com.otaliastudios.cameraview.gesture.ScrollGestureLayout;
import com.otaliastudios.cameraview.gesture.TapGestureLayout;
import com.otaliastudios.cameraview.engine.MockCameraEngine;
import com.otaliastudios.cameraview.internal.utils.Task;
import com.otaliastudios.cameraview.markers.AutoFocusMarker;
import com.otaliastudios.cameraview.markers.DefaultAutoFocusMarker;
import com.otaliastudios.cameraview.markers.MarkerLayout;
import com.otaliastudios.cameraview.preview.MockCameraPreview;
import com.otaliastudios.cameraview.preview.CameraPreview;
import com.otaliastudios.cameraview.size.Size;
import com.otaliastudios.cameraview.size.SizeSelector;
import com.otaliastudios.cameraview.size.SizeSelectors;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.junit.Assert.*;
@ -728,5 +739,36 @@ public class CameraViewTest extends BaseTest {
//endregion
//region MarkerLayout
@Test
public void testMarkerLayout_forAutoFocus_onMarker() {
MarkerLayout markerLayout = mock(MarkerLayout.class);
AutoFocusMarker marker = new DefaultAutoFocusMarker();
cameraView.mMarkerLayout = markerLayout;
cameraView.setAutoFocusMarker(marker);
verify(markerLayout, times(1)).onMarker(MarkerLayout.TYPE_AUTOFOCUS, marker);
}
@Test
public void testMarkerLayout_forAutoFocus_onEvent() {
MarkerLayout markerLayout = spy(cameraView.mMarkerLayout);
cameraView.mMarkerLayout = markerLayout;
final PointF point = new PointF(0, 0);
final PointF[] points = new PointF[]{ point };
final Task<Boolean> task = new Task<>(true);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
task.end(true);
return null;
}
}).when(markerLayout).onEvent(MarkerLayout.TYPE_AUTOFOCUS, points);
cameraView.mCameraCallbacks.dispatchOnFocusStart(Gesture.TAP, point);
assertNotNull(task.await(100));
}
//endregion
// TODO: test permissions
}

@ -0,0 +1,80 @@
package com.otaliastudios.cameraview.markers;
import android.annotation.TargetApi;
import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import com.otaliastudios.cameraview.BaseTest;
import com.otaliastudios.cameraview.TestActivity;
import com.otaliastudios.cameraview.gesture.Gesture;
import com.otaliastudios.cameraview.gesture.GestureLayout;
import com.otaliastudios.cameraview.internal.utils.Task;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.matcher.RootMatchers;
import androidx.test.rule.ActivityTestRule;
import static androidx.test.espresso.Espresso.onView;
@TargetApi(17)
public class MarkerLayoutTest extends BaseTest {
@Rule
public ActivityTestRule<TestActivity> rule = new ActivityTestRule<>(TestActivity.class);
private MarkerLayout markerLayout;
private AutoFocusMarker autoFocusMarker;
@Before
public void setUp() {
ui(new Runnable() {
@Override
public void run() {
TestActivity a = rule.getActivity();
markerLayout = Mockito.spy(new MarkerLayout(a));
a.inflate(markerLayout);
autoFocusMarker = Mockito.spy(new DefaultAutoFocusMarker());
}
});
}
@Test
public void testOnMarker_callsOnAttach() {
markerLayout.onMarker(MarkerLayout.TYPE_AUTOFOCUS, autoFocusMarker);
Mockito.verify(autoFocusMarker, Mockito.times(1)).onAttach(
Mockito.any(Context.class),
Mockito.eq(markerLayout));
}
@Test
public void testOnMarker_addsView() {
Assert.assertEquals(markerLayout.getChildCount(), 0);
markerLayout.onMarker(MarkerLayout.TYPE_AUTOFOCUS, autoFocusMarker);
Assert.assertEquals(markerLayout.getChildCount(), 1);
}
@Test
public void testOnMarker_removesView() {
markerLayout.onMarker(MarkerLayout.TYPE_AUTOFOCUS, autoFocusMarker);
Assert.assertEquals(markerLayout.getChildCount(), 1);
markerLayout.onMarker(MarkerLayout.TYPE_AUTOFOCUS, autoFocusMarker);
Assert.assertEquals(markerLayout.getChildCount(), 1);
markerLayout.onMarker(MarkerLayout.TYPE_AUTOFOCUS, null);
Assert.assertEquals(markerLayout.getChildCount(), 0);
Mockito.verify(autoFocusMarker, Mockito.times(2)).onAttach(
Mockito.any(Context.class),
Mockito.eq(markerLayout));
}
}

@ -117,7 +117,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver {
PinchGestureLayout mPinchGestureLayout;
TapGestureLayout mTapGestureLayout;
ScrollGestureLayout mScrollGestureLayout;
private MarkerLayout mMarkerLayout;
MarkerLayout mMarkerLayout;
private boolean mKeepScreenOn;
@SuppressWarnings({"FieldCanBeLocal", "unused"})
private boolean mExperimental;

@ -16,7 +16,7 @@ import androidx.annotation.Nullable;
* Manages markers and provides an hierarchy / Canvas for them.
* It is responsible for calling {@link Marker#onAttach(Context, ViewGroup)}.
*/
public final class MarkerLayout extends FrameLayout {
public class MarkerLayout extends FrameLayout {
public final static int TYPE_AUTOFOCUS = 1;

Loading…
Cancel
Save