[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

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

View File

@@ -1,92 +0,0 @@
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

@@ -1,91 +0,0 @@
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

@@ -1,55 +0,0 @@
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

@@ -1,86 +0,0 @@
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

@@ -1,160 +0,0 @@
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

@@ -1,219 +0,0 @@
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

@@ -1,75 +0,0 @@
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

@@ -1,75 +0,0 @@
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

@@ -1,110 +0,0 @@
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

@@ -1,284 +0,0 @@
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

@@ -1,65 +0,0 @@
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

@@ -1,267 +0,0 @@
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

@@ -1,66 +0,0 @@
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

@@ -1,95 +0,0 @@
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

@@ -1,190 +0,0 @@
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

@@ -1,99 +0,0 @@
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

@@ -1,375 +0,0 @@
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

@@ -1,106 +0,0 @@
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

@@ -1,102 +0,0 @@
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

@@ -1,34 +0,0 @@
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

@@ -1,20 +0,0 @@
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

@@ -1,254 +0,0 @@
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());
}
}
}