diff options
author | Johan Klokkhammer Helsing <johan.helsing@qt.io> | 2018-10-24 08:46:06 +0200 |
---|---|---|
committer | Johan Helsing <johan.helsing@qt.io> | 2018-12-11 15:46:49 +0000 |
commit | ddef100d9fa7014b21280b1380e6fbcef80277b3 (patch) | |
tree | cd0e674010a585fd6bc28cf0c26523392d8fbb8b /tests/auto/client/shared/xdgshell.cpp | |
parent | 32b6f5f41349d109f872caf42a44352744bd48e2 (diff) |
Create a new type of mock compositor for client tests
There are a number of issues with the current client testing:
- Adding new compositor functionality is cumbersome (need to add compositor
send method, command, implementation, not to mention creating new wrapper
objects.
- Customizing available globals and their versions is not possible and would be
hard to implement. I.e. how to test that functionality works with old and new
versions of an interface? Handle globals being destroyed. We did this with
wl_output, but it was painfully cumbersome.
- Hard to verify that the compositor state is clean between tests. It is
currently done in some tests, but requires boiler plate code which needs to
be added and maintained for each test.
- In general lots of boiler-plate for new tests. (We have to have separate
tests as long as Qt has global/static state. I.e. if one shell extension has
been initialized, we can't deinitialize and initialize another one, so tests
have to be separate.)
- Dispatching server events tied to the client event loop sometimes makes it
hard to write tests without deadlocks.
- Abstraction, encapsulation and automatic behavior that can't be disabled
makes it hard to test low-level functionality like surface exposure.
So, in an attempt to mitigate these issues, I wrote a new testing framework.
- Compositor dispatch is running continuously in it's own thread, access to
compositor state is guarded by a mutex on the compositor, locking this will
make dispatching stop, so the test can safely access internals. Although a
bit cumbersome at first this makes it much easier to directly use server
protocol commands from the test itself, i.e. no need to create commands for
every single thing we want to test.
- The CoreCompositor::exec template method can accept a lambda that will be run
with dispatching stopped. It can also return a value, conveniently letting us
safely extract or modify compositor state from tests.
- This framework also takes full advantage of the qtwaylandscanner, using
wrapper classes for everything, reducing boiler plate considerably.
- The compositor parts are designed to do as little as possible automatically,
but still provide easy ways to enable common functionality, like releasing
buffers automatically, configuring shell surfaces etc.
- Compositor globals are pluggable, use add<GlobalClass>() and
remove<GlobalClass>() to add new global interfaces. I.e. easy to create a
compositor with or without data_device_manager for instance.
- DefaultCompositor provides a sensible default set of functionality and
convenience methods for most test-cases. Custom ones can still be made by
inheriting from CoreCompositor directly instead or by removing or adding
globals to DefaultCompositor.
- Globals have an isClean() method. Implement it to verify that the client
didn't leave any objects lying around from the previous test.
CoreCompositor::isClean calls isClean on the globals so a single call is all
that's needed.
In short, we've traded mock compositor encapsulation and thread safety
guarantees for less boiler-plate, easier and more convenient access to
internals.
Anything accessing compositor state should go into a exec() call, or through
the wrapper macros QCOMPOSITOR_VERIFY and QCOMPOSITOR_COMPARE (or the TRY
versions). I've also tried to make the compositor print warnings if compositor
state is accessed in an unsafe way.
The mock compositor is currently built once per test due to CI limitations
(same thing as with the old tests).
Change-Id: Ia3feb80ce175d3814292b7f4768a0cc719f8b0e8
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
Diffstat (limited to 'tests/auto/client/shared/xdgshell.cpp')
-rw-r--r-- | tests/auto/client/shared/xdgshell.cpp | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/tests/auto/client/shared/xdgshell.cpp b/tests/auto/client/shared/xdgshell.cpp new file mode 100644 index 000000000..ebbcc2942 --- /dev/null +++ b/tests/auto/client/shared/xdgshell.cpp @@ -0,0 +1,186 @@ +/**************************************************************************** +** +** Copyright (C) 2018 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "xdgshell.h" + +namespace MockCompositor { + +XdgWmBase::XdgWmBase(CoreCompositor *compositor, int version) + : QtWaylandServer::xdg_wm_base(compositor->m_display, version) + , m_compositor(compositor) +{ +} + +XdgToplevel *XdgWmBase::toplevel(int i) +{ + int j = 0; + for (auto *xdgSurface : qAsConst(m_xdgSurfaces)) { + if (auto *toplevel = xdgSurface->m_toplevel) { + if (j == i) + return toplevel; + ++j; + } + } + return nullptr; +} + +XdgPopup *XdgWmBase::popup(int i) +{ + int j = 0; + for (auto *xdgSurface : qAsConst(m_xdgSurfaces)) { + if (auto *popup = xdgSurface->m_popup) { + if (j == i) + return popup; + ++j; + } + } + return nullptr; +} + +void XdgWmBase::xdg_wm_base_get_xdg_surface(Resource *resource, uint32_t id, wl_resource *surface) +{ + auto *s = fromResource<Surface>(surface); + auto *xdgSurface = new XdgSurface(this, s, resource->client(), id, resource->version()); + m_xdgSurfaces << xdgSurface; + emit xdgSurfaceCreated(xdgSurface); +} + +void XdgWmBase::xdg_wm_base_pong(Resource *resource, uint32_t serial) +{ + Q_UNUSED(resource); + emit pong(serial); +} + +XdgSurface::XdgSurface(XdgWmBase *xdgWmBase, Surface *surface, wl_client *client, int id, int version) + : QtWaylandServer::xdg_surface(client, id, version) + , m_xdgWmBase(xdgWmBase) + , m_surface(surface) +{ + QVERIFY(!surface->m_pending.buffer); + QVERIFY(!surface->m_committed.buffer); + connect(this, &XdgSurface::toplevelCreated, xdgWmBase, &XdgWmBase::toplevelCreated); + connect(surface, &Surface::attach, this, &XdgSurface::verifyConfigured); + connect(surface, &Surface::commit, this, [this] { + if (m_ackedConfigureSerial != m_committedConfigureSerial) { + m_committedConfigureSerial = m_ackedConfigureSerial; + emit configureCommitted(m_committedConfigureSerial); + } + }); +} + +void XdgSurface::sendConfigure(uint serial) +{ + Q_ASSERT(serial); + m_pendingConfigureSerials.append(serial); + m_configureSent = true; + xdg_surface::send_configure(serial); +} + +uint XdgSurface::sendConfigure() +{ + const uint serial = m_xdgWmBase->m_compositor->nextSerial(); + sendConfigure(serial); + return serial; +} + +void XdgSurface::xdg_surface_get_toplevel(Resource *resource, uint32_t id) +{ + QVERIFY(!m_toplevel); + QVERIFY(!m_popup); + m_toplevel = new XdgToplevel(this, id, resource->version()); + emit toplevelCreated(m_toplevel); +} + +void XdgSurface::xdg_surface_get_popup(Resource *resource, uint32_t id, wl_resource *parent, wl_resource *positioner) +{ + Q_UNUSED(parent); + Q_UNUSED(positioner); + QVERIFY(!m_toplevel); + QVERIFY(!m_popup); + m_popup = new XdgPopup(this, id, resource->version()); +} + +void XdgSurface::xdg_surface_destroy_resource(Resource *resource) +{ + Q_UNUSED(resource); + bool removed = m_xdgWmBase->m_xdgSurfaces.removeOne(this); + Q_ASSERT(removed); + delete this; +} + +void XdgSurface::xdg_surface_ack_configure(Resource *resource, uint32_t serial) +{ + Q_UNUSED(resource); + QVERIFY2(m_pendingConfigureSerials.contains(serial), qPrintable(QString::number(serial))); + m_ackedConfigureSerial = serial; + while (!m_pendingConfigureSerials.empty()) { + uint s = m_pendingConfigureSerials.takeFirst(); + if (s == serial) + return; + } +} + +XdgToplevel::XdgToplevel(XdgSurface *xdgSurface, int id, int version) + : QtWaylandServer::xdg_toplevel(xdgSurface->resource()->client(), id, version) + , m_xdgSurface(xdgSurface) +{ +} + +void XdgToplevel::sendConfigure(const QSize &size, const QVector<uint> &states) +{ + send_configure(size.width(), size.height(), toByteArray(states)); +} + +uint XdgToplevel::sendCompleteConfigure(const QSize &size, const QVector<uint> &states) +{ + sendConfigure(size, states); + return m_xdgSurface->sendConfigure(); +} + +XdgPopup::XdgPopup(XdgSurface *xdgSurface, int id, int version) + : QtWaylandServer::xdg_popup(xdgSurface->resource()->client(), id, version) + , m_xdgSurface(xdgSurface) +{ +} + +void XdgPopup::sendConfigure(const QRect &geometry) +{ + send_configure(geometry.x(), geometry.y(), geometry.width(), geometry.height()); +} + +void XdgPopup::xdg_popup_grab(QtWaylandServer::xdg_popup::Resource *resource, wl_resource *seat, uint32_t serial) +{ + Q_UNUSED(resource); + Q_UNUSED(seat); // TODO: verify correct seat as well + //TODO: verify no other popup has grabbed + QVERIFY(!m_grabbed); + m_grabbed = true; + m_grabSerial = serial; +} + +} // namespace MockCompositor |