diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 14:36:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 14:36:24 +0000 |
commit | 9b6d8e63db85c30007b463e91f91a791969fa83f (patch) | |
tree | 0899af51d73c1bf986f73ae39a03c4436083018a /subprojects/libhandy/tests/test-avatar.c | |
parent | Initial commit. (diff) | |
download | gnome-control-center-9b6d8e63db85c30007b463e91f91a791969fa83f.tar.xz gnome-control-center-9b6d8e63db85c30007b463e91f91a791969fa83f.zip |
Adding upstream version 1:3.38.4.upstream/1%3.38.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'subprojects/libhandy/tests/test-avatar.c')
-rw-r--r-- | subprojects/libhandy/tests/test-avatar.c | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/subprojects/libhandy/tests/test-avatar.c b/subprojects/libhandy/tests/test-avatar.c new file mode 100644 index 0000000..ad330ae --- /dev/null +++ b/subprojects/libhandy/tests/test-avatar.c @@ -0,0 +1,210 @@ +/* + * Copyright (C) 2020 Purism SPC + * + * SPDX-License-Identifier: LGPL-2.1+ + */ + +#include <handy.h> + +#define TEST_ICON_NAME "avatar-default-symbolic" +#define TEST_STRING "Mario Rossi" +#define TEST_SIZE 128 + + +static gboolean +is_surface_empty (cairo_surface_t *surface) +{ + unsigned char * data; + guint length; + + cairo_surface_flush (surface); + data = cairo_image_surface_get_data (surface); + length = cairo_image_surface_get_width (surface) * cairo_image_surface_get_height (surface); + + for (int i = 0; i < length; i++) { + if (data[i] != 0) + return FALSE; + } + return TRUE; +} + +static GdkPixbuf * +load_null_image_func (gint size, + gpointer data) +{ + return NULL; +} + +static GdkPixbuf * +load_image_func (gint size, + GdkRGBA *color) +{ + GdkPixbuf *pixbuf; + cairo_surface_t *surface; + cairo_t *cr; + + surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size); + cr = cairo_create (surface); + if (color != NULL) { + gdk_cairo_set_source_rgba (cr, color); + cairo_paint (cr); + } + pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0, size, size); + + cairo_surface_destroy (surface); + cairo_destroy (cr); + return pixbuf; +} + + +static void +map_event_cb (GtkWidget *widget, GdkEvent *event, cairo_surface_t **surface) +{ + cairo_t *cr; + + g_assert (surface != NULL); + + *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, TEST_SIZE, TEST_SIZE); + cr = cairo_create (*surface); + gtk_widget_draw (widget, cr); + cairo_destroy (cr); + gtk_main_quit (); +} + + +static gboolean +did_draw_something (GtkWidget *widget) +{ + GtkWidget *window; + gboolean empty; + cairo_surface_t *surface; + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + + gtk_widget_set_events (widget, GDK_STRUCTURE_MASK); + g_signal_connect (widget, "map-event", G_CALLBACK (map_event_cb), &surface); + + gtk_window_resize (GTK_WINDOW (window), TEST_SIZE, TEST_SIZE); + gtk_container_add (GTK_CONTAINER (window), widget); + + gtk_widget_show (widget); + gtk_widget_show (window); + + gtk_main (); + + g_assert (surface); + g_assert (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS); + empty = is_surface_empty (surface); + + cairo_surface_destroy (surface); + gtk_widget_destroy (window); + + return !empty; +} + + +static void +test_hdy_avatar_generate (void) +{ + GtkWidget *avatar = hdy_avatar_new (TEST_SIZE, "", TRUE); + g_assert (HDY_IS_AVATAR (avatar)); + + g_assert_true (did_draw_something (GTK_WIDGET (avatar))); +} + + +static void +test_hdy_avatar_icon_name (void) +{ + HdyAvatar *avatar = HDY_AVATAR (hdy_avatar_new (128, NULL, TRUE)); + + g_assert_null (hdy_avatar_get_icon_name (avatar)); + hdy_avatar_set_icon_name (avatar, TEST_ICON_NAME); + g_assert_cmpstr (hdy_avatar_get_icon_name (avatar), ==, TEST_ICON_NAME); + + g_assert_true (did_draw_something (GTK_WIDGET (avatar))); +} + +static void +test_hdy_avatar_text (void) +{ + HdyAvatar *avatar = HDY_AVATAR (hdy_avatar_new (128, NULL, TRUE)); + + g_assert_null (hdy_avatar_get_text (avatar)); + hdy_avatar_set_text (avatar, TEST_STRING); + g_assert_cmpstr (hdy_avatar_get_text (avatar), ==, TEST_STRING); + + g_assert_true (did_draw_something (GTK_WIDGET (avatar))); +} + +static void +test_hdy_avatar_size (void) +{ + HdyAvatar *avatar = HDY_AVATAR (hdy_avatar_new (TEST_SIZE, NULL, TRUE)); + + g_assert_cmpint (hdy_avatar_get_size (avatar), ==, TEST_SIZE); + hdy_avatar_set_size (avatar, TEST_SIZE / 2); + g_assert_cmpint (hdy_avatar_get_size (avatar), ==, TEST_SIZE / 2); + + g_assert_true (did_draw_something (GTK_WIDGET (avatar))); +} + +static void +test_hdy_avatar_custom_image (void) +{ + GtkWidget *avatar; + GdkRGBA color; + + avatar = hdy_avatar_new (TEST_SIZE, NULL, TRUE); + + g_assert (HDY_IS_AVATAR (avatar)); + + hdy_avatar_set_image_load_func (HDY_AVATAR (avatar), + (HdyAvatarImageLoadFunc) load_image_func, + NULL, + NULL); + + g_object_ref (avatar); + g_assert_false (did_draw_something (avatar)); + + hdy_avatar_set_image_load_func (HDY_AVATAR (avatar), + NULL, + NULL, + NULL); + + g_assert_true (did_draw_something (avatar)); + + gdk_rgba_parse (&color, "#F00"); + hdy_avatar_set_image_load_func (HDY_AVATAR (avatar), + (HdyAvatarImageLoadFunc) load_image_func, + &color, + NULL); + + g_assert_true (did_draw_something (avatar)); + + hdy_avatar_set_image_load_func (HDY_AVATAR (avatar), + (HdyAvatarImageLoadFunc) load_null_image_func, + NULL, + NULL); + + g_assert_true (did_draw_something (avatar)); + + g_object_unref (avatar); +} + + +gint +main (gint argc, + gchar *argv[]) +{ + gtk_test_init (&argc, &argv, NULL); + hdy_init (); + + g_test_add_func ("/Handy/Avatar/generate", test_hdy_avatar_generate); + g_test_add_func ("/Handy/Avatar/custom_image", test_hdy_avatar_custom_image); + g_test_add_func ("/Handy/Avatar/icon_name", test_hdy_avatar_icon_name); + g_test_add_func ("/Handy/Avatar/text", test_hdy_avatar_text); + g_test_add_func ("/Handy/Avatar/size", test_hdy_avatar_size); + + return g_test_run (); +} |