[android] refactor move unit tests from test app to sdk and add some annotation manager tests (add marker and add markers) (#8261)

This commit is contained in:
Pablo Guardiola
2017-05-11 12:06:11 +02:00
committed by GitHub
parent c130d470a1
commit 5290d7ed4c
44 changed files with 942 additions and 392 deletions

View File

@@ -0,0 +1,15 @@
package com.mapbox.mapboxsdk;
/**
* Centralises the knowledge about "mapbox-gl" library loading.
*/
public class LibraryLoader {
/**
* Loads "libmapbox-gl.so" native shared library.
*/
public static void load() {
System.loadLibrary("mapbox-gl");
}
}

View File

@@ -0,0 +1,86 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.NonNull;
import android.support.v4.util.LongSparseArray;
import com.mapbox.mapboxsdk.annotations.Annotation;
import java.util.ArrayList;
import java.util.List;
/**
* Encapsulates {@link Annotation}'s functionality..
*/
class AnnotationContainer implements Annotations {
private final NativeMapView nativeMapView;
private final LongSparseArray<Annotation> annotations;
AnnotationContainer(NativeMapView nativeMapView, LongSparseArray<Annotation> annotations) {
this.nativeMapView = nativeMapView;
this.annotations = annotations;
}
@Override
public Annotation obtainBy(long id) {
return annotations.get(id);
}
@Override
public List<Annotation> obtainAll() {
List<Annotation> annotations = new ArrayList<>();
for (int i = 0; i < this.annotations.size(); i++) {
annotations.add(this.annotations.get(this.annotations.keyAt(i)));
}
return annotations;
}
@Override
public void removeBy(long id) {
if (nativeMapView != null) {
nativeMapView.removeAnnotation(id);
}
annotations.remove(id);
}
@Override
public void removeBy(@NonNull Annotation annotation) {
long id = annotation.getId();
removeBy(id);
}
@Override
public void removeBy(@NonNull List<? extends Annotation> annotationList) {
int count = annotationList.size();
long[] ids = new long[count];
for (int i = 0; i < count; i++) {
ids[i] = annotationList.get(i).getId();
}
removeNativeAnnotations(ids);
for (long id : ids) {
annotations.remove(id);
}
}
@Override
public void removeAll() {
int count = annotations.size();
long[] ids = new long[count];
for (int i = 0; i < count; i++) {
ids[i] = annotations.keyAt(i);
}
removeNativeAnnotations(ids);
annotations.clear();
}
private void removeNativeAnnotations(long[] ids) {
if (nativeMapView != null) {
nativeMapView.removeAnnotations(ids);
}
}
}

View File

@@ -9,7 +9,6 @@ import android.support.v4.util.LongSparseArray;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
@@ -22,8 +21,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import timber.log.Timber;
/**
* Responsible for managing and tracking state of Annotations linked to Map. All events related to
* annotations that occur on {@link MapboxMap} are forwarded to this class.
@@ -42,17 +39,28 @@ class AnnotationManager {
private final IconManager iconManager;
private final InfoWindowManager infoWindowManager = new InfoWindowManager();
private final MarkerViewManager markerViewManager;
private final LongSparseArray<Annotation> annotations = new LongSparseArray<>();
private final LongSparseArray<Annotation> annotationsArray;
private final List<Marker> selectedMarkers = new ArrayList<>();
private MapboxMap mapboxMap;
private MapboxMap.OnMarkerClickListener onMarkerClickListener;
private Annotations annotations;
private Markers markers;
private Polygons polygons;
private Polylines polylines;
AnnotationManager(NativeMapView view, MapView mapView, MarkerViewManager markerViewManager) {
AnnotationManager(NativeMapView view, MapView mapView, LongSparseArray<Annotation> annotationsArray,
MarkerViewManager markerViewManager, IconManager iconManager, Annotations annotations,
Markers markers, Polygons polygons, Polylines polylines) {
this.nativeMapView = view;
this.mapView = mapView;
this.iconManager = new IconManager(nativeMapView);
this.annotationsArray = annotationsArray;
this.markerViewManager = markerViewManager;
this.iconManager = iconManager;
this.annotations = annotations;
this.markers = markers;
this.polygons = polygons;
this.polylines = polylines;
if (view != null) {
// null checking needed for unit tests
nativeMapView.addOnMapChangedListener(markerViewManager);
@@ -77,15 +85,15 @@ class AnnotationManager {
//
Annotation getAnnotation(long id) {
return annotations.get(id);
return annotations.obtainBy(id);
}
List<Annotation> getAnnotations() {
List<Annotation> annotations = new ArrayList<>();
for (int i = 0; i < this.annotations.size(); i++) {
annotations.add(this.annotations.get(this.annotations.keyAt(i)));
}
return annotations;
return annotations.obtainAll();
}
void removeAnnotation(long id) {
annotations.removeBy(id);
}
void removeAnnotation(@NonNull Annotation annotation) {
@@ -96,25 +104,11 @@ class AnnotationManager {
markerViewManager.removeMarkerView((MarkerView) marker);
}
}
long id = annotation.getId();
if (nativeMapView != null) {
nativeMapView.removeAnnotation(id);
}
annotations.remove(id);
}
void removeAnnotation(long id) {
if (nativeMapView != null) {
nativeMapView.removeAnnotation(id);
}
annotations.remove(id);
annotations.removeBy(annotation);
}
void removeAnnotations(@NonNull List<? extends Annotation> annotationList) {
int count = annotationList.size();
long[] ids = new long[count];
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
for (Annotation annotation : annotationList) {
if (annotation instanceof Marker) {
Marker marker = (Marker) annotation;
marker.hideInfoWindow();
@@ -122,25 +116,17 @@ class AnnotationManager {
markerViewManager.removeMarkerView((MarkerView) marker);
}
}
ids[i] = annotationList.get(i).getId();
}
if (nativeMapView != null) {
nativeMapView.removeAnnotations(ids);
}
for (long id : ids) {
annotations.remove(id);
}
annotations.removeBy(annotationList);
}
void removeAnnotations() {
Annotation annotation;
int count = annotations.size();
int count = annotationsArray.size();
long[] ids = new long[count];
for (int i = 0; i < count; i++) {
ids[i] = annotations.keyAt(i);
annotation = annotations.get(ids[i]);
ids[i] = annotationsArray.keyAt(i);
annotation = annotationsArray.get(ids[i]);
if (annotation instanceof Marker) {
Marker marker = (Marker) annotation;
marker.hideInfoWindow();
@@ -149,12 +135,7 @@ class AnnotationManager {
}
}
}
if (nativeMapView != null) {
nativeMapView.removeAnnotations(ids);
}
annotations.clear();
annotations.removeAll();
}
//
@@ -162,139 +143,86 @@ class AnnotationManager {
//
Marker addMarker(@NonNull BaseMarkerOptions markerOptions, @NonNull MapboxMap mapboxMap) {
Marker marker = prepareMarker(markerOptions);
long id = nativeMapView != null ? nativeMapView.addMarker(marker) : 0;
marker.setMapboxMap(mapboxMap);
marker.setId(id);
annotations.put(id, marker);
return marker;
return markers.addBy(markerOptions, mapboxMap);
}
List<Marker> addMarkers(@NonNull List<? extends BaseMarkerOptions> markerOptionsList, @NonNull MapboxMap mapboxMap) {
int count = markerOptionsList.size();
List<Marker> markers = new ArrayList<>(count);
if (count > 0) {
BaseMarkerOptions markerOptions;
Marker marker;
for (int i = 0; i < count; i++) {
markerOptions = markerOptionsList.get(i);
marker = prepareMarker(markerOptions);
markers.add(marker);
}
if (markers.size() > 0) {
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addMarkers(markers);
}
long id = 0;
Marker m;
for (int i = 0; i < markers.size(); i++) {
m = markers.get(i);
m.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i];
} else {
// unit test
id++;
}
m.setId(id);
annotations.put(id, m);
}
}
}
return markers;
return markers.addBy(markerOptionsList, mapboxMap);
}
private Marker prepareMarker(BaseMarkerOptions markerOptions) {
Marker marker = markerOptions.getMarker();
Icon icon = iconManager.loadIconForMarker(marker);
marker.setTopOffsetPixels(iconManager.getTopOffsetPixelsForIcon(icon));
return marker;
void updateMarker(@NonNull Marker updatedMarker, @NonNull MapboxMap mapboxMap) {
markers.update(updatedMarker, mapboxMap);
}
List<Marker> getMarkers() {
return markers.obtainAll();
}
@NonNull
List<Marker> getMarkersInRect(@NonNull RectF rectangle) {
return markers.obtainAllIn(rectangle);
}
MarkerView addMarker(@NonNull BaseMarkerViewOptions markerOptions, @NonNull MapboxMap mapboxMap,
@Nullable MarkerViewManager.OnMarkerViewAddedListener onMarkerViewAddedListener) {
final MarkerView marker = prepareViewMarker(markerOptions);
// add marker to map
marker.setMapboxMap(mapboxMap);
long id = nativeMapView.addMarker(marker);
marker.setId(id);
annotations.put(id, marker);
if (onMarkerViewAddedListener != null) {
markerViewManager.addOnMarkerViewAddedListener(marker, onMarkerViewAddedListener);
}
markerViewManager.setEnabled(true);
markerViewManager.setWaitingForRenderInvoke(true);
return marker;
return markers.addViewBy(markerOptions, mapboxMap, onMarkerViewAddedListener);
}
List<MarkerView> addMarkerViews(@NonNull List<? extends BaseMarkerViewOptions> markerViewOptions,
@NonNull MapboxMap mapboxMap) {
List<MarkerView> markers = new ArrayList<>();
for (BaseMarkerViewOptions markerViewOption : markerViewOptions) {
// if last marker
if (markerViewOptions.indexOf(markerViewOption) == markerViewOptions.size() - 1) {
// get notified when render occurs to invalidate and draw MarkerViews
markerViewManager.setWaitingForRenderInvoke(true);
}
// add marker to map
MarkerView marker = prepareViewMarker(markerViewOption);
marker.setMapboxMap(mapboxMap);
long id = nativeMapView.addMarker(marker);
marker.setId(id);
annotations.put(id, marker);
markers.add(marker);
}
markerViewManager.setEnabled(true);
markerViewManager.update();
return markers;
return markers.addViewsBy(markerViewOptions, mapboxMap);
}
private MarkerView prepareViewMarker(BaseMarkerViewOptions markerViewOptions) {
MarkerView marker = markerViewOptions.getMarker();
iconManager.loadIconForMarkerView(marker);
return marker;
List<MarkerView> getMarkerViewsInRect(@NonNull RectF rectangle) {
return markers.obtainViewsIn(rectangle);
}
void updateMarker(@NonNull Marker updatedMarker) {
if (!isAddedToMap(updatedMarker)) {
Timber.w("Attempting to update non-added Marker with value %s", updatedMarker);
return;
}
ensureIconLoaded(updatedMarker);
nativeMapView.updateMarker(updatedMarker);
annotations.setValueAt(annotations.indexOfKey(updatedMarker.getId()), updatedMarker);
void reloadMarkers() {
markers.reload();
}
private boolean isAddedToMap(Annotation annotation) {
return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1;
//
// Polygons
//
Polygon addPolygon(@NonNull PolygonOptions polygonOptions, @NonNull MapboxMap mapboxMap) {
return polygons.addBy(polygonOptions, mapboxMap);
}
private void ensureIconLoaded(Marker marker) {
if (!(marker instanceof MarkerView)) {
iconManager.ensureIconLoaded(marker, mapboxMap);
}
List<Polygon> addPolygons(@NonNull List<PolygonOptions> polygonOptionsList, @NonNull MapboxMap mapboxMap) {
return polygons.addBy(polygonOptionsList, mapboxMap);
}
List<Marker> getMarkers() {
List<Marker> markers = new ArrayList<>();
Annotation annotation;
for (int i = 0; i < annotations.size(); i++) {
annotation = annotations.get(annotations.keyAt(i));
if (annotation instanceof Marker) {
markers.add((Marker) annotation);
}
}
return markers;
void updatePolygon(Polygon polygon) {
polygons.update(polygon);
}
List<Polygon> getPolygons() {
return polygons.obtainAll();
}
//
// Polylines
//
Polyline addPolyline(@NonNull PolylineOptions polylineOptions, @NonNull MapboxMap mapboxMap) {
return polylines.addBy(polylineOptions, mapboxMap);
}
List<Polyline> addPolylines(@NonNull List<PolylineOptions> polylineOptionsList, @NonNull MapboxMap mapboxMap) {
return polylines.addBy(polylineOptionsList, mapboxMap);
}
void updatePolyline(Polyline polyline) {
polylines.update(polyline);
}
List<Polyline> getPolylines() {
return polylines.obtainAll();
}
// TODO Refactor from here still in progress
void setOnMarkerClickListener(@Nullable MapboxMap.OnMarkerClickListener listener) {
onMarkerClickListener = listener;
}
@@ -361,207 +289,6 @@ class AnnotationManager {
return selectedMarkers;
}
@NonNull
List<Marker> getMarkersInRect(@NonNull RectF rectangle) {
// convert Rectangle to be density depedent
float pixelRatio = nativeMapView.getPixelRatio();
RectF rect = new RectF(rectangle.left / pixelRatio,
rectangle.top / pixelRatio,
rectangle.right / pixelRatio,
rectangle.bottom / pixelRatio);
long[] ids = nativeMapView.queryPointAnnotations(rect);
List<Long> idsList = new ArrayList<>(ids.length);
for (long id : ids) {
idsList.add(id);
}
List<Marker> annotations = new ArrayList<>(ids.length);
List<Annotation> annotationList = getAnnotations();
int count = annotationList.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
if (annotation instanceof com.mapbox.mapboxsdk.annotations.Marker && idsList.contains(annotation.getId())) {
annotations.add((com.mapbox.mapboxsdk.annotations.Marker) annotation);
}
}
return new ArrayList<>(annotations);
}
List<MarkerView> getMarkerViewsInRect(@NonNull RectF rectangle) {
float pixelRatio = nativeMapView.getPixelRatio();
RectF rect = new RectF(rectangle.left / pixelRatio,
rectangle.top / pixelRatio,
rectangle.right / pixelRatio,
rectangle.bottom / pixelRatio);
long[] ids = nativeMapView.queryPointAnnotations(rect);
List<Long> idsList = new ArrayList<>(ids.length);
for (long id : ids) {
idsList.add(id);
}
List<MarkerView> annotations = new ArrayList<>(ids.length);
List<Annotation> annotationList = getAnnotations();
int count = annotationList.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
if (annotation instanceof MarkerView && idsList.contains(annotation.getId())) {
annotations.add((MarkerView) annotation);
}
}
return new ArrayList<>(annotations);
}
//
// Polygons
//
Polygon addPolygon(@NonNull PolygonOptions polygonOptions, @NonNull MapboxMap mapboxMap) {
Polygon polygon = polygonOptions.getPolygon();
if (!polygon.getPoints().isEmpty()) {
long id = nativeMapView != null ? nativeMapView.addPolygon(polygon) : 0;
polygon.setId(id);
polygon.setMapboxMap(mapboxMap);
annotations.put(id, polygon);
}
return polygon;
}
List<Polygon> addPolygons(@NonNull List<PolygonOptions> polygonOptionsList, @NonNull MapboxMap mapboxMap) {
int count = polygonOptionsList.size();
Polygon polygon;
List<Polygon> polygons = new ArrayList<>(count);
if (count > 0) {
for (PolygonOptions polygonOptions : polygonOptionsList) {
polygon = polygonOptions.getPolygon();
if (!polygon.getPoints().isEmpty()) {
polygons.add(polygon);
}
}
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addPolygons(polygons);
}
long id = 0;
for (int i = 0; i < polygons.size(); i++) {
polygon = polygons.get(i);
polygon.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i];
} else {
// unit test
id++;
}
polygon.setId(id);
annotations.put(id, polygon);
}
}
return polygons;
}
void updatePolygon(@NonNull Polygon polygon) {
if (!isAddedToMap(polygon)) {
Timber.w("Attempting to update non-added Polygon with value %s", polygon);
return;
}
nativeMapView.updatePolygon(polygon);
annotations.setValueAt(annotations.indexOfKey(polygon.getId()), polygon);
}
List<Polygon> getPolygons() {
List<Polygon> polygons = new ArrayList<>();
Annotation annotation;
for (int i = 0; i < annotations.size(); i++) {
annotation = annotations.get(annotations.keyAt(i));
if (annotation instanceof Polygon) {
polygons.add((Polygon) annotation);
}
}
return polygons;
}
//
// Polylines
//
Polyline addPolyline(@NonNull PolylineOptions polylineOptions, @NonNull MapboxMap mapboxMap) {
Polyline polyline = polylineOptions.getPolyline();
if (!polyline.getPoints().isEmpty()) {
long id = nativeMapView != null ? nativeMapView.addPolyline(polyline) : 0;
polyline.setMapboxMap(mapboxMap);
polyline.setId(id);
annotations.put(id, polyline);
}
return polyline;
}
List<Polyline> addPolylines(@NonNull List<PolylineOptions> polylineOptionsList, @NonNull MapboxMap mapboxMap) {
int count = polylineOptionsList.size();
Polyline polyline;
List<Polyline> polylines = new ArrayList<>(count);
if (count > 0) {
for (PolylineOptions options : polylineOptionsList) {
polyline = options.getPolyline();
if (!polyline.getPoints().isEmpty()) {
polylines.add(polyline);
}
}
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addPolylines(polylines);
}
long id = 0;
Polyline p;
for (int i = 0; i < polylines.size(); i++) {
p = polylines.get(i);
p.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i];
} else {
// unit test
id++;
}
p.setId(id);
annotations.put(id, p);
}
}
return polylines;
}
void updatePolyline(@NonNull Polyline polyline) {
if (!isAddedToMap(polyline)) {
Timber.w("Attempting to update non-added Polyline with value %s", polyline);
}
nativeMapView.updatePolyline(polyline);
annotations.setValueAt(annotations.indexOfKey(polyline.getId()), polyline);
}
List<Polyline> getPolylines() {
List<Polyline> polylines = new ArrayList<>();
Annotation annotation;
for (int i = 0; i < annotations.size(); i++) {
annotation = annotations.get(annotations.keyAt(i));
if (annotation instanceof Polyline) {
polylines.add((Polyline) annotation);
}
}
return polylines;
}
InfoWindowManager getInfoWindowManager() {
return infoWindowManager;
}
@@ -571,9 +298,9 @@ class AnnotationManager {
}
void adjustTopOffsetPixels(MapboxMap mapboxMap) {
int count = annotations.size();
int count = annotationsArray.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotations.get(i);
Annotation annotation = annotationsArray.get(i);
if (annotation instanceof Marker) {
Marker marker = (Marker) annotation;
marker.setTopOffsetPixels(
@@ -589,20 +316,6 @@ class AnnotationManager {
}
}
void reloadMarkers() {
iconManager.reloadIcons();
int count = annotations.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotations.get(i);
if (annotation instanceof Marker) {
Marker marker = (Marker) annotation;
nativeMapView.removeAnnotation(annotation.getId());
long newId = nativeMapView.addMarker(marker);
marker.setId(newId);
}
}
}
//
// Click event
//

View File

@@ -0,0 +1,25 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.annotations.Annotation;
import java.util.List;
/**
* Interface that defines convenient methods for working with a {@link Annotation}'s collection.
*/
interface Annotations {
Annotation obtainBy(long id);
List<Annotation> obtainAll();
void removeBy(long id);
void removeBy(@NonNull Annotation annotation);
void removeBy(@NonNull List<? extends Annotation> annotationList);
void removeAll();
}

View File

@@ -13,6 +13,7 @@ import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.util.LongSparseArray;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
@@ -29,6 +30,7 @@ import android.widget.ZoomButtonsController;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.Style;
@@ -134,14 +136,22 @@ public class MapView extends FrameLayout {
UiSettings uiSettings = new UiSettings(proj, focalPoint, compassView, attrView, view.findViewById(R.id.logoView));
TrackingSettings trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPoint, zoomInvalidator);
MyLocationViewSettings myLocationViewSettings = new MyLocationViewSettings(myLocationView, proj, focalPoint);
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
MarkerViewManager markerViewManager = new MarkerViewManager((ViewGroup) findViewById(R.id.markerViewContainer));
AnnotationManager annotations = new AnnotationManager(nativeMapView, this, markerViewManager);
Transform transform = new Transform(nativeMapView, annotations.getMarkerViewManager(), trackingSettings);
IconManager iconManager = new IconManager(nativeMapView);
Annotations annotations = new AnnotationContainer(nativeMapView, annotationsArray);
Markers markers = new MarkerContainer(nativeMapView, this, annotationsArray, iconManager, markerViewManager);
Polygons polygons = new PolygonContainer(nativeMapView, annotationsArray);
Polylines polylines = new PolylineContainer(nativeMapView, annotationsArray);
AnnotationManager annotationManager = new AnnotationManager(nativeMapView, this, annotationsArray,
markerViewManager, iconManager, annotations, markers, polygons, polylines);
Transform transform = new Transform(nativeMapView, annotationManager.getMarkerViewManager(), trackingSettings);
mapboxMap = new MapboxMap(nativeMapView, transform, uiSettings, trackingSettings, myLocationViewSettings, proj,
registerTouchListener, annotations);
registerTouchListener, annotationManager);
// user input
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings, annotations);
mapGestureDetector = new MapGestureDetector(context, transform, proj, uiSettings, trackingSettings,
annotationManager);
mapKeyListener = new MapKeyListener(transform, trackingSettings, uiSettings);
MapZoomControllerListener zoomListener = new MapZoomControllerListener(mapGestureDetector, uiSettings, transform);

View File

@@ -1179,7 +1179,7 @@ public final class MapboxMap {
*/
@UiThread
public void updateMarker(@NonNull Marker updatedMarker) {
annotationManager.updateMarker(updatedMarker);
annotationManager.updateMarker(updatedMarker, this);
}
/**

View File

@@ -0,0 +1,267 @@
package com.mapbox.mapboxsdk.maps;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.util.LongSparseArray;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;
/**
* Encapsulates {@link Marker}'s functionality.
*/
class MarkerContainer implements Markers {
private final NativeMapView nativeMapView;
private final MapView mapView;
private final LongSparseArray<Annotation> annotations;
private final IconManager iconManager;
private final MarkerViewManager markerViewManager;
MarkerContainer(NativeMapView nativeMapView, MapView mapView, LongSparseArray<Annotation> annotations, IconManager
iconManager, MarkerViewManager markerViewManager) {
this.nativeMapView = nativeMapView;
this.mapView = mapView;
this.annotations = annotations;
this.iconManager = iconManager;
this.markerViewManager = markerViewManager;
}
@Override
public Marker addBy(@NonNull BaseMarkerOptions markerOptions, @NonNull MapboxMap mapboxMap) {
Marker marker = prepareMarker(markerOptions);
long id = nativeMapView != null ? nativeMapView.addMarker(marker) : 0;
marker.setMapboxMap(mapboxMap);
marker.setId(id);
annotations.put(id, marker);
return marker;
}
@Override
public List<Marker> addBy(@NonNull List<? extends BaseMarkerOptions> markerOptionsList, @NonNull MapboxMap
mapboxMap) {
int count = markerOptionsList.size();
List<Marker> markers = new ArrayList<>(count);
if (count > 0) {
BaseMarkerOptions markerOptions;
Marker marker;
for (int i = 0; i < count; i++) {
markerOptions = markerOptionsList.get(i);
marker = prepareMarker(markerOptions);
markers.add(marker);
}
if (markers.size() > 0) {
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addMarkers(markers);
}
long id = 0;
Marker m;
for (int i = 0; i < markers.size(); i++) {
m = markers.get(i);
m.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i];
} else {
// unit test
id++;
}
m.setId(id);
annotations.put(id, m);
}
}
}
return markers;
}
@Override
public void update(@NonNull Marker updatedMarker, @NonNull MapboxMap mapboxMap) {
if (!isAddedToMap(updatedMarker)) {
Timber.w("Attempting to update non-added Marker with value %s", updatedMarker);
return;
}
ensureIconLoaded(updatedMarker, mapboxMap);
nativeMapView.updateMarker(updatedMarker);
annotations.setValueAt(annotations.indexOfKey(updatedMarker.getId()), updatedMarker);
}
@Override
public List<Marker> obtainAll() {
List<Marker> markers = new ArrayList<>();
Annotation annotation;
for (int i = 0; i < annotations.size(); i++) {
annotation = annotations.get(annotations.keyAt(i));
if (annotation instanceof Marker) {
markers.add((Marker) annotation);
}
}
return markers;
}
@NonNull
@Override
public List<Marker> obtainAllIn(@NonNull RectF rectangle) {
// convert Rectangle to be density depedent
float pixelRatio = nativeMapView.getPixelRatio();
RectF rect = new RectF(rectangle.left / pixelRatio,
rectangle.top / pixelRatio,
rectangle.right / pixelRatio,
rectangle.bottom / pixelRatio);
long[] ids = nativeMapView.queryPointAnnotations(rect);
List<Long> idsList = new ArrayList<>(ids.length);
for (long id : ids) {
idsList.add(id);
}
List<Marker> annotations = new ArrayList<>(ids.length);
List<Annotation> annotationList = obtainAnnotations();
int count = annotationList.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
if (annotation instanceof com.mapbox.mapboxsdk.annotations.Marker && idsList.contains(annotation.getId())) {
annotations.add((com.mapbox.mapboxsdk.annotations.Marker) annotation);
}
}
return new ArrayList<>(annotations);
}
@Override
public MarkerView addViewBy(@NonNull BaseMarkerViewOptions markerOptions, @NonNull MapboxMap mapboxMap, @Nullable
MarkerViewManager.OnMarkerViewAddedListener onMarkerViewAddedListener) {
final MarkerView marker = prepareViewMarker(markerOptions);
// add marker to map
marker.setMapboxMap(mapboxMap);
long id = nativeMapView.addMarker(marker);
marker.setId(id);
annotations.put(id, marker);
if (onMarkerViewAddedListener != null) {
markerViewManager.addOnMarkerViewAddedListener(marker, onMarkerViewAddedListener);
}
markerViewManager.setEnabled(true);
markerViewManager.setWaitingForRenderInvoke(true);
return marker;
}
@Override
public List<MarkerView> addViewsBy(@NonNull List<? extends BaseMarkerViewOptions> markerViewOptions, @NonNull
MapboxMap mapboxMap) {
List<MarkerView> markers = new ArrayList<>();
for (BaseMarkerViewOptions markerViewOption : markerViewOptions) {
// if last marker
if (markerViewOptions.indexOf(markerViewOption) == markerViewOptions.size() - 1) {
// get notified when render occurs to invalidate and draw MarkerViews
markerViewManager.setWaitingForRenderInvoke(true);
}
// add marker to map
MarkerView marker = prepareViewMarker(markerViewOption);
marker.setMapboxMap(mapboxMap);
long id = nativeMapView.addMarker(marker);
marker.setId(id);
annotations.put(id, marker);
markers.add(marker);
}
markerViewManager.setEnabled(true);
markerViewManager.update();
return markers;
}
@Override
public List<MarkerView> obtainViewsIn(@NonNull RectF rectangle) {
float pixelRatio = nativeMapView.getPixelRatio();
RectF rect = new RectF(rectangle.left / pixelRatio,
rectangle.top / pixelRatio,
rectangle.right / pixelRatio,
rectangle.bottom / pixelRatio);
long[] ids = nativeMapView.queryPointAnnotations(rect);
List<Long> idsList = new ArrayList<>(ids.length);
for (long id : ids) {
idsList.add(id);
}
List<MarkerView> annotations = new ArrayList<>(ids.length);
List<Annotation> annotationList = obtainAnnotations();
int count = annotationList.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotationList.get(i);
if (annotation instanceof MarkerView && idsList.contains(annotation.getId())) {
annotations.add((MarkerView) annotation);
}
}
return new ArrayList<>(annotations);
}
@Override
public void reload() {
iconManager.reloadIcons();
int count = annotations.size();
for (int i = 0; i < count; i++) {
Annotation annotation = annotations.get(i);
if (annotation instanceof Marker) {
Marker marker = (Marker) annotation;
nativeMapView.removeAnnotation(annotation.getId());
long newId = nativeMapView.addMarker(marker);
marker.setId(newId);
}
}
}
private Marker prepareMarker(BaseMarkerOptions markerOptions) {
Marker marker = markerOptions.getMarker();
Icon icon = iconManager.loadIconForMarker(marker);
marker.setTopOffsetPixels(iconManager.getTopOffsetPixelsForIcon(icon));
return marker;
}
private boolean isAddedToMap(Annotation annotation) {
return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1;
}
private void ensureIconLoaded(Marker marker, MapboxMap mapboxMap) {
if (!(marker instanceof MarkerView)) {
iconManager.ensureIconLoaded(marker, mapboxMap);
}
}
private List<Annotation> obtainAnnotations() {
List<Annotation> annotations = new ArrayList<>();
for (int i = 0; i < this.annotations.size(); i++) {
annotations.add(this.annotations.get(this.annotations.keyAt(i)));
}
return annotations;
}
private MarkerView prepareViewMarker(BaseMarkerViewOptions markerViewOptions) {
MarkerView marker = markerViewOptions.getMarker();
Icon icon = markerViewOptions.getIcon();
if (icon == null) {
icon = IconFactory.getInstance(mapView.getContext()).defaultMarkerView();
}
marker.setIcon(icon);
return marker;
}
}

View File

@@ -0,0 +1,39 @@
package com.mapbox.mapboxsdk.maps;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.BaseMarkerViewOptions;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
import java.util.List;
/**
* Interface that defines convenient methods for working with a {@link Marker}'s collection.
*/
interface Markers {
Marker addBy(@NonNull BaseMarkerOptions markerOptions, @NonNull MapboxMap mapboxMap);
List<Marker> addBy(@NonNull List<? extends BaseMarkerOptions> markerOptionsList, @NonNull MapboxMap mapboxMap);
void update(@NonNull Marker updatedMarker, @NonNull MapboxMap mapboxMap);
List<Marker> obtainAll();
List<Marker> obtainAllIn(@NonNull RectF rectangle);
MarkerView addViewBy(@NonNull BaseMarkerViewOptions markerOptions, @NonNull MapboxMap mapboxMap,
@Nullable MarkerViewManager.OnMarkerViewAddedListener onMarkerViewAddedListener);
List<MarkerView> addViewsBy(@NonNull List<? extends BaseMarkerViewOptions> markerViewOptions,
@NonNull MapboxMap mapboxMap);
List<MarkerView> obtainViewsIn(@NonNull RectF rectangle);
void reload();
}

View File

@@ -13,6 +13,7 @@ import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Surface;
import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.Polygon;
@@ -63,12 +64,8 @@ final class NativeMapView {
// Listener invoked to return a bitmap of the map
private MapboxMap.SnapshotReadyCallback snapshotReadyCallback;
//
// Static methods
//
static {
System.loadLibrary("mapbox-gl");
LibraryLoader.load();
}
//

View File

@@ -0,0 +1,104 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.NonNull;
import android.support.v4.util.LongSparseArray;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;
/**
* Encapsulates {@link Polygon}'s functionality.
*/
class PolygonContainer implements Polygons {
private final NativeMapView nativeMapView;
private final LongSparseArray<Annotation> annotations;
PolygonContainer(NativeMapView nativeMapView, LongSparseArray<Annotation> annotations) {
this.nativeMapView = nativeMapView;
this.annotations = annotations;
}
@Override
public Polygon addBy(@NonNull PolygonOptions polygonOptions, @NonNull MapboxMap mapboxMap) {
Polygon polygon = polygonOptions.getPolygon();
if (!polygon.getPoints().isEmpty()) {
long id = nativeMapView != null ? nativeMapView.addPolygon(polygon) : 0;
polygon.setId(id);
polygon.setMapboxMap(mapboxMap);
annotations.put(id, polygon);
}
return polygon;
}
@Override
public List<Polygon> addBy(@NonNull List<PolygonOptions> polygonOptionsList, @NonNull MapboxMap mapboxMap) {
int count = polygonOptionsList.size();
Polygon polygon;
List<Polygon> polygons = new ArrayList<>(count);
if (count > 0) {
for (PolygonOptions polygonOptions : polygonOptionsList) {
polygon = polygonOptions.getPolygon();
if (!polygon.getPoints().isEmpty()) {
polygons.add(polygon);
}
}
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addPolygons(polygons);
}
long id = 0;
for (int i = 0; i < polygons.size(); i++) {
polygon = polygons.get(i);
polygon.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i];
} else {
// unit test
id++;
}
polygon.setId(id);
annotations.put(id, polygon);
}
}
return polygons;
}
@Override
public void update(Polygon polygon) {
if (!isAddedToMap(polygon)) {
Timber.w("Attempting to update non-added Polygon with value %s", polygon);
return;
}
nativeMapView.updatePolygon(polygon);
annotations.setValueAt(annotations.indexOfKey(polygon.getId()), polygon);
}
@Override
public List<Polygon> obtainAll() {
List<Polygon> polygons = new ArrayList<>();
Annotation annotation;
for (int i = 0; i < annotations.size(); i++) {
annotation = annotations.get(annotations.keyAt(i));
if (annotation instanceof Polygon) {
polygons.add((Polygon) annotation);
}
}
return polygons;
}
private boolean isAddedToMap(Annotation annotation) {
return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1;
}
}

View File

@@ -0,0 +1,22 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import java.util.List;
/**
* Interface that defines convenient methods for working with a {@link Polygon}'s collection.
*/
interface Polygons {
Polygon addBy(@NonNull PolygonOptions polygonOptions, @NonNull MapboxMap mapboxMap);
List<Polygon> addBy(@NonNull List<PolygonOptions> polygonOptionsList, @NonNull MapboxMap mapboxMap);
void update(Polygon polygon);
List<Polygon> obtainAll();
}

View File

@@ -0,0 +1,105 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.NonNull;
import android.support.v4.util.LongSparseArray;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;
/**
* Encapsulates {@link Polyline}'s functionality.
*/
class PolylineContainer implements Polylines {
private final NativeMapView nativeMapView;
private final LongSparseArray<Annotation> annotations;
PolylineContainer(NativeMapView nativeMapView, LongSparseArray<Annotation> annotations) {
this.nativeMapView = nativeMapView;
this.annotations = annotations;
}
@Override
public Polyline addBy(@NonNull PolylineOptions polylineOptions, @NonNull MapboxMap mapboxMap) {
Polyline polyline = polylineOptions.getPolyline();
if (!polyline.getPoints().isEmpty()) {
long id = nativeMapView != null ? nativeMapView.addPolyline(polyline) : 0;
polyline.setMapboxMap(mapboxMap);
polyline.setId(id);
annotations.put(id, polyline);
}
return polyline;
}
@Override
public List<Polyline> addBy(@NonNull List<PolylineOptions> polylineOptionsList, @NonNull MapboxMap mapboxMap) {
int count = polylineOptionsList.size();
Polyline polyline;
List<Polyline> polylines = new ArrayList<>(count);
if (count > 0) {
for (PolylineOptions options : polylineOptionsList) {
polyline = options.getPolyline();
if (!polyline.getPoints().isEmpty()) {
polylines.add(polyline);
}
}
long[] ids = null;
if (nativeMapView != null) {
ids = nativeMapView.addPolylines(polylines);
}
long id = 0;
Polyline p;
for (int i = 0; i < polylines.size(); i++) {
p = polylines.get(i);
p.setMapboxMap(mapboxMap);
if (ids != null) {
id = ids[i];
} else {
// unit test
id++;
}
p.setId(id);
annotations.put(id, p);
}
}
return polylines;
}
@Override
public void update(Polyline polyline) {
if (!isAddedToMap(polyline)) {
Timber.w("Attempting to update non-added Polyline with value %s", polyline);
}
nativeMapView.updatePolyline(polyline);
annotations.setValueAt(annotations.indexOfKey(polyline.getId()), polyline);
}
@Override
public List<Polyline> obtainAll() {
List<Polyline> polylines = new ArrayList<>();
Annotation annotation;
for (int i = 0; i < annotations.size(); i++) {
annotation = annotations.get(annotations.keyAt(i));
if (annotation instanceof Polyline) {
polylines.add((Polyline) annotation);
}
}
return polylines;
}
private boolean isAddedToMap(Annotation annotation) {
return annotation != null && annotation.getId() != -1 && annotations.indexOfKey(annotation.getId()) != -1;
}
}

View File

@@ -0,0 +1,22 @@
package com.mapbox.mapboxsdk.maps;
import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.annotations.PolylineOptions;
import java.util.List;
/**
* Interface that defines convenient methods for working with a {@link Polyline}'s collection.
*/
interface Polylines {
Polyline addBy(@NonNull PolylineOptions polylineOptions, @NonNull MapboxMap mapboxMap);
List<Polyline> addBy(@NonNull List<PolylineOptions> polylineOptionsList, @NonNull MapboxMap mapboxMap);
void update(Polyline polyline);
List<Polyline> obtainAll();
}

View File

@@ -1,12 +1,14 @@
package com.mapbox.mapboxsdk.net;
import com.mapbox.mapboxsdk.LibraryLoader;
/**
* Updates the native library's connectivity state
*/
class NativeConnectivityListener implements ConnectivityListener {
static {
System.loadLibrary("mapbox-gl");
LibraryLoader.load();
}
private long nativePtr;

View File

@@ -5,6 +5,7 @@ import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
@@ -25,7 +26,7 @@ public class OfflineManager {
//
static {
System.loadLibrary("mapbox-gl");
LibraryLoader.load();
}
// Native peer pointer

View File

@@ -5,6 +5,7 @@ import android.os.Looper;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.storage.FileSource;
import java.lang.annotation.Retention;
@@ -22,7 +23,7 @@ public class OfflineRegion {
//
static {
System.loadLibrary("mapbox-gl");
LibraryLoader.load();
}
// Members
@@ -205,7 +206,7 @@ public class OfflineRegion {
/**
* Constructor
*
* <p>
* For JNI use only, to create a new offline region, use
* {@link OfflineManager#createOfflineRegion} instead.
*/

View File

@@ -0,0 +1,92 @@
package com.mapbox.mapboxsdk;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import org.junit.Before;
import org.junit.Test;
import java.lang.reflect.Field;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertSame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MapboxTest {
private Context context;
private Context appContext;
@Before
public void before() {
context = mock(Context.class);
appContext = mock(Context.class);
when(context.getApplicationContext()).thenReturn(appContext);
}
@Test
public void testGetAccessToken() {
final String accessToken = "pk.0000000001";
injectMapboxSingleton(accessToken);
assertSame(accessToken, Mapbox.getAccessToken());
}
@Test(expected = MapboxConfigurationException.class)
public void testGetInvalidAccessToken() {
final String accessToken = "dummy";
injectMapboxSingleton(accessToken);
assertSame(accessToken, Mapbox.getAccessToken());
}
@Test
public void testApplicationContext() {
injectMapboxSingleton("dummy");
assertNotNull(Mapbox.getApplicationContext());
assertNotEquals(context, appContext);
assertEquals(appContext, appContext);
}
@Test
public void testConnected() {
injectMapboxSingleton("dummy");
// test Android connectivity
ConnectivityManager connectivityManager = mock(ConnectivityManager.class);
NetworkInfo networkInfo = mock(NetworkInfo.class);
when(appContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);
when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
when(networkInfo.isConnected()).thenReturn(false);
assertFalse(Mapbox.isConnected());
when(networkInfo.isConnected()).thenReturn(true);
assertTrue(Mapbox.isConnected());
// test manual connectivity
Mapbox.setConnected(true);
assertTrue(Mapbox.isConnected());
Mapbox.setConnected(false);
assertFalse(Mapbox.isConnected());
// reset to Android connectivity
Mapbox.setConnected(null);
assertTrue(Mapbox.isConnected());
}
private void injectMapboxSingleton(String accessToken) {
Mapbox mapbox = new Mapbox(appContext, accessToken);
try {
Field field = Mapbox.class.getDeclaredField("INSTANCE");
field.setAccessible(true);
field.set(mapbox, mapbox);
} catch (Exception exception) {
throw new AssertionError();
}
}
}

View File

@@ -0,0 +1,91 @@
package com.mapbox.mapboxsdk.annotations;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
public class AnnotationTest {
@InjectMocks
private MapboxMap mapboxMap = mock(MapboxMap.class);
private Annotation annotation;
private Annotation compare = new Annotation() {
@Override
public long getId() {
return 1;
}
};
@Before
public void beforeTest() {
annotation = new Annotation() {
// empty child
};
}
@Test
public void testSanity() {
assertNotNull("markerOptions should not be null", annotation);
}
@Test
public void testRemove() {
annotation.setId(1);
annotation.setMapboxMap(mapboxMap);
annotation.remove();
verify(mapboxMap, times(1)).removeAnnotation(annotation);
}
@Test
public void testRemoveUnboundMapboxMap() {
annotation.setId(1);
annotation.remove();
verify(mapboxMap, times(0)).removeAnnotation(annotation);
}
@Test
public void testCompareToEqual() {
annotation.setId(1);
assertEquals("conparable equal", 0, annotation.compareTo(compare));
}
@Test
public void testCompareToHigher() {
annotation.setId(3);
assertEquals("conparable higher", -1, annotation.compareTo(compare));
}
@Test
public void testCompareTolower() {
annotation.setId(0);
assertEquals("conparable lower", 1, annotation.compareTo(compare));
}
@Test
public void testEquals() {
Annotation holder = null;
assertFalse(annotation.equals(holder));
holder = annotation;
assertTrue(annotation.equals(holder));
assertFalse(annotation.equals(new Object()));
}
@Test
public void testHashcode() {
int id = 1;
annotation.setId(id);
assertSame("hashcode should match", annotation.hashCode(), id);
}
}

View File

@@ -0,0 +1,55 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.Bitmap;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotSame;
public class IconTest {
@Mock
Bitmap bitmap;
@Before
public void beforeTest() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testId() {
String id = "test";
Icon icon = IconFactory.recreate(id, Bitmap.createBitmap(0, 0, Bitmap.Config.ALPHA_8));
assertEquals("id should match", id, icon.getId());
}
@Test
public void testBitmap() {
Icon icon = IconFactory.recreate("test", bitmap);
assertEquals("bitmap should match", bitmap, icon.getBitmap());
}
@Test
public void testEquals() {
Icon icon1 = IconFactory.recreate("test", bitmap);
Icon icon2 = IconFactory.recreate("test", bitmap);
assertEquals("icons should not match", icon1, icon2);
}
@Test
public void testEqualsObject() {
Icon icon = IconFactory.recreate("test", Bitmap.createBitmap(0, 0, Bitmap.Config.ALPHA_8));
assertNotSame("icon should not match", new Object(), icon);
}
@Test
public void testHashcode() {
Icon icon = IconFactory.recreate("test", bitmap);
long expectedHashcode = 31 * bitmap.hashCode() + "test".hashCode();
assertEquals("hashcode should match", expectedHashcode, icon.hashCode());
}
}

View File

@@ -0,0 +1,86 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.PointF;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Projection;
import org.junit.Test;
import org.mockito.InjectMocks;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class InfoWindowTest {
@InjectMocks
MapView mMapView = mock(MapView.class);
@InjectMocks
MapboxMap mMapboxMap = mock(MapboxMap.class);
@Test
public void testSanity() {
InfoWindow infoWindow = new InfoWindow(mMapView, mMapboxMap);
assertNotNull("infoWindow should exist", infoWindow);
}
@Test
public void testBoundMarker() {
MarkerOptions markerOptions = new MarkerOptions();
Marker marker = markerOptions.position(new LatLng()).getMarker();
InfoWindow infoWindow = new InfoWindow(mMapView, mMapboxMap).setBoundMarker(marker);
assertEquals("marker should match", marker, infoWindow.getBoundMarker());
}
@Test
public void testClose() {
InfoWindow infoWindow = new InfoWindow(mMapView, mMapboxMap);
infoWindow.close();
assertEquals("infowindow should not be visible", false, infoWindow.isVisible());
}
@Test
public void testOpen() {
LatLng latLng = new LatLng(0, 0);
Projection projection = mock(Projection.class);
when(mMapboxMap.getProjection()).thenReturn(projection);
when(projection.toScreenLocation(latLng)).thenReturn(new PointF(0, 0));
InfoWindow infoWindow = new InfoWindow(mMapView, mMapboxMap);
infoWindow.open(mMapView, new MarkerOptions().position(new LatLng()).getMarker(), latLng, 0, 0);
assertEquals("infowindow should not be visible", true, infoWindow.isVisible());
}
@Test
public void testOpenClose() {
LatLng latLng = new LatLng(0, 0);
Projection projection = mock(Projection.class);
when(mMapboxMap.getProjection()).thenReturn(projection);
when(projection.toScreenLocation(latLng)).thenReturn(new PointF(0, 0));
InfoWindow infoWindow = new InfoWindow(mMapView, mMapboxMap);
infoWindow.open(mMapView, new MarkerOptions().position(new LatLng()).getMarker(), latLng, 0, 0);
infoWindow.close();
assertEquals("infowindow should not be visible", false, infoWindow.isVisible());
}
@Test
public void testUpdate() {
LatLng latLng = new LatLng(0, 0);
Projection projection = mock(Projection.class);
when(mMapboxMap.getProjection()).thenReturn(projection);
when(projection.toScreenLocation(latLng)).thenReturn(new PointF(0, 0));
InfoWindow infoWindow = new InfoWindow(mMapView, mMapboxMap);
infoWindow.open(mMapView, new MarkerOptions().position(latLng).getMarker(), latLng, 0, 0);
infoWindow.update();
}
}

View File

@@ -0,0 +1,160 @@
package com.mapbox.mapboxsdk.annotations;
import android.graphics.Bitmap;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.exceptions.InvalidMarkerPositionException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
public class MarkerTest {
@Test
public void testSanity() {
MarkerOptions markerOptions = new MarkerOptions();
assertNotNull("markerOptions should not be null", markerOptions);
}
@Test
public void testMarker() {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng());
assertNotNull("marker should not be null", markerOptions.getMarker());
}
@Test(expected = InvalidMarkerPositionException.class)
public void testInvalidMarker() {
new MarkerOptions().getMarker();
}
@Test
public void testPosition() {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(10, 12));
Marker marker = markerOptions.getMarker();
assertEquals(marker.getPosition(), new LatLng(10, 12));
assertEquals(markerOptions.getPosition(), new LatLng(10, 12));
}
@Test
public void testTitle() {
MarkerOptions markerOptions = new MarkerOptions().title("Mapbox").position(new LatLng());
Marker marker = markerOptions.getMarker();
assertEquals(marker.getTitle(), "Mapbox");
assertEquals(markerOptions.getTitle(), "Mapbox");
}
@Test
public void testSnippet() {
MarkerOptions markerOptions = new MarkerOptions().snippet("Mapbox").position(new LatLng());
Marker marker = markerOptions.getMarker();
assertEquals(marker.getSnippet(), "Mapbox");
}
@Test
public void testBuilder() {
Marker marker = new MarkerOptions().title("title").snippet("snippet").position(new LatLng(10, 12)).getMarker();
assertEquals(marker.getSnippet(), "snippet");
assertEquals(marker.getPosition(), new LatLng(10, 12));
}
@Test
public void testIcon() {
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_4444);
Icon icon = IconFactory.recreate("test", bitmap);
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng()).icon(icon);
Marker marker = markerOptions.getMarker();
assertEquals("Icon should match", icon, marker.getIcon());
assertEquals("Icon should match", icon, markerOptions.getIcon());
}
@Test
public void testHashCode() {
Marker marker = new MarkerOptions().position(new LatLng()).getMarker();
assertEquals("hash code should match", marker.hashCode(), 0);
}
@Test
public void testHashCodeBuilder() {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(10, 12));
assertEquals("hash code should match", markerOptions.hashCode(), 579999617);
}
@Test
public void testEquals() {
Marker markerOne = new MarkerOptions().position(new LatLng(0, 0)).getMarker();
Marker markerTwo = new MarkerOptions().position(new LatLng(0, 0)).getMarker();
assertEquals(markerOne, markerTwo);
}
@Test
public void testEqualityDifferentLocation() {
MarkerOptions marker = new MarkerOptions().position(new LatLng(0, 0));
MarkerOptions other = new MarkerOptions().position(new LatLng(1, 0));
assertNotEquals("Should not match", other, marker);
}
@Test
public void testEqualityDifferentSnippet() {
MarkerOptions marker = new MarkerOptions().snippet("s");
MarkerOptions other = new MarkerOptions();
assertNotEquals("Should not match", other, marker);
}
@Test
public void testEqualityDifferentIcon() {
MarkerOptions marker = new MarkerOptions().icon(mock(Icon.class));
MarkerOptions other = new MarkerOptions();
assertNotEquals("Should not match", other, marker);
}
@Test
public void testEqualityDifferentTitle() {
MarkerOptions marker = new MarkerOptions().title("t");
MarkerOptions other = new MarkerOptions();
assertNotEquals("Should not match", other, marker);
}
@Test
public void testEqualsItself() {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(0, 0));
Marker marker = markerOptions.getMarker();
assertEquals("Marker should match", marker, marker);
assertEquals("MarkerOptions should match", markerOptions, markerOptions);
}
@Test
public void testNotEquals() {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(0, 0));
Marker marker = markerOptions.getMarker();
assertNotEquals("MarkerOptions should match", markerOptions, new Object());
assertNotEquals("Marker should match", marker, new Object());
}
@Test
public void testEqualityBuilder() {
MarkerOptions markerOne = new MarkerOptions().position(new LatLng(0, 0));
MarkerOptions markerTwo = new MarkerOptions().position(new LatLng(0, 0));
assertEquals(markerOne, markerTwo);
}
@Test
public void testToString() {
Marker marker = new MarkerOptions().position(new LatLng(0, 0)).getMarker();
assertEquals(marker.toString(), "Marker [position[" + "LatLng [latitude=0.0, longitude=0.0, altitude=0.0]" + "]]");
}
@Test
public void testParcelable() {
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng()).title("t").snippet("s");
Parcelable parcelable = MockParcel.obtain(markerOptions);
assertEquals("Parcel should match original object", parcelable, markerOptions);
}
}

View File

@@ -0,0 +1,219 @@
package com.mapbox.mapboxsdk.annotations;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.exceptions.InvalidMarkerPositionException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class MarkerViewTest {
@Mock
MapboxMap mapboxMap;
@Mock
MarkerViewManager markerViewManager;
@Before
public void beforeTest() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSanity() {
MarkerViewOptions markerOptions = new MarkerViewOptions();
assertNotNull("markerOptions should not be null", markerOptions);
}
@Test
public void testMarker() {
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng());
assertNotNull("marker should not be null", markerOptions.getMarker());
}
@Test(expected = InvalidMarkerPositionException.class)
public void testInvalidMarker() {
new MarkerViewOptions().getMarker();
}
@Test
public void testPosition() {
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(10, 12));
MarkerView marker = markerOptions.getMarker();
assertEquals(marker.getPosition(), new LatLng(10, 12));
assertEquals(markerOptions.getPosition(), new LatLng(10, 12));
}
@Test
public void testSnippet() {
MarkerViewOptions markerOptions = new MarkerViewOptions().snippet("Mapbox").position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertEquals(marker.getSnippet(), "Mapbox");
}
@Test
public void testTitle() {
MarkerViewOptions markerOptions = new MarkerViewOptions().title("Mapbox").position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertEquals(marker.getTitle(), "Mapbox");
assertEquals(markerOptions.getTitle(), "Mapbox");
}
@Test
public void testFlat() {
MarkerViewOptions markerOptions = new MarkerViewOptions().flat(true).position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertTrue("flat should be true", marker.isFlat());
}
@Test
public void testFlatDefault() {
assertFalse("default value of flat should be false", new MarkerViewOptions().position(
new LatLng()).getMarker().isFlat());
}
@Test
public void testAnchor() {
float anchorU = 1;
float anchorV = 1;
MarkerViewOptions markerOptions = new MarkerViewOptions().anchor(anchorU, anchorV).position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertEquals("anchorU should match ", anchorU, marker.getAnchorU(), 0);
assertEquals("anchorU should match ", anchorV, marker.getAnchorV(), 0);
}
@Test
public void testAnchorDefault() {
MarkerView marker = new MarkerViewOptions().position(new LatLng()).getMarker();
assertEquals("anchorU should match ", 0.5, marker.getAnchorU(), 0);
assertEquals("anchorU should match ", 1, marker.getAnchorV(), 0);
}
@Test
public void testInfoWindowAnchor() {
float anchorU = 1;
float anchorV = 1;
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).infoWindowAnchor(anchorU, anchorV);
MarkerView marker = markerOptions.getMarker();
assertEquals("anchorU should match ", 1, marker.getInfoWindowAnchorU(), 0);
assertEquals("anchorU should match ", 1, marker.getInfoWindowAnchorV(), 0);
}
@Test
public void testInfoWindowAnchorDefault() {
MarkerView marker = new MarkerViewOptions().position(new LatLng()).getMarker();
assertEquals("anchorU should match ", 0.5, marker.getInfoWindowAnchorU(), 0);
assertEquals("anchorU should match ", 0, marker.getInfoWindowAnchorV(), 0);
}
@Test
public void testRotation() {
int rotation = 90;
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).rotation(rotation);
MarkerView marker = markerOptions.getMarker();
assertEquals("rotation should match ", rotation, marker.getRotation(), 0);
}
@Test
public void testRotationAboveMax() {
MarkerViewOptions markerOptions = new MarkerViewOptions().rotation(390).position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertEquals(marker.getRotation(), 30, 0);
}
@Test
public void testRotationBelowMin() {
MarkerViewOptions markerOptions = new MarkerViewOptions().rotation(-10).position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertEquals(marker.getRotation(), 350, 0);
}
@Test
public void testVisible() {
boolean visible = false;
MarkerViewOptions markerOptions = new MarkerViewOptions().visible(visible).position(new LatLng());
MarkerView marker = markerOptions.getMarker();
assertEquals("visible should match ", visible, marker.isVisible());
}
@Test
public void testVisibleDefault() {
assertTrue(new MarkerViewOptions().position(new LatLng()).getMarker().isVisible());
}
@Test
public void testBuilder() {
MarkerView marker = new MarkerViewOptions().title("title").snippet("snippet").position(
new LatLng(10, 12)).getMarker();
assertEquals(marker.getSnippet(), "snippet");
assertEquals(marker.getPosition(), new LatLng(10, 12));
}
@Test
public void testHashCode() {
MarkerView marker = new MarkerViewOptions().position(new LatLng()).getMarker();
assertEquals("hash code should match", marker.hashCode(), 0);
}
@Test
public void testHashCodeBuilder() {
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(10, 12));
assertEquals("hash code should match", markerOptions.hashCode(), 0);
}
@Test
public void testEquals() {
MarkerView markerOne = new MarkerViewOptions().position(new LatLng(0, 0)).getMarker();
MarkerView markerTwo = new MarkerViewOptions().position(new LatLng(0, 0)).getMarker();
assertEquals(markerOne, markerTwo);
}
@Test
public void testEqualsItself() {
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(0, 0));
MarkerView marker = markerOptions.getMarker();
assertEquals("MarkerView should match", marker, marker);
assertEquals("MarkerViewOptions should match", markerOptions, markerOptions);
}
@Test
public void testNotEquals() {
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(0, 0));
MarkerView marker = markerOptions.getMarker();
assertNotEquals("MarkerViewOptions should match", markerOptions, new Object());
assertNotEquals("MarkerView should match", marker, new Object());
}
@Test
public void testEqualityBuilder() {
MarkerViewOptions markerOne = new MarkerViewOptions().position(new LatLng(0, 0));
MarkerViewOptions markerTwo = new MarkerViewOptions().position(new LatLng(0, 0));
assertEquals(markerOne, markerTwo);
}
@Test
public void testToString() {
MarkerView marker = new MarkerViewOptions().position(new LatLng(0, 0)).getMarker();
assertEquals(marker.toString(), "MarkerView [position["
+ "LatLng [latitude=0.0, longitude=0.0, altitude=0.0]" + "]]");
}
@Test
public void testParcelable() {
MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).title("t").snippet("s");
Parcelable parcelable = MockParcel.obtain(markerOptions);
assertEquals("Parcel should match original object", parcelable, markerOptions);
}
}

View File

@@ -0,0 +1,75 @@
package com.mapbox.mapboxsdk.annotations;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class PolygonTest {
@Test
public void testSanity() {
PolygonOptions polygonOptions = new PolygonOptions();
assertNotNull("polygonOptions should not be null", polygonOptions);
}
@Test
public void testPolygon() {
Polygon polygon = new PolygonOptions().getPolygon();
assertNotNull("polyline should not be null", polygon);
}
@Test
public void testAlpha() {
Polygon polygon = new PolygonOptions().alpha(0.5f).getPolygon();
assertEquals(0.5f, polygon.getAlpha(), 0.0f);
}
@Test
public void testStrokeColor() {
Polygon polygon = new PolygonOptions().strokeColor(1).getPolygon();
assertEquals(1, polygon.getStrokeColor());
}
@Test
public void testFillColor() {
Polygon polygon = new PolygonOptions().fillColor(1).getPolygon();
assertEquals(1, polygon.getFillColor());
}
@Test
public void testLatLng() {
Polygon polygon = new PolygonOptions().add(new LatLng(0, 0)).getPolygon();
assertNotNull("points should not be null", polygon.getPoints());
assertEquals(new LatLng(0, 0), polygon.getPoints().get(0));
}
@Test
public void testAddAllLatLng() {
List<LatLng> coordinates = new ArrayList<>();
coordinates.add(new LatLng(0, 0));
Polygon polygon = new PolygonOptions().addAll(coordinates).getPolygon();
assertNotNull(polygon.getPoints());
assertEquals(new LatLng(0, 0), polygon.getPoints().get(0));
}
@Test
public void testBuilder() {
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.width(1.0f);
polylineOptions.color(2);
polylineOptions.add(new LatLng(0, 0));
Polyline polyline = polylineOptions.getPolyline();
assertEquals(1.0f, polyline.getWidth(), 0);
assertEquals(2, polyline.getColor());
assertNotNull("Points should not be null", polyline.getPoints());
assertEquals(new LatLng(0, 0), polyline.getPoints().get(0));
}
}

View File

@@ -0,0 +1,75 @@
package com.mapbox.mapboxsdk.annotations;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class PolylineTest {
@Test
public void testSanity() {
PolylineOptions polylineOptions = new PolylineOptions();
assertNotNull("polylineOptions should not be null", polylineOptions);
}
@Test
public void testPolyline() {
Polyline polyline = new PolylineOptions().getPolyline();
assertNotNull("polyline should not be null", polyline);
}
@Test
public void testAlpha() {
Polyline polyline = new PolylineOptions().alpha(0.2f).getPolyline();
assertEquals(0.2f, polyline.getAlpha(), 0.0f);
}
@Test
public void testWidth() {
Polyline polyline = new PolylineOptions().width(1).getPolyline();
assertEquals(1.0f, polyline.getWidth(), 0);
}
@Test
public void testColor() {
Polyline polyline = new PolylineOptions().color(1).getPolyline();
assertEquals(1, polyline.getColor());
}
@Test
public void testAddLatLng() {
Polyline polyline = new PolylineOptions().add(new LatLng(0, 0)).getPolyline();
assertNotNull("Points should not be null", polyline.getPoints());
assertEquals(new LatLng(0, 0), polyline.getPoints().get(0));
}
@Test
public void testAddAllLatLng() {
List<LatLng> coordinates = new ArrayList<>();
coordinates.add(new LatLng(0, 0));
Polyline polyline = new PolylineOptions().addAll(coordinates).getPolyline();
assertNotNull(polyline.getPoints());
assertEquals(new LatLng(0, 0), polyline.getPoints().get(0));
}
@Test
public void testBuilder() {
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.width(1.0f);
polylineOptions.color(2);
polylineOptions.add(new LatLng(0, 0));
Polyline polyline = polylineOptions.getPolyline();
assertEquals(1.0f, polyline.getWidth(), 0);
assertEquals(2, polyline.getColor());
assertNotNull("Points should not be null", polyline.getPoints());
assertEquals(new LatLng(0, 0), polyline.getPoints().get(0));
}
}

View File

@@ -0,0 +1,110 @@
package com.mapbox.mapboxsdk.camera;
import android.content.res.TypedArray;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CameraPositionTest {
private static final double DELTA = 1e-15;
@Test
public void testSanity() {
LatLng latLng = new LatLng(1, 2);
CameraPosition cameraPosition = new CameraPosition(latLng, 3, 4, 5);
assertNotNull("cameraPosition should not be null", cameraPosition);
}
@Test
public void testDefaultTypedArrayBuilder() {
TypedArray typedArray = null;
CameraPosition cameraPosition = new CameraPosition.Builder(typedArray).build();
assertEquals("bearing should match", -1, cameraPosition.bearing, DELTA);
assertEquals("latlng should match", null, cameraPosition.target);
assertEquals("tilt should match", -1, cameraPosition.tilt, DELTA);
assertEquals("zoom should match", -1, cameraPosition.zoom, DELTA);
}
@Test
public void testTypedArrayBuilder() {
float bearing = 180;
float zoom = 12;
float latitude = 10;
float longitude = 11;
float tilt = 44;
TypedArray typedArray = mock(TypedArray.class);
when(typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraBearing, 0.0f)).thenReturn(bearing);
when(typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLat, 0.0f)).thenReturn(latitude);
when(typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTargetLng, 0.0f)).thenReturn(longitude);
when(typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraZoom, 0.0f)).thenReturn(zoom);
when(typedArray.getFloat(R.styleable.mapbox_MapView_mapbox_cameraTilt, 0.0f)).thenReturn(tilt);
doNothing().when(typedArray).recycle();
CameraPosition cameraPosition = new CameraPosition.Builder(typedArray).build();
assertEquals("bearing should match", bearing, cameraPosition.bearing, DELTA);
assertEquals("latlng should match", new LatLng(latitude, longitude), cameraPosition.target);
assertEquals("tilt should match", tilt, cameraPosition.tilt, DELTA);
assertEquals("zoom should match", zoom, cameraPosition.zoom, DELTA);
}
@Test
public void testToString() {
LatLng latLng = new LatLng(1, 2);
CameraPosition cameraPosition = new CameraPosition(latLng, 3, 4, 5);
assertEquals("toString should match", "Target: LatLng [latitude=1.0, longitude=2.0, altitude=0.0], Zoom:3.0, "
+ "Bearing:5.0, Tilt:4.0", cameraPosition.toString());
}
@Test
public void testHashcode() {
LatLng latLng = new LatLng(1, 2);
CameraPosition cameraPosition = new CameraPosition(latLng, 3, 4, 5);
assertEquals("hashCode should match", -1007681505, cameraPosition.hashCode());
}
@Test
public void testZoomUpdateBuilder() {
float zoomLevel = 5;
CameraPosition.Builder builder = new CameraPosition.Builder(
(CameraUpdateFactory.ZoomUpdate) CameraUpdateFactory.zoomTo(zoomLevel));
assertEquals("zoom should match", zoomLevel, builder.build().zoom, 0);
}
@Test
public void testEquals() {
LatLng latLng = new LatLng(1, 2);
CameraPosition cameraPosition = new CameraPosition(latLng, 3, 4, 5);
CameraPosition cameraPositionBearing = new CameraPosition(latLng, 3, 4, 9);
CameraPosition cameraPositionTilt = new CameraPosition(latLng, 3, 9, 5);
CameraPosition cameraPositionZoom = new CameraPosition(latLng, 9, 4, 5);
CameraPosition cameraPositionTarget = new CameraPosition(new LatLng(), 3, 4, 5);
assertEquals("cameraPosition should match itself", cameraPosition, cameraPosition);
assertNotEquals("cameraPosition should not match null", null, cameraPosition);
assertNotEquals("cameraPosition should not match object", new Object(), cameraPosition);
assertNotEquals("cameraPosition should not match for bearing", cameraPositionBearing, cameraPosition);
assertNotEquals("cameraPosition should not match for tilt", cameraPositionTilt, cameraPosition);
assertNotEquals("cameraPosition should not match for zoom", cameraPositionZoom, cameraPosition);
assertNotEquals("cameraPosition should not match for target", cameraPositionTarget, cameraPosition);
}
@Test
public void testParcelable() {
CameraPosition object = new CameraPosition(new LatLng(1, 2), 3, 4, 5);
Parcelable parcelable = MockParcel.obtain(object);
assertEquals("Parcel should match original object", parcelable, object);
}
}

View File

@@ -0,0 +1,6 @@
package com.mapbox.mapboxsdk.constants;
public class AppConstant {
public static final int STYLE_VERSION = 9;
}

View File

@@ -0,0 +1,284 @@
package com.mapbox.mapboxsdk.geometry;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
public class LatLngBoundsTest {
private static final double DELTA = 1e-15;
private LatLngBounds latLngBounds;
private static final LatLng LAT_LNG_NULL_ISLAND = new LatLng(0, 0);
private static final LatLng LAT_LNG_NOT_NULL_ISLAND = new LatLng(2, 2);
@Before
public void beforeTest() {
latLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
}
@Test
public void testSanity() {
LatLngBounds.Builder latLngBoundsBuilder = new LatLngBounds.Builder();
latLngBoundsBuilder.include(LAT_LNG_NULL_ISLAND).include(LAT_LNG_NOT_NULL_ISLAND);
assertNotNull("latLng should not be null", latLngBoundsBuilder.build());
}
@Test(expected = InvalidLatLngBoundsException.class)
public void noLatLngs() {
new LatLngBounds.Builder().build();
}
@Test(expected = InvalidLatLngBoundsException.class)
public void oneLatLngs() {
new LatLngBounds.Builder().include(LAT_LNG_NULL_ISLAND).build();
}
@Test
public void latitiudeSpan() {
assertEquals("Span should be the same", 2, latLngBounds.getLatitudeSpan(), DELTA);
}
@Test
public void longitudeSpan() {
assertEquals("Span should be the same", 2, latLngBounds.getLongitudeSpan(), DELTA);
}
@Test
public void coordinateSpan() {
LatLngSpan latLngSpan = latLngBounds.getSpan();
assertEquals("LatLngSpan should be the same", new LatLngSpan(2, 2), latLngSpan);
}
@Test
public void center() {
LatLng center = latLngBounds.getCenter();
assertEquals("Center should match", new LatLng(1, 1), center);
}
@Test
public void emptySpan() {
latLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NOT_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
assertTrue("Should be empty", latLngBounds.isEmptySpan());
}
@Test
public void notEmptySpan() {
latLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NOT_NULL_ISLAND)
.include(LAT_LNG_NULL_ISLAND)
.build();
assertFalse("Should not be empty", latLngBounds.isEmptySpan());
}
@Test
public void toLatLngs() {
latLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NOT_NULL_ISLAND)
.include(LAT_LNG_NULL_ISLAND)
.build();
assertArrayEquals("LatLngs should match",
new LatLng[] {LAT_LNG_NOT_NULL_ISLAND, LAT_LNG_NULL_ISLAND},
latLngBounds.toLatLngs());
}
@Test
public void include() {
assertTrue("LatLng should be included", latLngBounds.contains(new LatLng(1, 1)));
}
@Test
public void includes() {
List<LatLng> points = new ArrayList<>();
points.add(LAT_LNG_NULL_ISLAND);
points.add(LAT_LNG_NOT_NULL_ISLAND);
LatLngBounds latLngBounds1 = new LatLngBounds.Builder()
.includes(points)
.build();
LatLngBounds latLngBounds2 = new LatLngBounds.Builder()
.include(LAT_LNG_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
assertEquals("LatLngBounds should match", latLngBounds1, latLngBounds2);
}
@Test
public void containsNot() {
assertFalse("LatLng should not be included", latLngBounds.contains(new LatLng(3, 1)));
}
@Test
public void containsBoundsInWorld() {
assertTrue("LatLngBounds should be contained in the world", LatLngBounds.world().contains(latLngBounds));
}
@Test
public void containsBounds() {
LatLngBounds inner = new LatLngBounds.Builder()
.include(new LatLng(-5, -5))
.include(new LatLng(5, 5))
.build();
LatLngBounds outer = new LatLngBounds.Builder()
.include(new LatLng(-10, -10))
.include(new LatLng(10, 10))
.build();
assertTrue(outer.contains(inner));
assertFalse(inner.contains(outer));
}
@Test
public void testHashCode() {
assertEquals(2147483647, latLngBounds.hashCode(), -1946419200);
}
@Test
public void equality() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(LAT_LNG_NULL_ISLAND)
.include(LAT_LNG_NOT_NULL_ISLAND)
.build();
assertEquals("equality should match", this.latLngBounds, latLngBounds);
assertEquals("not equal to a different object type", this.latLngBounds.equals(LAT_LNG_NOT_NULL_ISLAND), false);
}
@Test
public void testToString() {
assertEquals(latLngBounds.toString(), "N:2.0; E:2.0; S:0.0; W:0.0");
}
@Test
public void intersect() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(1, 1))
.include(LAT_LNG_NULL_ISLAND)
.build();
assertEquals("intersect should match", latLngBounds, latLngBounds.intersect(this.latLngBounds.getLatNorth(),
this.latLngBounds.getLonEast(), this.latLngBounds.getLatSouth(), this.latLngBounds.getLonWest()));
}
@Test
public void intersectNot() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(new LatLng(9, 8))
.build();
assertNull(latLngBounds.intersect(this.latLngBounds));
}
@Test
public void innerUnion() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(1, 1))
.include(LAT_LNG_NULL_ISLAND)
.build();
assertEquals("union should match", latLngBounds, latLngBounds.intersect(this.latLngBounds));
}
@Test
public void outerUnion() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(new LatLng(9, 8))
.build();
assertEquals("outer union should match",
latLngBounds.union(this.latLngBounds),
new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(LAT_LNG_NULL_ISLAND)
.build());
}
@Test
public void northWest() {
double minLat = 5;
double minLon = 6;
double maxLat = 20;
double maxLon = 21;
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(minLat, minLon))
.include(new LatLng(maxLat, maxLon))
.build();
assertEquals("NorthWest should match", latLngBounds.getNorthWest(), new LatLng(maxLat, minLon));
}
@Test
public void southWest() {
double minLat = 5;
double minLon = 6;
double maxLat = 20;
double maxLon = 21;
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(minLat, minLon))
.include(new LatLng(maxLat, maxLon))
.build();
assertEquals("SouthWest should match", latLngBounds.getSouthWest(), new LatLng(minLat, minLon));
}
@Test
public void northEast() {
double minLat = 5;
double minLon = 6;
double maxLat = 20;
double maxLon = 21;
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(minLat, minLon))
.include(new LatLng(maxLat, maxLon))
.build();
assertEquals("NorthEast should match", latLngBounds.getNorthEast(), new LatLng(maxLat, maxLon));
}
@Test
public void southEast() {
double minLat = 5;
double minLon = 6;
double maxLat = 20;
double maxLon = 21;
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(minLat, minLon))
.include(new LatLng(maxLat, maxLon))
.build();
assertEquals("SouthEast should match", latLngBounds.getSouthEast(), new LatLng(minLat, maxLon));
}
@Test
public void testParcelable() {
LatLngBounds latLngBounds = new LatLngBounds.Builder()
.include(new LatLng(10, 10))
.include(new LatLng(9, 8))
.build();
Parcelable parcel = MockParcel.obtain(latLngBounds);
assertEquals("Parcel should match original object", parcel, latLngBounds);
}
}

View File

@@ -0,0 +1,65 @@
package com.mapbox.mapboxsdk.geometry;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class LatLngSpanTest {
private static final double DELTA = 1e-15;
private static final LatLng LAT_LNG_NULL_ISLAND = new LatLng(0, 0);
@Test
public void testSanity() {
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
assertNotNull("latLngSpan should not be null", latLngSpan);
}
@Test
public void testEquality() {
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
assertEquals("latLngSpan is not equal to a LatLng", latLngSpan.equals(LAT_LNG_NULL_ISLAND), false);
}
@Test
public void testLatitudeConstructor() {
double latitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(latitude, 0.0);
assertEquals("latitude in constructor", latLngSpan.getLatitudeSpan(), latitude, DELTA);
}
@Test
public void testLongitudeConstructor() {
double longitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(0.0, longitude);
assertEquals("latitude in constructor", latLngSpan.getLongitudeSpan(), longitude, DELTA);
}
@Test
public void testLatitudeMethod() {
double latitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
latLngSpan.setLatitudeSpan(latitude);
assertEquals("latitude in constructor", latLngSpan.getLatitudeSpan(), latitude, DELTA);
}
@Test
public void testLongitudeMethod() {
double longitude = 1.23;
LatLngSpan latLngSpan = new LatLngSpan(0.0, 0.0);
latLngSpan.setLongitudeSpan(longitude);
assertEquals("latitude in constructor", latLngSpan.getLongitudeSpan(), longitude, DELTA);
}
@Test
public void testParcelable() {
LatLngSpan object = new LatLngSpan(1, 2);
Parcelable parcel = MockParcel.obtain(object);
assertEquals("parcel should match initial object", object, parcel);
}
}

View File

@@ -0,0 +1,267 @@
package com.mapbox.mapboxsdk.geometry;
import android.location.Location;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class LatLngTest {
private static final double DELTA = 1e-15;
@Test
public void testSanity() {
LatLng latLng = new LatLng(0.0, 0.0);
assertNotNull("latLng should not be null", latLng);
}
@Test
public void testLatitudeEmptyConstructor() {
LatLng latLng = new LatLng();
assertEquals("latitude default value", latLng.getLatitude(), 0, DELTA);
}
@Test
public void testLongitudeEmptyConstructor() {
LatLng latLng = new LatLng();
assertEquals("longitude default value", latLng.getLongitude(), 0, DELTA);
}
@Test
public void testAltitudeEmptyConstructor() {
LatLng latLng1 = new LatLng();
assertEquals("altitude default value", latLng1.getAltitude(), 0.0, DELTA);
}
@Test
public void testLatitudeConstructor() {
double latitude = 1.2;
LatLng latLng = new LatLng(latitude, 3.4);
assertEquals("latitude should match", latLng.getLatitude(), latitude, DELTA);
}
@Test
public void testLongitudeConstructor() {
double longitude = 3.4;
LatLng latLng = new LatLng(1.2, longitude);
assertEquals("longitude should match", latLng.getLongitude(), longitude, DELTA);
}
@Test
public void testAltitudeConstructor() {
LatLng latLng1 = new LatLng(1.2, 3.4);
assertEquals("altitude default value", latLng1.getAltitude(), 0.0, DELTA);
double altitude = 5.6;
LatLng latLng2 = new LatLng(1.2, 3.4, altitude);
assertEquals("altitude default value", latLng2.getAltitude(), altitude, DELTA);
}
@Test
public void testLatitudeSetter() {
LatLng latLng = new LatLng(1.2, 3.4);
latLng.setLatitude(3);
assertEquals("latitude should match", 3, latLng.getLatitude(), DELTA);
}
@Test
public void testLongitudeSetter() {
LatLng latLng = new LatLng(1.2, 3.4);
latLng.setLongitude(3);
assertEquals("longitude should match", 3, latLng.getLongitude(), DELTA);
}
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void testConstructorChecksLatitudeNaN() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latitude must not be NaN");
new LatLng(Double.NaN, 0);
}
@Test
public void testConstructorChecksLongitudeNaN() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("longitude must not be NaN");
new LatLng(0, Double.NaN);
}
@Test
public void testConstructorChecksLatitudeGreaterThan90() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latitude must be between -90 and 90");
new LatLng(95, 0);
}
@Test
public void testConstructorChecksLatitudeLessThanThanNegative90() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latitude must be between -90 and 90");
new LatLng(-95, 0);
}
@Test
public void testConstructorChecksLongitudeInfinity() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("longitude must not be infinite");
new LatLng(0, Double.POSITIVE_INFINITY);
}
@Test
public void testLatitudeSetterChecksNaN() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latitude must not be NaN");
new LatLng().setLatitude(Double.NaN);
}
@Test
public void testLongitudeSetterChecksNaN() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("longitude must not be NaN");
new LatLng().setLongitude(Double.NaN);
}
@Test
public void testLatitudeSetterChecksGreaterThan90() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latitude must be between -90 and 90");
new LatLng().setLatitude(95);
}
@Test
public void testLatitudeSetterChecksLessThanThanNegative90() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("latitude must be between -90 and 90");
new LatLng().setLatitude(-95);
}
@Test
public void testLongitudeSetterChecksInfinity() {
exception.expect(IllegalArgumentException.class);
exception.expectMessage("longitude must not be infinite");
new LatLng().setLongitude(Double.NEGATIVE_INFINITY);
}
@Test
public void testAltitudeSetter() {
LatLng latLng = new LatLng(1.2, 3.4);
latLng.setAltitude(3);
assertEquals("altitude should match", 3, latLng.getAltitude(), DELTA);
}
@Test
public void testLatLngConstructor() {
LatLng latLng1 = new LatLng(1.2, 3.4);
LatLng latLng2 = new LatLng(latLng1);
assertEquals("latLng should match", latLng1, latLng2);
}
@Test
public void testDistanceTo() {
LatLng latLng1 = new LatLng(0.0, 0.0);
LatLng latLng2 = new LatLng(1.0, 1.0);
assertEquals("distances should match",
latLng1.distanceTo(latLng2),
157425.53710839353, DELTA);
}
@Test
public void testDistanceToSamePoint() {
LatLng latLng1 = new LatLng(40.71199035644531, -74.0081);
LatLng latLng2 = new LatLng(40.71199035644531, -74.0081);
double distance = latLng1.distanceTo(latLng2);
assertEquals("distance should match", 0.0, distance, DELTA);
}
@Test
public void testLocationProvider() {
double latitude = 1.2;
double longitude = 3.4;
double altitude = 5.6;
// Mock the location class
Location locationMocked = mock(Location.class);
when(locationMocked.getLatitude()).thenReturn(latitude);
when(locationMocked.getLongitude()).thenReturn(longitude);
when(locationMocked.getAltitude()).thenReturn(altitude);
// Test the constructor
LatLng latLng = new LatLng(locationMocked);
assertEquals("latitude should match", latLng.getLatitude(), latitude, DELTA);
assertEquals("longitude should match", latLng.getLongitude(), longitude, DELTA);
assertEquals("altitude should match", latLng.getAltitude(), altitude, DELTA);
}
@Test
public void testHashCode() {
double latitude = 1.2;
double longitude = 3.4;
double altitude = 5.6;
LatLng latLng = new LatLng(latitude, longitude, altitude);
assertEquals("hash code should match", latLng.hashCode(), -151519232);
}
@Test
public void testToString() {
double latitude = 1.2;
double longitude = 3.4;
double altitude = 5.6;
LatLng latLng = new LatLng(latitude, longitude, altitude);
assertEquals("string should match",
latLng.toString(),
"LatLng [latitude=1.2, longitude=3.4, altitude=5.6]");
}
@Test
public void testEqualsOther() {
double latitude = 1.2;
double longitude = 3.4;
double altitude = 5.6;
LatLng latLng1 = new LatLng(latitude, longitude, altitude);
LatLng latLng2 = new LatLng(latitude, longitude, altitude);
assertEquals("LatLng should match", latLng1, latLng2);
}
@Test
public void testEqualsItself() {
LatLng latLng = new LatLng(1, 2, 3);
assertEquals("LatLng should match", latLng, latLng);
}
@Test
public void testNotEquals() {
LatLng latLng = new LatLng(1, 2);
assertNotEquals("LatLng should match", latLng, new Object());
}
@Test
public void testParcelable() {
LatLng latLng = new LatLng(45.0, -185.0);
Parcelable parcel = MockParcel.obtain(latLng);
assertEquals("parcel should match initial object", latLng, parcel);
}
@Test
public void testWrapped() {
LatLng latLng = new LatLng(45.0, -185.0).wrap();
assertEquals("longitude wrapped value", latLng.getLongitude(), 175.0, DELTA);
}
@Test
public void testUnnecessaryWrapped() {
LatLng latLng = new LatLng(45.0, 50.0).wrap();
assertEquals("longitude wrapped value", latLng.getLongitude(), 50.0, DELTA);
}
}

View File

@@ -0,0 +1,66 @@
package com.mapbox.mapboxsdk.geometry;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class ProjectedMetersTest {
private static final LatLng LAT_LNG_NULL_ISLAND = new LatLng(0, 0);
@Test
public void testSanity() {
ProjectedMeters projectedMeters = new ProjectedMeters(0.0, 0.0);
assertNotNull("projectedMeters should not be null", projectedMeters);
}
@Test
public void testEquality() {
ProjectedMeters projectedMeters = new ProjectedMeters(0.0, 0.0);
assertEquals("projectedMeters is not equal to a LatLng", projectedMeters.equals(LAT_LNG_NULL_ISLAND), false);
assertEquals("projectedMeters is equal to itself", projectedMeters.equals(projectedMeters), true);
}
@Test
public void testNorthing() {
ProjectedMeters projectedMeters = new ProjectedMeters(1.0, 0.0);
assertEquals("northing should be 1", 1, projectedMeters.getNorthing(), 0);
}
@Test
public void testEasting() {
ProjectedMeters projectedMeters = new ProjectedMeters(0.0, 1.0);
assertEquals("easting should be 1", 1, projectedMeters.getEasting(), 0);
}
@Test
public void testConstructor() {
ProjectedMeters projectedMeters1 = new ProjectedMeters(1, 2);
ProjectedMeters projectedMeters2 = new ProjectedMeters(projectedMeters1);
assertEquals("projectedmeters should match", projectedMeters1, projectedMeters2);
}
@Test
public void testHashcode() {
ProjectedMeters meters = new ProjectedMeters(1, 2);
assertEquals("hashcode should match", -1048576, meters.hashCode());
}
@Test
public void testToString() {
ProjectedMeters meters = new ProjectedMeters(1, 1);
assertEquals("toString should match", "ProjectedMeters [northing=1.0, easting=1.0]", meters.toString());
}
@Test
public void testParcelable() {
ProjectedMeters meters = new ProjectedMeters(1, 1);
Parcelable parcel = MockParcel.obtain(meters);
assertEquals("parcel should match initial object", meters, parcel);
}
}

View File

@@ -0,0 +1,95 @@
package com.mapbox.mapboxsdk.geometry;
import android.os.Parcelable;
import com.mapbox.mapboxsdk.utils.MockParcel;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class VisibleRegionTest {
private static final LatLng FAR_LEFT = new LatLng(52, -12);
private static final LatLng NEAR_LEFT = new LatLng(34, -12);
private static final LatLng FAR_RIGHT = new LatLng(52, 26);
private static final LatLng NEAR_RIGHT = new LatLng(34, 26);
private static final LatLngBounds BOUNDS =
new LatLngBounds.Builder().include(FAR_LEFT).include(FAR_RIGHT).include(NEAR_LEFT).include(NEAR_RIGHT).build();
@Test
public void testSanity() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertNotNull("region should not be null", region);
}
@Test
public void testEquality() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("visibleRegion is not equal to a LatLng", region.equals(FAR_LEFT), false);
assertEquals("visibleRegion is equal to itself", region.equals(region), true);
}
@Test
public void testFarLeftConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.farLeft, FAR_LEFT);
}
@Test
public void testNearLeftConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.nearLeft, NEAR_LEFT);
}
@Test
public void testFarRightConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.farRight, FAR_RIGHT);
}
@Test
public void testNearRightConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLng should match", region.nearRight, NEAR_RIGHT);
}
@Test
public void testLatLngBoundsConstructor() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("LatLngBounds should match", region.latLngBounds, BOUNDS);
}
@Test
public void testEquals() {
VisibleRegion regionLeft = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
VisibleRegion regionRight = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("VisibleRegions should match", regionLeft, regionRight);
}
@Test
public void testHashcode() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("hashcode should match", -923534102, region.hashCode());
}
@Test
public void testToString() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
assertEquals("string should match",
"[farLeft [LatLng [latitude=52.0, longitude=-12.0, altitude=0.0]], "
+ "farRight [LatLng [latitude=52.0, longitude=26.0, altitude=0.0]], "
+ "nearLeft [LatLng [latitude=34.0, longitude=-12.0, altitude=0.0]], "
+ "nearRight [LatLng [latitude=34.0, longitude=26.0, altitude=0.0]], "
+ "latLngBounds [N:52.0; E:26.0; S:34.0; W:-12.0]]",
region.toString());
}
@Test
public void testParcelable() {
VisibleRegion region = new VisibleRegion(FAR_LEFT, FAR_RIGHT, NEAR_LEFT, NEAR_RIGHT, BOUNDS);
Parcelable parcel = MockParcel.obtain(region);
assertEquals("parcel should match initial object", region, parcel);
}
}

View File

@@ -0,0 +1,81 @@
package com.mapbox.mapboxsdk.maps;
import android.support.v4.util.LongSparseArray;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.MarkerViewManager;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class AnnotationManagerTest {
@Test
public void checksAddAMarker() throws Exception {
NativeMapView aNativeMapView = mock(NativeMapView.class);
MapView aMapView = mock(MapView.class);
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
MarkerViewManager aMarkerViewManager = mock(MarkerViewManager.class);
IconManager aIconManager = mock(IconManager.class);
Annotations annotations = new AnnotationContainer(aNativeMapView, annotationsArray);
Markers markers = new MarkerContainer(aNativeMapView, aMapView, annotationsArray, aIconManager, aMarkerViewManager);
Polygons polygons = new PolygonContainer(aNativeMapView, annotationsArray);
Polylines polylines = new PolylineContainer(aNativeMapView, annotationsArray);
AnnotationManager annotationManager = new AnnotationManager(aNativeMapView, aMapView, annotationsArray,
aMarkerViewManager, aIconManager, annotations, markers, polygons, polylines);
Marker aMarker = mock(Marker.class);
long aId = 5L;
when(aNativeMapView.addMarker(aMarker)).thenReturn(aId);
BaseMarkerOptions aMarkerOptions = mock(BaseMarkerOptions.class);
MapboxMap aMapboxMap = mock(MapboxMap.class);
when(aMarkerOptions.getMarker()).thenReturn(aMarker);
annotationManager.addMarker(aMarkerOptions, aMapboxMap);
assertEquals(aMarker, annotationManager.getAnnotations().get(0));
assertEquals(aMarker, annotationManager.getAnnotation(aId));
}
@Test
public void checksAddMarkers() throws Exception {
NativeMapView aNativeMapView = mock(NativeMapView.class);
MapView aMapView = mock(MapView.class);
LongSparseArray<Annotation> annotationsArray = new LongSparseArray<>();
MarkerViewManager aMarkerViewManager = mock(MarkerViewManager.class);
IconManager aIconManager = mock(IconManager.class);
Annotations annotations = new AnnotationContainer(aNativeMapView, annotationsArray);
Markers markers = new MarkerContainer(aNativeMapView, aMapView, annotationsArray, aIconManager, aMarkerViewManager);
Polygons polygons = new PolygonContainer(aNativeMapView, annotationsArray);
Polylines polylines = new PolylineContainer(aNativeMapView, annotationsArray);
AnnotationManager annotationManager = new AnnotationManager(aNativeMapView, aMapView, annotationsArray,
aMarkerViewManager, aIconManager, annotations, markers, polygons, polylines);
long firstId = 1L;
long secondId = 2L;
List<BaseMarkerOptions> markerList = new ArrayList<>();
MarkerOptions firstMarkerOption = new MarkerOptions().position(new LatLng()).title("first");
MarkerOptions secondMarkerOption = new MarkerOptions().position(new LatLng()).title("second");
markerList.add(firstMarkerOption);
markerList.add(secondMarkerOption);
MapboxMap aMapboxMap = mock(MapboxMap.class);
when(aNativeMapView.addMarker(any(Marker.class))).thenReturn(firstId, secondId);
annotationManager.addMarkers(markerList, aMapboxMap);
assertEquals(2, annotationManager.getAnnotations().size());
assertEquals("first", ((Marker) annotationManager.getAnnotations().get(0)).getTitle());
assertEquals("second", ((Marker) annotationManager.getAnnotations().get(1)).getTitle());
assertEquals("first", ((Marker) annotationManager.getAnnotation(firstId)).getTitle());
assertEquals("second", ((Marker) annotationManager.getAnnotation(secondId)).getTitle());
}
}

View File

@@ -0,0 +1,190 @@
package com.mapbox.mapboxsdk.maps;
import android.graphics.Color;
import android.view.Gravity;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import org.junit.Test;
import java.util.Arrays;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class MapboxMapOptionsTest {
private static final double DELTA = 1e-15;
@Test
public void testSanity() {
assertNotNull("should not be null", new MapboxMapOptions());
}
@Test
public void testDebugEnabled() {
assertFalse(new MapboxMapOptions().getDebugActive());
assertTrue(new MapboxMapOptions().debugActive(true).getDebugActive());
assertFalse(new MapboxMapOptions().debugActive(false).getDebugActive());
}
@Test
public void testCompassEnabled() {
assertTrue(new MapboxMapOptions().compassEnabled(true).getCompassEnabled());
assertFalse(new MapboxMapOptions().compassEnabled(false).getCompassEnabled());
}
@Test
public void testCompassGravity() {
assertEquals(Gravity.TOP | Gravity.END, new MapboxMapOptions().getCompassGravity());
assertEquals(Gravity.BOTTOM, new MapboxMapOptions().compassGravity(Gravity.BOTTOM).getCompassGravity());
assertNotEquals(Gravity.START, new MapboxMapOptions().compassGravity(Gravity.BOTTOM).getCompassGravity());
}
@Test
public void testCompassMargins() {
assertTrue(Arrays.equals(new int[] {0, 1, 2, 3}, new MapboxMapOptions().compassMargins(
new int[] {0, 1, 2, 3}).getCompassMargins()));
assertFalse(Arrays.equals(new int[] {0, 1, 2, 3}, new MapboxMapOptions().compassMargins(
new int[] {0, 0, 0, 0}).getCompassMargins()));
}
@Test
public void testLogoEnabled() {
assertTrue(new MapboxMapOptions().logoEnabled(true).getLogoEnabled());
assertFalse(new MapboxMapOptions().logoEnabled(false).getLogoEnabled());
}
@Test
public void testLogoGravity() {
assertEquals(Gravity.BOTTOM | Gravity.START, new MapboxMapOptions().getLogoGravity());
assertEquals(Gravity.BOTTOM, new MapboxMapOptions().logoGravity(Gravity.BOTTOM).getLogoGravity());
assertNotEquals(Gravity.START, new MapboxMapOptions().logoGravity(Gravity.BOTTOM).getLogoGravity());
}
@Test
public void testLogoMargins() {
assertTrue(Arrays.equals(new int[] {0, 1, 2, 3}, new MapboxMapOptions().logoMargins(
new int[] {0, 1, 2, 3}).getLogoMargins()));
assertFalse(Arrays.equals(new int[] {0, 1, 2, 3}, new MapboxMapOptions().logoMargins(
new int[] {0, 0, 0, 0}).getLogoMargins()));
}
@Test
public void testAttributionTintColor() {
assertEquals(-1, new MapboxMapOptions().getAttributionTintColor());
assertEquals(Color.RED, new MapboxMapOptions().attributionTintColor(Color.RED).getAttributionTintColor());
}
@Test
public void testAttributionEnabled() {
assertTrue(new MapboxMapOptions().attributionEnabled(true).getAttributionEnabled());
assertFalse(new MapboxMapOptions().attributionEnabled(false).getAttributionEnabled());
}
@Test
public void testAttributionGravity() {
assertEquals(Gravity.BOTTOM, new MapboxMapOptions().getAttributionGravity());
assertEquals(Gravity.BOTTOM, new MapboxMapOptions().attributionGravity(Gravity.BOTTOM).getAttributionGravity());
assertNotEquals(Gravity.START, new MapboxMapOptions().attributionGravity(Gravity.BOTTOM).getAttributionGravity());
}
@Test
public void testAttributionMargins() {
assertTrue(Arrays.equals(new int[] {0, 1, 2, 3}, new MapboxMapOptions().attributionMargins(
new int[] {0, 1, 2, 3}).getAttributionMargins()));
assertFalse(Arrays.equals(new int[] {0, 1, 2, 3}, new MapboxMapOptions().attributionMargins(
new int[] {0, 0, 0, 0}).getAttributionMargins()));
}
@Test
public void testMinZoom() {
assertEquals(MapboxConstants.MINIMUM_ZOOM, new MapboxMapOptions().getMinZoomPreference(), DELTA);
assertEquals(5.0f, new MapboxMapOptions().minZoomPreference(5.0f).getMinZoomPreference(), DELTA);
assertNotEquals(2.0f, new MapboxMapOptions().minZoomPreference(5.0f).getMinZoomPreference(), DELTA);
}
@Test
public void testMaxZoom() {
assertEquals(MapboxConstants.MAXIMUM_ZOOM, new MapboxMapOptions().getMaxZoomPreference(), DELTA);
assertEquals(5.0f, new MapboxMapOptions().maxZoomPreference(5.0f).getMaxZoomPreference(), DELTA);
assertNotEquals(2.0f, new MapboxMapOptions().maxZoomPreference(5.0f).getMaxZoomPreference(), DELTA);
}
@Test
public void testLocationEnabled() {
assertFalse(new MapboxMapOptions().getLocationEnabled());
assertTrue(new MapboxMapOptions().locationEnabled(true).getLocationEnabled());
assertFalse(new MapboxMapOptions().locationEnabled(false).getLocationEnabled());
}
@Test
public void testTiltGesturesEnabled() {
assertTrue(new MapboxMapOptions().getTiltGesturesEnabled());
assertTrue(new MapboxMapOptions().tiltGesturesEnabled(true).getTiltGesturesEnabled());
assertFalse(new MapboxMapOptions().tiltGesturesEnabled(false).getTiltGesturesEnabled());
}
@Test
public void testScrollGesturesEnabled() {
assertTrue(new MapboxMapOptions().getScrollGesturesEnabled());
assertTrue(new MapboxMapOptions().scrollGesturesEnabled(true).getScrollGesturesEnabled());
assertFalse(new MapboxMapOptions().scrollGesturesEnabled(false).getScrollGesturesEnabled());
}
@Test
public void testZoomGesturesEnabled() {
assertTrue(new MapboxMapOptions().getZoomGesturesEnabled());
assertTrue(new MapboxMapOptions().zoomGesturesEnabled(true).getZoomGesturesEnabled());
assertFalse(new MapboxMapOptions().zoomGesturesEnabled(false).getZoomGesturesEnabled());
}
@Test
public void testRotateGesturesEnabled() {
assertTrue(new MapboxMapOptions().getRotateGesturesEnabled());
assertTrue(new MapboxMapOptions().rotateGesturesEnabled(true).getRotateGesturesEnabled());
assertFalse(new MapboxMapOptions().rotateGesturesEnabled(false).getRotateGesturesEnabled());
}
@Test
public void testZoomControlsEnabled() {
assertFalse(new MapboxMapOptions().getZoomControlsEnabled());
assertTrue(new MapboxMapOptions().zoomControlsEnabled(true).getZoomControlsEnabled());
assertFalse(new MapboxMapOptions().zoomControlsEnabled(false).getZoomControlsEnabled());
}
@Test
public void testStyleUrl() {
assertEquals(Style.DARK, new MapboxMapOptions().styleUrl(Style.DARK).getStyle());
assertNotEquals(Style.LIGHT, new MapboxMapOptions().styleUrl(Style.DARK).getStyle());
assertNull(new MapboxMapOptions().getStyle());
}
@Test
public void testCamera() {
CameraPosition position = new CameraPosition.Builder().build();
assertEquals(new CameraPosition.Builder(position).build(), new MapboxMapOptions().camera(position).getCamera());
assertNotEquals(new CameraPosition.Builder().target(new LatLng(1, 1)), new MapboxMapOptions().camera(position));
assertNull(new MapboxMapOptions().getCamera());
}
@Test
public void testMyLocationForegroundTint() {
assertEquals(Color.BLUE, new MapboxMapOptions()
.myLocationForegroundTintColor(Color.BLUE).getMyLocationForegroundTintColor());
}
@Test
public void testMyLocationBackgroundTint() {
assertEquals(Color.BLUE, new MapboxMapOptions()
.myLocationBackgroundTintColor(Color.BLUE).getMyLocationBackgroundTintColor());
}
}

View File

@@ -0,0 +1,99 @@
package com.mapbox.mapboxsdk.maps;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.PointF;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class TrackingSettingsTest {
@InjectMocks
MyLocationView myLocationView = mock(MyLocationView.class);
@InjectMocks
UiSettings uiSettings = mock(UiSettings.class);
@InjectMocks
FocalPointChangeListener focalPointChangeListener = mock(FocalPointChangeListener.class);
@InjectMocks
TrackingSettings.CameraZoomInvalidator zoomInvalidator = mock(TrackingSettings.CameraZoomInvalidator.class);
private TrackingSettings trackingSettings;
@Before
public void beforeTest() {
trackingSettings = new TrackingSettings(myLocationView, uiSettings, focalPointChangeListener, zoomInvalidator);
}
@Test
public void testSanity() {
assertNotNull("trackingsettings should not be null", trackingSettings);
}
@Test
public void testDismissTrackingModesOnGesture() {
trackingSettings.setDismissAllTrackingOnGesture(false);
assertFalse("DismissTrackingOnGesture should be false", trackingSettings.isAllDismissTrackingOnGesture());
}
@Test
public void testValidateGesturesForTrackingModes() {
trackingSettings.setDismissAllTrackingOnGesture(false);
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
assertFalse("DismissTrackingOnGesture should be false", trackingSettings.isAllDismissTrackingOnGesture());
}
@Test
public void testMyLocationEnabled() {
// setup mock context to provide accepted location permission
Context context = mock(Context.class);
when(myLocationView.getContext()).thenReturn(context);
when(context.checkPermission(eq(Manifest.permission.ACCESS_COARSE_LOCATION), anyInt(),
anyInt())).thenReturn(PackageManager.PERMISSION_GRANTED);
assertFalse("Location should be disabled by default.", trackingSettings.isMyLocationEnabled());
trackingSettings.setMyLocationEnabled(true);
assertTrue("Location should be enabled", trackingSettings.isMyLocationEnabled());
}
@Test
public void testCameraZoomTo2forTracking() {
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
verify(zoomInvalidator, atLeast(1)).zoomTo(2.0);
}
@Test
public void testFocalPointChangeForTracking() {
final float centerX = 32.3f;
final float centerY = 46.3f;
final PointF pointF = new PointF(centerX, centerY);
when(myLocationView.getCenter()).thenReturn(pointF);
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW);
verify(focalPointChangeListener, atLeast(1)).onFocalPointChanged(pointF);
}
@Test
public void testFocalPointChangeForNonTracking() {
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_NONE);
verify(focalPointChangeListener, atLeast(1)).onFocalPointChanged(null);
}
}

View File

@@ -0,0 +1,375 @@
package com.mapbox.mapboxsdk.maps;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class UiSettingsTest {
@InjectMocks
Projection projection = mock(Projection.class);
@InjectMocks
FocalPointChangeListener focalPointChangeListener = mock(FocalPointChangeListener.class);
@InjectMocks
CompassView compassView = mock(CompassView.class);
@InjectMocks
ImageView imageView = mock(ImageView.class);
@InjectMocks
ImageView logoView = mock(ImageView.class);
@InjectMocks
FrameLayout.LayoutParams layoutParams = mock(FrameLayout.LayoutParams.class);
private UiSettings uiSettings;
@Before
public void beforeTest() {
uiSettings = new UiSettings(projection, focalPointChangeListener, compassView, imageView, logoView);
}
@Test
public void testSanity() {
assertNotNull("uiSettings should not be null", uiSettings);
}
@Test
public void testCompassEnabled() {
when(compassView.isEnabled()).thenReturn(true);
uiSettings.setCompassEnabled(true);
assertEquals("Compass should be enabled", true, uiSettings.isCompassEnabled());
}
@Test
public void testCompassDisabled() {
uiSettings.setCompassEnabled(false);
assertEquals("Compass should be disabled", false, uiSettings.isCompassEnabled());
}
@Test
public void testCompassGravity() {
when(compassView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.gravity = Gravity.START;
uiSettings.setCompassGravity(Gravity.START);
assertEquals("Compass gravity should be same", Gravity.START, uiSettings.getCompassGravity());
}
@Test
public void testCompassMargins() {
when(projection.getContentPadding()).thenReturn(new int[] {0, 0, 0, 0});
when(compassView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.leftMargin = 1;
layoutParams.topMargin = 2;
layoutParams.rightMargin = 3;
layoutParams.bottomMargin = 4;
uiSettings.setCompassMargins(1, 2, 3, 4);
assertTrue("Compass margin left should be same", uiSettings.getCompassMarginLeft() == 1);
assertTrue("Compass margin top should be same", uiSettings.getCompassMarginTop() == 2);
assertTrue("Compass margin right should be same", uiSettings.getCompassMarginRight() == 3);
assertTrue("Compass margin bottom should be same", uiSettings.getCompassMarginBottom() == 4);
}
@Test
public void testCompassFadeWhenFacingNorth() {
when(compassView.isFadeCompassViewFacingNorth()).thenReturn(true);
assertTrue("Compass should fade when facing north by default.", uiSettings.isCompassFadeWhenFacingNorth());
uiSettings.setCompassFadeFacingNorth(false);
when(compassView.isFadeCompassViewFacingNorth()).thenReturn(false);
assertFalse("Compass fading should be disabled", uiSettings.isCompassFadeWhenFacingNorth());
}
@Test
public void testLogoEnabled() {
uiSettings.setLogoEnabled(true);
assertEquals("Logo should be enabled", true, uiSettings.isLogoEnabled());
}
@Test
public void testLogoDisabled() {
when(logoView.getVisibility()).thenReturn(View.GONE);
uiSettings.setLogoEnabled(false);
assertEquals("Logo should be disabled", false, uiSettings.isLogoEnabled());
}
@Test
public void testLogoGravity() {
layoutParams.gravity = Gravity.END;
when(logoView.getLayoutParams()).thenReturn(layoutParams);
uiSettings.setLogoGravity(Gravity.END);
assertEquals("Logo gravity should be same", Gravity.END, uiSettings.getLogoGravity());
}
@Test
public void testLogoMargins() {
when(projection.getContentPadding()).thenReturn(new int[] {0, 0, 0, 0});
when(logoView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.leftMargin = 1;
layoutParams.topMargin = 2;
layoutParams.rightMargin = 3;
layoutParams.bottomMargin = 4;
uiSettings.setLogoMargins(1, 2, 3, 4);
assertTrue("Compass margin left should be same", uiSettings.getLogoMarginLeft() == 1);
assertTrue("Compass margin top should be same", uiSettings.getLogoMarginTop() == 2);
assertTrue("Compass margin right should be same", uiSettings.getLogoMarginRight() == 3);
assertTrue("Compass margin bottom should be same", uiSettings.getLogoMarginBottom() == 4);
}
@Test
public void testAttributionEnabled() {
when(imageView.getVisibility()).thenReturn(View.VISIBLE);
uiSettings.setAttributionEnabled(true);
assertEquals("Attribution should be enabled", true, uiSettings.isAttributionEnabled());
}
@Test
public void testAttributionDisabled() {
when(imageView.getVisibility()).thenReturn(View.GONE);
uiSettings.setAttributionEnabled(false);
assertEquals("Attribution should be disabled", false, uiSettings.isAttributionEnabled());
}
@Test
public void testAttributionGravity() {
when(imageView.getLayoutParams()).thenReturn(layoutParams);
layoutParams.gravity = Gravity.END;
uiSettings.setAttributionGravity(Gravity.END);
assertEquals("Attribution gravity should be same", Gravity.END, uiSettings.getAttributionGravity());
}
@Test
public void testAttributionMargins() {
when(imageView.getLayoutParams()).thenReturn(layoutParams);
when(projection.getContentPadding()).thenReturn(new int[] {0, 0, 0, 0});
layoutParams.leftMargin = 1;
layoutParams.topMargin = 2;
layoutParams.rightMargin = 3;
layoutParams.bottomMargin = 4;
uiSettings.setAttributionMargins(1, 2, 3, 4);
assertTrue("Attribution margin left should be same", uiSettings.getAttributionMarginLeft() == 1);
assertTrue("Attribution margin top should be same", uiSettings.getAttributionMarginTop() == 2);
assertTrue("Attribution margin right should be same", uiSettings.getAttributionMarginRight() == 3);
assertTrue("Attribution margin bottom should be same", uiSettings.getAttributionMarginBottom() == 4);
}
@Test
public void testRotateGesturesEnabled() {
uiSettings.setRotateGesturesEnabled(true);
assertEquals("Rotate gesture should be enabled", true, uiSettings.isRotateGesturesEnabled());
}
@Test
public void testRotateGesturesDisabled() {
uiSettings.setRotateGesturesEnabled(false);
assertEquals("Rotate gesture should be disabled", false, uiSettings.isRotateGesturesEnabled());
}
@Test
public void testRotateGestureChange() {
assertEquals("Default state should be true", true, uiSettings.isRotateGestureChangeAllowed());
uiSettings.setRotateGestureChangeAllowed(false);
assertEquals("State should have been changed", false, uiSettings.isRotateGestureChangeAllowed());
}
@Test
public void testRotateGestureChangeAllowed() {
uiSettings.setRotateGesturesEnabled(false);
assertEquals("Rotate gesture should be false", false, uiSettings.isRotateGesturesEnabled());
uiSettings.setRotateGesturesEnabled(true);
assertEquals("Rotate gesture should be true", true, uiSettings.isRotateGesturesEnabled());
}
@Test
public void testRotateGestureChangeDisallowed() {
assertEquals("Rotate gesture should be true", true, uiSettings.isRotateGesturesEnabled());
uiSettings.setRotateGestureChangeAllowed(false);
uiSettings.setRotateGesturesEnabled(false);
assertEquals("Rotate gesture change should be ignored", true, uiSettings.isRotateGesturesEnabled());
}
@Test
public void testTiltGesturesEnabled() {
uiSettings.setTiltGesturesEnabled(true);
assertEquals("Tilt gesture should be enabled", true, uiSettings.isTiltGesturesEnabled());
}
@Test
public void testTiltGesturesDisabled() {
uiSettings.setTiltGesturesEnabled(false);
assertEquals("Tilt gesture should be disabled", false, uiSettings.isTiltGesturesEnabled());
}
@Test
public void testTiltGestureChange() {
assertEquals("Default state should be true", true, uiSettings.isTiltGestureChangeAllowed());
uiSettings.setTiltGestureChangeAllowed(false);
assertEquals("State should have been changed", false, uiSettings.isTiltGestureChangeAllowed());
}
@Test
public void testTiltGestureChangeAllowed() {
uiSettings.setTiltGesturesEnabled(false);
assertEquals("Tilt gesture should be false", false, uiSettings.isTiltGesturesEnabled());
uiSettings.setTiltGesturesEnabled(true);
assertEquals("Tilt gesture should be true", true, uiSettings.isTiltGesturesEnabled());
}
@Test
public void testTiltGestureChangeDisallowed() {
assertEquals("Tilt gesture should be true", true, uiSettings.isTiltGesturesEnabled());
uiSettings.setTiltGestureChangeAllowed(false);
uiSettings.setTiltGesturesEnabled(false);
assertEquals("Tilt gesture change should be ignored", true, uiSettings.isTiltGesturesEnabled());
}
@Test
public void testZoomGesturesEnabled() {
uiSettings.setZoomGesturesEnabled(true);
assertEquals("Zoom gesture should be enabled", true, uiSettings.isZoomGesturesEnabled());
}
@Test
public void testZoomGesturesDisabled() {
uiSettings.setZoomGesturesEnabled(false);
assertEquals("Zoom gesture should be disabled", false, uiSettings.isZoomGesturesEnabled());
}
@Test
public void testZoomGestureChange() {
assertEquals("Default state should be true", true, uiSettings.isZoomGestureChangeAllowed());
uiSettings.setZoomGestureChangeAllowed(false);
assertEquals("State should have been changed", false, uiSettings.isZoomGestureChangeAllowed());
}
@Test
public void testZoomGestureChangeAllowed() {
uiSettings.setZoomGesturesEnabled(false);
assertEquals("Zoom gesture should be false", false, uiSettings.isZoomGesturesEnabled());
uiSettings.setZoomGesturesEnabled(true);
assertEquals("Zoom gesture should be true", true, uiSettings.isZoomGesturesEnabled());
}
@Test
public void testZoomGestureChangeDisallowed() {
assertEquals("Zoom gesture should be true", true, uiSettings.isZoomGesturesEnabled());
uiSettings.setZoomGestureChangeAllowed(false);
uiSettings.setZoomGesturesEnabled(false);
assertEquals("Zooom gesture change should be ignored", true, uiSettings.isZoomGesturesEnabled());
}
@Test
public void testZoomControlsEnabled() {
uiSettings.setZoomControlsEnabled(true);
assertEquals("Zoom controls should be enabled", true, uiSettings.isZoomControlsEnabled());
}
@Test
public void testZoomControlsDisabled() {
uiSettings.setZoomControlsEnabled(false);
assertEquals("Zoom controls should be disabled", false, uiSettings.isZoomControlsEnabled());
}
@Test
public void testDoubleTapGesturesEnabled() {
uiSettings.setDoubleTapGesturesEnabled(true);
assertEquals("DoubleTap gesture should be enabled", true, uiSettings.isDoubleTapGesturesEnabled());
}
@Test
public void testDoubleTapGesturesDisabled() {
uiSettings.setDoubleTapGesturesEnabled(false);
assertEquals("DoubleTap gesture should be disabled", false, uiSettings.isDoubleTapGesturesEnabled());
}
@Test
public void testDoubleTapGestureChange() {
assertEquals("Default state should be true", true, uiSettings.isDoubleTapGestureChangeAllowed());
uiSettings.setDoubleTapGestureChangeAllowed(false);
assertEquals("State should have been changed", false, uiSettings.isDoubleTapGestureChangeAllowed());
}
@Test
public void testDoubleTapGestureChangeAllowed() {
uiSettings.setDoubleTapGesturesEnabled(false);
assertEquals("DoubleTap gesture should be false", false, uiSettings.isDoubleTapGesturesEnabled());
uiSettings.setDoubleTapGesturesEnabled(true);
assertEquals("DoubleTap gesture should be true", true, uiSettings.isDoubleTapGesturesEnabled());
}
@Test
public void testDoubleTapGestureChangeDisallowed() {
assertEquals("DoubleTap gesture should be true", true, uiSettings.isDoubleTapGesturesEnabled());
uiSettings.setDoubleTapGestureChangeAllowed(false);
uiSettings.setDoubleTapGesturesEnabled(false);
assertEquals("DoubleTap gesture change should be ignored", true, uiSettings.isDoubleTapGesturesEnabled());
}
@Test
public void testScrollGesturesEnabled() {
uiSettings.setScrollGesturesEnabled(true);
assertEquals("Scroll gesture should be enabled", true, uiSettings.isScrollGesturesEnabled());
}
@Test
public void testScrollGesturesDisabled() {
uiSettings.setScrollGesturesEnabled(false);
assertEquals("Scroll gesture should be disabled", false, uiSettings.isScrollGesturesEnabled());
}
@Test
public void testScrollGestureChange() {
assertEquals("Default state should be true", true, uiSettings.isScrollGestureChangeAllowed());
uiSettings.setScrollGestureChangeAllowed(false);
assertEquals("State should have been changed", false, uiSettings.isScrollGestureChangeAllowed());
}
@Test
public void testScrollGestureChangeAllowed() {
uiSettings.setScrollGesturesEnabled(false);
assertEquals("Scroll gesture should be false", false, uiSettings.isScrollGesturesEnabled());
uiSettings.setScrollGesturesEnabled(true);
assertEquals("Scroll gesture should be true", true, uiSettings.isScrollGesturesEnabled());
}
@Test
public void testScrollGestureChangeDisallowed() {
assertEquals("Scroll gesture should be true", true, uiSettings.isScrollGesturesEnabled());
uiSettings.setScrollGestureChangeAllowed(false);
uiSettings.setScrollGesturesEnabled(false);
assertEquals("Scroll gesture change should be ignored", true, uiSettings.isScrollGesturesEnabled());
}
@Test
public void testAllGesturesEnabled() {
uiSettings.setAllGesturesEnabled(true);
assertEquals("Rotate gesture should be enabled", true, uiSettings.isRotateGesturesEnabled());
assertEquals("Tilt gesture should be enabled", true, uiSettings.isTiltGesturesEnabled());
assertEquals("Zoom gesture should be enabled", true, uiSettings.isZoomGesturesEnabled());
assertEquals("Scroll gesture should be enabled", true, uiSettings.isScrollGesturesEnabled());
}
@Test
public void testAllGesturesDisabled() {
uiSettings.setAllGesturesEnabled(false);
assertEquals("Rotate gesture should be enabled", false, uiSettings.isRotateGesturesEnabled());
assertEquals("Tilt gesture should be disabled", false, uiSettings.isTiltGesturesEnabled());
assertEquals("Zoom gesture should be disabled", false, uiSettings.isZoomGesturesEnabled());
assertEquals("Scroll gesture should be disabled", false, uiSettings.isScrollGesturesEnabled());
}
}

View File

@@ -0,0 +1,106 @@
package com.mapbox.mapboxsdk.maps.widgets;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.maps.TrackingSettings;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import java.util.Arrays;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MyLocationViewSettingsTest {
@InjectMocks
Projection projection = mock(Projection.class);
@InjectMocks
MyLocationView myLocationView = mock(MyLocationView.class);
@InjectMocks
TrackingSettings trackingSettings = mock(TrackingSettings.class);
@InjectMocks
FocalPointChangeListener focalPointChangeListener = mock(FocalPointChangeListener.class);
private MyLocationViewSettings locationViewSettings;
@Before
public void beforeTest() {
locationViewSettings = new MyLocationViewSettings(myLocationView, projection, focalPointChangeListener);
}
@Test
public void testSanity() {
assertNotNull("should not be null", locationViewSettings);
}
@Test
public void testForegroundDrawables() {
Drawable foregroundDrawable = mock(Drawable.class);
Drawable foregroundBearingDrawable = mock(Drawable.class);
Drawable.ConstantState constantState = mock(Drawable.ConstantState.class);
when(foregroundDrawable.getConstantState()).thenReturn(constantState);
when(constantState.newDrawable()).thenReturn(foregroundDrawable);
locationViewSettings.setForegroundDrawable(foregroundDrawable, foregroundBearingDrawable);
assertEquals("foreground should match", foregroundDrawable, locationViewSettings.getForegroundDrawable());
assertEquals("foreground bearing should match", foregroundBearingDrawable,
locationViewSettings.getForegroundBearingDrawable());
}
@Test
public void testBackgroundDrawable() {
Drawable backgroundDrawable = mock(Drawable.class);
int[] offset = new int[] {1, 2, 3, 4};
locationViewSettings.setBackgroundDrawable(backgroundDrawable, offset);
assertEquals("foreground should match", backgroundDrawable, locationViewSettings.getBackgroundDrawable());
assertTrue("offsets should match", Arrays.equals(offset, locationViewSettings.getBackgroundOffset()));
}
@Test
public void testForegroundTint() {
int color = Color.RED;
locationViewSettings.setForegroundTintColor(Color.RED);
assertEquals("color should match", color, locationViewSettings.getForegroundTintColor());
}
@Test
public void testForegroundTransparentTint() {
int color = Color.TRANSPARENT;
locationViewSettings.setForegroundTintColor(Color.TRANSPARENT);
assertEquals("color should match", color, locationViewSettings.getForegroundTintColor());
}
@Test
public void testBackgroundTint() {
int color = Color.RED;
locationViewSettings.setBackgroundTintColor(Color.RED);
assertEquals("color should match", color, locationViewSettings.getBackgroundTintColor());
}
@Test
public void testBackgroundTransparentTint() {
int color = Color.TRANSPARENT;
locationViewSettings.setBackgroundTintColor(Color.TRANSPARENT);
assertEquals("color should match", color, locationViewSettings.getBackgroundTintColor());
}
@Test
public void testEnabled() {
assertFalse("initial state should be false", locationViewSettings.isEnabled());
locationViewSettings.setEnabled(true);
assertTrue("state should be true", locationViewSettings.isEnabled());
}
}

View File

@@ -0,0 +1,102 @@
package com.mapbox.mapboxsdk.style.layers;
import org.junit.Test;
import static com.mapbox.mapboxsdk.style.layers.Filter.all;
import static com.mapbox.mapboxsdk.style.layers.Filter.any;
import static com.mapbox.mapboxsdk.style.layers.Filter.eq;
import static com.mapbox.mapboxsdk.style.layers.Filter.gt;
import static com.mapbox.mapboxsdk.style.layers.Filter.gte;
import static com.mapbox.mapboxsdk.style.layers.Filter.has;
import static com.mapbox.mapboxsdk.style.layers.Filter.in;
import static com.mapbox.mapboxsdk.style.layers.Filter.lt;
import static com.mapbox.mapboxsdk.style.layers.Filter.lte;
import static com.mapbox.mapboxsdk.style.layers.Filter.neq;
import static com.mapbox.mapboxsdk.style.layers.Filter.none;
import static com.mapbox.mapboxsdk.style.layers.Filter.notHas;
import static com.mapbox.mapboxsdk.style.layers.Filter.notIn;
import static org.junit.Assert.assertArrayEquals;
/**
* Tests for Filter
*/
public class FilterTest {
@Test
public void testAll() {
assertArrayEquals(all().toArray(), new Object[] {"all"});
assertArrayEquals(
all(eq("key", 2), neq("key", 3)).toArray(),
new Object[] {"all", new Object[] {"==", "key", 2}, new Object[] {"!=", "key", 3}}
);
}
@Test
public void testAny() {
assertArrayEquals(any().toArray(), new Object[] {"any"});
assertArrayEquals(
any(eq("key", 2), neq("key", 3)).toArray(),
new Object[] {"any", new Object[] {"==", "key", 2}, new Object[] {"!=", "key", 3}}
);
}
@Test
public void testNone() {
assertArrayEquals(none().toArray(), new Object[] {"none"});
assertArrayEquals(
none(eq("key", 2), neq("key", 3)).toArray(),
new Object[] {"none", new Object[] {"==", "key", 2}, new Object[] {"!=", "key", 3}}
);
}
@Test
public void testHas() {
assertArrayEquals(has("key").toArray(), new Object[] {"has", "key"});
}
@Test
public void testHasNot() {
assertArrayEquals(notHas("key").toArray(), new Object[] {"!has", "key"});
}
@Test
public void testEq() {
assertArrayEquals(eq("key", 1).toArray(), new Object[] {"==", "key", 1});
}
@Test
public void testNeq() {
assertArrayEquals(neq("key", 1).toArray(), new Object[] {"!=", "key", 1});
}
@Test
public void testGt() {
assertArrayEquals(gt("key", 1).toArray(), new Object[] {">", "key", 1});
}
@Test
public void testGte() {
assertArrayEquals(gte("key", 1).toArray(), new Object[] {">=", "key", 1});
}
@Test
public void testLt() {
assertArrayEquals(lt("key", 1).toArray(), new Object[] {"<", "key", 1});
}
@Test
public void testLte() {
assertArrayEquals(lte("key", 1).toArray(), new Object[] {"<=", "key", 1});
}
@Test
public void testIn() {
assertArrayEquals(in("key", 1, 2, "Aap").toArray(), new Object[] {"in", "key", 1, 2, "Aap"});
}
@Test
public void testNotIn() {
assertArrayEquals(notIn("key", 1, 2, "Noot").toArray(), new Object[] {"!in", "key", 1, 2, "Noot"});
}
}

View File

@@ -0,0 +1,34 @@
package com.mapbox.mapboxsdk.style.layers;
import com.mapbox.mapboxsdk.style.functions.Function;
import org.junit.Test;
import static com.mapbox.mapboxsdk.style.functions.Function.zoom;
import static com.mapbox.mapboxsdk.style.functions.stops.Stop.stop;
import static com.mapbox.mapboxsdk.style.functions.stops.Stops.interval;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineBlur;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
/**
* Tests Function
*/
public class FunctionTest {
@Test
public void testZoomFunction() {
Function<Float, Float> zoomF = zoom(interval(
stop(1f, lineBlur(1f)),
stop(10f, lineBlur(20f))
)
);
assertNotNull(zoomF.toValueObject());
assertArrayEquals(
new Object[] {new Object[] {1f, 1f}, new Object[] {10f, 20f}},
(Object[]) zoomF.toValueObject().get("stops")
);
}
}

View File

@@ -0,0 +1,20 @@
package com.mapbox.mapboxsdk.telemetry;
import org.junit.Test;
import okhttp3.internal.Util;
import static junit.framework.Assert.assertEquals;
public class HttpTransportTest {
@Test
public void testNonAsciiUserAgent() {
final String swedishUserAgent = "Sveriges Fjäll/1.0/1 MapboxEventsAndroid/4.0.0-SNAPSHOT";
final String asciiVersion = "Sveriges Fj?ll/1.0/1 MapboxEventsAndroid/4.0.0-SNAPSHOT";
assertEquals("asciiVersion and swedishUserAgent should match", asciiVersion,
Util.toHumanReadableAscii(swedishUserAgent));
}
}

View File

@@ -0,0 +1,254 @@
package com.mapbox.mapboxsdk.utils;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyByte;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.anyFloat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockParcel {
public static Parcelable obtain(@NonNull Parcelable object) {
return obtain(object, 0);
}
public static Parcelable obtain(@NonNull Parcelable object, int describeContentsValue) {
testDescribeContents(object, describeContentsValue);
testParcelableArray(object);
return testParcelable(object);
}
public static Parcelable testParcelable(@NonNull Parcelable object) {
Parcel parcel = ParcelMocker.obtain(object);
object.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
try {
Field field = object.getClass().getDeclaredField("CREATOR");
field.setAccessible(true);
Class<?> creatorClass = field.getType();
Object fieldValue = field.get(object);
Method myMethod = creatorClass.getDeclaredMethod("createFromParcel", Parcel.class);
return (Parcelable) myMethod.invoke(fieldValue, parcel);
} catch (Exception exception) {
return null;
}
}
public static void testParcelableArray(@NonNull Parcelable object) {
Parcelable[] objects = new Parcelable[] {object};
Parcel parcel = ParcelMocker.obtain(objects);
parcel.writeParcelableArray(objects, 0);
parcel.setDataPosition(0);
Parcelable[] parcelableArray = parcel.readParcelableArray(object.getClass().getClassLoader());
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
}
public static void testDescribeContents(@NonNull Parcelable object, int describeContentsValue) {
if (describeContentsValue == 0) {
assertEquals("\nExpecting a describeContents() value of 0 for a " + object.getClass().getSimpleName()
+ " instance." + "\nYou can provide a different value for describeContentValue through the obtain method.",
0,
object.describeContents());
} else {
assertEquals("Expecting a describeContents() value of " + describeContentsValue,
describeContentsValue,
object.describeContents());
}
}
private static class ParcelMocker {
public static Parcel obtain(@NonNull Parcelable target) {
Parcel parcel = new ParcelMocker(target).getMockedParcel();
target.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
return parcel;
}
public static Parcel obtain(@NonNull Parcelable[] targets) {
if (targets.length == 0) {
throw new IllegalArgumentException("The passed argument may not be empty");
}
Parcel parcel = new ParcelMocker(targets[0]).getMockedParcel();
parcel.writeParcelableArray(targets, 0);
parcel.setDataPosition(0);
return parcel;
}
private List<Object> objects;
private Object object;
private Parcel mockedParcel;
private int position;
private ParcelMocker(Object o) {
this.object = o;
mockedParcel = mock(Parcel.class);
objects = new ArrayList<>();
setupMock();
}
private Parcel getMockedParcel() {
return mockedParcel;
}
private void setupMock() {
setupWrites();
setupReads();
setupOthers();
}
private void setupWrites() {
Answer<Void> writeValueAnswer = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object parameter = invocation.getArguments()[0];
objects.add(parameter);
return null;
}
};
Answer<Void> writeArrayAnswer = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Object[] parameters = (Object[]) invocation.getArguments()[0];
objects.add(parameters.length);
for (Object o : parameters) {
objects.add(o);
}
return null;
}
};
Answer<Void> writeIntArrayAnswer = new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
int[] parameters = (int[]) invocation.getArguments()[0];
if (parameters != null) {
objects.add(parameters.length);
for (Object o : parameters) {
objects.add(o);
}
} else {
objects.add(-1);
}
return null;
}
};
doAnswer(writeValueAnswer).when(mockedParcel).writeByte(anyByte());
doAnswer(writeValueAnswer).when(mockedParcel).writeLong(anyLong());
doAnswer(writeValueAnswer).when(mockedParcel).writeString(anyString());
doAnswer(writeValueAnswer).when(mockedParcel).writeInt(anyInt());
doAnswer(writeIntArrayAnswer).when(mockedParcel).writeIntArray(any(int[].class));
doAnswer(writeValueAnswer).when(mockedParcel).writeDouble(anyDouble());
doAnswer(writeValueAnswer).when(mockedParcel).writeFloat(anyFloat());
doAnswer(writeValueAnswer).when(mockedParcel).writeParcelable(any(Parcelable.class), eq(0));
doAnswer(writeArrayAnswer).when(mockedParcel).writeParcelableArray(any(Parcelable[].class), eq(0));
}
private void setupReads() {
when(mockedParcel.readInt()).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return (Integer) objects.get(position++);
}
});
when(mockedParcel.readByte()).thenAnswer(new Answer<Byte>() {
@Override
public Byte answer(InvocationOnMock invocation) throws Throwable {
return (Byte) objects.get(position++);
}
});
when(mockedParcel.readLong()).thenAnswer(new Answer<Long>() {
@Override
public Long answer(InvocationOnMock invocation) throws Throwable {
return (Long) objects.get(position++);
}
});
when(mockedParcel.readString()).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return (String) objects.get(position++);
}
});
when(mockedParcel.readDouble()).thenAnswer(new Answer<Double>() {
@Override
public Double answer(InvocationOnMock invocation) throws Throwable {
return (Double) objects.get(position++);
}
});
when(mockedParcel.readFloat()).thenAnswer(new Answer<Float>() {
@Override
public Float answer(InvocationOnMock invocation) throws Throwable {
return (Float) objects.get(position++);
}
});
when(mockedParcel.readParcelable(Parcelable.class.getClassLoader())).thenAnswer(new Answer<Parcelable>() {
@Override
public Parcelable answer(InvocationOnMock invocation) throws Throwable {
return (Parcelable) objects.get(position++);
}
});
when(mockedParcel.readParcelableArray(Parcelable.class.getClassLoader())).thenAnswer(new Answer<Object[]>() {
@Override
public Object[] answer(InvocationOnMock invocation) throws Throwable {
int size = (Integer) objects.get(position++);
Field field = object.getClass().getDeclaredField("CREATOR");
field.setAccessible(true);
Class<?> creatorClass = field.getType();
Object fieldValue = field.get(object);
Method myMethod = creatorClass.getDeclaredMethod("newArray", int.class);
Object[] array = (Object[]) myMethod.invoke(fieldValue, size);
for (int i = 0; i < size; i++) {
array[i] = objects.get(position++);
}
return array;
}
});
when(mockedParcel.createIntArray()).then(new Answer<int[]>() {
@Override
public int[] answer(InvocationOnMock invocation) throws Throwable {
int size = (Integer) objects.get(position++);
if (size == -1) {
return null;
}
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = (Integer) objects.get(position++);
}
return array;
}
});
}
private void setupOthers() {
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
position = ((Integer) invocation.getArguments()[0]);
return null;
}
}).when(mockedParcel).setDataPosition(anyInt());
}
}
}