[android] #4814 - simplified mock parcel testing, update tests to reflect these changes
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
package com.mapbox.mapboxsdk.annotations;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
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;
|
||||
@@ -152,27 +147,8 @@ public class MarkerTest {
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
MarkerOptions object = new MarkerOptions().position(new LatLng(0, 0)).snippet("s").title("t");
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
object.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
MarkerOptions parceable = MarkerOptions.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
MarkerOptions[] objects = new MarkerOptions[]{new MarkerOptions().position(new LatLng(0, 0)), new MarkerOptions().position(new LatLng(1, 1))};
|
||||
Parcel parcel = MockParcel.obtain(objects);
|
||||
parcel.writeParcelableArray(objects, 0);
|
||||
parcel.setDataPosition(0);
|
||||
MarkerOptions[] parcelableArray = (MarkerOptions[]) parcel.readParcelableArray(LatLng.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
MarkerOptions object = new MarkerOptions().position(new LatLng(0, 0)).snippet("s").title("t");
|
||||
assertEquals("contents should be 0", 1034152.0, object.describeContents(), 0);
|
||||
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng()).title("t").snippet("s");
|
||||
Parcelable parcelable = MockParcel.obtain(markerOptions);
|
||||
assertEquals("Parcel should match original object", parcelable, markerOptions);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.mapbox.mapboxsdk.camera;
|
||||
|
||||
import android.content.res.TypedArray;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.constants.MathConstants;
|
||||
@@ -12,7 +12,6 @@ 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;
|
||||
@@ -156,32 +155,8 @@ public class CameraPositionTest {
|
||||
|
||||
@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());
|
||||
CameraPosition object = new CameraPosition(new LatLng(1, 2), 3, 4, 5);
|
||||
Parcelable parcelable = MockParcel.obtain(object);
|
||||
assertEquals("Parcel should match original object", parcelable, object);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException;
|
||||
import com.mapbox.mapboxsdk.utils.MockParcel;
|
||||
@@ -177,36 +177,7 @@ public class LatLngBoundsTest {
|
||||
.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);
|
||||
Parcelable parcel = MockParcel.obtain(latLngBounds);
|
||||
assertEquals("Parcel should match original object", parcel, latLngBounds);
|
||||
}
|
||||
|
||||
@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,13 +1,11 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
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;
|
||||
|
||||
@@ -61,22 +59,7 @@ public class LatLngSpanTest {
|
||||
@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);
|
||||
Parcelable parcel = MockParcel.obtain(object);
|
||||
assertEquals("parcel should match initial object", object, parcel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.location.Location;
|
||||
import android.os.Parcel;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import com.mapbox.mapboxsdk.utils.MockParcel;
|
||||
|
||||
import org.junit.Test;
|
||||
import java.util.Objects;
|
||||
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.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -164,7 +162,7 @@ public class LatLngTest {
|
||||
|
||||
@Test
|
||||
public void testEqualsItself() {
|
||||
LatLng latLng = new LatLng(1,2,3);
|
||||
LatLng latLng = new LatLng(1, 2, 3);
|
||||
assertEquals("LatLng should match", latLng, latLng);
|
||||
}
|
||||
|
||||
@@ -176,28 +174,9 @@ public class LatLngTest {
|
||||
|
||||
@Test
|
||||
public void testParcelable() {
|
||||
LatLng object = new LatLng(1, 2, 3);
|
||||
Parcel parcel = MockParcel.obtain(object);
|
||||
object.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
LatLng parceable = LatLng.CREATOR.createFromParcel(parcel);
|
||||
assertEquals("parcel should match initial object", object, parceable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelableArray() {
|
||||
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[] parcelableArray = (LatLng[]) parcel.readParcelableArray(LatLng.class.getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDescribeContents() {
|
||||
LatLng latLng = new LatLng(1.2, 3.4);
|
||||
assertEquals("contents should be 0", 0, latLng.describeContents(), DELTA);
|
||||
LatLng latLng = new LatLng(45.0, -185.0);
|
||||
Parcelable parcel = MockParcel.obtain(latLng);
|
||||
assertEquals("parcel should match initial object", latLng, parcel);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
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;
|
||||
|
||||
@@ -60,30 +59,8 @@ public class ProjectedMetersTest {
|
||||
|
||||
@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);
|
||||
ProjectedMeters meters = new ProjectedMeters(1, 1);
|
||||
Parcelable parcel = MockParcel.obtain(meters);
|
||||
assertEquals("parcel should match initial object", meters, parcel);
|
||||
}
|
||||
|
||||
@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,12 +1,11 @@
|
||||
package com.mapbox.mapboxsdk.geometry;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
|
||||
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;
|
||||
|
||||
@@ -88,47 +87,8 @@ public class VisibleRegionTest {
|
||||
|
||||
@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);
|
||||
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);
|
||||
}
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ 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;
|
||||
@@ -11,6 +12,9 @@ import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyByte;
|
||||
import static org.mockito.Matchers.anyDouble;
|
||||
@@ -24,129 +28,182 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
public class MockParcel {
|
||||
|
||||
public static Parcel obtain(Parcelable target) {
|
||||
Parcel parcel = new MockParcel(target).getMockedParcel();
|
||||
target.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
return parcel;
|
||||
public static Parcelable obtain(@NonNull Parcelable object) {
|
||||
return obtain(object, 0);
|
||||
}
|
||||
|
||||
public static Parcel obtain(Parcelable[] targets) {
|
||||
if (targets == null || targets.length == 0) {
|
||||
throw new IllegalArgumentException("The passed argument may not be null or empty");
|
||||
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 e) {
|
||||
assertNotNull("Exception occurred:" + e.getLocalizedMessage(), null);
|
||||
}
|
||||
Parcel parcel = new MockParcel(targets[0]).getMockedParcel();
|
||||
parcel.writeParcelableArray(targets, 0);
|
||||
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);
|
||||
return parcel;
|
||||
Parcelable[] parcelableArray = parcel.readParcelableArray(object.getClass().getClassLoader());
|
||||
assertArrayEquals("parcel should match initial object", objects, parcelableArray);
|
||||
}
|
||||
|
||||
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();
|
||||
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 Parcel getMockedParcel() {
|
||||
return mockedParcel;
|
||||
}
|
||||
private static class ParcelMocker {
|
||||
|
||||
private void setupMock() {
|
||||
setupWrites();
|
||||
setupReads();
|
||||
setupOthers();
|
||||
}
|
||||
public static Parcel obtain(@NonNull Parcelable target) {
|
||||
Parcel parcel = new ParcelMocker(target).getMockedParcel();
|
||||
target.writeToParcel(parcel, 0);
|
||||
parcel.setDataPosition(0);
|
||||
return parcel;
|
||||
}
|
||||
|
||||
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;
|
||||
public static Parcel obtain(@NonNull Parcelable[] targets) {
|
||||
if (targets.length == 0) {
|
||||
throw new IllegalArgumentException("The passed argument may not be empty");
|
||||
}
|
||||
};
|
||||
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);
|
||||
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;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeByte(anyByte());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeLong(anyLong());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeString(anyString());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeDouble(anyDouble());
|
||||
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.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.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 = 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++) {
|
||||
array[i] = objects.get(position++);
|
||||
};
|
||||
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;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeByte(anyByte());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeLong(anyLong());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeString(anyString());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeDouble(anyDouble());
|
||||
doAnswer(writeValueAnswer).when(mockedParcel).writeParcelable(any(Parcelable.class), eq(0));
|
||||
doAnswer(writeArrayAnswer).when(mockedParcel).writeParcelableArray(any(Parcelable[].class), eq(0));
|
||||
}
|
||||
|
||||
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());
|
||||
private void setupReads() {
|
||||
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.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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user