From d6430995adec161ffbb052b3a04f171fed57d9f7 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 13 May 2023 22:21:23 -0400 Subject: Improve gdk test coverage Test the loadable icon implementation of GdkTexture. --- testsuite/gdk/texture.c | 200 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/testsuite/gdk/texture.c b/testsuite/gdk/texture.c index 76112b2449..f3991fc7e7 100644 --- a/testsuite/gdk/texture.c +++ b/testsuite/gdk/texture.c @@ -1,6 +1,7 @@ #include #include "gdk/gdkmemorytextureprivate.h" +#include "gdk/gdktextureprivate.h" static void compare_pixels (int width, @@ -23,6 +24,36 @@ compare_pixels (int width, } } +static void +compare_textures (GdkTexture *texture1, + GdkTexture *texture2) +{ + int width, height; + gsize stride; + guchar *data1; + guchar *data2; + + g_assert_true (gdk_texture_get_width (texture1) == gdk_texture_get_width (texture2)); + g_assert_true (gdk_texture_get_height (texture1) == gdk_texture_get_height (texture2)); + + width = gdk_texture_get_width (texture1); + height = gdk_texture_get_height (texture1); + stride = 4 * width; + + data1 = g_new0 (guchar, stride * height); + gdk_texture_download (texture1, data1, stride); + + data2 = g_new0 (guchar, stride * height); + gdk_texture_download (texture2, data2, stride); + + compare_pixels (width, height, + data1, stride, + data2, stride); + + g_free (data1); + g_free (data2); +} + static void test_texture_from_pixbuf (void) { @@ -106,6 +137,30 @@ test_texture_save_to_png (void) g_object_unref (file); g_assert_no_error (error); + compare_textures (texture, texture2); + + g_object_unref (texture); + g_object_unref (texture2); +} + +static void +test_texture_save_to_tiff (void) +{ + GdkTexture *texture; + GError *error = NULL; + GFile *file; + GdkTexture *texture2; + + texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png"); + + gdk_texture_save_to_tiff (texture, "test.tiff"); + file = g_file_new_for_path ("test.tiff"); + texture2 = gdk_texture_new_from_file (file, &error); + g_object_unref (file); + g_assert_no_error (error); + + compare_textures (texture, texture2); + g_object_unref (texture); g_object_unref (texture2); } @@ -160,6 +215,146 @@ test_texture_subtexture (void) g_object_unref (texture); } +static void +test_texture_icon (void) +{ + GdkTexture *texture; + GdkTexture *texture2; + GInputStream *stream; + GdkPixbuf *pixbuf; + GError *error = NULL; + + texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png"); + + stream = g_loadable_icon_load (G_LOADABLE_ICON (texture), 16, NULL, NULL, &error); + g_assert_no_error (error); + g_assert_nonnull (stream); + + pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &error); + g_assert_no_error (error); + g_assert_nonnull (pixbuf); + + texture2 = gdk_texture_new_for_pixbuf (pixbuf); + + compare_textures (texture, texture2); + + g_object_unref (texture2); + g_object_unref (pixbuf); + g_object_unref (stream); + g_object_unref (texture); +} + +static void +icon_loaded (GObject *source, + GAsyncResult *result, + gpointer data) +{ + GdkTexture *texture = GDK_TEXTURE (source); + GError *error = NULL; + GdkTexture *texture2; + GdkPixbuf *pixbuf; + GInputStream *stream; + gboolean *done = (gboolean *)data; + + stream = g_loadable_icon_load_finish (G_LOADABLE_ICON (texture), result, NULL, &error); + g_assert_no_error (error); + g_assert_nonnull (stream); + + pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, &error); + g_assert_no_error (error); + g_assert_nonnull (pixbuf); + + texture2 = gdk_texture_new_for_pixbuf (pixbuf); + + compare_textures (texture, texture2); + + g_object_unref (texture2); + g_object_unref (pixbuf); + g_object_unref (stream); + g_object_unref (texture); + + *done = TRUE; +} + +static void +test_texture_icon_async (void) +{ + GdkTexture *texture; + gboolean done = FALSE; + + texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png"); + + g_loadable_icon_load_async (G_LOADABLE_ICON (texture), 16, NULL, icon_loaded, &done); + + while (!done) + g_main_context_iteration (NULL, FALSE); +} + +static void +test_texture_icon_serialize (void) +{ + GdkTexture *texture; + GVariant *data; + GIcon *icon; + + texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png"); + + data = g_icon_serialize (G_ICON (texture)); + g_assert_nonnull (data); + icon = g_icon_deserialize (data); + g_assert_true (G_IS_BYTES_ICON (icon)); + + g_variant_unref (data); + g_object_unref (icon); + g_object_unref (texture); +} + +static void +test_texture_diff (void) +{ + GdkTexture *texture; + GdkTexture *texture2; + cairo_region_t *full; + cairo_region_t *center; + cairo_region_t *r; + + texture = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png"); + texture2 = gdk_texture_new_from_resource ("/org/gtk/libgtk/icons/16x16/places/user-trash.png"); + + full = cairo_region_create_rectangle (&(cairo_rectangle_int_t) { 0, 0, 16, 16 }); + center = cairo_region_create_rectangle (&(cairo_rectangle_int_t) { 4, 4, 8 ,8 }); + + r = cairo_region_create (); + gdk_texture_diff (texture, texture, r); + + g_assert_true (cairo_region_is_empty (r)); + + gdk_texture_diff (texture, texture2, r); + + /* No diff set, so we get the full area */ + g_assert_true (cairo_region_equal (r, full)); + cairo_region_destroy (r); + + gdk_texture_set_diff (texture, texture2, cairo_region_copy (center)); + + r = cairo_region_create (); + gdk_texture_diff (texture, texture2, r); + + g_assert_true (cairo_region_equal (r, center)); + cairo_region_destroy (r); + + r = cairo_region_create (); + gdk_texture_diff (texture2, texture, r); + + g_assert_true (cairo_region_equal (r, center)); + cairo_region_destroy (r); + + cairo_region_destroy (full); + cairo_region_destroy (center); + g_object_unref (texture); + g_object_unref (texture2); +} + int main (int argc, char *argv[]) { @@ -169,7 +364,12 @@ main (int argc, char *argv[]) g_test_add_func ("/texture/from-pixbuf", test_texture_from_pixbuf); g_test_add_func ("/texture/from-resource", test_texture_from_resource); g_test_add_func ("/texture/save-to-png", test_texture_save_to_png); + g_test_add_func ("/texture/save-to-tiff", test_texture_save_to_tiff); g_test_add_func ("/texture/subtexture", test_texture_subtexture); + g_test_add_func ("/texture/icon/load", test_texture_icon); + g_test_add_func ("/texture/icon/load-async", test_texture_icon_async); + g_test_add_func ("/texture/icon/serialize", test_texture_icon_serialize); + g_test_add_func ("/texture/diff", test_texture_diff); return g_test_run (); } -- cgit v1.2.1