Files
MapLibreParticleLab/test/style/style_image.test.cpp
Bart Louwers 2a8ebc4906 core: rename mbgl directories to mln (#4511)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-08-22 20:50:14 +02:00

141 lines
4.6 KiB
C++

#include <mln/test/util.hpp>
#include <mln/style/image.hpp>
#include <mln/util/image.hpp>
#include <mln/util/exception.hpp>
using namespace mln;
TEST(StyleImage, ZeroWidth) {
try {
style::Image("test", PremultipliedImage({0, 16}), 2.0f);
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("dimensions may not be zero", ex.what());
}
}
TEST(StyleImage, ZeroHeight) {
try {
style::Image("test", PremultipliedImage({16, 0}), 2.0f);
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("dimensions may not be zero", ex.what());
}
}
TEST(StyleImage, ZeroRatio) {
try {
style::Image("test", PremultipliedImage({16, 16}), 0.0f);
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("pixelRatio may not be <= 0", ex.what());
}
}
TEST(StyleImage, Retina) {
style::Image image("test", PremultipliedImage({32, 24}), 2.0f);
EXPECT_EQ(32u, image.getImage().size.width);
EXPECT_EQ(24u, image.getImage().size.height);
EXPECT_EQ(2, image.getPixelRatio());
}
TEST(StyleImage, FractionalRatio) {
style::Image image("test", PremultipliedImage({20, 12}), 1.5f);
EXPECT_EQ(20u, image.getImage().size.width);
EXPECT_EQ(12u, image.getImage().size.height);
EXPECT_EQ(1.5, image.getPixelRatio());
}
TEST(StyleImage, InvalidStretchX) {
// out of left bound
try {
style::Image("test", PremultipliedImage({16, 16}), 1, {{-1.0f, 3.0f}});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("stretchX is out of bounds or overlapping", ex.what());
}
// overlapping
try {
style::Image("test", PremultipliedImage({16, 16}), 1, {{0.0f, 3.0f}, {2.0f, 4.0f}});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("stretchX is out of bounds or overlapping", ex.what());
}
}
TEST(StyleImage, InvalidStretchY) {
// out of bottom bound
try {
style::Image("test", PremultipliedImage({16, 16}), 1, {}, {{14.0f, 20.0f}});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("stretchX is out of bounds or overlapping", ex.what());
}
// must be sorted
try {
style::Image("test", PremultipliedImage({16, 16}), 1, {}, {{4.0f, 8.0f}, {2.0f, 3.0f}});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("stretchX is out of bounds or overlapping", ex.what());
}
}
TEST(StyleImage, InvalidContent) {
// bottom right out of bounds
try {
style::Image("test",
PremultipliedImage({16, 16}),
1,
{},
{},
style::ImageContent{.left = 0, .top = 0, .right = 24, .bottom = 28});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("content area is invalid", ex.what());
}
// bottom right < top left
try {
style::Image("test",
PremultipliedImage({16, 16}),
1,
{},
{},
style::ImageContent{.left = 14, .top = 14, .right = 12, .bottom = 10});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("content area is invalid", ex.what());
}
// top left out of bounds
try {
style::Image("test",
PremultipliedImage({16, 16}),
1,
{},
{},
style::ImageContent{.left = -2, .top = -8, .right = 12, .bottom = 10});
FAIL() << "Expected exception";
} catch (util::StyleImageException& ex) {
EXPECT_STREQ("content area is invalid", ex.what());
}
}
TEST(StyleImage, StretchContent) {
style::Image image("test",
PremultipliedImage({16, 16}),
1,
{{2.0f, 14.0f}},
{{0.0f, 4.0f}, {12.0f, 16.0f}},
style::ImageContent{.left = 2, .top = 2, .right = 14, .bottom = 14});
EXPECT_EQ(16u, image.getImage().size.width);
EXPECT_EQ(16u, image.getImage().size.height);
EXPECT_EQ(1.0, image.getPixelRatio());
EXPECT_EQ((style::ImageStretches{{2.0f, 14.0f}}), image.getStretchX());
EXPECT_EQ((style::ImageStretches{{0.0f, 4.0f}, {12.0f, 16.0f}}), image.getStretchY());
EXPECT_EQ((style::ImageContent{2, 2, 14, 14}), image.getContent());
}