From b1852cbdd11e73def27e2ed7fddefbdcf2198815 Mon Sep 17 00:00:00 2001 From: Julian Sparber Date: Tue, 27 Feb 2024 15:01:21 +0100 Subject: [PATCH] WIP add icon test --- meson.build | 1 + tests/notification.c | 125 +++++++++++++++++++++++++++++++++++++++++++ tests/notification.h | 1 + tests/test-portals.c | 1 + 4 files changed, 128 insertions(+) diff --git a/meson.build b/meson.build index 433f1fccd..dd341d0c7 100644 --- a/meson.build +++ b/meson.build @@ -109,6 +109,7 @@ geoclue_dep = dependency('libgeoclue-2.0', version: '>= 2.5.2', required: get_option('geoclue')) libportal_dep = dependency('libportal', + version: '>= 0.7.2', required: get_option('libportal')) pipewire_dep = dependency('libpipewire-0.3', version: '>= 0.2.90') libsystemd_dep = dependency('libsystemd', required: get_option('systemd')) diff --git a/tests/notification.c b/tests/notification.c index 089060dd0..4e97dd7fb 100644 --- a/tests/notification.c +++ b/tests/notification.c @@ -4,8 +4,12 @@ #include "account.h" #include +#include #include "xdp-utils.h" +#define IMAGE_DATA "" \ + "" + extern char outdir[]; static int got_info; @@ -345,3 +349,124 @@ test_notification_bad_notification (void) xdp_portal_remove_notification (portal, "test6"); } + +static void +test_icon (const char *serialized_icon, + gboolean exp_fail) +{ + g_autoptr(XdpPortal) portal = NULL; + g_autoptr(GKeyFile) keyfile = NULL; + g_autoptr(GError) error = NULL; + g_autofree char *path = NULL; + g_autofree char *notification_s = NULL; + g_autoptr(GVariant) notification = NULL; + gulong id; + + notification_s = g_strdup_printf ("{ 'title': <'test notification 7'>, " + " 'body': <'test notification body 7'>, " + " 'icon': <%s>, " + " 'default-action': <'test-action'> " + "}", serialized_icon); + + notification = g_variant_parse (G_VARIANT_TYPE_VARDICT, + notification_s, + NULL, + NULL, + &error); + g_assert_no_error (error); + + keyfile = g_key_file_new (); + + g_key_file_set_string (keyfile, "notification", "data", notification_s); + g_key_file_set_string (keyfile, "notification", "id", "test7"); + g_key_file_set_string (keyfile, "notification", "action", "test-action"); + g_key_file_set_integer (keyfile, "backend", "delay", 200); + + path = g_build_filename (outdir, "notification", NULL); + g_key_file_save_to_file (keyfile, path, &error); + g_assert_no_error (error); + + portal = xdp_portal_new (); + + id = g_signal_connect (portal, "notification-action-invoked", G_CALLBACK (notification_action_invoked), keyfile); + + got_info = 0; + if (exp_fail) + xdp_portal_add_notification (portal, "test7", notification, 0, NULL, notification_fail, NULL); + else + xdp_portal_add_notification (portal, "test7", notification, 0, NULL, notification_succeed, NULL); + + while (!got_info) + g_main_context_iteration (NULL, TRUE); + + g_signal_handler_disconnect (portal, id); + + xdp_portal_remove_notification (portal, "test2"); +} + +static void +test_themed_icon () +{ + g_autoptr(GIcon) icon = NULL; + g_autoptr(GVariant) serialized_icon = NULL; + g_autofree char *serialized_icon_s = NULL; + + icon = g_themed_icon_new ("test-icon-symbolic"); + serialized_icon = g_icon_serialize (icon); + + serialized_icon_s = g_variant_print (serialized_icon, TRUE); + test_icon (serialized_icon_s, FALSE); +} + +static void +test_bytes_icon () +{ + g_autoptr(GIcon) icon = NULL; + g_autoptr(GVariant) serialized_icon = NULL; + g_autofree char *serialized_icon_s = NULL; + g_autoptr(GBytes) bytes = NULL; + + bytes = g_bytes_new_static (IMAGE_DATA, strlen (IMAGE_DATA)); + icon = g_bytes_icon_new (bytes); + serialized_icon = g_icon_serialize (icon); + + serialized_icon_s = g_variant_print (serialized_icon, TRUE); + test_icon (serialized_icon_s, FALSE); +} + +static void +test_file_icon () +{ + g_autoptr(GIcon) icon = NULL; + g_autofree char *path = NULL; + g_autofree char *serialized_icon_s = NULL; + g_autoptr(GFile) file = NULL; + g_autoptr(GFileIOStream) iostream = NULL; + GOutputStream *stream = NULL; + + file = g_file_new_tmp ("iconXXXXXX", &iostream, NULL); + stream = g_io_stream_get_output_stream (G_IO_STREAM (iostream)); + g_output_stream_write_all (stream, IMAGE_DATA, strlen(IMAGE_DATA), NULL, NULL, NULL); + g_output_stream_close (stream, NULL, NULL); + + path = g_file_get_path (file); + /* Manually create a serialized `GFileIcon` since we want a path not an URI in the serialized string. + * Passing URI instead of a path would work but then test would fail since the created + * serialized notification wouldn't match the expected notification in the backend test */ + serialized_icon_s = g_strdup_printf ("('file', <'%s'>)", path); + test_icon (serialized_icon_s, FALSE); +} + +void +test_notification_icon (void) +{ + test_themed_icon (); + test_bytes_icon (); + test_file_icon (); + + /* Tests that should fail */ + test_icon ("('themed', <'test'>)", TRUE); + test_icon ("('bytes', <['test-icon-symbolic', 'test-icon']>)", TRUE); + test_icon ("('file-descriptor', <''>)", TRUE); + test_icon ("('file-descriptor', )", TRUE); +} diff --git a/tests/notification.h b/tests/notification.h index 0644973fd..bfa59779b 100644 --- a/tests/notification.h +++ b/tests/notification.h @@ -7,3 +7,4 @@ void test_notification_bad_arg (void); void test_notification_bad_priority (void); void test_notification_bad_button (void); void test_notification_bad_notification (void); +void test_notification_icon (void); diff --git a/tests/test-portals.c b/tests/test-portals.c index ff04189cd..345eb2b79 100644 --- a/tests/test-portals.c +++ b/tests/test-portals.c @@ -602,6 +602,7 @@ main (int argc, char **argv) g_test_add_func ("/portal/notification/bad-priority", test_notification_bad_priority); g_test_add_func ("/portal/notification/bad-button", test_notification_bad_button); g_test_add_func ("/portal/notification/bad-notification", test_notification_bad_notification); + g_test_add_func ("/portal/notification/icon", test_notification_icon); #endif global_setup ();