diff options
Diffstat (limited to 'test/sprite/sprite_store.cpp')
-rw-r--r-- | test/sprite/sprite_store.cpp | 134 |
1 files changed, 98 insertions, 36 deletions
diff --git a/test/sprite/sprite_store.cpp b/test/sprite/sprite_store.cpp index ca5f0ed9e4..08977eac4f 100644 --- a/test/sprite/sprite_store.cpp +++ b/test/sprite/sprite_store.cpp @@ -1,10 +1,12 @@ #include "../fixtures/util.hpp" #include "../fixtures/fixture_log_observer.hpp" -#include "../fixtures/mock_file_source.hpp" +#include "../fixtures/stub_file_source.hpp" #include "../fixtures/stub_style_observer.hpp" #include <mbgl/sprite/sprite_store.hpp> #include <mbgl/util/run_loop.hpp> +#include <mbgl/util/string.hpp> +#include <mbgl/util/io.hpp> #include <utility> @@ -151,17 +153,16 @@ TEST(SpriteStore, ReplaceWithDifferentDimensions) { class SpriteStoreTest { public: - SpriteStoreTest(MockFileSource::Type type, const std::string& resource) - : fileSource(type, resource), - spriteStore(1.0) {} + SpriteStoreTest() + : spriteStore(1.0) {} util::ThreadContext context { "Map", util::ThreadType::Map, util::ThreadPriority::Regular }; util::RunLoop loop; - MockFileSource fileSource; + StubFileSource fileSource; StubStyleObserver observer; SpriteStore spriteStore; - void run(const std::string& url) { + void run() { // Squelch logging. Log::setObserver(std::make_unique<Log::NullObserver>()); @@ -169,7 +170,7 @@ public: util::ThreadContext::setFileSource(&fileSource); spriteStore.setObserver(&observer); - spriteStore.setURL(url); + spriteStore.setURL("test/fixtures/resources/sprite"); loop.run(); } @@ -179,70 +180,131 @@ public: } }; +Response successfulSpriteImageResponse(const Resource& resource) { + EXPECT_EQ("test/fixtures/resources/sprite.png", resource.url); + Response response; + response.data = std::make_shared<std::string>(util::read_file(resource.url)); + return response; +}; + +Response successfulSpriteJSONResponse(const Resource& resource) { + EXPECT_EQ("test/fixtures/resources/sprite.json", resource.url); + Response response; + response.data = std::make_shared<std::string>(util::read_file(resource.url)); + return response; +}; + +Response failedSpriteResponse(const Resource&) { + Response response; + response.error = std::make_unique<Response::Error>( + Response::Error::Reason::Other, + "Failed by the test case"); + return response; +}; + +Response corruptSpriteResponse(const Resource&) { + Response response; + response.data = std::make_shared<std::string>("CORRUPT"); + return response; +}; + TEST(SpriteStore, LoadingSuccess) { - SpriteStoreTest test(MockFileSource::Success, ""); + SpriteStoreTest test; - test.observer.spriteError = [&] (std::exception_ptr) { - FAIL(); + test.fileSource.spriteImageResponse = successfulSpriteImageResponse; + test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse; + + test.observer.spriteError = [&] (std::exception_ptr error) { + FAIL() << util::toString(error); test.end(); }; test.observer.spriteLoaded = [&] () { - ASSERT_TRUE(!test.spriteStore.getDirty().empty()); - ASSERT_EQ(test.spriteStore.pixelRatio, 1.0); - ASSERT_TRUE(test.spriteStore.isLoaded()); + EXPECT_TRUE(!test.spriteStore.getDirty().empty()); + EXPECT_EQ(1.0, test.spriteStore.pixelRatio); + EXPECT_TRUE(test.spriteStore.isLoaded()); test.end(); }; - test.run("test/fixtures/resources/sprite"); + test.run(); } -TEST(SpriteStore, LoadingFail) { - SpriteStoreTest test(MockFileSource::RequestFail, "sprite.json"); +TEST(SpriteStore, JSONLoadingFail) { + SpriteStoreTest test; + + test.fileSource.spriteImageResponse = successfulSpriteImageResponse; + test.fileSource.spriteJSONResponse = failedSpriteResponse; test.observer.spriteError = [&] (std::exception_ptr error) { - ASSERT_TRUE(error != nullptr); - ASSERT_FALSE(test.spriteStore.isLoaded()); + EXPECT_TRUE(error != nullptr); + EXPECT_EQ("Failed by the test case", util::toString(error)); + EXPECT_FALSE(test.spriteStore.isLoaded()); test.end(); }; - test.run("test/fixtures/resources/sprite"); + test.run(); } -TEST(SpriteStore, LoadingCorrupted) { - SpriteStoreTest test(MockFileSource::RequestWithCorruptedData, "sprite.json"); +TEST(SpriteStore, ImageLoadingFail) { + SpriteStoreTest test; + + test.fileSource.spriteImageResponse = failedSpriteResponse; + test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse; test.observer.spriteError = [&] (std::exception_ptr error) { - ASSERT_TRUE(error != nullptr); - ASSERT_FALSE(test.spriteStore.isLoaded()); + EXPECT_TRUE(error != nullptr); + EXPECT_EQ("Failed by the test case", util::toString(error)); + EXPECT_FALSE(test.spriteStore.isLoaded()); test.end(); }; - test.run("test/fixtures/resources/sprite"); + test.run(); } -TEST(SpriteStore, LoadingCancel) { - SpriteStoreTest test(MockFileSource::Success, "sprite.json"); +TEST(SpriteStore, JSONLoadingCorrupted) { + SpriteStoreTest test; - test.observer.spriteLoaded = [&] () { - FAIL() << "Should never be called"; - }; + test.fileSource.spriteImageResponse = successfulSpriteImageResponse; + test.fileSource.spriteJSONResponse = corruptSpriteResponse; - test.fileSource.requestEnqueuedCallback = [&]{ + test.observer.spriteError = [&] (std::exception_ptr error) { + EXPECT_TRUE(error != nullptr); + EXPECT_EQ("Failed to parse JSON: Invalid value. at offset 0", util::toString(error)); + EXPECT_FALSE(test.spriteStore.isLoaded()); test.end(); }; - test.run("test/fixtures/resources/sprite"); + test.run(); } -TEST(SpriteStore, InvalidURL) { - SpriteStoreTest test(MockFileSource::Success, ""); +TEST(SpriteStore, ImageLoadingCorrupted) { + SpriteStoreTest test; + + test.fileSource.spriteImageResponse = corruptSpriteResponse; + test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse; test.observer.spriteError = [&] (std::exception_ptr error) { - ASSERT_TRUE(error != nullptr); - ASSERT_EQ(test.spriteStore.isLoaded(), false); + EXPECT_TRUE(error != nullptr); + // Not asserting on platform-specific error text. + EXPECT_FALSE(test.spriteStore.isLoaded()); + test.end(); + }; + + test.run(); +} + +TEST(SpriteStore, LoadingCancel) { + SpriteStoreTest test; + + test.fileSource.spriteImageResponse = + test.fileSource.spriteJSONResponse = [&] (const Resource&) { test.end(); + return Response(); + }; + + test.observer.spriteLoaded = [&] () { + FAIL() << "Should never be called"; }; - test.run("foo bar"); + test.run(); } |