[mutter] tests: Move Wayland test driver helper protocol to its own file



commit 151c80d2ef4d31b1a642ed94fcf8d2ab0a0ac584
Author: Jonas Ã…dahl <jadahl gmail com>
Date:   Sat Apr 10 16:32:02 2021 +0100

    tests: Move Wayland test driver helper protocol to its own file
    
    While at it, make it a GObject so one can add signals to it.
    
    Fixes: https://gitlab.gnome.org/GNOME/mutter/-/issues/1716
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1795>

 src/tests/meson.build                |   2 +
 src/tests/meta-wayland-test-driver.c | 128 +++++++++++++++++++++++++++++++++++
 src/tests/meta-wayland-test-driver.h |  30 ++++++++
 src/tests/wayland-unit-tests.c       |  65 ++----------------
 4 files changed, 164 insertions(+), 61 deletions(-)
---
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 20c4725419..2a2e357822 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -84,6 +84,8 @@ unit_tests = executable('mutter-test-unit-tests',
     'meta-gpu-test.h',
     'meta-monitor-manager-test.c',
     'meta-monitor-manager-test.h',
+    'meta-wayland-test-driver.c',
+    'meta-wayland-test-driver.h',
     'monitor-config-migration-unit-tests.c',
     'monitor-config-migration-unit-tests.h',
     'monitor-store-unit-tests.c',
diff --git a/src/tests/meta-wayland-test-driver.c b/src/tests/meta-wayland-test-driver.c
new file mode 100644
index 0000000000..9913e779fa
--- /dev/null
+++ b/src/tests/meta-wayland-test-driver.c
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "tests/meta-wayland-test-driver.h"
+
+#include <wayland-server.h>
+
+#include "wayland/meta-wayland-actor-surface.h"
+#include "wayland/meta-wayland-private.h"
+
+#include "test-driver-server-protocol.h"
+
+struct _MetaWaylandTestDriver
+{
+  GObject parent;
+
+  struct wl_global *test_driver;
+};
+
+G_DEFINE_TYPE (MetaWaylandTestDriver, meta_wayland_test_driver,
+               G_TYPE_OBJECT)
+
+static void
+on_actor_destroyed (ClutterActor       *actor,
+                    struct wl_resource *callback)
+{
+  wl_callback_send_done (callback, 0);
+  wl_resource_destroy (callback);
+}
+
+static void
+sync_actor_destroy (struct wl_client   *client,
+                    struct wl_resource *resource,
+                    uint32_t            id,
+                    struct wl_resource *surface_resource)
+{
+  MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource);
+  MetaWaylandActorSurface *actor_surface;
+  MetaSurfaceActor *actor;
+  struct wl_resource *callback;
+
+  g_assert_nonnull (surface);
+
+  actor_surface = (MetaWaylandActorSurface *) surface->role;
+  g_assert_nonnull (actor_surface);
+
+  actor = meta_wayland_actor_surface_get_actor (actor_surface);
+  g_assert_nonnull (actor);
+
+  callback = wl_resource_create (client, &wl_callback_interface, 1, id);
+
+  g_signal_connect (actor, "destroy", G_CALLBACK (on_actor_destroyed),
+                    callback);
+}
+
+static const struct test_driver_interface meta_test_driver_interface = {
+  sync_actor_destroy,
+};
+
+static void
+bind_test_driver (struct wl_client *client,
+                  void             *user_data,
+                  uint32_t          version,
+                  uint32_t          id)
+{
+  MetaWaylandTestDriver *test_driver = user_data;
+  struct wl_resource *resource;
+
+  resource = wl_resource_create (client, &test_driver_interface,
+                                 version, id);
+  wl_resource_set_implementation (resource, &meta_test_driver_interface,
+                                  test_driver, NULL);
+}
+
+static void
+meta_wayland_test_driver_finalize (GObject *object)
+{
+  MetaWaylandTestDriver *test_driver = META_WAYLAND_TEST_DRIVER (object);
+
+  g_clear_pointer (&test_driver->test_driver, wl_global_destroy);
+
+  G_OBJECT_CLASS (meta_wayland_test_driver_parent_class)->finalize (object);
+}
+
+static void
+meta_wayland_test_driver_class_init (MetaWaylandTestDriverClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = meta_wayland_test_driver_finalize;
+}
+
+static void
+meta_wayland_test_driver_init (MetaWaylandTestDriver *test_driver)
+{
+}
+
+MetaWaylandTestDriver *
+meta_wayland_test_driver_new (MetaWaylandCompositor *compositor)
+{
+  MetaWaylandTestDriver *test_driver;
+
+  test_driver = g_object_new (META_TYPE_WAYLAND_TEST_DRIVER, NULL);
+  test_driver->test_driver = wl_global_create (compositor->wayland_display,
+                                               &test_driver_interface,
+                                               1,
+                                               test_driver, bind_test_driver);
+  if (!test_driver->test_driver)
+    g_error ("Failed to register a global wl-subcompositor object");
+
+  return test_driver;
+}
diff --git a/src/tests/meta-wayland-test-driver.h b/src/tests/meta-wayland-test-driver.h
new file mode 100644
index 0000000000..ccbfe1312d
--- /dev/null
+++ b/src/tests/meta-wayland-test-driver.h
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef META_WAYLAND_TEST_DRIVER_H
+#define META_WAYLAND_TEST_DRIVER_H
+
+#include "wayland/meta-wayland.h"
+
+#define META_TYPE_WAYLAND_TEST_DRIVER (meta_wayland_test_driver_get_type ())
+G_DECLARE_FINAL_TYPE (MetaWaylandTestDriver, meta_wayland_test_driver,
+                      META, WAYLAND_TEST_DRIVER,
+                      GObject)
+
+MetaWaylandTestDriver * meta_wayland_test_driver_new (MetaWaylandCompositor *compositor);
+
+#endif /* META_WAYLAND_TEST_DRIVER_H */
diff --git a/src/tests/wayland-unit-tests.c b/src/tests/wayland-unit-tests.c
index 507c9dba4d..f8e6508d98 100644
--- a/src/tests/wayland-unit-tests.c
+++ b/src/tests/wayland-unit-tests.c
@@ -20,14 +20,10 @@
 #include "tests/wayland-unit-tests.h"
 
 #include <gio/gio.h>
-#include <wayland-server.h>
 
+#include "tests/meta-wayland-test-driver.h"
 #include "wayland/meta-wayland.h"
-#include "wayland/meta-wayland-actor-surface.h"
 #include "wayland/meta-wayland-surface.h"
-#include "wayland/meta-wayland-private.h"
-
-#include "test-driver-server-protocol.h"
 
 typedef struct _WaylandTestClient
 {
@@ -36,6 +32,8 @@ typedef struct _WaylandTestClient
   GMainLoop *main_loop;
 } WaylandTestClient;
 
+static MetaWaylandTestDriver *test_driver;
+
 static char *
 get_test_client_path (const char *test_client_name)
 {
@@ -155,57 +153,6 @@ subsurface_invalid_xdg_shell_actions (void)
   g_test_assert_expected_messages ();
 }
 
-static void
-on_actor_destroyed (ClutterActor       *actor,
-                    struct wl_resource *callback)
-{
-  wl_callback_send_done (callback, 0);
-  wl_resource_destroy (callback);
-}
-
-static void
-sync_actor_destroy (struct wl_client   *client,
-                    struct wl_resource *resource,
-                    uint32_t            id,
-                    struct wl_resource *surface_resource)
-{
-  MetaWaylandSurface *surface = wl_resource_get_user_data (surface_resource);
-  MetaWaylandActorSurface *actor_surface;
-  MetaSurfaceActor *actor;
-  struct wl_resource *callback;
-
-  g_assert_nonnull (surface);
-
-  actor_surface = (MetaWaylandActorSurface *) surface->role;
-  g_assert_nonnull (actor_surface);
-
-  actor = meta_wayland_actor_surface_get_actor (actor_surface);
-  g_assert_nonnull (actor);
-
-  callback = wl_resource_create (client, &wl_callback_interface, 1, id);
-
-  g_signal_connect (actor, "destroy", G_CALLBACK (on_actor_destroyed),
-                    callback);
-}
-
-static const struct test_driver_interface meta_test_driver_interface = {
-  sync_actor_destroy,
-};
-
-static void
-bind_test_driver (struct wl_client *client,
-                  void             *data,
-                  uint32_t          version,
-                  uint32_t          id)
-{
-  struct wl_resource *resource;
-
-  resource = wl_resource_create (client, &test_driver_interface,
-                                 version, id);
-  wl_resource_set_implementation (resource, &meta_test_driver_interface,
-                                  NULL, NULL);
-}
-
 void
 pre_run_wayland_tests (void)
 {
@@ -214,11 +161,7 @@ pre_run_wayland_tests (void)
   compositor = meta_wayland_compositor_get_default ();
   g_assert_nonnull (compositor);
 
-  if (wl_global_create (compositor->wayland_display,
-                        &test_driver_interface,
-                        1,
-                        NULL, bind_test_driver) == NULL)
-    g_error ("Failed to register a global wl-subcompositor object");
+  test_driver = meta_wayland_test_driver_new (compositor);
 }
 
 void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]