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 | |
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')
-rw-r--r-- | test/api/annotations.test.cpp | 30 | ||||
-rw-r--r-- | test/api/query.test.cpp | 8 | ||||
-rw-r--r-- | test/map/map.test.cpp | 10 | ||||
-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 | ||||
-rw-r--r-- | test/style/style_image.test.cpp | 52 | ||||
-rw-r--r-- | test/text/quads.test.cpp | 4 |
8 files changed, 132 insertions, 184 deletions
diff --git a/test/api/annotations.test.cpp b/test/api/annotations.test.cpp index 4bcaa03897..97ccaae684 100644 --- a/test/api/annotations.test.cpp +++ b/test/api/annotations.test.cpp @@ -3,7 +3,7 @@ #include <mbgl/util/default_thread_pool.hpp> #include <mbgl/annotation/annotation.hpp> -#include <mbgl/sprite/sprite_image.hpp> +#include <mbgl/style/image.hpp> #include <mbgl/map/map.hpp> #include <mbgl/map/backend_scope.hpp> #include <mbgl/gl/headless_backend.hpp> @@ -16,9 +16,9 @@ using namespace mbgl; namespace { -std::shared_ptr<SpriteImage> namedMarker(const std::string &name) { +std::unique_ptr<style::Image> namedMarker(const std::string& name) { PremultipliedImage image = decodeImage(util::read_file("test/fixtures/sprites/" + name)); - return std::make_shared<SpriteImage>(std::move(image), 1.0); + return std::make_unique<style::Image>(std::move(image), 1.0); } class AnnotationTest { @@ -43,7 +43,7 @@ TEST(Annotations, SymbolAnnotation) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); test.map.addAnnotation(SymbolAnnotation { Point<double>(0, 0), "default_marker" }); test.checkRendering("point_annotation"); @@ -158,7 +158,7 @@ TEST(Annotations, AddMultiple) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); test.map.addAnnotation(SymbolAnnotation { Point<double> { -10, 0 }, "default_marker" }); test::render(test.map, test.view); @@ -185,8 +185,8 @@ TEST(Annotations, UpdateSymbolAnnotationGeometry) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); - test.map.addAnnotationIcon("flipped_marker", namedMarker("flipped_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("flipped_marker", namedMarker("flipped_marker.png")); AnnotationID point = test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" }); test::render(test.map, test.view); @@ -199,8 +199,8 @@ TEST(Annotations, UpdateSymbolAnnotationIcon) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); - test.map.addAnnotationIcon("flipped_marker", namedMarker("flipped_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("flipped_marker", namedMarker("flipped_marker.png")); AnnotationID point = test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" }); test::render(test.map, test.view); @@ -281,7 +281,7 @@ TEST(Annotations, RemovePoint) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); AnnotationID point = test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" }); test::render(test.map, test.view); @@ -320,7 +320,7 @@ TEST(Annotations, SwitchStyle) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" }); test::render(test.map, test.view); @@ -333,7 +333,7 @@ TEST(Annotations, QueryRenderedFeatures) { AnnotationTest test; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 0 }, "default_marker" }); test.map.addAnnotation(SymbolAnnotation { Point<double> { 0, 50 }, "default_marker" }); @@ -357,7 +357,7 @@ TEST(Annotations, QueryFractionalZoomLevels) { auto box = ScreenBox { {}, { double(viewSize.width), double(viewSize.height) } }; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); std::vector<mbgl::AnnotationID> ids; for (int longitude = 0; longitude < 10; ++longitude) { @@ -389,7 +389,7 @@ TEST(Annotations, VisibleFeatures) { auto box = ScreenBox { {}, { double(viewSize.width), double(viewSize.height) } }; test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); test.map.setLatLngZoom({ 5, 5 }, 3); std::vector<mbgl::AnnotationID> ids; @@ -442,7 +442,7 @@ TEST(Annotations, DebugSparse) { test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json")); test.map.setDebug(MapDebugOptions::TileBorders); test.map.setZoom(1); - test.map.addAnnotationIcon("default_marker", namedMarker("default_marker.png")); + test.map.addAnnotationImage("default_marker", namedMarker("default_marker.png")); test.map.addAnnotation(SymbolAnnotation { Point<double>(10, 10), "default_marker" }); test.checkRendering("debug_sparse"); diff --git a/test/api/query.test.cpp b/test/api/query.test.cpp index 532cc14d28..77dc095484 100644 --- a/test/api/query.test.cpp +++ b/test/api/query.test.cpp @@ -3,12 +3,12 @@ #include <mbgl/gl/headless_backend.hpp> #include <mbgl/gl/offscreen_view.hpp> #include <mbgl/util/default_thread_pool.hpp> -#include <mbgl/sprite/sprite_image.hpp> #include <mbgl/test/stub_file_source.hpp> #include <mbgl/test/util.hpp> #include <mbgl/util/image.hpp> #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> +#include <mbgl/style/image.hpp> #include <mbgl/style/source.hpp> using namespace mbgl; @@ -19,11 +19,9 @@ namespace { class QueryTest { public: QueryTest() { - auto decoded = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")); - auto image = std::make_unique<SpriteImage>(std::move(decoded), 1.0); - map.setStyleJSON(util::read_file("test/fixtures/api/query_style.json")); - map.addImage("test-icon", std::move(image)); + map.addImage("test-icon", std::make_unique<style::Image>( + decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")), 1.0)); test::render(map, view); } diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 2f3883fb1f..c24f736fcd 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -9,7 +9,6 @@ #include <mbgl/gl/offscreen_view.hpp> #include <mbgl/gl/context.hpp> #include <mbgl/util/default_thread_pool.hpp> -#include <mbgl/sprite/sprite_image.hpp> #include <mbgl/storage/network_status.hpp> #include <mbgl/storage/default_file_source.hpp> #include <mbgl/storage/online_file_source.hpp> @@ -17,6 +16,7 @@ #include <mbgl/util/io.hpp> #include <mbgl/util/run_loop.hpp> #include <mbgl/util/async_task.hpp> +#include <mbgl/style/image.hpp> #include <mbgl/style/layers/background_layer.hpp> #include <mbgl/util/color.hpp> @@ -479,8 +479,8 @@ TEST(Map, AddImage) { Map map(test.backend, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); auto decoded1 = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")); auto decoded2 = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")); - auto image1 = std::make_unique<SpriteImage>(std::move(decoded1), 1.0); - auto image2 = std::make_unique<SpriteImage>(std::move(decoded2), 1.0); + auto image1 = std::make_unique<style::Image>(std::move(decoded1), 1.0); + auto image2 = std::make_unique<style::Image>(std::move(decoded2), 1.0); // No-op. map.addImage("test-icon", std::move(image1)); @@ -495,7 +495,7 @@ TEST(Map, RemoveImage) { Map map(test.backend, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); auto decoded = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")); - auto image = std::make_unique<SpriteImage>(std::move(decoded), 1.0); + auto image = std::make_unique<style::Image>(std::move(decoded), 1.0); map.setStyleJSON(util::read_file("test/fixtures/api/icon_style.json")); map.addImage("test-icon", std::move(image)); @@ -508,7 +508,7 @@ TEST(Map, GetImage) { Map map(test.backend, test.view.getSize(), 1, test.fileSource, test.threadPool, MapMode::Still); auto decoded = decodeImage(util::read_file("test/fixtures/sprites/default_marker.png")); - auto image = std::make_unique<SpriteImage>(std::move(decoded), 1.0); + auto image = std::make_unique<style::Image>(std::move(decoded), 1.0); map.setStyleJSON(util::read_file("test/fixtures/api/icon_style.json")); map.addImage("test-icon", std::move(image)); 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); diff --git a/test/style/style_image.test.cpp b/test/style/style_image.test.cpp new file mode 100644 index 0000000000..319120df83 --- /dev/null +++ b/test/style/style_image.test.cpp @@ -0,0 +1,52 @@ +#include <mbgl/test/util.hpp> + +#include <mbgl/style/image.hpp> +#include <mbgl/util/image.hpp> +#include <mbgl/util/exception.hpp> + +using namespace mbgl; + +TEST(StyleImage, ZeroWidth) { + try { + style::Image(PremultipliedImage({ 0, 16 }), 2.0); + FAIL() << "Expected exception"; + } catch (util::SpriteImageException& ex) { + EXPECT_STREQ("Sprite image dimensions may not be zero", ex.what()); + } +} + +TEST(StyleImage, ZeroHeight) { + try { + style::Image(PremultipliedImage({ 16, 0 }), 2.0); + FAIL() << "Expected exception"; + } catch (util::SpriteImageException& ex) { + EXPECT_STREQ("Sprite image dimensions may not be zero", ex.what()); + } +} + +TEST(StyleImage, ZeroRatio) { + try { + style::Image(PremultipliedImage({ 16, 16 }), 0.0); + FAIL() << "Expected exception"; + } catch (util::SpriteImageException& ex) { + EXPECT_STREQ("Sprite pixelRatio may not be <= 0", ex.what()); + } +} + +TEST(StyleImage, Retina) { + style::Image image(PremultipliedImage({ 32, 24 }), 2.0); + EXPECT_EQ(16, image.getWidth()); + EXPECT_EQ(32u, image.image.size.width); + EXPECT_EQ(12, image.getHeight()); + EXPECT_EQ(24u, image.image.size.height); + EXPECT_EQ(2, image.pixelRatio); +} + +TEST(StyleImage, FractionalRatio) { + style::Image image(PremultipliedImage({ 20, 12 }), 1.5); + EXPECT_EQ(float(20.0 / 1.5), image.getWidth()); + EXPECT_EQ(20u, image.image.size.width); + EXPECT_EQ(float(12.0 / 1.5), image.getHeight()); + EXPECT_EQ(12u, image.image.size.height); + EXPECT_EQ(1.5, image.pixelRatio); +} diff --git a/test/text/quads.test.cpp b/test/text/quads.test.cpp index 91f2ea7af8..83fd249535 100644 --- a/test/text/quads.test.cpp +++ b/test/text/quads.test.cpp @@ -14,7 +14,7 @@ TEST(getIconQuads, normal) { Anchor anchor(2.0, 3.0, 0.0, 0.5f, 0); SpriteAtlasElement image = { Rect<uint16_t>( 0, 0, 15, 11 ), - std::make_shared<const SpriteImage>(PremultipliedImage({1,1}), 1.0), + style::Image(PremultipliedImage({1,1}), 1.0), { 0, 0 }, 1.0f }; @@ -47,7 +47,7 @@ TEST(getIconQuads, style) { Anchor anchor(0.0, 0.0, 0.0, 0.5f, 0); SpriteAtlasElement image = { Rect<uint16_t>( 0, 0, 20, 20 ), - std::make_shared<const SpriteImage>(PremultipliedImage({1,1}), 1.0), + style::Image(PremultipliedImage({1,1}), 1.0), { 0, 0 }, 1.0f }; |