diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java')
-rw-r--r-- | platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java | 262 |
1 files changed, 261 insertions, 1 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java index 4e284cdc14..773baf17eb 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/ExpressionTest.java @@ -1,16 +1,22 @@ 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; import com.mapbox.mapboxsdk.style.layers.Layer; +import com.mapbox.mapboxsdk.style.layers.SymbolLayer; import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; import com.mapbox.mapboxsdk.style.sources.Source; +import com.mapbox.mapboxsdk.style.types.Formatted; +import com.mapbox.mapboxsdk.style.types.FormattedSection; import com.mapbox.mapboxsdk.testapp.R; import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest; import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity; @@ -24,13 +30,18 @@ import java.io.IOException; import timber.log.Timber; +import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.fontScale; +import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.textFont; import static com.mapbox.mapboxsdk.style.expressions.Expression.collator; import static com.mapbox.mapboxsdk.style.expressions.Expression.eq; import static com.mapbox.mapboxsdk.style.expressions.Expression.exponential; +import static com.mapbox.mapboxsdk.style.expressions.Expression.format; +import static com.mapbox.mapboxsdk.style.expressions.Expression.formatEntry; import static com.mapbox.mapboxsdk.style.expressions.Expression.get; import static com.mapbox.mapboxsdk.style.expressions.Expression.interpolate; import static com.mapbox.mapboxsdk.style.expressions.Expression.literal; import static com.mapbox.mapboxsdk.style.expressions.Expression.match; +import static com.mapbox.mapboxsdk.style.expressions.Expression.number; import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb; import static com.mapbox.mapboxsdk.style.expressions.Expression.rgba; import static com.mapbox.mapboxsdk.style.expressions.Expression.step; @@ -43,9 +54,11 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillAntialias; import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; 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 org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; @RunWith(AndroidJUnit4.class) public class ExpressionTest extends BaseActivityTest { @@ -220,7 +233,6 @@ public class ExpressionTest extends BaseActivityTest { @Test public void testCollatorExpression() { validateTestSetup(); - setupStyle(); invoke(mapboxMap, (uiController, mapboxMap) -> { LatLng latLng = new LatLng(51, 17); @@ -265,6 +277,254 @@ public class ExpressionTest extends BaseActivityTest { }); } + @Test + public void testConstFormatExpressionNoParams() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry("test") + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertNull(layer.getTextField().getExpression()); + assertEquals("test", layer.getTextField().getValue()); + + Formatted expected = new Formatted(new FormattedSection[] { + new FormattedSection("test", 1.0f) + }); + assertNull(layer.getFormattedTextField().getExpression()); + assertEquals(expected, layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testConstFormatExpressionFontScaleParam() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry("test", fontScale(1.75)) + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertNull(layer.getTextField().getExpression()); + assertEquals("test", layer.getTextField().getValue()); + + Formatted expected = new Formatted(new FormattedSection[] { + new FormattedSection("test", 1.75f) + }); + assertNull(layer.getFormattedTextField().getExpression()); + assertEquals(expected, layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testConstFormatExpressionTextFontParam() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry( + literal("test"), fontScale(1.0), textFont(new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}) + ) + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertNull(layer.getTextField().getExpression()); + assertEquals("test", layer.getTextField().getValue()); + + Formatted expected = new Formatted(new FormattedSection[] { + new FormattedSection("test", 1.0f, + new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}) + }); + assertNull(layer.getFormattedTextField().getExpression()); + assertEquals(expected, layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testConstFormatExpressionAllParams() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry( + "test", fontScale(0.5), textFont(new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}) + ) + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertNull(layer.getTextField().getExpression()); + assertEquals("test", layer.getTextField().getValue()); + + Formatted expected = new Formatted(new FormattedSection[] { + new FormattedSection("test", 0.5f, new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"})}); + assertNull(layer.getFormattedTextField().getExpression()); + assertEquals(expected, layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testConstFormatExpressionMultipleInputs() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry( + "test", fontScale(1.5), textFont(new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}) + ), + formatEntry("\ntest2", fontScale(2)) + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertNull(layer.getTextField().getExpression()); + assertEquals("test\ntest2", layer.getTextField().getValue()); + + Formatted expected = new Formatted(new FormattedSection[] { + new FormattedSection("test", 1.5f, new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"}), + new FormattedSection("\ntest2", 2f), + }); + assertNull(layer.getFormattedTextField().getExpression()); + assertEquals(expected, layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testVariableFormatExpression() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + Feature feature = Feature.fromGeometry(Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())); + feature.addStringProperty("test_property", "test"); + feature.addNumberProperty("test_property_number", 1.5); + mapboxMap.addSource(new GeoJsonSource("source", feature)); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry( + get("test_property"), + fontScale(number(get("test_property_number"))), + textFont(new String[] {"Arial Unicode MS Regular", "DIN Offc Pro Regular"}) + ) + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertEquals(expression, layer.getTextField().getExpression()); + assertNull(layer.getTextField().getValue()); + + assertEquals(expression, layer.getFormattedTextField().getExpression()); + assertNull(layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testVariableFormatExpressionMultipleInputs() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + Feature feature = Feature.fromGeometry(Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())); + feature.addStringProperty("test_property", "test"); + feature.addNumberProperty("test_property_number", 1.5); + mapboxMap.addSource(new GeoJsonSource("source", feature)); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + Expression expression = format( + formatEntry( + get("test_property"), + fontScale(1.25), + textFont(new String[] {"Arial Unicode MS Regular", "DIN Offc Pro Regular"}) + ), + formatEntry("\ntest2", fontScale(2)) + ); + layer.setProperties(textField(expression)); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertEquals(expression, layer.getTextField().getExpression()); + assertNull(layer.getTextField().getValue()); + + assertEquals(expression, layer.getFormattedTextField().getExpression()); + assertNull(layer.getFormattedTextField().getValue()); + }); + } + + @Test + public void testFormatExpressionPlainTextCoercion() { + validateTestSetup(); + invoke(mapboxMap, (uiController, mapboxMap) -> { + LatLng latLng = new LatLng(51, 17); + mapboxMap.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()))); + SymbolLayer layer = new SymbolLayer("layer", "source"); + mapboxMap.addLayer(layer); + + layer.setProperties(textField("test")); + waitForLayer(uiController, mapboxMap, latLng); + assertFalse(mapboxMap.queryRenderedFeatures(mapboxMap.getProjection().toScreenLocation(latLng), "layer") + .isEmpty()); + + assertNull(layer.getTextField().getExpression()); + assertEquals("test", layer.getTextField().getValue()); + + assertNull(layer.getFormattedTextField().getExpression()); + Formatted expected = new Formatted(new FormattedSection[] {new FormattedSection("test", 1.0f)}); + assertEquals(expected, layer.getFormattedTextField().getValue()); + }); + } + + 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 |