Skip to content

Commit 2310ee9

Browse files
committed
test style parser
1 parent 478c348 commit 2310ee9

File tree

9 files changed

+187
-1
lines changed

9 files changed

+187
-1
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ if(APPLE)
1010
file(GLOB_RECURSE mapscore_SRC
1111
"shared/public/*.cpp"
1212
"shared/src/*.cpp"
13+
"external/djinni/support-lib/cpp/*.cpp"
1314
)
1415
else()
1516
# Include Android-specific files only if not on Apple devices
1617
file(GLOB_RECURSE mapscore_SRC
1718
"shared/public/*.cpp"
1819
"shared/src/*.cpp"
1920
"android/src/main/cpp/graphics/*.cpp"
21+
"external/djinni/support-lib/cpp/*.cpp"
2022
)
2123
endif()
2224

@@ -97,6 +99,8 @@ else()
9799
target_compile_definitions(mapscore PRIVATE OPENMOBILEMAPS_GL=1)
98100
endif()
99101

102+
target_compile_definitions(mapscore PUBLIC DATAREF_CPP=1)
103+
100104
target_compile_features(mapscore PRIVATE cxx_std_20)
101105
target_compile_options(mapscore PRIVATE -Werror -Wno-deprecated -Wno-reorder -fPIC) # fPIC so we can "embed" into shared mapscore_jni
102106
target_link_libraries(mapscore ${OPENGL_LIBRARIES})

shared/test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ add_executable(tests
1414
"TestTileSource.cpp"
1515
"TestGeometryHandler.cpp"
1616
"TestValueEvaluate.cpp"
17+
"TestStyleParser.cpp"
1718
"helper/TestData.cpp"
19+
"helper/TestLocalDataProvider.h"
1820
)
1921
# Use mapscore _private_ include to allow testing functionality declared in internal headers.
2022
target_include_directories(tests PRIVATE

shared/test/TestStyleParser.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "DataRef.hpp"
2+
#include "GeoJsonTypes.h"
3+
#include "Tiled2dMapVectorLayerParserHelper.h"
4+
#include "VectorLayerDescription.h"
5+
#include "geojsonvt.hpp"
6+
#include "helper/TestData.h"
7+
#include "helper/TestLocalDataProvider.h"
8+
#include "helper/TestScheduler.h"
9+
10+
#include <catch2/benchmark/catch_benchmark.hpp>
11+
#include <catch2/catch_test_macros.hpp>
12+
#include <catch2/generators/catch_generators.hpp>
13+
#include <random>
14+
15+
class TestGeoJSONTileDelegate : public GeoJSONTileDelegate, public ActorObject {
16+
public:
17+
bool didLoadCalled = false;
18+
bool failedToLoadCalled = false;
19+
20+
void didLoad(uint8_t maxZoom) override {
21+
didLoadCalled = true;
22+
}
23+
24+
void failedToLoad() override {
25+
failedToLoadCalled = true;
26+
}
27+
};
28+
29+
30+
TEST_CASE("TestStyleParser", "[GeoJson inline]") {
31+
auto jsonString = TestData::readFileToString("style/geojson_style_inline.json");
32+
auto result = Tiled2dMapVectorLayerParserHelper::parseStyleJsonFromString("test", jsonString, nullptr, {}, {});
33+
REQUIRE(result.mapDescription != nullptr);
34+
REQUIRE(!result.mapDescription->geoJsonSources.empty());
35+
36+
std::shared_ptr<GeoJSONVTInterface> geojsonSource = result.mapDescription->geoJsonSources.begin()->second;
37+
REQUIRE(geojsonSource->getMinZoom() == 0);
38+
REQUIRE(geojsonSource->getMaxZoom() == 0);
39+
40+
REQUIRE(result.mapDescription->layers[0]->sourceMinZoom == 0);
41+
REQUIRE(result.mapDescription->layers[0]->sourceMaxZoom == 0);
42+
REQUIRE_NOTHROW(geojsonSource->getTile(0,0,0));
43+
REQUIRE_THROWS(geojsonSource->getTile(6,33,22));
44+
}
45+
46+
TEST_CASE("TestStyleParser", "[GeoJson local provider]") {
47+
auto jsonString = TestData::readFileToString("style/geojson_style_provider.json");
48+
auto provider = std::make_shared<TestLocalDataProvider>(std::unordered_map<std::string, std::string>{
49+
{"wsource", "geojson.geojson"}
50+
});
51+
auto result = Tiled2dMapVectorLayerParserHelper::parseStyleJsonFromString("test", jsonString, provider, {}, {});
52+
REQUIRE(result.mapDescription != nullptr);
53+
REQUIRE(!result.mapDescription->geoJsonSources.empty());
54+
55+
std::shared_ptr<GeoJSONVTInterface> geojsonSource = result.mapDescription->geoJsonSources.begin()->second;
56+
57+
auto scheduler = std::make_shared<TestScheduler>();
58+
auto delegate = Actor<TestGeoJSONTileDelegate>(std::make_shared<Mailbox>(scheduler), std::make_shared<TestGeoJSONTileDelegate>());
59+
60+
geojsonSource->setDelegate(delegate.weakActor<GeoJSONTileDelegate>());
61+
62+
REQUIRE(!delegate.unsafe()->didLoadCalled);
63+
64+
auto promise = std::make_shared<::djinni::Promise<std::shared_ptr<DataLoaderResult>>>();
65+
geojsonSource->waitIfNotLoaded(promise);
66+
promise->getFuture().wait();
67+
68+
scheduler->drain();
69+
70+
REQUIRE(delegate.unsafe()->didLoadCalled);
71+
72+
REQUIRE(geojsonSource->getMinZoom() == 0);
73+
REQUIRE(geojsonSource->getMaxZoom() == 25);
74+
75+
const auto &tile = geojsonSource->getTile(6,33,22);
76+
77+
REQUIRE(!tile.getFeatures().empty());
78+
}

shared/test/data/style/geojson.geojson

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"version": 8,
3+
"name": "s",
4+
"sources": {
5+
"wsource": {
6+
"type": "geojson",
7+
"minzoom": 0,
8+
"maxzoom": 25,
9+
"data": {
10+
"type": "Point",
11+
"coordinates": [102.0, 0.5]
12+
}
13+
}
14+
},
15+
"sprite": "localdata-sprites",
16+
"layers": [
17+
{
18+
"id": "w",
19+
"type": "fill",
20+
"metadata": {
21+
"blend-mode": "multiply"
22+
},
23+
"minzoom": 0,
24+
"maxzoom": 6,
25+
"source": "wsource",
26+
"paint": {
27+
"fill-color": "rgb(202, 154, 255)"
28+
}
29+
}
30+
]
31+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"version": 8,
3+
"name": "s",
4+
"sources": {
5+
"wsource": {
6+
"type": "geojson",
7+
"minzoom": 0,
8+
"maxzoom": 25,
9+
"data": "localdata-url"
10+
}
11+
},
12+
"sprite": "localdata-sprites",
13+
"layers": [
14+
{
15+
"id": "w",
16+
"type": "fill",
17+
"metadata": {
18+
"blend-mode": "multiply"
19+
},
20+
"minzoom": 0,
21+
"maxzoom": 6,
22+
"source": "wsource",
23+
"paint": {
24+
"fill-color": "rgb(202, 154, 255)"
25+
}
26+
}
27+
]
28+
}

shared/test/helper/TestData.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* SPDX-License-Identifier: MPL-2.0
99
*/
1010

11+
#pragma once
1112
#include <vector>
1213
#include <string>
1314

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
#include "TestData.h"
3+
#include "Tiled2dMapVectorLayerLocalDataProviderInterface.h"
4+
5+
#include <string>
6+
#include <utility>
7+
8+
class TestLocalDataProvider: public Tiled2dMapVectorLayerLocalDataProviderInterface {
9+
public:
10+
explicit TestLocalDataProvider(const std::unordered_map<std::string, std::string> &&geojsonMapping)
11+
: geojsonMapping(geojsonMapping) {}
12+
13+
djinni::Future<DataLoaderResult> loadGeojson(const std::string &sourceName, const std::string &url) override {
14+
auto promise = ::djinni::Promise<DataLoaderResult>();
15+
auto it = geojsonMapping.find(sourceName);
16+
if (it != geojsonMapping.end()) {
17+
auto data = TestData::readFileToBuffer(("style/" + it->second).c_str());
18+
std::vector<uint8_t> uint8Vec(data.begin(), data.end());
19+
promise.setValue(DataLoaderResult(::djinni::DataRef(uint8Vec), std::nullopt, LoaderStatus::OK, std::nullopt));
20+
} else {
21+
throw std::runtime_error("Failed to load geojson from file");
22+
}
23+
return promise.getFuture();
24+
}
25+
std::optional<std::string> getStyleJson() override {
26+
return std::nullopt;
27+
}
28+
djinni::Future<TextureLoaderResult> loadSpriteAsync(int32_t scale) override {
29+
auto promise = ::djinni::Promise<TextureLoaderResult>();
30+
promise.setValue(TextureLoaderResult(nullptr, std::nullopt, LoaderStatus::ERROR_404, std::nullopt));
31+
return promise.getFuture();
32+
}
33+
djinni::Future<DataLoaderResult> loadSpriteJsonAsync(int32_t scale) override {
34+
auto promise = ::djinni::Promise<DataLoaderResult>();
35+
promise.setValue(DataLoaderResult(std::nullopt, std::nullopt, LoaderStatus::ERROR_404, std::nullopt));
36+
return promise.getFuture();
37+
}
38+
39+
private:
40+
std::unordered_map<std::string, std::string> geojsonMapping;
41+
};

0 commit comments

Comments
 (0)