File indexing completed on 2025-01-05 05:00:01

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Kevin Ottens <ervin@kde.org>
0003  SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 
0007 #include <testlib/qtest_zanshin.h>
0008 
0009 #include "utils/jobhandler.h"
0010 #include "utils/compositejob.h"
0011 
0012 #include "testlib/fakejob.h"
0013 
0014 using namespace Utils;
0015 
0016 namespace
0017 {
0018     template<typename T>
0019     QSet<T> listToSet(const QList<T> &list)
0020     {
0021         return {list.cbegin(), list.cend()};
0022     }
0023 }
0024 
0025 class JobHandlerTest : public QObject
0026 {
0027     Q_OBJECT
0028 private slots:
0029     void shouldCallHandlers()
0030     {
0031         int callCount = 0;
0032         QList<KJob*> seenJobs;
0033 
0034         auto handler = [&]() {
0035             callCount++;
0036         };
0037 
0038         auto handlerWithJob = [&](KJob *job) {
0039             callCount++;
0040             seenJobs << job;
0041         };
0042 
0043         FakeJob *job1 = new FakeJob(this);
0044         JobHandler::install(job1, handler);
0045         JobHandler::install(job1, handlerWithJob);
0046         QCOMPARE(JobHandler::jobCount(), 2);
0047         job1->start();
0048 
0049         FakeJob *job2 = new FakeJob(this);
0050         JobHandler::install(job2, handler);
0051         JobHandler::install(job2, handlerWithJob);
0052         QCOMPARE(JobHandler::jobCount(), 4);
0053         job2->start();
0054 
0055         QTest::qWait(FakeJob::DURATION + 10);
0056 
0057         QCOMPARE(callCount, 4);
0058         QCOMPARE(listToSet(seenJobs), QSet<KJob*>() << job1 << job2);
0059         QCOMPARE(JobHandler::jobCount(), 0);
0060     }
0061 };
0062 
0063 ZANSHIN_TEST_MAIN(JobHandlerTest)
0064 
0065 #include "jobhandlertest.moc"