Skip to content
Snippets Groups Projects
Commit 640a3c71 authored by Marius Vlad's avatar Marius Vlad Committed by Daniel Stone
Browse files

xcb-client-helper: Add a XCB client helper for tests


This patch introduces a small library wrapper around XCB to be used in
Xwayland tests.

It's being designed such that we do not advance without accounting for
all X11 events when changing the window state.  It adds a fence that
waits for all events to be processed, and only after all the events have
been accounted for, to proceed further, resuming execution of the
tests.

This works by keeping a tentative_state list for the client and a
window state that gets applied when the event we waited for has been
received.

This is useful in test clients, which could verify at the end after
receiving all events that the correct state has been applied. Acts as a
way to verify that the we never get or have a different state than the
one we expect.

With it, this converts test-xwayland to using libxcb (together with
xcb-cursor-dev) rather than using Xlib, and with it it removes any Xlib
dependency we might have in the tests.

This only adds support for map/unmap/create/destroy/property notify.
A follow-up would be to expand this library to track window movement
and resizing.

Signed-off-by: default avatarMarius Vlad <marius.vlad@collabora.com>
parent 87881e2c
No related branches found
No related tags found
1 merge request!946xcb-client-helper: Add a XCB client helper for tests
Pipeline #820919 passed
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
variables: variables:
FDO_UPSTREAM_REPO: wayland/weston FDO_UPSTREAM_REPO: wayland/weston
FDO_REPO_SUFFIX: "$BUILD_OS/$BUILD_ARCH" FDO_REPO_SUFFIX: "$BUILD_OS/$BUILD_ARCH"
FDO_DISTRIBUTION_TAG: '2023-02-21-bump-meson-to-1.0.0' FDO_DISTRIBUTION_TAG: '2023-02-28-libxcb-migration'
include: include:
......
...@@ -95,6 +95,7 @@ apt-get -y --no-install-recommends install \ ...@@ -95,6 +95,7 @@ apt-get -y --no-install-recommends install \
libxcb-xfixes0-dev \ libxcb-xfixes0-dev \
libxcb-xkb-dev \ libxcb-xkb-dev \
libxcursor-dev \ libxcursor-dev \
libxcb-cursor-dev \
libxdamage-dev \ libxdamage-dev \
libxext-dev \ libxext-dev \
libxfixes-dev \ libxfixes-dev \
......
...@@ -309,14 +309,33 @@ tests_standalone = [ ...@@ -309,14 +309,33 @@ tests_standalone = [
] ]
if get_option('xwayland') if get_option('xwayland')
d = dependency('x11', required: false) xcb_dep = dependency('xcb', required: false)
if not d.found() xcb_cursor_dep = dependency('xcb-cursor', required: false)
error('Xwayland tests require libX11 which was not found. Or, you can use \'-Dxwayland=false\'.')
if not xcb_dep.found() or not xcb_cursor_dep.found()
error('xcb and xcb-cursor required for running xwayland tests')
endif endif
tests += {
libxwayland_test_client = static_library(
'test-xwayland-client',
[ 'xcb-client-helper.c', weston_test_client_protocol_h ],
include_directories: common_inc,
dependencies: [
dep_pixman, dep_xcb_xwayland,
xcb_dep, xcb_cursor_dep
],
install: false,
)
dep_libxwayland_test = declare_dependency(
dependencies: [ xcb_dep, xcb_cursor_dep ],
link_with: libxwayland_test_client,
)
tests += [ {
'name': 'xwayland', 'name': 'xwayland',
'dep_objs': d, 'dep_objs': dep_libxwayland_test,
} } ]
endif endif
# Manual test plugin, not used in the automatic suite # Manual test plugin, not used in the automatic suite
......
This diff is collapsed.
/*
* Copyright 2022 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.
*/
#pragma once
#include "config.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <time.h>
#include "shared/xcb-xwayland.h"
#include <pixman.h>
#include <wayland-client.h>
#include <xcb/xcb.h>
#include <xcb/xcb_cursor.h>
enum w_state {
CREATED = 1 << 0,
MAPPED = 1 << 1,
UNMAPPED = 1 << 2,
PROPERTY_NAME = 1 << 3,
DESTROYED = 1 << 4,
EXPOSE = 1 << 5,
REPARENT = 1 << 6,
};
struct window_state {
uint8_t event;
enum w_state pending_state;
xcb_drawable_t wid;
struct wl_list link; /** window_x11.tentative_state::pending_events_list */
};
struct connection_x11 {
struct atom_x11 *atoms;
struct xcb_connection_t *connection;
};
struct window_x11 {
struct window_x11 *parent;
struct xcb_screen_t *screen;
struct connection_x11 *conn;
bool handle_in_progress;
xcb_drawable_t root_win_id; /* screen root */
xcb_drawable_t win_id; /* this window */
xcb_drawable_t parent_win_id; /* the parent, if set */
xcb_gcontext_t background;
xcb_cursor_context_t *ctx;
xcb_cursor_t cursor;
int width;
int height;
int pos_x;
int pos_y;
pixman_color_t bg_color;
/* these track what the X11 client does */
struct {
/* pending queue events */
struct wl_list pending_events_list; /** window_state::link */
} tentative_state;
/* these track what we got back from the server */
struct {
/* applied, received event */
uint32_t win_state;
} state;
struct wl_list window_list;
struct wl_list window_link;
xcb_window_t frame_id;
};
void
window_x11_map(struct window_x11 *window);
void
window_x11_unmap(struct window_x11 *window);
struct connection_x11 *
create_x11_connection(void);
void
destroy_x11_connection(struct connection_x11 *conn);
struct window_x11 *
create_x11_window(int width, int height, int pos_x, int pos_y, struct connection_x11 *conn,
pixman_color_t bg_color, struct window_x11 *parent);
void
destroy_x11_window(struct window_x11 *window);
void
window_x11_set_win_name(struct window_x11 *window, const char *name);
xcb_get_property_reply_t *
window_x11_dump_prop(struct window_x11 *window, xcb_drawable_t win, xcb_atom_t atom);
void
handle_event_set_pending(struct window_x11 *window, uint8_t event,
enum w_state pending_state, xcb_drawable_t wid);
void
handle_event_remove_pending(struct window_state *wstate);
int
handle_events_x11(struct window_x11 *window);
struct atom_x11 *
window_get_atoms(struct window_x11 *win);
struct xcb_connection_t *
window_get_connection(struct window_x11 *win);
void
window_x11_notify_for_root_events(struct window_x11 *window);
/* note that the flag is already bitshiftted */
static inline bool
window_state_has_flag(struct window_x11 *win, enum w_state flag)
{
return (win->state.win_state & flag) == flag;
}
static inline void
window_state_set_flag(struct window_x11 *win, enum w_state flag)
{
win->state.win_state |= flag;
}
static inline void
window_state_clear_flag(struct window_x11 *win, enum w_state flag)
{
win->state.win_state &= ~flag;
}
static inline void
window_state_clear_all_flags(struct window_x11 *win)
{
win->state.win_state = 0;
}
/**
* A wrapper over handle_events_x11() to check if the pending flag has been
* set, that waits for events calling handle_events_x11() and that verifies
* afterwards if the flag has indeed been applied.
*/
static inline void
handle_events_and_check_flags(struct window_x11 *win, enum w_state flag)
{
struct wl_list *pending_events =
&win->tentative_state.pending_events_list;
struct window_state *wstate;
bool found_pending_flag = false;
wl_list_for_each(wstate, pending_events, link) {
if ((wstate->pending_state & flag) == flag)
found_pending_flag = true;
}
assert(found_pending_flag);
handle_events_x11(win);
assert(window_state_has_flag(win, flag));
}
/* /*
* Copyright © 2015 Samsung Electronics Co., Ltd * Copyright © 2015 Samsung Electronics Co., Ltd
* Copyright 2022 Collabora, Ltd.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
...@@ -27,8 +28,10 @@ ...@@ -27,8 +28,10 @@
* *
* This is done in steps: * This is done in steps:
* 1) Confirm that the WL_SURFACE_ID atom exists * 1) Confirm that the WL_SURFACE_ID atom exists
* 2) Confirm that the window manager's name is "Weston WM" * 2) Confirm that our window name is "Xwayland Test Window"
* 3) Make sure we can map a window * 3) Confirm that there's conforming Window Manager
* 4) Confirm that the window manager's name is "Weston WM"
* 5) Make sure we can map a window
*/ */
#include "config.h" #include "config.h"
...@@ -37,12 +40,13 @@ ...@@ -37,12 +40,13 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <string.h> #include <string.h>
#include "weston-test-runner.h" #include "weston-test-runner.h"
#include "weston-test-fixture-compositor.h" #include "weston-test-fixture-compositor.h"
#include "shared/string-helpers.h"
#include "weston-test-client-helper.h"
#include "xcb-client-helper.h"
static enum test_result_code static enum test_result_code
fixture_setup(struct weston_test_harness *harness) fixture_setup(struct weston_test_harness *harness)
...@@ -52,68 +56,120 @@ fixture_setup(struct weston_test_harness *harness) ...@@ -52,68 +56,120 @@ fixture_setup(struct weston_test_harness *harness)
compositor_setup_defaults(&setup); compositor_setup_defaults(&setup);
setup.shell = SHELL_TEST_DESKTOP; setup.shell = SHELL_TEST_DESKTOP;
setup.xwayland = true; setup.xwayland = true;
/* setup.logging_scopes = "xwm-wm-x11"; */
return weston_test_harness_execute_as_client(harness, &setup); return weston_test_harness_execute_as_client(harness, &setup);
} }
DECLARE_FIXTURE_SETUP(fixture_setup); DECLARE_FIXTURE_SETUP(fixture_setup);
static char *
get_x11_window_name(struct window_x11 *window, xcb_drawable_t win)
{
xcb_get_property_reply_t *reply;
int reply_len;
char *name;
struct atom_x11 *atoms = window_get_atoms(window);
reply = window_x11_dump_prop(window, win, atoms->net_wm_name);
assert(reply);
assert(reply->type == atoms->string ||
reply->type == atoms->utf8_string);
reply_len = xcb_get_property_value_length(reply);
assert(reply_len > 0);
str_printf(&name, "%.*s", reply_len,
(char *) xcb_get_property_value(reply));
free(reply);
return name;
}
static char *
get_wm_name(struct window_x11 *window)
{
xcb_get_property_reply_t *reply;
xcb_generic_error_t *error;
xcb_get_property_cookie_t prop_cookie;
char *wm_name = NULL;
struct atom_x11 *atoms = window_get_atoms(window);
struct xcb_connection_t *conn = window_get_connection(window);
prop_cookie = xcb_get_property(conn, 0, window->root_win_id,
atoms->net_supporting_wm_check,
XCB_ATOM_WINDOW, 0, 1024);
reply = xcb_get_property_reply(conn, prop_cookie, &error);
assert(reply);
assert(reply->type == XCB_ATOM_WINDOW);
assert(reply->format == 32);
xcb_window_t wm_id = *(xcb_window_t *) xcb_get_property_value(reply);
wm_name = get_x11_window_name(window, wm_id);
free(error);
return wm_name;
}
TEST(xwayland_client_test) TEST(xwayland_client_test)
{ {
Display *display; struct window_x11 *window;
Window window, root, *support; struct connection_x11 *conn;
XEvent event; xcb_get_property_reply_t *reply;
int screen, status, actual_format; char *win_name;
unsigned long nitems, bytes;
Atom atom, type_atom, actual_type;
char *wm_name; char *wm_name;
pixman_color_t bg_color;
if (access(XSERVER_PATH, X_OK) != 0) struct atom_x11 *atoms;
exit(77);
color_rgb888(&bg_color, 255, 0, 0);
display = XOpenDisplay(NULL);
if (!display) conn = create_x11_connection();
exit(EXIT_FAILURE); assert(conn);
window = create_x11_window(100, 100, 100, 100, conn, bg_color, NULL);
atom = XInternAtom(display, "WL_SURFACE_ID", True); assert(window);
assert(atom != None);
window_x11_set_win_name(window, "Xwayland Test Window");
atom = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", True); handle_events_and_check_flags(window, PROPERTY_NAME);
assert(atom != None);
screen = DefaultScreen(display); /* The Window Manager MUST set _NET_SUPPORTING_WM_CHECK on the root
root = RootWindow(display, screen); * window to be the ID of a child window created by himself, to
* indicate that a compliant window manager is active.
status = XGetWindowProperty(display, root, atom, 0L, ~0L, *
False, XA_WINDOW, &actual_type, * The child window MUST also have the _NET_SUPPORTING_WM_CHECK
&actual_format, &nitems, &bytes, * property set to the ID of the child window. The child window MUST
(void *)&support); * also have the _NET_WM_NAME property set to the name of the Window
assert(status == Success); * Manager.
*
atom = XInternAtom(display, "_NET_WM_NAME", True); * See Extended Window Manager Hints,
assert(atom != None); * https://specifications.freedesktop.org/wm-spec/latest/ar01s03.html,
type_atom = XInternAtom(display, "UTF8_STRING", True); * _NET_SUPPORTING_WM_CHECK
assert(atom != None); * */
status = XGetWindowProperty(display, *support, atom, 0L, BUFSIZ, atoms = window_get_atoms(window);
False, type_atom, &actual_type, assert(atoms->net_supporting_wm_check != XCB_ATOM_NONE);
&actual_format, &nitems, &bytes, assert(atoms->wl_surface_id != XCB_ATOM_NONE);
(void *)&wm_name); assert(atoms->net_wm_name != XCB_ATOM_NONE);
assert(status == Success); assert(atoms->utf8_string != XCB_ATOM_NONE);
assert(nitems);
assert(strcmp("Weston WM", wm_name) == 0); reply = window_x11_dump_prop(window, window->root_win_id,
free(support); atoms->net_supporting_wm_check);
assert(reply);
assert(reply->type == XCB_ATOM_WINDOW);
free(reply);
window_x11_map(window);
handle_events_and_check_flags(window, MAPPED);
win_name = get_x11_window_name(window, window->win_id);
assert(strcmp(win_name, "Xwayland Test Window") == 0);
free(win_name);
wm_name = get_wm_name(window);
assert(wm_name);
assert(strcmp(wm_name, "Weston WM") == 0);
free(wm_name); free(wm_name);
window = XCreateSimpleWindow(display, root, 100, 100, 300, 300, 1, window_x11_unmap(window);
BlackPixel(display, screen), handle_events_and_check_flags(window, UNMAPPED);
WhitePixel(display, screen));
XSelectInput(display, window, ExposureMask);
XMapWindow(display, window);
while (1) {
XNextEvent(display, &event);
if (event.type == Expose)
break;
}
XCloseDisplay(display); destroy_x11_window(window);
destroy_x11_connection(conn);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment