summaryrefslogtreecommitdiff
path: root/test/api/custom_layer.test.cpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2016-09-28 11:45:33 +0200
committerKonstantin Käfer <mail@kkaefer.com>2016-09-28 16:34:22 +0200
commit3f3fc7b7723698e44427e2a14a2f4906832800bf (patch)
tree5acadfa4d77817c41f612c89c93925a149cbcfc0 /test/api/custom_layer.test.cpp
parenta8b007daa0e85ea4b1a4898fd591d55d0117cc85 (diff)
downloadqtlocation-mapboxgl-3f3fc7b7723698e44427e2a14a2f4906832800bf.tar.gz
[test] add .test.cpp suffix to test case files
Diffstat (limited to 'test/api/custom_layer.test.cpp')
-rw-r--r--test/api/custom_layer.test.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/test/api/custom_layer.test.cpp b/test/api/custom_layer.test.cpp
new file mode 100644
index 0000000000..2dded19256
--- /dev/null
+++ b/test/api/custom_layer.test.cpp
@@ -0,0 +1,118 @@
+#include <mbgl/test/util.hpp>
+
+#include <mbgl/gl/gl.hpp>
+#include <mbgl/map/map.hpp>
+#include <mbgl/platform/default/headless_display.hpp>
+#include <mbgl/platform/default/headless_view.hpp>
+#include <mbgl/storage/default_file_source.hpp>
+#include <mbgl/style/layers/custom_layer.hpp>
+#include <mbgl/style/layers/fill_layer.hpp>
+#include <mbgl/util/io.hpp>
+#include <mbgl/util/mat4.hpp>
+#include <mbgl/util/run_loop.hpp>
+
+using namespace mbgl;
+using namespace mbgl::style;
+
+// Note that custom layers need to draw geometry with a z value of 1 to take advantage of
+// depth-based fragment culling.
+static const GLchar* vertexShaderSource = R"MBGL_SHADER(
+attribute vec2 a_pos;
+void main() {
+ gl_Position = vec4(a_pos, 1, 1);
+}
+)MBGL_SHADER";
+
+static const GLchar* fragmentShaderSource = R"MBGL_SHADER(
+void main() {
+ gl_FragColor = vec4(0, 0.5, 0, 0.5);
+}
+)MBGL_SHADER";
+
+// Not using any mbgl-specific stuff (other than a basic error-checking macro) in the
+// layer implementation because it is intended to reflect how someone using custom layers
+// might actually write their own implementation.
+
+class TestLayer {
+public:
+ ~TestLayer() {
+ if (program) {
+ MBGL_CHECK_ERROR(glDeleteBuffers(1, &buffer));
+ MBGL_CHECK_ERROR(glDetachShader(program, vertexShader));
+ MBGL_CHECK_ERROR(glDetachShader(program, fragmentShader));
+ MBGL_CHECK_ERROR(glDeleteShader(vertexShader));
+ MBGL_CHECK_ERROR(glDeleteShader(fragmentShader));
+ MBGL_CHECK_ERROR(glDeleteProgram(program));
+ }
+ }
+
+ void initialize() {
+ program = MBGL_CHECK_ERROR(glCreateProgram());
+ vertexShader = MBGL_CHECK_ERROR(glCreateShader(GL_VERTEX_SHADER));
+ fragmentShader = MBGL_CHECK_ERROR(glCreateShader(GL_FRAGMENT_SHADER));
+
+ MBGL_CHECK_ERROR(glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr));
+ MBGL_CHECK_ERROR(glCompileShader(vertexShader));
+ MBGL_CHECK_ERROR(glAttachShader(program, vertexShader));
+ MBGL_CHECK_ERROR(glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr));
+ MBGL_CHECK_ERROR(glCompileShader(fragmentShader));
+ MBGL_CHECK_ERROR(glAttachShader(program, fragmentShader));
+ MBGL_CHECK_ERROR(glLinkProgram(program));
+ a_pos = glGetAttribLocation(program, "a_pos");
+
+ GLfloat triangle[] = { 0, 0.5, 0.5, -0.5, -0.5, -0.5 };
+ MBGL_CHECK_ERROR(glGenBuffers(1, &buffer));
+ MBGL_CHECK_ERROR(glBindBuffer(GL_ARRAY_BUFFER, buffer));
+ MBGL_CHECK_ERROR(glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(GLfloat), triangle, GL_STATIC_DRAW));
+ }
+
+ void render() {
+ MBGL_CHECK_ERROR(glUseProgram(program));
+ MBGL_CHECK_ERROR(glBindBuffer(GL_ARRAY_BUFFER, buffer));
+ MBGL_CHECK_ERROR(glEnableVertexAttribArray(a_pos));
+ MBGL_CHECK_ERROR(glVertexAttribPointer(a_pos, 2, GL_FLOAT, GL_FALSE, 0, nullptr));
+ MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLE_STRIP, 0, 3));
+ }
+
+ GLuint program = 0;
+ GLuint vertexShader = 0;
+ GLuint fragmentShader = 0;
+ GLuint buffer = 0;
+ GLuint a_pos = 0;
+};
+
+TEST(CustomLayer, Basic) {
+ util::RunLoop loop;
+
+ auto display = std::make_shared<mbgl::HeadlessDisplay>();
+ HeadlessView view(display, 1);
+
+#ifdef MBGL_ASSET_ZIP
+ // Regenerate with `cd test/fixtures/api/ && zip -r assets.zip assets/`
+ DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets.zip");
+#else
+ DefaultFileSource fileSource(":memory:", "test/fixtures/api/assets");
+#endif
+
+ Map map(view, fileSource, MapMode::Still);
+ map.setStyleJSON(util::read_file("test/fixtures/api/water.json"));
+ map.setLatLngZoom({ 37.8, -122.5 }, 10);
+ map.addLayer(std::make_unique<CustomLayer>(
+ "custom",
+ [] (void* context) {
+ reinterpret_cast<TestLayer*>(context)->initialize();
+ },
+ [] (void* context, const CustomLayerRenderParameters&) {
+ reinterpret_cast<TestLayer*>(context)->render();
+ },
+ [] (void* context) {
+ delete reinterpret_cast<TestLayer*>(context);
+ }, new TestLayer()));
+
+ auto layer = std::make_unique<FillLayer>("landcover", "mapbox");
+ layer->setSourceLayer("landcover");
+ layer->setFillColor(Color{ 1.0, 1.0, 0.0, 1.0 });
+ map.addLayer(std::move(layer));
+
+ test::checkImage("test/fixtures/custom_layer/basic", test::render(map), 0.0006, 0.1);
+}