From 724a7eee7496709f22de76213b8769ea90ab6393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Tue, 11 May 2021 09:35:04 +0200 Subject: [PATCH] tests: Move 'wait-for-X11-display' helper to MetaContextTest Gets rid of a un-prefixed helper function relying on a global singleton. Part-of: --- src/core/meta-context-private.h | 2 ++ src/tests/meta-context-test.c | 11 +++++++++++ src/tests/meta-context-test.h | 3 +++ src/tests/meta-test-utils.c | 22 +++++----------------- src/tests/meta-test-utils.h | 5 ++--- src/tests/monitor-unit-tests.c | 22 ++++++++++++++-------- src/tests/monitor-unit-tests.h | 2 +- src/tests/test-runner.c | 19 +++++++++++-------- src/tests/unit-tests.c | 6 ++++++ src/tests/unit-tests.h | 25 +++++++++++++++++++++++++ 10 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 src/tests/unit-tests.h diff --git a/src/core/meta-context-private.h b/src/core/meta-context-private.h index 503d78ef7..a3b7f9b4b 100644 --- a/src/core/meta-context-private.h +++ b/src/core/meta-context-private.h @@ -22,6 +22,7 @@ #define META_CONTEXT_PRIVATE_H #include "core/meta-private-enums.h" +#include "core/util-private.h" #include "meta/meta-backend.h" #include "meta/meta-context.h" #include "wayland/meta-wayland-types.h" @@ -54,6 +55,7 @@ const char * meta_context_get_name (MetaContext *context); const char * meta_context_get_gnome_wm_keybindings (MetaContext *context); +META_EXPORT_TEST MetaWaylandCompositor * meta_context_get_wayland_compositor (MetaContext *context); MetaX11DisplayPolicy meta_context_get_x11_display_policy (MetaContext *context); diff --git a/src/tests/meta-context-test.c b/src/tests/meta-context-test.c index 0de4d3903..9eafab250 100644 --- a/src/tests/meta-context-test.c +++ b/src/tests/meta-context-test.c @@ -256,6 +256,17 @@ meta_context_test_run_tests (MetaContextTest *context_test) } } +void +meta_context_test_wait_for_x11_display (MetaContextTest *context_test) +{ + MetaDisplay *display = meta_context_get_display (META_CONTEXT (context_test)); + + while (!meta_display_get_x11_display (display)) + g_main_context_iteration (NULL, TRUE); + + g_assert_nonnull (meta_display_get_x11_display (display)); +} + MetaContext * meta_create_test_context (MetaContextTestType type, MetaContextTestFlag flags) diff --git a/src/tests/meta-context-test.h b/src/tests/meta-context-test.h index f5d18445e..f9c8e1179 100644 --- a/src/tests/meta-context-test.h +++ b/src/tests/meta-context-test.h @@ -49,4 +49,7 @@ MetaContext * meta_create_test_context (MetaContextTestType type, int meta_context_test_run_tests (MetaContextTest *context_test); +META_EXPORT +void meta_context_test_wait_for_x11_display (MetaContextTest *context_test); + #endif /* META_CONTEXT_TEST_H */ diff --git a/src/tests/meta-test-utils.c b/src/tests/meta-test-utils.c index f7acd9230..84d82a303 100644 --- a/src/tests/meta-test-utils.c +++ b/src/tests/meta-test-utils.c @@ -27,6 +27,7 @@ #include "core/display-private.h" #include "core/window-private.h" +#include "tests/meta-context-test.h" #include "wayland/meta-wayland.h" #include "wayland/meta-xwayland.h" #include "x11/meta-x11-display-private.h" @@ -437,7 +438,8 @@ spawn_xwayland (gpointer user_data) } MetaTestClient * -meta_test_client_new (const char *id, +meta_test_client_new (MetaContext *context, + const char *id, MetaWindowClientType type, GError **error) { @@ -452,7 +454,7 @@ meta_test_client_new (const char *id, G_SUBPROCESS_FLAGS_STDOUT_PIPE)); g_assert (meta_is_wayland_compositor ()); - compositor = meta_wayland_compositor_get_default (); + compositor = meta_context_get_wayland_compositor (context); wayland_display_name = meta_wayland_get_wayland_display_name (compositor); x11_display_name = meta_wayland_get_public_xwayland_display_name (compositor); @@ -498,7 +500,7 @@ meta_test_client_new (const char *id, thread = g_thread_new ("Mutter Spawn Xwayland Thread", spawn_xwayland, NULL); - test_wait_for_x11_display (); + meta_context_test_wait_for_x11_display (META_CONTEXT_TEST (context)); g_thread_join (thread); } @@ -563,17 +565,3 @@ meta_test_get_plugin_name (void) else return "libdefault"; } - -void -test_wait_for_x11_display (void) -{ - MetaDisplay *display; - - display = meta_get_display (); - g_assert_nonnull (display); - - while (!display->x11_display) - g_main_context_iteration (NULL, TRUE); - - g_assert_nonnull (display->x11_display); -} diff --git a/src/tests/meta-test-utils.h b/src/tests/meta-test-utils.h index 2062063db..833f86fe9 100644 --- a/src/tests/meta-test-utils.h +++ b/src/tests/meta-test-utils.h @@ -73,7 +73,8 @@ void meta_test_client_wait_for_window_shown (MetaTestClient *client, gboolean meta_test_client_quit (MetaTestClient *client, GError **error); -MetaTestClient * meta_test_client_new (const char *id, +MetaTestClient * meta_test_client_new (MetaContext *context, + const char *id, MetaWindowClientType type, GError **error); @@ -81,8 +82,6 @@ void meta_test_client_destroy (MetaTestClient *client); const char * meta_test_get_plugin_name (void); -void test_wait_for_x11_display (void); - void meta_ensure_test_client_path (int argc, char **argv); diff --git a/src/tests/monitor-unit-tests.c b/src/tests/monitor-unit-tests.c index d1a92f012..6d6d5fce4 100644 --- a/src/tests/monitor-unit-tests.c +++ b/src/tests/monitor-unit-tests.c @@ -33,6 +33,7 @@ #include "tests/meta-monitor-manager-test.h" #include "tests/monitor-test-utils.h" #include "tests/meta-test-utils.h" +#include "tests/unit-tests.h" #include "x11/meta-x11-display-private.h" static MonitorTestCase initial_test_case = { @@ -175,17 +176,19 @@ monitor_tests_alarm_filter (MetaX11Display *x11_display, } static void -create_monitor_test_clients (void) +create_monitor_test_clients (MetaContext *context) { GError *error = NULL; - wayland_monitor_test_client = meta_test_client_new (WAYLAND_TEST_CLIENT_NAME, + wayland_monitor_test_client = meta_test_client_new (context, + WAYLAND_TEST_CLIENT_NAME, META_WINDOW_CLIENT_TYPE_WAYLAND, &error); if (!wayland_monitor_test_client) g_error ("Failed to launch Wayland test client: %s", error->message); - x11_monitor_test_client = meta_test_client_new (X11_TEST_CLIENT_NAME, + x11_monitor_test_client = meta_test_client_new (context, + X11_TEST_CLIENT_NAME, META_WINDOW_CLIENT_TYPE_X11, &error); if (!x11_monitor_test_client) @@ -5482,7 +5485,8 @@ dispatch (void) } static MetaTestClient * -create_test_window (const char *window_name) +create_test_window (MetaContext *context, + const char *window_name) { MetaTestClient *test_client; static int client_count = 0; @@ -5490,7 +5494,8 @@ create_test_window (const char *window_name) g_autoptr (GError) error = NULL; client_name = g_strdup_printf ("test_client_%d", client_count++); - test_client = meta_test_client_new (client_name, META_WINDOW_CLIENT_TYPE_WAYLAND, + test_client = meta_test_client_new (context, + client_name, META_WINDOW_CLIENT_TYPE_WAYLAND, &error); if (!test_client) g_error ("Failed to launch test client: %s", error->message); @@ -5506,6 +5511,7 @@ create_test_window (const char *window_name) static void meta_test_monitor_wm_tiling (void) { + MetaContext *context = test_context; MonitorTestCase test_case = initial_test_case; MetaMonitorTestSetup *test_setup; g_autoptr (GError) error = NULL; @@ -5523,7 +5529,7 @@ meta_test_monitor_wm_tiling (void) */ const char *test_window_name= "window1"; - test_client = create_test_window (test_window_name); + test_client = create_test_window (context, test_window_name); if (!meta_test_client_do (test_client, &error, "show", test_window_name, @@ -5850,9 +5856,9 @@ init_monitor_tests (void) } void -pre_run_monitor_tests (void) +pre_run_monitor_tests (MetaContext *context) { - create_monitor_test_clients (); + create_monitor_test_clients (context); } void diff --git a/src/tests/monitor-unit-tests.h b/src/tests/monitor-unit-tests.h index b497c8a3d..bbf096b39 100644 --- a/src/tests/monitor-unit-tests.h +++ b/src/tests/monitor-unit-tests.h @@ -27,7 +27,7 @@ typedef struct _MonitorTestCase MonitorTestCase; void init_monitor_tests (void); -void pre_run_monitor_tests (void); +void pre_run_monitor_tests (MetaContext *context); void finish_monitor_tests (void); diff --git a/src/tests/test-runner.c b/src/tests/test-runner.c index 395807e3c..4f6bdc81a 100644 --- a/src/tests/test-runner.c +++ b/src/tests/test-runner.c @@ -34,6 +34,7 @@ #include "x11/meta-x11-display-private.h" typedef struct { + MetaContext *context; GHashTable *clients; MetaAsyncWaiter *waiter; GString *warning_messages; @@ -75,14 +76,14 @@ on_x11_display_opened (MetaDisplay *display, } static TestCase * -test_case_new (void) +test_case_new (MetaContext *context) { TestCase *test = g_new0 (TestCase, 1); - MetaDisplay *display = meta_get_display (); + MetaDisplay *display = meta_context_get_display (context); if (!meta_is_wayland_compositor ()) { - test_wait_for_x11_display (); + meta_context_test_wait_for_x11_display (META_CONTEXT_TEST (context)); on_x11_display_opened (display, test); } else @@ -96,6 +97,7 @@ test_case_new (void) test); } + test->context = context; test->clients = g_hash_table_new (g_str_hash, g_str_equal); test->loop = g_main_loop_new (NULL, FALSE); @@ -485,7 +487,7 @@ test_case_do (TestCase *test, if (g_hash_table_lookup (test->clients, argv[1])) BAD_COMMAND("client %s already exists", argv[1]); - client = meta_test_client_new (argv[1], type, error); + client = meta_test_client_new (test->context, argv[1], type, error); if (!client) return FALSE; @@ -953,10 +955,11 @@ test_case_destroy (TestCase *test, /**********************************************************************/ static gboolean -run_test (const char *filename, - int index) +run_test (MetaContext *context, + const char *filename, + int index) { - TestCase *test = test_case_new (); + TestCase *test = test_case_new (context); GError *error = NULL; GFile *file = g_file_new_for_path (filename); @@ -1074,7 +1077,7 @@ run_tests (MetaContext *context, for (i = 0; i < info->n_tests; i++) { - if (!run_test (info->tests[i], i + 1)) + if (!run_test (context, info->tests[i], i + 1)) success = FALSE; } diff --git a/src/tests/unit-tests.c b/src/tests/unit-tests.c index 0ef5aff02..664fb24c5 100644 --- a/src/tests/unit-tests.c +++ b/src/tests/unit-tests.c @@ -19,6 +19,8 @@ #include "config.h" +#include "tests/unit-tests.h" + #include #include @@ -35,6 +37,8 @@ #include "tests/monitor-transform-tests.h" #include "tests/wayland-unit-tests.h" +MetaContext *test_context; + typedef struct _MetaTestLaterOrderCallbackData { GMainLoop *loop; /* Loop to terminate when done. */ @@ -239,6 +243,8 @@ main (int argc, char *argv[]) META_CONTEXT_TEST_FLAG_TEST_CLIENT); g_assert (meta_context_configure (context, &argc, &argv, NULL)); + test_context = context; + init_tests (); g_signal_connect (context, "before-tests", diff --git a/src/tests/unit-tests.h b/src/tests/unit-tests.h new file mode 100644 index 000000000..30f24979e --- /dev/null +++ b/src/tests/unit-tests.h @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2021 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef UNIT_TESTS_H +#define UNIT_TESTS_H + +#include "meta/meta-context.h" + +extern MetaContext *test_context; + +#endif /* UNIT_TESTS_H */