tests: Stop using GMainLoop in actions test

Instead, iterate the `GMainContext` directly. This allows tests on
asynchronously returned values to be done in the actual test function,
rather than a callback, which should make the tests a little clearer.

This introduces no functional changes.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2022-11-10 12:47:27 +00:00
parent 19eee4bc41
commit b836ed5c13

View File

@ -2,6 +2,7 @@
* Copyright © 2010, 2011, 2013, 2014 Codethink Limited * Copyright © 2010, 2011, 2013, 2014 Codethink Limited
* Copyright © 2010, 2011, 2012, 2013, 2015 Red Hat, Inc. * Copyright © 2010, 2011, 2012, 2013, 2015 Red Hat, Inc.
* Copyright © 2012 Pavel Vasin * Copyright © 2012 Pavel Vasin
* Copyright © 2022 Endless OS Foundation, LLC
* *
* SPDX-License-Identifier: LGPL-2.1-or-later * SPDX-License-Identifier: LGPL-2.1-or-later
* *
@ -644,11 +645,13 @@ compare_action_groups (GActionGroup *a, GActionGroup *b)
} }
static gboolean static gboolean
stop_loop (gpointer data) timeout_cb (gpointer user_data)
{ {
GMainLoop *loop = data; gboolean *timed_out = user_data;
g_main_loop_quit (loop); g_assert_false (*timed_out);
*timed_out = TRUE;
g_main_context_wakeup (NULL);
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
@ -664,96 +667,16 @@ static GActionEntry exported_entries[] = {
}; };
static void static void
list_cb (GObject *source, async_result_cb (GObject *source,
GAsyncResult *res, GAsyncResult *res,
gpointer user_data) gpointer user_data)
{ {
GDBusConnection *bus = G_DBUS_CONNECTION (source); GAsyncResult **result_out = user_data;
GMainLoop *loop = user_data;
GError *error = NULL;
GVariant *v;
gchar **actions;
v = g_dbus_connection_call_finish (bus, res, &error); g_assert_null (*result_out);
g_assert_nonnull (v); *result_out = g_object_ref (res);
g_variant_get (v, "(^a&s)", &actions);
g_assert_cmpint (g_strv_length (actions), ==, G_N_ELEMENTS (exported_entries));
g_free (actions);
g_variant_unref (v);
g_main_loop_quit (loop);
}
static gboolean g_main_context_wakeup (NULL);
call_list (gpointer user_data)
{
GDBusConnection *bus;
bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_dbus_connection_call (bus,
g_dbus_connection_get_unique_name (bus),
"/",
"org.gtk.Actions",
"List",
NULL,
NULL,
0,
G_MAXINT,
NULL,
list_cb,
user_data);
g_object_unref (bus);
return G_SOURCE_REMOVE;
}
static void
describe_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GDBusConnection *bus = G_DBUS_CONNECTION (source);
GMainLoop *loop = user_data;
GError *error = NULL;
GVariant *v;
gboolean enabled;
gchar *param;
GVariantIter *iter;
v = g_dbus_connection_call_finish (bus, res, &error);
g_assert_nonnull (v);
/* FIXME: there's an extra level of tuplelization in here */
g_variant_get (v, "((bgav))", &enabled, &param, &iter);
g_assert_true (enabled);
g_assert_cmpstr (param, ==, "");
g_assert_cmpint (g_variant_iter_n_children (iter), ==, 0);
g_free (param);
g_variant_iter_free (iter);
g_variant_unref (v);
g_main_loop_quit (loop);
}
static gboolean
call_describe (gpointer user_data)
{
GDBusConnection *bus;
bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_dbus_connection_call (bus,
g_dbus_connection_get_unique_name (bus),
"/",
"org.gtk.Actions",
"Describe",
g_variant_new ("(s)", "copy"),
NULL,
0,
G_MAXINT,
NULL,
describe_cb,
user_data);
g_object_unref (bus);
return G_SOURCE_REMOVE;
} }
G_GNUC_BEGIN_IGNORE_DEPRECATIONS G_GNUC_BEGIN_IGNORE_DEPRECATIONS
@ -800,15 +723,16 @@ test_dbus_export (void)
GSimpleActionGroup *group; GSimpleActionGroup *group;
GDBusActionGroup *proxy; GDBusActionGroup *proxy;
GSimpleAction *action; GSimpleAction *action;
GMainLoop *loop;
GError *error = NULL; GError *error = NULL;
GVariant *v; GVariant *v;
guint id; guint id;
gchar **actions; gchar **actions;
guint n_actions_added = 0, n_actions_enabled_changed = 0, n_actions_removed = 0, n_actions_state_changed = 0; guint n_actions_added = 0, n_actions_enabled_changed = 0, n_actions_removed = 0, n_actions_state_changed = 0;
gulong added_signal_id, enabled_changed_signal_id, removed_signal_id, state_changed_signal_id; gulong added_signal_id, enabled_changed_signal_id, removed_signal_id, state_changed_signal_id;
gboolean enabled;
loop = g_main_loop_new (NULL, FALSE); gchar *param;
GVariantIter *iter;
GAsyncResult *async_result = NULL;
session_bus_up (); session_bus_up ();
bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
@ -842,12 +766,58 @@ test_dbus_export (void)
g_strfreev (actions); g_strfreev (actions);
/* check that calling "List" works too */ /* check that calling "List" works too */
g_idle_add (call_list, loop); g_dbus_connection_call (bus,
g_main_loop_run (loop); g_dbus_connection_get_unique_name (bus),
"/",
"org.gtk.Actions",
"List",
NULL,
NULL,
0,
G_MAXINT,
NULL,
async_result_cb,
&async_result);
while (async_result == NULL)
g_main_context_iteration (NULL, TRUE);
v = g_dbus_connection_call_finish (bus, async_result, &error);
g_assert_nonnull (v);
g_variant_get (v, "(^a&s)", &actions);
g_assert_cmpuint (g_strv_length (actions), ==, G_N_ELEMENTS (exported_entries));
g_free (actions);
g_variant_unref (v);
g_clear_object (&async_result);
/* check that calling "Describe" works */ /* check that calling "Describe" works */
g_idle_add (call_describe, loop); g_dbus_connection_call (bus,
g_main_loop_run (loop); g_dbus_connection_get_unique_name (bus),
"/",
"org.gtk.Actions",
"Describe",
g_variant_new ("(s)", "copy"),
NULL,
0,
G_MAXINT,
NULL,
async_result_cb,
&async_result);
while (async_result == NULL)
g_main_context_iteration (NULL, TRUE);
v = g_dbus_connection_call_finish (bus, async_result, &error);
g_assert_nonnull (v);
/* FIXME: there's an extra level of tuplelization in here */
g_variant_get (v, "((bgav))", &enabled, &param, &iter);
g_assert_true (enabled);
g_assert_cmpstr (param, ==, "");
g_assert_cmpint (g_variant_iter_n_children (iter), ==, 0);
g_free (param);
g_variant_iter_free (iter);
g_variant_unref (v);
g_clear_object (&async_result);
/* test that the initial transfer works */ /* test that the initial transfer works */
g_assert_true (G_IS_DBUS_ACTION_GROUP (proxy)); g_assert_true (G_IS_DBUS_ACTION_GROUP (proxy));
@ -931,7 +901,6 @@ test_dbus_export (void)
g_signal_handler_disconnect (proxy, state_changed_signal_id); g_signal_handler_disconnect (proxy, state_changed_signal_id);
g_object_unref (proxy); g_object_unref (proxy);
g_object_unref (group); g_object_unref (group);
g_main_loop_unref (loop);
g_object_unref (bus); g_object_unref (bus);
session_bus_down (); session_bus_down ();
@ -1016,9 +985,7 @@ test_bug679509 (void)
{ {
GDBusConnection *bus; GDBusConnection *bus;
GDBusActionGroup *proxy; GDBusActionGroup *proxy;
GMainLoop *loop; gboolean timed_out = FALSE;
loop = g_main_loop_new (NULL, FALSE);
session_bus_up (); session_bus_up ();
bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL); bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
@ -1027,10 +994,10 @@ test_bug679509 (void)
g_strfreev (g_action_group_list_actions (G_ACTION_GROUP (proxy))); g_strfreev (g_action_group_list_actions (G_ACTION_GROUP (proxy)));
g_object_unref (proxy); g_object_unref (proxy);
g_timeout_add (100, stop_loop, loop); g_timeout_add (100, timeout_cb, &timed_out);
g_main_loop_run (loop); while (!timed_out)
g_main_context_iteration (NULL, TRUE);
g_main_loop_unref (loop);
g_object_unref (bus); g_object_unref (bus);
session_bus_down (); session_bus_down ();