[core] Add unit test for Style::Impl::onSpriteLoaded
This commit is contained in:
BIN
test/fixtures/map/online/0-0-0.vector.pbf
vendored
Normal file
BIN
test/fixtures/map/online/0-0-0.vector.pbf
vendored
Normal file
Binary file not shown.
26
test/fixtures/map/online/sprite.json
vendored
Normal file
26
test/fixtures/map/online/sprite.json
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"c": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 32,
|
||||
"height": 32,
|
||||
"pixelRatio": 1,
|
||||
"sdf": false
|
||||
},
|
||||
"a": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 32,
|
||||
"height": 32,
|
||||
"pixelRatio": 1,
|
||||
"sdf": false
|
||||
},
|
||||
"b": {
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 32,
|
||||
"height": 32,
|
||||
"pixelRatio": 1,
|
||||
"sdf": false
|
||||
}
|
||||
}
|
||||
BIN
test/fixtures/map/online/sprite.png
vendored
Normal file
BIN
test/fixtures/map/online/sprite.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
14
test/fixtures/map/online/streets.json
vendored
Normal file
14
test/fixtures/map/online/streets.json
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"bounds": [ -180, -85.0511, 180, 85.0511 ],
|
||||
"center": [ 0, 0, 0 ],
|
||||
"description": "",
|
||||
"format": "pbf",
|
||||
"id": "streets",
|
||||
"maskLevel": 8,
|
||||
"maxzoom": 15,
|
||||
"minzoom": 0,
|
||||
"name": "Streets",
|
||||
"scheme": "xyz",
|
||||
"tilejson": "2.0.0",
|
||||
"tiles": [ "http://127.0.0.1:3000/online/{z}-{x}-{y}.vector.pbf" ]
|
||||
}
|
||||
27
test/fixtures/map/online/style.json
vendored
Normal file
27
test/fixtures/map/online/style.json
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"version": 8,
|
||||
"name": "Water",
|
||||
"sources": {
|
||||
"mapbox": {
|
||||
"type": "vector",
|
||||
"url": "http://127.0.0.1:3000/online/streets.json"
|
||||
}
|
||||
},
|
||||
"sprite": "http://127.0.0.1:3000/online/sprite",
|
||||
"layers": [{
|
||||
"id": "background",
|
||||
"type": "background",
|
||||
"paint": {
|
||||
"background-color": "red"
|
||||
}
|
||||
}, {
|
||||
"id": "water",
|
||||
"type": "fill",
|
||||
"source": "mapbox",
|
||||
"source-layer": "water",
|
||||
"paint": {
|
||||
"fill-pattern": "noise",
|
||||
"fill-antialias": false
|
||||
}
|
||||
}]
|
||||
}
|
||||
@@ -10,12 +10,14 @@
|
||||
#include <mbgl/gl/context.hpp>
|
||||
#include <mbgl/map/map_options.hpp>
|
||||
#include <mbgl/math/log2.hpp>
|
||||
#include <mbgl/renderer/update_parameters.hpp>
|
||||
#include <mbgl/storage/file_source_manager.hpp>
|
||||
#include <mbgl/storage/main_resource_loader.hpp>
|
||||
#include <mbgl/storage/network_status.hpp>
|
||||
#include <mbgl/storage/online_file_source.hpp>
|
||||
#include <mbgl/storage/resource_options.hpp>
|
||||
#include <mbgl/style/image.hpp>
|
||||
#include <mbgl/style/image_impl.hpp>
|
||||
#include <mbgl/style/layers/background_layer.hpp>
|
||||
#include <mbgl/style/layers/raster_layer.hpp>
|
||||
#include <mbgl/style/layers/symbol_layer.hpp>
|
||||
@@ -36,13 +38,13 @@ using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
using namespace std::literals::string_literals;
|
||||
|
||||
template <class FileSource = StubFileSource>
|
||||
template <class FileSource = StubFileSource, class Frontend = HeadlessFrontend>
|
||||
class MapTest {
|
||||
public:
|
||||
util::RunLoop runLoop;
|
||||
std::shared_ptr<FileSource> fileSource;
|
||||
StubMapObserver observer;
|
||||
HeadlessFrontend frontend;
|
||||
Frontend frontend;
|
||||
MapAdapter map;
|
||||
|
||||
MapTest(float pixelRatio = 1, MapMode mode = MapMode::Static)
|
||||
@@ -1253,3 +1255,52 @@ TEST(Map, PrefetchDeltaOverrideCustomSource) {
|
||||
// and current zoom level + parent tiles would be re-requested.
|
||||
EXPECT_EQ(8, requestedTiles);
|
||||
}
|
||||
|
||||
// Test verifies that Style::Impl::onSpriteLoaded does not create duplicate
|
||||
// images when we get new spritesheet from the server and we merge it with
|
||||
// currently used spritesheet.
|
||||
TEST(Map, TEST_REQUIRES_SERVER(ExpiredSpriteSheet)) {
|
||||
class ForwardingHeadlessFrontend : public HeadlessFrontend {
|
||||
public:
|
||||
using HeadlessFrontend::HeadlessFrontend;
|
||||
~ForwardingHeadlessFrontend() override = default;
|
||||
void update(std::shared_ptr<UpdateParameters> params) override {
|
||||
if (checkParams) checkParams(params);
|
||||
HeadlessFrontend::update(std::move(params));
|
||||
}
|
||||
std::function<void(std::shared_ptr<UpdateParameters>)> checkParams;
|
||||
};
|
||||
|
||||
MapTest<MainResourceLoader, ForwardingHeadlessFrontend> test{":memory:", ".", 1, MapMode::Continuous};
|
||||
|
||||
auto makeResponse = [](const std::string& path, bool expires = false) {
|
||||
Response response;
|
||||
response.data = std::make_shared<std::string>(util::read_file("test/fixtures/map/online/"s + path));
|
||||
response.expires = expires ? Timestamp{Seconds(0)} : Timestamp::max();
|
||||
return response;
|
||||
};
|
||||
|
||||
test.observer.didFinishLoadingMapCallback = [&test] {
|
||||
test.frontend.checkParams = [](std::shared_ptr<UpdateParameters> params) {
|
||||
EXPECT_TRUE(std::is_sorted(params->images->begin(), params->images->end()));
|
||||
EXPECT_TRUE(params->images->size() < 4u);
|
||||
};
|
||||
|
||||
NetworkStatus::Set(NetworkStatus::Status::Online);
|
||||
|
||||
test.observer.didBecomeIdleCallback = [&test] { test.runLoop.stop(); };
|
||||
};
|
||||
|
||||
NetworkStatus::Set(NetworkStatus::Status::Offline);
|
||||
const std::string prefix = "http://127.0.0.1:3000/online/";
|
||||
auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{});
|
||||
dbfs->forward(Resource::style(prefix + "style.json"), makeResponse("style.json"));
|
||||
dbfs->forward(Resource::source(prefix + "streets.json"), makeResponse("streets.json"));
|
||||
dbfs->forward(Resource::spriteJSON(prefix + "sprite", 1.0), makeResponse("sprite.json", true));
|
||||
dbfs->forward(Resource::spriteImage(prefix + "sprite", 1.0), makeResponse("sprite.png", true));
|
||||
dbfs->forward(Resource::tile(prefix + "{z}-{x}-{y}.vector.pbf", 1.0, 0, 0, 0, Tileset::Scheme::XYZ),
|
||||
makeResponse("0-0-0.vector.pbf"),
|
||||
[&] { test.map.getStyle().loadURL(prefix + "style.json"); });
|
||||
|
||||
test.runLoop.run();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
#include <mbgl/util/io.hpp>
|
||||
#include <mbgl/util/logging.hpp>
|
||||
|
||||
// Limit to 4 threads (default is the nbr of CPU cores)
|
||||
@@ -9,6 +10,8 @@
|
||||
|
||||
#include <atomic>
|
||||
|
||||
using namespace std::literals::string_literals;
|
||||
|
||||
namespace mbgl {
|
||||
namespace test {
|
||||
|
||||
@@ -182,6 +185,13 @@ void runServer(std::unique_ptr<httplib::Server>& server) {
|
||||
res.set_content("Request " + std::string(numbers), "text/plain");
|
||||
});
|
||||
|
||||
server->Get(R"(/online/(.*))", [](const Request req, Response& res) {
|
||||
res.status = 200;
|
||||
auto file = "test/fixtures/map/online/"s + std::string(req.matches[1]);
|
||||
auto content = util::read_file(file);
|
||||
res.set_content(content, "text/plain");
|
||||
});
|
||||
|
||||
server->listen("127.0.0.1", 3000);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,11 +38,18 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void onDidBecomeIdle() final {
|
||||
if (didBecomeIdleCallback) {
|
||||
didBecomeIdleCallback();
|
||||
}
|
||||
}
|
||||
|
||||
std::function<void()> willStartLoadingMapCallback;
|
||||
std::function<void()> didFinishLoadingMapCallback;
|
||||
std::function<void()> didFailLoadingMapCallback;
|
||||
std::function<void()> didFinishLoadingStyleCallback;
|
||||
std::function<void(RenderFrameStatus)> didFinishRenderingFrameCallback;
|
||||
std::function<void()> didBecomeIdleCallback;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user