summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2020-02-25 10:48:17 +0200
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2020-02-26 18:15:23 +0200
commitfb5be4c80cca408b4a30e966a4e040e0c214588d (patch)
tree2c397331b56ffe560cea582d1c65d90b01b4864c /test
parent02a1a311364044820f3da096d8a6f31c8e7fc647 (diff)
downloadqtlocation-mapboxgl-fb5be4c80cca408b4a30e966a4e040e0c214588d.tar.gz
[core] Add unit test for layer serialization
Diffstat (limited to 'test')
-rw-r--r--test/style/conversion/layer.test.cpp96
1 files changed, 95 insertions, 1 deletions
diff --git a/test/style/conversion/layer.test.cpp b/test/style/conversion/layer.test.cpp
index d65644b91b..aae5d3495c 100644
--- a/test/style/conversion/layer.test.cpp
+++ b/test/style/conversion/layer.test.cpp
@@ -4,6 +4,8 @@
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/layers/background_layer_impl.hpp>
+#include <rapidjson/prettywriter.h>
+
using namespace mbgl;
using namespace mbgl::style;
using namespace mbgl::style::conversion;
@@ -11,7 +13,17 @@ using namespace std::literals::chrono_literals;
std::unique_ptr<Layer> parseLayer(const std::string& src) {
Error error;
- return std::move(*convertJSON<std::unique_ptr<Layer>>(src, error));
+ auto layer = convertJSON<std::unique_ptr<Layer>>(src, error);
+ if (layer) return std::move(*layer);
+ return nullptr;
+}
+
+std::string stringifyLayer(const Value& value) {
+ rapidjson::StringBuffer s;
+ rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(s);
+ writer.SetIndent(' ', 2u);
+ stringify(writer, value);
+ return s.GetString();
}
TEST(StyleConversion, LayerTransition) {
@@ -31,3 +43,85 @@ TEST(StyleConversion, LayerTransition) {
ASSERT_EQ(500ms, *static_cast<BackgroundLayer*>(layer.get())->impl().paint
.get<BackgroundColor>().options.delay);
}
+
+TEST(StyleConversion, SerializeDefaults) {
+ auto layer = parseLayer(R"JSON({
+ "type": "background",
+ "id": "background"
+ })JSON");
+
+ EXPECT_NE(nullptr, layer);
+ auto value = layer->serialize();
+ EXPECT_NE(nullptr, value.getObject());
+ EXPECT_EQ(2u, value.getObject()->size());
+}
+
+TEST(StyleConversion, RoundtripWithTransitions) {
+ auto layer = parseLayer(R"JSON({
+ "type": "background",
+ "id": "background",
+ "paint": {
+ "background-color-transition": {
+ "duration": 400,
+ "delay": 500
+ }
+ }
+ })JSON");
+
+ EXPECT_NE(nullptr, layer);
+ auto value = layer->serialize();
+ EXPECT_NE(nullptr, value.getObject());
+ EXPECT_EQ(3u, value.getObject()->size());
+
+ auto roundTripped = parseLayer(stringifyLayer(value));
+ EXPECT_NE(nullptr, roundTripped);
+ auto roundTrippedValue = roundTripped->serialize();
+ EXPECT_NE(nullptr, roundTrippedValue.getObject());
+ EXPECT_EQ(3u, roundTrippedValue.getObject()->size());
+}
+
+TEST(StyleConversion, OverrideDefaults) {
+ auto layer = parseLayer(R"JSON({
+ "type": "symbol",
+ "id": "symbol",
+ "source": "composite",
+ "source-layer": "landmarks",
+ "filter": ["has", "monuments"],
+ "minzoom": 12,
+ "maxzoom": 18,
+ "layout": {
+ "visibility": "none",
+ "text-field": ["format",
+ "Hello",
+ ["image", ["get", "world"]],
+ "Example", {"text-color": "rgba(128, 255, 39, 1)"}
+ ],
+ "icon-image": ["image", ["get", "landmark_image"]],
+ "text-size": 24
+ },
+ "paint": {
+ "text-color": "turquoise",
+ "text-color-transition": {
+ "duration": 300,
+ "delay": 100
+ }
+ }
+ })JSON");
+
+ EXPECT_NE(nullptr, layer);
+ auto value = layer->serialize();
+ EXPECT_NE(nullptr, value.getObject());
+ const auto& object = *(value.getObject());
+ EXPECT_EQ(9u, object.size());
+ EXPECT_EQ(4u, object.at("layout").getObject()->size());
+ EXPECT_EQ(2u, object.at("paint").getObject()->size());
+
+ auto roundTripped = parseLayer(stringifyLayer(value));
+ EXPECT_NE(nullptr, roundTripped);
+ auto roundTrippedValue = roundTripped->serialize();
+ const auto& roundTrippedObject = *(roundTrippedValue.getObject());
+ EXPECT_NE(nullptr, roundTrippedValue.getObject());
+ EXPECT_EQ(9u, roundTrippedObject.size());
+ EXPECT_EQ(4u, roundTrippedObject.at("layout").getObject()->size());
+ EXPECT_EQ(2u, roundTrippedObject.at("paint").getObject()->size());
+}