Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #3830 added mocked parcel implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Feb 5, 2016
1 parent 026e6a7 commit fa21051
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import android.os.Parcel;

import com.mapbox.mapboxsdk.geometry.LatLng;
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.assertSame;
import static org.mockito.Mockito.mock;
Expand All @@ -23,6 +28,24 @@ public void testSanity() {
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;
Expand Down Expand Up @@ -131,4 +154,52 @@ public void testToString() {
"LatLng [longitude=3.4, latitude=1.2, 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(1, 2, 3);
Parcel parcel = MockParcel.obtain();
latLng.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
LatLng parceledLatLng = LatLng.CREATOR.createFromParcel(parcel);
assertEquals("parcel should match initial object", latLng, parceledLatLng);
}

@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);
parcel.setDataPosition(0);
LatLng[] parceledLatLngs = (LatLng[]) parcel.readParcelableArray(LatLng.class.getClassLoader());
assertArrayEquals("parcel should match initial object", latLngs, parceledLatLngs);
}

@Test
public void testDescribeContents() {
LatLng latLng = new LatLng(1.2, 3.4);
assertEquals("contents should be 0", 0, latLng.describeContents(), DELTA);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package com.mapbox.mapboxsdk.utils;

import android.os.Parcel;

import com.mapbox.mapboxsdk.geometry.LatLng;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyDouble;
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 Parcel obtain() {
return new MockParcel().getMockedParcel();
}

Parcel mockedParcel;
int position;
List<Object> objects;

public Parcel getMockedParcel() {
return mockedParcel;
}

public MockParcel() {
mockedParcel = mock(Parcel.class);
objects = new ArrayList<>();
setupMock();
}

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;
}
};

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));
}

private void setupReads() {
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.readParcelableArray(LatLng.class.getClassLoader())).thenAnswer(new Answer<LatLng[]>() {
@Override
public LatLng[] answer(InvocationOnMock invocation) throws Throwable {
int size = (Integer) objects.get(position++);
LatLng[] latLngs = LatLng.CREATOR.newArray(size);
for (int i = 0; i < size; i++) {
latLngs[i] = (LatLng) objects.get(position++);
}
return latLngs;
}
});
}

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());
}

}

0 comments on commit fa21051

Please sign in to comment.