diff options
author | Philip Withnall <pwithnall@endlessos.org> | 2023-03-22 14:56:08 +0000 |
---|---|---|
committer | Philip Withnall <pwithnall@endlessos.org> | 2023-03-22 15:12:52 +0000 |
commit | e4546614a8f59793d89d201279dd1fd9f3db99d3 (patch) | |
tree | 572387d011325d5161c602aaa75c67b3f0f30669 /gio | |
parent | 666c72c9c5bff66c023aad806dc8f46f3da89cb4 (diff) | |
download | glib-e4546614a8f59793d89d201279dd1fd9f3db99d3.tar.gz |
tests: Port appmonitor test from GMainLoop to using GMainContext directly
This makes the exit conditions for each main loop clearer, and
eliminates use of global variables. It introduces no functional changes.
Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
Diffstat (limited to 'gio')
-rw-r--r-- | gio/tests/appmonitor.c | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/gio/tests/appmonitor.c b/gio/tests/appmonitor.c index e20860cec..6b7c1f723 100644 --- a/gio/tests/appmonitor.c +++ b/gio/tests/appmonitor.c @@ -76,22 +76,24 @@ delete_app (gpointer data) g_remove (path); } -static gboolean changed_fired; - static void -changed_cb (GAppInfoMonitor *monitor, GMainLoop *loop) +changed_cb (GAppInfoMonitor *monitor, + gpointer user_data) { - changed_fired = TRUE; - g_main_loop_quit (loop); + gboolean *changed_fired = user_data; + + *changed_fired = TRUE; + g_main_context_wakeup (g_main_context_get_thread_default ()); } static gboolean -quit_loop (gpointer data) +timeout_cb (gpointer user_data) { - GMainLoop *loop = data; + gboolean *timed_out = user_data; - if (g_main_loop_is_running (loop)) - g_main_loop_quit (loop); + g_assert (!timed_out); + *timed_out = TRUE; + g_main_context_wakeup (g_main_context_get_thread_default ()); return G_SOURCE_REMOVE; } @@ -104,10 +106,11 @@ test_app_monitor (Fixture *fixture, #ifdef G_OS_UNIX gchar *app_path; GAppInfoMonitor *monitor; - GMainLoop *loop; GMainContext *context = NULL; /* use the global default main context */ GSource *timeout_source = NULL; GDesktopAppInfo *app = NULL; + gboolean changed_fired = FALSE; + gboolean timed_out = FALSE; app_path = g_build_filename (fixture->applications_dir, "app.desktop", NULL); @@ -115,16 +118,17 @@ test_app_monitor (Fixture *fixture, g_list_free_full (g_app_info_get_all (), g_object_unref); monitor = g_app_info_monitor_get (); - loop = g_main_loop_new (context, FALSE); - g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop); + g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), &changed_fired); g_idle_add (create_app, app_path); timeout_source = g_timeout_source_new_seconds (3); - g_source_set_callback (timeout_source, quit_loop, loop, NULL); + g_source_set_callback (timeout_source, timeout_cb, &timed_out, NULL); g_source_attach (timeout_source, NULL); - g_main_loop_run (loop); + while (!changed_fired && !timed_out) + g_main_context_iteration (context, TRUE); + g_assert_true (changed_fired); changed_fired = FALSE; @@ -138,18 +142,18 @@ test_app_monitor (Fixture *fixture, g_clear_object (&app); timeout_source = g_timeout_source_new_seconds (3); - g_source_set_callback (timeout_source, quit_loop, loop, NULL); + g_source_set_callback (timeout_source, timeout_cb, &timed_out, NULL); g_source_attach (timeout_source, NULL); delete_app (app_path); - g_main_loop_run (loop); + while (!changed_fired && !timed_out) + g_main_context_iteration (context, TRUE); g_assert_true (changed_fired); g_source_destroy (timeout_source); g_clear_pointer (&timeout_source, g_source_unref); - g_main_loop_unref (loop); g_remove (app_path); g_object_unref (monitor); |