summaryrefslogtreecommitdiff
path: root/test/style/conversion/source.test.cpp
blob: f969384746ea4581d82f3058b08d1dfb62a1b0ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <mbgl/test/util.hpp>

#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion/source.hpp>
#include <mbgl/style/sources/geojson_source.hpp>

using namespace mbgl;
using namespace mbgl::style;
using namespace mbgl::style::conversion;
namespace {

std::unique_ptr<Source> parseSource(const std::string& src, const std::string& sourceName) {
    Error error;
    auto source = convertJSON<std::unique_ptr<mbgl::style::Source>>(src, error, sourceName);
    if (source) return std::move(*source);
    return nullptr;
}

void checkGetProperty(std::unique_ptr<Source>& source, const std::string& propertyName, const mbgl::Value& expected) {
    Value value = source->getProperty(propertyName);
    EXPECT_EQ(expected, value) << "get property: " << propertyName;
}

void checkGetPropertyDefaultValue(std::unique_ptr<Source>& source,
                                  const std::string& propertyName,
                                  const mbgl::Value& expected) {
    Value value = source->getPropertyDefaultValue(propertyName);
    EXPECT_EQ(expected, value) << "get property: " << propertyName;
}

void checkSetProperty(std::unique_ptr<Source>& source, const std::string& propertyName, const JSValue& value) {
    auto error = source->setProperty(propertyName, Convertible(&value));
    EXPECT_EQ(nullopt, error) << "set property: " << propertyName << ", error: " << error->message;
}

} // namespace

TEST(StyleConversion, SetSourceGenericProperties) {
    auto source = parseSource(R"JSON({
        "type": "vector",
        "tiles": ["http://example.com/{z}-{x}-{y}.vector.pbf"],
        "scheme": "xyz",
        "minzoom": 11,
        "maxzoom": 16,
        "attribution": "mapbox",
        "bounds": [-180, -73, -120, 73]
    })JSON",
                              "vector_source");

    ASSERT_NE(nullptr, source);
    checkGetProperty(source, "volatile", false);
    checkSetProperty(source, "volatile", JSValue(true));
    checkGetProperty(source, "volatile", true);

    checkGetProperty(source, "minimum-tile-update-interval", 0.0);
    checkSetProperty(source, "minimum-tile-update-interval", JSValue(10.5));
    checkGetProperty(source, "minimum-tile-update-interval", 10.5);

    checkGetProperty(source, "prefetch-zoom-delta", NullValue());
    checkSetProperty(source, "prefetch-zoom-delta", JSValue(0));
    checkGetProperty(source, "prefetch-zoom-delta", 0u);

    checkGetProperty(source, "max-overscale-factor-for-parent-tiles", NullValue());
    checkSetProperty(source, "max-overscale-factor-for-parent-tiles", JSValue(2));
    checkGetProperty(source, "max-overscale-factor-for-parent-tiles", 2u);

    source = parseSource(R"JSON({
        "type": "geojson",
        "data": "http://127.0.0.1:3000/geojson.json"
    })JSON",
                         "geojson_source");
    auto* geojsonSource = static_cast<GeoJSONSource*>(source.get());
    ASSERT_NE(nullptr, source);
    auto url = geojsonSource->getURL();
    ASSERT_TRUE(url);
    EXPECT_EQ("http://127.0.0.1:3000/geojson.json", *url);

    checkSetProperty(source, "data", JSValue("http://127.0.0.1:3000/geojson1.json"));
    url = geojsonSource->getURL();
    ASSERT_TRUE(url);
    EXPECT_EQ("http://127.0.0.1:3000/geojson1.json", *url);

    EXPECT_FALSE(geojsonSource->getGeoJSONData());
    JSDocument document;
    document.Parse<0>(R"({
        "type": "Point",
        "coordinates": [0, 0]
    })");
    const JSValue* geometry = &document;
    checkSetProperty(source, "data", *geometry);
    EXPECT_TRUE(geojsonSource->getGeoJSONData());
}

TEST(StyleConversion, GetSourceGenericPropertyDefaultValues) {
    auto source = parseSource(R"JSON({
        "type": "vector",
        "tiles": ["http://example.com/{z}-{x}-{y}.vector.pbf"],
        "scheme": "tms",
        "minzoom": 11,
        "maxzoom": 16,
        "attribution": "mapbox",
        "bounds": [-180, -73, -120, 73]
    })JSON",
                              "vector_source");

    ASSERT_NE(nullptr, source);
    checkGetPropertyDefaultValue(source, "volatile", false);
    checkGetPropertyDefaultValue(source, "minimum-tile-update-interval", 0.0);
    checkGetPropertyDefaultValue(source, "prefetch-zoom-delta", 4u);
    checkGetPropertyDefaultValue(source, "minzoom", 0u);
    checkGetPropertyDefaultValue(source, "maxzoom", 22u);
    checkGetPropertyDefaultValue(source, "scheme", "xyz");
}