diff options
author | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-10-06 23:42:20 +0300 |
---|---|---|
committer | Bruno de Oliveira Abinader <bruno@mapbox.com> | 2017-10-09 13:33:30 +0300 |
commit | c9e9cd2aff8eb2584f60e892e77337fb2d9f2d61 (patch) | |
tree | ff83fab42f856a7f685fae635847ded0992e08e9 /test | |
parent | b1239464c16ae1c3bad99435ca130c113b9a1d47 (diff) | |
download | qtlocation-mapboxgl-c9e9cd2aff8eb2584f60e892e77337fb2d9f2d61.tar.gz |
[test] Added GlyphManager.ImmediateFileSource
Diffstat (limited to 'test')
-rw-r--r-- | test/src/mbgl/test/stub_file_source.cpp | 16 | ||||
-rw-r--r-- | test/src/mbgl/test/stub_file_source.hpp | 8 | ||||
-rw-r--r-- | test/text/glyph_loader.test.cpp | 49 |
3 files changed, 70 insertions, 3 deletions
diff --git a/test/src/mbgl/test/stub_file_source.cpp b/test/src/mbgl/test/stub_file_source.cpp index 7891d5d907..0bbff84ff3 100644 --- a/test/src/mbgl/test/stub_file_source.cpp +++ b/test/src/mbgl/test/stub_file_source.cpp @@ -17,7 +17,12 @@ public: StubFileSource& fileSource; }; -StubFileSource::StubFileSource() { +StubFileSource::StubFileSource(ResponseType type_) + : type(type_) { + if (type == ResponseType::Synchronous) { + return; + } + timer.start(1ms, 1ms, [this] { // Explicit copy to avoid iterator invalidation if ~StubFileRequest gets called within the loop. auto pending_ = pending; @@ -46,7 +51,14 @@ StubFileSource::~StubFileSource() = default; std::unique_ptr<AsyncRequest> StubFileSource::request(const Resource& resource, Callback callback) { auto req = std::make_unique<StubFileRequest>(*this); - pending.emplace(req.get(), std::make_tuple(resource, response, callback)); + if (type == ResponseType::Synchronous) { + optional<Response> res = response(resource); + if (res) { + callback(*res); + } + } else { + pending.emplace(req.get(), std::make_tuple(resource, response, callback)); + } return std::move(req); } diff --git a/test/src/mbgl/test/stub_file_source.hpp b/test/src/mbgl/test/stub_file_source.hpp index 85118e1a77..6cee8377c6 100644 --- a/test/src/mbgl/test/stub_file_source.hpp +++ b/test/src/mbgl/test/stub_file_source.hpp @@ -9,7 +9,12 @@ namespace mbgl { class StubFileSource : public FileSource { public: - StubFileSource(); + enum class ResponseType { + Asynchronous = 0, + Synchronous + }; + + StubFileSource(ResponseType = ResponseType::Asynchronous); ~StubFileSource() override; std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override; @@ -36,6 +41,7 @@ private: optional<Response> defaultResponse(const Resource&); std::unordered_map<AsyncRequest*, std::tuple<Resource, ResponseFunction, Callback>> pending; + ResponseType type; util::Timer timer; }; diff --git a/test/text/glyph_loader.test.cpp b/test/text/glyph_loader.test.cpp index be197ebb46..20ac045925 100644 --- a/test/text/glyph_loader.test.cpp +++ b/test/text/glyph_loader.test.cpp @@ -214,3 +214,52 @@ TEST(GlyphManager, LoadingInvalid) { {{{"Test Stack"}}, {u'A', u'E'}} }); } + +TEST(GlyphManager, ImmediateFileSource) { + class GlyphManagerTestSynchronous { + public: + util::RunLoop loop; + StubFileSource fileSource = { StubFileSource::ResponseType::Synchronous }; + StubGlyphManagerObserver observer; + StubGlyphRequestor requestor; + GlyphManager glyphManager { fileSource }; + + void run(const std::string& url, GlyphDependencies dependencies) { + // Squelch logging. + Log::setObserver(std::make_unique<Log::NullObserver>()); + + glyphManager.setURL(url); + glyphManager.setObserver(&observer); + glyphManager.getGlyphs(requestor, std::move(dependencies)); + + loop.run(); + } + + void end() { + loop.stop(); + } + }; + + GlyphManagerTestSynchronous test; + + test.fileSource.glyphsResponse = [&] (const Resource&) { + Response response; + response.data = std::make_shared<std::string>(util::read_file("test/fixtures/resources/glyphs.pbf")); + return response; + }; + + test.observer.glyphsError = [&] (const FontStack&, const GlyphRange&, std::exception_ptr) { + FAIL(); + test.end(); + }; + + test.requestor.glyphsAvailable = [&] (GlyphMap) { + test.end(); + }; + + test.run( + "test/fixtures/resources/glyphs.pbf", + GlyphDependencies { + {{{"Test Stack"}}, {u'a', u'å', u' '}} + }); +} |