File indexing completed on 2024-04-21 15:05:28

0001 /*
0002     SPDX-FileCopyrightText: 2014 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include "kwindowinfo.h"
0008 #include "kwindowsystem.h"
0009 #include "kx11extras.h"
0010 #include "nettesthelper.h"
0011 #include "netwm.h"
0012 
0013 #include <QRunnable>
0014 #include <QSignalSpy>
0015 #include <QTest>
0016 #include <QThread>
0017 #include <QThreadPool>
0018 
0019 class KWindowSystemThreadTest : public QObject
0020 {
0021     Q_OBJECT
0022 private Q_SLOTS:
0023     void initTestCase();
0024 
0025     void testWindowAdded();
0026     void testAccessFromThread();
0027 
0028 private:
0029     QWidget *m_widget;
0030 };
0031 
0032 class KWindowSystemCreator : public QRunnable
0033 {
0034 public:
0035     void run() override
0036     {
0037         (void)KWindowSystem::self();
0038     }
0039 };
0040 
0041 class WindowInfoLister : public QThread
0042 {
0043 public:
0044     void run() override
0045     {
0046         // simulate some activity in another thread gathering window information
0047         const QList<WId> windows = KX11Extras::stackingOrder();
0048         for (auto wid : windows) {
0049             KWindowInfo info(wid, NET::WMVisibleName);
0050             if (info.valid()) {
0051                 m_names << info.visibleName();
0052             }
0053         }
0054     }
0055 
0056     QStringList m_names;
0057 };
0058 
0059 void KWindowSystemThreadTest::initTestCase()
0060 {
0061     m_widget = nullptr;
0062     QRunnable *creator = new KWindowSystemCreator;
0063     creator->setAutoDelete(true);
0064     QThreadPool::globalInstance()->start(creator);
0065     QVERIFY(QThreadPool::globalInstance()->waitForDone(5000));
0066 }
0067 
0068 void KWindowSystemThreadTest::testWindowAdded()
0069 {
0070     qRegisterMetaType<WId>("WId");
0071     QSignalSpy spy(KX11Extras::self(), &KX11Extras::windowAdded);
0072     m_widget = new QWidget;
0073     m_widget->show();
0074     QVERIFY(QTest::qWaitForWindowExposed(m_widget));
0075     QVERIFY(spy.count() > 0);
0076     bool hasWId = false;
0077     for (auto it = spy.constBegin(); it != spy.constEnd(); ++it) {
0078         if ((*it).isEmpty()) {
0079             continue;
0080         }
0081         QCOMPARE((*it).count(), 1);
0082         hasWId = (*it).at(0).toULongLong() == m_widget->winId();
0083         if (hasWId) {
0084             break;
0085         }
0086     }
0087     QVERIFY(hasWId);
0088     QVERIFY(KX11Extras::hasWId(m_widget->winId()));
0089 }
0090 
0091 void KWindowSystemThreadTest::testAccessFromThread()
0092 {
0093     WindowInfoLister listerThread;
0094     listerThread.start();
0095     QVERIFY(listerThread.wait(5000));
0096     QVERIFY(!listerThread.m_names.isEmpty());
0097 }
0098 
0099 QTEST_MAIN(KWindowSystemThreadTest)
0100 
0101 #include <kwindowsystem_threadtest.moc>