summaryrefslogtreecommitdiff
path: root/test/api
diff options
context:
space:
mode:
authorAlexander Shalamov <alexander.shalamov@mapbox.com>2018-11-19 10:52:22 +0200
committerAlexander Shalamov <alexander.shalamov@mapbox.com>2018-12-12 15:08:18 +0200
commit191d47b93560fd6c08d4f1fa0288ee54b0276871 (patch)
tree67b580366ff4031105b6cacbe41fbc3af821e364 /test/api
parenta1ca06e304a14582a90b258e817ec6f17d72fb11 (diff)
downloadqtlocation-mapboxgl-191d47b93560fd6c08d4f1fa0288ee54b0276871.tar.gz
[core] Add unit tests for queryFeatureExtensions
Diffstat (limited to 'test/api')
-rw-r--r--test/api/query.test.cpp147
1 files changed, 147 insertions, 0 deletions
diff --git a/test/api/query.test.cpp b/test/api/query.test.cpp
index 5960de3b31..f5eb32788d 100644
--- a/test/api/query.test.cpp
+++ b/test/api/query.test.cpp
@@ -5,9 +5,11 @@
#include <mbgl/util/image.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
+#include <mbgl/style/layers/symbol_layer.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/style/image.hpp>
#include <mbgl/style/source.hpp>
+#include <mbgl/style/sources/geojson_source.hpp>
#include <mbgl/style/expression/dsl.hpp>
#include <mbgl/renderer/renderer.hpp>
#include <mbgl/gl/headless_frontend.hpp>
@@ -15,6 +17,7 @@
using namespace mbgl;
using namespace mbgl::style;
using namespace mbgl::style::expression;
+using namespace std::literals;
namespace {
@@ -37,6 +40,37 @@ public:
threadPool, MapMode::Static};
};
+std::vector<Feature> getTopClusterFeature(QueryTest& test) {
+ test.fileSource.sourceResponse = [&] (const Resource& resource) {
+ EXPECT_EQ("http://url"s, resource.url);
+ Response response;
+ response.data = std::make_unique<std::string>(util::read_file("test/fixtures/supercluster/places.json"s));
+ return response;
+ };
+
+ LatLng coordinate{0, 0};
+ GeoJSONOptions options{};
+ options.cluster = true;
+ auto source = std::make_unique<GeoJSONSource>("cluster_source"s, options);
+ source->setURL("http://url"s);
+ source->loadDescription(test.fileSource);
+
+ auto clusterLayer = std::make_unique<SymbolLayer>("cluster_layer"s, "cluster_source"s);
+ clusterLayer->setIconImage("test-icon"s);
+ clusterLayer->setIconSize(12.0f);
+
+ test.map.setLatLngZoom(coordinate, 0);
+ test.map.getStyle().addSource(std::move(source));
+ test.map.getStyle().addLayer(std::move(clusterLayer));
+ test.loop.runOnce();
+ test.frontend.render(test.map);
+
+ auto screenCoordinate = test.map.pixelForLatLng(coordinate);
+ const RenderedQueryOptions queryOptions({{{ "cluster_layer"s}}, {}});
+ return test.frontend.getRenderer()->queryRenderedFeatures(screenCoordinate,
+ queryOptions);
+}
+
} // end namespace
TEST(Query, QueryRenderedFeatures) {
@@ -127,3 +161,116 @@ TEST(Query, QuerySourceFeaturesFilter) {
EXPECT_EQ(features3.size(), 1u);
}
+TEST(Query, QueryFeatureExtensionsInvalidExtension) {
+ QueryTest test;
+
+ auto unknownExt = test.frontend.getRenderer()->queryFeatureExtensions("source4"s,
+ {},
+ "unknown"s,
+ "children"s);
+ auto unknownValue = unknownExt.get<mbgl::Value>();
+ EXPECT_TRUE(unknownValue.is<NullValue>());
+}
+
+TEST(Query, QueryFeatureExtensionsSuperclusterChildren) {
+ QueryTest test;
+ auto topClusterFeature = getTopClusterFeature(test);
+
+ EXPECT_EQ(topClusterFeature.size(), 1u);
+ const auto featureProps = topClusterFeature[0].properties;
+ auto clusterId = featureProps.find("cluster_id"s);
+ auto cluster = featureProps.find("cluster"s);
+ EXPECT_TRUE(clusterId != featureProps.end());
+ EXPECT_TRUE(cluster != featureProps.end());
+
+ auto queryChildren = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ topClusterFeature[0],
+ "supercluster"s,
+ "children"s);
+
+ EXPECT_TRUE(queryChildren.is<FeatureCollection>());
+ auto children = queryChildren.get<FeatureCollection>();
+ EXPECT_EQ(children.size(), 4u);
+
+ // Compare results produced by supercluster with default clustering options.
+ EXPECT_EQ(children[0].properties["cluster_id"].get<uint64_t>(), 2u);
+ EXPECT_EQ(children[1].properties["cluster_id"].get<uint64_t>(), 34u);
+ EXPECT_EQ(children[2].properties["cluster_id"].get<uint64_t>(), 258u);
+ EXPECT_EQ(children[3].properties["cluster_id"].get<uint64_t>(), 2466u);
+ EXPECT_EQ(children[0].properties["point_count"].get<uint64_t>(), 7u);
+ EXPECT_EQ(children[1].properties["point_count"].get<uint64_t>(), 16u);
+ EXPECT_EQ(children[2].properties["point_count"].get<uint64_t>(), 7u);
+ EXPECT_EQ(children[3].properties["point_count"].get<uint64_t>(), 2u);
+}
+
+TEST(Query, QueryFeatureExtensionsSuperclusterExpansionZoom) {
+ QueryTest test;
+ auto topClusterFeature = getTopClusterFeature(test);
+ EXPECT_EQ(topClusterFeature.size(), 1u);
+
+ auto queryChildren = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ topClusterFeature[0],
+ "supercluster"s,
+ "children"s);
+ auto children = queryChildren.get<FeatureCollection>();
+
+
+ auto queryExpansionZoom1 = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ topClusterFeature[0],
+ "supercluster"s,
+ "expansion-zoom"s);
+
+ auto queryExpansionZoom2 = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ children[3],
+ "supercluster"s,
+ "expansion-zoom"s);
+ auto zoomValue1 = queryExpansionZoom1.get<mbgl::Value>();
+ auto zoomValue2 = queryExpansionZoom2.get<mbgl::Value>();
+ EXPECT_TRUE(zoomValue1.is<uint64_t>());
+ EXPECT_TRUE(zoomValue2.is<uint64_t>());
+ EXPECT_EQ(zoomValue1.get<uint64_t>(), 1u);
+ EXPECT_EQ(zoomValue2.get<uint64_t>(), 3u);
+}
+
+TEST(Query, QueryFeatureExtensionsSuperclusterLeaves) {
+ QueryTest test;
+ auto topClusterFeature = getTopClusterFeature(test);
+ EXPECT_EQ(topClusterFeature.size(), 1u);
+
+ // Get leaves for cluster 1, with default limit 10, offset 0.
+ auto queryClusterLeaves = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ topClusterFeature[0],
+ "supercluster"s,
+ "leaves"s);
+ EXPECT_TRUE(queryClusterLeaves.is<FeatureCollection>());
+ auto leaves = queryClusterLeaves.get<FeatureCollection>();
+ EXPECT_EQ(leaves.size(), 10u);
+
+ // Get leaves for cluster 1, with limit 3, offset 0.
+ const std::map<std::string, mbgl::Value> limitOpts = { {"limit"s, static_cast<uint64_t>(3u)} };
+ auto queryClusterLeavesLimit3 = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ topClusterFeature[0],
+ "supercluster"s,
+ "leaves"s,
+ limitOpts);
+ auto limitLeaves3 = queryClusterLeavesLimit3.get<FeatureCollection>();
+ EXPECT_EQ(limitLeaves3.size(), 3u);
+
+ EXPECT_EQ(limitLeaves3[0].properties["name"].get<std::string>(), "Niagara Falls"s);
+ EXPECT_EQ(limitLeaves3[1].properties["name"].get<std::string>(), "Cape May"s);
+ EXPECT_EQ(limitLeaves3[2].properties["name"].get<std::string>(), "Cape Fear"s);
+
+ // Get leaves for cluster 1, with limit 3, offset 3.
+ const std::map<std::string, mbgl::Value> offsetOpts = { {"limit"s, static_cast<uint64_t>(3u)},
+ {"offset"s, static_cast<uint64_t>(3u)} };
+ auto queryClusterLeavesOffset3 = test.frontend.getRenderer()->queryFeatureExtensions("cluster_source"s,
+ topClusterFeature[0],
+ "supercluster"s,
+ "leaves"s,
+ offsetOpts);
+ auto offsetLeaves3 = queryClusterLeavesOffset3.get<FeatureCollection>();
+ EXPECT_EQ(offsetLeaves3.size(), 3u);
+ EXPECT_EQ(offsetLeaves3[0].properties["name"].get<std::string>(), "Cape Hatteras"s);
+ EXPECT_EQ(offsetLeaves3[1].properties["name"].get<std::string>(), "Cape Sable"s);
+ EXPECT_EQ(offsetLeaves3[2].properties["name"].get<std::string>(), "Cape Cod"s);
+}