From 8cf41761a91b124794aa864c32577c8dd3b3b03c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Wed, 20 Nov 2024 20:59:45 +0100 Subject: [PATCH] tests/screen-cast: Move executable launching into helper It doesn't contain anything screen cast specific, except the XDG_RUNTIME_DIR, so make it usable from elsewhere. While at it, prepare it for being able to run the glib test functionality. Part-of: --- src/tests/meta-test-utils.c | 77 ++++++++++++++++++++++++++++++++++ src/tests/meta-test-utils.h | 8 ++++ src/tests/native-screen-cast.c | 51 ++-------------------- 3 files changed, 89 insertions(+), 47 deletions(-) diff --git a/src/tests/meta-test-utils.c b/src/tests/meta-test-utils.c index ea6589535..a4458825c 100644 --- a/src/tests/meta-test-utils.c +++ b/src/tests/meta-test-utils.c @@ -917,3 +917,80 @@ meta_flush_input (MetaContext *context) g_mutex_unlock (&mutex); #endif } + +GSubprocess * +meta_launch_test_executable (const char *name, + const char *argv0, + ...) +{ + g_autoptr (GPtrArray) args = NULL; + const char *arg; + va_list ap; + g_autofree char *test_client_path = NULL; + GSubprocessLauncher *launcher; + GSubprocess *subprocess; + GError *error = NULL; + + args = g_ptr_array_new (); + + test_client_path = g_test_build_filename (G_TEST_BUILT, name, NULL); + g_ptr_array_add (args, test_client_path); + + va_start (ap, argv0); + g_ptr_array_add (args, (char *) argv0); + while ((arg = va_arg (ap, const char *))) + g_ptr_array_add (args, (char *) arg); + + g_ptr_array_add (args, NULL); + va_end (ap); + + launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE); + g_subprocess_launcher_setenv (launcher, + "XDG_RUNTIME_DIR", getenv ("XDG_RUNTIME_DIR"), + TRUE); + g_subprocess_launcher_setenv (launcher, + "G_TEST_SRCDIR", g_test_get_dir (G_TEST_DIST), + TRUE); + g_subprocess_launcher_setenv (launcher, + "G_TEST_BUILDDIR", g_test_get_dir (G_TEST_BUILT), + TRUE); + g_subprocess_launcher_setenv (launcher, + "G_MESSAGES_DEBUG", "all", + TRUE); + subprocess = g_subprocess_launcher_spawnv (launcher, + (const char * const *) args->pdata, + &error); + if (!subprocess) + g_error ("Failed to launch screen cast test client: %s", error->message); + + return subprocess; +} + +static void +test_client_exited (GObject *source_object, + GAsyncResult *result, + gpointer user_data) +{ + GError *error = NULL; + + if (!g_subprocess_wait_finish (G_SUBPROCESS (source_object), + result, + &error)) + g_error ("Screen cast test client exited with an error: %s", error->message); + + g_main_loop_quit (user_data); +} + +void +meta_wait_test_process (GSubprocess *subprocess) +{ + GMainLoop *loop; + + loop = g_main_loop_new (NULL, FALSE); + g_subprocess_wait_check_async (subprocess, + NULL, + test_client_exited, + loop); + g_main_loop_run (loop); + g_assert_true (g_subprocess_get_successful (subprocess)); +} diff --git a/src/tests/meta-test-utils.h b/src/tests/meta-test-utils.h index c89ca52a6..42fec57db 100644 --- a/src/tests/meta-test-utils.h +++ b/src/tests/meta-test-utils.h @@ -128,3 +128,11 @@ MetaVirtualMonitor * meta_create_test_monitor (MetaContext *context, META_EXPORT void meta_flush_input (MetaContext *context); + +META_EXPORT +GSubprocess * meta_launch_test_executable (const char *name, + const char *argv0, + ...); + +META_EXPORT +void meta_wait_test_process (GSubprocess *subprocess); diff --git a/src/tests/native-screen-cast.c b/src/tests/native-screen-cast.c index 5c5115fae..01d4a876f 100644 --- a/src/tests/native-screen-cast.c +++ b/src/tests/native-screen-cast.c @@ -23,61 +23,18 @@ #include #include "meta/util.h" +#include "tests/meta-test-utils.h" #include "tests/meta-test/meta-context-test.h" -static void -test_client_exited (GObject *source_object, - GAsyncResult *result, - gpointer user_data) -{ - GError *error = NULL; - - if (!g_subprocess_wait_finish (G_SUBPROCESS (source_object), - result, - &error)) - g_error ("Screen cast test client exited with an error: %s", error->message); - - g_main_loop_quit (user_data); -} - static void meta_test_screen_cast_record_virtual (void) { - GSubprocessLauncher *launcher; - g_autofree char *test_client_path = NULL; - GError *error = NULL; - GSubprocess *subprocess; - GMainLoop *loop; - - launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE); + g_autoptr (GSubprocess) subprocess = NULL; meta_add_verbose_topic (META_DEBUG_SCREEN_CAST); - - test_client_path = g_test_build_filename (G_TEST_BUILT, - "mutter-screen-cast-client", + subprocess = meta_launch_test_executable ("mutter-screen-cast-client", NULL); - g_subprocess_launcher_setenv (launcher, - "XDG_RUNTIME_DIR", getenv ("XDG_RUNTIME_DIR"), - TRUE); - g_subprocess_launcher_setenv (launcher, - "G_MESSAGES_DEBUG", "all", - TRUE); - subprocess = g_subprocess_launcher_spawn (launcher, - &error, - test_client_path, - NULL); - if (!subprocess) - g_error ("Failed to launch screen cast test client: %s", error->message); - - loop = g_main_loop_new (NULL, FALSE); - g_subprocess_wait_check_async (subprocess, - NULL, - test_client_exited, - loop); - g_main_loop_run (loop); - g_assert_true (g_subprocess_get_successful (subprocess)); - g_object_unref (subprocess); - + meta_wait_test_process (subprocess); meta_remove_verbose_topic (META_DEBUG_SCREEN_CAST); }