[android] #4565 - improved coverage of camera package
This commit is contained in:
@@ -1,17 +1,29 @@
|
||||
package com.mapbox.mapboxsdk.camera;
|
||||
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MathConstants;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.utils.MathUtils;
|
||||
import com.mapbox.mapboxsdk.utils.MockParcel;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
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);
|
||||
@@ -19,6 +31,55 @@ public class CameraPositionTest {
|
||||
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.MapView_direction, 0.0f)).thenReturn(bearing);
|
||||
when(typedArray.getFloat(R.styleable.MapView_center_latitude, 0.0f)).thenReturn(latitude);
|
||||
when(typedArray.getFloat(R.styleable.MapView_center_longitude, 0.0f)).thenReturn(longitude);
|
||||
when(typedArray.getFloat(R.styleable.MapView_zoom, 0.0f)).thenReturn(zoom);
|
||||
when(typedArray.getFloat(R.styleable.MapView_tilt, 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 testJniBuilder() {
|
||||
double bearing = 180;
|
||||
double zoom = 12;
|
||||
double latitude = 10;
|
||||
double longitude = 11;
|
||||
double tilt = 44;
|
||||
|
||||
double[] cameraVars = new double[]{latitude, longitude, bearing, tilt, zoom};
|
||||
CameraPosition cameraPosition = new CameraPosition.Builder(cameraVars).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);
|
||||
@@ -74,4 +135,53 @@ public class CameraPositionTest {
|
||||
(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() {
|
||||
LatLng latLng = new LatLng(1, 2);
|
||||
CameraPosition object = new CameraPosition(latLng, 3, 4, 5);
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
object.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
CameraPosition parceable = CameraPosition.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
LatLng latLng = new LatLng(1, 2);
|
||||
CameraPosition object1 = new CameraPosition(latLng, 3, 4, 5);
|
||||
CameraPosition object2 = new CameraPosition(latLng, 9, 9, 9);
|
||||
CameraPosition[] objects = new CameraPosition[]{object1, object2};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
parcel.writeParcelableArray(objects, 0);
|
||||
parcel.setDataPosition(0);
|
||||
CameraPosition[] parcelableArray = (CameraPosition[]) parcel.readParcelableArray(CameraPosition.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
LatLng latLng = new LatLng(1, 2);
|
||||
CameraPosition object = new CameraPosition(latLng, 3, 4, 5);
|
||||
assertEquals("contents should be 0", 0, object.describeContents());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.mapbox.mapboxsdk.maps;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Point;
|
||||
import android.graphics.PointF;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
@@ -12,6 +13,7 @@ import com.mapbox.mapboxsdk.annotations.PolylineOptions;
|
||||
import com.mapbox.mapboxsdk.camera.CameraPosition;
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -26,6 +28,7 @@ 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.Matchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -371,6 +374,58 @@ public class MapboxMapTest {
|
||||
assertTrue("Zoomlevel should be same", 10 == mMapboxMap.getCameraPosition().zoom);
|
||||
}
|
||||
|
||||
//
|
||||
// Camera - LatLngBounds
|
||||
//
|
||||
@Test
|
||||
public void testLatLngBounds() {
|
||||
LatLng la = new LatLng(34.053940, -118.242622);
|
||||
LatLng ny = new LatLng(40.712730, -74.005953);
|
||||
LatLng centroid = new LatLng(
|
||||
(la.getLatitude() + ny.getLatitude()) / 2,
|
||||
(la.getLongitude() + ny.getLongitude()) / 2);
|
||||
|
||||
Projection projection = mock(Projection.class);
|
||||
when(projection.toScreenLocation(la)).thenReturn(new PointF(20, 20));
|
||||
when(projection.toScreenLocation(ny)).thenReturn(new PointF(100, 100));
|
||||
when(projection.fromScreenLocation(any(PointF.class))).thenReturn(centroid);
|
||||
|
||||
UiSettings uiSettings = mock(UiSettings.class);
|
||||
when(uiSettings.getHeight()).thenReturn(1000f);
|
||||
|
||||
mMapboxMap.setProjection(projection);
|
||||
mMapboxMap.setUiSettings(uiSettings);
|
||||
|
||||
LatLngBounds bounds = new LatLngBounds.Builder().include(la).include(ny).build();
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 1));
|
||||
|
||||
assertEquals("LatLng should be same", centroid, mMapboxMap.getCameraPosition().target);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// CameraPositionUpdate - NPX target
|
||||
//
|
||||
@Test
|
||||
public void testCamerePositionUpdateNullTarget() {
|
||||
LatLng latLng = new LatLng(1, 1);
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLng(null));
|
||||
assertEquals("LatLng should be same", latLng, mMapboxMap.getCameraPosition().target);
|
||||
}
|
||||
|
||||
//
|
||||
// Camera - ScrollBy
|
||||
//
|
||||
@Test
|
||||
public void testScrollBy() {
|
||||
LatLng latLng = new LatLng(1, 1);
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.scrollBy(0, 0));
|
||||
assertEquals("LatLng should be same", latLng, mMapboxMap.getCameraPosition().target);
|
||||
mMapboxMap.moveCamera(CameraUpdateFactory.scrollBy(12, 12));
|
||||
}
|
||||
|
||||
//
|
||||
// Camera - Zoom
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user