diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-21 14:52:19 -0700 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2017-04-24 15:52:28 -0700 |
commit | 5dd98df50ba1210b1eef0d8d6655713a725f2995 (patch) | |
tree | b289bb3cbad5d1c3076816f4891a057d6089fc93 /test/sprite | |
parent | 6f708ac5458fe332e25ca398431928b7ff5ba404 (diff) | |
download | qtlocation-mapboxgl-5dd98df50ba1210b1eef0d8d6655713a725f2995.tar.gz |
[all] Rationalize style::Image
A style has a collection of images, just as it has collections of sources and layers.
* Name things appropriately
* Use std::unique_ptr
Diffstat (limited to 'test/sprite')
-rw-r--r-- | test/sprite/sprite_atlas.test.cpp | 117 | ||||
-rw-r--r-- | test/sprite/sprite_image.test.cpp | 57 | ||||
-rw-r--r-- | test/sprite/sprite_parser.test.cpp | 38 |
3 files changed, 55 insertions, 157 deletions
diff --git a/test/sprite/sprite_atlas.test.cpp b/test/sprite/sprite_atlas.test.cpp index fc0219efb9..08388f0a93 100644 --- a/test/sprite/sprite_atlas.test.cpp +++ b/test/sprite/sprite_atlas.test.cpp @@ -18,12 +18,13 @@ using namespace mbgl; TEST(SpriteAtlas, Basic) { FixtureLog log; - - auto spriteParseResult = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"), - util::read_file("test/fixtures/annotations/emerald.json")); - SpriteAtlas atlas({ 63, 112 }, 1); - atlas.setSprites(spriteParseResult); + + auto images = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"), + util::read_file("test/fixtures/annotations/emerald.json")); + for (auto& pair : images) { + atlas.addImage(pair.first, std::move(pair.second)); + } EXPECT_EQ(1.0f, atlas.getPixelRatio()); EXPECT_EQ(63u, atlas.getSize().width); @@ -74,11 +75,13 @@ TEST(SpriteAtlas, Basic) { } 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); - atlas.setSprites(spriteParseResult); + + auto images = parseSprite(util::read_file("test/fixtures/annotations/emerald.png"), + util::read_file("test/fixtures/annotations/emerald.json")); + for (auto& pair : images) { + atlas.addImage(pair.first, std::move(pair.second)); + } EXPECT_DOUBLE_EQ(1.4f, atlas.getPixelRatio()); EXPECT_EQ(63u, atlas.getSize().width); @@ -110,7 +113,7 @@ TEST(SpriteAtlas, Updates) { EXPECT_EQ(32u, atlas.getSize().width); EXPECT_EQ(32u, atlas.getSize().height); - atlas.setSprite("one", std::make_shared<SpriteImage>(PremultipliedImage({ 16, 12 }), 1)); + atlas.addImage("one", std::make_unique<style::Image>(PremultipliedImage({ 16, 12 }), 1)); auto one = *atlas.getIcon("one"); float imagePixelRatio = one.relativePixelRatio * atlas.getPixelRatio(); EXPECT_EQ(0, one.pos.x); @@ -129,45 +132,30 @@ TEST(SpriteAtlas, Updates) { test::checkImage("test/fixtures/sprite_atlas/updates_before", atlas.getAtlasImage()); - // Update sprite + // Update image PremultipliedImage image2({ 16, 12 }); for (size_t i = 0; i < image2.bytes(); i++) { image2.data.get()[i] = 255; } - auto newSprite = std::make_shared<SpriteImage>(std::move(image2), 1); - atlas.setSprite("one", newSprite); - ASSERT_EQ(newSprite, atlas.getSprite("one")); + atlas.addImage("one", std::make_unique<style::Image>(std::move(image2), 1)); test::checkImage("test/fixtures/sprite_atlas/updates_after", atlas.getAtlasImage()); } TEST(SpriteAtlas, AddRemove) { FixtureLog log; - - const auto sprite1 = std::make_shared<SpriteImage>(PremultipliedImage({ 16, 16 }), 2); - 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); - // Adding single - atlas.setSprite("one", sprite1); + atlas.addImage("one", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2)); + atlas.addImage("two", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2)); + atlas.addImage("three", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2)); - // Adding multiple - atlas.setSprite("two", sprite2); - atlas.setSprite("three", sprite3); + atlas.removeImage("one"); + atlas.removeImage("two"); - // Removing - atlas.removeSprite("one"); - atlas.removeSprite("two"); - - // Accessing - EXPECT_EQ(sprite3, atlas.getSprite("three")); - - EXPECT_TRUE(log.empty()); - - EXPECT_EQ(nullptr, atlas.getSprite("two")); - EXPECT_EQ(nullptr, atlas.getSprite("four")); + EXPECT_NE(nullptr, atlas.getImage("three")); + EXPECT_EQ(nullptr, atlas.getImage("two")); + EXPECT_EQ(nullptr, atlas.getImage("four")); EXPECT_EQ(1u, log.count({ EventSeverity::Info, @@ -181,9 +169,6 @@ TEST(SpriteAtlas, AddRemove) { int64_t(-1), "Can't find sprite named 'four'", })); - - // Overwriting - atlas.setSprite("three", sprite1); } TEST(SpriteAtlas, RemoveReleasesBinPackRect) { @@ -191,68 +176,40 @@ TEST(SpriteAtlas, RemoveReleasesBinPackRect) { SpriteAtlas atlas({ 36, 36 }, 1); - const auto big = std::make_shared<SpriteImage>(PremultipliedImage({ 32, 32 }), 1); - - atlas.setSprite("big", big); + atlas.addImage("big", std::make_unique<style::Image>(PremultipliedImage({ 32, 32 }), 1)); EXPECT_TRUE(atlas.getIcon("big")); - atlas.removeSprite("big"); + atlas.removeImage("big"); - atlas.setSprite("big", big); + atlas.addImage("big", std::make_unique<style::Image>(PremultipliedImage({ 32, 32 }), 1)); EXPECT_TRUE(atlas.getIcon("big")); - - EXPECT_EQ(big, atlas.getSprite("big")); EXPECT_TRUE(log.empty()); } TEST(SpriteAtlas, OtherPixelRatio) { FixtureLog log; - - const auto sprite1 = std::make_shared<SpriteImage>(PremultipliedImage({ 8, 8 }), 1); - SpriteAtlas atlas({ 32, 32 }, 1); // Adding mismatched sprite image - atlas.setSprite("one", sprite1); -} - -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); - - atlas.setSprites({ - { "one", sprite1 }, { "two", sprite2 }, - }); + atlas.addImage("one", std::make_unique<style::Image>(PremultipliedImage({ 8, 8 }), 2)); } TEST(SpriteAtlas, Replace) { FixtureLog log; - - 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); - atlas.setSprite("sprite", sprite1); - EXPECT_EQ(sprite1, atlas.getSprite("sprite")); - atlas.setSprite("sprite", sprite2); - EXPECT_EQ(sprite2, atlas.getSprite("sprite")); + atlas.addImage("sprite", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2)); + auto image = atlas.getImage("sprite"); + atlas.addImage("sprite", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2)); + EXPECT_NE(image, atlas.getImage("sprite")); } TEST(SpriteAtlas, ReplaceWithDifferentDimensions) { FixtureLog log; - - PremultipliedImage image({ 16, 16 }); - PremultipliedImage image2({ 18, 18 }); - 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); - atlas.setSprite("sprite", sprite1); - atlas.setSprite("sprite", sprite2); + atlas.addImage("sprite", std::make_unique<style::Image>(PremultipliedImage({ 16, 16 }), 2)); + atlas.addImage("sprite", std::make_unique<style::Image>(PremultipliedImage({ 18, 18 }), 2)); EXPECT_EQ(1u, log.count({ EventSeverity::Warning, @@ -260,8 +217,6 @@ TEST(SpriteAtlas, ReplaceWithDifferentDimensions) { int64_t(-1), "Can't change sprite dimensions for 'sprite'", })); - - EXPECT_EQ(sprite1, atlas.getSprite("sprite")); } class SpriteAtlasTest { @@ -292,14 +247,14 @@ 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)); + 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_shared<std::string>(util::read_file(resource.url)); + response.data = std::make_unique<std::string>(util::read_file(resource.url)); return response; } @@ -313,7 +268,7 @@ Response failedSpriteResponse(const Resource&) { Response corruptSpriteResponse(const Resource&) { Response response; - response.data = std::make_shared<std::string>("CORRUPT"); + response.data = std::make_unique<std::string>("CORRUPT"); return response; } diff --git a/test/sprite/sprite_image.test.cpp b/test/sprite/sprite_image.test.cpp deleted file mode 100644 index 97a37513ac..0000000000 --- a/test/sprite/sprite_image.test.cpp +++ /dev/null @@ -1,57 +0,0 @@ -#include <mbgl/test/util.hpp> - -#include <mbgl/sprite/sprite_image.hpp> -#include <mbgl/util/image.hpp> -#include <mbgl/util/exception.hpp> - -using namespace mbgl; - -TEST(Sprite, SpriteImageZeroWidth) { - PremultipliedImage image({ 0, 16 }); - try { - SpriteImage(std::move(image), 2.0); - FAIL() << "Expected exception"; - } catch (util::SpriteImageException& ex) { - EXPECT_STREQ("Sprite image dimensions may not be zero", ex.what()); - } -} - -TEST(Sprite, SpriteImageZeroHeight) { - PremultipliedImage image({ 16, 0 }); - try { - SpriteImage(std::move(image), 2.0); - FAIL() << "Expected exception"; - } catch (util::SpriteImageException& ex) { - EXPECT_STREQ("Sprite image dimensions may not be zero", ex.what()); - } -} - -TEST(Sprite, SpriteImageZeroRatio) { - PremultipliedImage image({ 16, 16 }); - try { - SpriteImage(std::move(image), 0.0); - FAIL() << "Expected exception"; - } catch (util::SpriteImageException& ex) { - EXPECT_STREQ("Sprite pixelRatio may not be <= 0", ex.what()); - } -} - -TEST(Sprite, SpriteImage) { - PremultipliedImage image({ 32, 24 }); - SpriteImage sprite(std::move(image), 2.0); - EXPECT_EQ(16, sprite.getWidth()); - EXPECT_EQ(32u, sprite.image.size.width); - EXPECT_EQ(12, sprite.getHeight()); - EXPECT_EQ(24u, sprite.image.size.height); - EXPECT_EQ(2, sprite.pixelRatio); -} - -TEST(Sprite, SpriteImageFractionalRatio) { - PremultipliedImage image({ 20, 12 }); - SpriteImage sprite(std::move(image), 1.5); - EXPECT_EQ(float(20.0 / 1.5), sprite.getWidth()); - EXPECT_EQ(20u, sprite.image.size.width); - EXPECT_EQ(float(12.0 / 1.5), sprite.getHeight()); - EXPECT_EQ(12u, sprite.image.size.height); - EXPECT_EQ(1.5, sprite.pixelRatio); -} diff --git a/test/sprite/sprite_parser.test.cpp b/test/sprite/sprite_parser.test.cpp index 18b4b2a749..bb8e71db95 100644 --- a/test/sprite/sprite_parser.test.cpp +++ b/test/sprite/sprite_parser.test.cpp @@ -2,7 +2,7 @@ #include <mbgl/test/fixture_log_observer.hpp> #include <mbgl/sprite/sprite_parser.hpp> -#include <mbgl/sprite/sprite_image.hpp> +#include <mbgl/style/image.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/string.hpp> @@ -27,19 +27,19 @@ TEST(Sprite, SpriteImageCreationInvalid) { ASSERT_EQ(200u, image_1x.size.width); ASSERT_EQ(299u, image_1x.size.height); - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 0, 16, 1, false)); // width == 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 16, 0, 1, false)); // height == 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, -1, 16, 1, false)); // width < 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 16, -1, 1, false)); // height < 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 1, 1, 0, false)); // ratio == 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 1, 1, -1, false)); // ratio < 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 1, 1, 23, false)); // ratio too large - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 2048, 16, 1, false)); // too wide - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 16, 1025, 1, false)); // too tall - ASSERT_EQ(nullptr, createSpriteImage(image_1x, -1, 0, 16, 16, 1, false)); // srcX < 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, -1, 16, 16, 1, false)); // srcY < 0 - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, image_1x.size.width + 1, 16, 1, false)); // right edge out of bounds - ASSERT_EQ(nullptr, createSpriteImage(image_1x, 0, 0, 16, image_1x.size.height + 1, 1, false)); // bottom edge out of bounds + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 0, 16, 1, false)); // width == 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, 0, 1, false)); // height == 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, -1, 16, 1, false)); // width < 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, -1, 1, false)); // height < 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 1, 1, 0, false)); // ratio == 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 1, 1, -1, false)); // ratio < 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 1, 1, 23, false)); // ratio too large + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 2048, 16, 1, false)); // too wide + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, 1025, 1, false)); // too tall + ASSERT_EQ(nullptr, createStyleImage(image_1x, -1, 0, 16, 16, 1, false)); // srcX < 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, -1, 16, 16, 1, false)); // srcY < 0 + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, image_1x.size.width + 1, 16, 1, false)); // right edge out of bounds + ASSERT_EQ(nullptr, createStyleImage(image_1x, 0, 0, 16, image_1x.size.height + 1, 1, false)); // bottom edge out of bounds EXPECT_EQ(1u, log.count({ EventSeverity::Error, @@ -141,7 +141,7 @@ TEST(Sprite, SpriteImageCreation1x) { ASSERT_EQ(299u, image_1x.size.height); { // "museum_icon":{"x":177,"y":187,"width":18,"height":18,"pixelRatio":1,"sdf":false} - const auto sprite = createSpriteImage(image_1x, 177, 187, 18, 18, 1, false); + const auto sprite = createStyleImage(image_1x, 177, 187, 18, 18, 1, false); ASSERT_TRUE(sprite.get()); EXPECT_EQ(18, sprite->getWidth()); EXPECT_EQ(18, sprite->getHeight()); @@ -157,7 +157,7 @@ TEST(Sprite, SpriteImageCreation2x) { const PremultipliedImage image_2x = decodeImage(util::read_file("test/fixtures/annotations/emerald@2x.png")); // "museum_icon":{"x":354,"y":374,"width":36,"height":36,"pixelRatio":2,"sdf":false} - const auto sprite = createSpriteImage(image_2x, 354, 374, 36, 36, 2, false); + const auto sprite = createStyleImage(image_2x, 354, 374, 36, 36, 2, false); ASSERT_TRUE(sprite.get()); EXPECT_EQ(18, sprite->getWidth()); EXPECT_EQ(18, sprite->getHeight()); @@ -172,7 +172,7 @@ TEST(Sprite, SpriteImageCreation1_5x) { const PremultipliedImage image_2x = decodeImage(util::read_file("test/fixtures/annotations/emerald@2x.png")); // "museum_icon":{"x":354,"y":374,"width":36,"height":36,"pixelRatio":2,"sdf":false} - const auto sprite = createSpriteImage(image_2x, 354, 374, 36, 36, 1.5, false); + const auto sprite = createStyleImage(image_2x, 354, 374, 36, 36, 1.5, false); ASSERT_TRUE(sprite.get()); EXPECT_EQ(24, sprite->getWidth()); EXPECT_EQ(24, sprite->getHeight()); @@ -183,7 +183,7 @@ TEST(Sprite, SpriteImageCreation1_5x) { sprite->image); // "hospital_icon":{"x":314,"y":518,"width":36,"height":36,"pixelRatio":2,"sdf":false} - const auto sprite2 = createSpriteImage(image_2x, 314, 518, 35, 35, 1.5, false); + const auto sprite2 = createStyleImage(image_2x, 314, 518, 35, 35, 1.5, false); ASSERT_TRUE(sprite2.get()); EXPECT_EQ(float(35 / 1.5), sprite2->getWidth()); EXPECT_EQ(float(35 / 1.5), sprite2->getHeight()); @@ -280,7 +280,7 @@ TEST(Sprite, SpriteParsing) { names); { - auto sprite = images.find("generic-metro")->second; + auto& sprite = images.find("generic-metro")->second; EXPECT_EQ(18, sprite->getWidth()); EXPECT_EQ(18, sprite->getHeight()); EXPECT_EQ(18u, sprite->image.size.width); |