diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-05 14:54:50 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-12 13:03:14 -0800 |
commit | f9ebe54a6336431af98ebfb428d28a0513b2522d (patch) | |
tree | 83c01deae617e3e40bc0e47c32a36d6d24f7a23a /test | |
parent | 1e350b7ea485117cadc413d4d41062cf3c3c43a1 (diff) | |
download | qtlocation-mapboxgl-f9ebe54a6336431af98ebfb428d28a0513b2522d.tar.gz |
[core] Merge Sprite into SpriteStore
Diffstat (limited to 'test')
-rw-r--r-- | test/sprite/sprite.cpp | 173 | ||||
-rw-r--r-- | test/sprite/sprite_atlas.cpp | 6 | ||||
-rw-r--r-- | test/sprite/sprite_store.cpp | 179 | ||||
-rw-r--r-- | test/test.gypi | 1 |
4 files changed, 177 insertions, 182 deletions
diff --git a/test/sprite/sprite.cpp b/test/sprite/sprite.cpp deleted file mode 100644 index 7575cb3c9a..0000000000 --- a/test/sprite/sprite.cpp +++ /dev/null @@ -1,173 +0,0 @@ -#include "../fixtures/fixture_log_observer.hpp" -#include "../fixtures/mock_file_source.hpp" -#include "../fixtures/util.hpp" - -#include <mbgl/sprite/sprite.hpp> -#include <mbgl/util/run_loop.hpp> -#include <mbgl/util/thread.hpp> - -using namespace mbgl; - -using SpriteTestCallback = std::function<void(Sprite*, const Sprites&, std::exception_ptr)>; - -struct SpriteParams { - const std::string baseUrl; - const float pixelRatio; -}; - -class SpriteThread : public Sprite::Observer { -public: - SpriteThread(FileSource* fileSource, SpriteTestCallback callback) : callback_(callback) { - util::ThreadContext::setFileSource(fileSource); - } - - void loadSprite(const SpriteParams& params) { - sprite_.reset(new Sprite(params.baseUrl, params.pixelRatio)); - sprite_->setObserver(this); - } - - void unloadSprite() { - sprite_->setObserver(nullptr); - sprite_.reset(); - } - - void onSpriteLoaded(const Sprites& sprites) override { - callback_(sprite_.get(), sprites, nullptr); - } - - void onSpriteLoadingFailed(std::exception_ptr error) override { - callback_(sprite_.get(), Sprites(), error); - } - -private: - std::unique_ptr<Sprite> sprite_; - SpriteTestCallback callback_; -}; - -class SpriteTest : public testing::Test { -protected: - void runTest(const SpriteParams& params, FileSource* fileSource, SpriteTestCallback callback) { - util::RunLoop loop(uv_default_loop()); - - async_ = std::make_unique<uv::async>(loop.get(), [&] { loop.stop(); }); - async_->unref(); - - const util::ThreadContext context = {"Map", util::ThreadType::Map, util::ThreadPriority::Regular}; - - util::Thread<SpriteThread> tester(context, fileSource, callback); - tester.invoke(&SpriteThread::loadSprite, params); - - uv_run(loop.get(), UV_RUN_DEFAULT); - - tester.invoke(&SpriteThread::unloadSprite); - } - - void stopTest() { - async_->send(); - } - -private: - std::unique_ptr<uv::async> async_; -}; - -TEST_F(SpriteTest, LoadingSuccess) { - SpriteParams params = { - "test/fixtures/resources/sprite", - 1.0, - }; - - auto callback = [this, ¶ms](Sprite* sprite, const Sprites& sprites, std::exception_ptr error) { - ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - - ASSERT_TRUE(error == nullptr); - - ASSERT_TRUE(!sprites.empty()); - - ASSERT_EQ(sprite->pixelRatio, params.pixelRatio); - ASSERT_NE(sprite->pixelRatio, 1.5); - ASSERT_NE(sprite->pixelRatio, 2.0); - - ASSERT_TRUE(sprite->isLoaded()); - - stopTest(); - }; - - MockFileSource fileSource(MockFileSource::Success, ""); - runTest(params, &fileSource, callback); -} - -TEST_F(SpriteTest, LoadingFail) { - SpriteParams params = { - "test/fixtures/resources/sprite", - 1.0, - }; - - auto callback = [this, ¶ms](Sprite* sprite, const Sprites&, std::exception_ptr error) { - ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - - ASSERT_TRUE(error != nullptr); - - ASSERT_EQ(sprite->pixelRatio, params.pixelRatio); - ASSERT_NE(sprite->pixelRatio, 1.5); - ASSERT_NE(sprite->pixelRatio, 2.0); - - ASSERT_FALSE(sprite->isLoaded()); - - stopTest(); - }; - - MockFileSource fileSourceFailSpriteJSON(MockFileSource::RequestFail, "sprite.json"); - runTest(params, &fileSourceFailSpriteJSON, callback); - - MockFileSource fileSourceFailSpriteImage(MockFileSource::RequestFail, "sprite.png"); - runTest(params, &fileSourceFailSpriteImage, callback); - - MockFileSource fileSourceCorruptedSpriteJSON(MockFileSource::RequestWithCorruptedData, "sprite.json"); - runTest(params, &fileSourceCorruptedSpriteJSON, callback); - - MockFileSource fileSourceCorruptedSpriteImage(MockFileSource::RequestWithCorruptedData, "sprite.png"); - runTest(params, &fileSourceCorruptedSpriteImage, callback); -} - -TEST_F(SpriteTest, LoadingCancel) { - SpriteParams params = { - "test/fixtures/resources/sprite", - 1.0, - }; - - auto callback = [this](Sprite*, const Sprites&, std::exception_ptr) { - FAIL() << "Should never be called"; - }; - - MockFileSource fileSourceDelaySpriteJSON(MockFileSource::SuccessWithDelay, "sprite.json"); - fileSourceDelaySpriteJSON.setOnRequestDelayedCallback([this]{ - stopTest(); - }); - runTest(params, &fileSourceDelaySpriteJSON, callback); - - MockFileSource fileSourceDelaySpriteImage(MockFileSource::SuccessWithDelay, "sprite.png"); - fileSourceDelaySpriteImage.setOnRequestDelayedCallback([this]{ - stopTest(); - }); - runTest(params, &fileSourceDelaySpriteImage, callback); -} - -TEST_F(SpriteTest, InvalidURL) { - SpriteParams params = { - "foo bar", - 1.0, - }; - - auto callback = [this](Sprite* sprite, const Sprites&, std::exception_ptr error) { - ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); - - ASSERT_TRUE(error != nullptr); - - ASSERT_EQ(sprite->isLoaded(), false); - - stopTest(); - }; - - MockFileSource fileSource(MockFileSource::Success, ""); - runTest(params, &fileSource, callback); -} diff --git a/test/sprite/sprite_atlas.cpp b/test/sprite/sprite_atlas.cpp index 554ef16da9..e54a88dabe 100644 --- a/test/sprite/sprite_atlas.cpp +++ b/test/sprite/sprite_atlas.cpp @@ -15,7 +15,7 @@ TEST(Sprite, SpriteAtlas) { auto spriteParseResult = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"), util::read_file("test/fixtures/annotations/emerald.json")); - SpriteStore store; + SpriteStore store(1); store.setSprites(spriteParseResult.get<Sprites>()); SpriteAtlas atlas(63, 112, 1, store); @@ -90,7 +90,7 @@ TEST(Sprite, SpriteAtlasSize) { auto spriteParseResult = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"), util::read_file("test/fixtures/annotations/emerald.json")); - SpriteStore store; + SpriteStore store(1); store.setSprites(spriteParseResult.get<Sprites>()); SpriteAtlas atlas(63, 112, 1.4, store); @@ -124,7 +124,7 @@ TEST(Sprite, SpriteAtlasSize) { } TEST(Sprite, SpriteAtlasUpdates) { - SpriteStore store; + SpriteStore store(1); SpriteAtlas atlas(32, 32, 1, store); diff --git a/test/sprite/sprite_store.cpp b/test/sprite/sprite_store.cpp index 17fce62a2b..14ea6c957b 100644 --- a/test/sprite/sprite_store.cpp +++ b/test/sprite/sprite_store.cpp @@ -1,8 +1,12 @@ #include "../fixtures/util.hpp" #include "../fixtures/fixture_log_observer.hpp" +#include "../fixtures/mock_file_source.hpp" #include <mbgl/sprite/sprite_store.hpp> +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/thread.hpp> + using namespace mbgl; TEST(Sprite, SpriteStore) { @@ -13,7 +17,7 @@ TEST(Sprite, SpriteStore) { const auto sprite3 = std::make_shared<SpriteImage>(8, 8, 2, std::string(16 * 16 * 4, '\0')); using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>; - SpriteStore store; + SpriteStore store(1); // Adding single store.setSprite("one", sprite1); @@ -77,7 +81,7 @@ TEST(Sprite, SpriteStoreOtherPixelRatio) { const auto sprite1 = std::make_shared<SpriteImage>(8, 8, 1, std::string(8 * 8 * 4, '\0')); using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>; - SpriteStore store; + SpriteStore store(1); // Adding mismatched sprite image store.setSprite("one", sprite1); @@ -89,7 +93,7 @@ TEST(Sprite, SpriteStoreMultiple) { const auto sprite2 = std::make_shared<SpriteImage>(8, 8, 2, std::string(16 * 16 * 4, '\0')); using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>; - SpriteStore store; + SpriteStore store(1); store.setSprites({ { "one", sprite1 }, { "two", sprite2 }, @@ -108,7 +112,7 @@ TEST(Sprite, SpriteStoreReplace) { const auto sprite2 = std::make_shared<SpriteImage>(8, 8, 2, std::string(16 * 16 * 4, '\0')); using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>; - SpriteStore store; + SpriteStore store(1); store.setSprite("sprite", sprite1); EXPECT_EQ(sprite1, store.getSprite("sprite")); @@ -125,7 +129,7 @@ TEST(Sprite, SpriteStoreReplaceWithDifferentDimensions) { const auto sprite2 = std::make_shared<SpriteImage>(9, 9, 2, std::string(18 * 18 * 4, '\0')); using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>; - SpriteStore store; + SpriteStore store(1); store.setSprite("sprite", sprite1); store.setSprite("sprite", sprite2); @@ -141,3 +145,168 @@ TEST(Sprite, SpriteStoreReplaceWithDifferentDimensions) { EXPECT_EQ(Sprites({ { "sprite", sprite1 } }), store.getDirty()); } + +using SpriteTestCallback = std::function<void(SpriteStore*, std::exception_ptr)>; + +struct SpriteParams { + const std::string baseUrl; + const float pixelRatio; +}; + +class SpriteThread : public SpriteStore::Observer { +public: + SpriteThread(FileSource* fileSource, SpriteTestCallback callback) : callback_(callback) { + util::ThreadContext::setFileSource(fileSource); + } + + void loadSprite(const SpriteParams& params) { + spriteStore_.reset(new SpriteStore(params.pixelRatio)); + spriteStore_->setObserver(this); + spriteStore_->setURL(params.baseUrl); + } + + void unloadSprite() { + spriteStore_->setObserver(nullptr); + spriteStore_.reset(); + } + + void onSpriteLoaded() override { + callback_(spriteStore_.get(), nullptr); + } + + void onSpriteLoadingFailed(std::exception_ptr error) override { + callback_(spriteStore_.get(), error); + } + +private: + std::unique_ptr<SpriteStore> spriteStore_; + SpriteTestCallback callback_; +}; + +class SpriteTest : public testing::Test { +protected: + void runTest(const SpriteParams& params, FileSource* fileSource, SpriteTestCallback callback) { + util::RunLoop loop(uv_default_loop()); + + async_ = std::make_unique<uv::async>(loop.get(), [&] { loop.stop(); }); + async_->unref(); + + const util::ThreadContext context = {"Map", util::ThreadType::Map, util::ThreadPriority::Regular}; + + util::Thread<SpriteThread> tester(context, fileSource, callback); + tester.invoke(&SpriteThread::loadSprite, params); + + uv_run(loop.get(), UV_RUN_DEFAULT); + + tester.invoke(&SpriteThread::unloadSprite); + } + + void stopTest() { + async_->send(); + } + +private: + std::unique_ptr<uv::async> async_; +}; + +TEST_F(SpriteTest, LoadingSuccess) { + SpriteParams params = { + "test/fixtures/resources/sprite", + 1.0, + }; + + auto callback = [this, ¶ms](SpriteStore* spriteStore, std::exception_ptr error) { + ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + + ASSERT_TRUE(error == nullptr); + + ASSERT_TRUE(!spriteStore->getDirty().empty()); + + ASSERT_EQ(spriteStore->pixelRatio, params.pixelRatio); + ASSERT_NE(spriteStore->pixelRatio, 1.5); + ASSERT_NE(spriteStore->pixelRatio, 2.0); + + ASSERT_TRUE(spriteStore->isLoaded()); + + stopTest(); + }; + + MockFileSource fileSource(MockFileSource::Success, ""); + runTest(params, &fileSource, callback); +} + +TEST_F(SpriteTest, LoadingFail) { + SpriteParams params = { + "test/fixtures/resources/sprite", + 1.0, + }; + + auto callback = [this, ¶ms](SpriteStore* spriteStore, std::exception_ptr error) { + ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + + ASSERT_TRUE(error != nullptr); + + ASSERT_EQ(spriteStore->pixelRatio, params.pixelRatio); + ASSERT_NE(spriteStore->pixelRatio, 1.5); + ASSERT_NE(spriteStore->pixelRatio, 2.0); + + ASSERT_FALSE(spriteStore->isLoaded()); + + stopTest(); + }; + + MockFileSource fileSourceFailSpriteJSON(MockFileSource::RequestFail, "sprite.json"); + runTest(params, &fileSourceFailSpriteJSON, callback); + + MockFileSource fileSourceFailSpriteImage(MockFileSource::RequestFail, "sprite.png"); + runTest(params, &fileSourceFailSpriteImage, callback); + + MockFileSource fileSourceCorruptedSpriteJSON(MockFileSource::RequestWithCorruptedData, "sprite.json"); + runTest(params, &fileSourceCorruptedSpriteJSON, callback); + + MockFileSource fileSourceCorruptedSpriteImage(MockFileSource::RequestWithCorruptedData, "sprite.png"); + runTest(params, &fileSourceCorruptedSpriteImage, callback); +} + +TEST_F(SpriteTest, LoadingCancel) { + SpriteParams params = { + "test/fixtures/resources/sprite", + 1.0, + }; + + auto callback = [this](SpriteStore*, std::exception_ptr) { + FAIL() << "Should never be called"; + }; + + MockFileSource fileSourceDelaySpriteJSON(MockFileSource::SuccessWithDelay, "sprite.json"); + fileSourceDelaySpriteJSON.setOnRequestDelayedCallback([this]{ + stopTest(); + }); + runTest(params, &fileSourceDelaySpriteJSON, callback); + + MockFileSource fileSourceDelaySpriteImage(MockFileSource::SuccessWithDelay, "sprite.png"); + fileSourceDelaySpriteImage.setOnRequestDelayedCallback([this]{ + stopTest(); + }); + runTest(params, &fileSourceDelaySpriteImage, callback); +} + +TEST_F(SpriteTest, InvalidURL) { + SpriteParams params = { + "foo bar", + 1.0, + }; + + auto callback = [this](SpriteStore* spriteStore, std::exception_ptr error) { + ASSERT_TRUE(util::ThreadContext::currentlyOn(util::ThreadType::Map)); + + ASSERT_TRUE(error != nullptr); + + ASSERT_EQ(spriteStore->isLoaded(), false); + + stopTest(); + }; + + MockFileSource fileSource(MockFileSource::Success, ""); + runTest(params, &fileSource, callback); +} diff --git a/test/test.gypi b/test/test.gypi index e9678b0754..0d66b4c37c 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -86,7 +86,6 @@ 'style/pending_resources.cpp', 'style/resource_loading.cpp', - 'sprite/sprite.cpp', 'sprite/sprite_atlas.cpp', 'sprite/sprite_image.cpp', 'sprite/sprite_parser.cpp', |