Add additional sprite to sprite loader test (#2974)

This commit is contained in:
Bart Louwers
2024-10-28 16:56:49 +01:00
committed by GitHub
parent 1a63194078
commit ab00edfab1
5 changed files with 272 additions and 23 deletions

View File

@@ -29,11 +29,13 @@ public:
std::function<void(std::exception_ptr)> spriteError;
};
std::string defaultSpritePath = "test/fixtures/resources/sprite";
class SpriteLoaderTest {
public:
SpriteLoaderTest()
SpriteLoaderTest(float pixelRatio = 1)
: threadPool(Scheduler::GetBackground(), uniqueID),
spriteLoader(1, threadPool) {}
spriteLoader(pixelRatio, threadPool) {}
~SpriteLoaderTest() {
threadPool.waitForEmpty();
@@ -46,6 +48,7 @@ public:
StubSpriteLoaderObserver observer;
TaggedScheduler threadPool;
SpriteLoader spriteLoader;
std::string spritePath = defaultSpritePath; // default
void run() {
// Squelch logging.
@@ -53,7 +56,7 @@ public:
spriteLoader.setObserver(&observer);
Sprite sprite = Sprite("default", "test/fixtures/resources/sprite");
Sprite sprite = Sprite("default", spritePath);
spriteLoader.load(sprite, fileSource);
loop.run();
@@ -62,18 +65,22 @@ public:
void end() { loop.stop(); }
};
Response successfulSpriteImageResponse(const Resource& resource) {
EXPECT_EQ("test/fixtures/resources/sprite.png", resource.url);
Response response;
response.data = std::make_unique<std::string>(util::read_file(resource.url));
return response;
auto successfulSpriteImageResponse(const std::string& spritePath = defaultSpritePath, float pixelRatio = 1.0f) {
return [spritePath, pixelRatio](const Resource& resource) {
EXPECT_EQ(spritePath + (pixelRatio == 2.0 ? "@2x" : "") + ".png", resource.url);
Response response;
response.data = std::make_unique<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_unique<std::string>(util::read_file(resource.url));
return response;
auto successfulSpriteJSONResponse(const std::string& spritePath = defaultSpritePath, float pixelRatio = 1.0f) {
return [spritePath, pixelRatio](const Resource& resource) {
EXPECT_EQ(spritePath + (pixelRatio == 2.0 ? "@2x" : "") + ".json", resource.url);
Response response;
response.data = std::make_unique<std::string>(util::read_file(resource.url));
return response;
};
}
Response failedSpriteResponse(const Resource&) {
@@ -88,29 +95,43 @@ Response corruptSpriteResponse(const Resource&) {
return response;
}
TEST(SpriteLoader, LoadingSuccess) {
SpriteLoaderTest test;
class SpriteLoaderParametrized : public testing::TestWithParam<std::tuple<std::string, size_t, float>> {};
test.fileSource.spriteImageResponse = successfulSpriteImageResponse;
test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse;
TEST_P(SpriteLoaderParametrized, LoadingSuccess) {
const auto [spritePath, expectedImages_, pixelRatio_] = GetParam();
const auto expectedImages = expectedImages_; // lambda cannot capture structured binding yet
const auto pixelRatio = pixelRatio_;
SpriteLoaderTest test{pixelRatio};
test.spritePath = spritePath;
test.fileSource.spriteImageResponse = successfulSpriteImageResponse(spritePath, pixelRatio);
test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse(spritePath, pixelRatio);
test.observer.spriteError = [&](std::exception_ptr error) {
FAIL() << util::toString(error);
test.end();
};
test.observer.spriteLoaded = [&](std::vector<Immutable<style::Image::Impl>> images) {
EXPECT_EQ(images.size(), 418u);
test.observer.spriteLoaded = [&, expectedImages](std::vector<Immutable<style::Image::Impl>> images) {
EXPECT_EQ(images.size(), expectedImages);
test.end();
};
test.run();
}
INSTANTIATE_TEST_SUITE_P(
SpriteLoaderSprites,
SpriteLoaderParametrized,
::testing::Values(std::make_tuple("test/fixtures/resources/sprite", 418, 1.0f),
std::make_tuple("test/fixtures/resources/versatiles-sprite/sprite", 112, 1.0f),
std::make_tuple("test/fixtures/resources/versatiles-sprite/sprite", 112, 2.0f)));
TEST(SpriteLoader, JSONLoadingFail) {
SpriteLoaderTest test;
test.fileSource.spriteImageResponse = successfulSpriteImageResponse;
test.fileSource.spriteImageResponse = successfulSpriteImageResponse();
test.fileSource.spriteJSONResponse = failedSpriteResponse;
test.observer.spriteError = [&](std::exception_ptr error) {
@@ -126,7 +147,7 @@ TEST(SpriteLoader, ImageLoadingFail) {
SpriteLoaderTest test;
test.fileSource.spriteImageResponse = failedSpriteResponse;
test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse;
test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse();
test.observer.spriteError = [&](std::exception_ptr error) {
EXPECT_TRUE(error != nullptr);
@@ -140,7 +161,7 @@ TEST(SpriteLoader, ImageLoadingFail) {
TEST(SpriteLoader, JSONLoadingCorrupted) {
SpriteLoaderTest test;
test.fileSource.spriteImageResponse = successfulSpriteImageResponse;
test.fileSource.spriteImageResponse = successfulSpriteImageResponse();
test.fileSource.spriteJSONResponse = corruptSpriteResponse;
test.observer.spriteError = [&](std::exception_ptr error) {
@@ -156,7 +177,7 @@ TEST(SpriteLoader, ImageLoadingCorrupted) {
SpriteLoaderTest test;
test.fileSource.spriteImageResponse = corruptSpriteResponse;
test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse;
test.fileSource.spriteJSONResponse = successfulSpriteJSONResponse();
test.observer.spriteError = [&](std::exception_ptr error) {
EXPECT_TRUE(error != nullptr);