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 Mar 4, 2024
1 parent 726ed84 commit c750613
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
146 changes: 146 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 @@ -345,3 +349,145 @@ 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);
}

static void
test_file_icon ()
{
g_autofree char *path = NULL;
g_autofree char *serialized_icon_s = NULL;
g_autofree char *exp_serialized_icon_s = NULL;
g_autoptr(GVariant) serialized_icon = NULL;
g_autoptr(GFile) file = NULL;
g_autoptr(GIcon) icon = 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);
icon = g_file_icon_new (file);
serialized_icon = g_icon_serialize (icon);
serialized_icon_s = g_variant_print (serialized_icon, TRUE);
/* GFileIcon puts an URI into the serialized icon but we put a path into it,
* which is also valid for a GFileIcon */
exp_serialized_icon_s = g_strdup_printf ("('file', <'%s'>)", path);
test_icon (serialized_icon_s, exp_serialized_icon_s, 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-symbolic'", "('themed', <['test-icon-symbolic']>)", FALSE);

test_themed_icon ();
test_bytes_icon ();
test_file_icon ();

/* Tests that should fail */
test_icon ("('themed', <'test-icon-symbolic'>)", NULL, TRUE);
test_icon ("('bytes', <['test-icon-symbolic', 'test-icon']>)", NULL, TRUE);
test_icon ("('file-descriptor', <''>)", NULL, TRUE);
test_icon ("('file-descriptor', <handle 0>)", 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 @@ -609,6 +609,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 c750613

Please sign in to comment.