summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-10-26 16:39:25 -0700
committerKonstantin Käfer <mail@kkaefer.com>2016-11-01 18:41:52 +0100
commit9e3839781fdf1b1c6a2d61a5de9b2c7ddd68e9ed (patch)
treecde9eb4e99341e5200ca5a06a4a1d0fc602c9232 /test
parente2981c890e9a758c4dee703fa9e25e5d9eff65ce (diff)
downloadqtlocation-mapboxgl-9e3839781fdf1b1c6a2d61a5de9b2c7ddd68e9ed.tar.gz
[core] convert SpriteAtlas to use managed texture handling
Diffstat (limited to 'test')
-rw-r--r--test/sprite/sprite_atlas.test.cpp68
1 files changed, 30 insertions, 38 deletions
diff --git a/test/sprite/sprite_atlas.test.cpp b/test/sprite/sprite_atlas.test.cpp
index 231e2b1246..c9576013d4 100644
--- a/test/sprite/sprite_atlas.test.cpp
+++ b/test/sprite/sprite_atlas.test.cpp
@@ -21,14 +21,6 @@ auto readImage(const std::string& name) {
return decodeImage(util::read_file(name));
}
-auto imageFromAtlas(const SpriteAtlas& atlas) {
- const size_t bytes = atlas.getTextureWidth() * atlas.getTextureHeight() * 4;
- auto data = std::make_unique<uint8_t[]>(bytes);
- const auto src = reinterpret_cast<const uint8_t*>(atlas.getData());
- std::copy(src, src + bytes, data.get());
- return PremultipliedImage{ { atlas.getTextureWidth(), atlas.getTextureHeight() }, std::move(data) };
-}
-
} // namespace
TEST(SpriteAtlas, Basic) {
@@ -37,17 +29,12 @@ TEST(SpriteAtlas, Basic) {
auto spriteParseResult = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"),
util::read_file("test/fixtures/annotations/emerald.json"));
- SpriteAtlas atlas(63, 112, 1);
+ SpriteAtlas atlas({ 63, 112 }, 1);
atlas.setSprites(spriteParseResult.get<Sprites>());
EXPECT_EQ(1.0f, atlas.getPixelRatio());
- EXPECT_EQ(63, atlas.getWidth());
- EXPECT_EQ(112, atlas.getHeight());
- EXPECT_EQ(63, atlas.getTextureWidth());
- EXPECT_EQ(112, atlas.getTextureHeight());
-
- // Image hasn't been created yet.
- EXPECT_FALSE(atlas.getData());
+ EXPECT_EQ(63u, atlas.getSize().width);
+ EXPECT_EQ(112u, atlas.getSize().height);
auto metro = *atlas.getImage("metro", SpritePatternMode::Single);
EXPECT_EQ(0, metro.pos.x);
@@ -60,7 +47,8 @@ TEST(SpriteAtlas, Basic) {
EXPECT_EQ(18u, metro.spriteImage->image.size.height);
EXPECT_EQ(1.0f, metro.spriteImage->pixelRatio);
- EXPECT_TRUE(atlas.getData());
+ EXPECT_EQ(63u, atlas.getAtlasImage().size.width);
+ EXPECT_EQ(112u, atlas.getAtlasImage().size.height);
auto pos = *atlas.getPosition("metro", SpritePatternMode::Single);
EXPECT_DOUBLE_EQ(18, pos.size[0]);
@@ -87,21 +75,19 @@ TEST(SpriteAtlas, Basic) {
EXPECT_EQ(20, metro2.pos.w);
EXPECT_EQ(20, metro2.pos.h);
- EXPECT_EQ(readImage("test/fixtures/annotations/result-spriteatlas.png"), imageFromAtlas(atlas));
+ EXPECT_EQ(readImage("test/fixtures/annotations/result-spriteatlas.png"), atlas.getAtlasImage());
}
TEST(SpriteAtlas, Size) {
auto spriteParseResult = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"),
util::read_file("test/fixtures/annotations/emerald.json"));
- SpriteAtlas atlas(63, 112, 1.4);
+ SpriteAtlas atlas({ 63, 112 }, 1.4);
atlas.setSprites(spriteParseResult.get<Sprites>());
EXPECT_DOUBLE_EQ(1.4f, atlas.getPixelRatio());
- EXPECT_EQ(63, atlas.getWidth());
- EXPECT_EQ(112, atlas.getHeight());
- EXPECT_EQ(89, atlas.getTextureWidth());
- EXPECT_EQ(157, atlas.getTextureHeight());
+ EXPECT_EQ(63u, atlas.getSize().width);
+ EXPECT_EQ(112u, atlas.getSize().height);
auto metro = *atlas.getImage("metro", SpritePatternMode::Single);
EXPECT_EQ(0, metro.pos.x);
@@ -114,18 +100,20 @@ TEST(SpriteAtlas, Size) {
EXPECT_EQ(18u, metro.spriteImage->image.size.height);
EXPECT_EQ(1.0f, metro.spriteImage->pixelRatio);
+ // Now the image was created lazily.
+ EXPECT_EQ(89u, atlas.getAtlasImage().size.width);
+ EXPECT_EQ(157u, atlas.getAtlasImage().size.height);
+
EXPECT_EQ(readImage("test/fixtures/annotations/result-spriteatlassize.png"),
- imageFromAtlas(atlas));
+ atlas.getAtlasImage());
}
TEST(SpriteAtlas, Updates) {
- SpriteAtlas atlas(32, 32, 1);
+ SpriteAtlas atlas({ 32, 32 }, 1);
EXPECT_EQ(1.0f, atlas.getPixelRatio());
- EXPECT_EQ(32, atlas.getWidth());
- EXPECT_EQ(32, atlas.getHeight());
- EXPECT_EQ(32, atlas.getTextureWidth());
- EXPECT_EQ(32, atlas.getTextureHeight());
+ EXPECT_EQ(32u, atlas.getSize().width);
+ EXPECT_EQ(32u, atlas.getSize().height);
atlas.setSprite("one", std::make_shared<SpriteImage>(PremultipliedImage({ 16, 12 }), 1));
auto one = *atlas.getImage("one", SpritePatternMode::Single);
@@ -139,8 +127,12 @@ TEST(SpriteAtlas, Updates) {
EXPECT_EQ(12u, one.spriteImage->image.size.height);
EXPECT_EQ(1.0f, one.spriteImage->pixelRatio);
+ // Now the image was created lazily.
+ EXPECT_EQ(32u, atlas.getAtlasImage().size.width);
+ EXPECT_EQ(32u, atlas.getAtlasImage().size.height);
+
EXPECT_EQ(readImage("test/fixtures/annotations/result-spriteatlas-empty.png"),
- imageFromAtlas(atlas));
+ atlas.getAtlasImage());
// Update sprite
PremultipliedImage image2({ 16, 12 });
@@ -153,13 +145,13 @@ TEST(SpriteAtlas, Updates) {
// Atlas texture hasn't changed yet.
EXPECT_EQ(readImage("test/fixtures/annotations/result-spriteatlas-empty.png"),
- imageFromAtlas(atlas));
+ atlas.getAtlasImage());
atlas.updateDirty();
// Now the atlas texture has changed.
EXPECT_EQ(readImage("test/fixtures/annotations/result-spriteatlas-updated.png"),
- imageFromAtlas(atlas));
+ atlas.getAtlasImage());
}
TEST(SpriteAtlas, AddRemove) {
@@ -169,7 +161,7 @@ TEST(SpriteAtlas, AddRemove) {
const auto sprite2 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
const auto sprite3 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
- SpriteAtlas atlas(32, 32, 1);
+ SpriteAtlas atlas({ 32, 32 }, 1);
// Adding single
atlas.setSprite("one", sprite1);
@@ -212,7 +204,7 @@ TEST(SpriteAtlas, OtherPixelRatio) {
const auto sprite1 = std::make_shared<SpriteImage>(PremultipliedImage({ 8, 8 }), 1);
- SpriteAtlas atlas(32, 32, 1);
+ SpriteAtlas atlas({ 32, 32 }, 1);
// Adding mismatched sprite image
atlas.setSprite("one", sprite1);
@@ -222,7 +214,7 @@ TEST(SpriteAtlas, Multiple) {
const auto sprite1 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
const auto sprite2 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
- SpriteAtlas atlas(32, 32, 1);
+ SpriteAtlas atlas({ 32, 32 }, 1);
atlas.setSprites({
{ "one", sprite1 }, { "two", sprite2 },
@@ -235,7 +227,7 @@ TEST(SpriteAtlas, Replace) {
const auto sprite1 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
const auto sprite2 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
- SpriteAtlas atlas(32, 32, 1);
+ SpriteAtlas atlas({ 32, 32 }, 1);
atlas.setSprite("sprite", sprite1);
EXPECT_EQ(sprite1, atlas.getSprite("sprite"));
@@ -251,7 +243,7 @@ TEST(SpriteAtlas, ReplaceWithDifferentDimensions) {
const auto sprite1 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2);
const auto sprite2 = std::make_shared<SpriteImage>(PremultipliedImage({ 18, 18 }), 2);
- SpriteAtlas atlas(32, 32, 1);
+ SpriteAtlas atlas({ 32, 32 }, 1);
atlas.setSprite("sprite", sprite1);
atlas.setSprite("sprite", sprite2);
@@ -273,7 +265,7 @@ public:
util::RunLoop loop;
StubFileSource fileSource;
StubStyleObserver observer;
- SpriteAtlas spriteAtlas { 32, 32, 1 };
+ SpriteAtlas spriteAtlas{ { 32, 32 }, 1 };
void run() {
// Squelch logging.