1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/*
SPDX-FileCopyrightText: 2014 Aaron Seigo <aseigo@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "kwindowinfo.h"
#include "kwindowsystem.h"
#include "kx11extras.h"
#include "nettesthelper.h"
#include "netwm.h"
#include <QRunnable>
#include <QSignalSpy>
#include <QTest>
#include <QThread>
#include <QThreadPool>
class KWindowSystemThreadTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void testWindowAdded();
void testAccessFromThread();
private:
QWidget *m_widget;
};
class KWindowSystemCreator : public QRunnable
{
public:
void run() override
{
(void)KWindowSystem::self();
}
};
class WindowInfoLister : public QThread
{
public:
void run() override
{
// simulate some activity in another thread gathering window information
const QList<WId> windows = KX11Extras::stackingOrder();
for (auto wid : windows) {
KWindowInfo info(wid, NET::WMVisibleName);
if (info.valid()) {
m_names << info.visibleName();
}
}
}
QStringList m_names;
};
void KWindowSystemThreadTest::initTestCase()
{
m_widget = nullptr;
QRunnable *creator = new KWindowSystemCreator;
creator->setAutoDelete(true);
QThreadPool::globalInstance()->start(creator);
QVERIFY(QThreadPool::globalInstance()->waitForDone(5000));
}
void KWindowSystemThreadTest::testWindowAdded()
{
qRegisterMetaType<WId>("WId");
QSignalSpy spy(KX11Extras::self(), &KX11Extras::windowAdded);
m_widget = new QWidget;
m_widget->show();
QVERIFY(QTest::qWaitForWindowExposed(m_widget));
QVERIFY(spy.count() > 0);
bool hasWId = false;
for (auto it = spy.constBegin(); it != spy.constEnd(); ++it) {
if ((*it).isEmpty()) {
continue;
}
QCOMPARE((*it).count(), 1);
hasWId = (*it).at(0).toULongLong() == m_widget->winId();
if (hasWId) {
break;
}
}
QVERIFY(hasWId);
QVERIFY(KX11Extras::hasWId(m_widget->winId()));
}
void KWindowSystemThreadTest::testAccessFromThread()
{
WindowInfoLister listerThread;
listerThread.start();
QVERIFY(listerThread.wait(5000));
QVERIFY(!listerThread.m_names.isEmpty());
}
QTEST_MAIN(KWindowSystemThreadTest)
#include <kwindowsystem_threadtest.moc>
|