File indexing completed on 2024-03-24 03:56:24
0001 /* 0002 This file is part of the KDE project 0003 SPDX-FileCopyrightText: 2013 Kevin Funk <kevin@kfunk.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only 0006 */ 0007 0008 #include "kcompositejobtest.h" 0009 0010 #include <QSignalSpy> 0011 #include <QTest> 0012 #include <QTimer> 0013 0014 TestJob::TestJob(QObject *parent) 0015 : KJob(parent) 0016 { 0017 } 0018 0019 void TestJob::start() 0020 { 0021 QTimer::singleShot(1000, this, &TestJob::doEmit); 0022 } 0023 0024 void TestJob::doEmit() 0025 { 0026 emitResult(); 0027 } 0028 0029 void CompositeJob::start() 0030 { 0031 if (hasSubjobs()) { 0032 subjobs().first()->start(); 0033 } else { 0034 emitResult(); 0035 } 0036 } 0037 0038 bool CompositeJob::addSubjob(KJob *job) 0039 { 0040 return KCompositeJob::addSubjob(job); 0041 } 0042 0043 void CompositeJob::slotResult(KJob *job) 0044 { 0045 KCompositeJob::slotResult(job); 0046 0047 if (!error() && hasSubjobs()) { 0048 // start next 0049 subjobs().first()->start(); 0050 } else { 0051 setError(job->error()); 0052 setErrorText(job->errorText()); 0053 emitResult(); 0054 } 0055 } 0056 0057 KCompositeJobTest::KCompositeJobTest() 0058 : loop(this) 0059 { 0060 } 0061 0062 /** 0063 * In case a composite job is deleted during execution 0064 * we still want to assure that we don't crash 0065 * 0066 * see bug: https://bugs.kde.org/show_bug.cgi?id=230692 0067 */ 0068 void KCompositeJobTest::testDeletionDuringExecution() 0069 { 0070 QObject *someParent = new QObject; 0071 KJob *job = new TestJob(someParent); 0072 0073 CompositeJob *compositeJob = new CompositeJob; 0074 compositeJob->setAutoDelete(false); 0075 QVERIFY(compositeJob->addSubjob(job)); 0076 0077 QCOMPARE(job->parent(), compositeJob); 0078 0079 QSignalSpy destroyed_spy(job, &QObject::destroyed); 0080 // check if job got reparented properly 0081 delete someParent; 0082 someParent = nullptr; 0083 // the job should still exist, because it is a child of KCompositeJob now 0084 QCOMPARE(destroyed_spy.size(), 0); 0085 0086 // start async, the subjob takes 1 second to finish 0087 compositeJob->start(); 0088 0089 // delete the job during the execution 0090 delete compositeJob; 0091 compositeJob = nullptr; 0092 // at this point, the subjob should be deleted, too 0093 QCOMPARE(destroyed_spy.size(), 1); 0094 } 0095 0096 QTEST_GUILESS_MAIN(KCompositeJobTest) 0097 0098 #include "moc_kcompositejobtest.cpp"