diff options
author | paczos <lukasz.paczos@mapbox.com> | 2017-12-14 11:36:11 +0100 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2017-12-14 16:23:33 +0100 |
commit | 2afa8a172505674956e07655421a55e7aa9f41b2 (patch) | |
tree | 4295a4feb21052f12faedc8ef2e44575a2126361 /platform/android | |
parent | 44363d4826638b1c3b5c6ce9e1eda178cf71f32b (diff) | |
download | qtlocation-mapboxgl-2afa8a172505674956e07655421a55e7aa9f41b2.tar.gz |
[android] added map touch listeners test
Diffstat (limited to 'platform/android')
2 files changed, 153 insertions, 42 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java index 2c95c09485..7885defc93 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapGestureDetector.java @@ -45,10 +45,10 @@ final class MapGestureDetector { private final AnnotationManager annotationManager; private final CameraChangeDispatcher cameraChangeDispatcher; - private final GestureDetectorCompat gestureDetector; - private final ScaleGestureDetector scaleGestureDetector; - private final RotateGestureDetector rotateGestureDetector; - private final ShoveGestureDetector shoveGestureDetector; + private GestureDetectorCompat gestureDetector; + private ScaleGestureDetector scaleGestureDetector; + private RotateGestureDetector rotateGestureDetector; + private ShoveGestureDetector shoveGestureDetector; private MapboxMap.OnMapClickListener onMapClickListener; private MapboxMap.OnMapLongClickListener onMapLongClickListener; @@ -88,12 +88,14 @@ final class MapGestureDetector { this.cameraChangeDispatcher = cameraChangeDispatcher; // Touch gesture detectors - gestureDetector = new GestureDetectorCompat(context, new GestureListener()); - gestureDetector.setIsLongpressEnabled(true); - scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener()); - ScaleGestureDetectorCompat.setQuickScaleEnabled(scaleGestureDetector, true); - rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureListener()); - shoveGestureDetector = new ShoveGestureDetector(context, new ShoveGestureListener()); + if (context != null) { + gestureDetector = new GestureDetectorCompat(context, new GestureListener()); + gestureDetector.setIsLongpressEnabled(true); + scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener()); + ScaleGestureDetectorCompat.setQuickScaleEnabled(scaleGestureDetector, true); + rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureListener()); + shoveGestureDetector = new ShoveGestureDetector(context, new ShoveGestureListener()); + } } /** @@ -294,7 +296,6 @@ final class MapGestureDetector { return false; } - /** * Responsible for handling one finger gestures. */ @@ -362,14 +363,7 @@ final class MapGestureDetector { annotationManager.deselectMarkers(); } - // notify app of map click - if (onMapClickListener != null) { - onMapClickListener.onMapClick(projection.fromScreenLocation(tapPoint)); - } - - for (MapboxMap.OnMapClickListener listener : onMapClickListenerList) { - listener.onMapClick(projection.fromScreenLocation(tapPoint)); - } + notifyOnMapClickListeners(tapPoint); } MapboxTelemetry.getInstance().pushEvent(MapboxEventWrapper.buildMapClickEvent( @@ -381,17 +375,8 @@ final class MapGestureDetector { @Override public void onLongPress(MotionEvent motionEvent) { - if (onMapLongClickListener != null && !quickZoom) { - onMapLongClickListener.onMapLongClick( - projection.fromScreenLocation(new PointF(motionEvent.getX(), motionEvent.getY()))); - } - - if (!quickZoom) { - for (MapboxMap.OnMapLongClickListener listener : onMapLongClickListenerList) { - listener.onMapLongClick( - projection.fromScreenLocation(new PointF(motionEvent.getX(), motionEvent.getY()))); - } - } + PointF longClickPoint = new PointF(motionEvent.getX(), motionEvent.getY()); + notifyOnMapLongClickListeners(longClickPoint); } @Override @@ -430,13 +415,7 @@ final class MapGestureDetector { // update transformation transform.moveBy(offsetX, offsetY, animationTime); - if (onFlingListener != null) { - onFlingListener.onFling(); - } - - for (MapboxMap.OnFlingListener listener : onFlingListenerList) { - listener.onFling(); - } + notifyOnFlingListeners(); return true; } @@ -471,14 +450,51 @@ final class MapGestureDetector { // Scroll the map transform.moveBy(-distanceX, -distanceY, 0 /*no duration*/); - if (onScrollListener != null) { - onScrollListener.onScroll(); + notifyOnScrollListeners(); + return true; + } + } + + void notifyOnMapClickListeners(PointF tapPoint) { + // notify app of map click + if (onMapClickListener != null) { + onMapClickListener.onMapClick(projection.fromScreenLocation(tapPoint)); + } + + for (MapboxMap.OnMapClickListener listener : onMapClickListenerList) { + listener.onMapClick(projection.fromScreenLocation(tapPoint)); + } + } + + void notifyOnMapLongClickListeners(PointF longClickPoint) { + if (!quickZoom) { + if (onMapLongClickListener != null) { + onMapLongClickListener.onMapLongClick(projection.fromScreenLocation(longClickPoint)); } - for (MapboxMap.OnScrollListener listener : onScrollListenerList) { - listener.onScroll(); + for (MapboxMap.OnMapLongClickListener listener : onMapLongClickListenerList) { + listener.onMapLongClick(projection.fromScreenLocation(longClickPoint)); } - return true; + } + } + + void notifyOnFlingListeners() { + if (onFlingListener != null) { + onFlingListener.onFling(); + } + + for (MapboxMap.OnFlingListener listener : onFlingListenerList) { + listener.onFling(); + } + } + + void notifyOnScrollListeners() { + if (onScrollListener != null) { + onScrollListener.onScroll(); + } + + for (MapboxMap.OnScrollListener listener : onScrollListenerList) { + listener.onScroll(); } } diff --git a/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapTouchListenersTest.java b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapTouchListenersTest.java new file mode 100644 index 0000000000..1e27e2ea4d --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapTouchListenersTest.java @@ -0,0 +1,95 @@ +package com.mapbox.mapboxsdk.maps; + +import android.graphics.PointF; + +import com.mapbox.mapboxsdk.geometry.LatLng; + +import org.junit.Test; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class MapTouchListenersTest { + + @Test + public void OnMapClickListenerTest() throws Exception { + LatLng latLng = new LatLng(); + PointF pointF = new PointF(); + + Projection projection = mock(Projection.class); + when(projection.fromScreenLocation(pointF)).thenReturn(latLng); + MapGestureDetector mapGestureDetector = new MapGestureDetector(null, + null, projection, null, null, null, null); + + MapboxMap.OnMapClickListener listener = mock(MapboxMap.OnMapClickListener.class); + mapGestureDetector.addOnMapClickListener(listener); + mapGestureDetector.notifyOnMapClickListeners(pointF); + verify(listener, times(1)).onMapClick(latLng); + + mapGestureDetector.removeOnMapClickListener(listener); + mapGestureDetector.notifyOnMapClickListeners(pointF); + verify(listener, times(1)).onMapClick(latLng); + } + + @Test + public void OnMapLongClickListenerTest() throws Exception { + LatLng latLng = new LatLng(); + PointF pointF = new PointF(); + + Projection projection = mock(Projection.class); + when(projection.fromScreenLocation(pointF)).thenReturn(latLng); + MapGestureDetector mapGestureDetector = new MapGestureDetector(null, + null, projection, null, null, null, null); + + MapboxMap.OnMapLongClickListener listener = mock(MapboxMap.OnMapLongClickListener.class); + mapGestureDetector.addOnMapLongClickListener(listener); + mapGestureDetector.notifyOnMapLongClickListeners(pointF); + verify(listener, times(1)).onMapLongClick(latLng); + + mapGestureDetector.removeOnMapLongClickListener(listener); + mapGestureDetector.notifyOnMapLongClickListeners(pointF); + verify(listener, times(1)).onMapLongClick(latLng); + } + + @Test + public void OnFlingListenerTest() throws Exception { + LatLng latLng = new LatLng(); + PointF pointF = new PointF(); + + Projection projection = mock(Projection.class); + when(projection.fromScreenLocation(pointF)).thenReturn(latLng); + MapGestureDetector mapGestureDetector = new MapGestureDetector(null, + null, projection, null, null, null, null); + + MapboxMap.OnFlingListener listener = mock(MapboxMap.OnFlingListener.class); + mapGestureDetector.addOnFlingListener(listener); + mapGestureDetector.notifyOnFlingListeners(); + verify(listener, times(1)).onFling(); + + mapGestureDetector.removeOnFlingListener(listener); + mapGestureDetector.notifyOnFlingListeners(); + verify(listener, times(1)).onFling(); + } + + @Test + public void OnScrollListenerTest() throws Exception { + LatLng latLng = new LatLng(); + PointF pointF = new PointF(); + + Projection projection = mock(Projection.class); + when(projection.fromScreenLocation(pointF)).thenReturn(latLng); + MapGestureDetector mapGestureDetector = new MapGestureDetector(null, + null, projection, null, null, null, null); + + MapboxMap.OnScrollListener listener = mock(MapboxMap.OnScrollListener.class); + mapGestureDetector.addOnScrollListener(listener); + mapGestureDetector.notifyOnScrollListeners(); + verify(listener, times(1)).onScroll(); + + mapGestureDetector.removeOnScrollListener(listener); + mapGestureDetector.notifyOnScrollListeners(); + verify(listener, times(1)).onScroll(); + } +} |