Add number format expression (#15424)
* [android] add number format expression
This commit is contained in:
@@ -25,12 +25,17 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatFontScale;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextColor;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextFont;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.currency;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.locale;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.maxFractionDigits;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.minFractionDigits;
|
||||
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;
|
||||
@@ -41,6 +46,7 @@ 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.numberFormat;
|
||||
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;
|
||||
@@ -56,8 +62,10 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillOutlineColor
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textColor;
|
||||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textField;
|
||||
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
@@ -561,6 +569,169 @@ public class ExpressionTest extends EspressoTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberFormatCurrencyExpression() {
|
||||
validateTestSetup();
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
LatLng latLng = new LatLng(51, 17);
|
||||
mapboxMap.getStyle()
|
||||
.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())));
|
||||
SymbolLayer layer = new SymbolLayer("layer", "source");
|
||||
mapboxMap.getStyle().addLayer(layer);
|
||||
|
||||
layer.setProperties(
|
||||
textField(
|
||||
numberFormat(12.345, locale("en-US"), currency("USD"))
|
||||
)
|
||||
);
|
||||
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
|
||||
|
||||
assertFalse(mapboxMap.queryRenderedFeatures(
|
||||
mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
|
||||
);
|
||||
assertNull(layer.getTextField().getExpression());
|
||||
assertEquals("$12.35", layer.getTextField().getValue().getFormattedSections()[0].getText());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberFormatMaxExpression() {
|
||||
validateTestSetup();
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
LatLng latLng = new LatLng(51, 17);
|
||||
mapboxMap.getStyle()
|
||||
.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())));
|
||||
SymbolLayer layer = new SymbolLayer("layer", "source");
|
||||
mapboxMap.getStyle().addLayer(layer);
|
||||
|
||||
layer.setProperties(
|
||||
textField(
|
||||
numberFormat(12.34567890, maxFractionDigits(5), minFractionDigits(0))
|
||||
)
|
||||
);
|
||||
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
|
||||
|
||||
assertFalse(mapboxMap.queryRenderedFeatures(
|
||||
mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
|
||||
);
|
||||
assertNull(layer.getTextField().getExpression());
|
||||
assertEquals("12.34568", layer.getTextField().getValue().getFormattedSections()[0].getText());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberFormatMinExpression() {
|
||||
validateTestSetup();
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
LatLng latLng = new LatLng(51, 17);
|
||||
mapboxMap.getStyle()
|
||||
.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())));
|
||||
SymbolLayer layer = new SymbolLayer("layer", "source");
|
||||
mapboxMap.getStyle().addLayer(layer);
|
||||
|
||||
layer.setProperties(
|
||||
textField(
|
||||
numberFormat(12.0000001, maxFractionDigits(5), minFractionDigits(0))
|
||||
)
|
||||
);
|
||||
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
|
||||
|
||||
assertFalse(mapboxMap.queryRenderedFeatures(
|
||||
mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
|
||||
);
|
||||
assertNull(layer.getTextField().getExpression());
|
||||
assertEquals("12", layer.getTextField().getValue().getFormattedSections()[0].getText());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberFormatLocaleExpression() {
|
||||
validateTestSetup();
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
LatLng latLng = new LatLng(51, 17);
|
||||
mapboxMap.getStyle()
|
||||
.addSource(new GeoJsonSource("source", Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude())));
|
||||
SymbolLayer layer = new SymbolLayer("layer", "source");
|
||||
mapboxMap.getStyle().addLayer(layer);
|
||||
|
||||
layer.setProperties(
|
||||
textField(
|
||||
numberFormat(12.0000001, locale("nl-BE"), maxFractionDigits(5), minFractionDigits(1))
|
||||
)
|
||||
);
|
||||
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
|
||||
|
||||
assertFalse(mapboxMap.queryRenderedFeatures(
|
||||
mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
|
||||
);
|
||||
assertNull(layer.getTextField().getExpression());
|
||||
assertEquals("12,0", layer.getTextField().getValue().getFormattedSections()[0].getText());
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberFormatNonConstantExpression() {
|
||||
validateTestSetup();
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
LatLng latLng = new LatLng(51, 17);
|
||||
Feature feature = Feature.fromGeometry(Point.fromLngLat(latLng.getLongitude(), latLng.getLatitude()));
|
||||
feature.addNumberProperty("number_value", 12.345678);
|
||||
feature.addStringProperty("locale_value", "nl-BE");
|
||||
feature.addNumberProperty("max_value", 5);
|
||||
feature.addNumberProperty("min_value", 1);
|
||||
|
||||
|
||||
mapboxMap.getStyle().addSource(new GeoJsonSource("source", feature));
|
||||
SymbolLayer layer = new SymbolLayer("layer", "source");
|
||||
mapboxMap.getStyle().addLayer(layer);
|
||||
|
||||
Expression numberFormatExpression = numberFormat(
|
||||
number(number(get("number_value"))),
|
||||
locale(string(get("locale_value"))),
|
||||
maxFractionDigits(number(get("max_value"))),
|
||||
minFractionDigits(number(get("min_value")))
|
||||
);
|
||||
|
||||
layer.setProperties(textField(numberFormatExpression));
|
||||
TestingAsyncUtils.INSTANCE.waitForLayer(uiController, mapView);
|
||||
|
||||
assertFalse(mapboxMap.queryRenderedFeatures(
|
||||
mapboxMap.getProjection().toScreenLocation(latLng), "layer").isEmpty()
|
||||
);
|
||||
|
||||
assertNotNull(layer.getTextField().getExpression());
|
||||
|
||||
// Expressions evaluated to string are wrapped by a format expression, take array index 1 to get original
|
||||
Object[] returnExpression = (Object[]) layer.getTextField().getExpression().toArray()[1];
|
||||
Object[] setExpression = numberFormatExpression.toArray();
|
||||
assertEquals("Number format should match",returnExpression[0], setExpression[0]);
|
||||
assertArrayEquals("Get value expression should match",
|
||||
(Object[]) returnExpression[1],
|
||||
(Object[]) setExpression[1]
|
||||
);
|
||||
|
||||
// number format objects
|
||||
HashMap<String, Object> returnMap = (HashMap<String, Object>) returnExpression[2];
|
||||
HashMap<String, Object> setMap = (HashMap<String, Object>) returnExpression[2];
|
||||
|
||||
assertArrayEquals("Number format min fraction digits should match ",
|
||||
(Object[]) returnMap.get("min-fraction-digits"),
|
||||
(Object[]) setMap.get("min-fraction-digits")
|
||||
);
|
||||
|
||||
assertArrayEquals("Number format max fraction digits should match ",
|
||||
(Object[]) returnMap.get("max-fraction-digits"),
|
||||
(Object[]) setMap.get("max-fraction-digits")
|
||||
);
|
||||
|
||||
assertArrayEquals("Number format min fraction digits should match ",
|
||||
(Object[]) returnMap.get("locale"),
|
||||
(Object[]) setMap.get("locale")
|
||||
);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private void setupStyle() {
|
||||
invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
// Add a source
|
||||
|
||||
@@ -38,12 +38,15 @@ import java.util.Random;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatFontScale;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextColor;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextFont;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.currency;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.NumberFormatOption.locale;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.concat;
|
||||
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.literal;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.match;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.numberFormat;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.rgb;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.stop;
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.switchCase;
|
||||
@@ -72,8 +75,6 @@ import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.textSize;
|
||||
*/
|
||||
public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener, OnMapReadyCallback {
|
||||
|
||||
private static final String MARKER_SOURCE = "marker-source";
|
||||
private static final String MARKER_LAYER = "marker-layer";
|
||||
private static final String ID_FEATURE_PROPERTY = "id";
|
||||
private static final String SELECTED_FEATURE_PROPERTY = "selected";
|
||||
private static final String TITLE_FEATURE_PROPERTY = "title";
|
||||
@@ -81,8 +82,13 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
private static final String[] NORMAL_FONT_STACK = new String[] {"DIN Offc Pro Regular", "Arial Unicode MS Regular"};
|
||||
private static final String[] BOLD_FONT_STACK = new String[] {"DIN Offc Pro Bold", "Arial Unicode MS Regular"};
|
||||
|
||||
// layer & source constants
|
||||
private static final String MARKER_SOURCE = "marker-source";
|
||||
private static final String MARKER_LAYER = "marker-layer";
|
||||
private static final String MAPBOX_SIGN_SOURCE = "mapbox-sign-source";
|
||||
private static final String MAPBOX_SIGN_LAYER = "mapbox-sign-layer";
|
||||
private static final String NUMBER_FORMAT_SOURCE = "mapbox-number-source";
|
||||
private static final String NUMBER_FORMAT_LAYER = "mapbox-number-layer";
|
||||
|
||||
private static final Expression TEXT_FIELD_EXPRESSION =
|
||||
switchCase(toBool(get(SELECTED_FEATURE_PROPERTY)),
|
||||
@@ -108,6 +114,7 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
private FeatureCollection markerCollection;
|
||||
private SymbolLayer markerSymbolLayer;
|
||||
private SymbolLayer mapboxSignSymbolLayer;
|
||||
private SymbolLayer numberFormatSymbolLayer;
|
||||
private MapboxMap mapboxMap;
|
||||
private MapView mapView;
|
||||
|
||||
@@ -177,11 +184,20 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
mapboxSignSymbolLayer = new SymbolLayer(MAPBOX_SIGN_LAYER, MAPBOX_SIGN_SOURCE);
|
||||
shuffleMapboxSign();
|
||||
|
||||
// number format layer
|
||||
Source numberFormatSource = new GeoJsonSource(NUMBER_FORMAT_SOURCE, Point.fromLngLat(4.92756, 52.3516));
|
||||
numberFormatSymbolLayer = new SymbolLayer(NUMBER_FORMAT_LAYER, NUMBER_FORMAT_SOURCE);
|
||||
numberFormatSymbolLayer.setProperties(
|
||||
textField(
|
||||
numberFormat(123.456789, locale("nl-NL"), currency("EUR"))
|
||||
)
|
||||
);
|
||||
|
||||
mapboxMap.setStyle(new Style.Builder()
|
||||
.fromUri("asset://streets.json")
|
||||
.withImage("Car", Objects.requireNonNull(carBitmap), false)
|
||||
.withSources(markerSource, mapboxSignSource)
|
||||
.withLayers(markerSymbolLayer, mapboxSignSymbolLayer)
|
||||
.withSources(markerSource, mapboxSignSource, numberFormatSource)
|
||||
.withLayers(markerSymbolLayer, mapboxSignSymbolLayer, numberFormatSymbolLayer)
|
||||
);
|
||||
|
||||
// Set a click-listener so we can manipulate the map
|
||||
@@ -205,7 +221,7 @@ public class SymbolLayerActivity extends AppCompatActivity implements MapboxMap.
|
||||
// validate symbol flicker regression for #13407
|
||||
markerSymbolLayer.setProperties(iconOpacity(match(
|
||||
get(ID_FEATURE_PROPERTY), literal(1.0f),
|
||||
stop(feature.getStringProperty("id"), selected ? 0.3f : 1.0f)
|
||||
stop(feature.getStringProperty("id"), selected ? 0.3f : 1.0f)
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user