Cancel pending style request when loading style JSON (#3989)

Signed-off-by: Bart Louwers <bart@emeel.net>
This commit is contained in:
Bart Louwers
2025-12-09 13:21:22 +01:00
committed by GitHub
parent 421eab6dfb
commit 1f320fd089
5 changed files with 69 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
#include <chrono>
#include <mbgl/test/util.hpp>
#include <mbgl/test/stub_file_source.hpp>
#include <mbgl/test/fixture_log_observer.hpp>
@@ -9,6 +10,7 @@
#include <mbgl/style/layers/line_layer.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/client_options.hpp>
#include <memory>
@@ -110,6 +112,43 @@ TEST(Style, RemoveSourceInUse) {
EXPECT_EQ(log.count(logMessage), 1u);
}
TEST(Style, LoadJSONCancelsPendingLoadURL) {
util::RunLoop loop;
auto fileSource = std::make_shared<::StubFileSource>(
ResourceOptions::Default(), ClientOptions(), StubFileSource::ResponseType::Manual);
Style::Impl style{fileSource, 1.0, {Scheduler::GetBackground(), {}}};
// Start loading a URL (this will be pending)
auto url = "http://some-url";
fileSource->styleResponse = [](const Resource&) {
Response result;
result.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/style_vector.json"));
return result;
};
style.loadURL(url);
// Before the URL request completes, load JSON directly
const std::string json = R"STYLE({
"version": 8,
"name": "Test Style",
"sources": {},
"layers": []
})STYLE";
style.loadJSON(json);
// The style should now be loaded with our JSON content
ASSERT_EQ("Test Style", style.getName());
ASSERT_EQ("", style.getURL());
ASSERT_TRUE(style.getJSON().find("Test Style") != std::string::npos);
fileSource->respondToAll();
// The style should still show our JSON content, not the URL content
ASSERT_EQ("Test Style", style.getName());
ASSERT_NE("Streets", style.getName());
}
TEST(Style, SourceImplsOrder) {
util::RunLoop loop;
auto fileSource = std::make_shared<StubFileSource>();