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

Commit

Permalink
[android] - migrate tests from #13882
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Feb 7, 2019
1 parent 7ad9f34 commit a3a6016
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.mapbox.mapboxsdk.testapp.geometry;

import com.google.gson.JsonArray;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.sources.GeometryTileProvider;
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest;
import com.mapbox.mapboxsdk.testapp.utils.Utils;

import org.junit.Test;

import static com.mapbox.geojson.Feature.fromGeometry;
Expand All @@ -18,6 +26,7 @@
import static com.mapbox.geojson.MultiPolygon.fromPolygon;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertFalse;

/**
* Instrumentation test to validate java geojson conversion to c++
Expand All @@ -44,7 +53,7 @@ public void testPointFeatureCollection() {
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
mapboxMap.getStyle().addSource(
new CustomGeometrySource("test-id",
new CustomProvider(fromFeatures(singletonList(fromGeometry(Point.fromLngLat(0.0,0.0)))))
new CustomProvider(fromFeatures(singletonList(fromGeometry(Point.fromLngLat(0.0, 0.0)))))
)
);
mapboxMap.getStyle().addLayer(new SymbolLayer("test-id", "test-id"));
Expand Down Expand Up @@ -117,6 +126,34 @@ public void testMultiLineStringFeatureCollection() {
}));
}

@Test
public void testNegativeNumberPropertyConversion() {
validateTestSetup();
onMapView().perform(getMapboxMapAction((uiController, mapboxMap) -> {
LatLng latLng = new LatLng();
Feature feature = Feature.fromGeometry(Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()));

JsonArray foregroundJsonArray = new JsonArray();
foregroundJsonArray.add(0f);
foregroundJsonArray.add(-3f);
feature.addProperty("property", foregroundJsonArray);

GeoJsonSource source = new GeoJsonSource("source", feature);
mapboxMap.getStyle().addSource(source);

SymbolLayer layer = new SymbolLayer("layer", "source")
.withProperties(
PropertyFactory.iconOffset(Expression.get("property")),
PropertyFactory.iconImage("zoo-15")
);
mapboxMap.getStyle().addLayer(layer);

Utils.waitForLayer(uiController, mapboxMap, latLng, "layer");

assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng)).isEmpty());
}));
}

class CustomProvider implements GeometryTileProvider {

private FeatureCollection featureCollection;
Expand All @@ -130,4 +167,4 @@ public FeatureCollection getFeaturesForBounds(LatLngBounds bounds, int zoom) {
return featureCollection;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.mapbox.mapboxsdk.testapp.style;

import android.graphics.Color;
import android.support.test.espresso.UiController;
import android.support.test.runner.AndroidJUnit4;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Point;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.style.expressions.Expression;
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
import com.mapbox.mapboxsdk.style.layers.FillLayer;
Expand Down Expand Up @@ -55,6 +53,7 @@
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOutlineColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField;
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
import static com.mapbox.mapboxsdk.testapp.utils.Utils.waitForLayer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -291,7 +290,7 @@ public void testConstFormatExpression() {
formatEntry("test")
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
.isEmpty());

Expand All @@ -314,7 +313,7 @@ public void testConstFormatExpressionFontScaleParam() {
formatEntry("test", formatFontScale(1.75))
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
.isEmpty());

Expand All @@ -340,7 +339,7 @@ public void testConstFormatExpressionTextFontParam() {
)
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(
mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
);
Expand Down Expand Up @@ -371,7 +370,7 @@ public void testConstFormatExpressionAllParams() {
)
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(
mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
);
Expand Down Expand Up @@ -404,7 +403,7 @@ public void testConstFormatExpressionMultipleInputs() {
formatEntry("\ntest2", formatFontScale(2))
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(
mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
);
Expand Down Expand Up @@ -438,7 +437,7 @@ public void testVariableFormatExpression() {
)
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
.isEmpty());

Expand Down Expand Up @@ -468,7 +467,7 @@ public void testVariableFormatExpressionMultipleInputs() {
formatEntry("\ntest2", formatFontScale(2))
);
layer.setProperties(textField(expression));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
.isEmpty());

Expand All @@ -488,7 +487,7 @@ public void testFormatExpressionPlainTextCoercion() {
mapboxMap.getStyle().addLayer(layer);

layer.setProperties(textField("test"));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
.isEmpty());

Expand All @@ -513,7 +512,7 @@ public void testTextFieldFormattedArgument() {
new FormattedSection("\ntest", 0.5, new String[] {"Arial Unicode MS Regular", "DIN Offc Pro Regular"})
);
layer.setProperties(textField(formatted));
waitForLayer(uiController, mapboxMap, latLng);
waitForLayer(uiController, mapboxMap, latLng, "layer");
assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer")
.isEmpty());

Expand All @@ -522,18 +521,6 @@ public void testTextFieldFormattedArgument() {
});
}

private static final long WAIT_TIMEOUT = 5000;
private static final long WAIT_DELAY = 150;

private static void waitForLayer(UiController uiController, MapboxMap mapboxMap, LatLng latLng) {
int i = 0;
while (mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()) {
i++;
assertFalse("Waiting for layer timed out", i * WAIT_DELAY > WAIT_TIMEOUT);
uiController.loopMainThreadForAtLeast(WAIT_DELAY);
}
}

private void setupStyle() {
invoke(mapboxMap, (uiController, mapboxMap) -> {
// Add a source
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mapbox.mapboxsdk.testapp.utils;

import android.support.test.espresso.UiController;

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;

import static org.junit.Assert.assertFalse;

public class Utils {

private static final long WAIT_TIMEOUT = 5000;
private static final long WAIT_DELAY = 150;

public static void waitForLayer(UiController uiController, MapboxMap mapboxMap, LatLng latLng, String... layerIds) {
int i = 0;
while (mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), layerIds).isEmpty()) {
i++;
assertFalse("Waiting for layer timed out", i * WAIT_DELAY > WAIT_TIMEOUT);
uiController.loopMainThreadForAtLeast(WAIT_DELAY);
}
}
}

0 comments on commit a3a6016

Please sign in to comment.