diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2019-09-04 02:02:23 +0200 |
---|---|---|
committer | Dane Springmeyer <springmeyer@users.noreply.github.com> | 2019-09-03 17:02:23 -0700 |
commit | c7be3d52a709c98e93384bdcabc5cebc7adb9dac (patch) | |
tree | b9d36368491a8e03ecf435215a75a616b7059065 | |
parent | 1ef763c98e540b0a347cd59ab040a0449df4a14c (diff) | |
download | qtlocation-mapboxgl-c7be3d52a709c98e93384bdcabc5cebc7adb9dac.tar.gz |
Always call onSourceLoaded observers (#15548)
* [core] add sources to source collection before triggering load
* [test] add testcase for #15514
* [core] also call onSourceLoaded observers when no network request was necessary
-rw-r--r-- | src/mbgl/style/sources/custom_geometry_source.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/style/sources/raster_source.cpp | 1 | ||||
-rw-r--r-- | src/mbgl/style/sources/vector_source.cpp | 1 | ||||
-rw-r--r-- | src/mbgl/style/style_impl.cpp | 5 | ||||
-rw-r--r-- | test/storage/sync_file_source.test.cpp | 50 | ||||
-rw-r--r-- | test/test-files.json | 1 |
6 files changed, 57 insertions, 3 deletions
diff --git a/src/mbgl/style/sources/custom_geometry_source.cpp b/src/mbgl/style/sources/custom_geometry_source.cpp index 6e9d8d65fb..73675c056f 100644 --- a/src/mbgl/style/sources/custom_geometry_source.cpp +++ b/src/mbgl/style/sources/custom_geometry_source.cpp @@ -1,6 +1,7 @@ #include <mbgl/style/sources/custom_geometry_source.hpp> #include <mbgl/style/custom_tile_loader.hpp> #include <mbgl/style/sources/custom_geometry_source_impl.hpp> +#include <mbgl/style/source_observer.hpp> #include <mbgl/actor/actor.hpp> #include <mbgl/actor/scheduler.hpp> #include <mbgl/tile/tile_id.hpp> @@ -25,6 +26,7 @@ const CustomGeometrySource::Impl& CustomGeometrySource::impl() const { void CustomGeometrySource::loadDescription(FileSource&) { baseImpl = makeMutable<CustomGeometrySource::Impl>(impl(), loader->self()); loaded = true; + observer->onSourceLoaded(*this); } void CustomGeometrySource::setTileData(const CanonicalTileID& tileID, diff --git a/src/mbgl/style/sources/raster_source.cpp b/src/mbgl/style/sources/raster_source.cpp index b4fbe22ae1..115887d004 100644 --- a/src/mbgl/style/sources/raster_source.cpp +++ b/src/mbgl/style/sources/raster_source.cpp @@ -41,6 +41,7 @@ void RasterSource::loadDescription(FileSource& fileSource) { if (urlOrTileset.is<Tileset>()) { baseImpl = makeMutable<Impl>(impl(), urlOrTileset.get<Tileset>()); loaded = true; + observer->onSourceLoaded(*this); return; } diff --git a/src/mbgl/style/sources/vector_source.cpp b/src/mbgl/style/sources/vector_source.cpp index 8fa694e4d0..f103a7768f 100644 --- a/src/mbgl/style/sources/vector_source.cpp +++ b/src/mbgl/style/sources/vector_source.cpp @@ -38,6 +38,7 @@ void VectorSource::loadDescription(FileSource& fileSource) { if (urlOrTileset.is<Tileset>()) { baseImpl = makeMutable<Impl>(impl(), urlOrTileset.get<Tileset>()); loaded = true; + observer->onSourceLoaded(*this); return; } diff --git a/src/mbgl/style/style_impl.cpp b/src/mbgl/style/style_impl.cpp index 10fee73cdd..d3298c5cac 100644 --- a/src/mbgl/style/style_impl.cpp +++ b/src/mbgl/style/style_impl.cpp @@ -140,9 +140,8 @@ void Style::Impl::addSource(std::unique_ptr<Source> source) { } source->setObserver(this); - source->loadDescription(fileSource); - - sources.add(std::move(source)); + auto item = sources.add(std::move(source)); + item->loadDescription(fileSource); } std::unique_ptr<Source> Style::Impl::removeSource(const std::string& id) { diff --git a/test/storage/sync_file_source.test.cpp b/test/storage/sync_file_source.test.cpp new file mode 100644 index 0000000000..4bd964199d --- /dev/null +++ b/test/storage/sync_file_source.test.cpp @@ -0,0 +1,50 @@ +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/io.hpp> +#include <mbgl/storage/file_source.hpp> +#include <mbgl/gfx/headless_frontend.hpp> +#include <mbgl/map/map.hpp> +#include <mbgl/map/map_impl.hpp> +#include <mbgl/style/style.hpp> +#include <mbgl/test/map_adapter.hpp> +#include <unordered_map> + +#include <gtest/gtest.h> + +using namespace mbgl; + +class SyncFileSource : public FileSource { +public: + std::unique_ptr<AsyncRequest> request(const Resource& resource, FileSource::Callback callback) { + Response response; + auto it = assets.find(resource.url); + if (it == assets.end()) { + response.error = std::make_unique<Response::Error>( + Response::Error::Reason::NotFound, std::string{ "Not Found: " } + resource.url); + } else { + response.data = it->second; + } + callback(response); + return nullptr; + } + + void add(std::string const& key, std::string const& data) { + assets.emplace(key, std::make_shared<std::string>(data)); + }; + +private: + std::unordered_map<std::string, std::shared_ptr<std::string>> assets; +}; + +TEST(SyncFileSource, LoadSyncRender) { + util::RunLoop loop; + auto fs = std::make_shared<SyncFileSource>(); + fs->add("mapbox://mapbox.mapbox-terrain-v2,mapbox.mapbox-streets-v7", + util::read_file("test/fixtures/resources/source_vector.json")); + fs->add("mapbox://sprites/mapbox/streets-v9.png", + util::read_file("test/fixtures/resources/sprite.png")); + fs->add("mapbox://sprites/mapbox/streets-v9.json", + util::read_file("test/fixtures/resources/sprite.json")); + HeadlessFrontend frontend{ { 512, 512 }, 1.0 }; + MapAdapter map{ frontend, MapObserver::nullObserver(), fs, MapOptions() }; + map.getStyle().loadJSON(util::read_file("test/fixtures/resources/style_vector.json")); +} diff --git a/test/test-files.json b/test/test-files.json index e46f833269..f5e4013029 100644 --- a/test/test-files.json +++ b/test/test-files.json @@ -46,6 +46,7 @@ "test/storage/online_file_source.test.cpp", "test/storage/resource.test.cpp", "test/storage/sqlite.test.cpp", + "test/storage/sync_file_source.test.cpp", "test/style/conversion/conversion_impl.test.cpp", "test/style/conversion/function.test.cpp", "test/style/conversion/geojson_options.test.cpp", |