Changeset View
Changeset View
Standalone View
Standalone View
autotests/client/test_wayland_filter.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2017 David Edmundson <davidedmundson@kde.org> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | *********************************************************************/ | ||||
20 | // Qt | ||||
21 | #include <QtTest/QtTest> | ||||
22 | // KWin | ||||
23 | #include "../../src/client/compositor.h" | ||||
24 | #include "../../src/client/connection_thread.h" | ||||
25 | #include "../../src/client/event_queue.h" | ||||
26 | #include "../../src/client/region.h" | ||||
27 | #include "../../src/client/registry.h" | ||||
28 | #include "../../src/client/surface.h" | ||||
29 | #include "../../src/client/blur.h" | ||||
30 | #include "../../src/server/display.h" | ||||
31 | #include "../../src/server/compositor_interface.h" | ||||
32 | #include "../../src/server/region_interface.h" | ||||
33 | #include "../../src/server/blur_interface.h" | ||||
34 | #include "../../src/server/filtered_display.h" | ||||
35 | | ||||
36 | #include <wayland-server.h> | ||||
37 | | ||||
38 | using namespace KWayland::Client; | ||||
39 | | ||||
40 | class TestDisplay; | ||||
41 | | ||||
42 | class TestFilter : public QObject | ||||
43 | { | ||||
44 | Q_OBJECT | ||||
45 | public: | ||||
46 | explicit TestFilter(QObject *parent = nullptr); | ||||
47 | private Q_SLOTS: | ||||
48 | void init(); | ||||
49 | void cleanup(); | ||||
50 | void testFilter_data(); | ||||
51 | void testFilter(); | ||||
52 | | ||||
53 | private: | ||||
54 | TestDisplay *m_display; | ||||
55 | KWayland::Server::CompositorInterface *m_compositorInterface; | ||||
56 | KWayland::Server::BlurManagerInterface *m_blurManagerInterface; | ||||
57 | }; | ||||
58 | | ||||
59 | static const QString s_socketName = QStringLiteral("kwayland-test-wayland-blur-0"); | ||||
60 | | ||||
61 | //The following non-realistic class allows only clients in the m_allowedClients list to access the blur interface | ||||
62 | //all other interfaces are allowed | ||||
63 | class TestDisplay : public KWayland::Server::FilteredDisplay | ||||
64 | { | ||||
65 | public: | ||||
66 | TestDisplay(QObject *parent); | ||||
67 | bool allowInterface(KWayland::Server::ClientConnection * client, const QByteArray & interfaceName) override; | ||||
68 | QList<wl_client*> m_allowedClients; | ||||
69 | }; | ||||
70 | | ||||
71 | TestDisplay::TestDisplay(QObject *parent): | ||||
72 | KWayland::Server::FilteredDisplay(parent) | ||||
73 | {} | ||||
74 | | ||||
75 | bool TestDisplay::allowInterface(KWayland::Server::ClientConnection* client, const QByteArray& interfaceName) | ||||
76 | { | ||||
77 | if (interfaceName == "org_kde_kwin_blur_manager") { | ||||
78 | return m_allowedClients.contains(*client); | ||||
79 | } | ||||
80 | return true; | ||||
81 | } | ||||
82 | | ||||
83 | TestFilter::TestFilter(QObject *parent) | ||||
84 | : QObject(parent) | ||||
85 | , m_display(nullptr) | ||||
86 | , m_compositorInterface(nullptr) | ||||
87 | {} | ||||
88 | | ||||
89 | void TestFilter::init() | ||||
90 | { | ||||
91 | using namespace KWayland::Server; | ||||
92 | delete m_display; | ||||
93 | m_display = new TestDisplay(this); | ||||
94 | m_display->setSocketName(s_socketName); | ||||
95 | m_display->start(); | ||||
96 | QVERIFY(m_display->isRunning()); | ||||
97 | | ||||
98 | m_compositorInterface = m_display->createCompositor(m_display); | ||||
99 | m_compositorInterface->create(); | ||||
100 | QVERIFY(m_compositorInterface->isValid()); | ||||
101 | | ||||
102 | m_blurManagerInterface = m_display->createBlurManager(m_display); | ||||
103 | m_blurManagerInterface->create(); | ||||
104 | QVERIFY(m_blurManagerInterface->isValid()); | ||||
105 | } | ||||
106 | | ||||
107 | void TestFilter::cleanup() | ||||
108 | { | ||||
109 | } | ||||
110 | | ||||
111 | void TestFilter::testFilter_data() | ||||
112 | { | ||||
113 | QTest::addColumn<bool>("accessAllowed"); | ||||
114 | QTest::newRow("granted") << true; | ||||
115 | QTest::newRow("denied") << false; | ||||
116 | | ||||
117 | } | ||||
118 | | ||||
119 | void TestFilter::testFilter() | ||||
120 | { | ||||
121 | QFETCH(bool, accessAllowed); | ||||
122 | | ||||
123 | // setup connection | ||||
124 | QScopedPointer<KWayland::Client::ConnectionThread> connection(new KWayland::Client::ConnectionThread()); | ||||
125 | QSignalSpy connectedSpy(connection.data(), &ConnectionThread::connected); | ||||
126 | QVERIFY(connectedSpy.isValid()); | ||||
127 | connection->setSocketName(s_socketName); | ||||
128 | | ||||
129 | QScopedPointer<QThread> thread(new QThread(this)); | ||||
130 | connection->moveToThread(thread.data()); | ||||
131 | thread->start(); | ||||
132 | | ||||
133 | connection->initConnection(); | ||||
134 | QVERIFY(connectedSpy.wait()); | ||||
135 | | ||||
136 | //use low level API as Server::Display::connections only lists connections which have | ||||
137 | //been previous fetched via getConnection() | ||||
138 | if (accessAllowed) { | ||||
139 | wl_client *clientConnection; | ||||
140 | wl_client_for_each(clientConnection, wl_display_get_client_list(*m_display)) { | ||||
141 | m_display->m_allowedClients << clientConnection; | ||||
142 | } | ||||
143 | } | ||||
144 | | ||||
145 | KWayland::Client::EventQueue queue; | ||||
146 | queue.setup(connection.data()); | ||||
147 | | ||||
148 | Registry registry; | ||||
149 | QSignalSpy registryDoneSpy(®istry, &Registry::interfacesAnnounced); | ||||
150 | QSignalSpy compositorSpy(®istry, &Registry::compositorAnnounced); | ||||
151 | QSignalSpy blurSpy(®istry, &Registry::blurAnnounced); | ||||
152 | | ||||
153 | registry.setEventQueue(&queue); | ||||
154 | registry.create(connection->display()); | ||||
155 | QVERIFY(registry.isValid()); | ||||
156 | registry.setup(); | ||||
157 | | ||||
158 | QVERIFY(registryDoneSpy.wait()); | ||||
159 | QVERIFY(compositorSpy.count() == 1); | ||||
160 | QVERIFY(blurSpy.count() == accessAllowed ? 1 : 0); | ||||
161 | | ||||
162 | thread->quit(); | ||||
163 | thread->wait(); | ||||
164 | } | ||||
165 | | ||||
166 | | ||||
167 | QTEST_GUILESS_MAIN(TestFilter) | ||||
168 | #include "test_wayland_filter.moc" |