gio/tests/desktop-app-info: Add a TestLaunchContext to improve SN testing

Add a custom launch context implementation so that we can test the
startup notification code better.
This commit is contained in:
Marco Trevisan (Treviño) 2022-10-24 13:02:40 +02:00
parent ec67c7dbf2
commit 6bbc72d2c4

View File

@ -33,6 +33,62 @@
#include <sys/types.h>
#include <sys/stat.h>
G_DECLARE_FINAL_TYPE (TestLaunchContext, test_launch_context, TEST,
LAUNCH_CONTEXT, GAppLaunchContext);
struct _TestLaunchContext {
GAppLaunchContext parent;
char *overriden_startup_notify_id;
};
struct _TestLaunchContextClass {
GAppLaunchContextClass parent;
};
G_DEFINE_FINAL_TYPE (TestLaunchContext, test_launch_context,
G_TYPE_APP_LAUNCH_CONTEXT);
static void
test_launch_context_init (TestLaunchContext *test_context)
{
}
static char *
test_launch_context_get_startup_notify_id (GAppLaunchContext *context,
GAppInfo *app_info,
GList *files)
{
TestLaunchContext *test_context = TEST_LAUNCH_CONTEXT (context);
if (test_context->overriden_startup_notify_id)
return g_strdup (test_context->overriden_startup_notify_id);
if (g_app_info_get_id (app_info))
return g_strdup (g_app_info_get_id (app_info));
if (g_app_info_get_display_name (app_info))
return g_strdup (g_app_info_get_display_name (app_info));
return g_strdup (g_app_info_get_commandline (app_info));
}
static void
test_launch_context_get_startup_notify_dispose (GObject *object)
{
TestLaunchContext *test_context = TEST_LAUNCH_CONTEXT (object);
g_clear_pointer (&test_context->overriden_startup_notify_id, g_free);
G_OBJECT_CLASS (test_launch_context_parent_class)->dispose (object);
}
static void
test_launch_context_class_init (TestLaunchContextClass *klass)
{
G_APP_LAUNCH_CONTEXT_CLASS (klass)->get_startup_notify_id = test_launch_context_get_startup_notify_id;
G_OBJECT_CLASS (klass)->dispose = test_launch_context_get_startup_notify_dispose;
}
static GAppInfo *
create_command_line_app_info (const char *name,
const char *command_line,
@ -951,9 +1007,28 @@ on_launch_started (GAppLaunchContext *context, GAppInfo *info, GVariant *platfor
gboolean *invoked = data;
g_assert_true (G_IS_APP_LAUNCH_CONTEXT (context));
g_assert_true (G_IS_APP_INFO (info));
if (TEST_IS_LAUNCH_CONTEXT (context))
{
GVariantDict dict;
const char *sni;
char *expected_sni;
g_assert_nonnull (platform_data);
g_variant_dict_init (&dict, platform_data);
g_assert_true (
g_variant_dict_lookup (&dict, "startup-notification-id", "&s", &sni));
expected_sni = g_app_launch_context_get_startup_notify_id (context, info, NULL);
g_assert_cmpstr (sni, ==, expected_sni);
g_free (expected_sni);
g_variant_dict_clear (&dict);
}
else
{
/* Our default context doesn't fill in any platform data */
g_assert_null (platform_data);
}
g_assert_false (*invoked);
*invoked = TRUE;
@ -979,6 +1054,19 @@ on_launched (GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data
g_variant_dict_clear (&dict);
}
static void
on_launch_failed (GAppLaunchContext *context, const char *startup_notify_id, gpointer data)
{
gboolean *invoked = data;
g_assert_true (G_IS_APP_LAUNCH_CONTEXT (context));
g_assert_nonnull (startup_notify_id);
g_test_message ("Application launch failed: %s", startup_notify_id);
g_assert_false (*invoked);
*invoked = TRUE;
}
/* Test g_desktop_app_info_launch_uris_as_manager() and
* g_desktop_app_info_launch_uris_as_manager_with_fds()
*/
@ -991,6 +1079,7 @@ test_launch_as_manager (void)
const gchar *path;
gboolean invoked = FALSE;
gboolean launched = FALSE;
gboolean failed = FALSE;
GAppLaunchContext *context;
path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
@ -1002,13 +1091,16 @@ test_launch_as_manager (void)
return;
}
context = g_app_launch_context_new ();
context = g_object_new (test_launch_context_get_type (), NULL);
g_signal_connect (context, "launch-started",
G_CALLBACK (on_launch_started),
&invoked);
g_signal_connect (context, "launched",
G_CALLBACK (on_launched),
&launched);
g_signal_connect (context, "launch-failed",
G_CALLBACK (on_launch_failed),
&failed);
retval = g_desktop_app_info_launch_uris_as_manager (appinfo, NULL, context, 0,
NULL, NULL,
NULL, NULL,
@ -1017,9 +1109,11 @@ test_launch_as_manager (void)
g_assert_true (retval);
g_assert_true (invoked);
g_assert_true (launched);
g_assert_false (failed);
invoked = FALSE;
launched = FALSE;
failed = FALSE;
retval = g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
NULL, context, 0,
NULL, NULL,
@ -1030,6 +1124,7 @@ test_launch_as_manager (void)
g_assert_true (retval);
g_assert_true (invoked);
g_assert_true (launched);
g_assert_false (failed);
g_object_unref (appinfo);
g_assert_finalize_object (context);