File indexing completed on 2024-06-23 10:37:21

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2017 Friedrich W. H. Kossebau <kossebau@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include <KFile>
0009 #include <KIO/JobTracker>
0010 #include <KJob>
0011 #include <KJobTrackerInterface>
0012 
0013 #include <QEventLoop>
0014 #include <QTest>
0015 #include <QTimer>
0016 
0017 // widget is shown with hardcoded delay of 500 ms by KWidgetJobTracker
0018 static const int testJobRunningTime = 600;
0019 
0020 class TestJob : public KJob
0021 {
0022     Q_OBJECT
0023 public:
0024     void start() override
0025     {
0026         QTimer::singleShot(testJobRunningTime, this, &TestJob::doEmit);
0027     }
0028 
0029 private Q_SLOTS:
0030     void doEmit()
0031     {
0032         emitResult();
0033     }
0034 };
0035 
0036 class KDynamicJobTrackerTest : public QObject
0037 {
0038     Q_OBJECT
0039 
0040 private Q_SLOTS:
0041     void testNoCrashWithoutQWidgetsPossible();
0042 };
0043 
0044 void KDynamicJobTrackerTest::testNoCrashWithoutQWidgetsPossible()
0045 {
0046     // dummy call: need to use some symbol from KIOWidgets so linkers do not drop linking to it
0047     KFile::isDefaultView(KFile::Default);
0048 
0049     // simply linking to KIOWidgets results in KDynamicJobTracker installing itself as KIO's jobtracker
0050     KJobTrackerInterface *jobtracker = KIO::getJobTracker();
0051     QCOMPARE(jobtracker->metaObject()->className(), "KDynamicJobTracker");
0052 
0053     TestJob *job = new TestJob;
0054 
0055     jobtracker->registerJob(job);
0056 
0057     job->start();
0058     QEventLoop loop;
0059     connect(job, &KJob::result, &loop, &QEventLoop::quit);
0060     loop.exec();
0061     // if we are here, no crash has happened due to QWidgets tried to be used -> success
0062 }
0063 
0064 // GUILESS, so QWidgets are not possible
0065 QTEST_GUILESS_MAIN(KDynamicJobTrackerTest)
0066 
0067 #include "kdynamicjobtrackernowidgetstest.moc"