2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-11-04 14:10:22 +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-11-04 14:10:22 +00:00
|
|
|
// Qt
|
|
|
|
#include <QMimeDatabase>
|
2023-07-03 19:28:19 +00:00
|
|
|
#include <QSignalSpy>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QTest>
|
2022-04-22 09:27:33 +00:00
|
|
|
|
2023-09-13 05:52:59 +00:00
|
|
|
#include "wayland/datadevicemanager.h"
|
|
|
|
#include "wayland/datasource.h"
|
2022-04-22 09:27:33 +00:00
|
|
|
#include "wayland/display.h"
|
|
|
|
|
2014-11-04 14:10:22 +00:00
|
|
|
// KWayland
|
2020-04-29 13:59:23 +00:00
|
|
|
#include "KWayland/Client/connection_thread.h"
|
|
|
|
#include "KWayland/Client/datadevicemanager.h"
|
|
|
|
#include "KWayland/Client/datasource.h"
|
2021-08-29 05:11:06 +00:00
|
|
|
#include "KWayland/Client/event_queue.h"
|
2020-04-29 13:59:23 +00:00
|
|
|
#include "KWayland/Client/registry.h"
|
2022-04-22 09:27:33 +00:00
|
|
|
|
2014-11-04 14:10:22 +00:00
|
|
|
// Wayland
|
|
|
|
#include <wayland-client.h>
|
|
|
|
|
|
|
|
class TestDataSource : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private Q_SLOTS:
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
void testOffer();
|
|
|
|
void testTargetAccepts_data();
|
|
|
|
void testTargetAccepts();
|
|
|
|
void testRequestSend();
|
|
|
|
void testCancel();
|
2014-11-05 14:22:15 +00:00
|
|
|
void testServerGet();
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
private:
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::Display *m_display = nullptr;
|
|
|
|
KWin::DataDeviceManagerInterface *m_dataDeviceManagerInterface = nullptr;
|
2014-11-04 14:10:22 +00:00
|
|
|
KWayland::Client::ConnectionThread *m_connection = nullptr;
|
|
|
|
KWayland::Client::DataDeviceManager *m_dataDeviceManager = nullptr;
|
|
|
|
KWayland::Client::EventQueue *m_queue = nullptr;
|
|
|
|
QThread *m_thread = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const QString s_socketName = QStringLiteral("kwayland-test-wayland-datasource-0");
|
|
|
|
|
|
|
|
void TestDataSource::init()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-04 14:10:22 +00:00
|
|
|
delete m_display;
|
2023-09-13 17:59:29 +00:00
|
|
|
m_display = new KWin::Display(this);
|
2020-10-19 15:52:56 +00:00
|
|
|
m_display->addSocketName(s_socketName);
|
2014-11-04 14:10:22 +00:00
|
|
|
m_display->start();
|
|
|
|
QVERIFY(m_display->isRunning());
|
|
|
|
|
|
|
|
// setup connection
|
|
|
|
m_connection = new KWayland::Client::ConnectionThread;
|
2021-02-25 13:48:11 +00:00
|
|
|
QSignalSpy connectedSpy(m_connection, &KWayland::Client::ConnectionThread::connected);
|
2014-11-04 14:10:22 +00:00
|
|
|
m_connection->setSocketName(s_socketName);
|
|
|
|
|
|
|
|
m_thread = new QThread(this);
|
|
|
|
m_connection->moveToThread(m_thread);
|
|
|
|
m_thread->start();
|
|
|
|
|
|
|
|
m_connection->initConnection();
|
|
|
|
QVERIFY(connectedSpy.wait());
|
|
|
|
|
|
|
|
m_queue = new KWayland::Client::EventQueue(this);
|
|
|
|
QVERIFY(!m_queue->isValid());
|
|
|
|
m_queue->setup(m_connection);
|
|
|
|
QVERIFY(m_queue->isValid());
|
|
|
|
|
|
|
|
KWayland::Client::Registry registry;
|
2021-02-25 13:48:11 +00:00
|
|
|
QSignalSpy dataDeviceManagerSpy(®istry, &KWayland::Client::Registry::dataDeviceManagerAnnounced);
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(!registry.eventQueue());
|
|
|
|
registry.setEventQueue(m_queue);
|
|
|
|
QCOMPARE(registry.eventQueue(), m_queue);
|
|
|
|
registry.create(m_connection->display());
|
|
|
|
QVERIFY(registry.isValid());
|
|
|
|
registry.setup();
|
|
|
|
|
2020-12-09 20:13:19 +00:00
|
|
|
m_dataDeviceManagerInterface = new DataDeviceManagerInterface(m_display, m_display);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
QVERIFY(dataDeviceManagerSpy.wait());
|
2021-08-29 05:11:06 +00:00
|
|
|
m_dataDeviceManager =
|
|
|
|
registry.createDataDeviceManager(dataDeviceManagerSpy.first().first().value<quint32>(), dataDeviceManagerSpy.first().last().value<quint32>(), this);
|
2014-11-04 14:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestDataSource::cleanup()
|
|
|
|
{
|
|
|
|
if (m_dataDeviceManager) {
|
|
|
|
delete m_dataDeviceManager;
|
|
|
|
m_dataDeviceManager = nullptr;
|
|
|
|
}
|
|
|
|
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_connection;
|
|
|
|
m_connection = nullptr;
|
|
|
|
|
|
|
|
delete m_display;
|
|
|
|
m_display = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestDataSource::testOffer()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
qRegisterMetaType<KWin::DataSourceInterface *>();
|
|
|
|
QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, &KWin::DataDeviceManagerInterface::dataSourceCreated);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::DataSource> dataSource(m_dataDeviceManager->createDataSource());
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(dataSource->isValid());
|
|
|
|
|
|
|
|
QVERIFY(dataSourceCreatedSpy.wait());
|
|
|
|
QCOMPARE(dataSourceCreatedSpy.count(), 1);
|
|
|
|
|
2021-08-29 05:11:06 +00:00
|
|
|
QPointer<DataSourceInterface> serverDataSource = dataSourceCreatedSpy.first().first().value<DataSourceInterface *>();
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(!serverDataSource.isNull());
|
|
|
|
QCOMPARE(serverDataSource->mimeTypes().count(), 0);
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
QSignalSpy offeredSpy(serverDataSource.data(), &KWin::AbstractDataSource::mimeTypeOffered);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
const QString plain = QStringLiteral("text/plain");
|
|
|
|
QMimeDatabase db;
|
|
|
|
dataSource->offer(db.mimeTypeForName(plain));
|
|
|
|
|
|
|
|
QVERIFY(offeredSpy.wait());
|
|
|
|
QCOMPARE(offeredSpy.count(), 1);
|
|
|
|
QCOMPARE(offeredSpy.last().first().toString(), plain);
|
|
|
|
QCOMPARE(serverDataSource->mimeTypes().count(), 1);
|
|
|
|
QCOMPARE(serverDataSource->mimeTypes().first(), plain);
|
|
|
|
|
|
|
|
const QString html = QStringLiteral("text/html");
|
|
|
|
dataSource->offer(db.mimeTypeForName(html));
|
|
|
|
|
|
|
|
QVERIFY(offeredSpy.wait());
|
|
|
|
QCOMPARE(offeredSpy.count(), 2);
|
|
|
|
QCOMPARE(offeredSpy.first().first().toString(), plain);
|
|
|
|
QCOMPARE(offeredSpy.last().first().toString(), html);
|
|
|
|
QCOMPARE(serverDataSource->mimeTypes().count(), 2);
|
|
|
|
QCOMPARE(serverDataSource->mimeTypes().first(), plain);
|
|
|
|
QCOMPARE(serverDataSource->mimeTypes().last(), html);
|
|
|
|
|
|
|
|
// try destroying the client side, should trigger a destroy of server side
|
|
|
|
dataSource.reset();
|
|
|
|
QVERIFY(!serverDataSource.isNull());
|
|
|
|
wl_display_flush(m_connection->display());
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
QVERIFY(serverDataSource.isNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestDataSource::testTargetAccepts_data()
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>("mimeType");
|
|
|
|
|
|
|
|
QTest::newRow("empty") << QString();
|
|
|
|
QTest::newRow("text/plain") << QStringLiteral("text/plain");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestDataSource::testTargetAccepts()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, &KWin::DataDeviceManagerInterface::dataSourceCreated);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::DataSource> dataSource(m_dataDeviceManager->createDataSource());
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(dataSource->isValid());
|
|
|
|
|
2022-08-01 21:29:02 +00:00
|
|
|
QSignalSpy targetAcceptsSpy(dataSource.get(), &KWayland::Client::DataSource::targetAccepts);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
QVERIFY(dataSourceCreatedSpy.wait());
|
|
|
|
QCOMPARE(dataSourceCreatedSpy.count(), 1);
|
|
|
|
|
|
|
|
QFETCH(QString, mimeType);
|
2021-08-29 05:11:06 +00:00
|
|
|
dataSourceCreatedSpy.first().first().value<DataSourceInterface *>()->accept(mimeType);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
QVERIFY(targetAcceptsSpy.wait());
|
|
|
|
QCOMPARE(targetAcceptsSpy.count(), 1);
|
|
|
|
QCOMPARE(targetAcceptsSpy.first().first().toString(), mimeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestDataSource::testRequestSend()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, &KWin::DataDeviceManagerInterface::dataSourceCreated);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::DataSource> dataSource(m_dataDeviceManager->createDataSource());
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(dataSource->isValid());
|
2022-08-01 21:29:02 +00:00
|
|
|
QSignalSpy sendRequestedSpy(dataSource.get(), &KWayland::Client::DataSource::sendDataRequested);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
const QString plain = QStringLiteral("text/plain");
|
|
|
|
QVERIFY(dataSourceCreatedSpy.wait());
|
|
|
|
QCOMPARE(dataSourceCreatedSpy.count(), 1);
|
|
|
|
QTemporaryFile file;
|
|
|
|
QVERIFY(file.open());
|
2021-08-29 05:11:06 +00:00
|
|
|
dataSourceCreatedSpy.first().first().value<DataSourceInterface *>()->requestData(plain, file.handle());
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
QVERIFY(sendRequestedSpy.wait());
|
|
|
|
QCOMPARE(sendRequestedSpy.count(), 1);
|
|
|
|
QCOMPARE(sendRequestedSpy.first().first().toString(), plain);
|
2014-11-27 12:38:24 +00:00
|
|
|
QCOMPARE(sendRequestedSpy.first().last().value<qint32>(), file.handle());
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(sendRequestedSpy.first().last().value<qint32>() != -1);
|
|
|
|
|
|
|
|
QFile writeFile;
|
|
|
|
QVERIFY(writeFile.open(sendRequestedSpy.first().last().value<qint32>(), QFile::WriteOnly, QFileDevice::AutoCloseHandle));
|
|
|
|
writeFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestDataSource::testCancel()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, &KWin::DataDeviceManagerInterface::dataSourceCreated);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::DataSource> dataSource(m_dataDeviceManager->createDataSource());
|
2014-11-04 14:10:22 +00:00
|
|
|
QVERIFY(dataSource->isValid());
|
2022-08-01 21:29:02 +00:00
|
|
|
QSignalSpy cancelledSpy(dataSource.get(), &KWayland::Client::DataSource::cancelled);
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
QVERIFY(dataSourceCreatedSpy.wait());
|
|
|
|
|
|
|
|
QCOMPARE(cancelledSpy.count(), 0);
|
2021-08-29 05:11:06 +00:00
|
|
|
dataSourceCreatedSpy.first().first().value<DataSourceInterface *>()->cancel();
|
2014-11-04 14:10:22 +00:00
|
|
|
|
|
|
|
QVERIFY(cancelledSpy.wait());
|
|
|
|
QCOMPARE(cancelledSpy.count(), 1);
|
|
|
|
}
|
|
|
|
|
2014-11-05 14:22:15 +00:00
|
|
|
void TestDataSource::testServerGet()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-05 14:22:15 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
QSignalSpy dataSourceCreatedSpy(m_dataDeviceManagerInterface, &KWin::DataDeviceManagerInterface::dataSourceCreated);
|
2014-11-05 14:22:15 +00:00
|
|
|
|
2022-11-08 21:15:17 +00:00
|
|
|
std::unique_ptr<KWayland::Client::DataSource> dataSource(m_dataDeviceManager->createDataSource());
|
2014-11-05 14:22:15 +00:00
|
|
|
QVERIFY(dataSource->isValid());
|
|
|
|
|
|
|
|
QVERIFY(!DataSourceInterface::get(nullptr));
|
|
|
|
QVERIFY(dataSourceCreatedSpy.wait());
|
2021-08-29 05:11:06 +00:00
|
|
|
auto d = dataSourceCreatedSpy.first().first().value<DataSourceInterface *>();
|
2014-11-05 14:22:15 +00:00
|
|
|
|
|
|
|
QCOMPARE(DataSourceInterface::get(d->resource()), d);
|
|
|
|
QVERIFY(!DataSourceInterface::get(nullptr));
|
|
|
|
}
|
|
|
|
|
2014-11-04 14:10:22 +00:00
|
|
|
QTEST_GUILESS_MAIN(TestDataSource)
|
|
|
|
#include "test_datasource.moc"
|