diff options
-rw-r--r-- | test/fixtures/resources/style-unused-sources.json | 36 | ||||
-rw-r--r-- | test/style/unused_sources.cpp | 111 | ||||
-rw-r--r-- | test/test.gypi | 1 |
3 files changed, 148 insertions, 0 deletions
diff --git a/test/fixtures/resources/style-unused-sources.json b/test/fixtures/resources/style-unused-sources.json new file mode 100644 index 0000000000..f87166a678 --- /dev/null +++ b/test/fixtures/resources/style-unused-sources.json @@ -0,0 +1,36 @@ +{ + "version": 8, + "name": "Test", + "sources": { + "usedsource": { + "url": "test/fixtures/resources/source_vector.json", + "type": "vector" + }, + "unusedsource": { + "url": "test/fixtures/resources/source_vector.json", + "type": "vector" + } + }, + "sprite": "test/fixtures/resources/sprite", + "glyphs": "test/fixtures/resources/glyphs.pbf", + "layers": [{ + "id": "usedlayer", + "type": "symbol", + "source": "usedsource" + }, { + "id": "unusedlayeropacity", + "type": "symbol", + "source": "unusedsource", + "paint" : { + "icon-opacity" : 0, + "text-opacity" : 0 + } + }, { + "id": "unusedlayervisibility", + "type": "symbol", + "source": "unusedsource", + "layout" : { + "visibility" : "none" + } + }] +} diff --git a/test/style/unused_sources.cpp b/test/style/unused_sources.cpp new file mode 100644 index 0000000000..318a8928f7 --- /dev/null +++ b/test/style/unused_sources.cpp @@ -0,0 +1,111 @@ +#include "../fixtures/util.hpp" +#include "../fixtures/mock_file_source.hpp" +#include "../fixtures/mock_view.hpp" + +#include <mbgl/style/style.hpp> +#include <mbgl/map/view.hpp> +#include <mbgl/storage/file_source.hpp> +#include <mbgl/map/map_data.hpp> +#include <mbgl/map/transform.hpp> +#include <mbgl/util/exception.hpp> +#include <mbgl/util/io.hpp> +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/texture_pool.hpp> +#include <mbgl/util/thread.hpp> + +namespace mbgl { + +class MockStyleObserver: public Style::Observer { +public: + using MockStyleObserverCallback = std::function<void(std::exception_ptr, Style*)>; + + MockStyleObserver(View& view_, + FileSource& fileSource_, + const MockStyleObserverCallback& callback_) + : data(MapMode::Still, GLContextMode::Unique, view_.getPixelRatio()), + style(std::make_unique<Style>(data)), + transform(view_, ConstrainMode::HeightOnly), + callback(callback_) { + util::ThreadContext::setFileSource(&fileSource_); + + transform.resize({{ 1000, 1000 }}); + transform.setLatLngZoom({0, 0}, 16); + + const std::string style_ = util::read_file("test/fixtures/resources/style-unused-sources.json"); + style->setJSON(style_, ""); + style->setObserver(this); + } + + ~MockStyleObserver() { + cleanup(); + } + + void cleanup() { + style.reset(); + } + + void update() { + const auto now = Clock::now(); + + data.setAnimationTime(now); + transform.updateTransitions(now); + + style->cascade(); + style->recalculate(16); + style->update(transform.getState(), texturePool); + } + + // Style::Observer implementation. + void onTileDataChanged() override { + update(); + + if (style->isLoaded()) { + callback(nullptr, style.get()); + } + } + + void onResourceLoadingFailed(std::exception_ptr error) override { + callback(error, style.get()); + } + +private: + MapData data; + std::unique_ptr<Style> style; + Transform transform; + TexturePool texturePool; + + MockStyleObserverCallback callback; +}; + +TEST(Style, UnusedSources) { + util::RunLoop loop; + + MockView view; + MockFileSource fileSource(MockFileSource::Success, ""); + + auto callback = [&loop](std::exception_ptr error, Style* style) { + EXPECT_TRUE(error == nullptr); + loop.stop(); + + Source *usedSource = style->getSource("usedsource"); + EXPECT_TRUE(usedSource); + EXPECT_TRUE(usedSource->isLoaded()); + + Source *unusedSource = style->getSource("unusedsource"); + EXPECT_TRUE(unusedSource); + EXPECT_FALSE(unusedSource->isLoaded()); + }; + + std::unique_ptr<util::Thread<MockStyleObserver>> observer( + std::make_unique<util::Thread<MockStyleObserver>>( + util::ThreadContext{"Map", util::ThreadType::Map, util::ThreadPriority::Regular}, view, fileSource, callback)); + + loop.run(); + + // Needed because it will make the Map thread + // join and cease logging after this point. + observer->invoke(&MockStyleObserver::cleanup); + observer.reset(); +} + +} // namespace mbgl diff --git a/test/test.gypi b/test/test.gypi index 8fc953e745..6227d49750 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -91,6 +91,7 @@ 'style/pending_resources.cpp', 'style/resource_loading.cpp', 'style/style_layer.cpp', + 'style/unused_sources.cpp', 'sprite/sprite_atlas.cpp', 'sprite/sprite_image.cpp', |