diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-23 16:54:34 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-12-24 08:17:42 -0800 |
commit | 338f94e7ed73f6b0685f406ac1a8cf389b2aacbb (patch) | |
tree | 2ecf4dba0bf562e60a749fd1f984fff5b0f21fbb /test/style | |
parent | aca584e124a53f83e12d7318de274fce3ff6e290 (diff) | |
download | qtlocation-mapboxgl-338f94e7ed73f6b0685f406ac1a8cf389b2aacbb.tar.gz |
[tests] Rewrite {Sprite,Glyph}Store tests in the style of ResourceLoading
Diffstat (limited to 'test/style')
-rw-r--r-- | test/style/glyph_store.cpp | 252 | ||||
-rw-r--r-- | test/style/resource_loading.cpp | 73 |
2 files changed, 91 insertions, 234 deletions
diff --git a/test/style/glyph_store.cpp b/test/style/glyph_store.cpp index 8e7518c33f..f177036d87 100644 --- a/test/style/glyph_store.cpp +++ b/test/style/glyph_store.cpp @@ -1,233 +1,145 @@ -#include "../fixtures/fixture_log_observer.hpp" -#include "../fixtures/mock_file_source.hpp" #include "../fixtures/util.hpp" +#include "../fixtures/mock_file_source.hpp" +#include "../fixtures/stub_style_observer.hpp" #include <mbgl/text/font_stack.hpp> #include <mbgl/text/glyph_store.hpp> -#include <mbgl/util/async_task.hpp> #include <mbgl/util/run_loop.hpp> -#include <mbgl/util/thread.hpp> -#include <utility> +#include <mbgl/platform/log.hpp> using namespace mbgl; -using GlyphStoreTestCallback = std::function<void(GlyphStore*, std::exception_ptr)>; - -struct GlyphStoreParams { - const std::string url; - const std::string stack; - const std::set<GlyphRange> ranges; -}; - -class GlyphStoreThread : public GlyphStore::Observer { +class GlyphStoreTest { public: - GlyphStoreThread(FileSource* fileSource, GlyphStoreTestCallback callback) : callback_(std::move(callback)) { - util::ThreadContext::setFileSource(fileSource); - } - - void loadGlyphStore(const GlyphStoreParams& params) { - glyphStore_.reset(new GlyphStore()); - - glyphStore_->setObserver(this); - glyphStore_->setURL(params.url); - - ASSERT_FALSE(glyphStore_->hasGlyphRanges(params.stack, params.ranges)); - } - - void unloadGlyphStore() { - glyphStore_->setObserver(nullptr); - glyphStore_.reset(); - } - - void onGlyphsLoaded(const std::string&, const GlyphRange&) override { - callback_(glyphStore_.get(), nullptr); - } - - void onGlyphsError(const std::string&, const GlyphRange&, std::exception_ptr error) override { - callback_(glyphStore_.get(), error); - } - -private: - std::unique_ptr<GlyphStore> glyphStore_; - GlyphStoreTestCallback callback_; -}; + GlyphStoreTest(MockFileSource::Type type, const std::string& resource) + : fileSource(type, resource) {} -class GlyphStoreTest : public testing::Test { -protected: - void runTest(const GlyphStoreParams& params, FileSource* fileSource, GlyphStoreTestCallback callback) { - util::RunLoop loop; + util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular }; + util::RunLoop loop; + MockFileSource fileSource; + StubStyleObserver observer; + GlyphStore glyphStore; - async_ = std::make_unique<util::AsyncTask>([&]{ loop.stop(); }); - async_->unref(); + void run(const std::string& url, const std::string& fontStack, const std::set<GlyphRange>& glyphRanges) { + // Squelch logging. + Log::setObserver(std::make_unique<Log::NullObserver>()); - const util::ThreadContext context = {"Map", util::ThreadType::Map, util::ThreadPriority::Regular}; + util::ThreadContext::Set(&context); + util::ThreadContext::setFileSource(&fileSource); - util::Thread<GlyphStoreThread> tester(context, fileSource, callback); - tester.invoke(&GlyphStoreThread::loadGlyphStore, params); + glyphStore.setObserver(&observer); + glyphStore.setURL(url); + glyphStore.hasGlyphRanges(fontStack, glyphRanges); loop.run(); - - tester.invoke(&GlyphStoreThread::unloadGlyphStore); } - void stopTest() { - testDone = true; - async_->send(); + void end() { + loop.stop(); } - - bool isDone() const { - return testDone; - } - -private: - bool testDone = false; - - std::unique_ptr<util::AsyncTask> async_; }; -TEST_F(GlyphStoreTest, LoadingSuccess) { - GlyphStoreParams params = { - "test/fixtures/resources/glyphs.pbf", - "Test Stack", - {{0, 255}, {256, 511}} - }; - - auto callback = [this, ¶ms](GlyphStore* store, std::exception_ptr error) { - ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); +TEST(GlyphStore, LoadingSuccess) { + GlyphStoreTest test(MockFileSource::Success, ""); - // We need to check if the test is over because checking - // if the GlyphStore has glyphs below will cause more requests - // to happen and we don't want this endless loop. - if (isDone()) { - return; - } - - ASSERT_EQ(error, nullptr); + test.observer.glyphsError = [&] (const std::string&, const GlyphRange&, std::exception_ptr) { + FAIL(); + test.end(); + }; - if (!store->hasGlyphRanges(params.stack, params.ranges)) { + test.observer.glyphsLoaded = [&] (const std::string&, const GlyphRange&) { + if (!test.glyphStore.hasGlyphRanges("Test Stack", {{0, 255}, {256, 511}})) return; - } - ASSERT_FALSE(store->hasGlyphRanges("Foobar", params.ranges)); - ASSERT_FALSE(store->hasGlyphRanges("Foobar", {{512, 767}})); - ASSERT_FALSE(store->hasGlyphRanges("Test Stack", {{512, 767}})); - - auto fontStack = store->getFontStack(params.stack); + auto fontStack = test.glyphStore.getFontStack("Test Stack"); ASSERT_FALSE(fontStack->getMetrics().empty()); ASSERT_FALSE(fontStack->getSDFs().empty()); - stopTest(); + test.end(); }; - MockFileSource fileSource(MockFileSource::Success, ""); - runTest(params, &fileSource, callback); -} - -TEST_F(GlyphStoreTest, LoadingFail) { - GlyphStoreParams params = { + test.run( "test/fixtures/resources/glyphs.pbf", "Test Stack", - {{0, 255}, {256, 511}} - }; - - auto callback = [this, ¶ms](GlyphStore* store, std::exception_ptr error) { - ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + {{0, 255}, {256, 511}}); +} - if (isDone()) { - return; - } +TEST(GlyphStore, LoadingFail) { + GlyphStoreTest test(MockFileSource::RequestFail, "glyphs.pbf"); + test.observer.glyphsError = [&] (const std::string& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) { ASSERT_TRUE(error != nullptr); + ASSERT_EQ(fontStack, "Test Stack"); + ASSERT_EQ(glyphRange, GlyphRange(0, 255)); - auto fontStack = store->getFontStack(params.stack); - ASSERT_TRUE(fontStack->getMetrics().empty()); - ASSERT_TRUE(fontStack->getSDFs().empty()); - - for (const auto& range : params.ranges) { - ASSERT_FALSE(store->hasGlyphRanges(params.stack, {range})); - } + auto stack = test.glyphStore.getFontStack("Test Stack"); + ASSERT_TRUE(stack->getMetrics().empty()); + ASSERT_TRUE(stack->getSDFs().empty()); + ASSERT_FALSE(test.glyphStore.hasGlyphRanges("Test Stack", {{0, 255}})); - ASSERT_FALSE(store->hasGlyphRanges(params.stack, params.ranges)); - ASSERT_FALSE(store->hasGlyphRanges("Foobar", params.ranges)); - ASSERT_FALSE(store->hasGlyphRanges("Foobar", {{512, 767}})); - - stopTest(); + test.end(); }; - MockFileSource fileSource(MockFileSource::RequestFail, "glyphs.pbf"); - runTest(params, &fileSource, callback); -} - -TEST_F(GlyphStoreTest, LoadingCorrupted) { - GlyphStoreParams params = { + test.run( "test/fixtures/resources/glyphs.pbf", "Test Stack", - {{0, 255}, {256, 511}} - }; - - auto callback = [this, ¶ms](GlyphStore* store, std::exception_ptr error) { - ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + {{0, 255}}); +} - if (isDone()) { - return; - } +TEST(GlyphStore, LoadingCorrupted) { + GlyphStoreTest test(MockFileSource::RequestWithCorruptedData, "glyphs.pbf"); + test.observer.glyphsError = [&] (const std::string& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) { ASSERT_TRUE(error != nullptr); + ASSERT_EQ(fontStack, "Test Stack"); + ASSERT_EQ(glyphRange, GlyphRange(0, 255)); - auto fontStack = store->getFontStack(params.stack); - ASSERT_TRUE(fontStack->getMetrics().empty()); - ASSERT_TRUE(fontStack->getSDFs().empty()); + auto stack = test.glyphStore.getFontStack("Test Stack"); + ASSERT_TRUE(stack->getMetrics().empty()); + ASSERT_TRUE(stack->getSDFs().empty()); + ASSERT_FALSE(test.glyphStore.hasGlyphRanges("Test Stack", {{0, 255}})); - for (const auto& range : params.ranges) { - ASSERT_FALSE(store->hasGlyphRanges(params.stack, {range})); - } - - ASSERT_FALSE(store->hasGlyphRanges(params.stack, params.ranges)); - ASSERT_FALSE(store->hasGlyphRanges("Foobar", params.ranges)); - ASSERT_FALSE(store->hasGlyphRanges("Foobar", {{512, 767}})); - - stopTest(); + test.end(); }; - MockFileSource fileSource(MockFileSource::RequestWithCorruptedData, "glyphs.pbf"); - runTest(params, &fileSource, callback); -} - -TEST_F(GlyphStoreTest, LoadingCancel) { - GlyphStoreParams params = { + test.run( "test/fixtures/resources/glyphs.pbf", "Test Stack", - {{0, 255}, {256, 511}} - }; + {{0, 255}}); +} - auto callback = [this](GlyphStore*, std::exception_ptr) { +TEST(GlyphStore, LoadingCancel) { + GlyphStoreTest test(MockFileSource::SuccessWithDelay, "glyphs.pbf"); + + test.observer.glyphsLoaded = [&] (const std::string&, const GlyphRange&) { FAIL() << "Should never be called"; }; - MockFileSource fileSource(MockFileSource::SuccessWithDelay, "glyphs.pbf"); - fileSource.setOnRequestDelayedCallback([this]{ - stopTest(); + test.fileSource.setOnRequestDelayedCallback([&]{ + test.end(); }); - runTest(params, &fileSource, callback); -} -TEST_F(GlyphStoreTest, InvalidURL) { - GlyphStoreParams params = { - "foo bar", + test.run( + "test/fixtures/resources/glyphs.pbf", "Test Stack", - {{0, 255}, {256, 511}} - }; + {{0, 255}}); +} - auto callback = [this, ¶ms](GlyphStore* store, std::exception_ptr error) { +TEST(GlyphStore, InvalidURL) { + GlyphStoreTest test(MockFileSource::Success, ""); + + test.observer.glyphsError = [&] (const std::string&, const GlyphRange&, std::exception_ptr error) { ASSERT_TRUE(error != nullptr); - auto fontStack = store->getFontStack(params.stack); - ASSERT_TRUE(fontStack->getMetrics().empty()); - ASSERT_TRUE(fontStack->getSDFs().empty()); + auto stack = test.glyphStore.getFontStack("Test Stack"); + ASSERT_TRUE(stack->getMetrics().empty()); + ASSERT_TRUE(stack->getSDFs().empty()); - stopTest(); + test.end(); }; - MockFileSource fileSource(MockFileSource::Success, ""); - runTest(params, &fileSource, callback); + test.run( + "foo bar", + "Test Stack", + {{0, 255}}); } diff --git a/test/style/resource_loading.cpp b/test/style/resource_loading.cpp index 0476c53aa1..3edbf55e3d 100644 --- a/test/style/resource_loading.cpp +++ b/test/style/resource_loading.cpp @@ -2,6 +2,7 @@ #include "../fixtures/util.hpp" #include "../fixtures/mock_file_source.hpp" #include "../fixtures/mock_view.hpp" +#include "../fixtures/stub_style_observer.hpp" #include <mbgl/map/map_data.hpp> #include <mbgl/map/transform.hpp> @@ -15,72 +16,16 @@ using namespace mbgl; -class StyleObserver : public Style::Observer { -public: - void onGlyphsLoaded(const std::string& fontStack, const GlyphRange& glyphRange) override { - if (glyphsLoaded) glyphsLoaded(fontStack, glyphRange); - } - - void onGlyphsError(const std::string& fontStack, const GlyphRange& glyphRange, std::exception_ptr error) override { - if (glyphsError) glyphsError(fontStack, glyphRange, error); - } - - void onSpriteLoaded() override { - if (spriteLoaded) spriteLoaded(); - } - - void onSpriteError(std::exception_ptr error) override { - if (spriteError) spriteError(error); - } - - void onSourceLoaded(Source& source) override { - if (sourceLoaded) sourceLoaded(source); - } - - void onSourceError(Source& source, std::exception_ptr error) override { - if (sourceError) sourceError(source, error); - } - - void onTileLoaded(Source& source, const TileID& tileID, bool isNewTile) override { - if (tileLoaded) tileLoaded(source, tileID, isNewTile); - } - - void onTileError(Source& source, const TileID& tileID, std::exception_ptr error) override { - if (tileError) tileError(source, tileID, error); - } - - void onResourceLoaded() override { - if (resourceLoaded) resourceLoaded(); - }; - - void onResourceError(std::exception_ptr error) override { - if (resourceError) resourceError(error); - }; - - std::function<void (const std::string& fontStack, const GlyphRange&)> glyphsLoaded; - std::function<void (const std::string& fontStack, const GlyphRange&, std::exception_ptr)> glyphsError; - std::function<void ()> spriteLoaded; - std::function<void (std::exception_ptr)> spriteError; - std::function<void (Source&)> sourceLoaded; - std::function<void (Source&, std::exception_ptr)> sourceError; - std::function<void (Source&, const TileID&, bool isNewTile)> tileLoaded; - std::function<void (Source&, const TileID&, std::exception_ptr)> tileError; - std::function<void ()> resourceLoaded; - std::function<void (std::exception_ptr)> resourceError; - - std::function<void ()> fullyLoaded; -}; - class ResourceLoadingTest { public: ResourceLoadingTest(MockFileSource::Type type, const std::string& resource) : fileSource(type, resource) {} util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular }; - - MockFileSource fileSource; - StyleObserver observer; util::RunLoop loop; + MockFileSource fileSource; + StubStyleObserver observer; + std::function<void ()> onFullyLoaded; MapData data { MapMode::Still, GLContextMode::Unique, 1.0 }; MockView view; @@ -97,8 +42,8 @@ public: observer.resourceLoaded = [&] () { style.update(transform.getState(), texturePool); - if (style.isLoaded() && observer.fullyLoaded) { - observer.fullyLoaded(); + if (style.isLoaded() && onFullyLoaded) { + onFullyLoaded(); } }; @@ -125,7 +70,7 @@ TEST(ResourceLoading, Success) { FAIL() << util::toString(error); }; - test.observer.fullyLoaded = [&] () { + test.onFullyLoaded = [&] () { SUCCEED(); test.end(); }; @@ -306,7 +251,7 @@ TEST(ResourceLoading, GlyphsCorrupt) { TEST(ResourceLoading, UnusedSource) { ResourceLoadingTest test(MockFileSource::Success, ""); - test.observer.fullyLoaded = [&] () { + test.onFullyLoaded = [&] () { Source *usedSource = test.style.getSource("usedsource"); EXPECT_TRUE(usedSource); EXPECT_TRUE(usedSource->isLoaded()); @@ -326,7 +271,7 @@ TEST(ResourceLoading, UnusedSourceActiveViaClassUpdate) { test.data.addClass("visible"); - test.observer.fullyLoaded = [&] () { + test.onFullyLoaded = [&] () { Source *unusedSource = test.style.getSource("unusedsource"); EXPECT_TRUE(unusedSource); EXPECT_TRUE(unusedSource->isLoaded()); |