2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2020-03-15 15:19:28 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2014-08-26 14:07:39 +00:00
|
|
|
// Qt
|
2018-11-06 06:22:36 +00:00
|
|
|
#include <QtTest>
|
2014-08-26 14:07:39 +00:00
|
|
|
// WaylandServer
|
2014-09-17 12:35:33 +00:00
|
|
|
#include "../../src/server/display.h"
|
2014-11-17 15:01:18 +00:00
|
|
|
#include "../../src/server/clientconnection.h"
|
2015-11-04 14:38:10 +00:00
|
|
|
#include "../../src/server/outputmanagement_interface.h"
|
2014-09-17 12:35:33 +00:00
|
|
|
#include "../../src/server/output_interface.h"
|
2014-11-17 15:01:18 +00:00
|
|
|
// Wayland
|
|
|
|
#include <wayland-server.h>
|
|
|
|
// system
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2020-04-29 14:56:38 +00:00
|
|
|
using namespace KWaylandServer;
|
2014-08-26 14:07:39 +00:00
|
|
|
|
|
|
|
class TestWaylandServerDisplay : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void testSocketName();
|
|
|
|
void testStartStop();
|
2014-09-03 18:01:54 +00:00
|
|
|
void testAddRemoveOutput();
|
2014-11-17 15:01:18 +00:00
|
|
|
void testClientConnection();
|
2014-11-27 15:42:16 +00:00
|
|
|
void testConnectNoSocket();
|
2015-11-04 14:38:10 +00:00
|
|
|
void testOutputManagement();
|
2018-11-23 13:42:18 +00:00
|
|
|
void testAutoSocketName();
|
2014-08-26 14:07:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void TestWaylandServerDisplay::testSocketName()
|
|
|
|
{
|
|
|
|
Display display;
|
|
|
|
QSignalSpy changedSpy(&display, SIGNAL(socketNameChanged(QString)));
|
|
|
|
QVERIFY(changedSpy.isValid());
|
|
|
|
QCOMPARE(display.socketName(), QStringLiteral("wayland-0"));
|
|
|
|
const QString testSName = QStringLiteral("fooBar");
|
|
|
|
display.setSocketName(testSName);
|
|
|
|
QCOMPARE(display.socketName(), testSName);
|
|
|
|
QCOMPARE(changedSpy.count(), 1);
|
|
|
|
QCOMPARE(changedSpy.first().first().toString(), testSName);
|
|
|
|
|
|
|
|
// changing to same name again should not emit signal
|
|
|
|
display.setSocketName(testSName);
|
|
|
|
QCOMPARE(changedSpy.count(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWaylandServerDisplay::testStartStop()
|
|
|
|
{
|
|
|
|
const QString testSocketName = QStringLiteral("kwin-wayland-server-display-test-0");
|
|
|
|
QDir runtimeDir(qgetenv("XDG_RUNTIME_DIR"));
|
|
|
|
QVERIFY(runtimeDir.exists());
|
|
|
|
QVERIFY(!runtimeDir.exists(testSocketName));
|
|
|
|
|
2020-07-06 09:36:25 +00:00
|
|
|
QScopedPointer<Display> display(new Display);
|
|
|
|
QSignalSpy runningSpy(display.data(), SIGNAL(runningChanged(bool)));
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(runningSpy.isValid());
|
2020-07-06 09:36:25 +00:00
|
|
|
display->setSocketName(testSocketName);
|
|
|
|
QVERIFY(!display->isRunning());
|
|
|
|
display->start();
|
2014-08-26 14:07:39 +00:00
|
|
|
// QVERIFY(runningSpy.wait());
|
|
|
|
QCOMPARE(runningSpy.count(), 1);
|
|
|
|
QVERIFY(runningSpy.first().first().toBool());
|
2020-07-06 09:36:25 +00:00
|
|
|
QVERIFY(display->isRunning());
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(runtimeDir.exists(testSocketName));
|
|
|
|
|
2020-07-06 09:36:25 +00:00
|
|
|
display.reset();
|
2014-08-26 14:07:39 +00:00
|
|
|
QVERIFY(!runtimeDir.exists(testSocketName));
|
|
|
|
}
|
|
|
|
|
2014-09-03 18:01:54 +00:00
|
|
|
void TestWaylandServerDisplay::testAddRemoveOutput()
|
|
|
|
{
|
|
|
|
Display display;
|
|
|
|
display.setSocketName(QStringLiteral("kwin-wayland-server-display-test-output-0"));
|
|
|
|
display.start();
|
|
|
|
|
|
|
|
OutputInterface *output = display.createOutput();
|
|
|
|
QCOMPARE(display.outputs().size(), 1);
|
|
|
|
QCOMPARE(display.outputs().first(), output);
|
|
|
|
// create a second output
|
|
|
|
OutputInterface *output2 = display.createOutput();
|
|
|
|
QCOMPARE(display.outputs().size(), 2);
|
|
|
|
QCOMPARE(display.outputs().first(), output);
|
|
|
|
QCOMPARE(display.outputs().last(), output2);
|
|
|
|
// remove the first output
|
|
|
|
display.removeOutput(output);
|
|
|
|
QCOMPARE(display.outputs().size(), 1);
|
|
|
|
QCOMPARE(display.outputs().first(), output2);
|
|
|
|
// and delete the second
|
|
|
|
delete output2;
|
|
|
|
QVERIFY(display.outputs().isEmpty());
|
|
|
|
}
|
|
|
|
|
2014-11-17 15:01:18 +00:00
|
|
|
void TestWaylandServerDisplay::testClientConnection()
|
|
|
|
{
|
|
|
|
Display display;
|
|
|
|
display.setSocketName(QStringLiteral("kwin-wayland-server-display-test-client-connection"));
|
|
|
|
display.start();
|
2020-04-29 14:56:38 +00:00
|
|
|
QSignalSpy connectedSpy(&display, SIGNAL(clientConnected(KWaylandServer::ClientConnection*)));
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connectedSpy.isValid());
|
2020-04-29 14:56:38 +00:00
|
|
|
QSignalSpy disconnectedSpy(&display, SIGNAL(clientDisconnected(KWaylandServer::ClientConnection*)));
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(disconnectedSpy.isValid());
|
|
|
|
|
|
|
|
int sv[2];
|
|
|
|
QVERIFY(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) >= 0);
|
|
|
|
|
|
|
|
auto client = wl_client_create(display, sv[0]);
|
|
|
|
QVERIFY(client);
|
|
|
|
|
|
|
|
QVERIFY(connectedSpy.isEmpty());
|
2014-11-17 17:13:28 +00:00
|
|
|
QVERIFY(display.connections().isEmpty());
|
2014-11-17 15:01:18 +00:00
|
|
|
ClientConnection *connection = display.getConnection(client);
|
|
|
|
QVERIFY(connection);
|
|
|
|
QCOMPARE(connection->client(), client);
|
2017-07-01 15:01:18 +00:00
|
|
|
if (getuid() == 0) {
|
|
|
|
QEXPECT_FAIL("", "Please don't run test as root", Continue);
|
|
|
|
}
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connection->userId() != 0);
|
2017-07-01 15:01:18 +00:00
|
|
|
if (getgid() == 0) {
|
|
|
|
QEXPECT_FAIL("", "Please don't run test as root", Continue);
|
|
|
|
}
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connection->groupId() != 0);
|
|
|
|
QVERIFY(connection->processId() != 0);
|
2014-11-17 15:21:54 +00:00
|
|
|
QCOMPARE(connection->display(), &display);
|
2017-07-29 06:40:11 +00:00
|
|
|
QCOMPARE(connection->executablePath(), QCoreApplication::applicationFilePath());
|
2014-11-17 15:01:18 +00:00
|
|
|
QCOMPARE((wl_client*)*connection, client);
|
|
|
|
const ClientConnection &constRef = *connection;
|
|
|
|
QCOMPARE((wl_client*)constRef, client);
|
|
|
|
QCOMPARE(connectedSpy.count(), 1);
|
|
|
|
QCOMPARE(connectedSpy.first().first().value<ClientConnection*>(), connection);
|
2014-11-17 17:13:28 +00:00
|
|
|
QCOMPARE(display.connections().count(), 1);
|
|
|
|
QCOMPARE(display.connections().first(), connection);
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
QCOMPARE(connection, display.getConnection(client));
|
|
|
|
QCOMPARE(connectedSpy.count(), 1);
|
|
|
|
|
|
|
|
// create a second client
|
|
|
|
int sv2[2];
|
|
|
|
QVERIFY(socketpair(AF_UNIX, SOCK_STREAM, 0, sv2) >= 0);
|
2014-11-27 15:16:54 +00:00
|
|
|
auto client2 = display.createClient(sv2[0]);
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(client2);
|
2014-11-27 15:16:54 +00:00
|
|
|
ClientConnection *connection2 = display.getConnection(client2->client());
|
2014-11-17 15:01:18 +00:00
|
|
|
QVERIFY(connection2);
|
2014-11-27 15:16:54 +00:00
|
|
|
QCOMPARE(connection2, client2);
|
2014-11-17 15:01:18 +00:00
|
|
|
QCOMPARE(connectedSpy.count(), 2);
|
|
|
|
QCOMPARE(connectedSpy.first().first().value<ClientConnection*>(), connection);
|
|
|
|
QCOMPARE(connectedSpy.last().first().value<ClientConnection*>(), connection2);
|
2014-11-27 15:16:54 +00:00
|
|
|
QCOMPARE(connectedSpy.last().first().value<ClientConnection*>(), client2);
|
2014-11-17 17:13:28 +00:00
|
|
|
QCOMPARE(display.connections().count(), 2);
|
|
|
|
QCOMPARE(display.connections().first(), connection);
|
|
|
|
QCOMPARE(display.connections().last(), connection2);
|
2014-11-27 15:16:54 +00:00
|
|
|
QCOMPARE(display.connections().last(), client2);
|
2014-11-17 15:01:18 +00:00
|
|
|
|
|
|
|
// and destroy
|
|
|
|
QVERIFY(disconnectedSpy.isEmpty());
|
|
|
|
wl_client_destroy(client);
|
|
|
|
QCOMPARE(disconnectedSpy.count(), 1);
|
2016-10-10 11:41:42 +00:00
|
|
|
QSignalSpy clientDestroyedSpy(client2, &QObject::destroyed);
|
|
|
|
QVERIFY(clientDestroyedSpy.isValid());
|
2015-11-10 12:27:27 +00:00
|
|
|
client2->destroy();
|
2016-10-10 11:41:42 +00:00
|
|
|
QVERIFY(clientDestroyedSpy.wait());
|
2014-11-17 15:01:18 +00:00
|
|
|
QCOMPARE(disconnectedSpy.count(), 2);
|
|
|
|
close(sv[0]);
|
|
|
|
close(sv[1]);
|
|
|
|
close(sv2[0]);
|
|
|
|
close(sv2[1]);
|
2014-11-17 17:13:28 +00:00
|
|
|
QVERIFY(display.connections().isEmpty());
|
2014-11-17 15:01:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:42:16 +00:00
|
|
|
void TestWaylandServerDisplay::testConnectNoSocket()
|
|
|
|
{
|
|
|
|
Display display;
|
|
|
|
display.start(Display::StartMode::ConnectClientsOnly);
|
|
|
|
QVERIFY(display.isRunning());
|
|
|
|
|
|
|
|
// let's try connecting a client
|
|
|
|
int sv[2];
|
|
|
|
QVERIFY(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) >= 0);
|
|
|
|
auto client = display.createClient(sv[0]);
|
|
|
|
QVERIFY(client);
|
|
|
|
|
|
|
|
wl_client_destroy(client->client());
|
|
|
|
close(sv[0]);
|
|
|
|
close(sv[1]);
|
|
|
|
}
|
|
|
|
|
2015-11-04 14:38:10 +00:00
|
|
|
void TestWaylandServerDisplay::testOutputManagement()
|
|
|
|
{
|
2018-06-15 11:23:10 +00:00
|
|
|
Display display;
|
|
|
|
display.setSocketName("kwayland-test-0");
|
|
|
|
display.start();
|
|
|
|
auto kwin = display.createOutputManagement(this);
|
2015-11-04 14:38:10 +00:00
|
|
|
kwin->create();
|
|
|
|
QVERIFY(kwin->isValid());
|
|
|
|
}
|
|
|
|
|
2018-11-23 13:42:18 +00:00
|
|
|
void TestWaylandServerDisplay::testAutoSocketName()
|
|
|
|
{
|
|
|
|
QTemporaryDir runtimeDir;
|
|
|
|
QVERIFY(runtimeDir.isValid());
|
|
|
|
QVERIFY(qputenv("XDG_RUNTIME_DIR", runtimeDir.path().toUtf8()));
|
|
|
|
|
|
|
|
Display display0;
|
|
|
|
display0.setAutomaticSocketNaming(true);
|
|
|
|
QSignalSpy socketNameChangedSpy0(&display0, SIGNAL(socketNameChanged(QString)));
|
|
|
|
display0.start();
|
|
|
|
QVERIFY(display0.isRunning());
|
|
|
|
QCOMPARE(socketNameChangedSpy0.count(), 0);
|
|
|
|
QCOMPARE(display0.socketName(), QStringLiteral("wayland-0"));
|
|
|
|
|
|
|
|
Display display1;
|
|
|
|
display1.setAutomaticSocketNaming(true);
|
|
|
|
QSignalSpy socketNameChangedSpy1(&display1, SIGNAL(socketNameChanged(QString)));
|
|
|
|
display1.start();
|
|
|
|
QVERIFY(display1.isRunning());
|
|
|
|
QCOMPARE(socketNameChangedSpy1.count(), 1);
|
|
|
|
QCOMPARE(display1.socketName(), QStringLiteral("wayland-1"));
|
|
|
|
}
|
|
|
|
|
2015-11-04 14:38:10 +00:00
|
|
|
|
2014-09-23 10:00:17 +00:00
|
|
|
QTEST_GUILESS_MAIN(TestWaylandServerDisplay)
|
2014-08-26 14:07:39 +00:00
|
|
|
#include "test_display.moc"
|