Skip to content

Commit

Permalink
tests: Implement tests for notification icons
Browse files Browse the repository at this point in the history
  • Loading branch information
jsparber committed Apr 4, 2024
1 parent f6a9722 commit 832ee46
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
116 changes: 116 additions & 0 deletions tests/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#include "account.h"

#include <libportal/portal.h>
#include <gio/gunixoutputstream.h>
#include "xdp-utils.h"

#define IMAGE_DATA "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"16px\" width=\"16px\"/>"

extern char outdir[];

static int got_info;
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions tests/notification.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions tests/test-portals.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down

0 comments on commit 832ee46

Please sign in to comment.