[android] #3756 - unit test annotations integration in MapboxMap
[android] #3756 - unit tests fixes [android] #3756 - allow using inheritance Marker, added example to WindowAdapterActivity
This commit is contained in:
@@ -150,7 +150,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
|
||||
|
||||
public LoadBulkMarkerTask(Context context, MapboxMap mapboxMap, int amount) {
|
||||
mMapboxMap = mapboxMap;
|
||||
mapboxMap.removeAllAnnotations();
|
||||
mapboxMap.removeAnnotations();
|
||||
mProgressDialog = ProgressDialog.show(context, "Loading", "Fetching markers", false);
|
||||
mAppContext = context.getApplicationContext();
|
||||
mAmount = amount;
|
||||
|
||||
@@ -78,7 +78,7 @@ public class GeocoderActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onMapClick(@NonNull LatLng point) {
|
||||
setMessage("Geocoding...");
|
||||
mapboxMap.removeAllAnnotations();
|
||||
mapboxMap.removeAnnotations();
|
||||
mapboxMap.addMarker(new MarkerOptions()
|
||||
.position(point)
|
||||
.title("Your finger is here"));
|
||||
|
||||
@@ -16,10 +16,11 @@ import android.widget.TextView;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
|
||||
import com.mapbox.mapboxsdk.testapp.annotations.CityStateMarker;
|
||||
import com.mapbox.mapboxsdk.testapp.annotations.CityStateMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.utils.ApiAccess;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
|
||||
@@ -54,12 +55,12 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onMapReady(@NonNull MapboxMap mapboxMap) {
|
||||
// add markers
|
||||
mapboxMap.addMarker(generateMarker("Andorra", 42.505777, 1.52529, "#F44336"));
|
||||
mapboxMap.addMarker(generateMarker("Luxembourg", 49.815273, 6.129583, "#3F51B5"));
|
||||
mapboxMap.addMarker(generateMarker("Monaco", 43.738418, 7.424616, "#673AB7"));
|
||||
mapboxMap.addMarker(generateMarker("Vatican City", 41.902916, 12.453389, "#009688"));
|
||||
mapboxMap.addMarker(generateMarker("San Marino", 43.942360, 12.457777, "#795548"));
|
||||
mapboxMap.addMarker(generateMarker("Liechtenstein", 47.166000, 9.555373, "#FF5722"));
|
||||
mapboxMap.addMarker(generateCityStateMarker("Andorra", 42.505777, 1.52529, "#F44336"));
|
||||
mapboxMap.addMarker(generateCityStateMarker("Luxembourg", 49.815273, 6.129583, "#3F51B5"));
|
||||
mapboxMap.addMarker(generateCityStateMarker("Monaco", 43.738418, 7.424616, "#673AB7"));
|
||||
mapboxMap.addMarker(generateCityStateMarker("Vatican City", 41.902916, 12.453389, "#009688"));
|
||||
mapboxMap.addMarker(generateCityStateMarker("San Marino", 43.942360, 12.457777, "#795548"));
|
||||
mapboxMap.addMarker(generateCityStateMarker("Liechtenstein", 47.166000, 9.555373, "#FF5722"));
|
||||
|
||||
// add custom window adapter
|
||||
mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
|
||||
@@ -71,7 +72,12 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
TextView textView = new TextView(InfoWindowAdapterActivity.this);
|
||||
textView.setText(marker.getTitle());
|
||||
textView.setTextColor(Color.WHITE);
|
||||
textView.setBackgroundColor(Color.parseColor(marker.getSnippet()));
|
||||
|
||||
if(marker instanceof CityStateMarker){
|
||||
CityStateMarker cityStateMarker = (CityStateMarker)marker;
|
||||
textView.setBackgroundColor(Color.parseColor(cityStateMarker.getInfoWindowBackgroundColor()));
|
||||
}
|
||||
|
||||
textView.setPadding(tenDp, tenDp, tenDp, tenDp);
|
||||
return textView;
|
||||
}
|
||||
@@ -80,13 +86,13 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
private MarkerOptions generateMarker(String title, double lat, double lng, String color) {
|
||||
MarkerOptions marker = new MarkerOptions();
|
||||
private CityStateMarkerOptions generateCityStateMarker(String title, double lat, double lng, String color) {
|
||||
CityStateMarkerOptions marker = new CityStateMarkerOptions();
|
||||
marker.title(title);
|
||||
marker.snippet(color);
|
||||
marker.position(new LatLng(lat, lng));
|
||||
marker.infoWindowBackground(color);
|
||||
|
||||
mIconDrawable.setColorFilter(Color.parseColor(marker.getSnippet()), PorterDuff.Mode.SRC_IN);
|
||||
mIconDrawable.setColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN);
|
||||
Icon icon = mIconFactory.fromDrawable(mIconDrawable);
|
||||
marker.icon(icon);
|
||||
return marker;
|
||||
@@ -144,4 +150,5 @@ public class InfoWindowAdapterActivity extends AppCompatActivity {
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -655,7 +655,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private void removeAnnotations() {
|
||||
mMarkerList.clear();
|
||||
mMapboxMap.removeAllAnnotations();
|
||||
mMapboxMap.removeAnnotations();
|
||||
}
|
||||
|
||||
private void addCustomLayer() {
|
||||
|
||||
@@ -169,7 +169,7 @@ public class PolylineActivity extends AppCompatActivity {
|
||||
case R.id.action_id_remove:
|
||||
// test to remove all annotations
|
||||
mPolylineOptions.clear();
|
||||
mMapboxMap.removeAllAnnotations();
|
||||
mMapboxMap.removeAnnotations();
|
||||
return true;
|
||||
|
||||
case android.R.id.home:
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.mapbox.mapboxsdk.testapp.annotations;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
|
||||
public class CityStateMarker extends Marker {
|
||||
|
||||
private String mInfoWindowBackgroundColor;
|
||||
|
||||
public CityStateMarker(CityStateMarkerOptions cityStateOptions, String color) {
|
||||
super(cityStateOptions);
|
||||
mInfoWindowBackgroundColor = color;
|
||||
}
|
||||
|
||||
public String getInfoWindowBackgroundColor() {
|
||||
return mInfoWindowBackgroundColor;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.mapbox.mapboxsdk.testapp.annotations;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Icon;
|
||||
import com.mapbox.mapboxsdk.annotations.IconFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
public class CityStateMarkerOptions extends BaseMarkerOptions<CityStateMarker, CityStateMarkerOptions> {
|
||||
|
||||
private String mInfoWindowBackgroundColor;
|
||||
|
||||
public CityStateMarkerOptions infoWindowBackground(String color) {
|
||||
mInfoWindowBackgroundColor = color;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public CityStateMarkerOptions() {
|
||||
}
|
||||
|
||||
private CityStateMarkerOptions(Parcel in) {
|
||||
position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
|
||||
snippet(in.readString());
|
||||
String iconId = in.readString();
|
||||
Bitmap iconBitmap = in.readParcelable(Bitmap.class.getClassLoader());
|
||||
Icon icon = IconFactory.recreate(iconId, iconBitmap);
|
||||
icon(icon);
|
||||
title(in.readString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CityStateMarkerOptions getThis() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CityStateMarker getMarker() {
|
||||
return new CityStateMarker(this, mInfoWindowBackgroundColor);
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<CityStateMarkerOptions> CREATOR
|
||||
= new Parcelable.Creator<CityStateMarkerOptions>() {
|
||||
public CityStateMarkerOptions createFromParcel(Parcel in) {
|
||||
return new CityStateMarkerOptions(in);
|
||||
}
|
||||
|
||||
public CityStateMarkerOptions[] newArray(int size) {
|
||||
return new CityStateMarkerOptions[size];
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public int describeContents() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel out, int flags) {
|
||||
out.writeParcelable(position, flags);
|
||||
out.writeString(snippet);
|
||||
out.writeString(icon.getId());
|
||||
out.writeParcelable(icon.getBitmap(), flags);
|
||||
out.writeString(title);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
package com.mapbox.mapboxsdk.maps;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Point;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Polygon;
|
||||
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
|
||||
import com.mapbox.mapboxsdk.annotations.Polyline;
|
||||
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.Style;
|
||||
@@ -15,9 +20,13 @@ import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.matches;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -46,6 +55,16 @@ public class MapboxMapTest {
|
||||
assertNotNull("mMapboxMap should not be null", mMapboxMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMock() {
|
||||
assertNotNull("mMapView should be mocked", mMapView);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMapView(){
|
||||
assertNotNull("MapView should be non null", mMapboxMap.getMapView());
|
||||
}
|
||||
|
||||
//
|
||||
// UiSettings
|
||||
//
|
||||
@@ -60,12 +79,11 @@ public class MapboxMapTest {
|
||||
//
|
||||
|
||||
@Test
|
||||
public void testProjection(){
|
||||
assertNotNull("Projection should not be null",mMapboxMap.getProjection());
|
||||
public void testProjection() {
|
||||
assertNotNull("Projection should not be null", mMapboxMap.getProjection());
|
||||
}
|
||||
|
||||
|
||||
|
||||
//
|
||||
// InfoWindow
|
||||
//
|
||||
@@ -368,6 +386,206 @@ public class MapboxMapTest {
|
||||
assertTrue("Zoomlevel should be same", 12 == mMapboxMap.getCameraPosition().zoom);
|
||||
}
|
||||
|
||||
//
|
||||
// Annotations
|
||||
//
|
||||
|
||||
@Test
|
||||
public void testAddMarker() {
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker marker = mMapboxMap.addMarker(markerOptions);
|
||||
assertTrue("Marker should be contained", mMapboxMap.getMarkers().contains(marker));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddMarkers() {
|
||||
List<MarkerOptions> markerList = new ArrayList<>();
|
||||
MarkerOptions markerOptions1 = new MarkerOptions().title("a");
|
||||
MarkerOptions markerOptions2 = new MarkerOptions().title("b");
|
||||
markerList.add(markerOptions1);
|
||||
markerList.add(markerOptions2);
|
||||
mMapboxMap.addMarkers(markerList);
|
||||
assertEquals("Markers size should be 2", 2, mMapboxMap.getMarkers().size());
|
||||
assertTrue(mMapboxMap.getMarkers().contains(markerOptions1.getMarker()));
|
||||
assertTrue(mMapboxMap.getMarkers().contains(markerOptions2.getMarker()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddPolygon() {
|
||||
PolygonOptions polygonOptions = new PolygonOptions();
|
||||
Polygon polygon = mMapboxMap.addPolygon(polygonOptions);
|
||||
assertTrue("Polygon should be contained", mMapboxMap.getPolygons().contains(polygon));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddPolygons() {
|
||||
List<PolygonOptions> polygonList = new ArrayList<>();
|
||||
PolygonOptions polygonOptions1 = new PolygonOptions().fillColor(Color.BLACK);
|
||||
PolygonOptions polygonOptions2 = new PolygonOptions().fillColor(Color.WHITE);
|
||||
polygonList.add(polygonOptions1);
|
||||
polygonList.add(polygonOptions2);
|
||||
mMapboxMap.addPolygons(polygonList);
|
||||
assertEquals("Polygons size should be 2", 2, mMapboxMap.getPolygons().size());
|
||||
assertTrue(mMapboxMap.getPolygons().contains(polygonOptions1.getPolygon()));
|
||||
assertTrue(mMapboxMap.getPolygons().contains(polygonOptions2.getPolygon()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddPolyline() {
|
||||
PolylineOptions polylineOptions = new PolylineOptions();
|
||||
Polyline polyline = mMapboxMap.addPolyline(polylineOptions);
|
||||
assertTrue("Polyline should be contained", mMapboxMap.getPolylines().contains(polyline));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddPolylines() {
|
||||
List<PolylineOptions> polylineList = new ArrayList<>();
|
||||
PolylineOptions polygonOptions1 = new PolylineOptions().color(Color.BLACK);
|
||||
PolylineOptions polygonOptions2 = new PolylineOptions().color(Color.WHITE);
|
||||
polylineList.add(polygonOptions1);
|
||||
polylineList.add(polygonOptions2);
|
||||
mMapboxMap.addPolylines(polylineList);
|
||||
assertEquals("Polygons size should be 2", 2, mMapboxMap.getPolylines().size());
|
||||
assertTrue(mMapboxMap.getPolylines().contains(polygonOptions1.getPolyline()));
|
||||
assertTrue(mMapboxMap.getPolylines().contains(polygonOptions2.getPolyline()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveMarker() {
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker marker = mMapboxMap.addMarker(markerOptions);
|
||||
mMapboxMap.removeMarker(marker);
|
||||
assertTrue("Markers should be empty", mMapboxMap.getMarkers().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePolygon() {
|
||||
PolygonOptions polygonOptions = new PolygonOptions();
|
||||
Polygon polygon = mMapboxMap.addPolygon(polygonOptions);
|
||||
mMapboxMap.removePolygon(polygon);
|
||||
assertTrue("Polygons should be empty", mMapboxMap.getPolylines().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemovePolyline() {
|
||||
PolylineOptions polylineOptions = new PolylineOptions();
|
||||
Polyline polyline = mMapboxMap.addPolyline(polylineOptions);
|
||||
mMapboxMap.removePolyline(polyline);
|
||||
assertTrue("Polylines should be empty", mMapboxMap.getPolylines().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAnnotation() {
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker marker = mMapboxMap.addMarker(markerOptions);
|
||||
mMapboxMap.removeAnnotation(marker);
|
||||
assertTrue("Annotations should be empty", mMapboxMap.getAnnotations().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAnnotationById() {
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
mMapboxMap.addMarker(markerOptions);
|
||||
// id will always be 0 in unit tests
|
||||
mMapboxMap.removeAnnotation(0);
|
||||
assertTrue("Annotations should be empty", mMapboxMap.getAnnotations().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAnnotations() {
|
||||
List<MarkerOptions> markerList = new ArrayList<>();
|
||||
MarkerOptions markerOptions1 = new MarkerOptions().title("a");
|
||||
MarkerOptions markerOptions2 = new MarkerOptions().title("b");
|
||||
markerList.add(markerOptions1);
|
||||
markerList.add(markerOptions2);
|
||||
mMapboxMap.addMarkers(markerList);
|
||||
mMapboxMap.removeAnnotations();
|
||||
assertTrue("Annotations should be empty", mMapboxMap.getAnnotations().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveAnnotationsByList() {
|
||||
List<MarkerOptions> markerList = new ArrayList<>();
|
||||
MarkerOptions markerOptions1 = new MarkerOptions().title("a");
|
||||
MarkerOptions markerOptions2 = new MarkerOptions().title("b");
|
||||
markerList.add(markerOptions1);
|
||||
markerList.add(markerOptions2);
|
||||
List<Marker> markers = mMapboxMap.addMarkers(markerList);
|
||||
Marker marker = mMapboxMap.addMarker(new MarkerOptions().title("c"));
|
||||
mMapboxMap.removeAnnotations(markers);
|
||||
assertTrue("Annotations should not be empty", mMapboxMap.getAnnotations().size() == 1);
|
||||
assertTrue("Marker should be contained", mMapboxMap.getAnnotations().contains(marker));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotationById() {
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker initialMarker = mMapboxMap.addMarker(markerOptions);
|
||||
Marker retrievedMarker = (Marker) mMapboxMap.getAnnotation(0);
|
||||
assertEquals("Markers should match", initialMarker, retrievedMarker);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAnnotations() {
|
||||
assertNotNull("Annotations should be non null", mMapboxMap.getAnnotations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetMarkers() {
|
||||
assertNotNull("Markers should be non null", mMapboxMap.getMarkers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPolygons() {
|
||||
assertNotNull("Polygons should be non null", mMapboxMap.getPolygons());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPolylines() {
|
||||
assertNotNull("Polylines should be non null", mMapboxMap.getPolylines());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSelectedMarkers() {
|
||||
assertNotNull("Selected markers should be non null", mMapboxMap.getSelectedMarkers());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectMarker() {
|
||||
mMapboxMap.setOnMarkerClickListener(mOnMarkerClickListener);
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker marker = mMapboxMap.addMarker(markerOptions);
|
||||
when(mOnMarkerClickListener.onMarkerClick(marker)).thenReturn(true);
|
||||
mMapboxMap.selectMarker(marker);
|
||||
assertTrue("Marker should be contained", mMapboxMap.getSelectedMarkers().contains(marker));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeselectMarker() {
|
||||
mMapboxMap.setOnMarkerClickListener(mOnMarkerClickListener);
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker marker = mMapboxMap.addMarker(markerOptions);
|
||||
when(mOnMarkerClickListener.onMarkerClick(marker)).thenReturn(true);
|
||||
mMapboxMap.selectMarker(marker);
|
||||
mMapboxMap.deselectMarker(marker);
|
||||
assertTrue("Selected markers should be empty", mMapboxMap.getSelectedMarkers().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeselectMarkers() {
|
||||
mMapboxMap.setOnMarkerClickListener(mOnMarkerClickListener);
|
||||
MarkerOptions markerOptions = new MarkerOptions();
|
||||
Marker marker1 = mMapboxMap.addMarker(markerOptions);
|
||||
Marker marker2 = mMapboxMap.addMarker(markerOptions);
|
||||
when(mOnMarkerClickListener.onMarkerClick(marker1)).thenReturn(true);
|
||||
when(mOnMarkerClickListener.onMarkerClick(marker2)).thenReturn(true);
|
||||
mMapboxMap.selectMarker(marker1);
|
||||
mMapboxMap.selectMarker(marker2);
|
||||
mMapboxMap.deselectMarkers();
|
||||
assertTrue("Selected markers should be empty", mMapboxMap.getSelectedMarkers().isEmpty());
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// OnMarkerClick interface
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user