tests: Add xdg-client-helper library

And make use of it in kiosk-shell-test. There's no functional change,
this is for other shells to re-use the code here.

Signed-off-by: Marius Vlad <marius.vlad@collabora.com>
This commit is contained in:
Marius Vlad 2025-05-02 10:12:06 +03:00
parent 5ca78cdf05
commit 3b7d29f871
4 changed files with 309 additions and 231 deletions

View file

@ -35,7 +35,7 @@
#include "weston-test-client-helper.h"
#include "weston-test-fixture-compositor.h"
#include "weston-test-assert.h"
#include "xdg-shell-client-protocol.h"
#include "xdg-client-helper.h"
static enum test_result_code
fixture_setup(struct weston_test_harness *harness)
@ -54,236 +54,6 @@ fixture_setup(struct weston_test_harness *harness)
}
DECLARE_FIXTURE_SETUP(fixture_setup);
/*
* DO NOT COPY THIS CODE INTO ANOTHER FILE.
*
* This code is intended to be the starting point of a set of more generic XDG
* support code to go into weston-test-client-helper.c. If you are the second
* person here, please merge it into there, modify it as appropriate for your
* needs (whilst keeping this test working), and cc @daniels on the MR.
*
* Thanks!
*/
struct xdg_client {
struct client *client;
struct xdg_wm_base *xdg_wm_base;
};
struct xdg_surface_data {
struct xdg_client *xdg_client;
struct surface *surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct xdg_popup *xdg_popup;
struct xdg_surface *xdg_parent;
struct wl_list parent_link;
struct wl_list child_list;
struct {
int width;
int height;
bool fullscreen;
bool maximized;
bool resizing;
bool activated;
uint32_t serial; /* != 0 if configure pending */
} configure;
struct {
bool fullscreen;
bool maximized;
} target;
};
static void
handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *toplevel,
int32_t width, int32_t height,
struct wl_array *states)
{
struct xdg_surface_data *surface = data;
uint32_t *state;
surface->configure.width = width;
surface->configure.height = height;
surface->configure.fullscreen = false;
surface->configure.maximized = false;
surface->configure.resizing = false;
surface->configure.activated = false;
wl_array_for_each(state, states) {
if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN)
surface->configure.fullscreen = true;
else if (*state == XDG_TOPLEVEL_STATE_MAXIMIZED)
surface->configure.maximized = true;
else if (*state == XDG_TOPLEVEL_STATE_RESIZING)
surface->configure.resizing = true;
else if (*state == XDG_TOPLEVEL_STATE_ACTIVATED)
surface->configure.activated = true;
}
}
static void
handle_xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
}
static void
handle_xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *toplevel,
int32_t width, int32_t height)
{
}
static void
handle_xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *toplevel,
struct wl_array *capabilities)
{
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
handle_xdg_toplevel_configure,
handle_xdg_toplevel_close,
handle_xdg_toplevel_configure_bounds,
handle_xdg_toplevel_wm_capabilities,
};
static void
handle_xdg_surface_configure(void *data, struct xdg_surface *wm_surface,
uint32_t serial)
{
struct xdg_surface_data *surface = data;
surface->configure.serial = serial;
}
static const struct xdg_surface_listener xdg_surface_listener = {
handle_xdg_surface_configure,
};
static void
handle_xdg_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
{
struct xdg_client *xdg_client = data;
xdg_wm_base_pong(wm_base, serial);
wl_display_flush(xdg_client->client->wl_display);
}
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
handle_xdg_ping,
};
static struct xdg_surface_data *
create_xdg_surface(struct xdg_client *xdg_client)
{
struct xdg_surface_data *xdg_surface = xzalloc(sizeof(*xdg_surface));
test_assert_ptr_not_null(xdg_surface);
xdg_surface->surface = create_test_surface(xdg_client->client);
test_assert_ptr_not_null(xdg_surface->surface);
xdg_surface->xdg_surface =
xdg_wm_base_get_xdg_surface(xdg_client->xdg_wm_base,
xdg_surface->surface->wl_surface);
xdg_surface_add_listener(xdg_surface->xdg_surface,
&xdg_surface_listener, xdg_surface);
return xdg_surface;
}
static void
destroy_xdg_surface(struct xdg_surface_data *xdg_surface)
{
if (xdg_surface->xdg_toplevel)
xdg_toplevel_destroy(xdg_surface->xdg_toplevel);
xdg_surface_destroy(xdg_surface->xdg_surface);
surface_destroy(xdg_surface->surface);
free(xdg_surface);
}
static void
xdg_surface_make_toplevel(struct xdg_surface_data *xdg_surface,
const char *app_id, const char *title)
{
xdg_surface->xdg_toplevel =
xdg_surface_get_toplevel(xdg_surface->xdg_surface);
test_assert_ptr_not_null(xdg_surface->xdg_toplevel);
xdg_toplevel_add_listener(xdg_surface->xdg_toplevel,
&xdg_toplevel_listener, xdg_surface);
xdg_toplevel_set_app_id(xdg_surface->xdg_toplevel, app_id);
xdg_toplevel_set_title(xdg_surface->xdg_toplevel, title);
}
static void
xdg_surface_wait_configure(struct xdg_surface_data *xdg_surface)
{
wl_surface_commit(xdg_surface->surface->wl_surface);
wl_display_roundtrip(xdg_surface->surface->client->wl_display);
test_assert_u32_gt(xdg_surface->configure.serial, 0);
}
static void
xdg_surface_commit_solid(struct xdg_surface_data *xdg_surface,
uint8_t r, uint8_t g, uint8_t b)
{
pixman_color_t color;
struct buffer *buf;
int width = xdg_surface->configure.width;
int height = xdg_surface->configure.height;
buf = create_shm_buffer_a8r8g8b8(xdg_surface->surface->client,
width, height);
test_assert_ptr_not_null(buf);
xdg_surface->surface->buffer = buf;
color_rgb888(&color, r, g, b);
fill_image_with_color(buf->image, &color);
wl_surface_attach(xdg_surface->surface->wl_surface, buf->proxy, 0, 0);
wl_surface_damage_buffer(xdg_surface->surface->wl_surface,
0, 0, width, height);
if (xdg_surface->configure.serial > 0) {
xdg_surface_ack_configure(xdg_surface->xdg_surface,
xdg_surface->configure.serial);
xdg_surface->configure.serial = 0;
}
xdg_surface->surface->width = width;
xdg_surface->surface->height = height;
wl_surface_commit(xdg_surface->surface->wl_surface);
}
static struct xdg_client *
create_xdg_client(void)
{
struct xdg_client *xdg_client = xzalloc(sizeof(*xdg_client));
test_assert_ptr_not_null(xdg_client);
xdg_client->client = create_client();
test_assert_ptr_not_null(xdg_client->client);
xdg_client->xdg_wm_base = bind_to_singleton_global(xdg_client->client,
&xdg_wm_base_interface,
5);
test_assert_ptr_not_null(xdg_client->xdg_wm_base);
xdg_wm_base_add_listener(xdg_client->xdg_wm_base, &xdg_wm_base_listener,
xdg_client);
return xdg_client;
}
static void
xdg_client_destroy(struct xdg_client *xdg_client)
{
xdg_wm_base_destroy(xdg_client->xdg_wm_base);
client_destroy(xdg_client->client);
free(xdg_client);
}
#define DECLARE_LIST_ITERATOR(name, parent, list, child, link) \
static child * \
next_##name(parent *from, child *pos) \

View file

@ -30,6 +30,7 @@ lib_test_client = static_library(
[
'weston-test-client-helper.c',
'weston-test-fixture-compositor.c',
'xdg-client-helper.c',
linux_dmabuf_unstable_v1_client_protocol_h,
linux_dmabuf_unstable_v1_protocol_c,
weston_test_client_protocol_h,

222
tests/xdg-client-helper.c Normal file
View file

@ -0,0 +1,222 @@
/*
* Copyright © 2016-2023 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 "config.h"
#include <stdio.h>
#include <string.h>
#include <libweston/desktop.h>
#include "shared/xalloc.h"
#include "weston-test-client-helper.h"
#include "weston-test-assert.h"
#include "xdg-client-helper.h"
#include "xdg-shell-client-protocol.h"
static void
handle_xdg_toplevel_configure(void *data, struct xdg_toplevel *toplevel,
int32_t width, int32_t height,
struct wl_array *states)
{
struct xdg_surface_data *surface = data;
uint32_t *state;
surface->configure.width = width;
surface->configure.height = height;
surface->configure.fullscreen = false;
surface->configure.maximized = false;
surface->configure.resizing = false;
surface->configure.activated = false;
wl_array_for_each(state, states) {
if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN)
surface->configure.fullscreen = true;
else if (*state == XDG_TOPLEVEL_STATE_MAXIMIZED)
surface->configure.maximized = true;
else if (*state == XDG_TOPLEVEL_STATE_RESIZING)
surface->configure.resizing = true;
else if (*state == XDG_TOPLEVEL_STATE_ACTIVATED)
surface->configure.activated = true;
}
}
static void
handle_xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
}
static void
handle_xdg_toplevel_configure_bounds(void *data, struct xdg_toplevel *toplevel,
int32_t width, int32_t height)
{
}
static void
handle_xdg_toplevel_wm_capabilities(void *data, struct xdg_toplevel *toplevel,
struct wl_array *capabilities)
{
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
handle_xdg_toplevel_configure,
handle_xdg_toplevel_close,
handle_xdg_toplevel_configure_bounds,
handle_xdg_toplevel_wm_capabilities,
};
static void
handle_xdg_surface_configure(void *data, struct xdg_surface *wm_surface,
uint32_t serial)
{
struct xdg_surface_data *surface = data;
surface->configure.serial = serial;
}
static const struct xdg_surface_listener xdg_surface_listener = {
handle_xdg_surface_configure,
};
static void
handle_xdg_ping(void *data, struct xdg_wm_base *wm_base, uint32_t serial)
{
struct xdg_client *xdg_client = data;
xdg_wm_base_pong(wm_base, serial);
wl_display_flush(xdg_client->client->wl_display);
}
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
handle_xdg_ping,
};
struct xdg_surface_data *
create_xdg_surface(struct xdg_client *xdg_client)
{
struct xdg_surface_data *xdg_surface = xzalloc(sizeof(*xdg_surface));
test_assert_ptr_not_null(xdg_surface);
xdg_surface->surface = create_test_surface(xdg_client->client);
test_assert_ptr_not_null(xdg_surface->surface);
xdg_surface->xdg_surface =
xdg_wm_base_get_xdg_surface(xdg_client->xdg_wm_base,
xdg_surface->surface->wl_surface);
xdg_surface_add_listener(xdg_surface->xdg_surface,
&xdg_surface_listener, xdg_surface);
return xdg_surface;
}
void
destroy_xdg_surface(struct xdg_surface_data *xdg_surface)
{
if (xdg_surface->xdg_toplevel)
xdg_toplevel_destroy(xdg_surface->xdg_toplevel);
xdg_surface_destroy(xdg_surface->xdg_surface);
surface_destroy(xdg_surface->surface);
free(xdg_surface);
}
void
xdg_surface_make_toplevel(struct xdg_surface_data *xdg_surface,
const char *app_id, const char *title)
{
xdg_surface->xdg_toplevel =
xdg_surface_get_toplevel(xdg_surface->xdg_surface);
test_assert_ptr_not_null(xdg_surface->xdg_toplevel);
xdg_toplevel_add_listener(xdg_surface->xdg_toplevel,
&xdg_toplevel_listener, xdg_surface);
xdg_toplevel_set_app_id(xdg_surface->xdg_toplevel, app_id);
xdg_toplevel_set_title(xdg_surface->xdg_toplevel, title);
}
void
xdg_surface_wait_configure(struct xdg_surface_data *xdg_surface)
{
wl_surface_commit(xdg_surface->surface->wl_surface);
wl_display_roundtrip(xdg_surface->surface->client->wl_display);
test_assert_u32_gt(xdg_surface->configure.serial, 0);
}
void
xdg_surface_commit_solid(struct xdg_surface_data *xdg_surface,
uint8_t r, uint8_t g, uint8_t b)
{
pixman_color_t color;
struct buffer *buf;
int width = xdg_surface->configure.width;
int height = xdg_surface->configure.height;
buf = create_shm_buffer_a8r8g8b8(xdg_surface->surface->client,
width, height);
test_assert_ptr_not_null(buf);
xdg_surface->surface->buffer = buf;
color_rgb888(&color, r, g, b);
fill_image_with_color(buf->image, &color);
wl_surface_attach(xdg_surface->surface->wl_surface, buf->proxy, 0, 0);
wl_surface_damage_buffer(xdg_surface->surface->wl_surface,
0, 0, width, height);
if (xdg_surface->configure.serial > 0) {
xdg_surface_ack_configure(xdg_surface->xdg_surface,
xdg_surface->configure.serial);
xdg_surface->configure.serial = 0;
}
xdg_surface->surface->width = width;
xdg_surface->surface->height = height;
wl_surface_commit(xdg_surface->surface->wl_surface);
}
struct xdg_client *
create_xdg_client(void)
{
struct xdg_client *xdg_client = xzalloc(sizeof(*xdg_client));
test_assert_ptr_not_null(xdg_client);
xdg_client->client = create_client();
test_assert_ptr_not_null(xdg_client->client);
xdg_client->xdg_wm_base = bind_to_singleton_global(xdg_client->client,
&xdg_wm_base_interface,
5);
test_assert_ptr_not_null(xdg_client->xdg_wm_base);
xdg_wm_base_add_listener(xdg_client->xdg_wm_base, &xdg_wm_base_listener,
xdg_client);
return xdg_client;
}
void
xdg_client_destroy(struct xdg_client *xdg_client)
{
xdg_wm_base_destroy(xdg_client->xdg_wm_base);
client_destroy(xdg_client->client);
free(xdg_client);
}

85
tests/xdg-client-helper.h Normal file
View file

@ -0,0 +1,85 @@
/*
* Copyright © 2016-2023 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.
*/
#ifndef __HAVE_XDG_CLIENT_HELPER_H
#define __HAVE_XDG_CLIENT_HELPER_H
struct xdg_client {
struct client *client;
struct xdg_wm_base *xdg_wm_base;
};
struct xdg_surface_data {
struct xdg_client *xdg_client;
struct surface *surface;
struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel;
struct xdg_popup *xdg_popup;
struct xdg_surface *xdg_parent;
struct wl_list parent_link;
struct wl_list child_list;
struct {
int width;
int height;
bool fullscreen;
bool maximized;
bool resizing;
bool activated;
uint32_t serial; /* != 0 if configure pending */
} configure;
struct {
bool fullscreen;
bool maximized;
} target;
};
struct xdg_client *
create_xdg_client(void);
void
xdg_client_destroy(struct xdg_client *xdg_client);
void
xdg_surface_make_toplevel(struct xdg_surface_data *xdg_surface,
const char *app_id, const char *title);
void
xdg_surface_wait_configure(struct xdg_surface_data *xdg_surface);
struct xdg_surface_data *
create_xdg_surface(struct xdg_client *xdg_client);
void
xdg_surface_commit_solid(struct xdg_surface_data *xdg_surface,
uint8_t r, uint8_t g, uint8_t b);
void
destroy_xdg_surface(struct xdg_surface_data *xdg_surface);
#endif