Debian Codegraph @

tests/auto/client/xdgdecorationv1/tst_xdgdecorationv1.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29 #include "mockcompositor.h"
30
31 #include <qwayland-server-xdg-decoration-unstable-v1.h>
32
33 #include <QtGui/QRasterWindow>
34 #include <QtGui/QOpenGLWindow>
35 #include <QtGui/QClipboard>
36 #include <QtCore/private/qcore_unix_p.h>
37
38 #include <fcntl.h>
39
40 using namespace MockCompositor;
41
42 constexpr int xdgDecorationVersion = 1; // protocol VERSION, not the name suffix (_v1)
43
44 class XdgDecorationManagerV1;
45 class XdgToplevelDecorationV1 : public QObject, public QtWaylandServer::zxdg_toplevel_decoration_v1
46 {
47 Q_OBJECT
48 public:
49 explicit XdgToplevelDecorationV1(XdgDecorationManagerV1 *manager, XdgToplevel *toplevel, int id, int version)
50 : zxdg_toplevel_decoration_v1(toplevel->resource()->client(), id, version)
51 , m_manager(manager)
52 , m_toplevel(toplevel)
53 {
54 }
55 void sendConfigure(mode mode)
56 {
57 if (!m_configureSent) {
58 // Attaching buffers before the configure is a protocol error
59 QVERIFY(!m_toplevel->surface()->m_pending.buffer);
60 QVERIFY(!m_toplevel->surface()->m_committed.buffer);
61 }
62 send_configure(mode);
63 m_configureSent = true;
64 }
65 void zxdg_toplevel_decoration_v1_destroy(Resource *resource) override
66 {
67 wl_resource_destroy(resource->handle);
68 }
69 void zxdg_toplevel_decoration_v1_destroy_resource(Resource *resource) override;
70 void zxdg_toplevel_decoration_v1_set_mode(Resource *resource, uint32_t mode) override
71 {
72 Q_UNUSED(resource);
73 m_unsetModeRequested = false;
74 m_requestedMode = XdgToplevelDecorationV1::mode(mode);
75 }
76 void zxdg_toplevel_decoration_v1_unset_mode(Resource *resource) override
77 {
78 Q_UNUSED(resource);
79 m_unsetModeRequested = true;
80 m_requestedMode = mode(0);
81 }
82 XdgDecorationManagerV1 *m_manager = nullptr;
83 XdgToplevel *m_toplevel = nullptr;
84 mode m_requestedMode = mode(0);
85 bool m_unsetModeRequested = false;
86 bool m_configureSent = false;
87 };
88
89 class XdgDecorationManagerV1 : public Global, public QtWaylandServer::zxdg_decoration_manager_v1
90 {
91 Q_OBJECT
92 public:
93 explicit XdgDecorationManagerV1(CoreCompositor *compositor, int version = 1)
94 : QtWaylandServer::zxdg_decoration_manager_v1(compositor->m_display, version)
95 , m_version(version)
96 {}
97 bool isClean() override { return m_decorations.empty(); }
98 XdgToplevelDecorationV1 *decorationFor(XdgToplevel *toplevel)
99 {
100 return m_decorations.value(toplevel, nullptr);
101 }
102
103 int m_version = 1; // TODO: Remove on libwayland upgrade
104 QMap<XdgToplevel *, XdgToplevelDecorationV1 *> m_decorations;
105
106 protected:
107 void zxdg_decoration_manager_v1_destroy(Resource *resource) override
108 {
109 //TODO: Should the decorations be destroyed at this point?
110 wl_resource_destroy(resource->handle);
111 }
112
113 void zxdg_decoration_manager_v1_get_toplevel_decoration(Resource *resource, uint32_t id, ::wl_resource *toplevelResource) override
114 {
115 auto *toplevel = fromResource<XdgToplevel>(toplevelResource);
116 QVERIFY(toplevel);
117 QVERIFY(!decorationFor(toplevel));
118
119 // Attaching buffers before the configure is a protocol error
120 QVERIFY(!toplevel->surface()->m_pending.buffer);
121 QVERIFY(!toplevel->surface()->m_committed.buffer);
122
123 m_decorations[toplevel] = new XdgToplevelDecorationV1(this, toplevel, id, resource->version());
124 }
125 };
126
127 void XdgToplevelDecorationV1::zxdg_toplevel_decoration_v1_destroy_resource(QtWaylandServer::zxdg_toplevel_decoration_v1::Resource *resource)
128 {
129 Q_UNUSED(resource);
130 int removed = m_manager->m_decorations.remove(m_toplevel);
131 Q_ASSERT(removed == 1);
132 delete this;
133 }
134
135 class XdgDecorationCompositor : public DefaultCompositor {
136 public:
137 explicit XdgDecorationCompositor()
138 {
139 exec([this] {
140 m_config.autoConfigure = true;
141 add<XdgDecorationManagerV1>(xdgDecorationVersion);
142 });
143 }
144 XdgToplevelDecorationV1 *toplevelDecoration(int i = 0) {
145 return get<XdgDecorationManagerV1>()->decorationFor(xdgToplevel(i));
146 }
147 };
148
149 class tst_xdgdecorationv1 : public QObject, private XdgDecorationCompositor
150 {
151 Q_OBJECT
152 private slots:
153 void initTestCase();
154 void cleanup() { QTRY_VERIFY2(isClean(), qPrintable(dirtyMessage())); }
155 void clientSidePreferredByCompositor();
156 void initialFramelessWindowHint();
157 void delayedFramelessWindowHint();
158 };
159
160 void tst_xdgdecorationv1::initTestCase()
161 {
162 if (qEnvironmentVariableIntValue("QT_WAYLAND_DISABLE_WINDOWDECORATION"))
163 QSKIP("This test doesn't make sense when QT_WAYLAND_DISABLE_WINDOWDECORATION is set in the environment");
164 }
165
166 void tst_xdgdecorationv1::clientSidePreferredByCompositor()
167 {
168 QRasterWindow window;
169 window.show();
170 QCOMPOSITOR_TRY_COMPARE(get<XdgDecorationManagerV1>()->resourceMap().size(), 1);
171 QCOMPOSITOR_TRY_COMPARE(get<XdgDecorationManagerV1>()->resourceMap().first()->version(), xdgDecorationVersion);
172 QCOMPOSITOR_TRY_VERIFY(toplevelDecoration()); // The client creates a toplevel object
173
174 // Check that we don't assume decorations before the server has configured them
175 QVERIFY(window.frameMargins().isNull());
176
177 QCOMPOSITOR_TRY_VERIFY(xdgToplevel());
178 QCOMPOSITOR_TRY_VERIFY(toplevelDecoration()->m_unsetModeRequested);
179 QVERIFY(window.frameMargins().isNull()); // We're still waiting for a configure
180 exec([&] {
181 toplevelDecoration()->sendConfigure(XdgToplevelDecorationV1::mode_client_side);
182 xdgToplevel()->sendCompleteConfigure();
183 });
184 QTRY_VERIFY(!window.frameMargins().isNull());
185 }
186
187 void tst_xdgdecorationv1::initialFramelessWindowHint()
188 {
189 QRasterWindow window;
190 window.setFlag(Qt::FramelessWindowHint, true);
191 window.show();
192 QCOMPOSITOR_TRY_COMPARE(get<XdgDecorationManagerV1>()->resourceMap().size(), 1);
193 QCOMPOSITOR_TRY_VERIFY(xdgToplevel());
194 exec([&]{
195 xdgToplevel()->sendCompleteConfigure();
196 });
197 QCOMPOSITOR_TRY_VERIFY(xdgSurface()->m_committedConfigureSerial);
198
199 // The client should not have create a decoration object, because that allows the compositor
200 // to override our decision and add server side decorations to our window.
201 QCOMPOSITOR_TRY_VERIFY(!toplevelDecoration());
202 }
203
204 void tst_xdgdecorationv1::delayedFramelessWindowHint()
205 {
206 QRasterWindow window;
207 window.show();
208 QCOMPOSITOR_TRY_COMPARE(get<XdgDecorationManagerV1>()->resourceMap().size(), 1);
209 QCOMPOSITOR_TRY_VERIFY(xdgToplevel());
210 exec([&]{
211 xdgToplevel()->sendCompleteConfigure();
212 });
213 QCOMPOSITOR_TRY_VERIFY(xdgSurface()->m_committedConfigureSerial);
214 QCOMPOSITOR_TRY_VERIFY(toplevelDecoration());
215
216 window.setFlag(Qt::FramelessWindowHint, true);
217
218 // The client should now destroy the decoration object, so the compositor is no longer
219 // able to force window decorations
220 QCOMPOSITOR_TRY_VERIFY(!toplevelDecoration());
221 }
222
223 QCOMPOSITOR_TEST_MAIN(tst_xdgdecorationv1)
224 #include "tst_xdgdecorationv1.moc"