diff --git a/tests/notification.c b/tests/notification.c index 0aa61f200..e82f6758b 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; @@ -319,3 +323,115 @@ test_notification_bad_notification (void) xdp_portal_remove_notification (portal, "test6"); } + +static void +test_icon (const char *serialized_icon, + const char *exp_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_autofree char *exp_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); + + if (exp_serialized_icon) + exp_notification_s = g_strdup_printf ("{ 'title': <'test notification 7'>, " + " 'body': <'test notification body 7'>, " + " 'icon': <%s>, " + " 'default-action': <'test-action'> " + "}", exp_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", + exp_notification_s ? exp_notification_s : 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, NULL, 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, NULL, FALSE); +} + +void +test_notification_icon (void) +{ + /* For historical reasons we also accept just an icon name but it's + * converted to a "normal" themed icon */ + test_icon ("'test-icon'", "('themed', <['test-icon', 'test-icon-symbolic']>)", FALSE); + + test_themed_icon (); + test_bytes_icon (); + + /* Tests that should fail */ + test_icon ("('themed', <'test-icon-symbolic'>)", NULL, TRUE); + test_icon ("('bytes', <['test-icon-symbolic', 'test-icon']>)", NULL, 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 87cbe56d9..b8af7c3c9 100644 --- a/tests/test-portals.c +++ b/tests/test-portals.c @@ -606,6 +606,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 ();