2014-08-26 14:07:39 +00:00
|
|
|
/********************************************************************
|
2014-09-17 13:57:56 +00:00
|
|
|
Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2014-09-17 13:57:56 +00:00
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) version 3, or any
|
|
|
|
later version accepted by the membership of KDE e.V. (or its
|
|
|
|
successor approved by the membership of KDE e.V.), which shall
|
|
|
|
act as a proxy defined in Section 6 of version 3 of the license.
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2014-09-17 13:57:56 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
2014-08-26 14:07:39 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2014-09-17 13:57:56 +00:00
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
2014-08-26 14:07:39 +00:00
|
|
|
|
2014-09-17 13:57:56 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2014-08-26 14:07:39 +00:00
|
|
|
*********************************************************************/
|
|
|
|
// Qt
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
// 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"
|
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
|
|
|
|
2014-09-17 14:10:38 +00:00
|
|
|
using namespace KWayland::Server;
|
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-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));
|
|
|
|
|
|
|
|
Display display;
|
|
|
|
QSignalSpy runningSpy(&display, SIGNAL(runningChanged(bool)));
|
|
|
|
QVERIFY(runningSpy.isValid());
|
|
|
|
display.setSocketName(testSocketName);
|
|
|
|
QVERIFY(!display.isRunning());
|
|
|
|
display.start();
|
|
|
|
// QVERIFY(runningSpy.wait());
|
|
|
|
QCOMPARE(runningSpy.count(), 1);
|
|
|
|
QVERIFY(runningSpy.first().first().toBool());
|
|
|
|
QVERIFY(display.isRunning());
|
|
|
|
QVERIFY(runtimeDir.exists(testSocketName));
|
|
|
|
|
|
|
|
display.terminate();
|
|
|
|
QVERIFY(!display.isRunning());
|
|
|
|
QCOMPARE(runningSpy.count(), 2);
|
|
|
|
QVERIFY(runningSpy.first().first().toBool());
|
|
|
|
QVERIFY(!runningSpy.last().first().toBool());
|
|
|
|
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();
|
|
|
|
QSignalSpy connectedSpy(&display, SIGNAL(clientConnected(KWayland::Server::ClientConnection*)));
|
|
|
|
QVERIFY(connectedSpy.isValid());
|
|
|
|
QSignalSpy disconnectedSpy(&display, SIGNAL(clientDisconnected(KWayland::Server::ClientConnection*)));
|
|
|
|
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);
|
|
|
|
QVERIFY(connection->userId() != 0);
|
|
|
|
QVERIFY(connection->groupId() != 0);
|
|
|
|
QVERIFY(connection->processId() != 0);
|
2014-11-17 15:21:54 +00:00
|
|
|
QCOMPARE(connection->display(), &display);
|
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);
|
2014-11-27 15:16:54 +00:00
|
|
|
wl_client_destroy(*client2);
|
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-09-23 10:00:17 +00:00
|
|
|
QTEST_GUILESS_MAIN(TestWaylandServerDisplay)
|
2014-08-26 14:07:39 +00:00
|
|
|
#include "test_display.moc"
|