Skip to content

Commit

Permalink
test: WIP sound
Browse files Browse the repository at this point in the history
  • Loading branch information
jsparber committed Mar 7, 2024
1 parent 30cadc9 commit 5ecd97f
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
103 changes: 103 additions & 0 deletions tests/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#define IMAGE_DATA "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
"<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"16px\" width=\"16px\"/>"
/* We currently don't validated the sound itself so it doesn't matter what we give to the test */
#define SOUND_DATA "SOME SOUND DATA"

extern char outdir[];

Expand Down Expand Up @@ -492,3 +494,104 @@ test_notification_icon (void)
test_icon ("('file-descriptor', <''>)", NULL, TRUE);
test_icon ("('file-descriptor', <handle 0>)", NULL, TRUE);
}

static void
test_sound (const char *serialized_sound,
const char *exp_serialized_sound,
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'>, "
" 'sound': <%s>, "
" 'default-action': <'test-action'> "
"}", serialized_sound);

if (exp_serialized_sound)
exp_notification_s = g_strdup_printf ("{ 'title': <'test notification 7'>, "
" 'body': <'test notification body 7'>, "
" 'sound': <%s>, "
" 'default-action': <'test-action'> "
"}", exp_serialized_sound);


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", "test8");
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, "test8", notification, 0, NULL, notification_fail, NULL);
else
xdp_portal_add_notification (portal, "test8", 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, "test8");
}

static void
test_file_sound ()
{
g_autofree char *path = NULL;
g_autofree char *serialized_sound_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);
serialized_sound_s = g_strdup_printf ("('file', <'%s'>)", path);
test_icon (serialized_sound_s, NULL, FALSE);
g_assert (g_file_delete (file, NULL, NULL));
}

void
test_notification_sound (void)
{
test_sound ("('themed', <['message-new-instant']>)", NULL, FALSE);
test_sound ("('bytes', <'" SOUND_DATA "'>)", NULL, TRUE);
test_file_sound ();

/* Tests that should fail */
test_sound ("('themed', <'message-new-instant'>)", NULL, TRUE);
test_sound ("('bytes', <['test-sound', 'test-sound']>)", NULL, TRUE);
test_sound ("('file-descriptor', <''>)", NULL, TRUE);
test_sound ("('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 @@ -8,3 +8,4 @@ void test_notification_bad_priority (void);
void test_notification_bad_button (void);
void test_notification_bad_notification (void);
void test_notification_icon (void);
void test_notification_sound (void);
1 change: 1 addition & 0 deletions tests/test-portals.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ main (int argc, char **argv)
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);
g_test_add_func ("/portal/notification/sound", test_notification_sound);
#endif

global_setup ();
Expand Down

0 comments on commit 5ecd97f

Please sign in to comment.