File indexing completed on 2025-01-19 04:56:58
0001 /* 0002 * SPDX-FileCopyrightText: 2014 Mario Bensi <mbensi@ipsquad.net> 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/compositejob.h" 0010 0011 #include "testlib/fakejob.h" 0012 0013 using namespace Utils; 0014 0015 namespace 0016 { 0017 template<typename T> 0018 QSet<T> listToSet(const QList<T> &list) 0019 { 0020 return {list.cbegin(), list.cend()}; 0021 } 0022 } 0023 0024 class CompositeJobTest : public QObject 0025 { 0026 Q_OBJECT 0027 public: 0028 explicit CompositeJobTest(QObject *parent = nullptr) 0029 : QObject(parent) 0030 , m_callCount(0) 0031 { 0032 } 0033 0034 private: 0035 int m_callCount; 0036 0037 private slots: 0038 void shouldCallHandlers() 0039 { 0040 // GIVEN 0041 int callCount = 0; 0042 0043 auto handler = [&]() { 0044 callCount++; 0045 }; 0046 0047 FakeJob *job1 = new FakeJob(this); 0048 FakeJob *job2 = new FakeJob(this); 0049 CompositeJob *compositeJob = new CompositeJob(this); 0050 compositeJob->setAutoDelete(false); 0051 QVERIFY(compositeJob->install(job1, handler)); 0052 QVERIFY(compositeJob->install(job2, handler)); 0053 0054 // WHEN 0055 compositeJob->start(); 0056 QTest::qWait(FakeJob::DURATION + 10); 0057 0058 // THEN 0059 QCOMPARE(callCount, 2); 0060 QVERIFY(!compositeJob->error()); 0061 delete compositeJob; 0062 } 0063 0064 void shouldCallHandlersWithJob() 0065 { 0066 // GIVEN 0067 int callCount = 0; 0068 QList<KJob*> seenJobs; 0069 0070 auto handlerWithJob = [&](KJob *job) { 0071 callCount++; 0072 seenJobs << job; 0073 }; 0074 0075 FakeJob *job1 = new FakeJob(this); 0076 FakeJob *job2 = new FakeJob(this); 0077 CompositeJob *compositeJob = new CompositeJob(this); 0078 compositeJob->setAutoDelete(false); 0079 QVERIFY(compositeJob->install(job1, handlerWithJob)); 0080 QVERIFY(compositeJob->install(job2, handlerWithJob)); 0081 0082 // WHEN 0083 compositeJob->start(); 0084 QTest::qWait(FakeJob::DURATION + 10); 0085 0086 // THEN 0087 QCOMPARE(callCount, 2); 0088 QCOMPARE(listToSet(seenJobs), QSet<KJob*>() << job1 << job2); 0089 QVERIFY(!compositeJob->error()); 0090 delete compositeJob; 0091 } 0092 0093 void handleJobResult(KJob*) 0094 { 0095 m_callCount++; 0096 } 0097 0098 void shouldCallJobInHandler() 0099 { 0100 // GIVEN 0101 CompositeJob *compositeJob = new CompositeJob(this); 0102 compositeJob->setAutoDelete(false); 0103 0104 auto handler = [&]() { 0105 FakeJob *job2 = new FakeJob(this); 0106 QObject::connect(job2, &KJob::result, this, &CompositeJobTest::handleJobResult); 0107 compositeJob->addSubjob(job2); 0108 job2->start(); 0109 }; 0110 0111 FakeJob *job1 = new FakeJob(this); 0112 QVERIFY(compositeJob->install(job1, handler)); 0113 0114 // WHEN 0115 compositeJob->start(); 0116 QTest::qWait(FakeJob::DURATION*2 + 10); 0117 0118 QCOMPARE(m_callCount, 1); 0119 QVERIFY(!compositeJob->error()); 0120 delete compositeJob; 0121 } 0122 0123 void shouldEmitErrorFromHandler() 0124 { 0125 // GIVEN 0126 CompositeJob *compositeJob = new CompositeJob(this); 0127 compositeJob->setAutoDelete(false); 0128 0129 auto handler = [&]() { 0130 compositeJob->emitError(QStringLiteral("Error reached")); 0131 }; 0132 0133 FakeJob *job = new FakeJob(this); 0134 QVERIFY(compositeJob->install(job, handler)); 0135 0136 // WHEN 0137 compositeJob->start(); 0138 QTest::qWait(FakeJob::DURATION*2 + 10); 0139 0140 QCOMPARE(compositeJob->error(), static_cast<int>(KJob::UserDefinedError)); 0141 QCOMPARE(compositeJob->errorText(), QStringLiteral("Error reached")); 0142 delete compositeJob; 0143 } 0144 0145 void shouldNotEmitResultTwiceOnSecondSubJobError() 0146 { 0147 // GIVEN 0148 int callCount = 0; 0149 0150 auto handler = [&]() { 0151 callCount++; 0152 }; 0153 0154 FakeJob *job1 = new FakeJob(this); 0155 FakeJob *job2 = new FakeJob(this); 0156 job2->setExpectedError(KJob::UserDefinedError, "Fake error"); 0157 0158 CompositeJob *compositeJob = new CompositeJob(this); 0159 compositeJob->setAutoDelete(false); 0160 compositeJob->install(job1, handler); 0161 compositeJob->install(job2, handler); 0162 0163 QSignalSpy spy(compositeJob, &KJob::result); 0164 0165 // WHEN 0166 compositeJob->start(); 0167 QTest::qWait(FakeJob::DURATION*2 + 10); 0168 0169 // THEN 0170 QCOMPARE(spy.count(), 1); 0171 delete compositeJob; 0172 0173 } 0174 }; 0175 0176 ZANSHIN_TEST_MAIN(CompositeJobTest) 0177 0178 #include "compositejobtest.moc"