Changeset View
Changeset View
Standalone View
Standalone View
autotests/server/test_screencasting.cpp
- This file was added.
1 | /******************************************************************** | ||||
---|---|---|---|---|---|
2 | Copyright 2020 Aleix Pol Gonzalez <aleixpol@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 <QHash> | ||||
22 | #include <QThread> | ||||
23 | #include <QtTest> | ||||
24 | | ||||
25 | #include <wayland-client.h> | ||||
26 | | ||||
27 | // WaylandServer | ||||
28 | #include "../../src/server/compositor_interface.h" | ||||
29 | #include "../../src/server/display.h" | ||||
30 | #include "../../src/server/seat_interface.h" | ||||
31 | #include "../../src/server/screencasting_interface.h" | ||||
32 | | ||||
33 | #include "../../src/client/connection_thread.h" | ||||
34 | #include "../../src/client/event_queue.h" | ||||
35 | #include "../../src/client/registry.h" | ||||
36 | #include "../../src/client/seat.h" | ||||
37 | #include "../../src/client/screencasting.h" | ||||
38 | #include "../../src/client/compositor.h" | ||||
39 | #include "../../src/client/screencasting.h" | ||||
40 | | ||||
41 | class TestScreencastingInterface : public QObject | ||||
42 | { | ||||
43 | Q_OBJECT | ||||
44 | public: | ||||
45 | TestScreencastingInterface() | ||||
46 | { | ||||
47 | } | ||||
48 | | ||||
49 | ~TestScreencastingInterface() override; | ||||
50 | | ||||
51 | private Q_SLOTS: | ||||
52 | void initTestCase(); | ||||
53 | void testAdd(); | ||||
54 | void testCreate(); | ||||
55 | | ||||
56 | private: | ||||
57 | QPointer<KWayland::Server::ScreencastingStreamInterface> m_triggered = nullptr; | ||||
58 | void sourceTriggered(KWayland::Server::ScreencastingStreamInterface* stream) | ||||
59 | { | ||||
60 | static int nodeid = 1; | ||||
61 | m_triggered = stream; | ||||
62 | stream->sendCreated(nodeid++); | ||||
63 | } | ||||
64 | KWayland::Server::ScreencastingSource createTestSource() { | ||||
65 | m_triggered.clear(); | ||||
66 | auto f = [this] (KWayland::Server::ScreencastingStreamInterface *r) { sourceTriggered(r); }; | ||||
67 | return KWayland::Server::ScreencastingSource("potatoes", "video-display", true, {0,0,200,200}, {400, 400}, f); | ||||
68 | } | ||||
69 | | ||||
70 | KWayland::Client::ConnectionThread *m_connection; | ||||
71 | KWayland::Client::EventQueue *m_queue = nullptr; | ||||
72 | KWayland::Client::Screencasting *m_screencasting = nullptr; | ||||
73 | | ||||
74 | KWayland::Server::ScreencastingInterface *m_screencastingInterface = nullptr; | ||||
75 | | ||||
76 | QThread *m_thread; | ||||
77 | KWayland::Server::Display *m_display = nullptr; | ||||
78 | }; | ||||
79 | | ||||
80 | static const QString s_socketName = QStringLiteral("kwin-wayland-server-screencasting-test-0"); | ||||
81 | | ||||
82 | void TestScreencastingInterface::initTestCase() | ||||
83 | { | ||||
84 | delete m_display; | ||||
85 | m_display = new KWayland::Server::Display(this); | ||||
86 | m_display->setSocketName(s_socketName); | ||||
87 | m_display->start(); | ||||
88 | QVERIFY(m_display->isRunning()); | ||||
89 | | ||||
90 | // setup connection | ||||
91 | m_connection = new KWayland::Client::ConnectionThread; | ||||
92 | QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected); | ||||
93 | QVERIFY(connectedSpy.isValid()); | ||||
94 | m_connection->setSocketName(s_socketName); | ||||
95 | | ||||
96 | m_thread = new QThread(this); | ||||
97 | m_connection->moveToThread(m_thread); | ||||
98 | m_thread->start(); | ||||
99 | | ||||
100 | m_connection->initConnection(); | ||||
101 | QVERIFY(connectedSpy.wait()); | ||||
102 | | ||||
103 | m_queue = new KWayland::Client::EventQueue(this); | ||||
104 | QVERIFY(!m_queue->isValid()); | ||||
105 | m_queue->setup(m_connection); | ||||
106 | QVERIFY(m_queue->isValid()); | ||||
107 | | ||||
108 | KWayland::Client::Registry registry; | ||||
109 | | ||||
110 | QSignalSpy screencastingSpy(®istry, &KWayland::Client::Registry::screencastingAnnounced); | ||||
111 | QVERIFY(screencastingSpy.isValid()); | ||||
112 | m_screencastingInterface = m_display->createScreencastingInterface(this); | ||||
113 | | ||||
114 | connect(®istry, &KWayland::Client::Registry::screencastingAnnounced, this, [this, ®istry] (quint32 name, quint32 version) { | ||||
115 | Q_ASSERT(version == 1); | ||||
116 | m_screencasting = new KWayland::Client::Screencasting(®istry, name, version, this); | ||||
117 | }); | ||||
118 | registry.setEventQueue(m_queue); | ||||
119 | registry.create(m_connection->display()); | ||||
120 | QVERIFY(registry.isValid()); | ||||
121 | registry.setup(); | ||||
122 | wl_display_flush(m_connection->display()); | ||||
123 | | ||||
124 | QVERIFY(m_screencastingInterface); | ||||
125 | QVERIFY(m_screencasting || screencastingSpy.wait()); | ||||
126 | QVERIFY(m_screencasting); | ||||
127 | } | ||||
128 | | ||||
129 | TestScreencastingInterface::~TestScreencastingInterface() | ||||
130 | { | ||||
131 | delete m_queue; | ||||
132 | m_queue = nullptr; | ||||
133 | | ||||
134 | if (m_thread) { | ||||
135 | m_thread->quit(); | ||||
136 | m_thread->wait(); | ||||
137 | delete m_thread; | ||||
138 | m_thread = nullptr; | ||||
139 | } | ||||
140 | m_connection->deleteLater(); | ||||
141 | m_connection = nullptr; | ||||
142 | | ||||
143 | delete m_display; | ||||
144 | } | ||||
145 | | ||||
146 | void TestScreencastingInterface::testAdd() | ||||
147 | { | ||||
148 | QSignalSpy spySourcesChanged(m_screencasting, &KWayland::Client::Screencasting::sourcesChanged); | ||||
149 | | ||||
150 | QCOMPARE(m_screencasting->sources().count(), 0); | ||||
151 | | ||||
152 | auto serverSource = createTestSource(); | ||||
153 | m_screencastingInterface->addSource(serverSource); | ||||
154 | QVERIFY(spySourcesChanged.count() || spySourcesChanged.wait()); | ||||
155 | QCOMPARE(m_screencasting->sources().count(), 1); | ||||
156 | | ||||
157 | m_screencastingInterface->removeSource(serverSource); | ||||
158 | QVERIFY(spySourcesChanged.count() == 2 || spySourcesChanged.wait()); | ||||
159 | QCOMPARE(m_screencasting->sources().count(), 0); | ||||
160 | QVERIFY(!m_triggered); | ||||
161 | } | ||||
162 | | ||||
163 | void TestScreencastingInterface::testCreate() | ||||
164 | { | ||||
165 | QSignalSpy spySourcesChanged(m_screencasting, &KWayland::Client::Screencasting::sourcesChanged); | ||||
166 | | ||||
167 | QCOMPARE(m_screencasting->sources().count(), 0); | ||||
168 | m_screencastingInterface->addSource(createTestSource()); | ||||
169 | QVERIFY(spySourcesChanged.count() || spySourcesChanged.wait()); | ||||
170 | QCOMPARE(m_screencasting->sources().count(), 1); | ||||
171 | | ||||
172 | QVERIFY(!m_triggered); | ||||
173 | KWayland::Client::ScreencastingStream* stream = m_screencasting->create(m_screencasting->sources().constFirst()); | ||||
174 | QVERIFY(stream); | ||||
175 | | ||||
176 | QSignalSpy spyWorking(stream, &KWayland::Client::ScreencastingStream::created); | ||||
177 | QVERIFY(spyWorking.count() || spyWorking.wait()); | ||||
178 | QVERIFY(m_triggered); | ||||
179 | | ||||
180 | QSignalSpy spyStop(m_triggered, &KWayland::Server::ScreencastingStreamInterface::stop); | ||||
181 | stream->close(); | ||||
182 | QVERIFY(spyStop.count() || spyStop.wait()); | ||||
183 | } | ||||
184 | | ||||
185 | QTEST_GUILESS_MAIN(TestScreencastingInterface) | ||||
186 | #include "test_screencasting.moc" |