[android] geojson source - add conversion tests

This commit is contained in:
Ivo van Dongen
2017-03-23 17:54:22 +02:00
committed by Ivo van Dongen
parent 02fd38ccbc
commit 6fa578aa4e
10 changed files with 457 additions and 0 deletions

View File

@@ -0,0 +1,215 @@
package com.mapbox.mapboxsdk.testapp.style;
import android.content.res.Resources;
import android.support.annotation.RawRes;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.Layer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity;
import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource;
import com.mapbox.mapboxsdk.testapp.utils.ViewUtils;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.Point;
import org.hamcrest.Matcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.junit.Assert.fail;
/**
* Tests for {@link GeoJsonSource}
*/
@RunWith(AndroidJUnit4.class)
public class GeoJsonSourceTests {
@Rule
public final ActivityTestRule<RuntimeStyleTestActivity> rule =
new ActivityTestRule<>(RuntimeStyleTestActivity.class);
private OnMapReadyIdlingResource idlingResource;
@Before
public void registerIdlingResource() {
idlingResource = new OnMapReadyIdlingResource(rule.getActivity());
Espresso.registerIdlingResources(idlingResource);
}
@After
public void unregisterIntentServiceIdlingResource() {
Espresso.unregisterIdlingResources(idlingResource);
}
@Test
public void testFeatureCollection() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new BaseViewAction() {
@Override
public void perform(UiController uiController, View view) {
MapboxMap mapboxMap = rule.getActivity().getMapboxMap();
GeoJsonSource source = new GeoJsonSource("source", FeatureCollection
.fromJson(readRawResource(rule.getActivity().getResources(), R.raw.test_feature_collection)));
mapboxMap.addSource(source);
mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
}
});
}
@Test
public void testPointGeometry() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new BaseViewAction() {
@Override
public void perform(UiController uiController, View view) {
MapboxMap mapboxMap = rule.getActivity().getMapboxMap();
GeoJsonSource source = new GeoJsonSource("source", Point.fromCoordinates(new double[] {0d, 0d}));
mapboxMap.addSource(source);
mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
}
});
}
@Test
public void testFeatureProperties() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new BaseViewAction() {
@Override
public void perform(UiController uiController, View view) {
MapboxMap mapboxMap = rule.getActivity().getMapboxMap();
GeoJsonSource source = new GeoJsonSource("source",
readRawResource(rule.getActivity().getResources(), R.raw.test_feature_properties));
mapboxMap.addSource(source);
mapboxMap.addLayer(new CircleLayer("layer", source.getId()));
}
});
}
@Test
public void testPointFeature() {
testFeatureFromResource(R.raw.test_point_feature);
}
@Test
public void testLineStringFeature() {
testFeatureFromResource(R.raw.test_line_string_feature);
}
@Test
public void testPolygonFeature() {
testFeatureFromResource(R.raw.test_polygon_feature);
}
@Test
public void testPolygonWithHoleFeature() {
testFeatureFromResource(R.raw.test_polygon_with_hole_feature);
}
@Test
public void testMultiPointFeature() {
testFeatureFromResource(R.raw.test_multi_point_feature);
}
@Test
public void testMultiLineStringFeature() {
testFeatureFromResource(R.raw.test_multi_line_string_feature);
}
@Test
public void testMultiPolygonFeature() {
testFeatureFromResource(R.raw.test_multi_polygon_feature);
}
protected void testFeatureFromResource(final @RawRes int resource) {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new BaseViewAction() {
@Override
public void perform(UiController uiController, View view) {
MapboxMap mapboxMap = rule.getActivity().getMapboxMap();
GeoJsonSource source = new GeoJsonSource("source");
mapboxMap.addSource(source);
Layer layer = new CircleLayer("layer", source.getId());
mapboxMap.addLayer(layer);
source.setGeoJson(Feature.fromJson(
readRawResource(rule.getActivity().getResources(), resource)));
mapboxMap.removeLayer(layer);
mapboxMap.removeSource(source);
}
});
}
private String readRawResource(Resources resources, @RawRes int rawResource) {
InputStream is = resources.openRawResource(rawResource);
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
try {
Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
} finally {
is.close();
}
} catch (IOException err) {
fail(err.getMessage());
}
return writer.toString();
}
public abstract class BaseViewAction implements ViewAction {
@Override
public Matcher<View> getConstraints() {
return isDisplayed();
}
@Override
public String getDescription() {
return getClass().getSimpleName();
}
}
}

View File

@@ -0,0 +1,26 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
5.1080,
52.0962
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
5.1090,
52.0962
]
}
}
]
}

View File

@@ -0,0 +1,21 @@
{
"type": "Feature",
"id": 1,
"properties": {
"null_prop": null,
"integer_prop": 10000,
"float_prop": 10000.10,
"string_prop": "my_string",
"bool_prop": true,
"object_prop": {
"nested_string_prop": "my_string"
}
},
"geometry": {
"type": "Point",
"coordinates": [
5.112419128417969,
52.09622422366772
]
}
}

View File

@@ -0,0 +1,17 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
5.1080,
52.0960
],
[
5.1080,
52.0970
]
]
}
}

View File

@@ -0,0 +1,29 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
5.1080,
52.0960
],
[
5.1080,
52.0970
]
],
[
[
5.1090,
52.0960
],
[
5.1090,
52.0970
]
]
]
}
}

View File

@@ -0,0 +1,17 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
5.1080,
52.0960
],
[
5.1080,
52.0970
]
]
}
}

View File

@@ -0,0 +1,49 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
5.112419128417969,
52.09622422366772
],
[
5.112419128417969,
52.094062282906954
],
[
5.115251541137695,
52.094747787662364
],
[
5.112419128417969,
52.09622422366772
]
]
],
[
[
[
5.11662483215332,
52.09485324899753
],
[
5.11662483215332,
52.09306037239377
],
[
5.120058059692383,
52.093376767618174
],
[
5.11662483215332,
52.09485324899753
]
]
]
]
}
}

View File

@@ -0,0 +1,11 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
5.1080,
52.0962
]
}
}

View File

@@ -0,0 +1,27 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.112419128417969,
52.09622422366772
],
[
5.112419128417969,
52.094062282906954
],
[
5.115251541137695,
52.094747787662364
],
[
5.112419128417969,
52.09622422366772
]
]
]
}
}

View File

@@ -0,0 +1,45 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.112419128417969,
52.09622422366772
],
[
5.112419128417969,
52.094062282906954
],
[
5.115251541137695,
52.094747787662364
],
[
5.112419128417969,
52.09622422366772
]
],
[
[
5.1127249002456665,
52.094362192533545
],
[
5.114580988883972,
52.0948104053602
],
[
5.11263906955719,
52.095845232479846
],
[
5.1127249002456665,
52.094362192533545
]
]
]
}
}