[android] Minimal Android support for 'format' expression
- No dedicated support for creating format expressions - Java accessors for 'text-field' flatten back to String - 'text-field' setter implicitly creates a 'format' expression. For tests, use JsonArray to build an equivalent format expression by hand.
This commit is contained in:
@@ -13,6 +13,8 @@ import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.*;
|
||||
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
|
||||
@@ -477,7 +479,18 @@ public class SymbolLayerTest extends BaseActivityTest {
|
||||
assertEquals((String) layer.getTextField().getValue(), (String) "");
|
||||
|
||||
layer.setProperties(textField("{token}"));
|
||||
assertEquals(layer.getTextField().getExpression(), Expression.toString(Expression.get("token")));
|
||||
JsonArray formatExpression = new JsonArray();
|
||||
formatExpression.add("format");
|
||||
JsonArray getExpression = new JsonArray();
|
||||
getExpression.add("get");
|
||||
getExpression.add("token");
|
||||
JsonArray stringCoercion = new JsonArray();
|
||||
stringCoercion.add("to-string");
|
||||
stringCoercion.add(getExpression);
|
||||
formatExpression.add(stringCoercion);
|
||||
formatExpression.add(new JsonObject());
|
||||
assertEquals(layer.getTextField().getExpression(),
|
||||
Converter.convert(formatExpression));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -490,7 +503,17 @@ public class SymbolLayerTest extends BaseActivityTest {
|
||||
assertNotNull(layer);
|
||||
|
||||
// Set and Get
|
||||
Expression expression = string(Expression.get("undefined"));
|
||||
JsonArray formatExpression = new JsonArray();
|
||||
formatExpression.add("format");
|
||||
JsonArray getExpression = new JsonArray();
|
||||
getExpression.add("get");
|
||||
getExpression.add("undefined");
|
||||
JsonArray stringAssertion = new JsonArray();
|
||||
stringAssertion.add("string");
|
||||
stringAssertion.add(getExpression);
|
||||
formatExpression.add(stringAssertion);
|
||||
formatExpression.add(new JsonObject());
|
||||
Expression expression = Converter.convert(formatExpression);
|
||||
layer.setProperties(textField(expression));
|
||||
assertEquals(layer.getTextField().getExpression(), expression);
|
||||
});
|
||||
|
||||
@@ -17,6 +17,10 @@ import com.mapbox.mapboxsdk.testapp.activity.BaseActivityTest;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
<% if (type === 'symbol') { -%>
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
<% } -%>
|
||||
|
||||
import static com.mapbox.mapboxsdk.style.expressions.Expression.*;
|
||||
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
|
||||
@@ -165,7 +169,22 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
|
||||
<% if (property.tokens) { -%>
|
||||
|
||||
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>("{token}"));
|
||||
<% if (property.type === 'formatted') { -%>
|
||||
JsonArray formatExpression = new JsonArray();
|
||||
formatExpression.add("format");
|
||||
JsonArray getExpression = new JsonArray();
|
||||
getExpression.add("get");
|
||||
getExpression.add("token");
|
||||
JsonArray stringCoercion = new JsonArray();
|
||||
stringCoercion.add("to-string");
|
||||
stringCoercion.add(getExpression);
|
||||
formatExpression.add(stringCoercion);
|
||||
formatExpression.add(new JsonObject());
|
||||
assertEquals(layer.getTextField().getExpression(),
|
||||
Converter.convert(formatExpression));
|
||||
<% } else { -%>
|
||||
assertEquals(layer.get<%- camelize(property.name) %>().getExpression(), Expression.toString(Expression.get("token")));
|
||||
<% } -%>
|
||||
<% } -%>
|
||||
});
|
||||
}
|
||||
@@ -181,7 +200,21 @@ public class <%- camelize(type) %>LayerTest extends BaseActivityTest {
|
||||
assertNotNull(layer);
|
||||
|
||||
// Set and Get
|
||||
<% if (property.type === 'formatted') { -%>
|
||||
JsonArray formatExpression = new JsonArray();
|
||||
formatExpression.add("format");
|
||||
JsonArray getExpression = new JsonArray();
|
||||
getExpression.add("get");
|
||||
getExpression.add("undefined");
|
||||
JsonArray stringAssertion = new JsonArray();
|
||||
stringAssertion.add("string");
|
||||
stringAssertion.add(getExpression);
|
||||
formatExpression.add(stringAssertion);
|
||||
formatExpression.add(new JsonObject());
|
||||
Expression expression = Converter.convert(formatExpression);
|
||||
<% } else { -%>
|
||||
Expression expression = <%- defaultExpressionJava(property) %>(Expression.get("undefined"));
|
||||
<% } -%>
|
||||
layer.setProperties(<%- camelizeWithLeadingLowercase(property.name) %>(expression));
|
||||
assertEquals(layer.get<%- camelize(property.name) %>().getExpression(), expression);
|
||||
});
|
||||
|
||||
@@ -29,6 +29,10 @@ Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, Color>::o
|
||||
return jni::Make<jni::String>(env, sstream.str());
|
||||
}
|
||||
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, style::expression::Formatted>::operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const {
|
||||
return jni::Make<jni::String>(env, value.toString());
|
||||
}
|
||||
|
||||
Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, std::vector<std::string>>::operator()(jni::JNIEnv& env, const std::vector<std::string>& value) const {
|
||||
auto result = jni::Array<jni::String>::New(env, value.size());
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <mbgl/util/color.hpp>
|
||||
#include <mbgl/util/enum.hpp>
|
||||
|
||||
#include <mbgl/style/expression/formatted.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
|
||||
#include <string>
|
||||
@@ -53,6 +55,11 @@ struct Converter<jni::Local<jni::Object<>>, Color> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const Color& value) const;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<jni::Local<jni::Object<>>, style::expression::Formatted> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const;
|
||||
};
|
||||
|
||||
template <std::size_t N>
|
||||
struct Converter<jni::Local<jni::Object<>>, std::array<float, N>> {
|
||||
Result<jni::Local<jni::Object<>>> operator()(jni::JNIEnv& env, const std::array<float, N>& value) const {
|
||||
|
||||
Reference in New Issue
Block a user