[libshumate] Add a simple ShumateNetworkTileSource test



commit de70bf7afff9a4fc08f75a3f65fb04872477db5c
Author: James Westman <james jwestman net>
Date:   Tue Mar 23 21:10:22 2021 -0500

    Add a simple ShumateNetworkTileSource test

 tests/meson.build           | 17 +++++++++++-
 tests/network-tile-source.c | 64 ++++++++++++++++++++++++++++++++++++++++++
 tests/test-tile-server.c    | 68 +++++++++++++++++++++++++++++++++++++++++++++
 tests/test-tile-server.h    |  1 +
 4 files changed, 149 insertions(+), 1 deletion(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index 3176ef6..a9eaea6 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -12,14 +12,29 @@ tests = [
   'marker',
   'view',
   'marker-layer',
+  'network-tile-source',
   'viewport',
 ]
 
+test_utils_sources = [
+  'test-tile-server.c',
+]
+testutils_lib = shared_library(
+  'testutils',
+  test_utils_sources,
+  dependencies: libshumate_dep,
+)
+testutils_dep = declare_dependency(
+  link_with: testutils_lib,
+  dependencies: libshumate_deps,
+  sources: test_utils_sources,
+)
+
 foreach test : tests
   executable = executable(
     test,
     '@0@.c'.format(test),
-    dependencies: libshumate_dep,
+    dependencies: [libshumate_dep, testutils_dep],
   )
 
   test(test, executable, env: test_env)
diff --git a/tests/network-tile-source.c b/tests/network-tile-source.c
new file mode 100644
index 0000000..d2aa402
--- /dev/null
+++ b/tests/network-tile-source.c
@@ -0,0 +1,64 @@
+#include <gtk/gtk.h>
+#include <shumate/shumate.h>
+#include "test-tile-server.h"
+
+
+static ShumateMapSource *
+create_tile_source (const char *uri)
+{
+  // TODO: Disable the file cache to make sure we're really testing the network
+  // source
+
+  g_autofree char *template = g_strdup_printf ("%s/#X#/#Y#/#Z#", uri);
+
+  return SHUMATE_MAP_SOURCE (
+    shumate_network_tile_source_new_full ("testsrc",
+                                          "Test Source",
+                                          NULL, NULL,
+                                          0, 20,
+                                          256, SHUMATE_MAP_PROJECTION_MERCATOR,
+                                          template)
+  );
+}
+
+
+static void
+on_tile_filled (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+  g_autoptr(GError) error = NULL;
+  GMainLoop *loop = user_data;
+
+  shumate_map_source_fill_tile_finish ((ShumateMapSource *) object, res, &error);
+  g_assert_no_error (error);
+
+  g_main_loop_quit (loop);
+}
+
+static void
+test_network_tile_source_tile (void)
+{
+  g_autofree char *uri = testserver_start ();
+  g_autoptr(ShumateMapSource) source = create_tile_source (uri);
+  g_autoptr(ShumateTile) tile = shumate_tile_new_full (0, 0, 256, 0);
+  g_autoptr(GMainLoop) loop = NULL;
+
+  g_object_ref_sink (tile);
+
+  /* Fill the tile */
+  loop = g_main_loop_new (NULL, TRUE);
+  shumate_map_source_fill_tile_async (source, tile, NULL, on_tile_filled, loop);
+  g_main_loop_run (loop);
+
+  g_assert_nonnull (shumate_tile_get_texture (tile));
+}
+
+int
+main (int argc, char *argv[])
+{
+  g_test_init (&argc, &argv, NULL);
+  gtk_init ();
+
+  g_test_add_func ("/network-tile-source/tile", test_network_tile_source_tile);
+
+  return g_test_run ();
+}
diff --git a/tests/test-tile-server.c b/tests/test-tile-server.c
new file mode 100644
index 0000000..412f447
--- /dev/null
+++ b/tests/test-tile-server.c
@@ -0,0 +1,68 @@
+/* Serves tiles over HTTP for the network source tests. */
+
+
+#include <libsoup/soup.h>
+#include <gdk/gdk.h>
+
+
+static cairo_status_t
+write_func (void *bytearray, const unsigned char *data, unsigned int length)
+{
+  g_byte_array_append ((GByteArray *) bytearray, data, length);
+  return CAIRO_STATUS_SUCCESS;
+}
+
+
+static GBytes *
+generate_image ()
+{
+  GByteArray *bytearray = g_byte_array_new ();
+  cairo_surface_t *surface;
+  cairo_t *cr;
+
+  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 256, 256);
+
+  cr = cairo_create (surface);
+  cairo_set_source_rgb (cr, 1, 0, 0);
+  cairo_paint (cr);
+  cairo_destroy (cr);
+
+  cairo_surface_write_to_png_stream (surface, write_func, bytearray);
+  cairo_surface_destroy (surface);
+
+  return g_byte_array_free_to_bytes (bytearray);
+}
+
+
+static void
+server_callback (SoupServer *server,
+                 SoupMessage *msg,
+                 const char *path,
+                 GHashTable *query,
+                 SoupClientContext *client,
+                 gpointer user_data)
+{
+  gsize data_size;
+  const char *data = g_bytes_get_data ((GBytes *) user_data, &data_size);
+
+  soup_message_set_response (msg, "image/png", SOUP_MEMORY_STATIC, data, data_size);
+  soup_message_set_status (msg, 200);
+}
+
+
+char *
+testserver_start (void)
+{
+  SoupServer *server = soup_server_new (NULL, NULL);
+  g_autoptr(GError) error = NULL;
+  g_autoptr(GSList) uris = NULL;
+
+  soup_server_add_handler (server, NULL, server_callback, generate_image (), (GDestroyNotify) g_bytes_unref);
+
+  soup_server_listen_local (server, 0, 0, &error);
+  g_assert_no_error (error);
+
+  uris = soup_server_get_uris (server);
+  g_assert_true (g_slist_length (uris) >= 1);
+  return soup_uri_to_string (uris->data, FALSE);
+}
diff --git a/tests/test-tile-server.h b/tests/test-tile-server.h
new file mode 100644
index 0000000..5c75e32
--- /dev/null
+++ b/tests/test-tile-server.h
@@ -0,0 +1 @@
+char *testserver_start (void);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]