Commit 6394b6cd authored by Martin Flöser's avatar Martin Flöser
Browse files

Add unit test for ShmPool

The unit test found a few problems which are now addressed
* getBuffer did not check the format when reusing a buffer
* creatBuffer used the wrong method on QSize to check whether it is empty
* destroy didn't call destroy on the Buffer. This is now added by moving
  the Buffer::Private in a dedicated header which can also be included
  from the ShmPool
parent d8e4f5ed
......@@ -78,3 +78,14 @@ add_executable(testWaylandSeat ${testWaylandSeat_SRCS})
target_link_libraries( testWaylandSeat Qt5::Test Qt5::Gui KF5::WaylandClient KF5::WaylandServer Wayland::Client Wayland::Server)
add_test(kwayland-testWaylandSeat testWaylandSeat)
ecm_mark_as_test(testWaylandSeat)
########################################################
# Test ShmPool
########################################################
set( testShmPool_SRCS
test_shm_pool.cpp
)
add_executable(testShmPool ${testShmPool_SRCS})
target_link_libraries( testShmPool Qt5::Test Qt5::Gui KF5::WaylandClient KF5::WaylandServer)
add_test(kwayland-testShmPool testShmPool)
ecm_mark_as_test(testShmPool)
/********************************************************************
Copyright 2014 Martin Gräßlin <mgraesslin@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 <QtTest/QtTest>
#include <QImage>
// KWin
#include "../../src/client/compositor.h"
#include "../../src/client/connection_thread.h"
#include "../../src/client/surface.h"
#include "../../src/client/registry.h"
#include "../../src/client/shm_pool.h"
#include "../../src/server/buffer_interface.h"
#include "../../src/server/compositor_interface.h"
#include "../../src/server/display.h"
#include "../../src/server/surface_interface.h"
// Wayland
#include <wayland-client-protocol.h>
class TestShmPool : public QObject
{
Q_OBJECT
public:
explicit TestShmPool(QObject *parent = nullptr);
private Q_SLOTS:
void init();
void cleanup();
void testCreateBufferNullImage();
void testCreateBufferNullSize();
void testCreateBufferInvalidSize();
void testCreateBufferFromImage();
void testCreateBufferFromData();
void testReuseBuffer();
void testDestroy();
private:
KWayland::Server::Display *m_display;
KWayland::Server::CompositorInterface *m_compositorInterface;
KWayland::Client::ConnectionThread *m_connection;
KWayland::Client::Compositor *m_compositor;
KWayland::Client::ShmPool *m_shmPool;
QThread *m_thread;
};
static const QString s_socketName = QStringLiteral("kwin-test-wayland-surface-0");
TestShmPool::TestShmPool(QObject *parent)
: QObject(parent)
, m_display(nullptr)
, m_compositorInterface(nullptr)
, m_connection(nullptr)
, m_compositor(nullptr)
, m_shmPool(nullptr)
, m_thread(nullptr)
{
}
void TestShmPool::init()
{
using namespace KWayland::Server;
delete m_display;
m_display = new Display(this);
m_display->setSocketName(s_socketName);
m_display->start();
QVERIFY(m_display->isRunning());
// setup connection
m_connection = new KWayland::Client::ConnectionThread;
QSignalSpy connectedSpy(m_connection, SIGNAL(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());
KWayland::Client::Registry registry;
QSignalSpy shmSpy(&registry, SIGNAL(shmAnnounced(quint32,quint32)));
registry.create(m_connection->display());
QVERIFY(registry.isValid());
registry.setup();
// here we need a shm pool
m_display->createShm();
QVERIFY(shmSpy.wait());
m_shmPool = registry.createShmPool(shmSpy.first().first().value<quint32>(), shmSpy.first().last().value<quint32>(), this);
}
void TestShmPool::cleanup()
{
if (m_compositor) {
delete m_compositor;
m_compositor = nullptr;
}
if (m_shmPool) {
delete m_shmPool;
m_shmPool = 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 TestShmPool::testCreateBufferNullImage()
{
QVERIFY(m_shmPool->isValid());
QImage img;
QVERIFY(img.isNull());
QVERIFY(!m_shmPool->createBuffer(img));
}
void TestShmPool::testCreateBufferNullSize()
{
QVERIFY(m_shmPool->isValid());
QSize size(0, 0);
QVERIFY(size.isNull());
QVERIFY(!m_shmPool->createBuffer(size, 0, nullptr));
}
void TestShmPool::testCreateBufferInvalidSize()
{
QVERIFY(m_shmPool->isValid());
QSize size;
QVERIFY(!size.isValid());
QVERIFY(!m_shmPool->createBuffer(size, 0, nullptr));
}
void TestShmPool::testCreateBufferFromImage()
{
QVERIFY(m_shmPool->isValid());
QImage img(24, 24, QImage::Format_ARGB32);
img.fill(Qt::black);
QVERIFY(!img.isNull());
auto buffer = m_shmPool->createBuffer(img);
QVERIFY(buffer);
QCOMPARE(buffer->size(), img.size());
QImage img2(buffer->address(), img.width(), img.height(), QImage::Format_ARGB32);
QCOMPARE(img2, img);
}
void TestShmPool::testCreateBufferFromData()
{
QVERIFY(m_shmPool->isValid());
QImage img(24, 24, QImage::Format_ARGB32);
img.fill(Qt::black);
QVERIFY(!img.isNull());
auto buffer = m_shmPool->createBuffer(img.size(), img.bytesPerLine(), img.constBits());
QVERIFY(buffer);
QCOMPARE(buffer->size(), img.size());
QImage img2(buffer->address(), img.width(), img.height(), QImage::Format_ARGB32);
QCOMPARE(img2, img);
}
void TestShmPool::testReuseBuffer()
{
QVERIFY(m_shmPool->isValid());
QImage img(24, 24, QImage::Format_ARGB32);
img.fill(Qt::black);
QVERIFY(!img.isNull());
auto buffer = m_shmPool->createBuffer(img);
QVERIFY(buffer);
buffer->setReleased(true);
buffer->setUsed(false);
// same image should get the same buffer
auto buffer2 = m_shmPool->createBuffer(img);
QCOMPARE(buffer, buffer2);
buffer2->setReleased(true);
buffer2->setUsed(false);
// image with different size should get us a new buffer
auto buffer3 = m_shmPool->getBuffer(QSize(10, 10), 8);
QVERIFY(buffer3 != buffer2);
// image with a different format should get us a new buffer
QImage img2(24, 24, QImage::Format_RGB32);
img2.fill(Qt::black);
QVERIFY(!img2.isNull());
auto buffer4 = m_shmPool->createBuffer(img2);
QVERIFY(buffer4);
QVERIFY(buffer4 != buffer2);
QVERIFY(buffer4 != buffer3);
}
void TestShmPool::testDestroy()
{
using namespace KWayland::Client;
connect(m_connection, &ConnectionThread::connectionDied, m_shmPool, &ShmPool::destroy);
QVERIFY(m_shmPool->isValid());
// let's create one Buffer
m_shmPool->getBuffer(QSize(10, 10), 8);
QSignalSpy connectionDiedSpy(m_connection, SIGNAL(connectionDied()));
QVERIFY(connectionDiedSpy.isValid());
delete m_display;
m_display = nullptr;
QVERIFY(connectionDiedSpy.wait());
// now the pool should be destroyed;
QVERIFY(!m_shmPool->isValid());
// calling destroy again should not fail
m_shmPool->destroy();
}
QTEST_MAIN(TestShmPool)
#include "test_shm_pool.moc"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment