[mutter] tests/wayland: Test subsurface commits after parent was reset



commit 3e90070b88a3788f8397cb04257cb47b1d7f9642
Author: Jonas Ådahl <jadahl gmail com>
Date:   Fri Dec 6 19:05:32 2019 +0100

    tests/wayland: Test subsurface commits after parent was reset
    
    Without 'wayland/surface-actor: Reset and sync subsurface state when
    resetting' this test would fail.
    
    This also adds a simple framework for testing lower level Wayland
    semantics.
    
    In contrast to the test-client and test-driver framework, which uses
    gtk and tests mostly window management related things, this framework is
    aimed to run Wayland clients made to test a particular protocol flow,
    thus will likely consist of manual lower level Wayland mechanics.
    
    A private protocol is added in order to help out clients do things they
    cannot do by themself. The protocol currently only consists of a request
    meant to be used for getting a callback when the actor of a given
    surface is eventually destroyed. This is different from the wl_surface
    being destroyed due to window destroy animations taking an arbitrary
    amount of time. It'll be used by the first test added in the next
    commit.
    
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/961

 meson.build                                        |   1 +
 src/meson.build                                    |  22 +-
 src/tests/meson.build                              |   6 +
 src/tests/unit-tests.c                             |   3 +
 src/tests/wayland-test-clients/meson.build         |  61 ++++
 .../subsurface-remap-toplevel.c                    | 397 +++++++++++++++++++++
 src/tests/wayland-test-clients/test-driver.xml     |   9 +
 .../wayland-test-client-utils.c                    |  88 +++++
 .../wayland-test-client-utils.h                    |   8 +
 src/tests/wayland-unit-tests.c                     | 203 +++++++++++
 src/tests/wayland-unit-tests.h                     |  25 ++
 src/wayland/meta-wayland-actor-surface.h           |   3 +
 12 files changed, 824 insertions(+), 2 deletions(-)
---
diff --git a/meson.build b/meson.build
index 700a36029..e2c116ff7 100644
--- a/meson.build
+++ b/meson.build
@@ -164,6 +164,7 @@ endif
 have_wayland = get_option('wayland')
 if have_wayland
   wayland_server_dep = dependency('wayland-server', version: wayland_server_req)
+  wayland_client_dep = dependency('wayland-client', version: wayland_server_req)
   wayland_protocols_dep = dependency('wayland-protocols',
                                      version: wayland_protocols_req)
   wayland_egl_dep = dependency('wayland-egl')
diff --git a/src/meson.build b/src/meson.build
index fca40d8dc..012d78310 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -769,6 +769,10 @@ if have_remote_desktop
   mutter_built_sources += dbus_screen_cast_built_sources
 endif
 
+wayland_protocol_server_headers = []
+wayland_protocol_client_headers = []
+wayland_protocol_sources = []
+
 if have_wayland
   # Format:
   #  - protocol name
@@ -831,7 +835,7 @@ if have_wayland
                                                   output_base))
     endif
 
-    mutter_built_sources += custom_target('@0@ server header'.format(output_base),
+    wayland_protocol_server_headers += custom_target('@0@ server header'.format(output_base),
       input: input,
       output: '@0@-server-protocol.h'.format(output_base),
       command: [
@@ -841,7 +845,18 @@ if have_wayland
       ]
     )
 
-    mutter_built_sources += custom_target('@0@ source'.format(output_base),
+    # used by tests
+    wayland_protocol_client_headers += custom_target('@0@ client header'.format(output_base),
+      input: input,
+      output: '@0@-client-protocol.h'.format(output_base),
+      command: [
+        wayland_scanner,
+        'client-header',
+        '@INPUT@', '@OUTPUT@',
+      ]
+    )
+
+    wayland_protocol_sources += custom_target('@0@ source'.format(output_base),
       input: input,
       output: '@0@-protocol.c'.format(output_base),
       command: [
@@ -853,6 +868,9 @@ if have_wayland
   endforeach
 endif
 
+mutter_built_sources += wayland_protocol_server_headers
+mutter_built_sources += wayland_protocol_sources
+
 subdir('meta')
 
 mutter_built_sources += mutter_enum_types
diff --git a/src/tests/meson.build b/src/tests/meson.build
index f5a132f6b..a9a5a4ff4 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -17,6 +17,8 @@ tests_deps = [
   libmutter_clutter_dep,
 ]
 
+subdir('wayland-test-clients')
+
 if have_installed_tests
   stacking_files_datadir = join_paths(pkgdatadir, 'tests')
 
@@ -88,6 +90,10 @@ unit_tests = executable('mutter-test-unit-tests',
     'monitor-test-utils.h',
     'monitor-unit-tests.c',
     'monitor-unit-tests.h',
+    'wayland-unit-tests.c',
+    'wayland-unit-tests.h',
+    test_driver_server_header,
+    test_driver_protocol_code,
   ],
   include_directories: tests_includepath,
   c_args: tests_c_args,
diff --git a/src/tests/unit-tests.c b/src/tests/unit-tests.c
index e3873c4eb..53033bdca 100644
--- a/src/tests/unit-tests.c
+++ b/src/tests/unit-tests.c
@@ -34,6 +34,7 @@
 #include "tests/monitor-unit-tests.h"
 #include "tests/monitor-store-unit-tests.h"
 #include "tests/test-utils.h"
+#include "tests/wayland-unit-tests.h"
 #include "wayland/meta-wayland.h"
 
 typedef struct _MetaTestLaterOrderCallbackData
@@ -228,6 +229,7 @@ run_tests (gpointer data)
     META_EXPERIMENTAL_FEATURE_SCALE_MONITOR_FRAMEBUFFER);
 
   pre_run_monitor_tests ();
+  pre_run_wayland_tests ();
 
   ret = g_test_run ();
 
@@ -251,6 +253,7 @@ init_tests (int argc, char **argv)
   init_monitor_config_migration_tests ();
   init_monitor_tests ();
   init_boxes_tests ();
+  init_wayland_tests ();
 }
 
 int
diff --git a/src/tests/wayland-test-clients/meson.build b/src/tests/wayland-test-clients/meson.build
new file mode 100644
index 000000000..3d795dd0e
--- /dev/null
+++ b/src/tests/wayland-test-clients/meson.build
@@ -0,0 +1,61 @@
+wayland_test_client_installed_tests_libexecdir = join_paths(
+  mutter_installed_tests_libexecdir,
+  'wayland-test-clients',
+)
+
+test_driver_server_header = custom_target(
+  'test-driver server header',
+  input: 'test-driver.xml',
+  output: 'test-driver-server-protocol.h',
+  command: [
+    wayland_scanner,
+    'server-header',
+    '@INPUT@', '@OUTPUT@',
+  ]
+)
+
+test_driver_client_header = custom_target(
+  'test-driver client header',
+  input: 'test-driver.xml',
+  output: 'test-driver-client-protocol.h',
+  command: [
+    wayland_scanner,
+    'client-header',
+    '@INPUT@', '@OUTPUT@',
+  ]
+)
+
+test_driver_protocol_code = custom_target(
+  'test-driver source',
+  input: 'test-driver.xml',
+  output: 'test-driver-protocol.c',
+  command: [
+    wayland_scanner,
+    'private-code',
+    '@INPUT@', '@OUTPUT@',
+  ]
+)
+
+common_sources = [
+  'wayland-test-client-utils.c',
+  'wayland-test-client-utils.h',
+  wayland_protocol_client_headers,
+  wayland_protocol_sources,
+  test_driver_client_header,
+  test_driver_protocol_code,
+]
+
+executable('subsurface-remap-toplevel',
+  sources: [
+    'subsurface-remap-toplevel.c',
+    common_sources,
+  ],
+  include_directories: tests_includepath,
+  c_args: tests_c_args,
+  dependencies: [
+    glib_dep,
+    wayland_client_dep,
+  ],
+  install: have_installed_tests,
+  install_dir: wayland_test_client_installed_tests_libexecdir,
+)
diff --git a/src/tests/wayland-test-clients/subsurface-remap-toplevel.c 
b/src/tests/wayland-test-clients/subsurface-remap-toplevel.c
new file mode 100644
index 000000000..25ff3a2d1
--- /dev/null
+++ b/src/tests/wayland-test-clients/subsurface-remap-toplevel.c
@@ -0,0 +1,397 @@
+/*
+ * 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 <glib.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#include <wayland-client.h>
+
+#include "wayland-test-client-utils.h"
+
+#include "test-driver-client-protocol.h"
+#include "xdg-shell-client-protocol.h"
+
+typedef enum _State
+{
+  STATE_INIT = 0,
+  STATE_WAIT_FOR_CONFIGURE_1,
+  STATE_WAIT_FOR_FRAME_1,
+  STATE_WAIT_FOR_ACTOR_DESTROYED,
+  STATE_WAIT_FOR_CONFIGURE_2,
+  STATE_WAIT_FOR_FRAME_2
+} State;
+
+static struct wl_display *display;
+static struct wl_registry *registry;
+static struct wl_compositor *compositor;
+static struct wl_subcompositor *subcompositor;
+static struct xdg_wm_base *xdg_wm_base;
+static struct wl_shm *shm;
+static struct test_driver *test_driver;
+
+static struct wl_surface *surface;
+static struct xdg_surface *xdg_surface;
+static struct xdg_toplevel *xdg_toplevel;
+
+static struct wl_surface *subsurface_surface;
+static struct wl_subsurface *subsurface;
+
+static struct wl_callback *frame_callback;
+
+static gboolean running;
+
+static State state;
+
+static void
+init_surface (void)
+{
+  xdg_toplevel_set_title (xdg_toplevel, "gradient-test");
+  wl_surface_commit (surface);
+}
+
+static void
+actor_destroyed (void               *data,
+                 struct wl_callback *callback,
+                 uint32_t            serial)
+{
+  g_assert_cmpint (state, ==, STATE_WAIT_FOR_ACTOR_DESTROYED);
+
+  init_surface ();
+  state = STATE_WAIT_FOR_CONFIGURE_2;
+
+  wl_callback_destroy (callback);
+}
+
+static const struct wl_callback_listener actor_destroy_listener = {
+  actor_destroyed,
+};
+
+static void
+reset_surface (void)
+{
+  struct wl_callback *callback;
+
+  callback = test_driver_sync_actor_destroyed (test_driver, surface);
+  wl_callback_add_listener (callback, &actor_destroy_listener, NULL);
+
+  wl_surface_attach (surface, NULL, 0, 0);
+  wl_surface_commit (surface);
+
+  state = STATE_WAIT_FOR_ACTOR_DESTROYED;
+}
+
+static void
+handle_buffer_release (void             *data,
+                       struct wl_buffer *buffer)
+{
+  wl_buffer_destroy (buffer);
+}
+
+static const struct wl_buffer_listener buffer_listener = {
+  handle_buffer_release
+};
+
+static gboolean
+create_shm_buffer (int                width,
+                   int                height,
+                   struct wl_buffer **out_buffer,
+                   void             **out_data,
+                   int               *out_size)
+{
+  struct wl_shm_pool *pool;
+  static struct wl_buffer *buffer;
+  int fd, size, stride;
+  int bytes_per_pixel;
+  void *data;
+
+  bytes_per_pixel = 4;
+  stride = width * bytes_per_pixel;
+  size = stride * height;
+
+  fd = create_anonymous_file (size);
+  if (fd < 0)
+    {
+      fprintf (stderr, "Creating a buffer file for %d B failed: %m\n",
+               size);
+      return FALSE;
+    }
+
+  data = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+  if (data == MAP_FAILED)
+    {
+      fprintf (stderr, "mmap failed: %m\n");
+      close (fd);
+      return FALSE;
+    }
+
+  pool = wl_shm_create_pool (shm, fd, size);
+  buffer = wl_shm_pool_create_buffer (pool, 0,
+                                      width, height,
+                                      stride,
+                                      WL_SHM_FORMAT_ARGB8888);
+  wl_buffer_add_listener (buffer, &buffer_listener, buffer);
+  wl_shm_pool_destroy (pool);
+  close (fd);
+
+  *out_buffer = buffer;
+  *out_data = data;
+  *out_size = size;
+
+  return TRUE;
+}
+
+static void
+fill (void    *buffer_data,
+      int      width,
+      int      height,
+      uint32_t color)
+{
+  uint32_t *pixels = buffer_data;
+  int x, y;
+
+  for (y = 0; y < height; y++)
+    {
+      for (x = 0; x < width; x++)
+        pixels[y * width + x] = color;
+    }
+}
+
+static void
+draw (struct wl_surface *surface,
+      int                width,
+      int                height,
+      uint32_t           color)
+{
+  struct wl_buffer *buffer;
+  void *buffer_data;
+  int size;
+
+  if (!create_shm_buffer (width, height,
+                          &buffer, &buffer_data, &size))
+    g_error ("Failed to create shm buffer");
+
+  fill (buffer_data, width, height, color);
+
+  wl_surface_attach (surface, buffer, 0, 0);
+}
+
+static void
+draw_main (void)
+{
+  draw (surface, 700, 500, 0xff00ff00);
+}
+
+static void
+draw_subsurface (void)
+{
+  draw (subsurface_surface, 500, 300, 0xff007f00);
+}
+
+static void
+handle_xdg_toplevel_configure (void                *data,
+                               struct xdg_toplevel *xdg_toplevel,
+                               int32_t              width,
+                               int32_t              height,
+                               struct wl_array     *state)
+{
+}
+
+static void
+handle_xdg_toplevel_close(void                *data,
+                          struct xdg_toplevel *xdg_toplevel)
+{
+  g_assert_not_reached ();
+}
+
+static const struct xdg_toplevel_listener xdg_toplevel_listener = {
+  handle_xdg_toplevel_configure,
+  handle_xdg_toplevel_close,
+};
+
+static void
+handle_frame_callback (void               *data,
+                       struct wl_callback *callback,
+                       uint32_t            time)
+{
+  switch (state)
+    {
+    case STATE_WAIT_FOR_FRAME_1:
+      reset_surface ();
+      break;
+    case STATE_WAIT_FOR_FRAME_2:
+      exit (EXIT_SUCCESS);
+    case STATE_INIT:
+      g_assert_not_reached ();
+    case STATE_WAIT_FOR_CONFIGURE_1:
+      g_assert_not_reached ();
+    case STATE_WAIT_FOR_ACTOR_DESTROYED:
+      g_assert_not_reached ();
+    case STATE_WAIT_FOR_CONFIGURE_2:
+      g_assert_not_reached ();
+    }
+}
+
+static const struct wl_callback_listener frame_listener = {
+  handle_frame_callback,
+};
+
+static void
+handle_xdg_surface_configure (void               *data,
+                              struct xdg_surface *xdg_surface,
+                              uint32_t            serial)
+{
+  switch (state)
+    {
+    case STATE_INIT:
+      g_assert_not_reached ();
+    case STATE_WAIT_FOR_CONFIGURE_1:
+      draw_main ();
+      state = STATE_WAIT_FOR_FRAME_1;
+      break;
+    case STATE_WAIT_FOR_CONFIGURE_2:
+      draw_main ();
+      state = STATE_WAIT_FOR_FRAME_2;
+      break;
+    case STATE_WAIT_FOR_ACTOR_DESTROYED:
+      g_assert_not_reached ();
+    case STATE_WAIT_FOR_FRAME_1:
+    case STATE_WAIT_FOR_FRAME_2:
+      /* ignore */
+      return;
+    }
+
+  xdg_surface_ack_configure (xdg_surface, serial);
+  frame_callback = wl_surface_frame (surface);
+  wl_callback_add_listener (frame_callback, &frame_listener, NULL);
+  wl_surface_commit (surface);
+  wl_display_flush (display);
+}
+
+static const struct xdg_surface_listener xdg_surface_listener = {
+  handle_xdg_surface_configure,
+};
+
+static void
+handle_xdg_wm_base_ping (void               *data,
+                         struct xdg_wm_base *xdg_wm_base,
+                         uint32_t            serial)
+{
+  xdg_wm_base_pong (xdg_wm_base, serial);
+}
+
+static const struct xdg_wm_base_listener xdg_wm_base_listener = {
+  handle_xdg_wm_base_ping,
+};
+
+static void
+handle_registry_global (void               *data,
+                        struct wl_registry *registry,
+                        uint32_t            id,
+                        const char         *interface,
+                        uint32_t            version)
+{
+  if (strcmp (interface, "wl_compositor") == 0)
+    {
+      compositor = wl_registry_bind (registry, id, &wl_compositor_interface, 1);
+    }
+  else if (strcmp (interface, "wl_subcompositor") == 0)
+    {
+      subcompositor = wl_registry_bind (registry,
+                                        id, &wl_subcompositor_interface, 1);
+    }
+  else if (strcmp (interface, "xdg_wm_base") == 0)
+    {
+      xdg_wm_base = wl_registry_bind (registry, id,
+                                      &xdg_wm_base_interface, 1);
+      xdg_wm_base_add_listener (xdg_wm_base, &xdg_wm_base_listener, NULL);
+    }
+  else if (strcmp (interface, "wl_shm") == 0)
+    {
+      shm = wl_registry_bind (registry,
+                              id, &wl_shm_interface, 1);
+    }
+  else if (strcmp (interface, "test_driver") == 0)
+    {
+      test_driver = wl_registry_bind (registry, id, &test_driver_interface, 1);
+    }
+}
+
+static void
+handle_registry_global_remove (void               *data,
+                               struct wl_registry *registry,
+                               uint32_t            name)
+{
+}
+
+static const struct wl_registry_listener registry_listener = {
+  handle_registry_global,
+  handle_registry_global_remove
+};
+
+int
+main (int    argc,
+      char **argv)
+{
+  display = wl_display_connect (NULL);
+  registry = wl_display_get_registry (display);
+  wl_registry_add_listener (registry, &registry_listener, NULL);
+  wl_display_roundtrip (display);
+
+  if (!shm)
+    {
+      fprintf (stderr, "No wl_shm global\n");
+      return EXIT_FAILURE;
+    }
+
+  if (!xdg_wm_base)
+    {
+      fprintf (stderr, "No xdg_wm_base global\n");
+      return EXIT_FAILURE;
+    }
+
+  wl_display_roundtrip (display);
+
+  g_assert_nonnull (test_driver);
+
+  surface = wl_compositor_create_surface (compositor);
+  xdg_surface = xdg_wm_base_get_xdg_surface (xdg_wm_base, surface);
+  xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, NULL);
+  xdg_toplevel = xdg_surface_get_toplevel (xdg_surface);
+  xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, NULL);
+
+  subsurface_surface = wl_compositor_create_surface (compositor);
+  subsurface = wl_subcompositor_get_subsurface (subcompositor,
+                                                subsurface_surface,
+                                                surface);
+  wl_subsurface_set_position (subsurface, 100, 100);
+  draw_subsurface ();
+  wl_surface_commit (subsurface_surface);
+
+  init_surface ();
+  state = STATE_WAIT_FOR_CONFIGURE_1;
+
+  running = TRUE;
+  while (running)
+    {
+      if (wl_display_dispatch (display) == -1)
+        return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}
diff --git a/src/tests/wayland-test-clients/test-driver.xml b/src/tests/wayland-test-clients/test-driver.xml
new file mode 100644
index 000000000..dd5f52145
--- /dev/null
+++ b/src/tests/wayland-test-clients/test-driver.xml
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<protocol name="test_driver">
+  <interface name="test_driver" version="1">
+    <request name="sync_actor_destroyed">
+      <arg name="callback" type="new_id" interface="wl_callback"/>
+      <arg name="surface" type="object" interface="wl_surface"/>
+    </request>
+  </interface>
+</protocol>
diff --git a/src/tests/wayland-test-clients/wayland-test-client-utils.c 
b/src/tests/wayland-test-clients/wayland-test-client-utils.c
new file mode 100644
index 000000000..9deb122cb
--- /dev/null
+++ b/src/tests/wayland-test-clients/wayland-test-client-utils.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright © 2012 Collabora, Ltd.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial
+ * portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "wayland-test-client-utils.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static int
+create_tmpfile_cloexec (char *tmpname)
+{
+  int fd;
+
+  fd = mkostemp (tmpname, O_CLOEXEC);
+  if (fd >= 0)
+    unlink (tmpname);
+
+  return fd;
+}
+
+int
+create_anonymous_file (off_t size)
+{
+  static const char template[] = "/wayland-test-client-shared-XXXXXX";
+  const char *path;
+  char *name;
+  int fd;
+  int ret;
+
+  path = getenv ("XDG_RUNTIME_DIR");
+  if (!path)
+    {
+      errno = ENOENT;
+      return -1;
+    }
+
+  name = malloc (strlen (path) + sizeof (template));
+  if (!name)
+    return -1;
+
+  strcpy (name, path);
+  strcat (name, template);
+
+  fd = create_tmpfile_cloexec (name);
+
+  free (name);
+
+  if (fd < 0)
+    return -1;
+
+  do
+    ret = posix_fallocate (fd, 0, size);
+  while (ret == EINTR);
+
+  if (ret != 0)
+    {
+      close (fd);
+      errno = ret;
+      return -1;
+    }
+
+  return fd;
+}
diff --git a/src/tests/wayland-test-clients/wayland-test-client-utils.h 
b/src/tests/wayland-test-clients/wayland-test-client-utils.h
new file mode 100644
index 000000000..a8dcb5321
--- /dev/null
+++ b/src/tests/wayland-test-clients/wayland-test-client-utils.h
@@ -0,0 +1,8 @@
+#ifndef WAYLAND_TEST_CLIENT_UTILS_H
+#define WAYLAND_TEST_CLIENT_UTILS_H
+
+#include <stdio.h>
+
+int create_anonymous_file (off_t size);
+
+#endif /* WAYLAND_TEST_CLIENT_UTILS_H */
diff --git a/src/tests/wayland-unit-tests.c b/src/tests/wayland-unit-tests.c
new file mode 100644
index 000000000..30c7c7c24
--- /dev/null
+++ b/src/tests/wayland-unit-tests.c
@@ -0,0 +1,203 @@
+/*
+ * 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/wayland-unit-tests.h"
+
+#include <gio/gio.h>
+#include <wayland-server.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
+{
+  GSubprocess *subprocess;
+  char *path;
+  GMainLoop *main_loop;
+} WaylandTestClient;
+
+static char *
+get_test_client_path (const char *test_client_name)
+{
+  return g_test_build_filename (G_TEST_BUILT,
+                                "src",
+                                "tests",
+                                "wayland-test-clients",
+                                test_client_name,
+                                NULL);
+}
+
+static WaylandTestClient *
+wayland_test_client_new (const char *test_client_name)
+{
+  MetaWaylandCompositor *compositor;
+  const char *wayland_display_name;
+  g_autofree char *test_client_path = NULL;
+  g_autoptr (GSubprocessLauncher) launcher = NULL;
+  GSubprocess *subprocess;
+  GError *error = NULL;
+  WaylandTestClient *wayland_test_client;
+
+  compositor = meta_wayland_compositor_get_default ();
+  wayland_display_name = meta_wayland_get_wayland_display_name (compositor);
+  test_client_path = get_test_client_path (test_client_name);
+
+  launcher =  g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
+  g_subprocess_launcher_setenv (launcher,
+                                "WAYLAND_DISPLAY", wayland_display_name,
+                                TRUE);
+
+  subprocess = g_subprocess_launcher_spawn (launcher,
+                                            &error,
+                                            test_client_path,
+                                            NULL);
+  if (!subprocess)
+    {
+      g_error ("Failed to launch Wayland test client '%s': %s",
+               test_client_path, error->message);
+    }
+
+  wayland_test_client = g_new0 (WaylandTestClient, 1);
+  wayland_test_client->subprocess = subprocess;
+  wayland_test_client->path = g_strdup (test_client_name);
+  wayland_test_client->main_loop = g_main_loop_new (NULL, FALSE);
+
+  return wayland_test_client;
+}
+
+static void
+wayland_test_client_finished (GObject      *source_object,
+                              GAsyncResult *res,
+                              gpointer      user_data)
+{
+  WaylandTestClient *wayland_test_client = user_data;
+  GError *error = NULL;
+
+  if (!g_subprocess_wait_finish (wayland_test_client->subprocess,
+                                 res,
+                                 &error))
+    {
+      g_error ("Failed to wait for Wayland test client '%s': %s",
+               wayland_test_client->path, error->message);
+    }
+
+  g_main_loop_quit (wayland_test_client->main_loop);
+}
+
+static void
+wayland_test_client_finish (WaylandTestClient *wayland_test_client)
+{
+  g_subprocess_wait_async (wayland_test_client->subprocess, NULL,
+                           wayland_test_client_finished, wayland_test_client);
+
+  g_main_loop_run (wayland_test_client->main_loop);
+
+  g_assert_true (g_subprocess_get_successful (wayland_test_client->subprocess));
+
+  g_main_loop_unref (wayland_test_client->main_loop);
+  g_free (wayland_test_client->path);
+  g_object_unref (wayland_test_client->subprocess);
+  g_free (wayland_test_client);
+}
+
+static void
+subsurface_remap_toplevel (void)
+{
+  WaylandTestClient *wayland_test_client;
+
+  wayland_test_client = wayland_test_client_new ("subsurface-remap-toplevel");
+  wayland_test_client_finish (wayland_test_client);
+}
+
+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)
+{
+  MetaWaylandCompositor *compositor;
+
+  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");
+}
+
+void
+init_wayland_tests (void)
+{
+  g_test_add_func ("/wayland/subsurface/remap-toplevel",
+                   subsurface_remap_toplevel);
+}
diff --git a/src/tests/wayland-unit-tests.h b/src/tests/wayland-unit-tests.h
new file mode 100644
index 000000000..b7d8d4204
--- /dev/null
+++ b/src/tests/wayland-unit-tests.h
@@ -0,0 +1,25 @@
+/*
+ * 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 WAYLAND_UNIT_TESTS_H
+#define WAYLAND_UNIT_TESTS_H
+
+void init_wayland_tests (void);
+
+void pre_run_wayland_tests (void);
+
+#endif /* WAYLAND_UNIT_TESTS_H */
diff --git a/src/wayland/meta-wayland-actor-surface.h b/src/wayland/meta-wayland-actor-surface.h
index dc315ce02..caa3cbe31 100644
--- a/src/wayland/meta-wayland-actor-surface.h
+++ b/src/wayland/meta-wayland-actor-surface.h
@@ -39,7 +39,10 @@ struct _MetaWaylandActorSurfaceClass
 
 void meta_wayland_actor_surface_sync_actor_state (MetaWaylandActorSurface *actor_surface);
 double meta_wayland_actor_surface_get_geometry_scale (MetaWaylandActorSurface *actor_surface);
+
+META_EXPORT_TEST
 MetaSurfaceActor * meta_wayland_actor_surface_get_actor (MetaWaylandActorSurface *actor_surface);
+
 void meta_wayland_actor_surface_reset_actor (MetaWaylandActorSurface *actor_surface);
 
 void meta_wayland_actor_surface_queue_frame_callbacks (MetaWaylandActorSurface *actor_surface,


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