[android] #3832 - improve unit testing geometry package
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException;
|
||||
import com.mapbox.mapboxsdk.utils.MockParcel;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -168,5 +171,42 @@ public class LatLngBoundsTest {
|
||||
.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
LatLngBounds latLngBounds = new LatLngBounds.Builder()
|
||||
.include(new LatLng(10, 10))
|
||||
.include(new LatLng(9, 8))
|
||||
.build();
|
||||
Parcel parcel = MockParcel.obtain(latLngBounds);
|
||||
latLngBounds.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
LatLngBounds parceable = LatLngBounds.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", latLngBounds, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
LatLngBounds latLngBounds1 = new LatLngBounds.Builder()
|
||||
.include(new LatLng(10, 10))
|
||||
.include(new LatLng(9, 8))
|
||||
.build();
|
||||
LatLngBounds latLngBounds2 = new LatLngBounds.Builder()
|
||||
.include(new LatLng(11, 12))
|
||||
.include(new LatLng(95, 9))
|
||||
.build();
|
||||
|
||||
LatLngBounds[] objects = new LatLngBounds[]{latLngBounds1, latLngBounds2};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
LatLngBounds[] parcelableArray = (LatLngBounds[]) parcel.readParcelableArray(LatLngSpan.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
LatLngBounds latLngBounds = new LatLngBounds.Builder()
|
||||
.include(new LatLng(10, 10))
|
||||
.include(new LatLng(9, 8))
|
||||
.build();
|
||||
assertEquals("contents should be 0", 0, latLngBounds.describeContents(), DELTA);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.mapbox.mapboxsdk.geometry.LatLngSpan;
|
||||
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.assertNotNull;
|
||||
|
||||
@@ -54,4 +58,25 @@ public class LatLngSpanTest {
|
||||
assertEquals("latitude in constructor", latLngSpan.getLongitudeSpan(), longitude, DELTA);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
LatLngSpan object = new LatLngSpan(1, 2);
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
LatLngSpan parcelable = LatLngSpan.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parcelable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
LatLngSpan[] objects = new LatLngSpan[]{new LatLngSpan(1, 2), new LatLngSpan(2, 2)};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
LatLngSpan[] parcelableArray = (LatLngSpan[]) parcel.readParcelableArray(LatLngSpan.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
LatLngSpan latLngSpan = new LatLngSpan(1.2, 3.4);
|
||||
assertEquals("contents should be 0", 0, latLngSpan.describeContents(), DELTA);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,22 +176,22 @@ public class LatLngTest {
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
LatLng latLng = new LatLng(1, 2, 3);
|
||||
Parcel parcel = MockParcel.obtain();
|
||||
latLng.writeToParcel(parcel, 0);
|
||||
LatLng object = new LatLng(1, 2, 3);
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
object.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
LatLng parceledLatLng = LatLng.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", latLng, parceledLatLng);
|
||||
LatLng parceable = LatLng.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
LatLng[] latLngs = new LatLng[]{new LatLng(1, 2, 3), new LatLng(1, 2)};
|
||||
Parcel parcel = MockParcel.obtain();
|
||||
parcel.writeParcelableArray(latLngs, 0);
|
||||
LatLng[] objects = new LatLng[]{new LatLng(1, 2, 3), new LatLng(1, 2)};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
parcel.writeParcelableArray(objects, 0);
|
||||
parcel.setDataPosition(0);
|
||||
LatLng[] parceledLatLngs = (LatLng[]) parcel.readParcelableArray(LatLng.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", latLngs, parceledLatLngs);
|
||||
LatLng[] parcelableArray = (LatLng[]) parcel.readParcelableArray(LatLng.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
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.assertNotNull;
|
||||
|
||||
@@ -48,8 +53,37 @@ public class ProjectedMetersTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString(){
|
||||
public void testToString() {
|
||||
ProjectedMeters meters = new ProjectedMeters(1, 1);
|
||||
assertEquals("toString should match","ProjectedMeters [northing=1.0, easting=1.0]",meters.toString());
|
||||
assertEquals("toString should match", "ProjectedMeters [northing=1.0, easting=1.0]", meters.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
ProjectedMeters object = new ProjectedMeters(1, 1);
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
object.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
ProjectedMeters parceable = ProjectedMeters.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
ProjectedMeters object1 = new ProjectedMeters(1, 1);
|
||||
ProjectedMeters object2 = new ProjectedMeters(2, 2);
|
||||
ProjectedMeters[] objects = new ProjectedMeters[]{object1, object2};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
parcel.writeParcelableArray(objects, 0);
|
||||
parcel.setDataPosition(0);
|
||||
ProjectedMeters[] parcelableArray = (ProjectedMeters[]) parcel.readParcelableArray(ProjectedMeters.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
ProjectedMeters object = new ProjectedMeters(1, 1);
|
||||
assertEquals("contents should be 0", 0, object.describeContents());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
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.assertNotNull;
|
||||
|
||||
@@ -81,4 +86,49 @@ public class VisibleRegionTest {
|
||||
, region.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
VisibleRegion object = new VisibleRegion(new LatLng(-1, -1),
|
||||
new LatLng(-1, 1),
|
||||
new LatLng(1, 1),
|
||||
new LatLng(1, -1),
|
||||
new LatLngBounds.Builder().include(new LatLng(1,1)).include(new LatLng(-1,-1)).build());
|
||||
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
VisibleRegion parceable = VisibleRegion.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
VisibleRegion object1 = new VisibleRegion(new LatLng(-1, -1),
|
||||
new LatLng(-1, 1),
|
||||
new LatLng(1, 1),
|
||||
new LatLng(1, -1),
|
||||
new LatLngBounds.Builder().include(new LatLng(1,1)).include(new LatLng(-1, -1)).build());
|
||||
|
||||
VisibleRegion object2 = new VisibleRegion(new LatLng(-1, -1),
|
||||
new LatLng(-1, 1),
|
||||
new LatLng(1, 1),
|
||||
new LatLng(1, -1),
|
||||
new LatLngBounds.Builder().include(new LatLng(1,1)).include(new LatLng(-1, -1)).build());
|
||||
|
||||
VisibleRegion[] objects = new VisibleRegion[]{object1,object2};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
parcel.writeParcelableArray(objects, 0);
|
||||
parcel.setDataPosition(0);
|
||||
VisibleRegion[] parcelableArray = (VisibleRegion[]) parcel.readParcelableArray(VisibleRegion.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
VisibleRegion object = new VisibleRegion(new LatLng(-1, -1),
|
||||
new LatLng(-1, 1),
|
||||
new LatLng(1, 1),
|
||||
new LatLng(1, -1),
|
||||
new LatLngBounds.Builder().include(new LatLng(1,1)).include(new LatLng(-1, -1)).build());
|
||||
assertEquals("contents should be 0", 0, object.describeContents());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package com.mapbox.mapboxsdk.utils;
|
||||
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import android.os.Parcelable;
|
||||
|
||||
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;
|
||||
|
||||
@@ -22,24 +23,39 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
public class MockParcel {
|
||||
|
||||
public static Parcel obtain() {
|
||||
return new MockParcel().getMockedParcel();
|
||||
public static Parcel obtain(Parcelable target) {
|
||||
Parcel parcel = new MockParcel(target).getMockedParcel();
|
||||
target.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
return parcel;
|
||||
}
|
||||
|
||||
Parcel mockedParcel;
|
||||
int position;
|
||||
List<Object> objects;
|
||||
|
||||
public Parcel getMockedParcel() {
|
||||
return mockedParcel;
|
||||
public static Parcel obtain(Parcelable[] targets) {
|
||||
if (targets == null || targets.length == 0) {
|
||||
throw new IllegalArgumentException("The passed argument may not be null or empty");
|
||||
}
|
||||
Parcel parcel = new MockParcel(targets[0]).getMockedParcel();
|
||||
parcel.writeParcelableArray(targets, 0);
|
||||
parcel.setDataPosition(0);
|
||||
return parcel;
|
||||
}
|
||||
|
||||
public MockParcel() {
|
||||
private Parcel mockedParcel;
|
||||
private int position;
|
||||
private List<Object> objects;
|
||||
private Object o;
|
||||
|
||||
private MockParcel(Object o) {
|
||||
this.o = o;
|
||||
mockedParcel = mock(Parcel.class);
|
||||
objects = new ArrayList<>();
|
||||
setupMock();
|
||||
}
|
||||
|
||||
private Parcel getMockedParcel() {
|
||||
return mockedParcel;
|
||||
}
|
||||
|
||||
private void setupMock() {
|
||||
setupWrites();
|
||||
setupReads();
|
||||
@@ -66,11 +82,11 @@ public class MockParcel {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeLong(anyLong());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeString(anyString());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeDouble(anyDouble());
|
||||
doAnswer(writeArrayAnswer).when(mockedParcel).writeParcelableArray(any(LatLng[].class), eq(0));
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeParcelable(any(Parcelable.class), eq(0));
|
||||
doAnswer(writeArrayAnswer).when(mockedParcel).writeParcelableArray(any(Parcelable[].class), eq(0));
|
||||
}
|
||||
|
||||
private void setupReads() {
|
||||
@@ -92,15 +108,26 @@ public class MockParcel {
|
||||
return (Double) objects.get(position++);
|
||||
}
|
||||
});
|
||||
when(mockedParcel.readParcelableArray(LatLng.class.getClassLoader())).thenAnswer(new Answer<LatLng[]>() {
|
||||
when(mockedParcel.readParcelable(Parcelable.class.getClassLoader())).thenAnswer(new Answer<Parcelable>() {
|
||||
@Override
|
||||
public LatLng[] answer(InvocationOnMock invocation) throws Throwable {
|
||||
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++);
|
||||
LatLng[] latLngs = LatLng.CREATOR.newArray(size);
|
||||
Field field = o.getClass().getDeclaredField("CREATOR");
|
||||
field.setAccessible(true);
|
||||
Class<?> creatorClass = field.getType();
|
||||
Object fieldValue = field.get(o);
|
||||
Method myMethod = creatorClass.getDeclaredMethod("newArray", int.class);
|
||||
Object[] array = (Object[]) myMethod.invoke(fieldValue, size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
latLngs[i] = (LatLng) objects.get(position++);
|
||||
array[i] = objects.get(position++);
|
||||
}
|
||||
return latLngs;
|
||||
return array;
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -114,5 +141,4 @@ public class MockParcel {
|
||||
}
|
||||
}).when(mockedParcel).setDataPosition(anyInt());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user