2020-05-12 11:26:57 +00:00
|
|
|
/********************************************************************
|
|
|
|
Copyright 2020 David Edmundson <davidedmundson@kde.org>
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
// Qt
|
|
|
|
#include <QHash>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
// WaylandServer
|
|
|
|
#include "../../src/server/compositor_interface.h"
|
|
|
|
#include "../../src/server/display.h"
|
|
|
|
#include "../../src/server/seat_interface.h"
|
2020-05-28 07:25:56 +00:00
|
|
|
#include "../../src/server/datacontroldevice_v1_interface.h"
|
|
|
|
#include "../../src/server/datacontroldevicemanager_v1_interface.h"
|
|
|
|
#include "../../src/server/datacontrolsource_v1_interface.h"
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
#include <KWayland/Client/compositor.h>
|
|
|
|
#include <KWayland/Client/connection_thread.h>
|
|
|
|
#include <KWayland/Client/event_queue.h>
|
|
|
|
#include <KWayland/Client/registry.h>
|
|
|
|
#include <KWayland/Client/seat.h>
|
|
|
|
|
|
|
|
#include "qwayland-wlr-data-control-unstable-v1.h"
|
|
|
|
|
|
|
|
using namespace KWaylandServer;
|
|
|
|
|
|
|
|
// Faux-client API for tests
|
|
|
|
|
2020-10-01 06:47:42 +00:00
|
|
|
Q_DECLARE_OPAQUE_POINTER(::zwlr_data_control_offer_v1*)
|
|
|
|
Q_DECLARE_METATYPE(::zwlr_data_control_offer_v1*)
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
class DataControlDeviceManager : public QObject, public QtWayland::zwlr_data_control_manager_v1
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataControlOffer: public QObject, public QtWayland::zwlr_data_control_offer_v1
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2020-08-04 10:34:57 +00:00
|
|
|
~DataControlOffer() {
|
|
|
|
destroy();
|
|
|
|
}
|
2020-05-12 11:26:57 +00:00
|
|
|
QStringList receivedOffers() {
|
|
|
|
return m_receivedOffers;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
virtual void zwlr_data_control_offer_v1_offer(const QString &mime_type) override {
|
|
|
|
m_receivedOffers << mime_type;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
QStringList m_receivedOffers;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataControlDevice : public QObject, public QtWayland::zwlr_data_control_device_v1
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-08-04 10:34:57 +00:00
|
|
|
public:
|
|
|
|
~DataControlDevice() {
|
|
|
|
destroy();
|
|
|
|
}
|
2020-05-12 11:26:57 +00:00
|
|
|
Q_SIGNALS:
|
|
|
|
void dataControlOffer(DataControlOffer *offer); //our event receives a new ID, so we make a new object
|
|
|
|
void selection(struct ::zwlr_data_control_offer_v1 *id);
|
|
|
|
protected:
|
|
|
|
void zwlr_data_control_device_v1_data_offer(struct ::zwlr_data_control_offer_v1 *id) override {
|
|
|
|
auto offer = new DataControlOffer;
|
|
|
|
offer->init(id);
|
|
|
|
Q_EMIT dataControlOffer(offer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void zwlr_data_control_device_v1_selection(struct ::zwlr_data_control_offer_v1 *id) override {
|
|
|
|
Q_EMIT selection(id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataControlSource: public QObject, public QtWayland::zwlr_data_control_source_v1
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-08-04 10:34:57 +00:00
|
|
|
public:
|
|
|
|
~DataControlSource() {
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
public:
|
2020-05-12 11:26:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TestDataSource : public AbstractDataSource
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
TestDataSource() :
|
|
|
|
AbstractDataSource(nullptr)
|
|
|
|
{}
|
2020-08-04 10:34:57 +00:00
|
|
|
~TestDataSource() {
|
2020-06-25 15:27:52 +00:00
|
|
|
emit aboutToBeDestroyed();
|
2020-08-04 10:34:57 +00:00
|
|
|
}
|
2020-10-01 06:47:42 +00:00
|
|
|
void requestData(const QString &mimeType, qint32 fd) override {
|
2020-05-12 11:26:57 +00:00
|
|
|
Q_UNUSED(mimeType);
|
|
|
|
Q_UNUSED(fd);
|
|
|
|
};
|
2020-10-01 06:47:42 +00:00
|
|
|
void cancel() override {};
|
|
|
|
QStringList mimeTypes() const override {
|
2020-05-12 11:26:57 +00:00
|
|
|
return {"text/test1", "text/test2"};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// The test itself
|
|
|
|
|
|
|
|
class DataControlInterfaceTest : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
DataControlInterfaceTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~DataControlInterfaceTest() override;
|
|
|
|
|
|
|
|
private Q_SLOTS:
|
2020-08-04 10:34:57 +00:00
|
|
|
void init();
|
|
|
|
void cleanup();
|
2020-05-12 11:26:57 +00:00
|
|
|
void testCopyToControl();
|
|
|
|
void testCopyFromControl();
|
2020-08-04 11:58:43 +00:00
|
|
|
void testKlipperCase();
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
KWayland::Client::ConnectionThread *m_connection;
|
|
|
|
KWayland::Client::EventQueue *m_queue;
|
|
|
|
KWayland::Client::Compositor *m_clientCompositor;
|
|
|
|
KWayland::Client::Seat *m_clientSeat = nullptr;
|
|
|
|
|
|
|
|
QThread *m_thread;
|
2020-08-04 10:34:57 +00:00
|
|
|
Display *m_display;
|
2020-05-12 11:26:57 +00:00
|
|
|
SeatInterface *m_seat;
|
|
|
|
CompositorInterface *m_serverCompositor;
|
|
|
|
|
2020-05-28 07:25:56 +00:00
|
|
|
DataControlDeviceManagerV1Interface *m_dataControlDeviceManagerInterface;
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
DataControlDeviceManager *m_dataControlDeviceManager;
|
|
|
|
|
|
|
|
QVector<SurfaceInterface *> m_surfaces;
|
|
|
|
};
|
|
|
|
|
|
|
|
DataControlInterfaceTest::~DataControlInterfaceTest()
|
|
|
|
{
|
|
|
|
if (m_queue) {
|
|
|
|
delete m_queue;
|
|
|
|
m_queue = nullptr;
|
|
|
|
}
|
|
|
|
if (m_thread) {
|
|
|
|
m_thread->quit();
|
|
|
|
m_thread->wait();
|
|
|
|
delete m_thread;
|
|
|
|
m_thread = nullptr;
|
|
|
|
}
|
|
|
|
delete m_seat;
|
|
|
|
m_connection->deleteLater();
|
|
|
|
m_connection = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-04 10:34:57 +00:00
|
|
|
static const QString s_socketName = QStringLiteral("kwin-wayland-datacontrol-test-0");
|
2020-05-12 11:26:57 +00:00
|
|
|
|
2020-08-04 10:34:57 +00:00
|
|
|
void DataControlInterfaceTest::init()
|
2020-05-12 11:26:57 +00:00
|
|
|
{
|
2020-08-04 10:34:57 +00:00
|
|
|
m_display = new Display;
|
|
|
|
m_display->setSocketName(s_socketName);
|
|
|
|
m_display->start();
|
|
|
|
QVERIFY(m_display->isRunning());
|
2020-05-12 11:26:57 +00:00
|
|
|
|
2020-08-04 10:34:57 +00:00
|
|
|
m_seat = m_display->createSeat(this);
|
2020-05-12 11:26:57 +00:00
|
|
|
m_seat->create();
|
2020-08-04 10:34:57 +00:00
|
|
|
m_serverCompositor = m_display->createCompositor(this);
|
|
|
|
m_dataControlDeviceManagerInterface = m_display->createDataControlDeviceManagerV1(this);
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
// setup connection
|
|
|
|
m_connection = new KWayland::Client::ConnectionThread;
|
|
|
|
QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
|
|
|
|
m_connection->setSocketName(s_socketName);
|
|
|
|
|
|
|
|
m_thread = new QThread(this);
|
|
|
|
m_connection->moveToThread(m_thread);
|
|
|
|
m_thread->start();
|
|
|
|
|
|
|
|
m_connection->initConnection();
|
|
|
|
QVERIFY(connectedSpy.wait());
|
|
|
|
QVERIFY(!m_connection->connections().isEmpty());
|
|
|
|
|
|
|
|
m_queue = new KWayland::Client::EventQueue(this);
|
|
|
|
QVERIFY(!m_queue->isValid());
|
|
|
|
m_queue->setup(m_connection);
|
|
|
|
QVERIFY(m_queue->isValid());
|
|
|
|
|
|
|
|
auto registry = new KWayland::Client::Registry(this);
|
|
|
|
connect(registry, &KWayland::Client::Registry::interfaceAnnounced, this, [this, registry](const QByteArray &interface, quint32 name, quint32 version) {
|
|
|
|
if (interface == "zwlr_data_control_manager_v1") {
|
|
|
|
m_dataControlDeviceManager = new DataControlDeviceManager;
|
|
|
|
m_dataControlDeviceManager->init(registry->registry(), name, version);
|
|
|
|
m_dataControlDeviceManager->setParent(registry);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(registry, &KWayland::Client::Registry::seatAnnounced, this, [this, registry](quint32 name, quint32 version) {
|
|
|
|
m_clientSeat = registry->createSeat(name, version);
|
|
|
|
});
|
|
|
|
registry->setEventQueue(m_queue);
|
|
|
|
QSignalSpy compositorSpy(registry, &KWayland::Client::Registry::compositorAnnounced);
|
|
|
|
registry->create(m_connection->display());
|
|
|
|
QVERIFY(registry->isValid());
|
|
|
|
registry->setup();
|
|
|
|
wl_display_flush(m_connection->display());
|
|
|
|
|
|
|
|
QVERIFY(compositorSpy.wait());
|
|
|
|
m_clientCompositor = registry->createCompositor(compositorSpy.first().first().value<quint32>(), compositorSpy.first().last().value<quint32>(), this);
|
|
|
|
QVERIFY(m_clientCompositor->isValid());
|
|
|
|
|
|
|
|
QVERIFY(m_dataControlDeviceManager);
|
|
|
|
}
|
|
|
|
|
2020-08-04 10:34:57 +00:00
|
|
|
void DataControlInterfaceTest::cleanup()
|
|
|
|
{
|
|
|
|
#define CLEANUP(variable) \
|
|
|
|
if (variable) { \
|
|
|
|
delete variable; \
|
|
|
|
variable = nullptr; \
|
|
|
|
}
|
|
|
|
CLEANUP(m_dataControlDeviceManager)
|
|
|
|
CLEANUP(m_queue)
|
|
|
|
if (m_connection) {
|
|
|
|
m_connection->deleteLater();
|
|
|
|
m_connection = nullptr;
|
|
|
|
}
|
|
|
|
if (m_thread) {
|
|
|
|
m_thread->quit();
|
|
|
|
m_thread->wait();
|
|
|
|
delete m_thread;
|
|
|
|
m_thread = nullptr;
|
|
|
|
}
|
|
|
|
CLEANUP(m_display)
|
|
|
|
#undef CLEANUP
|
|
|
|
}
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
void DataControlInterfaceTest::testCopyToControl()
|
|
|
|
{
|
|
|
|
// we set a dummy data source on the seat using abstract client directly
|
|
|
|
// then confirm we receive the offer despite not having a surface
|
|
|
|
|
|
|
|
QScopedPointer<DataControlDevice> dataControlDevice(new DataControlDevice);
|
|
|
|
dataControlDevice->init(m_dataControlDeviceManager->get_data_device(*m_clientSeat));
|
|
|
|
|
|
|
|
QSignalSpy newOfferSpy(dataControlDevice.data(), &DataControlDevice::dataControlOffer);
|
|
|
|
QSignalSpy selectionSpy(dataControlDevice.data(), &DataControlDevice::selection);
|
|
|
|
|
2020-08-04 10:34:57 +00:00
|
|
|
QScopedPointer<TestDataSource> testSelection(new TestDataSource);
|
|
|
|
m_seat->setSelection(testSelection.data());
|
2020-05-12 11:26:57 +00:00
|
|
|
|
|
|
|
// selection will be sent after we've been sent a new offer object and the mimes have been sent to that object
|
|
|
|
selectionSpy.wait();
|
|
|
|
|
|
|
|
QCOMPARE(newOfferSpy.count(), 1);
|
|
|
|
QScopedPointer<DataControlOffer> offer(newOfferSpy.first().first().value<DataControlOffer*>());
|
|
|
|
QCOMPARE(selectionSpy.first().first().value<struct ::zwlr_data_control_offer_v1*>(), offer->object());
|
|
|
|
|
|
|
|
QCOMPARE(offer->receivedOffers().count(), 2);
|
|
|
|
QCOMPARE(offer->receivedOffers()[0], "text/test1");
|
|
|
|
QCOMPARE(offer->receivedOffers()[1], "text/test2");
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataControlInterfaceTest::testCopyFromControl()
|
|
|
|
{
|
|
|
|
// we create a data device and set a selection
|
|
|
|
// then confirm the server sees the new selection
|
|
|
|
QSignalSpy serverSelectionChangedSpy(m_seat, &SeatInterface::selectionChanged);
|
|
|
|
|
|
|
|
QScopedPointer<DataControlDevice> dataControlDevice(new DataControlDevice);
|
|
|
|
dataControlDevice->init(m_dataControlDeviceManager->get_data_device(*m_clientSeat));
|
|
|
|
|
|
|
|
QScopedPointer<DataControlSource> source(new DataControlSource);
|
|
|
|
source->init(m_dataControlDeviceManager->create_data_source());
|
|
|
|
source->offer("cheese/test1");
|
|
|
|
source->offer("cheese/test2");
|
|
|
|
|
|
|
|
dataControlDevice->set_selection(source->object());
|
|
|
|
|
|
|
|
serverSelectionChangedSpy.wait();
|
|
|
|
QVERIFY(m_seat->selection());
|
|
|
|
QCOMPARE(m_seat->selection()->mimeTypes(), QStringList({"cheese/test1", "cheese/test2"}));
|
2020-08-04 10:34:57 +00:00
|
|
|
}
|
2020-08-04 11:58:43 +00:00
|
|
|
|
|
|
|
void DataControlInterfaceTest::testKlipperCase()
|
|
|
|
{
|
|
|
|
// This tests the setup of klipper's real world operation and a race with a common pattern seen between clients and klipper
|
|
|
|
// The client's behaviour is faked with direct access to the seat
|
|
|
|
|
|
|
|
QScopedPointer<DataControlDevice> dataControlDevice(new DataControlDevice);
|
|
|
|
dataControlDevice->init(m_dataControlDeviceManager->get_data_device(*m_clientSeat));
|
|
|
|
|
|
|
|
QSignalSpy newOfferSpy(dataControlDevice.data(), &DataControlDevice::dataControlOffer);
|
|
|
|
QSignalSpy selectionSpy(dataControlDevice.data(), &DataControlDevice::selection);
|
|
|
|
QSignalSpy serverSelectionChangedSpy(m_seat, &SeatInterface::selectionChanged);
|
|
|
|
|
|
|
|
// Client A has a data source
|
|
|
|
QScopedPointer<TestDataSource> testSelection(new TestDataSource);
|
|
|
|
m_seat->setSelection(testSelection.data());
|
|
|
|
|
|
|
|
// klipper gets it
|
|
|
|
selectionSpy.wait();
|
|
|
|
|
|
|
|
// Client A deletes it
|
|
|
|
testSelection.reset();
|
|
|
|
|
|
|
|
//klipper gets told
|
|
|
|
selectionSpy.wait();
|
|
|
|
|
|
|
|
// Client A sets something else
|
|
|
|
QScopedPointer<TestDataSource> testSelection2(new TestDataSource);
|
|
|
|
m_seat->setSelection(testSelection2.data());
|
|
|
|
|
|
|
|
// Meanwhile klipper updates with the old content
|
|
|
|
QScopedPointer<DataControlSource> source(new DataControlSource);
|
|
|
|
source->init(m_dataControlDeviceManager->create_data_source());
|
|
|
|
source->offer("fromKlipper/test1");
|
|
|
|
source->offer("application/x-kde-onlyReplaceEmpty");
|
|
|
|
|
|
|
|
dataControlDevice->set_selection(source->object());
|
|
|
|
|
|
|
|
QVERIFY(!serverSelectionChangedSpy.wait(10));
|
|
|
|
QCOMPARE(m_seat->selection(), testSelection2.data());
|
2020-05-12 11:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(DataControlInterfaceTest)
|
|
|
|
|
|
|
|
#include "test_datacontrol_interface.moc"
|