Verified Commit de70bf7a authored by James Westman's avatar James Westman
Browse files

Add a simple ShumateNetworkTileSource test

parent 060fc44f
......@@ -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)
......
#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 ();
}
/* 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);
}
char *testserver_start (void);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment