This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] #3830 added mocked parcel implementation
- Loading branch information
Showing
2 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...ndroid/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/utils/MockParcel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |