diff --git a/libportal/globalshortcuts.c b/libportal/globalshortcuts.c index 59b35c24..1dc0900c 100644 --- a/libportal/globalshortcuts.c +++ b/libportal/globalshortcuts.c @@ -20,6 +20,7 @@ #include "globalshortcuts.h" #include "portal-private.h" #include "session-private.h" +#include "parent-private.h" struct _XdpGlobalShortcut { @@ -350,7 +351,7 @@ xdp_global_shortcuts_session_init (XdpGlobalShortcutsSession *session) } /* A request-based method call */ -typedef struct { +typedef struct _Call { XdpPortal *portal; char *session_path; /* object path for session */ GTask *task; @@ -358,6 +359,11 @@ typedef struct { char *request_path; /* object path for request */ gulong cancelled_id; /* signal id for cancelled gobject signal */ XdpGlobalShortcutsSession *session; + XdpParent *parent; + char *parent_handle; + GVariant *shortcuts; + char *activation_token; + void (*parent_ready) (struct _Call *call); } Call; @@ -388,6 +394,16 @@ call_dispose (void *p) call_disconnect_cancelled (call); g_clear_pointer (&call->request_path, g_free); + if (call->parent) + { + if (call->parent_handle != NULL) + call->parent->parent_unexport (call->parent); + xdp_parent_free (call->parent); + call->parent = NULL; + } + g_clear_pointer (&call->parent_handle, g_free); + g_clear_pointer (&call->shortcuts, g_variant_unref); + g_clear_pointer (&call->activation_token, g_free); g_clear_object (&call->portal); g_clear_object (&call->session); @@ -934,101 +950,188 @@ list_shortcuts (Call *call) call_ref (call)); } -/** - * xdp_global_shortcuts_session_bind_shortcuts: - * @session: a [class@GlobalShortcutsSession] - * @shortcuts: (element-type XdpGlobalShortcut) (transfer none): - * array of shortcuts to bind - * @parent_window: (nullable): parent window identifier, or %NULL - * @cancellable: (nullable): optional [class@Gio.Cancellable] - * @callback: (scope async): a callback to call when the request is done - * @data: data to pass to @callback - * - * Bind shortcuts and list triggers. - */ -void -xdp_global_shortcuts_session_bind_shortcuts (XdpGlobalShortcutsSession *session, - GPtrArray *shortcuts, - const char *parent_window, - GCancellable *cancellable, - GAsyncReadyCallback callback, - gpointer data) +static void +parent_exported (XdpParent *parent, + const char *handle, + gpointer data) { - g_autoptr(Call) call = NULL; - XdpPortal *portal; - guint i; - GVariantBuilder options; - GVariantBuilder shortcuts_builder; - GVariant *v; + Call *call = data; - g_return_if_fail (_xdp_global_shortcuts_session_is_valid (session)); - g_return_if_fail (shortcuts != NULL); + call->parent_handle = g_strdup (handle); + if (g_task_return_error_if_cancelled (call->task)) + call_dispose (call); + else + call->parent_ready (call); + call_unref (call); +} - for (i = 0; i < shortcuts->len; i++) +static void +call_export_parent (Call *call, + XdpParent *parent, + void (*parent_ready) (Call *call)) +{ + call->parent_ready = parent_ready; + + if (g_task_return_error_if_cancelled (call->task)) { - XdpGlobalShortcut *shortcut; + call_dispose (call); + return; + } - shortcut = g_ptr_array_index (shortcuts, i); + if (parent) + { + Call *export_call; - g_return_if_fail (shortcut != NULL); - g_return_if_fail (shortcut->shortcut_id != NULL); - g_return_if_fail (shortcut->description != NULL); + call->parent = xdp_parent_copy (parent); + export_call = call_ref (call); + if (!call->parent->parent_export (call->parent, parent_exported, export_call)) + { + call_unref (export_call); + g_task_return_new_error (call->task, + G_IO_ERROR, + G_IO_ERROR_FAILED, + "Failed to export parent window handle"); + call_dispose (call); + } } + else + { + call->parent_handle = g_strdup (""); + call->parent_ready (call); + } +} - portal = session->parent_session->portal; - - call = call_new (portal, session, session, cancellable, callback, data); +static void +bind_shortcuts (Call *call) +{ + GVariantBuilder options; + GCancellable *cancellable; prep_call (call, bind_shortcuts_done, &options, NULL); + cancellable = g_task_get_cancellable (call->task); if (cancellable) call->cancelled_id = g_signal_connect (cancellable, "cancelled", G_CALLBACK (call_cancelled_cb), call); + g_dbus_connection_call (call->portal->bus, + PORTAL_BUS_NAME, + PORTAL_OBJECT_PATH, + "org.freedesktop.portal.GlobalShortcuts", + "BindShortcuts", + g_variant_new ("(o@a(sa{sv})s@a{sv})", + call->session->parent_session->id, + g_variant_ref (call->shortcuts), + call->parent_handle, + g_variant_builder_end (&options)), + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + cancellable, + call_returned, + call_ref (call)); +} + +static Call * +bind_shortcuts_call_new (XdpGlobalShortcutsSession *session, + GPtrArray *shortcuts, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer data) +{ + g_autoptr(Call) call = NULL; + GVariantBuilder shortcuts_builder; + guint i; + + g_return_val_if_fail (_xdp_global_shortcuts_session_is_valid (session), NULL); + g_return_val_if_fail (shortcuts != NULL, NULL); + + for (i = 0; i < shortcuts->len; i++) + { + XdpGlobalShortcut *shortcut = g_ptr_array_index (shortcuts, i); + + g_return_val_if_fail (shortcut != NULL, NULL); + g_return_val_if_fail (shortcut->shortcut_id != NULL, NULL); + g_return_val_if_fail (shortcut->description != NULL, NULL); + } + + call = call_new (session->parent_session->portal, session, session, + cancellable, callback, data); g_variant_builder_init (&shortcuts_builder, G_VARIANT_TYPE ("a(sa{sv})")); for (i = 0; i < shortcuts->len; i++) { GVariantDict dict; - XdpGlobalShortcut *shortcut; + XdpGlobalShortcut *shortcut = g_ptr_array_index (shortcuts, i); - shortcut = g_ptr_array_index (shortcuts, i); g_variant_dict_init (&dict, NULL); if (shortcut->preferred_trigger != NULL) - g_variant_dict_insert (&dict, - "preferred_trigger", - "s", - shortcut->preferred_trigger); - g_variant_dict_insert (&dict, - "description", - "s", - shortcut->description); - - g_variant_builder_add (&shortcuts_builder, - "(s@a{sv})", - shortcut->shortcut_id, - g_variant_dict_end (&dict)); + g_variant_dict_insert (&dict, "preferred_trigger", "s", shortcut->preferred_trigger); + g_variant_dict_insert (&dict, "description", "s", shortcut->description); + g_variant_builder_add (&shortcuts_builder, "(s@a{sv})", + shortcut->shortcut_id, g_variant_dict_end (&dict)); } - if (parent_window == NULL) - parent_window = ""; + call->shortcuts = g_variant_ref_sink (g_variant_builder_end (&shortcuts_builder)); + return g_steal_pointer (&call); +} - v = g_variant_new ("(o@a(sa{sv})s@a{sv})", - session->parent_session->id, - g_variant_builder_end (&shortcuts_builder), - parent_window, - g_variant_builder_end (&options)); - g_dbus_connection_call (call->portal->bus, - PORTAL_BUS_NAME, - PORTAL_OBJECT_PATH, - "org.freedesktop.portal.GlobalShortcuts", - "BindShortcuts", - v, - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - g_task_get_cancellable (call->task), - call_returned, - call_ref (call)); +/** + * xdp_global_shortcuts_session_bind_shortcuts: + * @session: a [class@GlobalShortcutsSession] + * @shortcuts: (element-type XdpGlobalShortcut) (transfer none): array of shortcuts to bind + * @parent_window: (nullable): parent window identifier, or %NULL + * @cancellable: (nullable): optional [class@Gio.Cancellable] + * @callback: (scope async): a callback to call when the request is done + * @data: data to pass to @callback + * + * Bind shortcuts and list triggers using an already exported window identifier. + * For a toolkit window, use [method@GlobalShortcutsSession.bind_shortcuts_with_parent]. + */ +void +xdp_global_shortcuts_session_bind_shortcuts (XdpGlobalShortcutsSession *session, + GPtrArray *shortcuts, + const char *parent_window, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer data) +{ + g_autoptr(Call) call = bind_shortcuts_call_new (session, shortcuts, cancellable, callback, data); + + if (call == NULL) + return; + + call->parent_handle = g_strdup (parent_window != NULL ? parent_window : ""); + bind_shortcuts (call); +} + +/** + * xdp_global_shortcuts_session_bind_shortcuts_with_parent: + * @session: a [class@GlobalShortcutsSession] + * @shortcuts: (element-type XdpGlobalShortcut) (transfer none): array of shortcuts to bind + * @parent: (nullable): parent window information + * @cancellable: (nullable): optional [class@Gio.Cancellable] + * @callback: (scope async): a callback to call when the request is done + * @data: data to pass to @callback + * + * Bind shortcuts and list triggers, exporting @parent before making the request. + * The parent and shortcut descriptions are copied before this function returns. + * + * Since: 0.11.1 + */ +void +xdp_global_shortcuts_session_bind_shortcuts_with_parent (XdpGlobalShortcutsSession *session, + GPtrArray *shortcuts, + XdpParent *parent, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer data) +{ + g_autoptr(Call) call = bind_shortcuts_call_new (session, shortcuts, cancellable, callback, data); + + if (call == NULL) + return; + + call_export_parent (call, parent, bind_shortcuts); } @@ -1054,6 +1157,26 @@ xdp_global_shortcuts_session_bind_shortcuts_finish (XdpGlobalShortcutsSession *s return g_task_propagate_pointer (G_TASK (result), error); } +/** + * xdp_global_shortcuts_session_bind_shortcuts_with_parent_finish: + * @session: a [class@GlobalShortcutsSession] + * @result: a [iface@Gio.AsyncResult] + * @error: return location for an error + * + * Finishes a [method@GlobalShortcutsSession.bind_shortcuts_with_parent] request. + * + * Returns: (transfer full) (element-type XdpGlobalShortcutAssigned): array of assigned shortcuts + * + * Since: 0.11.1 + */ +GPtrArray * +xdp_global_shortcuts_session_bind_shortcuts_with_parent_finish (XdpGlobalShortcutsSession *session, + GAsyncResult *result, + GError **error) +{ + return xdp_global_shortcuts_session_bind_shortcuts_finish (session, result, error); +} + /** * xdp_global_shortcuts_session_list_shortcuts: * @session: a [class@GlobalShortcutsSession] @@ -1105,18 +1228,44 @@ configure_shortcuts_done (GObject *object, GAsyncResult *result, gpointer data) { - g_autoptr(GTask) task = data; + g_autoptr(Call) call = data; g_autoptr(GVariant) ret = NULL; GError *error = NULL; ret = g_dbus_connection_call_finish (G_DBUS_CONNECTION (object), result, &error); if (error) - { - g_task_return_error (task, error); - return; - } + g_task_return_error (call->task, error); + else + g_task_return_boolean (call->task, TRUE); - g_task_return_boolean (task, TRUE); + call_dispose (call); +} + +static void +configure_shortcuts (Call *call) +{ + GVariantBuilder options; + + g_variant_builder_init (&options, G_VARIANT_TYPE_VARDICT); + if (call->activation_token != NULL && *call->activation_token != '\0') + g_variant_builder_add (&options, "{sv}", "activation_token", + g_variant_new_string (call->activation_token)); + + g_dbus_connection_call (call->portal->bus, + PORTAL_BUS_NAME, + PORTAL_OBJECT_PATH, + "org.freedesktop.portal.GlobalShortcuts", + "ConfigureShortcuts", + g_variant_new ("(os@a{sv})", + call->session->parent_session->id, + call->parent_handle, + g_variant_builder_end (&options)), + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + g_task_get_cancellable (call->task), + configure_shortcuts_done, + call_ref (call)); } /** @@ -1128,10 +1277,9 @@ configure_shortcuts_done (GObject *object, * @callback: (scope async): callback called when the method call is done * @data: data to pass to @callback * - * Requests that the portal show configuration UI for all shortcuts of this - * session. + * Show configuration UI using an already exported window identifier. + * For a toolkit window, use [method@GlobalShortcutsSession.configure_shortcuts_with_parent]. */ - void xdp_global_shortcuts_session_configure_shortcuts (XdpGlobalShortcutsSession *session, const char *parent_window, @@ -1140,44 +1288,47 @@ xdp_global_shortcuts_session_configure_shortcuts (XdpGlobalShortcutsSession *ses GAsyncReadyCallback callback, gpointer data) { - XdpPortal *portal; - GVariantBuilder options; - g_autoptr(GTask) task = NULL; + g_autoptr(Call) call = NULL; g_return_if_fail (_xdp_global_shortcuts_session_is_valid (session)); - portal = session->parent_session->portal; - - if (parent_window == NULL) - parent_window = ""; - - g_variant_builder_init (&options, G_VARIANT_TYPE_VARDICT); + call = call_new (session->parent_session->portal, session, session, + cancellable, callback, data); + call->parent_handle = g_strdup (parent_window != NULL ? parent_window : ""); + call->activation_token = g_strdup (activation_token); + configure_shortcuts (call); +} - if (activation_token != NULL && *activation_token != '\0') - { - g_variant_builder_add (&options, - "{sv}", - "activation_token", - g_variant_new_string (activation_token)); - } +/** + * xdp_global_shortcuts_session_configure_shortcuts_with_parent: + * @session: a [class@GlobalShortcutsSession] + * @parent: (nullable): parent window information + * @activation_token: (nullable): token used to activate the configuration window + * @cancellable: (nullable): optional [class@Gio.Cancellable] + * @callback: (scope async): callback called when the method call is done + * @data: data to pass to @callback + * + * Requests that the portal show configuration UI for all shortcuts of this + * session, exporting @parent before making the call. + * + * Since: 0.11.1 + */ +void +xdp_global_shortcuts_session_configure_shortcuts_with_parent (XdpGlobalShortcutsSession *session, + XdpParent *parent, + const char *activation_token, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer data) +{ + g_autoptr(Call) call = NULL; - task = g_task_new (session, cancellable, callback, data); + g_return_if_fail (_xdp_global_shortcuts_session_is_valid (session)); - g_dbus_connection_call (portal->bus, - PORTAL_BUS_NAME, - PORTAL_OBJECT_PATH, - "org.freedesktop.portal.GlobalShortcuts", - "ConfigureShortcuts", - g_variant_new ("(os@a{sv})", - session->parent_session->id, - parent_window, - g_variant_builder_end (&options)), - NULL, - G_DBUS_CALL_FLAGS_NONE, - -1, - cancellable, - configure_shortcuts_done, - g_object_ref (task)); + call = call_new (session->parent_session->portal, session, session, + cancellable, callback, data); + call->activation_token = g_strdup (activation_token); + call_export_parent (call, parent, configure_shortcuts); } /** @@ -1201,3 +1352,23 @@ xdp_global_shortcuts_session_configure_shortcuts_finish (XdpGlobalShortcutsSessi return g_task_propagate_boolean (G_TASK (result), error); } + +/** + * xdp_global_shortcuts_session_configure_shortcuts_with_parent_finish: + * @session: a [class@GlobalShortcutsSession] + * @result: a [iface@Gio.AsyncResult] + * @error: return location for an error + * + * Finishes a [method@GlobalShortcutsSession.configure_shortcuts_with_parent] call. + * + * Returns: %TRUE if the call succeeded, %FALSE otherwise + * + * Since: 0.11.1 + */ +gboolean +xdp_global_shortcuts_session_configure_shortcuts_with_parent_finish (XdpGlobalShortcutsSession *session, + GAsyncResult *result, + GError **error) +{ + return xdp_global_shortcuts_session_configure_shortcuts_finish (session, result, error); +} diff --git a/libportal/globalshortcuts.h b/libportal/globalshortcuts.h index 70a6920e..24409d3a 100644 --- a/libportal/globalshortcuts.h +++ b/libportal/globalshortcuts.h @@ -19,6 +19,7 @@ #include #include +#include G_BEGIN_DECLS @@ -119,6 +120,19 @@ GPtrArray *xdp_global_shortcuts_session_bind_shortcuts_finish GAsyncResult *result, GError **error); +XDP_PUBLIC +void xdp_global_shortcuts_session_bind_shortcuts_with_parent (XdpGlobalShortcutsSession *session, + GPtrArray *shortcuts, + XdpParent *parent, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer data); + +XDP_PUBLIC +GPtrArray *xdp_global_shortcuts_session_bind_shortcuts_with_parent_finish (XdpGlobalShortcutsSession *session, + GAsyncResult *result, + GError **error); + XDP_PUBLIC void xdp_global_shortcuts_session_list_shortcuts (XdpGlobalShortcutsSession *session, GCancellable *cancellable, @@ -143,4 +157,17 @@ gboolean xdp_global_shortcuts_session_configure_shortcuts_fi GAsyncResult *result, GError **error); +XDP_PUBLIC +void xdp_global_shortcuts_session_configure_shortcuts_with_parent (XdpGlobalShortcutsSession *session, + XdpParent *parent, + const char *activation_token, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer data); + +XDP_PUBLIC +gboolean xdp_global_shortcuts_session_configure_shortcuts_with_parent_finish (XdpGlobalShortcutsSession *session, + GAsyncResult *result, + GError **error); + G_END_DECLS diff --git a/portal-test/gtk3/portal-test-win.c b/portal-test/gtk3/portal-test-win.c index 24e01059..54df2fbb 100644 --- a/portal-test/gtk3/portal-test-win.c +++ b/portal-test/gtk3/portal-test-win.c @@ -973,7 +973,7 @@ globalshortcuts_bind_done (GObject *source, if (session != win->gs_session) return; - shortcuts = xdp_global_shortcuts_session_bind_shortcuts_finish (session, result, &error); + shortcuts = xdp_global_shortcuts_session_bind_shortcuts_with_parent_finish (session, result, &error); if (shortcuts == NULL) { g_warning ("Failed to bind GlobalShortcuts: %s", error->message); @@ -1009,6 +1009,7 @@ static void globalshortcuts_bind (PortalTestWin *win) { g_autoptr(GPtrArray) shortcuts = NULL; + g_autoptr(XdpParent) parent = NULL; shortcuts = g_ptr_array_new_with_free_func ((GDestroyNotify) xdp_global_shortcut_free); @@ -1022,9 +1023,11 @@ globalshortcuts_bind (PortalTestWin *win) "Do Bar", NULL)); - xdp_global_shortcuts_session_bind_shortcuts (win->gs_session, + parent = xdp_parent_new_gtk (GTK_WINDOW (win)); + + xdp_global_shortcuts_session_bind_shortcuts_with_parent (win->gs_session, shortcuts, - NULL, + parent, NULL, globalshortcuts_bind_done, win); @@ -1101,7 +1104,7 @@ globalshortcuts_configure_done (GObject *source, if (session != win->gs_session) return; - if (!xdp_global_shortcuts_session_configure_shortcuts_finish (session, result, &error)) + if (!xdp_global_shortcuts_session_configure_shortcuts_with_parent_finish (session, result, &error)) { g_warning ("Failed to configure GlobalShortcuts: %s", error->message); gtk_label_set_label (GTK_LABEL (win->globalshortcuts_activations), "failed to configure"); @@ -1115,14 +1118,18 @@ static void global_shortcuts_configure (GtkButton *button, PortalTestWin *win) { + g_autoptr(XdpParent) parent = NULL; + if (win->gs_session == NULL) { gtk_label_set_label (GTK_LABEL (win->globalshortcuts_activations), "not created"); return; } - xdp_global_shortcuts_session_configure_shortcuts (win->gs_session, - NULL, + parent = xdp_parent_new_gtk (GTK_WINDOW (win)); + + xdp_global_shortcuts_session_configure_shortcuts_with_parent (win->gs_session, + parent, NULL, NULL, globalshortcuts_configure_done, diff --git a/tests/globalshortcuts.c b/tests/globalshortcuts.c new file mode 100644 index 00000000..5a0e9b96 --- /dev/null +++ b/tests/globalshortcuts.c @@ -0,0 +1,495 @@ +/* SPDX-License-Identifier: LGPL-3.0-only */ + +#include "config.h" + +#include +#include +#include + +#define PORTAL_NAME "org.freedesktop.portal.TestGlobalShortcuts" +#define SHORTCUTS_IFACE "org.freedesktop.portal.GlobalShortcuts" +#define SESSION_PATH "/org/freedesktop/portal/desktop/session/test/globalshortcuts" +#define EXPORTED_HANDLE "wayland:test-parent" + +typedef enum { + NULL_PARENT, + SYNC_PARENT, + DEFERRED_PARENT, + EXPORT_FAILURE, + CANCEL_BEFORE_EXPORT, + CANCEL_DURING_EXPORT, + METHOD_ERROR, + PORTAL_CANCELLED, + LEGACY_HANDLE, + LEGACY_NULL, +} Mode; + +typedef struct { + const char *name; + gboolean bind; + Mode mode; +} TestCase; + +typedef struct { + GDBusConnection *bus; + GDBusNodeInfo *node; + guint registration; + GMainLoop *loop; + XdpPortal *portal; + XdpGlobalShortcutsSession *session; + GAsyncResult *result; + const TestCase *test; + guint calls; + guint completions; + char *parent_handle; + GVariant *shortcuts; + GVariant *options; + guint exports; + guint unexports; + XdpParent *pending_parent; + XdpParentExported pending_callback; + gpointer pending_data; +} Fixture; + +static const char portal_xml[] = + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + "" + ""; + +static void +return_response (Fixture *f, + const char *sender, + GDBusMethodInvocation *invocation, + guint response, + GVariantBuilder *results, + GVariant *options) +{ + g_autofree char *sender_path = g_strdup (sender + 1); + g_autofree char *request_path = NULL; + g_autoptr(GError) error = NULL; + const char *token; + + g_assert_true (g_variant_lookup (options, "handle_token", "&s", &token)); + g_strdelimit (sender_path, ".", '_'); + request_path = g_strconcat (REQUEST_PATH_PREFIX, sender_path, "/", token, NULL); + + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(o)", request_path)); + /* Unicast responses also work with libportal's NO_MATCH_RULE subscriptions. */ + g_dbus_connection_emit_signal (f->bus, sender, request_path, + REQUEST_INTERFACE, "Response", + g_variant_new ("(ua{sv})", response, results), &error); + g_assert_no_error (error); +} + +static void +method_call (GDBusConnection *connection, + const char *sender, + const char *path, + const char *interface, + const char *method, + GVariant *parameters, + GDBusMethodInvocation *invocation, + gpointer data) +{ + Fixture *f = data; + GVariantBuilder results; + const char *session; + const char *parent; + + g_variant_builder_init (&results, G_VARIANT_TYPE_VARDICT); + if (g_str_equal (method, "CreateSession")) + { + g_autoptr(GVariant) options = NULL; + + g_variant_get (parameters, "(@a{sv})", &options); + g_variant_builder_add (&results, "{sv}", "session_handle", + g_variant_new_string (SESSION_PATH)); + return_response (f, sender, invocation, 0, &results, options); + return; + } + + f->calls++; + if (g_str_equal (method, "BindShortcuts")) + g_variant_get (parameters, "(&o@a(sa{sv})&s@a{sv})", + &session, &f->shortcuts, &parent, &f->options); + else + { + g_assert_cmpstr (method, ==, "ConfigureShortcuts"); + g_variant_get (parameters, "(&o&s@a{sv})", &session, &parent, &f->options); + } + + g_assert_cmpstr (session, ==, SESSION_PATH); + f->parent_handle = g_strdup (parent); + + if (f->test->mode == METHOD_ERROR) + { + g_variant_builder_clear (&results); + g_dbus_method_invocation_return_dbus_error (invocation, + "org.freedesktop.portal.Error.Failed", + "Test method failure"); + return; + } + + if (g_str_equal (method, "BindShortcuts")) + { + GVariantBuilder shortcuts; + GVariantBuilder properties; + + g_variant_builder_init (&shortcuts, G_VARIANT_TYPE ("a(sa{sv})")); + g_variant_builder_init (&properties, G_VARIANT_TYPE_VARDICT); + g_variant_builder_add (&properties, "{sv}", "description", g_variant_new_string ("Do Foo")); + g_variant_builder_add (&properties, "{sv}", "trigger_description", g_variant_new_string ("Control+F")); + g_variant_builder_add (&shortcuts, "(sa{sv})", "foo", &properties); + g_variant_builder_add (&results, "{sv}", "shortcuts", g_variant_builder_end (&shortcuts)); + return_response (f, sender, invocation, + f->test->mode == PORTAL_CANCELLED ? 1 : 0, &results, f->options); + } + else + { + g_variant_builder_clear (&results); + g_dbus_method_invocation_return_value (invocation, g_variant_new ("()")); + } +} + +static const GDBusInterfaceVTable vtable = { + .method_call = method_call, +}; + +static void +capture_result (GObject *source, + GAsyncResult *result, + gpointer data) +{ + Fixture *f = data; + + g_assert_null (f->result); + f->completions++; + f->result = g_object_ref (result); + g_main_loop_quit (f->loop); +} + +static gboolean +timeout (gpointer data) +{ + g_error ("GlobalShortcuts operation did not complete within 10 seconds"); + return G_SOURCE_REMOVE; +} + +static void +wait_result (Fixture *f) +{ + guint timeout_id = g_timeout_add_seconds (10, timeout, NULL); + + g_main_loop_run (f->loop); + g_source_remove (timeout_id); + g_assert_nonnull (f->result); +} + +static void +fixture_setup (Fixture *f) +{ + g_autoptr(GError) error = NULL; + g_autoptr(GVariant) reply = NULL; + guint result; + + f->loop = g_main_loop_new (NULL, FALSE); + f->bus = g_dbus_connection_new_for_address_sync (g_getenv ("DBUS_SESSION_BUS_ADDRESS"), + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT | + G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION, + NULL, NULL, &error); + g_assert_no_error (error); + reply = g_dbus_connection_call_sync (f->bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", + "org.freedesktop.DBus", "RequestName", + g_variant_new ("(su)", PORTAL_NAME, 0), + G_VARIANT_TYPE ("(u)"), G_DBUS_CALL_FLAGS_NONE, + -1, NULL, &error); + g_assert_no_error (error); + g_variant_get (reply, "(u)", &result); + g_assert_cmpuint (result, ==, 1); + + f->node = g_dbus_node_info_new_for_xml (portal_xml, &error); + g_assert_no_error (error); + f->registration = g_dbus_connection_register_object (f->bus, PORTAL_OBJECT_PATH, + f->node->interfaces[0], &vtable, + f, NULL, &error); + g_assert_no_error (error); + f->portal = xdp_portal_initable_new (&error); + g_assert_no_error (error); + g_assert_nonnull (f->portal); + + xdp_portal_create_global_shortcuts_session (f->portal, NULL, capture_result, f); + wait_result (f); + f->session = xdp_portal_create_global_shortcuts_session_finish (f->portal, f->result, &error); + g_assert_no_error (error); + g_assert_nonnull (f->session); + g_clear_object (&f->result); + f->completions = 0; +} + +static void +fixture_clear (Fixture *f) +{ + g_autoptr(GError) error = NULL; + g_autoptr(GVariant) reply = NULL; + + g_clear_object (&f->result); + g_clear_object (&f->session); + /* Drop the portal's registry references to its test sessions. */ + g_hash_table_remove_all (f->portal->sessions); + g_clear_object (&f->portal); + g_dbus_connection_unregister_object (f->bus, f->registration); + reply = g_dbus_connection_call_sync (f->bus, "org.freedesktop.DBus", "/org/freedesktop/DBus", + "org.freedesktop.DBus", "ReleaseName", + g_variant_new ("(s)", PORTAL_NAME), + G_VARIANT_TYPE ("(u)"), G_DBUS_CALL_FLAGS_NONE, + -1, NULL, &error); + g_assert_no_error (error); + g_assert_nonnull (reply); + g_dbus_connection_close_sync (f->bus, NULL, NULL); + g_clear_object (&f->bus); + g_clear_pointer (&f->node, g_dbus_node_info_unref); + g_clear_pointer (&f->loop, g_main_loop_unref); + g_clear_pointer (&f->parent_handle, g_free); + g_clear_pointer (&f->shortcuts, g_variant_unref); + g_clear_pointer (&f->options, g_variant_unref); + while (g_main_context_iteration (NULL, FALSE)) + ; +} + +static gboolean +export_parent (XdpParent *parent, + XdpParentExported callback, + gpointer data) +{ + Fixture *f = parent->data; + + f->exports++; + if (f->test->mode == EXPORT_FAILURE) + return FALSE; + + if (f->test->mode == DEFERRED_PARENT || f->test->mode == CANCEL_DURING_EXPORT) + { + f->pending_parent = parent; + f->pending_callback = callback; + f->pending_data = data; + } + else + callback (parent, EXPORTED_HANDLE, data); + + return TRUE; +} + +static void +unexport_parent (XdpParent *parent) +{ + Fixture *f = parent->data; + + f->unexports++; +} + +static void +test_parent (gconstpointer data) +{ + const TestCase *test = data; + Fixture f = { .test = test }; + g_autoptr(XdpParent) parent = NULL; + g_autoptr(GPtrArray) shortcuts = NULL; + g_autoptr(GPtrArray) assigned = NULL; + g_autoptr(GCancellable) cancellable = g_cancellable_new (); + g_autoptr(GError) error = NULL; + g_autofree char *token = g_strdup ("activation-token-234"); + gpointer parent_object = NULL; + gboolean legacy = test->mode == LEGACY_HANDLE || test->mode == LEGACY_NULL; + gboolean has_parent = !legacy && test->mode != NULL_PARENT; + gboolean cancelled = test->mode == CANCEL_BEFORE_EXPORT || test->mode == CANCEL_DURING_EXPORT || + test->mode == PORTAL_CANCELLED; + gboolean success; + + fixture_setup (&f); + shortcuts = g_ptr_array_new_with_free_func ((GDestroyNotify) xdp_global_shortcut_free); + g_ptr_array_add (shortcuts, xdp_global_shortcut_new ("foo", "Do Foo", "CTRL+F")); + if (has_parent) + { + parent = g_new0 (XdpParent, 1); + parent->parent_export = export_parent; + parent->parent_unexport = unexport_parent; + parent->data = &f; + parent->object = g_object_new (G_TYPE_OBJECT, NULL); + parent_object = parent->object; + g_object_add_weak_pointer (parent->object, &parent_object); + } + + if (test->mode == CANCEL_BEFORE_EXPORT) + g_cancellable_cancel (cancellable); + + if (test->bind && legacy) + xdp_global_shortcuts_session_bind_shortcuts (f.session, shortcuts, + test->mode == LEGACY_HANDLE ? "x11:234" : NULL, + cancellable, capture_result, &f); + else if (test->bind) + xdp_global_shortcuts_session_bind_shortcuts_with_parent (f.session, shortcuts, parent, + cancellable, capture_result, &f); + else if (legacy) + xdp_global_shortcuts_session_configure_shortcuts (f.session, + test->mode == LEGACY_HANDLE ? "x11:234" : NULL, + token, cancellable, capture_result, &f); + else + xdp_global_shortcuts_session_configure_shortcuts_with_parent (f.session, parent, + token, cancellable, capture_result, &f); + + /* The public API must retain everything needed after the caller frees its inputs. */ + g_clear_pointer (&parent, xdp_parent_free); + xdp_global_shortcut_set_description (g_ptr_array_index (shortcuts, 0), "Changed after call"); + g_clear_pointer (&shortcuts, g_ptr_array_unref); + token[0] = 'X'; + g_clear_pointer (&token, g_free); + g_assert_null (f.result); /* GAsyncReadyCallback must not run inline. */ + + if (test->mode == DEFERRED_PARENT || test->mode == CANCEL_DURING_EXPORT) + { + g_assert_nonnull (parent_object); + g_assert_nonnull (f.pending_parent); + g_assert_cmpuint (f.calls, ==, 0); + g_assert_cmpuint (f.unexports, ==, 0); + if (test->mode == CANCEL_DURING_EXPORT) + g_cancellable_cancel (cancellable); + f.pending_callback (f.pending_parent, EXPORTED_HANDLE, f.pending_data); + f.pending_parent = NULL; + } + + wait_result (&f); + if (test->bind) + { + if (legacy) + assigned = xdp_global_shortcuts_session_bind_shortcuts_finish (f.session, f.result, &error); + else + assigned = xdp_global_shortcuts_session_bind_shortcuts_with_parent_finish (f.session, f.result, &error); + success = assigned != NULL; + } + else if (legacy) + success = xdp_global_shortcuts_session_configure_shortcuts_finish (f.session, f.result, &error); + else + success = xdp_global_shortcuts_session_configure_shortcuts_with_parent_finish (f.session, f.result, &error); + + if (cancelled) + { + g_assert_false (success); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED); + } + else if (test->mode == EXPORT_FAILURE) + { + g_assert_false (success); + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); + } + else if (test->mode == METHOD_ERROR) + { + g_assert_false (success); + g_assert_nonnull (error); + g_assert_true (g_dbus_error_is_remote_error (error)); + } + else + { + g_assert_no_error (error); + g_assert_true (success); + if (test->bind) + { + XdpGlobalShortcutAssigned *shortcut; + + g_assert_cmpuint (assigned->len, ==, 1); + shortcut = g_ptr_array_index (assigned, 0); + g_assert_cmpstr (xdp_global_shortcut_assigned_get_shortcut_id (shortcut), ==, "foo"); + g_assert_cmpstr (xdp_global_shortcut_assigned_get_trigger_description (shortcut), ==, "Control+F"); + } + } + + if (test->mode == EXPORT_FAILURE || test->mode == CANCEL_BEFORE_EXPORT || test->mode == CANCEL_DURING_EXPORT) + g_assert_cmpuint (f.calls, ==, 0); + else + { + g_assert_cmpuint (f.calls, ==, 1); + g_assert_cmpstr (f.parent_handle, ==, has_parent ? EXPORTED_HANDLE : + test->mode == LEGACY_HANDLE ? "x11:234" : ""); + if (test->bind) + { + g_autoptr(GVariant) item = g_variant_get_child_value (f.shortcuts, 0); + g_autoptr(GVariant) properties = g_variant_get_child_value (item, 1); + const char *description; + const char *trigger; + + g_assert_true (g_variant_lookup (properties, "description", "&s", &description)); + g_assert_true (g_variant_lookup (properties, "preferred_trigger", "&s", &trigger)); + g_assert_cmpstr (description, ==, "Do Foo"); + g_assert_cmpstr (trigger, ==, "CTRL+F"); + } + else + { + const char *activation_token; + + g_assert_true (g_variant_lookup (f.options, "activation_token", "&s", &activation_token)); + g_assert_cmpstr (activation_token, ==, "activation-token-234"); + } + } + + g_assert_cmpuint (f.exports, ==, has_parent && test->mode != CANCEL_BEFORE_EXPORT ? 1 : 0); + g_assert_cmpuint (f.unexports, ==, has_parent && test->mode != EXPORT_FAILURE && + test->mode != CANCEL_BEFORE_EXPORT ? 1 : 0); + g_assert_null (parent_object); + g_assert_cmpuint (f.completions, ==, 1); + fixture_clear (&f); +} + +int +main (int argc, char **argv) +{ + static const TestCase tests[] = { + { "bind/null-parent", TRUE, NULL_PARENT }, + { "bind/synchronous-export", TRUE, SYNC_PARENT }, + { "bind/deferred-export", TRUE, DEFERRED_PARENT }, + { "bind/export-failure", TRUE, EXPORT_FAILURE }, + { "bind/cancel-before-export", TRUE, CANCEL_BEFORE_EXPORT }, + { "bind/cancel-during-export", TRUE, CANCEL_DURING_EXPORT }, + { "bind/method-error", TRUE, METHOD_ERROR }, + { "bind/portal-cancelled", TRUE, PORTAL_CANCELLED }, + { "bind/legacy-handle", TRUE, LEGACY_HANDLE }, + { "bind/legacy-null", TRUE, LEGACY_NULL }, + { "configure/null-parent", FALSE, NULL_PARENT }, + { "configure/synchronous-export", FALSE, SYNC_PARENT }, + { "configure/deferred-export", FALSE, DEFERRED_PARENT }, + { "configure/export-failure", FALSE, EXPORT_FAILURE }, + { "configure/cancel-before-export", FALSE, CANCEL_BEFORE_EXPORT }, + { "configure/cancel-during-export", FALSE, CANCEL_DURING_EXPORT }, + { "configure/method-error", FALSE, METHOD_ERROR }, + { "configure/legacy-handle", FALSE, LEGACY_HANDLE }, + { "configure/legacy-null", FALSE, LEGACY_NULL }, + }; + g_autoptr(GTestDBus) bus = NULL; + int result; + + g_test_init (&argc, &argv, NULL); + g_setenv ("LIBPORTAL_TEST_SUITE", "1", TRUE); + g_setenv ("LIBPORTAL_PORTAL_BUS_NAME", PORTAL_NAME, TRUE); + bus = g_test_dbus_new (G_TEST_DBUS_NONE); + g_test_dbus_up (bus); + for (guint i = 0; i < G_N_ELEMENTS (tests); i++) + { + g_autofree char *name = g_strconcat ("/globalshortcuts/", tests[i].name, NULL); + + g_test_add_data_func (name, &tests[i], test_parent); + } + result = g_test_run (); + g_test_dbus_down (bus); + return result; +} diff --git a/tests/meson.build b/tests/meson.build index 15c30789..35eff6f6 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -1,3 +1,10 @@ +globalshortcuts_test = executable('globalshortcuts-test', + 'globalshortcuts.c', + include_directories: [top_inc, libportal_inc], + dependencies: libportal_dep, +) +test('globalshortcuts', globalshortcuts_test, timeout: 60) + if 'qt5' in enabled_backends subdir('qt5') endif