[android] - Marker test coverage

This commit is contained in:
Tobrun
2016-04-05 08:56:34 +02:00
parent f964e40e7e
commit f3c64a4c95
4 changed files with 78 additions and 6 deletions

View File

@@ -6,9 +6,10 @@ import com.mapbox.mapboxsdk.maps.MapView;
/**
* Icon is the visual representation of a {@link Marker} on a {@link MapView}.
*
* @see Marker
*/
public final class Icon {
public class Icon {
private Bitmap mBitmap;
private String mId;
@@ -39,8 +40,13 @@ public final class Icon {
@Override
public int hashCode() {
int result = mBitmap.hashCode();
result = 31 * result + mId.hashCode();
int result = 0;
if (mBitmap != null) {
result = mBitmap.hashCode();
}
if (mId != null) {
result = 31 * result + mId.hashCode();
}
return result;
}
}

View File

@@ -39,7 +39,7 @@ public final class MarkerOptions extends BaseMarkerOptions<Marker, MarkerOptions
marker = new Marker();
position((LatLng) in.readParcelable(LatLng.class.getClassLoader()));
snippet(in.readString());
title(in.readString());
if(in.readByte()!=0){
// this means we have an icon
String iconId = in.readString();
@@ -47,7 +47,6 @@ public final class MarkerOptions extends BaseMarkerOptions<Marker, MarkerOptions
Icon icon = new Icon(iconId, iconBitmap);
icon(icon);
}
title(in.readString());
}
@Override
@@ -64,13 +63,13 @@ public final class MarkerOptions extends BaseMarkerOptions<Marker, MarkerOptions
public void writeToParcel(Parcel out, int flags) {
out.writeParcelable(getPosition(), flags);
out.writeString(getSnippet());
out.writeString(getTitle());
Icon icon = getIcon();
out.writeByte((byte) (icon != null ? 1 : 0));
if (icon != null) {
out.writeString(getIcon().getId());
out.writeParcelable(getIcon().getBitmap(), flags);
}
out.writeString(getTitle());
}
private Marker marker;

View File

@@ -2,16 +2,21 @@ 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;
import static org.mockito.Mockito.mock;
public class MarkerTest {
@@ -87,6 +92,35 @@ public class MarkerTest {
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));
@@ -116,4 +150,29 @@ public class MarkerTest {
assertEquals(marker.toString(), "Marker [position[" + "LatLng [latitude=0.0, longitude=0.0, altitude=0.0]" + "]]");
}
@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);
}
}

View File

@@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.List;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyByte;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
@@ -82,6 +83,7 @@ public class MockParcel {
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());
@@ -90,6 +92,12 @@ public class MockParcel {
}
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 {