Convert java to kotlin (androidTest). (#1952)

Co-authored-by: abebeos <129396476+abebeos@users.noreply.github.com>
This commit is contained in:
abebeos
2023-12-12 19:48:08 +02:00
committed by GitHub
parent 8b2c748341
commit a29f18f32a
6 changed files with 551 additions and 590 deletions

View File

@@ -1,42 +0,0 @@
package org.maplibre.android.maps;
import org.maplibre.android.annotations.Icon;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import timber.log.Timber;
public class IconManagerResolver {
private IconManager iconManager;
public IconManagerResolver(MapLibreMap maplibreMap) {
try {
Field annotationManagerField = MapLibreMap.class.getDeclaredField("annotationManager");
annotationManagerField.setAccessible(true);
AnnotationManager annotationManager = (AnnotationManager) annotationManagerField.get(maplibreMap);
Field iconManagerField = AnnotationManager.class.getDeclaredField("iconManager");
iconManagerField.setAccessible(true);
iconManager = (IconManager) iconManagerField.get(annotationManager);
} catch (Exception exception) {
Timber.e(exception, "Could not create IconManagerResolver, unable to reflect.");
}
}
@SuppressWarnings("unchecked")
public Map<Icon, Integer> getIconMap() {
try {
Field field = IconManager.class.getDeclaredField("iconMap");
field.setAccessible(true);
return (Map<Icon, Integer>) field.get(iconManager);
} catch (NoSuchFieldException exception) {
Timber.e(exception, "Could not getIconMap, unable to reflect.");
} catch (IllegalAccessException exception) {
Timber.e(exception, "Could not getIconMap, unable to reflect.");
}
return new HashMap<>();
}
}

View File

@@ -0,0 +1,35 @@
package org.maplibre.android.maps
import org.maplibre.android.annotations.Icon
import timber.log.Timber
class IconManagerResolver(maplibreMap: MapLibreMap?) {
private var iconManager: IconManager? = null
init {
try {
val annotationManagerField = MapLibreMap::class.java.getDeclaredField("annotationManager")
annotationManagerField.isAccessible = true
val annotationManager = annotationManagerField[maplibreMap] as AnnotationManager
val iconManagerField = AnnotationManager::class.java.getDeclaredField("iconManager")
iconManagerField.isAccessible = true
iconManager = iconManagerField[annotationManager] as IconManager
} catch (exception: Exception) {
Timber.e(exception, "Could not create IconManagerResolver, unable to reflect.")
}
}
val iconMap: Map<Icon, Int>
get() {
try {
val field = IconManager::class.java.getDeclaredField("iconMap")
field.isAccessible = true
return field[iconManager] as Map<Icon, Int>
} catch (exception: NoSuchFieldException) {
Timber.e(exception, "Could not getIconMap, unable to reflect.")
} catch (exception: IllegalAccessException) {
Timber.e(exception, "Could not getIconMap, unable to reflect.")
}
return HashMap()
}
}

View File

@@ -1,480 +0,0 @@
package org.maplibre.android.maps;
import android.graphics.Color;
import android.view.View;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import org.maplibre.android.annotations.BaseMarkerOptions;
import org.maplibre.android.annotations.Marker;
import org.maplibre.android.annotations.MarkerOptions;
import org.maplibre.android.annotations.Polygon;
import org.maplibre.android.annotations.PolygonOptions;
import org.maplibre.android.annotations.Polyline;
import org.maplibre.android.annotations.PolylineOptions;
import org.maplibre.android.exceptions.InvalidMarkerPositionException;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.android.testapp.R;
import org.maplibre.android.testapp.activity.EspressoTest;
import org.hamcrest.Matcher;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* This test is responsible for testing the public API.
* <p>
* Methods executed on MapboxMap are called from a ViewAction to ensure correct synchronisation
* with the application UI-thread.
* </p>
* @deprecated remove this file when removing deprecated annotations
*/
@Deprecated
public class MapLibreMapTest extends EspressoTest {
//
// InfoWindow
//
@Test
public void testConcurrentInfoWindowEnabled() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
maplibreMap.setAllowConcurrentMultipleOpenInfoWindows(true);
assertTrue("ConcurrentWindows should be true", maplibreMap.isAllowConcurrentMultipleOpenInfoWindows());
}));
}
@Test
public void testConcurrentInfoWindowDisabled() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
maplibreMap.setAllowConcurrentMultipleOpenInfoWindows(false);
assertFalse("ConcurrentWindows should be false", maplibreMap.isAllowConcurrentMultipleOpenInfoWindows());
}));
}
@Test
public void testInfoWindowAdapter() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MapLibreMap.InfoWindowAdapter infoWindowAdapter = marker -> null;
maplibreMap.setInfoWindowAdapter(infoWindowAdapter);
assertEquals("InfoWindowAdpter should be the same", infoWindowAdapter, maplibreMap.getInfoWindowAdapter());
}));
}
//
// Annotations
//
@Test
public void testAddMarker() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker marker = maplibreMap.addMarker(markerOptions);
assertTrue("Marker should be contained", maplibreMap.getMarkers().contains(marker));
}));
}
@Test(expected = InvalidMarkerPositionException.class)
public void testAddMarkerInvalidPosition() {
new MarkerOptions().getMarker();
}
@Test
public void testAddMarkers() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<BaseMarkerOptions> markerList = new ArrayList<>();
MarkerOptions markerOptions1 = new MarkerOptions().position(new LatLng()).title("a");
MarkerOptions markerOptions2 = new MarkerOptions().position(new LatLng()).title("b");
markerList.add(markerOptions1);
markerList.add(markerOptions2);
List<Marker> markers = maplibreMap.addMarkers(markerList);
assertEquals("Markers size should be 2", 2, maplibreMap.getMarkers().size());
assertTrue(maplibreMap.getMarkers().contains(markers.get(0)));
assertTrue(maplibreMap.getMarkers().contains(markers.get(1)));
}));
}
@Test
public void testAddMarkersEmpty() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<BaseMarkerOptions> markerList = new ArrayList<>();
maplibreMap.addMarkers(markerList);
assertEquals("Markers size should be 0", 0, maplibreMap.getMarkers().size());
}));
}
@Test
public void testAddMarkersSingleMarker() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<BaseMarkerOptions> markerList = new ArrayList<>();
MarkerOptions markerOptions = new MarkerOptions().title("a").position(new LatLng());
markerList.add(markerOptions);
List<Marker> markers = maplibreMap.addMarkers(markerList);
assertEquals("Markers size should be 1", 1, maplibreMap.getMarkers().size());
assertTrue(maplibreMap.getMarkers().contains(markers.get(0)));
}));
}
@Test
public void testAddPolygon() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
PolygonOptions polygonOptions = new PolygonOptions().add(new LatLng());
Polygon polygon = maplibreMap.addPolygon(polygonOptions);
assertTrue("Polygon should be contained", maplibreMap.getPolygons().contains(polygon));
}));
}
@Test
public void testAddEmptyPolygon() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
PolygonOptions polygonOptions = new PolygonOptions();
Polygon polygon = maplibreMap.addPolygon(polygonOptions);
assertTrue("Polygon should be ignored", !maplibreMap.getPolygons().contains(polygon));
}));
}
@Test
public void testAddPolygons() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<PolygonOptions> polygonList = new ArrayList<>();
PolygonOptions polygonOptions1 = new PolygonOptions().fillColor(Color.BLACK).add(new LatLng());
PolygonOptions polygonOptions2 = new PolygonOptions().fillColor(Color.WHITE).add(new LatLng());
PolygonOptions polygonOptions3 = new PolygonOptions();
polygonList.add(polygonOptions1);
polygonList.add(polygonOptions2);
polygonList.add(polygonOptions3);
maplibreMap.addPolygons(polygonList);
assertEquals("Polygons size should be 2", 2, maplibreMap.getPolygons().size());
assertTrue(maplibreMap.getPolygons().contains(polygonOptions1.getPolygon()));
assertTrue(maplibreMap.getPolygons().contains(polygonOptions2.getPolygon()));
assertTrue("Polygon should be ignored", !maplibreMap.getPolygons().contains(polygonOptions3.getPolygon()));
}));
}
@Test
public void addPolygonsEmpty() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
maplibreMap.addPolygons(new ArrayList<PolygonOptions>());
assertEquals("Polygons size should be 0", 0, maplibreMap.getPolygons().size());
}));
}
@Test
public void addPolygonsSingle() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<PolygonOptions> polygonList = new ArrayList<>();
PolygonOptions polygonOptions = new PolygonOptions().fillColor(Color.BLACK).add(new LatLng());
polygonList.add(polygonOptions);
maplibreMap.addPolygons(polygonList);
assertEquals("Polygons size should be 1", 1, maplibreMap.getPolygons().size());
assertTrue(maplibreMap.getPolygons().contains(polygonOptions.getPolygon()));
}));
}
@Test
public void testAddPolyline() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
PolylineOptions polylineOptions = new PolylineOptions().add(new LatLng());
Polyline polyline = maplibreMap.addPolyline(polylineOptions);
assertTrue("Polyline should be contained", maplibreMap.getPolylines().contains(polyline));
}));
}
@Test
public void testAddEmptyPolyline() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
PolylineOptions polylineOptions = new PolylineOptions();
Polyline polyline = maplibreMap.addPolyline(polylineOptions);
assertTrue("Polyline should be ignored", !maplibreMap.getPolylines().contains(polyline));
}));
}
@Test
public void testAddPolylines() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<PolylineOptions> polylineList = new ArrayList<>();
PolylineOptions polygonOptions1 = new PolylineOptions().color(Color.BLACK).add(new LatLng());
PolylineOptions polygonOptions2 = new PolylineOptions().color(Color.WHITE).add(new LatLng());
PolylineOptions polygonOptions3 = new PolylineOptions();
polylineList.add(polygonOptions1);
polylineList.add(polygonOptions2);
polylineList.add(polygonOptions3);
maplibreMap.addPolylines(polylineList);
assertEquals("Polygons size should be 2", 2, maplibreMap.getPolylines().size());
assertTrue(maplibreMap.getPolylines().contains(polygonOptions1.getPolyline()));
assertTrue(maplibreMap.getPolylines().contains(polygonOptions2.getPolyline()));
assertTrue(
"Polyline should be ignored", !maplibreMap.getPolylines().contains(polygonOptions3.getPolyline())
);
}));
}
@Test
public void testAddPolylinesEmpty() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
maplibreMap.addPolylines(new ArrayList<PolylineOptions>());
assertEquals("Polygons size should be 0", 0, maplibreMap.getPolylines().size());
}));
}
@Test
public void testAddPolylinesSingle() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<PolylineOptions> polylineList = new ArrayList<>();
PolylineOptions polygonOptions = new PolylineOptions().color(Color.BLACK).add(new LatLng());
polylineList.add(polygonOptions);
maplibreMap.addPolylines(polylineList);
assertEquals("Polygons size should be 1", 1, maplibreMap.getPolylines().size());
assertTrue(maplibreMap.getPolylines().contains(polygonOptions.getPolyline()));
}));
}
@Test
public void testRemoveMarker() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker marker = maplibreMap.addMarker(markerOptions);
maplibreMap.removeMarker(marker);
assertTrue("Markers should be empty", maplibreMap.getMarkers().isEmpty());
}));
}
@Test
public void testRemovePolygon() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
PolygonOptions polygonOptions = new PolygonOptions();
Polygon polygon = maplibreMap.addPolygon(polygonOptions);
maplibreMap.removePolygon(polygon);
assertTrue("Polygons should be empty", maplibreMap.getPolylines().isEmpty());
}));
}
@Test
public void testRemovePolyline() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
PolylineOptions polylineOptions = new PolylineOptions();
Polyline polyline = maplibreMap.addPolyline(polylineOptions);
maplibreMap.removePolyline(polyline);
assertTrue("Polylines should be empty", maplibreMap.getPolylines().isEmpty());
}));
}
@Test
public void testRemoveAnnotation() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker marker = maplibreMap.addMarker(markerOptions);
maplibreMap.removeAnnotation(marker);
assertTrue("Annotations should be empty", maplibreMap.getAnnotations().isEmpty());
}));
}
@Test
public void testRemoveAnnotationById() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
maplibreMap.addMarker(markerOptions);
// id will always be 0 in unit tests
maplibreMap.removeAnnotation(0);
assertTrue("Annotations should be empty", maplibreMap.getAnnotations().isEmpty());
}));
}
@Test
public void testRemoveAnnotations() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<BaseMarkerOptions> markerList = new ArrayList<>();
MarkerOptions markerOptions1 = new MarkerOptions().title("a").position(new LatLng());
MarkerOptions markerOptions2 = new MarkerOptions().title("b").position(new LatLng());
markerList.add(markerOptions1);
markerList.add(markerOptions2);
maplibreMap.addMarkers(markerList);
maplibreMap.removeAnnotations();
assertTrue("Annotations should be empty", maplibreMap.getAnnotations().isEmpty());
}));
}
@Test
public void testClear() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<BaseMarkerOptions> markerList = new ArrayList<>();
MarkerOptions markerOptions1 = new MarkerOptions().title("a").position(new LatLng());
MarkerOptions markerOptions2 = new MarkerOptions().title("b").position(new LatLng());
markerList.add(markerOptions1);
markerList.add(markerOptions2);
maplibreMap.addMarkers(markerList);
maplibreMap.clear();
assertTrue("Annotations should be empty", maplibreMap.getAnnotations().isEmpty());
}));
}
@Test
public void testRemoveAnnotationsByList() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
List<BaseMarkerOptions> markerList = new ArrayList<>();
MarkerOptions markerOptions1 = new MarkerOptions().title("a").position(new LatLng());
MarkerOptions markerOptions2 = new MarkerOptions().title("b").position(new LatLng());
markerList.add(markerOptions1);
markerList.add(markerOptions2);
List<Marker> markers = maplibreMap.addMarkers(markerList);
Marker marker = maplibreMap.addMarker(new MarkerOptions().position(new LatLng()).title("c"));
maplibreMap.removeAnnotations(markers);
assertTrue("Annotations should not be empty", maplibreMap.getAnnotations().size() == 1);
assertTrue("Marker should be contained", maplibreMap.getAnnotations().contains(marker));
}));
}
@Test
public void testGetAnnotationById() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker initialMarker = maplibreMap.addMarker(markerOptions);
Marker retrievedMarker = (Marker) maplibreMap.getAnnotation(0);
assertEquals("Markers should match", initialMarker, retrievedMarker);
}));
}
@Test
public void testGetAnnotations() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(
new MapLibreMapAction((uiController, view) ->
assertNotNull("Annotations should be non null", maplibreMap.getAnnotations()))
);
}
@Test
public void testGetMarkers() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(
new MapLibreMapAction((uiController, view) ->
assertNotNull("Markers should be non null", maplibreMap.getMarkers()))
);
}
@Test
public void testGetPolygons() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) ->
assertNotNull("Polygons should be non null", maplibreMap.getPolygons()))
);
}
@Test
public void testGetPolylines() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) ->
assertNotNull("Polylines should be non null", maplibreMap.getPolylines()))
);
}
@Test
public void testGetSelectedMarkers() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) ->
assertNotNull("Selected markers should be non null", maplibreMap.getSelectedMarkers()))
);
}
@Test
public void testSelectMarker() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker marker = maplibreMap.addMarker(markerOptions);
maplibreMap.selectMarker(marker);
assertTrue("Marker should be contained", maplibreMap.getSelectedMarkers().contains(marker));
}));
}
@Test
public void testDeselectMarker() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker marker = maplibreMap.addMarker(markerOptions);
maplibreMap.selectMarker(marker);
maplibreMap.deselectMarker(marker);
assertTrue("Selected markers should be empty", maplibreMap.getSelectedMarkers().isEmpty());
}));
}
@Test
public void testDeselectMarkers() {
validateTestSetup();
onView(withId(R.id.mapView)).perform(new MapLibreMapAction((uiController, view) -> {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
Marker marker1 = maplibreMap.addMarker(markerOptions);
Marker marker2 = maplibreMap.addMarker(markerOptions);
maplibreMap.selectMarker(marker1);
maplibreMap.selectMarker(marker2);
maplibreMap.deselectMarkers();
assertTrue("Selected markers should be empty", maplibreMap.getSelectedMarkers().isEmpty());
}));
}
public class MapLibreMapAction implements ViewAction {
private InvokeViewAction invokeViewAction;
MapLibreMapAction(InvokeViewAction invokeViewAction) {
this.invokeViewAction = invokeViewAction;
}
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}
@Override
public String getDescription() {
return getClass().getSimpleName();
}
@Override
public void perform(UiController uiController, View view) {
invokeViewAction.onViewAction(uiController, view);
}
}
interface InvokeViewAction {
void onViewAction(UiController uiController, View view);
}
}

View File

@@ -0,0 +1,454 @@
package org.maplibre.android.maps
import android.graphics.Color
import android.view.View
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers
import junit.framework.TestCase
import org.hamcrest.Matcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.maplibre.android.annotations.BaseMarkerOptions
import org.maplibre.android.annotations.Marker
import org.maplibre.android.annotations.MarkerOptions
import org.maplibre.android.annotations.PolygonOptions
import org.maplibre.android.annotations.PolylineOptions
import org.maplibre.android.exceptions.InvalidMarkerPositionException
import org.maplibre.android.geometry.LatLng
import org.maplibre.android.maps.MapLibreMap.InfoWindowAdapter
import org.maplibre.android.testapp.R
import org.maplibre.android.testapp.activity.EspressoTest
// J2K: IDE suggest "Add 'fun' modifier
/**
* This test is responsible for testing the public API.
*
*
* Methods executed on MapboxMap are called from a ViewAction to ensure correct synchronisation
* with the application UI-thread.
*
*/
@Deprecated("remove this file when removing deprecated annotations")
class MapLibreMapTest : EspressoTest() {
//
// InfoWindow
//
@Test
fun testConcurrentInfoWindowEnabled() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
maplibreMap.isAllowConcurrentMultipleOpenInfoWindows = true
assertTrue("ConcurrentWindows should be true", maplibreMap.isAllowConcurrentMultipleOpenInfoWindows)
})
}
@Test
fun testConcurrentInfoWindowDisabled() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
maplibreMap.isAllowConcurrentMultipleOpenInfoWindows = false
TestCase.assertFalse("ConcurrentWindows should be false", maplibreMap.isAllowConcurrentMultipleOpenInfoWindows)
})
}
@Test
fun testInfoWindowAdapter() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val infoWindowAdapter = InfoWindowAdapter { marker: Marker? -> null }
maplibreMap.infoWindowAdapter = infoWindowAdapter
assertEquals("InfoWindowAdpter should be the same", infoWindowAdapter, maplibreMap.infoWindowAdapter)
})
}
//
// Annotations
//
@Test
fun testAddMarker() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val marker = maplibreMap.addMarker(markerOptions)
assertTrue("Marker should be contained", maplibreMap.markers.contains(marker))
})
}
@Test(expected = InvalidMarkerPositionException::class)
fun testAddMarkerInvalidPosition() {
MarkerOptions().marker
}
@Test
fun testAddMarkers() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerList: MutableList<BaseMarkerOptions<*, *>> = ArrayList()
val markerOptions1 = MarkerOptions().position(LatLng()).title("a")
val markerOptions2 = MarkerOptions().position(LatLng()).title("b")
markerList.add(markerOptions1)
markerList.add(markerOptions2)
val markers = maplibreMap.addMarkers(markerList)
assertEquals("Markers size should be 2", 2, maplibreMap.markers.size.toLong())
assertTrue(maplibreMap.markers.contains(markers[0]))
assertTrue(maplibreMap.markers.contains(markers[1]))
})
}
@Test
fun testAddMarkersEmpty() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerList: List<BaseMarkerOptions<*, *>> = ArrayList()
maplibreMap.addMarkers(markerList)
assertEquals("Markers size should be 0", 0, maplibreMap.markers.size.toLong())
})
}
@Test
fun testAddMarkersSingleMarker() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerList: MutableList<BaseMarkerOptions<*, *>> = ArrayList()
val markerOptions = MarkerOptions().title("a").position(LatLng())
markerList.add(markerOptions)
val markers = maplibreMap.addMarkers(markerList)
assertEquals("Markers size should be 1", 1, maplibreMap.markers.size.toLong())
assertTrue(maplibreMap.markers.contains(markers[0]))
})
}
@Test
fun testAddPolygon() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polygonOptions = PolygonOptions().add(LatLng())
val polygon = maplibreMap.addPolygon(polygonOptions)
assertTrue("Polygon should be contained", maplibreMap.polygons.contains(polygon))
})
}
@Test
fun testAddEmptyPolygon() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polygonOptions = PolygonOptions()
val polygon = maplibreMap.addPolygon(polygonOptions)
assertTrue("Polygon should be ignored", !maplibreMap.polygons.contains(polygon))
})
}
@Test
fun testAddPolygons() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polygonList: MutableList<PolygonOptions> = ArrayList()
val polygonOptions1 = PolygonOptions().fillColor(Color.BLACK).add(LatLng())
val polygonOptions2 = PolygonOptions().fillColor(Color.WHITE).add(LatLng())
val polygonOptions3 = PolygonOptions()
polygonList.add(polygonOptions1)
polygonList.add(polygonOptions2)
polygonList.add(polygonOptions3)
maplibreMap.addPolygons(polygonList)
assertEquals("Polygons size should be 2", 2, maplibreMap.polygons.size.toLong())
assertTrue(maplibreMap.polygons.contains(polygonOptions1.polygon))
assertTrue(maplibreMap.polygons.contains(polygonOptions2.polygon))
assertTrue("Polygon should be ignored", !maplibreMap.polygons.contains(polygonOptions3.polygon))
})
}
@Test
fun addPolygonsEmpty() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
maplibreMap.addPolygons(ArrayList())
assertEquals("Polygons size should be 0", 0, maplibreMap.polygons.size.toLong())
})
}
@Test
fun addPolygonsSingle() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polygonList: MutableList<PolygonOptions> = ArrayList()
val polygonOptions = PolygonOptions().fillColor(Color.BLACK).add(LatLng())
polygonList.add(polygonOptions)
maplibreMap.addPolygons(polygonList)
assertEquals("Polygons size should be 1", 1, maplibreMap.polygons.size.toLong())
assertTrue(maplibreMap.polygons.contains(polygonOptions.polygon))
})
}
@Test
fun testAddPolyline() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polylineOptions = PolylineOptions().add(LatLng())
val polyline = maplibreMap.addPolyline(polylineOptions)
assertTrue("Polyline should be contained", maplibreMap.polylines.contains(polyline))
})
}
@Test
fun testAddEmptyPolyline() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polylineOptions = PolylineOptions()
val polyline = maplibreMap.addPolyline(polylineOptions)
assertTrue("Polyline should be ignored", !maplibreMap.polylines.contains(polyline))
})
}
@Test
fun testAddPolylines() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polylineList: MutableList<PolylineOptions> = ArrayList()
val polygonOptions1 = PolylineOptions().color(Color.BLACK).add(LatLng())
val polygonOptions2 = PolylineOptions().color(Color.WHITE).add(LatLng())
val polygonOptions3 = PolylineOptions()
polylineList.add(polygonOptions1)
polylineList.add(polygonOptions2)
polylineList.add(polygonOptions3)
maplibreMap.addPolylines(polylineList)
assertEquals("Polygons size should be 2", 2, maplibreMap.polylines.size.toLong())
assertTrue(maplibreMap.polylines.contains(polygonOptions1.polyline))
assertTrue(maplibreMap.polylines.contains(polygonOptions2.polyline))
assertTrue(
"Polyline should be ignored", !maplibreMap.polylines.contains(polygonOptions3.polyline)
)
})
}
@Test
fun testAddPolylinesEmpty() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
maplibreMap.addPolylines(ArrayList())
assertEquals("Polygons size should be 0", 0, maplibreMap.polylines.size.toLong())
})
}
@Test
fun testAddPolylinesSingle() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polylineList: MutableList<PolylineOptions> = ArrayList()
val polygonOptions = PolylineOptions().color(Color.BLACK).add(LatLng())
polylineList.add(polygonOptions)
maplibreMap.addPolylines(polylineList)
assertEquals("Polygons size should be 1", 1, maplibreMap.polylines.size.toLong())
assertTrue(maplibreMap.polylines.contains(polygonOptions.polyline))
})
}
@Test
fun testRemoveMarker() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val marker = maplibreMap.addMarker(markerOptions)
maplibreMap.removeMarker(marker)
assertTrue("Markers should be empty", maplibreMap.markers.isEmpty())
})
}
@Test
fun testRemovePolygon() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polygonOptions = PolygonOptions()
val polygon = maplibreMap.addPolygon(polygonOptions)
maplibreMap.removePolygon(polygon)
assertTrue("Polygons should be empty", maplibreMap.polylines.isEmpty())
})
}
@Test
fun testRemovePolyline() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val polylineOptions = PolylineOptions()
val polyline = maplibreMap.addPolyline(polylineOptions)
maplibreMap.removePolyline(polyline)
assertTrue("Polylines should be empty", maplibreMap.polylines.isEmpty())
})
}
@Test
fun testRemoveAnnotation() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val marker = maplibreMap.addMarker(markerOptions)
maplibreMap.removeAnnotation(marker)
assertTrue("Annotations should be empty", maplibreMap.annotations.isEmpty())
})
}
@Test
fun testRemoveAnnotationById() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
maplibreMap.addMarker(markerOptions)
// id will always be 0 in unit tests
maplibreMap.removeAnnotation(0)
assertTrue("Annotations should be empty", maplibreMap.annotations.isEmpty())
})
}
@Test
fun testRemoveAnnotations() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerList: MutableList<BaseMarkerOptions<*, *>> = ArrayList()
val markerOptions1 = MarkerOptions().title("a").position(LatLng())
val markerOptions2 = MarkerOptions().title("b").position(LatLng())
markerList.add(markerOptions1)
markerList.add(markerOptions2)
maplibreMap.addMarkers(markerList)
maplibreMap.removeAnnotations()
assertTrue("Annotations should be empty", maplibreMap.annotations.isEmpty())
})
}
@Test
fun testClear() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerList: MutableList<BaseMarkerOptions<*, *>> = ArrayList()
val markerOptions1 = MarkerOptions().title("a").position(LatLng())
val markerOptions2 = MarkerOptions().title("b").position(LatLng())
markerList.add(markerOptions1)
markerList.add(markerOptions2)
maplibreMap.addMarkers(markerList)
maplibreMap.clear()
assertTrue("Annotations should be empty", maplibreMap.annotations.isEmpty())
})
}
@Test
fun testRemoveAnnotationsByList() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerList: MutableList<BaseMarkerOptions<*, *>> = ArrayList()
val markerOptions1 = MarkerOptions().title("a").position(LatLng())
val markerOptions2 = MarkerOptions().title("b").position(LatLng())
markerList.add(markerOptions1)
markerList.add(markerOptions2)
val markers = maplibreMap.addMarkers(markerList)
val marker = maplibreMap.addMarker(MarkerOptions().position(LatLng()).title("c"))
maplibreMap.removeAnnotations(markers)
assertTrue("Annotations should not be empty", maplibreMap.annotations.size == 1)
assertTrue("Marker should be contained", maplibreMap.annotations.contains(marker))
})
}
@Test
fun testGetAnnotationById() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val initialMarker = maplibreMap.addMarker(markerOptions)
val retrievedMarker = maplibreMap.getAnnotation(0) as Marker?
assertEquals("Markers should match", initialMarker, retrievedMarker)
})
}
@Test
fun testGetAnnotations() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(
MapLibreMapAction { uiController: UiController?, view: View? -> TestCase.assertNotNull("Annotations should be non null", maplibreMap.annotations) }
)
}
@Test
fun testGetMarkers() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(
MapLibreMapAction { uiController: UiController?, view: View? -> TestCase.assertNotNull("Markers should be non null", maplibreMap.markers) }
)
}
@Test
fun testGetPolygons() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? -> TestCase.assertNotNull("Polygons should be non null", maplibreMap.polygons) }
)
}
@Test
fun testGetPolylines() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? -> TestCase.assertNotNull("Polylines should be non null", maplibreMap.polylines) }
)
}
@Test
fun testGetSelectedMarkers() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? -> TestCase.assertNotNull("Selected markers should be non null", maplibreMap.selectedMarkers) }
)
}
@Test
fun testSelectMarker() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val marker = maplibreMap.addMarker(markerOptions)
maplibreMap.selectMarker(marker)
assertTrue("Marker should be contained", maplibreMap.selectedMarkers.contains(marker))
})
}
@Test
fun testDeselectMarker() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val marker = maplibreMap.addMarker(markerOptions)
maplibreMap.selectMarker(marker)
maplibreMap.deselectMarker(marker)
assertTrue("Selected markers should be empty", maplibreMap.selectedMarkers.isEmpty())
})
}
@Test
fun testDeselectMarkers() {
validateTestSetup()
onView(ViewMatchers.withId(R.id.mapView)).perform(MapLibreMapAction { uiController: UiController?, view: View? ->
val markerOptions = MarkerOptions().position(LatLng())
val marker1 = maplibreMap.addMarker(markerOptions)
val marker2 = maplibreMap.addMarker(markerOptions)
maplibreMap.selectMarker(marker1)
maplibreMap.selectMarker(marker2)
maplibreMap.deselectMarkers()
assertTrue("Selected markers should be empty", maplibreMap.selectedMarkers.isEmpty())
})
}
inner class MapLibreMapAction internal constructor(private val invokeViewAction: InvokeViewAction) : ViewAction {
override fun getConstraints(): Matcher<View> {
return ViewMatchers.isDisplayed()
}
override fun getDescription(): String {
return javaClass.simpleName
}
override fun perform(uiController: UiController, view: View) {
invokeViewAction.onViewAction(uiController, view)
}
}
internal fun interface InvokeViewAction {
fun onViewAction(uiController: UiController?, view: View?)
}
}

View File

@@ -1,68 +0,0 @@
package org.maplibre.android.maps;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import androidx.test.annotation.UiThreadTest;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import org.maplibre.android.AppCenter;
import org.maplibre.android.MapLibre;
import org.maplibre.android.exceptions.MapLibreConfigurationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4ClassRunner.class)
public class MapLibreTest extends AppCenter {
private static final String API_KEY = "pk.0000000001";
private static final String API_KEY_2 = "pk.0000000002";
private String realToken;
@Before
public void setup() {
realToken = MapLibre.getApiKey();
}
@Test
@UiThreadTest
public void testConnected() {
assertTrue(MapLibre.isConnected());
// test manual connectivity
MapLibre.setConnected(true);
assertTrue(MapLibre.isConnected());
MapLibre.setConnected(false);
assertFalse(MapLibre.isConnected());
// reset to Android connectivity
MapLibre.setConnected(null);
assertTrue(MapLibre.isConnected());
}
@Test
@UiThreadTest
public void setApiKey() {
MapLibre.setApiKey(API_KEY);
assertSame(API_KEY, MapLibre.getApiKey());
MapLibre.setApiKey(API_KEY_2);
assertSame(API_KEY_2, MapLibre.getApiKey());
}
@Test
@UiThreadTest
public void setNullApiKey() {
assertThrows(MapLibreConfigurationException.class, () -> MapLibre.setApiKey(null));
}
@After
public void tearDown() {
MapLibre.setApiKey(realToken);
}
}

View File

@@ -0,0 +1,62 @@
package org.maplibre.android.maps
import androidx.test.annotation.UiThreadTest
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.maplibre.android.AppCenter
import org.maplibre.android.MapLibre.*
import org.maplibre.android.exceptions.MapLibreConfigurationException
@RunWith(AndroidJUnit4ClassRunner::class)
class MapLibreTest : AppCenter() {
private var realToken: String? = null
@Before
fun setup() {
realToken = getApiKey()
}
@Test
@UiThreadTest
fun testConnected() {
assertTrue(isConnected())
// test manual connectivity
setConnected(true)
assertTrue(isConnected())
setConnected(false)
assertFalse(isConnected())
// reset to Android connectivity
setConnected(null)
assertTrue(isConnected())
}
@Test
@UiThreadTest
fun setApiKey() {
setApiKey(API_KEY)
assertSame(API_KEY, getApiKey())
setApiKey(API_KEY_2)
assertSame(API_KEY_2, getApiKey())
}
@Test
@UiThreadTest
fun setNullApiKey() {
assertThrows(MapLibreConfigurationException::class.java) { setApiKey(null) }
}
@After
fun tearDown() {
setApiKey(realToken)
}
companion object {
private const val API_KEY = "pk.0000000001"
private const val API_KEY_2 = "pk.0000000002"
}
}